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 #ifdef CONFIG_NFS_V4_2
76 #endif /* CONFIG_NFS_V4_2 */
78 #define NFSDBG_FACILITY NFSDBG_PROC
80 #define NFS4_BITMASK_SZ 3
82 #define NFS4_POLL_RETRY_MIN (HZ/10)
83 #define NFS4_POLL_RETRY_MAX (15*HZ)
85 /* file attributes which can be mapped to nfs attributes */
86 #define NFS4_VALID_ATTRS (ATTR_MODE \
97 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
98 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
99 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
100 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label, struct inode *inode);
101 static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
102 struct nfs_fattr *fattr, struct iattr *sattr,
103 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
104 struct nfs4_label *olabel);
105 #ifdef CONFIG_NFS_V4_1
106 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
107 const struct cred *cred,
108 struct nfs4_slot *slot,
110 static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
111 const struct cred *);
112 static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
113 const struct cred *, bool);
115 static void nfs4_bitmask_adjust(__u32 *bitmask, struct inode *inode,
116 struct nfs_server *server,
117 struct nfs4_label *label);
119 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
120 static inline struct nfs4_label *
121 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
122 struct iattr *sattr, struct nfs4_label *label)
129 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
132 err = security_dentry_init_security(dentry, sattr->ia_mode,
133 &dentry->d_name, (void **)&label->label, &label->len);
140 nfs4_label_release_security(struct nfs4_label *label)
143 security_release_secctx(label->label, label->len);
145 static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
148 return server->attr_bitmask;
150 return server->attr_bitmask_nl;
153 static inline struct nfs4_label *
154 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
155 struct iattr *sattr, struct nfs4_label *l)
158 nfs4_label_release_security(struct nfs4_label *label)
161 nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
162 { return server->attr_bitmask; }
165 /* Prevent leaks of NFSv4 errors into userland */
166 static int nfs4_map_errors(int err)
171 case -NFS4ERR_RESOURCE:
172 case -NFS4ERR_LAYOUTTRYLATER:
173 case -NFS4ERR_RECALLCONFLICT:
175 case -NFS4ERR_WRONGSEC:
176 case -NFS4ERR_WRONG_CRED:
178 case -NFS4ERR_BADOWNER:
179 case -NFS4ERR_BADNAME:
181 case -NFS4ERR_SHARE_DENIED:
183 case -NFS4ERR_MINOR_VERS_MISMATCH:
184 return -EPROTONOSUPPORT;
185 case -NFS4ERR_FILE_OPEN:
188 dprintk("%s could not handle NFSv4 error %d\n",
196 * This is our standard bitmap for GETATTR requests.
198 const u32 nfs4_fattr_bitmap[3] = {
200 | FATTR4_WORD0_CHANGE
203 | FATTR4_WORD0_FILEID,
205 | FATTR4_WORD1_NUMLINKS
207 | FATTR4_WORD1_OWNER_GROUP
208 | FATTR4_WORD1_RAWDEV
209 | FATTR4_WORD1_SPACE_USED
210 | FATTR4_WORD1_TIME_ACCESS
211 | FATTR4_WORD1_TIME_METADATA
212 | FATTR4_WORD1_TIME_MODIFY
213 | FATTR4_WORD1_MOUNTED_ON_FILEID,
214 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
215 FATTR4_WORD2_SECURITY_LABEL
219 static const u32 nfs4_pnfs_open_bitmap[3] = {
221 | FATTR4_WORD0_CHANGE
224 | FATTR4_WORD0_FILEID,
226 | FATTR4_WORD1_NUMLINKS
228 | FATTR4_WORD1_OWNER_GROUP
229 | FATTR4_WORD1_RAWDEV
230 | FATTR4_WORD1_SPACE_USED
231 | FATTR4_WORD1_TIME_ACCESS
232 | FATTR4_WORD1_TIME_METADATA
233 | FATTR4_WORD1_TIME_MODIFY,
234 FATTR4_WORD2_MDSTHRESHOLD
235 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
236 | FATTR4_WORD2_SECURITY_LABEL
240 static const u32 nfs4_open_noattr_bitmap[3] = {
242 | FATTR4_WORD0_FILEID,
245 const u32 nfs4_statfs_bitmap[3] = {
246 FATTR4_WORD0_FILES_AVAIL
247 | FATTR4_WORD0_FILES_FREE
248 | FATTR4_WORD0_FILES_TOTAL,
249 FATTR4_WORD1_SPACE_AVAIL
250 | FATTR4_WORD1_SPACE_FREE
251 | FATTR4_WORD1_SPACE_TOTAL
254 const u32 nfs4_pathconf_bitmap[3] = {
256 | FATTR4_WORD0_MAXNAME,
260 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
261 | FATTR4_WORD0_MAXREAD
262 | FATTR4_WORD0_MAXWRITE
263 | FATTR4_WORD0_LEASE_TIME,
264 FATTR4_WORD1_TIME_DELTA
265 | FATTR4_WORD1_FS_LAYOUT_TYPES,
266 FATTR4_WORD2_LAYOUT_BLKSIZE
267 | FATTR4_WORD2_CLONE_BLKSIZE
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,
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);
297 if (!(cache_validity & NFS_INO_REVAL_FORCED))
298 cache_validity &= ~(NFS_INO_INVALID_CHANGE
299 | NFS_INO_INVALID_SIZE);
301 if (!(cache_validity & NFS_INO_INVALID_SIZE))
302 dst[0] &= ~FATTR4_WORD0_SIZE;
304 if (!(cache_validity & NFS_INO_INVALID_CHANGE))
305 dst[0] &= ~FATTR4_WORD0_CHANGE;
308 static void nfs4_bitmap_copy_adjust_setattr(__u32 *dst,
309 const __u32 *src, struct inode *inode)
311 nfs4_bitmap_copy_adjust(dst, src, inode);
314 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
315 struct nfs4_readdir_arg *readdir)
317 unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
321 readdir->cookie = cookie;
322 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
327 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
332 * NFSv4 servers do not return entries for '.' and '..'
333 * Therefore, we fake these entries here. We let '.'
334 * have cookie 0 and '..' have cookie 1. Note that
335 * when talking to the server, we always send cookie 0
338 start = p = kmap_atomic(*readdir->pages);
341 *p++ = xdr_one; /* next */
342 *p++ = xdr_zero; /* cookie, first word */
343 *p++ = xdr_one; /* cookie, second word */
344 *p++ = xdr_one; /* entry len */
345 memcpy(p, ".\0\0\0", 4); /* entry */
347 *p++ = xdr_one; /* bitmap length */
348 *p++ = htonl(attrs); /* bitmap */
349 *p++ = htonl(12); /* attribute buffer length */
350 *p++ = htonl(NF4DIR);
351 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
354 *p++ = xdr_one; /* next */
355 *p++ = xdr_zero; /* cookie, first word */
356 *p++ = xdr_two; /* cookie, second word */
357 *p++ = xdr_two; /* entry len */
358 memcpy(p, "..\0\0", 4); /* entry */
360 *p++ = xdr_one; /* bitmap length */
361 *p++ = htonl(attrs); /* bitmap */
362 *p++ = htonl(12); /* attribute buffer length */
363 *p++ = htonl(NF4DIR);
364 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
366 readdir->pgbase = (char *)p - (char *)start;
367 readdir->count -= readdir->pgbase;
368 kunmap_atomic(start);
371 static void nfs4_test_and_free_stateid(struct nfs_server *server,
372 nfs4_stateid *stateid,
373 const struct cred *cred)
375 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
377 ops->test_and_free_expired(server, stateid, cred);
380 static void __nfs4_free_revoked_stateid(struct nfs_server *server,
381 nfs4_stateid *stateid,
382 const struct cred *cred)
384 stateid->type = NFS4_REVOKED_STATEID_TYPE;
385 nfs4_test_and_free_stateid(server, stateid, cred);
388 static void nfs4_free_revoked_stateid(struct nfs_server *server,
389 const nfs4_stateid *stateid,
390 const struct cred *cred)
394 nfs4_stateid_copy(&tmp, stateid);
395 __nfs4_free_revoked_stateid(server, &tmp, cred);
398 static long nfs4_update_delay(long *timeout)
402 return NFS4_POLL_RETRY_MAX;
404 *timeout = NFS4_POLL_RETRY_MIN;
405 if (*timeout > NFS4_POLL_RETRY_MAX)
406 *timeout = NFS4_POLL_RETRY_MAX;
412 static int nfs4_delay_killable(long *timeout)
416 freezable_schedule_timeout_killable_unsafe(
417 nfs4_update_delay(timeout));
418 if (!__fatal_signal_pending(current))
423 static int nfs4_delay_interruptible(long *timeout)
427 freezable_schedule_timeout_interruptible_unsafe(nfs4_update_delay(timeout));
428 if (!signal_pending(current))
430 return __fatal_signal_pending(current) ? -EINTR :-ERESTARTSYS;
433 static int nfs4_delay(long *timeout, bool interruptible)
436 return nfs4_delay_interruptible(timeout);
437 return nfs4_delay_killable(timeout);
440 static const nfs4_stateid *
441 nfs4_recoverable_stateid(const nfs4_stateid *stateid)
445 switch (stateid->type) {
446 case NFS4_OPEN_STATEID_TYPE:
447 case NFS4_LOCK_STATEID_TYPE:
448 case NFS4_DELEGATION_STATEID_TYPE:
456 /* This is the error handling routine for processes that are allowed
459 static int nfs4_do_handle_exception(struct nfs_server *server,
460 int errorcode, struct nfs4_exception *exception)
462 struct nfs_client *clp = server->nfs_client;
463 struct nfs4_state *state = exception->state;
464 const nfs4_stateid *stateid;
465 struct inode *inode = exception->inode;
468 exception->delay = 0;
469 exception->recovering = 0;
470 exception->retry = 0;
472 stateid = nfs4_recoverable_stateid(exception->stateid);
473 if (stateid == NULL && state != NULL)
474 stateid = nfs4_recoverable_stateid(&state->stateid);
479 case -NFS4ERR_BADHANDLE:
481 if (inode != NULL && S_ISREG(inode->i_mode))
482 pnfs_destroy_layout(NFS_I(inode));
484 case -NFS4ERR_DELEG_REVOKED:
485 case -NFS4ERR_ADMIN_REVOKED:
486 case -NFS4ERR_EXPIRED:
487 case -NFS4ERR_BAD_STATEID:
488 case -NFS4ERR_PARTNER_NO_AUTH:
489 if (inode != NULL && stateid != NULL) {
490 nfs_inode_find_state_and_recover(inode,
492 goto wait_on_recovery;
495 case -NFS4ERR_OPENMODE:
499 err = nfs_async_inode_return_delegation(inode,
502 goto wait_on_recovery;
503 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
504 exception->retry = 1;
510 ret = nfs4_schedule_stateid_recovery(server, state);
513 goto wait_on_recovery;
514 case -NFS4ERR_STALE_STATEID:
515 case -NFS4ERR_STALE_CLIENTID:
516 nfs4_schedule_lease_recovery(clp);
517 goto wait_on_recovery;
519 ret = nfs4_schedule_migration_recovery(server);
522 goto wait_on_recovery;
523 case -NFS4ERR_LEASE_MOVED:
524 nfs4_schedule_lease_moved_recovery(clp);
525 goto wait_on_recovery;
526 #if defined(CONFIG_NFS_V4_1)
527 case -NFS4ERR_BADSESSION:
528 case -NFS4ERR_BADSLOT:
529 case -NFS4ERR_BAD_HIGH_SLOT:
530 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
531 case -NFS4ERR_DEADSESSION:
532 case -NFS4ERR_SEQ_FALSE_RETRY:
533 case -NFS4ERR_SEQ_MISORDERED:
534 /* Handled in nfs41_sequence_process() */
535 goto wait_on_recovery;
536 #endif /* defined(CONFIG_NFS_V4_1) */
537 case -NFS4ERR_FILE_OPEN:
538 if (exception->timeout > HZ) {
539 /* We have retried a decent amount, time to
547 nfs_inc_server_stats(server, NFSIOS_DELAY);
550 case -NFS4ERR_LAYOUTTRYLATER:
551 case -NFS4ERR_RECALLCONFLICT:
552 exception->delay = 1;
555 case -NFS4ERR_RETRY_UNCACHED_REP:
556 case -NFS4ERR_OLD_STATEID:
557 exception->retry = 1;
559 case -NFS4ERR_BADOWNER:
560 /* The following works around a Linux server bug! */
561 case -NFS4ERR_BADNAME:
562 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
563 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
564 exception->retry = 1;
565 printk(KERN_WARNING "NFS: v4 server %s "
566 "does not accept raw "
568 "Reenabling the idmapper.\n",
569 server->nfs_client->cl_hostname);
572 /* We failed to handle the error */
573 return nfs4_map_errors(ret);
575 exception->recovering = 1;
579 /* This is the error handling routine for processes that are allowed
582 int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
584 struct nfs_client *clp = server->nfs_client;
587 ret = nfs4_do_handle_exception(server, errorcode, exception);
588 if (exception->delay) {
589 ret = nfs4_delay(&exception->timeout,
590 exception->interruptible);
593 if (exception->recovering) {
594 ret = nfs4_wait_clnt_recover(clp);
595 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
602 exception->retry = 1;
607 nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
608 int errorcode, struct nfs4_exception *exception)
610 struct nfs_client *clp = server->nfs_client;
613 ret = nfs4_do_handle_exception(server, errorcode, exception);
614 if (exception->delay) {
615 rpc_delay(task, nfs4_update_delay(&exception->timeout));
618 if (exception->recovering) {
619 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
620 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
621 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
624 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
629 exception->retry = 1;
631 * For NFS4ERR_MOVED, the client transport will need to
632 * be recomputed after migration recovery has completed.
634 if (errorcode == -NFS4ERR_MOVED)
635 rpc_task_release_transport(task);
641 nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
642 struct nfs4_state *state, long *timeout)
644 struct nfs4_exception exception = {
648 if (task->tk_status >= 0)
651 exception.timeout = *timeout;
652 task->tk_status = nfs4_async_handle_exception(task, server,
655 if (exception.delay && timeout)
656 *timeout = exception.timeout;
663 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
664 * or 'false' otherwise.
666 static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
668 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
669 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
672 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
674 spin_lock(&clp->cl_lock);
675 if (time_before(clp->cl_last_renewal,timestamp))
676 clp->cl_last_renewal = timestamp;
677 spin_unlock(&clp->cl_lock);
680 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
682 struct nfs_client *clp = server->nfs_client;
684 if (!nfs4_has_session(clp))
685 do_renew_lease(clp, timestamp);
688 struct nfs4_call_sync_data {
689 const struct nfs_server *seq_server;
690 struct nfs4_sequence_args *seq_args;
691 struct nfs4_sequence_res *seq_res;
694 void nfs4_init_sequence(struct nfs4_sequence_args *args,
695 struct nfs4_sequence_res *res, int cache_reply,
698 args->sa_slot = NULL;
699 args->sa_cache_this = cache_reply;
700 args->sa_privileged = privileged;
705 static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
707 struct nfs4_slot *slot = res->sr_slot;
708 struct nfs4_slot_table *tbl;
711 spin_lock(&tbl->slot_tbl_lock);
712 if (!nfs41_wake_and_assign_slot(tbl, slot))
713 nfs4_free_slot(tbl, slot);
714 spin_unlock(&tbl->slot_tbl_lock);
719 static int nfs40_sequence_done(struct rpc_task *task,
720 struct nfs4_sequence_res *res)
722 if (res->sr_slot != NULL)
723 nfs40_sequence_free_slot(res);
727 #if defined(CONFIG_NFS_V4_1)
729 static void nfs41_release_slot(struct nfs4_slot *slot)
731 struct nfs4_session *session;
732 struct nfs4_slot_table *tbl;
733 bool send_new_highest_used_slotid = false;
738 session = tbl->session;
740 /* Bump the slot sequence number */
745 spin_lock(&tbl->slot_tbl_lock);
746 /* Be nice to the server: try to ensure that the last transmitted
747 * value for highest_user_slotid <= target_highest_slotid
749 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
750 send_new_highest_used_slotid = true;
752 if (nfs41_wake_and_assign_slot(tbl, slot)) {
753 send_new_highest_used_slotid = false;
756 nfs4_free_slot(tbl, slot);
758 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
759 send_new_highest_used_slotid = false;
761 spin_unlock(&tbl->slot_tbl_lock);
762 if (send_new_highest_used_slotid)
763 nfs41_notify_server(session->clp);
764 if (waitqueue_active(&tbl->slot_waitq))
765 wake_up_all(&tbl->slot_waitq);
768 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
770 nfs41_release_slot(res->sr_slot);
774 static void nfs4_slot_sequence_record_sent(struct nfs4_slot *slot,
777 if ((s32)(seqnr - slot->seq_nr_highest_sent) > 0)
778 slot->seq_nr_highest_sent = seqnr;
780 static void nfs4_slot_sequence_acked(struct nfs4_slot *slot,
783 slot->seq_nr_highest_sent = seqnr;
784 slot->seq_nr_last_acked = seqnr;
787 static void nfs4_probe_sequence(struct nfs_client *client, const struct cred *cred,
788 struct nfs4_slot *slot)
790 struct rpc_task *task = _nfs41_proc_sequence(client, cred, slot, true);
792 rpc_put_task_async(task);
795 static int nfs41_sequence_process(struct rpc_task *task,
796 struct nfs4_sequence_res *res)
798 struct nfs4_session *session;
799 struct nfs4_slot *slot = res->sr_slot;
800 struct nfs_client *clp;
806 /* don't increment the sequence number if the task wasn't sent */
807 if (!RPC_WAS_SENT(task) || slot->seq_done)
810 session = slot->table->session;
813 trace_nfs4_sequence_done(session, res);
815 status = res->sr_status;
816 if (task->tk_status == -NFS4ERR_DEADSESSION)
817 status = -NFS4ERR_DEADSESSION;
819 /* Check the SEQUENCE operation status */
822 /* Mark this sequence number as having been acked */
823 nfs4_slot_sequence_acked(slot, slot->seq_nr);
824 /* Update the slot's sequence and clientid lease timer */
826 do_renew_lease(clp, res->sr_timestamp);
827 /* Check sequence flags */
828 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
830 nfs41_update_target_slotid(slot->table, slot, res);
834 * sr_status remains 1 if an RPC level error occurred.
835 * The server may or may not have processed the sequence
838 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
842 /* The server detected a resend of the RPC call and
843 * returned NFS4ERR_DELAY as per Section 2.10.6.2
846 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
850 nfs4_slot_sequence_acked(slot, slot->seq_nr);
852 case -NFS4ERR_RETRY_UNCACHED_REP:
853 case -NFS4ERR_SEQ_FALSE_RETRY:
855 * The server thinks we tried to replay a request.
856 * Retry the call after bumping the sequence ID.
858 nfs4_slot_sequence_acked(slot, slot->seq_nr);
860 case -NFS4ERR_BADSLOT:
862 * The slot id we used was probably retired. Try again
863 * using a different slot id.
865 if (slot->slot_nr < slot->table->target_highest_slotid)
866 goto session_recover;
868 case -NFS4ERR_SEQ_MISORDERED:
869 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
871 * Were one or more calls using this slot interrupted?
872 * If the server never received the request, then our
873 * transmitted slot sequence number may be too high. However,
874 * if the server did receive the request then it might
875 * accidentally give us a reply with a mismatched operation.
876 * We can sort this out by sending a lone sequence operation
877 * to the server on the same slot.
879 if ((s32)(slot->seq_nr - slot->seq_nr_last_acked) > 1) {
881 if (task->tk_msg.rpc_proc != &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE]) {
882 nfs4_probe_sequence(clp, task->tk_msg.rpc_cred, slot);
889 * A retry might be sent while the original request is
890 * still in progress on the replier. The replier SHOULD
891 * deal with the issue by returning NFS4ERR_DELAY as the
892 * reply to SEQUENCE or CB_SEQUENCE operation, but
893 * implementations MAY return NFS4ERR_SEQ_MISORDERED.
895 * Restart the search after a delay.
897 slot->seq_nr = slot->seq_nr_highest_sent;
899 case -NFS4ERR_BADSESSION:
900 case -NFS4ERR_DEADSESSION:
901 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
902 goto session_recover;
904 /* Just update the slot sequence no. */
908 /* The session may be reset by one of the error handlers. */
909 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
913 nfs4_schedule_session_recovery(session, status);
914 dprintk("%s ERROR: %d Reset session\n", __func__, status);
915 nfs41_sequence_free_slot(res);
920 if (rpc_restart_call_prepare(task)) {
921 nfs41_sequence_free_slot(res);
927 if (!rpc_restart_call(task))
929 rpc_delay(task, NFS4_POLL_RETRY_MAX);
933 int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
935 if (!nfs41_sequence_process(task, res))
937 if (res->sr_slot != NULL)
938 nfs41_sequence_free_slot(res);
942 EXPORT_SYMBOL_GPL(nfs41_sequence_done);
944 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
946 if (res->sr_slot == NULL)
948 if (res->sr_slot->table->session != NULL)
949 return nfs41_sequence_process(task, res);
950 return nfs40_sequence_done(task, res);
953 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
955 if (res->sr_slot != NULL) {
956 if (res->sr_slot->table->session != NULL)
957 nfs41_sequence_free_slot(res);
959 nfs40_sequence_free_slot(res);
963 int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
965 if (res->sr_slot == NULL)
967 if (!res->sr_slot->table->session)
968 return nfs40_sequence_done(task, res);
969 return nfs41_sequence_done(task, res);
971 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
973 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
975 struct nfs4_call_sync_data *data = calldata;
977 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
979 nfs4_setup_sequence(data->seq_server->nfs_client,
980 data->seq_args, data->seq_res, task);
983 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
985 struct nfs4_call_sync_data *data = calldata;
987 nfs41_sequence_done(task, data->seq_res);
990 static const struct rpc_call_ops nfs41_call_sync_ops = {
991 .rpc_call_prepare = nfs41_call_sync_prepare,
992 .rpc_call_done = nfs41_call_sync_done,
995 #else /* !CONFIG_NFS_V4_1 */
997 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
999 return nfs40_sequence_done(task, res);
1002 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
1004 if (res->sr_slot != NULL)
1005 nfs40_sequence_free_slot(res);
1008 int nfs4_sequence_done(struct rpc_task *task,
1009 struct nfs4_sequence_res *res)
1011 return nfs40_sequence_done(task, res);
1013 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
1015 #endif /* !CONFIG_NFS_V4_1 */
1017 static void nfs41_sequence_res_init(struct nfs4_sequence_res *res)
1019 res->sr_timestamp = jiffies;
1020 res->sr_status_flags = 0;
1025 void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
1026 struct nfs4_sequence_res *res,
1027 struct nfs4_slot *slot)
1031 slot->privileged = args->sa_privileged ? 1 : 0;
1032 args->sa_slot = slot;
1034 res->sr_slot = slot;
1037 int nfs4_setup_sequence(struct nfs_client *client,
1038 struct nfs4_sequence_args *args,
1039 struct nfs4_sequence_res *res,
1040 struct rpc_task *task)
1042 struct nfs4_session *session = nfs4_get_session(client);
1043 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
1044 struct nfs4_slot *slot;
1046 /* slot already allocated? */
1047 if (res->sr_slot != NULL)
1051 tbl = &session->fc_slot_table;
1053 spin_lock(&tbl->slot_tbl_lock);
1054 /* The state manager will wait until the slot table is empty */
1055 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
1058 slot = nfs4_alloc_slot(tbl);
1060 if (slot == ERR_PTR(-ENOMEM))
1061 goto out_sleep_timeout;
1064 spin_unlock(&tbl->slot_tbl_lock);
1066 nfs4_sequence_attach_slot(args, res, slot);
1068 trace_nfs4_setup_sequence(session, args);
1070 nfs41_sequence_res_init(res);
1071 rpc_call_start(task);
1074 /* Try again in 1/4 second */
1075 if (args->sa_privileged)
1076 rpc_sleep_on_priority_timeout(&tbl->slot_tbl_waitq, task,
1077 jiffies + (HZ >> 2), RPC_PRIORITY_PRIVILEGED);
1079 rpc_sleep_on_timeout(&tbl->slot_tbl_waitq, task,
1080 NULL, jiffies + (HZ >> 2));
1081 spin_unlock(&tbl->slot_tbl_lock);
1084 if (args->sa_privileged)
1085 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
1086 RPC_PRIORITY_PRIVILEGED);
1088 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
1089 spin_unlock(&tbl->slot_tbl_lock);
1092 EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
1094 static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1096 struct nfs4_call_sync_data *data = calldata;
1097 nfs4_setup_sequence(data->seq_server->nfs_client,
1098 data->seq_args, data->seq_res, task);
1101 static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1103 struct nfs4_call_sync_data *data = calldata;
1104 nfs4_sequence_done(task, data->seq_res);
1107 static const struct rpc_call_ops nfs40_call_sync_ops = {
1108 .rpc_call_prepare = nfs40_call_sync_prepare,
1109 .rpc_call_done = nfs40_call_sync_done,
1112 static int nfs4_call_sync_custom(struct rpc_task_setup *task_setup)
1115 struct rpc_task *task;
1117 task = rpc_run_task(task_setup);
1119 return PTR_ERR(task);
1121 ret = task->tk_status;
1126 static int nfs4_do_call_sync(struct rpc_clnt *clnt,
1127 struct nfs_server *server,
1128 struct rpc_message *msg,
1129 struct nfs4_sequence_args *args,
1130 struct nfs4_sequence_res *res,
1131 unsigned short task_flags)
1133 struct nfs_client *clp = server->nfs_client;
1134 struct nfs4_call_sync_data data = {
1135 .seq_server = server,
1139 struct rpc_task_setup task_setup = {
1142 .callback_ops = clp->cl_mvops->call_sync_ops,
1143 .callback_data = &data,
1144 .flags = task_flags,
1147 return nfs4_call_sync_custom(&task_setup);
1150 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
1151 struct nfs_server *server,
1152 struct rpc_message *msg,
1153 struct nfs4_sequence_args *args,
1154 struct nfs4_sequence_res *res)
1156 return nfs4_do_call_sync(clnt, server, msg, args, res, 0);
1160 int nfs4_call_sync(struct rpc_clnt *clnt,
1161 struct nfs_server *server,
1162 struct rpc_message *msg,
1163 struct nfs4_sequence_args *args,
1164 struct nfs4_sequence_res *res,
1167 nfs4_init_sequence(args, res, cache_reply, 0);
1168 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
1172 nfs4_inc_nlink_locked(struct inode *inode)
1174 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1179 nfs4_dec_nlink_locked(struct inode *inode)
1181 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1186 nfs4_update_changeattr_locked(struct inode *inode,
1187 struct nfs4_change_info *cinfo,
1188 unsigned long timestamp, unsigned long cache_validity)
1190 struct nfs_inode *nfsi = NFS_I(inode);
1192 nfsi->cache_validity |= NFS_INO_INVALID_CTIME
1193 | NFS_INO_INVALID_MTIME
1196 if (cinfo->atomic && cinfo->before == inode_peek_iversion_raw(inode)) {
1197 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1198 nfsi->attrtimeo_timestamp = jiffies;
1200 if (S_ISDIR(inode->i_mode)) {
1201 nfsi->cache_validity |= NFS_INO_INVALID_DATA;
1202 nfs_force_lookup_revalidate(inode);
1204 if (!NFS_PROTO(inode)->have_delegation(inode,
1206 nfsi->cache_validity |= NFS_INO_REVAL_PAGECACHE;
1209 if (cinfo->before != inode_peek_iversion_raw(inode))
1210 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1211 NFS_INO_INVALID_ACL |
1212 NFS_INO_INVALID_XATTR;
1214 inode_set_iversion_raw(inode, cinfo->after);
1215 nfsi->read_cache_jiffies = timestamp;
1216 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
1217 nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE;
1219 if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
1220 nfs_fscache_invalidate(inode);
1224 nfs4_update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1225 unsigned long timestamp, unsigned long cache_validity)
1227 spin_lock(&dir->i_lock);
1228 nfs4_update_changeattr_locked(dir, cinfo, timestamp, cache_validity);
1229 spin_unlock(&dir->i_lock);
1232 struct nfs4_open_createattrs {
1233 struct nfs4_label *label;
1234 struct iattr *sattr;
1235 const __u32 verf[2];
1238 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1239 int err, struct nfs4_exception *exception)
1243 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1245 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1246 exception->retry = 1;
1250 static fmode_t _nfs4_ctx_to_accessmode(const struct nfs_open_context *ctx)
1252 return ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
1255 static fmode_t _nfs4_ctx_to_openmode(const struct nfs_open_context *ctx)
1257 fmode_t ret = ctx->mode & (FMODE_READ|FMODE_WRITE);
1259 return (ctx->mode & FMODE_EXEC) ? FMODE_READ | ret : ret;
1263 nfs4_map_atomic_open_share(struct nfs_server *server,
1264 fmode_t fmode, int openflags)
1268 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1270 res = NFS4_SHARE_ACCESS_READ;
1273 res = NFS4_SHARE_ACCESS_WRITE;
1275 case FMODE_READ|FMODE_WRITE:
1276 res = NFS4_SHARE_ACCESS_BOTH;
1278 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1280 /* Want no delegation if we're using O_DIRECT */
1281 if (openflags & O_DIRECT)
1282 res |= NFS4_SHARE_WANT_NO_DELEG;
1287 static enum open_claim_type4
1288 nfs4_map_atomic_open_claim(struct nfs_server *server,
1289 enum open_claim_type4 claim)
1291 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1296 case NFS4_OPEN_CLAIM_FH:
1297 return NFS4_OPEN_CLAIM_NULL;
1298 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1299 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1300 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1301 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1305 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
1307 p->o_res.f_attr = &p->f_attr;
1308 p->o_res.f_label = p->f_label;
1309 p->o_res.seqid = p->o_arg.seqid;
1310 p->c_res.seqid = p->c_arg.seqid;
1311 p->o_res.server = p->o_arg.server;
1312 p->o_res.access_request = p->o_arg.access;
1313 nfs_fattr_init(&p->f_attr);
1314 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
1317 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
1318 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
1319 const struct nfs4_open_createattrs *c,
1320 enum open_claim_type4 claim,
1323 struct dentry *parent = dget_parent(dentry);
1324 struct inode *dir = d_inode(parent);
1325 struct nfs_server *server = NFS_SERVER(dir);
1326 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
1327 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
1328 struct nfs4_opendata *p;
1330 p = kzalloc(sizeof(*p), gfp_mask);
1334 p->f_label = nfs4_label_alloc(server, gfp_mask);
1335 if (IS_ERR(p->f_label))
1338 p->a_label = nfs4_label_alloc(server, gfp_mask);
1339 if (IS_ERR(p->a_label))
1342 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1343 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
1344 if (IS_ERR(p->o_arg.seqid))
1345 goto err_free_label;
1346 nfs_sb_active(dentry->d_sb);
1347 p->dentry = dget(dentry);
1350 atomic_inc(&sp->so_count);
1351 p->o_arg.open_flags = flags;
1352 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
1353 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1354 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1356 if (flags & O_CREAT) {
1357 p->o_arg.umask = current_umask();
1358 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1359 if (c->sattr != NULL && c->sattr->ia_valid != 0) {
1360 p->o_arg.u.attrs = &p->attrs;
1361 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
1363 memcpy(p->o_arg.u.verifier.data, c->verf,
1364 sizeof(p->o_arg.u.verifier.data));
1367 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1368 * will return permission denied for all bits until close */
1369 if (!(flags & O_EXCL)) {
1370 /* ask server to check for all possible rights as results
1372 switch (p->o_arg.claim) {
1375 case NFS4_OPEN_CLAIM_NULL:
1376 case NFS4_OPEN_CLAIM_FH:
1377 p->o_arg.access = NFS4_ACCESS_READ |
1378 NFS4_ACCESS_MODIFY |
1379 NFS4_ACCESS_EXTEND |
1380 NFS4_ACCESS_EXECUTE;
1381 #ifdef CONFIG_NFS_V4_2
1382 if (server->caps & NFS_CAP_XATTR)
1383 p->o_arg.access |= NFS4_ACCESS_XAREAD |
1384 NFS4_ACCESS_XAWRITE |
1389 p->o_arg.clientid = server->nfs_client->cl_clientid;
1390 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1391 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1392 p->o_arg.name = &dentry->d_name;
1393 p->o_arg.server = server;
1394 p->o_arg.bitmask = nfs4_bitmask(server, label);
1395 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1396 switch (p->o_arg.claim) {
1397 case NFS4_OPEN_CLAIM_NULL:
1398 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1399 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1400 p->o_arg.fh = NFS_FH(dir);
1402 case NFS4_OPEN_CLAIM_PREVIOUS:
1403 case NFS4_OPEN_CLAIM_FH:
1404 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1405 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1406 p->o_arg.fh = NFS_FH(d_inode(dentry));
1408 p->c_arg.fh = &p->o_res.fh;
1409 p->c_arg.stateid = &p->o_res.stateid;
1410 p->c_arg.seqid = p->o_arg.seqid;
1411 nfs4_init_opendata_res(p);
1412 kref_init(&p->kref);
1416 nfs4_label_free(p->a_label);
1418 nfs4_label_free(p->f_label);
1426 static void nfs4_opendata_free(struct kref *kref)
1428 struct nfs4_opendata *p = container_of(kref,
1429 struct nfs4_opendata, kref);
1430 struct super_block *sb = p->dentry->d_sb;
1432 nfs4_lgopen_release(p->lgp);
1433 nfs_free_seqid(p->o_arg.seqid);
1434 nfs4_sequence_free_slot(&p->o_res.seq_res);
1435 if (p->state != NULL)
1436 nfs4_put_open_state(p->state);
1437 nfs4_put_state_owner(p->owner);
1439 nfs4_label_free(p->a_label);
1440 nfs4_label_free(p->f_label);
1444 nfs_sb_deactive(sb);
1445 nfs_fattr_free_names(&p->f_attr);
1446 kfree(p->f_attr.mdsthreshold);
1450 static void nfs4_opendata_put(struct nfs4_opendata *p)
1453 kref_put(&p->kref, nfs4_opendata_free);
1456 static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1459 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1460 case FMODE_READ|FMODE_WRITE:
1461 return state->n_rdwr != 0;
1463 return state->n_wronly != 0;
1465 return state->n_rdonly != 0;
1471 static int can_open_cached(struct nfs4_state *state, fmode_t mode,
1472 int open_mode, enum open_claim_type4 claim)
1476 if (open_mode & (O_EXCL|O_TRUNC))
1479 case NFS4_OPEN_CLAIM_NULL:
1480 case NFS4_OPEN_CLAIM_FH:
1485 switch (mode & (FMODE_READ|FMODE_WRITE)) {
1487 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1488 && state->n_rdonly != 0;
1491 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1492 && state->n_wronly != 0;
1494 case FMODE_READ|FMODE_WRITE:
1495 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1496 && state->n_rdwr != 0;
1502 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1503 enum open_claim_type4 claim)
1505 if (delegation == NULL)
1507 if ((delegation->type & fmode) != fmode)
1510 case NFS4_OPEN_CLAIM_NULL:
1511 case NFS4_OPEN_CLAIM_FH:
1513 case NFS4_OPEN_CLAIM_PREVIOUS:
1514 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1520 nfs_mark_delegation_referenced(delegation);
1524 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1533 case FMODE_READ|FMODE_WRITE:
1536 nfs4_state_set_mode_locked(state, state->state | fmode);
1539 #ifdef CONFIG_NFS_V4_1
1540 static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1542 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1544 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1546 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1550 #endif /* CONFIG_NFS_V4_1 */
1552 static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1554 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1555 wake_up_all(&state->waitq);
1558 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
1560 struct nfs_client *clp = state->owner->so_server->nfs_client;
1561 bool need_recover = false;
1563 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1564 need_recover = true;
1565 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1566 need_recover = true;
1567 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1568 need_recover = true;
1570 nfs4_state_mark_reclaim_nograce(clp, state);
1574 * Check for whether or not the caller may update the open stateid
1575 * to the value passed in by stateid.
1577 * Note: This function relies heavily on the server implementing
1578 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1580 * i.e. The stateid seqids have to be initialised to 1, and
1581 * are then incremented on every state transition.
1583 static bool nfs_stateid_is_sequential(struct nfs4_state *state,
1584 const nfs4_stateid *stateid)
1586 if (test_bit(NFS_OPEN_STATE, &state->flags)) {
1587 /* The common case - we're updating to a new sequence number */
1588 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1589 nfs4_stateid_is_next(&state->open_stateid, stateid)) {
1593 /* This is the first OPEN in this generation */
1594 if (stateid->seqid == cpu_to_be32(1))
1600 static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1602 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1604 if (state->n_wronly)
1605 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1606 if (state->n_rdonly)
1607 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1609 set_bit(NFS_O_RDWR_STATE, &state->flags);
1610 set_bit(NFS_OPEN_STATE, &state->flags);
1613 static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1614 nfs4_stateid *stateid, fmode_t fmode)
1616 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1617 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1619 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1622 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1625 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1626 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1627 clear_bit(NFS_OPEN_STATE, &state->flags);
1629 if (stateid == NULL)
1631 /* Handle OPEN+OPEN_DOWNGRADE races */
1632 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1633 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1634 nfs_resync_open_stateid_locked(state);
1637 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1638 nfs4_stateid_copy(&state->stateid, stateid);
1639 nfs4_stateid_copy(&state->open_stateid, stateid);
1640 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
1642 nfs_state_log_update_open_stateid(state);
1645 static void nfs_clear_open_stateid(struct nfs4_state *state,
1646 nfs4_stateid *arg_stateid,
1647 nfs4_stateid *stateid, fmode_t fmode)
1649 write_seqlock(&state->seqlock);
1650 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1651 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1652 nfs_clear_open_stateid_locked(state, stateid, fmode);
1653 write_sequnlock(&state->seqlock);
1654 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1655 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1658 static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1659 const nfs4_stateid *stateid, nfs4_stateid *freeme)
1660 __must_hold(&state->owner->so_lock)
1661 __must_hold(&state->seqlock)
1669 if (nfs_stateid_is_sequential(state, stateid))
1674 /* Rely on seqids for serialisation with NFSv4.0 */
1675 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1678 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1679 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1681 * Ensure we process the state changes in the same order
1682 * in which the server processed them by delaying the
1683 * update of the stateid until we are in sequence.
1685 write_sequnlock(&state->seqlock);
1686 spin_unlock(&state->owner->so_lock);
1688 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
1690 if (!signal_pending(current)) {
1691 if (schedule_timeout(5*HZ) == 0)
1697 finish_wait(&state->waitq, &wait);
1699 spin_lock(&state->owner->so_lock);
1700 write_seqlock(&state->seqlock);
1703 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1704 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1705 nfs4_stateid_copy(freeme, &state->open_stateid);
1706 nfs_test_and_clear_all_open_stateid(state);
1709 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1710 nfs4_stateid_copy(&state->stateid, stateid);
1711 nfs4_stateid_copy(&state->open_stateid, stateid);
1712 trace_nfs4_open_stateid_update(state->inode, stateid, status);
1713 nfs_state_log_update_open_stateid(state);
1716 static void nfs_state_set_open_stateid(struct nfs4_state *state,
1717 const nfs4_stateid *open_stateid,
1719 nfs4_stateid *freeme)
1722 * Protect the call to nfs4_state_set_mode_locked and
1723 * serialise the stateid update
1725 write_seqlock(&state->seqlock);
1726 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1729 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1732 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1734 case FMODE_READ|FMODE_WRITE:
1735 set_bit(NFS_O_RDWR_STATE, &state->flags);
1737 set_bit(NFS_OPEN_STATE, &state->flags);
1738 write_sequnlock(&state->seqlock);
1741 static void nfs_state_clear_open_state_flags(struct nfs4_state *state)
1743 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1744 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1745 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1746 clear_bit(NFS_OPEN_STATE, &state->flags);
1749 static void nfs_state_set_delegation(struct nfs4_state *state,
1750 const nfs4_stateid *deleg_stateid,
1754 * Protect the call to nfs4_state_set_mode_locked and
1755 * serialise the stateid update
1757 write_seqlock(&state->seqlock);
1758 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1759 set_bit(NFS_DELEGATED_STATE, &state->flags);
1760 write_sequnlock(&state->seqlock);
1763 static void nfs_state_clear_delegation(struct nfs4_state *state)
1765 write_seqlock(&state->seqlock);
1766 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1767 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1768 write_sequnlock(&state->seqlock);
1771 int update_open_stateid(struct nfs4_state *state,
1772 const nfs4_stateid *open_stateid,
1773 const nfs4_stateid *delegation,
1776 struct nfs_server *server = NFS_SERVER(state->inode);
1777 struct nfs_client *clp = server->nfs_client;
1778 struct nfs_inode *nfsi = NFS_I(state->inode);
1779 struct nfs_delegation *deleg_cur;
1780 nfs4_stateid freeme = { };
1783 fmode &= (FMODE_READ|FMODE_WRITE);
1786 spin_lock(&state->owner->so_lock);
1787 if (open_stateid != NULL) {
1788 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1792 deleg_cur = nfs4_get_valid_delegation(state->inode);
1793 if (deleg_cur == NULL)
1796 spin_lock(&deleg_cur->lock);
1797 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1798 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1799 (deleg_cur->type & fmode) != fmode)
1800 goto no_delegation_unlock;
1802 if (delegation == NULL)
1803 delegation = &deleg_cur->stateid;
1804 else if (!nfs4_stateid_match_other(&deleg_cur->stateid, delegation))
1805 goto no_delegation_unlock;
1807 nfs_mark_delegation_referenced(deleg_cur);
1808 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
1810 no_delegation_unlock:
1811 spin_unlock(&deleg_cur->lock);
1814 update_open_stateflags(state, fmode);
1815 spin_unlock(&state->owner->so_lock);
1818 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1819 nfs4_schedule_state_manager(clp);
1820 if (freeme.type != 0)
1821 nfs4_test_and_free_stateid(server, &freeme,
1822 state->owner->so_cred);
1827 static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1828 const nfs4_stateid *stateid)
1830 struct nfs4_state *state = lsp->ls_state;
1833 spin_lock(&state->state_lock);
1834 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1836 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1838 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1841 spin_unlock(&state->state_lock);
1845 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1847 struct nfs_delegation *delegation;
1849 fmode &= FMODE_READ|FMODE_WRITE;
1851 delegation = nfs4_get_valid_delegation(inode);
1852 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1857 nfs4_inode_return_delegation(inode);
1860 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1862 struct nfs4_state *state = opendata->state;
1863 struct nfs_delegation *delegation;
1864 int open_mode = opendata->o_arg.open_flags;
1865 fmode_t fmode = opendata->o_arg.fmode;
1866 enum open_claim_type4 claim = opendata->o_arg.claim;
1867 nfs4_stateid stateid;
1871 spin_lock(&state->owner->so_lock);
1872 if (can_open_cached(state, fmode, open_mode, claim)) {
1873 update_open_stateflags(state, fmode);
1874 spin_unlock(&state->owner->so_lock);
1875 goto out_return_state;
1877 spin_unlock(&state->owner->so_lock);
1879 delegation = nfs4_get_valid_delegation(state->inode);
1880 if (!can_open_delegated(delegation, fmode, claim)) {
1884 /* Save the delegation */
1885 nfs4_stateid_copy(&stateid, &delegation->stateid);
1887 nfs_release_seqid(opendata->o_arg.seqid);
1888 if (!opendata->is_recover) {
1889 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1895 /* Try to update the stateid using the delegation */
1896 if (update_open_stateid(state, NULL, &stateid, fmode))
1897 goto out_return_state;
1900 return ERR_PTR(ret);
1902 refcount_inc(&state->count);
1907 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1909 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1910 struct nfs_delegation *delegation;
1911 int delegation_flags = 0;
1914 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1916 delegation_flags = delegation->flags;
1918 switch (data->o_arg.claim) {
1921 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1922 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1923 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1924 "returning a delegation for "
1925 "OPEN(CLAIM_DELEGATE_CUR)\n",
1929 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1930 nfs_inode_set_delegation(state->inode,
1931 data->owner->so_cred,
1932 data->o_res.delegation_type,
1933 &data->o_res.delegation,
1934 data->o_res.pagemod_limit);
1936 nfs_inode_reclaim_delegation(state->inode,
1937 data->owner->so_cred,
1938 data->o_res.delegation_type,
1939 &data->o_res.delegation,
1940 data->o_res.pagemod_limit);
1942 if (data->o_res.do_recall)
1943 nfs_async_inode_return_delegation(state->inode,
1944 &data->o_res.delegation);
1948 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1949 * and update the nfs4_state.
1951 static struct nfs4_state *
1952 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1954 struct inode *inode = data->state->inode;
1955 struct nfs4_state *state = data->state;
1958 if (!data->rpc_done) {
1959 if (data->rpc_status)
1960 return ERR_PTR(data->rpc_status);
1961 /* cached opens have already been processed */
1965 ret = nfs_refresh_inode(inode, &data->f_attr);
1967 return ERR_PTR(ret);
1969 if (data->o_res.delegation_type != 0)
1970 nfs4_opendata_check_deleg(data, state);
1972 if (!update_open_stateid(state, &data->o_res.stateid,
1973 NULL, data->o_arg.fmode))
1974 return ERR_PTR(-EAGAIN);
1975 refcount_inc(&state->count);
1980 static struct inode *
1981 nfs4_opendata_get_inode(struct nfs4_opendata *data)
1983 struct inode *inode;
1985 switch (data->o_arg.claim) {
1986 case NFS4_OPEN_CLAIM_NULL:
1987 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1988 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1989 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1990 return ERR_PTR(-EAGAIN);
1991 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1992 &data->f_attr, data->f_label);
1995 inode = d_inode(data->dentry);
1997 nfs_refresh_inode(inode, &data->f_attr);
2002 static struct nfs4_state *
2003 nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
2005 struct nfs4_state *state;
2006 struct inode *inode;
2008 inode = nfs4_opendata_get_inode(data);
2010 return ERR_CAST(inode);
2011 if (data->state != NULL && data->state->inode == inode) {
2012 state = data->state;
2013 refcount_inc(&state->count);
2015 state = nfs4_get_open_state(inode, data->owner);
2018 state = ERR_PTR(-ENOMEM);
2022 static struct nfs4_state *
2023 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2025 struct nfs4_state *state;
2027 if (!data->rpc_done) {
2028 state = nfs4_try_open_cached(data);
2029 trace_nfs4_cached_open(data->state);
2033 state = nfs4_opendata_find_nfs4_state(data);
2037 if (data->o_res.delegation_type != 0)
2038 nfs4_opendata_check_deleg(data, state);
2039 if (!update_open_stateid(state, &data->o_res.stateid,
2040 NULL, data->o_arg.fmode)) {
2041 nfs4_put_open_state(state);
2042 state = ERR_PTR(-EAGAIN);
2045 nfs_release_seqid(data->o_arg.seqid);
2049 static struct nfs4_state *
2050 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2052 struct nfs4_state *ret;
2054 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
2055 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
2057 ret = _nfs4_opendata_to_nfs4_state(data);
2058 nfs4_sequence_free_slot(&data->o_res.seq_res);
2062 static struct nfs_open_context *
2063 nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode)
2065 struct nfs_inode *nfsi = NFS_I(state->inode);
2066 struct nfs_open_context *ctx;
2069 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
2070 if (ctx->state != state)
2072 if ((ctx->mode & mode) != mode)
2074 if (!get_nfs_open_context(ctx))
2080 return ERR_PTR(-ENOENT);
2083 static struct nfs_open_context *
2084 nfs4_state_find_open_context(struct nfs4_state *state)
2086 struct nfs_open_context *ctx;
2088 ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE);
2091 ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE);
2094 return nfs4_state_find_open_context_mode(state, FMODE_READ);
2097 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
2098 struct nfs4_state *state, enum open_claim_type4 claim)
2100 struct nfs4_opendata *opendata;
2102 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
2103 NULL, claim, GFP_NOFS);
2104 if (opendata == NULL)
2105 return ERR_PTR(-ENOMEM);
2106 opendata->state = state;
2107 refcount_inc(&state->count);
2111 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
2114 struct nfs4_state *newstate;
2117 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
2119 opendata->o_arg.open_flags = 0;
2120 opendata->o_arg.fmode = fmode;
2121 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
2122 NFS_SB(opendata->dentry->d_sb),
2124 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
2125 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
2126 nfs4_init_opendata_res(opendata);
2127 ret = _nfs4_recover_proc_open(opendata);
2130 newstate = nfs4_opendata_to_nfs4_state(opendata);
2131 if (IS_ERR(newstate))
2132 return PTR_ERR(newstate);
2133 if (newstate != opendata->state)
2135 nfs4_close_state(newstate, fmode);
2139 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
2143 /* memory barrier prior to reading state->n_* */
2145 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2148 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2151 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2155 * We may have performed cached opens for all three recoveries.
2156 * Check if we need to update the current stateid.
2158 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
2159 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
2160 write_seqlock(&state->seqlock);
2161 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2162 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2163 write_sequnlock(&state->seqlock);
2170 * reclaim state on the server after a reboot.
2172 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2174 struct nfs_delegation *delegation;
2175 struct nfs4_opendata *opendata;
2176 fmode_t delegation_type = 0;
2179 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2180 NFS4_OPEN_CLAIM_PREVIOUS);
2181 if (IS_ERR(opendata))
2182 return PTR_ERR(opendata);
2184 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2185 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
2186 delegation_type = delegation->type;
2188 opendata->o_arg.u.delegation_type = delegation_type;
2189 status = nfs4_open_recover(opendata, state);
2190 nfs4_opendata_put(opendata);
2194 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2196 struct nfs_server *server = NFS_SERVER(state->inode);
2197 struct nfs4_exception exception = { };
2200 err = _nfs4_do_open_reclaim(ctx, state);
2201 trace_nfs4_open_reclaim(ctx, 0, err);
2202 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2204 if (err != -NFS4ERR_DELAY)
2206 nfs4_handle_exception(server, err, &exception);
2207 } while (exception.retry);
2211 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2213 struct nfs_open_context *ctx;
2216 ctx = nfs4_state_find_open_context(state);
2219 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2220 nfs_state_clear_open_state_flags(state);
2221 ret = nfs4_do_open_reclaim(ctx, state);
2222 put_nfs_open_context(ctx);
2226 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)
2230 printk(KERN_ERR "NFS: %s: unhandled error "
2231 "%d.\n", __func__, err);
2238 case -NFS4ERR_BADSESSION:
2239 case -NFS4ERR_BADSLOT:
2240 case -NFS4ERR_BAD_HIGH_SLOT:
2241 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2242 case -NFS4ERR_DEADSESSION:
2244 case -NFS4ERR_STALE_CLIENTID:
2245 case -NFS4ERR_STALE_STATEID:
2246 /* Don't recall a delegation if it was lost */
2247 nfs4_schedule_lease_recovery(server->nfs_client);
2249 case -NFS4ERR_MOVED:
2250 nfs4_schedule_migration_recovery(server);
2252 case -NFS4ERR_LEASE_MOVED:
2253 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2255 case -NFS4ERR_DELEG_REVOKED:
2256 case -NFS4ERR_ADMIN_REVOKED:
2257 case -NFS4ERR_EXPIRED:
2258 case -NFS4ERR_BAD_STATEID:
2259 case -NFS4ERR_OPENMODE:
2260 nfs_inode_find_state_and_recover(state->inode,
2262 nfs4_schedule_stateid_recovery(server, state);
2264 case -NFS4ERR_DELAY:
2265 case -NFS4ERR_GRACE:
2269 case -NFS4ERR_DENIED:
2271 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2273 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2280 int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2281 struct nfs4_state *state, const nfs4_stateid *stateid)
2283 struct nfs_server *server = NFS_SERVER(state->inode);
2284 struct nfs4_opendata *opendata;
2287 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2288 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2289 if (IS_ERR(opendata))
2290 return PTR_ERR(opendata);
2291 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
2292 if (!test_bit(NFS_O_RDWR_STATE, &state->flags)) {
2293 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2297 if (!test_bit(NFS_O_WRONLY_STATE, &state->flags)) {
2298 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2302 if (!test_bit(NFS_O_RDONLY_STATE, &state->flags)) {
2303 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2307 nfs_state_clear_delegation(state);
2309 nfs4_opendata_put(opendata);
2310 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
2313 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2315 struct nfs4_opendata *data = calldata;
2317 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2318 &data->c_arg.seq_args, &data->c_res.seq_res, task);
2321 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2323 struct nfs4_opendata *data = calldata;
2325 nfs40_sequence_done(task, &data->c_res.seq_res);
2327 data->rpc_status = task->tk_status;
2328 if (data->rpc_status == 0) {
2329 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
2330 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2331 renew_lease(data->o_res.server, data->timestamp);
2332 data->rpc_done = true;
2336 static void nfs4_open_confirm_release(void *calldata)
2338 struct nfs4_opendata *data = calldata;
2339 struct nfs4_state *state = NULL;
2341 /* If this request hasn't been cancelled, do nothing */
2342 if (!data->cancelled)
2344 /* In case of error, no cleanup! */
2345 if (!data->rpc_done)
2347 state = nfs4_opendata_to_nfs4_state(data);
2349 nfs4_close_state(state, data->o_arg.fmode);
2351 nfs4_opendata_put(data);
2354 static const struct rpc_call_ops nfs4_open_confirm_ops = {
2355 .rpc_call_prepare = nfs4_open_confirm_prepare,
2356 .rpc_call_done = nfs4_open_confirm_done,
2357 .rpc_release = nfs4_open_confirm_release,
2361 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2363 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2365 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
2366 struct rpc_task *task;
2367 struct rpc_message msg = {
2368 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2369 .rpc_argp = &data->c_arg,
2370 .rpc_resp = &data->c_res,
2371 .rpc_cred = data->owner->so_cred,
2373 struct rpc_task_setup task_setup_data = {
2374 .rpc_client = server->client,
2375 .rpc_message = &msg,
2376 .callback_ops = &nfs4_open_confirm_ops,
2377 .callback_data = data,
2378 .workqueue = nfsiod_workqueue,
2379 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
2383 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2385 kref_get(&data->kref);
2386 data->rpc_done = false;
2387 data->rpc_status = 0;
2388 data->timestamp = jiffies;
2389 task = rpc_run_task(&task_setup_data);
2391 return PTR_ERR(task);
2392 status = rpc_wait_for_completion_task(task);
2394 data->cancelled = true;
2397 status = data->rpc_status;
2402 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
2404 struct nfs4_opendata *data = calldata;
2405 struct nfs4_state_owner *sp = data->owner;
2406 struct nfs_client *clp = sp->so_server->nfs_client;
2407 enum open_claim_type4 claim = data->o_arg.claim;
2409 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
2412 * Check if we still need to send an OPEN call, or if we can use
2413 * a delegation instead.
2415 if (data->state != NULL) {
2416 struct nfs_delegation *delegation;
2418 if (can_open_cached(data->state, data->o_arg.fmode,
2419 data->o_arg.open_flags, claim))
2422 delegation = nfs4_get_valid_delegation(data->state->inode);
2423 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
2424 goto unlock_no_action;
2427 /* Update client id. */
2428 data->o_arg.clientid = clp->cl_clientid;
2432 case NFS4_OPEN_CLAIM_PREVIOUS:
2433 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2434 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
2435 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
2437 case NFS4_OPEN_CLAIM_FH:
2438 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
2440 data->timestamp = jiffies;
2441 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
2442 &data->o_arg.seq_args,
2443 &data->o_res.seq_res,
2445 nfs_release_seqid(data->o_arg.seqid);
2447 /* Set the create mode (note dependency on the session type) */
2448 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2449 if (data->o_arg.open_flags & O_EXCL) {
2450 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2451 if (nfs4_has_persistent_session(clp))
2452 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2453 else if (clp->cl_mvops->minor_version > 0)
2454 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2458 trace_nfs4_cached_open(data->state);
2461 task->tk_action = NULL;
2463 nfs4_sequence_done(task, &data->o_res.seq_res);
2466 static void nfs4_open_done(struct rpc_task *task, void *calldata)
2468 struct nfs4_opendata *data = calldata;
2470 data->rpc_status = task->tk_status;
2472 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
2475 if (task->tk_status == 0) {
2476 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2477 switch (data->o_res.f_attr->mode & S_IFMT) {
2481 data->rpc_status = -ELOOP;
2484 data->rpc_status = -EISDIR;
2487 data->rpc_status = -ENOTDIR;
2490 renew_lease(data->o_res.server, data->timestamp);
2491 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2492 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2494 data->rpc_done = true;
2497 static void nfs4_open_release(void *calldata)
2499 struct nfs4_opendata *data = calldata;
2500 struct nfs4_state *state = NULL;
2502 /* If this request hasn't been cancelled, do nothing */
2503 if (!data->cancelled)
2505 /* In case of error, no cleanup! */
2506 if (data->rpc_status != 0 || !data->rpc_done)
2508 /* In case we need an open_confirm, no cleanup! */
2509 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2511 state = nfs4_opendata_to_nfs4_state(data);
2513 nfs4_close_state(state, data->o_arg.fmode);
2515 nfs4_opendata_put(data);
2518 static const struct rpc_call_ops nfs4_open_ops = {
2519 .rpc_call_prepare = nfs4_open_prepare,
2520 .rpc_call_done = nfs4_open_done,
2521 .rpc_release = nfs4_open_release,
2524 static int nfs4_run_open_task(struct nfs4_opendata *data,
2525 struct nfs_open_context *ctx)
2527 struct inode *dir = d_inode(data->dir);
2528 struct nfs_server *server = NFS_SERVER(dir);
2529 struct nfs_openargs *o_arg = &data->o_arg;
2530 struct nfs_openres *o_res = &data->o_res;
2531 struct rpc_task *task;
2532 struct rpc_message msg = {
2533 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2536 .rpc_cred = data->owner->so_cred,
2538 struct rpc_task_setup task_setup_data = {
2539 .rpc_client = server->client,
2540 .rpc_message = &msg,
2541 .callback_ops = &nfs4_open_ops,
2542 .callback_data = data,
2543 .workqueue = nfsiod_workqueue,
2544 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
2548 kref_get(&data->kref);
2549 data->rpc_done = false;
2550 data->rpc_status = 0;
2551 data->cancelled = false;
2552 data->is_recover = false;
2554 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
2555 data->is_recover = true;
2556 task_setup_data.flags |= RPC_TASK_TIMEOUT;
2558 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
2559 pnfs_lgopen_prepare(data, ctx);
2561 task = rpc_run_task(&task_setup_data);
2563 return PTR_ERR(task);
2564 status = rpc_wait_for_completion_task(task);
2566 data->cancelled = true;
2569 status = data->rpc_status;
2575 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2577 struct inode *dir = d_inode(data->dir);
2578 struct nfs_openres *o_res = &data->o_res;
2581 status = nfs4_run_open_task(data, NULL);
2582 if (status != 0 || !data->rpc_done)
2585 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2587 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
2588 status = _nfs4_proc_open_confirm(data);
2594 * Additional permission checks in order to distinguish between an
2595 * open for read, and an open for execute. This works around the
2596 * fact that NFSv4 OPEN treats read and execute permissions as being
2598 * Note that in the non-execute case, we want to turn off permission
2599 * checking if we just created a new file (POSIX open() semantics).
2601 static int nfs4_opendata_access(const struct cred *cred,
2602 struct nfs4_opendata *opendata,
2603 struct nfs4_state *state, fmode_t fmode,
2606 struct nfs_access_entry cache;
2609 /* access call failed or for some reason the server doesn't
2610 * support any access modes -- defer access call until later */
2611 if (opendata->o_res.access_supported == 0)
2616 * Use openflags to check for exec, because fmode won't
2617 * always have FMODE_EXEC set when file open for exec.
2619 if (openflags & __FMODE_EXEC) {
2620 /* ONLY check for exec rights */
2621 if (S_ISDIR(state->inode->i_mode))
2622 mask = NFS4_ACCESS_LOOKUP;
2624 mask = NFS4_ACCESS_EXECUTE;
2625 } else if ((fmode & FMODE_READ) && !opendata->file_created)
2626 mask = NFS4_ACCESS_READ;
2629 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2630 nfs_access_add_cache(state->inode, &cache);
2632 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2633 if ((mask & ~cache.mask & flags) == 0)
2640 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2642 static int _nfs4_proc_open(struct nfs4_opendata *data,
2643 struct nfs_open_context *ctx)
2645 struct inode *dir = d_inode(data->dir);
2646 struct nfs_server *server = NFS_SERVER(dir);
2647 struct nfs_openargs *o_arg = &data->o_arg;
2648 struct nfs_openres *o_res = &data->o_res;
2651 status = nfs4_run_open_task(data, ctx);
2652 if (!data->rpc_done)
2655 if (status == -NFS4ERR_BADNAME &&
2656 !(o_arg->open_flags & O_CREAT))
2661 nfs_fattr_map_and_free_names(server, &data->f_attr);
2663 if (o_arg->open_flags & O_CREAT) {
2664 if (o_arg->open_flags & O_EXCL)
2665 data->file_created = true;
2666 else if (o_res->cinfo.before != o_res->cinfo.after)
2667 data->file_created = true;
2668 if (data->file_created ||
2669 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
2670 nfs4_update_changeattr(dir, &o_res->cinfo,
2671 o_res->f_attr->time_start,
2672 NFS_INO_INVALID_DATA);
2674 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2675 server->caps &= ~NFS_CAP_POSIX_LOCK;
2676 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2677 status = _nfs4_proc_open_confirm(data);
2681 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2682 nfs4_sequence_free_slot(&o_res->seq_res);
2683 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr,
2684 o_res->f_label, NULL);
2691 * reclaim state on the server after a network partition.
2692 * Assumes caller holds the appropriate lock
2694 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2696 struct nfs4_opendata *opendata;
2699 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2700 NFS4_OPEN_CLAIM_FH);
2701 if (IS_ERR(opendata))
2702 return PTR_ERR(opendata);
2703 ret = nfs4_open_recover(opendata, state);
2705 d_drop(ctx->dentry);
2706 nfs4_opendata_put(opendata);
2710 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2712 struct nfs_server *server = NFS_SERVER(state->inode);
2713 struct nfs4_exception exception = { };
2717 err = _nfs4_open_expired(ctx, state);
2718 trace_nfs4_open_expired(ctx, 0, err);
2719 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2724 case -NFS4ERR_GRACE:
2725 case -NFS4ERR_DELAY:
2726 nfs4_handle_exception(server, err, &exception);
2729 } while (exception.retry);
2734 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2736 struct nfs_open_context *ctx;
2739 ctx = nfs4_state_find_open_context(state);
2742 ret = nfs4_do_open_expired(ctx, state);
2743 put_nfs_open_context(ctx);
2747 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2748 const nfs4_stateid *stateid)
2750 nfs_remove_bad_delegation(state->inode, stateid);
2751 nfs_state_clear_delegation(state);
2754 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2756 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2757 nfs_finish_clear_delegation_stateid(state, NULL);
2760 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2762 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2763 nfs40_clear_delegation_stateid(state);
2764 nfs_state_clear_open_state_flags(state);
2765 return nfs4_open_expired(sp, state);
2768 static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2769 nfs4_stateid *stateid,
2770 const struct cred *cred)
2772 return -NFS4ERR_BAD_STATEID;
2775 #if defined(CONFIG_NFS_V4_1)
2776 static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2777 nfs4_stateid *stateid,
2778 const struct cred *cred)
2782 switch (stateid->type) {
2785 case NFS4_INVALID_STATEID_TYPE:
2786 case NFS4_SPECIAL_STATEID_TYPE:
2787 return -NFS4ERR_BAD_STATEID;
2788 case NFS4_REVOKED_STATEID_TYPE:
2792 status = nfs41_test_stateid(server, stateid, cred);
2794 case -NFS4ERR_EXPIRED:
2795 case -NFS4ERR_ADMIN_REVOKED:
2796 case -NFS4ERR_DELEG_REVOKED:
2802 /* Ack the revoked state to the server */
2803 nfs41_free_stateid(server, stateid, cred, true);
2804 return -NFS4ERR_EXPIRED;
2807 static int nfs41_check_delegation_stateid(struct nfs4_state *state)
2809 struct nfs_server *server = NFS_SERVER(state->inode);
2810 nfs4_stateid stateid;
2811 struct nfs_delegation *delegation;
2812 const struct cred *cred = NULL;
2813 int status, ret = NFS_OK;
2815 /* Get the delegation credential for use by test/free_stateid */
2817 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2818 if (delegation == NULL) {
2820 nfs_state_clear_delegation(state);
2824 spin_lock(&delegation->lock);
2825 nfs4_stateid_copy(&stateid, &delegation->stateid);
2827 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2828 &delegation->flags)) {
2829 spin_unlock(&delegation->lock);
2834 if (delegation->cred)
2835 cred = get_cred(delegation->cred);
2836 spin_unlock(&delegation->lock);
2838 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
2839 trace_nfs4_test_delegation_stateid(state, NULL, status);
2840 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
2841 nfs_finish_clear_delegation_stateid(state, &stateid);
2849 static void nfs41_delegation_recover_stateid(struct nfs4_state *state)
2853 if (test_bit(NFS_DELEGATED_STATE, &state->flags) &&
2854 nfs4_copy_delegation_stateid(state->inode, state->state,
2856 nfs4_stateid_match_other(&state->stateid, &tmp))
2857 nfs_state_set_delegation(state, &tmp, state->state);
2859 nfs_state_clear_delegation(state);
2863 * nfs41_check_expired_locks - possibly free a lock stateid
2865 * @state: NFSv4 state for an inode
2867 * Returns NFS_OK if recovery for this stateid is now finished.
2868 * Otherwise a negative NFS4ERR value is returned.
2870 static int nfs41_check_expired_locks(struct nfs4_state *state)
2872 int status, ret = NFS_OK;
2873 struct nfs4_lock_state *lsp, *prev = NULL;
2874 struct nfs_server *server = NFS_SERVER(state->inode);
2876 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2879 spin_lock(&state->state_lock);
2880 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2881 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2882 const struct cred *cred = lsp->ls_state->owner->so_cred;
2884 refcount_inc(&lsp->ls_count);
2885 spin_unlock(&state->state_lock);
2887 nfs4_put_lock_state(prev);
2890 status = nfs41_test_and_free_expired_stateid(server,
2893 trace_nfs4_test_lock_stateid(state, lsp, status);
2894 if (status == -NFS4ERR_EXPIRED ||
2895 status == -NFS4ERR_BAD_STATEID) {
2896 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
2897 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
2898 if (!recover_lost_locks)
2899 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2900 } else if (status != NFS_OK) {
2902 nfs4_put_lock_state(prev);
2905 spin_lock(&state->state_lock);
2908 spin_unlock(&state->state_lock);
2909 nfs4_put_lock_state(prev);
2915 * nfs41_check_open_stateid - possibly free an open stateid
2917 * @state: NFSv4 state for an inode
2919 * Returns NFS_OK if recovery for this stateid is now finished.
2920 * Otherwise a negative NFS4ERR value is returned.
2922 static int nfs41_check_open_stateid(struct nfs4_state *state)
2924 struct nfs_server *server = NFS_SERVER(state->inode);
2925 nfs4_stateid *stateid = &state->open_stateid;
2926 const struct cred *cred = state->owner->so_cred;
2929 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0)
2930 return -NFS4ERR_BAD_STATEID;
2931 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
2932 trace_nfs4_test_open_stateid(state, NULL, status);
2933 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
2934 nfs_state_clear_open_state_flags(state);
2935 stateid->type = NFS4_INVALID_STATEID_TYPE;
2938 if (nfs_open_stateid_recover_openmode(state))
2939 return -NFS4ERR_OPENMODE;
2943 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2947 status = nfs41_check_delegation_stateid(state);
2948 if (status != NFS_OK)
2950 nfs41_delegation_recover_stateid(state);
2952 status = nfs41_check_expired_locks(state);
2953 if (status != NFS_OK)
2955 status = nfs41_check_open_stateid(state);
2956 if (status != NFS_OK)
2957 status = nfs4_open_expired(sp, state);
2963 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2964 * fields corresponding to attributes that were used to store the verifier.
2965 * Make sure we clobber those fields in the later setattr call
2967 static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2968 struct iattr *sattr, struct nfs4_label **label)
2970 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2975 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
2976 attrset[i] = opendata->o_res.attrset[i];
2977 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
2978 attrset[i] &= ~bitmask[i];
2981 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
2982 sattr->ia_valid : 0;
2984 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
2985 if (sattr->ia_valid & ATTR_ATIME_SET)
2986 ret |= ATTR_ATIME_SET;
2991 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
2992 if (sattr->ia_valid & ATTR_MTIME_SET)
2993 ret |= ATTR_MTIME_SET;
2998 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
3003 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
3004 int flags, struct nfs_open_context *ctx)
3006 struct nfs4_state_owner *sp = opendata->owner;
3007 struct nfs_server *server = sp->so_server;
3008 struct dentry *dentry;
3009 struct nfs4_state *state;
3010 fmode_t acc_mode = _nfs4_ctx_to_accessmode(ctx);
3011 struct inode *dir = d_inode(opendata->dir);
3012 unsigned long dir_verifier;
3016 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
3017 dir_verifier = nfs_save_change_attribute(dir);
3019 ret = _nfs4_proc_open(opendata, ctx);
3023 state = _nfs4_opendata_to_nfs4_state(opendata);
3024 ret = PTR_ERR(state);
3028 if (server->caps & NFS_CAP_POSIX_LOCK)
3029 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
3030 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
3031 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
3033 dentry = opendata->dentry;
3034 if (d_really_is_negative(dentry)) {
3035 struct dentry *alias;
3037 alias = d_exact_alias(dentry, state->inode);
3039 alias = d_splice_alias(igrab(state->inode), dentry);
3040 /* d_splice_alias() can't fail here - it's a non-directory */
3043 ctx->dentry = dentry = alias;
3047 switch(opendata->o_arg.claim) {
3050 case NFS4_OPEN_CLAIM_NULL:
3051 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
3052 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
3053 if (!opendata->rpc_done)
3055 if (opendata->o_res.delegation_type != 0)
3056 dir_verifier = nfs_save_change_attribute(dir);
3057 nfs_set_verifier(dentry, dir_verifier);
3060 /* Parse layoutget results before we check for access */
3061 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
3063 ret = nfs4_opendata_access(sp->so_cred, opendata, state,
3068 if (d_inode(dentry) == state->inode) {
3069 nfs_inode_attach_open_context(ctx);
3070 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
3071 nfs4_schedule_stateid_recovery(server, state);
3075 if (!opendata->cancelled)
3076 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
3081 * Returns a referenced nfs4_state
3083 static int _nfs4_do_open(struct inode *dir,
3084 struct nfs_open_context *ctx,
3086 const struct nfs4_open_createattrs *c,
3089 struct nfs4_state_owner *sp;
3090 struct nfs4_state *state = NULL;
3091 struct nfs_server *server = NFS_SERVER(dir);
3092 struct nfs4_opendata *opendata;
3093 struct dentry *dentry = ctx->dentry;
3094 const struct cred *cred = ctx->cred;
3095 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
3096 fmode_t fmode = _nfs4_ctx_to_openmode(ctx);
3097 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
3098 struct iattr *sattr = c->sattr;
3099 struct nfs4_label *label = c->label;
3100 struct nfs4_label *olabel = NULL;
3103 /* Protect against reboot recovery conflicts */
3105 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
3107 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
3110 status = nfs4_client_recover_expired_lease(server->nfs_client);
3112 goto err_put_state_owner;
3113 if (d_really_is_positive(dentry))
3114 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
3116 if (d_really_is_positive(dentry))
3117 claim = NFS4_OPEN_CLAIM_FH;
3118 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
3119 c, claim, GFP_KERNEL);
3120 if (opendata == NULL)
3121 goto err_put_state_owner;
3124 olabel = nfs4_label_alloc(server, GFP_KERNEL);
3125 if (IS_ERR(olabel)) {
3126 status = PTR_ERR(olabel);
3127 goto err_opendata_put;
3131 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
3132 if (!opendata->f_attr.mdsthreshold) {
3133 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
3134 if (!opendata->f_attr.mdsthreshold)
3135 goto err_free_label;
3137 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
3139 if (d_really_is_positive(dentry))
3140 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
3142 status = _nfs4_open_and_get_state(opendata, flags, ctx);
3144 goto err_free_label;
3147 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
3148 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
3149 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
3151 * send create attributes which was not set by open
3152 * with an extra setattr.
3154 if (attrs || label) {
3155 unsigned ia_old = sattr->ia_valid;
3157 sattr->ia_valid = attrs;
3158 nfs_fattr_init(opendata->o_res.f_attr);
3159 status = nfs4_do_setattr(state->inode, cred,
3160 opendata->o_res.f_attr, sattr,
3161 ctx, label, olabel);
3163 nfs_setattr_update_inode(state->inode, sattr,
3164 opendata->o_res.f_attr);
3165 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
3167 sattr->ia_valid = ia_old;
3170 if (opened && opendata->file_created)
3173 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
3174 *ctx_th = opendata->f_attr.mdsthreshold;
3175 opendata->f_attr.mdsthreshold = NULL;
3178 nfs4_label_free(olabel);
3180 nfs4_opendata_put(opendata);
3181 nfs4_put_state_owner(sp);
3184 nfs4_label_free(olabel);
3186 nfs4_opendata_put(opendata);
3187 err_put_state_owner:
3188 nfs4_put_state_owner(sp);
3194 static struct nfs4_state *nfs4_do_open(struct inode *dir,
3195 struct nfs_open_context *ctx,
3197 struct iattr *sattr,
3198 struct nfs4_label *label,
3201 struct nfs_server *server = NFS_SERVER(dir);
3202 struct nfs4_exception exception = {
3203 .interruptible = true,
3205 struct nfs4_state *res;
3206 struct nfs4_open_createattrs c = {
3210 [0] = (__u32)jiffies,
3211 [1] = (__u32)current->pid,
3217 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
3219 trace_nfs4_open_file(ctx, flags, status);
3222 /* NOTE: BAD_SEQID means the server and client disagree about the
3223 * book-keeping w.r.t. state-changing operations
3224 * (OPEN/CLOSE/LOCK/LOCKU...)
3225 * It is actually a sign of a bug on the client or on the server.
3227 * If we receive a BAD_SEQID error in the particular case of
3228 * doing an OPEN, we assume that nfs_increment_open_seqid() will
3229 * have unhashed the old state_owner for us, and that we can
3230 * therefore safely retry using a new one. We should still warn
3231 * the user though...
3233 if (status == -NFS4ERR_BAD_SEQID) {
3234 pr_warn_ratelimited("NFS: v4 server %s "
3235 " returned a bad sequence-id error!\n",
3236 NFS_SERVER(dir)->nfs_client->cl_hostname);
3237 exception.retry = 1;
3241 * BAD_STATEID on OPEN means that the server cancelled our
3242 * state before it received the OPEN_CONFIRM.
3243 * Recover by retrying the request as per the discussion
3244 * on Page 181 of RFC3530.
3246 if (status == -NFS4ERR_BAD_STATEID) {
3247 exception.retry = 1;
3250 if (status == -NFS4ERR_EXPIRED) {
3251 nfs4_schedule_lease_recovery(server->nfs_client);
3252 exception.retry = 1;
3255 if (status == -EAGAIN) {
3256 /* We must have found a delegation */
3257 exception.retry = 1;
3260 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3262 res = ERR_PTR(nfs4_handle_exception(server,
3263 status, &exception));
3264 } while (exception.retry);
3268 static int _nfs4_do_setattr(struct inode *inode,
3269 struct nfs_setattrargs *arg,
3270 struct nfs_setattrres *res,
3271 const struct cred *cred,
3272 struct nfs_open_context *ctx)
3274 struct nfs_server *server = NFS_SERVER(inode);
3275 struct rpc_message msg = {
3276 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3281 const struct cred *delegation_cred = NULL;
3282 unsigned long timestamp = jiffies;
3286 nfs_fattr_init(res->fattr);
3288 /* Servers should only apply open mode checks for file size changes */
3289 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
3291 nfs4_inode_make_writeable(inode);
3295 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
3296 /* Use that stateid */
3297 } else if (ctx != NULL && ctx->state) {
3298 struct nfs_lock_context *l_ctx;
3299 if (!nfs4_valid_open_stateid(ctx->state))
3301 l_ctx = nfs_get_lock_context(ctx);
3303 return PTR_ERR(l_ctx);
3304 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3305 &arg->stateid, &delegation_cred);
3306 nfs_put_lock_context(l_ctx);
3309 else if (status == -EAGAIN)
3313 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
3315 if (delegation_cred)
3316 msg.rpc_cred = delegation_cred;
3318 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3320 put_cred(delegation_cred);
3321 if (status == 0 && ctx != NULL)
3322 renew_lease(server, timestamp);
3323 trace_nfs4_setattr(inode, &arg->stateid, status);
3327 static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
3328 struct nfs_fattr *fattr, struct iattr *sattr,
3329 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
3330 struct nfs4_label *olabel)
3332 struct nfs_server *server = NFS_SERVER(inode);
3333 __u32 bitmask[NFS4_BITMASK_SZ];
3334 struct nfs4_state *state = ctx ? ctx->state : NULL;
3335 struct nfs_setattrargs arg = {
3336 .fh = NFS_FH(inode),
3342 struct nfs_setattrres res = {
3347 struct nfs4_exception exception = {
3350 .stateid = &arg.stateid,
3355 nfs4_bitmap_copy_adjust_setattr(bitmask,
3356 nfs4_bitmask(server, olabel),
3359 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
3361 case -NFS4ERR_OPENMODE:
3362 if (!(sattr->ia_valid & ATTR_SIZE)) {
3363 pr_warn_once("NFSv4: server %s is incorrectly "
3364 "applying open mode checks to "
3365 "a SETATTR that is not "
3366 "changing file size.\n",
3367 server->nfs_client->cl_hostname);
3369 if (state && !(state->state & FMODE_WRITE)) {
3371 if (sattr->ia_valid & ATTR_OPEN)
3376 err = nfs4_handle_exception(server, err, &exception);
3377 } while (exception.retry);
3383 nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3385 if (inode == NULL || !nfs_have_layout(inode))
3388 return pnfs_wait_on_layoutreturn(inode, task);
3392 * Update the seqid of an open stateid
3394 static void nfs4_sync_open_stateid(nfs4_stateid *dst,
3395 struct nfs4_state *state)
3402 if (!nfs4_valid_open_stateid(state))
3404 seq = read_seqbegin(&state->seqlock);
3405 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3406 nfs4_stateid_copy(dst, &state->open_stateid);
3407 if (read_seqretry(&state->seqlock, seq))
3411 seqid_open = state->open_stateid.seqid;
3412 if (read_seqretry(&state->seqlock, seq))
3415 dst_seqid = be32_to_cpu(dst->seqid);
3416 if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) < 0)
3417 dst->seqid = seqid_open;
3423 * Update the seqid of an open stateid after receiving
3424 * NFS4ERR_OLD_STATEID
3426 static bool nfs4_refresh_open_old_stateid(nfs4_stateid *dst,
3427 struct nfs4_state *state)
3432 int seq, status = -EAGAIN;
3437 if (!nfs4_valid_open_stateid(state))
3439 seq = read_seqbegin(&state->seqlock);
3440 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3441 if (read_seqretry(&state->seqlock, seq))
3446 write_seqlock(&state->seqlock);
3447 seqid_open = state->open_stateid.seqid;
3449 dst_seqid = be32_to_cpu(dst->seqid);
3451 /* Did another OPEN bump the state's seqid? try again: */
3452 if ((s32)(be32_to_cpu(seqid_open) - dst_seqid) > 0) {
3453 dst->seqid = seqid_open;
3454 write_sequnlock(&state->seqlock);
3459 /* server says we're behind but we haven't seen the update yet */
3460 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
3461 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
3462 write_sequnlock(&state->seqlock);
3463 trace_nfs4_close_stateid_update_wait(state->inode, dst, 0);
3465 if (signal_pending(current))
3468 if (schedule_timeout(5*HZ) != 0)
3471 finish_wait(&state->waitq, &wait);
3475 if (status == -EINTR)
3478 /* we slept the whole 5 seconds, we must have lost a seqid */
3479 dst->seqid = cpu_to_be32(dst_seqid + 1);
3487 struct nfs4_closedata {
3488 struct inode *inode;
3489 struct nfs4_state *state;
3490 struct nfs_closeargs arg;
3491 struct nfs_closeres res;
3493 struct nfs4_layoutreturn_args arg;
3494 struct nfs4_layoutreturn_res res;
3495 struct nfs4_xdr_opaque_data ld_private;
3499 struct nfs_fattr fattr;
3500 unsigned long timestamp;
3503 static void nfs4_free_closedata(void *data)
3505 struct nfs4_closedata *calldata = data;
3506 struct nfs4_state_owner *sp = calldata->state->owner;
3507 struct super_block *sb = calldata->state->inode->i_sb;
3509 if (calldata->lr.roc)
3510 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3511 calldata->res.lr_ret);
3512 nfs4_put_open_state(calldata->state);
3513 nfs_free_seqid(calldata->arg.seqid);
3514 nfs4_put_state_owner(sp);
3515 nfs_sb_deactive(sb);
3519 static void nfs4_close_done(struct rpc_task *task, void *data)
3521 struct nfs4_closedata *calldata = data;
3522 struct nfs4_state *state = calldata->state;
3523 struct nfs_server *server = NFS_SERVER(calldata->inode);
3524 nfs4_stateid *res_stateid = NULL;
3525 struct nfs4_exception exception = {
3527 .inode = calldata->inode,
3528 .stateid = &calldata->arg.stateid,
3531 dprintk("%s: begin!\n", __func__);
3532 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3534 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
3536 /* Handle Layoutreturn errors */
3537 if (pnfs_roc_done(task, calldata->inode,
3538 &calldata->arg.lr_args,
3539 &calldata->res.lr_res,
3540 &calldata->res.lr_ret) == -EAGAIN)
3543 /* hmm. we are done with the inode, and in the process of freeing
3544 * the state_owner. we keep this around to process errors
3546 switch (task->tk_status) {
3548 res_stateid = &calldata->res.stateid;
3549 renew_lease(server, calldata->timestamp);
3551 case -NFS4ERR_ACCESS:
3552 if (calldata->arg.bitmask != NULL) {
3553 calldata->arg.bitmask = NULL;
3554 calldata->res.fattr = NULL;
3559 case -NFS4ERR_OLD_STATEID:
3560 /* Did we race with OPEN? */
3561 if (nfs4_refresh_open_old_stateid(&calldata->arg.stateid,
3565 case -NFS4ERR_ADMIN_REVOKED:
3566 case -NFS4ERR_STALE_STATEID:
3567 case -NFS4ERR_EXPIRED:
3568 nfs4_free_revoked_stateid(server,
3569 &calldata->arg.stateid,
3570 task->tk_msg.rpc_cred);
3572 case -NFS4ERR_BAD_STATEID:
3573 if (calldata->arg.fmode == 0)
3577 task->tk_status = nfs4_async_handle_exception(task,
3578 server, task->tk_status, &exception);
3579 if (exception.retry)
3582 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3583 res_stateid, calldata->arg.fmode);
3585 task->tk_status = 0;
3586 nfs_release_seqid(calldata->arg.seqid);
3587 nfs_refresh_inode(calldata->inode, &calldata->fattr);
3588 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
3591 task->tk_status = 0;
3592 rpc_restart_call_prepare(task);
3596 static void nfs4_close_prepare(struct rpc_task *task, void *data)
3598 struct nfs4_closedata *calldata = data;
3599 struct nfs4_state *state = calldata->state;
3600 struct inode *inode = calldata->inode;
3601 struct pnfs_layout_hdr *lo;
3602 bool is_rdonly, is_wronly, is_rdwr;
3605 dprintk("%s: begin!\n", __func__);
3606 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3609 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
3610 spin_lock(&state->owner->so_lock);
3611 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3612 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3613 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
3614 /* Calculate the change in open mode */
3615 calldata->arg.fmode = 0;
3616 if (state->n_rdwr == 0) {
3617 if (state->n_rdonly == 0)
3618 call_close |= is_rdonly;
3620 calldata->arg.fmode |= FMODE_READ;
3621 if (state->n_wronly == 0)
3622 call_close |= is_wronly;
3624 calldata->arg.fmode |= FMODE_WRITE;
3625 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3626 call_close |= is_rdwr;
3628 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3630 nfs4_sync_open_stateid(&calldata->arg.stateid, state);
3631 if (!nfs4_valid_open_stateid(state))
3633 spin_unlock(&state->owner->so_lock);
3636 /* Note: exit _without_ calling nfs4_close_done */
3640 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
3641 nfs_release_seqid(calldata->arg.seqid);
3645 lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3646 if (lo && !pnfs_layout_is_valid(lo)) {
3647 calldata->arg.lr_args = NULL;
3648 calldata->res.lr_res = NULL;
3651 if (calldata->arg.fmode == 0)
3652 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
3654 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
3655 /* Close-to-open cache consistency revalidation */
3656 if (!nfs4_have_delegation(inode, FMODE_READ)) {
3657 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3658 nfs4_bitmask_adjust(calldata->arg.bitmask, inode, NFS_SERVER(inode), NULL);
3660 calldata->arg.bitmask = NULL;
3663 calldata->arg.share_access =
3664 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3665 calldata->arg.fmode, 0);
3667 if (calldata->res.fattr == NULL)
3668 calldata->arg.bitmask = NULL;
3669 else if (calldata->arg.bitmask == NULL)
3670 calldata->res.fattr = NULL;
3671 calldata->timestamp = jiffies;
3672 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
3673 &calldata->arg.seq_args,
3674 &calldata->res.seq_res,
3676 nfs_release_seqid(calldata->arg.seqid);
3677 dprintk("%s: done!\n", __func__);
3680 task->tk_action = NULL;
3682 nfs4_sequence_done(task, &calldata->res.seq_res);
3685 static const struct rpc_call_ops nfs4_close_ops = {
3686 .rpc_call_prepare = nfs4_close_prepare,
3687 .rpc_call_done = nfs4_close_done,
3688 .rpc_release = nfs4_free_closedata,
3692 * It is possible for data to be read/written from a mem-mapped file
3693 * after the sys_close call (which hits the vfs layer as a flush).
3694 * This means that we can't safely call nfsv4 close on a file until
3695 * the inode is cleared. This in turn means that we are not good
3696 * NFSv4 citizens - we do not indicate to the server to update the file's
3697 * share state even when we are done with one of the three share
3698 * stateid's in the inode.
3700 * NOTE: Caller must be holding the sp->so_owner semaphore!
3702 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
3704 struct nfs_server *server = NFS_SERVER(state->inode);
3705 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
3706 struct nfs4_closedata *calldata;
3707 struct nfs4_state_owner *sp = state->owner;
3708 struct rpc_task *task;
3709 struct rpc_message msg = {
3710 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3711 .rpc_cred = state->owner->so_cred,
3713 struct rpc_task_setup task_setup_data = {
3714 .rpc_client = server->client,
3715 .rpc_message = &msg,
3716 .callback_ops = &nfs4_close_ops,
3717 .workqueue = nfsiod_workqueue,
3718 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
3720 int status = -ENOMEM;
3722 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3723 &task_setup_data.rpc_client, &msg);
3725 calldata = kzalloc(sizeof(*calldata), gfp_mask);
3726 if (calldata == NULL)
3728 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
3729 calldata->inode = state->inode;
3730 calldata->state = state;
3731 calldata->arg.fh = NFS_FH(state->inode);
3732 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3733 goto out_free_calldata;
3734 /* Serialization for the sequence id */
3735 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3736 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
3737 if (IS_ERR(calldata->arg.seqid))
3738 goto out_free_calldata;
3739 nfs_fattr_init(&calldata->fattr);
3740 calldata->arg.fmode = 0;
3741 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
3742 calldata->res.fattr = &calldata->fattr;
3743 calldata->res.seqid = calldata->arg.seqid;
3744 calldata->res.server = server;
3745 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3746 calldata->lr.roc = pnfs_roc(state->inode,
3747 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3748 if (calldata->lr.roc) {
3749 calldata->arg.lr_args = &calldata->lr.arg;
3750 calldata->res.lr_res = &calldata->lr.res;
3752 nfs_sb_active(calldata->inode->i_sb);
3754 msg.rpc_argp = &calldata->arg;
3755 msg.rpc_resp = &calldata->res;
3756 task_setup_data.callback_data = calldata;
3757 task = rpc_run_task(&task_setup_data);
3759 return PTR_ERR(task);
3762 status = rpc_wait_for_completion_task(task);
3768 nfs4_put_open_state(state);
3769 nfs4_put_state_owner(sp);
3773 static struct inode *
3774 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3775 int open_flags, struct iattr *attr, int *opened)
3777 struct nfs4_state *state;
3778 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3780 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
3782 /* Protect against concurrent sillydeletes */
3783 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
3785 nfs4_label_release_security(label);
3788 return ERR_CAST(state);
3789 return state->inode;
3792 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
3794 if (ctx->state == NULL)
3797 nfs4_close_sync(ctx->state, _nfs4_ctx_to_openmode(ctx));
3799 nfs4_close_state(ctx->state, _nfs4_ctx_to_openmode(ctx));
3802 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3803 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3804 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_XATTR_SUPPORT - 1UL)
3806 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3808 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
3809 struct nfs4_server_caps_arg args = {
3813 struct nfs4_server_caps_res res = {};
3814 struct rpc_message msg = {
3815 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
3822 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3823 FATTR4_WORD0_FH_EXPIRE_TYPE |
3824 FATTR4_WORD0_LINK_SUPPORT |
3825 FATTR4_WORD0_SYMLINK_SUPPORT |
3826 FATTR4_WORD0_ACLSUPPORT;
3828 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3830 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3832 /* Sanity check the server answers */
3833 switch (minorversion) {
3835 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3836 res.attr_bitmask[2] = 0;
3839 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3842 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3844 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
3845 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3846 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3847 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3848 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
3849 NFS_CAP_CTIME|NFS_CAP_MTIME|
3850 NFS_CAP_SECURITY_LABEL);
3851 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3852 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3853 server->caps |= NFS_CAP_ACLS;
3854 if (res.has_links != 0)
3855 server->caps |= NFS_CAP_HARDLINKS;
3856 if (res.has_symlinks != 0)
3857 server->caps |= NFS_CAP_SYMLINKS;
3858 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3859 server->caps |= NFS_CAP_FILEID;
3860 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3861 server->caps |= NFS_CAP_MODE;
3862 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3863 server->caps |= NFS_CAP_NLINK;
3864 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3865 server->caps |= NFS_CAP_OWNER;
3866 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3867 server->caps |= NFS_CAP_OWNER_GROUP;
3868 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3869 server->caps |= NFS_CAP_ATIME;
3870 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3871 server->caps |= NFS_CAP_CTIME;
3872 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3873 server->caps |= NFS_CAP_MTIME;
3874 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3875 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3876 server->caps |= NFS_CAP_SECURITY_LABEL;
3878 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3879 sizeof(server->attr_bitmask));
3880 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3882 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3883 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3884 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
3885 server->cache_consistency_bitmask[2] = 0;
3887 /* Avoid a regression due to buggy server */
3888 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3889 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
3890 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3891 sizeof(server->exclcreat_bitmask));
3893 server->acl_bitmask = res.acl_bitmask;
3894 server->fh_expire_type = res.fh_expire_type;
3900 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3902 struct nfs4_exception exception = {
3903 .interruptible = true,
3907 err = nfs4_handle_exception(server,
3908 _nfs4_server_capabilities(server, fhandle),
3910 } while (exception.retry);
3914 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3915 struct nfs_fsinfo *info)
3918 struct nfs4_lookup_root_arg args = {
3921 struct nfs4_lookup_res res = {
3923 .fattr = info->fattr,
3926 struct rpc_message msg = {
3927 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3932 bitmask[0] = nfs4_fattr_bitmap[0];
3933 bitmask[1] = nfs4_fattr_bitmap[1];
3935 * Process the label in the upcoming getfattr
3937 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3939 nfs_fattr_init(info->fattr);
3940 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3943 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3944 struct nfs_fsinfo *info)
3946 struct nfs4_exception exception = {
3947 .interruptible = true,
3951 err = _nfs4_lookup_root(server, fhandle, info);
3952 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
3955 case -NFS4ERR_WRONGSEC:
3958 err = nfs4_handle_exception(server, err, &exception);
3960 } while (exception.retry);
3965 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3966 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3968 struct rpc_auth_create_args auth_args = {
3969 .pseudoflavor = flavor,
3971 struct rpc_auth *auth;
3973 auth = rpcauth_create(&auth_args, server->client);
3976 return nfs4_lookup_root(server, fhandle, info);
3980 * Retry pseudoroot lookup with various security flavors. We do this when:
3982 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3983 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3985 * Returns zero on success, or a negative NFS4ERR value, or a
3986 * negative errno value.
3988 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3989 struct nfs_fsinfo *info)
3991 /* Per 3530bis 15.33.5 */
3992 static const rpc_authflavor_t flav_array[] = {
3996 RPC_AUTH_UNIX, /* courtesy */
3999 int status = -EPERM;
4002 if (server->auth_info.flavor_len > 0) {
4003 /* try each flavor specified by user */
4004 for (i = 0; i < server->auth_info.flavor_len; i++) {
4005 status = nfs4_lookup_root_sec(server, fhandle, info,
4006 server->auth_info.flavors[i]);
4007 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4012 /* no flavors specified by user, try default list */
4013 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
4014 status = nfs4_lookup_root_sec(server, fhandle, info,
4016 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4023 * -EACCES could mean that the user doesn't have correct permissions
4024 * to access the mount. It could also mean that we tried to mount
4025 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
4026 * existing mount programs don't handle -EACCES very well so it should
4027 * be mapped to -EPERM instead.
4029 if (status == -EACCES)
4035 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
4036 * @server: initialized nfs_server handle
4037 * @fhandle: we fill in the pseudo-fs root file handle
4038 * @info: we fill in an FSINFO struct
4039 * @auth_probe: probe the auth flavours
4041 * Returns zero on success, or a negative errno.
4043 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
4044 struct nfs_fsinfo *info,
4050 status = nfs4_lookup_root(server, fhandle, info);
4052 if (auth_probe || status == NFS4ERR_WRONGSEC)
4053 status = server->nfs_client->cl_mvops->find_root_sec(server,
4057 status = nfs4_server_capabilities(server, fhandle);
4059 status = nfs4_do_fsinfo(server, fhandle, info);
4061 return nfs4_map_errors(status);
4064 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
4065 struct nfs_fsinfo *info)
4068 struct nfs_fattr *fattr = info->fattr;
4069 struct nfs4_label *label = fattr->label;
4071 error = nfs4_server_capabilities(server, mntfh);
4073 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
4077 error = nfs4_proc_getattr(server, mntfh, fattr, label, NULL);
4079 dprintk("nfs4_get_root: getattr error = %d\n", -error);
4083 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
4084 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
4085 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
4092 * Get locations and (maybe) other attributes of a referral.
4093 * Note that we'll actually follow the referral later when
4094 * we detect fsid mismatch in inode revalidation
4096 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
4097 const struct qstr *name, struct nfs_fattr *fattr,
4098 struct nfs_fh *fhandle)
4100 int status = -ENOMEM;
4101 struct page *page = NULL;
4102 struct nfs4_fs_locations *locations = NULL;
4104 page = alloc_page(GFP_KERNEL);
4107 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4108 if (locations == NULL)
4111 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
4116 * If the fsid didn't change, this is a migration event, not a
4117 * referral. Cause us to drop into the exception handler, which
4118 * will kick off migration recovery.
4120 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
4121 dprintk("%s: server did not return a different fsid for"
4122 " a referral at %s\n", __func__, name->name);
4123 status = -NFS4ERR_MOVED;
4126 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
4127 nfs_fixup_referral_attributes(&locations->fattr);
4129 /* replace the lookup nfs_fattr with the locations nfs_fattr */
4130 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
4131 memset(fhandle, 0, sizeof(struct nfs_fh));
4139 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4140 struct nfs_fattr *fattr, struct nfs4_label *label,
4141 struct inode *inode)
4143 __u32 bitmask[NFS4_BITMASK_SZ];
4144 struct nfs4_getattr_arg args = {
4148 struct nfs4_getattr_res res = {
4153 struct rpc_message msg = {
4154 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4158 unsigned short task_flags = 0;
4160 /* Is this is an attribute revalidation, subject to softreval? */
4161 if (inode && (server->flags & NFS_MOUNT_SOFTREVAL))
4162 task_flags |= RPC_TASK_TIMEOUT;
4164 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode);
4166 nfs_fattr_init(fattr);
4167 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4168 return nfs4_do_call_sync(server->client, server, &msg,
4169 &args.seq_args, &res.seq_res, task_flags);
4172 int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4173 struct nfs_fattr *fattr, struct nfs4_label *label,
4174 struct inode *inode)
4176 struct nfs4_exception exception = {
4177 .interruptible = true,
4181 err = _nfs4_proc_getattr(server, fhandle, fattr, label, inode);
4182 trace_nfs4_getattr(server, fhandle, fattr, err);
4183 err = nfs4_handle_exception(server, err,
4185 } while (exception.retry);
4190 * The file is not closed if it is opened due to the a request to change
4191 * the size of the file. The open call will not be needed once the
4192 * VFS layer lookup-intents are implemented.
4194 * Close is called when the inode is destroyed.
4195 * If we haven't opened the file for O_WRONLY, we
4196 * need to in the size_change case to obtain a stateid.
4199 * Because OPEN is always done by name in nfsv4, it is
4200 * possible that we opened a different file by the same
4201 * name. We can recognize this race condition, but we
4202 * can't do anything about it besides returning an error.
4204 * This will be fixed with VFS changes (lookup-intent).
4207 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
4208 struct iattr *sattr)
4210 struct inode *inode = d_inode(dentry);
4211 const struct cred *cred = NULL;
4212 struct nfs_open_context *ctx = NULL;
4213 struct nfs4_label *label = NULL;
4216 if (pnfs_ld_layoutret_on_setattr(inode) &&
4217 sattr->ia_valid & ATTR_SIZE &&
4218 sattr->ia_size < i_size_read(inode))
4219 pnfs_commit_and_return_layout(inode);
4221 nfs_fattr_init(fattr);
4223 /* Deal with open(O_TRUNC) */
4224 if (sattr->ia_valid & ATTR_OPEN)
4225 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
4227 /* Optimization: if the end result is no change, don't RPC */
4228 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
4231 /* Search for an existing open(O_WRITE) file */
4232 if (sattr->ia_valid & ATTR_FILE) {
4234 ctx = nfs_file_open_context(sattr->ia_file);
4239 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4241 return PTR_ERR(label);
4243 /* Return any delegations if we're going to change ACLs */
4244 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
4245 nfs4_inode_make_writeable(inode);
4247 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
4249 nfs_setattr_update_inode(inode, sattr, fattr);
4250 nfs_setsecurity(inode, fattr, label);
4252 nfs4_label_free(label);
4256 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
4257 struct dentry *dentry, struct nfs_fh *fhandle,
4258 struct nfs_fattr *fattr, struct nfs4_label *label)
4260 struct nfs_server *server = NFS_SERVER(dir);
4262 struct nfs4_lookup_arg args = {
4263 .bitmask = server->attr_bitmask,
4264 .dir_fh = NFS_FH(dir),
4265 .name = &dentry->d_name,
4267 struct nfs4_lookup_res res = {
4273 struct rpc_message msg = {
4274 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4278 unsigned short task_flags = 0;
4280 /* Is this is an attribute revalidation, subject to softreval? */
4281 if (nfs_lookup_is_soft_revalidate(dentry))
4282 task_flags |= RPC_TASK_TIMEOUT;
4284 args.bitmask = nfs4_bitmask(server, label);
4286 nfs_fattr_init(fattr);
4288 dprintk("NFS call lookup %pd2\n", dentry);
4289 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4290 status = nfs4_do_call_sync(clnt, server, &msg,
4291 &args.seq_args, &res.seq_res, task_flags);
4292 dprintk("NFS reply lookup: %d\n", status);
4296 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
4298 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4299 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
4300 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4304 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
4305 struct dentry *dentry, struct nfs_fh *fhandle,
4306 struct nfs_fattr *fattr, struct nfs4_label *label)
4308 struct nfs4_exception exception = {
4309 .interruptible = true,
4311 struct rpc_clnt *client = *clnt;
4312 const struct qstr *name = &dentry->d_name;
4315 err = _nfs4_proc_lookup(client, dir, dentry, fhandle, fattr, label);
4316 trace_nfs4_lookup(dir, name, err);
4318 case -NFS4ERR_BADNAME:
4321 case -NFS4ERR_MOVED:
4322 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
4323 if (err == -NFS4ERR_MOVED)
4324 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4326 case -NFS4ERR_WRONGSEC:
4328 if (client != *clnt)
4330 client = nfs4_negotiate_security(client, dir, name);
4332 return PTR_ERR(client);
4334 exception.retry = 1;
4337 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4339 } while (exception.retry);
4344 else if (client != *clnt)
4345 rpc_shutdown_client(client);
4350 static int nfs4_proc_lookup(struct inode *dir, struct dentry *dentry,
4351 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4352 struct nfs4_label *label)
4355 struct rpc_clnt *client = NFS_CLIENT(dir);
4357 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr, label);
4358 if (client != NFS_CLIENT(dir)) {
4359 rpc_shutdown_client(client);
4360 nfs_fixup_secinfo_attributes(fattr);
4366 nfs4_proc_lookup_mountpoint(struct inode *dir, struct dentry *dentry,
4367 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4369 struct rpc_clnt *client = NFS_CLIENT(dir);
4372 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr, NULL);
4374 return ERR_PTR(status);
4375 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
4378 static int _nfs4_proc_lookupp(struct inode *inode,
4379 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4380 struct nfs4_label *label)
4382 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4383 struct nfs_server *server = NFS_SERVER(inode);
4385 struct nfs4_lookupp_arg args = {
4386 .bitmask = server->attr_bitmask,
4387 .fh = NFS_FH(inode),
4389 struct nfs4_lookupp_res res = {
4395 struct rpc_message msg = {
4396 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4401 args.bitmask = nfs4_bitmask(server, label);
4403 nfs_fattr_init(fattr);
4405 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4406 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4408 dprintk("NFS reply lookupp: %d\n", status);
4412 static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4413 struct nfs_fattr *fattr, struct nfs4_label *label)
4415 struct nfs4_exception exception = {
4416 .interruptible = true,
4420 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4421 trace_nfs4_lookupp(inode, err);
4422 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4424 } while (exception.retry);
4428 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4430 struct nfs_server *server = NFS_SERVER(inode);
4431 struct nfs4_accessargs args = {
4432 .fh = NFS_FH(inode),
4433 .access = entry->mask,
4435 struct nfs4_accessres res = {
4438 struct rpc_message msg = {
4439 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4442 .rpc_cred = entry->cred,
4446 if (!nfs4_have_delegation(inode, FMODE_READ)) {
4447 res.fattr = nfs_alloc_fattr();
4448 if (res.fattr == NULL)
4450 args.bitmask = server->cache_consistency_bitmask;
4452 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4454 nfs_access_set_mask(entry, res.access);
4456 nfs_refresh_inode(inode, res.fattr);
4458 nfs_free_fattr(res.fattr);
4462 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4464 struct nfs4_exception exception = {
4465 .interruptible = true,
4469 err = _nfs4_proc_access(inode, entry);
4470 trace_nfs4_access(inode, err);
4471 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4473 } while (exception.retry);
4478 * TODO: For the time being, we don't try to get any attributes
4479 * along with any of the zero-copy operations READ, READDIR,
4482 * In the case of the first three, we want to put the GETATTR
4483 * after the read-type operation -- this is because it is hard
4484 * to predict the length of a GETATTR response in v4, and thus
4485 * align the READ data correctly. This means that the GETATTR
4486 * may end up partially falling into the page cache, and we should
4487 * shift it into the 'tail' of the xdr_buf before processing.
4488 * To do this efficiently, we need to know the total length
4489 * of data received, which doesn't seem to be available outside
4492 * In the case of WRITE, we also want to put the GETATTR after
4493 * the operation -- in this case because we want to make sure
4494 * we get the post-operation mtime and size.
4496 * Both of these changes to the XDR layer would in fact be quite
4497 * minor, but I decided to leave them for a subsequent patch.
4499 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4500 unsigned int pgbase, unsigned int pglen)
4502 struct nfs4_readlink args = {
4503 .fh = NFS_FH(inode),
4508 struct nfs4_readlink_res res;
4509 struct rpc_message msg = {
4510 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4515 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
4518 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4519 unsigned int pgbase, unsigned int pglen)
4521 struct nfs4_exception exception = {
4522 .interruptible = true,
4526 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4527 trace_nfs4_readlink(inode, err);
4528 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4530 } while (exception.retry);
4535 * This is just for mknod. open(O_CREAT) will always do ->open_context().
4538 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
4541 struct nfs_server *server = NFS_SERVER(dir);
4542 struct nfs4_label l, *ilabel = NULL;
4543 struct nfs_open_context *ctx;
4544 struct nfs4_state *state;
4547 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
4549 return PTR_ERR(ctx);
4551 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4553 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4554 sattr->ia_mode &= ~current_umask();
4555 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
4556 if (IS_ERR(state)) {
4557 status = PTR_ERR(state);
4561 nfs4_label_release_security(ilabel);
4562 put_nfs_open_context(ctx);
4567 _nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
4569 struct nfs_server *server = NFS_SERVER(dir);
4570 struct nfs_removeargs args = {
4574 struct nfs_removeres res = {
4577 struct rpc_message msg = {
4578 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4582 unsigned long timestamp = jiffies;
4585 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
4587 spin_lock(&dir->i_lock);
4588 nfs4_update_changeattr_locked(dir, &res.cinfo, timestamp,
4589 NFS_INO_INVALID_DATA);
4590 /* Removing a directory decrements nlink in the parent */
4591 if (ftype == NF4DIR && dir->i_nlink > 2)
4592 nfs4_dec_nlink_locked(dir);
4593 spin_unlock(&dir->i_lock);
4598 static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4600 struct nfs4_exception exception = {
4601 .interruptible = true,
4603 struct inode *inode = d_inode(dentry);
4607 if (inode->i_nlink == 1)
4608 nfs4_inode_return_delegation(inode);
4610 nfs4_inode_make_writeable(inode);
4613 err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
4614 trace_nfs4_remove(dir, &dentry->d_name, err);
4615 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4617 } while (exception.retry);
4621 static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
4623 struct nfs4_exception exception = {
4624 .interruptible = true,
4629 err = _nfs4_proc_remove(dir, name, NF4DIR);
4630 trace_nfs4_remove(dir, name, err);
4631 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4633 } while (exception.retry);
4637 static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4638 struct dentry *dentry,
4639 struct inode *inode)
4641 struct nfs_removeargs *args = msg->rpc_argp;
4642 struct nfs_removeres *res = msg->rpc_resp;
4644 res->server = NFS_SB(dentry->d_sb);
4645 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
4646 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
4648 nfs_fattr_init(res->dir_attr);
4651 nfs4_inode_return_delegation(inode);
4654 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4656 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
4657 &data->args.seq_args,
4662 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
4664 struct nfs_unlinkdata *data = task->tk_calldata;
4665 struct nfs_removeres *res = &data->res;
4667 if (!nfs4_sequence_done(task, &res->seq_res))
4669 if (nfs4_async_handle_error(task, res->server, NULL,
4670 &data->timeout) == -EAGAIN)
4672 if (task->tk_status == 0)
4673 nfs4_update_changeattr(dir, &res->cinfo,
4674 res->dir_attr->time_start,
4675 NFS_INO_INVALID_DATA);
4679 static void nfs4_proc_rename_setup(struct rpc_message *msg,
4680 struct dentry *old_dentry,
4681 struct dentry *new_dentry)
4683 struct nfs_renameargs *arg = msg->rpc_argp;
4684 struct nfs_renameres *res = msg->rpc_resp;
4685 struct inode *old_inode = d_inode(old_dentry);
4686 struct inode *new_inode = d_inode(new_dentry);
4689 nfs4_inode_make_writeable(old_inode);
4691 nfs4_inode_return_delegation(new_inode);
4692 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
4693 res->server = NFS_SB(old_dentry->d_sb);
4694 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
4697 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4699 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
4700 &data->args.seq_args,
4705 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4706 struct inode *new_dir)
4708 struct nfs_renamedata *data = task->tk_calldata;
4709 struct nfs_renameres *res = &data->res;
4711 if (!nfs4_sequence_done(task, &res->seq_res))
4713 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
4716 if (task->tk_status == 0) {
4717 if (new_dir != old_dir) {
4718 /* Note: If we moved a directory, nlink will change */
4719 nfs4_update_changeattr(old_dir, &res->old_cinfo,
4720 res->old_fattr->time_start,
4721 NFS_INO_INVALID_OTHER |
4722 NFS_INO_INVALID_DATA);
4723 nfs4_update_changeattr(new_dir, &res->new_cinfo,
4724 res->new_fattr->time_start,
4725 NFS_INO_INVALID_OTHER |
4726 NFS_INO_INVALID_DATA);
4728 nfs4_update_changeattr(old_dir, &res->old_cinfo,
4729 res->old_fattr->time_start,
4730 NFS_INO_INVALID_DATA);
4735 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4737 struct nfs_server *server = NFS_SERVER(inode);
4738 __u32 bitmask[NFS4_BITMASK_SZ];
4739 struct nfs4_link_arg arg = {
4740 .fh = NFS_FH(inode),
4741 .dir_fh = NFS_FH(dir),
4745 struct nfs4_link_res res = {
4749 struct rpc_message msg = {
4750 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4754 int status = -ENOMEM;
4756 res.fattr = nfs_alloc_fattr();
4757 if (res.fattr == NULL)
4760 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4761 if (IS_ERR(res.label)) {
4762 status = PTR_ERR(res.label);
4766 nfs4_inode_make_writeable(inode);
4767 nfs4_bitmap_copy_adjust_setattr(bitmask, nfs4_bitmask(server, res.label), inode);
4769 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4771 nfs4_update_changeattr(dir, &res.cinfo, res.fattr->time_start,
4772 NFS_INO_INVALID_DATA);
4773 status = nfs_post_op_update_inode(inode, res.fattr);
4775 nfs_setsecurity(inode, res.fattr, res.label);
4779 nfs4_label_free(res.label);
4782 nfs_free_fattr(res.fattr);
4786 static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4788 struct nfs4_exception exception = {
4789 .interruptible = true,
4793 err = nfs4_handle_exception(NFS_SERVER(inode),
4794 _nfs4_proc_link(inode, dir, name),
4796 } while (exception.retry);
4800 struct nfs4_createdata {
4801 struct rpc_message msg;
4802 struct nfs4_create_arg arg;
4803 struct nfs4_create_res res;
4805 struct nfs_fattr fattr;
4806 struct nfs4_label *label;
4809 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
4810 const struct qstr *name, struct iattr *sattr, u32 ftype)
4812 struct nfs4_createdata *data;
4814 data = kzalloc(sizeof(*data), GFP_KERNEL);
4816 struct nfs_server *server = NFS_SERVER(dir);
4818 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4819 if (IS_ERR(data->label))
4822 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4823 data->msg.rpc_argp = &data->arg;
4824 data->msg.rpc_resp = &data->res;
4825 data->arg.dir_fh = NFS_FH(dir);
4826 data->arg.server = server;
4827 data->arg.name = name;
4828 data->arg.attrs = sattr;
4829 data->arg.ftype = ftype;
4830 data->arg.bitmask = nfs4_bitmask(server, data->label);
4831 data->arg.umask = current_umask();
4832 data->res.server = server;
4833 data->res.fh = &data->fh;
4834 data->res.fattr = &data->fattr;
4835 data->res.label = data->label;
4836 nfs_fattr_init(data->res.fattr);
4844 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4846 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
4847 &data->arg.seq_args, &data->res.seq_res, 1);
4849 spin_lock(&dir->i_lock);
4850 nfs4_update_changeattr_locked(dir, &data->res.dir_cinfo,
4851 data->res.fattr->time_start,
4852 NFS_INO_INVALID_DATA);
4853 /* Creating a directory bumps nlink in the parent */
4854 if (data->arg.ftype == NF4DIR)
4855 nfs4_inc_nlink_locked(dir);
4856 spin_unlock(&dir->i_lock);
4857 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
4862 static void nfs4_free_createdata(struct nfs4_createdata *data)
4864 nfs4_label_free(data->label);
4868 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4869 struct page *page, unsigned int len, struct iattr *sattr,
4870 struct nfs4_label *label)
4872 struct nfs4_createdata *data;
4873 int status = -ENAMETOOLONG;
4875 if (len > NFS4_MAXPATHLEN)
4879 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4883 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4884 data->arg.u.symlink.pages = &page;
4885 data->arg.u.symlink.len = len;
4886 data->arg.label = label;
4888 status = nfs4_do_create(dir, dentry, data);
4890 nfs4_free_createdata(data);
4895 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4896 struct page *page, unsigned int len, struct iattr *sattr)
4898 struct nfs4_exception exception = {
4899 .interruptible = true,
4901 struct nfs4_label l, *label = NULL;
4904 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4907 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4908 trace_nfs4_symlink(dir, &dentry->d_name, err);
4909 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4911 } while (exception.retry);
4913 nfs4_label_release_security(label);
4917 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4918 struct iattr *sattr, struct nfs4_label *label)
4920 struct nfs4_createdata *data;
4921 int status = -ENOMEM;
4923 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4927 data->arg.label = label;
4928 status = nfs4_do_create(dir, dentry, data);
4930 nfs4_free_createdata(data);
4935 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4936 struct iattr *sattr)
4938 struct nfs_server *server = NFS_SERVER(dir);
4939 struct nfs4_exception exception = {
4940 .interruptible = true,
4942 struct nfs4_label l, *label = NULL;
4945 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4947 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4948 sattr->ia_mode &= ~current_umask();
4950 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4951 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4952 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4954 } while (exception.retry);
4955 nfs4_label_release_security(label);
4960 static int _nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
4961 u64 cookie, struct page **pages, unsigned int count, bool plus)
4963 struct inode *dir = d_inode(dentry);
4964 struct nfs4_readdir_arg args = {
4969 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
4972 struct nfs4_readdir_res res;
4973 struct rpc_message msg = {
4974 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4981 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4983 (unsigned long long)cookie);
4984 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
4985 res.pgbase = args.pgbase;
4986 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4988 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
4989 status += args.pgbase;
4992 nfs_invalidate_atime(dir);
4994 dprintk("%s: returns %d\n", __func__, status);
4998 static int nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
4999 u64 cookie, struct page **pages, unsigned int count, bool plus)
5001 struct nfs4_exception exception = {
5002 .interruptible = true,
5006 err = _nfs4_proc_readdir(dentry, cred, cookie,
5007 pages, count, plus);
5008 trace_nfs4_readdir(d_inode(dentry), err);
5009 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
5011 } while (exception.retry);
5015 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5016 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
5018 struct nfs4_createdata *data;
5019 int mode = sattr->ia_mode;
5020 int status = -ENOMEM;
5022 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
5027 data->arg.ftype = NF4FIFO;
5028 else if (S_ISBLK(mode)) {
5029 data->arg.ftype = NF4BLK;
5030 data->arg.u.device.specdata1 = MAJOR(rdev);
5031 data->arg.u.device.specdata2 = MINOR(rdev);
5033 else if (S_ISCHR(mode)) {
5034 data->arg.ftype = NF4CHR;
5035 data->arg.u.device.specdata1 = MAJOR(rdev);
5036 data->arg.u.device.specdata2 = MINOR(rdev);
5037 } else if (!S_ISSOCK(mode)) {
5042 data->arg.label = label;
5043 status = nfs4_do_create(dir, dentry, data);
5045 nfs4_free_createdata(data);
5050 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5051 struct iattr *sattr, dev_t rdev)
5053 struct nfs_server *server = NFS_SERVER(dir);
5054 struct nfs4_exception exception = {
5055 .interruptible = true,
5057 struct nfs4_label l, *label = NULL;
5060 label = nfs4_label_init_security(dir, dentry, sattr, &l);
5062 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
5063 sattr->ia_mode &= ~current_umask();
5065 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
5066 trace_nfs4_mknod(dir, &dentry->d_name, err);
5067 err = nfs4_handle_exception(NFS_SERVER(dir), err,
5069 } while (exception.retry);
5071 nfs4_label_release_security(label);
5076 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
5077 struct nfs_fsstat *fsstat)
5079 struct nfs4_statfs_arg args = {
5081 .bitmask = server->attr_bitmask,
5083 struct nfs4_statfs_res res = {
5086 struct rpc_message msg = {
5087 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
5092 nfs_fattr_init(fsstat->fattr);
5093 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5096 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
5098 struct nfs4_exception exception = {
5099 .interruptible = true,
5103 err = nfs4_handle_exception(server,
5104 _nfs4_proc_statfs(server, fhandle, fsstat),
5106 } while (exception.retry);
5110 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
5111 struct nfs_fsinfo *fsinfo)
5113 struct nfs4_fsinfo_arg args = {
5115 .bitmask = server->attr_bitmask,
5117 struct nfs4_fsinfo_res res = {
5120 struct rpc_message msg = {
5121 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
5126 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5129 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5131 struct nfs4_exception exception = {
5132 .interruptible = true,
5137 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
5138 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
5140 nfs4_set_lease_period(server->nfs_client, fsinfo->lease_time * HZ);
5143 err = nfs4_handle_exception(server, err, &exception);
5144 } while (exception.retry);
5148 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5152 nfs_fattr_init(fsinfo->fattr);
5153 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
5155 /* block layout checks this! */
5156 server->pnfs_blksize = fsinfo->blksize;
5157 set_pnfs_layoutdriver(server, fhandle, fsinfo);
5163 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5164 struct nfs_pathconf *pathconf)
5166 struct nfs4_pathconf_arg args = {
5168 .bitmask = server->attr_bitmask,
5170 struct nfs4_pathconf_res res = {
5171 .pathconf = pathconf,
5173 struct rpc_message msg = {
5174 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
5179 /* None of the pathconf attributes are mandatory to implement */
5180 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
5181 memset(pathconf, 0, sizeof(*pathconf));
5185 nfs_fattr_init(pathconf->fattr);
5186 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5189 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5190 struct nfs_pathconf *pathconf)
5192 struct nfs4_exception exception = {
5193 .interruptible = true,
5198 err = nfs4_handle_exception(server,
5199 _nfs4_proc_pathconf(server, fhandle, pathconf),
5201 } while (exception.retry);
5205 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
5206 const struct nfs_open_context *ctx,
5207 const struct nfs_lock_context *l_ctx,
5210 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
5212 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
5214 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
5215 const struct nfs_open_context *ctx,
5216 const struct nfs_lock_context *l_ctx,
5219 nfs4_stateid _current_stateid;
5221 /* If the current stateid represents a lost lock, then exit */
5222 if (nfs4_set_rw_stateid(&_current_stateid, ctx, l_ctx, fmode) == -EIO)
5224 return nfs4_stateid_match(stateid, &_current_stateid);
5227 static bool nfs4_error_stateid_expired(int err)
5230 case -NFS4ERR_DELEG_REVOKED:
5231 case -NFS4ERR_ADMIN_REVOKED:
5232 case -NFS4ERR_BAD_STATEID:
5233 case -NFS4ERR_STALE_STATEID:
5234 case -NFS4ERR_OLD_STATEID:
5235 case -NFS4ERR_OPENMODE:
5236 case -NFS4ERR_EXPIRED:
5242 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
5244 struct nfs_server *server = NFS_SERVER(hdr->inode);
5246 trace_nfs4_read(hdr, task->tk_status);
5247 if (task->tk_status < 0) {
5248 struct nfs4_exception exception = {
5249 .inode = hdr->inode,
5250 .state = hdr->args.context->state,
5251 .stateid = &hdr->args.stateid,
5253 task->tk_status = nfs4_async_handle_exception(task,
5254 server, task->tk_status, &exception);
5255 if (exception.retry) {
5256 rpc_restart_call_prepare(task);
5261 if (task->tk_status > 0)
5262 renew_lease(server, hdr->timestamp);
5266 static bool nfs4_read_stateid_changed(struct rpc_task *task,
5267 struct nfs_pgio_args *args)
5270 if (!nfs4_error_stateid_expired(task->tk_status) ||
5271 nfs4_stateid_is_current(&args->stateid,
5276 rpc_restart_call_prepare(task);
5280 static bool nfs4_read_plus_not_supported(struct rpc_task *task,
5281 struct nfs_pgio_header *hdr)
5283 struct nfs_server *server = NFS_SERVER(hdr->inode);
5284 struct rpc_message *msg = &task->tk_msg;
5286 if (msg->rpc_proc == &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS] &&
5287 server->caps & NFS_CAP_READ_PLUS && task->tk_status == -ENOTSUPP) {
5288 server->caps &= ~NFS_CAP_READ_PLUS;
5289 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5290 rpc_restart_call_prepare(task);
5296 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5298 dprintk("--> %s\n", __func__);
5300 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5302 if (nfs4_read_stateid_changed(task, &hdr->args))
5304 if (nfs4_read_plus_not_supported(task, hdr))
5306 if (task->tk_status > 0)
5307 nfs_invalidate_atime(hdr->inode);
5308 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5309 nfs4_read_done_cb(task, hdr);
5312 #ifdef CONFIG_NFS_V4_2
5313 static void nfs42_read_plus_support(struct nfs_server *server, struct rpc_message *msg)
5315 if (server->caps & NFS_CAP_READ_PLUS)
5316 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS];
5318 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5321 static void nfs42_read_plus_support(struct nfs_server *server, struct rpc_message *msg)
5323 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5325 #endif /* CONFIG_NFS_V4_2 */
5327 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5328 struct rpc_message *msg)
5330 hdr->timestamp = jiffies;
5331 if (!hdr->pgio_done_cb)
5332 hdr->pgio_done_cb = nfs4_read_done_cb;
5333 nfs42_read_plus_support(NFS_SERVER(hdr->inode), msg);
5334 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5337 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5338 struct nfs_pgio_header *hdr)
5340 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
5341 &hdr->args.seq_args,
5345 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5346 hdr->args.lock_context,
5347 hdr->rw_mode) == -EIO)
5349 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
5354 static int nfs4_write_done_cb(struct rpc_task *task,
5355 struct nfs_pgio_header *hdr)
5357 struct inode *inode = hdr->inode;
5359 trace_nfs4_write(hdr, task->tk_status);
5360 if (task->tk_status < 0) {
5361 struct nfs4_exception exception = {
5362 .inode = hdr->inode,
5363 .state = hdr->args.context->state,
5364 .stateid = &hdr->args.stateid,
5366 task->tk_status = nfs4_async_handle_exception(task,
5367 NFS_SERVER(inode), task->tk_status,
5369 if (exception.retry) {
5370 rpc_restart_call_prepare(task);
5374 if (task->tk_status >= 0) {
5375 renew_lease(NFS_SERVER(inode), hdr->timestamp);
5376 nfs_writeback_update_inode(hdr);
5381 static bool nfs4_write_stateid_changed(struct rpc_task *task,
5382 struct nfs_pgio_args *args)
5385 if (!nfs4_error_stateid_expired(task->tk_status) ||
5386 nfs4_stateid_is_current(&args->stateid,
5391 rpc_restart_call_prepare(task);
5395 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5397 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5399 if (nfs4_write_stateid_changed(task, &hdr->args))
5401 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5402 nfs4_write_done_cb(task, hdr);
5406 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
5408 /* Don't request attributes for pNFS or O_DIRECT writes */
5409 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
5411 /* Otherwise, request attributes if and only if we don't hold
5414 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
5417 static void nfs4_bitmask_adjust(__u32 *bitmask, struct inode *inode,
5418 struct nfs_server *server,
5419 struct nfs4_label *label)
5422 unsigned long cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
5424 if ((cache_validity & NFS_INO_INVALID_DATA) ||
5425 (cache_validity & NFS_INO_REVAL_PAGECACHE) ||
5426 (cache_validity & NFS_INO_REVAL_FORCED) ||
5427 (cache_validity & NFS_INO_INVALID_OTHER))
5428 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode);
5430 if (cache_validity & NFS_INO_INVALID_ATIME)
5431 bitmask[1] |= FATTR4_WORD1_TIME_ACCESS;
5432 if (cache_validity & NFS_INO_INVALID_ACCESS)
5433 bitmask[0] |= FATTR4_WORD1_MODE | FATTR4_WORD1_OWNER |
5434 FATTR4_WORD1_OWNER_GROUP;
5435 if (cache_validity & NFS_INO_INVALID_ACL)
5436 bitmask[0] |= FATTR4_WORD0_ACL;
5437 if (cache_validity & NFS_INO_INVALID_LABEL)
5438 bitmask[2] |= FATTR4_WORD2_SECURITY_LABEL;
5439 if (cache_validity & NFS_INO_INVALID_CTIME)
5440 bitmask[0] |= FATTR4_WORD0_CHANGE;
5441 if (cache_validity & NFS_INO_INVALID_MTIME)
5442 bitmask[1] |= FATTR4_WORD1_TIME_MODIFY;
5443 if (cache_validity & NFS_INO_INVALID_SIZE)
5444 bitmask[0] |= FATTR4_WORD0_SIZE;
5445 if (cache_validity & NFS_INO_INVALID_BLOCKS)
5446 bitmask[1] |= FATTR4_WORD1_SPACE_USED;
5449 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
5450 struct rpc_message *msg,
5451 struct rpc_clnt **clnt)
5453 struct nfs_server *server = NFS_SERVER(hdr->inode);
5455 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5456 hdr->args.bitmask = NULL;
5457 hdr->res.fattr = NULL;
5459 hdr->args.bitmask = server->cache_consistency_bitmask;
5460 nfs4_bitmask_adjust(hdr->args.bitmask, hdr->inode, server, NULL);
5463 if (!hdr->pgio_done_cb)
5464 hdr->pgio_done_cb = nfs4_write_done_cb;
5465 hdr->res.server = server;
5466 hdr->timestamp = jiffies;
5468 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
5469 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5470 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
5473 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5475 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
5476 &data->args.seq_args,
5481 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
5483 struct inode *inode = data->inode;
5485 trace_nfs4_commit(data, task->tk_status);
5486 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5487 NULL, NULL) == -EAGAIN) {
5488 rpc_restart_call_prepare(task);
5494 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
5496 if (!nfs4_sequence_done(task, &data->res.seq_res))
5498 return data->commit_done_cb(task, data);
5501 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5502 struct rpc_clnt **clnt)
5504 struct nfs_server *server = NFS_SERVER(data->inode);
5506 if (data->commit_done_cb == NULL)
5507 data->commit_done_cb = nfs4_commit_done_cb;
5508 data->res.server = server;
5509 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
5510 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
5511 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
5514 static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5515 struct nfs_commitres *res)
5517 struct inode *dst_inode = file_inode(dst);
5518 struct nfs_server *server = NFS_SERVER(dst_inode);
5519 struct rpc_message msg = {
5520 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5525 args->fh = NFS_FH(dst_inode);
5526 return nfs4_call_sync(server->client, server, &msg,
5527 &args->seq_args, &res->seq_res, 1);
5530 int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5532 struct nfs_commitargs args = {
5536 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5537 struct nfs4_exception exception = { };
5541 status = _nfs4_proc_commit(dst, &args, res);
5542 status = nfs4_handle_exception(dst_server, status, &exception);
5543 } while (exception.retry);
5548 struct nfs4_renewdata {
5549 struct nfs_client *client;
5550 unsigned long timestamp;
5554 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5555 * standalone procedure for queueing an asynchronous RENEW.
5557 static void nfs4_renew_release(void *calldata)
5559 struct nfs4_renewdata *data = calldata;
5560 struct nfs_client *clp = data->client;
5562 if (refcount_read(&clp->cl_count) > 1)
5563 nfs4_schedule_state_renewal(clp);
5564 nfs_put_client(clp);
5568 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
5570 struct nfs4_renewdata *data = calldata;
5571 struct nfs_client *clp = data->client;
5572 unsigned long timestamp = data->timestamp;
5574 trace_nfs4_renew_async(clp, task->tk_status);
5575 switch (task->tk_status) {
5578 case -NFS4ERR_LEASE_MOVED:
5579 nfs4_schedule_lease_moved_recovery(clp);
5582 /* Unless we're shutting down, schedule state recovery! */
5583 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5585 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
5586 nfs4_schedule_lease_recovery(clp);
5589 nfs4_schedule_path_down_recovery(clp);
5591 do_renew_lease(clp, timestamp);
5594 static const struct rpc_call_ops nfs4_renew_ops = {
5595 .rpc_call_done = nfs4_renew_done,
5596 .rpc_release = nfs4_renew_release,
5599 static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
5601 struct rpc_message msg = {
5602 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5606 struct nfs4_renewdata *data;
5608 if (renew_flags == 0)
5610 if (!refcount_inc_not_zero(&clp->cl_count))
5612 data = kmalloc(sizeof(*data), GFP_NOFS);
5614 nfs_put_client(clp);
5618 data->timestamp = jiffies;
5619 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
5620 &nfs4_renew_ops, data);
5623 static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
5625 struct rpc_message msg = {
5626 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5630 unsigned long now = jiffies;
5633 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5636 do_renew_lease(clp, now);
5640 static inline int nfs4_server_supports_acls(struct nfs_server *server)
5642 return server->caps & NFS_CAP_ACLS;
5645 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5646 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
5649 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
5651 int nfs4_buf_to_pages_noslab(const void *buf, size_t buflen,
5652 struct page **pages)
5654 struct page *newpage, **spages;
5660 len = min_t(size_t, PAGE_SIZE, buflen);
5661 newpage = alloc_page(GFP_KERNEL);
5663 if (newpage == NULL)
5665 memcpy(page_address(newpage), buf, len);
5670 } while (buflen != 0);
5676 __free_page(spages[rc-1]);
5680 struct nfs4_cached_acl {
5686 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
5688 struct nfs_inode *nfsi = NFS_I(inode);
5690 spin_lock(&inode->i_lock);
5691 kfree(nfsi->nfs4_acl);
5692 nfsi->nfs4_acl = acl;
5693 spin_unlock(&inode->i_lock);
5696 static void nfs4_zap_acl_attr(struct inode *inode)
5698 nfs4_set_cached_acl(inode, NULL);
5701 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5703 struct nfs_inode *nfsi = NFS_I(inode);
5704 struct nfs4_cached_acl *acl;
5707 spin_lock(&inode->i_lock);
5708 acl = nfsi->nfs4_acl;
5711 if (buf == NULL) /* user is just asking for length */
5713 if (acl->cached == 0)
5715 ret = -ERANGE; /* see getxattr(2) man page */
5716 if (acl->len > buflen)
5718 memcpy(buf, acl->data, acl->len);
5722 spin_unlock(&inode->i_lock);
5726 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
5728 struct nfs4_cached_acl *acl;
5729 size_t buflen = sizeof(*acl) + acl_len;
5731 if (buflen <= PAGE_SIZE) {
5732 acl = kmalloc(buflen, GFP_KERNEL);
5736 _copy_from_pages(acl->data, pages, pgbase, acl_len);
5738 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5745 nfs4_set_cached_acl(inode, acl);
5749 * The getxattr API returns the required buffer length when called with a
5750 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5751 * the required buf. On a NULL buf, we send a page of data to the server
5752 * guessing that the ACL request can be serviced by a page. If so, we cache
5753 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5754 * the cache. If not so, we throw away the page, and cache the required
5755 * length. The next getxattr call will then produce another round trip to
5756 * the server, this time with the input buf of the required size.
5758 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5760 struct page **pages;
5761 struct nfs_getaclargs args = {
5762 .fh = NFS_FH(inode),
5765 struct nfs_getaclres res = {
5768 struct rpc_message msg = {
5769 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5773 unsigned int npages;
5774 int ret = -ENOMEM, i;
5775 struct nfs_server *server = NFS_SERVER(inode);
5778 buflen = server->rsize;
5780 npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
5781 pages = kmalloc_array(npages, sizeof(struct page *), GFP_NOFS);
5785 args.acl_pages = pages;
5787 for (i = 0; i < npages; i++) {
5788 pages[i] = alloc_page(GFP_KERNEL);
5793 /* for decoding across pages */
5794 res.acl_scratch = alloc_page(GFP_KERNEL);
5795 if (!res.acl_scratch)
5798 args.acl_len = npages * PAGE_SIZE;
5800 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
5801 __func__, buf, buflen, npages, args.acl_len);
5802 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5803 &msg, &args.seq_args, &res.seq_res, 0);
5807 /* Handle the case where the passed-in buffer is too short */
5808 if (res.acl_flags & NFS4_ACL_TRUNC) {
5809 /* Did the user only issue a request for the acl length? */
5815 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
5817 if (res.acl_len > buflen) {
5821 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
5826 for (i = 0; i < npages; i++)
5828 __free_page(pages[i]);
5829 if (res.acl_scratch)
5830 __free_page(res.acl_scratch);
5835 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5837 struct nfs4_exception exception = {
5838 .interruptible = true,
5842 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
5843 trace_nfs4_get_acl(inode, ret);
5846 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5847 } while (exception.retry);
5851 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5853 struct nfs_server *server = NFS_SERVER(inode);
5856 if (!nfs4_server_supports_acls(server))
5858 ret = nfs_revalidate_inode(server, inode);
5861 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5862 nfs_zap_acl_cache(inode);
5863 ret = nfs4_read_cached_acl(inode, buf, buflen);
5865 /* -ENOENT is returned if there is no ACL or if there is an ACL
5866 * but no cached acl data, just the acl length */
5868 return nfs4_get_acl_uncached(inode, buf, buflen);
5871 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5873 struct nfs_server *server = NFS_SERVER(inode);
5874 struct page *pages[NFS4ACL_MAXPAGES];
5875 struct nfs_setaclargs arg = {
5876 .fh = NFS_FH(inode),
5880 struct nfs_setaclres res;
5881 struct rpc_message msg = {
5882 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5886 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
5889 if (!nfs4_server_supports_acls(server))
5891 if (npages > ARRAY_SIZE(pages))
5893 i = nfs4_buf_to_pages_noslab(buf, buflen, arg.acl_pages);
5896 nfs4_inode_make_writeable(inode);
5897 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5900 * Free each page after tx, so the only ref left is
5901 * held by the network stack
5904 put_page(pages[i-1]);
5907 * Acl update can result in inode attribute update.
5908 * so mark the attribute cache invalid.
5910 spin_lock(&inode->i_lock);
5911 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
5912 | NFS_INO_INVALID_CTIME
5913 | NFS_INO_REVAL_FORCED;
5914 spin_unlock(&inode->i_lock);
5915 nfs_access_zap_cache(inode);
5916 nfs_zap_acl_cache(inode);
5920 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5922 struct nfs4_exception exception = { };
5925 err = __nfs4_proc_set_acl(inode, buf, buflen);
5926 trace_nfs4_set_acl(inode, err);
5927 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5929 } while (exception.retry);
5933 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
5934 static int _nfs4_get_security_label(struct inode *inode, void *buf,
5937 struct nfs_server *server = NFS_SERVER(inode);
5938 struct nfs_fattr fattr;
5939 struct nfs4_label label = {0, 0, buflen, buf};
5941 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5942 struct nfs4_getattr_arg arg = {
5943 .fh = NFS_FH(inode),
5946 struct nfs4_getattr_res res = {
5951 struct rpc_message msg = {
5952 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
5958 nfs_fattr_init(&fattr);
5960 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
5963 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5968 static int nfs4_get_security_label(struct inode *inode, void *buf,
5971 struct nfs4_exception exception = {
5972 .interruptible = true,
5976 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5980 err = _nfs4_get_security_label(inode, buf, buflen);
5981 trace_nfs4_get_security_label(inode, err);
5982 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5984 } while (exception.retry);
5988 static int _nfs4_do_set_security_label(struct inode *inode,
5989 struct nfs4_label *ilabel,
5990 struct nfs_fattr *fattr,
5991 struct nfs4_label *olabel)
5994 struct iattr sattr = {0};
5995 struct nfs_server *server = NFS_SERVER(inode);
5996 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5997 struct nfs_setattrargs arg = {
5998 .fh = NFS_FH(inode),
6004 struct nfs_setattrres res = {
6009 struct rpc_message msg = {
6010 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
6016 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
6018 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6020 dprintk("%s failed: %d\n", __func__, status);
6025 static int nfs4_do_set_security_label(struct inode *inode,
6026 struct nfs4_label *ilabel,
6027 struct nfs_fattr *fattr,
6028 struct nfs4_label *olabel)
6030 struct nfs4_exception exception = { };
6034 err = _nfs4_do_set_security_label(inode, ilabel,
6036 trace_nfs4_set_security_label(inode, err);
6037 err = nfs4_handle_exception(NFS_SERVER(inode), err,
6039 } while (exception.retry);
6044 nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
6046 struct nfs4_label ilabel, *olabel = NULL;
6047 struct nfs_fattr fattr;
6050 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
6053 nfs_fattr_init(&fattr);
6057 ilabel.label = (char *)buf;
6058 ilabel.len = buflen;
6060 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
6061 if (IS_ERR(olabel)) {
6062 status = -PTR_ERR(olabel);
6066 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
6068 nfs_setsecurity(inode, &fattr, olabel);
6070 nfs4_label_free(olabel);
6074 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
6077 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
6078 nfs4_verifier *bootverf)
6082 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
6083 /* An impossible timestamp guarantees this value
6084 * will never match a generated boot time. */
6085 verf[0] = cpu_to_be32(U32_MAX);
6086 verf[1] = cpu_to_be32(U32_MAX);
6088 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
6089 u64 ns = ktime_to_ns(nn->boot_time);
6091 verf[0] = cpu_to_be32(ns >> 32);
6092 verf[1] = cpu_to_be32(ns);
6094 memcpy(bootverf->data, verf, sizeof(bootverf->data));
6098 nfs4_get_uniquifier(struct nfs_client *clp, char *buf, size_t buflen)
6100 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
6101 struct nfs_netns_client *nn_clp = nn->nfs_client;
6108 id = rcu_dereference(nn_clp->identifier);
6110 strscpy(buf, id, buflen);
6114 if (nfs4_client_id_uniquifier[0] != '\0' && buf[0] == '\0')
6115 strscpy(buf, nfs4_client_id_uniquifier, buflen);
6121 nfs4_init_nonuniform_client_string(struct nfs_client *clp)
6123 char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6128 if (clp->cl_owner_id != NULL)
6133 strlen(clp->cl_rpcclient->cl_nodename) +
6135 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
6139 buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
6143 if (len > NFS4_OPAQUE_LIMIT + 1)
6147 * Since this string is allocated at mount time, and held until the
6148 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6149 * about a memory-reclaim deadlock.
6151 str = kmalloc(len, GFP_KERNEL);
6157 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
6158 clp->cl_rpcclient->cl_nodename, buf,
6159 rpc_peeraddr2str(clp->cl_rpcclient,
6162 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
6163 clp->cl_rpcclient->cl_nodename,
6164 rpc_peeraddr2str(clp->cl_rpcclient,
6168 clp->cl_owner_id = str;
6173 nfs4_init_uniform_client_string(struct nfs_client *clp)
6175 char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6180 if (clp->cl_owner_id != NULL)
6183 len = 10 + 10 + 1 + 10 + 1 +
6184 strlen(clp->cl_rpcclient->cl_nodename) + 1;
6186 buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
6190 if (len > NFS4_OPAQUE_LIMIT + 1)
6194 * Since this string is allocated at mount time, and held until the
6195 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6196 * about a memory-reclaim deadlock.
6198 str = kmalloc(len, GFP_KERNEL);
6203 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
6204 clp->rpc_ops->version, clp->cl_minorversion,
6205 buf, clp->cl_rpcclient->cl_nodename);
6207 scnprintf(str, len, "Linux NFSv%u.%u %s",
6208 clp->rpc_ops->version, clp->cl_minorversion,
6209 clp->cl_rpcclient->cl_nodename);
6210 clp->cl_owner_id = str;
6215 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
6216 * services. Advertise one based on the address family of the
6220 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
6222 if (strchr(clp->cl_ipaddr, ':') != NULL)
6223 return scnprintf(buf, len, "tcp6");
6225 return scnprintf(buf, len, "tcp");
6228 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
6230 struct nfs4_setclientid *sc = calldata;
6232 if (task->tk_status == 0)
6233 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
6236 static const struct rpc_call_ops nfs4_setclientid_ops = {
6237 .rpc_call_done = nfs4_setclientid_done,
6241 * nfs4_proc_setclientid - Negotiate client ID
6242 * @clp: state data structure
6243 * @program: RPC program for NFSv4 callback service
6244 * @port: IP port number for NFS4 callback service
6245 * @cred: credential to use for this call
6246 * @res: where to place the result
6248 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6250 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
6251 unsigned short port, const struct cred *cred,
6252 struct nfs4_setclientid_res *res)
6254 nfs4_verifier sc_verifier;
6255 struct nfs4_setclientid setclientid = {
6256 .sc_verifier = &sc_verifier,
6260 struct rpc_message msg = {
6261 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
6262 .rpc_argp = &setclientid,
6266 struct rpc_task_setup task_setup_data = {
6267 .rpc_client = clp->cl_rpcclient,
6268 .rpc_message = &msg,
6269 .callback_ops = &nfs4_setclientid_ops,
6270 .callback_data = &setclientid,
6271 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
6273 unsigned long now = jiffies;
6276 /* nfs_client_id4 */
6277 nfs4_init_boot_verifier(clp, &sc_verifier);
6279 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
6280 status = nfs4_init_uniform_client_string(clp);
6282 status = nfs4_init_nonuniform_client_string(clp);
6288 setclientid.sc_netid_len =
6289 nfs4_init_callback_netid(clp,
6290 setclientid.sc_netid,
6291 sizeof(setclientid.sc_netid));
6292 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
6293 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
6294 clp->cl_ipaddr, port >> 8, port & 255);
6296 dprintk("NFS call setclientid auth=%s, '%s'\n",
6297 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6300 status = nfs4_call_sync_custom(&task_setup_data);
6301 if (setclientid.sc_cred) {
6302 kfree(clp->cl_acceptor);
6303 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
6304 put_rpccred(setclientid.sc_cred);
6308 do_renew_lease(clp, now);
6310 trace_nfs4_setclientid(clp, status);
6311 dprintk("NFS reply setclientid: %d\n", status);
6316 * nfs4_proc_setclientid_confirm - Confirm client ID
6317 * @clp: state data structure
6318 * @arg: result of a previous SETCLIENTID
6319 * @cred: credential to use for this call
6321 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6323 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
6324 struct nfs4_setclientid_res *arg,
6325 const struct cred *cred)
6327 struct rpc_message msg = {
6328 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
6334 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
6335 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6337 status = rpc_call_sync(clp->cl_rpcclient, &msg,
6338 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
6339 trace_nfs4_setclientid_confirm(clp, status);
6340 dprintk("NFS reply setclientid_confirm: %d\n", status);
6344 struct nfs4_delegreturndata {
6345 struct nfs4_delegreturnargs args;
6346 struct nfs4_delegreturnres res;
6348 nfs4_stateid stateid;
6349 unsigned long timestamp;
6351 struct nfs4_layoutreturn_args arg;
6352 struct nfs4_layoutreturn_res res;
6353 struct nfs4_xdr_opaque_data ld_private;
6357 struct nfs_fattr fattr;
6359 struct inode *inode;
6362 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6364 struct nfs4_delegreturndata *data = calldata;
6365 struct nfs4_exception exception = {
6366 .inode = data->inode,
6367 .stateid = &data->stateid,
6370 if (!nfs4_sequence_done(task, &data->res.seq_res))
6373 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
6375 /* Handle Layoutreturn errors */
6376 if (pnfs_roc_done(task, data->inode,
6377 &data->args.lr_args,
6379 &data->res.lr_ret) == -EAGAIN)
6382 switch (task->tk_status) {
6384 renew_lease(data->res.server, data->timestamp);
6386 case -NFS4ERR_ADMIN_REVOKED:
6387 case -NFS4ERR_DELEG_REVOKED:
6388 case -NFS4ERR_EXPIRED:
6389 nfs4_free_revoked_stateid(data->res.server,
6391 task->tk_msg.rpc_cred);
6393 case -NFS4ERR_BAD_STATEID:
6394 case -NFS4ERR_STALE_STATEID:
6396 task->tk_status = 0;
6398 case -NFS4ERR_OLD_STATEID:
6399 if (!nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6400 nfs4_stateid_seqid_inc(&data->stateid);
6401 if (data->args.bitmask) {
6402 data->args.bitmask = NULL;
6403 data->res.fattr = NULL;
6406 case -NFS4ERR_ACCESS:
6407 if (data->args.bitmask) {
6408 data->args.bitmask = NULL;
6409 data->res.fattr = NULL;
6414 task->tk_status = nfs4_async_handle_exception(task,
6415 data->res.server, task->tk_status,
6417 if (exception.retry)
6420 nfs_delegation_mark_returned(data->inode, data->args.stateid);
6421 data->rpc_status = task->tk_status;
6424 task->tk_status = 0;
6425 rpc_restart_call_prepare(task);
6428 static void nfs4_delegreturn_release(void *calldata)
6430 struct nfs4_delegreturndata *data = calldata;
6431 struct inode *inode = data->inode;
6435 pnfs_roc_release(&data->lr.arg, &data->lr.res,
6437 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
6438 nfs_iput_and_deactive(inode);
6443 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6445 struct nfs4_delegreturndata *d_data;
6446 struct pnfs_layout_hdr *lo;
6448 d_data = (struct nfs4_delegreturndata *)data;
6450 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task)) {
6451 nfs4_sequence_done(task, &d_data->res.seq_res);
6455 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6456 if (lo && !pnfs_layout_is_valid(lo)) {
6457 d_data->args.lr_args = NULL;
6458 d_data->res.lr_res = NULL;
6461 nfs4_setup_sequence(d_data->res.server->nfs_client,
6462 &d_data->args.seq_args,
6463 &d_data->res.seq_res,
6467 static const struct rpc_call_ops nfs4_delegreturn_ops = {
6468 .rpc_call_prepare = nfs4_delegreturn_prepare,
6469 .rpc_call_done = nfs4_delegreturn_done,
6470 .rpc_release = nfs4_delegreturn_release,
6473 static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6475 struct nfs4_delegreturndata *data;
6476 struct nfs_server *server = NFS_SERVER(inode);
6477 struct rpc_task *task;
6478 struct rpc_message msg = {
6479 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6482 struct rpc_task_setup task_setup_data = {
6483 .rpc_client = server->client,
6484 .rpc_message = &msg,
6485 .callback_ops = &nfs4_delegreturn_ops,
6486 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
6490 data = kzalloc(sizeof(*data), GFP_NOFS);
6493 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
6495 nfs4_state_protect(server->nfs_client,
6496 NFS_SP4_MACH_CRED_CLEANUP,
6497 &task_setup_data.rpc_client, &msg);
6499 data->args.fhandle = &data->fh;
6500 data->args.stateid = &data->stateid;
6501 data->args.bitmask = server->cache_consistency_bitmask;
6502 nfs4_bitmask_adjust(data->args.bitmask, inode, server, NULL);
6503 nfs_copy_fh(&data->fh, NFS_FH(inode));
6504 nfs4_stateid_copy(&data->stateid, stateid);
6505 data->res.fattr = &data->fattr;
6506 data->res.server = server;
6507 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
6508 data->lr.arg.ld_private = &data->lr.ld_private;
6509 nfs_fattr_init(data->res.fattr);
6510 data->timestamp = jiffies;
6511 data->rpc_status = 0;
6512 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
6513 data->inode = nfs_igrab_and_active(inode);
6516 data->args.lr_args = &data->lr.arg;
6517 data->res.lr_res = &data->lr.res;
6519 } else if (data->lr.roc) {
6520 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
6521 data->lr.roc = false;
6524 task_setup_data.callback_data = data;
6525 msg.rpc_argp = &data->args;
6526 msg.rpc_resp = &data->res;
6527 task = rpc_run_task(&task_setup_data);
6529 return PTR_ERR(task);
6532 status = rpc_wait_for_completion_task(task);
6535 status = data->rpc_status;
6541 int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6543 struct nfs_server *server = NFS_SERVER(inode);
6544 struct nfs4_exception exception = { };
6547 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
6548 trace_nfs4_delegreturn(inode, stateid, err);
6550 case -NFS4ERR_STALE_STATEID:
6551 case -NFS4ERR_EXPIRED:
6555 err = nfs4_handle_exception(server, err, &exception);
6556 } while (exception.retry);
6560 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6562 struct inode *inode = state->inode;
6563 struct nfs_server *server = NFS_SERVER(inode);
6564 struct nfs_client *clp = server->nfs_client;
6565 struct nfs_lockt_args arg = {
6566 .fh = NFS_FH(inode),
6569 struct nfs_lockt_res res = {
6572 struct rpc_message msg = {
6573 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
6576 .rpc_cred = state->owner->so_cred,
6578 struct nfs4_lock_state *lsp;
6581 arg.lock_owner.clientid = clp->cl_clientid;
6582 status = nfs4_set_lock_state(state, request);
6585 lsp = request->fl_u.nfs4_fl.owner;
6586 arg.lock_owner.id = lsp->ls_seqid.owner_id;
6587 arg.lock_owner.s_dev = server->s_dev;
6588 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6591 request->fl_type = F_UNLCK;
6593 case -NFS4ERR_DENIED:
6596 request->fl_ops->fl_release_private(request);
6597 request->fl_ops = NULL;
6602 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6604 struct nfs4_exception exception = {
6605 .interruptible = true,
6610 err = _nfs4_proc_getlk(state, cmd, request);
6611 trace_nfs4_get_lock(request, state, cmd, err);
6612 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
6614 } while (exception.retry);
6619 * Update the seqid of a lock stateid after receiving
6620 * NFS4ERR_OLD_STATEID
6622 static bool nfs4_refresh_lock_old_stateid(nfs4_stateid *dst,
6623 struct nfs4_lock_state *lsp)
6625 struct nfs4_state *state = lsp->ls_state;
6628 spin_lock(&state->state_lock);
6629 if (!nfs4_stateid_match_other(dst, &lsp->ls_stateid))
6631 if (!nfs4_stateid_is_newer(&lsp->ls_stateid, dst))
6632 nfs4_stateid_seqid_inc(dst);
6634 dst->seqid = lsp->ls_stateid.seqid;
6637 spin_unlock(&state->state_lock);
6641 static bool nfs4_sync_lock_stateid(nfs4_stateid *dst,
6642 struct nfs4_lock_state *lsp)
6644 struct nfs4_state *state = lsp->ls_state;
6647 spin_lock(&state->state_lock);
6648 ret = !nfs4_stateid_match_other(dst, &lsp->ls_stateid);
6649 nfs4_stateid_copy(dst, &lsp->ls_stateid);
6650 spin_unlock(&state->state_lock);
6654 struct nfs4_unlockdata {
6655 struct nfs_locku_args arg;
6656 struct nfs_locku_res res;
6657 struct nfs4_lock_state *lsp;
6658 struct nfs_open_context *ctx;
6659 struct nfs_lock_context *l_ctx;
6660 struct file_lock fl;
6661 struct nfs_server *server;
6662 unsigned long timestamp;
6665 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6666 struct nfs_open_context *ctx,
6667 struct nfs4_lock_state *lsp,
6668 struct nfs_seqid *seqid)
6670 struct nfs4_unlockdata *p;
6671 struct nfs4_state *state = lsp->ls_state;
6672 struct inode *inode = state->inode;
6674 p = kzalloc(sizeof(*p), GFP_NOFS);
6677 p->arg.fh = NFS_FH(inode);
6679 p->arg.seqid = seqid;
6680 p->res.seqid = seqid;
6682 /* Ensure we don't close file until we're done freeing locks! */
6683 p->ctx = get_nfs_open_context(ctx);
6684 p->l_ctx = nfs_get_lock_context(ctx);
6685 locks_init_lock(&p->fl);
6686 locks_copy_lock(&p->fl, fl);
6687 p->server = NFS_SERVER(inode);
6688 spin_lock(&state->state_lock);
6689 nfs4_stateid_copy(&p->arg.stateid, &lsp->ls_stateid);
6690 spin_unlock(&state->state_lock);
6694 static void nfs4_locku_release_calldata(void *data)
6696 struct nfs4_unlockdata *calldata = data;
6697 nfs_free_seqid(calldata->arg.seqid);
6698 nfs4_put_lock_state(calldata->lsp);
6699 nfs_put_lock_context(calldata->l_ctx);
6700 put_nfs_open_context(calldata->ctx);
6704 static void nfs4_locku_done(struct rpc_task *task, void *data)
6706 struct nfs4_unlockdata *calldata = data;
6707 struct nfs4_exception exception = {
6708 .inode = calldata->lsp->ls_state->inode,
6709 .stateid = &calldata->arg.stateid,
6712 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6714 switch (task->tk_status) {
6716 renew_lease(calldata->server, calldata->timestamp);
6717 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
6718 if (nfs4_update_lock_stateid(calldata->lsp,
6719 &calldata->res.stateid))
6722 case -NFS4ERR_ADMIN_REVOKED:
6723 case -NFS4ERR_EXPIRED:
6724 nfs4_free_revoked_stateid(calldata->server,
6725 &calldata->arg.stateid,
6726 task->tk_msg.rpc_cred);
6728 case -NFS4ERR_BAD_STATEID:
6729 case -NFS4ERR_STALE_STATEID:
6730 if (nfs4_sync_lock_stateid(&calldata->arg.stateid,
6732 rpc_restart_call_prepare(task);
6734 case -NFS4ERR_OLD_STATEID:
6735 if (nfs4_refresh_lock_old_stateid(&calldata->arg.stateid,
6737 rpc_restart_call_prepare(task);
6740 task->tk_status = nfs4_async_handle_exception(task,
6741 calldata->server, task->tk_status,
6743 if (exception.retry)
6744 rpc_restart_call_prepare(task);
6746 nfs_release_seqid(calldata->arg.seqid);
6749 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
6751 struct nfs4_unlockdata *calldata = data;
6753 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6754 nfs_async_iocounter_wait(task, calldata->l_ctx))
6757 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
6759 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
6760 /* Note: exit _without_ running nfs4_locku_done */
6763 calldata->timestamp = jiffies;
6764 if (nfs4_setup_sequence(calldata->server->nfs_client,
6765 &calldata->arg.seq_args,
6766 &calldata->res.seq_res,
6768 nfs_release_seqid(calldata->arg.seqid);
6771 task->tk_action = NULL;
6773 nfs4_sequence_done(task, &calldata->res.seq_res);
6776 static const struct rpc_call_ops nfs4_locku_ops = {
6777 .rpc_call_prepare = nfs4_locku_prepare,
6778 .rpc_call_done = nfs4_locku_done,
6779 .rpc_release = nfs4_locku_release_calldata,
6782 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6783 struct nfs_open_context *ctx,
6784 struct nfs4_lock_state *lsp,
6785 struct nfs_seqid *seqid)
6787 struct nfs4_unlockdata *data;
6788 struct rpc_message msg = {
6789 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6790 .rpc_cred = ctx->cred,
6792 struct rpc_task_setup task_setup_data = {
6793 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
6794 .rpc_message = &msg,
6795 .callback_ops = &nfs4_locku_ops,
6796 .workqueue = nfsiod_workqueue,
6797 .flags = RPC_TASK_ASYNC,
6800 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6801 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6803 /* Ensure this is an unlock - when canceling a lock, the
6804 * canceled lock is passed in, and it won't be an unlock.
6806 fl->fl_type = F_UNLCK;
6807 if (fl->fl_flags & FL_CLOSE)
6808 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
6810 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6812 nfs_free_seqid(seqid);
6813 return ERR_PTR(-ENOMEM);
6816 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
6817 msg.rpc_argp = &data->arg;
6818 msg.rpc_resp = &data->res;
6819 task_setup_data.callback_data = data;
6820 return rpc_run_task(&task_setup_data);
6823 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6825 struct inode *inode = state->inode;
6826 struct nfs4_state_owner *sp = state->owner;
6827 struct nfs_inode *nfsi = NFS_I(inode);
6828 struct nfs_seqid *seqid;
6829 struct nfs4_lock_state *lsp;
6830 struct rpc_task *task;
6831 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6833 unsigned char fl_flags = request->fl_flags;
6835 status = nfs4_set_lock_state(state, request);
6836 /* Unlock _before_ we do the RPC call */
6837 request->fl_flags |= FL_EXISTS;
6838 /* Exclude nfs_delegation_claim_locks() */
6839 mutex_lock(&sp->so_delegreturn_mutex);
6840 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
6841 down_read(&nfsi->rwsem);
6842 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
6843 up_read(&nfsi->rwsem);
6844 mutex_unlock(&sp->so_delegreturn_mutex);
6847 up_read(&nfsi->rwsem);
6848 mutex_unlock(&sp->so_delegreturn_mutex);
6851 /* Is this a delegated lock? */
6852 lsp = request->fl_u.nfs4_fl.owner;
6853 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6855 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6856 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
6860 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
6861 status = PTR_ERR(task);
6864 status = rpc_wait_for_completion_task(task);
6867 request->fl_flags = fl_flags;
6868 trace_nfs4_unlock(request, state, F_SETLK, status);
6872 struct nfs4_lockdata {
6873 struct nfs_lock_args arg;
6874 struct nfs_lock_res res;
6875 struct nfs4_lock_state *lsp;
6876 struct nfs_open_context *ctx;
6877 struct file_lock fl;
6878 unsigned long timestamp;
6881 struct nfs_server *server;
6884 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
6885 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6888 struct nfs4_lockdata *p;
6889 struct inode *inode = lsp->ls_state->inode;
6890 struct nfs_server *server = NFS_SERVER(inode);
6891 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6893 p = kzalloc(sizeof(*p), gfp_mask);
6897 p->arg.fh = NFS_FH(inode);
6899 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
6900 if (IS_ERR(p->arg.open_seqid))
6902 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6903 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
6904 if (IS_ERR(p->arg.lock_seqid))
6905 goto out_free_seqid;
6906 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
6907 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
6908 p->arg.lock_owner.s_dev = server->s_dev;
6909 p->res.lock_seqid = p->arg.lock_seqid;
6912 p->ctx = get_nfs_open_context(ctx);
6913 locks_init_lock(&p->fl);
6914 locks_copy_lock(&p->fl, fl);
6917 nfs_free_seqid(p->arg.open_seqid);
6923 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6925 struct nfs4_lockdata *data = calldata;
6926 struct nfs4_state *state = data->lsp->ls_state;
6928 dprintk("%s: begin!\n", __func__);
6929 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
6931 /* Do we need to do an open_to_lock_owner? */
6932 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
6933 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
6934 goto out_release_lock_seqid;
6936 nfs4_stateid_copy(&data->arg.open_stateid,
6937 &state->open_stateid);
6938 data->arg.new_lock_owner = 1;
6939 data->res.open_seqid = data->arg.open_seqid;
6941 data->arg.new_lock_owner = 0;
6942 nfs4_stateid_copy(&data->arg.lock_stateid,
6943 &data->lsp->ls_stateid);
6945 if (!nfs4_valid_open_stateid(state)) {
6946 data->rpc_status = -EBADF;
6947 task->tk_action = NULL;
6948 goto out_release_open_seqid;
6950 data->timestamp = jiffies;
6951 if (nfs4_setup_sequence(data->server->nfs_client,
6952 &data->arg.seq_args,
6956 out_release_open_seqid:
6957 nfs_release_seqid(data->arg.open_seqid);
6958 out_release_lock_seqid:
6959 nfs_release_seqid(data->arg.lock_seqid);
6961 nfs4_sequence_done(task, &data->res.seq_res);
6962 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
6965 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6967 struct nfs4_lockdata *data = calldata;
6968 struct nfs4_lock_state *lsp = data->lsp;
6970 dprintk("%s: begin!\n", __func__);
6972 if (!nfs4_sequence_done(task, &data->res.seq_res))
6975 data->rpc_status = task->tk_status;
6976 switch (task->tk_status) {
6978 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
6980 if (data->arg.new_lock && !data->cancelled) {
6981 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
6982 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
6985 if (data->arg.new_lock_owner != 0) {
6986 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6987 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6988 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6989 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6992 case -NFS4ERR_BAD_STATEID:
6993 case -NFS4ERR_OLD_STATEID:
6994 case -NFS4ERR_STALE_STATEID:
6995 case -NFS4ERR_EXPIRED:
6996 if (data->arg.new_lock_owner != 0) {
6997 if (!nfs4_stateid_match(&data->arg.open_stateid,
6998 &lsp->ls_state->open_stateid))
7000 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
7005 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
7008 if (!data->cancelled)
7009 rpc_restart_call_prepare(task);
7013 static void nfs4_lock_release(void *calldata)
7015 struct nfs4_lockdata *data = calldata;
7017 dprintk("%s: begin!\n", __func__);
7018 nfs_free_seqid(data->arg.open_seqid);
7019 if (data->cancelled && data->rpc_status == 0) {
7020 struct rpc_task *task;
7021 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
7022 data->arg.lock_seqid);
7024 rpc_put_task_async(task);
7025 dprintk("%s: cancelling lock!\n", __func__);
7027 nfs_free_seqid(data->arg.lock_seqid);
7028 nfs4_put_lock_state(data->lsp);
7029 put_nfs_open_context(data->ctx);
7031 dprintk("%s: done!\n", __func__);
7034 static const struct rpc_call_ops nfs4_lock_ops = {
7035 .rpc_call_prepare = nfs4_lock_prepare,
7036 .rpc_call_done = nfs4_lock_done,
7037 .rpc_release = nfs4_lock_release,
7040 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
7043 case -NFS4ERR_ADMIN_REVOKED:
7044 case -NFS4ERR_EXPIRED:
7045 case -NFS4ERR_BAD_STATEID:
7046 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
7047 if (new_lock_owner != 0 ||
7048 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
7049 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
7051 case -NFS4ERR_STALE_STATEID:
7052 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
7053 nfs4_schedule_lease_recovery(server->nfs_client);
7057 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
7059 struct nfs4_lockdata *data;
7060 struct rpc_task *task;
7061 struct rpc_message msg = {
7062 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
7063 .rpc_cred = state->owner->so_cred,
7065 struct rpc_task_setup task_setup_data = {
7066 .rpc_client = NFS_CLIENT(state->inode),
7067 .rpc_message = &msg,
7068 .callback_ops = &nfs4_lock_ops,
7069 .workqueue = nfsiod_workqueue,
7070 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
7074 dprintk("%s: begin!\n", __func__);
7075 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
7076 fl->fl_u.nfs4_fl.owner,
7077 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
7081 data->arg.block = 1;
7082 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
7083 recovery_type > NFS_LOCK_NEW);
7084 msg.rpc_argp = &data->arg;
7085 msg.rpc_resp = &data->res;
7086 task_setup_data.callback_data = data;
7087 if (recovery_type > NFS_LOCK_NEW) {
7088 if (recovery_type == NFS_LOCK_RECLAIM)
7089 data->arg.reclaim = NFS_LOCK_RECLAIM;
7091 data->arg.new_lock = 1;
7092 task = rpc_run_task(&task_setup_data);
7094 return PTR_ERR(task);
7095 ret = rpc_wait_for_completion_task(task);
7097 ret = data->rpc_status;
7099 nfs4_handle_setlk_error(data->server, data->lsp,
7100 data->arg.new_lock_owner, ret);
7102 data->cancelled = true;
7104 dprintk("%s: done, ret = %d!\n", __func__, ret);
7105 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
7109 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
7111 struct nfs_server *server = NFS_SERVER(state->inode);
7112 struct nfs4_exception exception = {
7113 .inode = state->inode,
7118 /* Cache the lock if possible... */
7119 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7121 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
7122 if (err != -NFS4ERR_DELAY)
7124 nfs4_handle_exception(server, err, &exception);
7125 } while (exception.retry);
7129 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
7131 struct nfs_server *server = NFS_SERVER(state->inode);
7132 struct nfs4_exception exception = {
7133 .inode = state->inode,
7137 err = nfs4_set_lock_state(state, request);
7140 if (!recover_lost_locks) {
7141 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
7145 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7147 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
7151 case -NFS4ERR_GRACE:
7152 case -NFS4ERR_DELAY:
7153 nfs4_handle_exception(server, err, &exception);
7156 } while (exception.retry);
7161 #if defined(CONFIG_NFS_V4_1)
7162 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
7164 struct nfs4_lock_state *lsp;
7167 status = nfs4_set_lock_state(state, request);
7170 lsp = request->fl_u.nfs4_fl.owner;
7171 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
7172 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
7174 return nfs4_lock_expired(state, request);
7178 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7180 struct nfs_inode *nfsi = NFS_I(state->inode);
7181 struct nfs4_state_owner *sp = state->owner;
7182 unsigned char fl_flags = request->fl_flags;
7185 request->fl_flags |= FL_ACCESS;
7186 status = locks_lock_inode_wait(state->inode, request);
7189 mutex_lock(&sp->so_delegreturn_mutex);
7190 down_read(&nfsi->rwsem);
7191 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
7192 /* Yes: cache locks! */
7193 /* ...but avoid races with delegation recall... */
7194 request->fl_flags = fl_flags & ~FL_SLEEP;
7195 status = locks_lock_inode_wait(state->inode, request);
7196 up_read(&nfsi->rwsem);
7197 mutex_unlock(&sp->so_delegreturn_mutex);
7200 up_read(&nfsi->rwsem);
7201 mutex_unlock(&sp->so_delegreturn_mutex);
7202 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
7204 request->fl_flags = fl_flags;
7208 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7210 struct nfs4_exception exception = {
7212 .inode = state->inode,
7213 .interruptible = true,
7218 err = _nfs4_proc_setlk(state, cmd, request);
7219 if (err == -NFS4ERR_DENIED)
7221 err = nfs4_handle_exception(NFS_SERVER(state->inode),
7223 } while (exception.retry);
7227 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
7228 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
7231 nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
7232 struct file_lock *request)
7234 int status = -ERESTARTSYS;
7235 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
7237 while(!signalled()) {
7238 status = nfs4_proc_setlk(state, cmd, request);
7239 if ((status != -EAGAIN) || IS_SETLK(cmd))
7241 freezable_schedule_timeout_interruptible(timeout);
7243 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
7244 status = -ERESTARTSYS;
7249 #ifdef CONFIG_NFS_V4_1
7250 struct nfs4_lock_waiter {
7251 struct task_struct *task;
7252 struct inode *inode;
7253 struct nfs_lowner *owner;
7257 nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
7260 struct nfs4_lock_waiter *waiter = wait->private;
7262 /* NULL key means to wake up everyone */
7264 struct cb_notify_lock_args *cbnl = key;
7265 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
7266 *wowner = waiter->owner;
7268 /* Only wake if the callback was for the same owner. */
7269 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
7272 /* Make sure it's for the right inode */
7273 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
7277 /* override "private" so we can use default_wake_function */
7278 wait->private = waiter->task;
7279 ret = woken_wake_function(wait, mode, flags, key);
7281 list_del_init(&wait->entry);
7282 wait->private = waiter;
7287 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7289 int status = -ERESTARTSYS;
7290 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
7291 struct nfs_server *server = NFS_SERVER(state->inode);
7292 struct nfs_client *clp = server->nfs_client;
7293 wait_queue_head_t *q = &clp->cl_lock_waitq;
7294 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
7295 .id = lsp->ls_seqid.owner_id,
7296 .s_dev = server->s_dev };
7297 struct nfs4_lock_waiter waiter = { .task = current,
7298 .inode = state->inode,
7300 wait_queue_entry_t wait;
7302 /* Don't bother with waitqueue if we don't expect a callback */
7303 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
7304 return nfs4_retry_setlk_simple(state, cmd, request);
7307 wait.private = &waiter;
7308 wait.func = nfs4_wake_lock_waiter;
7310 while(!signalled()) {
7311 add_wait_queue(q, &wait);
7312 status = nfs4_proc_setlk(state, cmd, request);
7313 if ((status != -EAGAIN) || IS_SETLK(cmd)) {
7314 finish_wait(q, &wait);
7318 status = -ERESTARTSYS;
7319 freezer_do_not_count();
7320 wait_woken(&wait, TASK_INTERRUPTIBLE, NFS4_LOCK_MAXTIMEOUT);
7322 finish_wait(q, &wait);
7327 #else /* !CONFIG_NFS_V4_1 */
7329 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7331 return nfs4_retry_setlk_simple(state, cmd, request);
7336 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
7338 struct nfs_open_context *ctx;
7339 struct nfs4_state *state;
7342 /* verify open state */
7343 ctx = nfs_file_open_context(filp);
7346 if (IS_GETLK(cmd)) {
7348 return nfs4_proc_getlk(state, F_GETLK, request);
7352 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
7355 if (request->fl_type == F_UNLCK) {
7357 return nfs4_proc_unlck(state, cmd, request);
7364 if ((request->fl_flags & FL_POSIX) &&
7365 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
7369 * Don't rely on the VFS having checked the file open mode,
7370 * since it won't do this for flock() locks.
7372 switch (request->fl_type) {
7374 if (!(filp->f_mode & FMODE_READ))
7378 if (!(filp->f_mode & FMODE_WRITE))
7382 status = nfs4_set_lock_state(state, request);
7386 return nfs4_retry_setlk(state, cmd, request);
7389 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
7391 struct nfs_server *server = NFS_SERVER(state->inode);
7394 err = nfs4_set_lock_state(state, fl);
7398 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
7399 if (err != -NFS4ERR_DELAY)
7402 } while (err == -NFS4ERR_DELAY);
7403 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
7406 struct nfs_release_lockowner_data {
7407 struct nfs4_lock_state *lsp;
7408 struct nfs_server *server;
7409 struct nfs_release_lockowner_args args;
7410 struct nfs_release_lockowner_res res;
7411 unsigned long timestamp;
7414 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7416 struct nfs_release_lockowner_data *data = calldata;
7417 struct nfs_server *server = data->server;
7418 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7419 &data->res.seq_res, task);
7420 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7421 data->timestamp = jiffies;
7424 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7426 struct nfs_release_lockowner_data *data = calldata;
7427 struct nfs_server *server = data->server;
7429 nfs40_sequence_done(task, &data->res.seq_res);
7431 switch (task->tk_status) {
7433 renew_lease(server, data->timestamp);
7435 case -NFS4ERR_STALE_CLIENTID:
7436 case -NFS4ERR_EXPIRED:
7437 nfs4_schedule_lease_recovery(server->nfs_client);
7439 case -NFS4ERR_LEASE_MOVED:
7440 case -NFS4ERR_DELAY:
7441 if (nfs4_async_handle_error(task, server,
7442 NULL, NULL) == -EAGAIN)
7443 rpc_restart_call_prepare(task);
7447 static void nfs4_release_lockowner_release(void *calldata)
7449 struct nfs_release_lockowner_data *data = calldata;
7450 nfs4_free_lock_state(data->server, data->lsp);
7454 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
7455 .rpc_call_prepare = nfs4_release_lockowner_prepare,
7456 .rpc_call_done = nfs4_release_lockowner_done,
7457 .rpc_release = nfs4_release_lockowner_release,
7461 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
7463 struct nfs_release_lockowner_data *data;
7464 struct rpc_message msg = {
7465 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7468 if (server->nfs_client->cl_mvops->minor_version != 0)
7471 data = kmalloc(sizeof(*data), GFP_NOFS);
7475 data->server = server;
7476 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7477 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7478 data->args.lock_owner.s_dev = server->s_dev;
7480 msg.rpc_argp = &data->args;
7481 msg.rpc_resp = &data->res;
7482 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
7483 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
7486 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7488 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
7489 struct dentry *unused, struct inode *inode,
7490 const char *key, const void *buf,
7491 size_t buflen, int flags)
7493 return nfs4_proc_set_acl(inode, buf, buflen);
7496 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
7497 struct dentry *unused, struct inode *inode,
7498 const char *key, void *buf, size_t buflen)
7500 return nfs4_proc_get_acl(inode, buf, buflen);
7503 static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
7505 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
7508 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
7510 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
7511 struct dentry *unused, struct inode *inode,
7512 const char *key, const void *buf,
7513 size_t buflen, int flags)
7515 if (security_ismaclabel(key))
7516 return nfs4_set_security_label(inode, buf, buflen);
7521 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
7522 struct dentry *unused, struct inode *inode,
7523 const char *key, void *buf, size_t buflen)
7525 if (security_ismaclabel(key))
7526 return nfs4_get_security_label(inode, buf, buflen);
7531 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7535 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7536 len = security_inode_listsecurity(inode, list, list_len);
7537 if (len >= 0 && list_len && len > list_len)
7543 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7544 .prefix = XATTR_SECURITY_PREFIX,
7545 .get = nfs4_xattr_get_nfs4_label,
7546 .set = nfs4_xattr_set_nfs4_label,
7552 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7559 #ifdef CONFIG_NFS_V4_2
7560 static int nfs4_xattr_set_nfs4_user(const struct xattr_handler *handler,
7561 struct dentry *unused, struct inode *inode,
7562 const char *key, const void *buf,
7563 size_t buflen, int flags)
7565 struct nfs_access_entry cache;
7568 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7572 * There is no mapping from the MAY_* flags to the NFS_ACCESS_XA*
7573 * flags right now. Handling of xattr operations use the normal
7574 * file read/write permissions.
7576 * Just in case the server has other ideas (which RFC 8276 allows),
7577 * do a cached access check for the XA* flags to possibly avoid
7578 * doing an RPC and getting EACCES back.
7580 if (!nfs_access_get_cached(inode, current_cred(), &cache, true)) {
7581 if (!(cache.mask & NFS_ACCESS_XAWRITE))
7586 ret = nfs42_proc_removexattr(inode, key);
7588 nfs4_xattr_cache_remove(inode, key);
7590 ret = nfs42_proc_setxattr(inode, key, buf, buflen, flags);
7592 nfs4_xattr_cache_add(inode, key, buf, NULL, buflen);
7598 static int nfs4_xattr_get_nfs4_user(const struct xattr_handler *handler,
7599 struct dentry *unused, struct inode *inode,
7600 const char *key, void *buf, size_t buflen)
7602 struct nfs_access_entry cache;
7605 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7608 if (!nfs_access_get_cached(inode, current_cred(), &cache, true)) {
7609 if (!(cache.mask & NFS_ACCESS_XAREAD))
7613 ret = nfs_revalidate_inode(NFS_SERVER(inode), inode);
7617 ret = nfs4_xattr_cache_get(inode, key, buf, buflen);
7618 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7621 ret = nfs42_proc_getxattr(inode, key, buf, buflen);
7627 nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7634 struct nfs_access_entry cache;
7636 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7639 if (!nfs_access_get_cached(inode, current_cred(), &cache, true)) {
7640 if (!(cache.mask & NFS_ACCESS_XALIST))
7644 ret = nfs_revalidate_inode(NFS_SERVER(inode), inode);
7648 ret = nfs4_xattr_cache_list(inode, list, list_len);
7649 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7654 buflen = list_len ? list_len : XATTR_LIST_MAX;
7655 buf = list_len ? list : NULL;
7659 ret = nfs42_proc_listxattrs(inode, buf, buflen,
7672 nfs4_xattr_cache_set_list(inode, list, size);
7680 nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7684 #endif /* CONFIG_NFS_V4_2 */
7687 * nfs_fhget will use either the mounted_on_fileid or the fileid
7689 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7691 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7692 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7693 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
7694 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
7697 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
7698 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
7699 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7703 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7704 const struct qstr *name,
7705 struct nfs4_fs_locations *fs_locations,
7708 struct nfs_server *server = NFS_SERVER(dir);
7710 struct nfs4_fs_locations_arg args = {
7711 .dir_fh = NFS_FH(dir),
7716 struct nfs4_fs_locations_res res = {
7717 .fs_locations = fs_locations,
7719 struct rpc_message msg = {
7720 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7726 dprintk("%s: start\n", __func__);
7728 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7729 bitmask[1] = nfs4_fattr_bitmap[1];
7731 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7732 * is not supported */
7733 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
7734 bitmask[0] &= ~FATTR4_WORD0_FILEID;
7736 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
7738 nfs_fattr_init(&fs_locations->fattr);
7739 fs_locations->server = server;
7740 fs_locations->nlocations = 0;
7741 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
7742 dprintk("%s: returned status = %d\n", __func__, status);
7746 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7747 const struct qstr *name,
7748 struct nfs4_fs_locations *fs_locations,
7751 struct nfs4_exception exception = {
7752 .interruptible = true,
7756 err = _nfs4_proc_fs_locations(client, dir, name,
7757 fs_locations, page);
7758 trace_nfs4_get_fs_locations(dir, name, err);
7759 err = nfs4_handle_exception(NFS_SERVER(dir), err,
7761 } while (exception.retry);
7766 * This operation also signals the server that this client is
7767 * performing migration recovery. The server can stop returning
7768 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7769 * appended to this compound to identify the client ID which is
7770 * performing recovery.
7772 static int _nfs40_proc_get_locations(struct inode *inode,
7773 struct nfs4_fs_locations *locations,
7774 struct page *page, const struct cred *cred)
7776 struct nfs_server *server = NFS_SERVER(inode);
7777 struct rpc_clnt *clnt = server->client;
7779 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7781 struct nfs4_fs_locations_arg args = {
7782 .clientid = server->nfs_client->cl_clientid,
7783 .fh = NFS_FH(inode),
7786 .migration = 1, /* skip LOOKUP */
7787 .renew = 1, /* append RENEW */
7789 struct nfs4_fs_locations_res res = {
7790 .fs_locations = locations,
7794 struct rpc_message msg = {
7795 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7800 unsigned long now = jiffies;
7803 nfs_fattr_init(&locations->fattr);
7804 locations->server = server;
7805 locations->nlocations = 0;
7807 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7808 status = nfs4_call_sync_sequence(clnt, server, &msg,
7809 &args.seq_args, &res.seq_res);
7813 renew_lease(server, now);
7817 #ifdef CONFIG_NFS_V4_1
7820 * This operation also signals the server that this client is
7821 * performing migration recovery. The server can stop asserting
7822 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7823 * performing this operation is identified in the SEQUENCE
7824 * operation in this compound.
7826 * When the client supports GETATTR(fs_locations_info), it can
7827 * be plumbed in here.
7829 static int _nfs41_proc_get_locations(struct inode *inode,
7830 struct nfs4_fs_locations *locations,
7831 struct page *page, const struct cred *cred)
7833 struct nfs_server *server = NFS_SERVER(inode);
7834 struct rpc_clnt *clnt = server->client;
7836 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7838 struct nfs4_fs_locations_arg args = {
7839 .fh = NFS_FH(inode),
7842 .migration = 1, /* skip LOOKUP */
7844 struct nfs4_fs_locations_res res = {
7845 .fs_locations = locations,
7848 struct rpc_message msg = {
7849 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7856 nfs_fattr_init(&locations->fattr);
7857 locations->server = server;
7858 locations->nlocations = 0;
7860 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7861 status = nfs4_call_sync_sequence(clnt, server, &msg,
7862 &args.seq_args, &res.seq_res);
7863 if (status == NFS4_OK &&
7864 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7865 status = -NFS4ERR_LEASE_MOVED;
7869 #endif /* CONFIG_NFS_V4_1 */
7872 * nfs4_proc_get_locations - discover locations for a migrated FSID
7873 * @inode: inode on FSID that is migrating
7874 * @locations: result of query
7876 * @cred: credential to use for this operation
7878 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7879 * operation failed, or a negative errno if a local error occurred.
7881 * On success, "locations" is filled in, but if the server has
7882 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7885 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7886 * from this client that require migration recovery.
7888 int nfs4_proc_get_locations(struct inode *inode,
7889 struct nfs4_fs_locations *locations,
7890 struct page *page, const struct cred *cred)
7892 struct nfs_server *server = NFS_SERVER(inode);
7893 struct nfs_client *clp = server->nfs_client;
7894 const struct nfs4_mig_recovery_ops *ops =
7895 clp->cl_mvops->mig_recovery_ops;
7896 struct nfs4_exception exception = {
7897 .interruptible = true,
7901 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7902 (unsigned long long)server->fsid.major,
7903 (unsigned long long)server->fsid.minor,
7905 nfs_display_fhandle(NFS_FH(inode), __func__);
7908 status = ops->get_locations(inode, locations, page, cred);
7909 if (status != -NFS4ERR_DELAY)
7911 nfs4_handle_exception(server, status, &exception);
7912 } while (exception.retry);
7917 * This operation also signals the server that this client is
7918 * performing "lease moved" recovery. The server can stop
7919 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7920 * is appended to this compound to identify the client ID which is
7921 * performing recovery.
7923 static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
7925 struct nfs_server *server = NFS_SERVER(inode);
7926 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7927 struct rpc_clnt *clnt = server->client;
7928 struct nfs4_fsid_present_arg args = {
7929 .fh = NFS_FH(inode),
7930 .clientid = clp->cl_clientid,
7931 .renew = 1, /* append RENEW */
7933 struct nfs4_fsid_present_res res = {
7936 struct rpc_message msg = {
7937 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7942 unsigned long now = jiffies;
7945 res.fh = nfs_alloc_fhandle();
7949 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7950 status = nfs4_call_sync_sequence(clnt, server, &msg,
7951 &args.seq_args, &res.seq_res);
7952 nfs_free_fhandle(res.fh);
7956 do_renew_lease(clp, now);
7960 #ifdef CONFIG_NFS_V4_1
7963 * This operation also signals the server that this client is
7964 * performing "lease moved" recovery. The server can stop asserting
7965 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7966 * this operation is identified in the SEQUENCE operation in this
7969 static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
7971 struct nfs_server *server = NFS_SERVER(inode);
7972 struct rpc_clnt *clnt = server->client;
7973 struct nfs4_fsid_present_arg args = {
7974 .fh = NFS_FH(inode),
7976 struct nfs4_fsid_present_res res = {
7978 struct rpc_message msg = {
7979 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7986 res.fh = nfs_alloc_fhandle();
7990 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7991 status = nfs4_call_sync_sequence(clnt, server, &msg,
7992 &args.seq_args, &res.seq_res);
7993 nfs_free_fhandle(res.fh);
7994 if (status == NFS4_OK &&
7995 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7996 status = -NFS4ERR_LEASE_MOVED;
8000 #endif /* CONFIG_NFS_V4_1 */
8003 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
8004 * @inode: inode on FSID to check
8005 * @cred: credential to use for this operation
8007 * Server indicates whether the FSID is present, moved, or not
8008 * recognized. This operation is necessary to clear a LEASE_MOVED
8009 * condition for this client ID.
8011 * Returns NFS4_OK if the FSID is present on this server,
8012 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
8013 * NFS4ERR code if some error occurred on the server, or a
8014 * negative errno if a local failure occurred.
8016 int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
8018 struct nfs_server *server = NFS_SERVER(inode);
8019 struct nfs_client *clp = server->nfs_client;
8020 const struct nfs4_mig_recovery_ops *ops =
8021 clp->cl_mvops->mig_recovery_ops;
8022 struct nfs4_exception exception = {
8023 .interruptible = true,
8027 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
8028 (unsigned long long)server->fsid.major,
8029 (unsigned long long)server->fsid.minor,
8031 nfs_display_fhandle(NFS_FH(inode), __func__);
8034 status = ops->fsid_present(inode, cred);
8035 if (status != -NFS4ERR_DELAY)
8037 nfs4_handle_exception(server, status, &exception);
8038 } while (exception.retry);
8043 * If 'use_integrity' is true and the state managment nfs_client
8044 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
8045 * and the machine credential as per RFC3530bis and RFC5661 Security
8046 * Considerations sections. Otherwise, just use the user cred with the
8047 * filesystem's rpc_client.
8049 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
8052 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
8053 struct nfs_client *clp = NFS_SERVER(dir)->nfs_client;
8054 struct nfs4_secinfo_arg args = {
8055 .dir_fh = NFS_FH(dir),
8058 struct nfs4_secinfo_res res = {
8061 struct rpc_message msg = {
8062 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
8066 struct nfs4_call_sync_data data = {
8067 .seq_server = NFS_SERVER(dir),
8068 .seq_args = &args.seq_args,
8069 .seq_res = &res.seq_res,
8071 struct rpc_task_setup task_setup = {
8073 .rpc_message = &msg,
8074 .callback_ops = clp->cl_mvops->call_sync_ops,
8075 .callback_data = &data,
8076 .flags = RPC_TASK_NO_ROUND_ROBIN,
8078 const struct cred *cred = NULL;
8080 if (use_integrity) {
8081 clnt = clp->cl_rpcclient;
8082 task_setup.rpc_client = clnt;
8084 cred = nfs4_get_clid_cred(clp);
8085 msg.rpc_cred = cred;
8088 dprintk("NFS call secinfo %s\n", name->name);
8090 nfs4_state_protect(clp, NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
8091 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
8092 status = nfs4_call_sync_custom(&task_setup);
8094 dprintk("NFS reply secinfo: %d\n", status);
8100 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
8101 struct nfs4_secinfo_flavors *flavors)
8103 struct nfs4_exception exception = {
8104 .interruptible = true,
8108 err = -NFS4ERR_WRONGSEC;
8110 /* try to use integrity protection with machine cred */
8111 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
8112 err = _nfs4_proc_secinfo(dir, name, flavors, true);
8115 * if unable to use integrity protection, or SECINFO with
8116 * integrity protection returns NFS4ERR_WRONGSEC (which is
8117 * disallowed by spec, but exists in deployed servers) use
8118 * the current filesystem's rpc_client and the user cred.
8120 if (err == -NFS4ERR_WRONGSEC)
8121 err = _nfs4_proc_secinfo(dir, name, flavors, false);
8123 trace_nfs4_secinfo(dir, name, err);
8124 err = nfs4_handle_exception(NFS_SERVER(dir), err,
8126 } while (exception.retry);
8130 #ifdef CONFIG_NFS_V4_1
8132 * Check the exchange flags returned by the server for invalid flags, having
8133 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
8136 static int nfs4_check_cl_exchange_flags(u32 flags, u32 version)
8138 if (version >= 2 && (flags & ~EXCHGID4_2_FLAG_MASK_R))
8140 else if (version < 2 && (flags & ~EXCHGID4_FLAG_MASK_R))
8142 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
8143 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
8145 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
8149 return -NFS4ERR_INVAL;
8153 nfs41_same_server_scope(struct nfs41_server_scope *a,
8154 struct nfs41_server_scope *b)
8156 if (a->server_scope_sz != b->server_scope_sz)
8158 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
8162 nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
8164 struct nfs41_bind_conn_to_session_args *args = task->tk_msg.rpc_argp;
8165 struct nfs41_bind_conn_to_session_res *res = task->tk_msg.rpc_resp;
8166 struct nfs_client *clp = args->client;
8168 switch (task->tk_status) {
8169 case -NFS4ERR_BADSESSION:
8170 case -NFS4ERR_DEADSESSION:
8171 nfs4_schedule_session_recovery(clp->cl_session,
8174 if (args->dir == NFS4_CDFC4_FORE_OR_BOTH &&
8175 res->dir != NFS4_CDFS4_BOTH) {
8176 rpc_task_close_connection(task);
8177 if (args->retries++ < MAX_BIND_CONN_TO_SESSION_RETRIES)
8178 rpc_restart_call(task);
8182 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
8183 .rpc_call_done = nfs4_bind_one_conn_to_session_done,
8187 * nfs4_proc_bind_one_conn_to_session()
8189 * The 4.1 client currently uses the same TCP connection for the
8190 * fore and backchannel.
8193 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
8194 struct rpc_xprt *xprt,
8195 struct nfs_client *clp,
8196 const struct cred *cred)
8199 struct nfs41_bind_conn_to_session_args args = {
8201 .dir = NFS4_CDFC4_FORE_OR_BOTH,
8204 struct nfs41_bind_conn_to_session_res res;
8205 struct rpc_message msg = {
8207 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
8212 struct rpc_task_setup task_setup_data = {
8215 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
8216 .rpc_message = &msg,
8217 .flags = RPC_TASK_TIMEOUT,
8219 struct rpc_task *task;
8221 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
8222 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
8223 args.dir = NFS4_CDFC4_FORE;
8225 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
8226 if (xprt != rcu_access_pointer(clnt->cl_xprt))
8227 args.dir = NFS4_CDFC4_FORE;
8229 task = rpc_run_task(&task_setup_data);
8230 if (!IS_ERR(task)) {
8231 status = task->tk_status;
8234 status = PTR_ERR(task);
8235 trace_nfs4_bind_conn_to_session(clp, status);
8237 if (memcmp(res.sessionid.data,
8238 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
8239 dprintk("NFS: %s: Session ID mismatch\n", __func__);
8242 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
8243 dprintk("NFS: %s: Unexpected direction from server\n",
8247 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
8248 dprintk("NFS: %s: Server returned RDMA mode = true\n",
8257 struct rpc_bind_conn_calldata {
8258 struct nfs_client *clp;
8259 const struct cred *cred;
8263 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
8264 struct rpc_xprt *xprt,
8267 struct rpc_bind_conn_calldata *p = calldata;
8269 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
8272 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
8274 struct rpc_bind_conn_calldata data = {
8278 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
8279 nfs4_proc_bind_conn_to_session_callback, &data);
8283 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
8284 * and operations we'd like to see to enable certain features in the allow map
8286 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
8287 .how = SP4_MACH_CRED,
8288 .enforce.u.words = {
8289 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8290 1 << (OP_EXCHANGE_ID - 32) |
8291 1 << (OP_CREATE_SESSION - 32) |
8292 1 << (OP_DESTROY_SESSION - 32) |
8293 1 << (OP_DESTROY_CLIENTID - 32)
8296 [0] = 1 << (OP_CLOSE) |
8297 1 << (OP_OPEN_DOWNGRADE) |
8299 1 << (OP_DELEGRETURN) |
8301 [1] = 1 << (OP_SECINFO - 32) |
8302 1 << (OP_SECINFO_NO_NAME - 32) |
8303 1 << (OP_LAYOUTRETURN - 32) |
8304 1 << (OP_TEST_STATEID - 32) |
8305 1 << (OP_FREE_STATEID - 32) |
8306 1 << (OP_WRITE - 32)
8311 * Select the state protection mode for client `clp' given the server results
8312 * from exchange_id in `sp'.
8314 * Returns 0 on success, negative errno otherwise.
8316 static int nfs4_sp4_select_mode(struct nfs_client *clp,
8317 struct nfs41_state_protection *sp)
8319 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
8320 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8321 1 << (OP_EXCHANGE_ID - 32) |
8322 1 << (OP_CREATE_SESSION - 32) |
8323 1 << (OP_DESTROY_SESSION - 32) |
8324 1 << (OP_DESTROY_CLIENTID - 32)
8326 unsigned long flags = 0;
8330 if (sp->how == SP4_MACH_CRED) {
8331 /* Print state protect result */
8332 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
8333 for (i = 0; i <= LAST_NFS4_OP; i++) {
8334 if (test_bit(i, sp->enforce.u.longs))
8335 dfprintk(MOUNT, " enforce op %d\n", i);
8336 if (test_bit(i, sp->allow.u.longs))
8337 dfprintk(MOUNT, " allow op %d\n", i);
8340 /* make sure nothing is on enforce list that isn't supported */
8341 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
8342 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
8343 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
8350 * Minimal mode - state operations are allowed to use machine
8351 * credential. Note this already happens by default, so the
8352 * client doesn't have to do anything more than the negotiation.
8354 * NOTE: we don't care if EXCHANGE_ID is in the list -
8355 * we're already using the machine cred for exchange_id
8356 * and will never use a different cred.
8358 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
8359 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
8360 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
8361 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
8362 dfprintk(MOUNT, "sp4_mach_cred:\n");
8363 dfprintk(MOUNT, " minimal mode enabled\n");
8364 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
8366 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
8371 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
8372 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
8373 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
8374 test_bit(OP_LOCKU, sp->allow.u.longs)) {
8375 dfprintk(MOUNT, " cleanup mode enabled\n");
8376 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
8379 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
8380 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
8381 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
8384 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
8385 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
8386 dfprintk(MOUNT, " secinfo mode enabled\n");
8387 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
8390 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
8391 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
8392 dfprintk(MOUNT, " stateid mode enabled\n");
8393 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
8396 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
8397 dfprintk(MOUNT, " write mode enabled\n");
8398 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
8401 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
8402 dfprintk(MOUNT, " commit mode enabled\n");
8403 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
8407 clp->cl_sp4_flags = flags;
8411 struct nfs41_exchange_id_data {
8412 struct nfs41_exchange_id_res res;
8413 struct nfs41_exchange_id_args args;
8416 static void nfs4_exchange_id_release(void *data)
8418 struct nfs41_exchange_id_data *cdata =
8419 (struct nfs41_exchange_id_data *)data;
8421 nfs_put_client(cdata->args.client);
8422 kfree(cdata->res.impl_id);
8423 kfree(cdata->res.server_scope);
8424 kfree(cdata->res.server_owner);
8428 static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
8429 .rpc_release = nfs4_exchange_id_release,
8433 * _nfs4_proc_exchange_id()
8435 * Wrapper for EXCHANGE_ID operation.
8437 static struct rpc_task *
8438 nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
8439 u32 sp4_how, struct rpc_xprt *xprt)
8441 struct rpc_message msg = {
8442 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
8445 struct rpc_task_setup task_setup_data = {
8446 .rpc_client = clp->cl_rpcclient,
8447 .callback_ops = &nfs4_exchange_id_call_ops,
8448 .rpc_message = &msg,
8449 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
8451 struct nfs41_exchange_id_data *calldata;
8454 if (!refcount_inc_not_zero(&clp->cl_count))
8455 return ERR_PTR(-EIO);
8458 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8462 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
8464 status = nfs4_init_uniform_client_string(clp);
8468 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
8471 if (unlikely(calldata->res.server_owner == NULL))
8474 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
8476 if (unlikely(calldata->res.server_scope == NULL))
8477 goto out_server_owner;
8479 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
8480 if (unlikely(calldata->res.impl_id == NULL))
8481 goto out_server_scope;
8485 calldata->args.state_protect.how = SP4_NONE;
8489 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
8499 task_setup_data.rpc_xprt = xprt;
8500 task_setup_data.flags |= RPC_TASK_SOFTCONN;
8501 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
8502 sizeof(calldata->args.verifier.data));
8504 calldata->args.client = clp;
8505 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
8506 EXCHGID4_FLAG_BIND_PRINC_STATEID;
8507 #ifdef CONFIG_NFS_V4_1_MIGRATION
8508 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
8510 msg.rpc_argp = &calldata->args;
8511 msg.rpc_resp = &calldata->res;
8512 task_setup_data.callback_data = calldata;
8514 return rpc_run_task(&task_setup_data);
8517 kfree(calldata->res.impl_id);
8519 kfree(calldata->res.server_scope);
8521 kfree(calldata->res.server_owner);
8525 nfs_put_client(clp);
8526 return ERR_PTR(status);
8530 * _nfs4_proc_exchange_id()
8532 * Wrapper for EXCHANGE_ID operation.
8534 static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
8537 struct rpc_task *task;
8538 struct nfs41_exchange_id_args *argp;
8539 struct nfs41_exchange_id_res *resp;
8540 unsigned long now = jiffies;
8543 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
8545 return PTR_ERR(task);
8547 argp = task->tk_msg.rpc_argp;
8548 resp = task->tk_msg.rpc_resp;
8549 status = task->tk_status;
8553 status = nfs4_check_cl_exchange_flags(resp->flags,
8554 clp->cl_mvops->minor_version);
8558 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8562 do_renew_lease(clp, now);
8564 clp->cl_clientid = resp->clientid;
8565 clp->cl_exchange_flags = resp->flags;
8566 clp->cl_seqid = resp->seqid;
8567 /* Client ID is not confirmed */
8568 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8569 clear_bit(NFS4_SESSION_ESTABLISHED,
8570 &clp->cl_session->session_state);
8572 if (clp->cl_serverscope != NULL &&
8573 !nfs41_same_server_scope(clp->cl_serverscope,
8574 resp->server_scope)) {
8575 dprintk("%s: server_scope mismatch detected\n",
8577 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8580 swap(clp->cl_serverowner, resp->server_owner);
8581 swap(clp->cl_serverscope, resp->server_scope);
8582 swap(clp->cl_implid, resp->impl_id);
8584 /* Save the EXCHANGE_ID verifier session trunk tests */
8585 memcpy(clp->cl_confirm.data, argp->verifier.data,
8586 sizeof(clp->cl_confirm.data));
8588 trace_nfs4_exchange_id(clp, status);
8594 * nfs4_proc_exchange_id()
8596 * Returns zero, a negative errno, or a negative NFS4ERR status code.
8598 * Since the clientid has expired, all compounds using sessions
8599 * associated with the stale clientid will be returning
8600 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
8601 * be in some phase of session reset.
8603 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
8605 int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
8607 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8610 /* try SP4_MACH_CRED if krb5i/p */
8611 if (authflavor == RPC_AUTH_GSS_KRB5I ||
8612 authflavor == RPC_AUTH_GSS_KRB5P) {
8613 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
8619 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
8623 * nfs4_test_session_trunk
8625 * This is an add_xprt_test() test function called from
8626 * rpc_clnt_setup_test_and_add_xprt.
8628 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8629 * and is dereferrenced in nfs4_exchange_id_release
8631 * Upon success, add the new transport to the rpc_clnt
8633 * @clnt: struct rpc_clnt to get new transport
8634 * @xprt: the rpc_xprt to test
8635 * @data: call data for _nfs4_proc_exchange_id.
8637 void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
8640 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
8641 struct rpc_task *task;
8646 dprintk("--> %s try %s\n", __func__,
8647 xprt->address_strings[RPC_DISPLAY_ADDR]);
8649 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8651 /* Test connection for session trunking. Async exchange_id call */
8652 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8656 status = task->tk_status;
8658 status = nfs4_detect_session_trunking(adata->clp,
8659 task->tk_msg.rpc_resp, xprt);
8662 rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
8666 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
8668 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
8669 const struct cred *cred)
8671 struct rpc_message msg = {
8672 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8678 status = rpc_call_sync(clp->cl_rpcclient, &msg,
8679 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
8680 trace_nfs4_destroy_clientid(clp, status);
8682 dprintk("NFS: Got error %d from the server %s on "
8683 "DESTROY_CLIENTID.", status, clp->cl_hostname);
8687 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
8688 const struct cred *cred)
8693 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8694 ret = _nfs4_proc_destroy_clientid(clp, cred);
8696 case -NFS4ERR_DELAY:
8697 case -NFS4ERR_CLIENTID_BUSY:
8707 int nfs4_destroy_clientid(struct nfs_client *clp)
8709 const struct cred *cred;
8712 if (clp->cl_mvops->minor_version < 1)
8714 if (clp->cl_exchange_flags == 0)
8716 if (clp->cl_preserve_clid)
8718 cred = nfs4_get_clid_cred(clp);
8719 ret = nfs4_proc_destroy_clientid(clp, cred);
8723 case -NFS4ERR_STALE_CLIENTID:
8724 clp->cl_exchange_flags = 0;
8730 #endif /* CONFIG_NFS_V4_1 */
8732 struct nfs4_get_lease_time_data {
8733 struct nfs4_get_lease_time_args *args;
8734 struct nfs4_get_lease_time_res *res;
8735 struct nfs_client *clp;
8738 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
8741 struct nfs4_get_lease_time_data *data =
8742 (struct nfs4_get_lease_time_data *)calldata;
8744 dprintk("--> %s\n", __func__);
8745 /* just setup sequence, do not trigger session recovery
8746 since we're invoked within one */
8747 nfs4_setup_sequence(data->clp,
8748 &data->args->la_seq_args,
8749 &data->res->lr_seq_res,
8751 dprintk("<-- %s\n", __func__);
8755 * Called from nfs4_state_manager thread for session setup, so don't recover
8756 * from sequence operation or clientid errors.
8758 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8760 struct nfs4_get_lease_time_data *data =
8761 (struct nfs4_get_lease_time_data *)calldata;
8763 dprintk("--> %s\n", __func__);
8764 if (!nfs4_sequence_done(task, &data->res->lr_seq_res))
8766 switch (task->tk_status) {
8767 case -NFS4ERR_DELAY:
8768 case -NFS4ERR_GRACE:
8769 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
8770 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8771 task->tk_status = 0;
8773 case -NFS4ERR_RETRY_UNCACHED_REP:
8774 rpc_restart_call_prepare(task);
8777 dprintk("<-- %s\n", __func__);
8780 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
8781 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8782 .rpc_call_done = nfs4_get_lease_time_done,
8785 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8787 struct nfs4_get_lease_time_args args;
8788 struct nfs4_get_lease_time_res res = {
8789 .lr_fsinfo = fsinfo,
8791 struct nfs4_get_lease_time_data data = {
8796 struct rpc_message msg = {
8797 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8801 struct rpc_task_setup task_setup = {
8802 .rpc_client = clp->cl_rpcclient,
8803 .rpc_message = &msg,
8804 .callback_ops = &nfs4_get_lease_time_ops,
8805 .callback_data = &data,
8806 .flags = RPC_TASK_TIMEOUT,
8809 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
8810 return nfs4_call_sync_custom(&task_setup);
8813 #ifdef CONFIG_NFS_V4_1
8816 * Initialize the values to be used by the client in CREATE_SESSION
8817 * If nfs4_init_session set the fore channel request and response sizes,
8820 * Set the back channel max_resp_sz_cached to zero to force the client to
8821 * always set csa_cachethis to FALSE because the current implementation
8822 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8824 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8825 struct rpc_clnt *clnt)
8827 unsigned int max_rqst_sz, max_resp_sz;
8828 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
8829 unsigned int max_bc_slots = rpc_num_bc_slots(clnt);
8831 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8832 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8834 /* Fore channel attributes */
8835 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8836 args->fc_attrs.max_resp_sz = max_resp_sz;
8837 args->fc_attrs.max_ops = NFS4_MAX_OPS;
8838 args->fc_attrs.max_reqs = max_session_slots;
8840 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
8841 "max_ops=%u max_reqs=%u\n",
8843 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
8844 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
8846 /* Back channel attributes */
8847 args->bc_attrs.max_rqst_sz = max_bc_payload;
8848 args->bc_attrs.max_resp_sz = max_bc_payload;
8849 args->bc_attrs.max_resp_sz_cached = 0;
8850 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
8851 args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
8852 if (args->bc_attrs.max_reqs > max_bc_slots)
8853 args->bc_attrs.max_reqs = max_bc_slots;
8855 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8856 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8858 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8859 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8860 args->bc_attrs.max_reqs);
8863 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8864 struct nfs41_create_session_res *res)
8866 struct nfs4_channel_attrs *sent = &args->fc_attrs;
8867 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
8869 if (rcvd->max_resp_sz > sent->max_resp_sz)
8872 * Our requested max_ops is the minimum we need; we're not
8873 * prepared to break up compounds into smaller pieces than that.
8874 * So, no point even trying to continue if the server won't
8877 if (rcvd->max_ops < sent->max_ops)
8879 if (rcvd->max_reqs == 0)
8881 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8882 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
8886 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8887 struct nfs41_create_session_res *res)
8889 struct nfs4_channel_attrs *sent = &args->bc_attrs;
8890 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
8892 if (!(res->flags & SESSION4_BACK_CHAN))
8894 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8896 if (rcvd->max_resp_sz < sent->max_resp_sz)
8898 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8900 if (rcvd->max_ops > sent->max_ops)
8902 if (rcvd->max_reqs > sent->max_reqs)
8908 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
8909 struct nfs41_create_session_res *res)
8913 ret = nfs4_verify_fore_channel_attrs(args, res);
8916 return nfs4_verify_back_channel_attrs(args, res);
8919 static void nfs4_update_session(struct nfs4_session *session,
8920 struct nfs41_create_session_res *res)
8922 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
8923 /* Mark client id and session as being confirmed */
8924 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8925 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
8926 session->flags = res->flags;
8927 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
8928 if (res->flags & SESSION4_BACK_CHAN)
8929 memcpy(&session->bc_attrs, &res->bc_attrs,
8930 sizeof(session->bc_attrs));
8933 static int _nfs4_proc_create_session(struct nfs_client *clp,
8934 const struct cred *cred)
8936 struct nfs4_session *session = clp->cl_session;
8937 struct nfs41_create_session_args args = {
8939 .clientid = clp->cl_clientid,
8940 .seqid = clp->cl_seqid,
8941 .cb_program = NFS4_CALLBACK,
8943 struct nfs41_create_session_res res;
8945 struct rpc_message msg = {
8946 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8953 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
8954 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
8956 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
8957 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
8958 trace_nfs4_create_session(clp, status);
8961 case -NFS4ERR_STALE_CLIENTID:
8962 case -NFS4ERR_DELAY:
8971 /* Verify the session's negotiated channel_attrs values */
8972 status = nfs4_verify_channel_attrs(&args, &res);
8973 /* Increment the clientid slot sequence id */
8976 nfs4_update_session(session, &res);
8983 * Issues a CREATE_SESSION operation to the server.
8984 * It is the responsibility of the caller to verify the session is
8985 * expired before calling this routine.
8987 int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
8991 struct nfs4_session *session = clp->cl_session;
8993 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8995 status = _nfs4_proc_create_session(clp, cred);
8999 /* Init or reset the session slot tables */
9000 status = nfs4_setup_session_slot_tables(session);
9001 dprintk("slot table setup returned %d\n", status);
9005 ptr = (unsigned *)&session->sess_id.data[0];
9006 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
9007 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
9009 dprintk("<-- %s\n", __func__);
9014 * Issue the over-the-wire RPC DESTROY_SESSION.
9015 * The caller must serialize access to this routine.
9017 int nfs4_proc_destroy_session(struct nfs4_session *session,
9018 const struct cred *cred)
9020 struct rpc_message msg = {
9021 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
9022 .rpc_argp = session,
9027 dprintk("--> nfs4_proc_destroy_session\n");
9029 /* session is still being setup */
9030 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
9033 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
9034 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
9035 trace_nfs4_destroy_session(session->clp, status);
9038 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
9039 "Session has been destroyed regardless...\n", status);
9041 dprintk("<-- nfs4_proc_destroy_session\n");
9046 * Renew the cl_session lease.
9048 struct nfs4_sequence_data {
9049 struct nfs_client *clp;
9050 struct nfs4_sequence_args args;
9051 struct nfs4_sequence_res res;
9054 static void nfs41_sequence_release(void *data)
9056 struct nfs4_sequence_data *calldata = data;
9057 struct nfs_client *clp = calldata->clp;
9059 if (refcount_read(&clp->cl_count) > 1)
9060 nfs4_schedule_state_renewal(clp);
9061 nfs_put_client(clp);
9065 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9067 switch(task->tk_status) {
9068 case -NFS4ERR_DELAY:
9069 rpc_delay(task, NFS4_POLL_RETRY_MAX);
9072 nfs4_schedule_lease_recovery(clp);
9077 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
9079 struct nfs4_sequence_data *calldata = data;
9080 struct nfs_client *clp = calldata->clp;
9082 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
9085 trace_nfs4_sequence(clp, task->tk_status);
9086 if (task->tk_status < 0) {
9087 dprintk("%s ERROR %d\n", __func__, task->tk_status);
9088 if (refcount_read(&clp->cl_count) == 1)
9091 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
9092 rpc_restart_call_prepare(task);
9096 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
9098 dprintk("<-- %s\n", __func__);
9101 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
9103 struct nfs4_sequence_data *calldata = data;
9104 struct nfs_client *clp = calldata->clp;
9105 struct nfs4_sequence_args *args;
9106 struct nfs4_sequence_res *res;
9108 args = task->tk_msg.rpc_argp;
9109 res = task->tk_msg.rpc_resp;
9111 nfs4_setup_sequence(clp, args, res, task);
9114 static const struct rpc_call_ops nfs41_sequence_ops = {
9115 .rpc_call_done = nfs41_sequence_call_done,
9116 .rpc_call_prepare = nfs41_sequence_prepare,
9117 .rpc_release = nfs41_sequence_release,
9120 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
9121 const struct cred *cred,
9122 struct nfs4_slot *slot,
9125 struct nfs4_sequence_data *calldata;
9126 struct rpc_message msg = {
9127 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
9130 struct rpc_task_setup task_setup_data = {
9131 .rpc_client = clp->cl_rpcclient,
9132 .rpc_message = &msg,
9133 .callback_ops = &nfs41_sequence_ops,
9134 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
9136 struct rpc_task *ret;
9138 ret = ERR_PTR(-EIO);
9139 if (!refcount_inc_not_zero(&clp->cl_count))
9142 ret = ERR_PTR(-ENOMEM);
9143 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
9144 if (calldata == NULL)
9146 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
9147 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
9148 msg.rpc_argp = &calldata->args;
9149 msg.rpc_resp = &calldata->res;
9150 calldata->clp = clp;
9151 task_setup_data.callback_data = calldata;
9153 ret = rpc_run_task(&task_setup_data);
9158 nfs_put_client(clp);
9160 nfs41_release_slot(slot);
9164 static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
9166 struct rpc_task *task;
9169 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
9171 task = _nfs41_proc_sequence(clp, cred, NULL, false);
9173 ret = PTR_ERR(task);
9175 rpc_put_task_async(task);
9176 dprintk("<-- %s status=%d\n", __func__, ret);
9180 static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
9182 struct rpc_task *task;
9185 task = _nfs41_proc_sequence(clp, cred, NULL, true);
9187 ret = PTR_ERR(task);
9190 ret = rpc_wait_for_completion_task(task);
9192 ret = task->tk_status;
9195 dprintk("<-- %s status=%d\n", __func__, ret);
9199 struct nfs4_reclaim_complete_data {
9200 struct nfs_client *clp;
9201 struct nfs41_reclaim_complete_args arg;
9202 struct nfs41_reclaim_complete_res res;
9205 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
9207 struct nfs4_reclaim_complete_data *calldata = data;
9209 nfs4_setup_sequence(calldata->clp,
9210 &calldata->arg.seq_args,
9211 &calldata->res.seq_res,
9215 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9217 switch(task->tk_status) {
9219 wake_up_all(&clp->cl_lock_waitq);
9221 case -NFS4ERR_COMPLETE_ALREADY:
9222 case -NFS4ERR_WRONG_CRED: /* What to do here? */
9224 case -NFS4ERR_DELAY:
9225 rpc_delay(task, NFS4_POLL_RETRY_MAX);
9227 case -NFS4ERR_RETRY_UNCACHED_REP:
9229 case -NFS4ERR_BADSESSION:
9230 case -NFS4ERR_DEADSESSION:
9231 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9234 nfs4_schedule_lease_recovery(clp);
9239 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
9241 struct nfs4_reclaim_complete_data *calldata = data;
9242 struct nfs_client *clp = calldata->clp;
9243 struct nfs4_sequence_res *res = &calldata->res.seq_res;
9245 dprintk("--> %s\n", __func__);
9246 if (!nfs41_sequence_done(task, res))
9249 trace_nfs4_reclaim_complete(clp, task->tk_status);
9250 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
9251 rpc_restart_call_prepare(task);
9254 dprintk("<-- %s\n", __func__);
9257 static void nfs4_free_reclaim_complete_data(void *data)
9259 struct nfs4_reclaim_complete_data *calldata = data;
9264 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
9265 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
9266 .rpc_call_done = nfs4_reclaim_complete_done,
9267 .rpc_release = nfs4_free_reclaim_complete_data,
9271 * Issue a global reclaim complete.
9273 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
9274 const struct cred *cred)
9276 struct nfs4_reclaim_complete_data *calldata;
9277 struct rpc_message msg = {
9278 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
9281 struct rpc_task_setup task_setup_data = {
9282 .rpc_client = clp->cl_rpcclient,
9283 .rpc_message = &msg,
9284 .callback_ops = &nfs4_reclaim_complete_call_ops,
9285 .flags = RPC_TASK_NO_ROUND_ROBIN,
9287 int status = -ENOMEM;
9289 dprintk("--> %s\n", __func__);
9290 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
9291 if (calldata == NULL)
9293 calldata->clp = clp;
9294 calldata->arg.one_fs = 0;
9296 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
9297 msg.rpc_argp = &calldata->arg;
9298 msg.rpc_resp = &calldata->res;
9299 task_setup_data.callback_data = calldata;
9300 status = nfs4_call_sync_custom(&task_setup_data);
9302 dprintk("<-- %s status=%d\n", __func__, status);
9307 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
9309 struct nfs4_layoutget *lgp = calldata;
9310 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
9312 dprintk("--> %s\n", __func__);
9313 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
9314 &lgp->res.seq_res, task);
9315 dprintk("<-- %s\n", __func__);
9318 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
9320 struct nfs4_layoutget *lgp = calldata;
9322 dprintk("--> %s\n", __func__);
9323 nfs41_sequence_process(task, &lgp->res.seq_res);
9324 dprintk("<-- %s\n", __func__);
9328 nfs4_layoutget_handle_exception(struct rpc_task *task,
9329 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
9331 struct inode *inode = lgp->args.inode;
9332 struct nfs_server *server = NFS_SERVER(inode);
9333 struct pnfs_layout_hdr *lo;
9334 int nfs4err = task->tk_status;
9335 int err, status = 0;
9338 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
9340 nfs4_sequence_free_slot(&lgp->res.seq_res);
9347 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
9348 * on the file. set tk_status to -ENODATA to tell upper layer to
9351 case -NFS4ERR_LAYOUTUNAVAILABLE:
9355 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
9356 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
9358 case -NFS4ERR_BADLAYOUT:
9359 status = -EOVERFLOW;
9362 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
9363 * (or clients) writing to the same RAID stripe except when
9364 * the minlength argument is 0 (see RFC5661 section 18.43.3).
9366 * Treat it like we would RECALLCONFLICT -- we retry for a little
9367 * while, and then eventually give up.
9369 case -NFS4ERR_LAYOUTTRYLATER:
9370 if (lgp->args.minlength == 0) {
9371 status = -EOVERFLOW;
9376 case -NFS4ERR_RECALLCONFLICT:
9377 status = -ERECALLCONFLICT;
9379 case -NFS4ERR_DELEG_REVOKED:
9380 case -NFS4ERR_ADMIN_REVOKED:
9381 case -NFS4ERR_EXPIRED:
9382 case -NFS4ERR_BAD_STATEID:
9383 exception->timeout = 0;
9384 spin_lock(&inode->i_lock);
9385 lo = NFS_I(inode)->layout;
9386 /* If the open stateid was bad, then recover it. */
9387 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
9388 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
9389 spin_unlock(&inode->i_lock);
9390 exception->state = lgp->args.ctx->state;
9391 exception->stateid = &lgp->args.stateid;
9396 * Mark the bad layout state as invalid, then retry
9398 pnfs_mark_layout_stateid_invalid(lo, &head);
9399 spin_unlock(&inode->i_lock);
9400 nfs_commit_inode(inode, 0);
9401 pnfs_free_lseg_list(&head);
9406 err = nfs4_handle_exception(server, nfs4err, exception);
9408 if (exception->retry)
9414 dprintk("<-- %s\n", __func__);
9418 size_t max_response_pages(struct nfs_server *server)
9420 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
9421 return nfs_page_array_len(0, max_resp_sz);
9424 static void nfs4_layoutget_release(void *calldata)
9426 struct nfs4_layoutget *lgp = calldata;
9428 dprintk("--> %s\n", __func__);
9429 nfs4_sequence_free_slot(&lgp->res.seq_res);
9430 pnfs_layoutget_free(lgp);
9431 dprintk("<-- %s\n", __func__);
9434 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
9435 .rpc_call_prepare = nfs4_layoutget_prepare,
9436 .rpc_call_done = nfs4_layoutget_done,
9437 .rpc_release = nfs4_layoutget_release,
9440 struct pnfs_layout_segment *
9441 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
9443 struct inode *inode = lgp->args.inode;
9444 struct nfs_server *server = NFS_SERVER(inode);
9445 struct rpc_task *task;
9446 struct rpc_message msg = {
9447 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
9448 .rpc_argp = &lgp->args,
9449 .rpc_resp = &lgp->res,
9450 .rpc_cred = lgp->cred,
9452 struct rpc_task_setup task_setup_data = {
9453 .rpc_client = server->client,
9454 .rpc_message = &msg,
9455 .callback_ops = &nfs4_layoutget_call_ops,
9456 .callback_data = lgp,
9457 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
9459 struct pnfs_layout_segment *lseg = NULL;
9460 struct nfs4_exception exception = {
9462 .timeout = *timeout,
9466 dprintk("--> %s\n", __func__);
9468 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
9469 pnfs_get_layout_hdr(NFS_I(inode)->layout);
9471 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
9473 task = rpc_run_task(&task_setup_data);
9475 status = rpc_wait_for_completion_task(task);
9479 if (task->tk_status < 0) {
9480 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
9481 *timeout = exception.timeout;
9482 } else if (lgp->res.layoutp->len == 0) {
9484 *timeout = nfs4_update_delay(&exception.timeout);
9486 lseg = pnfs_layout_process(lgp);
9488 trace_nfs4_layoutget(lgp->args.ctx,
9495 dprintk("<-- %s status=%d\n", __func__, status);
9497 return ERR_PTR(status);
9502 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
9504 struct nfs4_layoutreturn *lrp = calldata;
9506 dprintk("--> %s\n", __func__);
9507 nfs4_setup_sequence(lrp->clp,
9508 &lrp->args.seq_args,
9511 if (!pnfs_layout_is_valid(lrp->args.layout))
9515 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
9517 struct nfs4_layoutreturn *lrp = calldata;
9518 struct nfs_server *server;
9520 dprintk("--> %s\n", __func__);
9522 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
9526 * Was there an RPC level error? Assume the call succeeded,
9527 * and that we need to release the layout
9529 if (task->tk_rpc_status != 0 && RPC_WAS_SENT(task)) {
9530 lrp->res.lrs_present = 0;
9534 server = NFS_SERVER(lrp->args.inode);
9535 switch (task->tk_status) {
9536 case -NFS4ERR_OLD_STATEID:
9537 if (nfs4_layout_refresh_old_stateid(&lrp->args.stateid,
9543 task->tk_status = 0;
9547 case -NFS4ERR_DELAY:
9548 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
9552 dprintk("<-- %s\n", __func__);
9555 task->tk_status = 0;
9556 nfs4_sequence_free_slot(&lrp->res.seq_res);
9557 rpc_restart_call_prepare(task);
9560 static void nfs4_layoutreturn_release(void *calldata)
9562 struct nfs4_layoutreturn *lrp = calldata;
9563 struct pnfs_layout_hdr *lo = lrp->args.layout;
9565 dprintk("--> %s\n", __func__);
9566 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
9567 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
9568 nfs4_sequence_free_slot(&lrp->res.seq_res);
9569 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9570 lrp->ld_private.ops->free(&lrp->ld_private);
9571 pnfs_put_layout_hdr(lrp->args.layout);
9572 nfs_iput_and_deactive(lrp->inode);
9573 put_cred(lrp->cred);
9575 dprintk("<-- %s\n", __func__);
9578 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9579 .rpc_call_prepare = nfs4_layoutreturn_prepare,
9580 .rpc_call_done = nfs4_layoutreturn_done,
9581 .rpc_release = nfs4_layoutreturn_release,
9584 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
9586 struct rpc_task *task;
9587 struct rpc_message msg = {
9588 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9589 .rpc_argp = &lrp->args,
9590 .rpc_resp = &lrp->res,
9591 .rpc_cred = lrp->cred,
9593 struct rpc_task_setup task_setup_data = {
9594 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
9595 .rpc_message = &msg,
9596 .callback_ops = &nfs4_layoutreturn_call_ops,
9597 .callback_data = lrp,
9601 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9602 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9603 &task_setup_data.rpc_client, &msg);
9605 dprintk("--> %s\n", __func__);
9607 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
9609 nfs4_layoutreturn_release(lrp);
9612 task_setup_data.flags |= RPC_TASK_ASYNC;
9614 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1, 0);
9615 task = rpc_run_task(&task_setup_data);
9617 return PTR_ERR(task);
9619 status = task->tk_status;
9620 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
9621 dprintk("<-- %s status=%d\n", __func__, status);
9627 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
9628 struct pnfs_device *pdev,
9629 const struct cred *cred)
9631 struct nfs4_getdeviceinfo_args args = {
9633 .notify_types = NOTIFY_DEVICEID4_CHANGE |
9634 NOTIFY_DEVICEID4_DELETE,
9636 struct nfs4_getdeviceinfo_res res = {
9639 struct rpc_message msg = {
9640 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9647 dprintk("--> %s\n", __func__);
9648 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
9649 if (res.notification & ~args.notify_types)
9650 dprintk("%s: unsupported notification\n", __func__);
9651 if (res.notification != args.notify_types)
9654 dprintk("<-- %s status=%d\n", __func__, status);
9659 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9660 struct pnfs_device *pdev,
9661 const struct cred *cred)
9663 struct nfs4_exception exception = { };
9667 err = nfs4_handle_exception(server,
9668 _nfs4_proc_getdeviceinfo(server, pdev, cred),
9670 } while (exception.retry);
9673 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9675 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9677 struct nfs4_layoutcommit_data *data = calldata;
9678 struct nfs_server *server = NFS_SERVER(data->args.inode);
9680 nfs4_setup_sequence(server->nfs_client,
9681 &data->args.seq_args,
9687 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9689 struct nfs4_layoutcommit_data *data = calldata;
9690 struct nfs_server *server = NFS_SERVER(data->args.inode);
9692 if (!nfs41_sequence_done(task, &data->res.seq_res))
9695 switch (task->tk_status) { /* Just ignore these failures */
9696 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9697 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9698 case -NFS4ERR_BADLAYOUT: /* no layout */
9699 case -NFS4ERR_GRACE: /* loca_recalim always false */
9700 task->tk_status = 0;
9704 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
9705 rpc_restart_call_prepare(task);
9711 static void nfs4_layoutcommit_release(void *calldata)
9713 struct nfs4_layoutcommit_data *data = calldata;
9715 pnfs_cleanup_layoutcommit(data);
9716 nfs_post_op_update_inode_force_wcc(data->args.inode,
9718 put_cred(data->cred);
9719 nfs_iput_and_deactive(data->inode);
9723 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9724 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9725 .rpc_call_done = nfs4_layoutcommit_done,
9726 .rpc_release = nfs4_layoutcommit_release,
9730 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
9732 struct rpc_message msg = {
9733 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9734 .rpc_argp = &data->args,
9735 .rpc_resp = &data->res,
9736 .rpc_cred = data->cred,
9738 struct rpc_task_setup task_setup_data = {
9739 .task = &data->task,
9740 .rpc_client = NFS_CLIENT(data->args.inode),
9741 .rpc_message = &msg,
9742 .callback_ops = &nfs4_layoutcommit_ops,
9743 .callback_data = data,
9745 struct rpc_task *task;
9748 dprintk("NFS: initiating layoutcommit call. sync %d "
9749 "lbw: %llu inode %lu\n", sync,
9750 data->args.lastbytewritten,
9751 data->args.inode->i_ino);
9754 data->inode = nfs_igrab_and_active(data->args.inode);
9755 if (data->inode == NULL) {
9756 nfs4_layoutcommit_release(data);
9759 task_setup_data.flags = RPC_TASK_ASYNC;
9761 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
9762 task = rpc_run_task(&task_setup_data);
9764 return PTR_ERR(task);
9766 status = task->tk_status;
9767 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
9768 dprintk("%s: status %d\n", __func__, status);
9774 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9775 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9778 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9779 struct nfs_fsinfo *info,
9780 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
9782 struct nfs41_secinfo_no_name_args args = {
9783 .style = SECINFO_STYLE_CURRENT_FH,
9785 struct nfs4_secinfo_res res = {
9788 struct rpc_message msg = {
9789 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9793 struct nfs4_call_sync_data data = {
9794 .seq_server = server,
9795 .seq_args = &args.seq_args,
9796 .seq_res = &res.seq_res,
9798 struct rpc_task_setup task_setup = {
9799 .rpc_client = server->client,
9800 .rpc_message = &msg,
9801 .callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
9802 .callback_data = &data,
9803 .flags = RPC_TASK_NO_ROUND_ROBIN,
9805 const struct cred *cred = NULL;
9808 if (use_integrity) {
9809 task_setup.rpc_client = server->nfs_client->cl_rpcclient;
9811 cred = nfs4_get_clid_cred(server->nfs_client);
9812 msg.rpc_cred = cred;
9815 dprintk("--> %s\n", __func__);
9816 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
9817 status = nfs4_call_sync_custom(&task_setup);
9818 dprintk("<-- %s status=%d\n", __func__, status);
9826 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9827 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9829 struct nfs4_exception exception = {
9830 .interruptible = true,
9834 /* first try using integrity protection */
9835 err = -NFS4ERR_WRONGSEC;
9837 /* try to use integrity protection with machine cred */
9838 if (_nfs4_is_integrity_protected(server->nfs_client))
9839 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9843 * if unable to use integrity protection, or SECINFO with
9844 * integrity protection returns NFS4ERR_WRONGSEC (which is
9845 * disallowed by spec, but exists in deployed servers) use
9846 * the current filesystem's rpc_client and the user cred.
9848 if (err == -NFS4ERR_WRONGSEC)
9849 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9854 case -NFS4ERR_WRONGSEC:
9858 err = nfs4_handle_exception(server, err, &exception);
9860 } while (exception.retry);
9866 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9867 struct nfs_fsinfo *info)
9871 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
9872 struct nfs4_secinfo_flavors *flavors;
9873 struct nfs4_secinfo4 *secinfo;
9876 page = alloc_page(GFP_KERNEL);
9882 flavors = page_address(page);
9883 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9886 * Fall back on "guess and check" method if
9887 * the server doesn't support SECINFO_NO_NAME
9889 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
9890 err = nfs4_find_root_sec(server, fhandle, info);
9896 for (i = 0; i < flavors->num_flavors; i++) {
9897 secinfo = &flavors->flavors[i];
9899 switch (secinfo->flavor) {
9903 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9904 &secinfo->flavor_info);
9907 flavor = RPC_AUTH_MAXFLAVOR;
9911 if (!nfs_auth_info_match(&server->auth_info, flavor))
9912 flavor = RPC_AUTH_MAXFLAVOR;
9914 if (flavor != RPC_AUTH_MAXFLAVOR) {
9915 err = nfs4_lookup_root_sec(server, fhandle,
9922 if (flavor == RPC_AUTH_MAXFLAVOR)
9933 static int _nfs41_test_stateid(struct nfs_server *server,
9934 nfs4_stateid *stateid,
9935 const struct cred *cred)
9938 struct nfs41_test_stateid_args args = {
9941 struct nfs41_test_stateid_res res;
9942 struct rpc_message msg = {
9943 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9948 struct rpc_clnt *rpc_client = server->client;
9950 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9953 dprintk("NFS call test_stateid %p\n", stateid);
9954 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
9955 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
9956 &args.seq_args, &res.seq_res);
9957 if (status != NFS_OK) {
9958 dprintk("NFS reply test_stateid: failed, %d\n", status);
9961 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
9965 static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9966 int err, struct nfs4_exception *exception)
9968 exception->retry = 0;
9970 case -NFS4ERR_DELAY:
9971 case -NFS4ERR_RETRY_UNCACHED_REP:
9972 nfs4_handle_exception(server, err, exception);
9974 case -NFS4ERR_BADSESSION:
9975 case -NFS4ERR_BADSLOT:
9976 case -NFS4ERR_BAD_HIGH_SLOT:
9977 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9978 case -NFS4ERR_DEADSESSION:
9979 nfs4_do_handle_exception(server, err, exception);
9984 * nfs41_test_stateid - perform a TEST_STATEID operation
9986 * @server: server / transport on which to perform the operation
9987 * @stateid: state ID to test
9990 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9991 * Otherwise a negative NFS4ERR value is returned if the operation
9992 * failed or the state ID is not currently valid.
9994 static int nfs41_test_stateid(struct nfs_server *server,
9995 nfs4_stateid *stateid,
9996 const struct cred *cred)
9998 struct nfs4_exception exception = {
9999 .interruptible = true,
10003 err = _nfs41_test_stateid(server, stateid, cred);
10004 nfs4_handle_delay_or_session_error(server, err, &exception);
10005 } while (exception.retry);
10009 struct nfs_free_stateid_data {
10010 struct nfs_server *server;
10011 struct nfs41_free_stateid_args args;
10012 struct nfs41_free_stateid_res res;
10015 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
10017 struct nfs_free_stateid_data *data = calldata;
10018 nfs4_setup_sequence(data->server->nfs_client,
10019 &data->args.seq_args,
10020 &data->res.seq_res,
10024 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
10026 struct nfs_free_stateid_data *data = calldata;
10028 nfs41_sequence_done(task, &data->res.seq_res);
10030 switch (task->tk_status) {
10031 case -NFS4ERR_DELAY:
10032 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
10033 rpc_restart_call_prepare(task);
10037 static void nfs41_free_stateid_release(void *calldata)
10042 static const struct rpc_call_ops nfs41_free_stateid_ops = {
10043 .rpc_call_prepare = nfs41_free_stateid_prepare,
10044 .rpc_call_done = nfs41_free_stateid_done,
10045 .rpc_release = nfs41_free_stateid_release,
10049 * nfs41_free_stateid - perform a FREE_STATEID operation
10051 * @server: server / transport on which to perform the operation
10052 * @stateid: state ID to release
10053 * @cred: credential
10054 * @privileged: set to true if this call needs to be privileged
10056 * Note: this function is always asynchronous.
10058 static int nfs41_free_stateid(struct nfs_server *server,
10059 const nfs4_stateid *stateid,
10060 const struct cred *cred,
10063 struct rpc_message msg = {
10064 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
10067 struct rpc_task_setup task_setup = {
10068 .rpc_client = server->client,
10069 .rpc_message = &msg,
10070 .callback_ops = &nfs41_free_stateid_ops,
10071 .flags = RPC_TASK_ASYNC,
10073 struct nfs_free_stateid_data *data;
10074 struct rpc_task *task;
10076 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
10077 &task_setup.rpc_client, &msg);
10079 dprintk("NFS call free_stateid %p\n", stateid);
10080 data = kmalloc(sizeof(*data), GFP_NOFS);
10083 data->server = server;
10084 nfs4_stateid_copy(&data->args.stateid, stateid);
10086 task_setup.callback_data = data;
10088 msg.rpc_argp = &data->args;
10089 msg.rpc_resp = &data->res;
10090 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
10091 task = rpc_run_task(&task_setup);
10093 return PTR_ERR(task);
10094 rpc_put_task(task);
10099 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
10101 const struct cred *cred = lsp->ls_state->owner->so_cred;
10103 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
10104 nfs4_free_lock_state(server, lsp);
10107 static bool nfs41_match_stateid(const nfs4_stateid *s1,
10108 const nfs4_stateid *s2)
10110 if (s1->type != s2->type)
10113 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
10116 if (s1->seqid == s2->seqid)
10119 return s1->seqid == 0 || s2->seqid == 0;
10122 #endif /* CONFIG_NFS_V4_1 */
10124 static bool nfs4_match_stateid(const nfs4_stateid *s1,
10125 const nfs4_stateid *s2)
10127 return nfs4_stateid_match(s1, s2);
10131 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
10132 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
10133 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
10134 .recover_open = nfs4_open_reclaim,
10135 .recover_lock = nfs4_lock_reclaim,
10136 .establish_clid = nfs4_init_clientid,
10137 .detect_trunking = nfs40_discover_server_trunking,
10140 #if defined(CONFIG_NFS_V4_1)
10141 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
10142 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
10143 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
10144 .recover_open = nfs4_open_reclaim,
10145 .recover_lock = nfs4_lock_reclaim,
10146 .establish_clid = nfs41_init_clientid,
10147 .reclaim_complete = nfs41_proc_reclaim_complete,
10148 .detect_trunking = nfs41_discover_server_trunking,
10150 #endif /* CONFIG_NFS_V4_1 */
10152 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
10153 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
10154 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
10155 .recover_open = nfs40_open_expired,
10156 .recover_lock = nfs4_lock_expired,
10157 .establish_clid = nfs4_init_clientid,
10160 #if defined(CONFIG_NFS_V4_1)
10161 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
10162 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
10163 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
10164 .recover_open = nfs41_open_expired,
10165 .recover_lock = nfs41_lock_expired,
10166 .establish_clid = nfs41_init_clientid,
10168 #endif /* CONFIG_NFS_V4_1 */
10170 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
10171 .sched_state_renewal = nfs4_proc_async_renew,
10172 .get_state_renewal_cred = nfs4_get_renew_cred,
10173 .renew_lease = nfs4_proc_renew,
10176 #if defined(CONFIG_NFS_V4_1)
10177 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
10178 .sched_state_renewal = nfs41_proc_async_sequence,
10179 .get_state_renewal_cred = nfs4_get_machine_cred,
10180 .renew_lease = nfs4_proc_sequence,
10184 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
10185 .get_locations = _nfs40_proc_get_locations,
10186 .fsid_present = _nfs40_proc_fsid_present,
10189 #if defined(CONFIG_NFS_V4_1)
10190 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
10191 .get_locations = _nfs41_proc_get_locations,
10192 .fsid_present = _nfs41_proc_fsid_present,
10194 #endif /* CONFIG_NFS_V4_1 */
10196 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
10197 .minor_version = 0,
10198 .init_caps = NFS_CAP_READDIRPLUS
10199 | NFS_CAP_ATOMIC_OPEN
10200 | NFS_CAP_POSIX_LOCK,
10201 .init_client = nfs40_init_client,
10202 .shutdown_client = nfs40_shutdown_client,
10203 .match_stateid = nfs4_match_stateid,
10204 .find_root_sec = nfs4_find_root_sec,
10205 .free_lock_state = nfs4_release_lockowner,
10206 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
10207 .alloc_seqid = nfs_alloc_seqid,
10208 .call_sync_ops = &nfs40_call_sync_ops,
10209 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
10210 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
10211 .state_renewal_ops = &nfs40_state_renewal_ops,
10212 .mig_recovery_ops = &nfs40_mig_recovery_ops,
10215 #if defined(CONFIG_NFS_V4_1)
10216 static struct nfs_seqid *
10217 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
10222 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
10223 .minor_version = 1,
10224 .init_caps = NFS_CAP_READDIRPLUS
10225 | NFS_CAP_ATOMIC_OPEN
10226 | NFS_CAP_POSIX_LOCK
10227 | NFS_CAP_STATEID_NFSV41
10228 | NFS_CAP_ATOMIC_OPEN_V1
10230 .init_client = nfs41_init_client,
10231 .shutdown_client = nfs41_shutdown_client,
10232 .match_stateid = nfs41_match_stateid,
10233 .find_root_sec = nfs41_find_root_sec,
10234 .free_lock_state = nfs41_free_lock_state,
10235 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
10236 .alloc_seqid = nfs_alloc_no_seqid,
10237 .session_trunk = nfs4_test_session_trunk,
10238 .call_sync_ops = &nfs41_call_sync_ops,
10239 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10240 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10241 .state_renewal_ops = &nfs41_state_renewal_ops,
10242 .mig_recovery_ops = &nfs41_mig_recovery_ops,
10246 #if defined(CONFIG_NFS_V4_2)
10247 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
10248 .minor_version = 2,
10249 .init_caps = NFS_CAP_READDIRPLUS
10250 | NFS_CAP_ATOMIC_OPEN
10251 | NFS_CAP_POSIX_LOCK
10252 | NFS_CAP_STATEID_NFSV41
10253 | NFS_CAP_ATOMIC_OPEN_V1
10257 | NFS_CAP_OFFLOAD_CANCEL
10258 | NFS_CAP_COPY_NOTIFY
10259 | NFS_CAP_DEALLOCATE
10261 | NFS_CAP_LAYOUTSTATS
10263 | NFS_CAP_LAYOUTERROR
10264 | NFS_CAP_READ_PLUS,
10265 .init_client = nfs41_init_client,
10266 .shutdown_client = nfs41_shutdown_client,
10267 .match_stateid = nfs41_match_stateid,
10268 .find_root_sec = nfs41_find_root_sec,
10269 .free_lock_state = nfs41_free_lock_state,
10270 .call_sync_ops = &nfs41_call_sync_ops,
10271 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
10272 .alloc_seqid = nfs_alloc_no_seqid,
10273 .session_trunk = nfs4_test_session_trunk,
10274 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10275 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10276 .state_renewal_ops = &nfs41_state_renewal_ops,
10277 .mig_recovery_ops = &nfs41_mig_recovery_ops,
10281 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
10282 [0] = &nfs_v4_0_minor_ops,
10283 #if defined(CONFIG_NFS_V4_1)
10284 [1] = &nfs_v4_1_minor_ops,
10286 #if defined(CONFIG_NFS_V4_2)
10287 [2] = &nfs_v4_2_minor_ops,
10291 static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
10293 ssize_t error, error2, error3;
10295 error = generic_listxattr(dentry, list, size);
10303 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
10312 error3 = nfs4_listxattr_nfs4_user(d_inode(dentry), list, size);
10316 return error + error2 + error3;
10319 static const struct inode_operations nfs4_dir_inode_operations = {
10320 .create = nfs_create,
10321 .lookup = nfs_lookup,
10322 .atomic_open = nfs_atomic_open,
10324 .unlink = nfs_unlink,
10325 .symlink = nfs_symlink,
10326 .mkdir = nfs_mkdir,
10327 .rmdir = nfs_rmdir,
10328 .mknod = nfs_mknod,
10329 .rename = nfs_rename,
10330 .permission = nfs_permission,
10331 .getattr = nfs_getattr,
10332 .setattr = nfs_setattr,
10333 .listxattr = nfs4_listxattr,
10336 static const struct inode_operations nfs4_file_inode_operations = {
10337 .permission = nfs_permission,
10338 .getattr = nfs_getattr,
10339 .setattr = nfs_setattr,
10340 .listxattr = nfs4_listxattr,
10343 const struct nfs_rpc_ops nfs_v4_clientops = {
10344 .version = 4, /* protocol version */
10345 .dentry_ops = &nfs4_dentry_operations,
10346 .dir_inode_ops = &nfs4_dir_inode_operations,
10347 .file_inode_ops = &nfs4_file_inode_operations,
10348 .file_ops = &nfs4_file_operations,
10349 .getroot = nfs4_proc_get_root,
10350 .submount = nfs4_submount,
10351 .try_get_tree = nfs4_try_get_tree,
10352 .getattr = nfs4_proc_getattr,
10353 .setattr = nfs4_proc_setattr,
10354 .lookup = nfs4_proc_lookup,
10355 .lookupp = nfs4_proc_lookupp,
10356 .access = nfs4_proc_access,
10357 .readlink = nfs4_proc_readlink,
10358 .create = nfs4_proc_create,
10359 .remove = nfs4_proc_remove,
10360 .unlink_setup = nfs4_proc_unlink_setup,
10361 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
10362 .unlink_done = nfs4_proc_unlink_done,
10363 .rename_setup = nfs4_proc_rename_setup,
10364 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
10365 .rename_done = nfs4_proc_rename_done,
10366 .link = nfs4_proc_link,
10367 .symlink = nfs4_proc_symlink,
10368 .mkdir = nfs4_proc_mkdir,
10369 .rmdir = nfs4_proc_rmdir,
10370 .readdir = nfs4_proc_readdir,
10371 .mknod = nfs4_proc_mknod,
10372 .statfs = nfs4_proc_statfs,
10373 .fsinfo = nfs4_proc_fsinfo,
10374 .pathconf = nfs4_proc_pathconf,
10375 .set_capabilities = nfs4_server_capabilities,
10376 .decode_dirent = nfs4_decode_dirent,
10377 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
10378 .read_setup = nfs4_proc_read_setup,
10379 .read_done = nfs4_read_done,
10380 .write_setup = nfs4_proc_write_setup,
10381 .write_done = nfs4_write_done,
10382 .commit_setup = nfs4_proc_commit_setup,
10383 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
10384 .commit_done = nfs4_commit_done,
10385 .lock = nfs4_proc_lock,
10386 .clear_acl_cache = nfs4_zap_acl_attr,
10387 .close_context = nfs4_close_context,
10388 .open_context = nfs4_atomic_open,
10389 .have_delegation = nfs4_have_delegation,
10390 .alloc_client = nfs4_alloc_client,
10391 .init_client = nfs4_init_client,
10392 .free_client = nfs4_free_client,
10393 .create_server = nfs4_create_server,
10394 .clone_server = nfs_clone_server,
10397 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
10398 .name = XATTR_NAME_NFSV4_ACL,
10399 .list = nfs4_xattr_list_nfs4_acl,
10400 .get = nfs4_xattr_get_nfs4_acl,
10401 .set = nfs4_xattr_set_nfs4_acl,
10404 #ifdef CONFIG_NFS_V4_2
10405 static const struct xattr_handler nfs4_xattr_nfs4_user_handler = {
10406 .prefix = XATTR_USER_PREFIX,
10407 .get = nfs4_xattr_get_nfs4_user,
10408 .set = nfs4_xattr_set_nfs4_user,
10412 const struct xattr_handler *nfs4_xattr_handlers[] = {
10413 &nfs4_xattr_nfs4_acl_handler,
10414 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
10415 &nfs4_xattr_nfs4_label_handler,
10417 #ifdef CONFIG_NFS_V4_2
10418 &nfs4_xattr_nfs4_user_handler,
10425 * c-basic-offset: 8