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 #define NFSDBG_FACILITY NFSDBG_PROC
76 #define NFS4_BITMASK_SZ 3
78 #define NFS4_POLL_RETRY_MIN (HZ/10)
79 #define NFS4_POLL_RETRY_MAX (15*HZ)
81 /* file attributes which can be mapped to nfs attributes */
82 #define NFS4_VALID_ATTRS (ATTR_MODE \
93 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
94 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
95 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
96 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label, struct inode *inode);
97 static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
98 struct nfs_fattr *fattr, struct iattr *sattr,
99 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
100 struct nfs4_label *olabel);
101 #ifdef CONFIG_NFS_V4_1
102 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
103 const struct cred *cred,
104 struct nfs4_slot *slot,
106 static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
107 const struct cred *);
108 static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
109 const struct cred *, bool);
111 static void nfs4_bitmask_adjust(__u32 *bitmask, struct inode *inode,
112 struct nfs_server *server,
113 struct nfs4_label *label);
115 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
116 static inline struct nfs4_label *
117 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
118 struct iattr *sattr, struct nfs4_label *label)
125 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
128 err = security_dentry_init_security(dentry, sattr->ia_mode,
129 &dentry->d_name, (void **)&label->label, &label->len);
136 nfs4_label_release_security(struct nfs4_label *label)
139 security_release_secctx(label->label, label->len);
141 static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
144 return server->attr_bitmask;
146 return server->attr_bitmask_nl;
149 static inline struct nfs4_label *
150 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
151 struct iattr *sattr, struct nfs4_label *l)
154 nfs4_label_release_security(struct nfs4_label *label)
157 nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
158 { return server->attr_bitmask; }
161 /* Prevent leaks of NFSv4 errors into userland */
162 static int nfs4_map_errors(int err)
167 case -NFS4ERR_RESOURCE:
168 case -NFS4ERR_LAYOUTTRYLATER:
169 case -NFS4ERR_RECALLCONFLICT:
171 case -NFS4ERR_WRONGSEC:
172 case -NFS4ERR_WRONG_CRED:
174 case -NFS4ERR_BADOWNER:
175 case -NFS4ERR_BADNAME:
177 case -NFS4ERR_SHARE_DENIED:
179 case -NFS4ERR_MINOR_VERS_MISMATCH:
180 return -EPROTONOSUPPORT;
181 case -NFS4ERR_FILE_OPEN:
183 case -NFS4ERR_NOT_SAME:
186 dprintk("%s could not handle NFSv4 error %d\n",
194 * This is our standard bitmap for GETATTR requests.
196 const u32 nfs4_fattr_bitmap[3] = {
198 | FATTR4_WORD0_CHANGE
201 | FATTR4_WORD0_FILEID,
203 | FATTR4_WORD1_NUMLINKS
205 | FATTR4_WORD1_OWNER_GROUP
206 | FATTR4_WORD1_RAWDEV
207 | FATTR4_WORD1_SPACE_USED
208 | FATTR4_WORD1_TIME_ACCESS
209 | FATTR4_WORD1_TIME_METADATA
210 | FATTR4_WORD1_TIME_MODIFY
211 | FATTR4_WORD1_MOUNTED_ON_FILEID,
212 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
213 FATTR4_WORD2_SECURITY_LABEL
217 static const u32 nfs4_pnfs_open_bitmap[3] = {
219 | FATTR4_WORD0_CHANGE
222 | FATTR4_WORD0_FILEID,
224 | FATTR4_WORD1_NUMLINKS
226 | FATTR4_WORD1_OWNER_GROUP
227 | FATTR4_WORD1_RAWDEV
228 | FATTR4_WORD1_SPACE_USED
229 | FATTR4_WORD1_TIME_ACCESS
230 | FATTR4_WORD1_TIME_METADATA
231 | FATTR4_WORD1_TIME_MODIFY,
232 FATTR4_WORD2_MDSTHRESHOLD
233 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
234 | FATTR4_WORD2_SECURITY_LABEL
238 static const u32 nfs4_open_noattr_bitmap[3] = {
240 | FATTR4_WORD0_FILEID,
243 const u32 nfs4_statfs_bitmap[3] = {
244 FATTR4_WORD0_FILES_AVAIL
245 | FATTR4_WORD0_FILES_FREE
246 | FATTR4_WORD0_FILES_TOTAL,
247 FATTR4_WORD1_SPACE_AVAIL
248 | FATTR4_WORD1_SPACE_FREE
249 | FATTR4_WORD1_SPACE_TOTAL
252 const u32 nfs4_pathconf_bitmap[3] = {
254 | FATTR4_WORD0_MAXNAME,
258 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
259 | FATTR4_WORD0_MAXREAD
260 | FATTR4_WORD0_MAXWRITE
261 | FATTR4_WORD0_LEASE_TIME,
262 FATTR4_WORD1_TIME_DELTA
263 | FATTR4_WORD1_FS_LAYOUT_TYPES,
264 FATTR4_WORD2_LAYOUT_BLKSIZE
265 | FATTR4_WORD2_CLONE_BLKSIZE
266 | FATTR4_WORD2_XATTR_SUPPORT
269 const u32 nfs4_fs_locations_bitmap[3] = {
273 | FATTR4_WORD0_FILEID
274 | FATTR4_WORD0_FS_LOCATIONS,
276 | FATTR4_WORD1_OWNER_GROUP
277 | FATTR4_WORD1_RAWDEV
278 | FATTR4_WORD1_SPACE_USED
279 | FATTR4_WORD1_TIME_ACCESS
280 | FATTR4_WORD1_TIME_METADATA
281 | FATTR4_WORD1_TIME_MODIFY
282 | FATTR4_WORD1_MOUNTED_ON_FILEID,
285 static void nfs4_bitmap_copy_adjust(__u32 *dst, const __u32 *src,
288 unsigned long cache_validity;
290 memcpy(dst, src, NFS4_BITMASK_SZ*sizeof(*dst));
291 if (!inode || !nfs4_have_delegation(inode, FMODE_READ))
294 cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
295 if (!(cache_validity & NFS_INO_REVAL_FORCED))
296 cache_validity &= ~(NFS_INO_INVALID_CHANGE
297 | NFS_INO_INVALID_SIZE);
299 if (!(cache_validity & NFS_INO_INVALID_SIZE))
300 dst[0] &= ~FATTR4_WORD0_SIZE;
302 if (!(cache_validity & NFS_INO_INVALID_CHANGE))
303 dst[0] &= ~FATTR4_WORD0_CHANGE;
306 static void nfs4_bitmap_copy_adjust_setattr(__u32 *dst,
307 const __u32 *src, struct inode *inode)
309 nfs4_bitmap_copy_adjust(dst, src, inode);
312 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
313 struct nfs4_readdir_arg *readdir)
315 unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
319 readdir->cookie = cookie;
320 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
325 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
330 * NFSv4 servers do not return entries for '.' and '..'
331 * Therefore, we fake these entries here. We let '.'
332 * have cookie 0 and '..' have cookie 1. Note that
333 * when talking to the server, we always send cookie 0
336 start = p = kmap_atomic(*readdir->pages);
339 *p++ = xdr_one; /* next */
340 *p++ = xdr_zero; /* cookie, first word */
341 *p++ = xdr_one; /* cookie, second word */
342 *p++ = xdr_one; /* entry len */
343 memcpy(p, ".\0\0\0", 4); /* entry */
345 *p++ = xdr_one; /* bitmap length */
346 *p++ = htonl(attrs); /* bitmap */
347 *p++ = htonl(12); /* attribute buffer length */
348 *p++ = htonl(NF4DIR);
349 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
352 *p++ = xdr_one; /* next */
353 *p++ = xdr_zero; /* cookie, first word */
354 *p++ = xdr_two; /* cookie, second word */
355 *p++ = xdr_two; /* entry len */
356 memcpy(p, "..\0\0", 4); /* entry */
358 *p++ = xdr_one; /* bitmap length */
359 *p++ = htonl(attrs); /* bitmap */
360 *p++ = htonl(12); /* attribute buffer length */
361 *p++ = htonl(NF4DIR);
362 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
364 readdir->pgbase = (char *)p - (char *)start;
365 readdir->count -= readdir->pgbase;
366 kunmap_atomic(start);
369 static void nfs4_test_and_free_stateid(struct nfs_server *server,
370 nfs4_stateid *stateid,
371 const struct cred *cred)
373 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
375 ops->test_and_free_expired(server, stateid, cred);
378 static void __nfs4_free_revoked_stateid(struct nfs_server *server,
379 nfs4_stateid *stateid,
380 const struct cred *cred)
382 stateid->type = NFS4_REVOKED_STATEID_TYPE;
383 nfs4_test_and_free_stateid(server, stateid, cred);
386 static void nfs4_free_revoked_stateid(struct nfs_server *server,
387 const nfs4_stateid *stateid,
388 const struct cred *cred)
392 nfs4_stateid_copy(&tmp, stateid);
393 __nfs4_free_revoked_stateid(server, &tmp, cred);
396 static long nfs4_update_delay(long *timeout)
400 return NFS4_POLL_RETRY_MAX;
402 *timeout = NFS4_POLL_RETRY_MIN;
403 if (*timeout > NFS4_POLL_RETRY_MAX)
404 *timeout = NFS4_POLL_RETRY_MAX;
410 static int nfs4_delay_killable(long *timeout)
414 freezable_schedule_timeout_killable_unsafe(
415 nfs4_update_delay(timeout));
416 if (!__fatal_signal_pending(current))
421 static int nfs4_delay_interruptible(long *timeout)
425 freezable_schedule_timeout_interruptible_unsafe(nfs4_update_delay(timeout));
426 if (!signal_pending(current))
428 return __fatal_signal_pending(current) ? -EINTR :-ERESTARTSYS;
431 static int nfs4_delay(long *timeout, bool interruptible)
434 return nfs4_delay_interruptible(timeout);
435 return nfs4_delay_killable(timeout);
438 static const nfs4_stateid *
439 nfs4_recoverable_stateid(const nfs4_stateid *stateid)
443 switch (stateid->type) {
444 case NFS4_OPEN_STATEID_TYPE:
445 case NFS4_LOCK_STATEID_TYPE:
446 case NFS4_DELEGATION_STATEID_TYPE:
454 /* This is the error handling routine for processes that are allowed
457 static int nfs4_do_handle_exception(struct nfs_server *server,
458 int errorcode, struct nfs4_exception *exception)
460 struct nfs_client *clp = server->nfs_client;
461 struct nfs4_state *state = exception->state;
462 const nfs4_stateid *stateid;
463 struct inode *inode = exception->inode;
466 exception->delay = 0;
467 exception->recovering = 0;
468 exception->retry = 0;
470 stateid = nfs4_recoverable_stateid(exception->stateid);
471 if (stateid == NULL && state != NULL)
472 stateid = nfs4_recoverable_stateid(&state->stateid);
477 case -NFS4ERR_BADHANDLE:
479 if (inode != NULL && S_ISREG(inode->i_mode))
480 pnfs_destroy_layout(NFS_I(inode));
482 case -NFS4ERR_DELEG_REVOKED:
483 case -NFS4ERR_ADMIN_REVOKED:
484 case -NFS4ERR_EXPIRED:
485 case -NFS4ERR_BAD_STATEID:
486 case -NFS4ERR_PARTNER_NO_AUTH:
487 if (inode != NULL && stateid != NULL) {
488 nfs_inode_find_state_and_recover(inode,
490 goto wait_on_recovery;
493 case -NFS4ERR_OPENMODE:
497 err = nfs_async_inode_return_delegation(inode,
500 goto wait_on_recovery;
501 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
502 exception->retry = 1;
508 ret = nfs4_schedule_stateid_recovery(server, state);
511 goto wait_on_recovery;
512 case -NFS4ERR_STALE_STATEID:
513 case -NFS4ERR_STALE_CLIENTID:
514 nfs4_schedule_lease_recovery(clp);
515 goto wait_on_recovery;
517 ret = nfs4_schedule_migration_recovery(server);
520 goto wait_on_recovery;
521 case -NFS4ERR_LEASE_MOVED:
522 nfs4_schedule_lease_moved_recovery(clp);
523 goto wait_on_recovery;
524 #if defined(CONFIG_NFS_V4_1)
525 case -NFS4ERR_BADSESSION:
526 case -NFS4ERR_BADSLOT:
527 case -NFS4ERR_BAD_HIGH_SLOT:
528 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
529 case -NFS4ERR_DEADSESSION:
530 case -NFS4ERR_SEQ_FALSE_RETRY:
531 case -NFS4ERR_SEQ_MISORDERED:
532 /* Handled in nfs41_sequence_process() */
533 goto wait_on_recovery;
534 #endif /* defined(CONFIG_NFS_V4_1) */
535 case -NFS4ERR_FILE_OPEN:
536 if (exception->timeout > HZ) {
537 /* We have retried a decent amount, time to
545 nfs_inc_server_stats(server, NFSIOS_DELAY);
548 case -NFS4ERR_LAYOUTTRYLATER:
549 case -NFS4ERR_RECALLCONFLICT:
550 exception->delay = 1;
553 case -NFS4ERR_RETRY_UNCACHED_REP:
554 case -NFS4ERR_OLD_STATEID:
555 exception->retry = 1;
557 case -NFS4ERR_BADOWNER:
558 /* The following works around a Linux server bug! */
559 case -NFS4ERR_BADNAME:
560 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
561 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
562 exception->retry = 1;
563 printk(KERN_WARNING "NFS: v4 server %s "
564 "does not accept raw "
566 "Reenabling the idmapper.\n",
567 server->nfs_client->cl_hostname);
570 /* We failed to handle the error */
571 return nfs4_map_errors(ret);
573 exception->recovering = 1;
577 /* This is the error handling routine for processes that are allowed
580 int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
582 struct nfs_client *clp = server->nfs_client;
585 ret = nfs4_do_handle_exception(server, errorcode, exception);
586 if (exception->delay) {
587 ret = nfs4_delay(&exception->timeout,
588 exception->interruptible);
591 if (exception->recovering) {
592 ret = nfs4_wait_clnt_recover(clp);
593 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
600 exception->retry = 1;
605 nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
606 int errorcode, struct nfs4_exception *exception)
608 struct nfs_client *clp = server->nfs_client;
611 ret = nfs4_do_handle_exception(server, errorcode, exception);
612 if (exception->delay) {
613 rpc_delay(task, nfs4_update_delay(&exception->timeout));
616 if (exception->recovering) {
617 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
618 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
619 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
622 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
627 exception->retry = 1;
629 * For NFS4ERR_MOVED, the client transport will need to
630 * be recomputed after migration recovery has completed.
632 if (errorcode == -NFS4ERR_MOVED)
633 rpc_task_release_transport(task);
639 nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
640 struct nfs4_state *state, long *timeout)
642 struct nfs4_exception exception = {
646 if (task->tk_status >= 0)
649 exception.timeout = *timeout;
650 task->tk_status = nfs4_async_handle_exception(task, server,
653 if (exception.delay && timeout)
654 *timeout = exception.timeout;
661 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
662 * or 'false' otherwise.
664 static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
666 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
667 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
670 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
672 spin_lock(&clp->cl_lock);
673 if (time_before(clp->cl_last_renewal,timestamp))
674 clp->cl_last_renewal = timestamp;
675 spin_unlock(&clp->cl_lock);
678 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
680 struct nfs_client *clp = server->nfs_client;
682 if (!nfs4_has_session(clp))
683 do_renew_lease(clp, timestamp);
686 struct nfs4_call_sync_data {
687 const struct nfs_server *seq_server;
688 struct nfs4_sequence_args *seq_args;
689 struct nfs4_sequence_res *seq_res;
692 void nfs4_init_sequence(struct nfs4_sequence_args *args,
693 struct nfs4_sequence_res *res, int cache_reply,
696 args->sa_slot = NULL;
697 args->sa_cache_this = cache_reply;
698 args->sa_privileged = privileged;
703 static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
705 struct nfs4_slot *slot = res->sr_slot;
706 struct nfs4_slot_table *tbl;
709 spin_lock(&tbl->slot_tbl_lock);
710 if (!nfs41_wake_and_assign_slot(tbl, slot))
711 nfs4_free_slot(tbl, slot);
712 spin_unlock(&tbl->slot_tbl_lock);
717 static int nfs40_sequence_done(struct rpc_task *task,
718 struct nfs4_sequence_res *res)
720 if (res->sr_slot != NULL)
721 nfs40_sequence_free_slot(res);
725 #if defined(CONFIG_NFS_V4_1)
727 static void nfs41_release_slot(struct nfs4_slot *slot)
729 struct nfs4_session *session;
730 struct nfs4_slot_table *tbl;
731 bool send_new_highest_used_slotid = false;
736 session = tbl->session;
738 /* Bump the slot sequence number */
743 spin_lock(&tbl->slot_tbl_lock);
744 /* Be nice to the server: try to ensure that the last transmitted
745 * value for highest_user_slotid <= target_highest_slotid
747 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
748 send_new_highest_used_slotid = true;
750 if (nfs41_wake_and_assign_slot(tbl, slot)) {
751 send_new_highest_used_slotid = false;
754 nfs4_free_slot(tbl, slot);
756 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
757 send_new_highest_used_slotid = false;
759 spin_unlock(&tbl->slot_tbl_lock);
760 if (send_new_highest_used_slotid)
761 nfs41_notify_server(session->clp);
762 if (waitqueue_active(&tbl->slot_waitq))
763 wake_up_all(&tbl->slot_waitq);
766 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
768 nfs41_release_slot(res->sr_slot);
772 static void nfs4_slot_sequence_record_sent(struct nfs4_slot *slot,
775 if ((s32)(seqnr - slot->seq_nr_highest_sent) > 0)
776 slot->seq_nr_highest_sent = seqnr;
778 static void nfs4_slot_sequence_acked(struct nfs4_slot *slot,
781 slot->seq_nr_highest_sent = seqnr;
782 slot->seq_nr_last_acked = seqnr;
785 static void nfs4_probe_sequence(struct nfs_client *client, const struct cred *cred,
786 struct nfs4_slot *slot)
788 struct rpc_task *task = _nfs41_proc_sequence(client, cred, slot, true);
790 rpc_put_task_async(task);
793 static int nfs41_sequence_process(struct rpc_task *task,
794 struct nfs4_sequence_res *res)
796 struct nfs4_session *session;
797 struct nfs4_slot *slot = res->sr_slot;
798 struct nfs_client *clp;
804 /* don't increment the sequence number if the task wasn't sent */
805 if (!RPC_WAS_SENT(task) || slot->seq_done)
808 session = slot->table->session;
811 trace_nfs4_sequence_done(session, res);
813 status = res->sr_status;
814 if (task->tk_status == -NFS4ERR_DEADSESSION)
815 status = -NFS4ERR_DEADSESSION;
817 /* Check the SEQUENCE operation status */
820 /* Mark this sequence number as having been acked */
821 nfs4_slot_sequence_acked(slot, slot->seq_nr);
822 /* Update the slot's sequence and clientid lease timer */
824 do_renew_lease(clp, res->sr_timestamp);
825 /* Check sequence flags */
826 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
828 nfs41_update_target_slotid(slot->table, slot, res);
832 * sr_status remains 1 if an RPC level error occurred.
833 * The server may or may not have processed the sequence
836 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
840 /* The server detected a resend of the RPC call and
841 * returned NFS4ERR_DELAY as per Section 2.10.6.2
844 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
848 nfs4_slot_sequence_acked(slot, slot->seq_nr);
850 case -NFS4ERR_RETRY_UNCACHED_REP:
851 case -NFS4ERR_SEQ_FALSE_RETRY:
853 * The server thinks we tried to replay a request.
854 * Retry the call after bumping the sequence ID.
856 nfs4_slot_sequence_acked(slot, slot->seq_nr);
858 case -NFS4ERR_BADSLOT:
860 * The slot id we used was probably retired. Try again
861 * using a different slot id.
863 if (slot->slot_nr < slot->table->target_highest_slotid)
864 goto session_recover;
866 case -NFS4ERR_SEQ_MISORDERED:
867 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
869 * Were one or more calls using this slot interrupted?
870 * If the server never received the request, then our
871 * transmitted slot sequence number may be too high. However,
872 * if the server did receive the request then it might
873 * accidentally give us a reply with a mismatched operation.
874 * We can sort this out by sending a lone sequence operation
875 * to the server on the same slot.
877 if ((s32)(slot->seq_nr - slot->seq_nr_last_acked) > 1) {
879 if (task->tk_msg.rpc_proc != &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE]) {
880 nfs4_probe_sequence(clp, task->tk_msg.rpc_cred, slot);
887 * A retry might be sent while the original request is
888 * still in progress on the replier. The replier SHOULD
889 * deal with the issue by returning NFS4ERR_DELAY as the
890 * reply to SEQUENCE or CB_SEQUENCE operation, but
891 * implementations MAY return NFS4ERR_SEQ_MISORDERED.
893 * Restart the search after a delay.
895 slot->seq_nr = slot->seq_nr_highest_sent;
897 case -NFS4ERR_BADSESSION:
898 case -NFS4ERR_DEADSESSION:
899 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
900 goto session_recover;
902 /* Just update the slot sequence no. */
906 /* The session may be reset by one of the error handlers. */
907 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
911 nfs4_schedule_session_recovery(session, status);
912 dprintk("%s ERROR: %d Reset session\n", __func__, status);
913 nfs41_sequence_free_slot(res);
918 if (rpc_restart_call_prepare(task)) {
919 nfs41_sequence_free_slot(res);
925 if (!rpc_restart_call(task))
927 rpc_delay(task, NFS4_POLL_RETRY_MAX);
931 int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
933 if (!nfs41_sequence_process(task, res))
935 if (res->sr_slot != NULL)
936 nfs41_sequence_free_slot(res);
940 EXPORT_SYMBOL_GPL(nfs41_sequence_done);
942 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
944 if (res->sr_slot == NULL)
946 if (res->sr_slot->table->session != NULL)
947 return nfs41_sequence_process(task, res);
948 return nfs40_sequence_done(task, res);
951 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
953 if (res->sr_slot != NULL) {
954 if (res->sr_slot->table->session != NULL)
955 nfs41_sequence_free_slot(res);
957 nfs40_sequence_free_slot(res);
961 int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
963 if (res->sr_slot == NULL)
965 if (!res->sr_slot->table->session)
966 return nfs40_sequence_done(task, res);
967 return nfs41_sequence_done(task, res);
969 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
971 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
973 struct nfs4_call_sync_data *data = calldata;
975 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
977 nfs4_setup_sequence(data->seq_server->nfs_client,
978 data->seq_args, data->seq_res, task);
981 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
983 struct nfs4_call_sync_data *data = calldata;
985 nfs41_sequence_done(task, data->seq_res);
988 static const struct rpc_call_ops nfs41_call_sync_ops = {
989 .rpc_call_prepare = nfs41_call_sync_prepare,
990 .rpc_call_done = nfs41_call_sync_done,
993 #else /* !CONFIG_NFS_V4_1 */
995 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
997 return nfs40_sequence_done(task, res);
1000 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
1002 if (res->sr_slot != NULL)
1003 nfs40_sequence_free_slot(res);
1006 int nfs4_sequence_done(struct rpc_task *task,
1007 struct nfs4_sequence_res *res)
1009 return nfs40_sequence_done(task, res);
1011 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
1013 #endif /* !CONFIG_NFS_V4_1 */
1015 static void nfs41_sequence_res_init(struct nfs4_sequence_res *res)
1017 res->sr_timestamp = jiffies;
1018 res->sr_status_flags = 0;
1023 void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
1024 struct nfs4_sequence_res *res,
1025 struct nfs4_slot *slot)
1029 slot->privileged = args->sa_privileged ? 1 : 0;
1030 args->sa_slot = slot;
1032 res->sr_slot = slot;
1035 int nfs4_setup_sequence(struct nfs_client *client,
1036 struct nfs4_sequence_args *args,
1037 struct nfs4_sequence_res *res,
1038 struct rpc_task *task)
1040 struct nfs4_session *session = nfs4_get_session(client);
1041 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
1042 struct nfs4_slot *slot;
1044 /* slot already allocated? */
1045 if (res->sr_slot != NULL)
1049 tbl = &session->fc_slot_table;
1051 spin_lock(&tbl->slot_tbl_lock);
1052 /* The state manager will wait until the slot table is empty */
1053 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
1056 slot = nfs4_alloc_slot(tbl);
1058 if (slot == ERR_PTR(-ENOMEM))
1059 goto out_sleep_timeout;
1062 spin_unlock(&tbl->slot_tbl_lock);
1064 nfs4_sequence_attach_slot(args, res, slot);
1066 trace_nfs4_setup_sequence(session, args);
1068 nfs41_sequence_res_init(res);
1069 rpc_call_start(task);
1072 /* Try again in 1/4 second */
1073 if (args->sa_privileged)
1074 rpc_sleep_on_priority_timeout(&tbl->slot_tbl_waitq, task,
1075 jiffies + (HZ >> 2), RPC_PRIORITY_PRIVILEGED);
1077 rpc_sleep_on_timeout(&tbl->slot_tbl_waitq, task,
1078 NULL, jiffies + (HZ >> 2));
1079 spin_unlock(&tbl->slot_tbl_lock);
1082 if (args->sa_privileged)
1083 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
1084 RPC_PRIORITY_PRIVILEGED);
1086 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
1087 spin_unlock(&tbl->slot_tbl_lock);
1090 EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
1092 static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1094 struct nfs4_call_sync_data *data = calldata;
1095 nfs4_setup_sequence(data->seq_server->nfs_client,
1096 data->seq_args, data->seq_res, task);
1099 static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1101 struct nfs4_call_sync_data *data = calldata;
1102 nfs4_sequence_done(task, data->seq_res);
1105 static const struct rpc_call_ops nfs40_call_sync_ops = {
1106 .rpc_call_prepare = nfs40_call_sync_prepare,
1107 .rpc_call_done = nfs40_call_sync_done,
1110 static int nfs4_call_sync_custom(struct rpc_task_setup *task_setup)
1113 struct rpc_task *task;
1115 task = rpc_run_task(task_setup);
1117 return PTR_ERR(task);
1119 ret = task->tk_status;
1124 static int nfs4_do_call_sync(struct rpc_clnt *clnt,
1125 struct nfs_server *server,
1126 struct rpc_message *msg,
1127 struct nfs4_sequence_args *args,
1128 struct nfs4_sequence_res *res,
1129 unsigned short task_flags)
1131 struct nfs_client *clp = server->nfs_client;
1132 struct nfs4_call_sync_data data = {
1133 .seq_server = server,
1137 struct rpc_task_setup task_setup = {
1140 .callback_ops = clp->cl_mvops->call_sync_ops,
1141 .callback_data = &data,
1142 .flags = task_flags,
1145 return nfs4_call_sync_custom(&task_setup);
1148 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
1149 struct nfs_server *server,
1150 struct rpc_message *msg,
1151 struct nfs4_sequence_args *args,
1152 struct nfs4_sequence_res *res)
1154 return nfs4_do_call_sync(clnt, server, msg, args, res, 0);
1158 int nfs4_call_sync(struct rpc_clnt *clnt,
1159 struct nfs_server *server,
1160 struct rpc_message *msg,
1161 struct nfs4_sequence_args *args,
1162 struct nfs4_sequence_res *res,
1165 nfs4_init_sequence(args, res, cache_reply, 0);
1166 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
1170 nfs4_inc_nlink_locked(struct inode *inode)
1172 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1177 nfs4_dec_nlink_locked(struct inode *inode)
1179 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1184 nfs4_update_changeattr_locked(struct inode *inode,
1185 struct nfs4_change_info *cinfo,
1186 unsigned long timestamp, unsigned long cache_validity)
1188 struct nfs_inode *nfsi = NFS_I(inode);
1190 nfsi->cache_validity |= NFS_INO_INVALID_CTIME
1191 | NFS_INO_INVALID_MTIME
1194 if (cinfo->atomic && cinfo->before == inode_peek_iversion_raw(inode)) {
1195 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1196 nfsi->attrtimeo_timestamp = jiffies;
1198 if (S_ISDIR(inode->i_mode)) {
1199 nfsi->cache_validity |= NFS_INO_INVALID_DATA;
1200 nfs_force_lookup_revalidate(inode);
1202 if (!NFS_PROTO(inode)->have_delegation(inode,
1204 nfsi->cache_validity |= NFS_INO_REVAL_PAGECACHE;
1207 if (cinfo->before != inode_peek_iversion_raw(inode))
1208 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1209 NFS_INO_INVALID_ACL |
1210 NFS_INO_INVALID_XATTR;
1212 inode_set_iversion_raw(inode, cinfo->after);
1213 nfsi->read_cache_jiffies = timestamp;
1214 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
1215 nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE;
1217 if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
1218 nfs_fscache_invalidate(inode);
1222 nfs4_update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1223 unsigned long timestamp, unsigned long cache_validity)
1225 spin_lock(&dir->i_lock);
1226 nfs4_update_changeattr_locked(dir, cinfo, timestamp, cache_validity);
1227 spin_unlock(&dir->i_lock);
1230 struct nfs4_open_createattrs {
1231 struct nfs4_label *label;
1232 struct iattr *sattr;
1233 const __u32 verf[2];
1236 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1237 int err, struct nfs4_exception *exception)
1241 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1243 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1244 exception->retry = 1;
1248 static fmode_t _nfs4_ctx_to_accessmode(const struct nfs_open_context *ctx)
1250 return ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
1253 static fmode_t _nfs4_ctx_to_openmode(const struct nfs_open_context *ctx)
1255 fmode_t ret = ctx->mode & (FMODE_READ|FMODE_WRITE);
1257 return (ctx->mode & FMODE_EXEC) ? FMODE_READ | ret : ret;
1261 nfs4_map_atomic_open_share(struct nfs_server *server,
1262 fmode_t fmode, int openflags)
1266 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1268 res = NFS4_SHARE_ACCESS_READ;
1271 res = NFS4_SHARE_ACCESS_WRITE;
1273 case FMODE_READ|FMODE_WRITE:
1274 res = NFS4_SHARE_ACCESS_BOTH;
1276 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1278 /* Want no delegation if we're using O_DIRECT */
1279 if (openflags & O_DIRECT)
1280 res |= NFS4_SHARE_WANT_NO_DELEG;
1285 static enum open_claim_type4
1286 nfs4_map_atomic_open_claim(struct nfs_server *server,
1287 enum open_claim_type4 claim)
1289 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1294 case NFS4_OPEN_CLAIM_FH:
1295 return NFS4_OPEN_CLAIM_NULL;
1296 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1297 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1298 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1299 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1303 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
1305 p->o_res.f_attr = &p->f_attr;
1306 p->o_res.f_label = p->f_label;
1307 p->o_res.seqid = p->o_arg.seqid;
1308 p->c_res.seqid = p->c_arg.seqid;
1309 p->o_res.server = p->o_arg.server;
1310 p->o_res.access_request = p->o_arg.access;
1311 nfs_fattr_init(&p->f_attr);
1312 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
1315 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
1316 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
1317 const struct nfs4_open_createattrs *c,
1318 enum open_claim_type4 claim,
1321 struct dentry *parent = dget_parent(dentry);
1322 struct inode *dir = d_inode(parent);
1323 struct nfs_server *server = NFS_SERVER(dir);
1324 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
1325 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
1326 struct nfs4_opendata *p;
1328 p = kzalloc(sizeof(*p), gfp_mask);
1332 p->f_label = nfs4_label_alloc(server, gfp_mask);
1333 if (IS_ERR(p->f_label))
1336 p->a_label = nfs4_label_alloc(server, gfp_mask);
1337 if (IS_ERR(p->a_label))
1340 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1341 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
1342 if (IS_ERR(p->o_arg.seqid))
1343 goto err_free_label;
1344 nfs_sb_active(dentry->d_sb);
1345 p->dentry = dget(dentry);
1348 atomic_inc(&sp->so_count);
1349 p->o_arg.open_flags = flags;
1350 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
1351 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1352 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1354 if (flags & O_CREAT) {
1355 p->o_arg.umask = current_umask();
1356 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1357 if (c->sattr != NULL && c->sattr->ia_valid != 0) {
1358 p->o_arg.u.attrs = &p->attrs;
1359 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
1361 memcpy(p->o_arg.u.verifier.data, c->verf,
1362 sizeof(p->o_arg.u.verifier.data));
1365 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1366 * will return permission denied for all bits until close */
1367 if (!(flags & O_EXCL)) {
1368 /* ask server to check for all possible rights as results
1370 switch (p->o_arg.claim) {
1373 case NFS4_OPEN_CLAIM_NULL:
1374 case NFS4_OPEN_CLAIM_FH:
1375 p->o_arg.access = NFS4_ACCESS_READ |
1376 NFS4_ACCESS_MODIFY |
1377 NFS4_ACCESS_EXTEND |
1378 NFS4_ACCESS_EXECUTE;
1379 #ifdef CONFIG_NFS_V4_2
1380 if (server->caps & NFS_CAP_XATTR)
1381 p->o_arg.access |= NFS4_ACCESS_XAREAD |
1382 NFS4_ACCESS_XAWRITE |
1387 p->o_arg.clientid = server->nfs_client->cl_clientid;
1388 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1389 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1390 p->o_arg.name = &dentry->d_name;
1391 p->o_arg.server = server;
1392 p->o_arg.bitmask = nfs4_bitmask(server, label);
1393 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1394 switch (p->o_arg.claim) {
1395 case NFS4_OPEN_CLAIM_NULL:
1396 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1397 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1398 p->o_arg.fh = NFS_FH(dir);
1400 case NFS4_OPEN_CLAIM_PREVIOUS:
1401 case NFS4_OPEN_CLAIM_FH:
1402 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1403 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1404 p->o_arg.fh = NFS_FH(d_inode(dentry));
1406 p->c_arg.fh = &p->o_res.fh;
1407 p->c_arg.stateid = &p->o_res.stateid;
1408 p->c_arg.seqid = p->o_arg.seqid;
1409 nfs4_init_opendata_res(p);
1410 kref_init(&p->kref);
1414 nfs4_label_free(p->a_label);
1416 nfs4_label_free(p->f_label);
1424 static void nfs4_opendata_free(struct kref *kref)
1426 struct nfs4_opendata *p = container_of(kref,
1427 struct nfs4_opendata, kref);
1428 struct super_block *sb = p->dentry->d_sb;
1430 nfs4_lgopen_release(p->lgp);
1431 nfs_free_seqid(p->o_arg.seqid);
1432 nfs4_sequence_free_slot(&p->o_res.seq_res);
1433 if (p->state != NULL)
1434 nfs4_put_open_state(p->state);
1435 nfs4_put_state_owner(p->owner);
1437 nfs4_label_free(p->a_label);
1438 nfs4_label_free(p->f_label);
1442 nfs_sb_deactive(sb);
1443 nfs_fattr_free_names(&p->f_attr);
1444 kfree(p->f_attr.mdsthreshold);
1448 static void nfs4_opendata_put(struct nfs4_opendata *p)
1451 kref_put(&p->kref, nfs4_opendata_free);
1454 static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1457 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1458 case FMODE_READ|FMODE_WRITE:
1459 return state->n_rdwr != 0;
1461 return state->n_wronly != 0;
1463 return state->n_rdonly != 0;
1469 static int can_open_cached(struct nfs4_state *state, fmode_t mode,
1470 int open_mode, enum open_claim_type4 claim)
1474 if (open_mode & (O_EXCL|O_TRUNC))
1477 case NFS4_OPEN_CLAIM_NULL:
1478 case NFS4_OPEN_CLAIM_FH:
1483 switch (mode & (FMODE_READ|FMODE_WRITE)) {
1485 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1486 && state->n_rdonly != 0;
1489 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1490 && state->n_wronly != 0;
1492 case FMODE_READ|FMODE_WRITE:
1493 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1494 && state->n_rdwr != 0;
1500 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1501 enum open_claim_type4 claim)
1503 if (delegation == NULL)
1505 if ((delegation->type & fmode) != fmode)
1508 case NFS4_OPEN_CLAIM_NULL:
1509 case NFS4_OPEN_CLAIM_FH:
1511 case NFS4_OPEN_CLAIM_PREVIOUS:
1512 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1518 nfs_mark_delegation_referenced(delegation);
1522 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1531 case FMODE_READ|FMODE_WRITE:
1534 nfs4_state_set_mode_locked(state, state->state | fmode);
1537 #ifdef CONFIG_NFS_V4_1
1538 static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1540 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1542 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1544 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1548 #endif /* CONFIG_NFS_V4_1 */
1550 static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1552 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1553 wake_up_all(&state->waitq);
1556 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
1558 struct nfs_client *clp = state->owner->so_server->nfs_client;
1559 bool need_recover = false;
1561 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1562 need_recover = true;
1563 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1564 need_recover = true;
1565 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1566 need_recover = true;
1568 nfs4_state_mark_reclaim_nograce(clp, state);
1572 * Check for whether or not the caller may update the open stateid
1573 * to the value passed in by stateid.
1575 * Note: This function relies heavily on the server implementing
1576 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1578 * i.e. The stateid seqids have to be initialised to 1, and
1579 * are then incremented on every state transition.
1581 static bool nfs_stateid_is_sequential(struct nfs4_state *state,
1582 const nfs4_stateid *stateid)
1584 if (test_bit(NFS_OPEN_STATE, &state->flags)) {
1585 /* The common case - we're updating to a new sequence number */
1586 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1587 nfs4_stateid_is_next(&state->open_stateid, stateid)) {
1591 /* This is the first OPEN in this generation */
1592 if (stateid->seqid == cpu_to_be32(1))
1598 static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1600 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1602 if (state->n_wronly)
1603 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1604 if (state->n_rdonly)
1605 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1607 set_bit(NFS_O_RDWR_STATE, &state->flags);
1608 set_bit(NFS_OPEN_STATE, &state->flags);
1611 static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1612 nfs4_stateid *stateid, fmode_t fmode)
1614 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1615 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1617 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1620 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1623 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1624 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1625 clear_bit(NFS_OPEN_STATE, &state->flags);
1627 if (stateid == NULL)
1629 /* Handle OPEN+OPEN_DOWNGRADE races */
1630 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1631 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1632 nfs_resync_open_stateid_locked(state);
1635 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1636 nfs4_stateid_copy(&state->stateid, stateid);
1637 nfs4_stateid_copy(&state->open_stateid, stateid);
1638 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
1640 nfs_state_log_update_open_stateid(state);
1643 static void nfs_clear_open_stateid(struct nfs4_state *state,
1644 nfs4_stateid *arg_stateid,
1645 nfs4_stateid *stateid, fmode_t fmode)
1647 write_seqlock(&state->seqlock);
1648 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1649 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1650 nfs_clear_open_stateid_locked(state, stateid, fmode);
1651 write_sequnlock(&state->seqlock);
1652 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1653 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1656 static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1657 const nfs4_stateid *stateid, nfs4_stateid *freeme)
1658 __must_hold(&state->owner->so_lock)
1659 __must_hold(&state->seqlock)
1667 if (nfs_stateid_is_sequential(state, stateid))
1672 /* Rely on seqids for serialisation with NFSv4.0 */
1673 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1676 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1677 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1679 * Ensure we process the state changes in the same order
1680 * in which the server processed them by delaying the
1681 * update of the stateid until we are in sequence.
1683 write_sequnlock(&state->seqlock);
1684 spin_unlock(&state->owner->so_lock);
1686 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
1688 if (!signal_pending(current)) {
1689 if (schedule_timeout(5*HZ) == 0)
1695 finish_wait(&state->waitq, &wait);
1697 spin_lock(&state->owner->so_lock);
1698 write_seqlock(&state->seqlock);
1701 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1702 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1703 nfs4_stateid_copy(freeme, &state->open_stateid);
1704 nfs_test_and_clear_all_open_stateid(state);
1707 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1708 nfs4_stateid_copy(&state->stateid, stateid);
1709 nfs4_stateid_copy(&state->open_stateid, stateid);
1710 trace_nfs4_open_stateid_update(state->inode, stateid, status);
1711 nfs_state_log_update_open_stateid(state);
1714 static void nfs_state_set_open_stateid(struct nfs4_state *state,
1715 const nfs4_stateid *open_stateid,
1717 nfs4_stateid *freeme)
1720 * Protect the call to nfs4_state_set_mode_locked and
1721 * serialise the stateid update
1723 write_seqlock(&state->seqlock);
1724 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1727 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1730 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1732 case FMODE_READ|FMODE_WRITE:
1733 set_bit(NFS_O_RDWR_STATE, &state->flags);
1735 set_bit(NFS_OPEN_STATE, &state->flags);
1736 write_sequnlock(&state->seqlock);
1739 static void nfs_state_clear_open_state_flags(struct nfs4_state *state)
1741 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1742 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1743 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1744 clear_bit(NFS_OPEN_STATE, &state->flags);
1747 static void nfs_state_set_delegation(struct nfs4_state *state,
1748 const nfs4_stateid *deleg_stateid,
1752 * Protect the call to nfs4_state_set_mode_locked and
1753 * serialise the stateid update
1755 write_seqlock(&state->seqlock);
1756 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1757 set_bit(NFS_DELEGATED_STATE, &state->flags);
1758 write_sequnlock(&state->seqlock);
1761 static void nfs_state_clear_delegation(struct nfs4_state *state)
1763 write_seqlock(&state->seqlock);
1764 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1765 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1766 write_sequnlock(&state->seqlock);
1769 int update_open_stateid(struct nfs4_state *state,
1770 const nfs4_stateid *open_stateid,
1771 const nfs4_stateid *delegation,
1774 struct nfs_server *server = NFS_SERVER(state->inode);
1775 struct nfs_client *clp = server->nfs_client;
1776 struct nfs_inode *nfsi = NFS_I(state->inode);
1777 struct nfs_delegation *deleg_cur;
1778 nfs4_stateid freeme = { };
1781 fmode &= (FMODE_READ|FMODE_WRITE);
1784 spin_lock(&state->owner->so_lock);
1785 if (open_stateid != NULL) {
1786 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1790 deleg_cur = nfs4_get_valid_delegation(state->inode);
1791 if (deleg_cur == NULL)
1794 spin_lock(&deleg_cur->lock);
1795 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1796 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1797 (deleg_cur->type & fmode) != fmode)
1798 goto no_delegation_unlock;
1800 if (delegation == NULL)
1801 delegation = &deleg_cur->stateid;
1802 else if (!nfs4_stateid_match_other(&deleg_cur->stateid, delegation))
1803 goto no_delegation_unlock;
1805 nfs_mark_delegation_referenced(deleg_cur);
1806 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
1808 no_delegation_unlock:
1809 spin_unlock(&deleg_cur->lock);
1812 update_open_stateflags(state, fmode);
1813 spin_unlock(&state->owner->so_lock);
1816 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1817 nfs4_schedule_state_manager(clp);
1818 if (freeme.type != 0)
1819 nfs4_test_and_free_stateid(server, &freeme,
1820 state->owner->so_cred);
1825 static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1826 const nfs4_stateid *stateid)
1828 struct nfs4_state *state = lsp->ls_state;
1831 spin_lock(&state->state_lock);
1832 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1834 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1836 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1839 spin_unlock(&state->state_lock);
1843 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1845 struct nfs_delegation *delegation;
1847 fmode &= FMODE_READ|FMODE_WRITE;
1849 delegation = nfs4_get_valid_delegation(inode);
1850 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1855 nfs4_inode_return_delegation(inode);
1858 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1860 struct nfs4_state *state = opendata->state;
1861 struct nfs_delegation *delegation;
1862 int open_mode = opendata->o_arg.open_flags;
1863 fmode_t fmode = opendata->o_arg.fmode;
1864 enum open_claim_type4 claim = opendata->o_arg.claim;
1865 nfs4_stateid stateid;
1869 spin_lock(&state->owner->so_lock);
1870 if (can_open_cached(state, fmode, open_mode, claim)) {
1871 update_open_stateflags(state, fmode);
1872 spin_unlock(&state->owner->so_lock);
1873 goto out_return_state;
1875 spin_unlock(&state->owner->so_lock);
1877 delegation = nfs4_get_valid_delegation(state->inode);
1878 if (!can_open_delegated(delegation, fmode, claim)) {
1882 /* Save the delegation */
1883 nfs4_stateid_copy(&stateid, &delegation->stateid);
1885 nfs_release_seqid(opendata->o_arg.seqid);
1886 if (!opendata->is_recover) {
1887 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1893 /* Try to update the stateid using the delegation */
1894 if (update_open_stateid(state, NULL, &stateid, fmode))
1895 goto out_return_state;
1898 return ERR_PTR(ret);
1900 refcount_inc(&state->count);
1905 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1907 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1908 struct nfs_delegation *delegation;
1909 int delegation_flags = 0;
1912 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1914 delegation_flags = delegation->flags;
1916 switch (data->o_arg.claim) {
1919 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1920 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1921 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1922 "returning a delegation for "
1923 "OPEN(CLAIM_DELEGATE_CUR)\n",
1927 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1928 nfs_inode_set_delegation(state->inode,
1929 data->owner->so_cred,
1930 data->o_res.delegation_type,
1931 &data->o_res.delegation,
1932 data->o_res.pagemod_limit);
1934 nfs_inode_reclaim_delegation(state->inode,
1935 data->owner->so_cred,
1936 data->o_res.delegation_type,
1937 &data->o_res.delegation,
1938 data->o_res.pagemod_limit);
1940 if (data->o_res.do_recall)
1941 nfs_async_inode_return_delegation(state->inode,
1942 &data->o_res.delegation);
1946 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1947 * and update the nfs4_state.
1949 static struct nfs4_state *
1950 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1952 struct inode *inode = data->state->inode;
1953 struct nfs4_state *state = data->state;
1956 if (!data->rpc_done) {
1957 if (data->rpc_status)
1958 return ERR_PTR(data->rpc_status);
1959 /* cached opens have already been processed */
1963 ret = nfs_refresh_inode(inode, &data->f_attr);
1965 return ERR_PTR(ret);
1967 if (data->o_res.delegation_type != 0)
1968 nfs4_opendata_check_deleg(data, state);
1970 if (!update_open_stateid(state, &data->o_res.stateid,
1971 NULL, data->o_arg.fmode))
1972 return ERR_PTR(-EAGAIN);
1973 refcount_inc(&state->count);
1978 static struct inode *
1979 nfs4_opendata_get_inode(struct nfs4_opendata *data)
1981 struct inode *inode;
1983 switch (data->o_arg.claim) {
1984 case NFS4_OPEN_CLAIM_NULL:
1985 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1986 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1987 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1988 return ERR_PTR(-EAGAIN);
1989 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1990 &data->f_attr, data->f_label);
1993 inode = d_inode(data->dentry);
1995 nfs_refresh_inode(inode, &data->f_attr);
2000 static struct nfs4_state *
2001 nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
2003 struct nfs4_state *state;
2004 struct inode *inode;
2006 inode = nfs4_opendata_get_inode(data);
2008 return ERR_CAST(inode);
2009 if (data->state != NULL && data->state->inode == inode) {
2010 state = data->state;
2011 refcount_inc(&state->count);
2013 state = nfs4_get_open_state(inode, data->owner);
2016 state = ERR_PTR(-ENOMEM);
2020 static struct nfs4_state *
2021 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2023 struct nfs4_state *state;
2025 if (!data->rpc_done) {
2026 state = nfs4_try_open_cached(data);
2027 trace_nfs4_cached_open(data->state);
2031 state = nfs4_opendata_find_nfs4_state(data);
2035 if (data->o_res.delegation_type != 0)
2036 nfs4_opendata_check_deleg(data, state);
2037 if (!update_open_stateid(state, &data->o_res.stateid,
2038 NULL, data->o_arg.fmode)) {
2039 nfs4_put_open_state(state);
2040 state = ERR_PTR(-EAGAIN);
2043 nfs_release_seqid(data->o_arg.seqid);
2047 static struct nfs4_state *
2048 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2050 struct nfs4_state *ret;
2052 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
2053 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
2055 ret = _nfs4_opendata_to_nfs4_state(data);
2056 nfs4_sequence_free_slot(&data->o_res.seq_res);
2060 static struct nfs_open_context *
2061 nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode)
2063 struct nfs_inode *nfsi = NFS_I(state->inode);
2064 struct nfs_open_context *ctx;
2067 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
2068 if (ctx->state != state)
2070 if ((ctx->mode & mode) != mode)
2072 if (!get_nfs_open_context(ctx))
2078 return ERR_PTR(-ENOENT);
2081 static struct nfs_open_context *
2082 nfs4_state_find_open_context(struct nfs4_state *state)
2084 struct nfs_open_context *ctx;
2086 ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE);
2089 ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE);
2092 return nfs4_state_find_open_context_mode(state, FMODE_READ);
2095 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
2096 struct nfs4_state *state, enum open_claim_type4 claim)
2098 struct nfs4_opendata *opendata;
2100 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
2101 NULL, claim, GFP_NOFS);
2102 if (opendata == NULL)
2103 return ERR_PTR(-ENOMEM);
2104 opendata->state = state;
2105 refcount_inc(&state->count);
2109 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
2112 struct nfs4_state *newstate;
2115 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
2117 opendata->o_arg.open_flags = 0;
2118 opendata->o_arg.fmode = fmode;
2119 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
2120 NFS_SB(opendata->dentry->d_sb),
2122 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
2123 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
2124 nfs4_init_opendata_res(opendata);
2125 ret = _nfs4_recover_proc_open(opendata);
2128 newstate = nfs4_opendata_to_nfs4_state(opendata);
2129 if (IS_ERR(newstate))
2130 return PTR_ERR(newstate);
2131 if (newstate != opendata->state)
2133 nfs4_close_state(newstate, fmode);
2137 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
2141 /* memory barrier prior to reading state->n_* */
2143 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2146 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2149 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2153 * We may have performed cached opens for all three recoveries.
2154 * Check if we need to update the current stateid.
2156 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
2157 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
2158 write_seqlock(&state->seqlock);
2159 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2160 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2161 write_sequnlock(&state->seqlock);
2168 * reclaim state on the server after a reboot.
2170 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2172 struct nfs_delegation *delegation;
2173 struct nfs4_opendata *opendata;
2174 fmode_t delegation_type = 0;
2177 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2178 NFS4_OPEN_CLAIM_PREVIOUS);
2179 if (IS_ERR(opendata))
2180 return PTR_ERR(opendata);
2182 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2183 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
2184 delegation_type = delegation->type;
2186 opendata->o_arg.u.delegation_type = delegation_type;
2187 status = nfs4_open_recover(opendata, state);
2188 nfs4_opendata_put(opendata);
2192 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2194 struct nfs_server *server = NFS_SERVER(state->inode);
2195 struct nfs4_exception exception = { };
2198 err = _nfs4_do_open_reclaim(ctx, state);
2199 trace_nfs4_open_reclaim(ctx, 0, err);
2200 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2202 if (err != -NFS4ERR_DELAY)
2204 nfs4_handle_exception(server, err, &exception);
2205 } while (exception.retry);
2209 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2211 struct nfs_open_context *ctx;
2214 ctx = nfs4_state_find_open_context(state);
2217 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2218 nfs_state_clear_open_state_flags(state);
2219 ret = nfs4_do_open_reclaim(ctx, state);
2220 put_nfs_open_context(ctx);
2224 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)
2228 printk(KERN_ERR "NFS: %s: unhandled error "
2229 "%d.\n", __func__, err);
2237 case -NFS4ERR_BADSESSION:
2238 case -NFS4ERR_BADSLOT:
2239 case -NFS4ERR_BAD_HIGH_SLOT:
2240 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2241 case -NFS4ERR_DEADSESSION:
2243 case -NFS4ERR_STALE_CLIENTID:
2244 case -NFS4ERR_STALE_STATEID:
2245 /* Don't recall a delegation if it was lost */
2246 nfs4_schedule_lease_recovery(server->nfs_client);
2248 case -NFS4ERR_MOVED:
2249 nfs4_schedule_migration_recovery(server);
2251 case -NFS4ERR_LEASE_MOVED:
2252 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2254 case -NFS4ERR_DELEG_REVOKED:
2255 case -NFS4ERR_ADMIN_REVOKED:
2256 case -NFS4ERR_EXPIRED:
2257 case -NFS4ERR_BAD_STATEID:
2258 case -NFS4ERR_OPENMODE:
2259 nfs_inode_find_state_and_recover(state->inode,
2261 nfs4_schedule_stateid_recovery(server, state);
2263 case -NFS4ERR_DELAY:
2264 case -NFS4ERR_GRACE:
2268 case -NFS4ERR_DENIED:
2270 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2272 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2279 int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2280 struct nfs4_state *state, const nfs4_stateid *stateid)
2282 struct nfs_server *server = NFS_SERVER(state->inode);
2283 struct nfs4_opendata *opendata;
2286 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2287 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2288 if (IS_ERR(opendata))
2289 return PTR_ERR(opendata);
2290 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
2291 if (!test_bit(NFS_O_RDWR_STATE, &state->flags)) {
2292 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2296 if (!test_bit(NFS_O_WRONLY_STATE, &state->flags)) {
2297 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2301 if (!test_bit(NFS_O_RDONLY_STATE, &state->flags)) {
2302 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2306 nfs_state_clear_delegation(state);
2308 nfs4_opendata_put(opendata);
2309 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
2312 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2314 struct nfs4_opendata *data = calldata;
2316 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2317 &data->c_arg.seq_args, &data->c_res.seq_res, task);
2320 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2322 struct nfs4_opendata *data = calldata;
2324 nfs40_sequence_done(task, &data->c_res.seq_res);
2326 data->rpc_status = task->tk_status;
2327 if (data->rpc_status == 0) {
2328 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
2329 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2330 renew_lease(data->o_res.server, data->timestamp);
2331 data->rpc_done = true;
2335 static void nfs4_open_confirm_release(void *calldata)
2337 struct nfs4_opendata *data = calldata;
2338 struct nfs4_state *state = NULL;
2340 /* If this request hasn't been cancelled, do nothing */
2341 if (!data->cancelled)
2343 /* In case of error, no cleanup! */
2344 if (!data->rpc_done)
2346 state = nfs4_opendata_to_nfs4_state(data);
2348 nfs4_close_state(state, data->o_arg.fmode);
2350 nfs4_opendata_put(data);
2353 static const struct rpc_call_ops nfs4_open_confirm_ops = {
2354 .rpc_call_prepare = nfs4_open_confirm_prepare,
2355 .rpc_call_done = nfs4_open_confirm_done,
2356 .rpc_release = nfs4_open_confirm_release,
2360 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2362 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2364 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
2365 struct rpc_task *task;
2366 struct rpc_message msg = {
2367 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2368 .rpc_argp = &data->c_arg,
2369 .rpc_resp = &data->c_res,
2370 .rpc_cred = data->owner->so_cred,
2372 struct rpc_task_setup task_setup_data = {
2373 .rpc_client = server->client,
2374 .rpc_message = &msg,
2375 .callback_ops = &nfs4_open_confirm_ops,
2376 .callback_data = data,
2377 .workqueue = nfsiod_workqueue,
2378 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
2382 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2384 kref_get(&data->kref);
2385 data->rpc_done = false;
2386 data->rpc_status = 0;
2387 data->timestamp = jiffies;
2388 task = rpc_run_task(&task_setup_data);
2390 return PTR_ERR(task);
2391 status = rpc_wait_for_completion_task(task);
2393 data->cancelled = true;
2396 status = data->rpc_status;
2401 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
2403 struct nfs4_opendata *data = calldata;
2404 struct nfs4_state_owner *sp = data->owner;
2405 struct nfs_client *clp = sp->so_server->nfs_client;
2406 enum open_claim_type4 claim = data->o_arg.claim;
2408 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
2411 * Check if we still need to send an OPEN call, or if we can use
2412 * a delegation instead.
2414 if (data->state != NULL) {
2415 struct nfs_delegation *delegation;
2417 if (can_open_cached(data->state, data->o_arg.fmode,
2418 data->o_arg.open_flags, claim))
2421 delegation = nfs4_get_valid_delegation(data->state->inode);
2422 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
2423 goto unlock_no_action;
2426 /* Update client id. */
2427 data->o_arg.clientid = clp->cl_clientid;
2431 case NFS4_OPEN_CLAIM_PREVIOUS:
2432 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2433 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
2434 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
2436 case NFS4_OPEN_CLAIM_FH:
2437 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
2439 data->timestamp = jiffies;
2440 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
2441 &data->o_arg.seq_args,
2442 &data->o_res.seq_res,
2444 nfs_release_seqid(data->o_arg.seqid);
2446 /* Set the create mode (note dependency on the session type) */
2447 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2448 if (data->o_arg.open_flags & O_EXCL) {
2449 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2450 if (nfs4_has_persistent_session(clp))
2451 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2452 else if (clp->cl_mvops->minor_version > 0)
2453 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2457 trace_nfs4_cached_open(data->state);
2460 task->tk_action = NULL;
2462 nfs4_sequence_done(task, &data->o_res.seq_res);
2465 static void nfs4_open_done(struct rpc_task *task, void *calldata)
2467 struct nfs4_opendata *data = calldata;
2469 data->rpc_status = task->tk_status;
2471 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
2474 if (task->tk_status == 0) {
2475 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2476 switch (data->o_res.f_attr->mode & S_IFMT) {
2480 data->rpc_status = -ELOOP;
2483 data->rpc_status = -EISDIR;
2486 data->rpc_status = -ENOTDIR;
2489 renew_lease(data->o_res.server, data->timestamp);
2490 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2491 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2493 data->rpc_done = true;
2496 static void nfs4_open_release(void *calldata)
2498 struct nfs4_opendata *data = calldata;
2499 struct nfs4_state *state = NULL;
2501 /* If this request hasn't been cancelled, do nothing */
2502 if (!data->cancelled)
2504 /* In case of error, no cleanup! */
2505 if (data->rpc_status != 0 || !data->rpc_done)
2507 /* In case we need an open_confirm, no cleanup! */
2508 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2510 state = nfs4_opendata_to_nfs4_state(data);
2512 nfs4_close_state(state, data->o_arg.fmode);
2514 nfs4_opendata_put(data);
2517 static const struct rpc_call_ops nfs4_open_ops = {
2518 .rpc_call_prepare = nfs4_open_prepare,
2519 .rpc_call_done = nfs4_open_done,
2520 .rpc_release = nfs4_open_release,
2523 static int nfs4_run_open_task(struct nfs4_opendata *data,
2524 struct nfs_open_context *ctx)
2526 struct inode *dir = d_inode(data->dir);
2527 struct nfs_server *server = NFS_SERVER(dir);
2528 struct nfs_openargs *o_arg = &data->o_arg;
2529 struct nfs_openres *o_res = &data->o_res;
2530 struct rpc_task *task;
2531 struct rpc_message msg = {
2532 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2535 .rpc_cred = data->owner->so_cred,
2537 struct rpc_task_setup task_setup_data = {
2538 .rpc_client = server->client,
2539 .rpc_message = &msg,
2540 .callback_ops = &nfs4_open_ops,
2541 .callback_data = data,
2542 .workqueue = nfsiod_workqueue,
2543 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
2547 kref_get(&data->kref);
2548 data->rpc_done = false;
2549 data->rpc_status = 0;
2550 data->cancelled = false;
2551 data->is_recover = false;
2553 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
2554 data->is_recover = true;
2555 task_setup_data.flags |= RPC_TASK_TIMEOUT;
2557 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
2558 pnfs_lgopen_prepare(data, ctx);
2560 task = rpc_run_task(&task_setup_data);
2562 return PTR_ERR(task);
2563 status = rpc_wait_for_completion_task(task);
2565 data->cancelled = true;
2568 status = data->rpc_status;
2574 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2576 struct inode *dir = d_inode(data->dir);
2577 struct nfs_openres *o_res = &data->o_res;
2580 status = nfs4_run_open_task(data, NULL);
2581 if (status != 0 || !data->rpc_done)
2584 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2586 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
2587 status = _nfs4_proc_open_confirm(data);
2593 * Additional permission checks in order to distinguish between an
2594 * open for read, and an open for execute. This works around the
2595 * fact that NFSv4 OPEN treats read and execute permissions as being
2597 * Note that in the non-execute case, we want to turn off permission
2598 * checking if we just created a new file (POSIX open() semantics).
2600 static int nfs4_opendata_access(const struct cred *cred,
2601 struct nfs4_opendata *opendata,
2602 struct nfs4_state *state, fmode_t fmode,
2605 struct nfs_access_entry cache;
2608 /* access call failed or for some reason the server doesn't
2609 * support any access modes -- defer access call until later */
2610 if (opendata->o_res.access_supported == 0)
2615 * Use openflags to check for exec, because fmode won't
2616 * always have FMODE_EXEC set when file open for exec.
2618 if (openflags & __FMODE_EXEC) {
2619 /* ONLY check for exec rights */
2620 if (S_ISDIR(state->inode->i_mode))
2621 mask = NFS4_ACCESS_LOOKUP;
2623 mask = NFS4_ACCESS_EXECUTE;
2624 } else if ((fmode & FMODE_READ) && !opendata->file_created)
2625 mask = NFS4_ACCESS_READ;
2628 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2629 nfs_access_add_cache(state->inode, &cache);
2631 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2632 if ((mask & ~cache.mask & flags) == 0)
2639 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2641 static int _nfs4_proc_open(struct nfs4_opendata *data,
2642 struct nfs_open_context *ctx)
2644 struct inode *dir = d_inode(data->dir);
2645 struct nfs_server *server = NFS_SERVER(dir);
2646 struct nfs_openargs *o_arg = &data->o_arg;
2647 struct nfs_openres *o_res = &data->o_res;
2650 status = nfs4_run_open_task(data, ctx);
2651 if (!data->rpc_done)
2654 if (status == -NFS4ERR_BADNAME &&
2655 !(o_arg->open_flags & O_CREAT))
2660 nfs_fattr_map_and_free_names(server, &data->f_attr);
2662 if (o_arg->open_flags & O_CREAT) {
2663 if (o_arg->open_flags & O_EXCL)
2664 data->file_created = true;
2665 else if (o_res->cinfo.before != o_res->cinfo.after)
2666 data->file_created = true;
2667 if (data->file_created ||
2668 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
2669 nfs4_update_changeattr(dir, &o_res->cinfo,
2670 o_res->f_attr->time_start,
2671 NFS_INO_INVALID_DATA);
2673 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2674 server->caps &= ~NFS_CAP_POSIX_LOCK;
2675 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2676 status = _nfs4_proc_open_confirm(data);
2680 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2681 nfs4_sequence_free_slot(&o_res->seq_res);
2682 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr,
2683 o_res->f_label, NULL);
2690 * reclaim state on the server after a network partition.
2691 * Assumes caller holds the appropriate lock
2693 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2695 struct nfs4_opendata *opendata;
2698 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2699 NFS4_OPEN_CLAIM_FH);
2700 if (IS_ERR(opendata))
2701 return PTR_ERR(opendata);
2702 ret = nfs4_open_recover(opendata, state);
2704 d_drop(ctx->dentry);
2705 nfs4_opendata_put(opendata);
2709 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2711 struct nfs_server *server = NFS_SERVER(state->inode);
2712 struct nfs4_exception exception = { };
2716 err = _nfs4_open_expired(ctx, state);
2717 trace_nfs4_open_expired(ctx, 0, err);
2718 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2723 case -NFS4ERR_GRACE:
2724 case -NFS4ERR_DELAY:
2725 nfs4_handle_exception(server, err, &exception);
2728 } while (exception.retry);
2733 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2735 struct nfs_open_context *ctx;
2738 ctx = nfs4_state_find_open_context(state);
2741 ret = nfs4_do_open_expired(ctx, state);
2742 put_nfs_open_context(ctx);
2746 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2747 const nfs4_stateid *stateid)
2749 nfs_remove_bad_delegation(state->inode, stateid);
2750 nfs_state_clear_delegation(state);
2753 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2755 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2756 nfs_finish_clear_delegation_stateid(state, NULL);
2759 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2761 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2762 nfs40_clear_delegation_stateid(state);
2763 nfs_state_clear_open_state_flags(state);
2764 return nfs4_open_expired(sp, state);
2767 static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2768 nfs4_stateid *stateid,
2769 const struct cred *cred)
2771 return -NFS4ERR_BAD_STATEID;
2774 #if defined(CONFIG_NFS_V4_1)
2775 static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2776 nfs4_stateid *stateid,
2777 const struct cred *cred)
2781 switch (stateid->type) {
2784 case NFS4_INVALID_STATEID_TYPE:
2785 case NFS4_SPECIAL_STATEID_TYPE:
2786 return -NFS4ERR_BAD_STATEID;
2787 case NFS4_REVOKED_STATEID_TYPE:
2791 status = nfs41_test_stateid(server, stateid, cred);
2793 case -NFS4ERR_EXPIRED:
2794 case -NFS4ERR_ADMIN_REVOKED:
2795 case -NFS4ERR_DELEG_REVOKED:
2801 /* Ack the revoked state to the server */
2802 nfs41_free_stateid(server, stateid, cred, true);
2803 return -NFS4ERR_EXPIRED;
2806 static int nfs41_check_delegation_stateid(struct nfs4_state *state)
2808 struct nfs_server *server = NFS_SERVER(state->inode);
2809 nfs4_stateid stateid;
2810 struct nfs_delegation *delegation;
2811 const struct cred *cred = NULL;
2812 int status, ret = NFS_OK;
2814 /* Get the delegation credential for use by test/free_stateid */
2816 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2817 if (delegation == NULL) {
2819 nfs_state_clear_delegation(state);
2823 spin_lock(&delegation->lock);
2824 nfs4_stateid_copy(&stateid, &delegation->stateid);
2826 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2827 &delegation->flags)) {
2828 spin_unlock(&delegation->lock);
2833 if (delegation->cred)
2834 cred = get_cred(delegation->cred);
2835 spin_unlock(&delegation->lock);
2837 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
2838 trace_nfs4_test_delegation_stateid(state, NULL, status);
2839 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
2840 nfs_finish_clear_delegation_stateid(state, &stateid);
2848 static void nfs41_delegation_recover_stateid(struct nfs4_state *state)
2852 if (test_bit(NFS_DELEGATED_STATE, &state->flags) &&
2853 nfs4_copy_delegation_stateid(state->inode, state->state,
2855 nfs4_stateid_match_other(&state->stateid, &tmp))
2856 nfs_state_set_delegation(state, &tmp, state->state);
2858 nfs_state_clear_delegation(state);
2862 * nfs41_check_expired_locks - possibly free a lock stateid
2864 * @state: NFSv4 state for an inode
2866 * Returns NFS_OK if recovery for this stateid is now finished.
2867 * Otherwise a negative NFS4ERR value is returned.
2869 static int nfs41_check_expired_locks(struct nfs4_state *state)
2871 int status, ret = NFS_OK;
2872 struct nfs4_lock_state *lsp, *prev = NULL;
2873 struct nfs_server *server = NFS_SERVER(state->inode);
2875 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2878 spin_lock(&state->state_lock);
2879 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2880 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2881 const struct cred *cred = lsp->ls_state->owner->so_cred;
2883 refcount_inc(&lsp->ls_count);
2884 spin_unlock(&state->state_lock);
2886 nfs4_put_lock_state(prev);
2889 status = nfs41_test_and_free_expired_stateid(server,
2892 trace_nfs4_test_lock_stateid(state, lsp, status);
2893 if (status == -NFS4ERR_EXPIRED ||
2894 status == -NFS4ERR_BAD_STATEID) {
2895 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
2896 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
2897 if (!recover_lost_locks)
2898 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2899 } else if (status != NFS_OK) {
2901 nfs4_put_lock_state(prev);
2904 spin_lock(&state->state_lock);
2907 spin_unlock(&state->state_lock);
2908 nfs4_put_lock_state(prev);
2914 * nfs41_check_open_stateid - possibly free an open stateid
2916 * @state: NFSv4 state for an inode
2918 * Returns NFS_OK if recovery for this stateid is now finished.
2919 * Otherwise a negative NFS4ERR value is returned.
2921 static int nfs41_check_open_stateid(struct nfs4_state *state)
2923 struct nfs_server *server = NFS_SERVER(state->inode);
2924 nfs4_stateid *stateid = &state->open_stateid;
2925 const struct cred *cred = state->owner->so_cred;
2928 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0)
2929 return -NFS4ERR_BAD_STATEID;
2930 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
2931 trace_nfs4_test_open_stateid(state, NULL, status);
2932 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
2933 nfs_state_clear_open_state_flags(state);
2934 stateid->type = NFS4_INVALID_STATEID_TYPE;
2937 if (nfs_open_stateid_recover_openmode(state))
2938 return -NFS4ERR_OPENMODE;
2942 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2946 status = nfs41_check_delegation_stateid(state);
2947 if (status != NFS_OK)
2949 nfs41_delegation_recover_stateid(state);
2951 status = nfs41_check_expired_locks(state);
2952 if (status != NFS_OK)
2954 status = nfs41_check_open_stateid(state);
2955 if (status != NFS_OK)
2956 status = nfs4_open_expired(sp, state);
2962 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2963 * fields corresponding to attributes that were used to store the verifier.
2964 * Make sure we clobber those fields in the later setattr call
2966 static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2967 struct iattr *sattr, struct nfs4_label **label)
2969 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2974 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
2975 attrset[i] = opendata->o_res.attrset[i];
2976 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
2977 attrset[i] &= ~bitmask[i];
2980 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
2981 sattr->ia_valid : 0;
2983 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
2984 if (sattr->ia_valid & ATTR_ATIME_SET)
2985 ret |= ATTR_ATIME_SET;
2990 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
2991 if (sattr->ia_valid & ATTR_MTIME_SET)
2992 ret |= ATTR_MTIME_SET;
2997 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
3002 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
3003 int flags, struct nfs_open_context *ctx)
3005 struct nfs4_state_owner *sp = opendata->owner;
3006 struct nfs_server *server = sp->so_server;
3007 struct dentry *dentry;
3008 struct nfs4_state *state;
3009 fmode_t acc_mode = _nfs4_ctx_to_accessmode(ctx);
3010 struct inode *dir = d_inode(opendata->dir);
3011 unsigned long dir_verifier;
3015 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
3016 dir_verifier = nfs_save_change_attribute(dir);
3018 ret = _nfs4_proc_open(opendata, ctx);
3022 state = _nfs4_opendata_to_nfs4_state(opendata);
3023 ret = PTR_ERR(state);
3027 if (server->caps & NFS_CAP_POSIX_LOCK)
3028 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
3029 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
3030 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
3032 dentry = opendata->dentry;
3033 if (d_really_is_negative(dentry)) {
3034 struct dentry *alias;
3036 alias = d_exact_alias(dentry, state->inode);
3038 alias = d_splice_alias(igrab(state->inode), dentry);
3039 /* d_splice_alias() can't fail here - it's a non-directory */
3042 ctx->dentry = dentry = alias;
3046 switch(opendata->o_arg.claim) {
3049 case NFS4_OPEN_CLAIM_NULL:
3050 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
3051 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
3052 if (!opendata->rpc_done)
3054 if (opendata->o_res.delegation_type != 0)
3055 dir_verifier = nfs_save_change_attribute(dir);
3056 nfs_set_verifier(dentry, dir_verifier);
3059 /* Parse layoutget results before we check for access */
3060 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
3062 ret = nfs4_opendata_access(sp->so_cred, opendata, state,
3067 if (d_inode(dentry) == state->inode) {
3068 nfs_inode_attach_open_context(ctx);
3069 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
3070 nfs4_schedule_stateid_recovery(server, state);
3074 if (!opendata->cancelled)
3075 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
3080 * Returns a referenced nfs4_state
3082 static int _nfs4_do_open(struct inode *dir,
3083 struct nfs_open_context *ctx,
3085 const struct nfs4_open_createattrs *c,
3088 struct nfs4_state_owner *sp;
3089 struct nfs4_state *state = NULL;
3090 struct nfs_server *server = NFS_SERVER(dir);
3091 struct nfs4_opendata *opendata;
3092 struct dentry *dentry = ctx->dentry;
3093 const struct cred *cred = ctx->cred;
3094 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
3095 fmode_t fmode = _nfs4_ctx_to_openmode(ctx);
3096 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
3097 struct iattr *sattr = c->sattr;
3098 struct nfs4_label *label = c->label;
3099 struct nfs4_label *olabel = NULL;
3102 /* Protect against reboot recovery conflicts */
3104 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
3106 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
3109 status = nfs4_client_recover_expired_lease(server->nfs_client);
3111 goto err_put_state_owner;
3112 if (d_really_is_positive(dentry))
3113 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
3115 if (d_really_is_positive(dentry))
3116 claim = NFS4_OPEN_CLAIM_FH;
3117 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
3118 c, claim, GFP_KERNEL);
3119 if (opendata == NULL)
3120 goto err_put_state_owner;
3123 olabel = nfs4_label_alloc(server, GFP_KERNEL);
3124 if (IS_ERR(olabel)) {
3125 status = PTR_ERR(olabel);
3126 goto err_opendata_put;
3130 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
3131 if (!opendata->f_attr.mdsthreshold) {
3132 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
3133 if (!opendata->f_attr.mdsthreshold)
3134 goto err_free_label;
3136 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
3138 if (d_really_is_positive(dentry))
3139 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
3141 status = _nfs4_open_and_get_state(opendata, flags, ctx);
3143 goto err_free_label;
3146 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
3147 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
3148 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
3150 * send create attributes which was not set by open
3151 * with an extra setattr.
3153 if (attrs || label) {
3154 unsigned ia_old = sattr->ia_valid;
3156 sattr->ia_valid = attrs;
3157 nfs_fattr_init(opendata->o_res.f_attr);
3158 status = nfs4_do_setattr(state->inode, cred,
3159 opendata->o_res.f_attr, sattr,
3160 ctx, label, olabel);
3162 nfs_setattr_update_inode(state->inode, sattr,
3163 opendata->o_res.f_attr);
3164 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
3166 sattr->ia_valid = ia_old;
3169 if (opened && opendata->file_created)
3172 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
3173 *ctx_th = opendata->f_attr.mdsthreshold;
3174 opendata->f_attr.mdsthreshold = NULL;
3177 nfs4_label_free(olabel);
3179 nfs4_opendata_put(opendata);
3180 nfs4_put_state_owner(sp);
3183 nfs4_label_free(olabel);
3185 nfs4_opendata_put(opendata);
3186 err_put_state_owner:
3187 nfs4_put_state_owner(sp);
3193 static struct nfs4_state *nfs4_do_open(struct inode *dir,
3194 struct nfs_open_context *ctx,
3196 struct iattr *sattr,
3197 struct nfs4_label *label,
3200 struct nfs_server *server = NFS_SERVER(dir);
3201 struct nfs4_exception exception = {
3202 .interruptible = true,
3204 struct nfs4_state *res;
3205 struct nfs4_open_createattrs c = {
3209 [0] = (__u32)jiffies,
3210 [1] = (__u32)current->pid,
3216 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
3218 trace_nfs4_open_file(ctx, flags, status);
3221 /* NOTE: BAD_SEQID means the server and client disagree about the
3222 * book-keeping w.r.t. state-changing operations
3223 * (OPEN/CLOSE/LOCK/LOCKU...)
3224 * It is actually a sign of a bug on the client or on the server.
3226 * If we receive a BAD_SEQID error in the particular case of
3227 * doing an OPEN, we assume that nfs_increment_open_seqid() will
3228 * have unhashed the old state_owner for us, and that we can
3229 * therefore safely retry using a new one. We should still warn
3230 * the user though...
3232 if (status == -NFS4ERR_BAD_SEQID) {
3233 pr_warn_ratelimited("NFS: v4 server %s "
3234 " returned a bad sequence-id error!\n",
3235 NFS_SERVER(dir)->nfs_client->cl_hostname);
3236 exception.retry = 1;
3240 * BAD_STATEID on OPEN means that the server cancelled our
3241 * state before it received the OPEN_CONFIRM.
3242 * Recover by retrying the request as per the discussion
3243 * on Page 181 of RFC3530.
3245 if (status == -NFS4ERR_BAD_STATEID) {
3246 exception.retry = 1;
3249 if (status == -NFS4ERR_EXPIRED) {
3250 nfs4_schedule_lease_recovery(server->nfs_client);
3251 exception.retry = 1;
3254 if (status == -EAGAIN) {
3255 /* We must have found a delegation */
3256 exception.retry = 1;
3259 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3261 res = ERR_PTR(nfs4_handle_exception(server,
3262 status, &exception));
3263 } while (exception.retry);
3267 static int _nfs4_do_setattr(struct inode *inode,
3268 struct nfs_setattrargs *arg,
3269 struct nfs_setattrres *res,
3270 const struct cred *cred,
3271 struct nfs_open_context *ctx)
3273 struct nfs_server *server = NFS_SERVER(inode);
3274 struct rpc_message msg = {
3275 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3280 const struct cred *delegation_cred = NULL;
3281 unsigned long timestamp = jiffies;
3285 nfs_fattr_init(res->fattr);
3287 /* Servers should only apply open mode checks for file size changes */
3288 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
3290 nfs4_inode_make_writeable(inode);
3294 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
3295 /* Use that stateid */
3296 } else if (ctx != NULL && ctx->state) {
3297 struct nfs_lock_context *l_ctx;
3298 if (!nfs4_valid_open_stateid(ctx->state))
3300 l_ctx = nfs_get_lock_context(ctx);
3302 return PTR_ERR(l_ctx);
3303 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3304 &arg->stateid, &delegation_cred);
3305 nfs_put_lock_context(l_ctx);
3308 else if (status == -EAGAIN)
3312 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
3314 if (delegation_cred)
3315 msg.rpc_cred = delegation_cred;
3317 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3319 put_cred(delegation_cred);
3320 if (status == 0 && ctx != NULL)
3321 renew_lease(server, timestamp);
3322 trace_nfs4_setattr(inode, &arg->stateid, status);
3326 static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
3327 struct nfs_fattr *fattr, struct iattr *sattr,
3328 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
3329 struct nfs4_label *olabel)
3331 struct nfs_server *server = NFS_SERVER(inode);
3332 __u32 bitmask[NFS4_BITMASK_SZ];
3333 struct nfs4_state *state = ctx ? ctx->state : NULL;
3334 struct nfs_setattrargs arg = {
3335 .fh = NFS_FH(inode),
3341 struct nfs_setattrres res = {
3346 struct nfs4_exception exception = {
3349 .stateid = &arg.stateid,
3354 nfs4_bitmap_copy_adjust_setattr(bitmask,
3355 nfs4_bitmask(server, olabel),
3358 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
3360 case -NFS4ERR_OPENMODE:
3361 if (!(sattr->ia_valid & ATTR_SIZE)) {
3362 pr_warn_once("NFSv4: server %s is incorrectly "
3363 "applying open mode checks to "
3364 "a SETATTR that is not "
3365 "changing file size.\n",
3366 server->nfs_client->cl_hostname);
3368 if (state && !(state->state & FMODE_WRITE)) {
3370 if (sattr->ia_valid & ATTR_OPEN)
3375 err = nfs4_handle_exception(server, err, &exception);
3376 } while (exception.retry);
3382 nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3384 if (inode == NULL || !nfs_have_layout(inode))
3387 return pnfs_wait_on_layoutreturn(inode, task);
3391 * Update the seqid of an open stateid
3393 static void nfs4_sync_open_stateid(nfs4_stateid *dst,
3394 struct nfs4_state *state)
3401 if (!nfs4_valid_open_stateid(state))
3403 seq = read_seqbegin(&state->seqlock);
3404 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3405 nfs4_stateid_copy(dst, &state->open_stateid);
3406 if (read_seqretry(&state->seqlock, seq))
3410 seqid_open = state->open_stateid.seqid;
3411 if (read_seqretry(&state->seqlock, seq))
3414 dst_seqid = be32_to_cpu(dst->seqid);
3415 if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) < 0)
3416 dst->seqid = seqid_open;
3422 * Update the seqid of an open stateid after receiving
3423 * NFS4ERR_OLD_STATEID
3425 static bool nfs4_refresh_open_old_stateid(nfs4_stateid *dst,
3426 struct nfs4_state *state)
3431 int seq, status = -EAGAIN;
3436 if (!nfs4_valid_open_stateid(state))
3438 seq = read_seqbegin(&state->seqlock);
3439 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3440 if (read_seqretry(&state->seqlock, seq))
3445 write_seqlock(&state->seqlock);
3446 seqid_open = state->open_stateid.seqid;
3448 dst_seqid = be32_to_cpu(dst->seqid);
3450 /* Did another OPEN bump the state's seqid? try again: */
3451 if ((s32)(be32_to_cpu(seqid_open) - dst_seqid) > 0) {
3452 dst->seqid = seqid_open;
3453 write_sequnlock(&state->seqlock);
3458 /* server says we're behind but we haven't seen the update yet */
3459 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
3460 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
3461 write_sequnlock(&state->seqlock);
3462 trace_nfs4_close_stateid_update_wait(state->inode, dst, 0);
3464 if (signal_pending(current))
3467 if (schedule_timeout(5*HZ) != 0)
3470 finish_wait(&state->waitq, &wait);
3474 if (status == -EINTR)
3477 /* we slept the whole 5 seconds, we must have lost a seqid */
3478 dst->seqid = cpu_to_be32(dst_seqid + 1);
3486 struct nfs4_closedata {
3487 struct inode *inode;
3488 struct nfs4_state *state;
3489 struct nfs_closeargs arg;
3490 struct nfs_closeres res;
3492 struct nfs4_layoutreturn_args arg;
3493 struct nfs4_layoutreturn_res res;
3494 struct nfs4_xdr_opaque_data ld_private;
3498 struct nfs_fattr fattr;
3499 unsigned long timestamp;
3502 static void nfs4_free_closedata(void *data)
3504 struct nfs4_closedata *calldata = data;
3505 struct nfs4_state_owner *sp = calldata->state->owner;
3506 struct super_block *sb = calldata->state->inode->i_sb;
3508 if (calldata->lr.roc)
3509 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3510 calldata->res.lr_ret);
3511 nfs4_put_open_state(calldata->state);
3512 nfs_free_seqid(calldata->arg.seqid);
3513 nfs4_put_state_owner(sp);
3514 nfs_sb_deactive(sb);
3518 static void nfs4_close_done(struct rpc_task *task, void *data)
3520 struct nfs4_closedata *calldata = data;
3521 struct nfs4_state *state = calldata->state;
3522 struct nfs_server *server = NFS_SERVER(calldata->inode);
3523 nfs4_stateid *res_stateid = NULL;
3524 struct nfs4_exception exception = {
3526 .inode = calldata->inode,
3527 .stateid = &calldata->arg.stateid,
3530 dprintk("%s: begin!\n", __func__);
3531 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3533 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
3535 /* Handle Layoutreturn errors */
3536 if (pnfs_roc_done(task, &calldata->arg.lr_args, &calldata->res.lr_res,
3537 &calldata->res.lr_ret) == -EAGAIN)
3540 /* hmm. we are done with the inode, and in the process of freeing
3541 * the state_owner. we keep this around to process errors
3543 switch (task->tk_status) {
3545 res_stateid = &calldata->res.stateid;
3546 renew_lease(server, calldata->timestamp);
3548 case -NFS4ERR_ACCESS:
3549 if (calldata->arg.bitmask != NULL) {
3550 calldata->arg.bitmask = NULL;
3551 calldata->res.fattr = NULL;
3556 case -NFS4ERR_OLD_STATEID:
3557 /* Did we race with OPEN? */
3558 if (nfs4_refresh_open_old_stateid(&calldata->arg.stateid,
3562 case -NFS4ERR_ADMIN_REVOKED:
3563 case -NFS4ERR_STALE_STATEID:
3564 case -NFS4ERR_EXPIRED:
3565 nfs4_free_revoked_stateid(server,
3566 &calldata->arg.stateid,
3567 task->tk_msg.rpc_cred);
3569 case -NFS4ERR_BAD_STATEID:
3570 if (calldata->arg.fmode == 0)
3574 task->tk_status = nfs4_async_handle_exception(task,
3575 server, task->tk_status, &exception);
3576 if (exception.retry)
3579 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3580 res_stateid, calldata->arg.fmode);
3582 task->tk_status = 0;
3583 nfs_release_seqid(calldata->arg.seqid);
3584 nfs_refresh_inode(calldata->inode, &calldata->fattr);
3585 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
3588 task->tk_status = 0;
3589 rpc_restart_call_prepare(task);
3593 static void nfs4_close_prepare(struct rpc_task *task, void *data)
3595 struct nfs4_closedata *calldata = data;
3596 struct nfs4_state *state = calldata->state;
3597 struct inode *inode = calldata->inode;
3598 struct pnfs_layout_hdr *lo;
3599 bool is_rdonly, is_wronly, is_rdwr;
3602 dprintk("%s: begin!\n", __func__);
3603 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3606 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
3607 spin_lock(&state->owner->so_lock);
3608 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3609 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3610 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
3611 /* Calculate the change in open mode */
3612 calldata->arg.fmode = 0;
3613 if (state->n_rdwr == 0) {
3614 if (state->n_rdonly == 0)
3615 call_close |= is_rdonly;
3617 calldata->arg.fmode |= FMODE_READ;
3618 if (state->n_wronly == 0)
3619 call_close |= is_wronly;
3621 calldata->arg.fmode |= FMODE_WRITE;
3622 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3623 call_close |= is_rdwr;
3625 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3627 nfs4_sync_open_stateid(&calldata->arg.stateid, state);
3628 if (!nfs4_valid_open_stateid(state))
3630 spin_unlock(&state->owner->so_lock);
3633 /* Note: exit _without_ calling nfs4_close_done */
3637 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
3638 nfs_release_seqid(calldata->arg.seqid);
3642 lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3643 if (lo && !pnfs_layout_is_valid(lo)) {
3644 calldata->arg.lr_args = NULL;
3645 calldata->res.lr_res = NULL;
3648 if (calldata->arg.fmode == 0)
3649 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
3651 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
3652 /* Close-to-open cache consistency revalidation */
3653 if (!nfs4_have_delegation(inode, FMODE_READ)) {
3654 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3655 nfs4_bitmask_adjust(calldata->arg.bitmask, inode, NFS_SERVER(inode), NULL);
3657 calldata->arg.bitmask = NULL;
3660 calldata->arg.share_access =
3661 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3662 calldata->arg.fmode, 0);
3664 if (calldata->res.fattr == NULL)
3665 calldata->arg.bitmask = NULL;
3666 else if (calldata->arg.bitmask == NULL)
3667 calldata->res.fattr = NULL;
3668 calldata->timestamp = jiffies;
3669 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
3670 &calldata->arg.seq_args,
3671 &calldata->res.seq_res,
3673 nfs_release_seqid(calldata->arg.seqid);
3674 dprintk("%s: done!\n", __func__);
3677 task->tk_action = NULL;
3679 nfs4_sequence_done(task, &calldata->res.seq_res);
3682 static const struct rpc_call_ops nfs4_close_ops = {
3683 .rpc_call_prepare = nfs4_close_prepare,
3684 .rpc_call_done = nfs4_close_done,
3685 .rpc_release = nfs4_free_closedata,
3689 * It is possible for data to be read/written from a mem-mapped file
3690 * after the sys_close call (which hits the vfs layer as a flush).
3691 * This means that we can't safely call nfsv4 close on a file until
3692 * the inode is cleared. This in turn means that we are not good
3693 * NFSv4 citizens - we do not indicate to the server to update the file's
3694 * share state even when we are done with one of the three share
3695 * stateid's in the inode.
3697 * NOTE: Caller must be holding the sp->so_owner semaphore!
3699 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
3701 struct nfs_server *server = NFS_SERVER(state->inode);
3702 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
3703 struct nfs4_closedata *calldata;
3704 struct nfs4_state_owner *sp = state->owner;
3705 struct rpc_task *task;
3706 struct rpc_message msg = {
3707 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3708 .rpc_cred = state->owner->so_cred,
3710 struct rpc_task_setup task_setup_data = {
3711 .rpc_client = server->client,
3712 .rpc_message = &msg,
3713 .callback_ops = &nfs4_close_ops,
3714 .workqueue = nfsiod_workqueue,
3715 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
3717 int status = -ENOMEM;
3719 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3720 &task_setup_data.rpc_client, &msg);
3722 calldata = kzalloc(sizeof(*calldata), gfp_mask);
3723 if (calldata == NULL)
3725 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
3726 calldata->inode = state->inode;
3727 calldata->state = state;
3728 calldata->arg.fh = NFS_FH(state->inode);
3729 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3730 goto out_free_calldata;
3731 /* Serialization for the sequence id */
3732 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3733 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
3734 if (IS_ERR(calldata->arg.seqid))
3735 goto out_free_calldata;
3736 nfs_fattr_init(&calldata->fattr);
3737 calldata->arg.fmode = 0;
3738 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
3739 calldata->res.fattr = &calldata->fattr;
3740 calldata->res.seqid = calldata->arg.seqid;
3741 calldata->res.server = server;
3742 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3743 calldata->lr.roc = pnfs_roc(state->inode,
3744 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3745 if (calldata->lr.roc) {
3746 calldata->arg.lr_args = &calldata->lr.arg;
3747 calldata->res.lr_res = &calldata->lr.res;
3749 nfs_sb_active(calldata->inode->i_sb);
3751 msg.rpc_argp = &calldata->arg;
3752 msg.rpc_resp = &calldata->res;
3753 task_setup_data.callback_data = calldata;
3754 task = rpc_run_task(&task_setup_data);
3756 return PTR_ERR(task);
3759 status = rpc_wait_for_completion_task(task);
3765 nfs4_put_open_state(state);
3766 nfs4_put_state_owner(sp);
3770 static struct inode *
3771 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3772 int open_flags, struct iattr *attr, int *opened)
3774 struct nfs4_state *state;
3775 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3777 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
3779 /* Protect against concurrent sillydeletes */
3780 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
3782 nfs4_label_release_security(label);
3785 return ERR_CAST(state);
3786 return state->inode;
3789 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
3791 if (ctx->state == NULL)
3794 nfs4_close_sync(ctx->state, _nfs4_ctx_to_openmode(ctx));
3796 nfs4_close_state(ctx->state, _nfs4_ctx_to_openmode(ctx));
3799 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3800 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3801 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_XATTR_SUPPORT - 1UL)
3803 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3805 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
3806 struct nfs4_server_caps_arg args = {
3810 struct nfs4_server_caps_res res = {};
3811 struct rpc_message msg = {
3812 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
3819 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3820 FATTR4_WORD0_FH_EXPIRE_TYPE |
3821 FATTR4_WORD0_LINK_SUPPORT |
3822 FATTR4_WORD0_SYMLINK_SUPPORT |
3823 FATTR4_WORD0_ACLSUPPORT;
3825 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3827 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3829 /* Sanity check the server answers */
3830 switch (minorversion) {
3832 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3833 res.attr_bitmask[2] = 0;
3836 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3839 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3841 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
3842 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3843 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3844 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3845 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
3846 NFS_CAP_CTIME|NFS_CAP_MTIME|
3847 NFS_CAP_SECURITY_LABEL);
3848 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3849 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3850 server->caps |= NFS_CAP_ACLS;
3851 if (res.has_links != 0)
3852 server->caps |= NFS_CAP_HARDLINKS;
3853 if (res.has_symlinks != 0)
3854 server->caps |= NFS_CAP_SYMLINKS;
3855 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3856 server->caps |= NFS_CAP_FILEID;
3857 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3858 server->caps |= NFS_CAP_MODE;
3859 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3860 server->caps |= NFS_CAP_NLINK;
3861 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3862 server->caps |= NFS_CAP_OWNER;
3863 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3864 server->caps |= NFS_CAP_OWNER_GROUP;
3865 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3866 server->caps |= NFS_CAP_ATIME;
3867 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3868 server->caps |= NFS_CAP_CTIME;
3869 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3870 server->caps |= NFS_CAP_MTIME;
3871 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3872 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3873 server->caps |= NFS_CAP_SECURITY_LABEL;
3875 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3876 sizeof(server->attr_bitmask));
3877 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3879 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3880 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3881 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
3882 server->cache_consistency_bitmask[2] = 0;
3884 /* Avoid a regression due to buggy server */
3885 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3886 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
3887 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3888 sizeof(server->exclcreat_bitmask));
3890 server->acl_bitmask = res.acl_bitmask;
3891 server->fh_expire_type = res.fh_expire_type;
3897 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3899 struct nfs4_exception exception = {
3900 .interruptible = true,
3904 err = nfs4_handle_exception(server,
3905 _nfs4_server_capabilities(server, fhandle),
3907 } while (exception.retry);
3911 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3912 struct nfs_fsinfo *info)
3915 struct nfs4_lookup_root_arg args = {
3918 struct nfs4_lookup_res res = {
3920 .fattr = info->fattr,
3923 struct rpc_message msg = {
3924 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3929 bitmask[0] = nfs4_fattr_bitmap[0];
3930 bitmask[1] = nfs4_fattr_bitmap[1];
3932 * Process the label in the upcoming getfattr
3934 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3936 nfs_fattr_init(info->fattr);
3937 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3940 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3941 struct nfs_fsinfo *info)
3943 struct nfs4_exception exception = {
3944 .interruptible = true,
3948 err = _nfs4_lookup_root(server, fhandle, info);
3949 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
3952 case -NFS4ERR_WRONGSEC:
3955 err = nfs4_handle_exception(server, err, &exception);
3957 } while (exception.retry);
3962 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3963 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3965 struct rpc_auth_create_args auth_args = {
3966 .pseudoflavor = flavor,
3968 struct rpc_auth *auth;
3970 auth = rpcauth_create(&auth_args, server->client);
3973 return nfs4_lookup_root(server, fhandle, info);
3977 * Retry pseudoroot lookup with various security flavors. We do this when:
3979 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3980 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3982 * Returns zero on success, or a negative NFS4ERR value, or a
3983 * negative errno value.
3985 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3986 struct nfs_fsinfo *info)
3988 /* Per 3530bis 15.33.5 */
3989 static const rpc_authflavor_t flav_array[] = {
3993 RPC_AUTH_UNIX, /* courtesy */
3996 int status = -EPERM;
3999 if (server->auth_info.flavor_len > 0) {
4000 /* try each flavor specified by user */
4001 for (i = 0; i < server->auth_info.flavor_len; i++) {
4002 status = nfs4_lookup_root_sec(server, fhandle, info,
4003 server->auth_info.flavors[i]);
4004 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4009 /* no flavors specified by user, try default list */
4010 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
4011 status = nfs4_lookup_root_sec(server, fhandle, info,
4013 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4020 * -EACCES could mean that the user doesn't have correct permissions
4021 * to access the mount. It could also mean that we tried to mount
4022 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
4023 * existing mount programs don't handle -EACCES very well so it should
4024 * be mapped to -EPERM instead.
4026 if (status == -EACCES)
4032 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
4033 * @server: initialized nfs_server handle
4034 * @fhandle: we fill in the pseudo-fs root file handle
4035 * @info: we fill in an FSINFO struct
4036 * @auth_probe: probe the auth flavours
4038 * Returns zero on success, or a negative errno.
4040 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
4041 struct nfs_fsinfo *info,
4047 status = nfs4_lookup_root(server, fhandle, info);
4049 if (auth_probe || status == NFS4ERR_WRONGSEC)
4050 status = server->nfs_client->cl_mvops->find_root_sec(server,
4054 status = nfs4_server_capabilities(server, fhandle);
4056 status = nfs4_do_fsinfo(server, fhandle, info);
4058 return nfs4_map_errors(status);
4061 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
4062 struct nfs_fsinfo *info)
4065 struct nfs_fattr *fattr = info->fattr;
4066 struct nfs4_label *label = fattr->label;
4068 error = nfs4_server_capabilities(server, mntfh);
4070 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
4074 error = nfs4_proc_getattr(server, mntfh, fattr, label, NULL);
4076 dprintk("nfs4_get_root: getattr error = %d\n", -error);
4080 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
4081 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
4082 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
4089 * Get locations and (maybe) other attributes of a referral.
4090 * Note that we'll actually follow the referral later when
4091 * we detect fsid mismatch in inode revalidation
4093 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
4094 const struct qstr *name, struct nfs_fattr *fattr,
4095 struct nfs_fh *fhandle)
4097 int status = -ENOMEM;
4098 struct page *page = NULL;
4099 struct nfs4_fs_locations *locations = NULL;
4101 page = alloc_page(GFP_KERNEL);
4104 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4105 if (locations == NULL)
4108 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
4113 * If the fsid didn't change, this is a migration event, not a
4114 * referral. Cause us to drop into the exception handler, which
4115 * will kick off migration recovery.
4117 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
4118 dprintk("%s: server did not return a different fsid for"
4119 " a referral at %s\n", __func__, name->name);
4120 status = -NFS4ERR_MOVED;
4123 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
4124 nfs_fixup_referral_attributes(&locations->fattr);
4126 /* replace the lookup nfs_fattr with the locations nfs_fattr */
4127 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
4128 memset(fhandle, 0, sizeof(struct nfs_fh));
4136 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4137 struct nfs_fattr *fattr, struct nfs4_label *label,
4138 struct inode *inode)
4140 __u32 bitmask[NFS4_BITMASK_SZ];
4141 struct nfs4_getattr_arg args = {
4145 struct nfs4_getattr_res res = {
4150 struct rpc_message msg = {
4151 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4155 unsigned short task_flags = 0;
4157 /* Is this is an attribute revalidation, subject to softreval? */
4158 if (inode && (server->flags & NFS_MOUNT_SOFTREVAL))
4159 task_flags |= RPC_TASK_TIMEOUT;
4161 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode);
4163 nfs_fattr_init(fattr);
4164 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4165 return nfs4_do_call_sync(server->client, server, &msg,
4166 &args.seq_args, &res.seq_res, task_flags);
4169 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 struct nfs4_exception exception = {
4174 .interruptible = true,
4178 err = _nfs4_proc_getattr(server, fhandle, fattr, label, inode);
4179 trace_nfs4_getattr(server, fhandle, fattr, err);
4180 err = nfs4_handle_exception(server, err,
4182 } while (exception.retry);
4187 * The file is not closed if it is opened due to the a request to change
4188 * the size of the file. The open call will not be needed once the
4189 * VFS layer lookup-intents are implemented.
4191 * Close is called when the inode is destroyed.
4192 * If we haven't opened the file for O_WRONLY, we
4193 * need to in the size_change case to obtain a stateid.
4196 * Because OPEN is always done by name in nfsv4, it is
4197 * possible that we opened a different file by the same
4198 * name. We can recognize this race condition, but we
4199 * can't do anything about it besides returning an error.
4201 * This will be fixed with VFS changes (lookup-intent).
4204 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
4205 struct iattr *sattr)
4207 struct inode *inode = d_inode(dentry);
4208 const struct cred *cred = NULL;
4209 struct nfs_open_context *ctx = NULL;
4210 struct nfs4_label *label = NULL;
4213 if (pnfs_ld_layoutret_on_setattr(inode) &&
4214 sattr->ia_valid & ATTR_SIZE &&
4215 sattr->ia_size < i_size_read(inode))
4216 pnfs_commit_and_return_layout(inode);
4218 nfs_fattr_init(fattr);
4220 /* Deal with open(O_TRUNC) */
4221 if (sattr->ia_valid & ATTR_OPEN)
4222 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
4224 /* Optimization: if the end result is no change, don't RPC */
4225 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
4228 /* Search for an existing open(O_WRITE) file */
4229 if (sattr->ia_valid & ATTR_FILE) {
4231 ctx = nfs_file_open_context(sattr->ia_file);
4236 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4238 return PTR_ERR(label);
4240 /* Return any delegations if we're going to change ACLs */
4241 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
4242 nfs4_inode_make_writeable(inode);
4244 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
4246 nfs_setattr_update_inode(inode, sattr, fattr);
4247 nfs_setsecurity(inode, fattr, label);
4249 nfs4_label_free(label);
4253 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
4254 struct dentry *dentry, struct nfs_fh *fhandle,
4255 struct nfs_fattr *fattr, struct nfs4_label *label)
4257 struct nfs_server *server = NFS_SERVER(dir);
4259 struct nfs4_lookup_arg args = {
4260 .bitmask = server->attr_bitmask,
4261 .dir_fh = NFS_FH(dir),
4262 .name = &dentry->d_name,
4264 struct nfs4_lookup_res res = {
4270 struct rpc_message msg = {
4271 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4275 unsigned short task_flags = 0;
4277 /* Is this is an attribute revalidation, subject to softreval? */
4278 if (nfs_lookup_is_soft_revalidate(dentry))
4279 task_flags |= RPC_TASK_TIMEOUT;
4281 args.bitmask = nfs4_bitmask(server, label);
4283 nfs_fattr_init(fattr);
4285 dprintk("NFS call lookup %pd2\n", dentry);
4286 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4287 status = nfs4_do_call_sync(clnt, server, &msg,
4288 &args.seq_args, &res.seq_res, task_flags);
4289 dprintk("NFS reply lookup: %d\n", status);
4293 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
4295 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4296 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
4297 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4301 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
4302 struct dentry *dentry, struct nfs_fh *fhandle,
4303 struct nfs_fattr *fattr, struct nfs4_label *label)
4305 struct nfs4_exception exception = {
4306 .interruptible = true,
4308 struct rpc_clnt *client = *clnt;
4309 const struct qstr *name = &dentry->d_name;
4312 err = _nfs4_proc_lookup(client, dir, dentry, fhandle, fattr, label);
4313 trace_nfs4_lookup(dir, name, err);
4315 case -NFS4ERR_BADNAME:
4318 case -NFS4ERR_MOVED:
4319 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
4320 if (err == -NFS4ERR_MOVED)
4321 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4323 case -NFS4ERR_WRONGSEC:
4325 if (client != *clnt)
4327 client = nfs4_negotiate_security(client, dir, name);
4329 return PTR_ERR(client);
4331 exception.retry = 1;
4334 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4336 } while (exception.retry);
4341 else if (client != *clnt)
4342 rpc_shutdown_client(client);
4347 static int nfs4_proc_lookup(struct inode *dir, struct dentry *dentry,
4348 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4349 struct nfs4_label *label)
4352 struct rpc_clnt *client = NFS_CLIENT(dir);
4354 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr, label);
4355 if (client != NFS_CLIENT(dir)) {
4356 rpc_shutdown_client(client);
4357 nfs_fixup_secinfo_attributes(fattr);
4363 nfs4_proc_lookup_mountpoint(struct inode *dir, struct dentry *dentry,
4364 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4366 struct rpc_clnt *client = NFS_CLIENT(dir);
4369 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr, NULL);
4371 return ERR_PTR(status);
4372 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
4375 static int _nfs4_proc_lookupp(struct inode *inode,
4376 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4377 struct nfs4_label *label)
4379 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4380 struct nfs_server *server = NFS_SERVER(inode);
4382 struct nfs4_lookupp_arg args = {
4383 .bitmask = server->attr_bitmask,
4384 .fh = NFS_FH(inode),
4386 struct nfs4_lookupp_res res = {
4392 struct rpc_message msg = {
4393 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4397 unsigned short task_flags = 0;
4399 if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL)
4400 task_flags |= RPC_TASK_TIMEOUT;
4402 args.bitmask = nfs4_bitmask(server, label);
4404 nfs_fattr_init(fattr);
4406 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4407 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4408 &res.seq_res, task_flags);
4409 dprintk("NFS reply lookupp: %d\n", status);
4413 static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4414 struct nfs_fattr *fattr, struct nfs4_label *label)
4416 struct nfs4_exception exception = {
4417 .interruptible = true,
4421 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4422 trace_nfs4_lookupp(inode, err);
4423 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4425 } while (exception.retry);
4429 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4431 struct nfs_server *server = NFS_SERVER(inode);
4432 struct nfs4_accessargs args = {
4433 .fh = NFS_FH(inode),
4434 .access = entry->mask,
4436 struct nfs4_accessres res = {
4439 struct rpc_message msg = {
4440 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4443 .rpc_cred = entry->cred,
4447 if (!nfs4_have_delegation(inode, FMODE_READ)) {
4448 res.fattr = nfs_alloc_fattr();
4449 if (res.fattr == NULL)
4451 args.bitmask = server->cache_consistency_bitmask;
4453 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4455 nfs_access_set_mask(entry, res.access);
4457 nfs_refresh_inode(inode, res.fattr);
4459 nfs_free_fattr(res.fattr);
4463 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4465 struct nfs4_exception exception = {
4466 .interruptible = true,
4470 err = _nfs4_proc_access(inode, entry);
4471 trace_nfs4_access(inode, err);
4472 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4474 } while (exception.retry);
4479 * TODO: For the time being, we don't try to get any attributes
4480 * along with any of the zero-copy operations READ, READDIR,
4483 * In the case of the first three, we want to put the GETATTR
4484 * after the read-type operation -- this is because it is hard
4485 * to predict the length of a GETATTR response in v4, and thus
4486 * align the READ data correctly. This means that the GETATTR
4487 * may end up partially falling into the page cache, and we should
4488 * shift it into the 'tail' of the xdr_buf before processing.
4489 * To do this efficiently, we need to know the total length
4490 * of data received, which doesn't seem to be available outside
4493 * In the case of WRITE, we also want to put the GETATTR after
4494 * the operation -- in this case because we want to make sure
4495 * we get the post-operation mtime and size.
4497 * Both of these changes to the XDR layer would in fact be quite
4498 * minor, but I decided to leave them for a subsequent patch.
4500 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4501 unsigned int pgbase, unsigned int pglen)
4503 struct nfs4_readlink args = {
4504 .fh = NFS_FH(inode),
4509 struct nfs4_readlink_res res;
4510 struct rpc_message msg = {
4511 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4516 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
4519 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4520 unsigned int pgbase, unsigned int pglen)
4522 struct nfs4_exception exception = {
4523 .interruptible = true,
4527 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4528 trace_nfs4_readlink(inode, err);
4529 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4531 } while (exception.retry);
4536 * This is just for mknod. open(O_CREAT) will always do ->open_context().
4539 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
4542 struct nfs_server *server = NFS_SERVER(dir);
4543 struct nfs4_label l, *ilabel = NULL;
4544 struct nfs_open_context *ctx;
4545 struct nfs4_state *state;
4548 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
4550 return PTR_ERR(ctx);
4552 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4554 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4555 sattr->ia_mode &= ~current_umask();
4556 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
4557 if (IS_ERR(state)) {
4558 status = PTR_ERR(state);
4562 nfs4_label_release_security(ilabel);
4563 put_nfs_open_context(ctx);
4568 _nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
4570 struct nfs_server *server = NFS_SERVER(dir);
4571 struct nfs_removeargs args = {
4575 struct nfs_removeres res = {
4578 struct rpc_message msg = {
4579 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4583 unsigned long timestamp = jiffies;
4586 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
4588 spin_lock(&dir->i_lock);
4589 nfs4_update_changeattr_locked(dir, &res.cinfo, timestamp,
4590 NFS_INO_INVALID_DATA);
4591 /* Removing a directory decrements nlink in the parent */
4592 if (ftype == NF4DIR && dir->i_nlink > 2)
4593 nfs4_dec_nlink_locked(dir);
4594 spin_unlock(&dir->i_lock);
4599 static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4601 struct nfs4_exception exception = {
4602 .interruptible = true,
4604 struct inode *inode = d_inode(dentry);
4608 if (inode->i_nlink == 1)
4609 nfs4_inode_return_delegation(inode);
4611 nfs4_inode_make_writeable(inode);
4614 err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
4615 trace_nfs4_remove(dir, &dentry->d_name, err);
4616 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4618 } while (exception.retry);
4622 static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
4624 struct nfs4_exception exception = {
4625 .interruptible = true,
4630 err = _nfs4_proc_remove(dir, name, NF4DIR);
4631 trace_nfs4_remove(dir, name, err);
4632 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4634 } while (exception.retry);
4638 static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4639 struct dentry *dentry,
4640 struct inode *inode)
4642 struct nfs_removeargs *args = msg->rpc_argp;
4643 struct nfs_removeres *res = msg->rpc_resp;
4645 res->server = NFS_SB(dentry->d_sb);
4646 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
4647 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
4649 nfs_fattr_init(res->dir_attr);
4652 nfs4_inode_return_delegation(inode);
4655 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4657 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
4658 &data->args.seq_args,
4663 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
4665 struct nfs_unlinkdata *data = task->tk_calldata;
4666 struct nfs_removeres *res = &data->res;
4668 if (!nfs4_sequence_done(task, &res->seq_res))
4670 if (nfs4_async_handle_error(task, res->server, NULL,
4671 &data->timeout) == -EAGAIN)
4673 if (task->tk_status == 0)
4674 nfs4_update_changeattr(dir, &res->cinfo,
4675 res->dir_attr->time_start,
4676 NFS_INO_INVALID_DATA);
4680 static void nfs4_proc_rename_setup(struct rpc_message *msg,
4681 struct dentry *old_dentry,
4682 struct dentry *new_dentry)
4684 struct nfs_renameargs *arg = msg->rpc_argp;
4685 struct nfs_renameres *res = msg->rpc_resp;
4686 struct inode *old_inode = d_inode(old_dentry);
4687 struct inode *new_inode = d_inode(new_dentry);
4690 nfs4_inode_make_writeable(old_inode);
4692 nfs4_inode_return_delegation(new_inode);
4693 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
4694 res->server = NFS_SB(old_dentry->d_sb);
4695 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
4698 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4700 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
4701 &data->args.seq_args,
4706 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4707 struct inode *new_dir)
4709 struct nfs_renamedata *data = task->tk_calldata;
4710 struct nfs_renameres *res = &data->res;
4712 if (!nfs4_sequence_done(task, &res->seq_res))
4714 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
4717 if (task->tk_status == 0) {
4718 if (new_dir != old_dir) {
4719 /* Note: If we moved a directory, nlink will change */
4720 nfs4_update_changeattr(old_dir, &res->old_cinfo,
4721 res->old_fattr->time_start,
4722 NFS_INO_INVALID_OTHER |
4723 NFS_INO_INVALID_DATA);
4724 nfs4_update_changeattr(new_dir, &res->new_cinfo,
4725 res->new_fattr->time_start,
4726 NFS_INO_INVALID_OTHER |
4727 NFS_INO_INVALID_DATA);
4729 nfs4_update_changeattr(old_dir, &res->old_cinfo,
4730 res->old_fattr->time_start,
4731 NFS_INO_INVALID_DATA);
4736 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4738 struct nfs_server *server = NFS_SERVER(inode);
4739 __u32 bitmask[NFS4_BITMASK_SZ];
4740 struct nfs4_link_arg arg = {
4741 .fh = NFS_FH(inode),
4742 .dir_fh = NFS_FH(dir),
4746 struct nfs4_link_res res = {
4750 struct rpc_message msg = {
4751 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4755 int status = -ENOMEM;
4757 res.fattr = nfs_alloc_fattr();
4758 if (res.fattr == NULL)
4761 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4762 if (IS_ERR(res.label)) {
4763 status = PTR_ERR(res.label);
4767 nfs4_inode_make_writeable(inode);
4768 nfs4_bitmap_copy_adjust_setattr(bitmask, nfs4_bitmask(server, res.label), inode);
4770 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4772 nfs4_update_changeattr(dir, &res.cinfo, res.fattr->time_start,
4773 NFS_INO_INVALID_DATA);
4774 status = nfs_post_op_update_inode(inode, res.fattr);
4776 nfs_setsecurity(inode, res.fattr, res.label);
4780 nfs4_label_free(res.label);
4783 nfs_free_fattr(res.fattr);
4787 static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4789 struct nfs4_exception exception = {
4790 .interruptible = true,
4794 err = nfs4_handle_exception(NFS_SERVER(inode),
4795 _nfs4_proc_link(inode, dir, name),
4797 } while (exception.retry);
4801 struct nfs4_createdata {
4802 struct rpc_message msg;
4803 struct nfs4_create_arg arg;
4804 struct nfs4_create_res res;
4806 struct nfs_fattr fattr;
4807 struct nfs4_label *label;
4810 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
4811 const struct qstr *name, struct iattr *sattr, u32 ftype)
4813 struct nfs4_createdata *data;
4815 data = kzalloc(sizeof(*data), GFP_KERNEL);
4817 struct nfs_server *server = NFS_SERVER(dir);
4819 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4820 if (IS_ERR(data->label))
4823 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4824 data->msg.rpc_argp = &data->arg;
4825 data->msg.rpc_resp = &data->res;
4826 data->arg.dir_fh = NFS_FH(dir);
4827 data->arg.server = server;
4828 data->arg.name = name;
4829 data->arg.attrs = sattr;
4830 data->arg.ftype = ftype;
4831 data->arg.bitmask = nfs4_bitmask(server, data->label);
4832 data->arg.umask = current_umask();
4833 data->res.server = server;
4834 data->res.fh = &data->fh;
4835 data->res.fattr = &data->fattr;
4836 data->res.label = data->label;
4837 nfs_fattr_init(data->res.fattr);
4845 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4847 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
4848 &data->arg.seq_args, &data->res.seq_res, 1);
4850 spin_lock(&dir->i_lock);
4851 nfs4_update_changeattr_locked(dir, &data->res.dir_cinfo,
4852 data->res.fattr->time_start,
4853 NFS_INO_INVALID_DATA);
4854 /* Creating a directory bumps nlink in the parent */
4855 if (data->arg.ftype == NF4DIR)
4856 nfs4_inc_nlink_locked(dir);
4857 spin_unlock(&dir->i_lock);
4858 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
4863 static void nfs4_free_createdata(struct nfs4_createdata *data)
4865 nfs4_label_free(data->label);
4869 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4870 struct page *page, unsigned int len, struct iattr *sattr,
4871 struct nfs4_label *label)
4873 struct nfs4_createdata *data;
4874 int status = -ENAMETOOLONG;
4876 if (len > NFS4_MAXPATHLEN)
4880 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4884 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4885 data->arg.u.symlink.pages = &page;
4886 data->arg.u.symlink.len = len;
4887 data->arg.label = label;
4889 status = nfs4_do_create(dir, dentry, data);
4891 nfs4_free_createdata(data);
4896 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4897 struct page *page, unsigned int len, struct iattr *sattr)
4899 struct nfs4_exception exception = {
4900 .interruptible = true,
4902 struct nfs4_label l, *label = NULL;
4905 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4908 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4909 trace_nfs4_symlink(dir, &dentry->d_name, err);
4910 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4912 } while (exception.retry);
4914 nfs4_label_release_security(label);
4918 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4919 struct iattr *sattr, struct nfs4_label *label)
4921 struct nfs4_createdata *data;
4922 int status = -ENOMEM;
4924 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4928 data->arg.label = label;
4929 status = nfs4_do_create(dir, dentry, data);
4931 nfs4_free_createdata(data);
4936 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4937 struct iattr *sattr)
4939 struct nfs_server *server = NFS_SERVER(dir);
4940 struct nfs4_exception exception = {
4941 .interruptible = true,
4943 struct nfs4_label l, *label = NULL;
4946 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4948 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4949 sattr->ia_mode &= ~current_umask();
4951 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4952 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4953 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4955 } while (exception.retry);
4956 nfs4_label_release_security(label);
4961 static int _nfs4_proc_readdir(struct nfs_readdir_arg *nr_arg,
4962 struct nfs_readdir_res *nr_res)
4964 struct inode *dir = d_inode(nr_arg->dentry);
4965 struct nfs_server *server = NFS_SERVER(dir);
4966 struct nfs4_readdir_arg args = {
4968 .pages = nr_arg->pages,
4970 .count = nr_arg->page_len,
4971 .plus = nr_arg->plus,
4973 struct nfs4_readdir_res res;
4974 struct rpc_message msg = {
4975 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4978 .rpc_cred = nr_arg->cred,
4982 dprintk("%s: dentry = %pd2, cookie = %llu\n", __func__,
4983 nr_arg->dentry, (unsigned long long)nr_arg->cookie);
4984 if (!(server->caps & NFS_CAP_SECURITY_LABEL))
4985 args.bitmask = server->attr_bitmask_nl;
4987 args.bitmask = server->attr_bitmask;
4989 nfs4_setup_readdir(nr_arg->cookie, nr_arg->verf, nr_arg->dentry, &args);
4990 res.pgbase = args.pgbase;
4991 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
4994 memcpy(nr_res->verf, res.verifier.data, NFS4_VERIFIER_SIZE);
4995 status += args.pgbase;
4998 nfs_invalidate_atime(dir);
5000 dprintk("%s: returns %d\n", __func__, status);
5004 static int nfs4_proc_readdir(struct nfs_readdir_arg *arg,
5005 struct nfs_readdir_res *res)
5007 struct nfs4_exception exception = {
5008 .interruptible = true,
5012 err = _nfs4_proc_readdir(arg, res);
5013 trace_nfs4_readdir(d_inode(arg->dentry), err);
5014 err = nfs4_handle_exception(NFS_SERVER(d_inode(arg->dentry)),
5016 } while (exception.retry);
5020 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5021 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
5023 struct nfs4_createdata *data;
5024 int mode = sattr->ia_mode;
5025 int status = -ENOMEM;
5027 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
5032 data->arg.ftype = NF4FIFO;
5033 else if (S_ISBLK(mode)) {
5034 data->arg.ftype = NF4BLK;
5035 data->arg.u.device.specdata1 = MAJOR(rdev);
5036 data->arg.u.device.specdata2 = MINOR(rdev);
5038 else if (S_ISCHR(mode)) {
5039 data->arg.ftype = NF4CHR;
5040 data->arg.u.device.specdata1 = MAJOR(rdev);
5041 data->arg.u.device.specdata2 = MINOR(rdev);
5042 } else if (!S_ISSOCK(mode)) {
5047 data->arg.label = label;
5048 status = nfs4_do_create(dir, dentry, data);
5050 nfs4_free_createdata(data);
5055 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5056 struct iattr *sattr, dev_t rdev)
5058 struct nfs_server *server = NFS_SERVER(dir);
5059 struct nfs4_exception exception = {
5060 .interruptible = true,
5062 struct nfs4_label l, *label = NULL;
5065 label = nfs4_label_init_security(dir, dentry, sattr, &l);
5067 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
5068 sattr->ia_mode &= ~current_umask();
5070 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
5071 trace_nfs4_mknod(dir, &dentry->d_name, err);
5072 err = nfs4_handle_exception(NFS_SERVER(dir), err,
5074 } while (exception.retry);
5076 nfs4_label_release_security(label);
5081 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
5082 struct nfs_fsstat *fsstat)
5084 struct nfs4_statfs_arg args = {
5086 .bitmask = server->attr_bitmask,
5088 struct nfs4_statfs_res res = {
5091 struct rpc_message msg = {
5092 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
5097 nfs_fattr_init(fsstat->fattr);
5098 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5101 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
5103 struct nfs4_exception exception = {
5104 .interruptible = true,
5108 err = nfs4_handle_exception(server,
5109 _nfs4_proc_statfs(server, fhandle, fsstat),
5111 } while (exception.retry);
5115 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
5116 struct nfs_fsinfo *fsinfo)
5118 struct nfs4_fsinfo_arg args = {
5120 .bitmask = server->attr_bitmask,
5122 struct nfs4_fsinfo_res res = {
5125 struct rpc_message msg = {
5126 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
5131 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5134 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5136 struct nfs4_exception exception = {
5137 .interruptible = true,
5142 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
5143 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
5145 nfs4_set_lease_period(server->nfs_client, fsinfo->lease_time * HZ);
5148 err = nfs4_handle_exception(server, err, &exception);
5149 } while (exception.retry);
5153 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5157 nfs_fattr_init(fsinfo->fattr);
5158 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
5160 /* block layout checks this! */
5161 server->pnfs_blksize = fsinfo->blksize;
5162 set_pnfs_layoutdriver(server, fhandle, fsinfo);
5168 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5169 struct nfs_pathconf *pathconf)
5171 struct nfs4_pathconf_arg args = {
5173 .bitmask = server->attr_bitmask,
5175 struct nfs4_pathconf_res res = {
5176 .pathconf = pathconf,
5178 struct rpc_message msg = {
5179 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
5184 /* None of the pathconf attributes are mandatory to implement */
5185 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
5186 memset(pathconf, 0, sizeof(*pathconf));
5190 nfs_fattr_init(pathconf->fattr);
5191 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5194 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5195 struct nfs_pathconf *pathconf)
5197 struct nfs4_exception exception = {
5198 .interruptible = true,
5203 err = nfs4_handle_exception(server,
5204 _nfs4_proc_pathconf(server, fhandle, pathconf),
5206 } while (exception.retry);
5210 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
5211 const struct nfs_open_context *ctx,
5212 const struct nfs_lock_context *l_ctx,
5215 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
5217 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
5219 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
5220 const struct nfs_open_context *ctx,
5221 const struct nfs_lock_context *l_ctx,
5224 nfs4_stateid _current_stateid;
5226 /* If the current stateid represents a lost lock, then exit */
5227 if (nfs4_set_rw_stateid(&_current_stateid, ctx, l_ctx, fmode) == -EIO)
5229 return nfs4_stateid_match(stateid, &_current_stateid);
5232 static bool nfs4_error_stateid_expired(int err)
5235 case -NFS4ERR_DELEG_REVOKED:
5236 case -NFS4ERR_ADMIN_REVOKED:
5237 case -NFS4ERR_BAD_STATEID:
5238 case -NFS4ERR_STALE_STATEID:
5239 case -NFS4ERR_OLD_STATEID:
5240 case -NFS4ERR_OPENMODE:
5241 case -NFS4ERR_EXPIRED:
5247 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
5249 struct nfs_server *server = NFS_SERVER(hdr->inode);
5251 trace_nfs4_read(hdr, task->tk_status);
5252 if (task->tk_status < 0) {
5253 struct nfs4_exception exception = {
5254 .inode = hdr->inode,
5255 .state = hdr->args.context->state,
5256 .stateid = &hdr->args.stateid,
5258 task->tk_status = nfs4_async_handle_exception(task,
5259 server, task->tk_status, &exception);
5260 if (exception.retry) {
5261 rpc_restart_call_prepare(task);
5266 if (task->tk_status > 0)
5267 renew_lease(server, hdr->timestamp);
5271 static bool nfs4_read_stateid_changed(struct rpc_task *task,
5272 struct nfs_pgio_args *args)
5275 if (!nfs4_error_stateid_expired(task->tk_status) ||
5276 nfs4_stateid_is_current(&args->stateid,
5281 rpc_restart_call_prepare(task);
5285 static bool nfs4_read_plus_not_supported(struct rpc_task *task,
5286 struct nfs_pgio_header *hdr)
5288 struct nfs_server *server = NFS_SERVER(hdr->inode);
5289 struct rpc_message *msg = &task->tk_msg;
5291 if (msg->rpc_proc == &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS] &&
5292 server->caps & NFS_CAP_READ_PLUS && task->tk_status == -ENOTSUPP) {
5293 server->caps &= ~NFS_CAP_READ_PLUS;
5294 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5295 rpc_restart_call_prepare(task);
5301 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5303 dprintk("--> %s\n", __func__);
5305 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5307 if (nfs4_read_stateid_changed(task, &hdr->args))
5309 if (nfs4_read_plus_not_supported(task, hdr))
5311 if (task->tk_status > 0)
5312 nfs_invalidate_atime(hdr->inode);
5313 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5314 nfs4_read_done_cb(task, hdr);
5317 #if defined CONFIG_NFS_V4_2 && defined CONFIG_NFS_V4_2_READ_PLUS
5318 static void nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5319 struct rpc_message *msg)
5321 /* Note: We don't use READ_PLUS with pNFS yet */
5322 if (nfs_server_capable(hdr->inode, NFS_CAP_READ_PLUS) && !hdr->ds_clp)
5323 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS];
5326 static void nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5327 struct rpc_message *msg)
5330 #endif /* CONFIG_NFS_V4_2 */
5332 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5333 struct rpc_message *msg)
5335 hdr->timestamp = jiffies;
5336 if (!hdr->pgio_done_cb)
5337 hdr->pgio_done_cb = nfs4_read_done_cb;
5338 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5339 nfs42_read_plus_support(hdr, msg);
5340 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5343 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5344 struct nfs_pgio_header *hdr)
5346 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
5347 &hdr->args.seq_args,
5351 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5352 hdr->args.lock_context,
5353 hdr->rw_mode) == -EIO)
5355 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
5360 static int nfs4_write_done_cb(struct rpc_task *task,
5361 struct nfs_pgio_header *hdr)
5363 struct inode *inode = hdr->inode;
5365 trace_nfs4_write(hdr, task->tk_status);
5366 if (task->tk_status < 0) {
5367 struct nfs4_exception exception = {
5368 .inode = hdr->inode,
5369 .state = hdr->args.context->state,
5370 .stateid = &hdr->args.stateid,
5372 task->tk_status = nfs4_async_handle_exception(task,
5373 NFS_SERVER(inode), task->tk_status,
5375 if (exception.retry) {
5376 rpc_restart_call_prepare(task);
5380 if (task->tk_status >= 0) {
5381 renew_lease(NFS_SERVER(inode), hdr->timestamp);
5382 nfs_writeback_update_inode(hdr);
5387 static bool nfs4_write_stateid_changed(struct rpc_task *task,
5388 struct nfs_pgio_args *args)
5391 if (!nfs4_error_stateid_expired(task->tk_status) ||
5392 nfs4_stateid_is_current(&args->stateid,
5397 rpc_restart_call_prepare(task);
5401 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5403 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5405 if (nfs4_write_stateid_changed(task, &hdr->args))
5407 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5408 nfs4_write_done_cb(task, hdr);
5412 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
5414 /* Don't request attributes for pNFS or O_DIRECT writes */
5415 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
5417 /* Otherwise, request attributes if and only if we don't hold
5420 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
5423 static void nfs4_bitmask_adjust(__u32 *bitmask, struct inode *inode,
5424 struct nfs_server *server,
5425 struct nfs4_label *label)
5428 unsigned long cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
5430 if ((cache_validity & NFS_INO_INVALID_DATA) ||
5431 (cache_validity & NFS_INO_REVAL_PAGECACHE) ||
5432 (cache_validity & NFS_INO_REVAL_FORCED) ||
5433 (cache_validity & NFS_INO_INVALID_OTHER))
5434 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode);
5436 if (cache_validity & NFS_INO_INVALID_ATIME)
5437 bitmask[1] |= FATTR4_WORD1_TIME_ACCESS;
5438 if (cache_validity & NFS_INO_INVALID_OTHER)
5439 bitmask[1] |= FATTR4_WORD1_MODE | FATTR4_WORD1_OWNER |
5440 FATTR4_WORD1_OWNER_GROUP |
5441 FATTR4_WORD1_NUMLINKS;
5442 if (label && label->len && cache_validity & NFS_INO_INVALID_LABEL)
5443 bitmask[2] |= FATTR4_WORD2_SECURITY_LABEL;
5444 if (cache_validity & NFS_INO_INVALID_CHANGE)
5445 bitmask[0] |= FATTR4_WORD0_CHANGE;
5446 if (cache_validity & NFS_INO_INVALID_CTIME)
5447 bitmask[1] |= FATTR4_WORD1_TIME_METADATA;
5448 if (cache_validity & NFS_INO_INVALID_MTIME)
5449 bitmask[1] |= FATTR4_WORD1_TIME_MODIFY;
5450 if (cache_validity & NFS_INO_INVALID_SIZE)
5451 bitmask[0] |= FATTR4_WORD0_SIZE;
5452 if (cache_validity & NFS_INO_INVALID_BLOCKS)
5453 bitmask[1] |= FATTR4_WORD1_SPACE_USED;
5456 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
5457 struct rpc_message *msg,
5458 struct rpc_clnt **clnt)
5460 struct nfs_server *server = NFS_SERVER(hdr->inode);
5462 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5463 hdr->args.bitmask = NULL;
5464 hdr->res.fattr = NULL;
5466 hdr->args.bitmask = server->cache_consistency_bitmask;
5467 nfs4_bitmask_adjust(hdr->args.bitmask, hdr->inode, server, NULL);
5470 if (!hdr->pgio_done_cb)
5471 hdr->pgio_done_cb = nfs4_write_done_cb;
5472 hdr->res.server = server;
5473 hdr->timestamp = jiffies;
5475 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
5476 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5477 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
5480 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5482 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
5483 &data->args.seq_args,
5488 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
5490 struct inode *inode = data->inode;
5492 trace_nfs4_commit(data, task->tk_status);
5493 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5494 NULL, NULL) == -EAGAIN) {
5495 rpc_restart_call_prepare(task);
5501 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
5503 if (!nfs4_sequence_done(task, &data->res.seq_res))
5505 return data->commit_done_cb(task, data);
5508 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5509 struct rpc_clnt **clnt)
5511 struct nfs_server *server = NFS_SERVER(data->inode);
5513 if (data->commit_done_cb == NULL)
5514 data->commit_done_cb = nfs4_commit_done_cb;
5515 data->res.server = server;
5516 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
5517 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
5518 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
5521 static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5522 struct nfs_commitres *res)
5524 struct inode *dst_inode = file_inode(dst);
5525 struct nfs_server *server = NFS_SERVER(dst_inode);
5526 struct rpc_message msg = {
5527 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5532 args->fh = NFS_FH(dst_inode);
5533 return nfs4_call_sync(server->client, server, &msg,
5534 &args->seq_args, &res->seq_res, 1);
5537 int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5539 struct nfs_commitargs args = {
5543 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5544 struct nfs4_exception exception = { };
5548 status = _nfs4_proc_commit(dst, &args, res);
5549 status = nfs4_handle_exception(dst_server, status, &exception);
5550 } while (exception.retry);
5555 struct nfs4_renewdata {
5556 struct nfs_client *client;
5557 unsigned long timestamp;
5561 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5562 * standalone procedure for queueing an asynchronous RENEW.
5564 static void nfs4_renew_release(void *calldata)
5566 struct nfs4_renewdata *data = calldata;
5567 struct nfs_client *clp = data->client;
5569 if (refcount_read(&clp->cl_count) > 1)
5570 nfs4_schedule_state_renewal(clp);
5571 nfs_put_client(clp);
5575 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
5577 struct nfs4_renewdata *data = calldata;
5578 struct nfs_client *clp = data->client;
5579 unsigned long timestamp = data->timestamp;
5581 trace_nfs4_renew_async(clp, task->tk_status);
5582 switch (task->tk_status) {
5585 case -NFS4ERR_LEASE_MOVED:
5586 nfs4_schedule_lease_moved_recovery(clp);
5589 /* Unless we're shutting down, schedule state recovery! */
5590 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5592 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
5593 nfs4_schedule_lease_recovery(clp);
5596 nfs4_schedule_path_down_recovery(clp);
5598 do_renew_lease(clp, timestamp);
5601 static const struct rpc_call_ops nfs4_renew_ops = {
5602 .rpc_call_done = nfs4_renew_done,
5603 .rpc_release = nfs4_renew_release,
5606 static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
5608 struct rpc_message msg = {
5609 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5613 struct nfs4_renewdata *data;
5615 if (renew_flags == 0)
5617 if (!refcount_inc_not_zero(&clp->cl_count))
5619 data = kmalloc(sizeof(*data), GFP_NOFS);
5621 nfs_put_client(clp);
5625 data->timestamp = jiffies;
5626 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
5627 &nfs4_renew_ops, data);
5630 static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
5632 struct rpc_message msg = {
5633 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5637 unsigned long now = jiffies;
5640 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5643 do_renew_lease(clp, now);
5647 static inline int nfs4_server_supports_acls(struct nfs_server *server)
5649 return server->caps & NFS_CAP_ACLS;
5652 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5653 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
5656 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
5658 int nfs4_buf_to_pages_noslab(const void *buf, size_t buflen,
5659 struct page **pages)
5661 struct page *newpage, **spages;
5667 len = min_t(size_t, PAGE_SIZE, buflen);
5668 newpage = alloc_page(GFP_KERNEL);
5670 if (newpage == NULL)
5672 memcpy(page_address(newpage), buf, len);
5677 } while (buflen != 0);
5683 __free_page(spages[rc-1]);
5687 struct nfs4_cached_acl {
5693 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
5695 struct nfs_inode *nfsi = NFS_I(inode);
5697 spin_lock(&inode->i_lock);
5698 kfree(nfsi->nfs4_acl);
5699 nfsi->nfs4_acl = acl;
5700 spin_unlock(&inode->i_lock);
5703 static void nfs4_zap_acl_attr(struct inode *inode)
5705 nfs4_set_cached_acl(inode, NULL);
5708 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5710 struct nfs_inode *nfsi = NFS_I(inode);
5711 struct nfs4_cached_acl *acl;
5714 spin_lock(&inode->i_lock);
5715 acl = nfsi->nfs4_acl;
5718 if (buf == NULL) /* user is just asking for length */
5720 if (acl->cached == 0)
5722 ret = -ERANGE; /* see getxattr(2) man page */
5723 if (acl->len > buflen)
5725 memcpy(buf, acl->data, acl->len);
5729 spin_unlock(&inode->i_lock);
5733 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
5735 struct nfs4_cached_acl *acl;
5736 size_t buflen = sizeof(*acl) + acl_len;
5738 if (buflen <= PAGE_SIZE) {
5739 acl = kmalloc(buflen, GFP_KERNEL);
5743 _copy_from_pages(acl->data, pages, pgbase, acl_len);
5745 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5752 nfs4_set_cached_acl(inode, acl);
5756 * The getxattr API returns the required buffer length when called with a
5757 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5758 * the required buf. On a NULL buf, we send a page of data to the server
5759 * guessing that the ACL request can be serviced by a page. If so, we cache
5760 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5761 * the cache. If not so, we throw away the page, and cache the required
5762 * length. The next getxattr call will then produce another round trip to
5763 * the server, this time with the input buf of the required size.
5765 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5767 struct page **pages;
5768 struct nfs_getaclargs args = {
5769 .fh = NFS_FH(inode),
5772 struct nfs_getaclres res = {
5775 struct rpc_message msg = {
5776 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5780 unsigned int npages;
5781 int ret = -ENOMEM, i;
5782 struct nfs_server *server = NFS_SERVER(inode);
5785 buflen = server->rsize;
5787 npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
5788 pages = kmalloc_array(npages, sizeof(struct page *), GFP_NOFS);
5792 args.acl_pages = pages;
5794 for (i = 0; i < npages; i++) {
5795 pages[i] = alloc_page(GFP_KERNEL);
5800 /* for decoding across pages */
5801 res.acl_scratch = alloc_page(GFP_KERNEL);
5802 if (!res.acl_scratch)
5805 args.acl_len = npages * PAGE_SIZE;
5807 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
5808 __func__, buf, buflen, npages, args.acl_len);
5809 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5810 &msg, &args.seq_args, &res.seq_res, 0);
5814 /* Handle the case where the passed-in buffer is too short */
5815 if (res.acl_flags & NFS4_ACL_TRUNC) {
5816 /* Did the user only issue a request for the acl length? */
5822 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
5824 if (res.acl_len > buflen) {
5828 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
5833 for (i = 0; i < npages; i++)
5835 __free_page(pages[i]);
5836 if (res.acl_scratch)
5837 __free_page(res.acl_scratch);
5842 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5844 struct nfs4_exception exception = {
5845 .interruptible = true,
5849 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
5850 trace_nfs4_get_acl(inode, ret);
5853 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5854 } while (exception.retry);
5858 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5860 struct nfs_server *server = NFS_SERVER(inode);
5863 if (!nfs4_server_supports_acls(server))
5865 ret = nfs_revalidate_inode(server, inode);
5868 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5869 nfs_zap_acl_cache(inode);
5870 ret = nfs4_read_cached_acl(inode, buf, buflen);
5872 /* -ENOENT is returned if there is no ACL or if there is an ACL
5873 * but no cached acl data, just the acl length */
5875 return nfs4_get_acl_uncached(inode, buf, buflen);
5878 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5880 struct nfs_server *server = NFS_SERVER(inode);
5881 struct page *pages[NFS4ACL_MAXPAGES];
5882 struct nfs_setaclargs arg = {
5883 .fh = NFS_FH(inode),
5887 struct nfs_setaclres res;
5888 struct rpc_message msg = {
5889 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5893 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
5896 if (!nfs4_server_supports_acls(server))
5898 if (npages > ARRAY_SIZE(pages))
5900 i = nfs4_buf_to_pages_noslab(buf, buflen, arg.acl_pages);
5903 nfs4_inode_make_writeable(inode);
5904 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5907 * Free each page after tx, so the only ref left is
5908 * held by the network stack
5911 put_page(pages[i-1]);
5914 * Acl update can result in inode attribute update.
5915 * so mark the attribute cache invalid.
5917 spin_lock(&inode->i_lock);
5918 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
5919 | NFS_INO_INVALID_CTIME
5920 | NFS_INO_REVAL_FORCED;
5921 spin_unlock(&inode->i_lock);
5922 nfs_access_zap_cache(inode);
5923 nfs_zap_acl_cache(inode);
5927 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5929 struct nfs4_exception exception = { };
5932 err = __nfs4_proc_set_acl(inode, buf, buflen);
5933 trace_nfs4_set_acl(inode, err);
5934 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5936 } while (exception.retry);
5940 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
5941 static int _nfs4_get_security_label(struct inode *inode, void *buf,
5944 struct nfs_server *server = NFS_SERVER(inode);
5945 struct nfs_fattr fattr;
5946 struct nfs4_label label = {0, 0, buflen, buf};
5948 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5949 struct nfs4_getattr_arg arg = {
5950 .fh = NFS_FH(inode),
5953 struct nfs4_getattr_res res = {
5958 struct rpc_message msg = {
5959 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
5965 nfs_fattr_init(&fattr);
5967 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
5970 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5975 static int nfs4_get_security_label(struct inode *inode, void *buf,
5978 struct nfs4_exception exception = {
5979 .interruptible = true,
5983 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5987 err = _nfs4_get_security_label(inode, buf, buflen);
5988 trace_nfs4_get_security_label(inode, err);
5989 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5991 } while (exception.retry);
5995 static int _nfs4_do_set_security_label(struct inode *inode,
5996 struct nfs4_label *ilabel,
5997 struct nfs_fattr *fattr,
5998 struct nfs4_label *olabel)
6001 struct iattr sattr = {0};
6002 struct nfs_server *server = NFS_SERVER(inode);
6003 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
6004 struct nfs_setattrargs arg = {
6005 .fh = NFS_FH(inode),
6011 struct nfs_setattrres res = {
6016 struct rpc_message msg = {
6017 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
6023 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
6025 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6027 dprintk("%s failed: %d\n", __func__, status);
6032 static int nfs4_do_set_security_label(struct inode *inode,
6033 struct nfs4_label *ilabel,
6034 struct nfs_fattr *fattr,
6035 struct nfs4_label *olabel)
6037 struct nfs4_exception exception = { };
6041 err = _nfs4_do_set_security_label(inode, ilabel,
6043 trace_nfs4_set_security_label(inode, err);
6044 err = nfs4_handle_exception(NFS_SERVER(inode), err,
6046 } while (exception.retry);
6051 nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
6053 struct nfs4_label ilabel, *olabel = NULL;
6054 struct nfs_fattr fattr;
6057 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
6060 nfs_fattr_init(&fattr);
6064 ilabel.label = (char *)buf;
6065 ilabel.len = buflen;
6067 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
6068 if (IS_ERR(olabel)) {
6069 status = -PTR_ERR(olabel);
6073 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
6075 nfs_setsecurity(inode, &fattr, olabel);
6077 nfs4_label_free(olabel);
6081 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
6084 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
6085 nfs4_verifier *bootverf)
6089 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
6090 /* An impossible timestamp guarantees this value
6091 * will never match a generated boot time. */
6092 verf[0] = cpu_to_be32(U32_MAX);
6093 verf[1] = cpu_to_be32(U32_MAX);
6095 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
6096 u64 ns = ktime_to_ns(nn->boot_time);
6098 verf[0] = cpu_to_be32(ns >> 32);
6099 verf[1] = cpu_to_be32(ns);
6101 memcpy(bootverf->data, verf, sizeof(bootverf->data));
6105 nfs4_get_uniquifier(struct nfs_client *clp, char *buf, size_t buflen)
6107 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
6108 struct nfs_netns_client *nn_clp = nn->nfs_client;
6115 id = rcu_dereference(nn_clp->identifier);
6117 strscpy(buf, id, buflen);
6121 if (nfs4_client_id_uniquifier[0] != '\0' && buf[0] == '\0')
6122 strscpy(buf, nfs4_client_id_uniquifier, buflen);
6128 nfs4_init_nonuniform_client_string(struct nfs_client *clp)
6130 char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6135 if (clp->cl_owner_id != NULL)
6140 strlen(clp->cl_rpcclient->cl_nodename) +
6142 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
6146 buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
6150 if (len > NFS4_OPAQUE_LIMIT + 1)
6154 * Since this string is allocated at mount time, and held until the
6155 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6156 * about a memory-reclaim deadlock.
6158 str = kmalloc(len, GFP_KERNEL);
6164 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
6165 clp->cl_rpcclient->cl_nodename, buf,
6166 rpc_peeraddr2str(clp->cl_rpcclient,
6169 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
6170 clp->cl_rpcclient->cl_nodename,
6171 rpc_peeraddr2str(clp->cl_rpcclient,
6175 clp->cl_owner_id = str;
6180 nfs4_init_uniform_client_string(struct nfs_client *clp)
6182 char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6187 if (clp->cl_owner_id != NULL)
6190 len = 10 + 10 + 1 + 10 + 1 +
6191 strlen(clp->cl_rpcclient->cl_nodename) + 1;
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);
6210 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
6211 clp->rpc_ops->version, clp->cl_minorversion,
6212 buf, clp->cl_rpcclient->cl_nodename);
6214 scnprintf(str, len, "Linux NFSv%u.%u %s",
6215 clp->rpc_ops->version, clp->cl_minorversion,
6216 clp->cl_rpcclient->cl_nodename);
6217 clp->cl_owner_id = str;
6222 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
6223 * services. Advertise one based on the address family of the
6227 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
6229 if (strchr(clp->cl_ipaddr, ':') != NULL)
6230 return scnprintf(buf, len, "tcp6");
6232 return scnprintf(buf, len, "tcp");
6235 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
6237 struct nfs4_setclientid *sc = calldata;
6239 if (task->tk_status == 0)
6240 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
6243 static const struct rpc_call_ops nfs4_setclientid_ops = {
6244 .rpc_call_done = nfs4_setclientid_done,
6248 * nfs4_proc_setclientid - Negotiate client ID
6249 * @clp: state data structure
6250 * @program: RPC program for NFSv4 callback service
6251 * @port: IP port number for NFS4 callback service
6252 * @cred: credential to use for this call
6253 * @res: where to place the result
6255 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6257 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
6258 unsigned short port, const struct cred *cred,
6259 struct nfs4_setclientid_res *res)
6261 nfs4_verifier sc_verifier;
6262 struct nfs4_setclientid setclientid = {
6263 .sc_verifier = &sc_verifier,
6267 struct rpc_message msg = {
6268 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
6269 .rpc_argp = &setclientid,
6273 struct rpc_task_setup task_setup_data = {
6274 .rpc_client = clp->cl_rpcclient,
6275 .rpc_message = &msg,
6276 .callback_ops = &nfs4_setclientid_ops,
6277 .callback_data = &setclientid,
6278 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
6280 unsigned long now = jiffies;
6283 /* nfs_client_id4 */
6284 nfs4_init_boot_verifier(clp, &sc_verifier);
6286 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
6287 status = nfs4_init_uniform_client_string(clp);
6289 status = nfs4_init_nonuniform_client_string(clp);
6295 setclientid.sc_netid_len =
6296 nfs4_init_callback_netid(clp,
6297 setclientid.sc_netid,
6298 sizeof(setclientid.sc_netid));
6299 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
6300 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
6301 clp->cl_ipaddr, port >> 8, port & 255);
6303 dprintk("NFS call setclientid auth=%s, '%s'\n",
6304 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6307 status = nfs4_call_sync_custom(&task_setup_data);
6308 if (setclientid.sc_cred) {
6309 kfree(clp->cl_acceptor);
6310 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
6311 put_rpccred(setclientid.sc_cred);
6315 do_renew_lease(clp, now);
6317 trace_nfs4_setclientid(clp, status);
6318 dprintk("NFS reply setclientid: %d\n", status);
6323 * nfs4_proc_setclientid_confirm - Confirm client ID
6324 * @clp: state data structure
6325 * @arg: result of a previous SETCLIENTID
6326 * @cred: credential to use for this call
6328 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6330 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
6331 struct nfs4_setclientid_res *arg,
6332 const struct cred *cred)
6334 struct rpc_message msg = {
6335 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
6341 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
6342 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6344 status = rpc_call_sync(clp->cl_rpcclient, &msg,
6345 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
6346 trace_nfs4_setclientid_confirm(clp, status);
6347 dprintk("NFS reply setclientid_confirm: %d\n", status);
6351 struct nfs4_delegreturndata {
6352 struct nfs4_delegreturnargs args;
6353 struct nfs4_delegreturnres res;
6355 nfs4_stateid stateid;
6356 unsigned long timestamp;
6358 struct nfs4_layoutreturn_args arg;
6359 struct nfs4_layoutreturn_res res;
6360 struct nfs4_xdr_opaque_data ld_private;
6364 struct nfs_fattr fattr;
6366 struct inode *inode;
6369 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6371 struct nfs4_delegreturndata *data = calldata;
6372 struct nfs4_exception exception = {
6373 .inode = data->inode,
6374 .stateid = &data->stateid,
6377 if (!nfs4_sequence_done(task, &data->res.seq_res))
6380 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
6382 /* Handle Layoutreturn errors */
6383 if (pnfs_roc_done(task, &data->args.lr_args, &data->res.lr_res,
6384 &data->res.lr_ret) == -EAGAIN)
6387 switch (task->tk_status) {
6389 renew_lease(data->res.server, data->timestamp);
6391 case -NFS4ERR_ADMIN_REVOKED:
6392 case -NFS4ERR_DELEG_REVOKED:
6393 case -NFS4ERR_EXPIRED:
6394 nfs4_free_revoked_stateid(data->res.server,
6396 task->tk_msg.rpc_cred);
6398 case -NFS4ERR_BAD_STATEID:
6399 case -NFS4ERR_STALE_STATEID:
6401 task->tk_status = 0;
6403 case -NFS4ERR_OLD_STATEID:
6404 if (!nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6405 nfs4_stateid_seqid_inc(&data->stateid);
6406 if (data->args.bitmask) {
6407 data->args.bitmask = NULL;
6408 data->res.fattr = NULL;
6411 case -NFS4ERR_ACCESS:
6412 if (data->args.bitmask) {
6413 data->args.bitmask = NULL;
6414 data->res.fattr = NULL;
6419 task->tk_status = nfs4_async_handle_exception(task,
6420 data->res.server, task->tk_status,
6422 if (exception.retry)
6425 nfs_delegation_mark_returned(data->inode, data->args.stateid);
6426 data->rpc_status = task->tk_status;
6429 task->tk_status = 0;
6430 rpc_restart_call_prepare(task);
6433 static void nfs4_delegreturn_release(void *calldata)
6435 struct nfs4_delegreturndata *data = calldata;
6436 struct inode *inode = data->inode;
6439 pnfs_roc_release(&data->lr.arg, &data->lr.res,
6442 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
6443 nfs_iput_and_deactive(inode);
6448 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6450 struct nfs4_delegreturndata *d_data;
6451 struct pnfs_layout_hdr *lo;
6453 d_data = (struct nfs4_delegreturndata *)data;
6455 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task)) {
6456 nfs4_sequence_done(task, &d_data->res.seq_res);
6460 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6461 if (lo && !pnfs_layout_is_valid(lo)) {
6462 d_data->args.lr_args = NULL;
6463 d_data->res.lr_res = NULL;
6466 nfs4_setup_sequence(d_data->res.server->nfs_client,
6467 &d_data->args.seq_args,
6468 &d_data->res.seq_res,
6472 static const struct rpc_call_ops nfs4_delegreturn_ops = {
6473 .rpc_call_prepare = nfs4_delegreturn_prepare,
6474 .rpc_call_done = nfs4_delegreturn_done,
6475 .rpc_release = nfs4_delegreturn_release,
6478 static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6480 struct nfs4_delegreturndata *data;
6481 struct nfs_server *server = NFS_SERVER(inode);
6482 struct rpc_task *task;
6483 struct rpc_message msg = {
6484 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6487 struct rpc_task_setup task_setup_data = {
6488 .rpc_client = server->client,
6489 .rpc_message = &msg,
6490 .callback_ops = &nfs4_delegreturn_ops,
6491 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
6495 data = kzalloc(sizeof(*data), GFP_NOFS);
6498 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
6500 nfs4_state_protect(server->nfs_client,
6501 NFS_SP4_MACH_CRED_CLEANUP,
6502 &task_setup_data.rpc_client, &msg);
6504 data->args.fhandle = &data->fh;
6505 data->args.stateid = &data->stateid;
6506 data->args.bitmask = server->cache_consistency_bitmask;
6507 nfs4_bitmask_adjust(data->args.bitmask, inode, server, NULL);
6508 nfs_copy_fh(&data->fh, NFS_FH(inode));
6509 nfs4_stateid_copy(&data->stateid, stateid);
6510 data->res.fattr = &data->fattr;
6511 data->res.server = server;
6512 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
6513 data->lr.arg.ld_private = &data->lr.ld_private;
6514 nfs_fattr_init(data->res.fattr);
6515 data->timestamp = jiffies;
6516 data->rpc_status = 0;
6517 data->inode = nfs_igrab_and_active(inode);
6518 if (data->inode || issync) {
6519 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res,
6522 data->args.lr_args = &data->lr.arg;
6523 data->res.lr_res = &data->lr.res;
6527 task_setup_data.callback_data = data;
6528 msg.rpc_argp = &data->args;
6529 msg.rpc_resp = &data->res;
6530 task = rpc_run_task(&task_setup_data);
6532 return PTR_ERR(task);
6535 status = rpc_wait_for_completion_task(task);
6538 status = data->rpc_status;
6544 int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6546 struct nfs_server *server = NFS_SERVER(inode);
6547 struct nfs4_exception exception = { };
6550 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
6551 trace_nfs4_delegreturn(inode, stateid, err);
6553 case -NFS4ERR_STALE_STATEID:
6554 case -NFS4ERR_EXPIRED:
6558 err = nfs4_handle_exception(server, err, &exception);
6559 } while (exception.retry);
6563 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6565 struct inode *inode = state->inode;
6566 struct nfs_server *server = NFS_SERVER(inode);
6567 struct nfs_client *clp = server->nfs_client;
6568 struct nfs_lockt_args arg = {
6569 .fh = NFS_FH(inode),
6572 struct nfs_lockt_res res = {
6575 struct rpc_message msg = {
6576 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
6579 .rpc_cred = state->owner->so_cred,
6581 struct nfs4_lock_state *lsp;
6584 arg.lock_owner.clientid = clp->cl_clientid;
6585 status = nfs4_set_lock_state(state, request);
6588 lsp = request->fl_u.nfs4_fl.owner;
6589 arg.lock_owner.id = lsp->ls_seqid.owner_id;
6590 arg.lock_owner.s_dev = server->s_dev;
6591 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6594 request->fl_type = F_UNLCK;
6596 case -NFS4ERR_DENIED:
6599 request->fl_ops->fl_release_private(request);
6600 request->fl_ops = NULL;
6605 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6607 struct nfs4_exception exception = {
6608 .interruptible = true,
6613 err = _nfs4_proc_getlk(state, cmd, request);
6614 trace_nfs4_get_lock(request, state, cmd, err);
6615 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
6617 } while (exception.retry);
6622 * Update the seqid of a lock stateid after receiving
6623 * NFS4ERR_OLD_STATEID
6625 static bool nfs4_refresh_lock_old_stateid(nfs4_stateid *dst,
6626 struct nfs4_lock_state *lsp)
6628 struct nfs4_state *state = lsp->ls_state;
6631 spin_lock(&state->state_lock);
6632 if (!nfs4_stateid_match_other(dst, &lsp->ls_stateid))
6634 if (!nfs4_stateid_is_newer(&lsp->ls_stateid, dst))
6635 nfs4_stateid_seqid_inc(dst);
6637 dst->seqid = lsp->ls_stateid.seqid;
6640 spin_unlock(&state->state_lock);
6644 static bool nfs4_sync_lock_stateid(nfs4_stateid *dst,
6645 struct nfs4_lock_state *lsp)
6647 struct nfs4_state *state = lsp->ls_state;
6650 spin_lock(&state->state_lock);
6651 ret = !nfs4_stateid_match_other(dst, &lsp->ls_stateid);
6652 nfs4_stateid_copy(dst, &lsp->ls_stateid);
6653 spin_unlock(&state->state_lock);
6657 struct nfs4_unlockdata {
6658 struct nfs_locku_args arg;
6659 struct nfs_locku_res res;
6660 struct nfs4_lock_state *lsp;
6661 struct nfs_open_context *ctx;
6662 struct nfs_lock_context *l_ctx;
6663 struct file_lock fl;
6664 struct nfs_server *server;
6665 unsigned long timestamp;
6668 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6669 struct nfs_open_context *ctx,
6670 struct nfs4_lock_state *lsp,
6671 struct nfs_seqid *seqid)
6673 struct nfs4_unlockdata *p;
6674 struct nfs4_state *state = lsp->ls_state;
6675 struct inode *inode = state->inode;
6677 p = kzalloc(sizeof(*p), GFP_NOFS);
6680 p->arg.fh = NFS_FH(inode);
6682 p->arg.seqid = seqid;
6683 p->res.seqid = seqid;
6685 /* Ensure we don't close file until we're done freeing locks! */
6686 p->ctx = get_nfs_open_context(ctx);
6687 p->l_ctx = nfs_get_lock_context(ctx);
6688 locks_init_lock(&p->fl);
6689 locks_copy_lock(&p->fl, fl);
6690 p->server = NFS_SERVER(inode);
6691 spin_lock(&state->state_lock);
6692 nfs4_stateid_copy(&p->arg.stateid, &lsp->ls_stateid);
6693 spin_unlock(&state->state_lock);
6697 static void nfs4_locku_release_calldata(void *data)
6699 struct nfs4_unlockdata *calldata = data;
6700 nfs_free_seqid(calldata->arg.seqid);
6701 nfs4_put_lock_state(calldata->lsp);
6702 nfs_put_lock_context(calldata->l_ctx);
6703 put_nfs_open_context(calldata->ctx);
6707 static void nfs4_locku_done(struct rpc_task *task, void *data)
6709 struct nfs4_unlockdata *calldata = data;
6710 struct nfs4_exception exception = {
6711 .inode = calldata->lsp->ls_state->inode,
6712 .stateid = &calldata->arg.stateid,
6715 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6717 switch (task->tk_status) {
6719 renew_lease(calldata->server, calldata->timestamp);
6720 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
6721 if (nfs4_update_lock_stateid(calldata->lsp,
6722 &calldata->res.stateid))
6725 case -NFS4ERR_ADMIN_REVOKED:
6726 case -NFS4ERR_EXPIRED:
6727 nfs4_free_revoked_stateid(calldata->server,
6728 &calldata->arg.stateid,
6729 task->tk_msg.rpc_cred);
6731 case -NFS4ERR_BAD_STATEID:
6732 case -NFS4ERR_STALE_STATEID:
6733 if (nfs4_sync_lock_stateid(&calldata->arg.stateid,
6735 rpc_restart_call_prepare(task);
6737 case -NFS4ERR_OLD_STATEID:
6738 if (nfs4_refresh_lock_old_stateid(&calldata->arg.stateid,
6740 rpc_restart_call_prepare(task);
6743 task->tk_status = nfs4_async_handle_exception(task,
6744 calldata->server, task->tk_status,
6746 if (exception.retry)
6747 rpc_restart_call_prepare(task);
6749 nfs_release_seqid(calldata->arg.seqid);
6752 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
6754 struct nfs4_unlockdata *calldata = data;
6756 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6757 nfs_async_iocounter_wait(task, calldata->l_ctx))
6760 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
6762 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
6763 /* Note: exit _without_ running nfs4_locku_done */
6766 calldata->timestamp = jiffies;
6767 if (nfs4_setup_sequence(calldata->server->nfs_client,
6768 &calldata->arg.seq_args,
6769 &calldata->res.seq_res,
6771 nfs_release_seqid(calldata->arg.seqid);
6774 task->tk_action = NULL;
6776 nfs4_sequence_done(task, &calldata->res.seq_res);
6779 static const struct rpc_call_ops nfs4_locku_ops = {
6780 .rpc_call_prepare = nfs4_locku_prepare,
6781 .rpc_call_done = nfs4_locku_done,
6782 .rpc_release = nfs4_locku_release_calldata,
6785 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6786 struct nfs_open_context *ctx,
6787 struct nfs4_lock_state *lsp,
6788 struct nfs_seqid *seqid)
6790 struct nfs4_unlockdata *data;
6791 struct rpc_message msg = {
6792 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6793 .rpc_cred = ctx->cred,
6795 struct rpc_task_setup task_setup_data = {
6796 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
6797 .rpc_message = &msg,
6798 .callback_ops = &nfs4_locku_ops,
6799 .workqueue = nfsiod_workqueue,
6800 .flags = RPC_TASK_ASYNC,
6803 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6804 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6806 /* Ensure this is an unlock - when canceling a lock, the
6807 * canceled lock is passed in, and it won't be an unlock.
6809 fl->fl_type = F_UNLCK;
6810 if (fl->fl_flags & FL_CLOSE)
6811 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
6813 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6815 nfs_free_seqid(seqid);
6816 return ERR_PTR(-ENOMEM);
6819 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
6820 msg.rpc_argp = &data->arg;
6821 msg.rpc_resp = &data->res;
6822 task_setup_data.callback_data = data;
6823 return rpc_run_task(&task_setup_data);
6826 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6828 struct inode *inode = state->inode;
6829 struct nfs4_state_owner *sp = state->owner;
6830 struct nfs_inode *nfsi = NFS_I(inode);
6831 struct nfs_seqid *seqid;
6832 struct nfs4_lock_state *lsp;
6833 struct rpc_task *task;
6834 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6836 unsigned char fl_flags = request->fl_flags;
6838 status = nfs4_set_lock_state(state, request);
6839 /* Unlock _before_ we do the RPC call */
6840 request->fl_flags |= FL_EXISTS;
6841 /* Exclude nfs_delegation_claim_locks() */
6842 mutex_lock(&sp->so_delegreturn_mutex);
6843 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
6844 down_read(&nfsi->rwsem);
6845 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
6846 up_read(&nfsi->rwsem);
6847 mutex_unlock(&sp->so_delegreturn_mutex);
6850 up_read(&nfsi->rwsem);
6851 mutex_unlock(&sp->so_delegreturn_mutex);
6854 /* Is this a delegated lock? */
6855 lsp = request->fl_u.nfs4_fl.owner;
6856 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6858 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6859 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
6863 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
6864 status = PTR_ERR(task);
6867 status = rpc_wait_for_completion_task(task);
6870 request->fl_flags = fl_flags;
6871 trace_nfs4_unlock(request, state, F_SETLK, status);
6875 struct nfs4_lockdata {
6876 struct nfs_lock_args arg;
6877 struct nfs_lock_res res;
6878 struct nfs4_lock_state *lsp;
6879 struct nfs_open_context *ctx;
6880 struct file_lock fl;
6881 unsigned long timestamp;
6884 struct nfs_server *server;
6887 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
6888 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6891 struct nfs4_lockdata *p;
6892 struct inode *inode = lsp->ls_state->inode;
6893 struct nfs_server *server = NFS_SERVER(inode);
6894 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6896 p = kzalloc(sizeof(*p), gfp_mask);
6900 p->arg.fh = NFS_FH(inode);
6902 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
6903 if (IS_ERR(p->arg.open_seqid))
6905 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6906 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
6907 if (IS_ERR(p->arg.lock_seqid))
6908 goto out_free_seqid;
6909 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
6910 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
6911 p->arg.lock_owner.s_dev = server->s_dev;
6912 p->res.lock_seqid = p->arg.lock_seqid;
6915 p->ctx = get_nfs_open_context(ctx);
6916 locks_init_lock(&p->fl);
6917 locks_copy_lock(&p->fl, fl);
6920 nfs_free_seqid(p->arg.open_seqid);
6926 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6928 struct nfs4_lockdata *data = calldata;
6929 struct nfs4_state *state = data->lsp->ls_state;
6931 dprintk("%s: begin!\n", __func__);
6932 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
6934 /* Do we need to do an open_to_lock_owner? */
6935 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
6936 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
6937 goto out_release_lock_seqid;
6939 nfs4_stateid_copy(&data->arg.open_stateid,
6940 &state->open_stateid);
6941 data->arg.new_lock_owner = 1;
6942 data->res.open_seqid = data->arg.open_seqid;
6944 data->arg.new_lock_owner = 0;
6945 nfs4_stateid_copy(&data->arg.lock_stateid,
6946 &data->lsp->ls_stateid);
6948 if (!nfs4_valid_open_stateid(state)) {
6949 data->rpc_status = -EBADF;
6950 task->tk_action = NULL;
6951 goto out_release_open_seqid;
6953 data->timestamp = jiffies;
6954 if (nfs4_setup_sequence(data->server->nfs_client,
6955 &data->arg.seq_args,
6959 out_release_open_seqid:
6960 nfs_release_seqid(data->arg.open_seqid);
6961 out_release_lock_seqid:
6962 nfs_release_seqid(data->arg.lock_seqid);
6964 nfs4_sequence_done(task, &data->res.seq_res);
6965 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
6968 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6970 struct nfs4_lockdata *data = calldata;
6971 struct nfs4_lock_state *lsp = data->lsp;
6973 dprintk("%s: begin!\n", __func__);
6975 if (!nfs4_sequence_done(task, &data->res.seq_res))
6978 data->rpc_status = task->tk_status;
6979 switch (task->tk_status) {
6981 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
6983 if (data->arg.new_lock && !data->cancelled) {
6984 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
6985 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
6988 if (data->arg.new_lock_owner != 0) {
6989 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6990 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6991 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6992 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6995 case -NFS4ERR_BAD_STATEID:
6996 case -NFS4ERR_OLD_STATEID:
6997 case -NFS4ERR_STALE_STATEID:
6998 case -NFS4ERR_EXPIRED:
6999 if (data->arg.new_lock_owner != 0) {
7000 if (!nfs4_stateid_match(&data->arg.open_stateid,
7001 &lsp->ls_state->open_stateid))
7003 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
7008 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
7011 if (!data->cancelled)
7012 rpc_restart_call_prepare(task);
7016 static void nfs4_lock_release(void *calldata)
7018 struct nfs4_lockdata *data = calldata;
7020 dprintk("%s: begin!\n", __func__);
7021 nfs_free_seqid(data->arg.open_seqid);
7022 if (data->cancelled && data->rpc_status == 0) {
7023 struct rpc_task *task;
7024 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
7025 data->arg.lock_seqid);
7027 rpc_put_task_async(task);
7028 dprintk("%s: cancelling lock!\n", __func__);
7030 nfs_free_seqid(data->arg.lock_seqid);
7031 nfs4_put_lock_state(data->lsp);
7032 put_nfs_open_context(data->ctx);
7034 dprintk("%s: done!\n", __func__);
7037 static const struct rpc_call_ops nfs4_lock_ops = {
7038 .rpc_call_prepare = nfs4_lock_prepare,
7039 .rpc_call_done = nfs4_lock_done,
7040 .rpc_release = nfs4_lock_release,
7043 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
7046 case -NFS4ERR_ADMIN_REVOKED:
7047 case -NFS4ERR_EXPIRED:
7048 case -NFS4ERR_BAD_STATEID:
7049 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
7050 if (new_lock_owner != 0 ||
7051 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
7052 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
7054 case -NFS4ERR_STALE_STATEID:
7055 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
7056 nfs4_schedule_lease_recovery(server->nfs_client);
7060 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
7062 struct nfs4_lockdata *data;
7063 struct rpc_task *task;
7064 struct rpc_message msg = {
7065 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
7066 .rpc_cred = state->owner->so_cred,
7068 struct rpc_task_setup task_setup_data = {
7069 .rpc_client = NFS_CLIENT(state->inode),
7070 .rpc_message = &msg,
7071 .callback_ops = &nfs4_lock_ops,
7072 .workqueue = nfsiod_workqueue,
7073 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
7077 dprintk("%s: begin!\n", __func__);
7078 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
7079 fl->fl_u.nfs4_fl.owner,
7080 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
7084 data->arg.block = 1;
7085 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
7086 recovery_type > NFS_LOCK_NEW);
7087 msg.rpc_argp = &data->arg;
7088 msg.rpc_resp = &data->res;
7089 task_setup_data.callback_data = data;
7090 if (recovery_type > NFS_LOCK_NEW) {
7091 if (recovery_type == NFS_LOCK_RECLAIM)
7092 data->arg.reclaim = NFS_LOCK_RECLAIM;
7094 data->arg.new_lock = 1;
7095 task = rpc_run_task(&task_setup_data);
7097 return PTR_ERR(task);
7098 ret = rpc_wait_for_completion_task(task);
7100 ret = data->rpc_status;
7102 nfs4_handle_setlk_error(data->server, data->lsp,
7103 data->arg.new_lock_owner, ret);
7105 data->cancelled = true;
7106 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
7108 dprintk("%s: done, ret = %d!\n", __func__, ret);
7112 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
7114 struct nfs_server *server = NFS_SERVER(state->inode);
7115 struct nfs4_exception exception = {
7116 .inode = state->inode,
7121 /* Cache the lock if possible... */
7122 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7124 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
7125 if (err != -NFS4ERR_DELAY)
7127 nfs4_handle_exception(server, err, &exception);
7128 } while (exception.retry);
7132 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
7134 struct nfs_server *server = NFS_SERVER(state->inode);
7135 struct nfs4_exception exception = {
7136 .inode = state->inode,
7140 err = nfs4_set_lock_state(state, request);
7143 if (!recover_lost_locks) {
7144 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
7148 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7150 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
7154 case -NFS4ERR_GRACE:
7155 case -NFS4ERR_DELAY:
7156 nfs4_handle_exception(server, err, &exception);
7159 } while (exception.retry);
7164 #if defined(CONFIG_NFS_V4_1)
7165 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
7167 struct nfs4_lock_state *lsp;
7170 status = nfs4_set_lock_state(state, request);
7173 lsp = request->fl_u.nfs4_fl.owner;
7174 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
7175 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
7177 return nfs4_lock_expired(state, request);
7181 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7183 struct nfs_inode *nfsi = NFS_I(state->inode);
7184 struct nfs4_state_owner *sp = state->owner;
7185 unsigned char fl_flags = request->fl_flags;
7188 request->fl_flags |= FL_ACCESS;
7189 status = locks_lock_inode_wait(state->inode, request);
7192 mutex_lock(&sp->so_delegreturn_mutex);
7193 down_read(&nfsi->rwsem);
7194 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
7195 /* Yes: cache locks! */
7196 /* ...but avoid races with delegation recall... */
7197 request->fl_flags = fl_flags & ~FL_SLEEP;
7198 status = locks_lock_inode_wait(state->inode, request);
7199 up_read(&nfsi->rwsem);
7200 mutex_unlock(&sp->so_delegreturn_mutex);
7203 up_read(&nfsi->rwsem);
7204 mutex_unlock(&sp->so_delegreturn_mutex);
7205 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
7207 request->fl_flags = fl_flags;
7211 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7213 struct nfs4_exception exception = {
7215 .inode = state->inode,
7216 .interruptible = true,
7221 err = _nfs4_proc_setlk(state, cmd, request);
7222 if (err == -NFS4ERR_DENIED)
7224 err = nfs4_handle_exception(NFS_SERVER(state->inode),
7226 } while (exception.retry);
7230 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
7231 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
7234 nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
7235 struct file_lock *request)
7237 int status = -ERESTARTSYS;
7238 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
7240 while(!signalled()) {
7241 status = nfs4_proc_setlk(state, cmd, request);
7242 if ((status != -EAGAIN) || IS_SETLK(cmd))
7244 freezable_schedule_timeout_interruptible(timeout);
7246 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
7247 status = -ERESTARTSYS;
7252 #ifdef CONFIG_NFS_V4_1
7253 struct nfs4_lock_waiter {
7254 struct task_struct *task;
7255 struct inode *inode;
7256 struct nfs_lowner *owner;
7260 nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
7263 struct nfs4_lock_waiter *waiter = wait->private;
7265 /* NULL key means to wake up everyone */
7267 struct cb_notify_lock_args *cbnl = key;
7268 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
7269 *wowner = waiter->owner;
7271 /* Only wake if the callback was for the same owner. */
7272 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
7275 /* Make sure it's for the right inode */
7276 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
7280 /* override "private" so we can use default_wake_function */
7281 wait->private = waiter->task;
7282 ret = woken_wake_function(wait, mode, flags, key);
7284 list_del_init(&wait->entry);
7285 wait->private = waiter;
7290 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7292 int status = -ERESTARTSYS;
7293 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
7294 struct nfs_server *server = NFS_SERVER(state->inode);
7295 struct nfs_client *clp = server->nfs_client;
7296 wait_queue_head_t *q = &clp->cl_lock_waitq;
7297 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
7298 .id = lsp->ls_seqid.owner_id,
7299 .s_dev = server->s_dev };
7300 struct nfs4_lock_waiter waiter = { .task = current,
7301 .inode = state->inode,
7303 wait_queue_entry_t wait;
7305 /* Don't bother with waitqueue if we don't expect a callback */
7306 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
7307 return nfs4_retry_setlk_simple(state, cmd, request);
7310 wait.private = &waiter;
7311 wait.func = nfs4_wake_lock_waiter;
7313 while(!signalled()) {
7314 add_wait_queue(q, &wait);
7315 status = nfs4_proc_setlk(state, cmd, request);
7316 if ((status != -EAGAIN) || IS_SETLK(cmd)) {
7317 finish_wait(q, &wait);
7321 status = -ERESTARTSYS;
7322 freezer_do_not_count();
7323 wait_woken(&wait, TASK_INTERRUPTIBLE, NFS4_LOCK_MAXTIMEOUT);
7325 finish_wait(q, &wait);
7330 #else /* !CONFIG_NFS_V4_1 */
7332 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7334 return nfs4_retry_setlk_simple(state, cmd, request);
7339 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
7341 struct nfs_open_context *ctx;
7342 struct nfs4_state *state;
7345 /* verify open state */
7346 ctx = nfs_file_open_context(filp);
7349 if (IS_GETLK(cmd)) {
7351 return nfs4_proc_getlk(state, F_GETLK, request);
7355 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
7358 if (request->fl_type == F_UNLCK) {
7360 return nfs4_proc_unlck(state, cmd, request);
7367 if ((request->fl_flags & FL_POSIX) &&
7368 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
7372 * Don't rely on the VFS having checked the file open mode,
7373 * since it won't do this for flock() locks.
7375 switch (request->fl_type) {
7377 if (!(filp->f_mode & FMODE_READ))
7381 if (!(filp->f_mode & FMODE_WRITE))
7385 status = nfs4_set_lock_state(state, request);
7389 return nfs4_retry_setlk(state, cmd, request);
7392 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
7394 struct nfs_server *server = NFS_SERVER(state->inode);
7397 err = nfs4_set_lock_state(state, fl);
7401 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
7402 if (err != -NFS4ERR_DELAY)
7405 } while (err == -NFS4ERR_DELAY);
7406 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
7409 struct nfs_release_lockowner_data {
7410 struct nfs4_lock_state *lsp;
7411 struct nfs_server *server;
7412 struct nfs_release_lockowner_args args;
7413 struct nfs_release_lockowner_res res;
7414 unsigned long timestamp;
7417 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7419 struct nfs_release_lockowner_data *data = calldata;
7420 struct nfs_server *server = data->server;
7421 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7422 &data->res.seq_res, task);
7423 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7424 data->timestamp = jiffies;
7427 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7429 struct nfs_release_lockowner_data *data = calldata;
7430 struct nfs_server *server = data->server;
7432 nfs40_sequence_done(task, &data->res.seq_res);
7434 switch (task->tk_status) {
7436 renew_lease(server, data->timestamp);
7438 case -NFS4ERR_STALE_CLIENTID:
7439 case -NFS4ERR_EXPIRED:
7440 nfs4_schedule_lease_recovery(server->nfs_client);
7442 case -NFS4ERR_LEASE_MOVED:
7443 case -NFS4ERR_DELAY:
7444 if (nfs4_async_handle_error(task, server,
7445 NULL, NULL) == -EAGAIN)
7446 rpc_restart_call_prepare(task);
7450 static void nfs4_release_lockowner_release(void *calldata)
7452 struct nfs_release_lockowner_data *data = calldata;
7453 nfs4_free_lock_state(data->server, data->lsp);
7457 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
7458 .rpc_call_prepare = nfs4_release_lockowner_prepare,
7459 .rpc_call_done = nfs4_release_lockowner_done,
7460 .rpc_release = nfs4_release_lockowner_release,
7464 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
7466 struct nfs_release_lockowner_data *data;
7467 struct rpc_message msg = {
7468 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7471 if (server->nfs_client->cl_mvops->minor_version != 0)
7474 data = kmalloc(sizeof(*data), GFP_NOFS);
7478 data->server = server;
7479 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7480 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7481 data->args.lock_owner.s_dev = server->s_dev;
7483 msg.rpc_argp = &data->args;
7484 msg.rpc_resp = &data->res;
7485 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
7486 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
7489 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7491 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
7492 struct user_namespace *mnt_userns,
7493 struct dentry *unused, struct inode *inode,
7494 const char *key, const void *buf,
7495 size_t buflen, int flags)
7497 return nfs4_proc_set_acl(inode, buf, buflen);
7500 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
7501 struct dentry *unused, struct inode *inode,
7502 const char *key, void *buf, size_t buflen)
7504 return nfs4_proc_get_acl(inode, buf, buflen);
7507 static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
7509 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
7512 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
7514 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
7515 struct user_namespace *mnt_userns,
7516 struct dentry *unused, struct inode *inode,
7517 const char *key, const void *buf,
7518 size_t buflen, int flags)
7520 if (security_ismaclabel(key))
7521 return nfs4_set_security_label(inode, buf, buflen);
7526 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
7527 struct dentry *unused, struct inode *inode,
7528 const char *key, void *buf, size_t buflen)
7530 if (security_ismaclabel(key))
7531 return nfs4_get_security_label(inode, buf, buflen);
7536 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7540 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7541 len = security_inode_listsecurity(inode, list, list_len);
7542 if (len >= 0 && list_len && len > list_len)
7548 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7549 .prefix = XATTR_SECURITY_PREFIX,
7550 .get = nfs4_xattr_get_nfs4_label,
7551 .set = nfs4_xattr_set_nfs4_label,
7557 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7564 #ifdef CONFIG_NFS_V4_2
7565 static int nfs4_xattr_set_nfs4_user(const struct xattr_handler *handler,
7566 struct user_namespace *mnt_userns,
7567 struct dentry *unused, struct inode *inode,
7568 const char *key, const void *buf,
7569 size_t buflen, int flags)
7571 struct nfs_access_entry cache;
7574 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7578 * There is no mapping from the MAY_* flags to the NFS_ACCESS_XA*
7579 * flags right now. Handling of xattr operations use the normal
7580 * file read/write permissions.
7582 * Just in case the server has other ideas (which RFC 8276 allows),
7583 * do a cached access check for the XA* flags to possibly avoid
7584 * doing an RPC and getting EACCES back.
7586 if (!nfs_access_get_cached(inode, current_cred(), &cache, true)) {
7587 if (!(cache.mask & NFS_ACCESS_XAWRITE))
7592 ret = nfs42_proc_removexattr(inode, key);
7594 nfs4_xattr_cache_remove(inode, key);
7596 ret = nfs42_proc_setxattr(inode, key, buf, buflen, flags);
7598 nfs4_xattr_cache_add(inode, key, buf, NULL, buflen);
7604 static int nfs4_xattr_get_nfs4_user(const struct xattr_handler *handler,
7605 struct dentry *unused, struct inode *inode,
7606 const char *key, void *buf, size_t buflen)
7608 struct nfs_access_entry cache;
7611 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7614 if (!nfs_access_get_cached(inode, current_cred(), &cache, true)) {
7615 if (!(cache.mask & NFS_ACCESS_XAREAD))
7619 ret = nfs_revalidate_inode(NFS_SERVER(inode), inode);
7623 ret = nfs4_xattr_cache_get(inode, key, buf, buflen);
7624 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7627 ret = nfs42_proc_getxattr(inode, key, buf, buflen);
7633 nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7640 struct nfs_access_entry cache;
7642 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7645 if (!nfs_access_get_cached(inode, current_cred(), &cache, true)) {
7646 if (!(cache.mask & NFS_ACCESS_XALIST))
7650 ret = nfs_revalidate_inode(NFS_SERVER(inode), inode);
7654 ret = nfs4_xattr_cache_list(inode, list, list_len);
7655 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7660 buflen = list_len ? list_len : XATTR_LIST_MAX;
7661 buf = list_len ? list : NULL;
7665 ret = nfs42_proc_listxattrs(inode, buf, buflen,
7678 nfs4_xattr_cache_set_list(inode, list, size);
7686 nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7690 #endif /* CONFIG_NFS_V4_2 */
7693 * nfs_fhget will use either the mounted_on_fileid or the fileid
7695 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7697 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7698 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7699 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
7700 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
7703 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
7704 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
7705 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7709 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7710 const struct qstr *name,
7711 struct nfs4_fs_locations *fs_locations,
7714 struct nfs_server *server = NFS_SERVER(dir);
7716 struct nfs4_fs_locations_arg args = {
7717 .dir_fh = NFS_FH(dir),
7722 struct nfs4_fs_locations_res res = {
7723 .fs_locations = fs_locations,
7725 struct rpc_message msg = {
7726 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7732 dprintk("%s: start\n", __func__);
7734 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7735 bitmask[1] = nfs4_fattr_bitmap[1];
7737 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7738 * is not supported */
7739 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
7740 bitmask[0] &= ~FATTR4_WORD0_FILEID;
7742 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
7744 nfs_fattr_init(&fs_locations->fattr);
7745 fs_locations->server = server;
7746 fs_locations->nlocations = 0;
7747 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
7748 dprintk("%s: returned status = %d\n", __func__, status);
7752 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7753 const struct qstr *name,
7754 struct nfs4_fs_locations *fs_locations,
7757 struct nfs4_exception exception = {
7758 .interruptible = true,
7762 err = _nfs4_proc_fs_locations(client, dir, name,
7763 fs_locations, page);
7764 trace_nfs4_get_fs_locations(dir, name, err);
7765 err = nfs4_handle_exception(NFS_SERVER(dir), err,
7767 } while (exception.retry);
7772 * This operation also signals the server that this client is
7773 * performing migration recovery. The server can stop returning
7774 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7775 * appended to this compound to identify the client ID which is
7776 * performing recovery.
7778 static int _nfs40_proc_get_locations(struct inode *inode,
7779 struct nfs4_fs_locations *locations,
7780 struct page *page, const struct cred *cred)
7782 struct nfs_server *server = NFS_SERVER(inode);
7783 struct rpc_clnt *clnt = server->client;
7785 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7787 struct nfs4_fs_locations_arg args = {
7788 .clientid = server->nfs_client->cl_clientid,
7789 .fh = NFS_FH(inode),
7792 .migration = 1, /* skip LOOKUP */
7793 .renew = 1, /* append RENEW */
7795 struct nfs4_fs_locations_res res = {
7796 .fs_locations = locations,
7800 struct rpc_message msg = {
7801 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7806 unsigned long now = jiffies;
7809 nfs_fattr_init(&locations->fattr);
7810 locations->server = server;
7811 locations->nlocations = 0;
7813 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7814 status = nfs4_call_sync_sequence(clnt, server, &msg,
7815 &args.seq_args, &res.seq_res);
7819 renew_lease(server, now);
7823 #ifdef CONFIG_NFS_V4_1
7826 * This operation also signals the server that this client is
7827 * performing migration recovery. The server can stop asserting
7828 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7829 * performing this operation is identified in the SEQUENCE
7830 * operation in this compound.
7832 * When the client supports GETATTR(fs_locations_info), it can
7833 * be plumbed in here.
7835 static int _nfs41_proc_get_locations(struct inode *inode,
7836 struct nfs4_fs_locations *locations,
7837 struct page *page, const struct cred *cred)
7839 struct nfs_server *server = NFS_SERVER(inode);
7840 struct rpc_clnt *clnt = server->client;
7842 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7844 struct nfs4_fs_locations_arg args = {
7845 .fh = NFS_FH(inode),
7848 .migration = 1, /* skip LOOKUP */
7850 struct nfs4_fs_locations_res res = {
7851 .fs_locations = locations,
7854 struct rpc_message msg = {
7855 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7862 nfs_fattr_init(&locations->fattr);
7863 locations->server = server;
7864 locations->nlocations = 0;
7866 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7867 status = nfs4_call_sync_sequence(clnt, server, &msg,
7868 &args.seq_args, &res.seq_res);
7869 if (status == NFS4_OK &&
7870 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7871 status = -NFS4ERR_LEASE_MOVED;
7875 #endif /* CONFIG_NFS_V4_1 */
7878 * nfs4_proc_get_locations - discover locations for a migrated FSID
7879 * @inode: inode on FSID that is migrating
7880 * @locations: result of query
7882 * @cred: credential to use for this operation
7884 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7885 * operation failed, or a negative errno if a local error occurred.
7887 * On success, "locations" is filled in, but if the server has
7888 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7891 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7892 * from this client that require migration recovery.
7894 int nfs4_proc_get_locations(struct inode *inode,
7895 struct nfs4_fs_locations *locations,
7896 struct page *page, const struct cred *cred)
7898 struct nfs_server *server = NFS_SERVER(inode);
7899 struct nfs_client *clp = server->nfs_client;
7900 const struct nfs4_mig_recovery_ops *ops =
7901 clp->cl_mvops->mig_recovery_ops;
7902 struct nfs4_exception exception = {
7903 .interruptible = true,
7907 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7908 (unsigned long long)server->fsid.major,
7909 (unsigned long long)server->fsid.minor,
7911 nfs_display_fhandle(NFS_FH(inode), __func__);
7914 status = ops->get_locations(inode, locations, page, cred);
7915 if (status != -NFS4ERR_DELAY)
7917 nfs4_handle_exception(server, status, &exception);
7918 } while (exception.retry);
7923 * This operation also signals the server that this client is
7924 * performing "lease moved" recovery. The server can stop
7925 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7926 * is appended to this compound to identify the client ID which is
7927 * performing recovery.
7929 static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
7931 struct nfs_server *server = NFS_SERVER(inode);
7932 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7933 struct rpc_clnt *clnt = server->client;
7934 struct nfs4_fsid_present_arg args = {
7935 .fh = NFS_FH(inode),
7936 .clientid = clp->cl_clientid,
7937 .renew = 1, /* append RENEW */
7939 struct nfs4_fsid_present_res res = {
7942 struct rpc_message msg = {
7943 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7948 unsigned long now = jiffies;
7951 res.fh = nfs_alloc_fhandle();
7955 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7956 status = nfs4_call_sync_sequence(clnt, server, &msg,
7957 &args.seq_args, &res.seq_res);
7958 nfs_free_fhandle(res.fh);
7962 do_renew_lease(clp, now);
7966 #ifdef CONFIG_NFS_V4_1
7969 * This operation also signals the server that this client is
7970 * performing "lease moved" recovery. The server can stop asserting
7971 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7972 * this operation is identified in the SEQUENCE operation in this
7975 static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
7977 struct nfs_server *server = NFS_SERVER(inode);
7978 struct rpc_clnt *clnt = server->client;
7979 struct nfs4_fsid_present_arg args = {
7980 .fh = NFS_FH(inode),
7982 struct nfs4_fsid_present_res res = {
7984 struct rpc_message msg = {
7985 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7992 res.fh = nfs_alloc_fhandle();
7996 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7997 status = nfs4_call_sync_sequence(clnt, server, &msg,
7998 &args.seq_args, &res.seq_res);
7999 nfs_free_fhandle(res.fh);
8000 if (status == NFS4_OK &&
8001 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
8002 status = -NFS4ERR_LEASE_MOVED;
8006 #endif /* CONFIG_NFS_V4_1 */
8009 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
8010 * @inode: inode on FSID to check
8011 * @cred: credential to use for this operation
8013 * Server indicates whether the FSID is present, moved, or not
8014 * recognized. This operation is necessary to clear a LEASE_MOVED
8015 * condition for this client ID.
8017 * Returns NFS4_OK if the FSID is present on this server,
8018 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
8019 * NFS4ERR code if some error occurred on the server, or a
8020 * negative errno if a local failure occurred.
8022 int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
8024 struct nfs_server *server = NFS_SERVER(inode);
8025 struct nfs_client *clp = server->nfs_client;
8026 const struct nfs4_mig_recovery_ops *ops =
8027 clp->cl_mvops->mig_recovery_ops;
8028 struct nfs4_exception exception = {
8029 .interruptible = true,
8033 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
8034 (unsigned long long)server->fsid.major,
8035 (unsigned long long)server->fsid.minor,
8037 nfs_display_fhandle(NFS_FH(inode), __func__);
8040 status = ops->fsid_present(inode, cred);
8041 if (status != -NFS4ERR_DELAY)
8043 nfs4_handle_exception(server, status, &exception);
8044 } while (exception.retry);
8049 * If 'use_integrity' is true and the state managment nfs_client
8050 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
8051 * and the machine credential as per RFC3530bis and RFC5661 Security
8052 * Considerations sections. Otherwise, just use the user cred with the
8053 * filesystem's rpc_client.
8055 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
8058 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
8059 struct nfs_client *clp = NFS_SERVER(dir)->nfs_client;
8060 struct nfs4_secinfo_arg args = {
8061 .dir_fh = NFS_FH(dir),
8064 struct nfs4_secinfo_res res = {
8067 struct rpc_message msg = {
8068 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
8072 struct nfs4_call_sync_data data = {
8073 .seq_server = NFS_SERVER(dir),
8074 .seq_args = &args.seq_args,
8075 .seq_res = &res.seq_res,
8077 struct rpc_task_setup task_setup = {
8079 .rpc_message = &msg,
8080 .callback_ops = clp->cl_mvops->call_sync_ops,
8081 .callback_data = &data,
8082 .flags = RPC_TASK_NO_ROUND_ROBIN,
8084 const struct cred *cred = NULL;
8086 if (use_integrity) {
8087 clnt = clp->cl_rpcclient;
8088 task_setup.rpc_client = clnt;
8090 cred = nfs4_get_clid_cred(clp);
8091 msg.rpc_cred = cred;
8094 dprintk("NFS call secinfo %s\n", name->name);
8096 nfs4_state_protect(clp, NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
8097 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
8098 status = nfs4_call_sync_custom(&task_setup);
8100 dprintk("NFS reply secinfo: %d\n", status);
8106 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
8107 struct nfs4_secinfo_flavors *flavors)
8109 struct nfs4_exception exception = {
8110 .interruptible = true,
8114 err = -NFS4ERR_WRONGSEC;
8116 /* try to use integrity protection with machine cred */
8117 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
8118 err = _nfs4_proc_secinfo(dir, name, flavors, true);
8121 * if unable to use integrity protection, or SECINFO with
8122 * integrity protection returns NFS4ERR_WRONGSEC (which is
8123 * disallowed by spec, but exists in deployed servers) use
8124 * the current filesystem's rpc_client and the user cred.
8126 if (err == -NFS4ERR_WRONGSEC)
8127 err = _nfs4_proc_secinfo(dir, name, flavors, false);
8129 trace_nfs4_secinfo(dir, name, err);
8130 err = nfs4_handle_exception(NFS_SERVER(dir), err,
8132 } while (exception.retry);
8136 #ifdef CONFIG_NFS_V4_1
8138 * Check the exchange flags returned by the server for invalid flags, having
8139 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
8142 static int nfs4_check_cl_exchange_flags(u32 flags, u32 version)
8144 if (version >= 2 && (flags & ~EXCHGID4_2_FLAG_MASK_R))
8146 else if (version < 2 && (flags & ~EXCHGID4_FLAG_MASK_R))
8148 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
8149 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
8151 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
8155 return -NFS4ERR_INVAL;
8159 nfs41_same_server_scope(struct nfs41_server_scope *a,
8160 struct nfs41_server_scope *b)
8162 if (a->server_scope_sz != b->server_scope_sz)
8164 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
8168 nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
8170 struct nfs41_bind_conn_to_session_args *args = task->tk_msg.rpc_argp;
8171 struct nfs41_bind_conn_to_session_res *res = task->tk_msg.rpc_resp;
8172 struct nfs_client *clp = args->client;
8174 switch (task->tk_status) {
8175 case -NFS4ERR_BADSESSION:
8176 case -NFS4ERR_DEADSESSION:
8177 nfs4_schedule_session_recovery(clp->cl_session,
8180 if (args->dir == NFS4_CDFC4_FORE_OR_BOTH &&
8181 res->dir != NFS4_CDFS4_BOTH) {
8182 rpc_task_close_connection(task);
8183 if (args->retries++ < MAX_BIND_CONN_TO_SESSION_RETRIES)
8184 rpc_restart_call(task);
8188 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
8189 .rpc_call_done = nfs4_bind_one_conn_to_session_done,
8193 * nfs4_proc_bind_one_conn_to_session()
8195 * The 4.1 client currently uses the same TCP connection for the
8196 * fore and backchannel.
8199 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
8200 struct rpc_xprt *xprt,
8201 struct nfs_client *clp,
8202 const struct cred *cred)
8205 struct nfs41_bind_conn_to_session_args args = {
8207 .dir = NFS4_CDFC4_FORE_OR_BOTH,
8210 struct nfs41_bind_conn_to_session_res res;
8211 struct rpc_message msg = {
8213 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
8218 struct rpc_task_setup task_setup_data = {
8221 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
8222 .rpc_message = &msg,
8223 .flags = RPC_TASK_TIMEOUT,
8225 struct rpc_task *task;
8227 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
8228 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
8229 args.dir = NFS4_CDFC4_FORE;
8231 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
8232 if (xprt != rcu_access_pointer(clnt->cl_xprt))
8233 args.dir = NFS4_CDFC4_FORE;
8235 task = rpc_run_task(&task_setup_data);
8236 if (!IS_ERR(task)) {
8237 status = task->tk_status;
8240 status = PTR_ERR(task);
8241 trace_nfs4_bind_conn_to_session(clp, status);
8243 if (memcmp(res.sessionid.data,
8244 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
8245 dprintk("NFS: %s: Session ID mismatch\n", __func__);
8248 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
8249 dprintk("NFS: %s: Unexpected direction from server\n",
8253 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
8254 dprintk("NFS: %s: Server returned RDMA mode = true\n",
8263 struct rpc_bind_conn_calldata {
8264 struct nfs_client *clp;
8265 const struct cred *cred;
8269 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
8270 struct rpc_xprt *xprt,
8273 struct rpc_bind_conn_calldata *p = calldata;
8275 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
8278 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
8280 struct rpc_bind_conn_calldata data = {
8284 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
8285 nfs4_proc_bind_conn_to_session_callback, &data);
8289 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
8290 * and operations we'd like to see to enable certain features in the allow map
8292 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
8293 .how = SP4_MACH_CRED,
8294 .enforce.u.words = {
8295 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8296 1 << (OP_EXCHANGE_ID - 32) |
8297 1 << (OP_CREATE_SESSION - 32) |
8298 1 << (OP_DESTROY_SESSION - 32) |
8299 1 << (OP_DESTROY_CLIENTID - 32)
8302 [0] = 1 << (OP_CLOSE) |
8303 1 << (OP_OPEN_DOWNGRADE) |
8305 1 << (OP_DELEGRETURN) |
8307 [1] = 1 << (OP_SECINFO - 32) |
8308 1 << (OP_SECINFO_NO_NAME - 32) |
8309 1 << (OP_LAYOUTRETURN - 32) |
8310 1 << (OP_TEST_STATEID - 32) |
8311 1 << (OP_FREE_STATEID - 32) |
8312 1 << (OP_WRITE - 32)
8317 * Select the state protection mode for client `clp' given the server results
8318 * from exchange_id in `sp'.
8320 * Returns 0 on success, negative errno otherwise.
8322 static int nfs4_sp4_select_mode(struct nfs_client *clp,
8323 struct nfs41_state_protection *sp)
8325 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
8326 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8327 1 << (OP_EXCHANGE_ID - 32) |
8328 1 << (OP_CREATE_SESSION - 32) |
8329 1 << (OP_DESTROY_SESSION - 32) |
8330 1 << (OP_DESTROY_CLIENTID - 32)
8332 unsigned long flags = 0;
8336 if (sp->how == SP4_MACH_CRED) {
8337 /* Print state protect result */
8338 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
8339 for (i = 0; i <= LAST_NFS4_OP; i++) {
8340 if (test_bit(i, sp->enforce.u.longs))
8341 dfprintk(MOUNT, " enforce op %d\n", i);
8342 if (test_bit(i, sp->allow.u.longs))
8343 dfprintk(MOUNT, " allow op %d\n", i);
8346 /* make sure nothing is on enforce list that isn't supported */
8347 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
8348 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
8349 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
8356 * Minimal mode - state operations are allowed to use machine
8357 * credential. Note this already happens by default, so the
8358 * client doesn't have to do anything more than the negotiation.
8360 * NOTE: we don't care if EXCHANGE_ID is in the list -
8361 * we're already using the machine cred for exchange_id
8362 * and will never use a different cred.
8364 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
8365 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
8366 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
8367 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
8368 dfprintk(MOUNT, "sp4_mach_cred:\n");
8369 dfprintk(MOUNT, " minimal mode enabled\n");
8370 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
8372 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
8377 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
8378 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
8379 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
8380 test_bit(OP_LOCKU, sp->allow.u.longs)) {
8381 dfprintk(MOUNT, " cleanup mode enabled\n");
8382 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
8385 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
8386 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
8387 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
8390 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
8391 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
8392 dfprintk(MOUNT, " secinfo mode enabled\n");
8393 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
8396 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
8397 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
8398 dfprintk(MOUNT, " stateid mode enabled\n");
8399 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
8402 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
8403 dfprintk(MOUNT, " write mode enabled\n");
8404 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
8407 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
8408 dfprintk(MOUNT, " commit mode enabled\n");
8409 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
8413 clp->cl_sp4_flags = flags;
8417 struct nfs41_exchange_id_data {
8418 struct nfs41_exchange_id_res res;
8419 struct nfs41_exchange_id_args args;
8422 static void nfs4_exchange_id_release(void *data)
8424 struct nfs41_exchange_id_data *cdata =
8425 (struct nfs41_exchange_id_data *)data;
8427 nfs_put_client(cdata->args.client);
8428 kfree(cdata->res.impl_id);
8429 kfree(cdata->res.server_scope);
8430 kfree(cdata->res.server_owner);
8434 static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
8435 .rpc_release = nfs4_exchange_id_release,
8439 * _nfs4_proc_exchange_id()
8441 * Wrapper for EXCHANGE_ID operation.
8443 static struct rpc_task *
8444 nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
8445 u32 sp4_how, struct rpc_xprt *xprt)
8447 struct rpc_message msg = {
8448 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
8451 struct rpc_task_setup task_setup_data = {
8452 .rpc_client = clp->cl_rpcclient,
8453 .callback_ops = &nfs4_exchange_id_call_ops,
8454 .rpc_message = &msg,
8455 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
8457 struct nfs41_exchange_id_data *calldata;
8460 if (!refcount_inc_not_zero(&clp->cl_count))
8461 return ERR_PTR(-EIO);
8464 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8468 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
8470 status = nfs4_init_uniform_client_string(clp);
8474 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
8477 if (unlikely(calldata->res.server_owner == NULL))
8480 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
8482 if (unlikely(calldata->res.server_scope == NULL))
8483 goto out_server_owner;
8485 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
8486 if (unlikely(calldata->res.impl_id == NULL))
8487 goto out_server_scope;
8491 calldata->args.state_protect.how = SP4_NONE;
8495 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
8505 task_setup_data.rpc_xprt = xprt;
8506 task_setup_data.flags |= RPC_TASK_SOFTCONN;
8507 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
8508 sizeof(calldata->args.verifier.data));
8510 calldata->args.client = clp;
8511 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
8512 EXCHGID4_FLAG_BIND_PRINC_STATEID;
8513 #ifdef CONFIG_NFS_V4_1_MIGRATION
8514 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
8516 msg.rpc_argp = &calldata->args;
8517 msg.rpc_resp = &calldata->res;
8518 task_setup_data.callback_data = calldata;
8520 return rpc_run_task(&task_setup_data);
8523 kfree(calldata->res.impl_id);
8525 kfree(calldata->res.server_scope);
8527 kfree(calldata->res.server_owner);
8531 nfs_put_client(clp);
8532 return ERR_PTR(status);
8536 * _nfs4_proc_exchange_id()
8538 * Wrapper for EXCHANGE_ID operation.
8540 static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
8543 struct rpc_task *task;
8544 struct nfs41_exchange_id_args *argp;
8545 struct nfs41_exchange_id_res *resp;
8546 unsigned long now = jiffies;
8549 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
8551 return PTR_ERR(task);
8553 argp = task->tk_msg.rpc_argp;
8554 resp = task->tk_msg.rpc_resp;
8555 status = task->tk_status;
8559 status = nfs4_check_cl_exchange_flags(resp->flags,
8560 clp->cl_mvops->minor_version);
8564 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8568 do_renew_lease(clp, now);
8570 clp->cl_clientid = resp->clientid;
8571 clp->cl_exchange_flags = resp->flags;
8572 clp->cl_seqid = resp->seqid;
8573 /* Client ID is not confirmed */
8574 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8575 clear_bit(NFS4_SESSION_ESTABLISHED,
8576 &clp->cl_session->session_state);
8578 if (clp->cl_serverscope != NULL &&
8579 !nfs41_same_server_scope(clp->cl_serverscope,
8580 resp->server_scope)) {
8581 dprintk("%s: server_scope mismatch detected\n",
8583 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8586 swap(clp->cl_serverowner, resp->server_owner);
8587 swap(clp->cl_serverscope, resp->server_scope);
8588 swap(clp->cl_implid, resp->impl_id);
8590 /* Save the EXCHANGE_ID verifier session trunk tests */
8591 memcpy(clp->cl_confirm.data, argp->verifier.data,
8592 sizeof(clp->cl_confirm.data));
8594 trace_nfs4_exchange_id(clp, status);
8600 * nfs4_proc_exchange_id()
8602 * Returns zero, a negative errno, or a negative NFS4ERR status code.
8604 * Since the clientid has expired, all compounds using sessions
8605 * associated with the stale clientid will be returning
8606 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
8607 * be in some phase of session reset.
8609 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
8611 int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
8613 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8616 /* try SP4_MACH_CRED if krb5i/p */
8617 if (authflavor == RPC_AUTH_GSS_KRB5I ||
8618 authflavor == RPC_AUTH_GSS_KRB5P) {
8619 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
8625 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
8629 * nfs4_test_session_trunk
8631 * This is an add_xprt_test() test function called from
8632 * rpc_clnt_setup_test_and_add_xprt.
8634 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8635 * and is dereferrenced in nfs4_exchange_id_release
8637 * Upon success, add the new transport to the rpc_clnt
8639 * @clnt: struct rpc_clnt to get new transport
8640 * @xprt: the rpc_xprt to test
8641 * @data: call data for _nfs4_proc_exchange_id.
8643 void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
8646 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
8647 struct rpc_task *task;
8652 dprintk("--> %s try %s\n", __func__,
8653 xprt->address_strings[RPC_DISPLAY_ADDR]);
8655 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8657 /* Test connection for session trunking. Async exchange_id call */
8658 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8662 status = task->tk_status;
8664 status = nfs4_detect_session_trunking(adata->clp,
8665 task->tk_msg.rpc_resp, xprt);
8668 rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
8672 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
8674 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
8675 const struct cred *cred)
8677 struct rpc_message msg = {
8678 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8684 status = rpc_call_sync(clp->cl_rpcclient, &msg,
8685 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
8686 trace_nfs4_destroy_clientid(clp, status);
8688 dprintk("NFS: Got error %d from the server %s on "
8689 "DESTROY_CLIENTID.", status, clp->cl_hostname);
8693 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
8694 const struct cred *cred)
8699 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8700 ret = _nfs4_proc_destroy_clientid(clp, cred);
8702 case -NFS4ERR_DELAY:
8703 case -NFS4ERR_CLIENTID_BUSY:
8713 int nfs4_destroy_clientid(struct nfs_client *clp)
8715 const struct cred *cred;
8718 if (clp->cl_mvops->minor_version < 1)
8720 if (clp->cl_exchange_flags == 0)
8722 if (clp->cl_preserve_clid)
8724 cred = nfs4_get_clid_cred(clp);
8725 ret = nfs4_proc_destroy_clientid(clp, cred);
8729 case -NFS4ERR_STALE_CLIENTID:
8730 clp->cl_exchange_flags = 0;
8736 #endif /* CONFIG_NFS_V4_1 */
8738 struct nfs4_get_lease_time_data {
8739 struct nfs4_get_lease_time_args *args;
8740 struct nfs4_get_lease_time_res *res;
8741 struct nfs_client *clp;
8744 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
8747 struct nfs4_get_lease_time_data *data =
8748 (struct nfs4_get_lease_time_data *)calldata;
8750 dprintk("--> %s\n", __func__);
8751 /* just setup sequence, do not trigger session recovery
8752 since we're invoked within one */
8753 nfs4_setup_sequence(data->clp,
8754 &data->args->la_seq_args,
8755 &data->res->lr_seq_res,
8757 dprintk("<-- %s\n", __func__);
8761 * Called from nfs4_state_manager thread for session setup, so don't recover
8762 * from sequence operation or clientid errors.
8764 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8766 struct nfs4_get_lease_time_data *data =
8767 (struct nfs4_get_lease_time_data *)calldata;
8769 dprintk("--> %s\n", __func__);
8770 if (!nfs4_sequence_done(task, &data->res->lr_seq_res))
8772 switch (task->tk_status) {
8773 case -NFS4ERR_DELAY:
8774 case -NFS4ERR_GRACE:
8775 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
8776 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8777 task->tk_status = 0;
8779 case -NFS4ERR_RETRY_UNCACHED_REP:
8780 rpc_restart_call_prepare(task);
8783 dprintk("<-- %s\n", __func__);
8786 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
8787 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8788 .rpc_call_done = nfs4_get_lease_time_done,
8791 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8793 struct nfs4_get_lease_time_args args;
8794 struct nfs4_get_lease_time_res res = {
8795 .lr_fsinfo = fsinfo,
8797 struct nfs4_get_lease_time_data data = {
8802 struct rpc_message msg = {
8803 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8807 struct rpc_task_setup task_setup = {
8808 .rpc_client = clp->cl_rpcclient,
8809 .rpc_message = &msg,
8810 .callback_ops = &nfs4_get_lease_time_ops,
8811 .callback_data = &data,
8812 .flags = RPC_TASK_TIMEOUT,
8815 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
8816 return nfs4_call_sync_custom(&task_setup);
8819 #ifdef CONFIG_NFS_V4_1
8822 * Initialize the values to be used by the client in CREATE_SESSION
8823 * If nfs4_init_session set the fore channel request and response sizes,
8826 * Set the back channel max_resp_sz_cached to zero to force the client to
8827 * always set csa_cachethis to FALSE because the current implementation
8828 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8830 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8831 struct rpc_clnt *clnt)
8833 unsigned int max_rqst_sz, max_resp_sz;
8834 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
8835 unsigned int max_bc_slots = rpc_num_bc_slots(clnt);
8837 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8838 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8840 /* Fore channel attributes */
8841 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8842 args->fc_attrs.max_resp_sz = max_resp_sz;
8843 args->fc_attrs.max_ops = NFS4_MAX_OPS;
8844 args->fc_attrs.max_reqs = max_session_slots;
8846 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
8847 "max_ops=%u max_reqs=%u\n",
8849 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
8850 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
8852 /* Back channel attributes */
8853 args->bc_attrs.max_rqst_sz = max_bc_payload;
8854 args->bc_attrs.max_resp_sz = max_bc_payload;
8855 args->bc_attrs.max_resp_sz_cached = 0;
8856 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
8857 args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
8858 if (args->bc_attrs.max_reqs > max_bc_slots)
8859 args->bc_attrs.max_reqs = max_bc_slots;
8861 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8862 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8864 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8865 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8866 args->bc_attrs.max_reqs);
8869 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8870 struct nfs41_create_session_res *res)
8872 struct nfs4_channel_attrs *sent = &args->fc_attrs;
8873 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
8875 if (rcvd->max_resp_sz > sent->max_resp_sz)
8878 * Our requested max_ops is the minimum we need; we're not
8879 * prepared to break up compounds into smaller pieces than that.
8880 * So, no point even trying to continue if the server won't
8883 if (rcvd->max_ops < sent->max_ops)
8885 if (rcvd->max_reqs == 0)
8887 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8888 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
8892 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8893 struct nfs41_create_session_res *res)
8895 struct nfs4_channel_attrs *sent = &args->bc_attrs;
8896 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
8898 if (!(res->flags & SESSION4_BACK_CHAN))
8900 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8902 if (rcvd->max_resp_sz < sent->max_resp_sz)
8904 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8906 if (rcvd->max_ops > sent->max_ops)
8908 if (rcvd->max_reqs > sent->max_reqs)
8914 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
8915 struct nfs41_create_session_res *res)
8919 ret = nfs4_verify_fore_channel_attrs(args, res);
8922 return nfs4_verify_back_channel_attrs(args, res);
8925 static void nfs4_update_session(struct nfs4_session *session,
8926 struct nfs41_create_session_res *res)
8928 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
8929 /* Mark client id and session as being confirmed */
8930 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8931 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
8932 session->flags = res->flags;
8933 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
8934 if (res->flags & SESSION4_BACK_CHAN)
8935 memcpy(&session->bc_attrs, &res->bc_attrs,
8936 sizeof(session->bc_attrs));
8939 static int _nfs4_proc_create_session(struct nfs_client *clp,
8940 const struct cred *cred)
8942 struct nfs4_session *session = clp->cl_session;
8943 struct nfs41_create_session_args args = {
8945 .clientid = clp->cl_clientid,
8946 .seqid = clp->cl_seqid,
8947 .cb_program = NFS4_CALLBACK,
8949 struct nfs41_create_session_res res;
8951 struct rpc_message msg = {
8952 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8959 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
8960 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
8962 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
8963 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
8964 trace_nfs4_create_session(clp, status);
8967 case -NFS4ERR_STALE_CLIENTID:
8968 case -NFS4ERR_DELAY:
8977 /* Verify the session's negotiated channel_attrs values */
8978 status = nfs4_verify_channel_attrs(&args, &res);
8979 /* Increment the clientid slot sequence id */
8982 nfs4_update_session(session, &res);
8989 * Issues a CREATE_SESSION operation to the server.
8990 * It is the responsibility of the caller to verify the session is
8991 * expired before calling this routine.
8993 int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
8997 struct nfs4_session *session = clp->cl_session;
8999 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
9001 status = _nfs4_proc_create_session(clp, cred);
9005 /* Init or reset the session slot tables */
9006 status = nfs4_setup_session_slot_tables(session);
9007 dprintk("slot table setup returned %d\n", status);
9011 ptr = (unsigned *)&session->sess_id.data[0];
9012 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
9013 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
9015 dprintk("<-- %s\n", __func__);
9020 * Issue the over-the-wire RPC DESTROY_SESSION.
9021 * The caller must serialize access to this routine.
9023 int nfs4_proc_destroy_session(struct nfs4_session *session,
9024 const struct cred *cred)
9026 struct rpc_message msg = {
9027 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
9028 .rpc_argp = session,
9033 dprintk("--> nfs4_proc_destroy_session\n");
9035 /* session is still being setup */
9036 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
9039 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
9040 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
9041 trace_nfs4_destroy_session(session->clp, status);
9044 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
9045 "Session has been destroyed regardless...\n", status);
9047 dprintk("<-- nfs4_proc_destroy_session\n");
9052 * Renew the cl_session lease.
9054 struct nfs4_sequence_data {
9055 struct nfs_client *clp;
9056 struct nfs4_sequence_args args;
9057 struct nfs4_sequence_res res;
9060 static void nfs41_sequence_release(void *data)
9062 struct nfs4_sequence_data *calldata = data;
9063 struct nfs_client *clp = calldata->clp;
9065 if (refcount_read(&clp->cl_count) > 1)
9066 nfs4_schedule_state_renewal(clp);
9067 nfs_put_client(clp);
9071 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9073 switch(task->tk_status) {
9074 case -NFS4ERR_DELAY:
9075 rpc_delay(task, NFS4_POLL_RETRY_MAX);
9078 nfs4_schedule_lease_recovery(clp);
9083 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
9085 struct nfs4_sequence_data *calldata = data;
9086 struct nfs_client *clp = calldata->clp;
9088 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
9091 trace_nfs4_sequence(clp, task->tk_status);
9092 if (task->tk_status < 0) {
9093 dprintk("%s ERROR %d\n", __func__, task->tk_status);
9094 if (refcount_read(&clp->cl_count) == 1)
9097 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
9098 rpc_restart_call_prepare(task);
9102 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
9104 dprintk("<-- %s\n", __func__);
9107 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
9109 struct nfs4_sequence_data *calldata = data;
9110 struct nfs_client *clp = calldata->clp;
9111 struct nfs4_sequence_args *args;
9112 struct nfs4_sequence_res *res;
9114 args = task->tk_msg.rpc_argp;
9115 res = task->tk_msg.rpc_resp;
9117 nfs4_setup_sequence(clp, args, res, task);
9120 static const struct rpc_call_ops nfs41_sequence_ops = {
9121 .rpc_call_done = nfs41_sequence_call_done,
9122 .rpc_call_prepare = nfs41_sequence_prepare,
9123 .rpc_release = nfs41_sequence_release,
9126 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
9127 const struct cred *cred,
9128 struct nfs4_slot *slot,
9131 struct nfs4_sequence_data *calldata;
9132 struct rpc_message msg = {
9133 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
9136 struct rpc_task_setup task_setup_data = {
9137 .rpc_client = clp->cl_rpcclient,
9138 .rpc_message = &msg,
9139 .callback_ops = &nfs41_sequence_ops,
9140 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
9142 struct rpc_task *ret;
9144 ret = ERR_PTR(-EIO);
9145 if (!refcount_inc_not_zero(&clp->cl_count))
9148 ret = ERR_PTR(-ENOMEM);
9149 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
9150 if (calldata == NULL)
9152 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
9153 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
9154 msg.rpc_argp = &calldata->args;
9155 msg.rpc_resp = &calldata->res;
9156 calldata->clp = clp;
9157 task_setup_data.callback_data = calldata;
9159 ret = rpc_run_task(&task_setup_data);
9164 nfs_put_client(clp);
9166 nfs41_release_slot(slot);
9170 static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
9172 struct rpc_task *task;
9175 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
9177 task = _nfs41_proc_sequence(clp, cred, NULL, false);
9179 ret = PTR_ERR(task);
9181 rpc_put_task_async(task);
9182 dprintk("<-- %s status=%d\n", __func__, ret);
9186 static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
9188 struct rpc_task *task;
9191 task = _nfs41_proc_sequence(clp, cred, NULL, true);
9193 ret = PTR_ERR(task);
9196 ret = rpc_wait_for_completion_task(task);
9198 ret = task->tk_status;
9201 dprintk("<-- %s status=%d\n", __func__, ret);
9205 struct nfs4_reclaim_complete_data {
9206 struct nfs_client *clp;
9207 struct nfs41_reclaim_complete_args arg;
9208 struct nfs41_reclaim_complete_res res;
9211 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
9213 struct nfs4_reclaim_complete_data *calldata = data;
9215 nfs4_setup_sequence(calldata->clp,
9216 &calldata->arg.seq_args,
9217 &calldata->res.seq_res,
9221 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9223 switch(task->tk_status) {
9225 wake_up_all(&clp->cl_lock_waitq);
9227 case -NFS4ERR_COMPLETE_ALREADY:
9228 case -NFS4ERR_WRONG_CRED: /* What to do here? */
9230 case -NFS4ERR_DELAY:
9231 rpc_delay(task, NFS4_POLL_RETRY_MAX);
9233 case -NFS4ERR_RETRY_UNCACHED_REP:
9235 case -NFS4ERR_BADSESSION:
9236 case -NFS4ERR_DEADSESSION:
9237 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9240 nfs4_schedule_lease_recovery(clp);
9245 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
9247 struct nfs4_reclaim_complete_data *calldata = data;
9248 struct nfs_client *clp = calldata->clp;
9249 struct nfs4_sequence_res *res = &calldata->res.seq_res;
9251 dprintk("--> %s\n", __func__);
9252 if (!nfs41_sequence_done(task, res))
9255 trace_nfs4_reclaim_complete(clp, task->tk_status);
9256 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
9257 rpc_restart_call_prepare(task);
9260 dprintk("<-- %s\n", __func__);
9263 static void nfs4_free_reclaim_complete_data(void *data)
9265 struct nfs4_reclaim_complete_data *calldata = data;
9270 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
9271 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
9272 .rpc_call_done = nfs4_reclaim_complete_done,
9273 .rpc_release = nfs4_free_reclaim_complete_data,
9277 * Issue a global reclaim complete.
9279 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
9280 const struct cred *cred)
9282 struct nfs4_reclaim_complete_data *calldata;
9283 struct rpc_message msg = {
9284 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
9287 struct rpc_task_setup task_setup_data = {
9288 .rpc_client = clp->cl_rpcclient,
9289 .rpc_message = &msg,
9290 .callback_ops = &nfs4_reclaim_complete_call_ops,
9291 .flags = RPC_TASK_NO_ROUND_ROBIN,
9293 int status = -ENOMEM;
9295 dprintk("--> %s\n", __func__);
9296 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
9297 if (calldata == NULL)
9299 calldata->clp = clp;
9300 calldata->arg.one_fs = 0;
9302 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
9303 msg.rpc_argp = &calldata->arg;
9304 msg.rpc_resp = &calldata->res;
9305 task_setup_data.callback_data = calldata;
9306 status = nfs4_call_sync_custom(&task_setup_data);
9308 dprintk("<-- %s status=%d\n", __func__, status);
9313 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
9315 struct nfs4_layoutget *lgp = calldata;
9316 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
9318 dprintk("--> %s\n", __func__);
9319 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
9320 &lgp->res.seq_res, task);
9321 dprintk("<-- %s\n", __func__);
9324 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
9326 struct nfs4_layoutget *lgp = calldata;
9328 dprintk("--> %s\n", __func__);
9329 nfs41_sequence_process(task, &lgp->res.seq_res);
9330 dprintk("<-- %s\n", __func__);
9334 nfs4_layoutget_handle_exception(struct rpc_task *task,
9335 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
9337 struct inode *inode = lgp->args.inode;
9338 struct nfs_server *server = NFS_SERVER(inode);
9339 struct pnfs_layout_hdr *lo;
9340 int nfs4err = task->tk_status;
9341 int err, status = 0;
9344 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
9346 nfs4_sequence_free_slot(&lgp->res.seq_res);
9353 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
9354 * on the file. set tk_status to -ENODATA to tell upper layer to
9357 case -NFS4ERR_LAYOUTUNAVAILABLE:
9361 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
9362 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
9364 case -NFS4ERR_BADLAYOUT:
9365 status = -EOVERFLOW;
9368 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
9369 * (or clients) writing to the same RAID stripe except when
9370 * the minlength argument is 0 (see RFC5661 section 18.43.3).
9372 * Treat it like we would RECALLCONFLICT -- we retry for a little
9373 * while, and then eventually give up.
9375 case -NFS4ERR_LAYOUTTRYLATER:
9376 if (lgp->args.minlength == 0) {
9377 status = -EOVERFLOW;
9382 case -NFS4ERR_RECALLCONFLICT:
9383 status = -ERECALLCONFLICT;
9385 case -NFS4ERR_DELEG_REVOKED:
9386 case -NFS4ERR_ADMIN_REVOKED:
9387 case -NFS4ERR_EXPIRED:
9388 case -NFS4ERR_BAD_STATEID:
9389 exception->timeout = 0;
9390 spin_lock(&inode->i_lock);
9391 lo = NFS_I(inode)->layout;
9392 /* If the open stateid was bad, then recover it. */
9393 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
9394 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
9395 spin_unlock(&inode->i_lock);
9396 exception->state = lgp->args.ctx->state;
9397 exception->stateid = &lgp->args.stateid;
9402 * Mark the bad layout state as invalid, then retry
9404 pnfs_mark_layout_stateid_invalid(lo, &head);
9405 spin_unlock(&inode->i_lock);
9406 nfs_commit_inode(inode, 0);
9407 pnfs_free_lseg_list(&head);
9412 err = nfs4_handle_exception(server, nfs4err, exception);
9414 if (exception->retry)
9420 dprintk("<-- %s\n", __func__);
9424 size_t max_response_pages(struct nfs_server *server)
9426 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
9427 return nfs_page_array_len(0, max_resp_sz);
9430 static void nfs4_layoutget_release(void *calldata)
9432 struct nfs4_layoutget *lgp = calldata;
9434 dprintk("--> %s\n", __func__);
9435 nfs4_sequence_free_slot(&lgp->res.seq_res);
9436 pnfs_layoutget_free(lgp);
9437 dprintk("<-- %s\n", __func__);
9440 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
9441 .rpc_call_prepare = nfs4_layoutget_prepare,
9442 .rpc_call_done = nfs4_layoutget_done,
9443 .rpc_release = nfs4_layoutget_release,
9446 struct pnfs_layout_segment *
9447 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
9449 struct inode *inode = lgp->args.inode;
9450 struct nfs_server *server = NFS_SERVER(inode);
9451 struct rpc_task *task;
9452 struct rpc_message msg = {
9453 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
9454 .rpc_argp = &lgp->args,
9455 .rpc_resp = &lgp->res,
9456 .rpc_cred = lgp->cred,
9458 struct rpc_task_setup task_setup_data = {
9459 .rpc_client = server->client,
9460 .rpc_message = &msg,
9461 .callback_ops = &nfs4_layoutget_call_ops,
9462 .callback_data = lgp,
9463 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
9465 struct pnfs_layout_segment *lseg = NULL;
9466 struct nfs4_exception exception = {
9468 .timeout = *timeout,
9472 dprintk("--> %s\n", __func__);
9474 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
9475 pnfs_get_layout_hdr(NFS_I(inode)->layout);
9477 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
9479 task = rpc_run_task(&task_setup_data);
9481 status = rpc_wait_for_completion_task(task);
9485 if (task->tk_status < 0) {
9486 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
9487 *timeout = exception.timeout;
9488 } else if (lgp->res.layoutp->len == 0) {
9490 *timeout = nfs4_update_delay(&exception.timeout);
9492 lseg = pnfs_layout_process(lgp);
9494 trace_nfs4_layoutget(lgp->args.ctx,
9501 dprintk("<-- %s status=%d\n", __func__, status);
9503 return ERR_PTR(status);
9508 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
9510 struct nfs4_layoutreturn *lrp = calldata;
9512 dprintk("--> %s\n", __func__);
9513 nfs4_setup_sequence(lrp->clp,
9514 &lrp->args.seq_args,
9517 if (!pnfs_layout_is_valid(lrp->args.layout))
9521 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
9523 struct nfs4_layoutreturn *lrp = calldata;
9524 struct nfs_server *server;
9526 dprintk("--> %s\n", __func__);
9528 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
9532 * Was there an RPC level error? Assume the call succeeded,
9533 * and that we need to release the layout
9535 if (task->tk_rpc_status != 0 && RPC_WAS_SENT(task)) {
9536 lrp->res.lrs_present = 0;
9540 server = NFS_SERVER(lrp->args.inode);
9541 switch (task->tk_status) {
9542 case -NFS4ERR_OLD_STATEID:
9543 if (nfs4_layout_refresh_old_stateid(&lrp->args.stateid,
9549 task->tk_status = 0;
9553 case -NFS4ERR_DELAY:
9554 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
9558 dprintk("<-- %s\n", __func__);
9561 task->tk_status = 0;
9562 nfs4_sequence_free_slot(&lrp->res.seq_res);
9563 rpc_restart_call_prepare(task);
9566 static void nfs4_layoutreturn_release(void *calldata)
9568 struct nfs4_layoutreturn *lrp = calldata;
9569 struct pnfs_layout_hdr *lo = lrp->args.layout;
9571 dprintk("--> %s\n", __func__);
9572 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
9573 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
9574 nfs4_sequence_free_slot(&lrp->res.seq_res);
9575 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9576 lrp->ld_private.ops->free(&lrp->ld_private);
9577 pnfs_put_layout_hdr(lrp->args.layout);
9578 nfs_iput_and_deactive(lrp->inode);
9579 put_cred(lrp->cred);
9581 dprintk("<-- %s\n", __func__);
9584 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9585 .rpc_call_prepare = nfs4_layoutreturn_prepare,
9586 .rpc_call_done = nfs4_layoutreturn_done,
9587 .rpc_release = nfs4_layoutreturn_release,
9590 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
9592 struct rpc_task *task;
9593 struct rpc_message msg = {
9594 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9595 .rpc_argp = &lrp->args,
9596 .rpc_resp = &lrp->res,
9597 .rpc_cred = lrp->cred,
9599 struct rpc_task_setup task_setup_data = {
9600 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
9601 .rpc_message = &msg,
9602 .callback_ops = &nfs4_layoutreturn_call_ops,
9603 .callback_data = lrp,
9607 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9608 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9609 &task_setup_data.rpc_client, &msg);
9611 dprintk("--> %s\n", __func__);
9613 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
9615 nfs4_layoutreturn_release(lrp);
9618 task_setup_data.flags |= RPC_TASK_ASYNC;
9620 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1, 0);
9621 task = rpc_run_task(&task_setup_data);
9623 return PTR_ERR(task);
9625 status = task->tk_status;
9626 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
9627 dprintk("<-- %s status=%d\n", __func__, status);
9633 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
9634 struct pnfs_device *pdev,
9635 const struct cred *cred)
9637 struct nfs4_getdeviceinfo_args args = {
9639 .notify_types = NOTIFY_DEVICEID4_CHANGE |
9640 NOTIFY_DEVICEID4_DELETE,
9642 struct nfs4_getdeviceinfo_res res = {
9645 struct rpc_message msg = {
9646 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9653 dprintk("--> %s\n", __func__);
9654 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
9655 if (res.notification & ~args.notify_types)
9656 dprintk("%s: unsupported notification\n", __func__);
9657 if (res.notification != args.notify_types)
9660 trace_nfs4_getdeviceinfo(server, &pdev->dev_id, status);
9662 dprintk("<-- %s status=%d\n", __func__, status);
9667 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9668 struct pnfs_device *pdev,
9669 const struct cred *cred)
9671 struct nfs4_exception exception = { };
9675 err = nfs4_handle_exception(server,
9676 _nfs4_proc_getdeviceinfo(server, pdev, cred),
9678 } while (exception.retry);
9681 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9683 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9685 struct nfs4_layoutcommit_data *data = calldata;
9686 struct nfs_server *server = NFS_SERVER(data->args.inode);
9688 nfs4_setup_sequence(server->nfs_client,
9689 &data->args.seq_args,
9695 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9697 struct nfs4_layoutcommit_data *data = calldata;
9698 struct nfs_server *server = NFS_SERVER(data->args.inode);
9700 if (!nfs41_sequence_done(task, &data->res.seq_res))
9703 switch (task->tk_status) { /* Just ignore these failures */
9704 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9705 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9706 case -NFS4ERR_BADLAYOUT: /* no layout */
9707 case -NFS4ERR_GRACE: /* loca_recalim always false */
9708 task->tk_status = 0;
9713 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
9714 rpc_restart_call_prepare(task);
9720 static void nfs4_layoutcommit_release(void *calldata)
9722 struct nfs4_layoutcommit_data *data = calldata;
9724 pnfs_cleanup_layoutcommit(data);
9725 nfs_post_op_update_inode_force_wcc(data->args.inode,
9727 put_cred(data->cred);
9728 nfs_iput_and_deactive(data->inode);
9732 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9733 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9734 .rpc_call_done = nfs4_layoutcommit_done,
9735 .rpc_release = nfs4_layoutcommit_release,
9739 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
9741 struct rpc_message msg = {
9742 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9743 .rpc_argp = &data->args,
9744 .rpc_resp = &data->res,
9745 .rpc_cred = data->cred,
9747 struct rpc_task_setup task_setup_data = {
9748 .task = &data->task,
9749 .rpc_client = NFS_CLIENT(data->args.inode),
9750 .rpc_message = &msg,
9751 .callback_ops = &nfs4_layoutcommit_ops,
9752 .callback_data = data,
9754 struct rpc_task *task;
9757 dprintk("NFS: initiating layoutcommit call. sync %d "
9758 "lbw: %llu inode %lu\n", sync,
9759 data->args.lastbytewritten,
9760 data->args.inode->i_ino);
9763 data->inode = nfs_igrab_and_active(data->args.inode);
9764 if (data->inode == NULL) {
9765 nfs4_layoutcommit_release(data);
9768 task_setup_data.flags = RPC_TASK_ASYNC;
9770 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
9771 task = rpc_run_task(&task_setup_data);
9773 return PTR_ERR(task);
9775 status = task->tk_status;
9776 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
9777 dprintk("%s: status %d\n", __func__, status);
9783 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9784 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9787 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9788 struct nfs_fsinfo *info,
9789 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
9791 struct nfs41_secinfo_no_name_args args = {
9792 .style = SECINFO_STYLE_CURRENT_FH,
9794 struct nfs4_secinfo_res res = {
9797 struct rpc_message msg = {
9798 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9802 struct nfs4_call_sync_data data = {
9803 .seq_server = server,
9804 .seq_args = &args.seq_args,
9805 .seq_res = &res.seq_res,
9807 struct rpc_task_setup task_setup = {
9808 .rpc_client = server->client,
9809 .rpc_message = &msg,
9810 .callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
9811 .callback_data = &data,
9812 .flags = RPC_TASK_NO_ROUND_ROBIN,
9814 const struct cred *cred = NULL;
9817 if (use_integrity) {
9818 task_setup.rpc_client = server->nfs_client->cl_rpcclient;
9820 cred = nfs4_get_clid_cred(server->nfs_client);
9821 msg.rpc_cred = cred;
9824 dprintk("--> %s\n", __func__);
9825 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
9826 status = nfs4_call_sync_custom(&task_setup);
9827 dprintk("<-- %s status=%d\n", __func__, status);
9835 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9836 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9838 struct nfs4_exception exception = {
9839 .interruptible = true,
9843 /* first try using integrity protection */
9844 err = -NFS4ERR_WRONGSEC;
9846 /* try to use integrity protection with machine cred */
9847 if (_nfs4_is_integrity_protected(server->nfs_client))
9848 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9852 * if unable to use integrity protection, or SECINFO with
9853 * integrity protection returns NFS4ERR_WRONGSEC (which is
9854 * disallowed by spec, but exists in deployed servers) use
9855 * the current filesystem's rpc_client and the user cred.
9857 if (err == -NFS4ERR_WRONGSEC)
9858 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9863 case -NFS4ERR_WRONGSEC:
9867 err = nfs4_handle_exception(server, err, &exception);
9869 } while (exception.retry);
9875 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9876 struct nfs_fsinfo *info)
9880 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
9881 struct nfs4_secinfo_flavors *flavors;
9882 struct nfs4_secinfo4 *secinfo;
9885 page = alloc_page(GFP_KERNEL);
9891 flavors = page_address(page);
9892 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9895 * Fall back on "guess and check" method if
9896 * the server doesn't support SECINFO_NO_NAME
9898 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
9899 err = nfs4_find_root_sec(server, fhandle, info);
9905 for (i = 0; i < flavors->num_flavors; i++) {
9906 secinfo = &flavors->flavors[i];
9908 switch (secinfo->flavor) {
9912 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9913 &secinfo->flavor_info);
9916 flavor = RPC_AUTH_MAXFLAVOR;
9920 if (!nfs_auth_info_match(&server->auth_info, flavor))
9921 flavor = RPC_AUTH_MAXFLAVOR;
9923 if (flavor != RPC_AUTH_MAXFLAVOR) {
9924 err = nfs4_lookup_root_sec(server, fhandle,
9931 if (flavor == RPC_AUTH_MAXFLAVOR)
9942 static int _nfs41_test_stateid(struct nfs_server *server,
9943 nfs4_stateid *stateid,
9944 const struct cred *cred)
9947 struct nfs41_test_stateid_args args = {
9950 struct nfs41_test_stateid_res res;
9951 struct rpc_message msg = {
9952 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9957 struct rpc_clnt *rpc_client = server->client;
9959 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9962 dprintk("NFS call test_stateid %p\n", stateid);
9963 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
9964 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
9965 &args.seq_args, &res.seq_res);
9966 if (status != NFS_OK) {
9967 dprintk("NFS reply test_stateid: failed, %d\n", status);
9970 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
9974 static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9975 int err, struct nfs4_exception *exception)
9977 exception->retry = 0;
9979 case -NFS4ERR_DELAY:
9980 case -NFS4ERR_RETRY_UNCACHED_REP:
9981 nfs4_handle_exception(server, err, exception);
9983 case -NFS4ERR_BADSESSION:
9984 case -NFS4ERR_BADSLOT:
9985 case -NFS4ERR_BAD_HIGH_SLOT:
9986 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9987 case -NFS4ERR_DEADSESSION:
9988 nfs4_do_handle_exception(server, err, exception);
9993 * nfs41_test_stateid - perform a TEST_STATEID operation
9995 * @server: server / transport on which to perform the operation
9996 * @stateid: state ID to test
9999 * Returns NFS_OK if the server recognizes that "stateid" is valid.
10000 * Otherwise a negative NFS4ERR value is returned if the operation
10001 * failed or the state ID is not currently valid.
10003 static int nfs41_test_stateid(struct nfs_server *server,
10004 nfs4_stateid *stateid,
10005 const struct cred *cred)
10007 struct nfs4_exception exception = {
10008 .interruptible = true,
10012 err = _nfs41_test_stateid(server, stateid, cred);
10013 nfs4_handle_delay_or_session_error(server, err, &exception);
10014 } while (exception.retry);
10018 struct nfs_free_stateid_data {
10019 struct nfs_server *server;
10020 struct nfs41_free_stateid_args args;
10021 struct nfs41_free_stateid_res res;
10024 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
10026 struct nfs_free_stateid_data *data = calldata;
10027 nfs4_setup_sequence(data->server->nfs_client,
10028 &data->args.seq_args,
10029 &data->res.seq_res,
10033 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
10035 struct nfs_free_stateid_data *data = calldata;
10037 nfs41_sequence_done(task, &data->res.seq_res);
10039 switch (task->tk_status) {
10040 case -NFS4ERR_DELAY:
10041 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
10042 rpc_restart_call_prepare(task);
10046 static void nfs41_free_stateid_release(void *calldata)
10051 static const struct rpc_call_ops nfs41_free_stateid_ops = {
10052 .rpc_call_prepare = nfs41_free_stateid_prepare,
10053 .rpc_call_done = nfs41_free_stateid_done,
10054 .rpc_release = nfs41_free_stateid_release,
10058 * nfs41_free_stateid - perform a FREE_STATEID operation
10060 * @server: server / transport on which to perform the operation
10061 * @stateid: state ID to release
10062 * @cred: credential
10063 * @privileged: set to true if this call needs to be privileged
10065 * Note: this function is always asynchronous.
10067 static int nfs41_free_stateid(struct nfs_server *server,
10068 const nfs4_stateid *stateid,
10069 const struct cred *cred,
10072 struct rpc_message msg = {
10073 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
10076 struct rpc_task_setup task_setup = {
10077 .rpc_client = server->client,
10078 .rpc_message = &msg,
10079 .callback_ops = &nfs41_free_stateid_ops,
10080 .flags = RPC_TASK_ASYNC,
10082 struct nfs_free_stateid_data *data;
10083 struct rpc_task *task;
10085 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
10086 &task_setup.rpc_client, &msg);
10088 dprintk("NFS call free_stateid %p\n", stateid);
10089 data = kmalloc(sizeof(*data), GFP_NOFS);
10092 data->server = server;
10093 nfs4_stateid_copy(&data->args.stateid, stateid);
10095 task_setup.callback_data = data;
10097 msg.rpc_argp = &data->args;
10098 msg.rpc_resp = &data->res;
10099 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
10100 task = rpc_run_task(&task_setup);
10102 return PTR_ERR(task);
10103 rpc_put_task(task);
10108 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
10110 const struct cred *cred = lsp->ls_state->owner->so_cred;
10112 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
10113 nfs4_free_lock_state(server, lsp);
10116 static bool nfs41_match_stateid(const nfs4_stateid *s1,
10117 const nfs4_stateid *s2)
10119 if (s1->type != s2->type)
10122 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
10125 if (s1->seqid == s2->seqid)
10128 return s1->seqid == 0 || s2->seqid == 0;
10131 #endif /* CONFIG_NFS_V4_1 */
10133 static bool nfs4_match_stateid(const nfs4_stateid *s1,
10134 const nfs4_stateid *s2)
10136 return nfs4_stateid_match(s1, s2);
10140 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
10141 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
10142 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
10143 .recover_open = nfs4_open_reclaim,
10144 .recover_lock = nfs4_lock_reclaim,
10145 .establish_clid = nfs4_init_clientid,
10146 .detect_trunking = nfs40_discover_server_trunking,
10149 #if defined(CONFIG_NFS_V4_1)
10150 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
10151 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
10152 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
10153 .recover_open = nfs4_open_reclaim,
10154 .recover_lock = nfs4_lock_reclaim,
10155 .establish_clid = nfs41_init_clientid,
10156 .reclaim_complete = nfs41_proc_reclaim_complete,
10157 .detect_trunking = nfs41_discover_server_trunking,
10159 #endif /* CONFIG_NFS_V4_1 */
10161 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
10162 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
10163 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
10164 .recover_open = nfs40_open_expired,
10165 .recover_lock = nfs4_lock_expired,
10166 .establish_clid = nfs4_init_clientid,
10169 #if defined(CONFIG_NFS_V4_1)
10170 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
10171 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
10172 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
10173 .recover_open = nfs41_open_expired,
10174 .recover_lock = nfs41_lock_expired,
10175 .establish_clid = nfs41_init_clientid,
10177 #endif /* CONFIG_NFS_V4_1 */
10179 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
10180 .sched_state_renewal = nfs4_proc_async_renew,
10181 .get_state_renewal_cred = nfs4_get_renew_cred,
10182 .renew_lease = nfs4_proc_renew,
10185 #if defined(CONFIG_NFS_V4_1)
10186 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
10187 .sched_state_renewal = nfs41_proc_async_sequence,
10188 .get_state_renewal_cred = nfs4_get_machine_cred,
10189 .renew_lease = nfs4_proc_sequence,
10193 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
10194 .get_locations = _nfs40_proc_get_locations,
10195 .fsid_present = _nfs40_proc_fsid_present,
10198 #if defined(CONFIG_NFS_V4_1)
10199 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
10200 .get_locations = _nfs41_proc_get_locations,
10201 .fsid_present = _nfs41_proc_fsid_present,
10203 #endif /* CONFIG_NFS_V4_1 */
10205 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
10206 .minor_version = 0,
10207 .init_caps = NFS_CAP_READDIRPLUS
10208 | NFS_CAP_ATOMIC_OPEN
10209 | NFS_CAP_POSIX_LOCK,
10210 .init_client = nfs40_init_client,
10211 .shutdown_client = nfs40_shutdown_client,
10212 .match_stateid = nfs4_match_stateid,
10213 .find_root_sec = nfs4_find_root_sec,
10214 .free_lock_state = nfs4_release_lockowner,
10215 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
10216 .alloc_seqid = nfs_alloc_seqid,
10217 .call_sync_ops = &nfs40_call_sync_ops,
10218 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
10219 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
10220 .state_renewal_ops = &nfs40_state_renewal_ops,
10221 .mig_recovery_ops = &nfs40_mig_recovery_ops,
10224 #if defined(CONFIG_NFS_V4_1)
10225 static struct nfs_seqid *
10226 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
10231 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
10232 .minor_version = 1,
10233 .init_caps = NFS_CAP_READDIRPLUS
10234 | NFS_CAP_ATOMIC_OPEN
10235 | NFS_CAP_POSIX_LOCK
10236 | NFS_CAP_STATEID_NFSV41
10237 | NFS_CAP_ATOMIC_OPEN_V1
10239 .init_client = nfs41_init_client,
10240 .shutdown_client = nfs41_shutdown_client,
10241 .match_stateid = nfs41_match_stateid,
10242 .find_root_sec = nfs41_find_root_sec,
10243 .free_lock_state = nfs41_free_lock_state,
10244 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
10245 .alloc_seqid = nfs_alloc_no_seqid,
10246 .session_trunk = nfs4_test_session_trunk,
10247 .call_sync_ops = &nfs41_call_sync_ops,
10248 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10249 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10250 .state_renewal_ops = &nfs41_state_renewal_ops,
10251 .mig_recovery_ops = &nfs41_mig_recovery_ops,
10255 #if defined(CONFIG_NFS_V4_2)
10256 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
10257 .minor_version = 2,
10258 .init_caps = NFS_CAP_READDIRPLUS
10259 | NFS_CAP_ATOMIC_OPEN
10260 | NFS_CAP_POSIX_LOCK
10261 | NFS_CAP_STATEID_NFSV41
10262 | NFS_CAP_ATOMIC_OPEN_V1
10266 | NFS_CAP_OFFLOAD_CANCEL
10267 | NFS_CAP_COPY_NOTIFY
10268 | NFS_CAP_DEALLOCATE
10270 | NFS_CAP_LAYOUTSTATS
10272 | NFS_CAP_LAYOUTERROR
10273 | NFS_CAP_READ_PLUS,
10274 .init_client = nfs41_init_client,
10275 .shutdown_client = nfs41_shutdown_client,
10276 .match_stateid = nfs41_match_stateid,
10277 .find_root_sec = nfs41_find_root_sec,
10278 .free_lock_state = nfs41_free_lock_state,
10279 .call_sync_ops = &nfs41_call_sync_ops,
10280 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
10281 .alloc_seqid = nfs_alloc_no_seqid,
10282 .session_trunk = nfs4_test_session_trunk,
10283 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10284 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10285 .state_renewal_ops = &nfs41_state_renewal_ops,
10286 .mig_recovery_ops = &nfs41_mig_recovery_ops,
10290 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
10291 [0] = &nfs_v4_0_minor_ops,
10292 #if defined(CONFIG_NFS_V4_1)
10293 [1] = &nfs_v4_1_minor_ops,
10295 #if defined(CONFIG_NFS_V4_2)
10296 [2] = &nfs_v4_2_minor_ops,
10300 static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
10302 ssize_t error, error2, error3;
10304 error = generic_listxattr(dentry, list, size);
10312 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
10321 error3 = nfs4_listxattr_nfs4_user(d_inode(dentry), list, size);
10325 return error + error2 + error3;
10328 static const struct inode_operations nfs4_dir_inode_operations = {
10329 .create = nfs_create,
10330 .lookup = nfs_lookup,
10331 .atomic_open = nfs_atomic_open,
10333 .unlink = nfs_unlink,
10334 .symlink = nfs_symlink,
10335 .mkdir = nfs_mkdir,
10336 .rmdir = nfs_rmdir,
10337 .mknod = nfs_mknod,
10338 .rename = nfs_rename,
10339 .permission = nfs_permission,
10340 .getattr = nfs_getattr,
10341 .setattr = nfs_setattr,
10342 .listxattr = nfs4_listxattr,
10345 static const struct inode_operations nfs4_file_inode_operations = {
10346 .permission = nfs_permission,
10347 .getattr = nfs_getattr,
10348 .setattr = nfs_setattr,
10349 .listxattr = nfs4_listxattr,
10352 const struct nfs_rpc_ops nfs_v4_clientops = {
10353 .version = 4, /* protocol version */
10354 .dentry_ops = &nfs4_dentry_operations,
10355 .dir_inode_ops = &nfs4_dir_inode_operations,
10356 .file_inode_ops = &nfs4_file_inode_operations,
10357 .file_ops = &nfs4_file_operations,
10358 .getroot = nfs4_proc_get_root,
10359 .submount = nfs4_submount,
10360 .try_get_tree = nfs4_try_get_tree,
10361 .getattr = nfs4_proc_getattr,
10362 .setattr = nfs4_proc_setattr,
10363 .lookup = nfs4_proc_lookup,
10364 .lookupp = nfs4_proc_lookupp,
10365 .access = nfs4_proc_access,
10366 .readlink = nfs4_proc_readlink,
10367 .create = nfs4_proc_create,
10368 .remove = nfs4_proc_remove,
10369 .unlink_setup = nfs4_proc_unlink_setup,
10370 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
10371 .unlink_done = nfs4_proc_unlink_done,
10372 .rename_setup = nfs4_proc_rename_setup,
10373 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
10374 .rename_done = nfs4_proc_rename_done,
10375 .link = nfs4_proc_link,
10376 .symlink = nfs4_proc_symlink,
10377 .mkdir = nfs4_proc_mkdir,
10378 .rmdir = nfs4_proc_rmdir,
10379 .readdir = nfs4_proc_readdir,
10380 .mknod = nfs4_proc_mknod,
10381 .statfs = nfs4_proc_statfs,
10382 .fsinfo = nfs4_proc_fsinfo,
10383 .pathconf = nfs4_proc_pathconf,
10384 .set_capabilities = nfs4_server_capabilities,
10385 .decode_dirent = nfs4_decode_dirent,
10386 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
10387 .read_setup = nfs4_proc_read_setup,
10388 .read_done = nfs4_read_done,
10389 .write_setup = nfs4_proc_write_setup,
10390 .write_done = nfs4_write_done,
10391 .commit_setup = nfs4_proc_commit_setup,
10392 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
10393 .commit_done = nfs4_commit_done,
10394 .lock = nfs4_proc_lock,
10395 .clear_acl_cache = nfs4_zap_acl_attr,
10396 .close_context = nfs4_close_context,
10397 .open_context = nfs4_atomic_open,
10398 .have_delegation = nfs4_have_delegation,
10399 .alloc_client = nfs4_alloc_client,
10400 .init_client = nfs4_init_client,
10401 .free_client = nfs4_free_client,
10402 .create_server = nfs4_create_server,
10403 .clone_server = nfs_clone_server,
10406 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
10407 .name = XATTR_NAME_NFSV4_ACL,
10408 .list = nfs4_xattr_list_nfs4_acl,
10409 .get = nfs4_xattr_get_nfs4_acl,
10410 .set = nfs4_xattr_set_nfs4_acl,
10413 #ifdef CONFIG_NFS_V4_2
10414 static const struct xattr_handler nfs4_xattr_nfs4_user_handler = {
10415 .prefix = XATTR_USER_PREFIX,
10416 .get = nfs4_xattr_get_nfs4_user,
10417 .set = nfs4_xattr_set_nfs4_user,
10421 const struct xattr_handler *nfs4_xattr_handlers[] = {
10422 &nfs4_xattr_nfs4_acl_handler,
10423 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
10424 &nfs4_xattr_nfs4_label_handler,
10426 #ifdef CONFIG_NFS_V4_2
10427 &nfs4_xattr_nfs4_user_handler,
10434 * c-basic-offset: 8