4 * Client-side procedure declarations for NFSv4.
6 * Copyright (c) 2002 The Regents of the University of Michigan.
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <linux/delay.h>
40 #include <linux/errno.h>
41 #include <linux/string.h>
42 #include <linux/ratelimit.h>
43 #include <linux/printk.h>
44 #include <linux/slab.h>
45 #include <linux/sunrpc/clnt.h>
46 #include <linux/nfs.h>
47 #include <linux/nfs4.h>
48 #include <linux/nfs_fs.h>
49 #include <linux/nfs_page.h>
50 #include <linux/nfs_mount.h>
51 #include <linux/namei.h>
52 #include <linux/mount.h>
53 #include <linux/module.h>
54 #include <linux/xattr.h>
55 #include <linux/utsname.h>
56 #include <linux/freezer.h>
57 #include <linux/iversion.h>
60 #include "delegation.h"
67 #include "nfs4idmap.h"
68 #include "nfs4session.h"
72 #include "nfs4trace.h"
74 #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_set(__u32 bitmask[NFS4_BITMASK_SZ],
116 const __u32 *src, struct inode *inode,
117 struct nfs_server *server,
118 struct nfs4_label *label);
120 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
121 static inline struct nfs4_label *
122 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
123 struct iattr *sattr, struct nfs4_label *label)
130 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
138 err = security_dentry_init_security(dentry, sattr->ia_mode,
139 &dentry->d_name, (void **)&label->label, &label->len);
146 nfs4_label_release_security(struct nfs4_label *label)
149 security_release_secctx(label->label, label->len);
151 static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
154 return server->attr_bitmask;
156 return server->attr_bitmask_nl;
159 static inline struct nfs4_label *
160 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
161 struct iattr *sattr, struct nfs4_label *l)
164 nfs4_label_release_security(struct nfs4_label *label)
167 nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
168 { return server->attr_bitmask; }
171 /* Prevent leaks of NFSv4 errors into userland */
172 static int nfs4_map_errors(int err)
177 case -NFS4ERR_RESOURCE:
178 case -NFS4ERR_LAYOUTTRYLATER:
179 case -NFS4ERR_RECALLCONFLICT:
180 case -NFS4ERR_RETURNCONFLICT:
182 case -NFS4ERR_WRONGSEC:
183 case -NFS4ERR_WRONG_CRED:
185 case -NFS4ERR_BADOWNER:
186 case -NFS4ERR_BADNAME:
188 case -NFS4ERR_SHARE_DENIED:
190 case -NFS4ERR_MINOR_VERS_MISMATCH:
191 return -EPROTONOSUPPORT;
192 case -NFS4ERR_FILE_OPEN:
195 dprintk("%s could not handle NFSv4 error %d\n",
203 * This is our standard bitmap for GETATTR requests.
205 const u32 nfs4_fattr_bitmap[3] = {
207 | FATTR4_WORD0_CHANGE
210 | FATTR4_WORD0_FILEID,
212 | FATTR4_WORD1_NUMLINKS
214 | FATTR4_WORD1_OWNER_GROUP
215 | FATTR4_WORD1_RAWDEV
216 | FATTR4_WORD1_SPACE_USED
217 | FATTR4_WORD1_TIME_ACCESS
218 | FATTR4_WORD1_TIME_METADATA
219 | FATTR4_WORD1_TIME_MODIFY
220 | FATTR4_WORD1_MOUNTED_ON_FILEID,
221 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
222 FATTR4_WORD2_SECURITY_LABEL
226 static const u32 nfs4_pnfs_open_bitmap[3] = {
228 | FATTR4_WORD0_CHANGE
231 | FATTR4_WORD0_FILEID,
233 | FATTR4_WORD1_NUMLINKS
235 | FATTR4_WORD1_OWNER_GROUP
236 | FATTR4_WORD1_RAWDEV
237 | FATTR4_WORD1_SPACE_USED
238 | FATTR4_WORD1_TIME_ACCESS
239 | FATTR4_WORD1_TIME_METADATA
240 | FATTR4_WORD1_TIME_MODIFY,
241 FATTR4_WORD2_MDSTHRESHOLD
242 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
243 | FATTR4_WORD2_SECURITY_LABEL
247 static const u32 nfs4_open_noattr_bitmap[3] = {
249 | FATTR4_WORD0_FILEID,
252 const u32 nfs4_statfs_bitmap[3] = {
253 FATTR4_WORD0_FILES_AVAIL
254 | FATTR4_WORD0_FILES_FREE
255 | FATTR4_WORD0_FILES_TOTAL,
256 FATTR4_WORD1_SPACE_AVAIL
257 | FATTR4_WORD1_SPACE_FREE
258 | FATTR4_WORD1_SPACE_TOTAL
261 const u32 nfs4_pathconf_bitmap[3] = {
263 | FATTR4_WORD0_MAXNAME,
267 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
268 | FATTR4_WORD0_MAXREAD
269 | FATTR4_WORD0_MAXWRITE
270 | FATTR4_WORD0_LEASE_TIME,
271 FATTR4_WORD1_TIME_DELTA
272 | FATTR4_WORD1_FS_LAYOUT_TYPES,
273 FATTR4_WORD2_LAYOUT_BLKSIZE
274 | FATTR4_WORD2_CLONE_BLKSIZE
275 | FATTR4_WORD2_XATTR_SUPPORT
278 const u32 nfs4_fs_locations_bitmap[3] = {
282 | FATTR4_WORD0_FILEID
283 | FATTR4_WORD0_FS_LOCATIONS,
285 | FATTR4_WORD1_OWNER_GROUP
286 | FATTR4_WORD1_RAWDEV
287 | FATTR4_WORD1_SPACE_USED
288 | FATTR4_WORD1_TIME_ACCESS
289 | FATTR4_WORD1_TIME_METADATA
290 | FATTR4_WORD1_TIME_MODIFY
291 | FATTR4_WORD1_MOUNTED_ON_FILEID,
294 static void nfs4_bitmap_copy_adjust(__u32 *dst, const __u32 *src,
297 unsigned long cache_validity;
299 memcpy(dst, src, NFS4_BITMASK_SZ*sizeof(*dst));
300 if (!inode || !nfs4_have_delegation(inode, FMODE_READ))
303 cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
304 if (!(cache_validity & NFS_INO_REVAL_FORCED))
305 cache_validity &= ~(NFS_INO_INVALID_CHANGE
306 | NFS_INO_INVALID_SIZE);
308 if (!(cache_validity & NFS_INO_INVALID_SIZE))
309 dst[0] &= ~FATTR4_WORD0_SIZE;
311 if (!(cache_validity & NFS_INO_INVALID_CHANGE))
312 dst[0] &= ~FATTR4_WORD0_CHANGE;
315 static void nfs4_bitmap_copy_adjust_setattr(__u32 *dst,
316 const __u32 *src, struct inode *inode)
318 nfs4_bitmap_copy_adjust(dst, src, inode);
321 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
322 struct nfs4_readdir_arg *readdir)
324 unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
328 readdir->cookie = cookie;
329 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
334 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
339 * NFSv4 servers do not return entries for '.' and '..'
340 * Therefore, we fake these entries here. We let '.'
341 * have cookie 0 and '..' have cookie 1. Note that
342 * when talking to the server, we always send cookie 0
345 start = p = kmap_atomic(*readdir->pages);
348 *p++ = xdr_one; /* next */
349 *p++ = xdr_zero; /* cookie, first word */
350 *p++ = xdr_one; /* cookie, second word */
351 *p++ = xdr_one; /* entry len */
352 memcpy(p, ".\0\0\0", 4); /* entry */
354 *p++ = xdr_one; /* bitmap length */
355 *p++ = htonl(attrs); /* bitmap */
356 *p++ = htonl(12); /* attribute buffer length */
357 *p++ = htonl(NF4DIR);
358 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
361 *p++ = xdr_one; /* next */
362 *p++ = xdr_zero; /* cookie, first word */
363 *p++ = xdr_two; /* cookie, second word */
364 *p++ = xdr_two; /* entry len */
365 memcpy(p, "..\0\0", 4); /* entry */
367 *p++ = xdr_one; /* bitmap length */
368 *p++ = htonl(attrs); /* bitmap */
369 *p++ = htonl(12); /* attribute buffer length */
370 *p++ = htonl(NF4DIR);
371 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
373 readdir->pgbase = (char *)p - (char *)start;
374 readdir->count -= readdir->pgbase;
375 kunmap_atomic(start);
378 static void nfs4_fattr_set_prechange(struct nfs_fattr *fattr, u64 version)
380 if (!(fattr->valid & NFS_ATTR_FATTR_PRECHANGE)) {
381 fattr->pre_change_attr = version;
382 fattr->valid |= NFS_ATTR_FATTR_PRECHANGE;
386 static void nfs4_test_and_free_stateid(struct nfs_server *server,
387 nfs4_stateid *stateid,
388 const struct cred *cred)
390 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
392 ops->test_and_free_expired(server, stateid, cred);
395 static void __nfs4_free_revoked_stateid(struct nfs_server *server,
396 nfs4_stateid *stateid,
397 const struct cred *cred)
399 stateid->type = NFS4_REVOKED_STATEID_TYPE;
400 nfs4_test_and_free_stateid(server, stateid, cred);
403 static void nfs4_free_revoked_stateid(struct nfs_server *server,
404 const nfs4_stateid *stateid,
405 const struct cred *cred)
409 nfs4_stateid_copy(&tmp, stateid);
410 __nfs4_free_revoked_stateid(server, &tmp, cred);
413 static long nfs4_update_delay(long *timeout)
417 return NFS4_POLL_RETRY_MAX;
419 *timeout = NFS4_POLL_RETRY_MIN;
420 if (*timeout > NFS4_POLL_RETRY_MAX)
421 *timeout = NFS4_POLL_RETRY_MAX;
427 static int nfs4_delay_killable(long *timeout)
431 freezable_schedule_timeout_killable_unsafe(
432 nfs4_update_delay(timeout));
433 if (!__fatal_signal_pending(current))
438 static int nfs4_delay_interruptible(long *timeout)
442 freezable_schedule_timeout_interruptible_unsafe(nfs4_update_delay(timeout));
443 if (!signal_pending(current))
445 return __fatal_signal_pending(current) ? -EINTR :-ERESTARTSYS;
448 static int nfs4_delay(long *timeout, bool interruptible)
451 return nfs4_delay_interruptible(timeout);
452 return nfs4_delay_killable(timeout);
455 static const nfs4_stateid *
456 nfs4_recoverable_stateid(const nfs4_stateid *stateid)
460 switch (stateid->type) {
461 case NFS4_OPEN_STATEID_TYPE:
462 case NFS4_LOCK_STATEID_TYPE:
463 case NFS4_DELEGATION_STATEID_TYPE:
471 /* This is the error handling routine for processes that are allowed
474 static int nfs4_do_handle_exception(struct nfs_server *server,
475 int errorcode, struct nfs4_exception *exception)
477 struct nfs_client *clp = server->nfs_client;
478 struct nfs4_state *state = exception->state;
479 const nfs4_stateid *stateid;
480 struct inode *inode = exception->inode;
483 exception->delay = 0;
484 exception->recovering = 0;
485 exception->retry = 0;
487 stateid = nfs4_recoverable_stateid(exception->stateid);
488 if (stateid == NULL && state != NULL)
489 stateid = nfs4_recoverable_stateid(&state->stateid);
494 case -NFS4ERR_BADHANDLE:
496 if (inode != NULL && S_ISREG(inode->i_mode))
497 pnfs_destroy_layout(NFS_I(inode));
499 case -NFS4ERR_DELEG_REVOKED:
500 case -NFS4ERR_ADMIN_REVOKED:
501 case -NFS4ERR_EXPIRED:
502 case -NFS4ERR_BAD_STATEID:
503 case -NFS4ERR_PARTNER_NO_AUTH:
504 if (inode != NULL && stateid != NULL) {
505 nfs_inode_find_state_and_recover(inode,
507 goto wait_on_recovery;
510 case -NFS4ERR_OPENMODE:
514 err = nfs_async_inode_return_delegation(inode,
517 goto wait_on_recovery;
518 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
519 exception->retry = 1;
525 ret = nfs4_schedule_stateid_recovery(server, state);
528 goto wait_on_recovery;
529 case -NFS4ERR_STALE_STATEID:
530 case -NFS4ERR_STALE_CLIENTID:
531 nfs4_schedule_lease_recovery(clp);
532 goto wait_on_recovery;
534 ret = nfs4_schedule_migration_recovery(server);
537 goto wait_on_recovery;
538 case -NFS4ERR_LEASE_MOVED:
539 nfs4_schedule_lease_moved_recovery(clp);
540 goto wait_on_recovery;
541 #if defined(CONFIG_NFS_V4_1)
542 case -NFS4ERR_BADSESSION:
543 case -NFS4ERR_BADSLOT:
544 case -NFS4ERR_BAD_HIGH_SLOT:
545 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
546 case -NFS4ERR_DEADSESSION:
547 case -NFS4ERR_SEQ_FALSE_RETRY:
548 case -NFS4ERR_SEQ_MISORDERED:
549 /* Handled in nfs41_sequence_process() */
550 goto wait_on_recovery;
551 #endif /* defined(CONFIG_NFS_V4_1) */
552 case -NFS4ERR_FILE_OPEN:
553 if (exception->timeout > HZ) {
554 /* We have retried a decent amount, time to
562 nfs_inc_server_stats(server, NFSIOS_DELAY);
565 case -NFS4ERR_LAYOUTTRYLATER:
566 case -NFS4ERR_RECALLCONFLICT:
567 case -NFS4ERR_RETURNCONFLICT:
568 exception->delay = 1;
571 case -NFS4ERR_RETRY_UNCACHED_REP:
572 case -NFS4ERR_OLD_STATEID:
573 exception->retry = 1;
575 case -NFS4ERR_BADOWNER:
576 /* The following works around a Linux server bug! */
577 case -NFS4ERR_BADNAME:
578 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
579 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
580 exception->retry = 1;
581 printk(KERN_WARNING "NFS: v4 server %s "
582 "does not accept raw "
584 "Reenabling the idmapper.\n",
585 server->nfs_client->cl_hostname);
588 /* We failed to handle the error */
589 return nfs4_map_errors(ret);
591 exception->recovering = 1;
595 /* This is the error handling routine for processes that are allowed
598 int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
600 struct nfs_client *clp = server->nfs_client;
603 ret = nfs4_do_handle_exception(server, errorcode, exception);
604 if (exception->delay) {
605 ret = nfs4_delay(&exception->timeout,
606 exception->interruptible);
609 if (exception->recovering) {
610 if (exception->task_is_privileged)
612 ret = nfs4_wait_clnt_recover(clp);
613 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
620 exception->retry = 1;
625 nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
626 int errorcode, struct nfs4_exception *exception)
628 struct nfs_client *clp = server->nfs_client;
631 ret = nfs4_do_handle_exception(server, errorcode, exception);
632 if (exception->delay) {
633 rpc_delay(task, nfs4_update_delay(&exception->timeout));
636 if (exception->recovering) {
637 if (exception->task_is_privileged)
639 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
640 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
641 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
644 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
649 exception->retry = 1;
651 * For NFS4ERR_MOVED, the client transport will need to
652 * be recomputed after migration recovery has completed.
654 if (errorcode == -NFS4ERR_MOVED)
655 rpc_task_release_transport(task);
661 nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
662 struct nfs4_state *state, long *timeout)
664 struct nfs4_exception exception = {
668 if (task->tk_status >= 0)
671 exception.timeout = *timeout;
672 task->tk_status = nfs4_async_handle_exception(task, server,
675 if (exception.delay && timeout)
676 *timeout = exception.timeout;
683 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
684 * or 'false' otherwise.
686 static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
688 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
689 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
692 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
694 spin_lock(&clp->cl_lock);
695 if (time_before(clp->cl_last_renewal,timestamp))
696 clp->cl_last_renewal = timestamp;
697 spin_unlock(&clp->cl_lock);
700 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
702 struct nfs_client *clp = server->nfs_client;
704 if (!nfs4_has_session(clp))
705 do_renew_lease(clp, timestamp);
708 struct nfs4_call_sync_data {
709 const struct nfs_server *seq_server;
710 struct nfs4_sequence_args *seq_args;
711 struct nfs4_sequence_res *seq_res;
714 void nfs4_init_sequence(struct nfs4_sequence_args *args,
715 struct nfs4_sequence_res *res, int cache_reply,
718 args->sa_slot = NULL;
719 args->sa_cache_this = cache_reply;
720 args->sa_privileged = privileged;
725 static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
727 struct nfs4_slot *slot = res->sr_slot;
728 struct nfs4_slot_table *tbl;
731 spin_lock(&tbl->slot_tbl_lock);
732 if (!nfs41_wake_and_assign_slot(tbl, slot))
733 nfs4_free_slot(tbl, slot);
734 spin_unlock(&tbl->slot_tbl_lock);
739 static int nfs40_sequence_done(struct rpc_task *task,
740 struct nfs4_sequence_res *res)
742 if (res->sr_slot != NULL)
743 nfs40_sequence_free_slot(res);
747 #if defined(CONFIG_NFS_V4_1)
749 static void nfs41_release_slot(struct nfs4_slot *slot)
751 struct nfs4_session *session;
752 struct nfs4_slot_table *tbl;
753 bool send_new_highest_used_slotid = false;
758 session = tbl->session;
760 /* Bump the slot sequence number */
765 spin_lock(&tbl->slot_tbl_lock);
766 /* Be nice to the server: try to ensure that the last transmitted
767 * value for highest_user_slotid <= target_highest_slotid
769 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
770 send_new_highest_used_slotid = true;
772 if (nfs41_wake_and_assign_slot(tbl, slot)) {
773 send_new_highest_used_slotid = false;
776 nfs4_free_slot(tbl, slot);
778 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
779 send_new_highest_used_slotid = false;
781 spin_unlock(&tbl->slot_tbl_lock);
782 if (send_new_highest_used_slotid)
783 nfs41_notify_server(session->clp);
784 if (waitqueue_active(&tbl->slot_waitq))
785 wake_up_all(&tbl->slot_waitq);
788 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
790 nfs41_release_slot(res->sr_slot);
794 static void nfs4_slot_sequence_record_sent(struct nfs4_slot *slot,
797 if ((s32)(seqnr - slot->seq_nr_highest_sent) > 0)
798 slot->seq_nr_highest_sent = seqnr;
800 static void nfs4_slot_sequence_acked(struct nfs4_slot *slot, u32 seqnr)
802 nfs4_slot_sequence_record_sent(slot, seqnr);
803 slot->seq_nr_last_acked = seqnr;
806 static void nfs4_probe_sequence(struct nfs_client *client, const struct cred *cred,
807 struct nfs4_slot *slot)
809 struct rpc_task *task = _nfs41_proc_sequence(client, cred, slot, true);
811 rpc_put_task_async(task);
814 static int nfs41_sequence_process(struct rpc_task *task,
815 struct nfs4_sequence_res *res)
817 struct nfs4_session *session;
818 struct nfs4_slot *slot = res->sr_slot;
819 struct nfs_client *clp;
825 /* don't increment the sequence number if the task wasn't sent */
826 if (!RPC_WAS_SENT(task) || slot->seq_done)
829 session = slot->table->session;
832 trace_nfs4_sequence_done(session, res);
834 status = res->sr_status;
835 if (task->tk_status == -NFS4ERR_DEADSESSION)
836 status = -NFS4ERR_DEADSESSION;
838 /* Check the SEQUENCE operation status */
841 /* Mark this sequence number as having been acked */
842 nfs4_slot_sequence_acked(slot, slot->seq_nr);
843 /* Update the slot's sequence and clientid lease timer */
845 do_renew_lease(clp, res->sr_timestamp);
846 /* Check sequence flags */
847 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
849 nfs41_update_target_slotid(slot->table, slot, res);
853 * sr_status remains 1 if an RPC level error occurred.
854 * The server may or may not have processed the sequence
857 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
861 /* The server detected a resend of the RPC call and
862 * returned NFS4ERR_DELAY as per Section 2.10.6.2
865 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
870 case -NFS4ERR_RETRY_UNCACHED_REP:
871 case -NFS4ERR_SEQ_FALSE_RETRY:
873 * The server thinks we tried to replay a request.
874 * Retry the call after bumping the sequence ID.
876 nfs4_slot_sequence_acked(slot, slot->seq_nr);
878 case -NFS4ERR_BADSLOT:
880 * The slot id we used was probably retired. Try again
881 * using a different slot id.
883 if (slot->slot_nr < slot->table->target_highest_slotid)
884 goto session_recover;
886 case -NFS4ERR_SEQ_MISORDERED:
887 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
889 * Were one or more calls using this slot interrupted?
890 * If the server never received the request, then our
891 * transmitted slot sequence number may be too high. However,
892 * if the server did receive the request then it might
893 * accidentally give us a reply with a mismatched operation.
894 * We can sort this out by sending a lone sequence operation
895 * to the server on the same slot.
897 if ((s32)(slot->seq_nr - slot->seq_nr_last_acked) > 1) {
899 if (task->tk_msg.rpc_proc != &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE]) {
900 nfs4_probe_sequence(clp, task->tk_msg.rpc_cred, slot);
907 * A retry might be sent while the original request is
908 * still in progress on the replier. The replier SHOULD
909 * deal with the issue by returning NFS4ERR_DELAY as the
910 * reply to SEQUENCE or CB_SEQUENCE operation, but
911 * implementations MAY return NFS4ERR_SEQ_MISORDERED.
913 * Restart the search after a delay.
915 slot->seq_nr = slot->seq_nr_highest_sent;
917 case -NFS4ERR_BADSESSION:
918 case -NFS4ERR_DEADSESSION:
919 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
920 goto session_recover;
922 /* Just update the slot sequence no. */
926 /* The session may be reset by one of the error handlers. */
927 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
931 set_bit(NFS4_SLOT_TBL_DRAINING, &session->fc_slot_table.slot_tbl_state);
932 nfs4_schedule_session_recovery(session, status);
933 dprintk("%s ERROR: %d Reset session\n", __func__, status);
934 nfs41_sequence_free_slot(res);
939 if (rpc_restart_call_prepare(task)) {
940 nfs41_sequence_free_slot(res);
946 if (!rpc_restart_call(task))
948 rpc_delay(task, NFS4_POLL_RETRY_MAX);
952 int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
954 if (!nfs41_sequence_process(task, res))
956 if (res->sr_slot != NULL)
957 nfs41_sequence_free_slot(res);
961 EXPORT_SYMBOL_GPL(nfs41_sequence_done);
963 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
965 if (res->sr_slot == NULL)
967 if (res->sr_slot->table->session != NULL)
968 return nfs41_sequence_process(task, res);
969 return nfs40_sequence_done(task, res);
972 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
974 if (res->sr_slot != NULL) {
975 if (res->sr_slot->table->session != NULL)
976 nfs41_sequence_free_slot(res);
978 nfs40_sequence_free_slot(res);
982 int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
984 if (res->sr_slot == NULL)
986 if (!res->sr_slot->table->session)
987 return nfs40_sequence_done(task, res);
988 return nfs41_sequence_done(task, res);
990 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
992 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
994 struct nfs4_call_sync_data *data = calldata;
996 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
998 nfs4_setup_sequence(data->seq_server->nfs_client,
999 data->seq_args, data->seq_res, task);
1002 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
1004 struct nfs4_call_sync_data *data = calldata;
1006 nfs41_sequence_done(task, data->seq_res);
1009 static const struct rpc_call_ops nfs41_call_sync_ops = {
1010 .rpc_call_prepare = nfs41_call_sync_prepare,
1011 .rpc_call_done = nfs41_call_sync_done,
1014 #else /* !CONFIG_NFS_V4_1 */
1016 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
1018 return nfs40_sequence_done(task, res);
1021 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
1023 if (res->sr_slot != NULL)
1024 nfs40_sequence_free_slot(res);
1027 int nfs4_sequence_done(struct rpc_task *task,
1028 struct nfs4_sequence_res *res)
1030 return nfs40_sequence_done(task, res);
1032 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
1034 #endif /* !CONFIG_NFS_V4_1 */
1036 static void nfs41_sequence_res_init(struct nfs4_sequence_res *res)
1038 res->sr_timestamp = jiffies;
1039 res->sr_status_flags = 0;
1044 void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
1045 struct nfs4_sequence_res *res,
1046 struct nfs4_slot *slot)
1050 slot->privileged = args->sa_privileged ? 1 : 0;
1051 args->sa_slot = slot;
1053 res->sr_slot = slot;
1056 int nfs4_setup_sequence(struct nfs_client *client,
1057 struct nfs4_sequence_args *args,
1058 struct nfs4_sequence_res *res,
1059 struct rpc_task *task)
1061 struct nfs4_session *session = nfs4_get_session(client);
1062 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
1063 struct nfs4_slot *slot;
1065 /* slot already allocated? */
1066 if (res->sr_slot != NULL)
1070 tbl = &session->fc_slot_table;
1072 spin_lock(&tbl->slot_tbl_lock);
1073 /* The state manager will wait until the slot table is empty */
1074 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
1077 slot = nfs4_alloc_slot(tbl);
1079 if (slot == ERR_PTR(-ENOMEM))
1080 goto out_sleep_timeout;
1083 spin_unlock(&tbl->slot_tbl_lock);
1085 nfs4_sequence_attach_slot(args, res, slot);
1087 trace_nfs4_setup_sequence(session, args);
1089 nfs41_sequence_res_init(res);
1090 rpc_call_start(task);
1093 /* Try again in 1/4 second */
1094 if (args->sa_privileged)
1095 rpc_sleep_on_priority_timeout(&tbl->slot_tbl_waitq, task,
1096 jiffies + (HZ >> 2), RPC_PRIORITY_PRIVILEGED);
1098 rpc_sleep_on_timeout(&tbl->slot_tbl_waitq, task,
1099 NULL, jiffies + (HZ >> 2));
1100 spin_unlock(&tbl->slot_tbl_lock);
1103 if (args->sa_privileged)
1104 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
1105 RPC_PRIORITY_PRIVILEGED);
1107 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
1108 spin_unlock(&tbl->slot_tbl_lock);
1111 EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
1113 static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1115 struct nfs4_call_sync_data *data = calldata;
1116 nfs4_setup_sequence(data->seq_server->nfs_client,
1117 data->seq_args, data->seq_res, task);
1120 static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1122 struct nfs4_call_sync_data *data = calldata;
1123 nfs4_sequence_done(task, data->seq_res);
1126 static const struct rpc_call_ops nfs40_call_sync_ops = {
1127 .rpc_call_prepare = nfs40_call_sync_prepare,
1128 .rpc_call_done = nfs40_call_sync_done,
1131 static int nfs4_call_sync_custom(struct rpc_task_setup *task_setup)
1134 struct rpc_task *task;
1136 task = rpc_run_task(task_setup);
1138 return PTR_ERR(task);
1140 ret = task->tk_status;
1145 static int nfs4_do_call_sync(struct rpc_clnt *clnt,
1146 struct nfs_server *server,
1147 struct rpc_message *msg,
1148 struct nfs4_sequence_args *args,
1149 struct nfs4_sequence_res *res,
1150 unsigned short task_flags)
1152 struct nfs_client *clp = server->nfs_client;
1153 struct nfs4_call_sync_data data = {
1154 .seq_server = server,
1158 struct rpc_task_setup task_setup = {
1161 .callback_ops = clp->cl_mvops->call_sync_ops,
1162 .callback_data = &data,
1163 .flags = task_flags,
1166 return nfs4_call_sync_custom(&task_setup);
1169 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
1170 struct nfs_server *server,
1171 struct rpc_message *msg,
1172 struct nfs4_sequence_args *args,
1173 struct nfs4_sequence_res *res)
1175 return nfs4_do_call_sync(clnt, server, msg, args, res, 0);
1179 int nfs4_call_sync(struct rpc_clnt *clnt,
1180 struct nfs_server *server,
1181 struct rpc_message *msg,
1182 struct nfs4_sequence_args *args,
1183 struct nfs4_sequence_res *res,
1186 nfs4_init_sequence(args, res, cache_reply, 0);
1187 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
1191 nfs4_inc_nlink_locked(struct inode *inode)
1193 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1198 nfs4_dec_nlink_locked(struct inode *inode)
1200 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1205 nfs4_update_changeattr_locked(struct inode *inode,
1206 struct nfs4_change_info *cinfo,
1207 unsigned long timestamp, unsigned long cache_validity)
1209 struct nfs_inode *nfsi = NFS_I(inode);
1211 nfsi->cache_validity |= NFS_INO_INVALID_CTIME
1212 | NFS_INO_INVALID_MTIME
1215 if (cinfo->atomic && cinfo->before == inode_peek_iversion_raw(inode)) {
1216 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1217 nfsi->attrtimeo_timestamp = jiffies;
1219 if (S_ISDIR(inode->i_mode)) {
1220 nfsi->cache_validity |= NFS_INO_INVALID_DATA;
1221 nfs_force_lookup_revalidate(inode);
1223 if (!NFS_PROTO(inode)->have_delegation(inode,
1225 nfsi->cache_validity |= NFS_INO_REVAL_PAGECACHE;
1228 if (cinfo->before != inode_peek_iversion_raw(inode))
1229 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1230 NFS_INO_INVALID_ACL |
1231 NFS_INO_INVALID_XATTR;
1233 inode_set_iversion_raw(inode, cinfo->after);
1234 nfsi->read_cache_jiffies = timestamp;
1235 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
1236 nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE;
1238 if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
1239 nfs_fscache_invalidate(inode);
1243 nfs4_update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1244 unsigned long timestamp, unsigned long cache_validity)
1246 spin_lock(&dir->i_lock);
1247 nfs4_update_changeattr_locked(dir, cinfo, timestamp, cache_validity);
1248 spin_unlock(&dir->i_lock);
1251 struct nfs4_open_createattrs {
1252 struct nfs4_label *label;
1253 struct iattr *sattr;
1254 const __u32 verf[2];
1257 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1258 int err, struct nfs4_exception *exception)
1262 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1264 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1265 exception->retry = 1;
1269 static fmode_t _nfs4_ctx_to_accessmode(const struct nfs_open_context *ctx)
1271 return ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
1274 static fmode_t _nfs4_ctx_to_openmode(const struct nfs_open_context *ctx)
1276 fmode_t ret = ctx->mode & (FMODE_READ|FMODE_WRITE);
1278 return (ctx->mode & FMODE_EXEC) ? FMODE_READ | ret : ret;
1282 nfs4_map_atomic_open_share(struct nfs_server *server,
1283 fmode_t fmode, int openflags)
1287 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1289 res = NFS4_SHARE_ACCESS_READ;
1292 res = NFS4_SHARE_ACCESS_WRITE;
1294 case FMODE_READ|FMODE_WRITE:
1295 res = NFS4_SHARE_ACCESS_BOTH;
1297 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1299 /* Want no delegation if we're using O_DIRECT */
1300 if (openflags & O_DIRECT)
1301 res |= NFS4_SHARE_WANT_NO_DELEG;
1306 static enum open_claim_type4
1307 nfs4_map_atomic_open_claim(struct nfs_server *server,
1308 enum open_claim_type4 claim)
1310 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1315 case NFS4_OPEN_CLAIM_FH:
1316 return NFS4_OPEN_CLAIM_NULL;
1317 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1318 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1319 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1320 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1324 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
1326 p->o_res.f_attr = &p->f_attr;
1327 p->o_res.f_label = p->f_label;
1328 p->o_res.seqid = p->o_arg.seqid;
1329 p->c_res.seqid = p->c_arg.seqid;
1330 p->o_res.server = p->o_arg.server;
1331 p->o_res.access_request = p->o_arg.access;
1332 nfs_fattr_init(&p->f_attr);
1333 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
1336 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
1337 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
1338 const struct nfs4_open_createattrs *c,
1339 enum open_claim_type4 claim,
1342 struct dentry *parent = dget_parent(dentry);
1343 struct inode *dir = d_inode(parent);
1344 struct nfs_server *server = NFS_SERVER(dir);
1345 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
1346 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
1347 struct nfs4_opendata *p;
1349 p = kzalloc(sizeof(*p), gfp_mask);
1353 p->f_label = nfs4_label_alloc(server, gfp_mask);
1354 if (IS_ERR(p->f_label))
1357 p->a_label = nfs4_label_alloc(server, gfp_mask);
1358 if (IS_ERR(p->a_label))
1361 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1362 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
1363 if (IS_ERR(p->o_arg.seqid))
1364 goto err_free_label;
1365 nfs_sb_active(dentry->d_sb);
1366 p->dentry = dget(dentry);
1369 atomic_inc(&sp->so_count);
1370 p->o_arg.open_flags = flags;
1371 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
1372 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1373 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1375 if (flags & O_CREAT) {
1376 p->o_arg.umask = current_umask();
1377 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1378 if (c->sattr != NULL && c->sattr->ia_valid != 0) {
1379 p->o_arg.u.attrs = &p->attrs;
1380 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
1382 memcpy(p->o_arg.u.verifier.data, c->verf,
1383 sizeof(p->o_arg.u.verifier.data));
1386 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1387 * will return permission denied for all bits until close */
1388 if (!(flags & O_EXCL)) {
1389 /* ask server to check for all possible rights as results
1391 switch (p->o_arg.claim) {
1394 case NFS4_OPEN_CLAIM_NULL:
1395 case NFS4_OPEN_CLAIM_FH:
1396 p->o_arg.access = NFS4_ACCESS_READ |
1397 NFS4_ACCESS_MODIFY |
1398 NFS4_ACCESS_EXTEND |
1399 NFS4_ACCESS_EXECUTE;
1400 #ifdef CONFIG_NFS_V4_2
1401 if (server->caps & NFS_CAP_XATTR)
1402 p->o_arg.access |= NFS4_ACCESS_XAREAD |
1403 NFS4_ACCESS_XAWRITE |
1408 p->o_arg.clientid = server->nfs_client->cl_clientid;
1409 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1410 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1411 p->o_arg.name = &dentry->d_name;
1412 p->o_arg.server = server;
1413 p->o_arg.bitmask = nfs4_bitmask(server, label);
1414 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1415 switch (p->o_arg.claim) {
1416 case NFS4_OPEN_CLAIM_NULL:
1417 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1418 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1419 p->o_arg.fh = NFS_FH(dir);
1421 case NFS4_OPEN_CLAIM_PREVIOUS:
1422 case NFS4_OPEN_CLAIM_FH:
1423 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1424 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1425 p->o_arg.fh = NFS_FH(d_inode(dentry));
1427 p->c_arg.fh = &p->o_res.fh;
1428 p->c_arg.stateid = &p->o_res.stateid;
1429 p->c_arg.seqid = p->o_arg.seqid;
1430 nfs4_init_opendata_res(p);
1431 kref_init(&p->kref);
1435 nfs4_label_free(p->a_label);
1437 nfs4_label_free(p->f_label);
1445 static void nfs4_opendata_free(struct kref *kref)
1447 struct nfs4_opendata *p = container_of(kref,
1448 struct nfs4_opendata, kref);
1449 struct super_block *sb = p->dentry->d_sb;
1451 nfs4_lgopen_release(p->lgp);
1452 nfs_free_seqid(p->o_arg.seqid);
1453 nfs4_sequence_free_slot(&p->o_res.seq_res);
1454 if (p->state != NULL)
1455 nfs4_put_open_state(p->state);
1456 nfs4_put_state_owner(p->owner);
1458 nfs4_label_free(p->a_label);
1459 nfs4_label_free(p->f_label);
1463 nfs_sb_deactive(sb);
1464 nfs_fattr_free_names(&p->f_attr);
1465 kfree(p->f_attr.mdsthreshold);
1469 static void nfs4_opendata_put(struct nfs4_opendata *p)
1472 kref_put(&p->kref, nfs4_opendata_free);
1475 static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1478 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1479 case FMODE_READ|FMODE_WRITE:
1480 return state->n_rdwr != 0;
1482 return state->n_wronly != 0;
1484 return state->n_rdonly != 0;
1490 static int can_open_cached(struct nfs4_state *state, fmode_t mode,
1491 int open_mode, enum open_claim_type4 claim)
1495 if (open_mode & (O_EXCL|O_TRUNC))
1498 case NFS4_OPEN_CLAIM_NULL:
1499 case NFS4_OPEN_CLAIM_FH:
1504 switch (mode & (FMODE_READ|FMODE_WRITE)) {
1506 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1507 && state->n_rdonly != 0;
1510 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1511 && state->n_wronly != 0;
1513 case FMODE_READ|FMODE_WRITE:
1514 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1515 && state->n_rdwr != 0;
1521 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1522 enum open_claim_type4 claim)
1524 if (delegation == NULL)
1526 if ((delegation->type & fmode) != fmode)
1529 case NFS4_OPEN_CLAIM_NULL:
1530 case NFS4_OPEN_CLAIM_FH:
1532 case NFS4_OPEN_CLAIM_PREVIOUS:
1533 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1539 nfs_mark_delegation_referenced(delegation);
1543 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1552 case FMODE_READ|FMODE_WRITE:
1555 nfs4_state_set_mode_locked(state, state->state | fmode);
1558 #ifdef CONFIG_NFS_V4_1
1559 static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1561 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1563 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1565 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1569 #endif /* CONFIG_NFS_V4_1 */
1571 static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1573 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1574 wake_up_all(&state->waitq);
1577 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
1579 struct nfs_client *clp = state->owner->so_server->nfs_client;
1580 bool need_recover = false;
1582 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1583 need_recover = true;
1584 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1585 need_recover = true;
1586 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1587 need_recover = true;
1589 nfs4_state_mark_reclaim_nograce(clp, state);
1593 * Check for whether or not the caller may update the open stateid
1594 * to the value passed in by stateid.
1596 * Note: This function relies heavily on the server implementing
1597 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1599 * i.e. The stateid seqids have to be initialised to 1, and
1600 * are then incremented on every state transition.
1602 static bool nfs_stateid_is_sequential(struct nfs4_state *state,
1603 const nfs4_stateid *stateid)
1605 if (test_bit(NFS_OPEN_STATE, &state->flags)) {
1606 /* The common case - we're updating to a new sequence number */
1607 if (nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1608 if (nfs4_stateid_is_next(&state->open_stateid, stateid))
1612 /* The server returned a new stateid */
1614 /* This is the first OPEN in this generation */
1615 if (stateid->seqid == cpu_to_be32(1))
1620 static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1622 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1624 if (state->n_wronly)
1625 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1626 if (state->n_rdonly)
1627 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1629 set_bit(NFS_O_RDWR_STATE, &state->flags);
1630 set_bit(NFS_OPEN_STATE, &state->flags);
1633 static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1634 nfs4_stateid *stateid, fmode_t fmode)
1636 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1637 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1639 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1642 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1645 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1646 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1647 clear_bit(NFS_OPEN_STATE, &state->flags);
1649 if (stateid == NULL)
1651 /* Handle OPEN+OPEN_DOWNGRADE races */
1652 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1653 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1654 nfs_resync_open_stateid_locked(state);
1657 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1658 nfs4_stateid_copy(&state->stateid, stateid);
1659 nfs4_stateid_copy(&state->open_stateid, stateid);
1660 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
1662 nfs_state_log_update_open_stateid(state);
1665 static void nfs_clear_open_stateid(struct nfs4_state *state,
1666 nfs4_stateid *arg_stateid,
1667 nfs4_stateid *stateid, fmode_t fmode)
1669 write_seqlock(&state->seqlock);
1670 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1671 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1672 nfs_clear_open_stateid_locked(state, stateid, fmode);
1673 write_sequnlock(&state->seqlock);
1674 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1675 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1678 static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1679 const nfs4_stateid *stateid, nfs4_stateid *freeme)
1680 __must_hold(&state->owner->so_lock)
1681 __must_hold(&state->seqlock)
1689 if (nfs_stateid_is_sequential(state, stateid))
1694 /* Rely on seqids for serialisation with NFSv4.0 */
1695 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1698 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1699 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1701 * Ensure we process the state changes in the same order
1702 * in which the server processed them by delaying the
1703 * update of the stateid until we are in sequence.
1705 write_sequnlock(&state->seqlock);
1706 spin_unlock(&state->owner->so_lock);
1708 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
1710 if (!fatal_signal_pending(current)) {
1711 if (schedule_timeout(5*HZ) == 0)
1717 finish_wait(&state->waitq, &wait);
1719 spin_lock(&state->owner->so_lock);
1720 write_seqlock(&state->seqlock);
1723 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1724 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1725 nfs4_stateid_copy(freeme, &state->open_stateid);
1726 nfs_test_and_clear_all_open_stateid(state);
1729 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1730 nfs4_stateid_copy(&state->stateid, stateid);
1731 nfs4_stateid_copy(&state->open_stateid, stateid);
1732 trace_nfs4_open_stateid_update(state->inode, stateid, status);
1733 nfs_state_log_update_open_stateid(state);
1736 static void nfs_state_set_open_stateid(struct nfs4_state *state,
1737 const nfs4_stateid *open_stateid,
1739 nfs4_stateid *freeme)
1742 * Protect the call to nfs4_state_set_mode_locked and
1743 * serialise the stateid update
1745 write_seqlock(&state->seqlock);
1746 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1749 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1752 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1754 case FMODE_READ|FMODE_WRITE:
1755 set_bit(NFS_O_RDWR_STATE, &state->flags);
1757 set_bit(NFS_OPEN_STATE, &state->flags);
1758 write_sequnlock(&state->seqlock);
1761 static void nfs_state_clear_open_state_flags(struct nfs4_state *state)
1763 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1764 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1765 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1766 clear_bit(NFS_OPEN_STATE, &state->flags);
1769 static void nfs_state_set_delegation(struct nfs4_state *state,
1770 const nfs4_stateid *deleg_stateid,
1774 * Protect the call to nfs4_state_set_mode_locked and
1775 * serialise the stateid update
1777 write_seqlock(&state->seqlock);
1778 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1779 set_bit(NFS_DELEGATED_STATE, &state->flags);
1780 write_sequnlock(&state->seqlock);
1783 static void nfs_state_clear_delegation(struct nfs4_state *state)
1785 write_seqlock(&state->seqlock);
1786 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1787 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1788 write_sequnlock(&state->seqlock);
1791 int update_open_stateid(struct nfs4_state *state,
1792 const nfs4_stateid *open_stateid,
1793 const nfs4_stateid *delegation,
1796 struct nfs_server *server = NFS_SERVER(state->inode);
1797 struct nfs_client *clp = server->nfs_client;
1798 struct nfs_inode *nfsi = NFS_I(state->inode);
1799 struct nfs_delegation *deleg_cur;
1800 nfs4_stateid freeme = { };
1803 fmode &= (FMODE_READ|FMODE_WRITE);
1806 spin_lock(&state->owner->so_lock);
1807 if (open_stateid != NULL) {
1808 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1812 deleg_cur = nfs4_get_valid_delegation(state->inode);
1813 if (deleg_cur == NULL)
1816 spin_lock(&deleg_cur->lock);
1817 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1818 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1819 (deleg_cur->type & fmode) != fmode)
1820 goto no_delegation_unlock;
1822 if (delegation == NULL)
1823 delegation = &deleg_cur->stateid;
1824 else if (!nfs4_stateid_match_other(&deleg_cur->stateid, delegation))
1825 goto no_delegation_unlock;
1827 nfs_mark_delegation_referenced(deleg_cur);
1828 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
1830 no_delegation_unlock:
1831 spin_unlock(&deleg_cur->lock);
1834 update_open_stateflags(state, fmode);
1835 spin_unlock(&state->owner->so_lock);
1838 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1839 nfs4_schedule_state_manager(clp);
1840 if (freeme.type != 0)
1841 nfs4_test_and_free_stateid(server, &freeme,
1842 state->owner->so_cred);
1847 static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1848 const nfs4_stateid *stateid)
1850 struct nfs4_state *state = lsp->ls_state;
1853 spin_lock(&state->state_lock);
1854 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1856 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1858 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1861 spin_unlock(&state->state_lock);
1865 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1867 struct nfs_delegation *delegation;
1869 fmode &= FMODE_READ|FMODE_WRITE;
1871 delegation = nfs4_get_valid_delegation(inode);
1872 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1877 nfs4_inode_return_delegation(inode);
1880 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1882 struct nfs4_state *state = opendata->state;
1883 struct nfs_delegation *delegation;
1884 int open_mode = opendata->o_arg.open_flags;
1885 fmode_t fmode = opendata->o_arg.fmode;
1886 enum open_claim_type4 claim = opendata->o_arg.claim;
1887 nfs4_stateid stateid;
1891 spin_lock(&state->owner->so_lock);
1892 if (can_open_cached(state, fmode, open_mode, claim)) {
1893 update_open_stateflags(state, fmode);
1894 spin_unlock(&state->owner->so_lock);
1895 goto out_return_state;
1897 spin_unlock(&state->owner->so_lock);
1899 delegation = nfs4_get_valid_delegation(state->inode);
1900 if (!can_open_delegated(delegation, fmode, claim)) {
1904 /* Save the delegation */
1905 nfs4_stateid_copy(&stateid, &delegation->stateid);
1907 nfs_release_seqid(opendata->o_arg.seqid);
1908 if (!opendata->is_recover) {
1909 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1915 /* Try to update the stateid using the delegation */
1916 if (update_open_stateid(state, NULL, &stateid, fmode))
1917 goto out_return_state;
1920 return ERR_PTR(ret);
1922 refcount_inc(&state->count);
1927 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1929 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1930 struct nfs_delegation *delegation;
1931 int delegation_flags = 0;
1934 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1936 delegation_flags = delegation->flags;
1938 switch (data->o_arg.claim) {
1941 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1942 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1943 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1944 "returning a delegation for "
1945 "OPEN(CLAIM_DELEGATE_CUR)\n",
1949 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1950 nfs_inode_set_delegation(state->inode,
1951 data->owner->so_cred,
1952 data->o_res.delegation_type,
1953 &data->o_res.delegation,
1954 data->o_res.pagemod_limit);
1956 nfs_inode_reclaim_delegation(state->inode,
1957 data->owner->so_cred,
1958 data->o_res.delegation_type,
1959 &data->o_res.delegation,
1960 data->o_res.pagemod_limit);
1962 if (data->o_res.do_recall)
1963 nfs_async_inode_return_delegation(state->inode,
1964 &data->o_res.delegation);
1968 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1969 * and update the nfs4_state.
1971 static struct nfs4_state *
1972 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1974 struct inode *inode = data->state->inode;
1975 struct nfs4_state *state = data->state;
1978 if (!data->rpc_done) {
1979 if (data->rpc_status)
1980 return ERR_PTR(data->rpc_status);
1981 return nfs4_try_open_cached(data);
1984 ret = nfs_refresh_inode(inode, &data->f_attr);
1986 return ERR_PTR(ret);
1988 if (data->o_res.delegation_type != 0)
1989 nfs4_opendata_check_deleg(data, state);
1991 if (!update_open_stateid(state, &data->o_res.stateid,
1992 NULL, data->o_arg.fmode))
1993 return ERR_PTR(-EAGAIN);
1994 refcount_inc(&state->count);
1999 static struct inode *
2000 nfs4_opendata_get_inode(struct nfs4_opendata *data)
2002 struct inode *inode;
2004 switch (data->o_arg.claim) {
2005 case NFS4_OPEN_CLAIM_NULL:
2006 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
2007 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
2008 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
2009 return ERR_PTR(-EAGAIN);
2010 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
2011 &data->f_attr, data->f_label);
2014 inode = d_inode(data->dentry);
2016 nfs_refresh_inode(inode, &data->f_attr);
2021 static struct nfs4_state *
2022 nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
2024 struct nfs4_state *state;
2025 struct inode *inode;
2027 inode = nfs4_opendata_get_inode(data);
2029 return ERR_CAST(inode);
2030 if (data->state != NULL && data->state->inode == inode) {
2031 state = data->state;
2032 refcount_inc(&state->count);
2034 state = nfs4_get_open_state(inode, data->owner);
2037 state = ERR_PTR(-ENOMEM);
2041 static struct nfs4_state *
2042 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2044 struct nfs4_state *state;
2046 if (!data->rpc_done) {
2047 state = nfs4_try_open_cached(data);
2048 trace_nfs4_cached_open(data->state);
2052 state = nfs4_opendata_find_nfs4_state(data);
2056 if (data->o_res.delegation_type != 0)
2057 nfs4_opendata_check_deleg(data, state);
2058 if (!update_open_stateid(state, &data->o_res.stateid,
2059 NULL, data->o_arg.fmode)) {
2060 nfs4_put_open_state(state);
2061 state = ERR_PTR(-EAGAIN);
2064 nfs_release_seqid(data->o_arg.seqid);
2068 static struct nfs4_state *
2069 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2071 struct nfs4_state *ret;
2073 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
2074 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
2076 ret = _nfs4_opendata_to_nfs4_state(data);
2077 nfs4_sequence_free_slot(&data->o_res.seq_res);
2081 static struct nfs_open_context *
2082 nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode)
2084 struct nfs_inode *nfsi = NFS_I(state->inode);
2085 struct nfs_open_context *ctx;
2088 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
2089 if (ctx->state != state)
2091 if ((ctx->mode & mode) != mode)
2093 if (!get_nfs_open_context(ctx))
2099 return ERR_PTR(-ENOENT);
2102 static struct nfs_open_context *
2103 nfs4_state_find_open_context(struct nfs4_state *state)
2105 struct nfs_open_context *ctx;
2107 ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE);
2110 ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE);
2113 return nfs4_state_find_open_context_mode(state, FMODE_READ);
2116 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
2117 struct nfs4_state *state, enum open_claim_type4 claim)
2119 struct nfs4_opendata *opendata;
2121 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
2122 NULL, claim, GFP_NOFS);
2123 if (opendata == NULL)
2124 return ERR_PTR(-ENOMEM);
2125 opendata->state = state;
2126 refcount_inc(&state->count);
2130 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
2133 struct nfs4_state *newstate;
2134 struct nfs_server *server = NFS_SB(opendata->dentry->d_sb);
2135 int openflags = opendata->o_arg.open_flags;
2138 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
2140 opendata->o_arg.fmode = fmode;
2141 opendata->o_arg.share_access =
2142 nfs4_map_atomic_open_share(server, fmode, openflags);
2143 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
2144 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
2145 nfs4_init_opendata_res(opendata);
2146 ret = _nfs4_recover_proc_open(opendata);
2149 newstate = nfs4_opendata_to_nfs4_state(opendata);
2150 if (IS_ERR(newstate))
2151 return PTR_ERR(newstate);
2152 if (newstate != opendata->state)
2154 nfs4_close_state(newstate, fmode);
2158 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
2162 /* memory barrier prior to reading state->n_* */
2164 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2167 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2170 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2174 * We may have performed cached opens for all three recoveries.
2175 * Check if we need to update the current stateid.
2177 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
2178 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
2179 write_seqlock(&state->seqlock);
2180 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2181 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2182 write_sequnlock(&state->seqlock);
2189 * reclaim state on the server after a reboot.
2191 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2193 struct nfs_delegation *delegation;
2194 struct nfs4_opendata *opendata;
2195 fmode_t delegation_type = 0;
2198 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2199 NFS4_OPEN_CLAIM_PREVIOUS);
2200 if (IS_ERR(opendata))
2201 return PTR_ERR(opendata);
2203 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2204 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
2205 delegation_type = delegation->type;
2207 opendata->o_arg.u.delegation_type = delegation_type;
2208 status = nfs4_open_recover(opendata, state);
2209 nfs4_opendata_put(opendata);
2213 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2215 struct nfs_server *server = NFS_SERVER(state->inode);
2216 struct nfs4_exception exception = { };
2219 err = _nfs4_do_open_reclaim(ctx, state);
2220 trace_nfs4_open_reclaim(ctx, 0, err);
2221 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2223 if (err != -NFS4ERR_DELAY)
2225 nfs4_handle_exception(server, err, &exception);
2226 } while (exception.retry);
2230 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2232 struct nfs_open_context *ctx;
2235 ctx = nfs4_state_find_open_context(state);
2238 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2239 nfs_state_clear_open_state_flags(state);
2240 ret = nfs4_do_open_reclaim(ctx, state);
2241 put_nfs_open_context(ctx);
2245 static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, struct file_lock *fl, int err)
2249 printk(KERN_ERR "NFS: %s: unhandled error "
2250 "%d.\n", __func__, err);
2257 case -NFS4ERR_BADSESSION:
2258 case -NFS4ERR_BADSLOT:
2259 case -NFS4ERR_BAD_HIGH_SLOT:
2260 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2261 case -NFS4ERR_DEADSESSION:
2263 case -NFS4ERR_STALE_CLIENTID:
2264 case -NFS4ERR_STALE_STATEID:
2265 /* Don't recall a delegation if it was lost */
2266 nfs4_schedule_lease_recovery(server->nfs_client);
2268 case -NFS4ERR_MOVED:
2269 nfs4_schedule_migration_recovery(server);
2271 case -NFS4ERR_LEASE_MOVED:
2272 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2274 case -NFS4ERR_DELEG_REVOKED:
2275 case -NFS4ERR_ADMIN_REVOKED:
2276 case -NFS4ERR_EXPIRED:
2277 case -NFS4ERR_BAD_STATEID:
2278 case -NFS4ERR_OPENMODE:
2279 nfs_inode_find_state_and_recover(state->inode,
2281 nfs4_schedule_stateid_recovery(server, state);
2283 case -NFS4ERR_DELAY:
2284 case -NFS4ERR_GRACE:
2288 case -NFS4ERR_DENIED:
2290 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2292 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2299 int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2300 struct nfs4_state *state, const nfs4_stateid *stateid)
2302 struct nfs_server *server = NFS_SERVER(state->inode);
2303 struct nfs4_opendata *opendata;
2306 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2307 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2308 if (IS_ERR(opendata))
2309 return PTR_ERR(opendata);
2310 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
2311 if (!test_bit(NFS_O_RDWR_STATE, &state->flags)) {
2312 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2316 if (!test_bit(NFS_O_WRONLY_STATE, &state->flags)) {
2317 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2321 if (!test_bit(NFS_O_RDONLY_STATE, &state->flags)) {
2322 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2326 nfs_state_clear_delegation(state);
2328 nfs4_opendata_put(opendata);
2329 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
2332 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2334 struct nfs4_opendata *data = calldata;
2336 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2337 &data->c_arg.seq_args, &data->c_res.seq_res, task);
2340 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2342 struct nfs4_opendata *data = calldata;
2344 nfs40_sequence_done(task, &data->c_res.seq_res);
2346 data->rpc_status = task->tk_status;
2347 if (data->rpc_status == 0) {
2348 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
2349 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2350 renew_lease(data->o_res.server, data->timestamp);
2351 data->rpc_done = true;
2355 static void nfs4_open_confirm_release(void *calldata)
2357 struct nfs4_opendata *data = calldata;
2358 struct nfs4_state *state = NULL;
2360 /* If this request hasn't been cancelled, do nothing */
2361 if (!data->cancelled)
2363 /* In case of error, no cleanup! */
2364 if (!data->rpc_done)
2366 state = nfs4_opendata_to_nfs4_state(data);
2368 nfs4_close_state(state, data->o_arg.fmode);
2370 nfs4_opendata_put(data);
2373 static const struct rpc_call_ops nfs4_open_confirm_ops = {
2374 .rpc_call_prepare = nfs4_open_confirm_prepare,
2375 .rpc_call_done = nfs4_open_confirm_done,
2376 .rpc_release = nfs4_open_confirm_release,
2380 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2382 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2384 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
2385 struct rpc_task *task;
2386 struct rpc_message msg = {
2387 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2388 .rpc_argp = &data->c_arg,
2389 .rpc_resp = &data->c_res,
2390 .rpc_cred = data->owner->so_cred,
2392 struct rpc_task_setup task_setup_data = {
2393 .rpc_client = server->client,
2394 .rpc_message = &msg,
2395 .callback_ops = &nfs4_open_confirm_ops,
2396 .callback_data = data,
2397 .workqueue = nfsiod_workqueue,
2398 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
2402 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2404 kref_get(&data->kref);
2405 data->rpc_done = false;
2406 data->rpc_status = 0;
2407 data->timestamp = jiffies;
2408 task = rpc_run_task(&task_setup_data);
2410 return PTR_ERR(task);
2411 status = rpc_wait_for_completion_task(task);
2413 data->cancelled = true;
2416 status = data->rpc_status;
2421 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
2423 struct nfs4_opendata *data = calldata;
2424 struct nfs4_state_owner *sp = data->owner;
2425 struct nfs_client *clp = sp->so_server->nfs_client;
2426 enum open_claim_type4 claim = data->o_arg.claim;
2428 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
2431 * Check if we still need to send an OPEN call, or if we can use
2432 * a delegation instead.
2434 if (data->state != NULL) {
2435 struct nfs_delegation *delegation;
2437 if (can_open_cached(data->state, data->o_arg.fmode,
2438 data->o_arg.open_flags, claim))
2441 delegation = nfs4_get_valid_delegation(data->state->inode);
2442 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
2443 goto unlock_no_action;
2446 /* Update client id. */
2447 data->o_arg.clientid = clp->cl_clientid;
2451 case NFS4_OPEN_CLAIM_PREVIOUS:
2452 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2453 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
2454 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
2456 case NFS4_OPEN_CLAIM_FH:
2457 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
2459 data->timestamp = jiffies;
2460 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
2461 &data->o_arg.seq_args,
2462 &data->o_res.seq_res,
2464 nfs_release_seqid(data->o_arg.seqid);
2466 /* Set the create mode (note dependency on the session type) */
2467 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2468 if (data->o_arg.open_flags & O_EXCL) {
2469 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2470 if (nfs4_has_persistent_session(clp))
2471 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2472 else if (clp->cl_mvops->minor_version > 0)
2473 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2477 trace_nfs4_cached_open(data->state);
2480 task->tk_action = NULL;
2482 nfs4_sequence_done(task, &data->o_res.seq_res);
2485 static void nfs4_open_done(struct rpc_task *task, void *calldata)
2487 struct nfs4_opendata *data = calldata;
2489 data->rpc_status = task->tk_status;
2491 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
2494 if (task->tk_status == 0) {
2495 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2496 switch (data->o_res.f_attr->mode & S_IFMT) {
2500 data->rpc_status = -ELOOP;
2503 data->rpc_status = -EISDIR;
2506 data->rpc_status = -ENOTDIR;
2509 renew_lease(data->o_res.server, data->timestamp);
2510 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2511 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2513 data->rpc_done = true;
2516 static void nfs4_open_release(void *calldata)
2518 struct nfs4_opendata *data = calldata;
2519 struct nfs4_state *state = NULL;
2521 /* If this request hasn't been cancelled, do nothing */
2522 if (!data->cancelled)
2524 /* In case of error, no cleanup! */
2525 if (data->rpc_status != 0 || !data->rpc_done)
2527 /* In case we need an open_confirm, no cleanup! */
2528 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2530 state = nfs4_opendata_to_nfs4_state(data);
2532 nfs4_close_state(state, data->o_arg.fmode);
2534 nfs4_opendata_put(data);
2537 static const struct rpc_call_ops nfs4_open_ops = {
2538 .rpc_call_prepare = nfs4_open_prepare,
2539 .rpc_call_done = nfs4_open_done,
2540 .rpc_release = nfs4_open_release,
2543 static int nfs4_run_open_task(struct nfs4_opendata *data,
2544 struct nfs_open_context *ctx)
2546 struct inode *dir = d_inode(data->dir);
2547 struct nfs_server *server = NFS_SERVER(dir);
2548 struct nfs_openargs *o_arg = &data->o_arg;
2549 struct nfs_openres *o_res = &data->o_res;
2550 struct rpc_task *task;
2551 struct rpc_message msg = {
2552 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2555 .rpc_cred = data->owner->so_cred,
2557 struct rpc_task_setup task_setup_data = {
2558 .rpc_client = server->client,
2559 .rpc_message = &msg,
2560 .callback_ops = &nfs4_open_ops,
2561 .callback_data = data,
2562 .workqueue = nfsiod_workqueue,
2563 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
2567 kref_get(&data->kref);
2568 data->rpc_done = false;
2569 data->rpc_status = 0;
2570 data->cancelled = false;
2571 data->is_recover = false;
2573 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
2574 data->is_recover = true;
2575 task_setup_data.flags |= RPC_TASK_TIMEOUT;
2577 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
2578 pnfs_lgopen_prepare(data, ctx);
2580 task = rpc_run_task(&task_setup_data);
2582 return PTR_ERR(task);
2583 status = rpc_wait_for_completion_task(task);
2585 data->cancelled = true;
2588 status = data->rpc_status;
2594 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2596 struct inode *dir = d_inode(data->dir);
2597 struct nfs_openres *o_res = &data->o_res;
2600 status = nfs4_run_open_task(data, NULL);
2601 if (status != 0 || !data->rpc_done)
2604 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2606 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
2607 status = _nfs4_proc_open_confirm(data);
2613 * Additional permission checks in order to distinguish between an
2614 * open for read, and an open for execute. This works around the
2615 * fact that NFSv4 OPEN treats read and execute permissions as being
2617 * Note that in the non-execute case, we want to turn off permission
2618 * checking if we just created a new file (POSIX open() semantics).
2620 static int nfs4_opendata_access(const struct cred *cred,
2621 struct nfs4_opendata *opendata,
2622 struct nfs4_state *state, fmode_t fmode,
2625 struct nfs_access_entry cache;
2628 /* access call failed or for some reason the server doesn't
2629 * support any access modes -- defer access call until later */
2630 if (opendata->o_res.access_supported == 0)
2635 * Use openflags to check for exec, because fmode won't
2636 * always have FMODE_EXEC set when file open for exec.
2638 if (openflags & __FMODE_EXEC) {
2639 /* ONLY check for exec rights */
2640 if (S_ISDIR(state->inode->i_mode))
2641 mask = NFS4_ACCESS_LOOKUP;
2643 mask = NFS4_ACCESS_EXECUTE;
2644 } else if ((fmode & FMODE_READ) && !opendata->file_created)
2645 mask = NFS4_ACCESS_READ;
2648 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2649 nfs_access_add_cache(state->inode, &cache);
2651 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2652 if ((mask & ~cache.mask & flags) == 0)
2659 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2661 static int _nfs4_proc_open(struct nfs4_opendata *data,
2662 struct nfs_open_context *ctx)
2664 struct inode *dir = d_inode(data->dir);
2665 struct nfs_server *server = NFS_SERVER(dir);
2666 struct nfs_openargs *o_arg = &data->o_arg;
2667 struct nfs_openres *o_res = &data->o_res;
2670 status = nfs4_run_open_task(data, ctx);
2671 if (!data->rpc_done)
2674 if (status == -NFS4ERR_BADNAME &&
2675 !(o_arg->open_flags & O_CREAT))
2680 nfs_fattr_map_and_free_names(server, &data->f_attr);
2682 if (o_arg->open_flags & O_CREAT) {
2683 if (o_arg->open_flags & O_EXCL)
2684 data->file_created = true;
2685 else if (o_res->cinfo.before != o_res->cinfo.after)
2686 data->file_created = true;
2687 if (data->file_created ||
2688 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
2689 nfs4_update_changeattr(dir, &o_res->cinfo,
2690 o_res->f_attr->time_start,
2691 NFS_INO_INVALID_DATA);
2693 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2694 server->caps &= ~NFS_CAP_POSIX_LOCK;
2695 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2696 status = _nfs4_proc_open_confirm(data);
2700 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2701 nfs4_sequence_free_slot(&o_res->seq_res);
2702 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr,
2703 o_res->f_label, NULL);
2710 * reclaim state on the server after a network partition.
2711 * Assumes caller holds the appropriate lock
2713 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2715 struct nfs4_opendata *opendata;
2718 opendata = nfs4_open_recoverdata_alloc(ctx, state, NFS4_OPEN_CLAIM_FH);
2719 if (IS_ERR(opendata))
2720 return PTR_ERR(opendata);
2722 * We're not recovering a delegation, so ask for no delegation.
2723 * Otherwise the recovery thread could deadlock with an outstanding
2724 * delegation return.
2726 opendata->o_arg.open_flags = O_DIRECT;
2727 ret = nfs4_open_recover(opendata, state);
2729 d_drop(ctx->dentry);
2730 nfs4_opendata_put(opendata);
2734 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2736 struct nfs_server *server = NFS_SERVER(state->inode);
2737 struct nfs4_exception exception = { };
2741 err = _nfs4_open_expired(ctx, state);
2742 trace_nfs4_open_expired(ctx, 0, err);
2743 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2748 case -NFS4ERR_GRACE:
2749 case -NFS4ERR_DELAY:
2750 nfs4_handle_exception(server, err, &exception);
2753 } while (exception.retry);
2758 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2760 struct nfs_open_context *ctx;
2763 ctx = nfs4_state_find_open_context(state);
2766 ret = nfs4_do_open_expired(ctx, state);
2767 put_nfs_open_context(ctx);
2771 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2772 const nfs4_stateid *stateid)
2774 nfs_remove_bad_delegation(state->inode, stateid);
2775 nfs_state_clear_delegation(state);
2778 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2780 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2781 nfs_finish_clear_delegation_stateid(state, NULL);
2784 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2786 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2787 nfs40_clear_delegation_stateid(state);
2788 nfs_state_clear_open_state_flags(state);
2789 return nfs4_open_expired(sp, state);
2792 static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2793 nfs4_stateid *stateid,
2794 const struct cred *cred)
2796 return -NFS4ERR_BAD_STATEID;
2799 #if defined(CONFIG_NFS_V4_1)
2800 static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2801 nfs4_stateid *stateid,
2802 const struct cred *cred)
2806 switch (stateid->type) {
2809 case NFS4_INVALID_STATEID_TYPE:
2810 case NFS4_SPECIAL_STATEID_TYPE:
2811 return -NFS4ERR_BAD_STATEID;
2812 case NFS4_REVOKED_STATEID_TYPE:
2816 status = nfs41_test_stateid(server, stateid, cred);
2818 case -NFS4ERR_EXPIRED:
2819 case -NFS4ERR_ADMIN_REVOKED:
2820 case -NFS4ERR_DELEG_REVOKED:
2826 /* Ack the revoked state to the server */
2827 nfs41_free_stateid(server, stateid, cred, true);
2828 return -NFS4ERR_EXPIRED;
2831 static int nfs41_check_delegation_stateid(struct nfs4_state *state)
2833 struct nfs_server *server = NFS_SERVER(state->inode);
2834 nfs4_stateid stateid;
2835 struct nfs_delegation *delegation;
2836 const struct cred *cred = NULL;
2837 int status, ret = NFS_OK;
2839 /* Get the delegation credential for use by test/free_stateid */
2841 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2842 if (delegation == NULL) {
2844 nfs_state_clear_delegation(state);
2848 spin_lock(&delegation->lock);
2849 nfs4_stateid_copy(&stateid, &delegation->stateid);
2851 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2852 &delegation->flags)) {
2853 spin_unlock(&delegation->lock);
2858 if (delegation->cred)
2859 cred = get_cred(delegation->cred);
2860 spin_unlock(&delegation->lock);
2862 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
2863 trace_nfs4_test_delegation_stateid(state, NULL, status);
2864 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
2865 nfs_finish_clear_delegation_stateid(state, &stateid);
2873 static void nfs41_delegation_recover_stateid(struct nfs4_state *state)
2877 if (test_bit(NFS_DELEGATED_STATE, &state->flags) &&
2878 nfs4_copy_delegation_stateid(state->inode, state->state,
2880 nfs4_stateid_match_other(&state->stateid, &tmp))
2881 nfs_state_set_delegation(state, &tmp, state->state);
2883 nfs_state_clear_delegation(state);
2887 * nfs41_check_expired_locks - possibly free a lock stateid
2889 * @state: NFSv4 state for an inode
2891 * Returns NFS_OK if recovery for this stateid is now finished.
2892 * Otherwise a negative NFS4ERR value is returned.
2894 static int nfs41_check_expired_locks(struct nfs4_state *state)
2896 int status, ret = NFS_OK;
2897 struct nfs4_lock_state *lsp, *prev = NULL;
2898 struct nfs_server *server = NFS_SERVER(state->inode);
2900 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2903 spin_lock(&state->state_lock);
2904 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2905 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2906 const struct cred *cred = lsp->ls_state->owner->so_cred;
2908 refcount_inc(&lsp->ls_count);
2909 spin_unlock(&state->state_lock);
2911 nfs4_put_lock_state(prev);
2914 status = nfs41_test_and_free_expired_stateid(server,
2917 trace_nfs4_test_lock_stateid(state, lsp, status);
2918 if (status == -NFS4ERR_EXPIRED ||
2919 status == -NFS4ERR_BAD_STATEID) {
2920 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
2921 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
2922 if (!recover_lost_locks)
2923 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2924 } else if (status != NFS_OK) {
2926 nfs4_put_lock_state(prev);
2929 spin_lock(&state->state_lock);
2932 spin_unlock(&state->state_lock);
2933 nfs4_put_lock_state(prev);
2939 * nfs41_check_open_stateid - possibly free an open stateid
2941 * @state: NFSv4 state for an inode
2943 * Returns NFS_OK if recovery for this stateid is now finished.
2944 * Otherwise a negative NFS4ERR value is returned.
2946 static int nfs41_check_open_stateid(struct nfs4_state *state)
2948 struct nfs_server *server = NFS_SERVER(state->inode);
2949 nfs4_stateid *stateid = &state->open_stateid;
2950 const struct cred *cred = state->owner->so_cred;
2953 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0)
2954 return -NFS4ERR_BAD_STATEID;
2955 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
2956 trace_nfs4_test_open_stateid(state, NULL, status);
2957 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
2958 nfs_state_clear_open_state_flags(state);
2959 stateid->type = NFS4_INVALID_STATEID_TYPE;
2962 if (nfs_open_stateid_recover_openmode(state))
2963 return -NFS4ERR_OPENMODE;
2967 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2971 status = nfs41_check_delegation_stateid(state);
2972 if (status != NFS_OK)
2974 nfs41_delegation_recover_stateid(state);
2976 status = nfs41_check_expired_locks(state);
2977 if (status != NFS_OK)
2979 status = nfs41_check_open_stateid(state);
2980 if (status != NFS_OK)
2981 status = nfs4_open_expired(sp, state);
2987 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2988 * fields corresponding to attributes that were used to store the verifier.
2989 * Make sure we clobber those fields in the later setattr call
2991 static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2992 struct iattr *sattr, struct nfs4_label **label)
2994 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2999 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
3000 attrset[i] = opendata->o_res.attrset[i];
3001 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
3002 attrset[i] &= ~bitmask[i];
3005 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
3006 sattr->ia_valid : 0;
3008 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
3009 if (sattr->ia_valid & ATTR_ATIME_SET)
3010 ret |= ATTR_ATIME_SET;
3015 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
3016 if (sattr->ia_valid & ATTR_MTIME_SET)
3017 ret |= ATTR_MTIME_SET;
3022 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
3027 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
3028 int flags, struct nfs_open_context *ctx)
3030 struct nfs4_state_owner *sp = opendata->owner;
3031 struct nfs_server *server = sp->so_server;
3032 struct dentry *dentry;
3033 struct nfs4_state *state;
3034 fmode_t acc_mode = _nfs4_ctx_to_accessmode(ctx);
3035 struct inode *dir = d_inode(opendata->dir);
3036 unsigned long dir_verifier;
3040 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
3041 dir_verifier = nfs_save_change_attribute(dir);
3043 ret = _nfs4_proc_open(opendata, ctx);
3047 state = _nfs4_opendata_to_nfs4_state(opendata);
3048 ret = PTR_ERR(state);
3052 if (server->caps & NFS_CAP_POSIX_LOCK)
3053 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
3054 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
3055 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
3057 dentry = opendata->dentry;
3058 if (d_really_is_negative(dentry)) {
3059 struct dentry *alias;
3061 alias = d_exact_alias(dentry, state->inode);
3063 alias = d_splice_alias(igrab(state->inode), dentry);
3064 /* d_splice_alias() can't fail here - it's a non-directory */
3067 ctx->dentry = dentry = alias;
3071 switch(opendata->o_arg.claim) {
3074 case NFS4_OPEN_CLAIM_NULL:
3075 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
3076 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
3077 if (!opendata->rpc_done)
3079 if (opendata->o_res.delegation_type != 0)
3080 dir_verifier = nfs_save_change_attribute(dir);
3081 nfs_set_verifier(dentry, dir_verifier);
3084 /* Parse layoutget results before we check for access */
3085 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
3087 ret = nfs4_opendata_access(sp->so_cred, opendata, state,
3092 if (d_inode(dentry) == state->inode) {
3093 nfs_inode_attach_open_context(ctx);
3094 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
3095 nfs4_schedule_stateid_recovery(server, state);
3099 if (!opendata->cancelled) {
3100 if (opendata->lgp) {
3101 nfs4_lgopen_release(opendata->lgp);
3102 opendata->lgp = NULL;
3104 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
3110 * Returns a referenced nfs4_state
3112 static int _nfs4_do_open(struct inode *dir,
3113 struct nfs_open_context *ctx,
3115 const struct nfs4_open_createattrs *c,
3118 struct nfs4_state_owner *sp;
3119 struct nfs4_state *state = NULL;
3120 struct nfs_server *server = NFS_SERVER(dir);
3121 struct nfs4_opendata *opendata;
3122 struct dentry *dentry = ctx->dentry;
3123 const struct cred *cred = ctx->cred;
3124 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
3125 fmode_t fmode = _nfs4_ctx_to_openmode(ctx);
3126 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
3127 struct iattr *sattr = c->sattr;
3128 struct nfs4_label *label = c->label;
3129 struct nfs4_label *olabel = NULL;
3132 /* Protect against reboot recovery conflicts */
3134 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
3136 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
3139 status = nfs4_client_recover_expired_lease(server->nfs_client);
3141 goto err_put_state_owner;
3142 if (d_really_is_positive(dentry))
3143 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
3145 if (d_really_is_positive(dentry))
3146 claim = NFS4_OPEN_CLAIM_FH;
3147 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
3148 c, claim, GFP_KERNEL);
3149 if (opendata == NULL)
3150 goto err_put_state_owner;
3153 olabel = nfs4_label_alloc(server, GFP_KERNEL);
3154 if (IS_ERR(olabel)) {
3155 status = PTR_ERR(olabel);
3156 goto err_opendata_put;
3160 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
3161 if (!opendata->f_attr.mdsthreshold) {
3162 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
3163 if (!opendata->f_attr.mdsthreshold)
3164 goto err_free_label;
3166 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
3168 if (d_really_is_positive(dentry))
3169 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
3171 status = _nfs4_open_and_get_state(opendata, flags, ctx);
3173 goto err_free_label;
3176 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
3177 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
3178 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
3180 * send create attributes which was not set by open
3181 * with an extra setattr.
3183 if (attrs || label) {
3184 unsigned ia_old = sattr->ia_valid;
3186 sattr->ia_valid = attrs;
3187 nfs_fattr_init(opendata->o_res.f_attr);
3188 status = nfs4_do_setattr(state->inode, cred,
3189 opendata->o_res.f_attr, sattr,
3190 ctx, label, olabel);
3192 nfs_setattr_update_inode(state->inode, sattr,
3193 opendata->o_res.f_attr);
3194 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
3196 sattr->ia_valid = ia_old;
3199 if (opened && opendata->file_created)
3202 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
3203 *ctx_th = opendata->f_attr.mdsthreshold;
3204 opendata->f_attr.mdsthreshold = NULL;
3207 nfs4_label_free(olabel);
3209 nfs4_opendata_put(opendata);
3210 nfs4_put_state_owner(sp);
3213 nfs4_label_free(olabel);
3215 nfs4_opendata_put(opendata);
3216 err_put_state_owner:
3217 nfs4_put_state_owner(sp);
3223 static struct nfs4_state *nfs4_do_open(struct inode *dir,
3224 struct nfs_open_context *ctx,
3226 struct iattr *sattr,
3227 struct nfs4_label *label,
3230 struct nfs_server *server = NFS_SERVER(dir);
3231 struct nfs4_exception exception = {
3232 .interruptible = true,
3234 struct nfs4_state *res;
3235 struct nfs4_open_createattrs c = {
3239 [0] = (__u32)jiffies,
3240 [1] = (__u32)current->pid,
3246 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
3248 trace_nfs4_open_file(ctx, flags, status);
3251 /* NOTE: BAD_SEQID means the server and client disagree about the
3252 * book-keeping w.r.t. state-changing operations
3253 * (OPEN/CLOSE/LOCK/LOCKU...)
3254 * It is actually a sign of a bug on the client or on the server.
3256 * If we receive a BAD_SEQID error in the particular case of
3257 * doing an OPEN, we assume that nfs_increment_open_seqid() will
3258 * have unhashed the old state_owner for us, and that we can
3259 * therefore safely retry using a new one. We should still warn
3260 * the user though...
3262 if (status == -NFS4ERR_BAD_SEQID) {
3263 pr_warn_ratelimited("NFS: v4 server %s "
3264 " returned a bad sequence-id error!\n",
3265 NFS_SERVER(dir)->nfs_client->cl_hostname);
3266 exception.retry = 1;
3270 * BAD_STATEID on OPEN means that the server cancelled our
3271 * state before it received the OPEN_CONFIRM.
3272 * Recover by retrying the request as per the discussion
3273 * on Page 181 of RFC3530.
3275 if (status == -NFS4ERR_BAD_STATEID) {
3276 exception.retry = 1;
3279 if (status == -NFS4ERR_EXPIRED) {
3280 nfs4_schedule_lease_recovery(server->nfs_client);
3281 exception.retry = 1;
3284 if (status == -EAGAIN) {
3285 /* We must have found a delegation */
3286 exception.retry = 1;
3289 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3291 res = ERR_PTR(nfs4_handle_exception(server,
3292 status, &exception));
3293 } while (exception.retry);
3297 static int _nfs4_do_setattr(struct inode *inode,
3298 struct nfs_setattrargs *arg,
3299 struct nfs_setattrres *res,
3300 const struct cred *cred,
3301 struct nfs_open_context *ctx)
3303 struct nfs_server *server = NFS_SERVER(inode);
3304 struct rpc_message msg = {
3305 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3310 const struct cred *delegation_cred = NULL;
3311 unsigned long timestamp = jiffies;
3315 nfs_fattr_init(res->fattr);
3317 /* Servers should only apply open mode checks for file size changes */
3318 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
3320 nfs4_inode_make_writeable(inode);
3324 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
3325 /* Use that stateid */
3326 } else if (ctx != NULL && ctx->state) {
3327 struct nfs_lock_context *l_ctx;
3328 if (!nfs4_valid_open_stateid(ctx->state))
3330 l_ctx = nfs_get_lock_context(ctx);
3332 return PTR_ERR(l_ctx);
3333 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3334 &arg->stateid, &delegation_cred);
3335 nfs_put_lock_context(l_ctx);
3338 else if (status == -EAGAIN)
3342 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
3344 if (delegation_cred)
3345 msg.rpc_cred = delegation_cred;
3347 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3349 put_cred(delegation_cred);
3350 if (status == 0 && ctx != NULL)
3351 renew_lease(server, timestamp);
3352 trace_nfs4_setattr(inode, &arg->stateid, status);
3356 static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
3357 struct nfs_fattr *fattr, struct iattr *sattr,
3358 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
3359 struct nfs4_label *olabel)
3361 struct nfs_server *server = NFS_SERVER(inode);
3362 __u32 bitmask[NFS4_BITMASK_SZ];
3363 struct nfs4_state *state = ctx ? ctx->state : NULL;
3364 struct nfs_setattrargs arg = {
3365 .fh = NFS_FH(inode),
3371 struct nfs_setattrres res = {
3376 struct nfs4_exception exception = {
3379 .stateid = &arg.stateid,
3384 nfs4_bitmap_copy_adjust_setattr(bitmask,
3385 nfs4_bitmask(server, olabel),
3388 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
3390 case -NFS4ERR_OPENMODE:
3391 if (!(sattr->ia_valid & ATTR_SIZE)) {
3392 pr_warn_once("NFSv4: server %s is incorrectly "
3393 "applying open mode checks to "
3394 "a SETATTR that is not "
3395 "changing file size.\n",
3396 server->nfs_client->cl_hostname);
3398 if (state && !(state->state & FMODE_WRITE)) {
3400 if (sattr->ia_valid & ATTR_OPEN)
3405 err = nfs4_handle_exception(server, err, &exception);
3406 } while (exception.retry);
3412 nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3414 if (inode == NULL || !nfs_have_layout(inode))
3417 return pnfs_wait_on_layoutreturn(inode, task);
3421 * Update the seqid of an open stateid
3423 static void nfs4_sync_open_stateid(nfs4_stateid *dst,
3424 struct nfs4_state *state)
3431 if (!nfs4_valid_open_stateid(state))
3433 seq = read_seqbegin(&state->seqlock);
3434 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3435 nfs4_stateid_copy(dst, &state->open_stateid);
3436 if (read_seqretry(&state->seqlock, seq))
3440 seqid_open = state->open_stateid.seqid;
3441 if (read_seqretry(&state->seqlock, seq))
3444 dst_seqid = be32_to_cpu(dst->seqid);
3445 if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) < 0)
3446 dst->seqid = seqid_open;
3452 * Update the seqid of an open stateid after receiving
3453 * NFS4ERR_OLD_STATEID
3455 static bool nfs4_refresh_open_old_stateid(nfs4_stateid *dst,
3456 struct nfs4_state *state)
3461 int seq, status = -EAGAIN;
3466 if (!nfs4_valid_open_stateid(state))
3468 seq = read_seqbegin(&state->seqlock);
3469 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3470 if (read_seqretry(&state->seqlock, seq))
3475 write_seqlock(&state->seqlock);
3476 seqid_open = state->open_stateid.seqid;
3478 dst_seqid = be32_to_cpu(dst->seqid);
3480 /* Did another OPEN bump the state's seqid? try again: */
3481 if ((s32)(be32_to_cpu(seqid_open) - dst_seqid) > 0) {
3482 dst->seqid = seqid_open;
3483 write_sequnlock(&state->seqlock);
3488 /* server says we're behind but we haven't seen the update yet */
3489 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
3490 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
3491 write_sequnlock(&state->seqlock);
3492 trace_nfs4_close_stateid_update_wait(state->inode, dst, 0);
3494 if (fatal_signal_pending(current))
3497 if (schedule_timeout(5*HZ) != 0)
3500 finish_wait(&state->waitq, &wait);
3504 if (status == -EINTR)
3507 /* we slept the whole 5 seconds, we must have lost a seqid */
3508 dst->seqid = cpu_to_be32(dst_seqid + 1);
3516 struct nfs4_closedata {
3517 struct inode *inode;
3518 struct nfs4_state *state;
3519 struct nfs_closeargs arg;
3520 struct nfs_closeres res;
3522 struct nfs4_layoutreturn_args arg;
3523 struct nfs4_layoutreturn_res res;
3524 struct nfs4_xdr_opaque_data ld_private;
3528 struct nfs_fattr fattr;
3529 unsigned long timestamp;
3532 static void nfs4_free_closedata(void *data)
3534 struct nfs4_closedata *calldata = data;
3535 struct nfs4_state_owner *sp = calldata->state->owner;
3536 struct super_block *sb = calldata->state->inode->i_sb;
3538 if (calldata->lr.roc)
3539 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3540 calldata->res.lr_ret);
3541 nfs4_put_open_state(calldata->state);
3542 nfs_free_seqid(calldata->arg.seqid);
3543 nfs4_put_state_owner(sp);
3544 nfs_sb_deactive(sb);
3548 static void nfs4_close_done(struct rpc_task *task, void *data)
3550 struct nfs4_closedata *calldata = data;
3551 struct nfs4_state *state = calldata->state;
3552 struct nfs_server *server = NFS_SERVER(calldata->inode);
3553 nfs4_stateid *res_stateid = NULL;
3554 struct nfs4_exception exception = {
3556 .inode = calldata->inode,
3557 .stateid = &calldata->arg.stateid,
3560 dprintk("%s: begin!\n", __func__);
3561 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3563 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
3565 /* Handle Layoutreturn errors */
3566 if (pnfs_roc_done(task, &calldata->arg.lr_args, &calldata->res.lr_res,
3567 &calldata->res.lr_ret) == -EAGAIN)
3570 /* hmm. we are done with the inode, and in the process of freeing
3571 * the state_owner. we keep this around to process errors
3573 switch (task->tk_status) {
3575 res_stateid = &calldata->res.stateid;
3576 renew_lease(server, calldata->timestamp);
3578 case -NFS4ERR_ACCESS:
3579 if (calldata->arg.bitmask != NULL) {
3580 calldata->arg.bitmask = NULL;
3581 calldata->res.fattr = NULL;
3586 case -NFS4ERR_OLD_STATEID:
3587 /* Did we race with OPEN? */
3588 if (nfs4_refresh_open_old_stateid(&calldata->arg.stateid,
3592 case -NFS4ERR_ADMIN_REVOKED:
3593 case -NFS4ERR_STALE_STATEID:
3594 case -NFS4ERR_EXPIRED:
3595 nfs4_free_revoked_stateid(server,
3596 &calldata->arg.stateid,
3597 task->tk_msg.rpc_cred);
3599 case -NFS4ERR_BAD_STATEID:
3600 if (calldata->arg.fmode == 0)
3604 task->tk_status = nfs4_async_handle_exception(task,
3605 server, task->tk_status, &exception);
3606 if (exception.retry)
3609 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3610 res_stateid, calldata->arg.fmode);
3612 task->tk_status = 0;
3613 nfs_release_seqid(calldata->arg.seqid);
3614 nfs_refresh_inode(calldata->inode, &calldata->fattr);
3615 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
3618 task->tk_status = 0;
3619 rpc_restart_call_prepare(task);
3623 static void nfs4_close_prepare(struct rpc_task *task, void *data)
3625 struct nfs4_closedata *calldata = data;
3626 struct nfs4_state *state = calldata->state;
3627 struct inode *inode = calldata->inode;
3628 struct nfs_server *server = NFS_SERVER(inode);
3629 struct pnfs_layout_hdr *lo;
3630 bool is_rdonly, is_wronly, is_rdwr;
3633 dprintk("%s: begin!\n", __func__);
3634 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3637 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
3638 spin_lock(&state->owner->so_lock);
3639 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3640 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3641 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
3642 /* Calculate the change in open mode */
3643 calldata->arg.fmode = 0;
3644 if (state->n_rdwr == 0) {
3645 if (state->n_rdonly == 0)
3646 call_close |= is_rdonly;
3648 calldata->arg.fmode |= FMODE_READ;
3649 if (state->n_wronly == 0)
3650 call_close |= is_wronly;
3652 calldata->arg.fmode |= FMODE_WRITE;
3653 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3654 call_close |= is_rdwr;
3656 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3658 nfs4_sync_open_stateid(&calldata->arg.stateid, state);
3659 if (!nfs4_valid_open_stateid(state))
3661 spin_unlock(&state->owner->so_lock);
3664 /* Note: exit _without_ calling nfs4_close_done */
3668 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
3669 nfs_release_seqid(calldata->arg.seqid);
3673 lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3674 if (lo && !pnfs_layout_is_valid(lo)) {
3675 calldata->arg.lr_args = NULL;
3676 calldata->res.lr_res = NULL;
3679 if (calldata->arg.fmode == 0)
3680 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
3682 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
3683 /* Close-to-open cache consistency revalidation */
3684 if (!nfs4_have_delegation(inode, FMODE_READ)) {
3685 nfs4_bitmask_set(calldata->arg.bitmask_store,
3686 server->cache_consistency_bitmask,
3687 inode, server, NULL);
3688 calldata->arg.bitmask = calldata->arg.bitmask_store;
3690 calldata->arg.bitmask = NULL;
3693 calldata->arg.share_access =
3694 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3695 calldata->arg.fmode, 0);
3697 if (calldata->res.fattr == NULL)
3698 calldata->arg.bitmask = NULL;
3699 else if (calldata->arg.bitmask == NULL)
3700 calldata->res.fattr = NULL;
3701 calldata->timestamp = jiffies;
3702 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
3703 &calldata->arg.seq_args,
3704 &calldata->res.seq_res,
3706 nfs_release_seqid(calldata->arg.seqid);
3707 dprintk("%s: done!\n", __func__);
3710 task->tk_action = NULL;
3712 nfs4_sequence_done(task, &calldata->res.seq_res);
3715 static const struct rpc_call_ops nfs4_close_ops = {
3716 .rpc_call_prepare = nfs4_close_prepare,
3717 .rpc_call_done = nfs4_close_done,
3718 .rpc_release = nfs4_free_closedata,
3722 * It is possible for data to be read/written from a mem-mapped file
3723 * after the sys_close call (which hits the vfs layer as a flush).
3724 * This means that we can't safely call nfsv4 close on a file until
3725 * the inode is cleared. This in turn means that we are not good
3726 * NFSv4 citizens - we do not indicate to the server to update the file's
3727 * share state even when we are done with one of the three share
3728 * stateid's in the inode.
3730 * NOTE: Caller must be holding the sp->so_owner semaphore!
3732 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
3734 struct nfs_server *server = NFS_SERVER(state->inode);
3735 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
3736 struct nfs4_closedata *calldata;
3737 struct nfs4_state_owner *sp = state->owner;
3738 struct rpc_task *task;
3739 struct rpc_message msg = {
3740 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3741 .rpc_cred = state->owner->so_cred,
3743 struct rpc_task_setup task_setup_data = {
3744 .rpc_client = server->client,
3745 .rpc_message = &msg,
3746 .callback_ops = &nfs4_close_ops,
3747 .workqueue = nfsiod_workqueue,
3748 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
3750 int status = -ENOMEM;
3752 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3753 &task_setup_data.rpc_client, &msg);
3755 calldata = kzalloc(sizeof(*calldata), gfp_mask);
3756 if (calldata == NULL)
3758 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
3759 calldata->inode = state->inode;
3760 calldata->state = state;
3761 calldata->arg.fh = NFS_FH(state->inode);
3762 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3763 goto out_free_calldata;
3764 /* Serialization for the sequence id */
3765 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3766 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
3767 if (IS_ERR(calldata->arg.seqid))
3768 goto out_free_calldata;
3769 nfs_fattr_init(&calldata->fattr);
3770 calldata->arg.fmode = 0;
3771 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
3772 calldata->res.fattr = &calldata->fattr;
3773 calldata->res.seqid = calldata->arg.seqid;
3774 calldata->res.server = server;
3775 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3776 calldata->lr.roc = pnfs_roc(state->inode,
3777 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3778 if (calldata->lr.roc) {
3779 calldata->arg.lr_args = &calldata->lr.arg;
3780 calldata->res.lr_res = &calldata->lr.res;
3782 nfs_sb_active(calldata->inode->i_sb);
3784 msg.rpc_argp = &calldata->arg;
3785 msg.rpc_resp = &calldata->res;
3786 task_setup_data.callback_data = calldata;
3787 task = rpc_run_task(&task_setup_data);
3789 return PTR_ERR(task);
3792 status = rpc_wait_for_completion_task(task);
3798 nfs4_put_open_state(state);
3799 nfs4_put_state_owner(sp);
3803 static struct inode *
3804 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3805 int open_flags, struct iattr *attr, int *opened)
3807 struct nfs4_state *state;
3808 struct nfs4_label l, *label;
3810 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
3812 /* Protect against concurrent sillydeletes */
3813 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
3815 nfs4_label_release_security(label);
3818 return ERR_CAST(state);
3819 return state->inode;
3822 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
3824 if (ctx->state == NULL)
3827 nfs4_close_sync(ctx->state, _nfs4_ctx_to_openmode(ctx));
3829 nfs4_close_state(ctx->state, _nfs4_ctx_to_openmode(ctx));
3832 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3833 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3834 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_XATTR_SUPPORT - 1UL)
3836 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3838 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
3839 struct nfs4_server_caps_arg args = {
3843 struct nfs4_server_caps_res res = {};
3844 struct rpc_message msg = {
3845 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
3852 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3853 FATTR4_WORD0_FH_EXPIRE_TYPE |
3854 FATTR4_WORD0_LINK_SUPPORT |
3855 FATTR4_WORD0_SYMLINK_SUPPORT |
3856 FATTR4_WORD0_ACLSUPPORT;
3858 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3860 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3862 /* Sanity check the server answers */
3863 switch (minorversion) {
3865 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3866 res.attr_bitmask[2] = 0;
3869 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3872 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3874 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
3875 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3876 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3877 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3878 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
3879 NFS_CAP_CTIME|NFS_CAP_MTIME|
3880 NFS_CAP_SECURITY_LABEL);
3881 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3882 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3883 server->caps |= NFS_CAP_ACLS;
3884 if (res.has_links != 0)
3885 server->caps |= NFS_CAP_HARDLINKS;
3886 if (res.has_symlinks != 0)
3887 server->caps |= NFS_CAP_SYMLINKS;
3888 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3889 server->caps |= NFS_CAP_FILEID;
3890 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3891 server->caps |= NFS_CAP_MODE;
3892 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3893 server->caps |= NFS_CAP_NLINK;
3894 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3895 server->caps |= NFS_CAP_OWNER;
3896 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3897 server->caps |= NFS_CAP_OWNER_GROUP;
3898 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3899 server->caps |= NFS_CAP_ATIME;
3900 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3901 server->caps |= NFS_CAP_CTIME;
3902 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3903 server->caps |= NFS_CAP_MTIME;
3904 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3905 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3906 server->caps |= NFS_CAP_SECURITY_LABEL;
3908 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3909 sizeof(server->attr_bitmask));
3910 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3912 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3913 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3914 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
3915 server->cache_consistency_bitmask[2] = 0;
3917 /* Avoid a regression due to buggy server */
3918 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3919 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
3920 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3921 sizeof(server->exclcreat_bitmask));
3923 server->acl_bitmask = res.acl_bitmask;
3924 server->fh_expire_type = res.fh_expire_type;
3930 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3932 struct nfs4_exception exception = {
3933 .interruptible = true,
3937 err = nfs4_handle_exception(server,
3938 _nfs4_server_capabilities(server, fhandle),
3940 } while (exception.retry);
3944 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3945 struct nfs_fsinfo *info)
3948 struct nfs4_lookup_root_arg args = {
3951 struct nfs4_lookup_res res = {
3953 .fattr = info->fattr,
3956 struct rpc_message msg = {
3957 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3962 bitmask[0] = nfs4_fattr_bitmap[0];
3963 bitmask[1] = nfs4_fattr_bitmap[1];
3965 * Process the label in the upcoming getfattr
3967 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3969 nfs_fattr_init(info->fattr);
3970 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3973 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3974 struct nfs_fsinfo *info)
3976 struct nfs4_exception exception = {
3977 .interruptible = true,
3981 err = _nfs4_lookup_root(server, fhandle, info);
3982 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
3985 case -NFS4ERR_WRONGSEC:
3988 err = nfs4_handle_exception(server, err, &exception);
3990 } while (exception.retry);
3995 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3996 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3998 struct rpc_auth_create_args auth_args = {
3999 .pseudoflavor = flavor,
4001 struct rpc_auth *auth;
4003 auth = rpcauth_create(&auth_args, server->client);
4006 return nfs4_lookup_root(server, fhandle, info);
4010 * Retry pseudoroot lookup with various security flavors. We do this when:
4012 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
4013 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
4015 * Returns zero on success, or a negative NFS4ERR value, or a
4016 * negative errno value.
4018 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
4019 struct nfs_fsinfo *info)
4021 /* Per 3530bis 15.33.5 */
4022 static const rpc_authflavor_t flav_array[] = {
4026 RPC_AUTH_UNIX, /* courtesy */
4029 int status = -EPERM;
4032 if (server->auth_info.flavor_len > 0) {
4033 /* try each flavor specified by user */
4034 for (i = 0; i < server->auth_info.flavor_len; i++) {
4035 status = nfs4_lookup_root_sec(server, fhandle, info,
4036 server->auth_info.flavors[i]);
4037 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4042 /* no flavors specified by user, try default list */
4043 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
4044 status = nfs4_lookup_root_sec(server, fhandle, info,
4046 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4053 * -EACCES could mean that the user doesn't have correct permissions
4054 * to access the mount. It could also mean that we tried to mount
4055 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
4056 * existing mount programs don't handle -EACCES very well so it should
4057 * be mapped to -EPERM instead.
4059 if (status == -EACCES)
4065 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
4066 * @server: initialized nfs_server handle
4067 * @fhandle: we fill in the pseudo-fs root file handle
4068 * @info: we fill in an FSINFO struct
4069 * @auth_probe: probe the auth flavours
4071 * Returns zero on success, or a negative errno.
4073 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
4074 struct nfs_fsinfo *info,
4080 status = nfs4_lookup_root(server, fhandle, info);
4082 if (auth_probe || status == NFS4ERR_WRONGSEC)
4083 status = server->nfs_client->cl_mvops->find_root_sec(server,
4087 status = nfs4_server_capabilities(server, fhandle);
4089 status = nfs4_do_fsinfo(server, fhandle, info);
4091 return nfs4_map_errors(status);
4094 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
4095 struct nfs_fsinfo *info)
4098 struct nfs_fattr *fattr = info->fattr;
4099 struct nfs4_label *label = fattr->label;
4101 error = nfs4_server_capabilities(server, mntfh);
4103 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
4107 error = nfs4_proc_getattr(server, mntfh, fattr, label, NULL);
4109 dprintk("nfs4_get_root: getattr error = %d\n", -error);
4113 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
4114 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
4115 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
4122 * Get locations and (maybe) other attributes of a referral.
4123 * Note that we'll actually follow the referral later when
4124 * we detect fsid mismatch in inode revalidation
4126 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
4127 const struct qstr *name, struct nfs_fattr *fattr,
4128 struct nfs_fh *fhandle)
4130 int status = -ENOMEM;
4131 struct page *page = NULL;
4132 struct nfs4_fs_locations *locations = NULL;
4134 page = alloc_page(GFP_KERNEL);
4137 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4138 if (locations == NULL)
4141 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
4146 * If the fsid didn't change, this is a migration event, not a
4147 * referral. Cause us to drop into the exception handler, which
4148 * will kick off migration recovery.
4150 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
4151 dprintk("%s: server did not return a different fsid for"
4152 " a referral at %s\n", __func__, name->name);
4153 status = -NFS4ERR_MOVED;
4156 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
4157 nfs_fixup_referral_attributes(&locations->fattr);
4159 /* replace the lookup nfs_fattr with the locations nfs_fattr */
4160 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
4161 memset(fhandle, 0, sizeof(struct nfs_fh));
4169 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4170 struct nfs_fattr *fattr, struct nfs4_label *label,
4171 struct inode *inode)
4173 __u32 bitmask[NFS4_BITMASK_SZ];
4174 struct nfs4_getattr_arg args = {
4178 struct nfs4_getattr_res res = {
4183 struct rpc_message msg = {
4184 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4188 unsigned short task_flags = 0;
4190 /* Is this is an attribute revalidation, subject to softreval? */
4191 if (inode && (server->flags & NFS_MOUNT_SOFTREVAL))
4192 task_flags |= RPC_TASK_TIMEOUT;
4194 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode);
4196 nfs_fattr_init(fattr);
4197 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4198 return nfs4_do_call_sync(server->client, server, &msg,
4199 &args.seq_args, &res.seq_res, task_flags);
4202 int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4203 struct nfs_fattr *fattr, struct nfs4_label *label,
4204 struct inode *inode)
4206 struct nfs4_exception exception = {
4207 .interruptible = true,
4211 err = _nfs4_proc_getattr(server, fhandle, fattr, label, inode);
4212 trace_nfs4_getattr(server, fhandle, fattr, err);
4213 err = nfs4_handle_exception(server, err,
4215 } while (exception.retry);
4220 * The file is not closed if it is opened due to the a request to change
4221 * the size of the file. The open call will not be needed once the
4222 * VFS layer lookup-intents are implemented.
4224 * Close is called when the inode is destroyed.
4225 * If we haven't opened the file for O_WRONLY, we
4226 * need to in the size_change case to obtain a stateid.
4229 * Because OPEN is always done by name in nfsv4, it is
4230 * possible that we opened a different file by the same
4231 * name. We can recognize this race condition, but we
4232 * can't do anything about it besides returning an error.
4234 * This will be fixed with VFS changes (lookup-intent).
4237 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
4238 struct iattr *sattr)
4240 struct inode *inode = d_inode(dentry);
4241 const struct cred *cred = NULL;
4242 struct nfs_open_context *ctx = NULL;
4243 struct nfs4_label *label = NULL;
4246 if (pnfs_ld_layoutret_on_setattr(inode) &&
4247 sattr->ia_valid & ATTR_SIZE &&
4248 sattr->ia_size < i_size_read(inode))
4249 pnfs_commit_and_return_layout(inode);
4251 nfs_fattr_init(fattr);
4253 /* Deal with open(O_TRUNC) */
4254 if (sattr->ia_valid & ATTR_OPEN)
4255 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
4257 /* Optimization: if the end result is no change, don't RPC */
4258 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
4261 /* Search for an existing open(O_WRITE) file */
4262 if (sattr->ia_valid & ATTR_FILE) {
4264 ctx = nfs_file_open_context(sattr->ia_file);
4269 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4271 return PTR_ERR(label);
4273 /* Return any delegations if we're going to change ACLs */
4274 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
4275 nfs4_inode_make_writeable(inode);
4277 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
4279 nfs_setattr_update_inode(inode, sattr, fattr);
4280 nfs_setsecurity(inode, fattr, label);
4282 nfs4_label_free(label);
4286 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
4287 struct dentry *dentry, struct nfs_fh *fhandle,
4288 struct nfs_fattr *fattr, struct nfs4_label *label)
4290 struct nfs_server *server = NFS_SERVER(dir);
4292 struct nfs4_lookup_arg args = {
4293 .bitmask = server->attr_bitmask,
4294 .dir_fh = NFS_FH(dir),
4295 .name = &dentry->d_name,
4297 struct nfs4_lookup_res res = {
4303 struct rpc_message msg = {
4304 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4308 unsigned short task_flags = 0;
4310 /* Is this is an attribute revalidation, subject to softreval? */
4311 if (nfs_lookup_is_soft_revalidate(dentry))
4312 task_flags |= RPC_TASK_TIMEOUT;
4314 args.bitmask = nfs4_bitmask(server, label);
4316 nfs_fattr_init(fattr);
4318 dprintk("NFS call lookup %pd2\n", dentry);
4319 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4320 status = nfs4_do_call_sync(clnt, server, &msg,
4321 &args.seq_args, &res.seq_res, task_flags);
4322 dprintk("NFS reply lookup: %d\n", status);
4326 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
4328 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4329 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
4330 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4334 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
4335 struct dentry *dentry, struct nfs_fh *fhandle,
4336 struct nfs_fattr *fattr, struct nfs4_label *label)
4338 struct nfs4_exception exception = {
4339 .interruptible = true,
4341 struct rpc_clnt *client = *clnt;
4342 const struct qstr *name = &dentry->d_name;
4345 err = _nfs4_proc_lookup(client, dir, dentry, fhandle, fattr, label);
4346 trace_nfs4_lookup(dir, name, err);
4348 case -NFS4ERR_BADNAME:
4351 case -NFS4ERR_MOVED:
4352 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
4353 if (err == -NFS4ERR_MOVED)
4354 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4356 case -NFS4ERR_WRONGSEC:
4358 if (client != *clnt)
4360 client = nfs4_negotiate_security(client, dir, name);
4362 return PTR_ERR(client);
4364 exception.retry = 1;
4367 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4369 } while (exception.retry);
4374 else if (client != *clnt)
4375 rpc_shutdown_client(client);
4380 static int nfs4_proc_lookup(struct inode *dir, struct dentry *dentry,
4381 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4382 struct nfs4_label *label)
4385 struct rpc_clnt *client = NFS_CLIENT(dir);
4387 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr, label);
4388 if (client != NFS_CLIENT(dir)) {
4389 rpc_shutdown_client(client);
4390 nfs_fixup_secinfo_attributes(fattr);
4396 nfs4_proc_lookup_mountpoint(struct inode *dir, struct dentry *dentry,
4397 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4399 struct rpc_clnt *client = NFS_CLIENT(dir);
4402 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr, NULL);
4404 return ERR_PTR(status);
4405 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
4408 static int _nfs4_proc_lookupp(struct inode *inode,
4409 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4410 struct nfs4_label *label)
4412 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4413 struct nfs_server *server = NFS_SERVER(inode);
4415 struct nfs4_lookupp_arg args = {
4416 .bitmask = server->attr_bitmask,
4417 .fh = NFS_FH(inode),
4419 struct nfs4_lookupp_res res = {
4425 struct rpc_message msg = {
4426 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4431 args.bitmask = nfs4_bitmask(server, label);
4433 nfs_fattr_init(fattr);
4435 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4436 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4438 dprintk("NFS reply lookupp: %d\n", status);
4442 static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4443 struct nfs_fattr *fattr, struct nfs4_label *label)
4445 struct nfs4_exception exception = {
4446 .interruptible = true,
4450 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4451 trace_nfs4_lookupp(inode, err);
4452 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4454 } while (exception.retry);
4458 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4460 struct nfs_server *server = NFS_SERVER(inode);
4461 struct nfs4_accessargs args = {
4462 .fh = NFS_FH(inode),
4463 .access = entry->mask,
4465 struct nfs4_accessres res = {
4468 struct rpc_message msg = {
4469 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4472 .rpc_cred = entry->cred,
4476 if (!nfs4_have_delegation(inode, FMODE_READ)) {
4477 res.fattr = nfs_alloc_fattr();
4478 if (res.fattr == NULL)
4480 args.bitmask = server->cache_consistency_bitmask;
4482 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4484 nfs_access_set_mask(entry, res.access);
4486 nfs_refresh_inode(inode, res.fattr);
4488 nfs_free_fattr(res.fattr);
4492 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4494 struct nfs4_exception exception = {
4495 .interruptible = true,
4499 err = _nfs4_proc_access(inode, entry);
4500 trace_nfs4_access(inode, err);
4501 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4503 } while (exception.retry);
4508 * TODO: For the time being, we don't try to get any attributes
4509 * along with any of the zero-copy operations READ, READDIR,
4512 * In the case of the first three, we want to put the GETATTR
4513 * after the read-type operation -- this is because it is hard
4514 * to predict the length of a GETATTR response in v4, and thus
4515 * align the READ data correctly. This means that the GETATTR
4516 * may end up partially falling into the page cache, and we should
4517 * shift it into the 'tail' of the xdr_buf before processing.
4518 * To do this efficiently, we need to know the total length
4519 * of data received, which doesn't seem to be available outside
4522 * In the case of WRITE, we also want to put the GETATTR after
4523 * the operation -- in this case because we want to make sure
4524 * we get the post-operation mtime and size.
4526 * Both of these changes to the XDR layer would in fact be quite
4527 * minor, but I decided to leave them for a subsequent patch.
4529 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4530 unsigned int pgbase, unsigned int pglen)
4532 struct nfs4_readlink args = {
4533 .fh = NFS_FH(inode),
4538 struct nfs4_readlink_res res;
4539 struct rpc_message msg = {
4540 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4545 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
4548 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4549 unsigned int pgbase, unsigned int pglen)
4551 struct nfs4_exception exception = {
4552 .interruptible = true,
4556 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4557 trace_nfs4_readlink(inode, err);
4558 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4560 } while (exception.retry);
4565 * This is just for mknod. open(O_CREAT) will always do ->open_context().
4568 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
4571 struct nfs_server *server = NFS_SERVER(dir);
4572 struct nfs4_label l, *ilabel;
4573 struct nfs_open_context *ctx;
4574 struct nfs4_state *state;
4577 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
4579 return PTR_ERR(ctx);
4581 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4583 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4584 sattr->ia_mode &= ~current_umask();
4585 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
4586 if (IS_ERR(state)) {
4587 status = PTR_ERR(state);
4591 nfs4_label_release_security(ilabel);
4592 put_nfs_open_context(ctx);
4597 _nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
4599 struct nfs_server *server = NFS_SERVER(dir);
4600 struct nfs_removeargs args = {
4604 struct nfs_removeres res = {
4607 struct rpc_message msg = {
4608 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4612 unsigned long timestamp = jiffies;
4615 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
4617 spin_lock(&dir->i_lock);
4618 nfs4_update_changeattr_locked(dir, &res.cinfo, timestamp,
4619 NFS_INO_INVALID_DATA);
4620 /* Removing a directory decrements nlink in the parent */
4621 if (ftype == NF4DIR && dir->i_nlink > 2)
4622 nfs4_dec_nlink_locked(dir);
4623 spin_unlock(&dir->i_lock);
4628 static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4630 struct nfs4_exception exception = {
4631 .interruptible = true,
4633 struct inode *inode = d_inode(dentry);
4637 if (inode->i_nlink == 1)
4638 nfs4_inode_return_delegation(inode);
4640 nfs4_inode_make_writeable(inode);
4643 err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
4644 trace_nfs4_remove(dir, &dentry->d_name, err);
4645 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4647 } while (exception.retry);
4651 static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
4653 struct nfs4_exception exception = {
4654 .interruptible = true,
4659 err = _nfs4_proc_remove(dir, name, NF4DIR);
4660 trace_nfs4_remove(dir, name, err);
4661 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4663 } while (exception.retry);
4667 static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4668 struct dentry *dentry,
4669 struct inode *inode)
4671 struct nfs_removeargs *args = msg->rpc_argp;
4672 struct nfs_removeres *res = msg->rpc_resp;
4674 res->server = NFS_SB(dentry->d_sb);
4675 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
4676 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
4678 nfs_fattr_init(res->dir_attr);
4681 nfs4_inode_return_delegation(inode);
4684 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4686 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
4687 &data->args.seq_args,
4692 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
4694 struct nfs_unlinkdata *data = task->tk_calldata;
4695 struct nfs_removeres *res = &data->res;
4697 if (!nfs4_sequence_done(task, &res->seq_res))
4699 if (nfs4_async_handle_error(task, res->server, NULL,
4700 &data->timeout) == -EAGAIN)
4702 if (task->tk_status == 0)
4703 nfs4_update_changeattr(dir, &res->cinfo,
4704 res->dir_attr->time_start,
4705 NFS_INO_INVALID_DATA);
4709 static void nfs4_proc_rename_setup(struct rpc_message *msg,
4710 struct dentry *old_dentry,
4711 struct dentry *new_dentry)
4713 struct nfs_renameargs *arg = msg->rpc_argp;
4714 struct nfs_renameres *res = msg->rpc_resp;
4715 struct inode *old_inode = d_inode(old_dentry);
4716 struct inode *new_inode = d_inode(new_dentry);
4719 nfs4_inode_make_writeable(old_inode);
4721 nfs4_inode_return_delegation(new_inode);
4722 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
4723 res->server = NFS_SB(old_dentry->d_sb);
4724 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
4727 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4729 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
4730 &data->args.seq_args,
4735 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4736 struct inode *new_dir)
4738 struct nfs_renamedata *data = task->tk_calldata;
4739 struct nfs_renameres *res = &data->res;
4741 if (!nfs4_sequence_done(task, &res->seq_res))
4743 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
4746 if (task->tk_status == 0) {
4747 if (new_dir != old_dir) {
4748 /* Note: If we moved a directory, nlink will change */
4749 nfs4_update_changeattr(old_dir, &res->old_cinfo,
4750 res->old_fattr->time_start,
4751 NFS_INO_INVALID_OTHER |
4752 NFS_INO_INVALID_DATA);
4753 nfs4_update_changeattr(new_dir, &res->new_cinfo,
4754 res->new_fattr->time_start,
4755 NFS_INO_INVALID_OTHER |
4756 NFS_INO_INVALID_DATA);
4758 nfs4_update_changeattr(old_dir, &res->old_cinfo,
4759 res->old_fattr->time_start,
4760 NFS_INO_INVALID_DATA);
4765 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4767 struct nfs_server *server = NFS_SERVER(inode);
4768 __u32 bitmask[NFS4_BITMASK_SZ];
4769 struct nfs4_link_arg arg = {
4770 .fh = NFS_FH(inode),
4771 .dir_fh = NFS_FH(dir),
4775 struct nfs4_link_res res = {
4779 struct rpc_message msg = {
4780 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4784 int status = -ENOMEM;
4786 res.fattr = nfs_alloc_fattr();
4787 if (res.fattr == NULL)
4790 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4791 if (IS_ERR(res.label)) {
4792 status = PTR_ERR(res.label);
4796 nfs4_inode_make_writeable(inode);
4797 nfs4_bitmap_copy_adjust_setattr(bitmask, nfs4_bitmask(server, res.label), inode);
4799 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4801 nfs4_update_changeattr(dir, &res.cinfo, res.fattr->time_start,
4802 NFS_INO_INVALID_DATA);
4803 status = nfs_post_op_update_inode(inode, res.fattr);
4805 nfs_setsecurity(inode, res.fattr, res.label);
4809 nfs4_label_free(res.label);
4812 nfs_free_fattr(res.fattr);
4816 static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4818 struct nfs4_exception exception = {
4819 .interruptible = true,
4823 err = nfs4_handle_exception(NFS_SERVER(inode),
4824 _nfs4_proc_link(inode, dir, name),
4826 } while (exception.retry);
4830 struct nfs4_createdata {
4831 struct rpc_message msg;
4832 struct nfs4_create_arg arg;
4833 struct nfs4_create_res res;
4835 struct nfs_fattr fattr;
4836 struct nfs4_label *label;
4839 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
4840 const struct qstr *name, struct iattr *sattr, u32 ftype)
4842 struct nfs4_createdata *data;
4844 data = kzalloc(sizeof(*data), GFP_KERNEL);
4846 struct nfs_server *server = NFS_SERVER(dir);
4848 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4849 if (IS_ERR(data->label))
4852 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4853 data->msg.rpc_argp = &data->arg;
4854 data->msg.rpc_resp = &data->res;
4855 data->arg.dir_fh = NFS_FH(dir);
4856 data->arg.server = server;
4857 data->arg.name = name;
4858 data->arg.attrs = sattr;
4859 data->arg.ftype = ftype;
4860 data->arg.bitmask = nfs4_bitmask(server, data->label);
4861 data->arg.umask = current_umask();
4862 data->res.server = server;
4863 data->res.fh = &data->fh;
4864 data->res.fattr = &data->fattr;
4865 data->res.label = data->label;
4866 nfs_fattr_init(data->res.fattr);
4874 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4876 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
4877 &data->arg.seq_args, &data->res.seq_res, 1);
4879 spin_lock(&dir->i_lock);
4880 nfs4_update_changeattr_locked(dir, &data->res.dir_cinfo,
4881 data->res.fattr->time_start,
4882 NFS_INO_INVALID_DATA);
4883 /* Creating a directory bumps nlink in the parent */
4884 if (data->arg.ftype == NF4DIR)
4885 nfs4_inc_nlink_locked(dir);
4886 spin_unlock(&dir->i_lock);
4887 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
4892 static void nfs4_free_createdata(struct nfs4_createdata *data)
4894 nfs4_label_free(data->label);
4898 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4899 struct page *page, unsigned int len, struct iattr *sattr,
4900 struct nfs4_label *label)
4902 struct nfs4_createdata *data;
4903 int status = -ENAMETOOLONG;
4905 if (len > NFS4_MAXPATHLEN)
4909 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4913 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4914 data->arg.u.symlink.pages = &page;
4915 data->arg.u.symlink.len = len;
4916 data->arg.label = label;
4918 status = nfs4_do_create(dir, dentry, data);
4920 nfs4_free_createdata(data);
4925 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4926 struct page *page, unsigned int len, struct iattr *sattr)
4928 struct nfs4_exception exception = {
4929 .interruptible = true,
4931 struct nfs4_label l, *label;
4934 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4937 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4938 trace_nfs4_symlink(dir, &dentry->d_name, err);
4939 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4941 } while (exception.retry);
4943 nfs4_label_release_security(label);
4947 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4948 struct iattr *sattr, struct nfs4_label *label)
4950 struct nfs4_createdata *data;
4951 int status = -ENOMEM;
4953 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4957 data->arg.label = label;
4958 status = nfs4_do_create(dir, dentry, data);
4960 nfs4_free_createdata(data);
4965 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4966 struct iattr *sattr)
4968 struct nfs_server *server = NFS_SERVER(dir);
4969 struct nfs4_exception exception = {
4970 .interruptible = true,
4972 struct nfs4_label l, *label;
4975 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4977 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4978 sattr->ia_mode &= ~current_umask();
4980 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4981 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4982 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4984 } while (exception.retry);
4985 nfs4_label_release_security(label);
4990 static int _nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
4991 u64 cookie, struct page **pages, unsigned int count, bool plus)
4993 struct inode *dir = d_inode(dentry);
4994 struct nfs_server *server = NFS_SERVER(dir);
4995 struct nfs4_readdir_arg args = {
5002 struct nfs4_readdir_res res;
5003 struct rpc_message msg = {
5004 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
5011 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
5013 (unsigned long long)cookie);
5014 if (!(server->caps & NFS_CAP_SECURITY_LABEL))
5015 args.bitmask = server->attr_bitmask_nl;
5017 args.bitmask = server->attr_bitmask;
5019 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
5020 res.pgbase = args.pgbase;
5021 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
5024 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
5025 status += args.pgbase;
5028 nfs_invalidate_atime(dir);
5030 dprintk("%s: returns %d\n", __func__, status);
5034 static int nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
5035 u64 cookie, struct page **pages, unsigned int count, bool plus)
5037 struct nfs4_exception exception = {
5038 .interruptible = true,
5042 err = _nfs4_proc_readdir(dentry, cred, cookie,
5043 pages, count, plus);
5044 trace_nfs4_readdir(d_inode(dentry), err);
5045 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
5047 } while (exception.retry);
5051 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5052 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
5054 struct nfs4_createdata *data;
5055 int mode = sattr->ia_mode;
5056 int status = -ENOMEM;
5058 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
5063 data->arg.ftype = NF4FIFO;
5064 else if (S_ISBLK(mode)) {
5065 data->arg.ftype = NF4BLK;
5066 data->arg.u.device.specdata1 = MAJOR(rdev);
5067 data->arg.u.device.specdata2 = MINOR(rdev);
5069 else if (S_ISCHR(mode)) {
5070 data->arg.ftype = NF4CHR;
5071 data->arg.u.device.specdata1 = MAJOR(rdev);
5072 data->arg.u.device.specdata2 = MINOR(rdev);
5073 } else if (!S_ISSOCK(mode)) {
5078 data->arg.label = label;
5079 status = nfs4_do_create(dir, dentry, data);
5081 nfs4_free_createdata(data);
5086 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5087 struct iattr *sattr, dev_t rdev)
5089 struct nfs_server *server = NFS_SERVER(dir);
5090 struct nfs4_exception exception = {
5091 .interruptible = true,
5093 struct nfs4_label l, *label;
5096 label = nfs4_label_init_security(dir, dentry, sattr, &l);
5098 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
5099 sattr->ia_mode &= ~current_umask();
5101 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
5102 trace_nfs4_mknod(dir, &dentry->d_name, err);
5103 err = nfs4_handle_exception(NFS_SERVER(dir), err,
5105 } while (exception.retry);
5107 nfs4_label_release_security(label);
5112 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
5113 struct nfs_fsstat *fsstat)
5115 struct nfs4_statfs_arg args = {
5117 .bitmask = server->attr_bitmask,
5119 struct nfs4_statfs_res res = {
5122 struct rpc_message msg = {
5123 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
5128 nfs_fattr_init(fsstat->fattr);
5129 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5132 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
5134 struct nfs4_exception exception = {
5135 .interruptible = true,
5139 err = nfs4_handle_exception(server,
5140 _nfs4_proc_statfs(server, fhandle, fsstat),
5142 } while (exception.retry);
5146 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
5147 struct nfs_fsinfo *fsinfo)
5149 struct nfs4_fsinfo_arg args = {
5151 .bitmask = server->attr_bitmask,
5153 struct nfs4_fsinfo_res res = {
5156 struct rpc_message msg = {
5157 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
5162 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5165 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5167 struct nfs4_exception exception = {
5168 .interruptible = true,
5173 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
5174 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
5176 nfs4_set_lease_period(server->nfs_client, fsinfo->lease_time * HZ);
5179 err = nfs4_handle_exception(server, err, &exception);
5180 } while (exception.retry);
5184 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5188 nfs_fattr_init(fsinfo->fattr);
5189 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
5191 /* block layout checks this! */
5192 server->pnfs_blksize = fsinfo->blksize;
5193 set_pnfs_layoutdriver(server, fhandle, fsinfo);
5199 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5200 struct nfs_pathconf *pathconf)
5202 struct nfs4_pathconf_arg args = {
5204 .bitmask = server->attr_bitmask,
5206 struct nfs4_pathconf_res res = {
5207 .pathconf = pathconf,
5209 struct rpc_message msg = {
5210 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
5215 /* None of the pathconf attributes are mandatory to implement */
5216 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
5217 memset(pathconf, 0, sizeof(*pathconf));
5221 nfs_fattr_init(pathconf->fattr);
5222 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5225 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5226 struct nfs_pathconf *pathconf)
5228 struct nfs4_exception exception = {
5229 .interruptible = true,
5234 err = nfs4_handle_exception(server,
5235 _nfs4_proc_pathconf(server, fhandle, pathconf),
5237 } while (exception.retry);
5241 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
5242 const struct nfs_open_context *ctx,
5243 const struct nfs_lock_context *l_ctx,
5246 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
5248 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
5250 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
5251 const struct nfs_open_context *ctx,
5252 const struct nfs_lock_context *l_ctx,
5255 nfs4_stateid _current_stateid;
5257 /* If the current stateid represents a lost lock, then exit */
5258 if (nfs4_set_rw_stateid(&_current_stateid, ctx, l_ctx, fmode) == -EIO)
5260 return nfs4_stateid_match(stateid, &_current_stateid);
5263 static bool nfs4_error_stateid_expired(int err)
5266 case -NFS4ERR_DELEG_REVOKED:
5267 case -NFS4ERR_ADMIN_REVOKED:
5268 case -NFS4ERR_BAD_STATEID:
5269 case -NFS4ERR_STALE_STATEID:
5270 case -NFS4ERR_OLD_STATEID:
5271 case -NFS4ERR_OPENMODE:
5272 case -NFS4ERR_EXPIRED:
5278 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
5280 struct nfs_server *server = NFS_SERVER(hdr->inode);
5282 trace_nfs4_read(hdr, task->tk_status);
5283 if (task->tk_status < 0) {
5284 struct nfs4_exception exception = {
5285 .inode = hdr->inode,
5286 .state = hdr->args.context->state,
5287 .stateid = &hdr->args.stateid,
5289 task->tk_status = nfs4_async_handle_exception(task,
5290 server, task->tk_status, &exception);
5291 if (exception.retry) {
5292 rpc_restart_call_prepare(task);
5297 if (task->tk_status > 0)
5298 renew_lease(server, hdr->timestamp);
5302 static bool nfs4_read_stateid_changed(struct rpc_task *task,
5303 struct nfs_pgio_args *args)
5306 if (!nfs4_error_stateid_expired(task->tk_status) ||
5307 nfs4_stateid_is_current(&args->stateid,
5312 rpc_restart_call_prepare(task);
5316 static bool nfs4_read_plus_not_supported(struct rpc_task *task,
5317 struct nfs_pgio_header *hdr)
5319 struct nfs_server *server = NFS_SERVER(hdr->inode);
5320 struct rpc_message *msg = &task->tk_msg;
5322 if (msg->rpc_proc == &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS] &&
5323 server->caps & NFS_CAP_READ_PLUS && task->tk_status == -ENOTSUPP) {
5324 server->caps &= ~NFS_CAP_READ_PLUS;
5325 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5326 rpc_restart_call_prepare(task);
5332 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5334 dprintk("--> %s\n", __func__);
5336 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5338 if (nfs4_read_stateid_changed(task, &hdr->args))
5340 if (nfs4_read_plus_not_supported(task, hdr))
5342 if (task->tk_status > 0)
5343 nfs_invalidate_atime(hdr->inode);
5344 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5345 nfs4_read_done_cb(task, hdr);
5348 #if defined CONFIG_NFS_V4_2 && defined CONFIG_NFS_V4_2_READ_PLUS
5349 static void nfs42_read_plus_support(struct nfs_server *server, struct rpc_message *msg)
5351 if (server->caps & NFS_CAP_READ_PLUS)
5352 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS];
5354 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5357 static void nfs42_read_plus_support(struct nfs_server *server, struct rpc_message *msg)
5359 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5361 #endif /* CONFIG_NFS_V4_2 */
5363 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5364 struct rpc_message *msg)
5366 hdr->timestamp = jiffies;
5367 if (!hdr->pgio_done_cb)
5368 hdr->pgio_done_cb = nfs4_read_done_cb;
5369 nfs42_read_plus_support(NFS_SERVER(hdr->inode), msg);
5370 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5373 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5374 struct nfs_pgio_header *hdr)
5376 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
5377 &hdr->args.seq_args,
5381 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5382 hdr->args.lock_context,
5383 hdr->rw_mode) == -EIO)
5385 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
5390 static int nfs4_write_done_cb(struct rpc_task *task,
5391 struct nfs_pgio_header *hdr)
5393 struct inode *inode = hdr->inode;
5395 trace_nfs4_write(hdr, task->tk_status);
5396 if (task->tk_status < 0) {
5397 struct nfs4_exception exception = {
5398 .inode = hdr->inode,
5399 .state = hdr->args.context->state,
5400 .stateid = &hdr->args.stateid,
5402 task->tk_status = nfs4_async_handle_exception(task,
5403 NFS_SERVER(inode), task->tk_status,
5405 if (exception.retry) {
5406 rpc_restart_call_prepare(task);
5410 if (task->tk_status >= 0) {
5411 renew_lease(NFS_SERVER(inode), hdr->timestamp);
5412 nfs_writeback_update_inode(hdr);
5417 static bool nfs4_write_stateid_changed(struct rpc_task *task,
5418 struct nfs_pgio_args *args)
5421 if (!nfs4_error_stateid_expired(task->tk_status) ||
5422 nfs4_stateid_is_current(&args->stateid,
5427 rpc_restart_call_prepare(task);
5431 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5433 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5435 if (nfs4_write_stateid_changed(task, &hdr->args))
5437 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5438 nfs4_write_done_cb(task, hdr);
5442 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
5444 /* Don't request attributes for pNFS or O_DIRECT writes */
5445 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
5447 /* Otherwise, request attributes if and only if we don't hold
5450 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
5453 static void nfs4_bitmask_set(__u32 bitmask[NFS4_BITMASK_SZ], const __u32 *src,
5454 struct inode *inode, struct nfs_server *server,
5455 struct nfs4_label *label)
5457 unsigned long cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
5460 memcpy(bitmask, src, sizeof(*bitmask) * NFS4_BITMASK_SZ);
5462 if (cache_validity & (NFS_INO_INVALID_CHANGE | NFS_INO_REVAL_PAGECACHE))
5463 bitmask[0] |= FATTR4_WORD0_CHANGE;
5464 if (cache_validity & NFS_INO_INVALID_ATIME)
5465 bitmask[1] |= FATTR4_WORD1_TIME_ACCESS;
5466 if (cache_validity & NFS_INO_INVALID_OTHER)
5467 bitmask[1] |= FATTR4_WORD1_MODE | FATTR4_WORD1_OWNER |
5468 FATTR4_WORD1_OWNER_GROUP |
5469 FATTR4_WORD1_NUMLINKS;
5470 if (label && label->len && cache_validity & NFS_INO_INVALID_LABEL)
5471 bitmask[2] |= FATTR4_WORD2_SECURITY_LABEL;
5472 if (cache_validity & NFS_INO_INVALID_CTIME)
5473 bitmask[1] |= FATTR4_WORD1_TIME_METADATA;
5474 if (cache_validity & NFS_INO_INVALID_MTIME)
5475 bitmask[1] |= FATTR4_WORD1_TIME_MODIFY;
5476 if (cache_validity & NFS_INO_INVALID_BLOCKS)
5477 bitmask[1] |= FATTR4_WORD1_SPACE_USED;
5479 if (nfs4_have_delegation(inode, FMODE_READ) &&
5480 !(cache_validity & NFS_INO_REVAL_FORCED))
5481 bitmask[0] &= ~FATTR4_WORD0_SIZE;
5482 else if (cache_validity &
5483 (NFS_INO_INVALID_SIZE | NFS_INO_REVAL_PAGECACHE))
5484 bitmask[0] |= FATTR4_WORD0_SIZE;
5486 for (i = 0; i < NFS4_BITMASK_SZ; i++)
5487 bitmask[i] &= server->attr_bitmask[i];
5490 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
5491 struct rpc_message *msg,
5492 struct rpc_clnt **clnt)
5494 struct nfs_server *server = NFS_SERVER(hdr->inode);
5496 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5497 hdr->args.bitmask = NULL;
5498 hdr->res.fattr = NULL;
5500 nfs4_bitmask_set(hdr->args.bitmask_store,
5501 server->cache_consistency_bitmask,
5502 hdr->inode, server, NULL);
5503 hdr->args.bitmask = hdr->args.bitmask_store;
5506 if (!hdr->pgio_done_cb)
5507 hdr->pgio_done_cb = nfs4_write_done_cb;
5508 hdr->res.server = server;
5509 hdr->timestamp = jiffies;
5511 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
5512 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5513 nfs4_state_protect_write(hdr->ds_clp ? hdr->ds_clp : server->nfs_client, clnt, msg, hdr);
5516 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5518 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
5519 &data->args.seq_args,
5524 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
5526 struct inode *inode = data->inode;
5528 trace_nfs4_commit(data, task->tk_status);
5529 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5530 NULL, NULL) == -EAGAIN) {
5531 rpc_restart_call_prepare(task);
5537 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
5539 if (!nfs4_sequence_done(task, &data->res.seq_res))
5541 return data->commit_done_cb(task, data);
5544 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5545 struct rpc_clnt **clnt)
5547 struct nfs_server *server = NFS_SERVER(data->inode);
5549 if (data->commit_done_cb == NULL)
5550 data->commit_done_cb = nfs4_commit_done_cb;
5551 data->res.server = server;
5552 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
5553 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
5554 nfs4_state_protect(data->ds_clp ? data->ds_clp : server->nfs_client,
5555 NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
5558 static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5559 struct nfs_commitres *res)
5561 struct inode *dst_inode = file_inode(dst);
5562 struct nfs_server *server = NFS_SERVER(dst_inode);
5563 struct rpc_message msg = {
5564 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5569 args->fh = NFS_FH(dst_inode);
5570 return nfs4_call_sync(server->client, server, &msg,
5571 &args->seq_args, &res->seq_res, 1);
5574 int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5576 struct nfs_commitargs args = {
5580 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5581 struct nfs4_exception exception = { };
5585 status = _nfs4_proc_commit(dst, &args, res);
5586 status = nfs4_handle_exception(dst_server, status, &exception);
5587 } while (exception.retry);
5592 struct nfs4_renewdata {
5593 struct nfs_client *client;
5594 unsigned long timestamp;
5598 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5599 * standalone procedure for queueing an asynchronous RENEW.
5601 static void nfs4_renew_release(void *calldata)
5603 struct nfs4_renewdata *data = calldata;
5604 struct nfs_client *clp = data->client;
5606 if (refcount_read(&clp->cl_count) > 1)
5607 nfs4_schedule_state_renewal(clp);
5608 nfs_put_client(clp);
5612 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
5614 struct nfs4_renewdata *data = calldata;
5615 struct nfs_client *clp = data->client;
5616 unsigned long timestamp = data->timestamp;
5618 trace_nfs4_renew_async(clp, task->tk_status);
5619 switch (task->tk_status) {
5622 case -NFS4ERR_LEASE_MOVED:
5623 nfs4_schedule_lease_moved_recovery(clp);
5626 /* Unless we're shutting down, schedule state recovery! */
5627 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5629 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
5630 nfs4_schedule_lease_recovery(clp);
5633 nfs4_schedule_path_down_recovery(clp);
5635 do_renew_lease(clp, timestamp);
5638 static const struct rpc_call_ops nfs4_renew_ops = {
5639 .rpc_call_done = nfs4_renew_done,
5640 .rpc_release = nfs4_renew_release,
5643 static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
5645 struct rpc_message msg = {
5646 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5650 struct nfs4_renewdata *data;
5652 if (renew_flags == 0)
5654 if (!refcount_inc_not_zero(&clp->cl_count))
5656 data = kmalloc(sizeof(*data), GFP_NOFS);
5658 nfs_put_client(clp);
5662 data->timestamp = jiffies;
5663 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
5664 &nfs4_renew_ops, data);
5667 static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
5669 struct rpc_message msg = {
5670 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5674 unsigned long now = jiffies;
5677 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5680 do_renew_lease(clp, now);
5684 static inline int nfs4_server_supports_acls(struct nfs_server *server)
5686 return server->caps & NFS_CAP_ACLS;
5689 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5690 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
5693 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
5695 int nfs4_buf_to_pages_noslab(const void *buf, size_t buflen,
5696 struct page **pages)
5698 struct page *newpage, **spages;
5704 len = min_t(size_t, PAGE_SIZE, buflen);
5705 newpage = alloc_page(GFP_KERNEL);
5707 if (newpage == NULL)
5709 memcpy(page_address(newpage), buf, len);
5714 } while (buflen != 0);
5720 __free_page(spages[rc-1]);
5724 struct nfs4_cached_acl {
5730 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
5732 struct nfs_inode *nfsi = NFS_I(inode);
5734 spin_lock(&inode->i_lock);
5735 kfree(nfsi->nfs4_acl);
5736 nfsi->nfs4_acl = acl;
5737 spin_unlock(&inode->i_lock);
5740 static void nfs4_zap_acl_attr(struct inode *inode)
5742 nfs4_set_cached_acl(inode, NULL);
5745 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5747 struct nfs_inode *nfsi = NFS_I(inode);
5748 struct nfs4_cached_acl *acl;
5751 spin_lock(&inode->i_lock);
5752 acl = nfsi->nfs4_acl;
5755 if (buf == NULL) /* user is just asking for length */
5757 if (acl->cached == 0)
5759 ret = -ERANGE; /* see getxattr(2) man page */
5760 if (acl->len > buflen)
5762 memcpy(buf, acl->data, acl->len);
5766 spin_unlock(&inode->i_lock);
5770 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
5772 struct nfs4_cached_acl *acl;
5773 size_t buflen = sizeof(*acl) + acl_len;
5775 if (buflen <= PAGE_SIZE) {
5776 acl = kmalloc(buflen, GFP_KERNEL);
5780 _copy_from_pages(acl->data, pages, pgbase, acl_len);
5782 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5789 nfs4_set_cached_acl(inode, acl);
5793 * The getxattr API returns the required buffer length when called with a
5794 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5795 * the required buf. On a NULL buf, we send a page of data to the server
5796 * guessing that the ACL request can be serviced by a page. If so, we cache
5797 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5798 * the cache. If not so, we throw away the page, and cache the required
5799 * length. The next getxattr call will then produce another round trip to
5800 * the server, this time with the input buf of the required size.
5802 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5804 struct page **pages;
5805 struct nfs_getaclargs args = {
5806 .fh = NFS_FH(inode),
5809 struct nfs_getaclres res = {
5812 struct rpc_message msg = {
5813 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5817 unsigned int npages;
5818 int ret = -ENOMEM, i;
5819 struct nfs_server *server = NFS_SERVER(inode);
5822 buflen = server->rsize;
5824 npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
5825 pages = kmalloc_array(npages, sizeof(struct page *), GFP_NOFS);
5829 args.acl_pages = pages;
5831 for (i = 0; i < npages; i++) {
5832 pages[i] = alloc_page(GFP_KERNEL);
5837 /* for decoding across pages */
5838 res.acl_scratch = alloc_page(GFP_KERNEL);
5839 if (!res.acl_scratch)
5842 args.acl_len = npages * PAGE_SIZE;
5844 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
5845 __func__, buf, buflen, npages, args.acl_len);
5846 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5847 &msg, &args.seq_args, &res.seq_res, 0);
5851 /* Handle the case where the passed-in buffer is too short */
5852 if (res.acl_flags & NFS4_ACL_TRUNC) {
5853 /* Did the user only issue a request for the acl length? */
5859 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
5861 if (res.acl_len > buflen) {
5865 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
5871 __free_page(pages[i]);
5872 if (res.acl_scratch)
5873 __free_page(res.acl_scratch);
5878 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5880 struct nfs4_exception exception = {
5881 .interruptible = true,
5885 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
5886 trace_nfs4_get_acl(inode, ret);
5889 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5890 } while (exception.retry);
5894 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5896 struct nfs_server *server = NFS_SERVER(inode);
5899 if (!nfs4_server_supports_acls(server))
5901 ret = nfs_revalidate_inode(server, inode);
5904 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5905 nfs_zap_acl_cache(inode);
5906 ret = nfs4_read_cached_acl(inode, buf, buflen);
5908 /* -ENOENT is returned if there is no ACL or if there is an ACL
5909 * but no cached acl data, just the acl length */
5911 return nfs4_get_acl_uncached(inode, buf, buflen);
5914 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5916 struct nfs_server *server = NFS_SERVER(inode);
5917 struct page *pages[NFS4ACL_MAXPAGES];
5918 struct nfs_setaclargs arg = {
5919 .fh = NFS_FH(inode),
5923 struct nfs_setaclres res;
5924 struct rpc_message msg = {
5925 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5929 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
5932 /* You can't remove system.nfs4_acl: */
5935 if (!nfs4_server_supports_acls(server))
5937 if (npages > ARRAY_SIZE(pages))
5939 i = nfs4_buf_to_pages_noslab(buf, buflen, arg.acl_pages);
5942 nfs4_inode_make_writeable(inode);
5943 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5946 * Free each page after tx, so the only ref left is
5947 * held by the network stack
5950 put_page(pages[i-1]);
5953 * Acl update can result in inode attribute update.
5954 * so mark the attribute cache invalid.
5956 spin_lock(&inode->i_lock);
5957 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
5958 | NFS_INO_INVALID_CTIME
5959 | NFS_INO_REVAL_FORCED;
5960 spin_unlock(&inode->i_lock);
5961 nfs_access_zap_cache(inode);
5962 nfs_zap_acl_cache(inode);
5966 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5968 struct nfs4_exception exception = { };
5971 err = __nfs4_proc_set_acl(inode, buf, buflen);
5972 trace_nfs4_set_acl(inode, err);
5973 if (err == -NFS4ERR_BADOWNER || err == -NFS4ERR_BADNAME) {
5975 * no need to retry since the kernel
5976 * isn't involved in encoding the ACEs.
5981 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5983 } while (exception.retry);
5987 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
5988 static int _nfs4_get_security_label(struct inode *inode, void *buf,
5991 struct nfs_server *server = NFS_SERVER(inode);
5992 struct nfs_fattr fattr;
5993 struct nfs4_label label = {0, 0, buflen, buf};
5995 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5996 struct nfs4_getattr_arg arg = {
5997 .fh = NFS_FH(inode),
6000 struct nfs4_getattr_res res = {
6005 struct rpc_message msg = {
6006 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
6012 nfs_fattr_init(&fattr);
6014 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
6017 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
6022 static int nfs4_get_security_label(struct inode *inode, void *buf,
6025 struct nfs4_exception exception = {
6026 .interruptible = true,
6030 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
6034 err = _nfs4_get_security_label(inode, buf, buflen);
6035 trace_nfs4_get_security_label(inode, err);
6036 err = nfs4_handle_exception(NFS_SERVER(inode), err,
6038 } while (exception.retry);
6042 static int _nfs4_do_set_security_label(struct inode *inode,
6043 struct nfs4_label *ilabel,
6044 struct nfs_fattr *fattr,
6045 struct nfs4_label *olabel)
6048 struct iattr sattr = {0};
6049 struct nfs_server *server = NFS_SERVER(inode);
6050 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
6051 struct nfs_setattrargs arg = {
6052 .fh = NFS_FH(inode),
6058 struct nfs_setattrres res = {
6063 struct rpc_message msg = {
6064 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
6070 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
6072 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6074 dprintk("%s failed: %d\n", __func__, status);
6079 static int nfs4_do_set_security_label(struct inode *inode,
6080 struct nfs4_label *ilabel,
6081 struct nfs_fattr *fattr,
6082 struct nfs4_label *olabel)
6084 struct nfs4_exception exception = { };
6088 err = _nfs4_do_set_security_label(inode, ilabel,
6090 trace_nfs4_set_security_label(inode, err);
6091 err = nfs4_handle_exception(NFS_SERVER(inode), err,
6093 } while (exception.retry);
6098 nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
6100 struct nfs4_label ilabel, *olabel = NULL;
6101 struct nfs_fattr fattr;
6104 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
6107 nfs_fattr_init(&fattr);
6111 ilabel.label = (char *)buf;
6112 ilabel.len = buflen;
6114 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
6115 if (IS_ERR(olabel)) {
6116 status = -PTR_ERR(olabel);
6120 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
6122 nfs_setsecurity(inode, &fattr, olabel);
6124 nfs4_label_free(olabel);
6128 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
6131 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
6132 nfs4_verifier *bootverf)
6136 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
6137 /* An impossible timestamp guarantees this value
6138 * will never match a generated boot time. */
6139 verf[0] = cpu_to_be32(U32_MAX);
6140 verf[1] = cpu_to_be32(U32_MAX);
6142 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
6143 u64 ns = ktime_to_ns(nn->boot_time);
6145 verf[0] = cpu_to_be32(ns >> 32);
6146 verf[1] = cpu_to_be32(ns);
6148 memcpy(bootverf->data, verf, sizeof(bootverf->data));
6152 nfs4_get_uniquifier(struct nfs_client *clp, char *buf, size_t buflen)
6154 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
6155 struct nfs_netns_client *nn_clp = nn->nfs_client;
6162 id = rcu_dereference(nn_clp->identifier);
6164 strscpy(buf, id, buflen);
6168 if (nfs4_client_id_uniquifier[0] != '\0' && buf[0] == '\0')
6169 strscpy(buf, nfs4_client_id_uniquifier, buflen);
6175 nfs4_init_nonuniform_client_string(struct nfs_client *clp)
6177 char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6182 if (clp->cl_owner_id != NULL)
6187 strlen(clp->cl_rpcclient->cl_nodename) +
6189 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
6193 buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
6197 if (len > NFS4_OPAQUE_LIMIT + 1)
6201 * Since this string is allocated at mount time, and held until the
6202 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6203 * about a memory-reclaim deadlock.
6205 str = kmalloc(len, GFP_KERNEL);
6211 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
6212 clp->cl_rpcclient->cl_nodename, buf,
6213 rpc_peeraddr2str(clp->cl_rpcclient,
6216 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
6217 clp->cl_rpcclient->cl_nodename,
6218 rpc_peeraddr2str(clp->cl_rpcclient,
6222 clp->cl_owner_id = str;
6227 nfs4_init_uniform_client_string(struct nfs_client *clp)
6229 char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6234 if (clp->cl_owner_id != NULL)
6237 len = 10 + 10 + 1 + 10 + 1 +
6238 strlen(clp->cl_rpcclient->cl_nodename) + 1;
6240 buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
6244 if (len > NFS4_OPAQUE_LIMIT + 1)
6248 * Since this string is allocated at mount time, and held until the
6249 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6250 * about a memory-reclaim deadlock.
6252 str = kmalloc(len, GFP_KERNEL);
6257 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
6258 clp->rpc_ops->version, clp->cl_minorversion,
6259 buf, clp->cl_rpcclient->cl_nodename);
6261 scnprintf(str, len, "Linux NFSv%u.%u %s",
6262 clp->rpc_ops->version, clp->cl_minorversion,
6263 clp->cl_rpcclient->cl_nodename);
6264 clp->cl_owner_id = str;
6269 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
6270 * services. Advertise one based on the address family of the
6274 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
6276 if (strchr(clp->cl_ipaddr, ':') != NULL)
6277 return scnprintf(buf, len, "tcp6");
6279 return scnprintf(buf, len, "tcp");
6282 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
6284 struct nfs4_setclientid *sc = calldata;
6286 if (task->tk_status == 0)
6287 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
6290 static const struct rpc_call_ops nfs4_setclientid_ops = {
6291 .rpc_call_done = nfs4_setclientid_done,
6295 * nfs4_proc_setclientid - Negotiate client ID
6296 * @clp: state data structure
6297 * @program: RPC program for NFSv4 callback service
6298 * @port: IP port number for NFS4 callback service
6299 * @cred: credential to use for this call
6300 * @res: where to place the result
6302 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6304 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
6305 unsigned short port, const struct cred *cred,
6306 struct nfs4_setclientid_res *res)
6308 nfs4_verifier sc_verifier;
6309 struct nfs4_setclientid setclientid = {
6310 .sc_verifier = &sc_verifier,
6314 struct rpc_message msg = {
6315 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
6316 .rpc_argp = &setclientid,
6320 struct rpc_task_setup task_setup_data = {
6321 .rpc_client = clp->cl_rpcclient,
6322 .rpc_message = &msg,
6323 .callback_ops = &nfs4_setclientid_ops,
6324 .callback_data = &setclientid,
6325 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
6327 unsigned long now = jiffies;
6330 /* nfs_client_id4 */
6331 nfs4_init_boot_verifier(clp, &sc_verifier);
6333 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
6334 status = nfs4_init_uniform_client_string(clp);
6336 status = nfs4_init_nonuniform_client_string(clp);
6342 setclientid.sc_netid_len =
6343 nfs4_init_callback_netid(clp,
6344 setclientid.sc_netid,
6345 sizeof(setclientid.sc_netid));
6346 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
6347 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
6348 clp->cl_ipaddr, port >> 8, port & 255);
6350 dprintk("NFS call setclientid auth=%s, '%s'\n",
6351 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6354 status = nfs4_call_sync_custom(&task_setup_data);
6355 if (setclientid.sc_cred) {
6356 kfree(clp->cl_acceptor);
6357 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
6358 put_rpccred(setclientid.sc_cred);
6362 do_renew_lease(clp, now);
6364 trace_nfs4_setclientid(clp, status);
6365 dprintk("NFS reply setclientid: %d\n", status);
6370 * nfs4_proc_setclientid_confirm - Confirm client ID
6371 * @clp: state data structure
6372 * @arg: result of a previous SETCLIENTID
6373 * @cred: credential to use for this call
6375 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6377 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
6378 struct nfs4_setclientid_res *arg,
6379 const struct cred *cred)
6381 struct rpc_message msg = {
6382 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
6388 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
6389 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6391 status = rpc_call_sync(clp->cl_rpcclient, &msg,
6392 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
6393 trace_nfs4_setclientid_confirm(clp, status);
6394 dprintk("NFS reply setclientid_confirm: %d\n", status);
6398 struct nfs4_delegreturndata {
6399 struct nfs4_delegreturnargs args;
6400 struct nfs4_delegreturnres res;
6402 nfs4_stateid stateid;
6403 unsigned long timestamp;
6405 struct nfs4_layoutreturn_args arg;
6406 struct nfs4_layoutreturn_res res;
6407 struct nfs4_xdr_opaque_data ld_private;
6411 struct nfs_fattr fattr;
6413 struct inode *inode;
6416 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6418 struct nfs4_delegreturndata *data = calldata;
6419 struct nfs4_exception exception = {
6420 .inode = data->inode,
6421 .stateid = &data->stateid,
6422 .task_is_privileged = data->args.seq_args.sa_privileged,
6425 if (!nfs4_sequence_done(task, &data->res.seq_res))
6428 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
6430 /* Handle Layoutreturn errors */
6431 if (pnfs_roc_done(task, &data->args.lr_args, &data->res.lr_res,
6432 &data->res.lr_ret) == -EAGAIN)
6435 switch (task->tk_status) {
6437 renew_lease(data->res.server, data->timestamp);
6439 case -NFS4ERR_ADMIN_REVOKED:
6440 case -NFS4ERR_DELEG_REVOKED:
6441 case -NFS4ERR_EXPIRED:
6442 nfs4_free_revoked_stateid(data->res.server,
6444 task->tk_msg.rpc_cred);
6446 case -NFS4ERR_BAD_STATEID:
6447 case -NFS4ERR_STALE_STATEID:
6449 task->tk_status = 0;
6451 case -NFS4ERR_OLD_STATEID:
6452 if (!nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6453 nfs4_stateid_seqid_inc(&data->stateid);
6454 if (data->args.bitmask) {
6455 data->args.bitmask = NULL;
6456 data->res.fattr = NULL;
6459 case -NFS4ERR_ACCESS:
6460 if (data->args.bitmask) {
6461 data->args.bitmask = NULL;
6462 data->res.fattr = NULL;
6467 task->tk_status = nfs4_async_handle_exception(task,
6468 data->res.server, task->tk_status,
6470 if (exception.retry)
6473 nfs_delegation_mark_returned(data->inode, data->args.stateid);
6474 data->rpc_status = task->tk_status;
6477 task->tk_status = 0;
6478 rpc_restart_call_prepare(task);
6481 static void nfs4_delegreturn_release(void *calldata)
6483 struct nfs4_delegreturndata *data = calldata;
6484 struct inode *inode = data->inode;
6487 pnfs_roc_release(&data->lr.arg, &data->lr.res,
6490 nfs4_fattr_set_prechange(&data->fattr,
6491 inode_peek_iversion_raw(inode));
6492 nfs_refresh_inode(inode, &data->fattr);
6493 nfs_iput_and_deactive(inode);
6498 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6500 struct nfs4_delegreturndata *d_data;
6501 struct pnfs_layout_hdr *lo;
6503 d_data = (struct nfs4_delegreturndata *)data;
6505 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task)) {
6506 nfs4_sequence_done(task, &d_data->res.seq_res);
6510 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6511 if (lo && !pnfs_layout_is_valid(lo)) {
6512 d_data->args.lr_args = NULL;
6513 d_data->res.lr_res = NULL;
6516 nfs4_setup_sequence(d_data->res.server->nfs_client,
6517 &d_data->args.seq_args,
6518 &d_data->res.seq_res,
6522 static const struct rpc_call_ops nfs4_delegreturn_ops = {
6523 .rpc_call_prepare = nfs4_delegreturn_prepare,
6524 .rpc_call_done = nfs4_delegreturn_done,
6525 .rpc_release = nfs4_delegreturn_release,
6528 static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6530 struct nfs4_delegreturndata *data;
6531 struct nfs_server *server = NFS_SERVER(inode);
6532 struct rpc_task *task;
6533 struct rpc_message msg = {
6534 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6537 struct rpc_task_setup task_setup_data = {
6538 .rpc_client = server->client,
6539 .rpc_message = &msg,
6540 .callback_ops = &nfs4_delegreturn_ops,
6541 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
6545 data = kzalloc(sizeof(*data), GFP_NOFS);
6549 nfs4_state_protect(server->nfs_client,
6550 NFS_SP4_MACH_CRED_CLEANUP,
6551 &task_setup_data.rpc_client, &msg);
6553 data->args.fhandle = &data->fh;
6554 data->args.stateid = &data->stateid;
6555 nfs4_bitmask_set(data->args.bitmask_store,
6556 server->cache_consistency_bitmask, inode, server,
6558 data->args.bitmask = data->args.bitmask_store;
6559 nfs_copy_fh(&data->fh, NFS_FH(inode));
6560 nfs4_stateid_copy(&data->stateid, stateid);
6561 data->res.fattr = &data->fattr;
6562 data->res.server = server;
6563 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
6564 data->lr.arg.ld_private = &data->lr.ld_private;
6565 nfs_fattr_init(data->res.fattr);
6566 data->timestamp = jiffies;
6567 data->rpc_status = 0;
6568 data->inode = nfs_igrab_and_active(inode);
6569 if (data->inode || issync) {
6570 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res,
6573 data->args.lr_args = &data->lr.arg;
6574 data->res.lr_res = &data->lr.res;
6579 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1,
6582 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1,
6584 task_setup_data.callback_data = data;
6585 msg.rpc_argp = &data->args;
6586 msg.rpc_resp = &data->res;
6587 task = rpc_run_task(&task_setup_data);
6589 return PTR_ERR(task);
6592 status = rpc_wait_for_completion_task(task);
6595 status = data->rpc_status;
6601 int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6603 struct nfs_server *server = NFS_SERVER(inode);
6604 struct nfs4_exception exception = { };
6607 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
6608 trace_nfs4_delegreturn(inode, stateid, err);
6610 case -NFS4ERR_STALE_STATEID:
6611 case -NFS4ERR_EXPIRED:
6615 err = nfs4_handle_exception(server, err, &exception);
6616 } while (exception.retry);
6620 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6622 struct inode *inode = state->inode;
6623 struct nfs_server *server = NFS_SERVER(inode);
6624 struct nfs_client *clp = server->nfs_client;
6625 struct nfs_lockt_args arg = {
6626 .fh = NFS_FH(inode),
6629 struct nfs_lockt_res res = {
6632 struct rpc_message msg = {
6633 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
6636 .rpc_cred = state->owner->so_cred,
6638 struct nfs4_lock_state *lsp;
6641 arg.lock_owner.clientid = clp->cl_clientid;
6642 status = nfs4_set_lock_state(state, request);
6645 lsp = request->fl_u.nfs4_fl.owner;
6646 arg.lock_owner.id = lsp->ls_seqid.owner_id;
6647 arg.lock_owner.s_dev = server->s_dev;
6648 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6651 request->fl_type = F_UNLCK;
6653 case -NFS4ERR_DENIED:
6656 request->fl_ops->fl_release_private(request);
6657 request->fl_ops = NULL;
6662 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6664 struct nfs4_exception exception = {
6665 .interruptible = true,
6670 err = _nfs4_proc_getlk(state, cmd, request);
6671 trace_nfs4_get_lock(request, state, cmd, err);
6672 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
6674 } while (exception.retry);
6679 * Update the seqid of a lock stateid after receiving
6680 * NFS4ERR_OLD_STATEID
6682 static bool nfs4_refresh_lock_old_stateid(nfs4_stateid *dst,
6683 struct nfs4_lock_state *lsp)
6685 struct nfs4_state *state = lsp->ls_state;
6688 spin_lock(&state->state_lock);
6689 if (!nfs4_stateid_match_other(dst, &lsp->ls_stateid))
6691 if (!nfs4_stateid_is_newer(&lsp->ls_stateid, dst))
6692 nfs4_stateid_seqid_inc(dst);
6694 dst->seqid = lsp->ls_stateid.seqid;
6697 spin_unlock(&state->state_lock);
6701 static bool nfs4_sync_lock_stateid(nfs4_stateid *dst,
6702 struct nfs4_lock_state *lsp)
6704 struct nfs4_state *state = lsp->ls_state;
6707 spin_lock(&state->state_lock);
6708 ret = !nfs4_stateid_match_other(dst, &lsp->ls_stateid);
6709 nfs4_stateid_copy(dst, &lsp->ls_stateid);
6710 spin_unlock(&state->state_lock);
6714 struct nfs4_unlockdata {
6715 struct nfs_locku_args arg;
6716 struct nfs_locku_res res;
6717 struct nfs4_lock_state *lsp;
6718 struct nfs_open_context *ctx;
6719 struct nfs_lock_context *l_ctx;
6720 struct file_lock fl;
6721 struct nfs_server *server;
6722 unsigned long timestamp;
6725 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6726 struct nfs_open_context *ctx,
6727 struct nfs4_lock_state *lsp,
6728 struct nfs_seqid *seqid)
6730 struct nfs4_unlockdata *p;
6731 struct nfs4_state *state = lsp->ls_state;
6732 struct inode *inode = state->inode;
6734 p = kzalloc(sizeof(*p), GFP_NOFS);
6737 p->arg.fh = NFS_FH(inode);
6739 p->arg.seqid = seqid;
6740 p->res.seqid = seqid;
6742 /* Ensure we don't close file until we're done freeing locks! */
6743 p->ctx = get_nfs_open_context(ctx);
6744 p->l_ctx = nfs_get_lock_context(ctx);
6745 locks_init_lock(&p->fl);
6746 locks_copy_lock(&p->fl, fl);
6747 p->server = NFS_SERVER(inode);
6748 spin_lock(&state->state_lock);
6749 nfs4_stateid_copy(&p->arg.stateid, &lsp->ls_stateid);
6750 spin_unlock(&state->state_lock);
6754 static void nfs4_locku_release_calldata(void *data)
6756 struct nfs4_unlockdata *calldata = data;
6757 nfs_free_seqid(calldata->arg.seqid);
6758 nfs4_put_lock_state(calldata->lsp);
6759 nfs_put_lock_context(calldata->l_ctx);
6760 put_nfs_open_context(calldata->ctx);
6764 static void nfs4_locku_done(struct rpc_task *task, void *data)
6766 struct nfs4_unlockdata *calldata = data;
6767 struct nfs4_exception exception = {
6768 .inode = calldata->lsp->ls_state->inode,
6769 .stateid = &calldata->arg.stateid,
6772 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6774 switch (task->tk_status) {
6776 renew_lease(calldata->server, calldata->timestamp);
6777 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
6778 if (nfs4_update_lock_stateid(calldata->lsp,
6779 &calldata->res.stateid))
6782 case -NFS4ERR_ADMIN_REVOKED:
6783 case -NFS4ERR_EXPIRED:
6784 nfs4_free_revoked_stateid(calldata->server,
6785 &calldata->arg.stateid,
6786 task->tk_msg.rpc_cred);
6788 case -NFS4ERR_BAD_STATEID:
6789 case -NFS4ERR_STALE_STATEID:
6790 if (nfs4_sync_lock_stateid(&calldata->arg.stateid,
6792 rpc_restart_call_prepare(task);
6794 case -NFS4ERR_OLD_STATEID:
6795 if (nfs4_refresh_lock_old_stateid(&calldata->arg.stateid,
6797 rpc_restart_call_prepare(task);
6800 task->tk_status = nfs4_async_handle_exception(task,
6801 calldata->server, task->tk_status,
6803 if (exception.retry)
6804 rpc_restart_call_prepare(task);
6806 nfs_release_seqid(calldata->arg.seqid);
6809 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
6811 struct nfs4_unlockdata *calldata = data;
6813 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6814 nfs_async_iocounter_wait(task, calldata->l_ctx))
6817 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
6819 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
6820 /* Note: exit _without_ running nfs4_locku_done */
6823 calldata->timestamp = jiffies;
6824 if (nfs4_setup_sequence(calldata->server->nfs_client,
6825 &calldata->arg.seq_args,
6826 &calldata->res.seq_res,
6828 nfs_release_seqid(calldata->arg.seqid);
6831 task->tk_action = NULL;
6833 nfs4_sequence_done(task, &calldata->res.seq_res);
6836 static const struct rpc_call_ops nfs4_locku_ops = {
6837 .rpc_call_prepare = nfs4_locku_prepare,
6838 .rpc_call_done = nfs4_locku_done,
6839 .rpc_release = nfs4_locku_release_calldata,
6842 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6843 struct nfs_open_context *ctx,
6844 struct nfs4_lock_state *lsp,
6845 struct nfs_seqid *seqid)
6847 struct nfs4_unlockdata *data;
6848 struct rpc_message msg = {
6849 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6850 .rpc_cred = ctx->cred,
6852 struct rpc_task_setup task_setup_data = {
6853 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
6854 .rpc_message = &msg,
6855 .callback_ops = &nfs4_locku_ops,
6856 .workqueue = nfsiod_workqueue,
6857 .flags = RPC_TASK_ASYNC,
6860 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6861 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6863 /* Ensure this is an unlock - when canceling a lock, the
6864 * canceled lock is passed in, and it won't be an unlock.
6866 fl->fl_type = F_UNLCK;
6867 if (fl->fl_flags & FL_CLOSE)
6868 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
6870 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6872 nfs_free_seqid(seqid);
6873 return ERR_PTR(-ENOMEM);
6876 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
6877 msg.rpc_argp = &data->arg;
6878 msg.rpc_resp = &data->res;
6879 task_setup_data.callback_data = data;
6880 return rpc_run_task(&task_setup_data);
6883 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6885 struct inode *inode = state->inode;
6886 struct nfs4_state_owner *sp = state->owner;
6887 struct nfs_inode *nfsi = NFS_I(inode);
6888 struct nfs_seqid *seqid;
6889 struct nfs4_lock_state *lsp;
6890 struct rpc_task *task;
6891 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6893 unsigned char fl_flags = request->fl_flags;
6895 status = nfs4_set_lock_state(state, request);
6896 /* Unlock _before_ we do the RPC call */
6897 request->fl_flags |= FL_EXISTS;
6898 /* Exclude nfs_delegation_claim_locks() */
6899 mutex_lock(&sp->so_delegreturn_mutex);
6900 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
6901 down_read(&nfsi->rwsem);
6902 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
6903 up_read(&nfsi->rwsem);
6904 mutex_unlock(&sp->so_delegreturn_mutex);
6907 up_read(&nfsi->rwsem);
6908 mutex_unlock(&sp->so_delegreturn_mutex);
6911 /* Is this a delegated lock? */
6912 lsp = request->fl_u.nfs4_fl.owner;
6913 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6915 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6916 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
6920 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
6921 status = PTR_ERR(task);
6924 status = rpc_wait_for_completion_task(task);
6927 request->fl_flags = fl_flags;
6928 trace_nfs4_unlock(request, state, F_SETLK, status);
6932 struct nfs4_lockdata {
6933 struct nfs_lock_args arg;
6934 struct nfs_lock_res res;
6935 struct nfs4_lock_state *lsp;
6936 struct nfs_open_context *ctx;
6937 struct file_lock fl;
6938 unsigned long timestamp;
6941 struct nfs_server *server;
6944 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
6945 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6948 struct nfs4_lockdata *p;
6949 struct inode *inode = lsp->ls_state->inode;
6950 struct nfs_server *server = NFS_SERVER(inode);
6951 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6953 p = kzalloc(sizeof(*p), gfp_mask);
6957 p->arg.fh = NFS_FH(inode);
6959 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
6960 if (IS_ERR(p->arg.open_seqid))
6962 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6963 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
6964 if (IS_ERR(p->arg.lock_seqid))
6965 goto out_free_seqid;
6966 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
6967 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
6968 p->arg.lock_owner.s_dev = server->s_dev;
6969 p->res.lock_seqid = p->arg.lock_seqid;
6972 p->ctx = get_nfs_open_context(ctx);
6973 locks_init_lock(&p->fl);
6974 locks_copy_lock(&p->fl, fl);
6977 nfs_free_seqid(p->arg.open_seqid);
6983 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6985 struct nfs4_lockdata *data = calldata;
6986 struct nfs4_state *state = data->lsp->ls_state;
6988 dprintk("%s: begin!\n", __func__);
6989 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
6991 /* Do we need to do an open_to_lock_owner? */
6992 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
6993 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
6994 goto out_release_lock_seqid;
6996 nfs4_stateid_copy(&data->arg.open_stateid,
6997 &state->open_stateid);
6998 data->arg.new_lock_owner = 1;
6999 data->res.open_seqid = data->arg.open_seqid;
7001 data->arg.new_lock_owner = 0;
7002 nfs4_stateid_copy(&data->arg.lock_stateid,
7003 &data->lsp->ls_stateid);
7005 if (!nfs4_valid_open_stateid(state)) {
7006 data->rpc_status = -EBADF;
7007 task->tk_action = NULL;
7008 goto out_release_open_seqid;
7010 data->timestamp = jiffies;
7011 if (nfs4_setup_sequence(data->server->nfs_client,
7012 &data->arg.seq_args,
7016 out_release_open_seqid:
7017 nfs_release_seqid(data->arg.open_seqid);
7018 out_release_lock_seqid:
7019 nfs_release_seqid(data->arg.lock_seqid);
7021 nfs4_sequence_done(task, &data->res.seq_res);
7022 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
7025 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
7027 struct nfs4_lockdata *data = calldata;
7028 struct nfs4_lock_state *lsp = data->lsp;
7029 struct nfs_server *server = NFS_SERVER(d_inode(data->ctx->dentry));
7031 dprintk("%s: begin!\n", __func__);
7033 if (!nfs4_sequence_done(task, &data->res.seq_res))
7036 data->rpc_status = task->tk_status;
7037 switch (task->tk_status) {
7039 renew_lease(server, data->timestamp);
7040 if (data->arg.new_lock && !data->cancelled) {
7041 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
7042 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
7045 if (data->arg.new_lock_owner != 0) {
7046 nfs_confirm_seqid(&lsp->ls_seqid, 0);
7047 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
7048 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
7049 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
7052 case -NFS4ERR_OLD_STATEID:
7053 if (data->arg.new_lock_owner != 0 &&
7054 nfs4_refresh_open_old_stateid(&data->arg.open_stateid,
7057 if (nfs4_refresh_lock_old_stateid(&data->arg.lock_stateid, lsp))
7060 case -NFS4ERR_BAD_STATEID:
7061 case -NFS4ERR_STALE_STATEID:
7062 case -NFS4ERR_EXPIRED:
7063 if (data->arg.new_lock_owner != 0) {
7064 if (!nfs4_stateid_match(&data->arg.open_stateid,
7065 &lsp->ls_state->open_stateid))
7067 else if (nfs4_async_handle_error(task, server, lsp->ls_state, NULL) == -EAGAIN)
7069 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
7074 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
7077 if (!data->cancelled)
7078 rpc_restart_call_prepare(task);
7082 static void nfs4_lock_release(void *calldata)
7084 struct nfs4_lockdata *data = calldata;
7086 dprintk("%s: begin!\n", __func__);
7087 nfs_free_seqid(data->arg.open_seqid);
7088 if (data->cancelled && data->rpc_status == 0) {
7089 struct rpc_task *task;
7090 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
7091 data->arg.lock_seqid);
7093 rpc_put_task_async(task);
7094 dprintk("%s: cancelling lock!\n", __func__);
7096 nfs_free_seqid(data->arg.lock_seqid);
7097 nfs4_put_lock_state(data->lsp);
7098 put_nfs_open_context(data->ctx);
7100 dprintk("%s: done!\n", __func__);
7103 static const struct rpc_call_ops nfs4_lock_ops = {
7104 .rpc_call_prepare = nfs4_lock_prepare,
7105 .rpc_call_done = nfs4_lock_done,
7106 .rpc_release = nfs4_lock_release,
7109 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
7112 case -NFS4ERR_ADMIN_REVOKED:
7113 case -NFS4ERR_EXPIRED:
7114 case -NFS4ERR_BAD_STATEID:
7115 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
7116 if (new_lock_owner != 0 ||
7117 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
7118 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
7120 case -NFS4ERR_STALE_STATEID:
7121 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
7122 nfs4_schedule_lease_recovery(server->nfs_client);
7126 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
7128 struct nfs4_lockdata *data;
7129 struct rpc_task *task;
7130 struct rpc_message msg = {
7131 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
7132 .rpc_cred = state->owner->so_cred,
7134 struct rpc_task_setup task_setup_data = {
7135 .rpc_client = NFS_CLIENT(state->inode),
7136 .rpc_message = &msg,
7137 .callback_ops = &nfs4_lock_ops,
7138 .workqueue = nfsiod_workqueue,
7139 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
7143 dprintk("%s: begin!\n", __func__);
7144 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
7145 fl->fl_u.nfs4_fl.owner,
7146 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
7150 data->arg.block = 1;
7151 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
7152 recovery_type > NFS_LOCK_NEW);
7153 msg.rpc_argp = &data->arg;
7154 msg.rpc_resp = &data->res;
7155 task_setup_data.callback_data = data;
7156 if (recovery_type > NFS_LOCK_NEW) {
7157 if (recovery_type == NFS_LOCK_RECLAIM)
7158 data->arg.reclaim = NFS_LOCK_RECLAIM;
7160 data->arg.new_lock = 1;
7161 task = rpc_run_task(&task_setup_data);
7163 return PTR_ERR(task);
7164 ret = rpc_wait_for_completion_task(task);
7166 ret = data->rpc_status;
7168 nfs4_handle_setlk_error(data->server, data->lsp,
7169 data->arg.new_lock_owner, ret);
7171 data->cancelled = true;
7172 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
7174 dprintk("%s: done, ret = %d!\n", __func__, ret);
7178 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
7180 struct nfs_server *server = NFS_SERVER(state->inode);
7181 struct nfs4_exception exception = {
7182 .inode = state->inode,
7187 /* Cache the lock if possible... */
7188 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7190 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
7191 if (err != -NFS4ERR_DELAY)
7193 nfs4_handle_exception(server, err, &exception);
7194 } while (exception.retry);
7198 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
7200 struct nfs_server *server = NFS_SERVER(state->inode);
7201 struct nfs4_exception exception = {
7202 .inode = state->inode,
7206 err = nfs4_set_lock_state(state, request);
7209 if (!recover_lost_locks) {
7210 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
7214 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7216 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
7220 case -NFS4ERR_GRACE:
7221 case -NFS4ERR_DELAY:
7222 nfs4_handle_exception(server, err, &exception);
7225 } while (exception.retry);
7230 #if defined(CONFIG_NFS_V4_1)
7231 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
7233 struct nfs4_lock_state *lsp;
7236 status = nfs4_set_lock_state(state, request);
7239 lsp = request->fl_u.nfs4_fl.owner;
7240 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
7241 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
7243 return nfs4_lock_expired(state, request);
7247 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7249 struct nfs_inode *nfsi = NFS_I(state->inode);
7250 struct nfs4_state_owner *sp = state->owner;
7251 unsigned char fl_flags = request->fl_flags;
7254 request->fl_flags |= FL_ACCESS;
7255 status = locks_lock_inode_wait(state->inode, request);
7258 mutex_lock(&sp->so_delegreturn_mutex);
7259 down_read(&nfsi->rwsem);
7260 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
7261 /* Yes: cache locks! */
7262 /* ...but avoid races with delegation recall... */
7263 request->fl_flags = fl_flags & ~FL_SLEEP;
7264 status = locks_lock_inode_wait(state->inode, request);
7265 up_read(&nfsi->rwsem);
7266 mutex_unlock(&sp->so_delegreturn_mutex);
7269 up_read(&nfsi->rwsem);
7270 mutex_unlock(&sp->so_delegreturn_mutex);
7271 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
7273 request->fl_flags = fl_flags;
7277 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7279 struct nfs4_exception exception = {
7281 .inode = state->inode,
7282 .interruptible = true,
7287 err = _nfs4_proc_setlk(state, cmd, request);
7288 if (err == -NFS4ERR_DENIED)
7290 err = nfs4_handle_exception(NFS_SERVER(state->inode),
7292 } while (exception.retry);
7296 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
7297 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
7300 nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
7301 struct file_lock *request)
7303 int status = -ERESTARTSYS;
7304 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
7306 while(!signalled()) {
7307 status = nfs4_proc_setlk(state, cmd, request);
7308 if ((status != -EAGAIN) || IS_SETLK(cmd))
7310 freezable_schedule_timeout_interruptible(timeout);
7312 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
7313 status = -ERESTARTSYS;
7318 #ifdef CONFIG_NFS_V4_1
7319 struct nfs4_lock_waiter {
7320 struct task_struct *task;
7321 struct inode *inode;
7322 struct nfs_lowner *owner;
7326 nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
7329 struct nfs4_lock_waiter *waiter = wait->private;
7331 /* NULL key means to wake up everyone */
7333 struct cb_notify_lock_args *cbnl = key;
7334 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
7335 *wowner = waiter->owner;
7337 /* Only wake if the callback was for the same owner. */
7338 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
7341 /* Make sure it's for the right inode */
7342 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
7346 /* override "private" so we can use default_wake_function */
7347 wait->private = waiter->task;
7348 ret = woken_wake_function(wait, mode, flags, key);
7350 list_del_init(&wait->entry);
7351 wait->private = waiter;
7356 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7358 int status = -ERESTARTSYS;
7359 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
7360 struct nfs_server *server = NFS_SERVER(state->inode);
7361 struct nfs_client *clp = server->nfs_client;
7362 wait_queue_head_t *q = &clp->cl_lock_waitq;
7363 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
7364 .id = lsp->ls_seqid.owner_id,
7365 .s_dev = server->s_dev };
7366 struct nfs4_lock_waiter waiter = { .task = current,
7367 .inode = state->inode,
7369 wait_queue_entry_t wait;
7371 /* Don't bother with waitqueue if we don't expect a callback */
7372 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
7373 return nfs4_retry_setlk_simple(state, cmd, request);
7376 wait.private = &waiter;
7377 wait.func = nfs4_wake_lock_waiter;
7379 while(!signalled()) {
7380 add_wait_queue(q, &wait);
7381 status = nfs4_proc_setlk(state, cmd, request);
7382 if ((status != -EAGAIN) || IS_SETLK(cmd)) {
7383 finish_wait(q, &wait);
7387 status = -ERESTARTSYS;
7388 freezer_do_not_count();
7389 wait_woken(&wait, TASK_INTERRUPTIBLE, NFS4_LOCK_MAXTIMEOUT);
7391 finish_wait(q, &wait);
7396 #else /* !CONFIG_NFS_V4_1 */
7398 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7400 return nfs4_retry_setlk_simple(state, cmd, request);
7405 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
7407 struct nfs_open_context *ctx;
7408 struct nfs4_state *state;
7411 /* verify open state */
7412 ctx = nfs_file_open_context(filp);
7415 if (IS_GETLK(cmd)) {
7417 return nfs4_proc_getlk(state, F_GETLK, request);
7421 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
7424 if (request->fl_type == F_UNLCK) {
7426 return nfs4_proc_unlck(state, cmd, request);
7433 if ((request->fl_flags & FL_POSIX) &&
7434 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
7438 * Don't rely on the VFS having checked the file open mode,
7439 * since it won't do this for flock() locks.
7441 switch (request->fl_type) {
7443 if (!(filp->f_mode & FMODE_READ))
7447 if (!(filp->f_mode & FMODE_WRITE))
7451 status = nfs4_set_lock_state(state, request);
7455 return nfs4_retry_setlk(state, cmd, request);
7458 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
7460 struct nfs_server *server = NFS_SERVER(state->inode);
7463 err = nfs4_set_lock_state(state, fl);
7467 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
7468 if (err != -NFS4ERR_DELAY)
7471 } while (err == -NFS4ERR_DELAY);
7472 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
7475 struct nfs_release_lockowner_data {
7476 struct nfs4_lock_state *lsp;
7477 struct nfs_server *server;
7478 struct nfs_release_lockowner_args args;
7479 struct nfs_release_lockowner_res res;
7480 unsigned long timestamp;
7483 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7485 struct nfs_release_lockowner_data *data = calldata;
7486 struct nfs_server *server = data->server;
7487 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7488 &data->res.seq_res, task);
7489 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7490 data->timestamp = jiffies;
7493 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7495 struct nfs_release_lockowner_data *data = calldata;
7496 struct nfs_server *server = data->server;
7498 nfs40_sequence_done(task, &data->res.seq_res);
7500 switch (task->tk_status) {
7502 renew_lease(server, data->timestamp);
7504 case -NFS4ERR_STALE_CLIENTID:
7505 case -NFS4ERR_EXPIRED:
7506 nfs4_schedule_lease_recovery(server->nfs_client);
7508 case -NFS4ERR_LEASE_MOVED:
7509 case -NFS4ERR_DELAY:
7510 if (nfs4_async_handle_error(task, server,
7511 NULL, NULL) == -EAGAIN)
7512 rpc_restart_call_prepare(task);
7516 static void nfs4_release_lockowner_release(void *calldata)
7518 struct nfs_release_lockowner_data *data = calldata;
7519 nfs4_free_lock_state(data->server, data->lsp);
7523 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
7524 .rpc_call_prepare = nfs4_release_lockowner_prepare,
7525 .rpc_call_done = nfs4_release_lockowner_done,
7526 .rpc_release = nfs4_release_lockowner_release,
7530 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
7532 struct nfs_release_lockowner_data *data;
7533 struct rpc_message msg = {
7534 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7537 if (server->nfs_client->cl_mvops->minor_version != 0)
7540 data = kmalloc(sizeof(*data), GFP_NOFS);
7544 data->server = server;
7545 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7546 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7547 data->args.lock_owner.s_dev = server->s_dev;
7549 msg.rpc_argp = &data->args;
7550 msg.rpc_resp = &data->res;
7551 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
7552 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
7555 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7557 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
7558 struct dentry *unused, struct inode *inode,
7559 const char *key, const void *buf,
7560 size_t buflen, int flags)
7562 return nfs4_proc_set_acl(inode, buf, buflen);
7565 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
7566 struct dentry *unused, struct inode *inode,
7567 const char *key, void *buf, size_t buflen)
7569 return nfs4_proc_get_acl(inode, buf, buflen);
7572 static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
7574 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
7577 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
7579 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
7580 struct dentry *unused, struct inode *inode,
7581 const char *key, const void *buf,
7582 size_t buflen, int flags)
7584 if (security_ismaclabel(key))
7585 return nfs4_set_security_label(inode, buf, buflen);
7590 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
7591 struct dentry *unused, struct inode *inode,
7592 const char *key, void *buf, size_t buflen)
7594 if (security_ismaclabel(key))
7595 return nfs4_get_security_label(inode, buf, buflen);
7600 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7604 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7605 len = security_inode_listsecurity(inode, list, list_len);
7606 if (len >= 0 && list_len && len > list_len)
7612 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7613 .prefix = XATTR_SECURITY_PREFIX,
7614 .get = nfs4_xattr_get_nfs4_label,
7615 .set = nfs4_xattr_set_nfs4_label,
7621 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7628 #ifdef CONFIG_NFS_V4_2
7629 static int nfs4_xattr_set_nfs4_user(const struct xattr_handler *handler,
7630 struct dentry *unused, struct inode *inode,
7631 const char *key, const void *buf,
7632 size_t buflen, int flags)
7637 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7641 * There is no mapping from the MAY_* flags to the NFS_ACCESS_XA*
7642 * flags right now. Handling of xattr operations use the normal
7643 * file read/write permissions.
7645 * Just in case the server has other ideas (which RFC 8276 allows),
7646 * do a cached access check for the XA* flags to possibly avoid
7647 * doing an RPC and getting EACCES back.
7649 if (!nfs_access_get_cached(inode, current_cred(), &mask, true)) {
7650 if (!(mask & NFS_ACCESS_XAWRITE))
7655 ret = nfs42_proc_removexattr(inode, key);
7657 nfs4_xattr_cache_remove(inode, key);
7659 ret = nfs42_proc_setxattr(inode, key, buf, buflen, flags);
7661 nfs4_xattr_cache_add(inode, key, buf, NULL, buflen);
7667 static int nfs4_xattr_get_nfs4_user(const struct xattr_handler *handler,
7668 struct dentry *unused, struct inode *inode,
7669 const char *key, void *buf, size_t buflen)
7674 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7677 if (!nfs_access_get_cached(inode, current_cred(), &mask, true)) {
7678 if (!(mask & NFS_ACCESS_XAREAD))
7682 ret = nfs_revalidate_inode(NFS_SERVER(inode), inode);
7686 ret = nfs4_xattr_cache_get(inode, key, buf, buflen);
7687 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7690 ret = nfs42_proc_getxattr(inode, key, buf, buflen);
7696 nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7705 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7708 if (!nfs_access_get_cached(inode, current_cred(), &mask, true)) {
7709 if (!(mask & NFS_ACCESS_XALIST))
7713 ret = nfs_revalidate_inode(NFS_SERVER(inode), inode);
7717 ret = nfs4_xattr_cache_list(inode, list, list_len);
7718 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7723 buflen = list_len ? list_len : XATTR_LIST_MAX;
7724 buf = list_len ? list : NULL;
7728 ret = nfs42_proc_listxattrs(inode, buf, buflen,
7741 nfs4_xattr_cache_set_list(inode, list, size);
7749 nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7753 #endif /* CONFIG_NFS_V4_2 */
7756 * nfs_fhget will use either the mounted_on_fileid or the fileid
7758 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7760 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7761 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7762 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
7763 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
7766 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
7767 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
7768 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7772 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7773 const struct qstr *name,
7774 struct nfs4_fs_locations *fs_locations,
7777 struct nfs_server *server = NFS_SERVER(dir);
7779 struct nfs4_fs_locations_arg args = {
7780 .dir_fh = NFS_FH(dir),
7785 struct nfs4_fs_locations_res res = {
7786 .fs_locations = fs_locations,
7788 struct rpc_message msg = {
7789 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7795 dprintk("%s: start\n", __func__);
7797 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7798 bitmask[1] = nfs4_fattr_bitmap[1];
7800 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7801 * is not supported */
7802 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
7803 bitmask[0] &= ~FATTR4_WORD0_FILEID;
7805 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
7807 nfs_fattr_init(&fs_locations->fattr);
7808 fs_locations->server = server;
7809 fs_locations->nlocations = 0;
7810 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
7811 dprintk("%s: returned status = %d\n", __func__, status);
7815 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7816 const struct qstr *name,
7817 struct nfs4_fs_locations *fs_locations,
7820 struct nfs4_exception exception = {
7821 .interruptible = true,
7825 err = _nfs4_proc_fs_locations(client, dir, name,
7826 fs_locations, page);
7827 trace_nfs4_get_fs_locations(dir, name, err);
7828 err = nfs4_handle_exception(NFS_SERVER(dir), err,
7830 } while (exception.retry);
7835 * This operation also signals the server that this client is
7836 * performing migration recovery. The server can stop returning
7837 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7838 * appended to this compound to identify the client ID which is
7839 * performing recovery.
7841 static int _nfs40_proc_get_locations(struct inode *inode,
7842 struct nfs4_fs_locations *locations,
7843 struct page *page, const struct cred *cred)
7845 struct nfs_server *server = NFS_SERVER(inode);
7846 struct rpc_clnt *clnt = server->client;
7848 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7850 struct nfs4_fs_locations_arg args = {
7851 .clientid = server->nfs_client->cl_clientid,
7852 .fh = NFS_FH(inode),
7855 .migration = 1, /* skip LOOKUP */
7856 .renew = 1, /* append RENEW */
7858 struct nfs4_fs_locations_res res = {
7859 .fs_locations = locations,
7863 struct rpc_message msg = {
7864 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7869 unsigned long now = jiffies;
7872 nfs_fattr_init(&locations->fattr);
7873 locations->server = server;
7874 locations->nlocations = 0;
7876 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7877 status = nfs4_call_sync_sequence(clnt, server, &msg,
7878 &args.seq_args, &res.seq_res);
7882 renew_lease(server, now);
7886 #ifdef CONFIG_NFS_V4_1
7889 * This operation also signals the server that this client is
7890 * performing migration recovery. The server can stop asserting
7891 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7892 * performing this operation is identified in the SEQUENCE
7893 * operation in this compound.
7895 * When the client supports GETATTR(fs_locations_info), it can
7896 * be plumbed in here.
7898 static int _nfs41_proc_get_locations(struct inode *inode,
7899 struct nfs4_fs_locations *locations,
7900 struct page *page, const struct cred *cred)
7902 struct nfs_server *server = NFS_SERVER(inode);
7903 struct rpc_clnt *clnt = server->client;
7905 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7907 struct nfs4_fs_locations_arg args = {
7908 .fh = NFS_FH(inode),
7911 .migration = 1, /* skip LOOKUP */
7913 struct nfs4_fs_locations_res res = {
7914 .fs_locations = locations,
7917 struct rpc_message msg = {
7918 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7925 nfs_fattr_init(&locations->fattr);
7926 locations->server = server;
7927 locations->nlocations = 0;
7929 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7930 status = nfs4_call_sync_sequence(clnt, server, &msg,
7931 &args.seq_args, &res.seq_res);
7932 if (status == NFS4_OK &&
7933 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7934 status = -NFS4ERR_LEASE_MOVED;
7938 #endif /* CONFIG_NFS_V4_1 */
7941 * nfs4_proc_get_locations - discover locations for a migrated FSID
7942 * @inode: inode on FSID that is migrating
7943 * @locations: result of query
7945 * @cred: credential to use for this operation
7947 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7948 * operation failed, or a negative errno if a local error occurred.
7950 * On success, "locations" is filled in, but if the server has
7951 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7954 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7955 * from this client that require migration recovery.
7957 int nfs4_proc_get_locations(struct inode *inode,
7958 struct nfs4_fs_locations *locations,
7959 struct page *page, const struct cred *cred)
7961 struct nfs_server *server = NFS_SERVER(inode);
7962 struct nfs_client *clp = server->nfs_client;
7963 const struct nfs4_mig_recovery_ops *ops =
7964 clp->cl_mvops->mig_recovery_ops;
7965 struct nfs4_exception exception = {
7966 .interruptible = true,
7970 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7971 (unsigned long long)server->fsid.major,
7972 (unsigned long long)server->fsid.minor,
7974 nfs_display_fhandle(NFS_FH(inode), __func__);
7977 status = ops->get_locations(inode, locations, page, cred);
7978 if (status != -NFS4ERR_DELAY)
7980 nfs4_handle_exception(server, status, &exception);
7981 } while (exception.retry);
7986 * This operation also signals the server that this client is
7987 * performing "lease moved" recovery. The server can stop
7988 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7989 * is appended to this compound to identify the client ID which is
7990 * performing recovery.
7992 static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
7994 struct nfs_server *server = NFS_SERVER(inode);
7995 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7996 struct rpc_clnt *clnt = server->client;
7997 struct nfs4_fsid_present_arg args = {
7998 .fh = NFS_FH(inode),
7999 .clientid = clp->cl_clientid,
8000 .renew = 1, /* append RENEW */
8002 struct nfs4_fsid_present_res res = {
8005 struct rpc_message msg = {
8006 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
8011 unsigned long now = jiffies;
8014 res.fh = nfs_alloc_fhandle();
8018 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8019 status = nfs4_call_sync_sequence(clnt, server, &msg,
8020 &args.seq_args, &res.seq_res);
8021 nfs_free_fhandle(res.fh);
8025 do_renew_lease(clp, now);
8029 #ifdef CONFIG_NFS_V4_1
8032 * This operation also signals the server that this client is
8033 * performing "lease moved" recovery. The server can stop asserting
8034 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
8035 * this operation is identified in the SEQUENCE operation in this
8038 static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
8040 struct nfs_server *server = NFS_SERVER(inode);
8041 struct rpc_clnt *clnt = server->client;
8042 struct nfs4_fsid_present_arg args = {
8043 .fh = NFS_FH(inode),
8045 struct nfs4_fsid_present_res res = {
8047 struct rpc_message msg = {
8048 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
8055 res.fh = nfs_alloc_fhandle();
8059 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8060 status = nfs4_call_sync_sequence(clnt, server, &msg,
8061 &args.seq_args, &res.seq_res);
8062 nfs_free_fhandle(res.fh);
8063 if (status == NFS4_OK &&
8064 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
8065 status = -NFS4ERR_LEASE_MOVED;
8069 #endif /* CONFIG_NFS_V4_1 */
8072 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
8073 * @inode: inode on FSID to check
8074 * @cred: credential to use for this operation
8076 * Server indicates whether the FSID is present, moved, or not
8077 * recognized. This operation is necessary to clear a LEASE_MOVED
8078 * condition for this client ID.
8080 * Returns NFS4_OK if the FSID is present on this server,
8081 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
8082 * NFS4ERR code if some error occurred on the server, or a
8083 * negative errno if a local failure occurred.
8085 int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
8087 struct nfs_server *server = NFS_SERVER(inode);
8088 struct nfs_client *clp = server->nfs_client;
8089 const struct nfs4_mig_recovery_ops *ops =
8090 clp->cl_mvops->mig_recovery_ops;
8091 struct nfs4_exception exception = {
8092 .interruptible = true,
8096 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
8097 (unsigned long long)server->fsid.major,
8098 (unsigned long long)server->fsid.minor,
8100 nfs_display_fhandle(NFS_FH(inode), __func__);
8103 status = ops->fsid_present(inode, cred);
8104 if (status != -NFS4ERR_DELAY)
8106 nfs4_handle_exception(server, status, &exception);
8107 } while (exception.retry);
8112 * If 'use_integrity' is true and the state managment nfs_client
8113 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
8114 * and the machine credential as per RFC3530bis and RFC5661 Security
8115 * Considerations sections. Otherwise, just use the user cred with the
8116 * filesystem's rpc_client.
8118 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
8121 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
8122 struct nfs_client *clp = NFS_SERVER(dir)->nfs_client;
8123 struct nfs4_secinfo_arg args = {
8124 .dir_fh = NFS_FH(dir),
8127 struct nfs4_secinfo_res res = {
8130 struct rpc_message msg = {
8131 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
8135 struct nfs4_call_sync_data data = {
8136 .seq_server = NFS_SERVER(dir),
8137 .seq_args = &args.seq_args,
8138 .seq_res = &res.seq_res,
8140 struct rpc_task_setup task_setup = {
8142 .rpc_message = &msg,
8143 .callback_ops = clp->cl_mvops->call_sync_ops,
8144 .callback_data = &data,
8145 .flags = RPC_TASK_NO_ROUND_ROBIN,
8147 const struct cred *cred = NULL;
8149 if (use_integrity) {
8150 clnt = clp->cl_rpcclient;
8151 task_setup.rpc_client = clnt;
8153 cred = nfs4_get_clid_cred(clp);
8154 msg.rpc_cred = cred;
8157 dprintk("NFS call secinfo %s\n", name->name);
8159 nfs4_state_protect(clp, NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
8160 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
8161 status = nfs4_call_sync_custom(&task_setup);
8163 dprintk("NFS reply secinfo: %d\n", status);
8169 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
8170 struct nfs4_secinfo_flavors *flavors)
8172 struct nfs4_exception exception = {
8173 .interruptible = true,
8177 err = -NFS4ERR_WRONGSEC;
8179 /* try to use integrity protection with machine cred */
8180 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
8181 err = _nfs4_proc_secinfo(dir, name, flavors, true);
8184 * if unable to use integrity protection, or SECINFO with
8185 * integrity protection returns NFS4ERR_WRONGSEC (which is
8186 * disallowed by spec, but exists in deployed servers) use
8187 * the current filesystem's rpc_client and the user cred.
8189 if (err == -NFS4ERR_WRONGSEC)
8190 err = _nfs4_proc_secinfo(dir, name, flavors, false);
8192 trace_nfs4_secinfo(dir, name, err);
8193 err = nfs4_handle_exception(NFS_SERVER(dir), err,
8195 } while (exception.retry);
8199 #ifdef CONFIG_NFS_V4_1
8201 * Check the exchange flags returned by the server for invalid flags, having
8202 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
8205 static int nfs4_check_cl_exchange_flags(u32 flags, u32 version)
8207 if (version >= 2 && (flags & ~EXCHGID4_2_FLAG_MASK_R))
8209 else if (version < 2 && (flags & ~EXCHGID4_FLAG_MASK_R))
8211 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
8212 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
8214 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
8218 return -NFS4ERR_INVAL;
8222 nfs41_same_server_scope(struct nfs41_server_scope *a,
8223 struct nfs41_server_scope *b)
8225 if (a->server_scope_sz != b->server_scope_sz)
8227 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
8231 nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
8233 struct nfs41_bind_conn_to_session_args *args = task->tk_msg.rpc_argp;
8234 struct nfs41_bind_conn_to_session_res *res = task->tk_msg.rpc_resp;
8235 struct nfs_client *clp = args->client;
8237 switch (task->tk_status) {
8238 case -NFS4ERR_BADSESSION:
8239 case -NFS4ERR_DEADSESSION:
8240 nfs4_schedule_session_recovery(clp->cl_session,
8244 if (args->dir == NFS4_CDFC4_FORE_OR_BOTH &&
8245 res->dir != NFS4_CDFS4_BOTH) {
8246 rpc_task_close_connection(task);
8247 if (args->retries++ < MAX_BIND_CONN_TO_SESSION_RETRIES)
8248 rpc_restart_call(task);
8252 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
8253 .rpc_call_done = nfs4_bind_one_conn_to_session_done,
8257 * nfs4_proc_bind_one_conn_to_session()
8259 * The 4.1 client currently uses the same TCP connection for the
8260 * fore and backchannel.
8263 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
8264 struct rpc_xprt *xprt,
8265 struct nfs_client *clp,
8266 const struct cred *cred)
8269 struct nfs41_bind_conn_to_session_args args = {
8271 .dir = NFS4_CDFC4_FORE_OR_BOTH,
8274 struct nfs41_bind_conn_to_session_res res;
8275 struct rpc_message msg = {
8277 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
8282 struct rpc_task_setup task_setup_data = {
8285 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
8286 .rpc_message = &msg,
8287 .flags = RPC_TASK_TIMEOUT,
8289 struct rpc_task *task;
8291 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
8292 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
8293 args.dir = NFS4_CDFC4_FORE;
8295 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
8296 if (xprt != rcu_access_pointer(clnt->cl_xprt))
8297 args.dir = NFS4_CDFC4_FORE;
8299 task = rpc_run_task(&task_setup_data);
8300 if (!IS_ERR(task)) {
8301 status = task->tk_status;
8304 status = PTR_ERR(task);
8305 trace_nfs4_bind_conn_to_session(clp, status);
8307 if (memcmp(res.sessionid.data,
8308 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
8309 dprintk("NFS: %s: Session ID mismatch\n", __func__);
8312 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
8313 dprintk("NFS: %s: Unexpected direction from server\n",
8317 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
8318 dprintk("NFS: %s: Server returned RDMA mode = true\n",
8327 struct rpc_bind_conn_calldata {
8328 struct nfs_client *clp;
8329 const struct cred *cred;
8333 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
8334 struct rpc_xprt *xprt,
8337 struct rpc_bind_conn_calldata *p = calldata;
8339 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
8342 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
8344 struct rpc_bind_conn_calldata data = {
8348 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
8349 nfs4_proc_bind_conn_to_session_callback, &data);
8353 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
8354 * and operations we'd like to see to enable certain features in the allow map
8356 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
8357 .how = SP4_MACH_CRED,
8358 .enforce.u.words = {
8359 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8360 1 << (OP_EXCHANGE_ID - 32) |
8361 1 << (OP_CREATE_SESSION - 32) |
8362 1 << (OP_DESTROY_SESSION - 32) |
8363 1 << (OP_DESTROY_CLIENTID - 32)
8366 [0] = 1 << (OP_CLOSE) |
8367 1 << (OP_OPEN_DOWNGRADE) |
8369 1 << (OP_DELEGRETURN) |
8371 [1] = 1 << (OP_SECINFO - 32) |
8372 1 << (OP_SECINFO_NO_NAME - 32) |
8373 1 << (OP_LAYOUTRETURN - 32) |
8374 1 << (OP_TEST_STATEID - 32) |
8375 1 << (OP_FREE_STATEID - 32) |
8376 1 << (OP_WRITE - 32)
8381 * Select the state protection mode for client `clp' given the server results
8382 * from exchange_id in `sp'.
8384 * Returns 0 on success, negative errno otherwise.
8386 static int nfs4_sp4_select_mode(struct nfs_client *clp,
8387 struct nfs41_state_protection *sp)
8389 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
8390 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8391 1 << (OP_EXCHANGE_ID - 32) |
8392 1 << (OP_CREATE_SESSION - 32) |
8393 1 << (OP_DESTROY_SESSION - 32) |
8394 1 << (OP_DESTROY_CLIENTID - 32)
8396 unsigned long flags = 0;
8400 if (sp->how == SP4_MACH_CRED) {
8401 /* Print state protect result */
8402 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
8403 for (i = 0; i <= LAST_NFS4_OP; i++) {
8404 if (test_bit(i, sp->enforce.u.longs))
8405 dfprintk(MOUNT, " enforce op %d\n", i);
8406 if (test_bit(i, sp->allow.u.longs))
8407 dfprintk(MOUNT, " allow op %d\n", i);
8410 /* make sure nothing is on enforce list that isn't supported */
8411 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
8412 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
8413 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
8420 * Minimal mode - state operations are allowed to use machine
8421 * credential. Note this already happens by default, so the
8422 * client doesn't have to do anything more than the negotiation.
8424 * NOTE: we don't care if EXCHANGE_ID is in the list -
8425 * we're already using the machine cred for exchange_id
8426 * and will never use a different cred.
8428 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
8429 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
8430 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
8431 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
8432 dfprintk(MOUNT, "sp4_mach_cred:\n");
8433 dfprintk(MOUNT, " minimal mode enabled\n");
8434 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
8436 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
8441 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
8442 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
8443 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
8444 test_bit(OP_LOCKU, sp->allow.u.longs)) {
8445 dfprintk(MOUNT, " cleanup mode enabled\n");
8446 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
8449 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
8450 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
8451 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
8454 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
8455 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
8456 dfprintk(MOUNT, " secinfo mode enabled\n");
8457 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
8460 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
8461 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
8462 dfprintk(MOUNT, " stateid mode enabled\n");
8463 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
8466 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
8467 dfprintk(MOUNT, " write mode enabled\n");
8468 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
8471 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
8472 dfprintk(MOUNT, " commit mode enabled\n");
8473 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
8477 clp->cl_sp4_flags = flags;
8481 struct nfs41_exchange_id_data {
8482 struct nfs41_exchange_id_res res;
8483 struct nfs41_exchange_id_args args;
8486 static void nfs4_exchange_id_release(void *data)
8488 struct nfs41_exchange_id_data *cdata =
8489 (struct nfs41_exchange_id_data *)data;
8491 nfs_put_client(cdata->args.client);
8492 kfree(cdata->res.impl_id);
8493 kfree(cdata->res.server_scope);
8494 kfree(cdata->res.server_owner);
8498 static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
8499 .rpc_release = nfs4_exchange_id_release,
8503 * _nfs4_proc_exchange_id()
8505 * Wrapper for EXCHANGE_ID operation.
8507 static struct rpc_task *
8508 nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
8509 u32 sp4_how, struct rpc_xprt *xprt)
8511 struct rpc_message msg = {
8512 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
8515 struct rpc_task_setup task_setup_data = {
8516 .rpc_client = clp->cl_rpcclient,
8517 .callback_ops = &nfs4_exchange_id_call_ops,
8518 .rpc_message = &msg,
8519 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
8521 struct nfs41_exchange_id_data *calldata;
8524 if (!refcount_inc_not_zero(&clp->cl_count))
8525 return ERR_PTR(-EIO);
8528 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8532 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
8534 status = nfs4_init_uniform_client_string(clp);
8538 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
8541 if (unlikely(calldata->res.server_owner == NULL))
8544 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
8546 if (unlikely(calldata->res.server_scope == NULL))
8547 goto out_server_owner;
8549 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
8550 if (unlikely(calldata->res.impl_id == NULL))
8551 goto out_server_scope;
8555 calldata->args.state_protect.how = SP4_NONE;
8559 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
8569 task_setup_data.rpc_xprt = xprt;
8570 task_setup_data.flags |= RPC_TASK_SOFTCONN;
8571 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
8572 sizeof(calldata->args.verifier.data));
8574 calldata->args.client = clp;
8575 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
8576 EXCHGID4_FLAG_BIND_PRINC_STATEID;
8577 #ifdef CONFIG_NFS_V4_1_MIGRATION
8578 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
8580 msg.rpc_argp = &calldata->args;
8581 msg.rpc_resp = &calldata->res;
8582 task_setup_data.callback_data = calldata;
8584 return rpc_run_task(&task_setup_data);
8587 kfree(calldata->res.impl_id);
8589 kfree(calldata->res.server_scope);
8591 kfree(calldata->res.server_owner);
8595 nfs_put_client(clp);
8596 return ERR_PTR(status);
8600 * _nfs4_proc_exchange_id()
8602 * Wrapper for EXCHANGE_ID operation.
8604 static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
8607 struct rpc_task *task;
8608 struct nfs41_exchange_id_args *argp;
8609 struct nfs41_exchange_id_res *resp;
8610 unsigned long now = jiffies;
8613 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
8615 return PTR_ERR(task);
8617 argp = task->tk_msg.rpc_argp;
8618 resp = task->tk_msg.rpc_resp;
8619 status = task->tk_status;
8623 status = nfs4_check_cl_exchange_flags(resp->flags,
8624 clp->cl_mvops->minor_version);
8628 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8632 do_renew_lease(clp, now);
8634 clp->cl_clientid = resp->clientid;
8635 clp->cl_exchange_flags = resp->flags;
8636 clp->cl_seqid = resp->seqid;
8637 /* Client ID is not confirmed */
8638 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8639 clear_bit(NFS4_SESSION_ESTABLISHED,
8640 &clp->cl_session->session_state);
8642 if (clp->cl_serverscope != NULL &&
8643 !nfs41_same_server_scope(clp->cl_serverscope,
8644 resp->server_scope)) {
8645 dprintk("%s: server_scope mismatch detected\n",
8647 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8650 swap(clp->cl_serverowner, resp->server_owner);
8651 swap(clp->cl_serverscope, resp->server_scope);
8652 swap(clp->cl_implid, resp->impl_id);
8654 /* Save the EXCHANGE_ID verifier session trunk tests */
8655 memcpy(clp->cl_confirm.data, argp->verifier.data,
8656 sizeof(clp->cl_confirm.data));
8658 trace_nfs4_exchange_id(clp, status);
8664 * nfs4_proc_exchange_id()
8666 * Returns zero, a negative errno, or a negative NFS4ERR status code.
8668 * Since the clientid has expired, all compounds using sessions
8669 * associated with the stale clientid will be returning
8670 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
8671 * be in some phase of session reset.
8673 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
8675 int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
8677 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8680 /* try SP4_MACH_CRED if krb5i/p */
8681 if (authflavor == RPC_AUTH_GSS_KRB5I ||
8682 authflavor == RPC_AUTH_GSS_KRB5P) {
8683 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
8689 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
8693 * nfs4_test_session_trunk
8695 * This is an add_xprt_test() test function called from
8696 * rpc_clnt_setup_test_and_add_xprt.
8698 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8699 * and is dereferrenced in nfs4_exchange_id_release
8701 * Upon success, add the new transport to the rpc_clnt
8703 * @clnt: struct rpc_clnt to get new transport
8704 * @xprt: the rpc_xprt to test
8705 * @data: call data for _nfs4_proc_exchange_id.
8707 void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
8710 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
8711 struct rpc_task *task;
8716 dprintk("--> %s try %s\n", __func__,
8717 xprt->address_strings[RPC_DISPLAY_ADDR]);
8719 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8721 /* Test connection for session trunking. Async exchange_id call */
8722 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8726 status = task->tk_status;
8728 status = nfs4_detect_session_trunking(adata->clp,
8729 task->tk_msg.rpc_resp, xprt);
8732 rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
8736 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
8738 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
8739 const struct cred *cred)
8741 struct rpc_message msg = {
8742 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8748 status = rpc_call_sync(clp->cl_rpcclient, &msg,
8749 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
8750 trace_nfs4_destroy_clientid(clp, status);
8752 dprintk("NFS: Got error %d from the server %s on "
8753 "DESTROY_CLIENTID.", status, clp->cl_hostname);
8757 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
8758 const struct cred *cred)
8763 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8764 ret = _nfs4_proc_destroy_clientid(clp, cred);
8766 case -NFS4ERR_DELAY:
8767 case -NFS4ERR_CLIENTID_BUSY:
8777 int nfs4_destroy_clientid(struct nfs_client *clp)
8779 const struct cred *cred;
8782 if (clp->cl_mvops->minor_version < 1)
8784 if (clp->cl_exchange_flags == 0)
8786 if (clp->cl_preserve_clid)
8788 cred = nfs4_get_clid_cred(clp);
8789 ret = nfs4_proc_destroy_clientid(clp, cred);
8793 case -NFS4ERR_STALE_CLIENTID:
8794 clp->cl_exchange_flags = 0;
8800 #endif /* CONFIG_NFS_V4_1 */
8802 struct nfs4_get_lease_time_data {
8803 struct nfs4_get_lease_time_args *args;
8804 struct nfs4_get_lease_time_res *res;
8805 struct nfs_client *clp;
8808 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
8811 struct nfs4_get_lease_time_data *data =
8812 (struct nfs4_get_lease_time_data *)calldata;
8814 dprintk("--> %s\n", __func__);
8815 /* just setup sequence, do not trigger session recovery
8816 since we're invoked within one */
8817 nfs4_setup_sequence(data->clp,
8818 &data->args->la_seq_args,
8819 &data->res->lr_seq_res,
8821 dprintk("<-- %s\n", __func__);
8825 * Called from nfs4_state_manager thread for session setup, so don't recover
8826 * from sequence operation or clientid errors.
8828 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8830 struct nfs4_get_lease_time_data *data =
8831 (struct nfs4_get_lease_time_data *)calldata;
8833 dprintk("--> %s\n", __func__);
8834 if (!nfs4_sequence_done(task, &data->res->lr_seq_res))
8836 switch (task->tk_status) {
8837 case -NFS4ERR_DELAY:
8838 case -NFS4ERR_GRACE:
8839 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
8840 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8841 task->tk_status = 0;
8843 case -NFS4ERR_RETRY_UNCACHED_REP:
8844 rpc_restart_call_prepare(task);
8847 dprintk("<-- %s\n", __func__);
8850 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
8851 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8852 .rpc_call_done = nfs4_get_lease_time_done,
8855 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8857 struct nfs4_get_lease_time_args args;
8858 struct nfs4_get_lease_time_res res = {
8859 .lr_fsinfo = fsinfo,
8861 struct nfs4_get_lease_time_data data = {
8866 struct rpc_message msg = {
8867 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8871 struct rpc_task_setup task_setup = {
8872 .rpc_client = clp->cl_rpcclient,
8873 .rpc_message = &msg,
8874 .callback_ops = &nfs4_get_lease_time_ops,
8875 .callback_data = &data,
8876 .flags = RPC_TASK_TIMEOUT,
8879 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
8880 return nfs4_call_sync_custom(&task_setup);
8883 #ifdef CONFIG_NFS_V4_1
8886 * Initialize the values to be used by the client in CREATE_SESSION
8887 * If nfs4_init_session set the fore channel request and response sizes,
8890 * Set the back channel max_resp_sz_cached to zero to force the client to
8891 * always set csa_cachethis to FALSE because the current implementation
8892 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8894 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8895 struct rpc_clnt *clnt)
8897 unsigned int max_rqst_sz, max_resp_sz;
8898 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
8899 unsigned int max_bc_slots = rpc_num_bc_slots(clnt);
8901 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8902 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8904 /* Fore channel attributes */
8905 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8906 args->fc_attrs.max_resp_sz = max_resp_sz;
8907 args->fc_attrs.max_ops = NFS4_MAX_OPS;
8908 args->fc_attrs.max_reqs = max_session_slots;
8910 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
8911 "max_ops=%u max_reqs=%u\n",
8913 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
8914 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
8916 /* Back channel attributes */
8917 args->bc_attrs.max_rqst_sz = max_bc_payload;
8918 args->bc_attrs.max_resp_sz = max_bc_payload;
8919 args->bc_attrs.max_resp_sz_cached = 0;
8920 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
8921 args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
8922 if (args->bc_attrs.max_reqs > max_bc_slots)
8923 args->bc_attrs.max_reqs = max_bc_slots;
8925 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8926 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8928 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8929 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8930 args->bc_attrs.max_reqs);
8933 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8934 struct nfs41_create_session_res *res)
8936 struct nfs4_channel_attrs *sent = &args->fc_attrs;
8937 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
8939 if (rcvd->max_resp_sz > sent->max_resp_sz)
8942 * Our requested max_ops is the minimum we need; we're not
8943 * prepared to break up compounds into smaller pieces than that.
8944 * So, no point even trying to continue if the server won't
8947 if (rcvd->max_ops < sent->max_ops)
8949 if (rcvd->max_reqs == 0)
8951 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8952 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
8956 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8957 struct nfs41_create_session_res *res)
8959 struct nfs4_channel_attrs *sent = &args->bc_attrs;
8960 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
8962 if (!(res->flags & SESSION4_BACK_CHAN))
8964 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8966 if (rcvd->max_resp_sz < sent->max_resp_sz)
8968 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8970 if (rcvd->max_ops > sent->max_ops)
8972 if (rcvd->max_reqs > sent->max_reqs)
8978 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
8979 struct nfs41_create_session_res *res)
8983 ret = nfs4_verify_fore_channel_attrs(args, res);
8986 return nfs4_verify_back_channel_attrs(args, res);
8989 static void nfs4_update_session(struct nfs4_session *session,
8990 struct nfs41_create_session_res *res)
8992 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
8993 /* Mark client id and session as being confirmed */
8994 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8995 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
8996 session->flags = res->flags;
8997 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
8998 if (res->flags & SESSION4_BACK_CHAN)
8999 memcpy(&session->bc_attrs, &res->bc_attrs,
9000 sizeof(session->bc_attrs));
9003 static int _nfs4_proc_create_session(struct nfs_client *clp,
9004 const struct cred *cred)
9006 struct nfs4_session *session = clp->cl_session;
9007 struct nfs41_create_session_args args = {
9009 .clientid = clp->cl_clientid,
9010 .seqid = clp->cl_seqid,
9011 .cb_program = NFS4_CALLBACK,
9013 struct nfs41_create_session_res res;
9015 struct rpc_message msg = {
9016 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
9023 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
9024 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
9026 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
9027 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
9028 trace_nfs4_create_session(clp, status);
9031 case -NFS4ERR_STALE_CLIENTID:
9032 case -NFS4ERR_DELAY:
9041 /* Verify the session's negotiated channel_attrs values */
9042 status = nfs4_verify_channel_attrs(&args, &res);
9043 /* Increment the clientid slot sequence id */
9046 nfs4_update_session(session, &res);
9053 * Issues a CREATE_SESSION operation to the server.
9054 * It is the responsibility of the caller to verify the session is
9055 * expired before calling this routine.
9057 int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
9061 struct nfs4_session *session = clp->cl_session;
9063 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
9065 status = _nfs4_proc_create_session(clp, cred);
9069 /* Init or reset the session slot tables */
9070 status = nfs4_setup_session_slot_tables(session);
9071 dprintk("slot table setup returned %d\n", status);
9075 ptr = (unsigned *)&session->sess_id.data[0];
9076 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
9077 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
9079 dprintk("<-- %s\n", __func__);
9084 * Issue the over-the-wire RPC DESTROY_SESSION.
9085 * The caller must serialize access to this routine.
9087 int nfs4_proc_destroy_session(struct nfs4_session *session,
9088 const struct cred *cred)
9090 struct rpc_message msg = {
9091 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
9092 .rpc_argp = session,
9097 dprintk("--> nfs4_proc_destroy_session\n");
9099 /* session is still being setup */
9100 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
9103 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
9104 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
9105 trace_nfs4_destroy_session(session->clp, status);
9108 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
9109 "Session has been destroyed regardless...\n", status);
9111 dprintk("<-- nfs4_proc_destroy_session\n");
9116 * Renew the cl_session lease.
9118 struct nfs4_sequence_data {
9119 struct nfs_client *clp;
9120 struct nfs4_sequence_args args;
9121 struct nfs4_sequence_res res;
9124 static void nfs41_sequence_release(void *data)
9126 struct nfs4_sequence_data *calldata = data;
9127 struct nfs_client *clp = calldata->clp;
9129 if (refcount_read(&clp->cl_count) > 1)
9130 nfs4_schedule_state_renewal(clp);
9131 nfs_put_client(clp);
9135 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9137 switch(task->tk_status) {
9138 case -NFS4ERR_DELAY:
9139 rpc_delay(task, NFS4_POLL_RETRY_MAX);
9142 nfs4_schedule_lease_recovery(clp);
9147 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
9149 struct nfs4_sequence_data *calldata = data;
9150 struct nfs_client *clp = calldata->clp;
9152 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
9155 trace_nfs4_sequence(clp, task->tk_status);
9156 if (task->tk_status < 0) {
9157 dprintk("%s ERROR %d\n", __func__, task->tk_status);
9158 if (refcount_read(&clp->cl_count) == 1)
9161 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
9162 rpc_restart_call_prepare(task);
9166 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
9168 dprintk("<-- %s\n", __func__);
9171 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
9173 struct nfs4_sequence_data *calldata = data;
9174 struct nfs_client *clp = calldata->clp;
9175 struct nfs4_sequence_args *args;
9176 struct nfs4_sequence_res *res;
9178 args = task->tk_msg.rpc_argp;
9179 res = task->tk_msg.rpc_resp;
9181 nfs4_setup_sequence(clp, args, res, task);
9184 static const struct rpc_call_ops nfs41_sequence_ops = {
9185 .rpc_call_done = nfs41_sequence_call_done,
9186 .rpc_call_prepare = nfs41_sequence_prepare,
9187 .rpc_release = nfs41_sequence_release,
9190 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
9191 const struct cred *cred,
9192 struct nfs4_slot *slot,
9195 struct nfs4_sequence_data *calldata;
9196 struct rpc_message msg = {
9197 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
9200 struct rpc_task_setup task_setup_data = {
9201 .rpc_client = clp->cl_rpcclient,
9202 .rpc_message = &msg,
9203 .callback_ops = &nfs41_sequence_ops,
9204 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
9206 struct rpc_task *ret;
9208 ret = ERR_PTR(-EIO);
9209 if (!refcount_inc_not_zero(&clp->cl_count))
9212 ret = ERR_PTR(-ENOMEM);
9213 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
9214 if (calldata == NULL)
9216 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
9217 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
9218 msg.rpc_argp = &calldata->args;
9219 msg.rpc_resp = &calldata->res;
9220 calldata->clp = clp;
9221 task_setup_data.callback_data = calldata;
9223 ret = rpc_run_task(&task_setup_data);
9228 nfs_put_client(clp);
9230 nfs41_release_slot(slot);
9234 static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
9236 struct rpc_task *task;
9239 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
9241 task = _nfs41_proc_sequence(clp, cred, NULL, false);
9243 ret = PTR_ERR(task);
9245 rpc_put_task_async(task);
9246 dprintk("<-- %s status=%d\n", __func__, ret);
9250 static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
9252 struct rpc_task *task;
9255 task = _nfs41_proc_sequence(clp, cred, NULL, true);
9257 ret = PTR_ERR(task);
9260 ret = rpc_wait_for_completion_task(task);
9262 ret = task->tk_status;
9265 dprintk("<-- %s status=%d\n", __func__, ret);
9269 struct nfs4_reclaim_complete_data {
9270 struct nfs_client *clp;
9271 struct nfs41_reclaim_complete_args arg;
9272 struct nfs41_reclaim_complete_res res;
9275 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
9277 struct nfs4_reclaim_complete_data *calldata = data;
9279 nfs4_setup_sequence(calldata->clp,
9280 &calldata->arg.seq_args,
9281 &calldata->res.seq_res,
9285 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9287 switch(task->tk_status) {
9289 wake_up_all(&clp->cl_lock_waitq);
9291 case -NFS4ERR_COMPLETE_ALREADY:
9292 case -NFS4ERR_WRONG_CRED: /* What to do here? */
9294 case -NFS4ERR_DELAY:
9295 rpc_delay(task, NFS4_POLL_RETRY_MAX);
9297 case -NFS4ERR_RETRY_UNCACHED_REP:
9299 dprintk("%s: failed to reclaim complete error %d for server %s, retrying\n",
9300 __func__, task->tk_status, clp->cl_hostname);
9302 case -NFS4ERR_BADSESSION:
9303 case -NFS4ERR_DEADSESSION:
9304 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9307 nfs4_schedule_lease_recovery(clp);
9312 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
9314 struct nfs4_reclaim_complete_data *calldata = data;
9315 struct nfs_client *clp = calldata->clp;
9316 struct nfs4_sequence_res *res = &calldata->res.seq_res;
9318 dprintk("--> %s\n", __func__);
9319 if (!nfs41_sequence_done(task, res))
9322 trace_nfs4_reclaim_complete(clp, task->tk_status);
9323 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
9324 rpc_restart_call_prepare(task);
9327 dprintk("<-- %s\n", __func__);
9330 static void nfs4_free_reclaim_complete_data(void *data)
9332 struct nfs4_reclaim_complete_data *calldata = data;
9337 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
9338 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
9339 .rpc_call_done = nfs4_reclaim_complete_done,
9340 .rpc_release = nfs4_free_reclaim_complete_data,
9344 * Issue a global reclaim complete.
9346 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
9347 const struct cred *cred)
9349 struct nfs4_reclaim_complete_data *calldata;
9350 struct rpc_message msg = {
9351 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
9354 struct rpc_task_setup task_setup_data = {
9355 .rpc_client = clp->cl_rpcclient,
9356 .rpc_message = &msg,
9357 .callback_ops = &nfs4_reclaim_complete_call_ops,
9358 .flags = RPC_TASK_NO_ROUND_ROBIN,
9360 int status = -ENOMEM;
9362 dprintk("--> %s\n", __func__);
9363 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
9364 if (calldata == NULL)
9366 calldata->clp = clp;
9367 calldata->arg.one_fs = 0;
9369 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
9370 msg.rpc_argp = &calldata->arg;
9371 msg.rpc_resp = &calldata->res;
9372 task_setup_data.callback_data = calldata;
9373 status = nfs4_call_sync_custom(&task_setup_data);
9375 dprintk("<-- %s status=%d\n", __func__, status);
9380 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
9382 struct nfs4_layoutget *lgp = calldata;
9383 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
9385 dprintk("--> %s\n", __func__);
9386 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
9387 &lgp->res.seq_res, task);
9388 dprintk("<-- %s\n", __func__);
9391 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
9393 struct nfs4_layoutget *lgp = calldata;
9395 dprintk("--> %s\n", __func__);
9396 nfs41_sequence_process(task, &lgp->res.seq_res);
9397 dprintk("<-- %s\n", __func__);
9401 nfs4_layoutget_handle_exception(struct rpc_task *task,
9402 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
9404 struct inode *inode = lgp->args.inode;
9405 struct nfs_server *server = NFS_SERVER(inode);
9406 struct pnfs_layout_hdr *lo;
9407 int nfs4err = task->tk_status;
9408 int err, status = 0;
9411 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
9413 nfs4_sequence_free_slot(&lgp->res.seq_res);
9420 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
9421 * on the file. set tk_status to -ENODATA to tell upper layer to
9424 case -NFS4ERR_LAYOUTUNAVAILABLE:
9428 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
9429 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
9431 case -NFS4ERR_BADLAYOUT:
9432 status = -EOVERFLOW;
9435 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
9436 * (or clients) writing to the same RAID stripe except when
9437 * the minlength argument is 0 (see RFC5661 section 18.43.3).
9439 * Treat it like we would RECALLCONFLICT -- we retry for a little
9440 * while, and then eventually give up.
9442 case -NFS4ERR_LAYOUTTRYLATER:
9443 if (lgp->args.minlength == 0) {
9444 status = -EOVERFLOW;
9449 case -NFS4ERR_RECALLCONFLICT:
9450 case -NFS4ERR_RETURNCONFLICT:
9451 status = -ERECALLCONFLICT;
9453 case -NFS4ERR_DELEG_REVOKED:
9454 case -NFS4ERR_ADMIN_REVOKED:
9455 case -NFS4ERR_EXPIRED:
9456 case -NFS4ERR_BAD_STATEID:
9457 exception->timeout = 0;
9458 spin_lock(&inode->i_lock);
9459 lo = NFS_I(inode)->layout;
9460 /* If the open stateid was bad, then recover it. */
9461 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
9462 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
9463 spin_unlock(&inode->i_lock);
9464 exception->state = lgp->args.ctx->state;
9465 exception->stateid = &lgp->args.stateid;
9470 * Mark the bad layout state as invalid, then retry
9472 pnfs_mark_layout_stateid_invalid(lo, &head);
9473 spin_unlock(&inode->i_lock);
9474 nfs_commit_inode(inode, 0);
9475 pnfs_free_lseg_list(&head);
9480 err = nfs4_handle_exception(server, nfs4err, exception);
9482 if (exception->retry)
9488 dprintk("<-- %s\n", __func__);
9492 size_t max_response_pages(struct nfs_server *server)
9494 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
9495 return nfs_page_array_len(0, max_resp_sz);
9498 static void nfs4_layoutget_release(void *calldata)
9500 struct nfs4_layoutget *lgp = calldata;
9502 dprintk("--> %s\n", __func__);
9503 nfs4_sequence_free_slot(&lgp->res.seq_res);
9504 pnfs_layoutget_free(lgp);
9505 dprintk("<-- %s\n", __func__);
9508 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
9509 .rpc_call_prepare = nfs4_layoutget_prepare,
9510 .rpc_call_done = nfs4_layoutget_done,
9511 .rpc_release = nfs4_layoutget_release,
9514 struct pnfs_layout_segment *
9515 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
9517 struct inode *inode = lgp->args.inode;
9518 struct nfs_server *server = NFS_SERVER(inode);
9519 struct rpc_task *task;
9520 struct rpc_message msg = {
9521 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
9522 .rpc_argp = &lgp->args,
9523 .rpc_resp = &lgp->res,
9524 .rpc_cred = lgp->cred,
9526 struct rpc_task_setup task_setup_data = {
9527 .rpc_client = server->client,
9528 .rpc_message = &msg,
9529 .callback_ops = &nfs4_layoutget_call_ops,
9530 .callback_data = lgp,
9531 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
9533 struct pnfs_layout_segment *lseg = NULL;
9534 struct nfs4_exception exception = {
9536 .timeout = *timeout,
9540 dprintk("--> %s\n", __func__);
9542 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
9543 pnfs_get_layout_hdr(NFS_I(inode)->layout);
9545 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
9547 task = rpc_run_task(&task_setup_data);
9549 status = rpc_wait_for_completion_task(task);
9553 if (task->tk_status < 0) {
9554 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
9555 *timeout = exception.timeout;
9556 } else if (lgp->res.layoutp->len == 0) {
9558 *timeout = nfs4_update_delay(&exception.timeout);
9560 lseg = pnfs_layout_process(lgp);
9562 trace_nfs4_layoutget(lgp->args.ctx,
9569 dprintk("<-- %s status=%d\n", __func__, status);
9571 return ERR_PTR(status);
9576 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
9578 struct nfs4_layoutreturn *lrp = calldata;
9580 dprintk("--> %s\n", __func__);
9581 nfs4_setup_sequence(lrp->clp,
9582 &lrp->args.seq_args,
9585 if (!pnfs_layout_is_valid(lrp->args.layout))
9589 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
9591 struct nfs4_layoutreturn *lrp = calldata;
9592 struct nfs_server *server;
9594 dprintk("--> %s\n", __func__);
9596 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
9600 * Was there an RPC level error? Assume the call succeeded,
9601 * and that we need to release the layout
9603 if (task->tk_rpc_status != 0 && RPC_WAS_SENT(task)) {
9604 lrp->res.lrs_present = 0;
9608 server = NFS_SERVER(lrp->args.inode);
9609 switch (task->tk_status) {
9610 case -NFS4ERR_OLD_STATEID:
9611 if (nfs4_layout_refresh_old_stateid(&lrp->args.stateid,
9617 task->tk_status = 0;
9621 case -NFS4ERR_DELAY:
9622 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
9626 dprintk("<-- %s\n", __func__);
9629 task->tk_status = 0;
9630 nfs4_sequence_free_slot(&lrp->res.seq_res);
9631 rpc_restart_call_prepare(task);
9634 static void nfs4_layoutreturn_release(void *calldata)
9636 struct nfs4_layoutreturn *lrp = calldata;
9637 struct pnfs_layout_hdr *lo = lrp->args.layout;
9639 dprintk("--> %s\n", __func__);
9640 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
9641 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
9642 nfs4_sequence_free_slot(&lrp->res.seq_res);
9643 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9644 lrp->ld_private.ops->free(&lrp->ld_private);
9645 pnfs_put_layout_hdr(lrp->args.layout);
9646 nfs_iput_and_deactive(lrp->inode);
9647 put_cred(lrp->cred);
9649 dprintk("<-- %s\n", __func__);
9652 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9653 .rpc_call_prepare = nfs4_layoutreturn_prepare,
9654 .rpc_call_done = nfs4_layoutreturn_done,
9655 .rpc_release = nfs4_layoutreturn_release,
9658 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
9660 struct rpc_task *task;
9661 struct rpc_message msg = {
9662 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9663 .rpc_argp = &lrp->args,
9664 .rpc_resp = &lrp->res,
9665 .rpc_cred = lrp->cred,
9667 struct rpc_task_setup task_setup_data = {
9668 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
9669 .rpc_message = &msg,
9670 .callback_ops = &nfs4_layoutreturn_call_ops,
9671 .callback_data = lrp,
9675 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9676 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9677 &task_setup_data.rpc_client, &msg);
9679 dprintk("--> %s\n", __func__);
9680 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
9683 nfs4_layoutreturn_release(lrp);
9686 task_setup_data.flags |= RPC_TASK_ASYNC;
9689 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1,
9692 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1,
9694 task = rpc_run_task(&task_setup_data);
9696 return PTR_ERR(task);
9698 status = task->tk_status;
9699 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
9700 dprintk("<-- %s status=%d\n", __func__, status);
9706 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
9707 struct pnfs_device *pdev,
9708 const struct cred *cred)
9710 struct nfs4_getdeviceinfo_args args = {
9712 .notify_types = NOTIFY_DEVICEID4_CHANGE |
9713 NOTIFY_DEVICEID4_DELETE,
9715 struct nfs4_getdeviceinfo_res res = {
9718 struct rpc_message msg = {
9719 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9726 dprintk("--> %s\n", __func__);
9727 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
9728 if (res.notification & ~args.notify_types)
9729 dprintk("%s: unsupported notification\n", __func__);
9730 if (res.notification != args.notify_types)
9733 dprintk("<-- %s status=%d\n", __func__, status);
9738 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9739 struct pnfs_device *pdev,
9740 const struct cred *cred)
9742 struct nfs4_exception exception = { };
9746 err = nfs4_handle_exception(server,
9747 _nfs4_proc_getdeviceinfo(server, pdev, cred),
9749 } while (exception.retry);
9752 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9754 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9756 struct nfs4_layoutcommit_data *data = calldata;
9757 struct nfs_server *server = NFS_SERVER(data->args.inode);
9759 nfs4_setup_sequence(server->nfs_client,
9760 &data->args.seq_args,
9766 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9768 struct nfs4_layoutcommit_data *data = calldata;
9769 struct nfs_server *server = NFS_SERVER(data->args.inode);
9771 if (!nfs41_sequence_done(task, &data->res.seq_res))
9774 switch (task->tk_status) { /* Just ignore these failures */
9775 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9776 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9777 case -NFS4ERR_BADLAYOUT: /* no layout */
9778 case -NFS4ERR_GRACE: /* loca_recalim always false */
9779 task->tk_status = 0;
9783 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
9784 rpc_restart_call_prepare(task);
9790 static void nfs4_layoutcommit_release(void *calldata)
9792 struct nfs4_layoutcommit_data *data = calldata;
9794 pnfs_cleanup_layoutcommit(data);
9795 nfs_post_op_update_inode_force_wcc(data->args.inode,
9797 put_cred(data->cred);
9798 nfs_iput_and_deactive(data->inode);
9802 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9803 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9804 .rpc_call_done = nfs4_layoutcommit_done,
9805 .rpc_release = nfs4_layoutcommit_release,
9809 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
9811 struct rpc_message msg = {
9812 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9813 .rpc_argp = &data->args,
9814 .rpc_resp = &data->res,
9815 .rpc_cred = data->cred,
9817 struct rpc_task_setup task_setup_data = {
9818 .task = &data->task,
9819 .rpc_client = NFS_CLIENT(data->args.inode),
9820 .rpc_message = &msg,
9821 .callback_ops = &nfs4_layoutcommit_ops,
9822 .callback_data = data,
9824 struct rpc_task *task;
9827 dprintk("NFS: initiating layoutcommit call. sync %d "
9828 "lbw: %llu inode %lu\n", sync,
9829 data->args.lastbytewritten,
9830 data->args.inode->i_ino);
9833 data->inode = nfs_igrab_and_active(data->args.inode);
9834 if (data->inode == NULL) {
9835 nfs4_layoutcommit_release(data);
9838 task_setup_data.flags = RPC_TASK_ASYNC;
9840 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
9841 task = rpc_run_task(&task_setup_data);
9843 return PTR_ERR(task);
9845 status = task->tk_status;
9846 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
9847 dprintk("%s: status %d\n", __func__, status);
9853 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9854 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9857 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9858 struct nfs_fsinfo *info,
9859 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
9861 struct nfs41_secinfo_no_name_args args = {
9862 .style = SECINFO_STYLE_CURRENT_FH,
9864 struct nfs4_secinfo_res res = {
9867 struct rpc_message msg = {
9868 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9872 struct nfs4_call_sync_data data = {
9873 .seq_server = server,
9874 .seq_args = &args.seq_args,
9875 .seq_res = &res.seq_res,
9877 struct rpc_task_setup task_setup = {
9878 .rpc_client = server->client,
9879 .rpc_message = &msg,
9880 .callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
9881 .callback_data = &data,
9882 .flags = RPC_TASK_NO_ROUND_ROBIN,
9884 const struct cred *cred = NULL;
9887 if (use_integrity) {
9888 task_setup.rpc_client = server->nfs_client->cl_rpcclient;
9890 cred = nfs4_get_clid_cred(server->nfs_client);
9891 msg.rpc_cred = cred;
9894 dprintk("--> %s\n", __func__);
9895 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
9896 status = nfs4_call_sync_custom(&task_setup);
9897 dprintk("<-- %s status=%d\n", __func__, status);
9905 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9906 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9908 struct nfs4_exception exception = {
9909 .interruptible = true,
9913 /* first try using integrity protection */
9914 err = -NFS4ERR_WRONGSEC;
9916 /* try to use integrity protection with machine cred */
9917 if (_nfs4_is_integrity_protected(server->nfs_client))
9918 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9922 * if unable to use integrity protection, or SECINFO with
9923 * integrity protection returns NFS4ERR_WRONGSEC (which is
9924 * disallowed by spec, but exists in deployed servers) use
9925 * the current filesystem's rpc_client and the user cred.
9927 if (err == -NFS4ERR_WRONGSEC)
9928 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9933 case -NFS4ERR_WRONGSEC:
9937 err = nfs4_handle_exception(server, err, &exception);
9939 } while (exception.retry);
9945 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9946 struct nfs_fsinfo *info)
9950 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
9951 struct nfs4_secinfo_flavors *flavors;
9952 struct nfs4_secinfo4 *secinfo;
9955 page = alloc_page(GFP_KERNEL);
9961 flavors = page_address(page);
9962 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9965 * Fall back on "guess and check" method if
9966 * the server doesn't support SECINFO_NO_NAME
9968 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
9969 err = nfs4_find_root_sec(server, fhandle, info);
9975 for (i = 0; i < flavors->num_flavors; i++) {
9976 secinfo = &flavors->flavors[i];
9978 switch (secinfo->flavor) {
9982 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9983 &secinfo->flavor_info);
9986 flavor = RPC_AUTH_MAXFLAVOR;
9990 if (!nfs_auth_info_match(&server->auth_info, flavor))
9991 flavor = RPC_AUTH_MAXFLAVOR;
9993 if (flavor != RPC_AUTH_MAXFLAVOR) {
9994 err = nfs4_lookup_root_sec(server, fhandle,
10001 if (flavor == RPC_AUTH_MAXFLAVOR)
10006 if (err == -EACCES)
10012 static int _nfs41_test_stateid(struct nfs_server *server,
10013 nfs4_stateid *stateid,
10014 const struct cred *cred)
10017 struct nfs41_test_stateid_args args = {
10018 .stateid = stateid,
10020 struct nfs41_test_stateid_res res;
10021 struct rpc_message msg = {
10022 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
10027 struct rpc_clnt *rpc_client = server->client;
10029 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
10030 &rpc_client, &msg);
10032 dprintk("NFS call test_stateid %p\n", stateid);
10033 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
10034 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
10035 &args.seq_args, &res.seq_res);
10036 if (status != NFS_OK) {
10037 dprintk("NFS reply test_stateid: failed, %d\n", status);
10040 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
10041 return -res.status;
10044 static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
10045 int err, struct nfs4_exception *exception)
10047 exception->retry = 0;
10049 case -NFS4ERR_DELAY:
10050 case -NFS4ERR_RETRY_UNCACHED_REP:
10051 nfs4_handle_exception(server, err, exception);
10053 case -NFS4ERR_BADSESSION:
10054 case -NFS4ERR_BADSLOT:
10055 case -NFS4ERR_BAD_HIGH_SLOT:
10056 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
10057 case -NFS4ERR_DEADSESSION:
10058 nfs4_do_handle_exception(server, err, exception);
10063 * nfs41_test_stateid - perform a TEST_STATEID operation
10065 * @server: server / transport on which to perform the operation
10066 * @stateid: state ID to test
10067 * @cred: credential
10069 * Returns NFS_OK if the server recognizes that "stateid" is valid.
10070 * Otherwise a negative NFS4ERR value is returned if the operation
10071 * failed or the state ID is not currently valid.
10073 static int nfs41_test_stateid(struct nfs_server *server,
10074 nfs4_stateid *stateid,
10075 const struct cred *cred)
10077 struct nfs4_exception exception = {
10078 .interruptible = true,
10082 err = _nfs41_test_stateid(server, stateid, cred);
10083 nfs4_handle_delay_or_session_error(server, err, &exception);
10084 } while (exception.retry);
10088 struct nfs_free_stateid_data {
10089 struct nfs_server *server;
10090 struct nfs41_free_stateid_args args;
10091 struct nfs41_free_stateid_res res;
10094 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
10096 struct nfs_free_stateid_data *data = calldata;
10097 nfs4_setup_sequence(data->server->nfs_client,
10098 &data->args.seq_args,
10099 &data->res.seq_res,
10103 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
10105 struct nfs_free_stateid_data *data = calldata;
10107 nfs41_sequence_done(task, &data->res.seq_res);
10109 switch (task->tk_status) {
10110 case -NFS4ERR_DELAY:
10111 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
10112 rpc_restart_call_prepare(task);
10116 static void nfs41_free_stateid_release(void *calldata)
10121 static const struct rpc_call_ops nfs41_free_stateid_ops = {
10122 .rpc_call_prepare = nfs41_free_stateid_prepare,
10123 .rpc_call_done = nfs41_free_stateid_done,
10124 .rpc_release = nfs41_free_stateid_release,
10128 * nfs41_free_stateid - perform a FREE_STATEID operation
10130 * @server: server / transport on which to perform the operation
10131 * @stateid: state ID to release
10132 * @cred: credential
10133 * @privileged: set to true if this call needs to be privileged
10135 * Note: this function is always asynchronous.
10137 static int nfs41_free_stateid(struct nfs_server *server,
10138 const nfs4_stateid *stateid,
10139 const struct cred *cred,
10142 struct rpc_message msg = {
10143 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
10146 struct rpc_task_setup task_setup = {
10147 .rpc_client = server->client,
10148 .rpc_message = &msg,
10149 .callback_ops = &nfs41_free_stateid_ops,
10150 .flags = RPC_TASK_ASYNC,
10152 struct nfs_free_stateid_data *data;
10153 struct rpc_task *task;
10155 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
10156 &task_setup.rpc_client, &msg);
10158 dprintk("NFS call free_stateid %p\n", stateid);
10159 data = kmalloc(sizeof(*data), GFP_NOFS);
10162 data->server = server;
10163 nfs4_stateid_copy(&data->args.stateid, stateid);
10165 task_setup.callback_data = data;
10167 msg.rpc_argp = &data->args;
10168 msg.rpc_resp = &data->res;
10169 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
10170 task = rpc_run_task(&task_setup);
10172 return PTR_ERR(task);
10173 rpc_put_task(task);
10178 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
10180 const struct cred *cred = lsp->ls_state->owner->so_cred;
10182 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
10183 nfs4_free_lock_state(server, lsp);
10186 static bool nfs41_match_stateid(const nfs4_stateid *s1,
10187 const nfs4_stateid *s2)
10189 if (s1->type != s2->type)
10192 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
10195 if (s1->seqid == s2->seqid)
10198 return s1->seqid == 0 || s2->seqid == 0;
10201 #endif /* CONFIG_NFS_V4_1 */
10203 static bool nfs4_match_stateid(const nfs4_stateid *s1,
10204 const nfs4_stateid *s2)
10206 return nfs4_stateid_match(s1, s2);
10210 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
10211 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
10212 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
10213 .recover_open = nfs4_open_reclaim,
10214 .recover_lock = nfs4_lock_reclaim,
10215 .establish_clid = nfs4_init_clientid,
10216 .detect_trunking = nfs40_discover_server_trunking,
10219 #if defined(CONFIG_NFS_V4_1)
10220 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
10221 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
10222 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
10223 .recover_open = nfs4_open_reclaim,
10224 .recover_lock = nfs4_lock_reclaim,
10225 .establish_clid = nfs41_init_clientid,
10226 .reclaim_complete = nfs41_proc_reclaim_complete,
10227 .detect_trunking = nfs41_discover_server_trunking,
10229 #endif /* CONFIG_NFS_V4_1 */
10231 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
10232 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
10233 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
10234 .recover_open = nfs40_open_expired,
10235 .recover_lock = nfs4_lock_expired,
10236 .establish_clid = nfs4_init_clientid,
10239 #if defined(CONFIG_NFS_V4_1)
10240 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
10241 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
10242 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
10243 .recover_open = nfs41_open_expired,
10244 .recover_lock = nfs41_lock_expired,
10245 .establish_clid = nfs41_init_clientid,
10247 #endif /* CONFIG_NFS_V4_1 */
10249 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
10250 .sched_state_renewal = nfs4_proc_async_renew,
10251 .get_state_renewal_cred = nfs4_get_renew_cred,
10252 .renew_lease = nfs4_proc_renew,
10255 #if defined(CONFIG_NFS_V4_1)
10256 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
10257 .sched_state_renewal = nfs41_proc_async_sequence,
10258 .get_state_renewal_cred = nfs4_get_machine_cred,
10259 .renew_lease = nfs4_proc_sequence,
10263 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
10264 .get_locations = _nfs40_proc_get_locations,
10265 .fsid_present = _nfs40_proc_fsid_present,
10268 #if defined(CONFIG_NFS_V4_1)
10269 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
10270 .get_locations = _nfs41_proc_get_locations,
10271 .fsid_present = _nfs41_proc_fsid_present,
10273 #endif /* CONFIG_NFS_V4_1 */
10275 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
10276 .minor_version = 0,
10277 .init_caps = NFS_CAP_READDIRPLUS
10278 | NFS_CAP_ATOMIC_OPEN
10279 | NFS_CAP_POSIX_LOCK,
10280 .init_client = nfs40_init_client,
10281 .shutdown_client = nfs40_shutdown_client,
10282 .match_stateid = nfs4_match_stateid,
10283 .find_root_sec = nfs4_find_root_sec,
10284 .free_lock_state = nfs4_release_lockowner,
10285 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
10286 .alloc_seqid = nfs_alloc_seqid,
10287 .call_sync_ops = &nfs40_call_sync_ops,
10288 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
10289 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
10290 .state_renewal_ops = &nfs40_state_renewal_ops,
10291 .mig_recovery_ops = &nfs40_mig_recovery_ops,
10294 #if defined(CONFIG_NFS_V4_1)
10295 static struct nfs_seqid *
10296 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
10301 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
10302 .minor_version = 1,
10303 .init_caps = NFS_CAP_READDIRPLUS
10304 | NFS_CAP_ATOMIC_OPEN
10305 | NFS_CAP_POSIX_LOCK
10306 | NFS_CAP_STATEID_NFSV41
10307 | NFS_CAP_ATOMIC_OPEN_V1
10309 .init_client = nfs41_init_client,
10310 .shutdown_client = nfs41_shutdown_client,
10311 .match_stateid = nfs41_match_stateid,
10312 .find_root_sec = nfs41_find_root_sec,
10313 .free_lock_state = nfs41_free_lock_state,
10314 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
10315 .alloc_seqid = nfs_alloc_no_seqid,
10316 .session_trunk = nfs4_test_session_trunk,
10317 .call_sync_ops = &nfs41_call_sync_ops,
10318 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10319 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10320 .state_renewal_ops = &nfs41_state_renewal_ops,
10321 .mig_recovery_ops = &nfs41_mig_recovery_ops,
10325 #if defined(CONFIG_NFS_V4_2)
10326 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
10327 .minor_version = 2,
10328 .init_caps = NFS_CAP_READDIRPLUS
10329 | NFS_CAP_ATOMIC_OPEN
10330 | NFS_CAP_POSIX_LOCK
10331 | NFS_CAP_STATEID_NFSV41
10332 | NFS_CAP_ATOMIC_OPEN_V1
10336 | NFS_CAP_OFFLOAD_CANCEL
10337 | NFS_CAP_COPY_NOTIFY
10338 | NFS_CAP_DEALLOCATE
10340 | NFS_CAP_LAYOUTSTATS
10342 | NFS_CAP_LAYOUTERROR
10343 | NFS_CAP_READ_PLUS,
10344 .init_client = nfs41_init_client,
10345 .shutdown_client = nfs41_shutdown_client,
10346 .match_stateid = nfs41_match_stateid,
10347 .find_root_sec = nfs41_find_root_sec,
10348 .free_lock_state = nfs41_free_lock_state,
10349 .call_sync_ops = &nfs41_call_sync_ops,
10350 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
10351 .alloc_seqid = nfs_alloc_no_seqid,
10352 .session_trunk = nfs4_test_session_trunk,
10353 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10354 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10355 .state_renewal_ops = &nfs41_state_renewal_ops,
10356 .mig_recovery_ops = &nfs41_mig_recovery_ops,
10360 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
10361 [0] = &nfs_v4_0_minor_ops,
10362 #if defined(CONFIG_NFS_V4_1)
10363 [1] = &nfs_v4_1_minor_ops,
10365 #if defined(CONFIG_NFS_V4_2)
10366 [2] = &nfs_v4_2_minor_ops,
10370 static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
10372 ssize_t error, error2, error3;
10373 size_t left = size;
10375 error = generic_listxattr(dentry, list, left);
10383 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, left);
10392 error3 = nfs4_listxattr_nfs4_user(d_inode(dentry), list, left);
10396 error += error2 + error3;
10397 if (size && error > size)
10402 static void nfs4_enable_swap(struct inode *inode)
10404 /* The state manager thread must always be running.
10405 * It will notice the client is a swapper, and stay put.
10407 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
10409 nfs4_schedule_state_manager(clp);
10412 static void nfs4_disable_swap(struct inode *inode)
10414 /* The state manager thread will now exit once it is
10417 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
10419 set_bit(NFS4CLNT_RUN_MANAGER, &clp->cl_state);
10420 clear_bit(NFS4CLNT_MANAGER_AVAILABLE, &clp->cl_state);
10421 wake_up_var(&clp->cl_state);
10424 static const struct inode_operations nfs4_dir_inode_operations = {
10425 .create = nfs_create,
10426 .lookup = nfs_lookup,
10427 .atomic_open = nfs_atomic_open,
10429 .unlink = nfs_unlink,
10430 .symlink = nfs_symlink,
10431 .mkdir = nfs_mkdir,
10432 .rmdir = nfs_rmdir,
10433 .mknod = nfs_mknod,
10434 .rename = nfs_rename,
10435 .permission = nfs_permission,
10436 .getattr = nfs_getattr,
10437 .setattr = nfs_setattr,
10438 .listxattr = nfs4_listxattr,
10441 static const struct inode_operations nfs4_file_inode_operations = {
10442 .permission = nfs_permission,
10443 .getattr = nfs_getattr,
10444 .setattr = nfs_setattr,
10445 .listxattr = nfs4_listxattr,
10448 const struct nfs_rpc_ops nfs_v4_clientops = {
10449 .version = 4, /* protocol version */
10450 .dentry_ops = &nfs4_dentry_operations,
10451 .dir_inode_ops = &nfs4_dir_inode_operations,
10452 .file_inode_ops = &nfs4_file_inode_operations,
10453 .file_ops = &nfs4_file_operations,
10454 .getroot = nfs4_proc_get_root,
10455 .submount = nfs4_submount,
10456 .try_get_tree = nfs4_try_get_tree,
10457 .getattr = nfs4_proc_getattr,
10458 .setattr = nfs4_proc_setattr,
10459 .lookup = nfs4_proc_lookup,
10460 .lookupp = nfs4_proc_lookupp,
10461 .access = nfs4_proc_access,
10462 .readlink = nfs4_proc_readlink,
10463 .create = nfs4_proc_create,
10464 .remove = nfs4_proc_remove,
10465 .unlink_setup = nfs4_proc_unlink_setup,
10466 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
10467 .unlink_done = nfs4_proc_unlink_done,
10468 .rename_setup = nfs4_proc_rename_setup,
10469 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
10470 .rename_done = nfs4_proc_rename_done,
10471 .link = nfs4_proc_link,
10472 .symlink = nfs4_proc_symlink,
10473 .mkdir = nfs4_proc_mkdir,
10474 .rmdir = nfs4_proc_rmdir,
10475 .readdir = nfs4_proc_readdir,
10476 .mknod = nfs4_proc_mknod,
10477 .statfs = nfs4_proc_statfs,
10478 .fsinfo = nfs4_proc_fsinfo,
10479 .pathconf = nfs4_proc_pathconf,
10480 .set_capabilities = nfs4_server_capabilities,
10481 .decode_dirent = nfs4_decode_dirent,
10482 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
10483 .read_setup = nfs4_proc_read_setup,
10484 .read_done = nfs4_read_done,
10485 .write_setup = nfs4_proc_write_setup,
10486 .write_done = nfs4_write_done,
10487 .commit_setup = nfs4_proc_commit_setup,
10488 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
10489 .commit_done = nfs4_commit_done,
10490 .lock = nfs4_proc_lock,
10491 .clear_acl_cache = nfs4_zap_acl_attr,
10492 .close_context = nfs4_close_context,
10493 .open_context = nfs4_atomic_open,
10494 .have_delegation = nfs4_have_delegation,
10495 .alloc_client = nfs4_alloc_client,
10496 .init_client = nfs4_init_client,
10497 .free_client = nfs4_free_client,
10498 .create_server = nfs4_create_server,
10499 .clone_server = nfs_clone_server,
10500 .enable_swap = nfs4_enable_swap,
10501 .disable_swap = nfs4_disable_swap,
10504 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
10505 .name = XATTR_NAME_NFSV4_ACL,
10506 .list = nfs4_xattr_list_nfs4_acl,
10507 .get = nfs4_xattr_get_nfs4_acl,
10508 .set = nfs4_xattr_set_nfs4_acl,
10511 #ifdef CONFIG_NFS_V4_2
10512 static const struct xattr_handler nfs4_xattr_nfs4_user_handler = {
10513 .prefix = XATTR_USER_PREFIX,
10514 .get = nfs4_xattr_get_nfs4_user,
10515 .set = nfs4_xattr_set_nfs4_user,
10519 const struct xattr_handler *nfs4_xattr_handlers[] = {
10520 &nfs4_xattr_nfs4_acl_handler,
10521 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
10522 &nfs4_xattr_nfs4_label_handler,
10524 #ifdef CONFIG_NFS_V4_2
10525 &nfs4_xattr_nfs4_user_handler,
10532 * c-basic-offset: 8