4 * Client-side procedure declarations for NFSv4.
6 * Copyright (c) 2002 The Regents of the University of Michigan.
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <linux/delay.h>
40 #include <linux/errno.h>
41 #include <linux/string.h>
42 #include <linux/ratelimit.h>
43 #include <linux/printk.h>
44 #include <linux/slab.h>
45 #include <linux/sunrpc/clnt.h>
46 #include <linux/nfs.h>
47 #include <linux/nfs4.h>
48 #include <linux/nfs_fs.h>
49 #include <linux/nfs_page.h>
50 #include <linux/nfs_mount.h>
51 #include <linux/namei.h>
52 #include <linux/mount.h>
53 #include <linux/module.h>
54 #include <linux/xattr.h>
55 #include <linux/utsname.h>
56 #include <linux/freezer.h>
57 #include <linux/iversion.h>
60 #include "delegation.h"
66 #include "nfs4idmap.h"
67 #include "nfs4session.h"
70 #include "nfs4trace.h"
72 #define NFSDBG_FACILITY NFSDBG_PROC
74 #define NFS4_BITMASK_SZ 3
76 #define NFS4_POLL_RETRY_MIN (HZ/10)
77 #define NFS4_POLL_RETRY_MAX (15*HZ)
79 /* file attributes which can be mapped to nfs attributes */
80 #define NFS4_VALID_ATTRS (ATTR_MODE \
91 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
92 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
93 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
94 static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label, struct inode *inode);
95 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label, struct inode *inode);
96 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
97 struct nfs_fattr *fattr, struct iattr *sattr,
98 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
99 struct nfs4_label *olabel);
100 #ifdef CONFIG_NFS_V4_1
101 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
102 struct rpc_cred *cred,
103 struct nfs4_slot *slot,
105 static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
107 static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
108 struct rpc_cred *, bool);
111 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
112 static inline struct nfs4_label *
113 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
114 struct iattr *sattr, struct nfs4_label *label)
121 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
124 err = security_dentry_init_security(dentry, sattr->ia_mode,
125 &dentry->d_name, (void **)&label->label, &label->len);
132 nfs4_label_release_security(struct nfs4_label *label)
135 security_release_secctx(label->label, label->len);
137 static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
140 return server->attr_bitmask;
142 return server->attr_bitmask_nl;
145 static inline struct nfs4_label *
146 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
147 struct iattr *sattr, struct nfs4_label *l)
150 nfs4_label_release_security(struct nfs4_label *label)
153 nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
154 { return server->attr_bitmask; }
157 /* Prevent leaks of NFSv4 errors into userland */
158 static int nfs4_map_errors(int err)
163 case -NFS4ERR_RESOURCE:
164 case -NFS4ERR_LAYOUTTRYLATER:
165 case -NFS4ERR_RECALLCONFLICT:
167 case -NFS4ERR_WRONGSEC:
168 case -NFS4ERR_WRONG_CRED:
170 case -NFS4ERR_BADOWNER:
171 case -NFS4ERR_BADNAME:
173 case -NFS4ERR_SHARE_DENIED:
175 case -NFS4ERR_MINOR_VERS_MISMATCH:
176 return -EPROTONOSUPPORT;
177 case -NFS4ERR_FILE_OPEN:
180 dprintk("%s could not handle NFSv4 error %d\n",
188 * This is our standard bitmap for GETATTR requests.
190 const u32 nfs4_fattr_bitmap[3] = {
192 | FATTR4_WORD0_CHANGE
195 | FATTR4_WORD0_FILEID,
197 | FATTR4_WORD1_NUMLINKS
199 | FATTR4_WORD1_OWNER_GROUP
200 | FATTR4_WORD1_RAWDEV
201 | FATTR4_WORD1_SPACE_USED
202 | FATTR4_WORD1_TIME_ACCESS
203 | FATTR4_WORD1_TIME_METADATA
204 | FATTR4_WORD1_TIME_MODIFY
205 | FATTR4_WORD1_MOUNTED_ON_FILEID,
206 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
207 FATTR4_WORD2_SECURITY_LABEL
211 static const u32 nfs4_pnfs_open_bitmap[3] = {
213 | FATTR4_WORD0_CHANGE
216 | FATTR4_WORD0_FILEID,
218 | FATTR4_WORD1_NUMLINKS
220 | FATTR4_WORD1_OWNER_GROUP
221 | FATTR4_WORD1_RAWDEV
222 | FATTR4_WORD1_SPACE_USED
223 | FATTR4_WORD1_TIME_ACCESS
224 | FATTR4_WORD1_TIME_METADATA
225 | FATTR4_WORD1_TIME_MODIFY,
226 FATTR4_WORD2_MDSTHRESHOLD
227 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
228 | FATTR4_WORD2_SECURITY_LABEL
232 static const u32 nfs4_open_noattr_bitmap[3] = {
234 | FATTR4_WORD0_FILEID,
237 const u32 nfs4_statfs_bitmap[3] = {
238 FATTR4_WORD0_FILES_AVAIL
239 | FATTR4_WORD0_FILES_FREE
240 | FATTR4_WORD0_FILES_TOTAL,
241 FATTR4_WORD1_SPACE_AVAIL
242 | FATTR4_WORD1_SPACE_FREE
243 | FATTR4_WORD1_SPACE_TOTAL
246 const u32 nfs4_pathconf_bitmap[3] = {
248 | FATTR4_WORD0_MAXNAME,
252 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
253 | FATTR4_WORD0_MAXREAD
254 | FATTR4_WORD0_MAXWRITE
255 | FATTR4_WORD0_LEASE_TIME,
256 FATTR4_WORD1_TIME_DELTA
257 | FATTR4_WORD1_FS_LAYOUT_TYPES,
258 FATTR4_WORD2_LAYOUT_BLKSIZE
259 | FATTR4_WORD2_CLONE_BLKSIZE
262 const u32 nfs4_fs_locations_bitmap[3] = {
266 | FATTR4_WORD0_FILEID
267 | FATTR4_WORD0_FS_LOCATIONS,
269 | FATTR4_WORD1_OWNER_GROUP
270 | FATTR4_WORD1_RAWDEV
271 | FATTR4_WORD1_SPACE_USED
272 | FATTR4_WORD1_TIME_ACCESS
273 | FATTR4_WORD1_TIME_METADATA
274 | FATTR4_WORD1_TIME_MODIFY
275 | FATTR4_WORD1_MOUNTED_ON_FILEID,
278 static void nfs4_bitmap_copy_adjust(__u32 *dst, const __u32 *src,
281 unsigned long cache_validity;
283 memcpy(dst, src, NFS4_BITMASK_SZ*sizeof(*dst));
284 if (!inode || !nfs4_have_delegation(inode, FMODE_READ))
287 cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
288 if (!(cache_validity & NFS_INO_REVAL_FORCED))
289 cache_validity &= ~(NFS_INO_INVALID_CHANGE
290 | NFS_INO_INVALID_SIZE);
292 if (!(cache_validity & NFS_INO_INVALID_SIZE))
293 dst[0] &= ~FATTR4_WORD0_SIZE;
295 if (!(cache_validity & NFS_INO_INVALID_CHANGE))
296 dst[0] &= ~FATTR4_WORD0_CHANGE;
299 static void nfs4_bitmap_copy_adjust_setattr(__u32 *dst,
300 const __u32 *src, struct inode *inode)
302 nfs4_bitmap_copy_adjust(dst, src, inode);
305 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
306 struct nfs4_readdir_arg *readdir)
308 unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
312 readdir->cookie = cookie;
313 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
318 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
323 * NFSv4 servers do not return entries for '.' and '..'
324 * Therefore, we fake these entries here. We let '.'
325 * have cookie 0 and '..' have cookie 1. Note that
326 * when talking to the server, we always send cookie 0
329 start = p = kmap_atomic(*readdir->pages);
332 *p++ = xdr_one; /* next */
333 *p++ = xdr_zero; /* cookie, first word */
334 *p++ = xdr_one; /* cookie, second word */
335 *p++ = xdr_one; /* entry len */
336 memcpy(p, ".\0\0\0", 4); /* entry */
338 *p++ = xdr_one; /* bitmap length */
339 *p++ = htonl(attrs); /* bitmap */
340 *p++ = htonl(12); /* attribute buffer length */
341 *p++ = htonl(NF4DIR);
342 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
345 *p++ = xdr_one; /* next */
346 *p++ = xdr_zero; /* cookie, first word */
347 *p++ = xdr_two; /* cookie, second word */
348 *p++ = xdr_two; /* entry len */
349 memcpy(p, "..\0\0", 4); /* entry */
351 *p++ = xdr_one; /* bitmap length */
352 *p++ = htonl(attrs); /* bitmap */
353 *p++ = htonl(12); /* attribute buffer length */
354 *p++ = htonl(NF4DIR);
355 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
357 readdir->pgbase = (char *)p - (char *)start;
358 readdir->count -= readdir->pgbase;
359 kunmap_atomic(start);
362 static void nfs4_test_and_free_stateid(struct nfs_server *server,
363 nfs4_stateid *stateid,
364 struct rpc_cred *cred)
366 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
368 ops->test_and_free_expired(server, stateid, cred);
371 static void __nfs4_free_revoked_stateid(struct nfs_server *server,
372 nfs4_stateid *stateid,
373 struct rpc_cred *cred)
375 stateid->type = NFS4_REVOKED_STATEID_TYPE;
376 nfs4_test_and_free_stateid(server, stateid, cred);
379 static void nfs4_free_revoked_stateid(struct nfs_server *server,
380 const nfs4_stateid *stateid,
381 struct rpc_cred *cred)
385 nfs4_stateid_copy(&tmp, stateid);
386 __nfs4_free_revoked_stateid(server, &tmp, cred);
389 static long nfs4_update_delay(long *timeout)
393 return NFS4_POLL_RETRY_MAX;
395 *timeout = NFS4_POLL_RETRY_MIN;
396 if (*timeout > NFS4_POLL_RETRY_MAX)
397 *timeout = NFS4_POLL_RETRY_MAX;
403 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
409 freezable_schedule_timeout_killable_unsafe(
410 nfs4_update_delay(timeout));
411 if (fatal_signal_pending(current))
416 /* This is the error handling routine for processes that are allowed
419 static int nfs4_do_handle_exception(struct nfs_server *server,
420 int errorcode, struct nfs4_exception *exception)
422 struct nfs_client *clp = server->nfs_client;
423 struct nfs4_state *state = exception->state;
424 const nfs4_stateid *stateid = exception->stateid;
425 struct inode *inode = exception->inode;
428 exception->delay = 0;
429 exception->recovering = 0;
430 exception->retry = 0;
432 if (stateid == NULL && state != NULL)
433 stateid = &state->stateid;
438 case -NFS4ERR_BADHANDLE:
440 if (inode != NULL && S_ISREG(inode->i_mode))
441 pnfs_destroy_layout(NFS_I(inode));
443 case -NFS4ERR_DELEG_REVOKED:
444 case -NFS4ERR_ADMIN_REVOKED:
445 case -NFS4ERR_EXPIRED:
446 case -NFS4ERR_BAD_STATEID:
447 if (inode != NULL && stateid != NULL) {
448 nfs_inode_find_state_and_recover(inode,
450 goto wait_on_recovery;
453 case -NFS4ERR_OPENMODE:
457 err = nfs_async_inode_return_delegation(inode,
460 goto wait_on_recovery;
461 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
462 exception->retry = 1;
468 ret = nfs4_schedule_stateid_recovery(server, state);
471 goto wait_on_recovery;
472 case -NFS4ERR_STALE_STATEID:
473 case -NFS4ERR_STALE_CLIENTID:
474 nfs4_schedule_lease_recovery(clp);
475 goto wait_on_recovery;
477 ret = nfs4_schedule_migration_recovery(server);
480 goto wait_on_recovery;
481 case -NFS4ERR_LEASE_MOVED:
482 nfs4_schedule_lease_moved_recovery(clp);
483 goto wait_on_recovery;
484 #if defined(CONFIG_NFS_V4_1)
485 case -NFS4ERR_BADSESSION:
486 case -NFS4ERR_BADSLOT:
487 case -NFS4ERR_BAD_HIGH_SLOT:
488 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
489 case -NFS4ERR_DEADSESSION:
490 case -NFS4ERR_SEQ_FALSE_RETRY:
491 case -NFS4ERR_SEQ_MISORDERED:
492 dprintk("%s ERROR: %d Reset session\n", __func__,
494 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
495 goto wait_on_recovery;
496 #endif /* defined(CONFIG_NFS_V4_1) */
497 case -NFS4ERR_FILE_OPEN:
498 if (exception->timeout > HZ) {
499 /* We have retried a decent amount, time to
507 nfs_inc_server_stats(server, NFSIOS_DELAY);
510 case -NFS4ERR_LAYOUTTRYLATER:
511 case -NFS4ERR_RECALLCONFLICT:
512 exception->delay = 1;
515 case -NFS4ERR_RETRY_UNCACHED_REP:
516 case -NFS4ERR_OLD_STATEID:
517 exception->retry = 1;
519 case -NFS4ERR_BADOWNER:
520 /* The following works around a Linux server bug! */
521 case -NFS4ERR_BADNAME:
522 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
523 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
524 exception->retry = 1;
525 printk(KERN_WARNING "NFS: v4 server %s "
526 "does not accept raw "
528 "Reenabling the idmapper.\n",
529 server->nfs_client->cl_hostname);
532 /* We failed to handle the error */
533 return nfs4_map_errors(ret);
535 exception->recovering = 1;
539 /* This is the error handling routine for processes that are allowed
542 int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
544 struct nfs_client *clp = server->nfs_client;
547 ret = nfs4_do_handle_exception(server, errorcode, exception);
548 if (exception->delay) {
549 ret = nfs4_delay(server->client, &exception->timeout);
552 if (exception->recovering) {
553 ret = nfs4_wait_clnt_recover(clp);
554 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
561 exception->retry = 1;
566 nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
567 int errorcode, struct nfs4_exception *exception)
569 struct nfs_client *clp = server->nfs_client;
572 ret = nfs4_do_handle_exception(server, errorcode, exception);
573 if (exception->delay) {
574 rpc_delay(task, nfs4_update_delay(&exception->timeout));
577 if (exception->recovering) {
578 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
579 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
580 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
583 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
588 exception->retry = 1;
590 * For NFS4ERR_MOVED, the client transport will need to
591 * be recomputed after migration recovery has completed.
593 if (errorcode == -NFS4ERR_MOVED)
594 rpc_task_release_transport(task);
600 nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
601 struct nfs4_state *state, long *timeout)
603 struct nfs4_exception exception = {
607 if (task->tk_status >= 0)
610 exception.timeout = *timeout;
611 task->tk_status = nfs4_async_handle_exception(task, server,
614 if (exception.delay && timeout)
615 *timeout = exception.timeout;
622 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
623 * or 'false' otherwise.
625 static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
627 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
628 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
631 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
633 spin_lock(&clp->cl_lock);
634 if (time_before(clp->cl_last_renewal,timestamp))
635 clp->cl_last_renewal = timestamp;
636 spin_unlock(&clp->cl_lock);
639 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
641 struct nfs_client *clp = server->nfs_client;
643 if (!nfs4_has_session(clp))
644 do_renew_lease(clp, timestamp);
647 struct nfs4_call_sync_data {
648 const struct nfs_server *seq_server;
649 struct nfs4_sequence_args *seq_args;
650 struct nfs4_sequence_res *seq_res;
653 void nfs4_init_sequence(struct nfs4_sequence_args *args,
654 struct nfs4_sequence_res *res, int cache_reply,
657 args->sa_slot = NULL;
658 args->sa_cache_this = cache_reply;
659 args->sa_privileged = privileged;
664 static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
666 struct nfs4_slot *slot = res->sr_slot;
667 struct nfs4_slot_table *tbl;
670 spin_lock(&tbl->slot_tbl_lock);
671 if (!nfs41_wake_and_assign_slot(tbl, slot))
672 nfs4_free_slot(tbl, slot);
673 spin_unlock(&tbl->slot_tbl_lock);
678 static int nfs40_sequence_done(struct rpc_task *task,
679 struct nfs4_sequence_res *res)
681 if (res->sr_slot != NULL)
682 nfs40_sequence_free_slot(res);
686 #if defined(CONFIG_NFS_V4_1)
688 static void nfs41_release_slot(struct nfs4_slot *slot)
690 struct nfs4_session *session;
691 struct nfs4_slot_table *tbl;
692 bool send_new_highest_used_slotid = false;
697 session = tbl->session;
699 /* Bump the slot sequence number */
704 spin_lock(&tbl->slot_tbl_lock);
705 /* Be nice to the server: try to ensure that the last transmitted
706 * value for highest_user_slotid <= target_highest_slotid
708 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
709 send_new_highest_used_slotid = true;
711 if (nfs41_wake_and_assign_slot(tbl, slot)) {
712 send_new_highest_used_slotid = false;
715 nfs4_free_slot(tbl, slot);
717 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
718 send_new_highest_used_slotid = false;
720 spin_unlock(&tbl->slot_tbl_lock);
721 if (send_new_highest_used_slotid)
722 nfs41_notify_server(session->clp);
723 if (waitqueue_active(&tbl->slot_waitq))
724 wake_up_all(&tbl->slot_waitq);
727 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
729 nfs41_release_slot(res->sr_slot);
733 static int nfs41_sequence_process(struct rpc_task *task,
734 struct nfs4_sequence_res *res)
736 struct nfs4_session *session;
737 struct nfs4_slot *slot = res->sr_slot;
738 struct nfs_client *clp;
739 bool interrupted = false;
744 /* don't increment the sequence number if the task wasn't sent */
745 if (!RPC_WAS_SENT(task))
748 session = slot->table->session;
750 if (slot->interrupted) {
751 if (res->sr_status != -NFS4ERR_DELAY)
752 slot->interrupted = 0;
756 trace_nfs4_sequence_done(session, res);
757 /* Check the SEQUENCE operation status */
758 switch (res->sr_status) {
760 /* Update the slot's sequence and clientid lease timer */
763 do_renew_lease(clp, res->sr_timestamp);
764 /* Check sequence flags */
765 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
767 nfs41_update_target_slotid(slot->table, slot, res);
771 * sr_status remains 1 if an RPC level error occurred.
772 * The server may or may not have processed the sequence
774 * Mark the slot as having hosted an interrupted RPC call.
776 slot->interrupted = 1;
779 /* The server detected a resend of the RPC call and
780 * returned NFS4ERR_DELAY as per Section 2.10.6.2
783 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
788 case -NFS4ERR_RETRY_UNCACHED_REP:
789 case -NFS4ERR_SEQ_FALSE_RETRY:
791 * The server thinks we tried to replay a request.
792 * Retry the call after bumping the sequence ID.
795 case -NFS4ERR_BADSLOT:
797 * The slot id we used was probably retired. Try again
798 * using a different slot id.
800 if (slot->slot_nr < slot->table->target_highest_slotid)
801 goto session_recover;
803 case -NFS4ERR_SEQ_MISORDERED:
805 * Was the last operation on this sequence interrupted?
806 * If so, retry after bumping the sequence number.
811 * Could this slot have been previously retired?
812 * If so, then the server may be expecting seq_nr = 1!
814 if (slot->seq_nr != 1) {
818 goto session_recover;
820 /* Just update the slot sequence no. */
824 /* The session may be reset by one of the error handlers. */
825 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
829 nfs4_schedule_session_recovery(session, res->sr_status);
834 if (rpc_restart_call_prepare(task)) {
835 nfs41_sequence_free_slot(res);
841 if (!rpc_restart_call(task))
843 rpc_delay(task, NFS4_POLL_RETRY_MAX);
847 int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
849 if (!nfs41_sequence_process(task, res))
851 if (res->sr_slot != NULL)
852 nfs41_sequence_free_slot(res);
856 EXPORT_SYMBOL_GPL(nfs41_sequence_done);
858 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
860 if (res->sr_slot == NULL)
862 if (res->sr_slot->table->session != NULL)
863 return nfs41_sequence_process(task, res);
864 return nfs40_sequence_done(task, res);
867 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
869 if (res->sr_slot != NULL) {
870 if (res->sr_slot->table->session != NULL)
871 nfs41_sequence_free_slot(res);
873 nfs40_sequence_free_slot(res);
877 int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
879 if (res->sr_slot == NULL)
881 if (!res->sr_slot->table->session)
882 return nfs40_sequence_done(task, res);
883 return nfs41_sequence_done(task, res);
885 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
887 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
889 struct nfs4_call_sync_data *data = calldata;
891 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
893 nfs4_setup_sequence(data->seq_server->nfs_client,
894 data->seq_args, data->seq_res, task);
897 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
899 struct nfs4_call_sync_data *data = calldata;
901 nfs41_sequence_done(task, data->seq_res);
904 static const struct rpc_call_ops nfs41_call_sync_ops = {
905 .rpc_call_prepare = nfs41_call_sync_prepare,
906 .rpc_call_done = nfs41_call_sync_done,
910 nfs4_sequence_process_interrupted(struct nfs_client *client,
911 struct nfs4_slot *slot, struct rpc_cred *cred)
913 struct rpc_task *task;
915 task = _nfs41_proc_sequence(client, cred, slot, true);
917 rpc_put_task_async(task);
920 #else /* !CONFIG_NFS_V4_1 */
922 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
924 return nfs40_sequence_done(task, res);
927 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
929 if (res->sr_slot != NULL)
930 nfs40_sequence_free_slot(res);
933 int nfs4_sequence_done(struct rpc_task *task,
934 struct nfs4_sequence_res *res)
936 return nfs40_sequence_done(task, res);
938 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
941 nfs4_sequence_process_interrupted(struct nfs_client *client,
942 struct nfs4_slot *slot, struct rpc_cred *cred)
945 slot->interrupted = 0;
948 #endif /* !CONFIG_NFS_V4_1 */
951 void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
952 struct nfs4_sequence_res *res,
953 struct nfs4_slot *slot)
957 slot->privileged = args->sa_privileged ? 1 : 0;
958 args->sa_slot = slot;
961 res->sr_timestamp = jiffies;
962 res->sr_status_flags = 0;
967 int nfs4_setup_sequence(struct nfs_client *client,
968 struct nfs4_sequence_args *args,
969 struct nfs4_sequence_res *res,
970 struct rpc_task *task)
972 struct nfs4_session *session = nfs4_get_session(client);
973 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
974 struct nfs4_slot *slot;
976 /* slot already allocated? */
977 if (res->sr_slot != NULL)
981 tbl = &session->fc_slot_table;
982 task->tk_timeout = 0;
986 spin_lock(&tbl->slot_tbl_lock);
987 /* The state manager will wait until the slot table is empty */
988 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
991 slot = nfs4_alloc_slot(tbl);
993 /* Try again in 1/4 second */
994 if (slot == ERR_PTR(-ENOMEM))
995 task->tk_timeout = HZ >> 2;
998 spin_unlock(&tbl->slot_tbl_lock);
1000 if (likely(!slot->interrupted))
1002 nfs4_sequence_process_interrupted(client,
1003 slot, task->tk_msg.rpc_cred);
1006 nfs4_sequence_attach_slot(args, res, slot);
1008 trace_nfs4_setup_sequence(session, args);
1010 rpc_call_start(task);
1014 if (args->sa_privileged)
1015 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
1016 NULL, RPC_PRIORITY_PRIVILEGED);
1018 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
1019 spin_unlock(&tbl->slot_tbl_lock);
1022 EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
1024 static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1026 struct nfs4_call_sync_data *data = calldata;
1027 nfs4_setup_sequence(data->seq_server->nfs_client,
1028 data->seq_args, data->seq_res, task);
1031 static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1033 struct nfs4_call_sync_data *data = calldata;
1034 nfs4_sequence_done(task, data->seq_res);
1037 static const struct rpc_call_ops nfs40_call_sync_ops = {
1038 .rpc_call_prepare = nfs40_call_sync_prepare,
1039 .rpc_call_done = nfs40_call_sync_done,
1042 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
1043 struct nfs_server *server,
1044 struct rpc_message *msg,
1045 struct nfs4_sequence_args *args,
1046 struct nfs4_sequence_res *res)
1049 struct rpc_task *task;
1050 struct nfs_client *clp = server->nfs_client;
1051 struct nfs4_call_sync_data data = {
1052 .seq_server = server,
1056 struct rpc_task_setup task_setup = {
1059 .callback_ops = clp->cl_mvops->call_sync_ops,
1060 .callback_data = &data
1063 task = rpc_run_task(&task_setup);
1065 ret = PTR_ERR(task);
1067 ret = task->tk_status;
1073 int nfs4_call_sync(struct rpc_clnt *clnt,
1074 struct nfs_server *server,
1075 struct rpc_message *msg,
1076 struct nfs4_sequence_args *args,
1077 struct nfs4_sequence_res *res,
1080 nfs4_init_sequence(args, res, cache_reply, 0);
1081 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
1085 nfs4_inc_nlink_locked(struct inode *inode)
1087 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1092 nfs4_dec_nlink_locked(struct inode *inode)
1094 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1099 update_changeattr_locked(struct inode *dir, struct nfs4_change_info *cinfo,
1100 unsigned long timestamp, unsigned long cache_validity)
1102 struct nfs_inode *nfsi = NFS_I(dir);
1104 nfsi->cache_validity |= NFS_INO_INVALID_CTIME
1105 | NFS_INO_INVALID_MTIME
1106 | NFS_INO_INVALID_DATA
1108 if (cinfo->atomic && cinfo->before == inode_peek_iversion_raw(dir)) {
1109 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1110 nfsi->attrtimeo_timestamp = jiffies;
1112 nfs_force_lookup_revalidate(dir);
1113 if (cinfo->before != inode_peek_iversion_raw(dir))
1114 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1115 NFS_INO_INVALID_ACL;
1117 inode_set_iversion_raw(dir, cinfo->after);
1118 nfsi->read_cache_jiffies = timestamp;
1119 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
1120 nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE;
1121 nfs_fscache_invalidate(dir);
1125 update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1126 unsigned long timestamp, unsigned long cache_validity)
1128 spin_lock(&dir->i_lock);
1129 update_changeattr_locked(dir, cinfo, timestamp, cache_validity);
1130 spin_unlock(&dir->i_lock);
1133 struct nfs4_open_createattrs {
1134 struct nfs4_label *label;
1135 struct iattr *sattr;
1136 const __u32 verf[2];
1139 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1140 int err, struct nfs4_exception *exception)
1144 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1146 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1147 exception->retry = 1;
1152 nfs4_map_atomic_open_share(struct nfs_server *server,
1153 fmode_t fmode, int openflags)
1157 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1159 res = NFS4_SHARE_ACCESS_READ;
1162 res = NFS4_SHARE_ACCESS_WRITE;
1164 case FMODE_READ|FMODE_WRITE:
1165 res = NFS4_SHARE_ACCESS_BOTH;
1167 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1169 /* Want no delegation if we're using O_DIRECT */
1170 if (openflags & O_DIRECT)
1171 res |= NFS4_SHARE_WANT_NO_DELEG;
1176 static enum open_claim_type4
1177 nfs4_map_atomic_open_claim(struct nfs_server *server,
1178 enum open_claim_type4 claim)
1180 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1185 case NFS4_OPEN_CLAIM_FH:
1186 return NFS4_OPEN_CLAIM_NULL;
1187 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1188 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1189 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1190 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1194 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
1196 p->o_res.f_attr = &p->f_attr;
1197 p->o_res.f_label = p->f_label;
1198 p->o_res.seqid = p->o_arg.seqid;
1199 p->c_res.seqid = p->c_arg.seqid;
1200 p->o_res.server = p->o_arg.server;
1201 p->o_res.access_request = p->o_arg.access;
1202 nfs_fattr_init(&p->f_attr);
1203 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
1206 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
1207 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
1208 const struct nfs4_open_createattrs *c,
1209 enum open_claim_type4 claim,
1212 struct dentry *parent = dget_parent(dentry);
1213 struct inode *dir = d_inode(parent);
1214 struct nfs_server *server = NFS_SERVER(dir);
1215 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
1216 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
1217 struct nfs4_opendata *p;
1219 p = kzalloc(sizeof(*p), gfp_mask);
1223 p->f_label = nfs4_label_alloc(server, gfp_mask);
1224 if (IS_ERR(p->f_label))
1227 p->a_label = nfs4_label_alloc(server, gfp_mask);
1228 if (IS_ERR(p->a_label))
1231 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1232 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
1233 if (IS_ERR(p->o_arg.seqid))
1234 goto err_free_label;
1235 nfs_sb_active(dentry->d_sb);
1236 p->dentry = dget(dentry);
1239 atomic_inc(&sp->so_count);
1240 p->o_arg.open_flags = flags;
1241 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
1242 p->o_arg.umask = current_umask();
1243 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1244 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1246 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1247 * will return permission denied for all bits until close */
1248 if (!(flags & O_EXCL)) {
1249 /* ask server to check for all possible rights as results
1251 switch (p->o_arg.claim) {
1254 case NFS4_OPEN_CLAIM_NULL:
1255 case NFS4_OPEN_CLAIM_FH:
1256 p->o_arg.access = NFS4_ACCESS_READ |
1257 NFS4_ACCESS_MODIFY |
1258 NFS4_ACCESS_EXTEND |
1259 NFS4_ACCESS_EXECUTE;
1262 p->o_arg.clientid = server->nfs_client->cl_clientid;
1263 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1264 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1265 p->o_arg.name = &dentry->d_name;
1266 p->o_arg.server = server;
1267 p->o_arg.bitmask = nfs4_bitmask(server, label);
1268 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1269 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1270 switch (p->o_arg.claim) {
1271 case NFS4_OPEN_CLAIM_NULL:
1272 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1273 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1274 p->o_arg.fh = NFS_FH(dir);
1276 case NFS4_OPEN_CLAIM_PREVIOUS:
1277 case NFS4_OPEN_CLAIM_FH:
1278 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1279 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1280 p->o_arg.fh = NFS_FH(d_inode(dentry));
1282 if (c != NULL && c->sattr != NULL && c->sattr->ia_valid != 0) {
1283 p->o_arg.u.attrs = &p->attrs;
1284 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
1286 memcpy(p->o_arg.u.verifier.data, c->verf,
1287 sizeof(p->o_arg.u.verifier.data));
1289 p->c_arg.fh = &p->o_res.fh;
1290 p->c_arg.stateid = &p->o_res.stateid;
1291 p->c_arg.seqid = p->o_arg.seqid;
1292 nfs4_init_opendata_res(p);
1293 kref_init(&p->kref);
1297 nfs4_label_free(p->a_label);
1299 nfs4_label_free(p->f_label);
1307 static void nfs4_opendata_free(struct kref *kref)
1309 struct nfs4_opendata *p = container_of(kref,
1310 struct nfs4_opendata, kref);
1311 struct super_block *sb = p->dentry->d_sb;
1313 nfs4_lgopen_release(p->lgp);
1314 nfs_free_seqid(p->o_arg.seqid);
1315 nfs4_sequence_free_slot(&p->o_res.seq_res);
1316 if (p->state != NULL)
1317 nfs4_put_open_state(p->state);
1318 nfs4_put_state_owner(p->owner);
1320 nfs4_label_free(p->a_label);
1321 nfs4_label_free(p->f_label);
1325 nfs_sb_deactive(sb);
1326 nfs_fattr_free_names(&p->f_attr);
1327 kfree(p->f_attr.mdsthreshold);
1331 static void nfs4_opendata_put(struct nfs4_opendata *p)
1334 kref_put(&p->kref, nfs4_opendata_free);
1337 static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1340 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1341 case FMODE_READ|FMODE_WRITE:
1342 return state->n_rdwr != 0;
1344 return state->n_wronly != 0;
1346 return state->n_rdonly != 0;
1352 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
1356 if (open_mode & (O_EXCL|O_TRUNC))
1358 switch (mode & (FMODE_READ|FMODE_WRITE)) {
1360 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1361 && state->n_rdonly != 0;
1364 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1365 && state->n_wronly != 0;
1367 case FMODE_READ|FMODE_WRITE:
1368 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1369 && state->n_rdwr != 0;
1375 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1376 enum open_claim_type4 claim)
1378 if (delegation == NULL)
1380 if ((delegation->type & fmode) != fmode)
1382 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1385 case NFS4_OPEN_CLAIM_NULL:
1386 case NFS4_OPEN_CLAIM_FH:
1388 case NFS4_OPEN_CLAIM_PREVIOUS:
1389 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1395 nfs_mark_delegation_referenced(delegation);
1399 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1408 case FMODE_READ|FMODE_WRITE:
1411 nfs4_state_set_mode_locked(state, state->state | fmode);
1414 #ifdef CONFIG_NFS_V4_1
1415 static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1417 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1419 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1421 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1425 #endif /* CONFIG_NFS_V4_1 */
1427 static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1429 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1430 wake_up_all(&state->waitq);
1433 static void nfs_state_log_out_of_order_open_stateid(struct nfs4_state *state,
1434 const nfs4_stateid *stateid)
1436 u32 state_seqid = be32_to_cpu(state->open_stateid.seqid);
1437 u32 stateid_seqid = be32_to_cpu(stateid->seqid);
1439 if (stateid_seqid == state_seqid + 1U ||
1440 (stateid_seqid == 1U && state_seqid == 0xffffffffU))
1441 nfs_state_log_update_open_stateid(state);
1443 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1446 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
1448 struct nfs_client *clp = state->owner->so_server->nfs_client;
1449 bool need_recover = false;
1451 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1452 need_recover = true;
1453 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1454 need_recover = true;
1455 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1456 need_recover = true;
1458 nfs4_state_mark_reclaim_nograce(clp, state);
1462 * Check for whether or not the caller may update the open stateid
1463 * to the value passed in by stateid.
1465 * Note: This function relies heavily on the server implementing
1466 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1468 * i.e. The stateid seqids have to be initialised to 1, and
1469 * are then incremented on every state transition.
1471 static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1472 const nfs4_stateid *stateid)
1474 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0 ||
1475 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1476 if (stateid->seqid == cpu_to_be32(1))
1477 nfs_state_log_update_open_stateid(state);
1479 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1483 if (nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1484 nfs_state_log_out_of_order_open_stateid(state, stateid);
1490 static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1492 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1494 if (state->n_wronly)
1495 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1496 if (state->n_rdonly)
1497 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1499 set_bit(NFS_O_RDWR_STATE, &state->flags);
1500 set_bit(NFS_OPEN_STATE, &state->flags);
1503 static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1504 nfs4_stateid *stateid, fmode_t fmode)
1506 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1507 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1509 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1512 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1515 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1516 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1517 clear_bit(NFS_OPEN_STATE, &state->flags);
1519 if (stateid == NULL)
1521 /* Handle OPEN+OPEN_DOWNGRADE races */
1522 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1523 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1524 nfs_resync_open_stateid_locked(state);
1527 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1528 nfs4_stateid_copy(&state->stateid, stateid);
1529 nfs4_stateid_copy(&state->open_stateid, stateid);
1530 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
1532 nfs_state_log_update_open_stateid(state);
1535 static void nfs_clear_open_stateid(struct nfs4_state *state,
1536 nfs4_stateid *arg_stateid,
1537 nfs4_stateid *stateid, fmode_t fmode)
1539 write_seqlock(&state->seqlock);
1540 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1541 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1542 nfs_clear_open_stateid_locked(state, stateid, fmode);
1543 write_sequnlock(&state->seqlock);
1544 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1545 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1548 static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1549 const nfs4_stateid *stateid, nfs4_stateid *freeme)
1555 if (!nfs_need_update_open_stateid(state, stateid))
1557 if (!test_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1561 /* Rely on seqids for serialisation with NFSv4.0 */
1562 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1565 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1567 * Ensure we process the state changes in the same order
1568 * in which the server processed them by delaying the
1569 * update of the stateid until we are in sequence.
1571 write_sequnlock(&state->seqlock);
1572 spin_unlock(&state->owner->so_lock);
1574 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
1575 if (!signal_pending(current)) {
1576 if (schedule_timeout(5*HZ) == 0)
1582 finish_wait(&state->waitq, &wait);
1584 spin_lock(&state->owner->so_lock);
1585 write_seqlock(&state->seqlock);
1588 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1589 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1590 nfs4_stateid_copy(freeme, &state->open_stateid);
1591 nfs_test_and_clear_all_open_stateid(state);
1594 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1595 nfs4_stateid_copy(&state->stateid, stateid);
1596 nfs4_stateid_copy(&state->open_stateid, stateid);
1597 trace_nfs4_open_stateid_update(state->inode, stateid, status);
1598 nfs_state_log_update_open_stateid(state);
1601 static void nfs_state_set_open_stateid(struct nfs4_state *state,
1602 const nfs4_stateid *open_stateid,
1604 nfs4_stateid *freeme)
1607 * Protect the call to nfs4_state_set_mode_locked and
1608 * serialise the stateid update
1610 write_seqlock(&state->seqlock);
1611 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1614 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1617 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1619 case FMODE_READ|FMODE_WRITE:
1620 set_bit(NFS_O_RDWR_STATE, &state->flags);
1622 set_bit(NFS_OPEN_STATE, &state->flags);
1623 write_sequnlock(&state->seqlock);
1626 static void nfs_state_set_delegation(struct nfs4_state *state,
1627 const nfs4_stateid *deleg_stateid,
1631 * Protect the call to nfs4_state_set_mode_locked and
1632 * serialise the stateid update
1634 write_seqlock(&state->seqlock);
1635 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1636 set_bit(NFS_DELEGATED_STATE, &state->flags);
1637 write_sequnlock(&state->seqlock);
1640 static int update_open_stateid(struct nfs4_state *state,
1641 const nfs4_stateid *open_stateid,
1642 const nfs4_stateid *delegation,
1645 struct nfs_server *server = NFS_SERVER(state->inode);
1646 struct nfs_client *clp = server->nfs_client;
1647 struct nfs_inode *nfsi = NFS_I(state->inode);
1648 struct nfs_delegation *deleg_cur;
1649 nfs4_stateid freeme = { };
1652 fmode &= (FMODE_READ|FMODE_WRITE);
1655 spin_lock(&state->owner->so_lock);
1656 if (open_stateid != NULL) {
1657 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1661 deleg_cur = rcu_dereference(nfsi->delegation);
1662 if (deleg_cur == NULL)
1665 spin_lock(&deleg_cur->lock);
1666 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1667 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1668 (deleg_cur->type & fmode) != fmode)
1669 goto no_delegation_unlock;
1671 if (delegation == NULL)
1672 delegation = &deleg_cur->stateid;
1673 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
1674 goto no_delegation_unlock;
1676 nfs_mark_delegation_referenced(deleg_cur);
1677 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
1679 no_delegation_unlock:
1680 spin_unlock(&deleg_cur->lock);
1683 update_open_stateflags(state, fmode);
1684 spin_unlock(&state->owner->so_lock);
1687 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1688 nfs4_schedule_state_manager(clp);
1689 if (freeme.type != 0)
1690 nfs4_test_and_free_stateid(server, &freeme,
1691 state->owner->so_cred);
1696 static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1697 const nfs4_stateid *stateid)
1699 struct nfs4_state *state = lsp->ls_state;
1702 spin_lock(&state->state_lock);
1703 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1705 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1707 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1710 spin_unlock(&state->state_lock);
1714 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1716 struct nfs_delegation *delegation;
1718 fmode &= FMODE_READ|FMODE_WRITE;
1720 delegation = rcu_dereference(NFS_I(inode)->delegation);
1721 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1726 nfs4_inode_return_delegation(inode);
1729 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1731 struct nfs4_state *state = opendata->state;
1732 struct nfs_inode *nfsi = NFS_I(state->inode);
1733 struct nfs_delegation *delegation;
1734 int open_mode = opendata->o_arg.open_flags;
1735 fmode_t fmode = opendata->o_arg.fmode;
1736 enum open_claim_type4 claim = opendata->o_arg.claim;
1737 nfs4_stateid stateid;
1741 spin_lock(&state->owner->so_lock);
1742 if (can_open_cached(state, fmode, open_mode)) {
1743 update_open_stateflags(state, fmode);
1744 spin_unlock(&state->owner->so_lock);
1745 goto out_return_state;
1747 spin_unlock(&state->owner->so_lock);
1749 delegation = rcu_dereference(nfsi->delegation);
1750 if (!can_open_delegated(delegation, fmode, claim)) {
1754 /* Save the delegation */
1755 nfs4_stateid_copy(&stateid, &delegation->stateid);
1757 nfs_release_seqid(opendata->o_arg.seqid);
1758 if (!opendata->is_recover) {
1759 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1765 /* Try to update the stateid using the delegation */
1766 if (update_open_stateid(state, NULL, &stateid, fmode))
1767 goto out_return_state;
1770 return ERR_PTR(ret);
1772 atomic_inc(&state->count);
1777 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1779 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1780 struct nfs_delegation *delegation;
1781 int delegation_flags = 0;
1784 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1786 delegation_flags = delegation->flags;
1788 switch (data->o_arg.claim) {
1791 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1792 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1793 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1794 "returning a delegation for "
1795 "OPEN(CLAIM_DELEGATE_CUR)\n",
1799 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1800 nfs_inode_set_delegation(state->inode,
1801 data->owner->so_cred,
1802 data->o_res.delegation_type,
1803 &data->o_res.delegation,
1804 data->o_res.pagemod_limit);
1806 nfs_inode_reclaim_delegation(state->inode,
1807 data->owner->so_cred,
1808 data->o_res.delegation_type,
1809 &data->o_res.delegation,
1810 data->o_res.pagemod_limit);
1812 if (data->o_res.do_recall)
1813 nfs_async_inode_return_delegation(state->inode,
1814 &data->o_res.delegation);
1818 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1819 * and update the nfs4_state.
1821 static struct nfs4_state *
1822 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1824 struct inode *inode = data->state->inode;
1825 struct nfs4_state *state = data->state;
1828 if (!data->rpc_done) {
1829 if (data->rpc_status)
1830 return ERR_PTR(data->rpc_status);
1831 /* cached opens have already been processed */
1835 ret = nfs_refresh_inode(inode, &data->f_attr);
1837 return ERR_PTR(ret);
1839 if (data->o_res.delegation_type != 0)
1840 nfs4_opendata_check_deleg(data, state);
1842 update_open_stateid(state, &data->o_res.stateid, NULL,
1844 atomic_inc(&state->count);
1849 static struct inode *
1850 nfs4_opendata_get_inode(struct nfs4_opendata *data)
1852 struct inode *inode;
1854 switch (data->o_arg.claim) {
1855 case NFS4_OPEN_CLAIM_NULL:
1856 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1857 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1858 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1859 return ERR_PTR(-EAGAIN);
1860 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1861 &data->f_attr, data->f_label);
1864 inode = d_inode(data->dentry);
1866 nfs_refresh_inode(inode, &data->f_attr);
1871 static struct nfs4_state *
1872 nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
1874 struct nfs4_state *state;
1875 struct inode *inode;
1877 inode = nfs4_opendata_get_inode(data);
1879 return ERR_CAST(inode);
1880 if (data->state != NULL && data->state->inode == inode) {
1881 state = data->state;
1882 atomic_inc(&state->count);
1884 state = nfs4_get_open_state(inode, data->owner);
1887 state = ERR_PTR(-ENOMEM);
1891 static struct nfs4_state *
1892 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1894 struct nfs4_state *state;
1896 if (!data->rpc_done) {
1897 state = nfs4_try_open_cached(data);
1898 trace_nfs4_cached_open(data->state);
1902 state = nfs4_opendata_find_nfs4_state(data);
1906 if (data->o_res.delegation_type != 0)
1907 nfs4_opendata_check_deleg(data, state);
1908 update_open_stateid(state, &data->o_res.stateid, NULL,
1911 nfs_release_seqid(data->o_arg.seqid);
1915 static struct nfs4_state *
1916 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1918 struct nfs4_state *ret;
1920 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1921 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1923 ret = _nfs4_opendata_to_nfs4_state(data);
1924 nfs4_sequence_free_slot(&data->o_res.seq_res);
1928 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1930 struct nfs_inode *nfsi = NFS_I(state->inode);
1931 struct nfs_open_context *ctx;
1933 spin_lock(&state->inode->i_lock);
1934 list_for_each_entry(ctx, &nfsi->open_files, list) {
1935 if (ctx->state != state)
1937 get_nfs_open_context(ctx);
1938 spin_unlock(&state->inode->i_lock);
1941 spin_unlock(&state->inode->i_lock);
1942 return ERR_PTR(-ENOENT);
1945 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1946 struct nfs4_state *state, enum open_claim_type4 claim)
1948 struct nfs4_opendata *opendata;
1950 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
1951 NULL, claim, GFP_NOFS);
1952 if (opendata == NULL)
1953 return ERR_PTR(-ENOMEM);
1954 opendata->state = state;
1955 atomic_inc(&state->count);
1959 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1962 struct nfs4_state *newstate;
1965 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
1967 opendata->o_arg.open_flags = 0;
1968 opendata->o_arg.fmode = fmode;
1969 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1970 NFS_SB(opendata->dentry->d_sb),
1972 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1973 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1974 nfs4_init_opendata_res(opendata);
1975 ret = _nfs4_recover_proc_open(opendata);
1978 newstate = nfs4_opendata_to_nfs4_state(opendata);
1979 if (IS_ERR(newstate))
1980 return PTR_ERR(newstate);
1981 if (newstate != opendata->state)
1983 nfs4_close_state(newstate, fmode);
1987 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1991 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1992 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1993 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1994 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1995 /* memory barrier prior to reading state->n_* */
1996 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1997 clear_bit(NFS_OPEN_STATE, &state->flags);
1999 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2002 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2005 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2009 * We may have performed cached opens for all three recoveries.
2010 * Check if we need to update the current stateid.
2012 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
2013 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
2014 write_seqlock(&state->seqlock);
2015 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2016 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2017 write_sequnlock(&state->seqlock);
2024 * reclaim state on the server after a reboot.
2026 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2028 struct nfs_delegation *delegation;
2029 struct nfs4_opendata *opendata;
2030 fmode_t delegation_type = 0;
2033 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2034 NFS4_OPEN_CLAIM_PREVIOUS);
2035 if (IS_ERR(opendata))
2036 return PTR_ERR(opendata);
2038 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2039 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
2040 delegation_type = delegation->type;
2042 opendata->o_arg.u.delegation_type = delegation_type;
2043 status = nfs4_open_recover(opendata, state);
2044 nfs4_opendata_put(opendata);
2048 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2050 struct nfs_server *server = NFS_SERVER(state->inode);
2051 struct nfs4_exception exception = { };
2054 err = _nfs4_do_open_reclaim(ctx, state);
2055 trace_nfs4_open_reclaim(ctx, 0, err);
2056 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2058 if (err != -NFS4ERR_DELAY)
2060 nfs4_handle_exception(server, err, &exception);
2061 } while (exception.retry);
2065 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2067 struct nfs_open_context *ctx;
2070 ctx = nfs4_state_find_open_context(state);
2073 ret = nfs4_do_open_reclaim(ctx, state);
2074 put_nfs_open_context(ctx);
2078 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)
2082 printk(KERN_ERR "NFS: %s: unhandled error "
2083 "%d.\n", __func__, err);
2089 case -NFS4ERR_BADSESSION:
2090 case -NFS4ERR_BADSLOT:
2091 case -NFS4ERR_BAD_HIGH_SLOT:
2092 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2093 case -NFS4ERR_DEADSESSION:
2094 set_bit(NFS_DELEGATED_STATE, &state->flags);
2095 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
2097 case -NFS4ERR_STALE_CLIENTID:
2098 case -NFS4ERR_STALE_STATEID:
2099 set_bit(NFS_DELEGATED_STATE, &state->flags);
2100 /* Don't recall a delegation if it was lost */
2101 nfs4_schedule_lease_recovery(server->nfs_client);
2103 case -NFS4ERR_MOVED:
2104 nfs4_schedule_migration_recovery(server);
2106 case -NFS4ERR_LEASE_MOVED:
2107 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2109 case -NFS4ERR_DELEG_REVOKED:
2110 case -NFS4ERR_ADMIN_REVOKED:
2111 case -NFS4ERR_EXPIRED:
2112 case -NFS4ERR_BAD_STATEID:
2113 case -NFS4ERR_OPENMODE:
2114 nfs_inode_find_state_and_recover(state->inode,
2116 nfs4_schedule_stateid_recovery(server, state);
2118 case -NFS4ERR_DELAY:
2119 case -NFS4ERR_GRACE:
2120 set_bit(NFS_DELEGATED_STATE, &state->flags);
2124 case -NFS4ERR_DENIED:
2126 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2128 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2135 int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2136 struct nfs4_state *state, const nfs4_stateid *stateid,
2139 struct nfs_server *server = NFS_SERVER(state->inode);
2140 struct nfs4_opendata *opendata;
2143 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2144 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2145 if (IS_ERR(opendata))
2146 return PTR_ERR(opendata);
2147 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
2148 write_seqlock(&state->seqlock);
2149 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2150 write_sequnlock(&state->seqlock);
2151 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2152 switch (type & (FMODE_READ|FMODE_WRITE)) {
2153 case FMODE_READ|FMODE_WRITE:
2155 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2158 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2163 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2165 nfs4_opendata_put(opendata);
2166 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
2169 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2171 struct nfs4_opendata *data = calldata;
2173 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2174 &data->c_arg.seq_args, &data->c_res.seq_res, task);
2177 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2179 struct nfs4_opendata *data = calldata;
2181 nfs40_sequence_done(task, &data->c_res.seq_res);
2183 data->rpc_status = task->tk_status;
2184 if (data->rpc_status == 0) {
2185 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
2186 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2187 renew_lease(data->o_res.server, data->timestamp);
2188 data->rpc_done = true;
2192 static void nfs4_open_confirm_release(void *calldata)
2194 struct nfs4_opendata *data = calldata;
2195 struct nfs4_state *state = NULL;
2197 /* If this request hasn't been cancelled, do nothing */
2198 if (!data->cancelled)
2200 /* In case of error, no cleanup! */
2201 if (!data->rpc_done)
2203 state = nfs4_opendata_to_nfs4_state(data);
2205 nfs4_close_state(state, data->o_arg.fmode);
2207 nfs4_opendata_put(data);
2210 static const struct rpc_call_ops nfs4_open_confirm_ops = {
2211 .rpc_call_prepare = nfs4_open_confirm_prepare,
2212 .rpc_call_done = nfs4_open_confirm_done,
2213 .rpc_release = nfs4_open_confirm_release,
2217 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2219 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2221 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
2222 struct rpc_task *task;
2223 struct rpc_message msg = {
2224 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2225 .rpc_argp = &data->c_arg,
2226 .rpc_resp = &data->c_res,
2227 .rpc_cred = data->owner->so_cred,
2229 struct rpc_task_setup task_setup_data = {
2230 .rpc_client = server->client,
2231 .rpc_message = &msg,
2232 .callback_ops = &nfs4_open_confirm_ops,
2233 .callback_data = data,
2234 .workqueue = nfsiod_workqueue,
2235 .flags = RPC_TASK_ASYNC,
2239 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2241 kref_get(&data->kref);
2242 data->rpc_done = false;
2243 data->rpc_status = 0;
2244 data->timestamp = jiffies;
2245 task = rpc_run_task(&task_setup_data);
2247 return PTR_ERR(task);
2248 status = rpc_wait_for_completion_task(task);
2250 data->cancelled = true;
2253 status = data->rpc_status;
2258 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
2260 struct nfs4_opendata *data = calldata;
2261 struct nfs4_state_owner *sp = data->owner;
2262 struct nfs_client *clp = sp->so_server->nfs_client;
2263 enum open_claim_type4 claim = data->o_arg.claim;
2265 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
2268 * Check if we still need to send an OPEN call, or if we can use
2269 * a delegation instead.
2271 if (data->state != NULL) {
2272 struct nfs_delegation *delegation;
2274 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
2277 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
2278 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
2279 goto unlock_no_action;
2282 /* Update client id. */
2283 data->o_arg.clientid = clp->cl_clientid;
2287 case NFS4_OPEN_CLAIM_PREVIOUS:
2288 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2289 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
2290 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
2292 case NFS4_OPEN_CLAIM_FH:
2293 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
2295 data->timestamp = jiffies;
2296 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
2297 &data->o_arg.seq_args,
2298 &data->o_res.seq_res,
2300 nfs_release_seqid(data->o_arg.seqid);
2302 /* Set the create mode (note dependency on the session type) */
2303 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2304 if (data->o_arg.open_flags & O_EXCL) {
2305 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2306 if (nfs4_has_persistent_session(clp))
2307 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2308 else if (clp->cl_mvops->minor_version > 0)
2309 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2313 trace_nfs4_cached_open(data->state);
2316 task->tk_action = NULL;
2318 nfs4_sequence_done(task, &data->o_res.seq_res);
2321 static void nfs4_open_done(struct rpc_task *task, void *calldata)
2323 struct nfs4_opendata *data = calldata;
2325 data->rpc_status = task->tk_status;
2327 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
2330 if (task->tk_status == 0) {
2331 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2332 switch (data->o_res.f_attr->mode & S_IFMT) {
2336 data->rpc_status = -ELOOP;
2339 data->rpc_status = -EISDIR;
2342 data->rpc_status = -ENOTDIR;
2345 renew_lease(data->o_res.server, data->timestamp);
2346 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2347 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2349 data->rpc_done = true;
2352 static void nfs4_open_release(void *calldata)
2354 struct nfs4_opendata *data = calldata;
2355 struct nfs4_state *state = NULL;
2357 /* If this request hasn't been cancelled, do nothing */
2358 if (!data->cancelled)
2360 /* In case of error, no cleanup! */
2361 if (data->rpc_status != 0 || !data->rpc_done)
2363 /* In case we need an open_confirm, no cleanup! */
2364 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2366 state = nfs4_opendata_to_nfs4_state(data);
2368 nfs4_close_state(state, data->o_arg.fmode);
2370 nfs4_opendata_put(data);
2373 static const struct rpc_call_ops nfs4_open_ops = {
2374 .rpc_call_prepare = nfs4_open_prepare,
2375 .rpc_call_done = nfs4_open_done,
2376 .rpc_release = nfs4_open_release,
2379 static int nfs4_run_open_task(struct nfs4_opendata *data,
2380 struct nfs_open_context *ctx)
2382 struct inode *dir = d_inode(data->dir);
2383 struct nfs_server *server = NFS_SERVER(dir);
2384 struct nfs_openargs *o_arg = &data->o_arg;
2385 struct nfs_openres *o_res = &data->o_res;
2386 struct rpc_task *task;
2387 struct rpc_message msg = {
2388 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
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_ops,
2397 .callback_data = data,
2398 .workqueue = nfsiod_workqueue,
2399 .flags = RPC_TASK_ASYNC,
2403 kref_get(&data->kref);
2404 data->rpc_done = false;
2405 data->rpc_status = 0;
2406 data->cancelled = false;
2407 data->is_recover = false;
2409 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
2410 data->is_recover = true;
2412 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
2413 pnfs_lgopen_prepare(data, ctx);
2415 task = rpc_run_task(&task_setup_data);
2417 return PTR_ERR(task);
2418 status = rpc_wait_for_completion_task(task);
2420 data->cancelled = true;
2423 status = data->rpc_status;
2429 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2431 struct inode *dir = d_inode(data->dir);
2432 struct nfs_openres *o_res = &data->o_res;
2435 status = nfs4_run_open_task(data, NULL);
2436 if (status != 0 || !data->rpc_done)
2439 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2441 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
2442 status = _nfs4_proc_open_confirm(data);
2448 * Additional permission checks in order to distinguish between an
2449 * open for read, and an open for execute. This works around the
2450 * fact that NFSv4 OPEN treats read and execute permissions as being
2452 * Note that in the non-execute case, we want to turn off permission
2453 * checking if we just created a new file (POSIX open() semantics).
2455 static int nfs4_opendata_access(struct rpc_cred *cred,
2456 struct nfs4_opendata *opendata,
2457 struct nfs4_state *state, fmode_t fmode,
2460 struct nfs_access_entry cache;
2463 /* access call failed or for some reason the server doesn't
2464 * support any access modes -- defer access call until later */
2465 if (opendata->o_res.access_supported == 0)
2470 * Use openflags to check for exec, because fmode won't
2471 * always have FMODE_EXEC set when file open for exec.
2473 if (openflags & __FMODE_EXEC) {
2474 /* ONLY check for exec rights */
2475 if (S_ISDIR(state->inode->i_mode))
2476 mask = NFS4_ACCESS_LOOKUP;
2478 mask = NFS4_ACCESS_EXECUTE;
2479 } else if ((fmode & FMODE_READ) && !opendata->file_created)
2480 mask = NFS4_ACCESS_READ;
2483 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2484 nfs_access_add_cache(state->inode, &cache);
2486 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2487 if ((mask & ~cache.mask & flags) == 0)
2494 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2496 static int _nfs4_proc_open(struct nfs4_opendata *data,
2497 struct nfs_open_context *ctx)
2499 struct inode *dir = d_inode(data->dir);
2500 struct nfs_server *server = NFS_SERVER(dir);
2501 struct nfs_openargs *o_arg = &data->o_arg;
2502 struct nfs_openres *o_res = &data->o_res;
2505 status = nfs4_run_open_task(data, ctx);
2506 if (!data->rpc_done)
2509 if (status == -NFS4ERR_BADNAME &&
2510 !(o_arg->open_flags & O_CREAT))
2515 nfs_fattr_map_and_free_names(server, &data->f_attr);
2517 if (o_arg->open_flags & O_CREAT) {
2518 if (o_arg->open_flags & O_EXCL)
2519 data->file_created = true;
2520 else if (o_res->cinfo.before != o_res->cinfo.after)
2521 data->file_created = true;
2522 if (data->file_created ||
2523 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
2524 update_changeattr(dir, &o_res->cinfo,
2525 o_res->f_attr->time_start, 0);
2527 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2528 server->caps &= ~NFS_CAP_POSIX_LOCK;
2529 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2530 status = _nfs4_proc_open_confirm(data);
2534 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2535 nfs4_sequence_free_slot(&o_res->seq_res);
2536 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr,
2537 o_res->f_label, NULL);
2544 * reclaim state on the server after a network partition.
2545 * Assumes caller holds the appropriate lock
2547 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2549 struct nfs4_opendata *opendata;
2552 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2553 NFS4_OPEN_CLAIM_FH);
2554 if (IS_ERR(opendata))
2555 return PTR_ERR(opendata);
2556 ret = nfs4_open_recover(opendata, state);
2558 d_drop(ctx->dentry);
2559 nfs4_opendata_put(opendata);
2563 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2565 struct nfs_server *server = NFS_SERVER(state->inode);
2566 struct nfs4_exception exception = { };
2570 err = _nfs4_open_expired(ctx, state);
2571 trace_nfs4_open_expired(ctx, 0, err);
2572 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2577 case -NFS4ERR_GRACE:
2578 case -NFS4ERR_DELAY:
2579 nfs4_handle_exception(server, err, &exception);
2582 } while (exception.retry);
2587 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2589 struct nfs_open_context *ctx;
2592 ctx = nfs4_state_find_open_context(state);
2595 ret = nfs4_do_open_expired(ctx, state);
2596 put_nfs_open_context(ctx);
2600 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2601 const nfs4_stateid *stateid)
2603 nfs_remove_bad_delegation(state->inode, stateid);
2604 write_seqlock(&state->seqlock);
2605 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2606 write_sequnlock(&state->seqlock);
2607 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2610 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2612 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2613 nfs_finish_clear_delegation_stateid(state, NULL);
2616 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2618 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2619 nfs40_clear_delegation_stateid(state);
2620 return nfs4_open_expired(sp, state);
2623 static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2624 nfs4_stateid *stateid,
2625 struct rpc_cred *cred)
2627 return -NFS4ERR_BAD_STATEID;
2630 #if defined(CONFIG_NFS_V4_1)
2631 static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2632 nfs4_stateid *stateid,
2633 struct rpc_cred *cred)
2637 switch (stateid->type) {
2640 case NFS4_INVALID_STATEID_TYPE:
2641 case NFS4_SPECIAL_STATEID_TYPE:
2642 return -NFS4ERR_BAD_STATEID;
2643 case NFS4_REVOKED_STATEID_TYPE:
2647 status = nfs41_test_stateid(server, stateid, cred);
2649 case -NFS4ERR_EXPIRED:
2650 case -NFS4ERR_ADMIN_REVOKED:
2651 case -NFS4ERR_DELEG_REVOKED:
2657 /* Ack the revoked state to the server */
2658 nfs41_free_stateid(server, stateid, cred, true);
2659 return -NFS4ERR_EXPIRED;
2662 static void nfs41_check_delegation_stateid(struct nfs4_state *state)
2664 struct nfs_server *server = NFS_SERVER(state->inode);
2665 nfs4_stateid stateid;
2666 struct nfs_delegation *delegation;
2667 struct rpc_cred *cred;
2670 /* Get the delegation credential for use by test/free_stateid */
2672 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2673 if (delegation == NULL) {
2678 nfs4_stateid_copy(&stateid, &delegation->stateid);
2679 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) ||
2680 !test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2681 &delegation->flags)) {
2683 nfs_finish_clear_delegation_stateid(state, &stateid);
2687 cred = get_rpccred(delegation->cred);
2689 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
2690 trace_nfs4_test_delegation_stateid(state, NULL, status);
2691 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
2692 nfs_finish_clear_delegation_stateid(state, &stateid);
2698 * nfs41_check_expired_locks - possibly free a lock stateid
2700 * @state: NFSv4 state for an inode
2702 * Returns NFS_OK if recovery for this stateid is now finished.
2703 * Otherwise a negative NFS4ERR value is returned.
2705 static int nfs41_check_expired_locks(struct nfs4_state *state)
2707 int status, ret = NFS_OK;
2708 struct nfs4_lock_state *lsp, *prev = NULL;
2709 struct nfs_server *server = NFS_SERVER(state->inode);
2711 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2714 spin_lock(&state->state_lock);
2715 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2716 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2717 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2719 refcount_inc(&lsp->ls_count);
2720 spin_unlock(&state->state_lock);
2722 nfs4_put_lock_state(prev);
2725 status = nfs41_test_and_free_expired_stateid(server,
2728 trace_nfs4_test_lock_stateid(state, lsp, status);
2729 if (status == -NFS4ERR_EXPIRED ||
2730 status == -NFS4ERR_BAD_STATEID) {
2731 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
2732 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
2733 if (!recover_lost_locks)
2734 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2735 } else if (status != NFS_OK) {
2737 nfs4_put_lock_state(prev);
2740 spin_lock(&state->state_lock);
2743 spin_unlock(&state->state_lock);
2744 nfs4_put_lock_state(prev);
2750 * nfs41_check_open_stateid - possibly free an open stateid
2752 * @state: NFSv4 state for an inode
2754 * Returns NFS_OK if recovery for this stateid is now finished.
2755 * Otherwise a negative NFS4ERR value is returned.
2757 static int nfs41_check_open_stateid(struct nfs4_state *state)
2759 struct nfs_server *server = NFS_SERVER(state->inode);
2760 nfs4_stateid *stateid = &state->open_stateid;
2761 struct rpc_cred *cred = state->owner->so_cred;
2764 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
2765 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2766 if (nfs4_have_delegation(state->inode, state->state))
2768 return -NFS4ERR_OPENMODE;
2770 return -NFS4ERR_BAD_STATEID;
2772 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
2773 trace_nfs4_test_open_stateid(state, NULL, status);
2774 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
2775 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2776 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2777 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2778 clear_bit(NFS_OPEN_STATE, &state->flags);
2779 stateid->type = NFS4_INVALID_STATEID_TYPE;
2782 if (nfs_open_stateid_recover_openmode(state))
2783 return -NFS4ERR_OPENMODE;
2787 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2791 nfs41_check_delegation_stateid(state);
2792 status = nfs41_check_expired_locks(state);
2793 if (status != NFS_OK)
2795 status = nfs41_check_open_stateid(state);
2796 if (status != NFS_OK)
2797 status = nfs4_open_expired(sp, state);
2803 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2804 * fields corresponding to attributes that were used to store the verifier.
2805 * Make sure we clobber those fields in the later setattr call
2807 static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2808 struct iattr *sattr, struct nfs4_label **label)
2810 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2815 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
2816 attrset[i] = opendata->o_res.attrset[i];
2817 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
2818 attrset[i] &= ~bitmask[i];
2821 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
2822 sattr->ia_valid : 0;
2824 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
2825 if (sattr->ia_valid & ATTR_ATIME_SET)
2826 ret |= ATTR_ATIME_SET;
2831 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
2832 if (sattr->ia_valid & ATTR_MTIME_SET)
2833 ret |= ATTR_MTIME_SET;
2838 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
2843 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2846 struct nfs_open_context *ctx)
2848 struct nfs4_state_owner *sp = opendata->owner;
2849 struct nfs_server *server = sp->so_server;
2850 struct dentry *dentry;
2851 struct nfs4_state *state;
2855 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2857 ret = _nfs4_proc_open(opendata, ctx);
2861 state = _nfs4_opendata_to_nfs4_state(opendata);
2862 ret = PTR_ERR(state);
2866 if (server->caps & NFS_CAP_POSIX_LOCK)
2867 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2868 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2869 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
2871 dentry = opendata->dentry;
2872 if (d_really_is_negative(dentry)) {
2873 struct dentry *alias;
2875 alias = d_exact_alias(dentry, state->inode);
2877 alias = d_splice_alias(igrab(state->inode), dentry);
2878 /* d_splice_alias() can't fail here - it's a non-directory */
2881 ctx->dentry = dentry = alias;
2883 nfs_set_verifier(dentry,
2884 nfs_save_change_attribute(d_inode(opendata->dir)));
2887 /* Parse layoutget results before we check for access */
2888 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
2890 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2894 if (d_inode(dentry) == state->inode) {
2895 nfs_inode_attach_open_context(ctx);
2896 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2897 nfs4_schedule_stateid_recovery(server, state);
2901 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
2906 * Returns a referenced nfs4_state
2908 static int _nfs4_do_open(struct inode *dir,
2909 struct nfs_open_context *ctx,
2911 const struct nfs4_open_createattrs *c,
2914 struct nfs4_state_owner *sp;
2915 struct nfs4_state *state = NULL;
2916 struct nfs_server *server = NFS_SERVER(dir);
2917 struct nfs4_opendata *opendata;
2918 struct dentry *dentry = ctx->dentry;
2919 struct rpc_cred *cred = ctx->cred;
2920 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2921 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
2922 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
2923 struct iattr *sattr = c->sattr;
2924 struct nfs4_label *label = c->label;
2925 struct nfs4_label *olabel = NULL;
2928 /* Protect against reboot recovery conflicts */
2930 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2932 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2935 status = nfs4_client_recover_expired_lease(server->nfs_client);
2937 goto err_put_state_owner;
2938 if (d_really_is_positive(dentry))
2939 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
2941 if (d_really_is_positive(dentry))
2942 claim = NFS4_OPEN_CLAIM_FH;
2943 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
2944 c, claim, GFP_KERNEL);
2945 if (opendata == NULL)
2946 goto err_put_state_owner;
2949 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2950 if (IS_ERR(olabel)) {
2951 status = PTR_ERR(olabel);
2952 goto err_opendata_put;
2956 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2957 if (!opendata->f_attr.mdsthreshold) {
2958 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2959 if (!opendata->f_attr.mdsthreshold)
2960 goto err_free_label;
2962 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
2964 if (d_really_is_positive(dentry))
2965 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
2967 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
2969 goto err_free_label;
2972 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
2973 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
2974 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
2976 * send create attributes which was not set by open
2977 * with an extra setattr.
2979 if (attrs || label) {
2980 unsigned ia_old = sattr->ia_valid;
2982 sattr->ia_valid = attrs;
2983 nfs_fattr_init(opendata->o_res.f_attr);
2984 status = nfs4_do_setattr(state->inode, cred,
2985 opendata->o_res.f_attr, sattr,
2986 ctx, label, olabel);
2988 nfs_setattr_update_inode(state->inode, sattr,
2989 opendata->o_res.f_attr);
2990 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2992 sattr->ia_valid = ia_old;
2995 if (opened && opendata->file_created)
2998 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
2999 *ctx_th = opendata->f_attr.mdsthreshold;
3000 opendata->f_attr.mdsthreshold = NULL;
3003 nfs4_label_free(olabel);
3005 nfs4_opendata_put(opendata);
3006 nfs4_put_state_owner(sp);
3009 nfs4_label_free(olabel);
3011 nfs4_opendata_put(opendata);
3012 err_put_state_owner:
3013 nfs4_put_state_owner(sp);
3019 static struct nfs4_state *nfs4_do_open(struct inode *dir,
3020 struct nfs_open_context *ctx,
3022 struct iattr *sattr,
3023 struct nfs4_label *label,
3026 struct nfs_server *server = NFS_SERVER(dir);
3027 struct nfs4_exception exception = { };
3028 struct nfs4_state *res;
3029 struct nfs4_open_createattrs c = {
3033 [0] = (__u32)jiffies,
3034 [1] = (__u32)current->pid,
3040 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
3042 trace_nfs4_open_file(ctx, flags, status);
3045 /* NOTE: BAD_SEQID means the server and client disagree about the
3046 * book-keeping w.r.t. state-changing operations
3047 * (OPEN/CLOSE/LOCK/LOCKU...)
3048 * It is actually a sign of a bug on the client or on the server.
3050 * If we receive a BAD_SEQID error in the particular case of
3051 * doing an OPEN, we assume that nfs_increment_open_seqid() will
3052 * have unhashed the old state_owner for us, and that we can
3053 * therefore safely retry using a new one. We should still warn
3054 * the user though...
3056 if (status == -NFS4ERR_BAD_SEQID) {
3057 pr_warn_ratelimited("NFS: v4 server %s "
3058 " returned a bad sequence-id error!\n",
3059 NFS_SERVER(dir)->nfs_client->cl_hostname);
3060 exception.retry = 1;
3064 * BAD_STATEID on OPEN means that the server cancelled our
3065 * state before it received the OPEN_CONFIRM.
3066 * Recover by retrying the request as per the discussion
3067 * on Page 181 of RFC3530.
3069 if (status == -NFS4ERR_BAD_STATEID) {
3070 exception.retry = 1;
3073 if (status == -EAGAIN) {
3074 /* We must have found a delegation */
3075 exception.retry = 1;
3078 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3080 res = ERR_PTR(nfs4_handle_exception(server,
3081 status, &exception));
3082 } while (exception.retry);
3086 static int _nfs4_do_setattr(struct inode *inode,
3087 struct nfs_setattrargs *arg,
3088 struct nfs_setattrres *res,
3089 struct rpc_cred *cred,
3090 struct nfs_open_context *ctx)
3092 struct nfs_server *server = NFS_SERVER(inode);
3093 struct rpc_message msg = {
3094 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3099 struct rpc_cred *delegation_cred = NULL;
3100 unsigned long timestamp = jiffies;
3104 nfs_fattr_init(res->fattr);
3106 /* Servers should only apply open mode checks for file size changes */
3107 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
3111 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
3112 /* Use that stateid */
3113 } else if (ctx != NULL) {
3114 struct nfs_lock_context *l_ctx;
3115 if (!nfs4_valid_open_stateid(ctx->state))
3117 l_ctx = nfs_get_lock_context(ctx);
3119 return PTR_ERR(l_ctx);
3120 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3121 &arg->stateid, &delegation_cred);
3122 nfs_put_lock_context(l_ctx);
3127 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
3129 if (delegation_cred)
3130 msg.rpc_cred = delegation_cred;
3132 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3134 put_rpccred(delegation_cred);
3135 if (status == 0 && ctx != NULL)
3136 renew_lease(server, timestamp);
3137 trace_nfs4_setattr(inode, &arg->stateid, status);
3141 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
3142 struct nfs_fattr *fattr, struct iattr *sattr,
3143 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
3144 struct nfs4_label *olabel)
3146 struct nfs_server *server = NFS_SERVER(inode);
3147 __u32 bitmask[NFS4_BITMASK_SZ];
3148 struct nfs4_state *state = ctx ? ctx->state : NULL;
3149 struct nfs_setattrargs arg = {
3150 .fh = NFS_FH(inode),
3156 struct nfs_setattrres res = {
3161 struct nfs4_exception exception = {
3164 .stateid = &arg.stateid,
3169 nfs4_bitmap_copy_adjust_setattr(bitmask,
3170 nfs4_bitmask(server, olabel),
3173 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
3175 case -NFS4ERR_OPENMODE:
3176 if (!(sattr->ia_valid & ATTR_SIZE)) {
3177 pr_warn_once("NFSv4: server %s is incorrectly "
3178 "applying open mode checks to "
3179 "a SETATTR that is not "
3180 "changing file size.\n",
3181 server->nfs_client->cl_hostname);
3183 if (state && !(state->state & FMODE_WRITE)) {
3185 if (sattr->ia_valid & ATTR_OPEN)
3190 err = nfs4_handle_exception(server, err, &exception);
3191 } while (exception.retry);
3197 nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3199 if (inode == NULL || !nfs_have_layout(inode))
3202 return pnfs_wait_on_layoutreturn(inode, task);
3205 struct nfs4_closedata {
3206 struct inode *inode;
3207 struct nfs4_state *state;
3208 struct nfs_closeargs arg;
3209 struct nfs_closeres res;
3211 struct nfs4_layoutreturn_args arg;
3212 struct nfs4_layoutreturn_res res;
3213 struct nfs4_xdr_opaque_data ld_private;
3217 struct nfs_fattr fattr;
3218 unsigned long timestamp;
3221 static void nfs4_free_closedata(void *data)
3223 struct nfs4_closedata *calldata = data;
3224 struct nfs4_state_owner *sp = calldata->state->owner;
3225 struct super_block *sb = calldata->state->inode->i_sb;
3227 if (calldata->lr.roc)
3228 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3229 calldata->res.lr_ret);
3230 nfs4_put_open_state(calldata->state);
3231 nfs_free_seqid(calldata->arg.seqid);
3232 nfs4_put_state_owner(sp);
3233 nfs_sb_deactive(sb);
3237 static void nfs4_close_done(struct rpc_task *task, void *data)
3239 struct nfs4_closedata *calldata = data;
3240 struct nfs4_state *state = calldata->state;
3241 struct nfs_server *server = NFS_SERVER(calldata->inode);
3242 nfs4_stateid *res_stateid = NULL;
3243 struct nfs4_exception exception = {
3245 .inode = calldata->inode,
3246 .stateid = &calldata->arg.stateid,
3249 dprintk("%s: begin!\n", __func__);
3250 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3252 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
3254 /* Handle Layoutreturn errors */
3255 if (calldata->arg.lr_args && task->tk_status != 0) {
3256 switch (calldata->res.lr_ret) {
3258 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3261 calldata->arg.lr_args = NULL;
3262 calldata->res.lr_res = NULL;
3264 case -NFS4ERR_OLD_STATEID:
3265 if (nfs4_layoutreturn_refresh_stateid(&calldata->arg.lr_args->stateid,
3266 &calldata->arg.lr_args->range,
3270 case -NFS4ERR_ADMIN_REVOKED:
3271 case -NFS4ERR_DELEG_REVOKED:
3272 case -NFS4ERR_EXPIRED:
3273 case -NFS4ERR_BAD_STATEID:
3274 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3275 case -NFS4ERR_WRONG_CRED:
3276 calldata->arg.lr_args = NULL;
3277 calldata->res.lr_res = NULL;