4 * Client-side procedure declarations for NFSv4.
6 * Copyright (c) 2002 The Regents of the University of Michigan.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <linux/delay.h>
40 #include <linux/errno.h>
41 #include <linux/string.h>
42 #include <linux/ratelimit.h>
43 #include <linux/printk.h>
44 #include <linux/slab.h>
45 #include <linux/sunrpc/clnt.h>
46 #include <linux/nfs.h>
47 #include <linux/nfs4.h>
48 #include <linux/nfs_fs.h>
49 #include <linux/nfs_page.h>
50 #include <linux/nfs_mount.h>
51 #include <linux/namei.h>
52 #include <linux/mount.h>
53 #include <linux/module.h>
54 #include <linux/xattr.h>
55 #include <linux/utsname.h>
56 #include <linux/freezer.h>
57 #include <linux/iversion.h>
60 #include "delegation.h"
67 #include "nfs4idmap.h"
68 #include "nfs4session.h"
72 #include "nfs4trace.h"
74 #define NFSDBG_FACILITY NFSDBG_PROC
76 #define NFS4_BITMASK_SZ 3
78 #define NFS4_POLL_RETRY_MIN (HZ/10)
79 #define NFS4_POLL_RETRY_MAX (15*HZ)
81 /* file attributes which can be mapped to nfs attributes */
82 #define NFS4_VALID_ATTRS (ATTR_MODE \
93 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
94 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
95 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
96 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label, struct inode *inode);
97 static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
98 struct nfs_fattr *fattr, struct iattr *sattr,
99 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
100 struct nfs4_label *olabel);
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);
111 static void nfs4_bitmask_set(__u32 bitmask[NFS4_BITMASK_SZ],
112 const __u32 *src, struct inode *inode,
113 struct nfs_server *server,
114 struct nfs4_label *label);
116 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
117 static inline struct nfs4_label *
118 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
119 struct iattr *sattr, struct nfs4_label *label)
126 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
129 err = security_dentry_init_security(dentry, sattr->ia_mode,
130 &dentry->d_name, (void **)&label->label, &label->len);
137 nfs4_label_release_security(struct nfs4_label *label)
140 security_release_secctx(label->label, label->len);
142 static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
145 return server->attr_bitmask;
147 return server->attr_bitmask_nl;
150 static inline struct nfs4_label *
151 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
152 struct iattr *sattr, struct nfs4_label *l)
155 nfs4_label_release_security(struct nfs4_label *label)
158 nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
159 { return server->attr_bitmask; }
162 /* Prevent leaks of NFSv4 errors into userland */
163 static int nfs4_map_errors(int err)
168 case -NFS4ERR_RESOURCE:
169 case -NFS4ERR_LAYOUTTRYLATER:
170 case -NFS4ERR_RECALLCONFLICT:
172 case -NFS4ERR_WRONGSEC:
173 case -NFS4ERR_WRONG_CRED:
175 case -NFS4ERR_BADOWNER:
176 case -NFS4ERR_BADNAME:
178 case -NFS4ERR_SHARE_DENIED:
180 case -NFS4ERR_MINOR_VERS_MISMATCH:
181 return -EPROTONOSUPPORT;
182 case -NFS4ERR_FILE_OPEN:
184 case -NFS4ERR_NOT_SAME:
187 dprintk("%s could not handle NFSv4 error %d\n",
195 * This is our standard bitmap for GETATTR requests.
197 const u32 nfs4_fattr_bitmap[3] = {
199 | FATTR4_WORD0_CHANGE
202 | FATTR4_WORD0_FILEID,
204 | FATTR4_WORD1_NUMLINKS
206 | FATTR4_WORD1_OWNER_GROUP
207 | FATTR4_WORD1_RAWDEV
208 | FATTR4_WORD1_SPACE_USED
209 | FATTR4_WORD1_TIME_ACCESS
210 | FATTR4_WORD1_TIME_METADATA
211 | FATTR4_WORD1_TIME_MODIFY
212 | FATTR4_WORD1_MOUNTED_ON_FILEID,
213 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
214 FATTR4_WORD2_SECURITY_LABEL
218 static const u32 nfs4_pnfs_open_bitmap[3] = {
220 | FATTR4_WORD0_CHANGE
223 | FATTR4_WORD0_FILEID,
225 | FATTR4_WORD1_NUMLINKS
227 | FATTR4_WORD1_OWNER_GROUP
228 | FATTR4_WORD1_RAWDEV
229 | FATTR4_WORD1_SPACE_USED
230 | FATTR4_WORD1_TIME_ACCESS
231 | FATTR4_WORD1_TIME_METADATA
232 | FATTR4_WORD1_TIME_MODIFY,
233 FATTR4_WORD2_MDSTHRESHOLD
234 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
235 | FATTR4_WORD2_SECURITY_LABEL
239 static const u32 nfs4_open_noattr_bitmap[3] = {
241 | FATTR4_WORD0_FILEID,
244 const u32 nfs4_statfs_bitmap[3] = {
245 FATTR4_WORD0_FILES_AVAIL
246 | FATTR4_WORD0_FILES_FREE
247 | FATTR4_WORD0_FILES_TOTAL,
248 FATTR4_WORD1_SPACE_AVAIL
249 | FATTR4_WORD1_SPACE_FREE
250 | FATTR4_WORD1_SPACE_TOTAL
253 const u32 nfs4_pathconf_bitmap[3] = {
255 | FATTR4_WORD0_MAXNAME,
259 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
260 | FATTR4_WORD0_MAXREAD
261 | FATTR4_WORD0_MAXWRITE
262 | FATTR4_WORD0_LEASE_TIME,
263 FATTR4_WORD1_TIME_DELTA
264 | FATTR4_WORD1_FS_LAYOUT_TYPES,
265 FATTR4_WORD2_LAYOUT_BLKSIZE
266 | FATTR4_WORD2_CLONE_BLKSIZE
267 | FATTR4_WORD2_CHANGE_ATTR_TYPE
268 | FATTR4_WORD2_XATTR_SUPPORT
271 const u32 nfs4_fs_locations_bitmap[3] = {
275 | FATTR4_WORD0_FILEID
276 | FATTR4_WORD0_FS_LOCATIONS,
278 | FATTR4_WORD1_OWNER_GROUP
279 | FATTR4_WORD1_RAWDEV
280 | FATTR4_WORD1_SPACE_USED
281 | FATTR4_WORD1_TIME_ACCESS
282 | FATTR4_WORD1_TIME_METADATA
283 | FATTR4_WORD1_TIME_MODIFY
284 | FATTR4_WORD1_MOUNTED_ON_FILEID,
287 static void nfs4_bitmap_copy_adjust(__u32 *dst, const __u32 *src,
288 struct inode *inode, unsigned long flags)
290 unsigned long cache_validity;
292 memcpy(dst, src, NFS4_BITMASK_SZ*sizeof(*dst));
293 if (!inode || !nfs4_have_delegation(inode, FMODE_READ))
296 cache_validity = READ_ONCE(NFS_I(inode)->cache_validity) | flags;
298 /* Remove the attributes over which we have full control */
299 dst[1] &= ~FATTR4_WORD1_RAWDEV;
300 if (!(cache_validity & NFS_INO_INVALID_SIZE))
301 dst[0] &= ~FATTR4_WORD0_SIZE;
303 if (!(cache_validity & NFS_INO_INVALID_CHANGE))
304 dst[0] &= ~FATTR4_WORD0_CHANGE;
306 if (!(cache_validity & NFS_INO_INVALID_MODE))
307 dst[1] &= ~FATTR4_WORD1_MODE;
308 if (!(cache_validity & NFS_INO_INVALID_OTHER))
309 dst[1] &= ~(FATTR4_WORD1_OWNER | FATTR4_WORD1_OWNER_GROUP);
312 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
313 struct nfs4_readdir_arg *readdir)
315 unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
319 readdir->cookie = cookie;
320 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
325 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
330 * NFSv4 servers do not return entries for '.' and '..'
331 * Therefore, we fake these entries here. We let '.'
332 * have cookie 0 and '..' have cookie 1. Note that
333 * when talking to the server, we always send cookie 0
336 start = p = kmap_atomic(*readdir->pages);
339 *p++ = xdr_one; /* next */
340 *p++ = xdr_zero; /* cookie, first word */
341 *p++ = xdr_one; /* cookie, second word */
342 *p++ = xdr_one; /* entry len */
343 memcpy(p, ".\0\0\0", 4); /* entry */
345 *p++ = xdr_one; /* bitmap length */
346 *p++ = htonl(attrs); /* bitmap */
347 *p++ = htonl(12); /* attribute buffer length */
348 *p++ = htonl(NF4DIR);
349 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
352 *p++ = xdr_one; /* next */
353 *p++ = xdr_zero; /* cookie, first word */
354 *p++ = xdr_two; /* cookie, second word */
355 *p++ = xdr_two; /* entry len */
356 memcpy(p, "..\0\0", 4); /* entry */
358 *p++ = xdr_one; /* bitmap length */
359 *p++ = htonl(attrs); /* bitmap */
360 *p++ = htonl(12); /* attribute buffer length */
361 *p++ = htonl(NF4DIR);
362 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
364 readdir->pgbase = (char *)p - (char *)start;
365 readdir->count -= readdir->pgbase;
366 kunmap_atomic(start);
369 static void nfs4_test_and_free_stateid(struct nfs_server *server,
370 nfs4_stateid *stateid,
371 const struct cred *cred)
373 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
375 ops->test_and_free_expired(server, stateid, cred);
378 static void __nfs4_free_revoked_stateid(struct nfs_server *server,
379 nfs4_stateid *stateid,
380 const struct cred *cred)
382 stateid->type = NFS4_REVOKED_STATEID_TYPE;
383 nfs4_test_and_free_stateid(server, stateid, cred);
386 static void nfs4_free_revoked_stateid(struct nfs_server *server,
387 const nfs4_stateid *stateid,
388 const struct cred *cred)
392 nfs4_stateid_copy(&tmp, stateid);
393 __nfs4_free_revoked_stateid(server, &tmp, cred);
396 static long nfs4_update_delay(long *timeout)
400 return NFS4_POLL_RETRY_MAX;
402 *timeout = NFS4_POLL_RETRY_MIN;
403 if (*timeout > NFS4_POLL_RETRY_MAX)
404 *timeout = NFS4_POLL_RETRY_MAX;
410 static int nfs4_delay_killable(long *timeout)
414 freezable_schedule_timeout_killable_unsafe(
415 nfs4_update_delay(timeout));
416 if (!__fatal_signal_pending(current))
421 static int nfs4_delay_interruptible(long *timeout)
425 freezable_schedule_timeout_interruptible_unsafe(nfs4_update_delay(timeout));
426 if (!signal_pending(current))
428 return __fatal_signal_pending(current) ? -EINTR :-ERESTARTSYS;
431 static int nfs4_delay(long *timeout, bool interruptible)
434 return nfs4_delay_interruptible(timeout);
435 return nfs4_delay_killable(timeout);
438 static const nfs4_stateid *
439 nfs4_recoverable_stateid(const nfs4_stateid *stateid)
443 switch (stateid->type) {
444 case NFS4_OPEN_STATEID_TYPE:
445 case NFS4_LOCK_STATEID_TYPE:
446 case NFS4_DELEGATION_STATEID_TYPE:
454 /* This is the error handling routine for processes that are allowed
457 static int nfs4_do_handle_exception(struct nfs_server *server,
458 int errorcode, struct nfs4_exception *exception)
460 struct nfs_client *clp = server->nfs_client;
461 struct nfs4_state *state = exception->state;
462 const nfs4_stateid *stateid;
463 struct inode *inode = exception->inode;
466 exception->delay = 0;
467 exception->recovering = 0;
468 exception->retry = 0;
470 stateid = nfs4_recoverable_stateid(exception->stateid);
471 if (stateid == NULL && state != NULL)
472 stateid = nfs4_recoverable_stateid(&state->stateid);
477 case -NFS4ERR_BADHANDLE:
479 if (inode != NULL && S_ISREG(inode->i_mode))
480 pnfs_destroy_layout(NFS_I(inode));
482 case -NFS4ERR_DELEG_REVOKED:
483 case -NFS4ERR_ADMIN_REVOKED:
484 case -NFS4ERR_EXPIRED:
485 case -NFS4ERR_BAD_STATEID:
486 case -NFS4ERR_PARTNER_NO_AUTH:
487 if (inode != NULL && stateid != NULL) {
488 nfs_inode_find_state_and_recover(inode,
490 goto wait_on_recovery;
493 case -NFS4ERR_OPENMODE:
497 err = nfs_async_inode_return_delegation(inode,
500 goto wait_on_recovery;
501 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
502 exception->retry = 1;
508 ret = nfs4_schedule_stateid_recovery(server, state);
511 goto wait_on_recovery;
512 case -NFS4ERR_STALE_STATEID:
513 case -NFS4ERR_STALE_CLIENTID:
514 nfs4_schedule_lease_recovery(clp);
515 goto wait_on_recovery;
517 ret = nfs4_schedule_migration_recovery(server);
520 goto wait_on_recovery;
521 case -NFS4ERR_LEASE_MOVED:
522 nfs4_schedule_lease_moved_recovery(clp);
523 goto wait_on_recovery;
524 #if defined(CONFIG_NFS_V4_1)
525 case -NFS4ERR_BADSESSION:
526 case -NFS4ERR_BADSLOT:
527 case -NFS4ERR_BAD_HIGH_SLOT:
528 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
529 case -NFS4ERR_DEADSESSION:
530 case -NFS4ERR_SEQ_FALSE_RETRY:
531 case -NFS4ERR_SEQ_MISORDERED:
532 /* Handled in nfs41_sequence_process() */
533 goto wait_on_recovery;
534 #endif /* defined(CONFIG_NFS_V4_1) */
535 case -NFS4ERR_FILE_OPEN:
536 if (exception->timeout > HZ) {
537 /* We have retried a decent amount, time to
545 nfs_inc_server_stats(server, NFSIOS_DELAY);
548 case -NFS4ERR_LAYOUTTRYLATER:
549 case -NFS4ERR_RECALLCONFLICT:
550 exception->delay = 1;
553 case -NFS4ERR_RETRY_UNCACHED_REP:
554 case -NFS4ERR_OLD_STATEID:
555 exception->retry = 1;
557 case -NFS4ERR_BADOWNER:
558 /* The following works around a Linux server bug! */
559 case -NFS4ERR_BADNAME:
560 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
561 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
562 exception->retry = 1;
563 printk(KERN_WARNING "NFS: v4 server %s "
564 "does not accept raw "
566 "Reenabling the idmapper.\n",
567 server->nfs_client->cl_hostname);
570 /* We failed to handle the error */
571 return nfs4_map_errors(ret);
573 exception->recovering = 1;
577 /* This is the error handling routine for processes that are allowed
580 int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
582 struct nfs_client *clp = server->nfs_client;
585 ret = nfs4_do_handle_exception(server, errorcode, exception);
586 if (exception->delay) {
587 ret = nfs4_delay(&exception->timeout,
588 exception->interruptible);
591 if (exception->recovering) {
592 if (exception->task_is_privileged)
594 ret = nfs4_wait_clnt_recover(clp);
595 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
602 exception->retry = 1;
607 nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
608 int errorcode, struct nfs4_exception *exception)
610 struct nfs_client *clp = server->nfs_client;
613 ret = nfs4_do_handle_exception(server, errorcode, exception);
614 if (exception->delay) {
615 rpc_delay(task, nfs4_update_delay(&exception->timeout));
618 if (exception->recovering) {
619 if (exception->task_is_privileged)
621 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
622 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
623 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
626 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
631 exception->retry = 1;
633 * For NFS4ERR_MOVED, the client transport will need to
634 * be recomputed after migration recovery has completed.
636 if (errorcode == -NFS4ERR_MOVED)
637 rpc_task_release_transport(task);
643 nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
644 struct nfs4_state *state, long *timeout)
646 struct nfs4_exception exception = {
650 if (task->tk_status >= 0)
653 exception.timeout = *timeout;
654 task->tk_status = nfs4_async_handle_exception(task, server,
657 if (exception.delay && timeout)
658 *timeout = exception.timeout;
665 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
666 * or 'false' otherwise.
668 static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
670 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
671 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
674 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
676 spin_lock(&clp->cl_lock);
677 if (time_before(clp->cl_last_renewal,timestamp))
678 clp->cl_last_renewal = timestamp;
679 spin_unlock(&clp->cl_lock);
682 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
684 struct nfs_client *clp = server->nfs_client;
686 if (!nfs4_has_session(clp))
687 do_renew_lease(clp, timestamp);
690 struct nfs4_call_sync_data {
691 const struct nfs_server *seq_server;
692 struct nfs4_sequence_args *seq_args;
693 struct nfs4_sequence_res *seq_res;
696 void nfs4_init_sequence(struct nfs4_sequence_args *args,
697 struct nfs4_sequence_res *res, int cache_reply,
700 args->sa_slot = NULL;
701 args->sa_cache_this = cache_reply;
702 args->sa_privileged = privileged;
707 static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
709 struct nfs4_slot *slot = res->sr_slot;
710 struct nfs4_slot_table *tbl;
713 spin_lock(&tbl->slot_tbl_lock);
714 if (!nfs41_wake_and_assign_slot(tbl, slot))
715 nfs4_free_slot(tbl, slot);
716 spin_unlock(&tbl->slot_tbl_lock);
721 static int nfs40_sequence_done(struct rpc_task *task,
722 struct nfs4_sequence_res *res)
724 if (res->sr_slot != NULL)
725 nfs40_sequence_free_slot(res);
729 #if defined(CONFIG_NFS_V4_1)
731 static void nfs41_release_slot(struct nfs4_slot *slot)
733 struct nfs4_session *session;
734 struct nfs4_slot_table *tbl;
735 bool send_new_highest_used_slotid = false;
740 session = tbl->session;
742 /* Bump the slot sequence number */
747 spin_lock(&tbl->slot_tbl_lock);
748 /* Be nice to the server: try to ensure that the last transmitted
749 * value for highest_user_slotid <= target_highest_slotid
751 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
752 send_new_highest_used_slotid = true;
754 if (nfs41_wake_and_assign_slot(tbl, slot)) {
755 send_new_highest_used_slotid = false;
758 nfs4_free_slot(tbl, slot);
760 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
761 send_new_highest_used_slotid = false;
763 spin_unlock(&tbl->slot_tbl_lock);
764 if (send_new_highest_used_slotid)
765 nfs41_notify_server(session->clp);
766 if (waitqueue_active(&tbl->slot_waitq))
767 wake_up_all(&tbl->slot_waitq);
770 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
772 nfs41_release_slot(res->sr_slot);
776 static void nfs4_slot_sequence_record_sent(struct nfs4_slot *slot,
779 if ((s32)(seqnr - slot->seq_nr_highest_sent) > 0)
780 slot->seq_nr_highest_sent = seqnr;
782 static void nfs4_slot_sequence_acked(struct nfs4_slot *slot,
785 slot->seq_nr_highest_sent = seqnr;
786 slot->seq_nr_last_acked = seqnr;
789 static void nfs4_probe_sequence(struct nfs_client *client, const struct cred *cred,
790 struct nfs4_slot *slot)
792 struct rpc_task *task = _nfs41_proc_sequence(client, cred, slot, true);
794 rpc_put_task_async(task);
797 static int nfs41_sequence_process(struct rpc_task *task,
798 struct nfs4_sequence_res *res)
800 struct nfs4_session *session;
801 struct nfs4_slot *slot = res->sr_slot;
802 struct nfs_client *clp;
808 /* don't increment the sequence number if the task wasn't sent */
809 if (!RPC_WAS_SENT(task) || slot->seq_done)
812 session = slot->table->session;
815 trace_nfs4_sequence_done(session, res);
817 status = res->sr_status;
818 if (task->tk_status == -NFS4ERR_DEADSESSION)
819 status = -NFS4ERR_DEADSESSION;
821 /* Check the SEQUENCE operation status */
824 /* Mark this sequence number as having been acked */
825 nfs4_slot_sequence_acked(slot, slot->seq_nr);
826 /* Update the slot's sequence and clientid lease timer */
828 do_renew_lease(clp, res->sr_timestamp);
829 /* Check sequence flags */
830 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
832 nfs41_update_target_slotid(slot->table, slot, res);
836 * sr_status remains 1 if an RPC level error occurred.
837 * The server may or may not have processed the sequence
840 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
844 /* The server detected a resend of the RPC call and
845 * returned NFS4ERR_DELAY as per Section 2.10.6.2
848 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
852 nfs4_slot_sequence_acked(slot, slot->seq_nr);
854 case -NFS4ERR_RETRY_UNCACHED_REP:
855 case -NFS4ERR_SEQ_FALSE_RETRY:
857 * The server thinks we tried to replay a request.
858 * Retry the call after bumping the sequence ID.
860 nfs4_slot_sequence_acked(slot, slot->seq_nr);
862 case -NFS4ERR_BADSLOT:
864 * The slot id we used was probably retired. Try again
865 * using a different slot id.
867 if (slot->slot_nr < slot->table->target_highest_slotid)
868 goto session_recover;
870 case -NFS4ERR_SEQ_MISORDERED:
871 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
873 * Were one or more calls using this slot interrupted?
874 * If the server never received the request, then our
875 * transmitted slot sequence number may be too high. However,
876 * if the server did receive the request then it might
877 * accidentally give us a reply with a mismatched operation.
878 * We can sort this out by sending a lone sequence operation
879 * to the server on the same slot.
881 if ((s32)(slot->seq_nr - slot->seq_nr_last_acked) > 1) {
883 if (task->tk_msg.rpc_proc != &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE]) {
884 nfs4_probe_sequence(clp, task->tk_msg.rpc_cred, slot);
891 * A retry might be sent while the original request is
892 * still in progress on the replier. The replier SHOULD
893 * deal with the issue by returning NFS4ERR_DELAY as the
894 * reply to SEQUENCE or CB_SEQUENCE operation, but
895 * implementations MAY return NFS4ERR_SEQ_MISORDERED.
897 * Restart the search after a delay.
899 slot->seq_nr = slot->seq_nr_highest_sent;
901 case -NFS4ERR_BADSESSION:
902 case -NFS4ERR_DEADSESSION:
903 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
904 goto session_recover;
906 /* Just update the slot sequence no. */
910 /* The session may be reset by one of the error handlers. */
911 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
915 nfs4_schedule_session_recovery(session, status);
916 dprintk("%s ERROR: %d Reset session\n", __func__, status);
917 nfs41_sequence_free_slot(res);
922 if (rpc_restart_call_prepare(task)) {
923 nfs41_sequence_free_slot(res);
929 if (!rpc_restart_call(task))
931 rpc_delay(task, NFS4_POLL_RETRY_MAX);
935 int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
937 if (!nfs41_sequence_process(task, res))
939 if (res->sr_slot != NULL)
940 nfs41_sequence_free_slot(res);
944 EXPORT_SYMBOL_GPL(nfs41_sequence_done);
946 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
948 if (res->sr_slot == NULL)
950 if (res->sr_slot->table->session != NULL)
951 return nfs41_sequence_process(task, res);
952 return nfs40_sequence_done(task, res);
955 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
957 if (res->sr_slot != NULL) {
958 if (res->sr_slot->table->session != NULL)
959 nfs41_sequence_free_slot(res);
961 nfs40_sequence_free_slot(res);
965 int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
967 if (res->sr_slot == NULL)
969 if (!res->sr_slot->table->session)
970 return nfs40_sequence_done(task, res);
971 return nfs41_sequence_done(task, res);
973 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
975 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
977 struct nfs4_call_sync_data *data = calldata;
979 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
981 nfs4_setup_sequence(data->seq_server->nfs_client,
982 data->seq_args, data->seq_res, task);
985 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
987 struct nfs4_call_sync_data *data = calldata;
989 nfs41_sequence_done(task, data->seq_res);
992 static const struct rpc_call_ops nfs41_call_sync_ops = {
993 .rpc_call_prepare = nfs41_call_sync_prepare,
994 .rpc_call_done = nfs41_call_sync_done,
997 #else /* !CONFIG_NFS_V4_1 */
999 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
1001 return nfs40_sequence_done(task, res);
1004 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
1006 if (res->sr_slot != NULL)
1007 nfs40_sequence_free_slot(res);
1010 int nfs4_sequence_done(struct rpc_task *task,
1011 struct nfs4_sequence_res *res)
1013 return nfs40_sequence_done(task, res);
1015 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
1017 #endif /* !CONFIG_NFS_V4_1 */
1019 static void nfs41_sequence_res_init(struct nfs4_sequence_res *res)
1021 res->sr_timestamp = jiffies;
1022 res->sr_status_flags = 0;
1027 void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
1028 struct nfs4_sequence_res *res,
1029 struct nfs4_slot *slot)
1033 slot->privileged = args->sa_privileged ? 1 : 0;
1034 args->sa_slot = slot;
1036 res->sr_slot = slot;
1039 int nfs4_setup_sequence(struct nfs_client *client,
1040 struct nfs4_sequence_args *args,
1041 struct nfs4_sequence_res *res,
1042 struct rpc_task *task)
1044 struct nfs4_session *session = nfs4_get_session(client);
1045 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
1046 struct nfs4_slot *slot;
1048 /* slot already allocated? */
1049 if (res->sr_slot != NULL)
1053 tbl = &session->fc_slot_table;
1055 spin_lock(&tbl->slot_tbl_lock);
1056 /* The state manager will wait until the slot table is empty */
1057 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
1060 slot = nfs4_alloc_slot(tbl);
1062 if (slot == ERR_PTR(-ENOMEM))
1063 goto out_sleep_timeout;
1066 spin_unlock(&tbl->slot_tbl_lock);
1068 nfs4_sequence_attach_slot(args, res, slot);
1070 trace_nfs4_setup_sequence(session, args);
1072 nfs41_sequence_res_init(res);
1073 rpc_call_start(task);
1076 /* Try again in 1/4 second */
1077 if (args->sa_privileged)
1078 rpc_sleep_on_priority_timeout(&tbl->slot_tbl_waitq, task,
1079 jiffies + (HZ >> 2), RPC_PRIORITY_PRIVILEGED);
1081 rpc_sleep_on_timeout(&tbl->slot_tbl_waitq, task,
1082 NULL, jiffies + (HZ >> 2));
1083 spin_unlock(&tbl->slot_tbl_lock);
1086 if (args->sa_privileged)
1087 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
1088 RPC_PRIORITY_PRIVILEGED);
1090 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
1091 spin_unlock(&tbl->slot_tbl_lock);
1094 EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
1096 static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1098 struct nfs4_call_sync_data *data = calldata;
1099 nfs4_setup_sequence(data->seq_server->nfs_client,
1100 data->seq_args, data->seq_res, task);
1103 static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1105 struct nfs4_call_sync_data *data = calldata;
1106 nfs4_sequence_done(task, data->seq_res);
1109 static const struct rpc_call_ops nfs40_call_sync_ops = {
1110 .rpc_call_prepare = nfs40_call_sync_prepare,
1111 .rpc_call_done = nfs40_call_sync_done,
1114 static int nfs4_call_sync_custom(struct rpc_task_setup *task_setup)
1117 struct rpc_task *task;
1119 task = rpc_run_task(task_setup);
1121 return PTR_ERR(task);
1123 ret = task->tk_status;
1128 static int nfs4_do_call_sync(struct rpc_clnt *clnt,
1129 struct nfs_server *server,
1130 struct rpc_message *msg,
1131 struct nfs4_sequence_args *args,
1132 struct nfs4_sequence_res *res,
1133 unsigned short task_flags)
1135 struct nfs_client *clp = server->nfs_client;
1136 struct nfs4_call_sync_data data = {
1137 .seq_server = server,
1141 struct rpc_task_setup task_setup = {
1144 .callback_ops = clp->cl_mvops->call_sync_ops,
1145 .callback_data = &data,
1146 .flags = task_flags,
1149 return nfs4_call_sync_custom(&task_setup);
1152 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
1153 struct nfs_server *server,
1154 struct rpc_message *msg,
1155 struct nfs4_sequence_args *args,
1156 struct nfs4_sequence_res *res)
1158 return nfs4_do_call_sync(clnt, server, msg, args, res, 0);
1162 int nfs4_call_sync(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,
1169 nfs4_init_sequence(args, res, cache_reply, 0);
1170 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
1174 nfs4_inc_nlink_locked(struct inode *inode)
1176 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
1177 NFS_INO_INVALID_CTIME |
1178 NFS_INO_INVALID_NLINK);
1183 nfs4_inc_nlink(struct inode *inode)
1185 spin_lock(&inode->i_lock);
1186 nfs4_inc_nlink_locked(inode);
1187 spin_unlock(&inode->i_lock);
1191 nfs4_dec_nlink_locked(struct inode *inode)
1193 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
1194 NFS_INO_INVALID_CTIME |
1195 NFS_INO_INVALID_NLINK);
1200 nfs4_update_changeattr_locked(struct inode *inode,
1201 struct nfs4_change_info *cinfo,
1202 unsigned long timestamp, unsigned long cache_validity)
1204 struct nfs_inode *nfsi = NFS_I(inode);
1205 u64 change_attr = inode_peek_iversion_raw(inode);
1207 cache_validity |= NFS_INO_INVALID_CTIME | NFS_INO_INVALID_MTIME;
1209 switch (NFS_SERVER(inode)->change_attr_type) {
1210 case NFS4_CHANGE_TYPE_IS_UNDEFINED:
1212 case NFS4_CHANGE_TYPE_IS_TIME_METADATA:
1213 if ((s64)(change_attr - cinfo->after) > 0)
1217 if ((s64)(change_attr - cinfo->after) >= 0)
1221 if (cinfo->atomic && cinfo->before == change_attr) {
1222 nfsi->attrtimeo_timestamp = jiffies;
1224 if (S_ISDIR(inode->i_mode)) {
1225 cache_validity |= NFS_INO_INVALID_DATA;
1226 nfs_force_lookup_revalidate(inode);
1228 if (!NFS_PROTO(inode)->have_delegation(inode,
1230 cache_validity |= NFS_INO_REVAL_PAGECACHE;
1233 if (cinfo->before != change_attr)
1234 cache_validity |= NFS_INO_INVALID_ACCESS |
1235 NFS_INO_INVALID_ACL |
1236 NFS_INO_INVALID_XATTR;
1238 inode_set_iversion_raw(inode, cinfo->after);
1239 nfsi->read_cache_jiffies = timestamp;
1240 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
1241 nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE;
1243 nfs_set_cache_invalid(inode, cache_validity);
1247 nfs4_update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1248 unsigned long timestamp, unsigned long cache_validity)
1250 spin_lock(&dir->i_lock);
1251 nfs4_update_changeattr_locked(dir, cinfo, timestamp, cache_validity);
1252 spin_unlock(&dir->i_lock);
1255 struct nfs4_open_createattrs {
1256 struct nfs4_label *label;
1257 struct iattr *sattr;
1258 const __u32 verf[2];
1261 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1262 int err, struct nfs4_exception *exception)
1266 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1268 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1269 exception->retry = 1;
1273 static fmode_t _nfs4_ctx_to_accessmode(const struct nfs_open_context *ctx)
1275 return ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
1278 static fmode_t _nfs4_ctx_to_openmode(const struct nfs_open_context *ctx)
1280 fmode_t ret = ctx->mode & (FMODE_READ|FMODE_WRITE);
1282 return (ctx->mode & FMODE_EXEC) ? FMODE_READ | ret : ret;
1286 nfs4_map_atomic_open_share(struct nfs_server *server,
1287 fmode_t fmode, int openflags)
1291 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1293 res = NFS4_SHARE_ACCESS_READ;
1296 res = NFS4_SHARE_ACCESS_WRITE;
1298 case FMODE_READ|FMODE_WRITE:
1299 res = NFS4_SHARE_ACCESS_BOTH;
1301 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1303 /* Want no delegation if we're using O_DIRECT */
1304 if (openflags & O_DIRECT)
1305 res |= NFS4_SHARE_WANT_NO_DELEG;
1310 static enum open_claim_type4
1311 nfs4_map_atomic_open_claim(struct nfs_server *server,
1312 enum open_claim_type4 claim)
1314 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1319 case NFS4_OPEN_CLAIM_FH:
1320 return NFS4_OPEN_CLAIM_NULL;
1321 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1322 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1323 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1324 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1328 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
1330 p->o_res.f_attr = &p->f_attr;
1331 p->o_res.f_label = p->f_label;
1332 p->o_res.seqid = p->o_arg.seqid;
1333 p->c_res.seqid = p->c_arg.seqid;
1334 p->o_res.server = p->o_arg.server;
1335 p->o_res.access_request = p->o_arg.access;
1336 nfs_fattr_init(&p->f_attr);
1337 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
1340 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
1341 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
1342 const struct nfs4_open_createattrs *c,
1343 enum open_claim_type4 claim,
1346 struct dentry *parent = dget_parent(dentry);
1347 struct inode *dir = d_inode(parent);
1348 struct nfs_server *server = NFS_SERVER(dir);
1349 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
1350 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
1351 struct nfs4_opendata *p;
1353 p = kzalloc(sizeof(*p), gfp_mask);
1357 p->f_label = nfs4_label_alloc(server, gfp_mask);
1358 if (IS_ERR(p->f_label))
1361 p->a_label = nfs4_label_alloc(server, gfp_mask);
1362 if (IS_ERR(p->a_label))
1365 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1366 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
1367 if (IS_ERR(p->o_arg.seqid))
1368 goto err_free_label;
1369 nfs_sb_active(dentry->d_sb);
1370 p->dentry = dget(dentry);
1373 atomic_inc(&sp->so_count);
1374 p->o_arg.open_flags = flags;
1375 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
1376 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1377 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1379 if (flags & O_CREAT) {
1380 p->o_arg.umask = current_umask();
1381 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1382 if (c->sattr != NULL && c->sattr->ia_valid != 0) {
1383 p->o_arg.u.attrs = &p->attrs;
1384 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
1386 memcpy(p->o_arg.u.verifier.data, c->verf,
1387 sizeof(p->o_arg.u.verifier.data));
1390 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1391 * will return permission denied for all bits until close */
1392 if (!(flags & O_EXCL)) {
1393 /* ask server to check for all possible rights as results
1395 switch (p->o_arg.claim) {
1398 case NFS4_OPEN_CLAIM_NULL:
1399 case NFS4_OPEN_CLAIM_FH:
1400 p->o_arg.access = NFS4_ACCESS_READ |
1401 NFS4_ACCESS_MODIFY |
1402 NFS4_ACCESS_EXTEND |
1403 NFS4_ACCESS_EXECUTE;
1404 #ifdef CONFIG_NFS_V4_2
1405 if (server->caps & NFS_CAP_XATTR)
1406 p->o_arg.access |= NFS4_ACCESS_XAREAD |
1407 NFS4_ACCESS_XAWRITE |
1412 p->o_arg.clientid = server->nfs_client->cl_clientid;
1413 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1414 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1415 p->o_arg.name = &dentry->d_name;
1416 p->o_arg.server = server;
1417 p->o_arg.bitmask = nfs4_bitmask(server, label);
1418 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1419 switch (p->o_arg.claim) {
1420 case NFS4_OPEN_CLAIM_NULL:
1421 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1422 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1423 p->o_arg.fh = NFS_FH(dir);
1425 case NFS4_OPEN_CLAIM_PREVIOUS:
1426 case NFS4_OPEN_CLAIM_FH:
1427 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1428 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1429 p->o_arg.fh = NFS_FH(d_inode(dentry));
1431 p->c_arg.fh = &p->o_res.fh;
1432 p->c_arg.stateid = &p->o_res.stateid;
1433 p->c_arg.seqid = p->o_arg.seqid;
1434 nfs4_init_opendata_res(p);
1435 kref_init(&p->kref);
1439 nfs4_label_free(p->a_label);
1441 nfs4_label_free(p->f_label);
1449 static void nfs4_opendata_free(struct kref *kref)
1451 struct nfs4_opendata *p = container_of(kref,
1452 struct nfs4_opendata, kref);
1453 struct super_block *sb = p->dentry->d_sb;
1455 nfs4_lgopen_release(p->lgp);
1456 nfs_free_seqid(p->o_arg.seqid);
1457 nfs4_sequence_free_slot(&p->o_res.seq_res);
1458 if (p->state != NULL)
1459 nfs4_put_open_state(p->state);
1460 nfs4_put_state_owner(p->owner);
1462 nfs4_label_free(p->a_label);
1463 nfs4_label_free(p->f_label);
1467 nfs_sb_deactive(sb);
1468 nfs_fattr_free_names(&p->f_attr);
1469 kfree(p->f_attr.mdsthreshold);
1473 static void nfs4_opendata_put(struct nfs4_opendata *p)
1476 kref_put(&p->kref, nfs4_opendata_free);
1479 static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1482 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1483 case FMODE_READ|FMODE_WRITE:
1484 return state->n_rdwr != 0;
1486 return state->n_wronly != 0;
1488 return state->n_rdonly != 0;
1494 static int can_open_cached(struct nfs4_state *state, fmode_t mode,
1495 int open_mode, enum open_claim_type4 claim)
1499 if (open_mode & (O_EXCL|O_TRUNC))
1502 case NFS4_OPEN_CLAIM_NULL:
1503 case NFS4_OPEN_CLAIM_FH:
1508 switch (mode & (FMODE_READ|FMODE_WRITE)) {
1510 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1511 && state->n_rdonly != 0;
1514 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1515 && state->n_wronly != 0;
1517 case FMODE_READ|FMODE_WRITE:
1518 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1519 && state->n_rdwr != 0;
1525 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1526 enum open_claim_type4 claim)
1528 if (delegation == NULL)
1530 if ((delegation->type & fmode) != fmode)
1533 case NFS4_OPEN_CLAIM_NULL:
1534 case NFS4_OPEN_CLAIM_FH:
1536 case NFS4_OPEN_CLAIM_PREVIOUS:
1537 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1543 nfs_mark_delegation_referenced(delegation);
1547 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1556 case FMODE_READ|FMODE_WRITE:
1559 nfs4_state_set_mode_locked(state, state->state | fmode);
1562 #ifdef CONFIG_NFS_V4_1
1563 static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1565 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1567 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1569 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1573 #endif /* CONFIG_NFS_V4_1 */
1575 static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1577 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1578 wake_up_all(&state->waitq);
1581 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
1583 struct nfs_client *clp = state->owner->so_server->nfs_client;
1584 bool need_recover = false;
1586 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1587 need_recover = true;
1588 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1589 need_recover = true;
1590 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1591 need_recover = true;
1593 nfs4_state_mark_reclaim_nograce(clp, state);
1597 * Check for whether or not the caller may update the open stateid
1598 * to the value passed in by stateid.
1600 * Note: This function relies heavily on the server implementing
1601 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1603 * i.e. The stateid seqids have to be initialised to 1, and
1604 * are then incremented on every state transition.
1606 static bool nfs_stateid_is_sequential(struct nfs4_state *state,
1607 const nfs4_stateid *stateid)
1609 if (test_bit(NFS_OPEN_STATE, &state->flags)) {
1610 /* The common case - we're updating to a new sequence number */
1611 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1612 nfs4_stateid_is_next(&state->open_stateid, stateid)) {
1616 /* This is the first OPEN in this generation */
1617 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 /* cached opens have already been processed */
1988 ret = nfs_refresh_inode(inode, &data->f_attr);
1990 return ERR_PTR(ret);
1992 if (data->o_res.delegation_type != 0)
1993 nfs4_opendata_check_deleg(data, state);
1995 if (!update_open_stateid(state, &data->o_res.stateid,
1996 NULL, data->o_arg.fmode))
1997 return ERR_PTR(-EAGAIN);
1998 refcount_inc(&state->count);
2003 static struct inode *
2004 nfs4_opendata_get_inode(struct nfs4_opendata *data)
2006 struct inode *inode;
2008 switch (data->o_arg.claim) {
2009 case NFS4_OPEN_CLAIM_NULL:
2010 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
2011 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
2012 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
2013 return ERR_PTR(-EAGAIN);
2014 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
2015 &data->f_attr, data->f_label);
2018 inode = d_inode(data->dentry);
2020 nfs_refresh_inode(inode, &data->f_attr);
2025 static struct nfs4_state *
2026 nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
2028 struct nfs4_state *state;
2029 struct inode *inode;
2031 inode = nfs4_opendata_get_inode(data);
2033 return ERR_CAST(inode);
2034 if (data->state != NULL && data->state->inode == inode) {
2035 state = data->state;
2036 refcount_inc(&state->count);
2038 state = nfs4_get_open_state(inode, data->owner);
2041 state = ERR_PTR(-ENOMEM);
2045 static struct nfs4_state *
2046 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2048 struct nfs4_state *state;
2050 if (!data->rpc_done) {
2051 state = nfs4_try_open_cached(data);
2052 trace_nfs4_cached_open(data->state);
2056 state = nfs4_opendata_find_nfs4_state(data);
2060 if (data->o_res.delegation_type != 0)
2061 nfs4_opendata_check_deleg(data, state);
2062 if (!update_open_stateid(state, &data->o_res.stateid,
2063 NULL, data->o_arg.fmode)) {
2064 nfs4_put_open_state(state);
2065 state = ERR_PTR(-EAGAIN);
2068 nfs_release_seqid(data->o_arg.seqid);
2072 static struct nfs4_state *
2073 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2075 struct nfs4_state *ret;
2077 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
2078 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
2080 ret = _nfs4_opendata_to_nfs4_state(data);
2081 nfs4_sequence_free_slot(&data->o_res.seq_res);
2085 static struct nfs_open_context *
2086 nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode)
2088 struct nfs_inode *nfsi = NFS_I(state->inode);
2089 struct nfs_open_context *ctx;
2092 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
2093 if (ctx->state != state)
2095 if ((ctx->mode & mode) != mode)
2097 if (!get_nfs_open_context(ctx))
2103 return ERR_PTR(-ENOENT);
2106 static struct nfs_open_context *
2107 nfs4_state_find_open_context(struct nfs4_state *state)
2109 struct nfs_open_context *ctx;
2111 ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE);
2114 ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE);
2117 return nfs4_state_find_open_context_mode(state, FMODE_READ);
2120 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
2121 struct nfs4_state *state, enum open_claim_type4 claim)
2123 struct nfs4_opendata *opendata;
2125 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
2126 NULL, claim, GFP_NOFS);
2127 if (opendata == NULL)
2128 return ERR_PTR(-ENOMEM);
2129 opendata->state = state;
2130 refcount_inc(&state->count);
2134 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
2137 struct nfs4_state *newstate;
2140 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
2142 opendata->o_arg.open_flags = 0;
2143 opendata->o_arg.fmode = fmode;
2144 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
2145 NFS_SB(opendata->dentry->d_sb),
2147 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
2148 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
2149 nfs4_init_opendata_res(opendata);
2150 ret = _nfs4_recover_proc_open(opendata);
2153 newstate = nfs4_opendata_to_nfs4_state(opendata);
2154 if (IS_ERR(newstate))
2155 return PTR_ERR(newstate);
2156 if (newstate != opendata->state)
2158 nfs4_close_state(newstate, fmode);
2162 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
2166 /* memory barrier prior to reading state->n_* */
2168 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2171 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2174 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2178 * We may have performed cached opens for all three recoveries.
2179 * Check if we need to update the current stateid.
2181 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
2182 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
2183 write_seqlock(&state->seqlock);
2184 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2185 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2186 write_sequnlock(&state->seqlock);
2193 * reclaim state on the server after a reboot.
2195 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2197 struct nfs_delegation *delegation;
2198 struct nfs4_opendata *opendata;
2199 fmode_t delegation_type = 0;
2202 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2203 NFS4_OPEN_CLAIM_PREVIOUS);
2204 if (IS_ERR(opendata))
2205 return PTR_ERR(opendata);
2207 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2208 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
2209 delegation_type = delegation->type;
2211 opendata->o_arg.u.delegation_type = delegation_type;
2212 status = nfs4_open_recover(opendata, state);
2213 nfs4_opendata_put(opendata);
2217 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2219 struct nfs_server *server = NFS_SERVER(state->inode);
2220 struct nfs4_exception exception = { };
2223 err = _nfs4_do_open_reclaim(ctx, state);
2224 trace_nfs4_open_reclaim(ctx, 0, err);
2225 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2227 if (err != -NFS4ERR_DELAY)
2229 nfs4_handle_exception(server, err, &exception);
2230 } while (exception.retry);
2234 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2236 struct nfs_open_context *ctx;
2239 ctx = nfs4_state_find_open_context(state);
2242 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2243 nfs_state_clear_open_state_flags(state);
2244 ret = nfs4_do_open_reclaim(ctx, state);
2245 put_nfs_open_context(ctx);
2249 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)
2253 printk(KERN_ERR "NFS: %s: unhandled error "
2254 "%d.\n", __func__, err);
2262 case -NFS4ERR_BADSESSION:
2263 case -NFS4ERR_BADSLOT:
2264 case -NFS4ERR_BAD_HIGH_SLOT:
2265 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2266 case -NFS4ERR_DEADSESSION:
2268 case -NFS4ERR_STALE_CLIENTID:
2269 case -NFS4ERR_STALE_STATEID:
2270 /* Don't recall a delegation if it was lost */
2271 nfs4_schedule_lease_recovery(server->nfs_client);
2273 case -NFS4ERR_MOVED:
2274 nfs4_schedule_migration_recovery(server);
2276 case -NFS4ERR_LEASE_MOVED:
2277 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2279 case -NFS4ERR_DELEG_REVOKED:
2280 case -NFS4ERR_ADMIN_REVOKED:
2281 case -NFS4ERR_EXPIRED:
2282 case -NFS4ERR_BAD_STATEID:
2283 case -NFS4ERR_OPENMODE:
2284 nfs_inode_find_state_and_recover(state->inode,
2286 nfs4_schedule_stateid_recovery(server, state);
2288 case -NFS4ERR_DELAY:
2289 case -NFS4ERR_GRACE:
2293 case -NFS4ERR_DENIED:
2295 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2297 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2304 int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2305 struct nfs4_state *state, const nfs4_stateid *stateid)
2307 struct nfs_server *server = NFS_SERVER(state->inode);
2308 struct nfs4_opendata *opendata;
2311 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2312 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2313 if (IS_ERR(opendata))
2314 return PTR_ERR(opendata);
2315 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
2316 if (!test_bit(NFS_O_RDWR_STATE, &state->flags)) {
2317 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2321 if (!test_bit(NFS_O_WRONLY_STATE, &state->flags)) {
2322 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2326 if (!test_bit(NFS_O_RDONLY_STATE, &state->flags)) {
2327 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2331 nfs_state_clear_delegation(state);
2333 nfs4_opendata_put(opendata);
2334 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
2337 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2339 struct nfs4_opendata *data = calldata;
2341 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2342 &data->c_arg.seq_args, &data->c_res.seq_res, task);
2345 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2347 struct nfs4_opendata *data = calldata;
2349 nfs40_sequence_done(task, &data->c_res.seq_res);
2351 data->rpc_status = task->tk_status;
2352 if (data->rpc_status == 0) {
2353 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
2354 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2355 renew_lease(data->o_res.server, data->timestamp);
2356 data->rpc_done = true;
2360 static void nfs4_open_confirm_release(void *calldata)
2362 struct nfs4_opendata *data = calldata;
2363 struct nfs4_state *state = NULL;
2365 /* If this request hasn't been cancelled, do nothing */
2366 if (!data->cancelled)
2368 /* In case of error, no cleanup! */
2369 if (!data->rpc_done)
2371 state = nfs4_opendata_to_nfs4_state(data);
2373 nfs4_close_state(state, data->o_arg.fmode);
2375 nfs4_opendata_put(data);
2378 static const struct rpc_call_ops nfs4_open_confirm_ops = {
2379 .rpc_call_prepare = nfs4_open_confirm_prepare,
2380 .rpc_call_done = nfs4_open_confirm_done,
2381 .rpc_release = nfs4_open_confirm_release,
2385 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2387 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2389 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
2390 struct rpc_task *task;
2391 struct rpc_message msg = {
2392 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2393 .rpc_argp = &data->c_arg,
2394 .rpc_resp = &data->c_res,
2395 .rpc_cred = data->owner->so_cred,
2397 struct rpc_task_setup task_setup_data = {
2398 .rpc_client = server->client,
2399 .rpc_message = &msg,
2400 .callback_ops = &nfs4_open_confirm_ops,
2401 .callback_data = data,
2402 .workqueue = nfsiod_workqueue,
2403 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
2407 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2409 kref_get(&data->kref);
2410 data->rpc_done = false;
2411 data->rpc_status = 0;
2412 data->timestamp = jiffies;
2413 task = rpc_run_task(&task_setup_data);
2415 return PTR_ERR(task);
2416 status = rpc_wait_for_completion_task(task);
2418 data->cancelled = true;
2421 status = data->rpc_status;
2426 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
2428 struct nfs4_opendata *data = calldata;
2429 struct nfs4_state_owner *sp = data->owner;
2430 struct nfs_client *clp = sp->so_server->nfs_client;
2431 enum open_claim_type4 claim = data->o_arg.claim;
2433 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
2436 * Check if we still need to send an OPEN call, or if we can use
2437 * a delegation instead.
2439 if (data->state != NULL) {
2440 struct nfs_delegation *delegation;
2442 if (can_open_cached(data->state, data->o_arg.fmode,
2443 data->o_arg.open_flags, claim))
2446 delegation = nfs4_get_valid_delegation(data->state->inode);
2447 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
2448 goto unlock_no_action;
2451 /* Update client id. */
2452 data->o_arg.clientid = clp->cl_clientid;
2456 case NFS4_OPEN_CLAIM_PREVIOUS:
2457 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2458 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
2459 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
2461 case NFS4_OPEN_CLAIM_FH:
2462 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
2464 data->timestamp = jiffies;
2465 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
2466 &data->o_arg.seq_args,
2467 &data->o_res.seq_res,
2469 nfs_release_seqid(data->o_arg.seqid);
2471 /* Set the create mode (note dependency on the session type) */
2472 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2473 if (data->o_arg.open_flags & O_EXCL) {
2474 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2475 if (nfs4_has_persistent_session(clp))
2476 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2477 else if (clp->cl_mvops->minor_version > 0)
2478 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2482 trace_nfs4_cached_open(data->state);
2485 task->tk_action = NULL;
2487 nfs4_sequence_done(task, &data->o_res.seq_res);
2490 static void nfs4_open_done(struct rpc_task *task, void *calldata)
2492 struct nfs4_opendata *data = calldata;
2494 data->rpc_status = task->tk_status;
2496 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
2499 if (task->tk_status == 0) {
2500 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2501 switch (data->o_res.f_attr->mode & S_IFMT) {
2505 data->rpc_status = -ELOOP;
2508 data->rpc_status = -EISDIR;
2511 data->rpc_status = -ENOTDIR;
2514 renew_lease(data->o_res.server, data->timestamp);
2515 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2516 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2518 data->rpc_done = true;
2521 static void nfs4_open_release(void *calldata)
2523 struct nfs4_opendata *data = calldata;
2524 struct nfs4_state *state = NULL;
2526 /* If this request hasn't been cancelled, do nothing */
2527 if (!data->cancelled)
2529 /* In case of error, no cleanup! */
2530 if (data->rpc_status != 0 || !data->rpc_done)
2532 /* In case we need an open_confirm, no cleanup! */
2533 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2535 state = nfs4_opendata_to_nfs4_state(data);
2537 nfs4_close_state(state, data->o_arg.fmode);
2539 nfs4_opendata_put(data);
2542 static const struct rpc_call_ops nfs4_open_ops = {
2543 .rpc_call_prepare = nfs4_open_prepare,
2544 .rpc_call_done = nfs4_open_done,
2545 .rpc_release = nfs4_open_release,
2548 static int nfs4_run_open_task(struct nfs4_opendata *data,
2549 struct nfs_open_context *ctx)
2551 struct inode *dir = d_inode(data->dir);
2552 struct nfs_server *server = NFS_SERVER(dir);
2553 struct nfs_openargs *o_arg = &data->o_arg;
2554 struct nfs_openres *o_res = &data->o_res;
2555 struct rpc_task *task;
2556 struct rpc_message msg = {
2557 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2560 .rpc_cred = data->owner->so_cred,
2562 struct rpc_task_setup task_setup_data = {
2563 .rpc_client = server->client,
2564 .rpc_message = &msg,
2565 .callback_ops = &nfs4_open_ops,
2566 .callback_data = data,
2567 .workqueue = nfsiod_workqueue,
2568 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
2572 kref_get(&data->kref);
2573 data->rpc_done = false;
2574 data->rpc_status = 0;
2575 data->cancelled = false;
2576 data->is_recover = false;
2578 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
2579 data->is_recover = true;
2580 task_setup_data.flags |= RPC_TASK_TIMEOUT;
2582 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
2583 pnfs_lgopen_prepare(data, ctx);
2585 task = rpc_run_task(&task_setup_data);
2587 return PTR_ERR(task);
2588 status = rpc_wait_for_completion_task(task);
2590 data->cancelled = true;
2593 status = data->rpc_status;
2599 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2601 struct inode *dir = d_inode(data->dir);
2602 struct nfs_openres *o_res = &data->o_res;
2605 status = nfs4_run_open_task(data, NULL);
2606 if (status != 0 || !data->rpc_done)
2609 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2611 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
2612 status = _nfs4_proc_open_confirm(data);
2618 * Additional permission checks in order to distinguish between an
2619 * open for read, and an open for execute. This works around the
2620 * fact that NFSv4 OPEN treats read and execute permissions as being
2622 * Note that in the non-execute case, we want to turn off permission
2623 * checking if we just created a new file (POSIX open() semantics).
2625 static int nfs4_opendata_access(const struct cred *cred,
2626 struct nfs4_opendata *opendata,
2627 struct nfs4_state *state, fmode_t fmode,
2630 struct nfs_access_entry cache;
2633 /* access call failed or for some reason the server doesn't
2634 * support any access modes -- defer access call until later */
2635 if (opendata->o_res.access_supported == 0)
2640 * Use openflags to check for exec, because fmode won't
2641 * always have FMODE_EXEC set when file open for exec.
2643 if (openflags & __FMODE_EXEC) {
2644 /* ONLY check for exec rights */
2645 if (S_ISDIR(state->inode->i_mode))
2646 mask = NFS4_ACCESS_LOOKUP;
2648 mask = NFS4_ACCESS_EXECUTE;
2649 } else if ((fmode & FMODE_READ) && !opendata->file_created)
2650 mask = NFS4_ACCESS_READ;
2653 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2654 nfs_access_add_cache(state->inode, &cache);
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 nfs4_sequence_free_slot(&o_res->seq_res);
2707 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr,
2708 o_res->f_label, NULL);
2715 * reclaim state on the server after a network partition.
2716 * Assumes caller holds the appropriate lock
2718 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2720 struct nfs4_opendata *opendata;
2723 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2724 NFS4_OPEN_CLAIM_FH);
2725 if (IS_ERR(opendata))
2726 return PTR_ERR(opendata);
2727 ret = nfs4_open_recover(opendata, state);
2729 d_drop(ctx->dentry);
2730 nfs4_opendata_put(opendata);
2734 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2736 struct nfs_server *server = NFS_SERVER(state->inode);
2737 struct nfs4_exception exception = { };
2741 err = _nfs4_open_expired(ctx, state);
2742 trace_nfs4_open_expired(ctx, 0, err);
2743 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2748 case -NFS4ERR_GRACE:
2749 case -NFS4ERR_DELAY:
2750 nfs4_handle_exception(server, err, &exception);
2753 } while (exception.retry);
2758 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2760 struct nfs_open_context *ctx;
2763 ctx = nfs4_state_find_open_context(state);
2766 ret = nfs4_do_open_expired(ctx, state);
2767 put_nfs_open_context(ctx);
2771 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2772 const nfs4_stateid *stateid)
2774 nfs_remove_bad_delegation(state->inode, stateid);
2775 nfs_state_clear_delegation(state);
2778 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2780 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2781 nfs_finish_clear_delegation_stateid(state, NULL);
2784 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2786 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2787 nfs40_clear_delegation_stateid(state);
2788 nfs_state_clear_open_state_flags(state);
2789 return nfs4_open_expired(sp, state);
2792 static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2793 nfs4_stateid *stateid,
2794 const struct cred *cred)
2796 return -NFS4ERR_BAD_STATEID;
2799 #if defined(CONFIG_NFS_V4_1)
2800 static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2801 nfs4_stateid *stateid,
2802 const struct cred *cred)
2806 switch (stateid->type) {
2809 case NFS4_INVALID_STATEID_TYPE:
2810 case NFS4_SPECIAL_STATEID_TYPE:
2811 return -NFS4ERR_BAD_STATEID;
2812 case NFS4_REVOKED_STATEID_TYPE:
2816 status = nfs41_test_stateid(server, stateid, cred);
2818 case -NFS4ERR_EXPIRED:
2819 case -NFS4ERR_ADMIN_REVOKED:
2820 case -NFS4ERR_DELEG_REVOKED:
2826 /* Ack the revoked state to the server */
2827 nfs41_free_stateid(server, stateid, cred, true);
2828 return -NFS4ERR_EXPIRED;
2831 static int nfs41_check_delegation_stateid(struct nfs4_state *state)
2833 struct nfs_server *server = NFS_SERVER(state->inode);
2834 nfs4_stateid stateid;
2835 struct nfs_delegation *delegation;
2836 const struct cred *cred = NULL;
2837 int status, ret = NFS_OK;
2839 /* Get the delegation credential for use by test/free_stateid */
2841 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2842 if (delegation == NULL) {
2844 nfs_state_clear_delegation(state);
2848 spin_lock(&delegation->lock);
2849 nfs4_stateid_copy(&stateid, &delegation->stateid);
2851 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2852 &delegation->flags)) {
2853 spin_unlock(&delegation->lock);
2858 if (delegation->cred)
2859 cred = get_cred(delegation->cred);
2860 spin_unlock(&delegation->lock);
2862 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
2863 trace_nfs4_test_delegation_stateid(state, NULL, status);
2864 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
2865 nfs_finish_clear_delegation_stateid(state, &stateid);
2873 static void nfs41_delegation_recover_stateid(struct nfs4_state *state)
2877 if (test_bit(NFS_DELEGATED_STATE, &state->flags) &&
2878 nfs4_copy_delegation_stateid(state->inode, state->state,
2880 nfs4_stateid_match_other(&state->stateid, &tmp))
2881 nfs_state_set_delegation(state, &tmp, state->state);
2883 nfs_state_clear_delegation(state);
2887 * nfs41_check_expired_locks - possibly free a lock stateid
2889 * @state: NFSv4 state for an inode
2891 * Returns NFS_OK if recovery for this stateid is now finished.
2892 * Otherwise a negative NFS4ERR value is returned.
2894 static int nfs41_check_expired_locks(struct nfs4_state *state)
2896 int status, ret = NFS_OK;
2897 struct nfs4_lock_state *lsp, *prev = NULL;
2898 struct nfs_server *server = NFS_SERVER(state->inode);
2900 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2903 spin_lock(&state->state_lock);
2904 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2905 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2906 const struct cred *cred = lsp->ls_state->owner->so_cred;
2908 refcount_inc(&lsp->ls_count);
2909 spin_unlock(&state->state_lock);
2911 nfs4_put_lock_state(prev);
2914 status = nfs41_test_and_free_expired_stateid(server,
2917 trace_nfs4_test_lock_stateid(state, lsp, status);
2918 if (status == -NFS4ERR_EXPIRED ||
2919 status == -NFS4ERR_BAD_STATEID) {
2920 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
2921 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
2922 if (!recover_lost_locks)
2923 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2924 } else if (status != NFS_OK) {
2926 nfs4_put_lock_state(prev);
2929 spin_lock(&state->state_lock);
2932 spin_unlock(&state->state_lock);
2933 nfs4_put_lock_state(prev);
2939 * nfs41_check_open_stateid - possibly free an open stateid
2941 * @state: NFSv4 state for an inode
2943 * Returns NFS_OK if recovery for this stateid is now finished.
2944 * Otherwise a negative NFS4ERR value is returned.
2946 static int nfs41_check_open_stateid(struct nfs4_state *state)
2948 struct nfs_server *server = NFS_SERVER(state->inode);
2949 nfs4_stateid *stateid = &state->open_stateid;
2950 const struct cred *cred = state->owner->so_cred;
2953 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0)
2954 return -NFS4ERR_BAD_STATEID;
2955 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
2956 trace_nfs4_test_open_stateid(state, NULL, status);
2957 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
2958 nfs_state_clear_open_state_flags(state);
2959 stateid->type = NFS4_INVALID_STATEID_TYPE;
2962 if (nfs_open_stateid_recover_openmode(state))
2963 return -NFS4ERR_OPENMODE;
2967 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2971 status = nfs41_check_delegation_stateid(state);
2972 if (status != NFS_OK)
2974 nfs41_delegation_recover_stateid(state);
2976 status = nfs41_check_expired_locks(state);
2977 if (status != NFS_OK)
2979 status = nfs41_check_open_stateid(state);
2980 if (status != NFS_OK)
2981 status = nfs4_open_expired(sp, state);
2987 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2988 * fields corresponding to attributes that were used to store the verifier.
2989 * Make sure we clobber those fields in the later setattr call
2991 static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2992 struct iattr *sattr, struct nfs4_label **label)
2994 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2999 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
3000 attrset[i] = opendata->o_res.attrset[i];
3001 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
3002 attrset[i] &= ~bitmask[i];
3005 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
3006 sattr->ia_valid : 0;
3008 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
3009 if (sattr->ia_valid & ATTR_ATIME_SET)
3010 ret |= ATTR_ATIME_SET;
3015 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
3016 if (sattr->ia_valid & ATTR_MTIME_SET)
3017 ret |= ATTR_MTIME_SET;
3022 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
3027 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
3028 int flags, struct nfs_open_context *ctx)
3030 struct nfs4_state_owner *sp = opendata->owner;
3031 struct nfs_server *server = sp->so_server;
3032 struct dentry *dentry;
3033 struct nfs4_state *state;
3034 fmode_t acc_mode = _nfs4_ctx_to_accessmode(ctx);
3035 struct inode *dir = d_inode(opendata->dir);
3036 unsigned long dir_verifier;
3040 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
3041 dir_verifier = nfs_save_change_attribute(dir);
3043 ret = _nfs4_proc_open(opendata, ctx);
3047 state = _nfs4_opendata_to_nfs4_state(opendata);
3048 ret = PTR_ERR(state);
3052 if (server->caps & NFS_CAP_POSIX_LOCK)
3053 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
3054 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
3055 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
3057 dentry = opendata->dentry;
3058 if (d_really_is_negative(dentry)) {
3059 struct dentry *alias;
3061 alias = d_exact_alias(dentry, state->inode);
3063 alias = d_splice_alias(igrab(state->inode), dentry);
3064 /* d_splice_alias() can't fail here - it's a non-directory */
3067 ctx->dentry = dentry = alias;
3071 switch(opendata->o_arg.claim) {
3074 case NFS4_OPEN_CLAIM_NULL:
3075 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
3076 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
3077 if (!opendata->rpc_done)
3079 if (opendata->o_res.delegation_type != 0)
3080 dir_verifier = nfs_save_change_attribute(dir);
3081 nfs_set_verifier(dentry, dir_verifier);
3084 /* Parse layoutget results before we check for access */
3085 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
3087 ret = nfs4_opendata_access(sp->so_cred, opendata, state,
3092 if (d_inode(dentry) == state->inode) {
3093 nfs_inode_attach_open_context(ctx);
3094 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
3095 nfs4_schedule_stateid_recovery(server, state);
3099 if (!opendata->cancelled)
3100 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
3105 * Returns a referenced nfs4_state
3107 static int _nfs4_do_open(struct inode *dir,
3108 struct nfs_open_context *ctx,
3110 const struct nfs4_open_createattrs *c,
3113 struct nfs4_state_owner *sp;
3114 struct nfs4_state *state = NULL;
3115 struct nfs_server *server = NFS_SERVER(dir);
3116 struct nfs4_opendata *opendata;
3117 struct dentry *dentry = ctx->dentry;
3118 const struct cred *cred = ctx->cred;
3119 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
3120 fmode_t fmode = _nfs4_ctx_to_openmode(ctx);
3121 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
3122 struct iattr *sattr = c->sattr;
3123 struct nfs4_label *label = c->label;
3124 struct nfs4_label *olabel = NULL;
3127 /* Protect against reboot recovery conflicts */
3129 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
3131 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
3134 status = nfs4_client_recover_expired_lease(server->nfs_client);
3136 goto err_put_state_owner;
3137 if (d_really_is_positive(dentry))
3138 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
3140 if (d_really_is_positive(dentry))
3141 claim = NFS4_OPEN_CLAIM_FH;
3142 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
3143 c, claim, GFP_KERNEL);
3144 if (opendata == NULL)
3145 goto err_put_state_owner;
3148 olabel = nfs4_label_alloc(server, GFP_KERNEL);
3149 if (IS_ERR(olabel)) {
3150 status = PTR_ERR(olabel);
3151 goto err_opendata_put;
3155 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
3156 if (!opendata->f_attr.mdsthreshold) {
3157 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
3158 if (!opendata->f_attr.mdsthreshold)
3159 goto err_free_label;
3161 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
3163 if (d_really_is_positive(dentry))
3164 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
3166 status = _nfs4_open_and_get_state(opendata, flags, ctx);
3168 goto err_free_label;
3171 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
3172 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
3173 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
3175 * send create attributes which was not set by open
3176 * with an extra setattr.
3178 if (attrs || label) {
3179 unsigned ia_old = sattr->ia_valid;
3181 sattr->ia_valid = attrs;
3182 nfs_fattr_init(opendata->o_res.f_attr);
3183 status = nfs4_do_setattr(state->inode, cred,
3184 opendata->o_res.f_attr, sattr,
3185 ctx, label, olabel);
3187 nfs_setattr_update_inode(state->inode, sattr,
3188 opendata->o_res.f_attr);
3189 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
3191 sattr->ia_valid = ia_old;
3194 if (opened && opendata->file_created)
3197 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
3198 *ctx_th = opendata->f_attr.mdsthreshold;
3199 opendata->f_attr.mdsthreshold = NULL;
3202 nfs4_label_free(olabel);
3204 nfs4_opendata_put(opendata);
3205 nfs4_put_state_owner(sp);
3208 nfs4_label_free(olabel);
3210 nfs4_opendata_put(opendata);
3211 err_put_state_owner:
3212 nfs4_put_state_owner(sp);
3218 static struct nfs4_state *nfs4_do_open(struct inode *dir,
3219 struct nfs_open_context *ctx,
3221 struct iattr *sattr,
3222 struct nfs4_label *label,
3225 struct nfs_server *server = NFS_SERVER(dir);
3226 struct nfs4_exception exception = {
3227 .interruptible = true,
3229 struct nfs4_state *res;
3230 struct nfs4_open_createattrs c = {
3234 [0] = (__u32)jiffies,
3235 [1] = (__u32)current->pid,
3241 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
3243 trace_nfs4_open_file(ctx, flags, status);
3246 /* NOTE: BAD_SEQID means the server and client disagree about the
3247 * book-keeping w.r.t. state-changing operations
3248 * (OPEN/CLOSE/LOCK/LOCKU...)
3249 * It is actually a sign of a bug on the client or on the server.
3251 * If we receive a BAD_SEQID error in the particular case of
3252 * doing an OPEN, we assume that nfs_increment_open_seqid() will
3253 * have unhashed the old state_owner for us, and that we can
3254 * therefore safely retry using a new one. We should still warn
3255 * the user though...
3257 if (status == -NFS4ERR_BAD_SEQID) {
3258 pr_warn_ratelimited("NFS: v4 server %s "
3259 " returned a bad sequence-id error!\n",
3260 NFS_SERVER(dir)->nfs_client->cl_hostname);
3261 exception.retry = 1;
3265 * BAD_STATEID on OPEN means that the server cancelled our
3266 * state before it received the OPEN_CONFIRM.
3267 * Recover by retrying the request as per the discussion
3268 * on Page 181 of RFC3530.
3270 if (status == -NFS4ERR_BAD_STATEID) {
3271 exception.retry = 1;
3274 if (status == -NFS4ERR_EXPIRED) {
3275 nfs4_schedule_lease_recovery(server->nfs_client);
3276 exception.retry = 1;
3279 if (status == -EAGAIN) {
3280 /* We must have found a delegation */
3281 exception.retry = 1;
3284 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3286 res = ERR_PTR(nfs4_handle_exception(server,
3287 status, &exception));
3288 } while (exception.retry);
3292 static int _nfs4_do_setattr(struct inode *inode,
3293 struct nfs_setattrargs *arg,
3294 struct nfs_setattrres *res,
3295 const struct cred *cred,
3296 struct nfs_open_context *ctx)
3298 struct nfs_server *server = NFS_SERVER(inode);
3299 struct rpc_message msg = {
3300 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3305 const struct cred *delegation_cred = NULL;
3306 unsigned long timestamp = jiffies;
3310 nfs_fattr_init(res->fattr);
3312 /* Servers should only apply open mode checks for file size changes */
3313 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
3315 nfs4_inode_make_writeable(inode);
3319 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
3320 /* Use that stateid */
3321 } else if (ctx != NULL && ctx->state) {
3322 struct nfs_lock_context *l_ctx;
3323 if (!nfs4_valid_open_stateid(ctx->state))
3325 l_ctx = nfs_get_lock_context(ctx);
3327 return PTR_ERR(l_ctx);
3328 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3329 &arg->stateid, &delegation_cred);
3330 nfs_put_lock_context(l_ctx);
3333 else if (status == -EAGAIN)
3337 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
3339 if (delegation_cred)
3340 msg.rpc_cred = delegation_cred;
3342 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3344 put_cred(delegation_cred);
3345 if (status == 0 && ctx != NULL)
3346 renew_lease(server, timestamp);
3347 trace_nfs4_setattr(inode, &arg->stateid, status);
3351 static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
3352 struct nfs_fattr *fattr, struct iattr *sattr,
3353 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
3354 struct nfs4_label *olabel)
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 = {
3371 struct nfs4_exception exception = {
3374 .stateid = &arg.stateid,
3376 unsigned long adjust_flags = NFS_INO_INVALID_CHANGE;
3379 if (sattr->ia_valid & (ATTR_MODE | ATTR_KILL_SUID | ATTR_KILL_SGID))
3380 adjust_flags |= NFS_INO_INVALID_MODE;
3381 if (sattr->ia_valid & (ATTR_UID | ATTR_GID))
3382 adjust_flags |= NFS_INO_INVALID_OTHER;
3385 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, olabel),
3386 inode, adjust_flags);
3388 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
3390 case -NFS4ERR_OPENMODE:
3391 if (!(sattr->ia_valid & ATTR_SIZE)) {
3392 pr_warn_once("NFSv4: server %s is incorrectly "
3393 "applying open mode checks to "
3394 "a SETATTR that is not "
3395 "changing file size.\n",
3396 server->nfs_client->cl_hostname);
3398 if (state && !(state->state & FMODE_WRITE)) {
3400 if (sattr->ia_valid & ATTR_OPEN)
3405 err = nfs4_handle_exception(server, err, &exception);
3406 } while (exception.retry);
3412 nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3414 if (inode == NULL || !nfs_have_layout(inode))
3417 return pnfs_wait_on_layoutreturn(inode, task);
3421 * Update the seqid of an open stateid
3423 static void nfs4_sync_open_stateid(nfs4_stateid *dst,
3424 struct nfs4_state *state)
3431 if (!nfs4_valid_open_stateid(state))
3433 seq = read_seqbegin(&state->seqlock);
3434 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3435 nfs4_stateid_copy(dst, &state->open_stateid);
3436 if (read_seqretry(&state->seqlock, seq))
3440 seqid_open = state->open_stateid.seqid;
3441 if (read_seqretry(&state->seqlock, seq))
3444 dst_seqid = be32_to_cpu(dst->seqid);
3445 if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) < 0)
3446 dst->seqid = seqid_open;
3452 * Update the seqid of an open stateid after receiving
3453 * NFS4ERR_OLD_STATEID
3455 static bool nfs4_refresh_open_old_stateid(nfs4_stateid *dst,
3456 struct nfs4_state *state)
3461 int seq, status = -EAGAIN;
3466 if (!nfs4_valid_open_stateid(state))
3468 seq = read_seqbegin(&state->seqlock);
3469 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3470 if (read_seqretry(&state->seqlock, seq))
3475 write_seqlock(&state->seqlock);
3476 seqid_open = state->open_stateid.seqid;
3478 dst_seqid = be32_to_cpu(dst->seqid);
3480 /* Did another OPEN bump the state's seqid? try again: */
3481 if ((s32)(be32_to_cpu(seqid_open) - dst_seqid) > 0) {
3482 dst->seqid = seqid_open;
3483 write_sequnlock(&state->seqlock);
3488 /* server says we're behind but we haven't seen the update yet */
3489 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
3490 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
3491 write_sequnlock(&state->seqlock);
3492 trace_nfs4_close_stateid_update_wait(state->inode, dst, 0);
3494 if (fatal_signal_pending(current))
3497 if (schedule_timeout(5*HZ) != 0)
3500 finish_wait(&state->waitq, &wait);
3504 if (status == -EINTR)
3507 /* we slept the whole 5 seconds, we must have lost a seqid */
3508 dst->seqid = cpu_to_be32(dst_seqid + 1);
3516 struct nfs4_closedata {
3517 struct inode *inode;
3518 struct nfs4_state *state;
3519 struct nfs_closeargs arg;
3520 struct nfs_closeres res;
3522 struct nfs4_layoutreturn_args arg;
3523 struct nfs4_layoutreturn_res res;
3524 struct nfs4_xdr_opaque_data ld_private;
3528 struct nfs_fattr fattr;
3529 unsigned long timestamp;
3532 static void nfs4_free_closedata(void *data)
3534 struct nfs4_closedata *calldata = data;
3535 struct nfs4_state_owner *sp = calldata->state->owner;
3536 struct super_block *sb = calldata->state->inode->i_sb;
3538 if (calldata->lr.roc)
3539 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3540 calldata->res.lr_ret);
3541 nfs4_put_open_state(calldata->state);
3542 nfs_free_seqid(calldata->arg.seqid);
3543 nfs4_put_state_owner(sp);
3544 nfs_sb_deactive(sb);
3548 static void nfs4_close_done(struct rpc_task *task, void *data)
3550 struct nfs4_closedata *calldata = data;
3551 struct nfs4_state *state = calldata->state;
3552 struct nfs_server *server = NFS_SERVER(calldata->inode);
3553 nfs4_stateid *res_stateid = NULL;
3554 struct nfs4_exception exception = {
3556 .inode = calldata->inode,
3557 .stateid = &calldata->arg.stateid,
3560 dprintk("%s: begin!\n", __func__);
3561 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3563 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
3565 /* Handle Layoutreturn errors */
3566 if (pnfs_roc_done(task, &calldata->arg.lr_args, &calldata->res.lr_res,
3567 &calldata->res.lr_ret) == -EAGAIN)
3570 /* hmm. we are done with the inode, and in the process of freeing
3571 * the state_owner. we keep this around to process errors
3573 switch (task->tk_status) {
3575 res_stateid = &calldata->res.stateid;
3576 renew_lease(server, calldata->timestamp);
3578 case -NFS4ERR_ACCESS:
3579 if (calldata->arg.bitmask != NULL) {
3580 calldata->arg.bitmask = NULL;
3581 calldata->res.fattr = NULL;
3586 case -NFS4ERR_OLD_STATEID:
3587 /* Did we race with OPEN? */
3588 if (nfs4_refresh_open_old_stateid(&calldata->arg.stateid,
3592 case -NFS4ERR_ADMIN_REVOKED:
3593 case -NFS4ERR_STALE_STATEID:
3594 case -NFS4ERR_EXPIRED:
3595 nfs4_free_revoked_stateid(server,
3596 &calldata->arg.stateid,
3597 task->tk_msg.rpc_cred);
3599 case -NFS4ERR_BAD_STATEID:
3600 if (calldata->arg.fmode == 0)
3604 task->tk_status = nfs4_async_handle_exception(task,
3605 server, task->tk_status, &exception);
3606 if (exception.retry)
3609 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3610 res_stateid, calldata->arg.fmode);
3612 task->tk_status = 0;
3613 nfs_release_seqid(calldata->arg.seqid);
3614 nfs_refresh_inode(calldata->inode, &calldata->fattr);
3615 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
3618 task->tk_status = 0;
3619 rpc_restart_call_prepare(task);
3623 static void nfs4_close_prepare(struct rpc_task *task, void *data)
3625 struct nfs4_closedata *calldata = data;
3626 struct nfs4_state *state = calldata->state;
3627 struct inode *inode = calldata->inode;
3628 struct nfs_server *server = NFS_SERVER(inode);
3629 struct pnfs_layout_hdr *lo;
3630 bool is_rdonly, is_wronly, is_rdwr;
3633 dprintk("%s: begin!\n", __func__);
3634 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3637 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
3638 spin_lock(&state->owner->so_lock);
3639 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3640 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3641 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
3642 /* Calculate the change in open mode */
3643 calldata->arg.fmode = 0;
3644 if (state->n_rdwr == 0) {
3645 if (state->n_rdonly == 0)
3646 call_close |= is_rdonly;
3648 calldata->arg.fmode |= FMODE_READ;
3649 if (state->n_wronly == 0)
3650 call_close |= is_wronly;
3652 calldata->arg.fmode |= FMODE_WRITE;
3653 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3654 call_close |= is_rdwr;
3656 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3658 nfs4_sync_open_stateid(&calldata->arg.stateid, state);
3659 if (!nfs4_valid_open_stateid(state))
3661 spin_unlock(&state->owner->so_lock);
3664 /* Note: exit _without_ calling nfs4_close_done */
3668 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
3669 nfs_release_seqid(calldata->arg.seqid);
3673 lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3674 if (lo && !pnfs_layout_is_valid(lo)) {
3675 calldata->arg.lr_args = NULL;
3676 calldata->res.lr_res = NULL;
3679 if (calldata->arg.fmode == 0)
3680 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
3682 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
3683 /* Close-to-open cache consistency revalidation */
3684 if (!nfs4_have_delegation(inode, FMODE_READ)) {
3685 nfs4_bitmask_set(calldata->arg.bitmask_store,
3686 server->cache_consistency_bitmask,
3687 inode, server, NULL);
3688 calldata->arg.bitmask = calldata->arg.bitmask_store;
3690 calldata->arg.bitmask = NULL;
3693 calldata->arg.share_access =
3694 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3695 calldata->arg.fmode, 0);
3697 if (calldata->res.fattr == NULL)
3698 calldata->arg.bitmask = NULL;
3699 else if (calldata->arg.bitmask == NULL)
3700 calldata->res.fattr = NULL;
3701 calldata->timestamp = jiffies;
3702 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
3703 &calldata->arg.seq_args,
3704 &calldata->res.seq_res,
3706 nfs_release_seqid(calldata->arg.seqid);
3707 dprintk("%s: done!\n", __func__);
3710 task->tk_action = NULL;
3712 nfs4_sequence_done(task, &calldata->res.seq_res);
3715 static const struct rpc_call_ops nfs4_close_ops = {
3716 .rpc_call_prepare = nfs4_close_prepare,
3717 .rpc_call_done = nfs4_close_done,
3718 .rpc_release = nfs4_free_closedata,
3722 * It is possible for data to be read/written from a mem-mapped file
3723 * after the sys_close call (which hits the vfs layer as a flush).
3724 * This means that we can't safely call nfsv4 close on a file until
3725 * the inode is cleared. This in turn means that we are not good
3726 * NFSv4 citizens - we do not indicate to the server to update the file's
3727 * share state even when we are done with one of the three share
3728 * stateid's in the inode.
3730 * NOTE: Caller must be holding the sp->so_owner semaphore!
3732 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
3734 struct nfs_server *server = NFS_SERVER(state->inode);
3735 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
3736 struct nfs4_closedata *calldata;
3737 struct nfs4_state_owner *sp = state->owner;
3738 struct rpc_task *task;
3739 struct rpc_message msg = {
3740 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3741 .rpc_cred = state->owner->so_cred,
3743 struct rpc_task_setup task_setup_data = {
3744 .rpc_client = server->client,
3745 .rpc_message = &msg,
3746 .callback_ops = &nfs4_close_ops,
3747 .workqueue = nfsiod_workqueue,
3748 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
3750 int status = -ENOMEM;
3752 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3753 &task_setup_data.rpc_client, &msg);
3755 calldata = kzalloc(sizeof(*calldata), gfp_mask);
3756 if (calldata == NULL)
3758 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
3759 calldata->inode = state->inode;
3760 calldata->state = state;
3761 calldata->arg.fh = NFS_FH(state->inode);
3762 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3763 goto out_free_calldata;
3764 /* Serialization for the sequence id */
3765 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3766 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
3767 if (IS_ERR(calldata->arg.seqid))
3768 goto out_free_calldata;
3769 nfs_fattr_init(&calldata->fattr);
3770 calldata->arg.fmode = 0;
3771 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
3772 calldata->res.fattr = &calldata->fattr;
3773 calldata->res.seqid = calldata->arg.seqid;
3774 calldata->res.server = server;
3775 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3776 calldata->lr.roc = pnfs_roc(state->inode,
3777 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3778 if (calldata->lr.roc) {
3779 calldata->arg.lr_args = &calldata->lr.arg;
3780 calldata->res.lr_res = &calldata->lr.res;
3782 nfs_sb_active(calldata->inode->i_sb);
3784 msg.rpc_argp = &calldata->arg;
3785 msg.rpc_resp = &calldata->res;
3786 task_setup_data.callback_data = calldata;
3787 task = rpc_run_task(&task_setup_data);
3789 return PTR_ERR(task);
3792 status = rpc_wait_for_completion_task(task);
3798 nfs4_put_open_state(state);
3799 nfs4_put_state_owner(sp);
3803 static struct inode *
3804 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3805 int open_flags, struct iattr *attr, int *opened)
3807 struct nfs4_state *state;
3808 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3810 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
3812 /* Protect against concurrent sillydeletes */
3813 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
3815 nfs4_label_release_security(label);
3818 return ERR_CAST(state);
3819 return state->inode;
3822 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
3824 if (ctx->state == NULL)
3827 nfs4_close_sync(ctx->state, _nfs4_ctx_to_openmode(ctx));
3829 nfs4_close_state(ctx->state, _nfs4_ctx_to_openmode(ctx));
3832 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3833 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3834 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_XATTR_SUPPORT - 1UL)
3836 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3838 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
3839 struct nfs4_server_caps_arg args = {
3843 struct nfs4_server_caps_res res = {};
3844 struct rpc_message msg = {
3845 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
3852 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3853 FATTR4_WORD0_FH_EXPIRE_TYPE |
3854 FATTR4_WORD0_LINK_SUPPORT |
3855 FATTR4_WORD0_SYMLINK_SUPPORT |
3856 FATTR4_WORD0_ACLSUPPORT;
3858 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3860 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3862 /* Sanity check the server answers */
3863 switch (minorversion) {
3865 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3866 res.attr_bitmask[2] = 0;
3869 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3872 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3874 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
3875 server->caps &= ~(NFS_CAP_ACLS | NFS_CAP_HARDLINKS |
3876 NFS_CAP_SYMLINKS| NFS_CAP_SECURITY_LABEL);
3877 server->fattr_valid = NFS_ATTR_FATTR_V4;
3878 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3879 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3880 server->caps |= NFS_CAP_ACLS;
3881 if (res.has_links != 0)
3882 server->caps |= NFS_CAP_HARDLINKS;
3883 if (res.has_symlinks != 0)
3884 server->caps |= NFS_CAP_SYMLINKS;
3885 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3886 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3887 server->caps |= NFS_CAP_SECURITY_LABEL;
3889 if (!(res.attr_bitmask[0] & FATTR4_WORD0_FILEID))
3890 server->fattr_valid &= ~NFS_ATTR_FATTR_FILEID;
3891 if (!(res.attr_bitmask[1] & FATTR4_WORD1_MODE))
3892 server->fattr_valid &= ~NFS_ATTR_FATTR_MODE;
3893 if (!(res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS))
3894 server->fattr_valid &= ~NFS_ATTR_FATTR_NLINK;
3895 if (!(res.attr_bitmask[1] & FATTR4_WORD1_OWNER))
3896 server->fattr_valid &= ~(NFS_ATTR_FATTR_OWNER |
3897 NFS_ATTR_FATTR_OWNER_NAME);
3898 if (!(res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP))
3899 server->fattr_valid &= ~(NFS_ATTR_FATTR_GROUP |
3900 NFS_ATTR_FATTR_GROUP_NAME);
3901 if (!(res.attr_bitmask[1] & FATTR4_WORD1_SPACE_USED))
3902 server->fattr_valid &= ~NFS_ATTR_FATTR_SPACE_USED;
3903 if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS))
3904 server->fattr_valid &= ~NFS_ATTR_FATTR_ATIME;
3905 if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA))
3906 server->fattr_valid &= ~NFS_ATTR_FATTR_CTIME;
3907 if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY))
3908 server->fattr_valid &= ~NFS_ATTR_FATTR_MTIME;
3909 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3910 sizeof(server->attr_bitmask));
3911 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3913 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3914 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3915 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
3916 server->cache_consistency_bitmask[2] = 0;
3918 /* Avoid a regression due to buggy server */
3919 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3920 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
3921 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3922 sizeof(server->exclcreat_bitmask));
3924 server->acl_bitmask = res.acl_bitmask;
3925 server->fh_expire_type = res.fh_expire_type;
3931 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3933 struct nfs4_exception exception = {
3934 .interruptible = true,
3938 err = nfs4_handle_exception(server,
3939 _nfs4_server_capabilities(server, fhandle),
3941 } while (exception.retry);
3945 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3946 struct nfs_fsinfo *info)
3949 struct nfs4_lookup_root_arg args = {
3952 struct nfs4_lookup_res res = {
3954 .fattr = info->fattr,
3957 struct rpc_message msg = {
3958 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3963 bitmask[0] = nfs4_fattr_bitmap[0];
3964 bitmask[1] = nfs4_fattr_bitmap[1];
3966 * Process the label in the upcoming getfattr
3968 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3970 nfs_fattr_init(info->fattr);
3971 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3974 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3975 struct nfs_fsinfo *info)
3977 struct nfs4_exception exception = {
3978 .interruptible = true,
3982 err = _nfs4_lookup_root(server, fhandle, info);
3983 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
3986 case -NFS4ERR_WRONGSEC:
3989 err = nfs4_handle_exception(server, err, &exception);
3991 } while (exception.retry);
3996 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3997 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3999 struct rpc_auth_create_args auth_args = {
4000 .pseudoflavor = flavor,
4002 struct rpc_auth *auth;
4004 auth = rpcauth_create(&auth_args, server->client);
4007 return nfs4_lookup_root(server, fhandle, info);
4011 * Retry pseudoroot lookup with various security flavors. We do this when:
4013 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
4014 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
4016 * Returns zero on success, or a negative NFS4ERR value, or a
4017 * negative errno value.
4019 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
4020 struct nfs_fsinfo *info)
4022 /* Per 3530bis 15.33.5 */
4023 static const rpc_authflavor_t flav_array[] = {
4027 RPC_AUTH_UNIX, /* courtesy */
4030 int status = -EPERM;
4033 if (server->auth_info.flavor_len > 0) {
4034 /* try each flavor specified by user */
4035 for (i = 0; i < server->auth_info.flavor_len; i++) {
4036 status = nfs4_lookup_root_sec(server, fhandle, info,
4037 server->auth_info.flavors[i]);
4038 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4043 /* no flavors specified by user, try default list */
4044 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
4045 status = nfs4_lookup_root_sec(server, fhandle, info,
4047 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4054 * -EACCES could mean that the user doesn't have correct permissions
4055 * to access the mount. It could also mean that we tried to mount
4056 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
4057 * existing mount programs don't handle -EACCES very well so it should
4058 * be mapped to -EPERM instead.
4060 if (status == -EACCES)
4066 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
4067 * @server: initialized nfs_server handle
4068 * @fhandle: we fill in the pseudo-fs root file handle
4069 * @info: we fill in an FSINFO struct
4070 * @auth_probe: probe the auth flavours
4072 * Returns zero on success, or a negative errno.
4074 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
4075 struct nfs_fsinfo *info,
4081 status = nfs4_lookup_root(server, fhandle, info);
4083 if (auth_probe || status == NFS4ERR_WRONGSEC)
4084 status = server->nfs_client->cl_mvops->find_root_sec(server,
4088 status = nfs4_server_capabilities(server, fhandle);
4090 status = nfs4_do_fsinfo(server, fhandle, info);
4092 return nfs4_map_errors(status);
4095 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
4096 struct nfs_fsinfo *info)
4099 struct nfs_fattr *fattr = info->fattr;
4100 struct nfs4_label *label = fattr->label;
4102 error = nfs4_server_capabilities(server, mntfh);
4104 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
4108 error = nfs4_proc_getattr(server, mntfh, fattr, label, NULL);
4110 dprintk("nfs4_get_root: getattr error = %d\n", -error);
4114 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
4115 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
4116 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
4123 * Get locations and (maybe) other attributes of a referral.
4124 * Note that we'll actually follow the referral later when
4125 * we detect fsid mismatch in inode revalidation
4127 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
4128 const struct qstr *name, struct nfs_fattr *fattr,
4129 struct nfs_fh *fhandle)
4131 int status = -ENOMEM;
4132 struct page *page = NULL;
4133 struct nfs4_fs_locations *locations = NULL;
4135 page = alloc_page(GFP_KERNEL);
4138 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4139 if (locations == NULL)
4142 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
4147 * If the fsid didn't change, this is a migration event, not a
4148 * referral. Cause us to drop into the exception handler, which
4149 * will kick off migration recovery.
4151 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
4152 dprintk("%s: server did not return a different fsid for"
4153 " a referral at %s\n", __func__, name->name);
4154 status = -NFS4ERR_MOVED;
4157 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
4158 nfs_fixup_referral_attributes(&locations->fattr);
4160 /* replace the lookup nfs_fattr with the locations nfs_fattr */
4161 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
4162 memset(fhandle, 0, sizeof(struct nfs_fh));
4170 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4171 struct nfs_fattr *fattr, struct nfs4_label *label,
4172 struct inode *inode)
4174 __u32 bitmask[NFS4_BITMASK_SZ];
4175 struct nfs4_getattr_arg args = {
4179 struct nfs4_getattr_res res = {
4184 struct rpc_message msg = {
4185 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4189 unsigned short task_flags = 0;
4191 /* Is this is an attribute revalidation, subject to softreval? */
4192 if (inode && (server->flags & NFS_MOUNT_SOFTREVAL))
4193 task_flags |= RPC_TASK_TIMEOUT;
4195 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode, 0);
4196 nfs_fattr_init(fattr);
4197 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4198 return nfs4_do_call_sync(server->client, server, &msg,
4199 &args.seq_args, &res.seq_res, task_flags);
4202 int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4203 struct nfs_fattr *fattr, struct nfs4_label *label,
4204 struct inode *inode)
4206 struct nfs4_exception exception = {
4207 .interruptible = true,
4211 err = _nfs4_proc_getattr(server, fhandle, fattr, label, inode);
4212 trace_nfs4_getattr(server, fhandle, fattr, err);
4213 err = nfs4_handle_exception(server, err,
4215 } while (exception.retry);
4220 * The file is not closed if it is opened due to the a request to change
4221 * the size of the file. The open call will not be needed once the
4222 * VFS layer lookup-intents are implemented.
4224 * Close is called when the inode is destroyed.
4225 * If we haven't opened the file for O_WRONLY, we
4226 * need to in the size_change case to obtain a stateid.
4229 * Because OPEN is always done by name in nfsv4, it is
4230 * possible that we opened a different file by the same
4231 * name. We can recognize this race condition, but we
4232 * can't do anything about it besides returning an error.
4234 * This will be fixed with VFS changes (lookup-intent).
4237 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
4238 struct iattr *sattr)
4240 struct inode *inode = d_inode(dentry);
4241 const struct cred *cred = NULL;
4242 struct nfs_open_context *ctx = NULL;
4243 struct nfs4_label *label = NULL;
4246 if (pnfs_ld_layoutret_on_setattr(inode) &&
4247 sattr->ia_valid & ATTR_SIZE &&
4248 sattr->ia_size < i_size_read(inode))
4249 pnfs_commit_and_return_layout(inode);
4251 nfs_fattr_init(fattr);
4253 /* Deal with open(O_TRUNC) */
4254 if (sattr->ia_valid & ATTR_OPEN)
4255 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
4257 /* Optimization: if the end result is no change, don't RPC */
4258 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
4261 /* Search for an existing open(O_WRITE) file */
4262 if (sattr->ia_valid & ATTR_FILE) {
4264 ctx = nfs_file_open_context(sattr->ia_file);
4269 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4271 return PTR_ERR(label);
4273 /* Return any delegations if we're going to change ACLs */
4274 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
4275 nfs4_inode_make_writeable(inode);
4277 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
4279 nfs_setattr_update_inode(inode, sattr, fattr);
4280 nfs_setsecurity(inode, fattr, label);
4282 nfs4_label_free(label);
4286 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
4287 struct dentry *dentry, struct nfs_fh *fhandle,
4288 struct nfs_fattr *fattr, struct nfs4_label *label)
4290 struct nfs_server *server = NFS_SERVER(dir);
4292 struct nfs4_lookup_arg args = {
4293 .bitmask = server->attr_bitmask,
4294 .dir_fh = NFS_FH(dir),
4295 .name = &dentry->d_name,
4297 struct nfs4_lookup_res res = {
4303 struct rpc_message msg = {
4304 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4308 unsigned short task_flags = 0;
4310 /* Is this is an attribute revalidation, subject to softreval? */
4311 if (nfs_lookup_is_soft_revalidate(dentry))
4312 task_flags |= RPC_TASK_TIMEOUT;
4314 args.bitmask = nfs4_bitmask(server, label);
4316 nfs_fattr_init(fattr);
4318 dprintk("NFS call lookup %pd2\n", dentry);
4319 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4320 status = nfs4_do_call_sync(clnt, server, &msg,
4321 &args.seq_args, &res.seq_res, task_flags);
4322 dprintk("NFS reply lookup: %d\n", status);
4326 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
4328 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4329 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
4330 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4334 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
4335 struct dentry *dentry, struct nfs_fh *fhandle,
4336 struct nfs_fattr *fattr, struct nfs4_label *label)
4338 struct nfs4_exception exception = {
4339 .interruptible = true,
4341 struct rpc_clnt *client = *clnt;
4342 const struct qstr *name = &dentry->d_name;
4345 err = _nfs4_proc_lookup(client, dir, dentry, fhandle, fattr, label);
4346 trace_nfs4_lookup(dir, name, err);
4348 case -NFS4ERR_BADNAME:
4351 case -NFS4ERR_MOVED:
4352 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
4353 if (err == -NFS4ERR_MOVED)
4354 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4356 case -NFS4ERR_WRONGSEC:
4358 if (client != *clnt)
4360 client = nfs4_negotiate_security(client, dir, name);
4362 return PTR_ERR(client);
4364 exception.retry = 1;
4367 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4369 } while (exception.retry);
4374 else if (client != *clnt)
4375 rpc_shutdown_client(client);
4380 static int nfs4_proc_lookup(struct inode *dir, struct dentry *dentry,
4381 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4382 struct nfs4_label *label)
4385 struct rpc_clnt *client = NFS_CLIENT(dir);
4387 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr, label);
4388 if (client != NFS_CLIENT(dir)) {
4389 rpc_shutdown_client(client);
4390 nfs_fixup_secinfo_attributes(fattr);
4396 nfs4_proc_lookup_mountpoint(struct inode *dir, struct dentry *dentry,
4397 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4399 struct rpc_clnt *client = NFS_CLIENT(dir);
4402 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr, NULL);
4404 return ERR_PTR(status);
4405 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
4408 static int _nfs4_proc_lookupp(struct inode *inode,
4409 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4410 struct nfs4_label *label)
4412 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4413 struct nfs_server *server = NFS_SERVER(inode);
4415 struct nfs4_lookupp_arg args = {
4416 .bitmask = server->attr_bitmask,
4417 .fh = NFS_FH(inode),
4419 struct nfs4_lookupp_res res = {
4425 struct rpc_message msg = {
4426 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4430 unsigned short task_flags = 0;
4432 if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL)
4433 task_flags |= RPC_TASK_TIMEOUT;
4435 args.bitmask = nfs4_bitmask(server, label);
4437 nfs_fattr_init(fattr);
4439 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4440 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4441 &res.seq_res, task_flags);
4442 dprintk("NFS reply lookupp: %d\n", status);
4446 static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4447 struct nfs_fattr *fattr, struct nfs4_label *label)
4449 struct nfs4_exception exception = {
4450 .interruptible = true,
4454 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4455 trace_nfs4_lookupp(inode, err);
4456 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4458 } while (exception.retry);
4462 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4464 struct nfs_server *server = NFS_SERVER(inode);
4465 struct nfs4_accessargs args = {
4466 .fh = NFS_FH(inode),
4467 .access = entry->mask,
4469 struct nfs4_accessres res = {
4472 struct rpc_message msg = {
4473 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4476 .rpc_cred = entry->cred,
4480 if (!nfs4_have_delegation(inode, FMODE_READ)) {
4481 res.fattr = nfs_alloc_fattr();
4482 if (res.fattr == NULL)
4484 args.bitmask = server->cache_consistency_bitmask;
4486 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4488 nfs_access_set_mask(entry, res.access);
4490 nfs_refresh_inode(inode, res.fattr);
4492 nfs_free_fattr(res.fattr);
4496 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4498 struct nfs4_exception exception = {
4499 .interruptible = true,
4503 err = _nfs4_proc_access(inode, entry);
4504 trace_nfs4_access(inode, err);
4505 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4507 } while (exception.retry);
4512 * TODO: For the time being, we don't try to get any attributes
4513 * along with any of the zero-copy operations READ, READDIR,
4516 * In the case of the first three, we want to put the GETATTR
4517 * after the read-type operation -- this is because it is hard
4518 * to predict the length of a GETATTR response in v4, and thus
4519 * align the READ data correctly. This means that the GETATTR
4520 * may end up partially falling into the page cache, and we should
4521 * shift it into the 'tail' of the xdr_buf before processing.
4522 * To do this efficiently, we need to know the total length
4523 * of data received, which doesn't seem to be available outside
4526 * In the case of WRITE, we also want to put the GETATTR after
4527 * the operation -- in this case because we want to make sure
4528 * we get the post-operation mtime and size.
4530 * Both of these changes to the XDR layer would in fact be quite
4531 * minor, but I decided to leave them for a subsequent patch.
4533 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4534 unsigned int pgbase, unsigned int pglen)
4536 struct nfs4_readlink args = {
4537 .fh = NFS_FH(inode),
4542 struct nfs4_readlink_res res;
4543 struct rpc_message msg = {
4544 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4549 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
4552 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4553 unsigned int pgbase, unsigned int pglen)
4555 struct nfs4_exception exception = {
4556 .interruptible = true,
4560 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4561 trace_nfs4_readlink(inode, err);
4562 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4564 } while (exception.retry);
4569 * This is just for mknod. open(O_CREAT) will always do ->open_context().
4572 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
4575 struct nfs_server *server = NFS_SERVER(dir);
4576 struct nfs4_label l, *ilabel = NULL;
4577 struct nfs_open_context *ctx;
4578 struct nfs4_state *state;
4581 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
4583 return PTR_ERR(ctx);
4585 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4587 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4588 sattr->ia_mode &= ~current_umask();
4589 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
4590 if (IS_ERR(state)) {
4591 status = PTR_ERR(state);
4595 nfs4_label_release_security(ilabel);
4596 put_nfs_open_context(ctx);
4601 _nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
4603 struct nfs_server *server = NFS_SERVER(dir);
4604 struct nfs_removeargs args = {
4608 struct nfs_removeres res = {
4611 struct rpc_message msg = {
4612 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4616 unsigned long timestamp = jiffies;
4619 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
4621 spin_lock(&dir->i_lock);
4622 /* Removing a directory decrements nlink in the parent */
4623 if (ftype == NF4DIR && dir->i_nlink > 2)
4624 nfs4_dec_nlink_locked(dir);
4625 nfs4_update_changeattr_locked(dir, &res.cinfo, timestamp,
4626 NFS_INO_INVALID_DATA);
4627 spin_unlock(&dir->i_lock);
4632 static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4634 struct nfs4_exception exception = {
4635 .interruptible = true,
4637 struct inode *inode = d_inode(dentry);
4641 if (inode->i_nlink == 1)
4642 nfs4_inode_return_delegation(inode);
4644 nfs4_inode_make_writeable(inode);
4647 err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
4648 trace_nfs4_remove(dir, &dentry->d_name, err);
4649 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4651 } while (exception.retry);
4655 static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
4657 struct nfs4_exception exception = {
4658 .interruptible = true,
4663 err = _nfs4_proc_remove(dir, name, NF4DIR);
4664 trace_nfs4_remove(dir, name, err);
4665 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4667 } while (exception.retry);
4671 static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4672 struct dentry *dentry,
4673 struct inode *inode)
4675 struct nfs_removeargs *args = msg->rpc_argp;
4676 struct nfs_removeres *res = msg->rpc_resp;
4678 res->server = NFS_SB(dentry->d_sb);
4679 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
4680 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
4682 nfs_fattr_init(res->dir_attr);
4685 nfs4_inode_return_delegation(inode);
4688 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4690 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
4691 &data->args.seq_args,
4696 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
4698 struct nfs_unlinkdata *data = task->tk_calldata;
4699 struct nfs_removeres *res = &data->res;
4701 if (!nfs4_sequence_done(task, &res->seq_res))
4703 if (nfs4_async_handle_error(task, res->server, NULL,
4704 &data->timeout) == -EAGAIN)
4706 if (task->tk_status == 0)
4707 nfs4_update_changeattr(dir, &res->cinfo,
4708 res->dir_attr->time_start,
4709 NFS_INO_INVALID_DATA);
4713 static void nfs4_proc_rename_setup(struct rpc_message *msg,
4714 struct dentry *old_dentry,
4715 struct dentry *new_dentry)
4717 struct nfs_renameargs *arg = msg->rpc_argp;
4718 struct nfs_renameres *res = msg->rpc_resp;
4719 struct inode *old_inode = d_inode(old_dentry);
4720 struct inode *new_inode = d_inode(new_dentry);
4723 nfs4_inode_make_writeable(old_inode);
4725 nfs4_inode_return_delegation(new_inode);
4726 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
4727 res->server = NFS_SB(old_dentry->d_sb);
4728 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
4731 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4733 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
4734 &data->args.seq_args,
4739 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4740 struct inode *new_dir)
4742 struct nfs_renamedata *data = task->tk_calldata;
4743 struct nfs_renameres *res = &data->res;
4745 if (!nfs4_sequence_done(task, &res->seq_res))
4747 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
4750 if (task->tk_status == 0) {
4751 if (new_dir != old_dir) {
4752 /* Note: If we moved a directory, nlink will change */
4753 nfs4_update_changeattr(old_dir, &res->old_cinfo,
4754 res->old_fattr->time_start,
4755 NFS_INO_INVALID_NLINK |
4756 NFS_INO_INVALID_DATA);
4757 nfs4_update_changeattr(new_dir, &res->new_cinfo,
4758 res->new_fattr->time_start,
4759 NFS_INO_INVALID_NLINK |
4760 NFS_INO_INVALID_DATA);
4762 nfs4_update_changeattr(old_dir, &res->old_cinfo,
4763 res->old_fattr->time_start,
4764 NFS_INO_INVALID_DATA);
4769 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4771 struct nfs_server *server = NFS_SERVER(inode);
4772 __u32 bitmask[NFS4_BITMASK_SZ];
4773 struct nfs4_link_arg arg = {
4774 .fh = NFS_FH(inode),
4775 .dir_fh = NFS_FH(dir),
4779 struct nfs4_link_res res = {
4783 struct rpc_message msg = {
4784 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4788 int status = -ENOMEM;
4790 res.fattr = nfs_alloc_fattr();
4791 if (res.fattr == NULL)
4794 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4795 if (IS_ERR(res.label)) {
4796 status = PTR_ERR(res.label);
4800 nfs4_inode_make_writeable(inode);
4801 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, res.label), inode,
4802 NFS_INO_INVALID_CHANGE);
4803 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4805 nfs4_update_changeattr(dir, &res.cinfo, res.fattr->time_start,
4806 NFS_INO_INVALID_DATA);
4807 nfs4_inc_nlink(inode);
4808 status = nfs_post_op_update_inode(inode, res.fattr);
4810 nfs_setsecurity(inode, res.fattr, res.label);
4814 nfs4_label_free(res.label);
4817 nfs_free_fattr(res.fattr);
4821 static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4823 struct nfs4_exception exception = {
4824 .interruptible = true,
4828 err = nfs4_handle_exception(NFS_SERVER(inode),
4829 _nfs4_proc_link(inode, dir, name),
4831 } while (exception.retry);
4835 struct nfs4_createdata {
4836 struct rpc_message msg;
4837 struct nfs4_create_arg arg;
4838 struct nfs4_create_res res;
4840 struct nfs_fattr fattr;
4841 struct nfs4_label *label;
4844 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
4845 const struct qstr *name, struct iattr *sattr, u32 ftype)
4847 struct nfs4_createdata *data;
4849 data = kzalloc(sizeof(*data), GFP_KERNEL);
4851 struct nfs_server *server = NFS_SERVER(dir);
4853 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4854 if (IS_ERR(data->label))
4857 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4858 data->msg.rpc_argp = &data->arg;
4859 data->msg.rpc_resp = &data->res;
4860 data->arg.dir_fh = NFS_FH(dir);
4861 data->arg.server = server;
4862 data->arg.name = name;
4863 data->arg.attrs = sattr;
4864 data->arg.ftype = ftype;
4865 data->arg.bitmask = nfs4_bitmask(server, data->label);
4866 data->arg.umask = current_umask();
4867 data->res.server = server;
4868 data->res.fh = &data->fh;
4869 data->res.fattr = &data->fattr;
4870 data->res.label = data->label;
4871 nfs_fattr_init(data->res.fattr);
4879 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4881 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
4882 &data->arg.seq_args, &data->res.seq_res, 1);
4884 spin_lock(&dir->i_lock);
4885 /* Creating a directory bumps nlink in the parent */
4886 if (data->arg.ftype == NF4DIR)
4887 nfs4_inc_nlink_locked(dir);
4888 nfs4_update_changeattr_locked(dir, &data->res.dir_cinfo,
4889 data->res.fattr->time_start,
4890 NFS_INO_INVALID_DATA);
4891 spin_unlock(&dir->i_lock);
4892 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
4897 static void nfs4_free_createdata(struct nfs4_createdata *data)
4899 nfs4_label_free(data->label);
4903 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4904 struct page *page, unsigned int len, struct iattr *sattr,
4905 struct nfs4_label *label)
4907 struct nfs4_createdata *data;
4908 int status = -ENAMETOOLONG;
4910 if (len > NFS4_MAXPATHLEN)
4914 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4918 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4919 data->arg.u.symlink.pages = &page;
4920 data->arg.u.symlink.len = len;
4921 data->arg.label = label;
4923 status = nfs4_do_create(dir, dentry, data);
4925 nfs4_free_createdata(data);
4930 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4931 struct page *page, unsigned int len, struct iattr *sattr)
4933 struct nfs4_exception exception = {
4934 .interruptible = true,
4936 struct nfs4_label l, *label = NULL;
4939 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4942 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4943 trace_nfs4_symlink(dir, &dentry->d_name, err);
4944 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4946 } while (exception.retry);
4948 nfs4_label_release_security(label);
4952 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4953 struct iattr *sattr, struct nfs4_label *label)
4955 struct nfs4_createdata *data;
4956 int status = -ENOMEM;
4958 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4962 data->arg.label = label;
4963 status = nfs4_do_create(dir, dentry, data);
4965 nfs4_free_createdata(data);
4970 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4971 struct iattr *sattr)
4973 struct nfs_server *server = NFS_SERVER(dir);
4974 struct nfs4_exception exception = {
4975 .interruptible = true,
4977 struct nfs4_label l, *label = NULL;
4980 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4982 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4983 sattr->ia_mode &= ~current_umask();
4985 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4986 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4987 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4989 } while (exception.retry);
4990 nfs4_label_release_security(label);
4995 static int _nfs4_proc_readdir(struct nfs_readdir_arg *nr_arg,
4996 struct nfs_readdir_res *nr_res)
4998 struct inode *dir = d_inode(nr_arg->dentry);
4999 struct nfs_server *server = NFS_SERVER(dir);
5000 struct nfs4_readdir_arg args = {
5002 .pages = nr_arg->pages,
5004 .count = nr_arg->page_len,
5005 .plus = nr_arg->plus,
5007 struct nfs4_readdir_res res;
5008 struct rpc_message msg = {
5009 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
5012 .rpc_cred = nr_arg->cred,
5016 dprintk("%s: dentry = %pd2, cookie = %llu\n", __func__,
5017 nr_arg->dentry, (unsigned long long)nr_arg->cookie);
5018 if (!(server->caps & NFS_CAP_SECURITY_LABEL))
5019 args.bitmask = server->attr_bitmask_nl;
5021 args.bitmask = server->attr_bitmask;
5023 nfs4_setup_readdir(nr_arg->cookie, nr_arg->verf, nr_arg->dentry, &args);
5024 res.pgbase = args.pgbase;
5025 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
5028 memcpy(nr_res->verf, res.verifier.data, NFS4_VERIFIER_SIZE);
5029 status += args.pgbase;
5032 nfs_invalidate_atime(dir);
5034 dprintk("%s: returns %d\n", __func__, status);
5038 static int nfs4_proc_readdir(struct nfs_readdir_arg *arg,
5039 struct nfs_readdir_res *res)
5041 struct nfs4_exception exception = {
5042 .interruptible = true,
5046 err = _nfs4_proc_readdir(arg, res);
5047 trace_nfs4_readdir(d_inode(arg->dentry), err);
5048 err = nfs4_handle_exception(NFS_SERVER(d_inode(arg->dentry)),
5050 } while (exception.retry);
5054 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5055 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
5057 struct nfs4_createdata *data;
5058 int mode = sattr->ia_mode;
5059 int status = -ENOMEM;
5061 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
5066 data->arg.ftype = NF4FIFO;
5067 else if (S_ISBLK(mode)) {
5068 data->arg.ftype = NF4BLK;
5069 data->arg.u.device.specdata1 = MAJOR(rdev);
5070 data->arg.u.device.specdata2 = MINOR(rdev);
5072 else if (S_ISCHR(mode)) {
5073 data->arg.ftype = NF4CHR;
5074 data->arg.u.device.specdata1 = MAJOR(rdev);
5075 data->arg.u.device.specdata2 = MINOR(rdev);
5076 } else if (!S_ISSOCK(mode)) {
5081 data->arg.label = label;
5082 status = nfs4_do_create(dir, dentry, data);
5084 nfs4_free_createdata(data);
5089 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5090 struct iattr *sattr, dev_t rdev)
5092 struct nfs_server *server = NFS_SERVER(dir);
5093 struct nfs4_exception exception = {
5094 .interruptible = true,
5096 struct nfs4_label l, *label = NULL;
5099 label = nfs4_label_init_security(dir, dentry, sattr, &l);
5101 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
5102 sattr->ia_mode &= ~current_umask();
5104 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
5105 trace_nfs4_mknod(dir, &dentry->d_name, err);
5106 err = nfs4_handle_exception(NFS_SERVER(dir), err,
5108 } while (exception.retry);
5110 nfs4_label_release_security(label);
5115 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
5116 struct nfs_fsstat *fsstat)
5118 struct nfs4_statfs_arg args = {
5120 .bitmask = server->attr_bitmask,
5122 struct nfs4_statfs_res res = {
5125 struct rpc_message msg = {
5126 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
5131 nfs_fattr_init(fsstat->fattr);
5132 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5135 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
5137 struct nfs4_exception exception = {
5138 .interruptible = true,
5142 err = nfs4_handle_exception(server,
5143 _nfs4_proc_statfs(server, fhandle, fsstat),
5145 } while (exception.retry);
5149 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
5150 struct nfs_fsinfo *fsinfo)
5152 struct nfs4_fsinfo_arg args = {
5154 .bitmask = server->attr_bitmask,
5156 struct nfs4_fsinfo_res res = {
5159 struct rpc_message msg = {
5160 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
5165 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5168 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5170 struct nfs4_exception exception = {
5171 .interruptible = true,
5176 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
5177 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
5179 nfs4_set_lease_period(server->nfs_client, fsinfo->lease_time * HZ);
5182 err = nfs4_handle_exception(server, err, &exception);
5183 } while (exception.retry);
5187 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5191 nfs_fattr_init(fsinfo->fattr);
5192 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
5194 /* block layout checks this! */
5195 server->pnfs_blksize = fsinfo->blksize;
5196 set_pnfs_layoutdriver(server, fhandle, fsinfo);
5202 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5203 struct nfs_pathconf *pathconf)
5205 struct nfs4_pathconf_arg args = {
5207 .bitmask = server->attr_bitmask,
5209 struct nfs4_pathconf_res res = {
5210 .pathconf = pathconf,
5212 struct rpc_message msg = {
5213 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
5218 /* None of the pathconf attributes are mandatory to implement */
5219 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
5220 memset(pathconf, 0, sizeof(*pathconf));
5224 nfs_fattr_init(pathconf->fattr);
5225 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5228 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5229 struct nfs_pathconf *pathconf)
5231 struct nfs4_exception exception = {
5232 .interruptible = true,
5237 err = nfs4_handle_exception(server,
5238 _nfs4_proc_pathconf(server, fhandle, pathconf),
5240 } while (exception.retry);
5244 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
5245 const struct nfs_open_context *ctx,
5246 const struct nfs_lock_context *l_ctx,
5249 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
5251 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
5253 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
5254 const struct nfs_open_context *ctx,
5255 const struct nfs_lock_context *l_ctx,
5258 nfs4_stateid _current_stateid;
5260 /* If the current stateid represents a lost lock, then exit */
5261 if (nfs4_set_rw_stateid(&_current_stateid, ctx, l_ctx, fmode) == -EIO)
5263 return nfs4_stateid_match(stateid, &_current_stateid);
5266 static bool nfs4_error_stateid_expired(int err)
5269 case -NFS4ERR_DELEG_REVOKED:
5270 case -NFS4ERR_ADMIN_REVOKED:
5271 case -NFS4ERR_BAD_STATEID:
5272 case -NFS4ERR_STALE_STATEID:
5273 case -NFS4ERR_OLD_STATEID:
5274 case -NFS4ERR_OPENMODE:
5275 case -NFS4ERR_EXPIRED:
5281 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
5283 struct nfs_server *server = NFS_SERVER(hdr->inode);
5285 trace_nfs4_read(hdr, task->tk_status);
5286 if (task->tk_status < 0) {
5287 struct nfs4_exception exception = {
5288 .inode = hdr->inode,
5289 .state = hdr->args.context->state,
5290 .stateid = &hdr->args.stateid,
5292 task->tk_status = nfs4_async_handle_exception(task,
5293 server, task->tk_status, &exception);
5294 if (exception.retry) {
5295 rpc_restart_call_prepare(task);
5300 if (task->tk_status > 0)
5301 renew_lease(server, hdr->timestamp);
5305 static bool nfs4_read_stateid_changed(struct rpc_task *task,
5306 struct nfs_pgio_args *args)
5309 if (!nfs4_error_stateid_expired(task->tk_status) ||
5310 nfs4_stateid_is_current(&args->stateid,
5315 rpc_restart_call_prepare(task);
5319 static bool nfs4_read_plus_not_supported(struct rpc_task *task,
5320 struct nfs_pgio_header *hdr)
5322 struct nfs_server *server = NFS_SERVER(hdr->inode);
5323 struct rpc_message *msg = &task->tk_msg;
5325 if (msg->rpc_proc == &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS] &&
5326 server->caps & NFS_CAP_READ_PLUS && task->tk_status == -ENOTSUPP) {
5327 server->caps &= ~NFS_CAP_READ_PLUS;
5328 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5329 rpc_restart_call_prepare(task);
5335 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5337 dprintk("--> %s\n", __func__);
5339 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5341 if (nfs4_read_stateid_changed(task, &hdr->args))
5343 if (nfs4_read_plus_not_supported(task, hdr))
5345 if (task->tk_status > 0)
5346 nfs_invalidate_atime(hdr->inode);
5347 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5348 nfs4_read_done_cb(task, hdr);
5351 #if defined CONFIG_NFS_V4_2 && defined CONFIG_NFS_V4_2_READ_PLUS
5352 static void nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5353 struct rpc_message *msg)
5355 /* Note: We don't use READ_PLUS with pNFS yet */
5356 if (nfs_server_capable(hdr->inode, NFS_CAP_READ_PLUS) && !hdr->ds_clp)
5357 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS];
5360 static void nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5361 struct rpc_message *msg)
5364 #endif /* CONFIG_NFS_V4_2 */
5366 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5367 struct rpc_message *msg)
5369 hdr->timestamp = jiffies;
5370 if (!hdr->pgio_done_cb)
5371 hdr->pgio_done_cb = nfs4_read_done_cb;
5372 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5373 nfs42_read_plus_support(hdr, msg);
5374 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5377 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5378 struct nfs_pgio_header *hdr)
5380 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
5381 &hdr->args.seq_args,
5385 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5386 hdr->args.lock_context,
5387 hdr->rw_mode) == -EIO)
5389 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
5394 static int nfs4_write_done_cb(struct rpc_task *task,
5395 struct nfs_pgio_header *hdr)
5397 struct inode *inode = hdr->inode;
5399 trace_nfs4_write(hdr, task->tk_status);
5400 if (task->tk_status < 0) {
5401 struct nfs4_exception exception = {
5402 .inode = hdr->inode,
5403 .state = hdr->args.context->state,
5404 .stateid = &hdr->args.stateid,
5406 task->tk_status = nfs4_async_handle_exception(task,
5407 NFS_SERVER(inode), task->tk_status,
5409 if (exception.retry) {
5410 rpc_restart_call_prepare(task);
5414 if (task->tk_status >= 0) {
5415 renew_lease(NFS_SERVER(inode), hdr->timestamp);
5416 nfs_writeback_update_inode(hdr);
5421 static bool nfs4_write_stateid_changed(struct rpc_task *task,
5422 struct nfs_pgio_args *args)
5425 if (!nfs4_error_stateid_expired(task->tk_status) ||
5426 nfs4_stateid_is_current(&args->stateid,
5431 rpc_restart_call_prepare(task);
5435 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5437 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5439 if (nfs4_write_stateid_changed(task, &hdr->args))
5441 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5442 nfs4_write_done_cb(task, hdr);
5446 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
5448 /* Don't request attributes for pNFS or O_DIRECT writes */
5449 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
5451 /* Otherwise, request attributes if and only if we don't hold
5454 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
5457 static void nfs4_bitmask_set(__u32 bitmask[NFS4_BITMASK_SZ], const __u32 *src,
5458 struct inode *inode, struct nfs_server *server,
5459 struct nfs4_label *label)
5461 unsigned long cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
5464 memcpy(bitmask, src, sizeof(*bitmask) * NFS4_BITMASK_SZ);
5466 if (cache_validity & NFS_INO_INVALID_CHANGE)
5467 bitmask[0] |= FATTR4_WORD0_CHANGE;
5468 if (cache_validity & NFS_INO_INVALID_ATIME)
5469 bitmask[1] |= FATTR4_WORD1_TIME_ACCESS;
5470 if (cache_validity & NFS_INO_INVALID_MODE)
5471 bitmask[1] |= FATTR4_WORD1_MODE;
5472 if (cache_validity & NFS_INO_INVALID_OTHER)
5473 bitmask[1] |= FATTR4_WORD1_OWNER | FATTR4_WORD1_OWNER_GROUP;
5474 if (cache_validity & NFS_INO_INVALID_NLINK)
5475 bitmask[1] |= FATTR4_WORD1_NUMLINKS;
5476 if (label && label->len && cache_validity & NFS_INO_INVALID_LABEL)
5477 bitmask[2] |= FATTR4_WORD2_SECURITY_LABEL;
5478 if (cache_validity & NFS_INO_INVALID_CTIME)
5479 bitmask[1] |= FATTR4_WORD1_TIME_METADATA;
5480 if (cache_validity & NFS_INO_INVALID_MTIME)
5481 bitmask[1] |= FATTR4_WORD1_TIME_MODIFY;
5482 if (cache_validity & NFS_INO_INVALID_BLOCKS)
5483 bitmask[1] |= FATTR4_WORD1_SPACE_USED;
5485 if (cache_validity & NFS_INO_INVALID_SIZE)
5486 bitmask[0] |= FATTR4_WORD0_SIZE;
5488 for (i = 0; i < NFS4_BITMASK_SZ; i++)
5489 bitmask[i] &= server->attr_bitmask[i];
5492 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
5493 struct rpc_message *msg,
5494 struct rpc_clnt **clnt)
5496 struct nfs_server *server = NFS_SERVER(hdr->inode);
5498 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5499 hdr->args.bitmask = NULL;
5500 hdr->res.fattr = NULL;
5502 nfs4_bitmask_set(hdr->args.bitmask_store,
5503 server->cache_consistency_bitmask,
5504 hdr->inode, server, NULL);
5505 hdr->args.bitmask = hdr->args.bitmask_store;
5508 if (!hdr->pgio_done_cb)
5509 hdr->pgio_done_cb = nfs4_write_done_cb;
5510 hdr->res.server = server;
5511 hdr->timestamp = jiffies;
5513 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
5514 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5515 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
5518 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5520 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
5521 &data->args.seq_args,
5526 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
5528 struct inode *inode = data->inode;
5530 trace_nfs4_commit(data, task->tk_status);
5531 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5532 NULL, NULL) == -EAGAIN) {
5533 rpc_restart_call_prepare(task);
5539 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
5541 if (!nfs4_sequence_done(task, &data->res.seq_res))
5543 return data->commit_done_cb(task, data);
5546 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5547 struct rpc_clnt **clnt)
5549 struct nfs_server *server = NFS_SERVER(data->inode);
5551 if (data->commit_done_cb == NULL)
5552 data->commit_done_cb = nfs4_commit_done_cb;
5553 data->res.server = server;
5554 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
5555 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
5556 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
5559 static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5560 struct nfs_commitres *res)
5562 struct inode *dst_inode = file_inode(dst);
5563 struct nfs_server *server = NFS_SERVER(dst_inode);
5564 struct rpc_message msg = {
5565 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5570 args->fh = NFS_FH(dst_inode);
5571 return nfs4_call_sync(server->client, server, &msg,
5572 &args->seq_args, &res->seq_res, 1);
5575 int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5577 struct nfs_commitargs args = {
5581 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5582 struct nfs4_exception exception = { };
5586 status = _nfs4_proc_commit(dst, &args, res);
5587 status = nfs4_handle_exception(dst_server, status, &exception);
5588 } while (exception.retry);
5593 struct nfs4_renewdata {
5594 struct nfs_client *client;
5595 unsigned long timestamp;
5599 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5600 * standalone procedure for queueing an asynchronous RENEW.
5602 static void nfs4_renew_release(void *calldata)
5604 struct nfs4_renewdata *data = calldata;
5605 struct nfs_client *clp = data->client;
5607 if (refcount_read(&clp->cl_count) > 1)
5608 nfs4_schedule_state_renewal(clp);
5609 nfs_put_client(clp);
5613 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
5615 struct nfs4_renewdata *data = calldata;
5616 struct nfs_client *clp = data->client;
5617 unsigned long timestamp = data->timestamp;
5619 trace_nfs4_renew_async(clp, task->tk_status);
5620 switch (task->tk_status) {
5623 case -NFS4ERR_LEASE_MOVED:
5624 nfs4_schedule_lease_moved_recovery(clp);
5627 /* Unless we're shutting down, schedule state recovery! */
5628 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5630 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
5631 nfs4_schedule_lease_recovery(clp);
5634 nfs4_schedule_path_down_recovery(clp);
5636 do_renew_lease(clp, timestamp);
5639 static const struct rpc_call_ops nfs4_renew_ops = {
5640 .rpc_call_done = nfs4_renew_done,
5641 .rpc_release = nfs4_renew_release,
5644 static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
5646 struct rpc_message msg = {
5647 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5651 struct nfs4_renewdata *data;
5653 if (renew_flags == 0)
5655 if (!refcount_inc_not_zero(&clp->cl_count))
5657 data = kmalloc(sizeof(*data), GFP_NOFS);
5659 nfs_put_client(clp);
5663 data->timestamp = jiffies;
5664 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
5665 &nfs4_renew_ops, data);
5668 static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
5670 struct rpc_message msg = {
5671 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5675 unsigned long now = jiffies;
5678 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5681 do_renew_lease(clp, now);
5685 static inline int nfs4_server_supports_acls(struct nfs_server *server)
5687 return server->caps & NFS_CAP_ACLS;
5690 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5691 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
5694 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
5696 int nfs4_buf_to_pages_noslab(const void *buf, size_t buflen,
5697 struct page **pages)
5699 struct page *newpage, **spages;
5705 len = min_t(size_t, PAGE_SIZE, buflen);
5706 newpage = alloc_page(GFP_KERNEL);
5708 if (newpage == NULL)
5710 memcpy(page_address(newpage), buf, len);
5715 } while (buflen != 0);
5721 __free_page(spages[rc-1]);
5725 struct nfs4_cached_acl {
5731 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
5733 struct nfs_inode *nfsi = NFS_I(inode);
5735 spin_lock(&inode->i_lock);
5736 kfree(nfsi->nfs4_acl);
5737 nfsi->nfs4_acl = acl;
5738 spin_unlock(&inode->i_lock);
5741 static void nfs4_zap_acl_attr(struct inode *inode)
5743 nfs4_set_cached_acl(inode, NULL);
5746 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5748 struct nfs_inode *nfsi = NFS_I(inode);
5749 struct nfs4_cached_acl *acl;
5752 spin_lock(&inode->i_lock);
5753 acl = nfsi->nfs4_acl;
5756 if (buf == NULL) /* user is just asking for length */
5758 if (acl->cached == 0)
5760 ret = -ERANGE; /* see getxattr(2) man page */
5761 if (acl->len > buflen)
5763 memcpy(buf, acl->data, acl->len);
5767 spin_unlock(&inode->i_lock);
5771 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
5773 struct nfs4_cached_acl *acl;
5774 size_t buflen = sizeof(*acl) + acl_len;
5776 if (buflen <= PAGE_SIZE) {
5777 acl = kmalloc(buflen, GFP_KERNEL);
5781 _copy_from_pages(acl->data, pages, pgbase, acl_len);
5783 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5790 nfs4_set_cached_acl(inode, acl);
5794 * The getxattr API returns the required buffer length when called with a
5795 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5796 * the required buf. On a NULL buf, we send a page of data to the server
5797 * guessing that the ACL request can be serviced by a page. If so, we cache
5798 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5799 * the cache. If not so, we throw away the page, and cache the required
5800 * length. The next getxattr call will then produce another round trip to
5801 * the server, this time with the input buf of the required size.
5803 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5805 struct page **pages;
5806 struct nfs_getaclargs args = {
5807 .fh = NFS_FH(inode),
5810 struct nfs_getaclres res = {
5813 struct rpc_message msg = {
5814 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5818 unsigned int npages;
5819 int ret = -ENOMEM, i;
5820 struct nfs_server *server = NFS_SERVER(inode);
5823 buflen = server->rsize;
5825 npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
5826 pages = kmalloc_array(npages, sizeof(struct page *), GFP_NOFS);
5830 args.acl_pages = pages;
5832 for (i = 0; i < npages; i++) {
5833 pages[i] = alloc_page(GFP_KERNEL);
5838 /* for decoding across pages */
5839 res.acl_scratch = alloc_page(GFP_KERNEL);
5840 if (!res.acl_scratch)
5843 args.acl_len = npages * PAGE_SIZE;
5845 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
5846 __func__, buf, buflen, npages, args.acl_len);
5847 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5848 &msg, &args.seq_args, &res.seq_res, 0);
5852 /* Handle the case where the passed-in buffer is too short */
5853 if (res.acl_flags & NFS4_ACL_TRUNC) {
5854 /* Did the user only issue a request for the acl length? */
5860 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
5862 if (res.acl_len > buflen) {
5866 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
5871 for (i = 0; i < npages; i++)
5873 __free_page(pages[i]);
5874 if (res.acl_scratch)
5875 __free_page(res.acl_scratch);
5880 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5882 struct nfs4_exception exception = {
5883 .interruptible = true,
5887 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
5888 trace_nfs4_get_acl(inode, ret);
5891 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5892 } while (exception.retry);
5896 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5898 struct nfs_server *server = NFS_SERVER(inode);
5901 if (!nfs4_server_supports_acls(server))
5903 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
5906 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5907 nfs_zap_acl_cache(inode);
5908 ret = nfs4_read_cached_acl(inode, buf, buflen);
5910 /* -ENOENT is returned if there is no ACL or if there is an ACL
5911 * but no cached acl data, just the acl length */
5913 return nfs4_get_acl_uncached(inode, buf, buflen);
5916 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5918 struct nfs_server *server = NFS_SERVER(inode);
5919 struct page *pages[NFS4ACL_MAXPAGES];
5920 struct nfs_setaclargs arg = {
5921 .fh = NFS_FH(inode),
5925 struct nfs_setaclres res;
5926 struct rpc_message msg = {
5927 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5931 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
5934 /* You can't remove system.nfs4_acl: */
5937 if (!nfs4_server_supports_acls(server))
5939 if (npages > ARRAY_SIZE(pages))
5941 i = nfs4_buf_to_pages_noslab(buf, buflen, arg.acl_pages);
5944 nfs4_inode_make_writeable(inode);
5945 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5948 * Free each page after tx, so the only ref left is
5949 * held by the network stack
5952 put_page(pages[i-1]);
5955 * Acl update can result in inode attribute update.
5956 * so mark the attribute cache invalid.
5958 spin_lock(&inode->i_lock);
5959 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
5960 NFS_INO_INVALID_CTIME |
5961 NFS_INO_REVAL_FORCED);
5962 spin_unlock(&inode->i_lock);
5963 nfs_access_zap_cache(inode);
5964 nfs_zap_acl_cache(inode);
5968 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5970 struct nfs4_exception exception = { };
5973 err = __nfs4_proc_set_acl(inode, buf, buflen);
5974 trace_nfs4_set_acl(inode, err);
5975 if (err == -NFS4ERR_BADOWNER || err == -NFS4ERR_BADNAME) {
5977 * no need to retry since the kernel
5978 * isn't involved in encoding the ACEs.
5983 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5985 } while (exception.retry);
5989 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
5990 static int _nfs4_get_security_label(struct inode *inode, void *buf,
5993 struct nfs_server *server = NFS_SERVER(inode);
5994 struct nfs_fattr fattr;
5995 struct nfs4_label label = {0, 0, buflen, buf};
5997 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5998 struct nfs4_getattr_arg arg = {
5999 .fh = NFS_FH(inode),
6002 struct nfs4_getattr_res res = {
6007 struct rpc_message msg = {
6008 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
6014 nfs_fattr_init(&fattr);
6016 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
6019 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
6024 static int nfs4_get_security_label(struct inode *inode, void *buf,
6027 struct nfs4_exception exception = {
6028 .interruptible = true,
6032 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
6036 err = _nfs4_get_security_label(inode, buf, buflen);
6037 trace_nfs4_get_security_label(inode, err);
6038 err = nfs4_handle_exception(NFS_SERVER(inode), err,
6040 } while (exception.retry);
6044 static int _nfs4_do_set_security_label(struct inode *inode,
6045 struct nfs4_label *ilabel,
6046 struct nfs_fattr *fattr,
6047 struct nfs4_label *olabel)
6050 struct iattr sattr = {0};
6051 struct nfs_server *server = NFS_SERVER(inode);
6052 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
6053 struct nfs_setattrargs arg = {
6054 .fh = NFS_FH(inode),
6060 struct nfs_setattrres res = {
6065 struct rpc_message msg = {
6066 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
6072 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
6074 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6076 dprintk("%s failed: %d\n", __func__, status);
6081 static int nfs4_do_set_security_label(struct inode *inode,
6082 struct nfs4_label *ilabel,
6083 struct nfs_fattr *fattr,
6084 struct nfs4_label *olabel)
6086 struct nfs4_exception exception = { };
6090 err = _nfs4_do_set_security_label(inode, ilabel,
6092 trace_nfs4_set_security_label(inode, err);
6093 err = nfs4_handle_exception(NFS_SERVER(inode), err,
6095 } while (exception.retry);
6100 nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
6102 struct nfs4_label ilabel, *olabel = NULL;
6103 struct nfs_fattr fattr;
6106 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
6109 nfs_fattr_init(&fattr);
6113 ilabel.label = (char *)buf;
6114 ilabel.len = buflen;
6116 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
6117 if (IS_ERR(olabel)) {
6118 status = -PTR_ERR(olabel);
6122 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
6124 nfs_setsecurity(inode, &fattr, olabel);
6126 nfs4_label_free(olabel);
6130 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
6133 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
6134 nfs4_verifier *bootverf)
6138 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
6139 /* An impossible timestamp guarantees this value
6140 * will never match a generated boot time. */
6141 verf[0] = cpu_to_be32(U32_MAX);
6142 verf[1] = cpu_to_be32(U32_MAX);
6144 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
6145 u64 ns = ktime_to_ns(nn->boot_time);
6147 verf[0] = cpu_to_be32(ns >> 32);
6148 verf[1] = cpu_to_be32(ns);
6150 memcpy(bootverf->data, verf, sizeof(bootverf->data));
6154 nfs4_get_uniquifier(struct nfs_client *clp, char *buf, size_t buflen)
6156 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
6157 struct nfs_netns_client *nn_clp = nn->nfs_client;
6164 id = rcu_dereference(nn_clp->identifier);
6166 strscpy(buf, id, buflen);
6170 if (nfs4_client_id_uniquifier[0] != '\0' && buf[0] == '\0')
6171 strscpy(buf, nfs4_client_id_uniquifier, buflen);
6177 nfs4_init_nonuniform_client_string(struct nfs_client *clp)
6179 char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6184 if (clp->cl_owner_id != NULL)
6189 strlen(clp->cl_rpcclient->cl_nodename) +
6191 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
6195 buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
6199 if (len > NFS4_OPAQUE_LIMIT + 1)
6203 * Since this string is allocated at mount time, and held until the
6204 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6205 * about a memory-reclaim deadlock.
6207 str = kmalloc(len, GFP_KERNEL);
6213 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
6214 clp->cl_rpcclient->cl_nodename, buf,
6215 rpc_peeraddr2str(clp->cl_rpcclient,
6218 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
6219 clp->cl_rpcclient->cl_nodename,
6220 rpc_peeraddr2str(clp->cl_rpcclient,
6224 clp->cl_owner_id = str;
6229 nfs4_init_uniform_client_string(struct nfs_client *clp)
6231 char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6236 if (clp->cl_owner_id != NULL)
6239 len = 10 + 10 + 1 + 10 + 1 +
6240 strlen(clp->cl_rpcclient->cl_nodename) + 1;
6242 buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
6246 if (len > NFS4_OPAQUE_LIMIT + 1)
6250 * Since this string is allocated at mount time, and held until the
6251 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6252 * about a memory-reclaim deadlock.
6254 str = kmalloc(len, GFP_KERNEL);
6259 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
6260 clp->rpc_ops->version, clp->cl_minorversion,
6261 buf, clp->cl_rpcclient->cl_nodename);
6263 scnprintf(str, len, "Linux NFSv%u.%u %s",
6264 clp->rpc_ops->version, clp->cl_minorversion,
6265 clp->cl_rpcclient->cl_nodename);
6266 clp->cl_owner_id = str;
6271 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
6272 * services. Advertise one based on the address family of the
6276 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
6278 if (strchr(clp->cl_ipaddr, ':') != NULL)
6279 return scnprintf(buf, len, "tcp6");
6281 return scnprintf(buf, len, "tcp");
6284 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
6286 struct nfs4_setclientid *sc = calldata;
6288 if (task->tk_status == 0)
6289 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
6292 static const struct rpc_call_ops nfs4_setclientid_ops = {
6293 .rpc_call_done = nfs4_setclientid_done,
6297 * nfs4_proc_setclientid - Negotiate client ID
6298 * @clp: state data structure
6299 * @program: RPC program for NFSv4 callback service
6300 * @port: IP port number for NFS4 callback service
6301 * @cred: credential to use for this call
6302 * @res: where to place the result
6304 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6306 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
6307 unsigned short port, const struct cred *cred,
6308 struct nfs4_setclientid_res *res)
6310 nfs4_verifier sc_verifier;
6311 struct nfs4_setclientid setclientid = {
6312 .sc_verifier = &sc_verifier,
6316 struct rpc_message msg = {
6317 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
6318 .rpc_argp = &setclientid,
6322 struct rpc_task_setup task_setup_data = {
6323 .rpc_client = clp->cl_rpcclient,
6324 .rpc_message = &msg,
6325 .callback_ops = &nfs4_setclientid_ops,
6326 .callback_data = &setclientid,
6327 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
6329 unsigned long now = jiffies;
6332 /* nfs_client_id4 */
6333 nfs4_init_boot_verifier(clp, &sc_verifier);
6335 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
6336 status = nfs4_init_uniform_client_string(clp);
6338 status = nfs4_init_nonuniform_client_string(clp);
6344 setclientid.sc_netid_len =
6345 nfs4_init_callback_netid(clp,
6346 setclientid.sc_netid,
6347 sizeof(setclientid.sc_netid));
6348 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
6349 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
6350 clp->cl_ipaddr, port >> 8, port & 255);
6352 dprintk("NFS call setclientid auth=%s, '%s'\n",
6353 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6356 status = nfs4_call_sync_custom(&task_setup_data);
6357 if (setclientid.sc_cred) {
6358 kfree(clp->cl_acceptor);
6359 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
6360 put_rpccred(setclientid.sc_cred);
6364 do_renew_lease(clp, now);
6366 trace_nfs4_setclientid(clp, status);
6367 dprintk("NFS reply setclientid: %d\n", status);
6372 * nfs4_proc_setclientid_confirm - Confirm client ID
6373 * @clp: state data structure
6374 * @arg: result of a previous SETCLIENTID
6375 * @cred: credential to use for this call
6377 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6379 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
6380 struct nfs4_setclientid_res *arg,
6381 const struct cred *cred)
6383 struct rpc_message msg = {
6384 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
6390 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
6391 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6393 status = rpc_call_sync(clp->cl_rpcclient, &msg,
6394 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
6395 trace_nfs4_setclientid_confirm(clp, status);
6396 dprintk("NFS reply setclientid_confirm: %d\n", status);
6400 struct nfs4_delegreturndata {
6401 struct nfs4_delegreturnargs args;
6402 struct nfs4_delegreturnres res;
6404 nfs4_stateid stateid;
6405 unsigned long timestamp;
6407 struct nfs4_layoutreturn_args arg;
6408 struct nfs4_layoutreturn_res res;
6409 struct nfs4_xdr_opaque_data ld_private;
6413 struct nfs_fattr fattr;
6415 struct inode *inode;
6418 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6420 struct nfs4_delegreturndata *data = calldata;
6421 struct nfs4_exception exception = {
6422 .inode = data->inode,
6423 .stateid = &data->stateid,
6424 .task_is_privileged = data->args.seq_args.sa_privileged,
6427 if (!nfs4_sequence_done(task, &data->res.seq_res))
6430 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
6432 /* Handle Layoutreturn errors */
6433 if (pnfs_roc_done(task, &data->args.lr_args, &data->res.lr_res,
6434 &data->res.lr_ret) == -EAGAIN)
6437 switch (task->tk_status) {
6439 renew_lease(data->res.server, data->timestamp);
6441 case -NFS4ERR_ADMIN_REVOKED:
6442 case -NFS4ERR_DELEG_REVOKED:
6443 case -NFS4ERR_EXPIRED:
6444 nfs4_free_revoked_stateid(data->res.server,
6446 task->tk_msg.rpc_cred);
6448 case -NFS4ERR_BAD_STATEID:
6449 case -NFS4ERR_STALE_STATEID:
6451 task->tk_status = 0;
6453 case -NFS4ERR_OLD_STATEID:
6454 if (!nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6455 nfs4_stateid_seqid_inc(&data->stateid);
6456 if (data->args.bitmask) {
6457 data->args.bitmask = NULL;
6458 data->res.fattr = NULL;
6461 case -NFS4ERR_ACCESS:
6462 if (data->args.bitmask) {
6463 data->args.bitmask = NULL;
6464 data->res.fattr = NULL;
6469 task->tk_status = nfs4_async_handle_exception(task,
6470 data->res.server, task->tk_status,
6472 if (exception.retry)
6475 nfs_delegation_mark_returned(data->inode, data->args.stateid);
6476 data->rpc_status = task->tk_status;
6479 task->tk_status = 0;
6480 rpc_restart_call_prepare(task);
6483 static void nfs4_delegreturn_release(void *calldata)
6485 struct nfs4_delegreturndata *data = calldata;
6486 struct inode *inode = data->inode;
6489 pnfs_roc_release(&data->lr.arg, &data->lr.res,
6492 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
6493 nfs_iput_and_deactive(inode);
6498 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6500 struct nfs4_delegreturndata *d_data;
6501 struct pnfs_layout_hdr *lo;
6503 d_data = (struct nfs4_delegreturndata *)data;
6505 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task)) {
6506 nfs4_sequence_done(task, &d_data->res.seq_res);
6510 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6511 if (lo && !pnfs_layout_is_valid(lo)) {
6512 d_data->args.lr_args = NULL;
6513 d_data->res.lr_res = NULL;
6516 nfs4_setup_sequence(d_data->res.server->nfs_client,
6517 &d_data->args.seq_args,
6518 &d_data->res.seq_res,
6522 static const struct rpc_call_ops nfs4_delegreturn_ops = {
6523 .rpc_call_prepare = nfs4_delegreturn_prepare,
6524 .rpc_call_done = nfs4_delegreturn_done,
6525 .rpc_release = nfs4_delegreturn_release,
6528 static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6530 struct nfs4_delegreturndata *data;
6531 struct nfs_server *server = NFS_SERVER(inode);
6532 struct rpc_task *task;
6533 struct rpc_message msg = {
6534 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6537 struct rpc_task_setup task_setup_data = {
6538 .rpc_client = server->client,
6539 .rpc_message = &msg,
6540 .callback_ops = &nfs4_delegreturn_ops,
6541 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
6545 data = kzalloc(sizeof(*data), GFP_NOFS);
6549 nfs4_state_protect(server->nfs_client,
6550 NFS_SP4_MACH_CRED_CLEANUP,
6551 &task_setup_data.rpc_client, &msg);
6553 data->args.fhandle = &data->fh;
6554 data->args.stateid = &data->stateid;
6555 nfs4_bitmask_set(data->args.bitmask_store,
6556 server->cache_consistency_bitmask, inode, server,
6558 data->args.bitmask = data->args.bitmask_store;
6559 nfs_copy_fh(&data->fh, NFS_FH(inode));
6560 nfs4_stateid_copy(&data->stateid, stateid);
6561 data->res.fattr = &data->fattr;
6562 data->res.server = server;
6563 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
6564 data->lr.arg.ld_private = &data->lr.ld_private;
6565 nfs_fattr_init(data->res.fattr);
6566 data->timestamp = jiffies;
6567 data->rpc_status = 0;
6568 data->inode = nfs_igrab_and_active(inode);
6569 if (data->inode || issync) {
6570 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res,
6573 data->args.lr_args = &data->lr.arg;
6574 data->res.lr_res = &data->lr.res;
6579 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1,
6582 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1,
6584 task_setup_data.callback_data = data;
6585 msg.rpc_argp = &data->args;
6586 msg.rpc_resp = &data->res;
6587 task = rpc_run_task(&task_setup_data);
6589 return PTR_ERR(task);
6592 status = rpc_wait_for_completion_task(task);
6595 status = data->rpc_status;
6601 int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6603 struct nfs_server *server = NFS_SERVER(inode);
6604 struct nfs4_exception exception = { };
6607 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
6608 trace_nfs4_delegreturn(inode, stateid, err);
6610 case -NFS4ERR_STALE_STATEID:
6611 case -NFS4ERR_EXPIRED:
6615 err = nfs4_handle_exception(server, err, &exception);
6616 } while (exception.retry);
6620 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6622 struct inode *inode = state->inode;
6623 struct nfs_server *server = NFS_SERVER(inode);
6624 struct nfs_client *clp = server->nfs_client;
6625 struct nfs_lockt_args arg = {
6626 .fh = NFS_FH(inode),
6629 struct nfs_lockt_res res = {
6632 struct rpc_message msg = {
6633 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
6636 .rpc_cred = state->owner->so_cred,
6638 struct nfs4_lock_state *lsp;
6641 arg.lock_owner.clientid = clp->cl_clientid;
6642 status = nfs4_set_lock_state(state, request);
6645 lsp = request->fl_u.nfs4_fl.owner;
6646 arg.lock_owner.id = lsp->ls_seqid.owner_id;
6647 arg.lock_owner.s_dev = server->s_dev;
6648 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6651 request->fl_type = F_UNLCK;
6653 case -NFS4ERR_DENIED:
6656 request->fl_ops->fl_release_private(request);
6657 request->fl_ops = NULL;
6662 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6664 struct nfs4_exception exception = {
6665 .interruptible = true,
6670 err = _nfs4_proc_getlk(state, cmd, request);
6671 trace_nfs4_get_lock(request, state, cmd, err);
6672 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
6674 } while (exception.retry);
6679 * Update the seqid of a lock stateid after receiving
6680 * NFS4ERR_OLD_STATEID
6682 static bool nfs4_refresh_lock_old_stateid(nfs4_stateid *dst,
6683 struct nfs4_lock_state *lsp)
6685 struct nfs4_state *state = lsp->ls_state;
6688 spin_lock(&state->state_lock);
6689 if (!nfs4_stateid_match_other(dst, &lsp->ls_stateid))
6691 if (!nfs4_stateid_is_newer(&lsp->ls_stateid, dst))
6692 nfs4_stateid_seqid_inc(dst);
6694 dst->seqid = lsp->ls_stateid.seqid;
6697 spin_unlock(&state->state_lock);
6701 static bool nfs4_sync_lock_stateid(nfs4_stateid *dst,
6702 struct nfs4_lock_state *lsp)
6704 struct nfs4_state *state = lsp->ls_state;
6707 spin_lock(&state->state_lock);
6708 ret = !nfs4_stateid_match_other(dst, &lsp->ls_stateid);
6709 nfs4_stateid_copy(dst, &lsp->ls_stateid);
6710 spin_unlock(&state->state_lock);
6714 struct nfs4_unlockdata {
6715 struct nfs_locku_args arg;
6716 struct nfs_locku_res res;
6717 struct nfs4_lock_state *lsp;
6718 struct nfs_open_context *ctx;
6719 struct nfs_lock_context *l_ctx;
6720 struct file_lock fl;
6721 struct nfs_server *server;
6722 unsigned long timestamp;
6725 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6726 struct nfs_open_context *ctx,
6727 struct nfs4_lock_state *lsp,
6728 struct nfs_seqid *seqid)
6730 struct nfs4_unlockdata *p;
6731 struct nfs4_state *state = lsp->ls_state;
6732 struct inode *inode = state->inode;
6734 p = kzalloc(sizeof(*p), GFP_NOFS);
6737 p->arg.fh = NFS_FH(inode);
6739 p->arg.seqid = seqid;
6740 p->res.seqid = seqid;
6742 /* Ensure we don't close file until we're done freeing locks! */
6743 p->ctx = get_nfs_open_context(ctx);
6744 p->l_ctx = nfs_get_lock_context(ctx);
6745 locks_init_lock(&p->fl);
6746 locks_copy_lock(&p->fl, fl);
6747 p->server = NFS_SERVER(inode);
6748 spin_lock(&state->state_lock);
6749 nfs4_stateid_copy(&p->arg.stateid, &lsp->ls_stateid);
6750 spin_unlock(&state->state_lock);
6754 static void nfs4_locku_release_calldata(void *data)
6756 struct nfs4_unlockdata *calldata = data;
6757 nfs_free_seqid(calldata->arg.seqid);
6758 nfs4_put_lock_state(calldata->lsp);
6759 nfs_put_lock_context(calldata->l_ctx);
6760 put_nfs_open_context(calldata->ctx);
6764 static void nfs4_locku_done(struct rpc_task *task, void *data)
6766 struct nfs4_unlockdata *calldata = data;
6767 struct nfs4_exception exception = {
6768 .inode = calldata->lsp->ls_state->inode,
6769 .stateid = &calldata->arg.stateid,
6772 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6774 switch (task->tk_status) {
6776 renew_lease(calldata->server, calldata->timestamp);
6777 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
6778 if (nfs4_update_lock_stateid(calldata->lsp,
6779 &calldata->res.stateid))
6782 case -NFS4ERR_ADMIN_REVOKED:
6783 case -NFS4ERR_EXPIRED:
6784 nfs4_free_revoked_stateid(calldata->server,
6785 &calldata->arg.stateid,
6786 task->tk_msg.rpc_cred);
6788 case -NFS4ERR_BAD_STATEID:
6789 case -NFS4ERR_STALE_STATEID:
6790 if (nfs4_sync_lock_stateid(&calldata->arg.stateid,
6792 rpc_restart_call_prepare(task);
6794 case -NFS4ERR_OLD_STATEID:
6795 if (nfs4_refresh_lock_old_stateid(&calldata->arg.stateid,
6797 rpc_restart_call_prepare(task);
6800 task->tk_status = nfs4_async_handle_exception(task,
6801 calldata->server, task->tk_status,
6803 if (exception.retry)
6804 rpc_restart_call_prepare(task);
6806 nfs_release_seqid(calldata->arg.seqid);
6809 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
6811 struct nfs4_unlockdata *calldata = data;
6813 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6814 nfs_async_iocounter_wait(task, calldata->l_ctx))
6817 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
6819 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
6820 /* Note: exit _without_ running nfs4_locku_done */
6823 calldata->timestamp = jiffies;
6824 if (nfs4_setup_sequence(calldata->server->nfs_client,
6825 &calldata->arg.seq_args,
6826 &calldata->res.seq_res,
6828 nfs_release_seqid(calldata->arg.seqid);
6831 task->tk_action = NULL;
6833 nfs4_sequence_done(task, &calldata->res.seq_res);
6836 static const struct rpc_call_ops nfs4_locku_ops = {
6837 .rpc_call_prepare = nfs4_locku_prepare,
6838 .rpc_call_done = nfs4_locku_done,
6839 .rpc_release = nfs4_locku_release_calldata,
6842 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6843 struct nfs_open_context *ctx,
6844 struct nfs4_lock_state *lsp,
6845 struct nfs_seqid *seqid)
6847 struct nfs4_unlockdata *data;
6848 struct rpc_message msg = {
6849 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6850 .rpc_cred = ctx->cred,
6852 struct rpc_task_setup task_setup_data = {
6853 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
6854 .rpc_message = &msg,
6855 .callback_ops = &nfs4_locku_ops,
6856 .workqueue = nfsiod_workqueue,
6857 .flags = RPC_TASK_ASYNC,
6860 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6861 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6863 /* Ensure this is an unlock - when canceling a lock, the
6864 * canceled lock is passed in, and it won't be an unlock.
6866 fl->fl_type = F_UNLCK;
6867 if (fl->fl_flags & FL_CLOSE)
6868 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
6870 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6872 nfs_free_seqid(seqid);
6873 return ERR_PTR(-ENOMEM);
6876 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
6877 msg.rpc_argp = &data->arg;
6878 msg.rpc_resp = &data->res;
6879 task_setup_data.callback_data = data;
6880 return rpc_run_task(&task_setup_data);
6883 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6885 struct inode *inode = state->inode;
6886 struct nfs4_state_owner *sp = state->owner;
6887 struct nfs_inode *nfsi = NFS_I(inode);
6888 struct nfs_seqid *seqid;
6889 struct nfs4_lock_state *lsp;
6890 struct rpc_task *task;
6891 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6893 unsigned char fl_flags = request->fl_flags;
6895 status = nfs4_set_lock_state(state, request);
6896 /* Unlock _before_ we do the RPC call */
6897 request->fl_flags |= FL_EXISTS;
6898 /* Exclude nfs_delegation_claim_locks() */
6899 mutex_lock(&sp->so_delegreturn_mutex);
6900 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
6901 down_read(&nfsi->rwsem);
6902 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
6903 up_read(&nfsi->rwsem);
6904 mutex_unlock(&sp->so_delegreturn_mutex);
6907 up_read(&nfsi->rwsem);
6908 mutex_unlock(&sp->so_delegreturn_mutex);
6911 /* Is this a delegated lock? */
6912 lsp = request->fl_u.nfs4_fl.owner;
6913 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6915 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6916 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
6920 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
6921 status = PTR_ERR(task);
6924 status = rpc_wait_for_completion_task(task);
6927 request->fl_flags = fl_flags;
6928 trace_nfs4_unlock(request, state, F_SETLK, status);
6932 struct nfs4_lockdata {
6933 struct nfs_lock_args arg;
6934 struct nfs_lock_res res;
6935 struct nfs4_lock_state *lsp;
6936 struct nfs_open_context *ctx;
6937 struct file_lock fl;
6938 unsigned long timestamp;
6941 struct nfs_server *server;
6944 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
6945 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6948 struct nfs4_lockdata *p;
6949 struct inode *inode = lsp->ls_state->inode;
6950 struct nfs_server *server = NFS_SERVER(inode);
6951 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6953 p = kzalloc(sizeof(*p), gfp_mask);
6957 p->arg.fh = NFS_FH(inode);
6959 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
6960 if (IS_ERR(p->arg.open_seqid))
6962 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6963 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
6964 if (IS_ERR(p->arg.lock_seqid))
6965 goto out_free_seqid;
6966 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
6967 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
6968 p->arg.lock_owner.s_dev = server->s_dev;
6969 p->res.lock_seqid = p->arg.lock_seqid;
6972 p->ctx = get_nfs_open_context(ctx);
6973 locks_init_lock(&p->fl);
6974 locks_copy_lock(&p->fl, fl);
6977 nfs_free_seqid(p->arg.open_seqid);
6983 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6985 struct nfs4_lockdata *data = calldata;
6986 struct nfs4_state *state = data->lsp->ls_state;
6988 dprintk("%s: begin!\n", __func__);
6989 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
6991 /* Do we need to do an open_to_lock_owner? */
6992 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
6993 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
6994 goto out_release_lock_seqid;
6996 nfs4_stateid_copy(&data->arg.open_stateid,
6997 &state->open_stateid);
6998 data->arg.new_lock_owner = 1;
6999 data->res.open_seqid = data->arg.open_seqid;
7001 data->arg.new_lock_owner = 0;
7002 nfs4_stateid_copy(&data->arg.lock_stateid,
7003 &data->lsp->ls_stateid);
7005 if (!nfs4_valid_open_stateid(state)) {
7006 data->rpc_status = -EBADF;
7007 task->tk_action = NULL;
7008 goto out_release_open_seqid;
7010 data->timestamp = jiffies;
7011 if (nfs4_setup_sequence(data->server->nfs_client,
7012 &data->arg.seq_args,
7016 out_release_open_seqid:
7017 nfs_release_seqid(data->arg.open_seqid);
7018 out_release_lock_seqid:
7019 nfs_release_seqid(data->arg.lock_seqid);
7021 nfs4_sequence_done(task, &data->res.seq_res);
7022 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
7025 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
7027 struct nfs4_lockdata *data = calldata;
7028 struct nfs4_lock_state *lsp = data->lsp;
7030 dprintk("%s: begin!\n", __func__);
7032 if (!nfs4_sequence_done(task, &data->res.seq_res))
7035 data->rpc_status = task->tk_status;
7036 switch (task->tk_status) {
7038 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
7040 if (data->arg.new_lock && !data->cancelled) {
7041 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
7042 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
7045 if (data->arg.new_lock_owner != 0) {
7046 nfs_confirm_seqid(&lsp->ls_seqid, 0);
7047 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
7048 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
7049 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
7052 case -NFS4ERR_BAD_STATEID:
7053 case -NFS4ERR_OLD_STATEID:
7054 case -NFS4ERR_STALE_STATEID:
7055 case -NFS4ERR_EXPIRED:
7056 if (data->arg.new_lock_owner != 0) {
7057 if (!nfs4_stateid_match(&data->arg.open_stateid,
7058 &lsp->ls_state->open_stateid))
7060 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
7065 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
7068 if (!data->cancelled)
7069 rpc_restart_call_prepare(task);
7073 static void nfs4_lock_release(void *calldata)
7075 struct nfs4_lockdata *data = calldata;
7077 dprintk("%s: begin!\n", __func__);
7078 nfs_free_seqid(data->arg.open_seqid);
7079 if (data->cancelled && data->rpc_status == 0) {
7080 struct rpc_task *task;
7081 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
7082 data->arg.lock_seqid);
7084 rpc_put_task_async(task);
7085 dprintk("%s: cancelling lock!\n", __func__);
7087 nfs_free_seqid(data->arg.lock_seqid);
7088 nfs4_put_lock_state(data->lsp);
7089 put_nfs_open_context(data->ctx);
7091 dprintk("%s: done!\n", __func__);
7094 static const struct rpc_call_ops nfs4_lock_ops = {
7095 .rpc_call_prepare = nfs4_lock_prepare,
7096 .rpc_call_done = nfs4_lock_done,
7097 .rpc_release = nfs4_lock_release,
7100 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
7103 case -NFS4ERR_ADMIN_REVOKED:
7104 case -NFS4ERR_EXPIRED:
7105 case -NFS4ERR_BAD_STATEID:
7106 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
7107 if (new_lock_owner != 0 ||
7108 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
7109 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
7111 case -NFS4ERR_STALE_STATEID:
7112 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
7113 nfs4_schedule_lease_recovery(server->nfs_client);
7117 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
7119 struct nfs4_lockdata *data;
7120 struct rpc_task *task;
7121 struct rpc_message msg = {
7122 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
7123 .rpc_cred = state->owner->so_cred,
7125 struct rpc_task_setup task_setup_data = {
7126 .rpc_client = NFS_CLIENT(state->inode),
7127 .rpc_message = &msg,
7128 .callback_ops = &nfs4_lock_ops,
7129 .workqueue = nfsiod_workqueue,
7130 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
7134 dprintk("%s: begin!\n", __func__);
7135 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
7136 fl->fl_u.nfs4_fl.owner,
7137 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
7141 data->arg.block = 1;
7142 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
7143 recovery_type > NFS_LOCK_NEW);
7144 msg.rpc_argp = &data->arg;
7145 msg.rpc_resp = &data->res;
7146 task_setup_data.callback_data = data;
7147 if (recovery_type > NFS_LOCK_NEW) {
7148 if (recovery_type == NFS_LOCK_RECLAIM)
7149 data->arg.reclaim = NFS_LOCK_RECLAIM;
7151 data->arg.new_lock = 1;
7152 task = rpc_run_task(&task_setup_data);
7154 return PTR_ERR(task);
7155 ret = rpc_wait_for_completion_task(task);
7157 ret = data->rpc_status;
7159 nfs4_handle_setlk_error(data->server, data->lsp,
7160 data->arg.new_lock_owner, ret);
7162 data->cancelled = true;
7163 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
7165 dprintk("%s: done, ret = %d!\n", __func__, ret);
7169 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
7171 struct nfs_server *server = NFS_SERVER(state->inode);
7172 struct nfs4_exception exception = {
7173 .inode = state->inode,
7178 /* Cache the lock if possible... */
7179 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7181 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
7182 if (err != -NFS4ERR_DELAY)
7184 nfs4_handle_exception(server, err, &exception);
7185 } while (exception.retry);
7189 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
7191 struct nfs_server *server = NFS_SERVER(state->inode);
7192 struct nfs4_exception exception = {
7193 .inode = state->inode,
7197 err = nfs4_set_lock_state(state, request);
7200 if (!recover_lost_locks) {
7201 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
7205 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7207 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
7211 case -NFS4ERR_GRACE:
7212 case -NFS4ERR_DELAY:
7213 nfs4_handle_exception(server, err, &exception);
7216 } while (exception.retry);
7221 #if defined(CONFIG_NFS_V4_1)
7222 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
7224 struct nfs4_lock_state *lsp;
7227 status = nfs4_set_lock_state(state, request);
7230 lsp = request->fl_u.nfs4_fl.owner;
7231 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
7232 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
7234 return nfs4_lock_expired(state, request);
7238 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7240 struct nfs_inode *nfsi = NFS_I(state->inode);
7241 struct nfs4_state_owner *sp = state->owner;
7242 unsigned char fl_flags = request->fl_flags;
7245 request->fl_flags |= FL_ACCESS;
7246 status = locks_lock_inode_wait(state->inode, request);
7249 mutex_lock(&sp->so_delegreturn_mutex);
7250 down_read(&nfsi->rwsem);
7251 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
7252 /* Yes: cache locks! */
7253 /* ...but avoid races with delegation recall... */
7254 request->fl_flags = fl_flags & ~FL_SLEEP;
7255 status = locks_lock_inode_wait(state->inode, request);
7256 up_read(&nfsi->rwsem);
7257 mutex_unlock(&sp->so_delegreturn_mutex);
7260 up_read(&nfsi->rwsem);
7261 mutex_unlock(&sp->so_delegreturn_mutex);
7262 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
7264 request->fl_flags = fl_flags;
7268 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7270 struct nfs4_exception exception = {
7272 .inode = state->inode,
7273 .interruptible = true,
7278 err = _nfs4_proc_setlk(state, cmd, request);
7279 if (err == -NFS4ERR_DENIED)
7281 err = nfs4_handle_exception(NFS_SERVER(state->inode),
7283 } while (exception.retry);
7287 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
7288 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
7291 nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
7292 struct file_lock *request)
7294 int status = -ERESTARTSYS;
7295 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
7297 while(!signalled()) {
7298 status = nfs4_proc_setlk(state, cmd, request);
7299 if ((status != -EAGAIN) || IS_SETLK(cmd))
7301 freezable_schedule_timeout_interruptible(timeout);
7303 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
7304 status = -ERESTARTSYS;
7309 #ifdef CONFIG_NFS_V4_1
7310 struct nfs4_lock_waiter {
7311 struct inode *inode;
7312 struct nfs_lowner owner;
7313 wait_queue_entry_t wait;
7317 nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
7319 struct nfs4_lock_waiter *waiter =
7320 container_of(wait, struct nfs4_lock_waiter, wait);
7322 /* NULL key means to wake up everyone */
7324 struct cb_notify_lock_args *cbnl = key;
7325 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
7326 *wowner = &waiter->owner;
7328 /* Only wake if the callback was for the same owner. */
7329 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
7332 /* Make sure it's for the right inode */
7333 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
7337 return woken_wake_function(wait, mode, flags, key);
7341 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7343 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
7344 struct nfs_server *server = NFS_SERVER(state->inode);
7345 struct nfs_client *clp = server->nfs_client;
7346 wait_queue_head_t *q = &clp->cl_lock_waitq;
7347 struct nfs4_lock_waiter waiter = {
7348 .inode = state->inode,
7349 .owner = { .clientid = clp->cl_clientid,
7350 .id = lsp->ls_seqid.owner_id,
7351 .s_dev = server->s_dev },
7355 /* Don't bother with waitqueue if we don't expect a callback */
7356 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
7357 return nfs4_retry_setlk_simple(state, cmd, request);
7359 init_wait(&waiter.wait);
7360 waiter.wait.func = nfs4_wake_lock_waiter;
7361 add_wait_queue(q, &waiter.wait);
7364 status = nfs4_proc_setlk(state, cmd, request);
7365 if (status != -EAGAIN || IS_SETLK(cmd))
7368 status = -ERESTARTSYS;
7369 freezer_do_not_count();
7370 wait_woken(&waiter.wait, TASK_INTERRUPTIBLE,
7371 NFS4_LOCK_MAXTIMEOUT);
7373 } while (!signalled());
7375 remove_wait_queue(q, &waiter.wait);
7379 #else /* !CONFIG_NFS_V4_1 */
7381 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7383 return nfs4_retry_setlk_simple(state, cmd, request);
7388 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
7390 struct nfs_open_context *ctx;
7391 struct nfs4_state *state;
7394 /* verify open state */
7395 ctx = nfs_file_open_context(filp);
7398 if (IS_GETLK(cmd)) {
7400 return nfs4_proc_getlk(state, F_GETLK, request);
7404 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
7407 if (request->fl_type == F_UNLCK) {
7409 return nfs4_proc_unlck(state, cmd, request);
7416 if ((request->fl_flags & FL_POSIX) &&
7417 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
7421 * Don't rely on the VFS having checked the file open mode,
7422 * since it won't do this for flock() locks.
7424 switch (request->fl_type) {
7426 if (!(filp->f_mode & FMODE_READ))
7430 if (!(filp->f_mode & FMODE_WRITE))
7434 status = nfs4_set_lock_state(state, request);
7438 return nfs4_retry_setlk(state, cmd, request);
7441 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
7443 struct nfs_server *server = NFS_SERVER(state->inode);
7446 err = nfs4_set_lock_state(state, fl);
7450 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
7451 if (err != -NFS4ERR_DELAY)
7454 } while (err == -NFS4ERR_DELAY);
7455 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
7458 struct nfs_release_lockowner_data {
7459 struct nfs4_lock_state *lsp;
7460 struct nfs_server *server;
7461 struct nfs_release_lockowner_args args;
7462 struct nfs_release_lockowner_res res;
7463 unsigned long timestamp;
7466 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7468 struct nfs_release_lockowner_data *data = calldata;
7469 struct nfs_server *server = data->server;
7470 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7471 &data->res.seq_res, task);
7472 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7473 data->timestamp = jiffies;
7476 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7478 struct nfs_release_lockowner_data *data = calldata;
7479 struct nfs_server *server = data->server;
7481 nfs40_sequence_done(task, &data->res.seq_res);
7483 switch (task->tk_status) {
7485 renew_lease(server, data->timestamp);
7487 case -NFS4ERR_STALE_CLIENTID:
7488 case -NFS4ERR_EXPIRED:
7489 nfs4_schedule_lease_recovery(server->nfs_client);
7491 case -NFS4ERR_LEASE_MOVED:
7492 case -NFS4ERR_DELAY:
7493 if (nfs4_async_handle_error(task, server,
7494 NULL, NULL) == -EAGAIN)
7495 rpc_restart_call_prepare(task);
7499 static void nfs4_release_lockowner_release(void *calldata)
7501 struct nfs_release_lockowner_data *data = calldata;
7502 nfs4_free_lock_state(data->server, data->lsp);
7506 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
7507 .rpc_call_prepare = nfs4_release_lockowner_prepare,
7508 .rpc_call_done = nfs4_release_lockowner_done,
7509 .rpc_release = nfs4_release_lockowner_release,
7513 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
7515 struct nfs_release_lockowner_data *data;
7516 struct rpc_message msg = {
7517 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7520 if (server->nfs_client->cl_mvops->minor_version != 0)
7523 data = kmalloc(sizeof(*data), GFP_NOFS);
7527 data->server = server;
7528 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7529 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7530 data->args.lock_owner.s_dev = server->s_dev;
7532 msg.rpc_argp = &data->args;
7533 msg.rpc_resp = &data->res;
7534 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
7535 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
7538 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7540 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
7541 struct user_namespace *mnt_userns,
7542 struct dentry *unused, struct inode *inode,
7543 const char *key, const void *buf,
7544 size_t buflen, int flags)
7546 return nfs4_proc_set_acl(inode, buf, buflen);
7549 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
7550 struct dentry *unused, struct inode *inode,
7551 const char *key, void *buf, size_t buflen)
7553 return nfs4_proc_get_acl(inode, buf, buflen);
7556 static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
7558 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
7561 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
7563 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
7564 struct user_namespace *mnt_userns,
7565 struct dentry *unused, struct inode *inode,
7566 const char *key, const void *buf,
7567 size_t buflen, int flags)
7569 if (security_ismaclabel(key))
7570 return nfs4_set_security_label(inode, buf, buflen);
7575 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
7576 struct dentry *unused, struct inode *inode,
7577 const char *key, void *buf, size_t buflen)
7579 if (security_ismaclabel(key))
7580 return nfs4_get_security_label(inode, buf, buflen);
7585 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7589 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7590 len = security_inode_listsecurity(inode, list, list_len);
7591 if (len >= 0 && list_len && len > list_len)
7597 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7598 .prefix = XATTR_SECURITY_PREFIX,
7599 .get = nfs4_xattr_get_nfs4_label,
7600 .set = nfs4_xattr_set_nfs4_label,
7606 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7613 #ifdef CONFIG_NFS_V4_2
7614 static int nfs4_xattr_set_nfs4_user(const struct xattr_handler *handler,
7615 struct user_namespace *mnt_userns,
7616 struct dentry *unused, struct inode *inode,
7617 const char *key, const void *buf,
7618 size_t buflen, int flags)
7620 struct nfs_access_entry cache;
7623 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7627 * There is no mapping from the MAY_* flags to the NFS_ACCESS_XA*
7628 * flags right now. Handling of xattr operations use the normal
7629 * file read/write permissions.
7631 * Just in case the server has other ideas (which RFC 8276 allows),
7632 * do a cached access check for the XA* flags to possibly avoid
7633 * doing an RPC and getting EACCES back.
7635 if (!nfs_access_get_cached(inode, current_cred(), &cache, true)) {
7636 if (!(cache.mask & NFS_ACCESS_XAWRITE))
7641 ret = nfs42_proc_removexattr(inode, key);
7643 nfs4_xattr_cache_remove(inode, key);
7645 ret = nfs42_proc_setxattr(inode, key, buf, buflen, flags);
7647 nfs4_xattr_cache_add(inode, key, buf, NULL, buflen);
7653 static int nfs4_xattr_get_nfs4_user(const struct xattr_handler *handler,
7654 struct dentry *unused, struct inode *inode,
7655 const char *key, void *buf, size_t buflen)
7657 struct nfs_access_entry cache;
7660 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7663 if (!nfs_access_get_cached(inode, current_cred(), &cache, true)) {
7664 if (!(cache.mask & NFS_ACCESS_XAREAD))
7668 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
7672 ret = nfs4_xattr_cache_get(inode, key, buf, buflen);
7673 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7676 ret = nfs42_proc_getxattr(inode, key, buf, buflen);
7682 nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7689 struct nfs_access_entry cache;
7691 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7694 if (!nfs_access_get_cached(inode, current_cred(), &cache, true)) {
7695 if (!(cache.mask & NFS_ACCESS_XALIST))
7699 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
7703 ret = nfs4_xattr_cache_list(inode, list, list_len);
7704 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7709 buflen = list_len ? list_len : XATTR_LIST_MAX;
7710 buf = list_len ? list : NULL;
7714 ret = nfs42_proc_listxattrs(inode, buf, buflen,
7727 nfs4_xattr_cache_set_list(inode, list, size);
7735 nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7739 #endif /* CONFIG_NFS_V4_2 */
7742 * nfs_fhget will use either the mounted_on_fileid or the fileid
7744 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7746 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7747 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7748 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
7749 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
7752 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
7753 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
7754 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7758 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7759 const struct qstr *name,
7760 struct nfs4_fs_locations *fs_locations,
7763 struct nfs_server *server = NFS_SERVER(dir);
7765 struct nfs4_fs_locations_arg args = {
7766 .dir_fh = NFS_FH(dir),
7771 struct nfs4_fs_locations_res res = {
7772 .fs_locations = fs_locations,
7774 struct rpc_message msg = {
7775 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7781 dprintk("%s: start\n", __func__);
7783 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7784 bitmask[1] = nfs4_fattr_bitmap[1];
7786 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7787 * is not supported */
7788 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
7789 bitmask[0] &= ~FATTR4_WORD0_FILEID;
7791 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
7793 nfs_fattr_init(&fs_locations->fattr);
7794 fs_locations->server = server;
7795 fs_locations->nlocations = 0;
7796 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
7797 dprintk("%s: returned status = %d\n", __func__, status);
7801 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7802 const struct qstr *name,
7803 struct nfs4_fs_locations *fs_locations,
7806 struct nfs4_exception exception = {
7807 .interruptible = true,
7811 err = _nfs4_proc_fs_locations(client, dir, name,
7812 fs_locations, page);
7813 trace_nfs4_get_fs_locations(dir, name, err);
7814 err = nfs4_handle_exception(NFS_SERVER(dir), err,
7816 } while (exception.retry);
7821 * This operation also signals the server that this client is
7822 * performing migration recovery. The server can stop returning
7823 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7824 * appended to this compound to identify the client ID which is
7825 * performing recovery.
7827 static int _nfs40_proc_get_locations(struct inode *inode,
7828 struct nfs4_fs_locations *locations,
7829 struct page *page, const struct cred *cred)
7831 struct nfs_server *server = NFS_SERVER(inode);
7832 struct rpc_clnt *clnt = server->client;
7834 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7836 struct nfs4_fs_locations_arg args = {
7837 .clientid = server->nfs_client->cl_clientid,
7838 .fh = NFS_FH(inode),
7841 .migration = 1, /* skip LOOKUP */
7842 .renew = 1, /* append RENEW */
7844 struct nfs4_fs_locations_res res = {
7845 .fs_locations = locations,
7849 struct rpc_message msg = {
7850 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7855 unsigned long now = jiffies;
7858 nfs_fattr_init(&locations->fattr);
7859 locations->server = server;
7860 locations->nlocations = 0;
7862 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7863 status = nfs4_call_sync_sequence(clnt, server, &msg,
7864 &args.seq_args, &res.seq_res);
7868 renew_lease(server, now);
7872 #ifdef CONFIG_NFS_V4_1
7875 * This operation also signals the server that this client is
7876 * performing migration recovery. The server can stop asserting
7877 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7878 * performing this operation is identified in the SEQUENCE
7879 * operation in this compound.
7881 * When the client supports GETATTR(fs_locations_info), it can
7882 * be plumbed in here.
7884 static int _nfs41_proc_get_locations(struct inode *inode,
7885 struct nfs4_fs_locations *locations,
7886 struct page *page, const struct cred *cred)
7888 struct nfs_server *server = NFS_SERVER(inode);
7889 struct rpc_clnt *clnt = server->client;
7891 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7893 struct nfs4_fs_locations_arg args = {
7894 .fh = NFS_FH(inode),
7897 .migration = 1, /* skip LOOKUP */
7899 struct nfs4_fs_locations_res res = {
7900 .fs_locations = locations,
7903 struct rpc_message msg = {
7904 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7911 nfs_fattr_init(&locations->fattr);
7912 locations->server = server;
7913 locations->nlocations = 0;
7915 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7916 status = nfs4_call_sync_sequence(clnt, server, &msg,
7917 &args.seq_args, &res.seq_res);
7918 if (status == NFS4_OK &&
7919 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7920 status = -NFS4ERR_LEASE_MOVED;
7924 #endif /* CONFIG_NFS_V4_1 */
7927 * nfs4_proc_get_locations - discover locations for a migrated FSID
7928 * @inode: inode on FSID that is migrating
7929 * @locations: result of query
7931 * @cred: credential to use for this operation
7933 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7934 * operation failed, or a negative errno if a local error occurred.
7936 * On success, "locations" is filled in, but if the server has
7937 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7940 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7941 * from this client that require migration recovery.
7943 int nfs4_proc_get_locations(struct inode *inode,
7944 struct nfs4_fs_locations *locations,
7945 struct page *page, const struct cred *cred)
7947 struct nfs_server *server = NFS_SERVER(inode);
7948 struct nfs_client *clp = server->nfs_client;
7949 const struct nfs4_mig_recovery_ops *ops =
7950 clp->cl_mvops->mig_recovery_ops;
7951 struct nfs4_exception exception = {
7952 .interruptible = true,
7956 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7957 (unsigned long long)server->fsid.major,
7958 (unsigned long long)server->fsid.minor,
7960 nfs_display_fhandle(NFS_FH(inode), __func__);
7963 status = ops->get_locations(inode, locations, page, cred);
7964 if (status != -NFS4ERR_DELAY)
7966 nfs4_handle_exception(server, status, &exception);
7967 } while (exception.retry);
7972 * This operation also signals the server that this client is
7973 * performing "lease moved" recovery. The server can stop
7974 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7975 * is appended to this compound to identify the client ID which is
7976 * performing recovery.
7978 static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
7980 struct nfs_server *server = NFS_SERVER(inode);
7981 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7982 struct rpc_clnt *clnt = server->client;
7983 struct nfs4_fsid_present_arg args = {
7984 .fh = NFS_FH(inode),
7985 .clientid = clp->cl_clientid,
7986 .renew = 1, /* append RENEW */
7988 struct nfs4_fsid_present_res res = {
7991 struct rpc_message msg = {
7992 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7997 unsigned long now = jiffies;
8000 res.fh = nfs_alloc_fhandle();
8004 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8005 status = nfs4_call_sync_sequence(clnt, server, &msg,
8006 &args.seq_args, &res.seq_res);
8007 nfs_free_fhandle(res.fh);
8011 do_renew_lease(clp, now);
8015 #ifdef CONFIG_NFS_V4_1
8018 * This operation also signals the server that this client is
8019 * performing "lease moved" recovery. The server can stop asserting
8020 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
8021 * this operation is identified in the SEQUENCE operation in this
8024 static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
8026 struct nfs_server *server = NFS_SERVER(inode);
8027 struct rpc_clnt *clnt = server->client;
8028 struct nfs4_fsid_present_arg args = {
8029 .fh = NFS_FH(inode),
8031 struct nfs4_fsid_present_res res = {
8033 struct rpc_message msg = {
8034 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
8041 res.fh = nfs_alloc_fhandle();
8045 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8046 status = nfs4_call_sync_sequence(clnt, server, &msg,
8047 &args.seq_args, &res.seq_res);
8048 nfs_free_fhandle(res.fh);
8049 if (status == NFS4_OK &&
8050 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
8051 status = -NFS4ERR_LEASE_MOVED;
8055 #endif /* CONFIG_NFS_V4_1 */
8058 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
8059 * @inode: inode on FSID to check
8060 * @cred: credential to use for this operation
8062 * Server indicates whether the FSID is present, moved, or not
8063 * recognized. This operation is necessary to clear a LEASE_MOVED
8064 * condition for this client ID.
8066 * Returns NFS4_OK if the FSID is present on this server,
8067 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
8068 * NFS4ERR code if some error occurred on the server, or a
8069 * negative errno if a local failure occurred.
8071 int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
8073 struct nfs_server *server = NFS_SERVER(inode);
8074 struct nfs_client *clp = server->nfs_client;
8075 const struct nfs4_mig_recovery_ops *ops =
8076 clp->cl_mvops->mig_recovery_ops;
8077 struct nfs4_exception exception = {
8078 .interruptible = true,
8082 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
8083 (unsigned long long)server->fsid.major,
8084 (unsigned long long)server->fsid.minor,
8086 nfs_display_fhandle(NFS_FH(inode), __func__);
8089 status = ops->fsid_present(inode, cred);
8090 if (status != -NFS4ERR_DELAY)
8092 nfs4_handle_exception(server, status, &exception);
8093 } while (exception.retry);
8098 * If 'use_integrity' is true and the state managment nfs_client
8099 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
8100 * and the machine credential as per RFC3530bis and RFC5661 Security
8101 * Considerations sections. Otherwise, just use the user cred with the
8102 * filesystem's rpc_client.
8104 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
8107 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
8108 struct nfs_client *clp = NFS_SERVER(dir)->nfs_client;
8109 struct nfs4_secinfo_arg args = {
8110 .dir_fh = NFS_FH(dir),
8113 struct nfs4_secinfo_res res = {
8116 struct rpc_message msg = {
8117 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
8121 struct nfs4_call_sync_data data = {
8122 .seq_server = NFS_SERVER(dir),
8123 .seq_args = &args.seq_args,
8124 .seq_res = &res.seq_res,
8126 struct rpc_task_setup task_setup = {
8128 .rpc_message = &msg,
8129 .callback_ops = clp->cl_mvops->call_sync_ops,
8130 .callback_data = &data,
8131 .flags = RPC_TASK_NO_ROUND_ROBIN,
8133 const struct cred *cred = NULL;
8135 if (use_integrity) {
8136 clnt = clp->cl_rpcclient;
8137 task_setup.rpc_client = clnt;
8139 cred = nfs4_get_clid_cred(clp);
8140 msg.rpc_cred = cred;
8143 dprintk("NFS call secinfo %s\n", name->name);
8145 nfs4_state_protect(clp, NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
8146 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
8147 status = nfs4_call_sync_custom(&task_setup);
8149 dprintk("NFS reply secinfo: %d\n", status);
8155 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
8156 struct nfs4_secinfo_flavors *flavors)
8158 struct nfs4_exception exception = {
8159 .interruptible = true,
8163 err = -NFS4ERR_WRONGSEC;
8165 /* try to use integrity protection with machine cred */
8166 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
8167 err = _nfs4_proc_secinfo(dir, name, flavors, true);
8170 * if unable to use integrity protection, or SECINFO with
8171 * integrity protection returns NFS4ERR_WRONGSEC (which is
8172 * disallowed by spec, but exists in deployed servers) use
8173 * the current filesystem's rpc_client and the user cred.
8175 if (err == -NFS4ERR_WRONGSEC)
8176 err = _nfs4_proc_secinfo(dir, name, flavors, false);
8178 trace_nfs4_secinfo(dir, name, err);
8179 err = nfs4_handle_exception(NFS_SERVER(dir), err,
8181 } while (exception.retry);
8185 #ifdef CONFIG_NFS_V4_1
8187 * Check the exchange flags returned by the server for invalid flags, having
8188 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
8191 static int nfs4_check_cl_exchange_flags(u32 flags, u32 version)
8193 if (version >= 2 && (flags & ~EXCHGID4_2_FLAG_MASK_R))
8195 else if (version < 2 && (flags & ~EXCHGID4_FLAG_MASK_R))
8197 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
8198 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
8200 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
8204 return -NFS4ERR_INVAL;
8208 nfs41_same_server_scope(struct nfs41_server_scope *a,
8209 struct nfs41_server_scope *b)
8211 if (a->server_scope_sz != b->server_scope_sz)
8213 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
8217 nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
8219 struct nfs41_bind_conn_to_session_args *args = task->tk_msg.rpc_argp;
8220 struct nfs41_bind_conn_to_session_res *res = task->tk_msg.rpc_resp;
8221 struct nfs_client *clp = args->client;
8223 switch (task->tk_status) {
8224 case -NFS4ERR_BADSESSION:
8225 case -NFS4ERR_DEADSESSION:
8226 nfs4_schedule_session_recovery(clp->cl_session,
8229 if (args->dir == NFS4_CDFC4_FORE_OR_BOTH &&
8230 res->dir != NFS4_CDFS4_BOTH) {
8231 rpc_task_close_connection(task);
8232 if (args->retries++ < MAX_BIND_CONN_TO_SESSION_RETRIES)
8233 rpc_restart_call(task);
8237 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
8238 .rpc_call_done = nfs4_bind_one_conn_to_session_done,
8242 * nfs4_proc_bind_one_conn_to_session()
8244 * The 4.1 client currently uses the same TCP connection for the
8245 * fore and backchannel.
8248 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
8249 struct rpc_xprt *xprt,
8250 struct nfs_client *clp,
8251 const struct cred *cred)
8254 struct nfs41_bind_conn_to_session_args args = {
8256 .dir = NFS4_CDFC4_FORE_OR_BOTH,
8259 struct nfs41_bind_conn_to_session_res res;
8260 struct rpc_message msg = {
8262 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
8267 struct rpc_task_setup task_setup_data = {
8270 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
8271 .rpc_message = &msg,
8272 .flags = RPC_TASK_TIMEOUT,
8274 struct rpc_task *task;
8276 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
8277 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
8278 args.dir = NFS4_CDFC4_FORE;
8280 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
8281 if (xprt != rcu_access_pointer(clnt->cl_xprt))
8282 args.dir = NFS4_CDFC4_FORE;
8284 task = rpc_run_task(&task_setup_data);
8285 if (!IS_ERR(task)) {
8286 status = task->tk_status;
8289 status = PTR_ERR(task);
8290 trace_nfs4_bind_conn_to_session(clp, status);
8292 if (memcmp(res.sessionid.data,
8293 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
8294 dprintk("NFS: %s: Session ID mismatch\n", __func__);
8297 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
8298 dprintk("NFS: %s: Unexpected direction from server\n",
8302 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
8303 dprintk("NFS: %s: Server returned RDMA mode = true\n",
8312 struct rpc_bind_conn_calldata {
8313 struct nfs_client *clp;
8314 const struct cred *cred;
8318 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
8319 struct rpc_xprt *xprt,
8322 struct rpc_bind_conn_calldata *p = calldata;
8324 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
8327 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
8329 struct rpc_bind_conn_calldata data = {
8333 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
8334 nfs4_proc_bind_conn_to_session_callback, &data);
8338 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
8339 * and operations we'd like to see to enable certain features in the allow map
8341 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
8342 .how = SP4_MACH_CRED,
8343 .enforce.u.words = {
8344 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8345 1 << (OP_EXCHANGE_ID - 32) |
8346 1 << (OP_CREATE_SESSION - 32) |
8347 1 << (OP_DESTROY_SESSION - 32) |
8348 1 << (OP_DESTROY_CLIENTID - 32)
8351 [0] = 1 << (OP_CLOSE) |
8352 1 << (OP_OPEN_DOWNGRADE) |
8354 1 << (OP_DELEGRETURN) |
8356 [1] = 1 << (OP_SECINFO - 32) |
8357 1 << (OP_SECINFO_NO_NAME - 32) |
8358 1 << (OP_LAYOUTRETURN - 32) |
8359 1 << (OP_TEST_STATEID - 32) |
8360 1 << (OP_FREE_STATEID - 32) |
8361 1 << (OP_WRITE - 32)
8366 * Select the state protection mode for client `clp' given the server results
8367 * from exchange_id in `sp'.
8369 * Returns 0 on success, negative errno otherwise.
8371 static int nfs4_sp4_select_mode(struct nfs_client *clp,
8372 struct nfs41_state_protection *sp)
8374 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
8375 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8376 1 << (OP_EXCHANGE_ID - 32) |
8377 1 << (OP_CREATE_SESSION - 32) |
8378 1 << (OP_DESTROY_SESSION - 32) |
8379 1 << (OP_DESTROY_CLIENTID - 32)
8381 unsigned long flags = 0;
8385 if (sp->how == SP4_MACH_CRED) {
8386 /* Print state protect result */
8387 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
8388 for (i = 0; i <= LAST_NFS4_OP; i++) {
8389 if (test_bit(i, sp->enforce.u.longs))
8390 dfprintk(MOUNT, " enforce op %d\n", i);
8391 if (test_bit(i, sp->allow.u.longs))
8392 dfprintk(MOUNT, " allow op %d\n", i);
8395 /* make sure nothing is on enforce list that isn't supported */
8396 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
8397 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
8398 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
8405 * Minimal mode - state operations are allowed to use machine
8406 * credential. Note this already happens by default, so the
8407 * client doesn't have to do anything more than the negotiation.
8409 * NOTE: we don't care if EXCHANGE_ID is in the list -
8410 * we're already using the machine cred for exchange_id
8411 * and will never use a different cred.
8413 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
8414 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
8415 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
8416 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
8417 dfprintk(MOUNT, "sp4_mach_cred:\n");
8418 dfprintk(MOUNT, " minimal mode enabled\n");
8419 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
8421 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
8426 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
8427 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
8428 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
8429 test_bit(OP_LOCKU, sp->allow.u.longs)) {
8430 dfprintk(MOUNT, " cleanup mode enabled\n");
8431 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
8434 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
8435 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
8436 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
8439 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
8440 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
8441 dfprintk(MOUNT, " secinfo mode enabled\n");
8442 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
8445 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
8446 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
8447 dfprintk(MOUNT, " stateid mode enabled\n");
8448 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
8451 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
8452 dfprintk(MOUNT, " write mode enabled\n");
8453 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
8456 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
8457 dfprintk(MOUNT, " commit mode enabled\n");
8458 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
8462 clp->cl_sp4_flags = flags;
8466 struct nfs41_exchange_id_data {
8467 struct nfs41_exchange_id_res res;
8468 struct nfs41_exchange_id_args args;
8471 static void nfs4_exchange_id_release(void *data)
8473 struct nfs41_exchange_id_data *cdata =
8474 (struct nfs41_exchange_id_data *)data;
8476 nfs_put_client(cdata->args.client);
8477 kfree(cdata->res.impl_id);
8478 kfree(cdata->res.server_scope);
8479 kfree(cdata->res.server_owner);
8483 static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
8484 .rpc_release = nfs4_exchange_id_release,
8488 * _nfs4_proc_exchange_id()
8490 * Wrapper for EXCHANGE_ID operation.
8492 static struct rpc_task *
8493 nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
8494 u32 sp4_how, struct rpc_xprt *xprt)
8496 struct rpc_message msg = {
8497 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
8500 struct rpc_task_setup task_setup_data = {
8501 .rpc_client = clp->cl_rpcclient,
8502 .callback_ops = &nfs4_exchange_id_call_ops,
8503 .rpc_message = &msg,
8504 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
8506 struct nfs41_exchange_id_data *calldata;
8509 if (!refcount_inc_not_zero(&clp->cl_count))
8510 return ERR_PTR(-EIO);
8513 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8517 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
8519 status = nfs4_init_uniform_client_string(clp);
8523 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
8526 if (unlikely(calldata->res.server_owner == NULL))
8529 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
8531 if (unlikely(calldata->res.server_scope == NULL))
8532 goto out_server_owner;
8534 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
8535 if (unlikely(calldata->res.impl_id == NULL))
8536 goto out_server_scope;
8540 calldata->args.state_protect.how = SP4_NONE;
8544 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
8554 task_setup_data.rpc_xprt = xprt;
8555 task_setup_data.flags |= RPC_TASK_SOFTCONN;
8556 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
8557 sizeof(calldata->args.verifier.data));
8559 calldata->args.client = clp;
8560 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
8561 EXCHGID4_FLAG_BIND_PRINC_STATEID;
8562 #ifdef CONFIG_NFS_V4_1_MIGRATION
8563 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
8565 msg.rpc_argp = &calldata->args;
8566 msg.rpc_resp = &calldata->res;
8567 task_setup_data.callback_data = calldata;
8569 return rpc_run_task(&task_setup_data);
8572 kfree(calldata->res.impl_id);
8574 kfree(calldata->res.server_scope);
8576 kfree(calldata->res.server_owner);
8580 nfs_put_client(clp);
8581 return ERR_PTR(status);
8585 * _nfs4_proc_exchange_id()
8587 * Wrapper for EXCHANGE_ID operation.
8589 static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
8592 struct rpc_task *task;
8593 struct nfs41_exchange_id_args *argp;
8594 struct nfs41_exchange_id_res *resp;
8595 unsigned long now = jiffies;
8598 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
8600 return PTR_ERR(task);
8602 argp = task->tk_msg.rpc_argp;
8603 resp = task->tk_msg.rpc_resp;
8604 status = task->tk_status;
8608 status = nfs4_check_cl_exchange_flags(resp->flags,
8609 clp->cl_mvops->minor_version);
8613 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8617 do_renew_lease(clp, now);
8619 clp->cl_clientid = resp->clientid;
8620 clp->cl_exchange_flags = resp->flags;
8621 clp->cl_seqid = resp->seqid;
8622 /* Client ID is not confirmed */
8623 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8624 clear_bit(NFS4_SESSION_ESTABLISHED,
8625 &clp->cl_session->session_state);
8627 if (clp->cl_serverscope != NULL &&
8628 !nfs41_same_server_scope(clp->cl_serverscope,
8629 resp->server_scope)) {
8630 dprintk("%s: server_scope mismatch detected\n",
8632 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8635 swap(clp->cl_serverowner, resp->server_owner);
8636 swap(clp->cl_serverscope, resp->server_scope);
8637 swap(clp->cl_implid, resp->impl_id);
8639 /* Save the EXCHANGE_ID verifier session trunk tests */
8640 memcpy(clp->cl_confirm.data, argp->verifier.data,
8641 sizeof(clp->cl_confirm.data));
8643 trace_nfs4_exchange_id(clp, status);
8649 * nfs4_proc_exchange_id()
8651 * Returns zero, a negative errno, or a negative NFS4ERR status code.
8653 * Since the clientid has expired, all compounds using sessions
8654 * associated with the stale clientid will be returning
8655 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
8656 * be in some phase of session reset.
8658 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
8660 int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
8662 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8665 /* try SP4_MACH_CRED if krb5i/p */
8666 if (authflavor == RPC_AUTH_GSS_KRB5I ||
8667 authflavor == RPC_AUTH_GSS_KRB5P) {
8668 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
8674 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
8678 * nfs4_test_session_trunk
8680 * This is an add_xprt_test() test function called from
8681 * rpc_clnt_setup_test_and_add_xprt.
8683 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8684 * and is dereferrenced in nfs4_exchange_id_release
8686 * Upon success, add the new transport to the rpc_clnt
8688 * @clnt: struct rpc_clnt to get new transport
8689 * @xprt: the rpc_xprt to test
8690 * @data: call data for _nfs4_proc_exchange_id.
8692 void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
8695 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
8696 struct rpc_task *task;
8701 dprintk("--> %s try %s\n", __func__,
8702 xprt->address_strings[RPC_DISPLAY_ADDR]);
8704 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8706 /* Test connection for session trunking. Async exchange_id call */
8707 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8711 status = task->tk_status;
8713 status = nfs4_detect_session_trunking(adata->clp,
8714 task->tk_msg.rpc_resp, xprt);
8717 rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
8721 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
8723 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
8724 const struct cred *cred)
8726 struct rpc_message msg = {
8727 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8733 status = rpc_call_sync(clp->cl_rpcclient, &msg,
8734 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
8735 trace_nfs4_destroy_clientid(clp, status);
8737 dprintk("NFS: Got error %d from the server %s on "
8738 "DESTROY_CLIENTID.", status, clp->cl_hostname);
8742 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
8743 const struct cred *cred)
8748 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8749 ret = _nfs4_proc_destroy_clientid(clp, cred);
8751 case -NFS4ERR_DELAY:
8752 case -NFS4ERR_CLIENTID_BUSY:
8762 int nfs4_destroy_clientid(struct nfs_client *clp)
8764 const struct cred *cred;
8767 if (clp->cl_mvops->minor_version < 1)
8769 if (clp->cl_exchange_flags == 0)
8771 if (clp->cl_preserve_clid)
8773 cred = nfs4_get_clid_cred(clp);
8774 ret = nfs4_proc_destroy_clientid(clp, cred);
8778 case -NFS4ERR_STALE_CLIENTID:
8779 clp->cl_exchange_flags = 0;
8785 #endif /* CONFIG_NFS_V4_1 */
8787 struct nfs4_get_lease_time_data {
8788 struct nfs4_get_lease_time_args *args;
8789 struct nfs4_get_lease_time_res *res;
8790 struct nfs_client *clp;
8793 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
8796 struct nfs4_get_lease_time_data *data =
8797 (struct nfs4_get_lease_time_data *)calldata;
8799 dprintk("--> %s\n", __func__);
8800 /* just setup sequence, do not trigger session recovery
8801 since we're invoked within one */
8802 nfs4_setup_sequence(data->clp,
8803 &data->args->la_seq_args,
8804 &data->res->lr_seq_res,
8806 dprintk("<-- %s\n", __func__);
8810 * Called from nfs4_state_manager thread for session setup, so don't recover
8811 * from sequence operation or clientid errors.
8813 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8815 struct nfs4_get_lease_time_data *data =
8816 (struct nfs4_get_lease_time_data *)calldata;
8818 dprintk("--> %s\n", __func__);
8819 if (!nfs4_sequence_done(task, &data->res->lr_seq_res))
8821 switch (task->tk_status) {
8822 case -NFS4ERR_DELAY:
8823 case -NFS4ERR_GRACE:
8824 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
8825 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8826 task->tk_status = 0;
8828 case -NFS4ERR_RETRY_UNCACHED_REP:
8829 rpc_restart_call_prepare(task);
8832 dprintk("<-- %s\n", __func__);
8835 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
8836 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8837 .rpc_call_done = nfs4_get_lease_time_done,
8840 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8842 struct nfs4_get_lease_time_args args;
8843 struct nfs4_get_lease_time_res res = {
8844 .lr_fsinfo = fsinfo,
8846 struct nfs4_get_lease_time_data data = {
8851 struct rpc_message msg = {
8852 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8856 struct rpc_task_setup task_setup = {
8857 .rpc_client = clp->cl_rpcclient,
8858 .rpc_message = &msg,
8859 .callback_ops = &nfs4_get_lease_time_ops,
8860 .callback_data = &data,
8861 .flags = RPC_TASK_TIMEOUT,
8864 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
8865 return nfs4_call_sync_custom(&task_setup);
8868 #ifdef CONFIG_NFS_V4_1
8871 * Initialize the values to be used by the client in CREATE_SESSION
8872 * If nfs4_init_session set the fore channel request and response sizes,
8875 * Set the back channel max_resp_sz_cached to zero to force the client to
8876 * always set csa_cachethis to FALSE because the current implementation
8877 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8879 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8880 struct rpc_clnt *clnt)
8882 unsigned int max_rqst_sz, max_resp_sz;
8883 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
8884 unsigned int max_bc_slots = rpc_num_bc_slots(clnt);
8886 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8887 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8889 /* Fore channel attributes */
8890 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8891 args->fc_attrs.max_resp_sz = max_resp_sz;
8892 args->fc_attrs.max_ops = NFS4_MAX_OPS;
8893 args->fc_attrs.max_reqs = max_session_slots;
8895 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
8896 "max_ops=%u max_reqs=%u\n",
8898 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
8899 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
8901 /* Back channel attributes */
8902 args->bc_attrs.max_rqst_sz = max_bc_payload;
8903 args->bc_attrs.max_resp_sz = max_bc_payload;
8904 args->bc_attrs.max_resp_sz_cached = 0;
8905 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
8906 args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
8907 if (args->bc_attrs.max_reqs > max_bc_slots)
8908 args->bc_attrs.max_reqs = max_bc_slots;
8910 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8911 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8913 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8914 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8915 args->bc_attrs.max_reqs);
8918 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8919 struct nfs41_create_session_res *res)
8921 struct nfs4_channel_attrs *sent = &args->fc_attrs;
8922 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
8924 if (rcvd->max_resp_sz > sent->max_resp_sz)
8927 * Our requested max_ops is the minimum we need; we're not
8928 * prepared to break up compounds into smaller pieces than that.
8929 * So, no point even trying to continue if the server won't
8932 if (rcvd->max_ops < sent->max_ops)
8934 if (rcvd->max_reqs == 0)
8936 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8937 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
8941 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8942 struct nfs41_create_session_res *res)
8944 struct nfs4_channel_attrs *sent = &args->bc_attrs;
8945 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
8947 if (!(res->flags & SESSION4_BACK_CHAN))
8949 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8951 if (rcvd->max_resp_sz < sent->max_resp_sz)
8953 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8955 if (rcvd->max_ops > sent->max_ops)
8957 if (rcvd->max_reqs > sent->max_reqs)
8963 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
8964 struct nfs41_create_session_res *res)
8968 ret = nfs4_verify_fore_channel_attrs(args, res);
8971 return nfs4_verify_back_channel_attrs(args, res);
8974 static void nfs4_update_session(struct nfs4_session *session,
8975 struct nfs41_create_session_res *res)
8977 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
8978 /* Mark client id and session as being confirmed */
8979 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8980 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
8981 session->flags = res->flags;
8982 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
8983 if (res->flags & SESSION4_BACK_CHAN)
8984 memcpy(&session->bc_attrs, &res->bc_attrs,
8985 sizeof(session->bc_attrs));
8988 static int _nfs4_proc_create_session(struct nfs_client *clp,
8989 const struct cred *cred)
8991 struct nfs4_session *session = clp->cl_session;
8992 struct nfs41_create_session_args args = {
8994 .clientid = clp->cl_clientid,
8995 .seqid = clp->cl_seqid,
8996 .cb_program = NFS4_CALLBACK,
8998 struct nfs41_create_session_res res;
9000 struct rpc_message msg = {
9001 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
9008 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
9009 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
9011 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
9012 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
9013 trace_nfs4_create_session(clp, status);
9016 case -NFS4ERR_STALE_CLIENTID:
9017 case -NFS4ERR_DELAY:
9026 /* Verify the session's negotiated channel_attrs values */
9027 status = nfs4_verify_channel_attrs(&args, &res);
9028 /* Increment the clientid slot sequence id */
9031 nfs4_update_session(session, &res);
9038 * Issues a CREATE_SESSION operation to the server.
9039 * It is the responsibility of the caller to verify the session is
9040 * expired before calling this routine.
9042 int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
9046 struct nfs4_session *session = clp->cl_session;
9048 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
9050 status = _nfs4_proc_create_session(clp, cred);
9054 /* Init or reset the session slot tables */
9055 status = nfs4_setup_session_slot_tables(session);
9056 dprintk("slot table setup returned %d\n", status);
9060 ptr = (unsigned *)&session->sess_id.data[0];
9061 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
9062 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
9064 dprintk("<-- %s\n", __func__);
9069 * Issue the over-the-wire RPC DESTROY_SESSION.
9070 * The caller must serialize access to this routine.
9072 int nfs4_proc_destroy_session(struct nfs4_session *session,
9073 const struct cred *cred)
9075 struct rpc_message msg = {
9076 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
9077 .rpc_argp = session,
9082 dprintk("--> nfs4_proc_destroy_session\n");
9084 /* session is still being setup */
9085 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
9088 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
9089 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
9090 trace_nfs4_destroy_session(session->clp, status);
9093 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
9094 "Session has been destroyed regardless...\n", status);
9096 dprintk("<-- nfs4_proc_destroy_session\n");
9101 * Renew the cl_session lease.
9103 struct nfs4_sequence_data {
9104 struct nfs_client *clp;
9105 struct nfs4_sequence_args args;
9106 struct nfs4_sequence_res res;
9109 static void nfs41_sequence_release(void *data)
9111 struct nfs4_sequence_data *calldata = data;
9112 struct nfs_client *clp = calldata->clp;
9114 if (refcount_read(&clp->cl_count) > 1)
9115 nfs4_schedule_state_renewal(clp);
9116 nfs_put_client(clp);
9120 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9122 switch(task->tk_status) {
9123 case -NFS4ERR_DELAY:
9124 rpc_delay(task, NFS4_POLL_RETRY_MAX);
9127 nfs4_schedule_lease_recovery(clp);
9132 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
9134 struct nfs4_sequence_data *calldata = data;
9135 struct nfs_client *clp = calldata->clp;
9137 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
9140 trace_nfs4_sequence(clp, task->tk_status);
9141 if (task->tk_status < 0) {
9142 dprintk("%s ERROR %d\n", __func__, task->tk_status);
9143 if (refcount_read(&clp->cl_count) == 1)
9146 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
9147 rpc_restart_call_prepare(task);
9151 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
9153 dprintk("<-- %s\n", __func__);
9156 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
9158 struct nfs4_sequence_data *calldata = data;
9159 struct nfs_client *clp = calldata->clp;
9160 struct nfs4_sequence_args *args;
9161 struct nfs4_sequence_res *res;
9163 args = task->tk_msg.rpc_argp;
9164 res = task->tk_msg.rpc_resp;
9166 nfs4_setup_sequence(clp, args, res, task);
9169 static const struct rpc_call_ops nfs41_sequence_ops = {
9170 .rpc_call_done = nfs41_sequence_call_done,
9171 .rpc_call_prepare = nfs41_sequence_prepare,
9172 .rpc_release = nfs41_sequence_release,
9175 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
9176 const struct cred *cred,
9177 struct nfs4_slot *slot,
9180 struct nfs4_sequence_data *calldata;
9181 struct rpc_message msg = {
9182 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
9185 struct rpc_task_setup task_setup_data = {
9186 .rpc_client = clp->cl_rpcclient,
9187 .rpc_message = &msg,
9188 .callback_ops = &nfs41_sequence_ops,
9189 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
9191 struct rpc_task *ret;
9193 ret = ERR_PTR(-EIO);
9194 if (!refcount_inc_not_zero(&clp->cl_count))
9197 ret = ERR_PTR(-ENOMEM);
9198 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
9199 if (calldata == NULL)
9201 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
9202 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
9203 msg.rpc_argp = &calldata->args;
9204 msg.rpc_resp = &calldata->res;
9205 calldata->clp = clp;
9206 task_setup_data.callback_data = calldata;
9208 ret = rpc_run_task(&task_setup_data);
9213 nfs_put_client(clp);
9215 nfs41_release_slot(slot);
9219 static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
9221 struct rpc_task *task;
9224 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
9226 task = _nfs41_proc_sequence(clp, cred, NULL, false);
9228 ret = PTR_ERR(task);
9230 rpc_put_task_async(task);
9231 dprintk("<-- %s status=%d\n", __func__, ret);
9235 static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
9237 struct rpc_task *task;
9240 task = _nfs41_proc_sequence(clp, cred, NULL, true);
9242 ret = PTR_ERR(task);
9245 ret = rpc_wait_for_completion_task(task);
9247 ret = task->tk_status;
9250 dprintk("<-- %s status=%d\n", __func__, ret);
9254 struct nfs4_reclaim_complete_data {
9255 struct nfs_client *clp;
9256 struct nfs41_reclaim_complete_args arg;
9257 struct nfs41_reclaim_complete_res res;
9260 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
9262 struct nfs4_reclaim_complete_data *calldata = data;
9264 nfs4_setup_sequence(calldata->clp,
9265 &calldata->arg.seq_args,
9266 &calldata->res.seq_res,
9270 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9272 switch(task->tk_status) {
9274 wake_up_all(&clp->cl_lock_waitq);
9276 case -NFS4ERR_COMPLETE_ALREADY:
9277 case -NFS4ERR_WRONG_CRED: /* What to do here? */
9279 case -NFS4ERR_DELAY:
9280 rpc_delay(task, NFS4_POLL_RETRY_MAX);
9282 case -NFS4ERR_RETRY_UNCACHED_REP:
9284 case -NFS4ERR_BADSESSION:
9285 case -NFS4ERR_DEADSESSION:
9286 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9289 nfs4_schedule_lease_recovery(clp);
9294 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
9296 struct nfs4_reclaim_complete_data *calldata = data;
9297 struct nfs_client *clp = calldata->clp;
9298 struct nfs4_sequence_res *res = &calldata->res.seq_res;
9300 dprintk("--> %s\n", __func__);
9301 if (!nfs41_sequence_done(task, res))
9304 trace_nfs4_reclaim_complete(clp, task->tk_status);
9305 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
9306 rpc_restart_call_prepare(task);
9309 dprintk("<-- %s\n", __func__);
9312 static void nfs4_free_reclaim_complete_data(void *data)
9314 struct nfs4_reclaim_complete_data *calldata = data;
9319 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
9320 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
9321 .rpc_call_done = nfs4_reclaim_complete_done,
9322 .rpc_release = nfs4_free_reclaim_complete_data,
9326 * Issue a global reclaim complete.
9328 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
9329 const struct cred *cred)
9331 struct nfs4_reclaim_complete_data *calldata;
9332 struct rpc_message msg = {
9333 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
9336 struct rpc_task_setup task_setup_data = {
9337 .rpc_client = clp->cl_rpcclient,
9338 .rpc_message = &msg,
9339 .callback_ops = &nfs4_reclaim_complete_call_ops,
9340 .flags = RPC_TASK_NO_ROUND_ROBIN,
9342 int status = -ENOMEM;
9344 dprintk("--> %s\n", __func__);
9345 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
9346 if (calldata == NULL)
9348 calldata->clp = clp;
9349 calldata->arg.one_fs = 0;
9351 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
9352 msg.rpc_argp = &calldata->arg;
9353 msg.rpc_resp = &calldata->res;
9354 task_setup_data.callback_data = calldata;
9355 status = nfs4_call_sync_custom(&task_setup_data);
9357 dprintk("<-- %s status=%d\n", __func__, status);
9362 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
9364 struct nfs4_layoutget *lgp = calldata;
9365 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
9367 dprintk("--> %s\n", __func__);
9368 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
9369 &lgp->res.seq_res, task);
9370 dprintk("<-- %s\n", __func__);
9373 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
9375 struct nfs4_layoutget *lgp = calldata;
9377 dprintk("--> %s\n", __func__);
9378 nfs41_sequence_process(task, &lgp->res.seq_res);
9379 dprintk("<-- %s\n", __func__);
9383 nfs4_layoutget_handle_exception(struct rpc_task *task,
9384 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
9386 struct inode *inode = lgp->args.inode;
9387 struct nfs_server *server = NFS_SERVER(inode);
9388 struct pnfs_layout_hdr *lo;
9389 int nfs4err = task->tk_status;
9390 int err, status = 0;
9393 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
9395 nfs4_sequence_free_slot(&lgp->res.seq_res);
9402 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
9403 * on the file. set tk_status to -ENODATA to tell upper layer to
9406 case -NFS4ERR_LAYOUTUNAVAILABLE:
9410 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
9411 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
9413 case -NFS4ERR_BADLAYOUT:
9414 status = -EOVERFLOW;
9417 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
9418 * (or clients) writing to the same RAID stripe except when
9419 * the minlength argument is 0 (see RFC5661 section 18.43.3).
9421 * Treat it like we would RECALLCONFLICT -- we retry for a little
9422 * while, and then eventually give up.
9424 case -NFS4ERR_LAYOUTTRYLATER:
9425 if (lgp->args.minlength == 0) {
9426 status = -EOVERFLOW;
9431 case -NFS4ERR_RECALLCONFLICT:
9432 status = -ERECALLCONFLICT;
9434 case -NFS4ERR_DELEG_REVOKED:
9435 case -NFS4ERR_ADMIN_REVOKED:
9436 case -NFS4ERR_EXPIRED:
9437 case -NFS4ERR_BAD_STATEID:
9438 exception->timeout = 0;
9439 spin_lock(&inode->i_lock);
9440 lo = NFS_I(inode)->layout;
9441 /* If the open stateid was bad, then recover it. */
9442 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
9443 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
9444 spin_unlock(&inode->i_lock);
9445 exception->state = lgp->args.ctx->state;
9446 exception->stateid = &lgp->args.stateid;
9451 * Mark the bad layout state as invalid, then retry
9453 pnfs_mark_layout_stateid_invalid(lo, &head);
9454 spin_unlock(&inode->i_lock);
9455 nfs_commit_inode(inode, 0);
9456 pnfs_free_lseg_list(&head);
9461 err = nfs4_handle_exception(server, nfs4err, exception);
9463 if (exception->retry)
9469 dprintk("<-- %s\n", __func__);
9473 size_t max_response_pages(struct nfs_server *server)
9475 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
9476 return nfs_page_array_len(0, max_resp_sz);
9479 static void nfs4_layoutget_release(void *calldata)
9481 struct nfs4_layoutget *lgp = calldata;
9483 dprintk("--> %s\n", __func__);
9484 nfs4_sequence_free_slot(&lgp->res.seq_res);
9485 pnfs_layoutget_free(lgp);
9486 dprintk("<-- %s\n", __func__);
9489 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
9490 .rpc_call_prepare = nfs4_layoutget_prepare,
9491 .rpc_call_done = nfs4_layoutget_done,
9492 .rpc_release = nfs4_layoutget_release,
9495 struct pnfs_layout_segment *
9496 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
9498 struct inode *inode = lgp->args.inode;
9499 struct nfs_server *server = NFS_SERVER(inode);
9500 struct rpc_task *task;
9501 struct rpc_message msg = {
9502 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
9503 .rpc_argp = &lgp->args,
9504 .rpc_resp = &lgp->res,
9505 .rpc_cred = lgp->cred,
9507 struct rpc_task_setup task_setup_data = {
9508 .rpc_client = server->client,
9509 .rpc_message = &msg,
9510 .callback_ops = &nfs4_layoutget_call_ops,
9511 .callback_data = lgp,
9512 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
9514 struct pnfs_layout_segment *lseg = NULL;
9515 struct nfs4_exception exception = {
9517 .timeout = *timeout,
9521 dprintk("--> %s\n", __func__);
9523 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
9524 pnfs_get_layout_hdr(NFS_I(inode)->layout);
9526 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
9528 task = rpc_run_task(&task_setup_data);
9530 status = rpc_wait_for_completion_task(task);
9534 if (task->tk_status < 0) {
9535 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
9536 *timeout = exception.timeout;
9537 } else if (lgp->res.layoutp->len == 0) {
9539 *timeout = nfs4_update_delay(&exception.timeout);
9541 lseg = pnfs_layout_process(lgp);
9543 trace_nfs4_layoutget(lgp->args.ctx,
9550 dprintk("<-- %s status=%d\n", __func__, status);
9552 return ERR_PTR(status);
9557 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
9559 struct nfs4_layoutreturn *lrp = calldata;
9561 dprintk("--> %s\n", __func__);
9562 nfs4_setup_sequence(lrp->clp,
9563 &lrp->args.seq_args,
9566 if (!pnfs_layout_is_valid(lrp->args.layout))
9570 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
9572 struct nfs4_layoutreturn *lrp = calldata;
9573 struct nfs_server *server;
9575 dprintk("--> %s\n", __func__);
9577 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
9581 * Was there an RPC level error? Assume the call succeeded,
9582 * and that we need to release the layout
9584 if (task->tk_rpc_status != 0 && RPC_WAS_SENT(task)) {
9585 lrp->res.lrs_present = 0;
9589 server = NFS_SERVER(lrp->args.inode);
9590 switch (task->tk_status) {
9591 case -NFS4ERR_OLD_STATEID:
9592 if (nfs4_layout_refresh_old_stateid(&lrp->args.stateid,
9598 task->tk_status = 0;
9602 case -NFS4ERR_DELAY:
9603 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
9607 dprintk("<-- %s\n", __func__);
9610 task->tk_status = 0;
9611 nfs4_sequence_free_slot(&lrp->res.seq_res);
9612 rpc_restart_call_prepare(task);
9615 static void nfs4_layoutreturn_release(void *calldata)
9617 struct nfs4_layoutreturn *lrp = calldata;
9618 struct pnfs_layout_hdr *lo = lrp->args.layout;
9620 dprintk("--> %s\n", __func__);
9621 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
9622 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
9623 nfs4_sequence_free_slot(&lrp->res.seq_res);
9624 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9625 lrp->ld_private.ops->free(&lrp->ld_private);
9626 pnfs_put_layout_hdr(lrp->args.layout);
9627 nfs_iput_and_deactive(lrp->inode);
9628 put_cred(lrp->cred);
9630 dprintk("<-- %s\n", __func__);
9633 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9634 .rpc_call_prepare = nfs4_layoutreturn_prepare,
9635 .rpc_call_done = nfs4_layoutreturn_done,
9636 .rpc_release = nfs4_layoutreturn_release,
9639 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
9641 struct rpc_task *task;
9642 struct rpc_message msg = {
9643 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9644 .rpc_argp = &lrp->args,
9645 .rpc_resp = &lrp->res,
9646 .rpc_cred = lrp->cred,
9648 struct rpc_task_setup task_setup_data = {
9649 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
9650 .rpc_message = &msg,
9651 .callback_ops = &nfs4_layoutreturn_call_ops,
9652 .callback_data = lrp,
9656 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9657 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9658 &task_setup_data.rpc_client, &msg);
9660 dprintk("--> %s\n", __func__);
9661 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
9664 nfs4_layoutreturn_release(lrp);
9667 task_setup_data.flags |= RPC_TASK_ASYNC;
9670 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1,
9673 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1,
9675 task = rpc_run_task(&task_setup_data);
9677 return PTR_ERR(task);
9679 status = task->tk_status;
9680 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
9681 dprintk("<-- %s status=%d\n", __func__, status);
9687 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
9688 struct pnfs_device *pdev,
9689 const struct cred *cred)
9691 struct nfs4_getdeviceinfo_args args = {
9693 .notify_types = NOTIFY_DEVICEID4_CHANGE |
9694 NOTIFY_DEVICEID4_DELETE,
9696 struct nfs4_getdeviceinfo_res res = {
9699 struct rpc_message msg = {
9700 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9707 dprintk("--> %s\n", __func__);
9708 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
9709 if (res.notification & ~args.notify_types)
9710 dprintk("%s: unsupported notification\n", __func__);
9711 if (res.notification != args.notify_types)
9714 trace_nfs4_getdeviceinfo(server, &pdev->dev_id, status);
9716 dprintk("<-- %s status=%d\n", __func__, status);
9721 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9722 struct pnfs_device *pdev,
9723 const struct cred *cred)
9725 struct nfs4_exception exception = { };
9729 err = nfs4_handle_exception(server,
9730 _nfs4_proc_getdeviceinfo(server, pdev, cred),
9732 } while (exception.retry);
9735 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9737 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9739 struct nfs4_layoutcommit_data *data = calldata;
9740 struct nfs_server *server = NFS_SERVER(data->args.inode);
9742 nfs4_setup_sequence(server->nfs_client,
9743 &data->args.seq_args,
9749 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9751 struct nfs4_layoutcommit_data *data = calldata;
9752 struct nfs_server *server = NFS_SERVER(data->args.inode);
9754 if (!nfs41_sequence_done(task, &data->res.seq_res))
9757 switch (task->tk_status) { /* Just ignore these failures */
9758 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9759 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9760 case -NFS4ERR_BADLAYOUT: /* no layout */
9761 case -NFS4ERR_GRACE: /* loca_recalim always false */
9762 task->tk_status = 0;
9767 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
9768 rpc_restart_call_prepare(task);
9774 static void nfs4_layoutcommit_release(void *calldata)
9776 struct nfs4_layoutcommit_data *data = calldata;
9778 pnfs_cleanup_layoutcommit(data);
9779 nfs_post_op_update_inode_force_wcc(data->args.inode,
9781 put_cred(data->cred);
9782 nfs_iput_and_deactive(data->inode);
9786 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9787 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9788 .rpc_call_done = nfs4_layoutcommit_done,
9789 .rpc_release = nfs4_layoutcommit_release,
9793 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
9795 struct rpc_message msg = {
9796 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9797 .rpc_argp = &data->args,
9798 .rpc_resp = &data->res,
9799 .rpc_cred = data->cred,
9801 struct rpc_task_setup task_setup_data = {
9802 .task = &data->task,
9803 .rpc_client = NFS_CLIENT(data->args.inode),
9804 .rpc_message = &msg,
9805 .callback_ops = &nfs4_layoutcommit_ops,
9806 .callback_data = data,
9808 struct rpc_task *task;
9811 dprintk("NFS: initiating layoutcommit call. sync %d "
9812 "lbw: %llu inode %lu\n", sync,
9813 data->args.lastbytewritten,
9814 data->args.inode->i_ino);
9817 data->inode = nfs_igrab_and_active(data->args.inode);
9818 if (data->inode == NULL) {
9819 nfs4_layoutcommit_release(data);
9822 task_setup_data.flags = RPC_TASK_ASYNC;
9824 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
9825 task = rpc_run_task(&task_setup_data);
9827 return PTR_ERR(task);
9829 status = task->tk_status;
9830 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
9831 dprintk("%s: status %d\n", __func__, status);
9837 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9838 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9841 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9842 struct nfs_fsinfo *info,
9843 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
9845 struct nfs41_secinfo_no_name_args args = {
9846 .style = SECINFO_STYLE_CURRENT_FH,
9848 struct nfs4_secinfo_res res = {
9851 struct rpc_message msg = {
9852 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9856 struct nfs4_call_sync_data data = {
9857 .seq_server = server,
9858 .seq_args = &args.seq_args,
9859 .seq_res = &res.seq_res,
9861 struct rpc_task_setup task_setup = {
9862 .rpc_client = server->client,
9863 .rpc_message = &msg,
9864 .callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
9865 .callback_data = &data,
9866 .flags = RPC_TASK_NO_ROUND_ROBIN,
9868 const struct cred *cred = NULL;
9871 if (use_integrity) {
9872 task_setup.rpc_client = server->nfs_client->cl_rpcclient;
9874 cred = nfs4_get_clid_cred(server->nfs_client);
9875 msg.rpc_cred = cred;
9878 dprintk("--> %s\n", __func__);
9879 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
9880 status = nfs4_call_sync_custom(&task_setup);
9881 dprintk("<-- %s status=%d\n", __func__, status);
9889 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9890 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9892 struct nfs4_exception exception = {
9893 .interruptible = true,
9897 /* first try using integrity protection */
9898 err = -NFS4ERR_WRONGSEC;
9900 /* try to use integrity protection with machine cred */
9901 if (_nfs4_is_integrity_protected(server->nfs_client))
9902 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9906 * if unable to use integrity protection, or SECINFO with
9907 * integrity protection returns NFS4ERR_WRONGSEC (which is
9908 * disallowed by spec, but exists in deployed servers) use
9909 * the current filesystem's rpc_client and the user cred.
9911 if (err == -NFS4ERR_WRONGSEC)
9912 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9917 case -NFS4ERR_WRONGSEC:
9921 err = nfs4_handle_exception(server, err, &exception);
9923 } while (exception.retry);
9929 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9930 struct nfs_fsinfo *info)
9934 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
9935 struct nfs4_secinfo_flavors *flavors;
9936 struct nfs4_secinfo4 *secinfo;
9939 page = alloc_page(GFP_KERNEL);
9945 flavors = page_address(page);
9946 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9949 * Fall back on "guess and check" method if
9950 * the server doesn't support SECINFO_NO_NAME
9952 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
9953 err = nfs4_find_root_sec(server, fhandle, info);
9959 for (i = 0; i < flavors->num_flavors; i++) {
9960 secinfo = &flavors->flavors[i];
9962 switch (secinfo->flavor) {
9966 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9967 &secinfo->flavor_info);
9970 flavor = RPC_AUTH_MAXFLAVOR;
9974 if (!nfs_auth_info_match(&server->auth_info, flavor))
9975 flavor = RPC_AUTH_MAXFLAVOR;
9977 if (flavor != RPC_AUTH_MAXFLAVOR) {
9978 err = nfs4_lookup_root_sec(server, fhandle,
9985 if (flavor == RPC_AUTH_MAXFLAVOR)
9996 static int _nfs41_test_stateid(struct nfs_server *server,
9997 nfs4_stateid *stateid,
9998 const struct cred *cred)
10001 struct nfs41_test_stateid_args args = {
10002 .stateid = stateid,
10004 struct nfs41_test_stateid_res res;
10005 struct rpc_message msg = {
10006 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
10011 struct rpc_clnt *rpc_client = server->client;
10013 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
10014 &rpc_client, &msg);
10016 dprintk("NFS call test_stateid %p\n", stateid);
10017 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
10018 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
10019 &args.seq_args, &res.seq_res);
10020 if (status != NFS_OK) {
10021 dprintk("NFS reply test_stateid: failed, %d\n", status);
10024 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
10025 return -res.status;
10028 static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
10029 int err, struct nfs4_exception *exception)
10031 exception->retry = 0;
10033 case -NFS4ERR_DELAY:
10034 case -NFS4ERR_RETRY_UNCACHED_REP:
10035 nfs4_handle_exception(server, err, exception);
10037 case -NFS4ERR_BADSESSION:
10038 case -NFS4ERR_BADSLOT:
10039 case -NFS4ERR_BAD_HIGH_SLOT:
10040 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
10041 case -NFS4ERR_DEADSESSION:
10042 nfs4_do_handle_exception(server, err, exception);
10047 * nfs41_test_stateid - perform a TEST_STATEID operation
10049 * @server: server / transport on which to perform the operation
10050 * @stateid: state ID to test
10051 * @cred: credential
10053 * Returns NFS_OK if the server recognizes that "stateid" is valid.
10054 * Otherwise a negative NFS4ERR value is returned if the operation
10055 * failed or the state ID is not currently valid.
10057 static int nfs41_test_stateid(struct nfs_server *server,
10058 nfs4_stateid *stateid,
10059 const struct cred *cred)
10061 struct nfs4_exception exception = {
10062 .interruptible = true,
10066 err = _nfs41_test_stateid(server, stateid, cred);
10067 nfs4_handle_delay_or_session_error(server, err, &exception);
10068 } while (exception.retry);
10072 struct nfs_free_stateid_data {
10073 struct nfs_server *server;
10074 struct nfs41_free_stateid_args args;
10075 struct nfs41_free_stateid_res res;
10078 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
10080 struct nfs_free_stateid_data *data = calldata;
10081 nfs4_setup_sequence(data->server->nfs_client,
10082 &data->args.seq_args,
10083 &data->res.seq_res,
10087 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
10089 struct nfs_free_stateid_data *data = calldata;
10091 nfs41_sequence_done(task, &data->res.seq_res);
10093 switch (task->tk_status) {
10094 case -NFS4ERR_DELAY:
10095 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
10096 rpc_restart_call_prepare(task);
10100 static void nfs41_free_stateid_release(void *calldata)
10105 static const struct rpc_call_ops nfs41_free_stateid_ops = {
10106 .rpc_call_prepare = nfs41_free_stateid_prepare,
10107 .rpc_call_done = nfs41_free_stateid_done,
10108 .rpc_release = nfs41_free_stateid_release,
10112 * nfs41_free_stateid - perform a FREE_STATEID operation
10114 * @server: server / transport on which to perform the operation
10115 * @stateid: state ID to release
10116 * @cred: credential
10117 * @privileged: set to true if this call needs to be privileged
10119 * Note: this function is always asynchronous.
10121 static int nfs41_free_stateid(struct nfs_server *server,
10122 const nfs4_stateid *stateid,
10123 const struct cred *cred,
10126 struct rpc_message msg = {
10127 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
10130 struct rpc_task_setup task_setup = {
10131 .rpc_client = server->client,
10132 .rpc_message = &msg,
10133 .callback_ops = &nfs41_free_stateid_ops,
10134 .flags = RPC_TASK_ASYNC,
10136 struct nfs_free_stateid_data *data;
10137 struct rpc_task *task;
10139 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
10140 &task_setup.rpc_client, &msg);
10142 dprintk("NFS call free_stateid %p\n", stateid);
10143 data = kmalloc(sizeof(*data), GFP_NOFS);
10146 data->server = server;
10147 nfs4_stateid_copy(&data->args.stateid, stateid);
10149 task_setup.callback_data = data;
10151 msg.rpc_argp = &data->args;
10152 msg.rpc_resp = &data->res;
10153 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
10154 task = rpc_run_task(&task_setup);
10156 return PTR_ERR(task);
10157 rpc_put_task(task);
10162 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
10164 const struct cred *cred = lsp->ls_state->owner->so_cred;
10166 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
10167 nfs4_free_lock_state(server, lsp);
10170 static bool nfs41_match_stateid(const nfs4_stateid *s1,
10171 const nfs4_stateid *s2)
10173 if (s1->type != s2->type)
10176 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
10179 if (s1->seqid == s2->seqid)
10182 return s1->seqid == 0 || s2->seqid == 0;
10185 #endif /* CONFIG_NFS_V4_1 */
10187 static bool nfs4_match_stateid(const nfs4_stateid *s1,
10188 const nfs4_stateid *s2)
10190 return nfs4_stateid_match(s1, s2);
10194 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
10195 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
10196 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
10197 .recover_open = nfs4_open_reclaim,
10198 .recover_lock = nfs4_lock_reclaim,
10199 .establish_clid = nfs4_init_clientid,
10200 .detect_trunking = nfs40_discover_server_trunking,
10203 #if defined(CONFIG_NFS_V4_1)
10204 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
10205 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
10206 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
10207 .recover_open = nfs4_open_reclaim,
10208 .recover_lock = nfs4_lock_reclaim,
10209 .establish_clid = nfs41_init_clientid,
10210 .reclaim_complete = nfs41_proc_reclaim_complete,
10211 .detect_trunking = nfs41_discover_server_trunking,
10213 #endif /* CONFIG_NFS_V4_1 */
10215 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
10216 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
10217 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
10218 .recover_open = nfs40_open_expired,
10219 .recover_lock = nfs4_lock_expired,
10220 .establish_clid = nfs4_init_clientid,
10223 #if defined(CONFIG_NFS_V4_1)
10224 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
10225 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
10226 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
10227 .recover_open = nfs41_open_expired,
10228 .recover_lock = nfs41_lock_expired,
10229 .establish_clid = nfs41_init_clientid,
10231 #endif /* CONFIG_NFS_V4_1 */
10233 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
10234 .sched_state_renewal = nfs4_proc_async_renew,
10235 .get_state_renewal_cred = nfs4_get_renew_cred,
10236 .renew_lease = nfs4_proc_renew,
10239 #if defined(CONFIG_NFS_V4_1)
10240 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
10241 .sched_state_renewal = nfs41_proc_async_sequence,
10242 .get_state_renewal_cred = nfs4_get_machine_cred,
10243 .renew_lease = nfs4_proc_sequence,
10247 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
10248 .get_locations = _nfs40_proc_get_locations,
10249 .fsid_present = _nfs40_proc_fsid_present,
10252 #if defined(CONFIG_NFS_V4_1)
10253 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
10254 .get_locations = _nfs41_proc_get_locations,
10255 .fsid_present = _nfs41_proc_fsid_present,
10257 #endif /* CONFIG_NFS_V4_1 */
10259 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
10260 .minor_version = 0,
10261 .init_caps = NFS_CAP_READDIRPLUS
10262 | NFS_CAP_ATOMIC_OPEN
10263 | NFS_CAP_POSIX_LOCK,
10264 .init_client = nfs40_init_client,
10265 .shutdown_client = nfs40_shutdown_client,
10266 .match_stateid = nfs4_match_stateid,
10267 .find_root_sec = nfs4_find_root_sec,
10268 .free_lock_state = nfs4_release_lockowner,
10269 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
10270 .alloc_seqid = nfs_alloc_seqid,
10271 .call_sync_ops = &nfs40_call_sync_ops,
10272 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
10273 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
10274 .state_renewal_ops = &nfs40_state_renewal_ops,
10275 .mig_recovery_ops = &nfs40_mig_recovery_ops,
10278 #if defined(CONFIG_NFS_V4_1)
10279 static struct nfs_seqid *
10280 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
10285 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
10286 .minor_version = 1,
10287 .init_caps = NFS_CAP_READDIRPLUS
10288 | NFS_CAP_ATOMIC_OPEN
10289 | NFS_CAP_POSIX_LOCK
10290 | NFS_CAP_STATEID_NFSV41
10291 | NFS_CAP_ATOMIC_OPEN_V1
10293 .init_client = nfs41_init_client,
10294 .shutdown_client = nfs41_shutdown_client,
10295 .match_stateid = nfs41_match_stateid,
10296 .find_root_sec = nfs41_find_root_sec,
10297 .free_lock_state = nfs41_free_lock_state,
10298 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
10299 .alloc_seqid = nfs_alloc_no_seqid,
10300 .session_trunk = nfs4_test_session_trunk,
10301 .call_sync_ops = &nfs41_call_sync_ops,
10302 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10303 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10304 .state_renewal_ops = &nfs41_state_renewal_ops,
10305 .mig_recovery_ops = &nfs41_mig_recovery_ops,
10309 #if defined(CONFIG_NFS_V4_2)
10310 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
10311 .minor_version = 2,
10312 .init_caps = NFS_CAP_READDIRPLUS
10313 | NFS_CAP_ATOMIC_OPEN
10314 | NFS_CAP_POSIX_LOCK
10315 | NFS_CAP_STATEID_NFSV41
10316 | NFS_CAP_ATOMIC_OPEN_V1
10320 | NFS_CAP_OFFLOAD_CANCEL
10321 | NFS_CAP_COPY_NOTIFY
10322 | NFS_CAP_DEALLOCATE
10324 | NFS_CAP_LAYOUTSTATS
10326 | NFS_CAP_LAYOUTERROR
10327 | NFS_CAP_READ_PLUS,
10328 .init_client = nfs41_init_client,
10329 .shutdown_client = nfs41_shutdown_client,
10330 .match_stateid = nfs41_match_stateid,
10331 .find_root_sec = nfs41_find_root_sec,
10332 .free_lock_state = nfs41_free_lock_state,
10333 .call_sync_ops = &nfs41_call_sync_ops,
10334 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
10335 .alloc_seqid = nfs_alloc_no_seqid,
10336 .session_trunk = nfs4_test_session_trunk,
10337 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10338 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10339 .state_renewal_ops = &nfs41_state_renewal_ops,
10340 .mig_recovery_ops = &nfs41_mig_recovery_ops,
10344 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
10345 [0] = &nfs_v4_0_minor_ops,
10346 #if defined(CONFIG_NFS_V4_1)
10347 [1] = &nfs_v4_1_minor_ops,
10349 #if defined(CONFIG_NFS_V4_2)
10350 [2] = &nfs_v4_2_minor_ops,
10354 static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
10356 ssize_t error, error2, error3;
10358 error = generic_listxattr(dentry, list, size);
10366 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
10375 error3 = nfs4_listxattr_nfs4_user(d_inode(dentry), list, size);
10379 return error + error2 + error3;
10382 static const struct inode_operations nfs4_dir_inode_operations = {
10383 .create = nfs_create,
10384 .lookup = nfs_lookup,
10385 .atomic_open = nfs_atomic_open,
10387 .unlink = nfs_unlink,
10388 .symlink = nfs_symlink,
10389 .mkdir = nfs_mkdir,
10390 .rmdir = nfs_rmdir,
10391 .mknod = nfs_mknod,
10392 .rename = nfs_rename,
10393 .permission = nfs_permission,
10394 .getattr = nfs_getattr,
10395 .setattr = nfs_setattr,
10396 .listxattr = nfs4_listxattr,
10399 static const struct inode_operations nfs4_file_inode_operations = {
10400 .permission = nfs_permission,
10401 .getattr = nfs_getattr,
10402 .setattr = nfs_setattr,
10403 .listxattr = nfs4_listxattr,
10406 const struct nfs_rpc_ops nfs_v4_clientops = {
10407 .version = 4, /* protocol version */
10408 .dentry_ops = &nfs4_dentry_operations,
10409 .dir_inode_ops = &nfs4_dir_inode_operations,
10410 .file_inode_ops = &nfs4_file_inode_operations,
10411 .file_ops = &nfs4_file_operations,
10412 .getroot = nfs4_proc_get_root,
10413 .submount = nfs4_submount,
10414 .try_get_tree = nfs4_try_get_tree,
10415 .getattr = nfs4_proc_getattr,
10416 .setattr = nfs4_proc_setattr,
10417 .lookup = nfs4_proc_lookup,
10418 .lookupp = nfs4_proc_lookupp,
10419 .access = nfs4_proc_access,
10420 .readlink = nfs4_proc_readlink,
10421 .create = nfs4_proc_create,
10422 .remove = nfs4_proc_remove,
10423 .unlink_setup = nfs4_proc_unlink_setup,
10424 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
10425 .unlink_done = nfs4_proc_unlink_done,
10426 .rename_setup = nfs4_proc_rename_setup,
10427 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
10428 .rename_done = nfs4_proc_rename_done,
10429 .link = nfs4_proc_link,
10430 .symlink = nfs4_proc_symlink,
10431 .mkdir = nfs4_proc_mkdir,
10432 .rmdir = nfs4_proc_rmdir,
10433 .readdir = nfs4_proc_readdir,
10434 .mknod = nfs4_proc_mknod,
10435 .statfs = nfs4_proc_statfs,
10436 .fsinfo = nfs4_proc_fsinfo,
10437 .pathconf = nfs4_proc_pathconf,
10438 .set_capabilities = nfs4_server_capabilities,
10439 .decode_dirent = nfs4_decode_dirent,
10440 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
10441 .read_setup = nfs4_proc_read_setup,
10442 .read_done = nfs4_read_done,
10443 .write_setup = nfs4_proc_write_setup,
10444 .write_done = nfs4_write_done,
10445 .commit_setup = nfs4_proc_commit_setup,
10446 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
10447 .commit_done = nfs4_commit_done,
10448 .lock = nfs4_proc_lock,
10449 .clear_acl_cache = nfs4_zap_acl_attr,
10450 .close_context = nfs4_close_context,
10451 .open_context = nfs4_atomic_open,
10452 .have_delegation = nfs4_have_delegation,
10453 .alloc_client = nfs4_alloc_client,
10454 .init_client = nfs4_init_client,
10455 .free_client = nfs4_free_client,
10456 .create_server = nfs4_create_server,
10457 .clone_server = nfs_clone_server,
10460 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
10461 .name = XATTR_NAME_NFSV4_ACL,
10462 .list = nfs4_xattr_list_nfs4_acl,
10463 .get = nfs4_xattr_get_nfs4_acl,
10464 .set = nfs4_xattr_set_nfs4_acl,
10467 #ifdef CONFIG_NFS_V4_2
10468 static const struct xattr_handler nfs4_xattr_nfs4_user_handler = {
10469 .prefix = XATTR_USER_PREFIX,
10470 .get = nfs4_xattr_get_nfs4_user,
10471 .set = nfs4_xattr_set_nfs4_user,
10475 const struct xattr_handler *nfs4_xattr_handlers[] = {
10476 &nfs4_xattr_nfs4_acl_handler,
10477 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
10478 &nfs4_xattr_nfs4_label_handler,
10480 #ifdef CONFIG_NFS_V4_2
10481 &nfs4_xattr_nfs4_user_handler,