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 ret = nfs4_wait_clnt_recover(clp);
593 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
600 exception->retry = 1;
605 nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
606 int errorcode, struct nfs4_exception *exception)
608 struct nfs_client *clp = server->nfs_client;
611 ret = nfs4_do_handle_exception(server, errorcode, exception);
612 if (exception->delay) {
613 rpc_delay(task, nfs4_update_delay(&exception->timeout));
616 if (exception->recovering) {
617 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
618 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
619 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
622 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
627 exception->retry = 1;
629 * For NFS4ERR_MOVED, the client transport will need to
630 * be recomputed after migration recovery has completed.
632 if (errorcode == -NFS4ERR_MOVED)
633 rpc_task_release_transport(task);
639 nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
640 struct nfs4_state *state, long *timeout)
642 struct nfs4_exception exception = {
646 if (task->tk_status >= 0)
649 exception.timeout = *timeout;
650 task->tk_status = nfs4_async_handle_exception(task, server,
653 if (exception.delay && timeout)
654 *timeout = exception.timeout;
661 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
662 * or 'false' otherwise.
664 static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
666 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
667 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
670 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
672 spin_lock(&clp->cl_lock);
673 if (time_before(clp->cl_last_renewal,timestamp))
674 clp->cl_last_renewal = timestamp;
675 spin_unlock(&clp->cl_lock);
678 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
680 struct nfs_client *clp = server->nfs_client;
682 if (!nfs4_has_session(clp))
683 do_renew_lease(clp, timestamp);
686 struct nfs4_call_sync_data {
687 const struct nfs_server *seq_server;
688 struct nfs4_sequence_args *seq_args;
689 struct nfs4_sequence_res *seq_res;
692 void nfs4_init_sequence(struct nfs4_sequence_args *args,
693 struct nfs4_sequence_res *res, int cache_reply,
696 args->sa_slot = NULL;
697 args->sa_cache_this = cache_reply;
698 args->sa_privileged = privileged;
703 static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
705 struct nfs4_slot *slot = res->sr_slot;
706 struct nfs4_slot_table *tbl;
709 spin_lock(&tbl->slot_tbl_lock);
710 if (!nfs41_wake_and_assign_slot(tbl, slot))
711 nfs4_free_slot(tbl, slot);
712 spin_unlock(&tbl->slot_tbl_lock);
717 static int nfs40_sequence_done(struct rpc_task *task,
718 struct nfs4_sequence_res *res)
720 if (res->sr_slot != NULL)
721 nfs40_sequence_free_slot(res);
725 #if defined(CONFIG_NFS_V4_1)
727 static void nfs41_release_slot(struct nfs4_slot *slot)
729 struct nfs4_session *session;
730 struct nfs4_slot_table *tbl;
731 bool send_new_highest_used_slotid = false;
736 session = tbl->session;
738 /* Bump the slot sequence number */
743 spin_lock(&tbl->slot_tbl_lock);
744 /* Be nice to the server: try to ensure that the last transmitted
745 * value for highest_user_slotid <= target_highest_slotid
747 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
748 send_new_highest_used_slotid = true;
750 if (nfs41_wake_and_assign_slot(tbl, slot)) {
751 send_new_highest_used_slotid = false;
754 nfs4_free_slot(tbl, slot);
756 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
757 send_new_highest_used_slotid = false;
759 spin_unlock(&tbl->slot_tbl_lock);
760 if (send_new_highest_used_slotid)
761 nfs41_notify_server(session->clp);
762 if (waitqueue_active(&tbl->slot_waitq))
763 wake_up_all(&tbl->slot_waitq);
766 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
768 nfs41_release_slot(res->sr_slot);
772 static void nfs4_slot_sequence_record_sent(struct nfs4_slot *slot,
775 if ((s32)(seqnr - slot->seq_nr_highest_sent) > 0)
776 slot->seq_nr_highest_sent = seqnr;
778 static void nfs4_slot_sequence_acked(struct nfs4_slot *slot,
781 slot->seq_nr_highest_sent = seqnr;
782 slot->seq_nr_last_acked = seqnr;
785 static void nfs4_probe_sequence(struct nfs_client *client, const struct cred *cred,
786 struct nfs4_slot *slot)
788 struct rpc_task *task = _nfs41_proc_sequence(client, cred, slot, true);
790 rpc_put_task_async(task);
793 static int nfs41_sequence_process(struct rpc_task *task,
794 struct nfs4_sequence_res *res)
796 struct nfs4_session *session;
797 struct nfs4_slot *slot = res->sr_slot;
798 struct nfs_client *clp;
804 /* don't increment the sequence number if the task wasn't sent */
805 if (!RPC_WAS_SENT(task) || slot->seq_done)
808 session = slot->table->session;
811 trace_nfs4_sequence_done(session, res);
813 status = res->sr_status;
814 if (task->tk_status == -NFS4ERR_DEADSESSION)
815 status = -NFS4ERR_DEADSESSION;
817 /* Check the SEQUENCE operation status */
820 /* Mark this sequence number as having been acked */
821 nfs4_slot_sequence_acked(slot, slot->seq_nr);
822 /* Update the slot's sequence and clientid lease timer */
824 do_renew_lease(clp, res->sr_timestamp);
825 /* Check sequence flags */
826 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
828 nfs41_update_target_slotid(slot->table, slot, res);
832 * sr_status remains 1 if an RPC level error occurred.
833 * The server may or may not have processed the sequence
836 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
840 /* The server detected a resend of the RPC call and
841 * returned NFS4ERR_DELAY as per Section 2.10.6.2
844 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
848 nfs4_slot_sequence_acked(slot, slot->seq_nr);
850 case -NFS4ERR_RETRY_UNCACHED_REP:
851 case -NFS4ERR_SEQ_FALSE_RETRY:
853 * The server thinks we tried to replay a request.
854 * Retry the call after bumping the sequence ID.
856 nfs4_slot_sequence_acked(slot, slot->seq_nr);
858 case -NFS4ERR_BADSLOT:
860 * The slot id we used was probably retired. Try again
861 * using a different slot id.
863 if (slot->slot_nr < slot->table->target_highest_slotid)
864 goto session_recover;
866 case -NFS4ERR_SEQ_MISORDERED:
867 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
869 * Were one or more calls using this slot interrupted?
870 * If the server never received the request, then our
871 * transmitted slot sequence number may be too high. However,
872 * if the server did receive the request then it might
873 * accidentally give us a reply with a mismatched operation.
874 * We can sort this out by sending a lone sequence operation
875 * to the server on the same slot.
877 if ((s32)(slot->seq_nr - slot->seq_nr_last_acked) > 1) {
879 if (task->tk_msg.rpc_proc != &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE]) {
880 nfs4_probe_sequence(clp, task->tk_msg.rpc_cred, slot);
887 * A retry might be sent while the original request is
888 * still in progress on the replier. The replier SHOULD
889 * deal with the issue by returning NFS4ERR_DELAY as the
890 * reply to SEQUENCE or CB_SEQUENCE operation, but
891 * implementations MAY return NFS4ERR_SEQ_MISORDERED.
893 * Restart the search after a delay.
895 slot->seq_nr = slot->seq_nr_highest_sent;
897 case -NFS4ERR_BADSESSION:
898 case -NFS4ERR_DEADSESSION:
899 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
900 goto session_recover;
902 /* Just update the slot sequence no. */
906 /* The session may be reset by one of the error handlers. */
907 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
911 nfs4_schedule_session_recovery(session, status);
912 dprintk("%s ERROR: %d Reset session\n", __func__, status);
913 nfs41_sequence_free_slot(res);
918 if (rpc_restart_call_prepare(task)) {
919 nfs41_sequence_free_slot(res);
925 if (!rpc_restart_call(task))
927 rpc_delay(task, NFS4_POLL_RETRY_MAX);
931 int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
933 if (!nfs41_sequence_process(task, res))
935 if (res->sr_slot != NULL)
936 nfs41_sequence_free_slot(res);
940 EXPORT_SYMBOL_GPL(nfs41_sequence_done);
942 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
944 if (res->sr_slot == NULL)
946 if (res->sr_slot->table->session != NULL)
947 return nfs41_sequence_process(task, res);
948 return nfs40_sequence_done(task, res);
951 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
953 if (res->sr_slot != NULL) {
954 if (res->sr_slot->table->session != NULL)
955 nfs41_sequence_free_slot(res);
957 nfs40_sequence_free_slot(res);
961 int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
963 if (res->sr_slot == NULL)
965 if (!res->sr_slot->table->session)
966 return nfs40_sequence_done(task, res);
967 return nfs41_sequence_done(task, res);
969 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
971 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
973 struct nfs4_call_sync_data *data = calldata;
975 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
977 nfs4_setup_sequence(data->seq_server->nfs_client,
978 data->seq_args, data->seq_res, task);
981 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
983 struct nfs4_call_sync_data *data = calldata;
985 nfs41_sequence_done(task, data->seq_res);
988 static const struct rpc_call_ops nfs41_call_sync_ops = {
989 .rpc_call_prepare = nfs41_call_sync_prepare,
990 .rpc_call_done = nfs41_call_sync_done,
993 #else /* !CONFIG_NFS_V4_1 */
995 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
997 return nfs40_sequence_done(task, res);
1000 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
1002 if (res->sr_slot != NULL)
1003 nfs40_sequence_free_slot(res);
1006 int nfs4_sequence_done(struct rpc_task *task,
1007 struct nfs4_sequence_res *res)
1009 return nfs40_sequence_done(task, res);
1011 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
1013 #endif /* !CONFIG_NFS_V4_1 */
1015 static void nfs41_sequence_res_init(struct nfs4_sequence_res *res)
1017 res->sr_timestamp = jiffies;
1018 res->sr_status_flags = 0;
1023 void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
1024 struct nfs4_sequence_res *res,
1025 struct nfs4_slot *slot)
1029 slot->privileged = args->sa_privileged ? 1 : 0;
1030 args->sa_slot = slot;
1032 res->sr_slot = slot;
1035 int nfs4_setup_sequence(struct nfs_client *client,
1036 struct nfs4_sequence_args *args,
1037 struct nfs4_sequence_res *res,
1038 struct rpc_task *task)
1040 struct nfs4_session *session = nfs4_get_session(client);
1041 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
1042 struct nfs4_slot *slot;
1044 /* slot already allocated? */
1045 if (res->sr_slot != NULL)
1049 tbl = &session->fc_slot_table;
1051 spin_lock(&tbl->slot_tbl_lock);
1052 /* The state manager will wait until the slot table is empty */
1053 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
1056 slot = nfs4_alloc_slot(tbl);
1058 if (slot == ERR_PTR(-ENOMEM))
1059 goto out_sleep_timeout;
1062 spin_unlock(&tbl->slot_tbl_lock);
1064 nfs4_sequence_attach_slot(args, res, slot);
1066 trace_nfs4_setup_sequence(session, args);
1068 nfs41_sequence_res_init(res);
1069 rpc_call_start(task);
1072 /* Try again in 1/4 second */
1073 if (args->sa_privileged)
1074 rpc_sleep_on_priority_timeout(&tbl->slot_tbl_waitq, task,
1075 jiffies + (HZ >> 2), RPC_PRIORITY_PRIVILEGED);
1077 rpc_sleep_on_timeout(&tbl->slot_tbl_waitq, task,
1078 NULL, jiffies + (HZ >> 2));
1079 spin_unlock(&tbl->slot_tbl_lock);
1082 if (args->sa_privileged)
1083 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
1084 RPC_PRIORITY_PRIVILEGED);
1086 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
1087 spin_unlock(&tbl->slot_tbl_lock);
1090 EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
1092 static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1094 struct nfs4_call_sync_data *data = calldata;
1095 nfs4_setup_sequence(data->seq_server->nfs_client,
1096 data->seq_args, data->seq_res, task);
1099 static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1101 struct nfs4_call_sync_data *data = calldata;
1102 nfs4_sequence_done(task, data->seq_res);
1105 static const struct rpc_call_ops nfs40_call_sync_ops = {
1106 .rpc_call_prepare = nfs40_call_sync_prepare,
1107 .rpc_call_done = nfs40_call_sync_done,
1110 static int nfs4_call_sync_custom(struct rpc_task_setup *task_setup)
1113 struct rpc_task *task;
1115 task = rpc_run_task(task_setup);
1117 return PTR_ERR(task);
1119 ret = task->tk_status;
1124 static int nfs4_do_call_sync(struct rpc_clnt *clnt,
1125 struct nfs_server *server,
1126 struct rpc_message *msg,
1127 struct nfs4_sequence_args *args,
1128 struct nfs4_sequence_res *res,
1129 unsigned short task_flags)
1131 struct nfs_client *clp = server->nfs_client;
1132 struct nfs4_call_sync_data data = {
1133 .seq_server = server,
1137 struct rpc_task_setup task_setup = {
1140 .callback_ops = clp->cl_mvops->call_sync_ops,
1141 .callback_data = &data,
1142 .flags = task_flags,
1145 return nfs4_call_sync_custom(&task_setup);
1148 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
1149 struct nfs_server *server,
1150 struct rpc_message *msg,
1151 struct nfs4_sequence_args *args,
1152 struct nfs4_sequence_res *res)
1154 return nfs4_do_call_sync(clnt, server, msg, args, res, 0);
1158 int nfs4_call_sync(struct rpc_clnt *clnt,
1159 struct nfs_server *server,
1160 struct rpc_message *msg,
1161 struct nfs4_sequence_args *args,
1162 struct nfs4_sequence_res *res,
1165 nfs4_init_sequence(args, res, cache_reply, 0);
1166 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
1170 nfs4_inc_nlink_locked(struct inode *inode)
1172 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
1173 NFS_INO_INVALID_CTIME |
1174 NFS_INO_INVALID_NLINK);
1179 nfs4_inc_nlink(struct inode *inode)
1181 spin_lock(&inode->i_lock);
1182 nfs4_inc_nlink_locked(inode);
1183 spin_unlock(&inode->i_lock);
1187 nfs4_dec_nlink_locked(struct inode *inode)
1189 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
1190 NFS_INO_INVALID_CTIME |
1191 NFS_INO_INVALID_NLINK);
1196 nfs4_update_changeattr_locked(struct inode *inode,
1197 struct nfs4_change_info *cinfo,
1198 unsigned long timestamp, unsigned long cache_validity)
1200 struct nfs_inode *nfsi = NFS_I(inode);
1201 u64 change_attr = inode_peek_iversion_raw(inode);
1203 cache_validity |= NFS_INO_INVALID_CTIME | NFS_INO_INVALID_MTIME;
1205 switch (NFS_SERVER(inode)->change_attr_type) {
1206 case NFS4_CHANGE_TYPE_IS_UNDEFINED:
1208 case NFS4_CHANGE_TYPE_IS_TIME_METADATA:
1209 if ((s64)(change_attr - cinfo->after) > 0)
1213 if ((s64)(change_attr - cinfo->after) >= 0)
1217 if (cinfo->atomic && cinfo->before == change_attr) {
1218 nfsi->attrtimeo_timestamp = jiffies;
1220 if (S_ISDIR(inode->i_mode)) {
1221 cache_validity |= NFS_INO_INVALID_DATA;
1222 nfs_force_lookup_revalidate(inode);
1224 if (!NFS_PROTO(inode)->have_delegation(inode,
1226 cache_validity |= NFS_INO_REVAL_PAGECACHE;
1229 if (cinfo->before != change_attr)
1230 cache_validity |= NFS_INO_INVALID_ACCESS |
1231 NFS_INO_INVALID_ACL |
1232 NFS_INO_INVALID_XATTR;
1234 inode_set_iversion_raw(inode, cinfo->after);
1235 nfsi->read_cache_jiffies = timestamp;
1236 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
1237 nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE;
1239 nfs_set_cache_invalid(inode, cache_validity);
1243 nfs4_update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1244 unsigned long timestamp, unsigned long cache_validity)
1246 spin_lock(&dir->i_lock);
1247 nfs4_update_changeattr_locked(dir, cinfo, timestamp, cache_validity);
1248 spin_unlock(&dir->i_lock);
1251 struct nfs4_open_createattrs {
1252 struct nfs4_label *label;
1253 struct iattr *sattr;
1254 const __u32 verf[2];
1257 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1258 int err, struct nfs4_exception *exception)
1262 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1264 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1265 exception->retry = 1;
1269 static fmode_t _nfs4_ctx_to_accessmode(const struct nfs_open_context *ctx)
1271 return ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
1274 static fmode_t _nfs4_ctx_to_openmode(const struct nfs_open_context *ctx)
1276 fmode_t ret = ctx->mode & (FMODE_READ|FMODE_WRITE);
1278 return (ctx->mode & FMODE_EXEC) ? FMODE_READ | ret : ret;
1282 nfs4_map_atomic_open_share(struct nfs_server *server,
1283 fmode_t fmode, int openflags)
1287 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1289 res = NFS4_SHARE_ACCESS_READ;
1292 res = NFS4_SHARE_ACCESS_WRITE;
1294 case FMODE_READ|FMODE_WRITE:
1295 res = NFS4_SHARE_ACCESS_BOTH;
1297 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1299 /* Want no delegation if we're using O_DIRECT */
1300 if (openflags & O_DIRECT)
1301 res |= NFS4_SHARE_WANT_NO_DELEG;
1306 static enum open_claim_type4
1307 nfs4_map_atomic_open_claim(struct nfs_server *server,
1308 enum open_claim_type4 claim)
1310 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1315 case NFS4_OPEN_CLAIM_FH:
1316 return NFS4_OPEN_CLAIM_NULL;
1317 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1318 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1319 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1320 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1324 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
1326 p->o_res.f_attr = &p->f_attr;
1327 p->o_res.f_label = p->f_label;
1328 p->o_res.seqid = p->o_arg.seqid;
1329 p->c_res.seqid = p->c_arg.seqid;
1330 p->o_res.server = p->o_arg.server;
1331 p->o_res.access_request = p->o_arg.access;
1332 nfs_fattr_init(&p->f_attr);
1333 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
1336 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
1337 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
1338 const struct nfs4_open_createattrs *c,
1339 enum open_claim_type4 claim,
1342 struct dentry *parent = dget_parent(dentry);
1343 struct inode *dir = d_inode(parent);
1344 struct nfs_server *server = NFS_SERVER(dir);
1345 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
1346 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
1347 struct nfs4_opendata *p;
1349 p = kzalloc(sizeof(*p), gfp_mask);
1353 p->f_label = nfs4_label_alloc(server, gfp_mask);
1354 if (IS_ERR(p->f_label))
1357 p->a_label = nfs4_label_alloc(server, gfp_mask);
1358 if (IS_ERR(p->a_label))
1361 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1362 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
1363 if (IS_ERR(p->o_arg.seqid))
1364 goto err_free_label;
1365 nfs_sb_active(dentry->d_sb);
1366 p->dentry = dget(dentry);
1369 atomic_inc(&sp->so_count);
1370 p->o_arg.open_flags = flags;
1371 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
1372 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1373 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1375 if (flags & O_CREAT) {
1376 p->o_arg.umask = current_umask();
1377 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1378 if (c->sattr != NULL && c->sattr->ia_valid != 0) {
1379 p->o_arg.u.attrs = &p->attrs;
1380 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
1382 memcpy(p->o_arg.u.verifier.data, c->verf,
1383 sizeof(p->o_arg.u.verifier.data));
1386 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1387 * will return permission denied for all bits until close */
1388 if (!(flags & O_EXCL)) {
1389 /* ask server to check for all possible rights as results
1391 switch (p->o_arg.claim) {
1394 case NFS4_OPEN_CLAIM_NULL:
1395 case NFS4_OPEN_CLAIM_FH:
1396 p->o_arg.access = NFS4_ACCESS_READ |
1397 NFS4_ACCESS_MODIFY |
1398 NFS4_ACCESS_EXTEND |
1399 NFS4_ACCESS_EXECUTE;
1400 #ifdef CONFIG_NFS_V4_2
1401 if (server->caps & NFS_CAP_XATTR)
1402 p->o_arg.access |= NFS4_ACCESS_XAREAD |
1403 NFS4_ACCESS_XAWRITE |
1408 p->o_arg.clientid = server->nfs_client->cl_clientid;
1409 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1410 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1411 p->o_arg.name = &dentry->d_name;
1412 p->o_arg.server = server;
1413 p->o_arg.bitmask = nfs4_bitmask(server, label);
1414 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1415 switch (p->o_arg.claim) {
1416 case NFS4_OPEN_CLAIM_NULL:
1417 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1418 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1419 p->o_arg.fh = NFS_FH(dir);
1421 case NFS4_OPEN_CLAIM_PREVIOUS:
1422 case NFS4_OPEN_CLAIM_FH:
1423 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1424 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1425 p->o_arg.fh = NFS_FH(d_inode(dentry));
1427 p->c_arg.fh = &p->o_res.fh;
1428 p->c_arg.stateid = &p->o_res.stateid;
1429 p->c_arg.seqid = p->o_arg.seqid;
1430 nfs4_init_opendata_res(p);
1431 kref_init(&p->kref);
1435 nfs4_label_free(p->a_label);
1437 nfs4_label_free(p->f_label);
1445 static void nfs4_opendata_free(struct kref *kref)
1447 struct nfs4_opendata *p = container_of(kref,
1448 struct nfs4_opendata, kref);
1449 struct super_block *sb = p->dentry->d_sb;
1451 nfs4_lgopen_release(p->lgp);
1452 nfs_free_seqid(p->o_arg.seqid);
1453 nfs4_sequence_free_slot(&p->o_res.seq_res);
1454 if (p->state != NULL)
1455 nfs4_put_open_state(p->state);
1456 nfs4_put_state_owner(p->owner);
1458 nfs4_label_free(p->a_label);
1459 nfs4_label_free(p->f_label);
1463 nfs_sb_deactive(sb);
1464 nfs_fattr_free_names(&p->f_attr);
1465 kfree(p->f_attr.mdsthreshold);
1469 static void nfs4_opendata_put(struct nfs4_opendata *p)
1472 kref_put(&p->kref, nfs4_opendata_free);
1475 static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1478 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1479 case FMODE_READ|FMODE_WRITE:
1480 return state->n_rdwr != 0;
1482 return state->n_wronly != 0;
1484 return state->n_rdonly != 0;
1490 static int can_open_cached(struct nfs4_state *state, fmode_t mode,
1491 int open_mode, enum open_claim_type4 claim)
1495 if (open_mode & (O_EXCL|O_TRUNC))
1498 case NFS4_OPEN_CLAIM_NULL:
1499 case NFS4_OPEN_CLAIM_FH:
1504 switch (mode & (FMODE_READ|FMODE_WRITE)) {
1506 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1507 && state->n_rdonly != 0;
1510 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1511 && state->n_wronly != 0;
1513 case FMODE_READ|FMODE_WRITE:
1514 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1515 && state->n_rdwr != 0;
1521 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1522 enum open_claim_type4 claim)
1524 if (delegation == NULL)
1526 if ((delegation->type & fmode) != fmode)
1529 case NFS4_OPEN_CLAIM_NULL:
1530 case NFS4_OPEN_CLAIM_FH:
1532 case NFS4_OPEN_CLAIM_PREVIOUS:
1533 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1539 nfs_mark_delegation_referenced(delegation);
1543 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1552 case FMODE_READ|FMODE_WRITE:
1555 nfs4_state_set_mode_locked(state, state->state | fmode);
1558 #ifdef CONFIG_NFS_V4_1
1559 static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1561 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1563 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1565 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1569 #endif /* CONFIG_NFS_V4_1 */
1571 static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1573 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1574 wake_up_all(&state->waitq);
1577 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
1579 struct nfs_client *clp = state->owner->so_server->nfs_client;
1580 bool need_recover = false;
1582 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1583 need_recover = true;
1584 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1585 need_recover = true;
1586 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1587 need_recover = true;
1589 nfs4_state_mark_reclaim_nograce(clp, state);
1593 * Check for whether or not the caller may update the open stateid
1594 * to the value passed in by stateid.
1596 * Note: This function relies heavily on the server implementing
1597 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1599 * i.e. The stateid seqids have to be initialised to 1, and
1600 * are then incremented on every state transition.
1602 static bool nfs_stateid_is_sequential(struct nfs4_state *state,
1603 const nfs4_stateid *stateid)
1605 if (test_bit(NFS_OPEN_STATE, &state->flags)) {
1606 /* The common case - we're updating to a new sequence number */
1607 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1608 nfs4_stateid_is_next(&state->open_stateid, stateid)) {
1612 /* This is the first OPEN in this generation */
1613 if (stateid->seqid == cpu_to_be32(1))
1619 static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1621 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1623 if (state->n_wronly)
1624 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1625 if (state->n_rdonly)
1626 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1628 set_bit(NFS_O_RDWR_STATE, &state->flags);
1629 set_bit(NFS_OPEN_STATE, &state->flags);
1632 static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1633 nfs4_stateid *stateid, fmode_t fmode)
1635 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1636 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1638 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1641 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1644 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1645 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1646 clear_bit(NFS_OPEN_STATE, &state->flags);
1648 if (stateid == NULL)
1650 /* Handle OPEN+OPEN_DOWNGRADE races */
1651 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1652 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1653 nfs_resync_open_stateid_locked(state);
1656 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1657 nfs4_stateid_copy(&state->stateid, stateid);
1658 nfs4_stateid_copy(&state->open_stateid, stateid);
1659 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
1661 nfs_state_log_update_open_stateid(state);
1664 static void nfs_clear_open_stateid(struct nfs4_state *state,
1665 nfs4_stateid *arg_stateid,
1666 nfs4_stateid *stateid, fmode_t fmode)
1668 write_seqlock(&state->seqlock);
1669 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1670 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1671 nfs_clear_open_stateid_locked(state, stateid, fmode);
1672 write_sequnlock(&state->seqlock);
1673 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1674 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1677 static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1678 const nfs4_stateid *stateid, nfs4_stateid *freeme)
1679 __must_hold(&state->owner->so_lock)
1680 __must_hold(&state->seqlock)
1688 if (nfs_stateid_is_sequential(state, stateid))
1693 /* Rely on seqids for serialisation with NFSv4.0 */
1694 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1697 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1698 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1700 * Ensure we process the state changes in the same order
1701 * in which the server processed them by delaying the
1702 * update of the stateid until we are in sequence.
1704 write_sequnlock(&state->seqlock);
1705 spin_unlock(&state->owner->so_lock);
1707 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
1709 if (!signal_pending(current)) {
1710 if (schedule_timeout(5*HZ) == 0)
1716 finish_wait(&state->waitq, &wait);
1718 spin_lock(&state->owner->so_lock);
1719 write_seqlock(&state->seqlock);
1722 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1723 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1724 nfs4_stateid_copy(freeme, &state->open_stateid);
1725 nfs_test_and_clear_all_open_stateid(state);
1728 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1729 nfs4_stateid_copy(&state->stateid, stateid);
1730 nfs4_stateid_copy(&state->open_stateid, stateid);
1731 trace_nfs4_open_stateid_update(state->inode, stateid, status);
1732 nfs_state_log_update_open_stateid(state);
1735 static void nfs_state_set_open_stateid(struct nfs4_state *state,
1736 const nfs4_stateid *open_stateid,
1738 nfs4_stateid *freeme)
1741 * Protect the call to nfs4_state_set_mode_locked and
1742 * serialise the stateid update
1744 write_seqlock(&state->seqlock);
1745 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1748 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1751 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1753 case FMODE_READ|FMODE_WRITE:
1754 set_bit(NFS_O_RDWR_STATE, &state->flags);
1756 set_bit(NFS_OPEN_STATE, &state->flags);
1757 write_sequnlock(&state->seqlock);
1760 static void nfs_state_clear_open_state_flags(struct nfs4_state *state)
1762 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1763 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1764 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1765 clear_bit(NFS_OPEN_STATE, &state->flags);
1768 static void nfs_state_set_delegation(struct nfs4_state *state,
1769 const nfs4_stateid *deleg_stateid,
1773 * Protect the call to nfs4_state_set_mode_locked and
1774 * serialise the stateid update
1776 write_seqlock(&state->seqlock);
1777 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1778 set_bit(NFS_DELEGATED_STATE, &state->flags);
1779 write_sequnlock(&state->seqlock);
1782 static void nfs_state_clear_delegation(struct nfs4_state *state)
1784 write_seqlock(&state->seqlock);
1785 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1786 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1787 write_sequnlock(&state->seqlock);
1790 int update_open_stateid(struct nfs4_state *state,
1791 const nfs4_stateid *open_stateid,
1792 const nfs4_stateid *delegation,
1795 struct nfs_server *server = NFS_SERVER(state->inode);
1796 struct nfs_client *clp = server->nfs_client;
1797 struct nfs_inode *nfsi = NFS_I(state->inode);
1798 struct nfs_delegation *deleg_cur;
1799 nfs4_stateid freeme = { };
1802 fmode &= (FMODE_READ|FMODE_WRITE);
1805 spin_lock(&state->owner->so_lock);
1806 if (open_stateid != NULL) {
1807 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1811 deleg_cur = nfs4_get_valid_delegation(state->inode);
1812 if (deleg_cur == NULL)
1815 spin_lock(&deleg_cur->lock);
1816 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1817 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1818 (deleg_cur->type & fmode) != fmode)
1819 goto no_delegation_unlock;
1821 if (delegation == NULL)
1822 delegation = &deleg_cur->stateid;
1823 else if (!nfs4_stateid_match_other(&deleg_cur->stateid, delegation))
1824 goto no_delegation_unlock;
1826 nfs_mark_delegation_referenced(deleg_cur);
1827 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
1829 no_delegation_unlock:
1830 spin_unlock(&deleg_cur->lock);
1833 update_open_stateflags(state, fmode);
1834 spin_unlock(&state->owner->so_lock);
1837 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1838 nfs4_schedule_state_manager(clp);
1839 if (freeme.type != 0)
1840 nfs4_test_and_free_stateid(server, &freeme,
1841 state->owner->so_cred);
1846 static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1847 const nfs4_stateid *stateid)
1849 struct nfs4_state *state = lsp->ls_state;
1852 spin_lock(&state->state_lock);
1853 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1855 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1857 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1860 spin_unlock(&state->state_lock);
1864 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1866 struct nfs_delegation *delegation;
1868 fmode &= FMODE_READ|FMODE_WRITE;
1870 delegation = nfs4_get_valid_delegation(inode);
1871 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1876 nfs4_inode_return_delegation(inode);
1879 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1881 struct nfs4_state *state = opendata->state;
1882 struct nfs_delegation *delegation;
1883 int open_mode = opendata->o_arg.open_flags;
1884 fmode_t fmode = opendata->o_arg.fmode;
1885 enum open_claim_type4 claim = opendata->o_arg.claim;
1886 nfs4_stateid stateid;
1890 spin_lock(&state->owner->so_lock);
1891 if (can_open_cached(state, fmode, open_mode, claim)) {
1892 update_open_stateflags(state, fmode);
1893 spin_unlock(&state->owner->so_lock);
1894 goto out_return_state;
1896 spin_unlock(&state->owner->so_lock);
1898 delegation = nfs4_get_valid_delegation(state->inode);
1899 if (!can_open_delegated(delegation, fmode, claim)) {
1903 /* Save the delegation */
1904 nfs4_stateid_copy(&stateid, &delegation->stateid);
1906 nfs_release_seqid(opendata->o_arg.seqid);
1907 if (!opendata->is_recover) {
1908 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1914 /* Try to update the stateid using the delegation */
1915 if (update_open_stateid(state, NULL, &stateid, fmode))
1916 goto out_return_state;
1919 return ERR_PTR(ret);
1921 refcount_inc(&state->count);
1926 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1928 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1929 struct nfs_delegation *delegation;
1930 int delegation_flags = 0;
1933 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1935 delegation_flags = delegation->flags;
1937 switch (data->o_arg.claim) {
1940 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1941 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1942 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1943 "returning a delegation for "
1944 "OPEN(CLAIM_DELEGATE_CUR)\n",
1948 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1949 nfs_inode_set_delegation(state->inode,
1950 data->owner->so_cred,
1951 data->o_res.delegation_type,
1952 &data->o_res.delegation,
1953 data->o_res.pagemod_limit);
1955 nfs_inode_reclaim_delegation(state->inode,
1956 data->owner->so_cred,
1957 data->o_res.delegation_type,
1958 &data->o_res.delegation,
1959 data->o_res.pagemod_limit);
1961 if (data->o_res.do_recall)
1962 nfs_async_inode_return_delegation(state->inode,
1963 &data->o_res.delegation);
1967 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1968 * and update the nfs4_state.
1970 static struct nfs4_state *
1971 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1973 struct inode *inode = data->state->inode;
1974 struct nfs4_state *state = data->state;
1977 if (!data->rpc_done) {
1978 if (data->rpc_status)
1979 return ERR_PTR(data->rpc_status);
1980 /* cached opens have already been processed */
1984 ret = nfs_refresh_inode(inode, &data->f_attr);
1986 return ERR_PTR(ret);
1988 if (data->o_res.delegation_type != 0)
1989 nfs4_opendata_check_deleg(data, state);
1991 if (!update_open_stateid(state, &data->o_res.stateid,
1992 NULL, data->o_arg.fmode))
1993 return ERR_PTR(-EAGAIN);
1994 refcount_inc(&state->count);
1999 static struct inode *
2000 nfs4_opendata_get_inode(struct nfs4_opendata *data)
2002 struct inode *inode;
2004 switch (data->o_arg.claim) {
2005 case NFS4_OPEN_CLAIM_NULL:
2006 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
2007 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
2008 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
2009 return ERR_PTR(-EAGAIN);
2010 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
2011 &data->f_attr, data->f_label);
2014 inode = d_inode(data->dentry);
2016 nfs_refresh_inode(inode, &data->f_attr);
2021 static struct nfs4_state *
2022 nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
2024 struct nfs4_state *state;
2025 struct inode *inode;
2027 inode = nfs4_opendata_get_inode(data);
2029 return ERR_CAST(inode);
2030 if (data->state != NULL && data->state->inode == inode) {
2031 state = data->state;
2032 refcount_inc(&state->count);
2034 state = nfs4_get_open_state(inode, data->owner);
2037 state = ERR_PTR(-ENOMEM);
2041 static struct nfs4_state *
2042 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2044 struct nfs4_state *state;
2046 if (!data->rpc_done) {
2047 state = nfs4_try_open_cached(data);
2048 trace_nfs4_cached_open(data->state);
2052 state = nfs4_opendata_find_nfs4_state(data);
2056 if (data->o_res.delegation_type != 0)
2057 nfs4_opendata_check_deleg(data, state);
2058 if (!update_open_stateid(state, &data->o_res.stateid,
2059 NULL, data->o_arg.fmode)) {
2060 nfs4_put_open_state(state);
2061 state = ERR_PTR(-EAGAIN);
2064 nfs_release_seqid(data->o_arg.seqid);
2068 static struct nfs4_state *
2069 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2071 struct nfs4_state *ret;
2073 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
2074 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
2076 ret = _nfs4_opendata_to_nfs4_state(data);
2077 nfs4_sequence_free_slot(&data->o_res.seq_res);
2081 static struct nfs_open_context *
2082 nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode)
2084 struct nfs_inode *nfsi = NFS_I(state->inode);
2085 struct nfs_open_context *ctx;
2088 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
2089 if (ctx->state != state)
2091 if ((ctx->mode & mode) != mode)
2093 if (!get_nfs_open_context(ctx))
2099 return ERR_PTR(-ENOENT);
2102 static struct nfs_open_context *
2103 nfs4_state_find_open_context(struct nfs4_state *state)
2105 struct nfs_open_context *ctx;
2107 ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE);
2110 ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE);
2113 return nfs4_state_find_open_context_mode(state, FMODE_READ);
2116 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
2117 struct nfs4_state *state, enum open_claim_type4 claim)
2119 struct nfs4_opendata *opendata;
2121 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
2122 NULL, claim, GFP_NOFS);
2123 if (opendata == NULL)
2124 return ERR_PTR(-ENOMEM);
2125 opendata->state = state;
2126 refcount_inc(&state->count);
2130 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
2133 struct nfs4_state *newstate;
2136 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
2138 opendata->o_arg.open_flags = 0;
2139 opendata->o_arg.fmode = fmode;
2140 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
2141 NFS_SB(opendata->dentry->d_sb),
2143 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
2144 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
2145 nfs4_init_opendata_res(opendata);
2146 ret = _nfs4_recover_proc_open(opendata);
2149 newstate = nfs4_opendata_to_nfs4_state(opendata);
2150 if (IS_ERR(newstate))
2151 return PTR_ERR(newstate);
2152 if (newstate != opendata->state)
2154 nfs4_close_state(newstate, fmode);
2158 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
2162 /* memory barrier prior to reading state->n_* */
2164 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2167 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2170 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2174 * We may have performed cached opens for all three recoveries.
2175 * Check if we need to update the current stateid.
2177 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
2178 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
2179 write_seqlock(&state->seqlock);
2180 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2181 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2182 write_sequnlock(&state->seqlock);
2189 * reclaim state on the server after a reboot.
2191 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2193 struct nfs_delegation *delegation;
2194 struct nfs4_opendata *opendata;
2195 fmode_t delegation_type = 0;
2198 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2199 NFS4_OPEN_CLAIM_PREVIOUS);
2200 if (IS_ERR(opendata))
2201 return PTR_ERR(opendata);
2203 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2204 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
2205 delegation_type = delegation->type;
2207 opendata->o_arg.u.delegation_type = delegation_type;
2208 status = nfs4_open_recover(opendata, state);
2209 nfs4_opendata_put(opendata);
2213 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2215 struct nfs_server *server = NFS_SERVER(state->inode);
2216 struct nfs4_exception exception = { };
2219 err = _nfs4_do_open_reclaim(ctx, state);
2220 trace_nfs4_open_reclaim(ctx, 0, err);
2221 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2223 if (err != -NFS4ERR_DELAY)
2225 nfs4_handle_exception(server, err, &exception);
2226 } while (exception.retry);
2230 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2232 struct nfs_open_context *ctx;
2235 ctx = nfs4_state_find_open_context(state);
2238 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2239 nfs_state_clear_open_state_flags(state);
2240 ret = nfs4_do_open_reclaim(ctx, state);
2241 put_nfs_open_context(ctx);
2245 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)
2249 printk(KERN_ERR "NFS: %s: unhandled error "
2250 "%d.\n", __func__, err);
2258 case -NFS4ERR_BADSESSION:
2259 case -NFS4ERR_BADSLOT:
2260 case -NFS4ERR_BAD_HIGH_SLOT:
2261 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2262 case -NFS4ERR_DEADSESSION:
2264 case -NFS4ERR_STALE_CLIENTID:
2265 case -NFS4ERR_STALE_STATEID:
2266 /* Don't recall a delegation if it was lost */
2267 nfs4_schedule_lease_recovery(server->nfs_client);
2269 case -NFS4ERR_MOVED:
2270 nfs4_schedule_migration_recovery(server);
2272 case -NFS4ERR_LEASE_MOVED:
2273 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2275 case -NFS4ERR_DELEG_REVOKED:
2276 case -NFS4ERR_ADMIN_REVOKED:
2277 case -NFS4ERR_EXPIRED:
2278 case -NFS4ERR_BAD_STATEID:
2279 case -NFS4ERR_OPENMODE:
2280 nfs_inode_find_state_and_recover(state->inode,
2282 nfs4_schedule_stateid_recovery(server, state);
2284 case -NFS4ERR_DELAY:
2285 case -NFS4ERR_GRACE:
2289 case -NFS4ERR_DENIED:
2291 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2293 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2300 int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2301 struct nfs4_state *state, const nfs4_stateid *stateid)
2303 struct nfs_server *server = NFS_SERVER(state->inode);
2304 struct nfs4_opendata *opendata;
2307 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2308 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2309 if (IS_ERR(opendata))
2310 return PTR_ERR(opendata);
2311 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
2312 if (!test_bit(NFS_O_RDWR_STATE, &state->flags)) {
2313 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2317 if (!test_bit(NFS_O_WRONLY_STATE, &state->flags)) {
2318 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2322 if (!test_bit(NFS_O_RDONLY_STATE, &state->flags)) {
2323 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2327 nfs_state_clear_delegation(state);
2329 nfs4_opendata_put(opendata);
2330 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
2333 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2335 struct nfs4_opendata *data = calldata;
2337 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2338 &data->c_arg.seq_args, &data->c_res.seq_res, task);
2341 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2343 struct nfs4_opendata *data = calldata;
2345 nfs40_sequence_done(task, &data->c_res.seq_res);
2347 data->rpc_status = task->tk_status;
2348 if (data->rpc_status == 0) {
2349 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
2350 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2351 renew_lease(data->o_res.server, data->timestamp);
2352 data->rpc_done = true;
2356 static void nfs4_open_confirm_release(void *calldata)
2358 struct nfs4_opendata *data = calldata;
2359 struct nfs4_state *state = NULL;
2361 /* If this request hasn't been cancelled, do nothing */
2362 if (!data->cancelled)
2364 /* In case of error, no cleanup! */
2365 if (!data->rpc_done)
2367 state = nfs4_opendata_to_nfs4_state(data);
2369 nfs4_close_state(state, data->o_arg.fmode);
2371 nfs4_opendata_put(data);
2374 static const struct rpc_call_ops nfs4_open_confirm_ops = {
2375 .rpc_call_prepare = nfs4_open_confirm_prepare,
2376 .rpc_call_done = nfs4_open_confirm_done,
2377 .rpc_release = nfs4_open_confirm_release,
2381 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2383 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2385 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
2386 struct rpc_task *task;
2387 struct rpc_message msg = {
2388 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2389 .rpc_argp = &data->c_arg,
2390 .rpc_resp = &data->c_res,
2391 .rpc_cred = data->owner->so_cred,
2393 struct rpc_task_setup task_setup_data = {
2394 .rpc_client = server->client,
2395 .rpc_message = &msg,
2396 .callback_ops = &nfs4_open_confirm_ops,
2397 .callback_data = data,
2398 .workqueue = nfsiod_workqueue,
2399 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
2403 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2405 kref_get(&data->kref);
2406 data->rpc_done = false;
2407 data->rpc_status = 0;
2408 data->timestamp = jiffies;
2409 task = rpc_run_task(&task_setup_data);
2411 return PTR_ERR(task);
2412 status = rpc_wait_for_completion_task(task);
2414 data->cancelled = true;
2417 status = data->rpc_status;
2422 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
2424 struct nfs4_opendata *data = calldata;
2425 struct nfs4_state_owner *sp = data->owner;
2426 struct nfs_client *clp = sp->so_server->nfs_client;
2427 enum open_claim_type4 claim = data->o_arg.claim;
2429 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
2432 * Check if we still need to send an OPEN call, or if we can use
2433 * a delegation instead.
2435 if (data->state != NULL) {
2436 struct nfs_delegation *delegation;
2438 if (can_open_cached(data->state, data->o_arg.fmode,
2439 data->o_arg.open_flags, claim))
2442 delegation = nfs4_get_valid_delegation(data->state->inode);
2443 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
2444 goto unlock_no_action;
2447 /* Update client id. */
2448 data->o_arg.clientid = clp->cl_clientid;
2452 case NFS4_OPEN_CLAIM_PREVIOUS:
2453 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2454 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
2455 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
2457 case NFS4_OPEN_CLAIM_FH:
2458 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
2460 data->timestamp = jiffies;
2461 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
2462 &data->o_arg.seq_args,
2463 &data->o_res.seq_res,
2465 nfs_release_seqid(data->o_arg.seqid);
2467 /* Set the create mode (note dependency on the session type) */
2468 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2469 if (data->o_arg.open_flags & O_EXCL) {
2470 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2471 if (nfs4_has_persistent_session(clp))
2472 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2473 else if (clp->cl_mvops->minor_version > 0)
2474 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2478 trace_nfs4_cached_open(data->state);
2481 task->tk_action = NULL;
2483 nfs4_sequence_done(task, &data->o_res.seq_res);
2486 static void nfs4_open_done(struct rpc_task *task, void *calldata)
2488 struct nfs4_opendata *data = calldata;
2490 data->rpc_status = task->tk_status;
2492 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
2495 if (task->tk_status == 0) {
2496 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2497 switch (data->o_res.f_attr->mode & S_IFMT) {
2501 data->rpc_status = -ELOOP;
2504 data->rpc_status = -EISDIR;
2507 data->rpc_status = -ENOTDIR;
2510 renew_lease(data->o_res.server, data->timestamp);
2511 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2512 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2514 data->rpc_done = true;
2517 static void nfs4_open_release(void *calldata)
2519 struct nfs4_opendata *data = calldata;
2520 struct nfs4_state *state = NULL;
2522 /* If this request hasn't been cancelled, do nothing */
2523 if (!data->cancelled)
2525 /* In case of error, no cleanup! */
2526 if (data->rpc_status != 0 || !data->rpc_done)
2528 /* In case we need an open_confirm, no cleanup! */
2529 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2531 state = nfs4_opendata_to_nfs4_state(data);
2533 nfs4_close_state(state, data->o_arg.fmode);
2535 nfs4_opendata_put(data);
2538 static const struct rpc_call_ops nfs4_open_ops = {
2539 .rpc_call_prepare = nfs4_open_prepare,
2540 .rpc_call_done = nfs4_open_done,
2541 .rpc_release = nfs4_open_release,
2544 static int nfs4_run_open_task(struct nfs4_opendata *data,
2545 struct nfs_open_context *ctx)
2547 struct inode *dir = d_inode(data->dir);
2548 struct nfs_server *server = NFS_SERVER(dir);
2549 struct nfs_openargs *o_arg = &data->o_arg;
2550 struct nfs_openres *o_res = &data->o_res;
2551 struct rpc_task *task;
2552 struct rpc_message msg = {
2553 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2556 .rpc_cred = data->owner->so_cred,
2558 struct rpc_task_setup task_setup_data = {
2559 .rpc_client = server->client,
2560 .rpc_message = &msg,
2561 .callback_ops = &nfs4_open_ops,
2562 .callback_data = data,
2563 .workqueue = nfsiod_workqueue,
2564 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
2568 kref_get(&data->kref);
2569 data->rpc_done = false;
2570 data->rpc_status = 0;
2571 data->cancelled = false;
2572 data->is_recover = false;
2574 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
2575 data->is_recover = true;
2576 task_setup_data.flags |= RPC_TASK_TIMEOUT;
2578 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
2579 pnfs_lgopen_prepare(data, ctx);
2581 task = rpc_run_task(&task_setup_data);
2583 return PTR_ERR(task);
2584 status = rpc_wait_for_completion_task(task);
2586 data->cancelled = true;
2589 status = data->rpc_status;
2595 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2597 struct inode *dir = d_inode(data->dir);
2598 struct nfs_openres *o_res = &data->o_res;
2601 status = nfs4_run_open_task(data, NULL);
2602 if (status != 0 || !data->rpc_done)
2605 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2607 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
2608 status = _nfs4_proc_open_confirm(data);
2614 * Additional permission checks in order to distinguish between an
2615 * open for read, and an open for execute. This works around the
2616 * fact that NFSv4 OPEN treats read and execute permissions as being
2618 * Note that in the non-execute case, we want to turn off permission
2619 * checking if we just created a new file (POSIX open() semantics).
2621 static int nfs4_opendata_access(const struct cred *cred,
2622 struct nfs4_opendata *opendata,
2623 struct nfs4_state *state, fmode_t fmode,
2626 struct nfs_access_entry cache;
2629 /* access call failed or for some reason the server doesn't
2630 * support any access modes -- defer access call until later */
2631 if (opendata->o_res.access_supported == 0)
2636 * Use openflags to check for exec, because fmode won't
2637 * always have FMODE_EXEC set when file open for exec.
2639 if (openflags & __FMODE_EXEC) {
2640 /* ONLY check for exec rights */
2641 if (S_ISDIR(state->inode->i_mode))
2642 mask = NFS4_ACCESS_LOOKUP;
2644 mask = NFS4_ACCESS_EXECUTE;
2645 } else if ((fmode & FMODE_READ) && !opendata->file_created)
2646 mask = NFS4_ACCESS_READ;
2649 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2650 nfs_access_add_cache(state->inode, &cache);
2652 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2653 if ((mask & ~cache.mask & flags) == 0)
2660 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2662 static int _nfs4_proc_open(struct nfs4_opendata *data,
2663 struct nfs_open_context *ctx)
2665 struct inode *dir = d_inode(data->dir);
2666 struct nfs_server *server = NFS_SERVER(dir);
2667 struct nfs_openargs *o_arg = &data->o_arg;
2668 struct nfs_openres *o_res = &data->o_res;
2671 status = nfs4_run_open_task(data, ctx);
2672 if (!data->rpc_done)
2675 if (status == -NFS4ERR_BADNAME &&
2676 !(o_arg->open_flags & O_CREAT))
2681 nfs_fattr_map_and_free_names(server, &data->f_attr);
2683 if (o_arg->open_flags & O_CREAT) {
2684 if (o_arg->open_flags & O_EXCL)
2685 data->file_created = true;
2686 else if (o_res->cinfo.before != o_res->cinfo.after)
2687 data->file_created = true;
2688 if (data->file_created ||
2689 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
2690 nfs4_update_changeattr(dir, &o_res->cinfo,
2691 o_res->f_attr->time_start,
2692 NFS_INO_INVALID_DATA);
2694 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2695 server->caps &= ~NFS_CAP_POSIX_LOCK;
2696 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2697 status = _nfs4_proc_open_confirm(data);
2701 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2702 nfs4_sequence_free_slot(&o_res->seq_res);
2703 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr,
2704 o_res->f_label, NULL);
2711 * reclaim state on the server after a network partition.
2712 * Assumes caller holds the appropriate lock
2714 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2716 struct nfs4_opendata *opendata;
2719 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2720 NFS4_OPEN_CLAIM_FH);
2721 if (IS_ERR(opendata))
2722 return PTR_ERR(opendata);
2723 ret = nfs4_open_recover(opendata, state);
2725 d_drop(ctx->dentry);
2726 nfs4_opendata_put(opendata);
2730 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2732 struct nfs_server *server = NFS_SERVER(state->inode);
2733 struct nfs4_exception exception = { };
2737 err = _nfs4_open_expired(ctx, state);
2738 trace_nfs4_open_expired(ctx, 0, err);
2739 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2744 case -NFS4ERR_GRACE:
2745 case -NFS4ERR_DELAY:
2746 nfs4_handle_exception(server, err, &exception);
2749 } while (exception.retry);
2754 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2756 struct nfs_open_context *ctx;
2759 ctx = nfs4_state_find_open_context(state);
2762 ret = nfs4_do_open_expired(ctx, state);
2763 put_nfs_open_context(ctx);
2767 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2768 const nfs4_stateid *stateid)
2770 nfs_remove_bad_delegation(state->inode, stateid);
2771 nfs_state_clear_delegation(state);
2774 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2776 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2777 nfs_finish_clear_delegation_stateid(state, NULL);
2780 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2782 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2783 nfs40_clear_delegation_stateid(state);
2784 nfs_state_clear_open_state_flags(state);
2785 return nfs4_open_expired(sp, state);
2788 static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2789 nfs4_stateid *stateid,
2790 const struct cred *cred)
2792 return -NFS4ERR_BAD_STATEID;
2795 #if defined(CONFIG_NFS_V4_1)
2796 static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2797 nfs4_stateid *stateid,
2798 const struct cred *cred)
2802 switch (stateid->type) {
2805 case NFS4_INVALID_STATEID_TYPE:
2806 case NFS4_SPECIAL_STATEID_TYPE:
2807 return -NFS4ERR_BAD_STATEID;
2808 case NFS4_REVOKED_STATEID_TYPE:
2812 status = nfs41_test_stateid(server, stateid, cred);
2814 case -NFS4ERR_EXPIRED:
2815 case -NFS4ERR_ADMIN_REVOKED:
2816 case -NFS4ERR_DELEG_REVOKED:
2822 /* Ack the revoked state to the server */
2823 nfs41_free_stateid(server, stateid, cred, true);
2824 return -NFS4ERR_EXPIRED;
2827 static int nfs41_check_delegation_stateid(struct nfs4_state *state)
2829 struct nfs_server *server = NFS_SERVER(state->inode);
2830 nfs4_stateid stateid;
2831 struct nfs_delegation *delegation;
2832 const struct cred *cred = NULL;
2833 int status, ret = NFS_OK;
2835 /* Get the delegation credential for use by test/free_stateid */
2837 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2838 if (delegation == NULL) {
2840 nfs_state_clear_delegation(state);
2844 spin_lock(&delegation->lock);
2845 nfs4_stateid_copy(&stateid, &delegation->stateid);
2847 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2848 &delegation->flags)) {
2849 spin_unlock(&delegation->lock);
2854 if (delegation->cred)
2855 cred = get_cred(delegation->cred);
2856 spin_unlock(&delegation->lock);
2858 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
2859 trace_nfs4_test_delegation_stateid(state, NULL, status);
2860 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
2861 nfs_finish_clear_delegation_stateid(state, &stateid);
2869 static void nfs41_delegation_recover_stateid(struct nfs4_state *state)
2873 if (test_bit(NFS_DELEGATED_STATE, &state->flags) &&
2874 nfs4_copy_delegation_stateid(state->inode, state->state,
2876 nfs4_stateid_match_other(&state->stateid, &tmp))
2877 nfs_state_set_delegation(state, &tmp, state->state);
2879 nfs_state_clear_delegation(state);
2883 * nfs41_check_expired_locks - possibly free a lock stateid
2885 * @state: NFSv4 state for an inode
2887 * Returns NFS_OK if recovery for this stateid is now finished.
2888 * Otherwise a negative NFS4ERR value is returned.
2890 static int nfs41_check_expired_locks(struct nfs4_state *state)
2892 int status, ret = NFS_OK;
2893 struct nfs4_lock_state *lsp, *prev = NULL;
2894 struct nfs_server *server = NFS_SERVER(state->inode);
2896 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2899 spin_lock(&state->state_lock);
2900 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2901 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2902 const struct cred *cred = lsp->ls_state->owner->so_cred;
2904 refcount_inc(&lsp->ls_count);
2905 spin_unlock(&state->state_lock);
2907 nfs4_put_lock_state(prev);
2910 status = nfs41_test_and_free_expired_stateid(server,
2913 trace_nfs4_test_lock_stateid(state, lsp, status);
2914 if (status == -NFS4ERR_EXPIRED ||
2915 status == -NFS4ERR_BAD_STATEID) {
2916 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
2917 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
2918 if (!recover_lost_locks)
2919 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2920 } else if (status != NFS_OK) {
2922 nfs4_put_lock_state(prev);
2925 spin_lock(&state->state_lock);
2928 spin_unlock(&state->state_lock);
2929 nfs4_put_lock_state(prev);
2935 * nfs41_check_open_stateid - possibly free an open stateid
2937 * @state: NFSv4 state for an inode
2939 * Returns NFS_OK if recovery for this stateid is now finished.
2940 * Otherwise a negative NFS4ERR value is returned.
2942 static int nfs41_check_open_stateid(struct nfs4_state *state)
2944 struct nfs_server *server = NFS_SERVER(state->inode);
2945 nfs4_stateid *stateid = &state->open_stateid;
2946 const struct cred *cred = state->owner->so_cred;
2949 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0)
2950 return -NFS4ERR_BAD_STATEID;
2951 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
2952 trace_nfs4_test_open_stateid(state, NULL, status);
2953 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
2954 nfs_state_clear_open_state_flags(state);
2955 stateid->type = NFS4_INVALID_STATEID_TYPE;
2958 if (nfs_open_stateid_recover_openmode(state))
2959 return -NFS4ERR_OPENMODE;
2963 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2967 status = nfs41_check_delegation_stateid(state);
2968 if (status != NFS_OK)
2970 nfs41_delegation_recover_stateid(state);
2972 status = nfs41_check_expired_locks(state);
2973 if (status != NFS_OK)
2975 status = nfs41_check_open_stateid(state);
2976 if (status != NFS_OK)
2977 status = nfs4_open_expired(sp, state);
2983 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2984 * fields corresponding to attributes that were used to store the verifier.
2985 * Make sure we clobber those fields in the later setattr call
2987 static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2988 struct iattr *sattr, struct nfs4_label **label)
2990 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2995 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
2996 attrset[i] = opendata->o_res.attrset[i];
2997 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
2998 attrset[i] &= ~bitmask[i];
3001 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
3002 sattr->ia_valid : 0;
3004 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
3005 if (sattr->ia_valid & ATTR_ATIME_SET)
3006 ret |= ATTR_ATIME_SET;
3011 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
3012 if (sattr->ia_valid & ATTR_MTIME_SET)
3013 ret |= ATTR_MTIME_SET;
3018 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
3023 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
3024 int flags, struct nfs_open_context *ctx)
3026 struct nfs4_state_owner *sp = opendata->owner;
3027 struct nfs_server *server = sp->so_server;
3028 struct dentry *dentry;
3029 struct nfs4_state *state;
3030 fmode_t acc_mode = _nfs4_ctx_to_accessmode(ctx);
3031 struct inode *dir = d_inode(opendata->dir);
3032 unsigned long dir_verifier;
3036 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
3037 dir_verifier = nfs_save_change_attribute(dir);
3039 ret = _nfs4_proc_open(opendata, ctx);
3043 state = _nfs4_opendata_to_nfs4_state(opendata);
3044 ret = PTR_ERR(state);
3048 if (server->caps & NFS_CAP_POSIX_LOCK)
3049 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
3050 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
3051 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
3053 dentry = opendata->dentry;
3054 if (d_really_is_negative(dentry)) {
3055 struct dentry *alias;
3057 alias = d_exact_alias(dentry, state->inode);
3059 alias = d_splice_alias(igrab(state->inode), dentry);
3060 /* d_splice_alias() can't fail here - it's a non-directory */
3063 ctx->dentry = dentry = alias;
3067 switch(opendata->o_arg.claim) {
3070 case NFS4_OPEN_CLAIM_NULL:
3071 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
3072 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
3073 if (!opendata->rpc_done)
3075 if (opendata->o_res.delegation_type != 0)
3076 dir_verifier = nfs_save_change_attribute(dir);
3077 nfs_set_verifier(dentry, dir_verifier);
3080 /* Parse layoutget results before we check for access */
3081 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
3083 ret = nfs4_opendata_access(sp->so_cred, opendata, state,
3088 if (d_inode(dentry) == state->inode) {
3089 nfs_inode_attach_open_context(ctx);
3090 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
3091 nfs4_schedule_stateid_recovery(server, state);
3095 if (!opendata->cancelled)
3096 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
3101 * Returns a referenced nfs4_state
3103 static int _nfs4_do_open(struct inode *dir,
3104 struct nfs_open_context *ctx,
3106 const struct nfs4_open_createattrs *c,
3109 struct nfs4_state_owner *sp;
3110 struct nfs4_state *state = NULL;
3111 struct nfs_server *server = NFS_SERVER(dir);
3112 struct nfs4_opendata *opendata;
3113 struct dentry *dentry = ctx->dentry;
3114 const struct cred *cred = ctx->cred;
3115 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
3116 fmode_t fmode = _nfs4_ctx_to_openmode(ctx);
3117 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
3118 struct iattr *sattr = c->sattr;
3119 struct nfs4_label *label = c->label;
3120 struct nfs4_label *olabel = NULL;
3123 /* Protect against reboot recovery conflicts */
3125 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
3127 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
3130 status = nfs4_client_recover_expired_lease(server->nfs_client);
3132 goto err_put_state_owner;
3133 if (d_really_is_positive(dentry))
3134 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
3136 if (d_really_is_positive(dentry))
3137 claim = NFS4_OPEN_CLAIM_FH;
3138 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
3139 c, claim, GFP_KERNEL);
3140 if (opendata == NULL)
3141 goto err_put_state_owner;
3144 olabel = nfs4_label_alloc(server, GFP_KERNEL);
3145 if (IS_ERR(olabel)) {
3146 status = PTR_ERR(olabel);
3147 goto err_opendata_put;
3151 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
3152 if (!opendata->f_attr.mdsthreshold) {
3153 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
3154 if (!opendata->f_attr.mdsthreshold)
3155 goto err_free_label;
3157 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
3159 if (d_really_is_positive(dentry))
3160 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
3162 status = _nfs4_open_and_get_state(opendata, flags, ctx);
3164 goto err_free_label;
3167 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
3168 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
3169 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
3171 * send create attributes which was not set by open
3172 * with an extra setattr.
3174 if (attrs || label) {
3175 unsigned ia_old = sattr->ia_valid;
3177 sattr->ia_valid = attrs;
3178 nfs_fattr_init(opendata->o_res.f_attr);
3179 status = nfs4_do_setattr(state->inode, cred,
3180 opendata->o_res.f_attr, sattr,
3181 ctx, label, olabel);
3183 nfs_setattr_update_inode(state->inode, sattr,
3184 opendata->o_res.f_attr);
3185 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
3187 sattr->ia_valid = ia_old;
3190 if (opened && opendata->file_created)
3193 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
3194 *ctx_th = opendata->f_attr.mdsthreshold;
3195 opendata->f_attr.mdsthreshold = NULL;
3198 nfs4_label_free(olabel);
3200 nfs4_opendata_put(opendata);
3201 nfs4_put_state_owner(sp);
3204 nfs4_label_free(olabel);
3206 nfs4_opendata_put(opendata);
3207 err_put_state_owner:
3208 nfs4_put_state_owner(sp);
3214 static struct nfs4_state *nfs4_do_open(struct inode *dir,
3215 struct nfs_open_context *ctx,
3217 struct iattr *sattr,
3218 struct nfs4_label *label,
3221 struct nfs_server *server = NFS_SERVER(dir);
3222 struct nfs4_exception exception = {
3223 .interruptible = true,
3225 struct nfs4_state *res;
3226 struct nfs4_open_createattrs c = {
3230 [0] = (__u32)jiffies,
3231 [1] = (__u32)current->pid,
3237 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
3239 trace_nfs4_open_file(ctx, flags, status);
3242 /* NOTE: BAD_SEQID means the server and client disagree about the
3243 * book-keeping w.r.t. state-changing operations
3244 * (OPEN/CLOSE/LOCK/LOCKU...)
3245 * It is actually a sign of a bug on the client or on the server.
3247 * If we receive a BAD_SEQID error in the particular case of
3248 * doing an OPEN, we assume that nfs_increment_open_seqid() will
3249 * have unhashed the old state_owner for us, and that we can
3250 * therefore safely retry using a new one. We should still warn
3251 * the user though...
3253 if (status == -NFS4ERR_BAD_SEQID) {
3254 pr_warn_ratelimited("NFS: v4 server %s "
3255 " returned a bad sequence-id error!\n",
3256 NFS_SERVER(dir)->nfs_client->cl_hostname);
3257 exception.retry = 1;
3261 * BAD_STATEID on OPEN means that the server cancelled our
3262 * state before it received the OPEN_CONFIRM.
3263 * Recover by retrying the request as per the discussion
3264 * on Page 181 of RFC3530.
3266 if (status == -NFS4ERR_BAD_STATEID) {
3267 exception.retry = 1;
3270 if (status == -NFS4ERR_EXPIRED) {
3271 nfs4_schedule_lease_recovery(server->nfs_client);
3272 exception.retry = 1;
3275 if (status == -EAGAIN) {
3276 /* We must have found a delegation */
3277 exception.retry = 1;
3280 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3282 res = ERR_PTR(nfs4_handle_exception(server,
3283 status, &exception));
3284 } while (exception.retry);
3288 static int _nfs4_do_setattr(struct inode *inode,
3289 struct nfs_setattrargs *arg,
3290 struct nfs_setattrres *res,
3291 const struct cred *cred,
3292 struct nfs_open_context *ctx)
3294 struct nfs_server *server = NFS_SERVER(inode);
3295 struct rpc_message msg = {
3296 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3301 const struct cred *delegation_cred = NULL;
3302 unsigned long timestamp = jiffies;
3306 nfs_fattr_init(res->fattr);
3308 /* Servers should only apply open mode checks for file size changes */
3309 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
3311 nfs4_inode_make_writeable(inode);
3315 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
3316 /* Use that stateid */
3317 } else if (ctx != NULL && ctx->state) {
3318 struct nfs_lock_context *l_ctx;
3319 if (!nfs4_valid_open_stateid(ctx->state))
3321 l_ctx = nfs_get_lock_context(ctx);
3323 return PTR_ERR(l_ctx);
3324 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3325 &arg->stateid, &delegation_cred);
3326 nfs_put_lock_context(l_ctx);
3329 else if (status == -EAGAIN)
3333 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
3335 if (delegation_cred)
3336 msg.rpc_cred = delegation_cred;
3338 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3340 put_cred(delegation_cred);
3341 if (status == 0 && ctx != NULL)
3342 renew_lease(server, timestamp);
3343 trace_nfs4_setattr(inode, &arg->stateid, status);
3347 static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
3348 struct nfs_fattr *fattr, struct iattr *sattr,
3349 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
3350 struct nfs4_label *olabel)
3352 struct nfs_server *server = NFS_SERVER(inode);
3353 __u32 bitmask[NFS4_BITMASK_SZ];
3354 struct nfs4_state *state = ctx ? ctx->state : NULL;
3355 struct nfs_setattrargs arg = {
3356 .fh = NFS_FH(inode),
3362 struct nfs_setattrres res = {
3367 struct nfs4_exception exception = {
3370 .stateid = &arg.stateid,
3372 unsigned long adjust_flags = NFS_INO_INVALID_CHANGE;
3375 if (sattr->ia_valid & (ATTR_MODE | ATTR_KILL_SUID | ATTR_KILL_SGID))
3376 adjust_flags |= NFS_INO_INVALID_MODE;
3377 if (sattr->ia_valid & (ATTR_UID | ATTR_GID))
3378 adjust_flags |= NFS_INO_INVALID_OTHER;
3381 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, olabel),
3382 inode, adjust_flags);
3384 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
3386 case -NFS4ERR_OPENMODE:
3387 if (!(sattr->ia_valid & ATTR_SIZE)) {
3388 pr_warn_once("NFSv4: server %s is incorrectly "
3389 "applying open mode checks to "
3390 "a SETATTR that is not "
3391 "changing file size.\n",
3392 server->nfs_client->cl_hostname);
3394 if (state && !(state->state & FMODE_WRITE)) {
3396 if (sattr->ia_valid & ATTR_OPEN)
3401 err = nfs4_handle_exception(server, err, &exception);
3402 } while (exception.retry);
3408 nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3410 if (inode == NULL || !nfs_have_layout(inode))
3413 return pnfs_wait_on_layoutreturn(inode, task);
3417 * Update the seqid of an open stateid
3419 static void nfs4_sync_open_stateid(nfs4_stateid *dst,
3420 struct nfs4_state *state)
3427 if (!nfs4_valid_open_stateid(state))
3429 seq = read_seqbegin(&state->seqlock);
3430 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3431 nfs4_stateid_copy(dst, &state->open_stateid);
3432 if (read_seqretry(&state->seqlock, seq))
3436 seqid_open = state->open_stateid.seqid;
3437 if (read_seqretry(&state->seqlock, seq))
3440 dst_seqid = be32_to_cpu(dst->seqid);
3441 if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) < 0)
3442 dst->seqid = seqid_open;
3448 * Update the seqid of an open stateid after receiving
3449 * NFS4ERR_OLD_STATEID
3451 static bool nfs4_refresh_open_old_stateid(nfs4_stateid *dst,
3452 struct nfs4_state *state)
3457 int seq, status = -EAGAIN;
3462 if (!nfs4_valid_open_stateid(state))
3464 seq = read_seqbegin(&state->seqlock);
3465 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3466 if (read_seqretry(&state->seqlock, seq))
3471 write_seqlock(&state->seqlock);
3472 seqid_open = state->open_stateid.seqid;
3474 dst_seqid = be32_to_cpu(dst->seqid);
3476 /* Did another OPEN bump the state's seqid? try again: */
3477 if ((s32)(be32_to_cpu(seqid_open) - dst_seqid) > 0) {
3478 dst->seqid = seqid_open;
3479 write_sequnlock(&state->seqlock);
3484 /* server says we're behind but we haven't seen the update yet */
3485 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
3486 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
3487 write_sequnlock(&state->seqlock);
3488 trace_nfs4_close_stateid_update_wait(state->inode, dst, 0);
3490 if (signal_pending(current))
3493 if (schedule_timeout(5*HZ) != 0)
3496 finish_wait(&state->waitq, &wait);
3500 if (status == -EINTR)
3503 /* we slept the whole 5 seconds, we must have lost a seqid */
3504 dst->seqid = cpu_to_be32(dst_seqid + 1);
3512 struct nfs4_closedata {
3513 struct inode *inode;
3514 struct nfs4_state *state;
3515 struct nfs_closeargs arg;
3516 struct nfs_closeres res;
3518 struct nfs4_layoutreturn_args arg;
3519 struct nfs4_layoutreturn_res res;
3520 struct nfs4_xdr_opaque_data ld_private;
3524 struct nfs_fattr fattr;
3525 unsigned long timestamp;
3528 static void nfs4_free_closedata(void *data)
3530 struct nfs4_closedata *calldata = data;
3531 struct nfs4_state_owner *sp = calldata->state->owner;
3532 struct super_block *sb = calldata->state->inode->i_sb;
3534 if (calldata->lr.roc)
3535 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3536 calldata->res.lr_ret);
3537 nfs4_put_open_state(calldata->state);
3538 nfs_free_seqid(calldata->arg.seqid);
3539 nfs4_put_state_owner(sp);
3540 nfs_sb_deactive(sb);
3544 static void nfs4_close_done(struct rpc_task *task, void *data)
3546 struct nfs4_closedata *calldata = data;
3547 struct nfs4_state *state = calldata->state;
3548 struct nfs_server *server = NFS_SERVER(calldata->inode);
3549 nfs4_stateid *res_stateid = NULL;
3550 struct nfs4_exception exception = {
3552 .inode = calldata->inode,
3553 .stateid = &calldata->arg.stateid,
3556 dprintk("%s: begin!\n", __func__);
3557 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3559 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
3561 /* Handle Layoutreturn errors */
3562 if (pnfs_roc_done(task, &calldata->arg.lr_args, &calldata->res.lr_res,
3563 &calldata->res.lr_ret) == -EAGAIN)
3566 /* hmm. we are done with the inode, and in the process of freeing
3567 * the state_owner. we keep this around to process errors
3569 switch (task->tk_status) {
3571 res_stateid = &calldata->res.stateid;
3572 renew_lease(server, calldata->timestamp);
3574 case -NFS4ERR_ACCESS:
3575 if (calldata->arg.bitmask != NULL) {
3576 calldata->arg.bitmask = NULL;
3577 calldata->res.fattr = NULL;
3582 case -NFS4ERR_OLD_STATEID:
3583 /* Did we race with OPEN? */
3584 if (nfs4_refresh_open_old_stateid(&calldata->arg.stateid,
3588 case -NFS4ERR_ADMIN_REVOKED:
3589 case -NFS4ERR_STALE_STATEID:
3590 case -NFS4ERR_EXPIRED:
3591 nfs4_free_revoked_stateid(server,
3592 &calldata->arg.stateid,
3593 task->tk_msg.rpc_cred);
3595 case -NFS4ERR_BAD_STATEID:
3596 if (calldata->arg.fmode == 0)
3600 task->tk_status = nfs4_async_handle_exception(task,
3601 server, task->tk_status, &exception);
3602 if (exception.retry)
3605 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3606 res_stateid, calldata->arg.fmode);
3608 task->tk_status = 0;
3609 nfs_release_seqid(calldata->arg.seqid);
3610 nfs_refresh_inode(calldata->inode, &calldata->fattr);
3611 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
3614 task->tk_status = 0;
3615 rpc_restart_call_prepare(task);
3619 static void nfs4_close_prepare(struct rpc_task *task, void *data)
3621 struct nfs4_closedata *calldata = data;
3622 struct nfs4_state *state = calldata->state;
3623 struct inode *inode = calldata->inode;
3624 struct nfs_server *server = NFS_SERVER(inode);
3625 struct pnfs_layout_hdr *lo;
3626 bool is_rdonly, is_wronly, is_rdwr;
3629 dprintk("%s: begin!\n", __func__);
3630 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3633 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
3634 spin_lock(&state->owner->so_lock);
3635 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3636 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3637 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
3638 /* Calculate the change in open mode */
3639 calldata->arg.fmode = 0;
3640 if (state->n_rdwr == 0) {
3641 if (state->n_rdonly == 0)
3642 call_close |= is_rdonly;
3644 calldata->arg.fmode |= FMODE_READ;
3645 if (state->n_wronly == 0)
3646 call_close |= is_wronly;
3648 calldata->arg.fmode |= FMODE_WRITE;
3649 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3650 call_close |= is_rdwr;
3652 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3654 nfs4_sync_open_stateid(&calldata->arg.stateid, state);
3655 if (!nfs4_valid_open_stateid(state))
3657 spin_unlock(&state->owner->so_lock);
3660 /* Note: exit _without_ calling nfs4_close_done */
3664 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
3665 nfs_release_seqid(calldata->arg.seqid);
3669 lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3670 if (lo && !pnfs_layout_is_valid(lo)) {
3671 calldata->arg.lr_args = NULL;
3672 calldata->res.lr_res = NULL;
3675 if (calldata->arg.fmode == 0)
3676 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
3678 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
3679 /* Close-to-open cache consistency revalidation */
3680 if (!nfs4_have_delegation(inode, FMODE_READ)) {
3681 nfs4_bitmask_set(calldata->arg.bitmask_store,
3682 server->cache_consistency_bitmask,
3683 inode, server, NULL);
3684 calldata->arg.bitmask = calldata->arg.bitmask_store;
3686 calldata->arg.bitmask = NULL;
3689 calldata->arg.share_access =
3690 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3691 calldata->arg.fmode, 0);
3693 if (calldata->res.fattr == NULL)
3694 calldata->arg.bitmask = NULL;
3695 else if (calldata->arg.bitmask == NULL)
3696 calldata->res.fattr = NULL;
3697 calldata->timestamp = jiffies;
3698 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
3699 &calldata->arg.seq_args,
3700 &calldata->res.seq_res,
3702 nfs_release_seqid(calldata->arg.seqid);
3703 dprintk("%s: done!\n", __func__);
3706 task->tk_action = NULL;
3708 nfs4_sequence_done(task, &calldata->res.seq_res);
3711 static const struct rpc_call_ops nfs4_close_ops = {
3712 .rpc_call_prepare = nfs4_close_prepare,
3713 .rpc_call_done = nfs4_close_done,
3714 .rpc_release = nfs4_free_closedata,
3718 * It is possible for data to be read/written from a mem-mapped file
3719 * after the sys_close call (which hits the vfs layer as a flush).
3720 * This means that we can't safely call nfsv4 close on a file until
3721 * the inode is cleared. This in turn means that we are not good
3722 * NFSv4 citizens - we do not indicate to the server to update the file's
3723 * share state even when we are done with one of the three share
3724 * stateid's in the inode.
3726 * NOTE: Caller must be holding the sp->so_owner semaphore!
3728 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
3730 struct nfs_server *server = NFS_SERVER(state->inode);
3731 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
3732 struct nfs4_closedata *calldata;
3733 struct nfs4_state_owner *sp = state->owner;
3734 struct rpc_task *task;
3735 struct rpc_message msg = {
3736 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3737 .rpc_cred = state->owner->so_cred,
3739 struct rpc_task_setup task_setup_data = {
3740 .rpc_client = server->client,
3741 .rpc_message = &msg,
3742 .callback_ops = &nfs4_close_ops,
3743 .workqueue = nfsiod_workqueue,
3744 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
3746 int status = -ENOMEM;
3748 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3749 &task_setup_data.rpc_client, &msg);
3751 calldata = kzalloc(sizeof(*calldata), gfp_mask);
3752 if (calldata == NULL)
3754 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
3755 calldata->inode = state->inode;
3756 calldata->state = state;
3757 calldata->arg.fh = NFS_FH(state->inode);
3758 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3759 goto out_free_calldata;
3760 /* Serialization for the sequence id */
3761 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3762 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
3763 if (IS_ERR(calldata->arg.seqid))
3764 goto out_free_calldata;
3765 nfs_fattr_init(&calldata->fattr);
3766 calldata->arg.fmode = 0;
3767 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
3768 calldata->res.fattr = &calldata->fattr;
3769 calldata->res.seqid = calldata->arg.seqid;
3770 calldata->res.server = server;
3771 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3772 calldata->lr.roc = pnfs_roc(state->inode,
3773 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3774 if (calldata->lr.roc) {
3775 calldata->arg.lr_args = &calldata->lr.arg;
3776 calldata->res.lr_res = &calldata->lr.res;
3778 nfs_sb_active(calldata->inode->i_sb);
3780 msg.rpc_argp = &calldata->arg;
3781 msg.rpc_resp = &calldata->res;
3782 task_setup_data.callback_data = calldata;
3783 task = rpc_run_task(&task_setup_data);
3785 return PTR_ERR(task);
3788 status = rpc_wait_for_completion_task(task);
3794 nfs4_put_open_state(state);
3795 nfs4_put_state_owner(sp);
3799 static struct inode *
3800 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3801 int open_flags, struct iattr *attr, int *opened)
3803 struct nfs4_state *state;
3804 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3806 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
3808 /* Protect against concurrent sillydeletes */
3809 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
3811 nfs4_label_release_security(label);
3814 return ERR_CAST(state);
3815 return state->inode;
3818 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
3820 if (ctx->state == NULL)
3823 nfs4_close_sync(ctx->state, _nfs4_ctx_to_openmode(ctx));
3825 nfs4_close_state(ctx->state, _nfs4_ctx_to_openmode(ctx));
3828 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3829 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3830 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_XATTR_SUPPORT - 1UL)
3832 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3834 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
3835 struct nfs4_server_caps_arg args = {
3839 struct nfs4_server_caps_res res = {};
3840 struct rpc_message msg = {
3841 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
3848 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3849 FATTR4_WORD0_FH_EXPIRE_TYPE |
3850 FATTR4_WORD0_LINK_SUPPORT |
3851 FATTR4_WORD0_SYMLINK_SUPPORT |
3852 FATTR4_WORD0_ACLSUPPORT;
3854 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3856 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3858 /* Sanity check the server answers */
3859 switch (minorversion) {
3861 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3862 res.attr_bitmask[2] = 0;
3865 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3868 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3870 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
3871 server->caps &= ~(NFS_CAP_ACLS | NFS_CAP_HARDLINKS |
3872 NFS_CAP_SYMLINKS| NFS_CAP_SECURITY_LABEL);
3873 server->fattr_valid = NFS_ATTR_FATTR_V4;
3874 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3875 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3876 server->caps |= NFS_CAP_ACLS;
3877 if (res.has_links != 0)
3878 server->caps |= NFS_CAP_HARDLINKS;
3879 if (res.has_symlinks != 0)
3880 server->caps |= NFS_CAP_SYMLINKS;
3881 if (!(res.attr_bitmask[0] & FATTR4_WORD0_FILEID))
3882 server->fattr_valid &= ~NFS_ATTR_FATTR_FILEID;
3883 if (!(res.attr_bitmask[1] & FATTR4_WORD1_MODE))
3884 server->fattr_valid &= ~NFS_ATTR_FATTR_MODE;
3885 if (!(res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS))
3886 server->fattr_valid &= ~NFS_ATTR_FATTR_NLINK;
3887 if (!(res.attr_bitmask[1] & FATTR4_WORD1_OWNER))
3888 server->fattr_valid &= ~(NFS_ATTR_FATTR_OWNER |
3889 NFS_ATTR_FATTR_OWNER_NAME);
3890 if (!(res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP))
3891 server->fattr_valid &= ~(NFS_ATTR_FATTR_GROUP |
3892 NFS_ATTR_FATTR_GROUP_NAME);
3893 if (!(res.attr_bitmask[1] & FATTR4_WORD1_SPACE_USED))
3894 server->fattr_valid &= ~NFS_ATTR_FATTR_SPACE_USED;
3895 if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS))
3896 server->fattr_valid &= ~NFS_ATTR_FATTR_ATIME;
3897 if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA))
3898 server->fattr_valid &= ~NFS_ATTR_FATTR_CTIME;
3899 if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY))
3900 server->fattr_valid &= ~NFS_ATTR_FATTR_MTIME;
3901 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3902 if (!(res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL))
3903 server->fattr_valid &= ~NFS_ATTR_FATTR_V4_SECURITY_LABEL;
3905 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3906 sizeof(server->attr_bitmask));
3907 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3909 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3910 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3911 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
3912 server->cache_consistency_bitmask[2] = 0;
3914 /* Avoid a regression due to buggy server */
3915 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3916 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
3917 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3918 sizeof(server->exclcreat_bitmask));
3920 server->acl_bitmask = res.acl_bitmask;
3921 server->fh_expire_type = res.fh_expire_type;
3927 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3929 struct nfs4_exception exception = {
3930 .interruptible = true,
3934 err = nfs4_handle_exception(server,
3935 _nfs4_server_capabilities(server, fhandle),
3937 } while (exception.retry);
3941 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3942 struct nfs_fsinfo *info)
3945 struct nfs4_lookup_root_arg args = {
3948 struct nfs4_lookup_res res = {
3950 .fattr = info->fattr,
3953 struct rpc_message msg = {
3954 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3959 bitmask[0] = nfs4_fattr_bitmap[0];
3960 bitmask[1] = nfs4_fattr_bitmap[1];
3962 * Process the label in the upcoming getfattr
3964 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3966 nfs_fattr_init(info->fattr);
3967 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3970 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3971 struct nfs_fsinfo *info)
3973 struct nfs4_exception exception = {
3974 .interruptible = true,
3978 err = _nfs4_lookup_root(server, fhandle, info);
3979 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
3982 case -NFS4ERR_WRONGSEC:
3985 err = nfs4_handle_exception(server, err, &exception);
3987 } while (exception.retry);
3992 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3993 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3995 struct rpc_auth_create_args auth_args = {
3996 .pseudoflavor = flavor,
3998 struct rpc_auth *auth;
4000 auth = rpcauth_create(&auth_args, server->client);
4003 return nfs4_lookup_root(server, fhandle, info);
4007 * Retry pseudoroot lookup with various security flavors. We do this when:
4009 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
4010 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
4012 * Returns zero on success, or a negative NFS4ERR value, or a
4013 * negative errno value.
4015 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
4016 struct nfs_fsinfo *info)
4018 /* Per 3530bis 15.33.5 */
4019 static const rpc_authflavor_t flav_array[] = {
4023 RPC_AUTH_UNIX, /* courtesy */
4026 int status = -EPERM;
4029 if (server->auth_info.flavor_len > 0) {
4030 /* try each flavor specified by user */
4031 for (i = 0; i < server->auth_info.flavor_len; i++) {
4032 status = nfs4_lookup_root_sec(server, fhandle, info,
4033 server->auth_info.flavors[i]);
4034 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4039 /* no flavors specified by user, try default list */
4040 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
4041 status = nfs4_lookup_root_sec(server, fhandle, info,
4043 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4050 * -EACCES could mean that the user doesn't have correct permissions
4051 * to access the mount. It could also mean that we tried to mount
4052 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
4053 * existing mount programs don't handle -EACCES very well so it should
4054 * be mapped to -EPERM instead.
4056 if (status == -EACCES)
4062 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
4063 * @server: initialized nfs_server handle
4064 * @fhandle: we fill in the pseudo-fs root file handle
4065 * @info: we fill in an FSINFO struct
4066 * @auth_probe: probe the auth flavours
4068 * Returns zero on success, or a negative errno.
4070 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
4071 struct nfs_fsinfo *info,
4077 status = nfs4_lookup_root(server, fhandle, info);
4079 if (auth_probe || status == NFS4ERR_WRONGSEC)
4080 status = server->nfs_client->cl_mvops->find_root_sec(server,
4084 status = nfs4_server_capabilities(server, fhandle);
4086 status = nfs4_do_fsinfo(server, fhandle, info);
4088 return nfs4_map_errors(status);
4091 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
4092 struct nfs_fsinfo *info)
4095 struct nfs_fattr *fattr = info->fattr;
4096 struct nfs4_label *label = fattr->label;
4098 error = nfs4_server_capabilities(server, mntfh);
4100 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
4104 error = nfs4_proc_getattr(server, mntfh, fattr, label, NULL);
4106 dprintk("nfs4_get_root: getattr error = %d\n", -error);
4110 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
4111 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
4112 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
4119 * Get locations and (maybe) other attributes of a referral.
4120 * Note that we'll actually follow the referral later when
4121 * we detect fsid mismatch in inode revalidation
4123 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
4124 const struct qstr *name, struct nfs_fattr *fattr,
4125 struct nfs_fh *fhandle)
4127 int status = -ENOMEM;
4128 struct page *page = NULL;
4129 struct nfs4_fs_locations *locations = NULL;
4131 page = alloc_page(GFP_KERNEL);
4134 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4135 if (locations == NULL)
4138 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
4143 * If the fsid didn't change, this is a migration event, not a
4144 * referral. Cause us to drop into the exception handler, which
4145 * will kick off migration recovery.
4147 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
4148 dprintk("%s: server did not return a different fsid for"
4149 " a referral at %s\n", __func__, name->name);
4150 status = -NFS4ERR_MOVED;
4153 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
4154 nfs_fixup_referral_attributes(&locations->fattr);
4156 /* replace the lookup nfs_fattr with the locations nfs_fattr */
4157 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
4158 memset(fhandle, 0, sizeof(struct nfs_fh));
4166 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4167 struct nfs_fattr *fattr, struct nfs4_label *label,
4168 struct inode *inode)
4170 __u32 bitmask[NFS4_BITMASK_SZ];
4171 struct nfs4_getattr_arg args = {
4175 struct nfs4_getattr_res res = {
4180 struct rpc_message msg = {
4181 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4185 unsigned short task_flags = 0;
4187 /* Is this is an attribute revalidation, subject to softreval? */
4188 if (inode && (server->flags & NFS_MOUNT_SOFTREVAL))
4189 task_flags |= RPC_TASK_TIMEOUT;
4191 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode, 0);
4192 nfs_fattr_init(fattr);
4193 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4194 return nfs4_do_call_sync(server->client, server, &msg,
4195 &args.seq_args, &res.seq_res, task_flags);
4198 int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4199 struct nfs_fattr *fattr, struct nfs4_label *label,
4200 struct inode *inode)
4202 struct nfs4_exception exception = {
4203 .interruptible = true,
4207 err = _nfs4_proc_getattr(server, fhandle, fattr, label, inode);
4208 trace_nfs4_getattr(server, fhandle, fattr, err);
4209 err = nfs4_handle_exception(server, err,
4211 } while (exception.retry);
4216 * The file is not closed if it is opened due to the a request to change
4217 * the size of the file. The open call will not be needed once the
4218 * VFS layer lookup-intents are implemented.
4220 * Close is called when the inode is destroyed.
4221 * If we haven't opened the file for O_WRONLY, we
4222 * need to in the size_change case to obtain a stateid.
4225 * Because OPEN is always done by name in nfsv4, it is
4226 * possible that we opened a different file by the same
4227 * name. We can recognize this race condition, but we
4228 * can't do anything about it besides returning an error.
4230 * This will be fixed with VFS changes (lookup-intent).
4233 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
4234 struct iattr *sattr)
4236 struct inode *inode = d_inode(dentry);
4237 const struct cred *cred = NULL;
4238 struct nfs_open_context *ctx = NULL;
4239 struct nfs4_label *label = NULL;
4242 if (pnfs_ld_layoutret_on_setattr(inode) &&
4243 sattr->ia_valid & ATTR_SIZE &&
4244 sattr->ia_size < i_size_read(inode))
4245 pnfs_commit_and_return_layout(inode);
4247 nfs_fattr_init(fattr);
4249 /* Deal with open(O_TRUNC) */
4250 if (sattr->ia_valid & ATTR_OPEN)
4251 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
4253 /* Optimization: if the end result is no change, don't RPC */
4254 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
4257 /* Search for an existing open(O_WRITE) file */
4258 if (sattr->ia_valid & ATTR_FILE) {
4260 ctx = nfs_file_open_context(sattr->ia_file);
4265 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4267 return PTR_ERR(label);
4269 /* Return any delegations if we're going to change ACLs */
4270 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
4271 nfs4_inode_make_writeable(inode);
4273 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
4275 nfs_setattr_update_inode(inode, sattr, fattr);
4276 nfs_setsecurity(inode, fattr, label);
4278 nfs4_label_free(label);
4282 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
4283 struct dentry *dentry, struct nfs_fh *fhandle,
4284 struct nfs_fattr *fattr, struct nfs4_label *label)
4286 struct nfs_server *server = NFS_SERVER(dir);
4288 struct nfs4_lookup_arg args = {
4289 .bitmask = server->attr_bitmask,
4290 .dir_fh = NFS_FH(dir),
4291 .name = &dentry->d_name,
4293 struct nfs4_lookup_res res = {
4299 struct rpc_message msg = {
4300 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4304 unsigned short task_flags = 0;
4306 /* Is this is an attribute revalidation, subject to softreval? */
4307 if (nfs_lookup_is_soft_revalidate(dentry))
4308 task_flags |= RPC_TASK_TIMEOUT;
4310 args.bitmask = nfs4_bitmask(server, label);
4312 nfs_fattr_init(fattr);
4314 dprintk("NFS call lookup %pd2\n", dentry);
4315 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4316 status = nfs4_do_call_sync(clnt, server, &msg,
4317 &args.seq_args, &res.seq_res, task_flags);
4318 dprintk("NFS reply lookup: %d\n", status);
4322 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
4324 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4325 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
4326 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4330 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
4331 struct dentry *dentry, struct nfs_fh *fhandle,
4332 struct nfs_fattr *fattr, struct nfs4_label *label)
4334 struct nfs4_exception exception = {
4335 .interruptible = true,
4337 struct rpc_clnt *client = *clnt;
4338 const struct qstr *name = &dentry->d_name;
4341 err = _nfs4_proc_lookup(client, dir, dentry, fhandle, fattr, label);
4342 trace_nfs4_lookup(dir, name, err);
4344 case -NFS4ERR_BADNAME:
4347 case -NFS4ERR_MOVED:
4348 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
4349 if (err == -NFS4ERR_MOVED)
4350 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4352 case -NFS4ERR_WRONGSEC:
4354 if (client != *clnt)
4356 client = nfs4_negotiate_security(client, dir, name);
4358 return PTR_ERR(client);
4360 exception.retry = 1;
4363 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4365 } while (exception.retry);
4370 else if (client != *clnt)
4371 rpc_shutdown_client(client);
4376 static int nfs4_proc_lookup(struct inode *dir, struct dentry *dentry,
4377 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4378 struct nfs4_label *label)
4381 struct rpc_clnt *client = NFS_CLIENT(dir);
4383 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr, label);
4384 if (client != NFS_CLIENT(dir)) {
4385 rpc_shutdown_client(client);
4386 nfs_fixup_secinfo_attributes(fattr);
4392 nfs4_proc_lookup_mountpoint(struct inode *dir, struct dentry *dentry,
4393 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4395 struct rpc_clnt *client = NFS_CLIENT(dir);
4398 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr, NULL);
4400 return ERR_PTR(status);
4401 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
4404 static int _nfs4_proc_lookupp(struct inode *inode,
4405 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4406 struct nfs4_label *label)
4408 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4409 struct nfs_server *server = NFS_SERVER(inode);
4411 struct nfs4_lookupp_arg args = {
4412 .bitmask = server->attr_bitmask,
4413 .fh = NFS_FH(inode),
4415 struct nfs4_lookupp_res res = {
4421 struct rpc_message msg = {
4422 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4426 unsigned short task_flags = 0;
4428 if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL)
4429 task_flags |= RPC_TASK_TIMEOUT;
4431 args.bitmask = nfs4_bitmask(server, label);
4433 nfs_fattr_init(fattr);
4435 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4436 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4437 &res.seq_res, task_flags);
4438 dprintk("NFS reply lookupp: %d\n", status);
4442 static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4443 struct nfs_fattr *fattr, struct nfs4_label *label)
4445 struct nfs4_exception exception = {
4446 .interruptible = true,
4450 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4451 trace_nfs4_lookupp(inode, err);
4452 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4454 } while (exception.retry);
4458 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4460 struct nfs_server *server = NFS_SERVER(inode);
4461 struct nfs4_accessargs args = {
4462 .fh = NFS_FH(inode),
4463 .access = entry->mask,
4465 struct nfs4_accessres res = {
4468 struct rpc_message msg = {
4469 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4472 .rpc_cred = entry->cred,
4476 if (!nfs4_have_delegation(inode, FMODE_READ)) {
4477 res.fattr = nfs_alloc_fattr();
4478 if (res.fattr == NULL)
4480 args.bitmask = server->cache_consistency_bitmask;
4482 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4484 nfs_access_set_mask(entry, res.access);
4486 nfs_refresh_inode(inode, res.fattr);
4488 nfs_free_fattr(res.fattr);
4492 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4494 struct nfs4_exception exception = {
4495 .interruptible = true,
4499 err = _nfs4_proc_access(inode, entry);
4500 trace_nfs4_access(inode, err);
4501 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4503 } while (exception.retry);
4508 * TODO: For the time being, we don't try to get any attributes
4509 * along with any of the zero-copy operations READ, READDIR,
4512 * In the case of the first three, we want to put the GETATTR
4513 * after the read-type operation -- this is because it is hard
4514 * to predict the length of a GETATTR response in v4, and thus
4515 * align the READ data correctly. This means that the GETATTR
4516 * may end up partially falling into the page cache, and we should
4517 * shift it into the 'tail' of the xdr_buf before processing.
4518 * To do this efficiently, we need to know the total length
4519 * of data received, which doesn't seem to be available outside
4522 * In the case of WRITE, we also want to put the GETATTR after
4523 * the operation -- in this case because we want to make sure
4524 * we get the post-operation mtime and size.
4526 * Both of these changes to the XDR layer would in fact be quite
4527 * minor, but I decided to leave them for a subsequent patch.
4529 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4530 unsigned int pgbase, unsigned int pglen)
4532 struct nfs4_readlink args = {
4533 .fh = NFS_FH(inode),
4538 struct nfs4_readlink_res res;
4539 struct rpc_message msg = {
4540 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4545 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
4548 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4549 unsigned int pgbase, unsigned int pglen)
4551 struct nfs4_exception exception = {
4552 .interruptible = true,
4556 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4557 trace_nfs4_readlink(inode, err);
4558 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4560 } while (exception.retry);
4565 * This is just for mknod. open(O_CREAT) will always do ->open_context().
4568 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
4571 struct nfs_server *server = NFS_SERVER(dir);
4572 struct nfs4_label l, *ilabel = NULL;
4573 struct nfs_open_context *ctx;
4574 struct nfs4_state *state;
4577 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
4579 return PTR_ERR(ctx);
4581 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4583 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4584 sattr->ia_mode &= ~current_umask();
4585 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
4586 if (IS_ERR(state)) {
4587 status = PTR_ERR(state);
4591 nfs4_label_release_security(ilabel);
4592 put_nfs_open_context(ctx);
4597 _nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
4599 struct nfs_server *server = NFS_SERVER(dir);
4600 struct nfs_removeargs args = {
4604 struct nfs_removeres res = {
4607 struct rpc_message msg = {
4608 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4612 unsigned long timestamp = jiffies;
4615 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
4617 spin_lock(&dir->i_lock);
4618 /* Removing a directory decrements nlink in the parent */
4619 if (ftype == NF4DIR && dir->i_nlink > 2)
4620 nfs4_dec_nlink_locked(dir);
4621 nfs4_update_changeattr_locked(dir, &res.cinfo, timestamp,
4622 NFS_INO_INVALID_DATA);
4623 spin_unlock(&dir->i_lock);
4628 static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4630 struct nfs4_exception exception = {
4631 .interruptible = true,
4633 struct inode *inode = d_inode(dentry);
4637 if (inode->i_nlink == 1)
4638 nfs4_inode_return_delegation(inode);
4640 nfs4_inode_make_writeable(inode);
4643 err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
4644 trace_nfs4_remove(dir, &dentry->d_name, err);
4645 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4647 } while (exception.retry);
4651 static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
4653 struct nfs4_exception exception = {
4654 .interruptible = true,
4659 err = _nfs4_proc_remove(dir, name, NF4DIR);
4660 trace_nfs4_remove(dir, name, err);
4661 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4663 } while (exception.retry);
4667 static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4668 struct dentry *dentry,
4669 struct inode *inode)
4671 struct nfs_removeargs *args = msg->rpc_argp;
4672 struct nfs_removeres *res = msg->rpc_resp;
4674 res->server = NFS_SB(dentry->d_sb);
4675 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
4676 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
4678 nfs_fattr_init(res->dir_attr);
4681 nfs4_inode_return_delegation(inode);
4684 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4686 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
4687 &data->args.seq_args,
4692 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
4694 struct nfs_unlinkdata *data = task->tk_calldata;
4695 struct nfs_removeres *res = &data->res;
4697 if (!nfs4_sequence_done(task, &res->seq_res))
4699 if (nfs4_async_handle_error(task, res->server, NULL,
4700 &data->timeout) == -EAGAIN)
4702 if (task->tk_status == 0)
4703 nfs4_update_changeattr(dir, &res->cinfo,
4704 res->dir_attr->time_start,
4705 NFS_INO_INVALID_DATA);
4709 static void nfs4_proc_rename_setup(struct rpc_message *msg,
4710 struct dentry *old_dentry,
4711 struct dentry *new_dentry)
4713 struct nfs_renameargs *arg = msg->rpc_argp;
4714 struct nfs_renameres *res = msg->rpc_resp;
4715 struct inode *old_inode = d_inode(old_dentry);
4716 struct inode *new_inode = d_inode(new_dentry);
4719 nfs4_inode_make_writeable(old_inode);
4721 nfs4_inode_return_delegation(new_inode);
4722 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
4723 res->server = NFS_SB(old_dentry->d_sb);
4724 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
4727 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4729 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
4730 &data->args.seq_args,
4735 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4736 struct inode *new_dir)
4738 struct nfs_renamedata *data = task->tk_calldata;
4739 struct nfs_renameres *res = &data->res;
4741 if (!nfs4_sequence_done(task, &res->seq_res))
4743 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
4746 if (task->tk_status == 0) {
4747 if (new_dir != old_dir) {
4748 /* Note: If we moved a directory, nlink will change */
4749 nfs4_update_changeattr(old_dir, &res->old_cinfo,
4750 res->old_fattr->time_start,
4751 NFS_INO_INVALID_NLINK |
4752 NFS_INO_INVALID_DATA);
4753 nfs4_update_changeattr(new_dir, &res->new_cinfo,
4754 res->new_fattr->time_start,
4755 NFS_INO_INVALID_NLINK |
4756 NFS_INO_INVALID_DATA);
4758 nfs4_update_changeattr(old_dir, &res->old_cinfo,
4759 res->old_fattr->time_start,
4760 NFS_INO_INVALID_DATA);
4765 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4767 struct nfs_server *server = NFS_SERVER(inode);
4768 __u32 bitmask[NFS4_BITMASK_SZ];
4769 struct nfs4_link_arg arg = {
4770 .fh = NFS_FH(inode),
4771 .dir_fh = NFS_FH(dir),
4775 struct nfs4_link_res res = {
4779 struct rpc_message msg = {
4780 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4784 int status = -ENOMEM;
4786 res.fattr = nfs_alloc_fattr();
4787 if (res.fattr == NULL)
4790 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4791 if (IS_ERR(res.label)) {
4792 status = PTR_ERR(res.label);
4796 nfs4_inode_make_writeable(inode);
4797 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, res.label), inode,
4798 NFS_INO_INVALID_CHANGE);
4799 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4801 nfs4_update_changeattr(dir, &res.cinfo, res.fattr->time_start,
4802 NFS_INO_INVALID_DATA);
4803 nfs4_inc_nlink(inode);
4804 status = nfs_post_op_update_inode(inode, res.fattr);
4806 nfs_setsecurity(inode, res.fattr, res.label);
4810 nfs4_label_free(res.label);
4813 nfs_free_fattr(res.fattr);
4817 static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4819 struct nfs4_exception exception = {
4820 .interruptible = true,
4824 err = nfs4_handle_exception(NFS_SERVER(inode),
4825 _nfs4_proc_link(inode, dir, name),
4827 } while (exception.retry);
4831 struct nfs4_createdata {
4832 struct rpc_message msg;
4833 struct nfs4_create_arg arg;
4834 struct nfs4_create_res res;
4836 struct nfs_fattr fattr;
4837 struct nfs4_label *label;
4840 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
4841 const struct qstr *name, struct iattr *sattr, u32 ftype)
4843 struct nfs4_createdata *data;
4845 data = kzalloc(sizeof(*data), GFP_KERNEL);
4847 struct nfs_server *server = NFS_SERVER(dir);
4849 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4850 if (IS_ERR(data->label))
4853 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4854 data->msg.rpc_argp = &data->arg;
4855 data->msg.rpc_resp = &data->res;
4856 data->arg.dir_fh = NFS_FH(dir);
4857 data->arg.server = server;
4858 data->arg.name = name;
4859 data->arg.attrs = sattr;
4860 data->arg.ftype = ftype;
4861 data->arg.bitmask = nfs4_bitmask(server, data->label);
4862 data->arg.umask = current_umask();
4863 data->res.server = server;
4864 data->res.fh = &data->fh;
4865 data->res.fattr = &data->fattr;
4866 data->res.label = data->label;
4867 nfs_fattr_init(data->res.fattr);
4875 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4877 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
4878 &data->arg.seq_args, &data->res.seq_res, 1);
4880 spin_lock(&dir->i_lock);
4881 /* Creating a directory bumps nlink in the parent */
4882 if (data->arg.ftype == NF4DIR)
4883 nfs4_inc_nlink_locked(dir);
4884 nfs4_update_changeattr_locked(dir, &data->res.dir_cinfo,
4885 data->res.fattr->time_start,
4886 NFS_INO_INVALID_DATA);
4887 spin_unlock(&dir->i_lock);
4888 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
4893 static void nfs4_free_createdata(struct nfs4_createdata *data)
4895 nfs4_label_free(data->label);
4899 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4900 struct page *page, unsigned int len, struct iattr *sattr,
4901 struct nfs4_label *label)
4903 struct nfs4_createdata *data;
4904 int status = -ENAMETOOLONG;
4906 if (len > NFS4_MAXPATHLEN)
4910 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4914 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4915 data->arg.u.symlink.pages = &page;
4916 data->arg.u.symlink.len = len;
4917 data->arg.label = label;
4919 status = nfs4_do_create(dir, dentry, data);
4921 nfs4_free_createdata(data);
4926 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4927 struct page *page, unsigned int len, struct iattr *sattr)
4929 struct nfs4_exception exception = {
4930 .interruptible = true,
4932 struct nfs4_label l, *label = NULL;
4935 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4938 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4939 trace_nfs4_symlink(dir, &dentry->d_name, err);
4940 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4942 } while (exception.retry);
4944 nfs4_label_release_security(label);
4948 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4949 struct iattr *sattr, struct nfs4_label *label)
4951 struct nfs4_createdata *data;
4952 int status = -ENOMEM;
4954 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4958 data->arg.label = label;
4959 status = nfs4_do_create(dir, dentry, data);
4961 nfs4_free_createdata(data);
4966 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4967 struct iattr *sattr)
4969 struct nfs_server *server = NFS_SERVER(dir);
4970 struct nfs4_exception exception = {
4971 .interruptible = true,
4973 struct nfs4_label l, *label = NULL;
4976 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4978 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4979 sattr->ia_mode &= ~current_umask();
4981 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4982 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4983 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4985 } while (exception.retry);
4986 nfs4_label_release_security(label);
4991 static int _nfs4_proc_readdir(struct nfs_readdir_arg *nr_arg,
4992 struct nfs_readdir_res *nr_res)
4994 struct inode *dir = d_inode(nr_arg->dentry);
4995 struct nfs_server *server = NFS_SERVER(dir);
4996 struct nfs4_readdir_arg args = {
4998 .pages = nr_arg->pages,
5000 .count = nr_arg->page_len,
5001 .plus = nr_arg->plus,
5003 struct nfs4_readdir_res res;
5004 struct rpc_message msg = {
5005 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
5008 .rpc_cred = nr_arg->cred,
5012 dprintk("%s: dentry = %pd2, cookie = %llu\n", __func__,
5013 nr_arg->dentry, (unsigned long long)nr_arg->cookie);
5014 if (!(server->caps & NFS_CAP_SECURITY_LABEL))
5015 args.bitmask = server->attr_bitmask_nl;
5017 args.bitmask = server->attr_bitmask;
5019 nfs4_setup_readdir(nr_arg->cookie, nr_arg->verf, nr_arg->dentry, &args);
5020 res.pgbase = args.pgbase;
5021 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
5024 memcpy(nr_res->verf, res.verifier.data, NFS4_VERIFIER_SIZE);
5025 status += args.pgbase;
5028 nfs_invalidate_atime(dir);
5030 dprintk("%s: returns %d\n", __func__, status);
5034 static int nfs4_proc_readdir(struct nfs_readdir_arg *arg,
5035 struct nfs_readdir_res *res)
5037 struct nfs4_exception exception = {
5038 .interruptible = true,
5042 err = _nfs4_proc_readdir(arg, res);
5043 trace_nfs4_readdir(d_inode(arg->dentry), err);
5044 err = nfs4_handle_exception(NFS_SERVER(d_inode(arg->dentry)),
5046 } while (exception.retry);
5050 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5051 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
5053 struct nfs4_createdata *data;
5054 int mode = sattr->ia_mode;
5055 int status = -ENOMEM;
5057 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
5062 data->arg.ftype = NF4FIFO;
5063 else if (S_ISBLK(mode)) {
5064 data->arg.ftype = NF4BLK;
5065 data->arg.u.device.specdata1 = MAJOR(rdev);
5066 data->arg.u.device.specdata2 = MINOR(rdev);
5068 else if (S_ISCHR(mode)) {
5069 data->arg.ftype = NF4CHR;
5070 data->arg.u.device.specdata1 = MAJOR(rdev);
5071 data->arg.u.device.specdata2 = MINOR(rdev);
5072 } else if (!S_ISSOCK(mode)) {
5077 data->arg.label = label;
5078 status = nfs4_do_create(dir, dentry, data);
5080 nfs4_free_createdata(data);
5085 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5086 struct iattr *sattr, dev_t rdev)
5088 struct nfs_server *server = NFS_SERVER(dir);
5089 struct nfs4_exception exception = {
5090 .interruptible = true,
5092 struct nfs4_label l, *label = NULL;
5095 label = nfs4_label_init_security(dir, dentry, sattr, &l);
5097 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
5098 sattr->ia_mode &= ~current_umask();
5100 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
5101 trace_nfs4_mknod(dir, &dentry->d_name, err);
5102 err = nfs4_handle_exception(NFS_SERVER(dir), err,
5104 } while (exception.retry);
5106 nfs4_label_release_security(label);
5111 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
5112 struct nfs_fsstat *fsstat)
5114 struct nfs4_statfs_arg args = {
5116 .bitmask = server->attr_bitmask,
5118 struct nfs4_statfs_res res = {
5121 struct rpc_message msg = {
5122 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
5127 nfs_fattr_init(fsstat->fattr);
5128 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5131 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
5133 struct nfs4_exception exception = {
5134 .interruptible = true,
5138 err = nfs4_handle_exception(server,
5139 _nfs4_proc_statfs(server, fhandle, fsstat),
5141 } while (exception.retry);
5145 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
5146 struct nfs_fsinfo *fsinfo)
5148 struct nfs4_fsinfo_arg args = {
5150 .bitmask = server->attr_bitmask,
5152 struct nfs4_fsinfo_res res = {
5155 struct rpc_message msg = {
5156 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
5161 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5164 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5166 struct nfs4_exception exception = {
5167 .interruptible = true,
5172 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
5173 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
5175 nfs4_set_lease_period(server->nfs_client, fsinfo->lease_time * HZ);
5178 err = nfs4_handle_exception(server, err, &exception);
5179 } while (exception.retry);
5183 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5187 nfs_fattr_init(fsinfo->fattr);
5188 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
5190 /* block layout checks this! */
5191 server->pnfs_blksize = fsinfo->blksize;
5192 set_pnfs_layoutdriver(server, fhandle, fsinfo);
5198 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5199 struct nfs_pathconf *pathconf)
5201 struct nfs4_pathconf_arg args = {
5203 .bitmask = server->attr_bitmask,
5205 struct nfs4_pathconf_res res = {
5206 .pathconf = pathconf,
5208 struct rpc_message msg = {
5209 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
5214 /* None of the pathconf attributes are mandatory to implement */
5215 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
5216 memset(pathconf, 0, sizeof(*pathconf));
5220 nfs_fattr_init(pathconf->fattr);
5221 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5224 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5225 struct nfs_pathconf *pathconf)
5227 struct nfs4_exception exception = {
5228 .interruptible = true,
5233 err = nfs4_handle_exception(server,
5234 _nfs4_proc_pathconf(server, fhandle, pathconf),
5236 } while (exception.retry);
5240 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
5241 const struct nfs_open_context *ctx,
5242 const struct nfs_lock_context *l_ctx,
5245 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
5247 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
5249 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
5250 const struct nfs_open_context *ctx,
5251 const struct nfs_lock_context *l_ctx,
5254 nfs4_stateid _current_stateid;
5256 /* If the current stateid represents a lost lock, then exit */
5257 if (nfs4_set_rw_stateid(&_current_stateid, ctx, l_ctx, fmode) == -EIO)
5259 return nfs4_stateid_match(stateid, &_current_stateid);
5262 static bool nfs4_error_stateid_expired(int err)
5265 case -NFS4ERR_DELEG_REVOKED:
5266 case -NFS4ERR_ADMIN_REVOKED:
5267 case -NFS4ERR_BAD_STATEID:
5268 case -NFS4ERR_STALE_STATEID:
5269 case -NFS4ERR_OLD_STATEID:
5270 case -NFS4ERR_OPENMODE:
5271 case -NFS4ERR_EXPIRED:
5277 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
5279 struct nfs_server *server = NFS_SERVER(hdr->inode);
5281 trace_nfs4_read(hdr, task->tk_status);
5282 if (task->tk_status < 0) {
5283 struct nfs4_exception exception = {
5284 .inode = hdr->inode,
5285 .state = hdr->args.context->state,
5286 .stateid = &hdr->args.stateid,
5288 task->tk_status = nfs4_async_handle_exception(task,
5289 server, task->tk_status, &exception);
5290 if (exception.retry) {
5291 rpc_restart_call_prepare(task);
5296 if (task->tk_status > 0)
5297 renew_lease(server, hdr->timestamp);
5301 static bool nfs4_read_stateid_changed(struct rpc_task *task,
5302 struct nfs_pgio_args *args)
5305 if (!nfs4_error_stateid_expired(task->tk_status) ||
5306 nfs4_stateid_is_current(&args->stateid,
5311 rpc_restart_call_prepare(task);
5315 static bool nfs4_read_plus_not_supported(struct rpc_task *task,
5316 struct nfs_pgio_header *hdr)
5318 struct nfs_server *server = NFS_SERVER(hdr->inode);
5319 struct rpc_message *msg = &task->tk_msg;
5321 if (msg->rpc_proc == &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS] &&
5322 server->caps & NFS_CAP_READ_PLUS && task->tk_status == -ENOTSUPP) {
5323 server->caps &= ~NFS_CAP_READ_PLUS;
5324 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5325 rpc_restart_call_prepare(task);
5331 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5333 dprintk("--> %s\n", __func__);
5335 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5337 if (nfs4_read_stateid_changed(task, &hdr->args))
5339 if (nfs4_read_plus_not_supported(task, hdr))
5341 if (task->tk_status > 0)
5342 nfs_invalidate_atime(hdr->inode);
5343 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5344 nfs4_read_done_cb(task, hdr);
5347 #if defined CONFIG_NFS_V4_2 && defined CONFIG_NFS_V4_2_READ_PLUS
5348 static void nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5349 struct rpc_message *msg)
5351 /* Note: We don't use READ_PLUS with pNFS yet */
5352 if (nfs_server_capable(hdr->inode, NFS_CAP_READ_PLUS) && !hdr->ds_clp)
5353 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS];
5356 static void nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5357 struct rpc_message *msg)
5360 #endif /* CONFIG_NFS_V4_2 */
5362 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5363 struct rpc_message *msg)
5365 hdr->timestamp = jiffies;
5366 if (!hdr->pgio_done_cb)
5367 hdr->pgio_done_cb = nfs4_read_done_cb;
5368 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5369 nfs42_read_plus_support(hdr, msg);
5370 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5373 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5374 struct nfs_pgio_header *hdr)
5376 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
5377 &hdr->args.seq_args,
5381 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5382 hdr->args.lock_context,
5383 hdr->rw_mode) == -EIO)
5385 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
5390 static int nfs4_write_done_cb(struct rpc_task *task,
5391 struct nfs_pgio_header *hdr)
5393 struct inode *inode = hdr->inode;
5395 trace_nfs4_write(hdr, task->tk_status);
5396 if (task->tk_status < 0) {
5397 struct nfs4_exception exception = {
5398 .inode = hdr->inode,
5399 .state = hdr->args.context->state,
5400 .stateid = &hdr->args.stateid,
5402 task->tk_status = nfs4_async_handle_exception(task,
5403 NFS_SERVER(inode), task->tk_status,
5405 if (exception.retry) {
5406 rpc_restart_call_prepare(task);
5410 if (task->tk_status >= 0) {
5411 renew_lease(NFS_SERVER(inode), hdr->timestamp);
5412 nfs_writeback_update_inode(hdr);
5417 static bool nfs4_write_stateid_changed(struct rpc_task *task,
5418 struct nfs_pgio_args *args)
5421 if (!nfs4_error_stateid_expired(task->tk_status) ||
5422 nfs4_stateid_is_current(&args->stateid,
5427 rpc_restart_call_prepare(task);
5431 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5433 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5435 if (nfs4_write_stateid_changed(task, &hdr->args))
5437 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5438 nfs4_write_done_cb(task, hdr);
5442 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
5444 /* Don't request attributes for pNFS or O_DIRECT writes */
5445 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
5447 /* Otherwise, request attributes if and only if we don't hold
5450 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
5453 static void nfs4_bitmask_set(__u32 bitmask[NFS4_BITMASK_SZ], const __u32 *src,
5454 struct inode *inode, struct nfs_server *server,
5455 struct nfs4_label *label)
5457 unsigned long cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
5460 memcpy(bitmask, src, sizeof(*bitmask) * NFS4_BITMASK_SZ);
5462 if (cache_validity & NFS_INO_INVALID_CHANGE)
5463 bitmask[0] |= FATTR4_WORD0_CHANGE;
5464 if (cache_validity & NFS_INO_INVALID_ATIME)
5465 bitmask[1] |= FATTR4_WORD1_TIME_ACCESS;
5466 if (cache_validity & NFS_INO_INVALID_MODE)
5467 bitmask[1] |= FATTR4_WORD1_MODE;
5468 if (cache_validity & NFS_INO_INVALID_OTHER)
5469 bitmask[1] |= FATTR4_WORD1_OWNER | FATTR4_WORD1_OWNER_GROUP;
5470 if (cache_validity & NFS_INO_INVALID_NLINK)
5471 bitmask[1] |= FATTR4_WORD1_NUMLINKS;
5472 if (label && label->len && cache_validity & NFS_INO_INVALID_LABEL)
5473 bitmask[2] |= FATTR4_WORD2_SECURITY_LABEL;
5474 if (cache_validity & NFS_INO_INVALID_CTIME)
5475 bitmask[1] |= FATTR4_WORD1_TIME_METADATA;
5476 if (cache_validity & NFS_INO_INVALID_MTIME)
5477 bitmask[1] |= FATTR4_WORD1_TIME_MODIFY;
5478 if (cache_validity & NFS_INO_INVALID_BLOCKS)
5479 bitmask[1] |= FATTR4_WORD1_SPACE_USED;
5481 if (cache_validity & NFS_INO_INVALID_SIZE)
5482 bitmask[0] |= FATTR4_WORD0_SIZE;
5484 for (i = 0; i < NFS4_BITMASK_SZ; i++)
5485 bitmask[i] &= server->attr_bitmask[i];
5488 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
5489 struct rpc_message *msg,
5490 struct rpc_clnt **clnt)
5492 struct nfs_server *server = NFS_SERVER(hdr->inode);
5494 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5495 hdr->args.bitmask = NULL;
5496 hdr->res.fattr = NULL;
5498 nfs4_bitmask_set(hdr->args.bitmask_store,
5499 server->cache_consistency_bitmask,
5500 hdr->inode, server, NULL);
5501 hdr->args.bitmask = hdr->args.bitmask_store;
5504 if (!hdr->pgio_done_cb)
5505 hdr->pgio_done_cb = nfs4_write_done_cb;
5506 hdr->res.server = server;
5507 hdr->timestamp = jiffies;
5509 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
5510 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5511 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
5514 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5516 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
5517 &data->args.seq_args,
5522 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
5524 struct inode *inode = data->inode;
5526 trace_nfs4_commit(data, task->tk_status);
5527 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5528 NULL, NULL) == -EAGAIN) {
5529 rpc_restart_call_prepare(task);
5535 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
5537 if (!nfs4_sequence_done(task, &data->res.seq_res))
5539 return data->commit_done_cb(task, data);
5542 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5543 struct rpc_clnt **clnt)
5545 struct nfs_server *server = NFS_SERVER(data->inode);
5547 if (data->commit_done_cb == NULL)
5548 data->commit_done_cb = nfs4_commit_done_cb;
5549 data->res.server = server;
5550 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
5551 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
5552 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
5555 static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5556 struct nfs_commitres *res)
5558 struct inode *dst_inode = file_inode(dst);
5559 struct nfs_server *server = NFS_SERVER(dst_inode);
5560 struct rpc_message msg = {
5561 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5566 args->fh = NFS_FH(dst_inode);
5567 return nfs4_call_sync(server->client, server, &msg,
5568 &args->seq_args, &res->seq_res, 1);
5571 int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5573 struct nfs_commitargs args = {
5577 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5578 struct nfs4_exception exception = { };
5582 status = _nfs4_proc_commit(dst, &args, res);
5583 status = nfs4_handle_exception(dst_server, status, &exception);
5584 } while (exception.retry);
5589 struct nfs4_renewdata {
5590 struct nfs_client *client;
5591 unsigned long timestamp;
5595 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5596 * standalone procedure for queueing an asynchronous RENEW.
5598 static void nfs4_renew_release(void *calldata)
5600 struct nfs4_renewdata *data = calldata;
5601 struct nfs_client *clp = data->client;
5603 if (refcount_read(&clp->cl_count) > 1)
5604 nfs4_schedule_state_renewal(clp);
5605 nfs_put_client(clp);
5609 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
5611 struct nfs4_renewdata *data = calldata;
5612 struct nfs_client *clp = data->client;
5613 unsigned long timestamp = data->timestamp;
5615 trace_nfs4_renew_async(clp, task->tk_status);
5616 switch (task->tk_status) {
5619 case -NFS4ERR_LEASE_MOVED:
5620 nfs4_schedule_lease_moved_recovery(clp);
5623 /* Unless we're shutting down, schedule state recovery! */
5624 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5626 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
5627 nfs4_schedule_lease_recovery(clp);
5630 nfs4_schedule_path_down_recovery(clp);
5632 do_renew_lease(clp, timestamp);
5635 static const struct rpc_call_ops nfs4_renew_ops = {
5636 .rpc_call_done = nfs4_renew_done,
5637 .rpc_release = nfs4_renew_release,
5640 static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
5642 struct rpc_message msg = {
5643 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5647 struct nfs4_renewdata *data;
5649 if (renew_flags == 0)
5651 if (!refcount_inc_not_zero(&clp->cl_count))
5653 data = kmalloc(sizeof(*data), GFP_NOFS);
5655 nfs_put_client(clp);
5659 data->timestamp = jiffies;
5660 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
5661 &nfs4_renew_ops, data);
5664 static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
5666 struct rpc_message msg = {
5667 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5671 unsigned long now = jiffies;
5674 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5677 do_renew_lease(clp, now);
5681 static inline int nfs4_server_supports_acls(struct nfs_server *server)
5683 return server->caps & NFS_CAP_ACLS;
5686 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5687 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
5690 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
5692 int nfs4_buf_to_pages_noslab(const void *buf, size_t buflen,
5693 struct page **pages)
5695 struct page *newpage, **spages;
5701 len = min_t(size_t, PAGE_SIZE, buflen);
5702 newpage = alloc_page(GFP_KERNEL);
5704 if (newpage == NULL)
5706 memcpy(page_address(newpage), buf, len);
5711 } while (buflen != 0);
5717 __free_page(spages[rc-1]);
5721 struct nfs4_cached_acl {
5727 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
5729 struct nfs_inode *nfsi = NFS_I(inode);
5731 spin_lock(&inode->i_lock);
5732 kfree(nfsi->nfs4_acl);
5733 nfsi->nfs4_acl = acl;
5734 spin_unlock(&inode->i_lock);
5737 static void nfs4_zap_acl_attr(struct inode *inode)
5739 nfs4_set_cached_acl(inode, NULL);
5742 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5744 struct nfs_inode *nfsi = NFS_I(inode);
5745 struct nfs4_cached_acl *acl;
5748 spin_lock(&inode->i_lock);
5749 acl = nfsi->nfs4_acl;
5752 if (buf == NULL) /* user is just asking for length */
5754 if (acl->cached == 0)
5756 ret = -ERANGE; /* see getxattr(2) man page */
5757 if (acl->len > buflen)
5759 memcpy(buf, acl->data, acl->len);
5763 spin_unlock(&inode->i_lock);
5767 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
5769 struct nfs4_cached_acl *acl;
5770 size_t buflen = sizeof(*acl) + acl_len;
5772 if (buflen <= PAGE_SIZE) {
5773 acl = kmalloc(buflen, GFP_KERNEL);
5777 _copy_from_pages(acl->data, pages, pgbase, acl_len);
5779 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5786 nfs4_set_cached_acl(inode, acl);
5790 * The getxattr API returns the required buffer length when called with a
5791 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5792 * the required buf. On a NULL buf, we send a page of data to the server
5793 * guessing that the ACL request can be serviced by a page. If so, we cache
5794 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5795 * the cache. If not so, we throw away the page, and cache the required
5796 * length. The next getxattr call will then produce another round trip to
5797 * the server, this time with the input buf of the required size.
5799 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5801 struct page **pages;
5802 struct nfs_getaclargs args = {
5803 .fh = NFS_FH(inode),
5806 struct nfs_getaclres res = {
5809 struct rpc_message msg = {
5810 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5814 unsigned int npages;
5815 int ret = -ENOMEM, i;
5816 struct nfs_server *server = NFS_SERVER(inode);
5819 buflen = server->rsize;
5821 npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
5822 pages = kmalloc_array(npages, sizeof(struct page *), GFP_NOFS);
5826 args.acl_pages = pages;
5828 for (i = 0; i < npages; i++) {
5829 pages[i] = alloc_page(GFP_KERNEL);
5834 /* for decoding across pages */
5835 res.acl_scratch = alloc_page(GFP_KERNEL);
5836 if (!res.acl_scratch)
5839 args.acl_len = npages * PAGE_SIZE;
5841 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
5842 __func__, buf, buflen, npages, args.acl_len);
5843 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5844 &msg, &args.seq_args, &res.seq_res, 0);
5848 /* Handle the case where the passed-in buffer is too short */
5849 if (res.acl_flags & NFS4_ACL_TRUNC) {
5850 /* Did the user only issue a request for the acl length? */
5856 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
5858 if (res.acl_len > buflen) {
5862 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
5867 for (i = 0; i < npages; i++)
5869 __free_page(pages[i]);
5870 if (res.acl_scratch)
5871 __free_page(res.acl_scratch);
5876 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5878 struct nfs4_exception exception = {
5879 .interruptible = true,
5883 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
5884 trace_nfs4_get_acl(inode, ret);
5887 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5888 } while (exception.retry);
5892 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5894 struct nfs_server *server = NFS_SERVER(inode);
5897 if (!nfs4_server_supports_acls(server))
5899 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
5902 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5903 nfs_zap_acl_cache(inode);
5904 ret = nfs4_read_cached_acl(inode, buf, buflen);
5906 /* -ENOENT is returned if there is no ACL or if there is an ACL
5907 * but no cached acl data, just the acl length */
5909 return nfs4_get_acl_uncached(inode, buf, buflen);
5912 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5914 struct nfs_server *server = NFS_SERVER(inode);
5915 struct page *pages[NFS4ACL_MAXPAGES];
5916 struct nfs_setaclargs arg = {
5917 .fh = NFS_FH(inode),
5921 struct nfs_setaclres res;
5922 struct rpc_message msg = {
5923 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5927 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
5930 /* You can't remove system.nfs4_acl: */
5933 if (!nfs4_server_supports_acls(server))
5935 if (npages > ARRAY_SIZE(pages))
5937 i = nfs4_buf_to_pages_noslab(buf, buflen, arg.acl_pages);
5940 nfs4_inode_make_writeable(inode);
5941 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5944 * Free each page after tx, so the only ref left is
5945 * held by the network stack
5948 put_page(pages[i-1]);
5951 * Acl update can result in inode attribute update.
5952 * so mark the attribute cache invalid.
5954 spin_lock(&inode->i_lock);
5955 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
5956 NFS_INO_INVALID_CTIME |
5957 NFS_INO_REVAL_FORCED);
5958 spin_unlock(&inode->i_lock);
5959 nfs_access_zap_cache(inode);
5960 nfs_zap_acl_cache(inode);
5964 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5966 struct nfs4_exception exception = { };
5969 err = __nfs4_proc_set_acl(inode, buf, buflen);
5970 trace_nfs4_set_acl(inode, err);
5971 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5973 } while (exception.retry);
5977 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
5978 static int _nfs4_get_security_label(struct inode *inode, void *buf,
5981 struct nfs_server *server = NFS_SERVER(inode);
5982 struct nfs_fattr fattr;
5983 struct nfs4_label label = {0, 0, buflen, buf};
5985 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5986 struct nfs4_getattr_arg arg = {
5987 .fh = NFS_FH(inode),
5990 struct nfs4_getattr_res res = {
5995 struct rpc_message msg = {
5996 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
6002 nfs_fattr_init(&fattr);
6004 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
6007 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
6012 static int nfs4_get_security_label(struct inode *inode, void *buf,
6015 struct nfs4_exception exception = {
6016 .interruptible = true,
6020 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
6024 err = _nfs4_get_security_label(inode, buf, buflen);
6025 trace_nfs4_get_security_label(inode, err);
6026 err = nfs4_handle_exception(NFS_SERVER(inode), err,
6028 } while (exception.retry);
6032 static int _nfs4_do_set_security_label(struct inode *inode,
6033 struct nfs4_label *ilabel,
6034 struct nfs_fattr *fattr,
6035 struct nfs4_label *olabel)
6038 struct iattr sattr = {0};
6039 struct nfs_server *server = NFS_SERVER(inode);
6040 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
6041 struct nfs_setattrargs arg = {
6042 .fh = NFS_FH(inode),
6048 struct nfs_setattrres res = {
6053 struct rpc_message msg = {
6054 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
6060 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
6062 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6064 dprintk("%s failed: %d\n", __func__, status);
6069 static int nfs4_do_set_security_label(struct inode *inode,
6070 struct nfs4_label *ilabel,
6071 struct nfs_fattr *fattr,
6072 struct nfs4_label *olabel)
6074 struct nfs4_exception exception = { };
6078 err = _nfs4_do_set_security_label(inode, ilabel,
6080 trace_nfs4_set_security_label(inode, err);
6081 err = nfs4_handle_exception(NFS_SERVER(inode), err,
6083 } while (exception.retry);
6088 nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
6090 struct nfs4_label ilabel, *olabel = NULL;
6091 struct nfs_fattr fattr;
6094 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
6097 nfs_fattr_init(&fattr);
6101 ilabel.label = (char *)buf;
6102 ilabel.len = buflen;
6104 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
6105 if (IS_ERR(olabel)) {
6106 status = -PTR_ERR(olabel);
6110 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
6112 nfs_setsecurity(inode, &fattr, olabel);
6114 nfs4_label_free(olabel);
6118 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
6121 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
6122 nfs4_verifier *bootverf)
6126 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
6127 /* An impossible timestamp guarantees this value
6128 * will never match a generated boot time. */
6129 verf[0] = cpu_to_be32(U32_MAX);
6130 verf[1] = cpu_to_be32(U32_MAX);
6132 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
6133 u64 ns = ktime_to_ns(nn->boot_time);
6135 verf[0] = cpu_to_be32(ns >> 32);
6136 verf[1] = cpu_to_be32(ns);
6138 memcpy(bootverf->data, verf, sizeof(bootverf->data));
6142 nfs4_get_uniquifier(struct nfs_client *clp, char *buf, size_t buflen)
6144 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
6145 struct nfs_netns_client *nn_clp = nn->nfs_client;
6152 id = rcu_dereference(nn_clp->identifier);
6154 strscpy(buf, id, buflen);
6158 if (nfs4_client_id_uniquifier[0] != '\0' && buf[0] == '\0')
6159 strscpy(buf, nfs4_client_id_uniquifier, buflen);
6165 nfs4_init_nonuniform_client_string(struct nfs_client *clp)
6167 char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6172 if (clp->cl_owner_id != NULL)
6177 strlen(clp->cl_rpcclient->cl_nodename) +
6179 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
6183 buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
6187 if (len > NFS4_OPAQUE_LIMIT + 1)
6191 * Since this string is allocated at mount time, and held until the
6192 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6193 * about a memory-reclaim deadlock.
6195 str = kmalloc(len, GFP_KERNEL);
6201 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
6202 clp->cl_rpcclient->cl_nodename, buf,
6203 rpc_peeraddr2str(clp->cl_rpcclient,
6206 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
6207 clp->cl_rpcclient->cl_nodename,
6208 rpc_peeraddr2str(clp->cl_rpcclient,
6212 clp->cl_owner_id = str;
6217 nfs4_init_uniform_client_string(struct nfs_client *clp)
6219 char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6224 if (clp->cl_owner_id != NULL)
6227 len = 10 + 10 + 1 + 10 + 1 +
6228 strlen(clp->cl_rpcclient->cl_nodename) + 1;
6230 buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
6234 if (len > NFS4_OPAQUE_LIMIT + 1)
6238 * Since this string is allocated at mount time, and held until the
6239 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6240 * about a memory-reclaim deadlock.
6242 str = kmalloc(len, GFP_KERNEL);
6247 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
6248 clp->rpc_ops->version, clp->cl_minorversion,
6249 buf, clp->cl_rpcclient->cl_nodename);
6251 scnprintf(str, len, "Linux NFSv%u.%u %s",
6252 clp->rpc_ops->version, clp->cl_minorversion,
6253 clp->cl_rpcclient->cl_nodename);
6254 clp->cl_owner_id = str;
6259 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
6260 * services. Advertise one based on the address family of the
6264 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
6266 if (strchr(clp->cl_ipaddr, ':') != NULL)
6267 return scnprintf(buf, len, "tcp6");
6269 return scnprintf(buf, len, "tcp");
6272 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
6274 struct nfs4_setclientid *sc = calldata;
6276 if (task->tk_status == 0)
6277 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
6280 static const struct rpc_call_ops nfs4_setclientid_ops = {
6281 .rpc_call_done = nfs4_setclientid_done,
6285 * nfs4_proc_setclientid - Negotiate client ID
6286 * @clp: state data structure
6287 * @program: RPC program for NFSv4 callback service
6288 * @port: IP port number for NFS4 callback service
6289 * @cred: credential to use for this call
6290 * @res: where to place the result
6292 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6294 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
6295 unsigned short port, const struct cred *cred,
6296 struct nfs4_setclientid_res *res)
6298 nfs4_verifier sc_verifier;
6299 struct nfs4_setclientid setclientid = {
6300 .sc_verifier = &sc_verifier,
6304 struct rpc_message msg = {
6305 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
6306 .rpc_argp = &setclientid,
6310 struct rpc_task_setup task_setup_data = {
6311 .rpc_client = clp->cl_rpcclient,
6312 .rpc_message = &msg,
6313 .callback_ops = &nfs4_setclientid_ops,
6314 .callback_data = &setclientid,
6315 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
6317 unsigned long now = jiffies;
6320 /* nfs_client_id4 */
6321 nfs4_init_boot_verifier(clp, &sc_verifier);
6323 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
6324 status = nfs4_init_uniform_client_string(clp);
6326 status = nfs4_init_nonuniform_client_string(clp);
6332 setclientid.sc_netid_len =
6333 nfs4_init_callback_netid(clp,
6334 setclientid.sc_netid,
6335 sizeof(setclientid.sc_netid));
6336 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
6337 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
6338 clp->cl_ipaddr, port >> 8, port & 255);
6340 dprintk("NFS call setclientid auth=%s, '%s'\n",
6341 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6344 status = nfs4_call_sync_custom(&task_setup_data);
6345 if (setclientid.sc_cred) {
6346 kfree(clp->cl_acceptor);
6347 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
6348 put_rpccred(setclientid.sc_cred);
6352 do_renew_lease(clp, now);
6354 trace_nfs4_setclientid(clp, status);
6355 dprintk("NFS reply setclientid: %d\n", status);
6360 * nfs4_proc_setclientid_confirm - Confirm client ID
6361 * @clp: state data structure
6362 * @arg: result of a previous SETCLIENTID
6363 * @cred: credential to use for this call
6365 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6367 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
6368 struct nfs4_setclientid_res *arg,
6369 const struct cred *cred)
6371 struct rpc_message msg = {
6372 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
6378 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
6379 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6381 status = rpc_call_sync(clp->cl_rpcclient, &msg,
6382 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
6383 trace_nfs4_setclientid_confirm(clp, status);
6384 dprintk("NFS reply setclientid_confirm: %d\n", status);
6388 struct nfs4_delegreturndata {
6389 struct nfs4_delegreturnargs args;
6390 struct nfs4_delegreturnres res;
6392 nfs4_stateid stateid;
6393 unsigned long timestamp;
6395 struct nfs4_layoutreturn_args arg;
6396 struct nfs4_layoutreturn_res res;
6397 struct nfs4_xdr_opaque_data ld_private;
6401 struct nfs_fattr fattr;
6403 struct inode *inode;
6406 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6408 struct nfs4_delegreturndata *data = calldata;
6409 struct nfs4_exception exception = {
6410 .inode = data->inode,
6411 .stateid = &data->stateid,
6414 if (!nfs4_sequence_done(task, &data->res.seq_res))
6417 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
6419 /* Handle Layoutreturn errors */
6420 if (pnfs_roc_done(task, &data->args.lr_args, &data->res.lr_res,
6421 &data->res.lr_ret) == -EAGAIN)
6424 switch (task->tk_status) {
6426 renew_lease(data->res.server, data->timestamp);
6428 case -NFS4ERR_ADMIN_REVOKED:
6429 case -NFS4ERR_DELEG_REVOKED:
6430 case -NFS4ERR_EXPIRED:
6431 nfs4_free_revoked_stateid(data->res.server,
6433 task->tk_msg.rpc_cred);
6435 case -NFS4ERR_BAD_STATEID:
6436 case -NFS4ERR_STALE_STATEID:
6438 task->tk_status = 0;
6440 case -NFS4ERR_OLD_STATEID:
6441 if (!nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6442 nfs4_stateid_seqid_inc(&data->stateid);
6443 if (data->args.bitmask) {
6444 data->args.bitmask = NULL;
6445 data->res.fattr = NULL;
6448 case -NFS4ERR_ACCESS:
6449 if (data->args.bitmask) {
6450 data->args.bitmask = NULL;
6451 data->res.fattr = NULL;
6456 task->tk_status = nfs4_async_handle_exception(task,
6457 data->res.server, task->tk_status,
6459 if (exception.retry)
6462 nfs_delegation_mark_returned(data->inode, data->args.stateid);
6463 data->rpc_status = task->tk_status;
6466 task->tk_status = 0;
6467 rpc_restart_call_prepare(task);
6470 static void nfs4_delegreturn_release(void *calldata)
6472 struct nfs4_delegreturndata *data = calldata;
6473 struct inode *inode = data->inode;
6476 pnfs_roc_release(&data->lr.arg, &data->lr.res,
6479 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
6480 nfs_iput_and_deactive(inode);
6485 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6487 struct nfs4_delegreturndata *d_data;
6488 struct pnfs_layout_hdr *lo;
6490 d_data = (struct nfs4_delegreturndata *)data;
6492 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task)) {
6493 nfs4_sequence_done(task, &d_data->res.seq_res);
6497 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6498 if (lo && !pnfs_layout_is_valid(lo)) {
6499 d_data->args.lr_args = NULL;
6500 d_data->res.lr_res = NULL;
6503 nfs4_setup_sequence(d_data->res.server->nfs_client,
6504 &d_data->args.seq_args,
6505 &d_data->res.seq_res,
6509 static const struct rpc_call_ops nfs4_delegreturn_ops = {
6510 .rpc_call_prepare = nfs4_delegreturn_prepare,
6511 .rpc_call_done = nfs4_delegreturn_done,
6512 .rpc_release = nfs4_delegreturn_release,
6515 static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6517 struct nfs4_delegreturndata *data;
6518 struct nfs_server *server = NFS_SERVER(inode);
6519 struct rpc_task *task;
6520 struct rpc_message msg = {
6521 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6524 struct rpc_task_setup task_setup_data = {
6525 .rpc_client = server->client,
6526 .rpc_message = &msg,
6527 .callback_ops = &nfs4_delegreturn_ops,
6528 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
6532 data = kzalloc(sizeof(*data), GFP_NOFS);
6535 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
6537 nfs4_state_protect(server->nfs_client,
6538 NFS_SP4_MACH_CRED_CLEANUP,
6539 &task_setup_data.rpc_client, &msg);
6541 data->args.fhandle = &data->fh;
6542 data->args.stateid = &data->stateid;
6543 nfs4_bitmask_set(data->args.bitmask_store,
6544 server->cache_consistency_bitmask, inode, server,
6546 data->args.bitmask = data->args.bitmask_store;
6547 nfs_copy_fh(&data->fh, NFS_FH(inode));
6548 nfs4_stateid_copy(&data->stateid, stateid);
6549 data->res.fattr = &data->fattr;
6550 data->res.server = server;
6551 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
6552 data->lr.arg.ld_private = &data->lr.ld_private;
6553 nfs_fattr_init(data->res.fattr);
6554 data->timestamp = jiffies;
6555 data->rpc_status = 0;
6556 data->inode = nfs_igrab_and_active(inode);
6557 if (data->inode || issync) {
6558 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res,
6561 data->args.lr_args = &data->lr.arg;
6562 data->res.lr_res = &data->lr.res;
6566 task_setup_data.callback_data = data;
6567 msg.rpc_argp = &data->args;
6568 msg.rpc_resp = &data->res;
6569 task = rpc_run_task(&task_setup_data);
6571 return PTR_ERR(task);
6574 status = rpc_wait_for_completion_task(task);
6577 status = data->rpc_status;
6583 int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6585 struct nfs_server *server = NFS_SERVER(inode);
6586 struct nfs4_exception exception = { };
6589 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
6590 trace_nfs4_delegreturn(inode, stateid, err);
6592 case -NFS4ERR_STALE_STATEID:
6593 case -NFS4ERR_EXPIRED:
6597 err = nfs4_handle_exception(server, err, &exception);
6598 } while (exception.retry);
6602 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6604 struct inode *inode = state->inode;
6605 struct nfs_server *server = NFS_SERVER(inode);
6606 struct nfs_client *clp = server->nfs_client;
6607 struct nfs_lockt_args arg = {
6608 .fh = NFS_FH(inode),
6611 struct nfs_lockt_res res = {
6614 struct rpc_message msg = {
6615 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
6618 .rpc_cred = state->owner->so_cred,
6620 struct nfs4_lock_state *lsp;
6623 arg.lock_owner.clientid = clp->cl_clientid;
6624 status = nfs4_set_lock_state(state, request);
6627 lsp = request->fl_u.nfs4_fl.owner;
6628 arg.lock_owner.id = lsp->ls_seqid.owner_id;
6629 arg.lock_owner.s_dev = server->s_dev;
6630 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6633 request->fl_type = F_UNLCK;
6635 case -NFS4ERR_DENIED:
6638 request->fl_ops->fl_release_private(request);
6639 request->fl_ops = NULL;
6644 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6646 struct nfs4_exception exception = {
6647 .interruptible = true,
6652 err = _nfs4_proc_getlk(state, cmd, request);
6653 trace_nfs4_get_lock(request, state, cmd, err);
6654 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
6656 } while (exception.retry);
6661 * Update the seqid of a lock stateid after receiving
6662 * NFS4ERR_OLD_STATEID
6664 static bool nfs4_refresh_lock_old_stateid(nfs4_stateid *dst,
6665 struct nfs4_lock_state *lsp)
6667 struct nfs4_state *state = lsp->ls_state;
6670 spin_lock(&state->state_lock);
6671 if (!nfs4_stateid_match_other(dst, &lsp->ls_stateid))
6673 if (!nfs4_stateid_is_newer(&lsp->ls_stateid, dst))
6674 nfs4_stateid_seqid_inc(dst);
6676 dst->seqid = lsp->ls_stateid.seqid;
6679 spin_unlock(&state->state_lock);
6683 static bool nfs4_sync_lock_stateid(nfs4_stateid *dst,
6684 struct nfs4_lock_state *lsp)
6686 struct nfs4_state *state = lsp->ls_state;
6689 spin_lock(&state->state_lock);
6690 ret = !nfs4_stateid_match_other(dst, &lsp->ls_stateid);
6691 nfs4_stateid_copy(dst, &lsp->ls_stateid);
6692 spin_unlock(&state->state_lock);
6696 struct nfs4_unlockdata {
6697 struct nfs_locku_args arg;
6698 struct nfs_locku_res res;
6699 struct nfs4_lock_state *lsp;
6700 struct nfs_open_context *ctx;
6701 struct nfs_lock_context *l_ctx;
6702 struct file_lock fl;
6703 struct nfs_server *server;
6704 unsigned long timestamp;
6707 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6708 struct nfs_open_context *ctx,
6709 struct nfs4_lock_state *lsp,
6710 struct nfs_seqid *seqid)
6712 struct nfs4_unlockdata *p;
6713 struct nfs4_state *state = lsp->ls_state;
6714 struct inode *inode = state->inode;
6716 p = kzalloc(sizeof(*p), GFP_NOFS);
6719 p->arg.fh = NFS_FH(inode);
6721 p->arg.seqid = seqid;
6722 p->res.seqid = seqid;
6724 /* Ensure we don't close file until we're done freeing locks! */
6725 p->ctx = get_nfs_open_context(ctx);
6726 p->l_ctx = nfs_get_lock_context(ctx);
6727 locks_init_lock(&p->fl);
6728 locks_copy_lock(&p->fl, fl);
6729 p->server = NFS_SERVER(inode);
6730 spin_lock(&state->state_lock);
6731 nfs4_stateid_copy(&p->arg.stateid, &lsp->ls_stateid);
6732 spin_unlock(&state->state_lock);
6736 static void nfs4_locku_release_calldata(void *data)
6738 struct nfs4_unlockdata *calldata = data;
6739 nfs_free_seqid(calldata->arg.seqid);
6740 nfs4_put_lock_state(calldata->lsp);
6741 nfs_put_lock_context(calldata->l_ctx);
6742 put_nfs_open_context(calldata->ctx);
6746 static void nfs4_locku_done(struct rpc_task *task, void *data)
6748 struct nfs4_unlockdata *calldata = data;
6749 struct nfs4_exception exception = {
6750 .inode = calldata->lsp->ls_state->inode,
6751 .stateid = &calldata->arg.stateid,
6754 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6756 switch (task->tk_status) {
6758 renew_lease(calldata->server, calldata->timestamp);
6759 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
6760 if (nfs4_update_lock_stateid(calldata->lsp,
6761 &calldata->res.stateid))
6764 case -NFS4ERR_ADMIN_REVOKED:
6765 case -NFS4ERR_EXPIRED:
6766 nfs4_free_revoked_stateid(calldata->server,
6767 &calldata->arg.stateid,
6768 task->tk_msg.rpc_cred);
6770 case -NFS4ERR_BAD_STATEID:
6771 case -NFS4ERR_STALE_STATEID:
6772 if (nfs4_sync_lock_stateid(&calldata->arg.stateid,
6774 rpc_restart_call_prepare(task);
6776 case -NFS4ERR_OLD_STATEID:
6777 if (nfs4_refresh_lock_old_stateid(&calldata->arg.stateid,
6779 rpc_restart_call_prepare(task);
6782 task->tk_status = nfs4_async_handle_exception(task,
6783 calldata->server, task->tk_status,
6785 if (exception.retry)
6786 rpc_restart_call_prepare(task);
6788 nfs_release_seqid(calldata->arg.seqid);
6791 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
6793 struct nfs4_unlockdata *calldata = data;
6795 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6796 nfs_async_iocounter_wait(task, calldata->l_ctx))
6799 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
6801 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
6802 /* Note: exit _without_ running nfs4_locku_done */
6805 calldata->timestamp = jiffies;
6806 if (nfs4_setup_sequence(calldata->server->nfs_client,
6807 &calldata->arg.seq_args,
6808 &calldata->res.seq_res,
6810 nfs_release_seqid(calldata->arg.seqid);
6813 task->tk_action = NULL;
6815 nfs4_sequence_done(task, &calldata->res.seq_res);
6818 static const struct rpc_call_ops nfs4_locku_ops = {
6819 .rpc_call_prepare = nfs4_locku_prepare,
6820 .rpc_call_done = nfs4_locku_done,
6821 .rpc_release = nfs4_locku_release_calldata,
6824 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6825 struct nfs_open_context *ctx,
6826 struct nfs4_lock_state *lsp,
6827 struct nfs_seqid *seqid)
6829 struct nfs4_unlockdata *data;
6830 struct rpc_message msg = {
6831 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6832 .rpc_cred = ctx->cred,
6834 struct rpc_task_setup task_setup_data = {
6835 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
6836 .rpc_message = &msg,
6837 .callback_ops = &nfs4_locku_ops,
6838 .workqueue = nfsiod_workqueue,
6839 .flags = RPC_TASK_ASYNC,
6842 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6843 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6845 /* Ensure this is an unlock - when canceling a lock, the
6846 * canceled lock is passed in, and it won't be an unlock.
6848 fl->fl_type = F_UNLCK;
6849 if (fl->fl_flags & FL_CLOSE)
6850 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
6852 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6854 nfs_free_seqid(seqid);
6855 return ERR_PTR(-ENOMEM);
6858 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
6859 msg.rpc_argp = &data->arg;
6860 msg.rpc_resp = &data->res;
6861 task_setup_data.callback_data = data;
6862 return rpc_run_task(&task_setup_data);
6865 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6867 struct inode *inode = state->inode;
6868 struct nfs4_state_owner *sp = state->owner;
6869 struct nfs_inode *nfsi = NFS_I(inode);
6870 struct nfs_seqid *seqid;
6871 struct nfs4_lock_state *lsp;
6872 struct rpc_task *task;
6873 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6875 unsigned char fl_flags = request->fl_flags;
6877 status = nfs4_set_lock_state(state, request);
6878 /* Unlock _before_ we do the RPC call */
6879 request->fl_flags |= FL_EXISTS;
6880 /* Exclude nfs_delegation_claim_locks() */
6881 mutex_lock(&sp->so_delegreturn_mutex);
6882 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
6883 down_read(&nfsi->rwsem);
6884 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
6885 up_read(&nfsi->rwsem);
6886 mutex_unlock(&sp->so_delegreturn_mutex);
6889 up_read(&nfsi->rwsem);
6890 mutex_unlock(&sp->so_delegreturn_mutex);
6893 /* Is this a delegated lock? */
6894 lsp = request->fl_u.nfs4_fl.owner;
6895 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6897 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6898 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
6902 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
6903 status = PTR_ERR(task);
6906 status = rpc_wait_for_completion_task(task);
6909 request->fl_flags = fl_flags;
6910 trace_nfs4_unlock(request, state, F_SETLK, status);
6914 struct nfs4_lockdata {
6915 struct nfs_lock_args arg;
6916 struct nfs_lock_res res;
6917 struct nfs4_lock_state *lsp;
6918 struct nfs_open_context *ctx;
6919 struct file_lock fl;
6920 unsigned long timestamp;
6923 struct nfs_server *server;
6926 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
6927 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6930 struct nfs4_lockdata *p;
6931 struct inode *inode = lsp->ls_state->inode;
6932 struct nfs_server *server = NFS_SERVER(inode);
6933 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6935 p = kzalloc(sizeof(*p), gfp_mask);
6939 p->arg.fh = NFS_FH(inode);
6941 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
6942 if (IS_ERR(p->arg.open_seqid))
6944 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6945 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
6946 if (IS_ERR(p->arg.lock_seqid))
6947 goto out_free_seqid;
6948 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
6949 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
6950 p->arg.lock_owner.s_dev = server->s_dev;
6951 p->res.lock_seqid = p->arg.lock_seqid;
6954 p->ctx = get_nfs_open_context(ctx);
6955 locks_init_lock(&p->fl);
6956 locks_copy_lock(&p->fl, fl);
6959 nfs_free_seqid(p->arg.open_seqid);
6965 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6967 struct nfs4_lockdata *data = calldata;
6968 struct nfs4_state *state = data->lsp->ls_state;
6970 dprintk("%s: begin!\n", __func__);
6971 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
6973 /* Do we need to do an open_to_lock_owner? */
6974 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
6975 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
6976 goto out_release_lock_seqid;
6978 nfs4_stateid_copy(&data->arg.open_stateid,
6979 &state->open_stateid);
6980 data->arg.new_lock_owner = 1;
6981 data->res.open_seqid = data->arg.open_seqid;
6983 data->arg.new_lock_owner = 0;
6984 nfs4_stateid_copy(&data->arg.lock_stateid,
6985 &data->lsp->ls_stateid);
6987 if (!nfs4_valid_open_stateid(state)) {
6988 data->rpc_status = -EBADF;
6989 task->tk_action = NULL;
6990 goto out_release_open_seqid;
6992 data->timestamp = jiffies;
6993 if (nfs4_setup_sequence(data->server->nfs_client,
6994 &data->arg.seq_args,
6998 out_release_open_seqid:
6999 nfs_release_seqid(data->arg.open_seqid);
7000 out_release_lock_seqid:
7001 nfs_release_seqid(data->arg.lock_seqid);
7003 nfs4_sequence_done(task, &data->res.seq_res);
7004 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
7007 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
7009 struct nfs4_lockdata *data = calldata;
7010 struct nfs4_lock_state *lsp = data->lsp;
7012 dprintk("%s: begin!\n", __func__);
7014 if (!nfs4_sequence_done(task, &data->res.seq_res))
7017 data->rpc_status = task->tk_status;
7018 switch (task->tk_status) {
7020 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
7022 if (data->arg.new_lock && !data->cancelled) {
7023 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
7024 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
7027 if (data->arg.new_lock_owner != 0) {
7028 nfs_confirm_seqid(&lsp->ls_seqid, 0);
7029 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
7030 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
7031 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
7034 case -NFS4ERR_BAD_STATEID:
7035 case -NFS4ERR_OLD_STATEID:
7036 case -NFS4ERR_STALE_STATEID:
7037 case -NFS4ERR_EXPIRED:
7038 if (data->arg.new_lock_owner != 0) {
7039 if (!nfs4_stateid_match(&data->arg.open_stateid,
7040 &lsp->ls_state->open_stateid))
7042 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
7047 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
7050 if (!data->cancelled)
7051 rpc_restart_call_prepare(task);
7055 static void nfs4_lock_release(void *calldata)
7057 struct nfs4_lockdata *data = calldata;
7059 dprintk("%s: begin!\n", __func__);
7060 nfs_free_seqid(data->arg.open_seqid);
7061 if (data->cancelled && data->rpc_status == 0) {
7062 struct rpc_task *task;
7063 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
7064 data->arg.lock_seqid);
7066 rpc_put_task_async(task);
7067 dprintk("%s: cancelling lock!\n", __func__);
7069 nfs_free_seqid(data->arg.lock_seqid);
7070 nfs4_put_lock_state(data->lsp);
7071 put_nfs_open_context(data->ctx);
7073 dprintk("%s: done!\n", __func__);
7076 static const struct rpc_call_ops nfs4_lock_ops = {
7077 .rpc_call_prepare = nfs4_lock_prepare,
7078 .rpc_call_done = nfs4_lock_done,
7079 .rpc_release = nfs4_lock_release,
7082 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
7085 case -NFS4ERR_ADMIN_REVOKED:
7086 case -NFS4ERR_EXPIRED:
7087 case -NFS4ERR_BAD_STATEID:
7088 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
7089 if (new_lock_owner != 0 ||
7090 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
7091 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
7093 case -NFS4ERR_STALE_STATEID:
7094 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
7095 nfs4_schedule_lease_recovery(server->nfs_client);
7099 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
7101 struct nfs4_lockdata *data;
7102 struct rpc_task *task;
7103 struct rpc_message msg = {
7104 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
7105 .rpc_cred = state->owner->so_cred,
7107 struct rpc_task_setup task_setup_data = {
7108 .rpc_client = NFS_CLIENT(state->inode),
7109 .rpc_message = &msg,
7110 .callback_ops = &nfs4_lock_ops,
7111 .workqueue = nfsiod_workqueue,
7112 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
7116 dprintk("%s: begin!\n", __func__);
7117 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
7118 fl->fl_u.nfs4_fl.owner,
7119 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
7123 data->arg.block = 1;
7124 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
7125 recovery_type > NFS_LOCK_NEW);
7126 msg.rpc_argp = &data->arg;
7127 msg.rpc_resp = &data->res;
7128 task_setup_data.callback_data = data;
7129 if (recovery_type > NFS_LOCK_NEW) {
7130 if (recovery_type == NFS_LOCK_RECLAIM)
7131 data->arg.reclaim = NFS_LOCK_RECLAIM;
7133 data->arg.new_lock = 1;
7134 task = rpc_run_task(&task_setup_data);
7136 return PTR_ERR(task);
7137 ret = rpc_wait_for_completion_task(task);
7139 ret = data->rpc_status;
7141 nfs4_handle_setlk_error(data->server, data->lsp,
7142 data->arg.new_lock_owner, ret);
7144 data->cancelled = true;
7145 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
7147 dprintk("%s: done, ret = %d!\n", __func__, ret);
7151 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
7153 struct nfs_server *server = NFS_SERVER(state->inode);
7154 struct nfs4_exception exception = {
7155 .inode = state->inode,
7160 /* Cache the lock if possible... */
7161 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7163 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
7164 if (err != -NFS4ERR_DELAY)
7166 nfs4_handle_exception(server, err, &exception);
7167 } while (exception.retry);
7171 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
7173 struct nfs_server *server = NFS_SERVER(state->inode);
7174 struct nfs4_exception exception = {
7175 .inode = state->inode,
7179 err = nfs4_set_lock_state(state, request);
7182 if (!recover_lost_locks) {
7183 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
7187 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7189 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
7193 case -NFS4ERR_GRACE:
7194 case -NFS4ERR_DELAY:
7195 nfs4_handle_exception(server, err, &exception);
7198 } while (exception.retry);
7203 #if defined(CONFIG_NFS_V4_1)
7204 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
7206 struct nfs4_lock_state *lsp;
7209 status = nfs4_set_lock_state(state, request);
7212 lsp = request->fl_u.nfs4_fl.owner;
7213 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
7214 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
7216 return nfs4_lock_expired(state, request);
7220 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7222 struct nfs_inode *nfsi = NFS_I(state->inode);
7223 struct nfs4_state_owner *sp = state->owner;
7224 unsigned char fl_flags = request->fl_flags;
7227 request->fl_flags |= FL_ACCESS;
7228 status = locks_lock_inode_wait(state->inode, request);
7231 mutex_lock(&sp->so_delegreturn_mutex);
7232 down_read(&nfsi->rwsem);
7233 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
7234 /* Yes: cache locks! */
7235 /* ...but avoid races with delegation recall... */
7236 request->fl_flags = fl_flags & ~FL_SLEEP;
7237 status = locks_lock_inode_wait(state->inode, request);
7238 up_read(&nfsi->rwsem);
7239 mutex_unlock(&sp->so_delegreturn_mutex);
7242 up_read(&nfsi->rwsem);
7243 mutex_unlock(&sp->so_delegreturn_mutex);
7244 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
7246 request->fl_flags = fl_flags;
7250 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7252 struct nfs4_exception exception = {
7254 .inode = state->inode,
7255 .interruptible = true,
7260 err = _nfs4_proc_setlk(state, cmd, request);
7261 if (err == -NFS4ERR_DENIED)
7263 err = nfs4_handle_exception(NFS_SERVER(state->inode),
7265 } while (exception.retry);
7269 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
7270 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
7273 nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
7274 struct file_lock *request)
7276 int status = -ERESTARTSYS;
7277 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
7279 while(!signalled()) {
7280 status = nfs4_proc_setlk(state, cmd, request);
7281 if ((status != -EAGAIN) || IS_SETLK(cmd))
7283 freezable_schedule_timeout_interruptible(timeout);
7285 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
7286 status = -ERESTARTSYS;
7291 #ifdef CONFIG_NFS_V4_1
7292 struct nfs4_lock_waiter {
7293 struct inode *inode;
7294 struct nfs_lowner owner;
7295 wait_queue_entry_t wait;
7299 nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
7301 struct nfs4_lock_waiter *waiter =
7302 container_of(wait, struct nfs4_lock_waiter, wait);
7304 /* NULL key means to wake up everyone */
7306 struct cb_notify_lock_args *cbnl = key;
7307 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
7308 *wowner = &waiter->owner;
7310 /* Only wake if the callback was for the same owner. */
7311 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
7314 /* Make sure it's for the right inode */
7315 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
7319 return woken_wake_function(wait, mode, flags, key);
7323 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7325 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
7326 struct nfs_server *server = NFS_SERVER(state->inode);
7327 struct nfs_client *clp = server->nfs_client;
7328 wait_queue_head_t *q = &clp->cl_lock_waitq;
7329 struct nfs4_lock_waiter waiter = {
7330 .inode = state->inode,
7331 .owner = { .clientid = clp->cl_clientid,
7332 .id = lsp->ls_seqid.owner_id,
7333 .s_dev = server->s_dev },
7337 /* Don't bother with waitqueue if we don't expect a callback */
7338 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
7339 return nfs4_retry_setlk_simple(state, cmd, request);
7341 init_wait(&waiter.wait);
7342 waiter.wait.func = nfs4_wake_lock_waiter;
7343 add_wait_queue(q, &waiter.wait);
7346 status = nfs4_proc_setlk(state, cmd, request);
7347 if (status != -EAGAIN || IS_SETLK(cmd))
7350 status = -ERESTARTSYS;
7351 freezer_do_not_count();
7352 wait_woken(&waiter.wait, TASK_INTERRUPTIBLE,
7353 NFS4_LOCK_MAXTIMEOUT);
7355 } while (!signalled());
7357 remove_wait_queue(q, &waiter.wait);
7361 #else /* !CONFIG_NFS_V4_1 */
7363 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7365 return nfs4_retry_setlk_simple(state, cmd, request);
7370 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
7372 struct nfs_open_context *ctx;
7373 struct nfs4_state *state;
7376 /* verify open state */
7377 ctx = nfs_file_open_context(filp);
7380 if (IS_GETLK(cmd)) {
7382 return nfs4_proc_getlk(state, F_GETLK, request);
7386 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
7389 if (request->fl_type == F_UNLCK) {
7391 return nfs4_proc_unlck(state, cmd, request);
7398 if ((request->fl_flags & FL_POSIX) &&
7399 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
7403 * Don't rely on the VFS having checked the file open mode,
7404 * since it won't do this for flock() locks.
7406 switch (request->fl_type) {
7408 if (!(filp->f_mode & FMODE_READ))
7412 if (!(filp->f_mode & FMODE_WRITE))
7416 status = nfs4_set_lock_state(state, request);
7420 return nfs4_retry_setlk(state, cmd, request);
7423 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
7425 struct nfs_server *server = NFS_SERVER(state->inode);
7428 err = nfs4_set_lock_state(state, fl);
7432 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
7433 if (err != -NFS4ERR_DELAY)
7436 } while (err == -NFS4ERR_DELAY);
7437 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
7440 struct nfs_release_lockowner_data {
7441 struct nfs4_lock_state *lsp;
7442 struct nfs_server *server;
7443 struct nfs_release_lockowner_args args;
7444 struct nfs_release_lockowner_res res;
7445 unsigned long timestamp;
7448 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7450 struct nfs_release_lockowner_data *data = calldata;
7451 struct nfs_server *server = data->server;
7452 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7453 &data->res.seq_res, task);
7454 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7455 data->timestamp = jiffies;
7458 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7460 struct nfs_release_lockowner_data *data = calldata;
7461 struct nfs_server *server = data->server;
7463 nfs40_sequence_done(task, &data->res.seq_res);
7465 switch (task->tk_status) {
7467 renew_lease(server, data->timestamp);
7469 case -NFS4ERR_STALE_CLIENTID:
7470 case -NFS4ERR_EXPIRED:
7471 nfs4_schedule_lease_recovery(server->nfs_client);
7473 case -NFS4ERR_LEASE_MOVED:
7474 case -NFS4ERR_DELAY:
7475 if (nfs4_async_handle_error(task, server,
7476 NULL, NULL) == -EAGAIN)
7477 rpc_restart_call_prepare(task);
7481 static void nfs4_release_lockowner_release(void *calldata)
7483 struct nfs_release_lockowner_data *data = calldata;
7484 nfs4_free_lock_state(data->server, data->lsp);
7488 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
7489 .rpc_call_prepare = nfs4_release_lockowner_prepare,
7490 .rpc_call_done = nfs4_release_lockowner_done,
7491 .rpc_release = nfs4_release_lockowner_release,
7495 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
7497 struct nfs_release_lockowner_data *data;
7498 struct rpc_message msg = {
7499 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7502 if (server->nfs_client->cl_mvops->minor_version != 0)
7505 data = kmalloc(sizeof(*data), GFP_NOFS);
7509 data->server = server;
7510 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7511 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7512 data->args.lock_owner.s_dev = server->s_dev;
7514 msg.rpc_argp = &data->args;
7515 msg.rpc_resp = &data->res;
7516 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
7517 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
7520 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7522 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
7523 struct user_namespace *mnt_userns,
7524 struct dentry *unused, struct inode *inode,
7525 const char *key, const void *buf,
7526 size_t buflen, int flags)
7528 return nfs4_proc_set_acl(inode, buf, buflen);
7531 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
7532 struct dentry *unused, struct inode *inode,
7533 const char *key, void *buf, size_t buflen)
7535 return nfs4_proc_get_acl(inode, buf, buflen);
7538 static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
7540 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
7543 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
7545 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
7546 struct user_namespace *mnt_userns,
7547 struct dentry *unused, struct inode *inode,
7548 const char *key, const void *buf,
7549 size_t buflen, int flags)
7551 if (security_ismaclabel(key))
7552 return nfs4_set_security_label(inode, buf, buflen);
7557 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
7558 struct dentry *unused, struct inode *inode,
7559 const char *key, void *buf, size_t buflen)
7561 if (security_ismaclabel(key))
7562 return nfs4_get_security_label(inode, buf, buflen);
7567 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7571 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7572 len = security_inode_listsecurity(inode, list, list_len);
7573 if (len >= 0 && list_len && len > list_len)
7579 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7580 .prefix = XATTR_SECURITY_PREFIX,
7581 .get = nfs4_xattr_get_nfs4_label,
7582 .set = nfs4_xattr_set_nfs4_label,
7588 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7595 #ifdef CONFIG_NFS_V4_2
7596 static int nfs4_xattr_set_nfs4_user(const struct xattr_handler *handler,
7597 struct user_namespace *mnt_userns,
7598 struct dentry *unused, struct inode *inode,
7599 const char *key, const void *buf,
7600 size_t buflen, int flags)
7602 struct nfs_access_entry cache;
7605 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7609 * There is no mapping from the MAY_* flags to the NFS_ACCESS_XA*
7610 * flags right now. Handling of xattr operations use the normal
7611 * file read/write permissions.
7613 * Just in case the server has other ideas (which RFC 8276 allows),
7614 * do a cached access check for the XA* flags to possibly avoid
7615 * doing an RPC and getting EACCES back.
7617 if (!nfs_access_get_cached(inode, current_cred(), &cache, true)) {
7618 if (!(cache.mask & NFS_ACCESS_XAWRITE))
7623 ret = nfs42_proc_removexattr(inode, key);
7625 nfs4_xattr_cache_remove(inode, key);
7627 ret = nfs42_proc_setxattr(inode, key, buf, buflen, flags);
7629 nfs4_xattr_cache_add(inode, key, buf, NULL, buflen);
7635 static int nfs4_xattr_get_nfs4_user(const struct xattr_handler *handler,
7636 struct dentry *unused, struct inode *inode,
7637 const char *key, void *buf, size_t buflen)
7639 struct nfs_access_entry cache;
7642 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7645 if (!nfs_access_get_cached(inode, current_cred(), &cache, true)) {
7646 if (!(cache.mask & NFS_ACCESS_XAREAD))
7650 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
7654 ret = nfs4_xattr_cache_get(inode, key, buf, buflen);
7655 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7658 ret = nfs42_proc_getxattr(inode, key, buf, buflen);
7664 nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7671 struct nfs_access_entry cache;
7673 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7676 if (!nfs_access_get_cached(inode, current_cred(), &cache, true)) {
7677 if (!(cache.mask & NFS_ACCESS_XALIST))
7681 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
7685 ret = nfs4_xattr_cache_list(inode, list, list_len);
7686 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7691 buflen = list_len ? list_len : XATTR_LIST_MAX;
7692 buf = list_len ? list : NULL;
7696 ret = nfs42_proc_listxattrs(inode, buf, buflen,
7709 nfs4_xattr_cache_set_list(inode, list, size);
7717 nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7721 #endif /* CONFIG_NFS_V4_2 */
7724 * nfs_fhget will use either the mounted_on_fileid or the fileid
7726 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7728 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7729 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7730 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
7731 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
7734 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
7735 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
7736 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7740 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7741 const struct qstr *name,
7742 struct nfs4_fs_locations *fs_locations,
7745 struct nfs_server *server = NFS_SERVER(dir);
7747 struct nfs4_fs_locations_arg args = {
7748 .dir_fh = NFS_FH(dir),
7753 struct nfs4_fs_locations_res res = {
7754 .fs_locations = fs_locations,
7756 struct rpc_message msg = {
7757 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7763 dprintk("%s: start\n", __func__);
7765 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7766 bitmask[1] = nfs4_fattr_bitmap[1];
7768 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7769 * is not supported */
7770 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
7771 bitmask[0] &= ~FATTR4_WORD0_FILEID;
7773 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
7775 nfs_fattr_init(&fs_locations->fattr);
7776 fs_locations->server = server;
7777 fs_locations->nlocations = 0;
7778 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
7779 dprintk("%s: returned status = %d\n", __func__, status);
7783 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7784 const struct qstr *name,
7785 struct nfs4_fs_locations *fs_locations,
7788 struct nfs4_exception exception = {
7789 .interruptible = true,
7793 err = _nfs4_proc_fs_locations(client, dir, name,
7794 fs_locations, page);
7795 trace_nfs4_get_fs_locations(dir, name, err);
7796 err = nfs4_handle_exception(NFS_SERVER(dir), err,
7798 } while (exception.retry);
7803 * This operation also signals the server that this client is
7804 * performing migration recovery. The server can stop returning
7805 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7806 * appended to this compound to identify the client ID which is
7807 * performing recovery.
7809 static int _nfs40_proc_get_locations(struct inode *inode,
7810 struct nfs4_fs_locations *locations,
7811 struct page *page, const struct cred *cred)
7813 struct nfs_server *server = NFS_SERVER(inode);
7814 struct rpc_clnt *clnt = server->client;
7816 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7818 struct nfs4_fs_locations_arg args = {
7819 .clientid = server->nfs_client->cl_clientid,
7820 .fh = NFS_FH(inode),
7823 .migration = 1, /* skip LOOKUP */
7824 .renew = 1, /* append RENEW */
7826 struct nfs4_fs_locations_res res = {
7827 .fs_locations = locations,
7831 struct rpc_message msg = {
7832 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7837 unsigned long now = jiffies;
7840 nfs_fattr_init(&locations->fattr);
7841 locations->server = server;
7842 locations->nlocations = 0;
7844 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7845 status = nfs4_call_sync_sequence(clnt, server, &msg,
7846 &args.seq_args, &res.seq_res);
7850 renew_lease(server, now);
7854 #ifdef CONFIG_NFS_V4_1
7857 * This operation also signals the server that this client is
7858 * performing migration recovery. The server can stop asserting
7859 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7860 * performing this operation is identified in the SEQUENCE
7861 * operation in this compound.
7863 * When the client supports GETATTR(fs_locations_info), it can
7864 * be plumbed in here.
7866 static int _nfs41_proc_get_locations(struct inode *inode,
7867 struct nfs4_fs_locations *locations,
7868 struct page *page, const struct cred *cred)
7870 struct nfs_server *server = NFS_SERVER(inode);
7871 struct rpc_clnt *clnt = server->client;
7873 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7875 struct nfs4_fs_locations_arg args = {
7876 .fh = NFS_FH(inode),
7879 .migration = 1, /* skip LOOKUP */
7881 struct nfs4_fs_locations_res res = {
7882 .fs_locations = locations,
7885 struct rpc_message msg = {
7886 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7893 nfs_fattr_init(&locations->fattr);
7894 locations->server = server;
7895 locations->nlocations = 0;
7897 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7898 status = nfs4_call_sync_sequence(clnt, server, &msg,
7899 &args.seq_args, &res.seq_res);
7900 if (status == NFS4_OK &&
7901 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7902 status = -NFS4ERR_LEASE_MOVED;
7906 #endif /* CONFIG_NFS_V4_1 */
7909 * nfs4_proc_get_locations - discover locations for a migrated FSID
7910 * @inode: inode on FSID that is migrating
7911 * @locations: result of query
7913 * @cred: credential to use for this operation
7915 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7916 * operation failed, or a negative errno if a local error occurred.
7918 * On success, "locations" is filled in, but if the server has
7919 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7922 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7923 * from this client that require migration recovery.
7925 int nfs4_proc_get_locations(struct inode *inode,
7926 struct nfs4_fs_locations *locations,
7927 struct page *page, const struct cred *cred)
7929 struct nfs_server *server = NFS_SERVER(inode);
7930 struct nfs_client *clp = server->nfs_client;
7931 const struct nfs4_mig_recovery_ops *ops =
7932 clp->cl_mvops->mig_recovery_ops;
7933 struct nfs4_exception exception = {
7934 .interruptible = true,
7938 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7939 (unsigned long long)server->fsid.major,
7940 (unsigned long long)server->fsid.minor,
7942 nfs_display_fhandle(NFS_FH(inode), __func__);
7945 status = ops->get_locations(inode, locations, page, cred);
7946 if (status != -NFS4ERR_DELAY)
7948 nfs4_handle_exception(server, status, &exception);
7949 } while (exception.retry);
7954 * This operation also signals the server that this client is
7955 * performing "lease moved" recovery. The server can stop
7956 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7957 * is appended to this compound to identify the client ID which is
7958 * performing recovery.
7960 static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
7962 struct nfs_server *server = NFS_SERVER(inode);
7963 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7964 struct rpc_clnt *clnt = server->client;
7965 struct nfs4_fsid_present_arg args = {
7966 .fh = NFS_FH(inode),
7967 .clientid = clp->cl_clientid,
7968 .renew = 1, /* append RENEW */
7970 struct nfs4_fsid_present_res res = {
7973 struct rpc_message msg = {
7974 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7979 unsigned long now = jiffies;
7982 res.fh = nfs_alloc_fhandle();
7986 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7987 status = nfs4_call_sync_sequence(clnt, server, &msg,
7988 &args.seq_args, &res.seq_res);
7989 nfs_free_fhandle(res.fh);
7993 do_renew_lease(clp, now);
7997 #ifdef CONFIG_NFS_V4_1
8000 * This operation also signals the server that this client is
8001 * performing "lease moved" recovery. The server can stop asserting
8002 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
8003 * this operation is identified in the SEQUENCE operation in this
8006 static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
8008 struct nfs_server *server = NFS_SERVER(inode);
8009 struct rpc_clnt *clnt = server->client;
8010 struct nfs4_fsid_present_arg args = {
8011 .fh = NFS_FH(inode),
8013 struct nfs4_fsid_present_res res = {
8015 struct rpc_message msg = {
8016 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
8023 res.fh = nfs_alloc_fhandle();
8027 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8028 status = nfs4_call_sync_sequence(clnt, server, &msg,
8029 &args.seq_args, &res.seq_res);
8030 nfs_free_fhandle(res.fh);
8031 if (status == NFS4_OK &&
8032 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
8033 status = -NFS4ERR_LEASE_MOVED;
8037 #endif /* CONFIG_NFS_V4_1 */
8040 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
8041 * @inode: inode on FSID to check
8042 * @cred: credential to use for this operation
8044 * Server indicates whether the FSID is present, moved, or not
8045 * recognized. This operation is necessary to clear a LEASE_MOVED
8046 * condition for this client ID.
8048 * Returns NFS4_OK if the FSID is present on this server,
8049 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
8050 * NFS4ERR code if some error occurred on the server, or a
8051 * negative errno if a local failure occurred.
8053 int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
8055 struct nfs_server *server = NFS_SERVER(inode);
8056 struct nfs_client *clp = server->nfs_client;
8057 const struct nfs4_mig_recovery_ops *ops =
8058 clp->cl_mvops->mig_recovery_ops;
8059 struct nfs4_exception exception = {
8060 .interruptible = true,
8064 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
8065 (unsigned long long)server->fsid.major,
8066 (unsigned long long)server->fsid.minor,
8068 nfs_display_fhandle(NFS_FH(inode), __func__);
8071 status = ops->fsid_present(inode, cred);
8072 if (status != -NFS4ERR_DELAY)
8074 nfs4_handle_exception(server, status, &exception);
8075 } while (exception.retry);
8080 * If 'use_integrity' is true and the state managment nfs_client
8081 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
8082 * and the machine credential as per RFC3530bis and RFC5661 Security
8083 * Considerations sections. Otherwise, just use the user cred with the
8084 * filesystem's rpc_client.
8086 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
8089 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
8090 struct nfs_client *clp = NFS_SERVER(dir)->nfs_client;
8091 struct nfs4_secinfo_arg args = {
8092 .dir_fh = NFS_FH(dir),
8095 struct nfs4_secinfo_res res = {
8098 struct rpc_message msg = {
8099 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
8103 struct nfs4_call_sync_data data = {
8104 .seq_server = NFS_SERVER(dir),
8105 .seq_args = &args.seq_args,
8106 .seq_res = &res.seq_res,
8108 struct rpc_task_setup task_setup = {
8110 .rpc_message = &msg,
8111 .callback_ops = clp->cl_mvops->call_sync_ops,
8112 .callback_data = &data,
8113 .flags = RPC_TASK_NO_ROUND_ROBIN,
8115 const struct cred *cred = NULL;
8117 if (use_integrity) {
8118 clnt = clp->cl_rpcclient;
8119 task_setup.rpc_client = clnt;
8121 cred = nfs4_get_clid_cred(clp);
8122 msg.rpc_cred = cred;
8125 dprintk("NFS call secinfo %s\n", name->name);
8127 nfs4_state_protect(clp, NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
8128 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
8129 status = nfs4_call_sync_custom(&task_setup);
8131 dprintk("NFS reply secinfo: %d\n", status);
8137 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
8138 struct nfs4_secinfo_flavors *flavors)
8140 struct nfs4_exception exception = {
8141 .interruptible = true,
8145 err = -NFS4ERR_WRONGSEC;
8147 /* try to use integrity protection with machine cred */
8148 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
8149 err = _nfs4_proc_secinfo(dir, name, flavors, true);
8152 * if unable to use integrity protection, or SECINFO with
8153 * integrity protection returns NFS4ERR_WRONGSEC (which is
8154 * disallowed by spec, but exists in deployed servers) use
8155 * the current filesystem's rpc_client and the user cred.
8157 if (err == -NFS4ERR_WRONGSEC)
8158 err = _nfs4_proc_secinfo(dir, name, flavors, false);
8160 trace_nfs4_secinfo(dir, name, err);
8161 err = nfs4_handle_exception(NFS_SERVER(dir), err,
8163 } while (exception.retry);
8167 #ifdef CONFIG_NFS_V4_1
8169 * Check the exchange flags returned by the server for invalid flags, having
8170 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
8173 static int nfs4_check_cl_exchange_flags(u32 flags, u32 version)
8175 if (version >= 2 && (flags & ~EXCHGID4_2_FLAG_MASK_R))
8177 else if (version < 2 && (flags & ~EXCHGID4_FLAG_MASK_R))
8179 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
8180 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
8182 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
8186 return -NFS4ERR_INVAL;
8190 nfs41_same_server_scope(struct nfs41_server_scope *a,
8191 struct nfs41_server_scope *b)
8193 if (a->server_scope_sz != b->server_scope_sz)
8195 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
8199 nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
8201 struct nfs41_bind_conn_to_session_args *args = task->tk_msg.rpc_argp;
8202 struct nfs41_bind_conn_to_session_res *res = task->tk_msg.rpc_resp;
8203 struct nfs_client *clp = args->client;
8205 switch (task->tk_status) {
8206 case -NFS4ERR_BADSESSION:
8207 case -NFS4ERR_DEADSESSION:
8208 nfs4_schedule_session_recovery(clp->cl_session,
8211 if (args->dir == NFS4_CDFC4_FORE_OR_BOTH &&
8212 res->dir != NFS4_CDFS4_BOTH) {
8213 rpc_task_close_connection(task);
8214 if (args->retries++ < MAX_BIND_CONN_TO_SESSION_RETRIES)
8215 rpc_restart_call(task);
8219 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
8220 .rpc_call_done = nfs4_bind_one_conn_to_session_done,
8224 * nfs4_proc_bind_one_conn_to_session()
8226 * The 4.1 client currently uses the same TCP connection for the
8227 * fore and backchannel.
8230 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
8231 struct rpc_xprt *xprt,
8232 struct nfs_client *clp,
8233 const struct cred *cred)
8236 struct nfs41_bind_conn_to_session_args args = {
8238 .dir = NFS4_CDFC4_FORE_OR_BOTH,
8241 struct nfs41_bind_conn_to_session_res res;
8242 struct rpc_message msg = {
8244 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
8249 struct rpc_task_setup task_setup_data = {
8252 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
8253 .rpc_message = &msg,
8254 .flags = RPC_TASK_TIMEOUT,
8256 struct rpc_task *task;
8258 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
8259 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
8260 args.dir = NFS4_CDFC4_FORE;
8262 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
8263 if (xprt != rcu_access_pointer(clnt->cl_xprt))
8264 args.dir = NFS4_CDFC4_FORE;
8266 task = rpc_run_task(&task_setup_data);
8267 if (!IS_ERR(task)) {
8268 status = task->tk_status;
8271 status = PTR_ERR(task);
8272 trace_nfs4_bind_conn_to_session(clp, status);
8274 if (memcmp(res.sessionid.data,
8275 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
8276 dprintk("NFS: %s: Session ID mismatch\n", __func__);
8279 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
8280 dprintk("NFS: %s: Unexpected direction from server\n",
8284 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
8285 dprintk("NFS: %s: Server returned RDMA mode = true\n",
8294 struct rpc_bind_conn_calldata {
8295 struct nfs_client *clp;
8296 const struct cred *cred;
8300 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
8301 struct rpc_xprt *xprt,
8304 struct rpc_bind_conn_calldata *p = calldata;
8306 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
8309 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
8311 struct rpc_bind_conn_calldata data = {
8315 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
8316 nfs4_proc_bind_conn_to_session_callback, &data);
8320 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
8321 * and operations we'd like to see to enable certain features in the allow map
8323 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
8324 .how = SP4_MACH_CRED,
8325 .enforce.u.words = {
8326 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8327 1 << (OP_EXCHANGE_ID - 32) |
8328 1 << (OP_CREATE_SESSION - 32) |
8329 1 << (OP_DESTROY_SESSION - 32) |
8330 1 << (OP_DESTROY_CLIENTID - 32)
8333 [0] = 1 << (OP_CLOSE) |
8334 1 << (OP_OPEN_DOWNGRADE) |
8336 1 << (OP_DELEGRETURN) |
8338 [1] = 1 << (OP_SECINFO - 32) |
8339 1 << (OP_SECINFO_NO_NAME - 32) |
8340 1 << (OP_LAYOUTRETURN - 32) |
8341 1 << (OP_TEST_STATEID - 32) |
8342 1 << (OP_FREE_STATEID - 32) |
8343 1 << (OP_WRITE - 32)
8348 * Select the state protection mode for client `clp' given the server results
8349 * from exchange_id in `sp'.
8351 * Returns 0 on success, negative errno otherwise.
8353 static int nfs4_sp4_select_mode(struct nfs_client *clp,
8354 struct nfs41_state_protection *sp)
8356 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
8357 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8358 1 << (OP_EXCHANGE_ID - 32) |
8359 1 << (OP_CREATE_SESSION - 32) |
8360 1 << (OP_DESTROY_SESSION - 32) |
8361 1 << (OP_DESTROY_CLIENTID - 32)
8363 unsigned long flags = 0;
8367 if (sp->how == SP4_MACH_CRED) {
8368 /* Print state protect result */
8369 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
8370 for (i = 0; i <= LAST_NFS4_OP; i++) {
8371 if (test_bit(i, sp->enforce.u.longs))
8372 dfprintk(MOUNT, " enforce op %d\n", i);
8373 if (test_bit(i, sp->allow.u.longs))
8374 dfprintk(MOUNT, " allow op %d\n", i);
8377 /* make sure nothing is on enforce list that isn't supported */
8378 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
8379 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
8380 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
8387 * Minimal mode - state operations are allowed to use machine
8388 * credential. Note this already happens by default, so the
8389 * client doesn't have to do anything more than the negotiation.
8391 * NOTE: we don't care if EXCHANGE_ID is in the list -
8392 * we're already using the machine cred for exchange_id
8393 * and will never use a different cred.
8395 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
8396 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
8397 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
8398 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
8399 dfprintk(MOUNT, "sp4_mach_cred:\n");
8400 dfprintk(MOUNT, " minimal mode enabled\n");
8401 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
8403 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
8408 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
8409 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
8410 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
8411 test_bit(OP_LOCKU, sp->allow.u.longs)) {
8412 dfprintk(MOUNT, " cleanup mode enabled\n");
8413 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
8416 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
8417 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
8418 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
8421 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
8422 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
8423 dfprintk(MOUNT, " secinfo mode enabled\n");
8424 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
8427 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
8428 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
8429 dfprintk(MOUNT, " stateid mode enabled\n");
8430 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
8433 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
8434 dfprintk(MOUNT, " write mode enabled\n");
8435 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
8438 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
8439 dfprintk(MOUNT, " commit mode enabled\n");
8440 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
8444 clp->cl_sp4_flags = flags;
8448 struct nfs41_exchange_id_data {
8449 struct nfs41_exchange_id_res res;
8450 struct nfs41_exchange_id_args args;
8453 static void nfs4_exchange_id_release(void *data)
8455 struct nfs41_exchange_id_data *cdata =
8456 (struct nfs41_exchange_id_data *)data;
8458 nfs_put_client(cdata->args.client);
8459 kfree(cdata->res.impl_id);
8460 kfree(cdata->res.server_scope);
8461 kfree(cdata->res.server_owner);
8465 static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
8466 .rpc_release = nfs4_exchange_id_release,
8470 * _nfs4_proc_exchange_id()
8472 * Wrapper for EXCHANGE_ID operation.
8474 static struct rpc_task *
8475 nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
8476 u32 sp4_how, struct rpc_xprt *xprt)
8478 struct rpc_message msg = {
8479 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
8482 struct rpc_task_setup task_setup_data = {
8483 .rpc_client = clp->cl_rpcclient,
8484 .callback_ops = &nfs4_exchange_id_call_ops,
8485 .rpc_message = &msg,
8486 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
8488 struct nfs41_exchange_id_data *calldata;
8491 if (!refcount_inc_not_zero(&clp->cl_count))
8492 return ERR_PTR(-EIO);
8495 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8499 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
8501 status = nfs4_init_uniform_client_string(clp);
8505 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
8508 if (unlikely(calldata->res.server_owner == NULL))
8511 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
8513 if (unlikely(calldata->res.server_scope == NULL))
8514 goto out_server_owner;
8516 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
8517 if (unlikely(calldata->res.impl_id == NULL))
8518 goto out_server_scope;
8522 calldata->args.state_protect.how = SP4_NONE;
8526 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
8536 task_setup_data.rpc_xprt = xprt;
8537 task_setup_data.flags |= RPC_TASK_SOFTCONN;
8538 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
8539 sizeof(calldata->args.verifier.data));
8541 calldata->args.client = clp;
8542 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
8543 EXCHGID4_FLAG_BIND_PRINC_STATEID;
8544 #ifdef CONFIG_NFS_V4_1_MIGRATION
8545 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
8547 msg.rpc_argp = &calldata->args;
8548 msg.rpc_resp = &calldata->res;
8549 task_setup_data.callback_data = calldata;
8551 return rpc_run_task(&task_setup_data);
8554 kfree(calldata->res.impl_id);
8556 kfree(calldata->res.server_scope);
8558 kfree(calldata->res.server_owner);
8562 nfs_put_client(clp);
8563 return ERR_PTR(status);
8567 * _nfs4_proc_exchange_id()
8569 * Wrapper for EXCHANGE_ID operation.
8571 static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
8574 struct rpc_task *task;
8575 struct nfs41_exchange_id_args *argp;
8576 struct nfs41_exchange_id_res *resp;
8577 unsigned long now = jiffies;
8580 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
8582 return PTR_ERR(task);
8584 argp = task->tk_msg.rpc_argp;
8585 resp = task->tk_msg.rpc_resp;
8586 status = task->tk_status;
8590 status = nfs4_check_cl_exchange_flags(resp->flags,
8591 clp->cl_mvops->minor_version);
8595 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8599 do_renew_lease(clp, now);
8601 clp->cl_clientid = resp->clientid;
8602 clp->cl_exchange_flags = resp->flags;
8603 clp->cl_seqid = resp->seqid;
8604 /* Client ID is not confirmed */
8605 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8606 clear_bit(NFS4_SESSION_ESTABLISHED,
8607 &clp->cl_session->session_state);
8609 if (clp->cl_serverscope != NULL &&
8610 !nfs41_same_server_scope(clp->cl_serverscope,
8611 resp->server_scope)) {
8612 dprintk("%s: server_scope mismatch detected\n",
8614 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8617 swap(clp->cl_serverowner, resp->server_owner);
8618 swap(clp->cl_serverscope, resp->server_scope);
8619 swap(clp->cl_implid, resp->impl_id);
8621 /* Save the EXCHANGE_ID verifier session trunk tests */
8622 memcpy(clp->cl_confirm.data, argp->verifier.data,
8623 sizeof(clp->cl_confirm.data));
8625 trace_nfs4_exchange_id(clp, status);
8631 * nfs4_proc_exchange_id()
8633 * Returns zero, a negative errno, or a negative NFS4ERR status code.
8635 * Since the clientid has expired, all compounds using sessions
8636 * associated with the stale clientid will be returning
8637 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
8638 * be in some phase of session reset.
8640 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
8642 int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
8644 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8647 /* try SP4_MACH_CRED if krb5i/p */
8648 if (authflavor == RPC_AUTH_GSS_KRB5I ||
8649 authflavor == RPC_AUTH_GSS_KRB5P) {
8650 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
8656 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
8660 * nfs4_test_session_trunk
8662 * This is an add_xprt_test() test function called from
8663 * rpc_clnt_setup_test_and_add_xprt.
8665 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8666 * and is dereferrenced in nfs4_exchange_id_release
8668 * Upon success, add the new transport to the rpc_clnt
8670 * @clnt: struct rpc_clnt to get new transport
8671 * @xprt: the rpc_xprt to test
8672 * @data: call data for _nfs4_proc_exchange_id.
8674 void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
8677 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
8678 struct rpc_task *task;
8683 dprintk("--> %s try %s\n", __func__,
8684 xprt->address_strings[RPC_DISPLAY_ADDR]);
8686 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8688 /* Test connection for session trunking. Async exchange_id call */
8689 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8693 status = task->tk_status;
8695 status = nfs4_detect_session_trunking(adata->clp,
8696 task->tk_msg.rpc_resp, xprt);
8699 rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
8703 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
8705 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
8706 const struct cred *cred)
8708 struct rpc_message msg = {
8709 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8715 status = rpc_call_sync(clp->cl_rpcclient, &msg,
8716 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
8717 trace_nfs4_destroy_clientid(clp, status);
8719 dprintk("NFS: Got error %d from the server %s on "
8720 "DESTROY_CLIENTID.", status, clp->cl_hostname);
8724 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
8725 const struct cred *cred)
8730 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8731 ret = _nfs4_proc_destroy_clientid(clp, cred);
8733 case -NFS4ERR_DELAY:
8734 case -NFS4ERR_CLIENTID_BUSY:
8744 int nfs4_destroy_clientid(struct nfs_client *clp)
8746 const struct cred *cred;
8749 if (clp->cl_mvops->minor_version < 1)
8751 if (clp->cl_exchange_flags == 0)
8753 if (clp->cl_preserve_clid)
8755 cred = nfs4_get_clid_cred(clp);
8756 ret = nfs4_proc_destroy_clientid(clp, cred);
8760 case -NFS4ERR_STALE_CLIENTID:
8761 clp->cl_exchange_flags = 0;
8767 #endif /* CONFIG_NFS_V4_1 */
8769 struct nfs4_get_lease_time_data {
8770 struct nfs4_get_lease_time_args *args;
8771 struct nfs4_get_lease_time_res *res;
8772 struct nfs_client *clp;
8775 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
8778 struct nfs4_get_lease_time_data *data =
8779 (struct nfs4_get_lease_time_data *)calldata;
8781 dprintk("--> %s\n", __func__);
8782 /* just setup sequence, do not trigger session recovery
8783 since we're invoked within one */
8784 nfs4_setup_sequence(data->clp,
8785 &data->args->la_seq_args,
8786 &data->res->lr_seq_res,
8788 dprintk("<-- %s\n", __func__);
8792 * Called from nfs4_state_manager thread for session setup, so don't recover
8793 * from sequence operation or clientid errors.
8795 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8797 struct nfs4_get_lease_time_data *data =
8798 (struct nfs4_get_lease_time_data *)calldata;
8800 dprintk("--> %s\n", __func__);
8801 if (!nfs4_sequence_done(task, &data->res->lr_seq_res))
8803 switch (task->tk_status) {
8804 case -NFS4ERR_DELAY:
8805 case -NFS4ERR_GRACE:
8806 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
8807 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8808 task->tk_status = 0;
8810 case -NFS4ERR_RETRY_UNCACHED_REP:
8811 rpc_restart_call_prepare(task);
8814 dprintk("<-- %s\n", __func__);
8817 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
8818 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8819 .rpc_call_done = nfs4_get_lease_time_done,
8822 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8824 struct nfs4_get_lease_time_args args;
8825 struct nfs4_get_lease_time_res res = {
8826 .lr_fsinfo = fsinfo,
8828 struct nfs4_get_lease_time_data data = {
8833 struct rpc_message msg = {
8834 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8838 struct rpc_task_setup task_setup = {
8839 .rpc_client = clp->cl_rpcclient,
8840 .rpc_message = &msg,
8841 .callback_ops = &nfs4_get_lease_time_ops,
8842 .callback_data = &data,
8843 .flags = RPC_TASK_TIMEOUT,
8846 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
8847 return nfs4_call_sync_custom(&task_setup);
8850 #ifdef CONFIG_NFS_V4_1
8853 * Initialize the values to be used by the client in CREATE_SESSION
8854 * If nfs4_init_session set the fore channel request and response sizes,
8857 * Set the back channel max_resp_sz_cached to zero to force the client to
8858 * always set csa_cachethis to FALSE because the current implementation
8859 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8861 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8862 struct rpc_clnt *clnt)
8864 unsigned int max_rqst_sz, max_resp_sz;
8865 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
8866 unsigned int max_bc_slots = rpc_num_bc_slots(clnt);
8868 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8869 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8871 /* Fore channel attributes */
8872 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8873 args->fc_attrs.max_resp_sz = max_resp_sz;
8874 args->fc_attrs.max_ops = NFS4_MAX_OPS;
8875 args->fc_attrs.max_reqs = max_session_slots;
8877 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
8878 "max_ops=%u max_reqs=%u\n",
8880 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
8881 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
8883 /* Back channel attributes */
8884 args->bc_attrs.max_rqst_sz = max_bc_payload;
8885 args->bc_attrs.max_resp_sz = max_bc_payload;
8886 args->bc_attrs.max_resp_sz_cached = 0;
8887 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
8888 args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
8889 if (args->bc_attrs.max_reqs > max_bc_slots)
8890 args->bc_attrs.max_reqs = max_bc_slots;
8892 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8893 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8895 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8896 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8897 args->bc_attrs.max_reqs);
8900 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8901 struct nfs41_create_session_res *res)
8903 struct nfs4_channel_attrs *sent = &args->fc_attrs;
8904 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
8906 if (rcvd->max_resp_sz > sent->max_resp_sz)
8909 * Our requested max_ops is the minimum we need; we're not
8910 * prepared to break up compounds into smaller pieces than that.
8911 * So, no point even trying to continue if the server won't
8914 if (rcvd->max_ops < sent->max_ops)
8916 if (rcvd->max_reqs == 0)
8918 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8919 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
8923 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8924 struct nfs41_create_session_res *res)
8926 struct nfs4_channel_attrs *sent = &args->bc_attrs;
8927 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
8929 if (!(res->flags & SESSION4_BACK_CHAN))
8931 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8933 if (rcvd->max_resp_sz < sent->max_resp_sz)
8935 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8937 if (rcvd->max_ops > sent->max_ops)
8939 if (rcvd->max_reqs > sent->max_reqs)
8945 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
8946 struct nfs41_create_session_res *res)
8950 ret = nfs4_verify_fore_channel_attrs(args, res);
8953 return nfs4_verify_back_channel_attrs(args, res);
8956 static void nfs4_update_session(struct nfs4_session *session,
8957 struct nfs41_create_session_res *res)
8959 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
8960 /* Mark client id and session as being confirmed */
8961 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8962 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
8963 session->flags = res->flags;
8964 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
8965 if (res->flags & SESSION4_BACK_CHAN)
8966 memcpy(&session->bc_attrs, &res->bc_attrs,
8967 sizeof(session->bc_attrs));
8970 static int _nfs4_proc_create_session(struct nfs_client *clp,
8971 const struct cred *cred)
8973 struct nfs4_session *session = clp->cl_session;
8974 struct nfs41_create_session_args args = {
8976 .clientid = clp->cl_clientid,
8977 .seqid = clp->cl_seqid,
8978 .cb_program = NFS4_CALLBACK,
8980 struct nfs41_create_session_res res;
8982 struct rpc_message msg = {
8983 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8990 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
8991 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
8993 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
8994 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
8995 trace_nfs4_create_session(clp, status);
8998 case -NFS4ERR_STALE_CLIENTID:
8999 case -NFS4ERR_DELAY:
9008 /* Verify the session's negotiated channel_attrs values */
9009 status = nfs4_verify_channel_attrs(&args, &res);
9010 /* Increment the clientid slot sequence id */
9013 nfs4_update_session(session, &res);
9020 * Issues a CREATE_SESSION operation to the server.
9021 * It is the responsibility of the caller to verify the session is
9022 * expired before calling this routine.
9024 int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
9028 struct nfs4_session *session = clp->cl_session;
9030 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
9032 status = _nfs4_proc_create_session(clp, cred);
9036 /* Init or reset the session slot tables */
9037 status = nfs4_setup_session_slot_tables(session);
9038 dprintk("slot table setup returned %d\n", status);
9042 ptr = (unsigned *)&session->sess_id.data[0];
9043 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
9044 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
9046 dprintk("<-- %s\n", __func__);
9051 * Issue the over-the-wire RPC DESTROY_SESSION.
9052 * The caller must serialize access to this routine.
9054 int nfs4_proc_destroy_session(struct nfs4_session *session,
9055 const struct cred *cred)
9057 struct rpc_message msg = {
9058 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
9059 .rpc_argp = session,
9064 dprintk("--> nfs4_proc_destroy_session\n");
9066 /* session is still being setup */
9067 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
9070 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
9071 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
9072 trace_nfs4_destroy_session(session->clp, status);
9075 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
9076 "Session has been destroyed regardless...\n", status);
9078 dprintk("<-- nfs4_proc_destroy_session\n");
9083 * Renew the cl_session lease.
9085 struct nfs4_sequence_data {
9086 struct nfs_client *clp;
9087 struct nfs4_sequence_args args;
9088 struct nfs4_sequence_res res;
9091 static void nfs41_sequence_release(void *data)
9093 struct nfs4_sequence_data *calldata = data;
9094 struct nfs_client *clp = calldata->clp;
9096 if (refcount_read(&clp->cl_count) > 1)
9097 nfs4_schedule_state_renewal(clp);
9098 nfs_put_client(clp);
9102 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9104 switch(task->tk_status) {
9105 case -NFS4ERR_DELAY:
9106 rpc_delay(task, NFS4_POLL_RETRY_MAX);
9109 nfs4_schedule_lease_recovery(clp);
9114 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
9116 struct nfs4_sequence_data *calldata = data;
9117 struct nfs_client *clp = calldata->clp;
9119 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
9122 trace_nfs4_sequence(clp, task->tk_status);
9123 if (task->tk_status < 0) {
9124 dprintk("%s ERROR %d\n", __func__, task->tk_status);
9125 if (refcount_read(&clp->cl_count) == 1)
9128 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
9129 rpc_restart_call_prepare(task);
9133 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
9135 dprintk("<-- %s\n", __func__);
9138 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
9140 struct nfs4_sequence_data *calldata = data;
9141 struct nfs_client *clp = calldata->clp;
9142 struct nfs4_sequence_args *args;
9143 struct nfs4_sequence_res *res;
9145 args = task->tk_msg.rpc_argp;
9146 res = task->tk_msg.rpc_resp;
9148 nfs4_setup_sequence(clp, args, res, task);
9151 static const struct rpc_call_ops nfs41_sequence_ops = {
9152 .rpc_call_done = nfs41_sequence_call_done,
9153 .rpc_call_prepare = nfs41_sequence_prepare,
9154 .rpc_release = nfs41_sequence_release,
9157 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
9158 const struct cred *cred,
9159 struct nfs4_slot *slot,
9162 struct nfs4_sequence_data *calldata;
9163 struct rpc_message msg = {
9164 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
9167 struct rpc_task_setup task_setup_data = {
9168 .rpc_client = clp->cl_rpcclient,
9169 .rpc_message = &msg,
9170 .callback_ops = &nfs41_sequence_ops,
9171 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
9173 struct rpc_task *ret;
9175 ret = ERR_PTR(-EIO);
9176 if (!refcount_inc_not_zero(&clp->cl_count))
9179 ret = ERR_PTR(-ENOMEM);
9180 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
9181 if (calldata == NULL)
9183 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
9184 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
9185 msg.rpc_argp = &calldata->args;
9186 msg.rpc_resp = &calldata->res;
9187 calldata->clp = clp;
9188 task_setup_data.callback_data = calldata;
9190 ret = rpc_run_task(&task_setup_data);
9195 nfs_put_client(clp);
9197 nfs41_release_slot(slot);
9201 static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
9203 struct rpc_task *task;
9206 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
9208 task = _nfs41_proc_sequence(clp, cred, NULL, false);
9210 ret = PTR_ERR(task);
9212 rpc_put_task_async(task);
9213 dprintk("<-- %s status=%d\n", __func__, ret);
9217 static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
9219 struct rpc_task *task;
9222 task = _nfs41_proc_sequence(clp, cred, NULL, true);
9224 ret = PTR_ERR(task);
9227 ret = rpc_wait_for_completion_task(task);
9229 ret = task->tk_status;
9232 dprintk("<-- %s status=%d\n", __func__, ret);
9236 struct nfs4_reclaim_complete_data {
9237 struct nfs_client *clp;
9238 struct nfs41_reclaim_complete_args arg;
9239 struct nfs41_reclaim_complete_res res;
9242 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
9244 struct nfs4_reclaim_complete_data *calldata = data;
9246 nfs4_setup_sequence(calldata->clp,
9247 &calldata->arg.seq_args,
9248 &calldata->res.seq_res,
9252 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9254 switch(task->tk_status) {
9256 wake_up_all(&clp->cl_lock_waitq);
9258 case -NFS4ERR_COMPLETE_ALREADY:
9259 case -NFS4ERR_WRONG_CRED: /* What to do here? */
9261 case -NFS4ERR_DELAY:
9262 rpc_delay(task, NFS4_POLL_RETRY_MAX);
9264 case -NFS4ERR_RETRY_UNCACHED_REP:
9266 case -NFS4ERR_BADSESSION:
9267 case -NFS4ERR_DEADSESSION:
9268 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9271 nfs4_schedule_lease_recovery(clp);
9276 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
9278 struct nfs4_reclaim_complete_data *calldata = data;
9279 struct nfs_client *clp = calldata->clp;
9280 struct nfs4_sequence_res *res = &calldata->res.seq_res;
9282 dprintk("--> %s\n", __func__);
9283 if (!nfs41_sequence_done(task, res))
9286 trace_nfs4_reclaim_complete(clp, task->tk_status);
9287 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
9288 rpc_restart_call_prepare(task);
9291 dprintk("<-- %s\n", __func__);
9294 static void nfs4_free_reclaim_complete_data(void *data)
9296 struct nfs4_reclaim_complete_data *calldata = data;
9301 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
9302 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
9303 .rpc_call_done = nfs4_reclaim_complete_done,
9304 .rpc_release = nfs4_free_reclaim_complete_data,
9308 * Issue a global reclaim complete.
9310 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
9311 const struct cred *cred)
9313 struct nfs4_reclaim_complete_data *calldata;
9314 struct rpc_message msg = {
9315 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
9318 struct rpc_task_setup task_setup_data = {
9319 .rpc_client = clp->cl_rpcclient,
9320 .rpc_message = &msg,
9321 .callback_ops = &nfs4_reclaim_complete_call_ops,
9322 .flags = RPC_TASK_NO_ROUND_ROBIN,
9324 int status = -ENOMEM;
9326 dprintk("--> %s\n", __func__);
9327 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
9328 if (calldata == NULL)
9330 calldata->clp = clp;
9331 calldata->arg.one_fs = 0;
9333 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
9334 msg.rpc_argp = &calldata->arg;
9335 msg.rpc_resp = &calldata->res;
9336 task_setup_data.callback_data = calldata;
9337 status = nfs4_call_sync_custom(&task_setup_data);
9339 dprintk("<-- %s status=%d\n", __func__, status);
9344 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
9346 struct nfs4_layoutget *lgp = calldata;
9347 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
9349 dprintk("--> %s\n", __func__);
9350 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
9351 &lgp->res.seq_res, task);
9352 dprintk("<-- %s\n", __func__);
9355 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
9357 struct nfs4_layoutget *lgp = calldata;
9359 dprintk("--> %s\n", __func__);
9360 nfs41_sequence_process(task, &lgp->res.seq_res);
9361 dprintk("<-- %s\n", __func__);
9365 nfs4_layoutget_handle_exception(struct rpc_task *task,
9366 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
9368 struct inode *inode = lgp->args.inode;
9369 struct nfs_server *server = NFS_SERVER(inode);
9370 struct pnfs_layout_hdr *lo;
9371 int nfs4err = task->tk_status;
9372 int err, status = 0;
9375 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
9377 nfs4_sequence_free_slot(&lgp->res.seq_res);
9384 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
9385 * on the file. set tk_status to -ENODATA to tell upper layer to
9388 case -NFS4ERR_LAYOUTUNAVAILABLE:
9392 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
9393 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
9395 case -NFS4ERR_BADLAYOUT:
9396 status = -EOVERFLOW;
9399 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
9400 * (or clients) writing to the same RAID stripe except when
9401 * the minlength argument is 0 (see RFC5661 section 18.43.3).
9403 * Treat it like we would RECALLCONFLICT -- we retry for a little
9404 * while, and then eventually give up.
9406 case -NFS4ERR_LAYOUTTRYLATER:
9407 if (lgp->args.minlength == 0) {
9408 status = -EOVERFLOW;
9413 case -NFS4ERR_RECALLCONFLICT:
9414 status = -ERECALLCONFLICT;
9416 case -NFS4ERR_DELEG_REVOKED:
9417 case -NFS4ERR_ADMIN_REVOKED:
9418 case -NFS4ERR_EXPIRED:
9419 case -NFS4ERR_BAD_STATEID:
9420 exception->timeout = 0;
9421 spin_lock(&inode->i_lock);
9422 lo = NFS_I(inode)->layout;
9423 /* If the open stateid was bad, then recover it. */
9424 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
9425 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
9426 spin_unlock(&inode->i_lock);
9427 exception->state = lgp->args.ctx->state;
9428 exception->stateid = &lgp->args.stateid;
9433 * Mark the bad layout state as invalid, then retry
9435 pnfs_mark_layout_stateid_invalid(lo, &head);
9436 spin_unlock(&inode->i_lock);
9437 nfs_commit_inode(inode, 0);
9438 pnfs_free_lseg_list(&head);
9443 err = nfs4_handle_exception(server, nfs4err, exception);
9445 if (exception->retry)
9451 dprintk("<-- %s\n", __func__);
9455 size_t max_response_pages(struct nfs_server *server)
9457 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
9458 return nfs_page_array_len(0, max_resp_sz);
9461 static void nfs4_layoutget_release(void *calldata)
9463 struct nfs4_layoutget *lgp = calldata;
9465 dprintk("--> %s\n", __func__);
9466 nfs4_sequence_free_slot(&lgp->res.seq_res);
9467 pnfs_layoutget_free(lgp);
9468 dprintk("<-- %s\n", __func__);
9471 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
9472 .rpc_call_prepare = nfs4_layoutget_prepare,
9473 .rpc_call_done = nfs4_layoutget_done,
9474 .rpc_release = nfs4_layoutget_release,
9477 struct pnfs_layout_segment *
9478 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
9480 struct inode *inode = lgp->args.inode;
9481 struct nfs_server *server = NFS_SERVER(inode);
9482 struct rpc_task *task;
9483 struct rpc_message msg = {
9484 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
9485 .rpc_argp = &lgp->args,
9486 .rpc_resp = &lgp->res,
9487 .rpc_cred = lgp->cred,
9489 struct rpc_task_setup task_setup_data = {
9490 .rpc_client = server->client,
9491 .rpc_message = &msg,
9492 .callback_ops = &nfs4_layoutget_call_ops,
9493 .callback_data = lgp,
9494 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
9496 struct pnfs_layout_segment *lseg = NULL;
9497 struct nfs4_exception exception = {
9499 .timeout = *timeout,
9503 dprintk("--> %s\n", __func__);
9505 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
9506 pnfs_get_layout_hdr(NFS_I(inode)->layout);
9508 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
9510 task = rpc_run_task(&task_setup_data);
9512 status = rpc_wait_for_completion_task(task);
9516 if (task->tk_status < 0) {
9517 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
9518 *timeout = exception.timeout;
9519 } else if (lgp->res.layoutp->len == 0) {
9521 *timeout = nfs4_update_delay(&exception.timeout);
9523 lseg = pnfs_layout_process(lgp);
9525 trace_nfs4_layoutget(lgp->args.ctx,
9532 dprintk("<-- %s status=%d\n", __func__, status);
9534 return ERR_PTR(status);
9539 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
9541 struct nfs4_layoutreturn *lrp = calldata;
9543 dprintk("--> %s\n", __func__);
9544 nfs4_setup_sequence(lrp->clp,
9545 &lrp->args.seq_args,
9548 if (!pnfs_layout_is_valid(lrp->args.layout))
9552 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
9554 struct nfs4_layoutreturn *lrp = calldata;
9555 struct nfs_server *server;
9557 dprintk("--> %s\n", __func__);
9559 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
9563 * Was there an RPC level error? Assume the call succeeded,
9564 * and that we need to release the layout
9566 if (task->tk_rpc_status != 0 && RPC_WAS_SENT(task)) {
9567 lrp->res.lrs_present = 0;
9571 server = NFS_SERVER(lrp->args.inode);
9572 switch (task->tk_status) {
9573 case -NFS4ERR_OLD_STATEID:
9574 if (nfs4_layout_refresh_old_stateid(&lrp->args.stateid,
9580 task->tk_status = 0;
9584 case -NFS4ERR_DELAY:
9585 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
9589 dprintk("<-- %s\n", __func__);
9592 task->tk_status = 0;
9593 nfs4_sequence_free_slot(&lrp->res.seq_res);
9594 rpc_restart_call_prepare(task);
9597 static void nfs4_layoutreturn_release(void *calldata)
9599 struct nfs4_layoutreturn *lrp = calldata;
9600 struct pnfs_layout_hdr *lo = lrp->args.layout;
9602 dprintk("--> %s\n", __func__);
9603 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
9604 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
9605 nfs4_sequence_free_slot(&lrp->res.seq_res);
9606 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9607 lrp->ld_private.ops->free(&lrp->ld_private);
9608 pnfs_put_layout_hdr(lrp->args.layout);
9609 nfs_iput_and_deactive(lrp->inode);
9610 put_cred(lrp->cred);
9612 dprintk("<-- %s\n", __func__);
9615 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9616 .rpc_call_prepare = nfs4_layoutreturn_prepare,
9617 .rpc_call_done = nfs4_layoutreturn_done,
9618 .rpc_release = nfs4_layoutreturn_release,
9621 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
9623 struct rpc_task *task;
9624 struct rpc_message msg = {
9625 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9626 .rpc_argp = &lrp->args,
9627 .rpc_resp = &lrp->res,
9628 .rpc_cred = lrp->cred,
9630 struct rpc_task_setup task_setup_data = {
9631 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
9632 .rpc_message = &msg,
9633 .callback_ops = &nfs4_layoutreturn_call_ops,
9634 .callback_data = lrp,
9638 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9639 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9640 &task_setup_data.rpc_client, &msg);
9642 dprintk("--> %s\n", __func__);
9644 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
9646 nfs4_layoutreturn_release(lrp);
9649 task_setup_data.flags |= RPC_TASK_ASYNC;
9651 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1, 0);
9652 task = rpc_run_task(&task_setup_data);
9654 return PTR_ERR(task);
9656 status = task->tk_status;
9657 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
9658 dprintk("<-- %s status=%d\n", __func__, status);
9664 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
9665 struct pnfs_device *pdev,
9666 const struct cred *cred)
9668 struct nfs4_getdeviceinfo_args args = {
9670 .notify_types = NOTIFY_DEVICEID4_CHANGE |
9671 NOTIFY_DEVICEID4_DELETE,
9673 struct nfs4_getdeviceinfo_res res = {
9676 struct rpc_message msg = {
9677 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9684 dprintk("--> %s\n", __func__);
9685 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
9686 if (res.notification & ~args.notify_types)
9687 dprintk("%s: unsupported notification\n", __func__);
9688 if (res.notification != args.notify_types)
9691 trace_nfs4_getdeviceinfo(server, &pdev->dev_id, status);
9693 dprintk("<-- %s status=%d\n", __func__, status);
9698 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9699 struct pnfs_device *pdev,
9700 const struct cred *cred)
9702 struct nfs4_exception exception = { };
9706 err = nfs4_handle_exception(server,
9707 _nfs4_proc_getdeviceinfo(server, pdev, cred),
9709 } while (exception.retry);
9712 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9714 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9716 struct nfs4_layoutcommit_data *data = calldata;
9717 struct nfs_server *server = NFS_SERVER(data->args.inode);
9719 nfs4_setup_sequence(server->nfs_client,
9720 &data->args.seq_args,
9726 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9728 struct nfs4_layoutcommit_data *data = calldata;
9729 struct nfs_server *server = NFS_SERVER(data->args.inode);
9731 if (!nfs41_sequence_done(task, &data->res.seq_res))
9734 switch (task->tk_status) { /* Just ignore these failures */
9735 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9736 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9737 case -NFS4ERR_BADLAYOUT: /* no layout */
9738 case -NFS4ERR_GRACE: /* loca_recalim always false */
9739 task->tk_status = 0;
9744 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
9745 rpc_restart_call_prepare(task);
9751 static void nfs4_layoutcommit_release(void *calldata)
9753 struct nfs4_layoutcommit_data *data = calldata;
9755 pnfs_cleanup_layoutcommit(data);
9756 nfs_post_op_update_inode_force_wcc(data->args.inode,
9758 put_cred(data->cred);
9759 nfs_iput_and_deactive(data->inode);
9763 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9764 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9765 .rpc_call_done = nfs4_layoutcommit_done,
9766 .rpc_release = nfs4_layoutcommit_release,
9770 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
9772 struct rpc_message msg = {
9773 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9774 .rpc_argp = &data->args,
9775 .rpc_resp = &data->res,
9776 .rpc_cred = data->cred,
9778 struct rpc_task_setup task_setup_data = {
9779 .task = &data->task,
9780 .rpc_client = NFS_CLIENT(data->args.inode),
9781 .rpc_message = &msg,
9782 .callback_ops = &nfs4_layoutcommit_ops,
9783 .callback_data = data,
9785 struct rpc_task *task;
9788 dprintk("NFS: initiating layoutcommit call. sync %d "
9789 "lbw: %llu inode %lu\n", sync,
9790 data->args.lastbytewritten,
9791 data->args.inode->i_ino);
9794 data->inode = nfs_igrab_and_active(data->args.inode);
9795 if (data->inode == NULL) {
9796 nfs4_layoutcommit_release(data);
9799 task_setup_data.flags = RPC_TASK_ASYNC;
9801 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
9802 task = rpc_run_task(&task_setup_data);
9804 return PTR_ERR(task);
9806 status = task->tk_status;
9807 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
9808 dprintk("%s: status %d\n", __func__, status);
9814 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9815 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9818 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9819 struct nfs_fsinfo *info,
9820 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
9822 struct nfs41_secinfo_no_name_args args = {
9823 .style = SECINFO_STYLE_CURRENT_FH,
9825 struct nfs4_secinfo_res res = {
9828 struct rpc_message msg = {
9829 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9833 struct nfs4_call_sync_data data = {
9834 .seq_server = server,
9835 .seq_args = &args.seq_args,
9836 .seq_res = &res.seq_res,
9838 struct rpc_task_setup task_setup = {
9839 .rpc_client = server->client,
9840 .rpc_message = &msg,
9841 .callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
9842 .callback_data = &data,
9843 .flags = RPC_TASK_NO_ROUND_ROBIN,
9845 const struct cred *cred = NULL;
9848 if (use_integrity) {
9849 task_setup.rpc_client = server->nfs_client->cl_rpcclient;
9851 cred = nfs4_get_clid_cred(server->nfs_client);
9852 msg.rpc_cred = cred;
9855 dprintk("--> %s\n", __func__);
9856 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
9857 status = nfs4_call_sync_custom(&task_setup);
9858 dprintk("<-- %s status=%d\n", __func__, status);
9866 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9867 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9869 struct nfs4_exception exception = {
9870 .interruptible = true,
9874 /* first try using integrity protection */
9875 err = -NFS4ERR_WRONGSEC;
9877 /* try to use integrity protection with machine cred */
9878 if (_nfs4_is_integrity_protected(server->nfs_client))
9879 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9883 * if unable to use integrity protection, or SECINFO with
9884 * integrity protection returns NFS4ERR_WRONGSEC (which is
9885 * disallowed by spec, but exists in deployed servers) use
9886 * the current filesystem's rpc_client and the user cred.
9888 if (err == -NFS4ERR_WRONGSEC)
9889 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9894 case -NFS4ERR_WRONGSEC:
9898 err = nfs4_handle_exception(server, err, &exception);
9900 } while (exception.retry);
9906 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9907 struct nfs_fsinfo *info)
9911 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
9912 struct nfs4_secinfo_flavors *flavors;
9913 struct nfs4_secinfo4 *secinfo;
9916 page = alloc_page(GFP_KERNEL);
9922 flavors = page_address(page);
9923 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9926 * Fall back on "guess and check" method if
9927 * the server doesn't support SECINFO_NO_NAME
9929 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
9930 err = nfs4_find_root_sec(server, fhandle, info);
9936 for (i = 0; i < flavors->num_flavors; i++) {
9937 secinfo = &flavors->flavors[i];
9939 switch (secinfo->flavor) {
9943 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9944 &secinfo->flavor_info);
9947 flavor = RPC_AUTH_MAXFLAVOR;
9951 if (!nfs_auth_info_match(&server->auth_info, flavor))
9952 flavor = RPC_AUTH_MAXFLAVOR;
9954 if (flavor != RPC_AUTH_MAXFLAVOR) {
9955 err = nfs4_lookup_root_sec(server, fhandle,
9962 if (flavor == RPC_AUTH_MAXFLAVOR)
9973 static int _nfs41_test_stateid(struct nfs_server *server,
9974 nfs4_stateid *stateid,
9975 const struct cred *cred)
9978 struct nfs41_test_stateid_args args = {
9981 struct nfs41_test_stateid_res res;
9982 struct rpc_message msg = {
9983 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9988 struct rpc_clnt *rpc_client = server->client;
9990 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9993 dprintk("NFS call test_stateid %p\n", stateid);
9994 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
9995 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
9996 &args.seq_args, &res.seq_res);
9997 if (status != NFS_OK) {
9998 dprintk("NFS reply test_stateid: failed, %d\n", status);
10001 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
10002 return -res.status;
10005 static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
10006 int err, struct nfs4_exception *exception)
10008 exception->retry = 0;
10010 case -NFS4ERR_DELAY:
10011 case -NFS4ERR_RETRY_UNCACHED_REP:
10012 nfs4_handle_exception(server, err, exception);
10014 case -NFS4ERR_BADSESSION:
10015 case -NFS4ERR_BADSLOT:
10016 case -NFS4ERR_BAD_HIGH_SLOT:
10017 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
10018 case -NFS4ERR_DEADSESSION:
10019 nfs4_do_handle_exception(server, err, exception);
10024 * nfs41_test_stateid - perform a TEST_STATEID operation
10026 * @server: server / transport on which to perform the operation
10027 * @stateid: state ID to test
10028 * @cred: credential
10030 * Returns NFS_OK if the server recognizes that "stateid" is valid.
10031 * Otherwise a negative NFS4ERR value is returned if the operation
10032 * failed or the state ID is not currently valid.
10034 static int nfs41_test_stateid(struct nfs_server *server,
10035 nfs4_stateid *stateid,
10036 const struct cred *cred)
10038 struct nfs4_exception exception = {
10039 .interruptible = true,
10043 err = _nfs41_test_stateid(server, stateid, cred);
10044 nfs4_handle_delay_or_session_error(server, err, &exception);
10045 } while (exception.retry);
10049 struct nfs_free_stateid_data {
10050 struct nfs_server *server;
10051 struct nfs41_free_stateid_args args;
10052 struct nfs41_free_stateid_res res;
10055 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
10057 struct nfs_free_stateid_data *data = calldata;
10058 nfs4_setup_sequence(data->server->nfs_client,
10059 &data->args.seq_args,
10060 &data->res.seq_res,
10064 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
10066 struct nfs_free_stateid_data *data = calldata;
10068 nfs41_sequence_done(task, &data->res.seq_res);
10070 switch (task->tk_status) {
10071 case -NFS4ERR_DELAY:
10072 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
10073 rpc_restart_call_prepare(task);
10077 static void nfs41_free_stateid_release(void *calldata)
10082 static const struct rpc_call_ops nfs41_free_stateid_ops = {
10083 .rpc_call_prepare = nfs41_free_stateid_prepare,
10084 .rpc_call_done = nfs41_free_stateid_done,
10085 .rpc_release = nfs41_free_stateid_release,
10089 * nfs41_free_stateid - perform a FREE_STATEID operation
10091 * @server: server / transport on which to perform the operation
10092 * @stateid: state ID to release
10093 * @cred: credential
10094 * @privileged: set to true if this call needs to be privileged
10096 * Note: this function is always asynchronous.
10098 static int nfs41_free_stateid(struct nfs_server *server,
10099 const nfs4_stateid *stateid,
10100 const struct cred *cred,
10103 struct rpc_message msg = {
10104 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
10107 struct rpc_task_setup task_setup = {
10108 .rpc_client = server->client,
10109 .rpc_message = &msg,
10110 .callback_ops = &nfs41_free_stateid_ops,
10111 .flags = RPC_TASK_ASYNC,
10113 struct nfs_free_stateid_data *data;
10114 struct rpc_task *task;
10116 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
10117 &task_setup.rpc_client, &msg);
10119 dprintk("NFS call free_stateid %p\n", stateid);
10120 data = kmalloc(sizeof(*data), GFP_NOFS);
10123 data->server = server;
10124 nfs4_stateid_copy(&data->args.stateid, stateid);
10126 task_setup.callback_data = data;
10128 msg.rpc_argp = &data->args;
10129 msg.rpc_resp = &data->res;
10130 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
10131 task = rpc_run_task(&task_setup);
10133 return PTR_ERR(task);
10134 rpc_put_task(task);
10139 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
10141 const struct cred *cred = lsp->ls_state->owner->so_cred;
10143 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
10144 nfs4_free_lock_state(server, lsp);
10147 static bool nfs41_match_stateid(const nfs4_stateid *s1,
10148 const nfs4_stateid *s2)
10150 if (s1->type != s2->type)
10153 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
10156 if (s1->seqid == s2->seqid)
10159 return s1->seqid == 0 || s2->seqid == 0;
10162 #endif /* CONFIG_NFS_V4_1 */
10164 static bool nfs4_match_stateid(const nfs4_stateid *s1,
10165 const nfs4_stateid *s2)
10167 return nfs4_stateid_match(s1, s2);
10171 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
10172 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
10173 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
10174 .recover_open = nfs4_open_reclaim,
10175 .recover_lock = nfs4_lock_reclaim,
10176 .establish_clid = nfs4_init_clientid,
10177 .detect_trunking = nfs40_discover_server_trunking,
10180 #if defined(CONFIG_NFS_V4_1)
10181 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
10182 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
10183 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
10184 .recover_open = nfs4_open_reclaim,
10185 .recover_lock = nfs4_lock_reclaim,
10186 .establish_clid = nfs41_init_clientid,
10187 .reclaim_complete = nfs41_proc_reclaim_complete,
10188 .detect_trunking = nfs41_discover_server_trunking,
10190 #endif /* CONFIG_NFS_V4_1 */
10192 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
10193 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
10194 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
10195 .recover_open = nfs40_open_expired,
10196 .recover_lock = nfs4_lock_expired,
10197 .establish_clid = nfs4_init_clientid,
10200 #if defined(CONFIG_NFS_V4_1)
10201 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
10202 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
10203 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
10204 .recover_open = nfs41_open_expired,
10205 .recover_lock = nfs41_lock_expired,
10206 .establish_clid = nfs41_init_clientid,
10208 #endif /* CONFIG_NFS_V4_1 */
10210 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
10211 .sched_state_renewal = nfs4_proc_async_renew,
10212 .get_state_renewal_cred = nfs4_get_renew_cred,
10213 .renew_lease = nfs4_proc_renew,
10216 #if defined(CONFIG_NFS_V4_1)
10217 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
10218 .sched_state_renewal = nfs41_proc_async_sequence,
10219 .get_state_renewal_cred = nfs4_get_machine_cred,
10220 .renew_lease = nfs4_proc_sequence,
10224 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
10225 .get_locations = _nfs40_proc_get_locations,
10226 .fsid_present = _nfs40_proc_fsid_present,
10229 #if defined(CONFIG_NFS_V4_1)
10230 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
10231 .get_locations = _nfs41_proc_get_locations,
10232 .fsid_present = _nfs41_proc_fsid_present,
10234 #endif /* CONFIG_NFS_V4_1 */
10236 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
10237 .minor_version = 0,
10238 .init_caps = NFS_CAP_READDIRPLUS
10239 | NFS_CAP_ATOMIC_OPEN
10240 | NFS_CAP_POSIX_LOCK,
10241 .init_client = nfs40_init_client,
10242 .shutdown_client = nfs40_shutdown_client,
10243 .match_stateid = nfs4_match_stateid,
10244 .find_root_sec = nfs4_find_root_sec,
10245 .free_lock_state = nfs4_release_lockowner,
10246 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
10247 .alloc_seqid = nfs_alloc_seqid,
10248 .call_sync_ops = &nfs40_call_sync_ops,
10249 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
10250 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
10251 .state_renewal_ops = &nfs40_state_renewal_ops,
10252 .mig_recovery_ops = &nfs40_mig_recovery_ops,
10255 #if defined(CONFIG_NFS_V4_1)
10256 static struct nfs_seqid *
10257 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
10262 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
10263 .minor_version = 1,
10264 .init_caps = NFS_CAP_READDIRPLUS
10265 | NFS_CAP_ATOMIC_OPEN
10266 | NFS_CAP_POSIX_LOCK
10267 | NFS_CAP_STATEID_NFSV41
10268 | NFS_CAP_ATOMIC_OPEN_V1
10270 .init_client = nfs41_init_client,
10271 .shutdown_client = nfs41_shutdown_client,
10272 .match_stateid = nfs41_match_stateid,
10273 .find_root_sec = nfs41_find_root_sec,
10274 .free_lock_state = nfs41_free_lock_state,
10275 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
10276 .alloc_seqid = nfs_alloc_no_seqid,
10277 .session_trunk = nfs4_test_session_trunk,
10278 .call_sync_ops = &nfs41_call_sync_ops,
10279 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10280 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10281 .state_renewal_ops = &nfs41_state_renewal_ops,
10282 .mig_recovery_ops = &nfs41_mig_recovery_ops,
10286 #if defined(CONFIG_NFS_V4_2)
10287 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
10288 .minor_version = 2,
10289 .init_caps = NFS_CAP_READDIRPLUS
10290 | NFS_CAP_ATOMIC_OPEN
10291 | NFS_CAP_POSIX_LOCK
10292 | NFS_CAP_STATEID_NFSV41
10293 | NFS_CAP_ATOMIC_OPEN_V1
10297 | NFS_CAP_OFFLOAD_CANCEL
10298 | NFS_CAP_COPY_NOTIFY
10299 | NFS_CAP_DEALLOCATE
10301 | NFS_CAP_LAYOUTSTATS
10303 | NFS_CAP_LAYOUTERROR
10304 | NFS_CAP_READ_PLUS,
10305 .init_client = nfs41_init_client,
10306 .shutdown_client = nfs41_shutdown_client,
10307 .match_stateid = nfs41_match_stateid,
10308 .find_root_sec = nfs41_find_root_sec,
10309 .free_lock_state = nfs41_free_lock_state,
10310 .call_sync_ops = &nfs41_call_sync_ops,
10311 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
10312 .alloc_seqid = nfs_alloc_no_seqid,
10313 .session_trunk = nfs4_test_session_trunk,
10314 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10315 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10316 .state_renewal_ops = &nfs41_state_renewal_ops,
10317 .mig_recovery_ops = &nfs41_mig_recovery_ops,
10321 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
10322 [0] = &nfs_v4_0_minor_ops,
10323 #if defined(CONFIG_NFS_V4_1)
10324 [1] = &nfs_v4_1_minor_ops,
10326 #if defined(CONFIG_NFS_V4_2)
10327 [2] = &nfs_v4_2_minor_ops,
10331 static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
10333 ssize_t error, error2, error3;
10335 error = generic_listxattr(dentry, list, size);
10343 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
10352 error3 = nfs4_listxattr_nfs4_user(d_inode(dentry), list, size);
10356 return error + error2 + error3;
10359 static const struct inode_operations nfs4_dir_inode_operations = {
10360 .create = nfs_create,
10361 .lookup = nfs_lookup,
10362 .atomic_open = nfs_atomic_open,
10364 .unlink = nfs_unlink,
10365 .symlink = nfs_symlink,
10366 .mkdir = nfs_mkdir,
10367 .rmdir = nfs_rmdir,
10368 .mknod = nfs_mknod,
10369 .rename = nfs_rename,
10370 .permission = nfs_permission,
10371 .getattr = nfs_getattr,
10372 .setattr = nfs_setattr,
10373 .listxattr = nfs4_listxattr,
10376 static const struct inode_operations nfs4_file_inode_operations = {
10377 .permission = nfs_permission,
10378 .getattr = nfs_getattr,
10379 .setattr = nfs_setattr,
10380 .listxattr = nfs4_listxattr,
10383 const struct nfs_rpc_ops nfs_v4_clientops = {
10384 .version = 4, /* protocol version */
10385 .dentry_ops = &nfs4_dentry_operations,
10386 .dir_inode_ops = &nfs4_dir_inode_operations,
10387 .file_inode_ops = &nfs4_file_inode_operations,
10388 .file_ops = &nfs4_file_operations,
10389 .getroot = nfs4_proc_get_root,
10390 .submount = nfs4_submount,
10391 .try_get_tree = nfs4_try_get_tree,
10392 .getattr = nfs4_proc_getattr,
10393 .setattr = nfs4_proc_setattr,
10394 .lookup = nfs4_proc_lookup,
10395 .lookupp = nfs4_proc_lookupp,
10396 .access = nfs4_proc_access,
10397 .readlink = nfs4_proc_readlink,
10398 .create = nfs4_proc_create,
10399 .remove = nfs4_proc_remove,
10400 .unlink_setup = nfs4_proc_unlink_setup,
10401 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
10402 .unlink_done = nfs4_proc_unlink_done,
10403 .rename_setup = nfs4_proc_rename_setup,
10404 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
10405 .rename_done = nfs4_proc_rename_done,
10406 .link = nfs4_proc_link,
10407 .symlink = nfs4_proc_symlink,
10408 .mkdir = nfs4_proc_mkdir,
10409 .rmdir = nfs4_proc_rmdir,
10410 .readdir = nfs4_proc_readdir,
10411 .mknod = nfs4_proc_mknod,
10412 .statfs = nfs4_proc_statfs,
10413 .fsinfo = nfs4_proc_fsinfo,
10414 .pathconf = nfs4_proc_pathconf,
10415 .set_capabilities = nfs4_server_capabilities,
10416 .decode_dirent = nfs4_decode_dirent,
10417 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
10418 .read_setup = nfs4_proc_read_setup,
10419 .read_done = nfs4_read_done,
10420 .write_setup = nfs4_proc_write_setup,
10421 .write_done = nfs4_write_done,
10422 .commit_setup = nfs4_proc_commit_setup,
10423 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
10424 .commit_done = nfs4_commit_done,
10425 .lock = nfs4_proc_lock,
10426 .clear_acl_cache = nfs4_zap_acl_attr,
10427 .close_context = nfs4_close_context,
10428 .open_context = nfs4_atomic_open,
10429 .have_delegation = nfs4_have_delegation,
10430 .alloc_client = nfs4_alloc_client,
10431 .init_client = nfs4_init_client,
10432 .free_client = nfs4_free_client,
10433 .create_server = nfs4_create_server,
10434 .clone_server = nfs_clone_server,
10437 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
10438 .name = XATTR_NAME_NFSV4_ACL,
10439 .list = nfs4_xattr_list_nfs4_acl,
10440 .get = nfs4_xattr_get_nfs4_acl,
10441 .set = nfs4_xattr_set_nfs4_acl,
10444 #ifdef CONFIG_NFS_V4_2
10445 static const struct xattr_handler nfs4_xattr_nfs4_user_handler = {
10446 .prefix = XATTR_USER_PREFIX,
10447 .get = nfs4_xattr_get_nfs4_user,
10448 .set = nfs4_xattr_set_nfs4_user,
10452 const struct xattr_handler *nfs4_xattr_handlers[] = {
10453 &nfs4_xattr_nfs4_acl_handler,
10454 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
10455 &nfs4_xattr_nfs4_label_handler,
10457 #ifdef CONFIG_NFS_V4_2
10458 &nfs4_xattr_nfs4_user_handler,