4 * Client-side procedure declarations for NFSv4.
6 * Copyright (c) 2002 The Regents of the University of Michigan.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <linux/delay.h>
40 #include <linux/errno.h>
41 #include <linux/string.h>
42 #include <linux/ratelimit.h>
43 #include <linux/printk.h>
44 #include <linux/slab.h>
45 #include <linux/sunrpc/clnt.h>
46 #include <linux/nfs.h>
47 #include <linux/nfs4.h>
48 #include <linux/nfs_fs.h>
49 #include <linux/nfs_page.h>
50 #include <linux/nfs_mount.h>
51 #include <linux/namei.h>
52 #include <linux/mount.h>
53 #include <linux/module.h>
54 #include <linux/xattr.h>
55 #include <linux/utsname.h>
56 #include <linux/freezer.h>
57 #include <linux/iversion.h>
60 #include "delegation.h"
67 #include "nfs4idmap.h"
68 #include "nfs4session.h"
72 #include "nfs4trace.h"
74 #define NFSDBG_FACILITY NFSDBG_PROC
76 #define NFS4_BITMASK_SZ 3
78 #define NFS4_POLL_RETRY_MIN (HZ/10)
79 #define NFS4_POLL_RETRY_MAX (15*HZ)
81 /* file attributes which can be mapped to nfs attributes */
82 #define NFS4_VALID_ATTRS (ATTR_MODE \
93 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
94 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
95 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
96 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label, struct inode *inode);
97 static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
98 struct nfs_fattr *fattr, struct iattr *sattr,
99 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
100 struct nfs4_label *olabel);
101 #ifdef CONFIG_NFS_V4_1
102 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
103 const struct cred *cred,
104 struct nfs4_slot *slot,
106 static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
107 const struct cred *);
108 static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
109 const struct cred *, bool);
111 static void nfs4_bitmask_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_set_cache_invalid(inode, NFS_INO_INVALID_OTHER);
1177 nfs4_dec_nlink_locked(struct inode *inode)
1179 nfs_set_cache_invalid(inode, 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 cache_validity |= NFS_INO_INVALID_CTIME | NFS_INO_INVALID_MTIME;
1192 if (cinfo->atomic && cinfo->before == inode_peek_iversion_raw(inode)) {
1193 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1194 nfsi->attrtimeo_timestamp = jiffies;
1196 if (S_ISDIR(inode->i_mode)) {
1197 cache_validity |= NFS_INO_INVALID_DATA;
1198 nfs_force_lookup_revalidate(inode);
1200 if (!NFS_PROTO(inode)->have_delegation(inode,
1202 cache_validity |= NFS_INO_REVAL_PAGECACHE;
1205 if (cinfo->before != inode_peek_iversion_raw(inode))
1206 cache_validity |= NFS_INO_INVALID_ACCESS |
1207 NFS_INO_INVALID_ACL |
1208 NFS_INO_INVALID_XATTR;
1210 inode_set_iversion_raw(inode, cinfo->after);
1211 nfsi->read_cache_jiffies = timestamp;
1212 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
1213 nfs_set_cache_invalid(inode, cache_validity);
1214 nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE;
1218 nfs4_update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1219 unsigned long timestamp, unsigned long cache_validity)
1221 spin_lock(&dir->i_lock);
1222 nfs4_update_changeattr_locked(dir, cinfo, timestamp, cache_validity);
1223 spin_unlock(&dir->i_lock);
1226 struct nfs4_open_createattrs {
1227 struct nfs4_label *label;
1228 struct iattr *sattr;
1229 const __u32 verf[2];
1232 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1233 int err, struct nfs4_exception *exception)
1237 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1239 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1240 exception->retry = 1;
1244 static fmode_t _nfs4_ctx_to_accessmode(const struct nfs_open_context *ctx)
1246 return ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
1249 static fmode_t _nfs4_ctx_to_openmode(const struct nfs_open_context *ctx)
1251 fmode_t ret = ctx->mode & (FMODE_READ|FMODE_WRITE);
1253 return (ctx->mode & FMODE_EXEC) ? FMODE_READ | ret : ret;
1257 nfs4_map_atomic_open_share(struct nfs_server *server,
1258 fmode_t fmode, int openflags)
1262 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1264 res = NFS4_SHARE_ACCESS_READ;
1267 res = NFS4_SHARE_ACCESS_WRITE;
1269 case FMODE_READ|FMODE_WRITE:
1270 res = NFS4_SHARE_ACCESS_BOTH;
1272 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1274 /* Want no delegation if we're using O_DIRECT */
1275 if (openflags & O_DIRECT)
1276 res |= NFS4_SHARE_WANT_NO_DELEG;
1281 static enum open_claim_type4
1282 nfs4_map_atomic_open_claim(struct nfs_server *server,
1283 enum open_claim_type4 claim)
1285 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1290 case NFS4_OPEN_CLAIM_FH:
1291 return NFS4_OPEN_CLAIM_NULL;
1292 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1293 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1294 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1295 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1299 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
1301 p->o_res.f_attr = &p->f_attr;
1302 p->o_res.f_label = p->f_label;
1303 p->o_res.seqid = p->o_arg.seqid;
1304 p->c_res.seqid = p->c_arg.seqid;
1305 p->o_res.server = p->o_arg.server;
1306 p->o_res.access_request = p->o_arg.access;
1307 nfs_fattr_init(&p->f_attr);
1308 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
1311 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
1312 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
1313 const struct nfs4_open_createattrs *c,
1314 enum open_claim_type4 claim,
1317 struct dentry *parent = dget_parent(dentry);
1318 struct inode *dir = d_inode(parent);
1319 struct nfs_server *server = NFS_SERVER(dir);
1320 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
1321 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
1322 struct nfs4_opendata *p;
1324 p = kzalloc(sizeof(*p), gfp_mask);
1328 p->f_label = nfs4_label_alloc(server, gfp_mask);
1329 if (IS_ERR(p->f_label))
1332 p->a_label = nfs4_label_alloc(server, gfp_mask);
1333 if (IS_ERR(p->a_label))
1336 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1337 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
1338 if (IS_ERR(p->o_arg.seqid))
1339 goto err_free_label;
1340 nfs_sb_active(dentry->d_sb);
1341 p->dentry = dget(dentry);
1344 atomic_inc(&sp->so_count);
1345 p->o_arg.open_flags = flags;
1346 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
1347 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1348 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1350 if (flags & O_CREAT) {
1351 p->o_arg.umask = current_umask();
1352 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1353 if (c->sattr != NULL && c->sattr->ia_valid != 0) {
1354 p->o_arg.u.attrs = &p->attrs;
1355 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
1357 memcpy(p->o_arg.u.verifier.data, c->verf,
1358 sizeof(p->o_arg.u.verifier.data));
1361 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1362 * will return permission denied for all bits until close */
1363 if (!(flags & O_EXCL)) {
1364 /* ask server to check for all possible rights as results
1366 switch (p->o_arg.claim) {
1369 case NFS4_OPEN_CLAIM_NULL:
1370 case NFS4_OPEN_CLAIM_FH:
1371 p->o_arg.access = NFS4_ACCESS_READ |
1372 NFS4_ACCESS_MODIFY |
1373 NFS4_ACCESS_EXTEND |
1374 NFS4_ACCESS_EXECUTE;
1375 #ifdef CONFIG_NFS_V4_2
1376 if (server->caps & NFS_CAP_XATTR)
1377 p->o_arg.access |= NFS4_ACCESS_XAREAD |
1378 NFS4_ACCESS_XAWRITE |
1383 p->o_arg.clientid = server->nfs_client->cl_clientid;
1384 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1385 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1386 p->o_arg.name = &dentry->d_name;
1387 p->o_arg.server = server;
1388 p->o_arg.bitmask = nfs4_bitmask(server, label);
1389 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1390 switch (p->o_arg.claim) {
1391 case NFS4_OPEN_CLAIM_NULL:
1392 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1393 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1394 p->o_arg.fh = NFS_FH(dir);
1396 case NFS4_OPEN_CLAIM_PREVIOUS:
1397 case NFS4_OPEN_CLAIM_FH:
1398 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1399 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1400 p->o_arg.fh = NFS_FH(d_inode(dentry));
1402 p->c_arg.fh = &p->o_res.fh;
1403 p->c_arg.stateid = &p->o_res.stateid;
1404 p->c_arg.seqid = p->o_arg.seqid;
1405 nfs4_init_opendata_res(p);
1406 kref_init(&p->kref);
1410 nfs4_label_free(p->a_label);
1412 nfs4_label_free(p->f_label);
1420 static void nfs4_opendata_free(struct kref *kref)
1422 struct nfs4_opendata *p = container_of(kref,
1423 struct nfs4_opendata, kref);
1424 struct super_block *sb = p->dentry->d_sb;
1426 nfs4_lgopen_release(p->lgp);
1427 nfs_free_seqid(p->o_arg.seqid);
1428 nfs4_sequence_free_slot(&p->o_res.seq_res);
1429 if (p->state != NULL)
1430 nfs4_put_open_state(p->state);
1431 nfs4_put_state_owner(p->owner);
1433 nfs4_label_free(p->a_label);
1434 nfs4_label_free(p->f_label);
1438 nfs_sb_deactive(sb);
1439 nfs_fattr_free_names(&p->f_attr);
1440 kfree(p->f_attr.mdsthreshold);
1444 static void nfs4_opendata_put(struct nfs4_opendata *p)
1447 kref_put(&p->kref, nfs4_opendata_free);
1450 static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1453 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1454 case FMODE_READ|FMODE_WRITE:
1455 return state->n_rdwr != 0;
1457 return state->n_wronly != 0;
1459 return state->n_rdonly != 0;
1465 static int can_open_cached(struct nfs4_state *state, fmode_t mode,
1466 int open_mode, enum open_claim_type4 claim)
1470 if (open_mode & (O_EXCL|O_TRUNC))
1473 case NFS4_OPEN_CLAIM_NULL:
1474 case NFS4_OPEN_CLAIM_FH:
1479 switch (mode & (FMODE_READ|FMODE_WRITE)) {
1481 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1482 && state->n_rdonly != 0;
1485 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1486 && state->n_wronly != 0;
1488 case FMODE_READ|FMODE_WRITE:
1489 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1490 && state->n_rdwr != 0;
1496 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1497 enum open_claim_type4 claim)
1499 if (delegation == NULL)
1501 if ((delegation->type & fmode) != fmode)
1504 case NFS4_OPEN_CLAIM_NULL:
1505 case NFS4_OPEN_CLAIM_FH:
1507 case NFS4_OPEN_CLAIM_PREVIOUS:
1508 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1514 nfs_mark_delegation_referenced(delegation);
1518 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1527 case FMODE_READ|FMODE_WRITE:
1530 nfs4_state_set_mode_locked(state, state->state | fmode);
1533 #ifdef CONFIG_NFS_V4_1
1534 static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1536 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1538 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1540 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1544 #endif /* CONFIG_NFS_V4_1 */
1546 static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1548 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1549 wake_up_all(&state->waitq);
1552 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
1554 struct nfs_client *clp = state->owner->so_server->nfs_client;
1555 bool need_recover = false;
1557 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1558 need_recover = true;
1559 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1560 need_recover = true;
1561 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1562 need_recover = true;
1564 nfs4_state_mark_reclaim_nograce(clp, state);
1568 * Check for whether or not the caller may update the open stateid
1569 * to the value passed in by stateid.
1571 * Note: This function relies heavily on the server implementing
1572 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1574 * i.e. The stateid seqids have to be initialised to 1, and
1575 * are then incremented on every state transition.
1577 static bool nfs_stateid_is_sequential(struct nfs4_state *state,
1578 const nfs4_stateid *stateid)
1580 if (test_bit(NFS_OPEN_STATE, &state->flags)) {
1581 /* The common case - we're updating to a new sequence number */
1582 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1583 nfs4_stateid_is_next(&state->open_stateid, stateid)) {
1587 /* This is the first OPEN in this generation */
1588 if (stateid->seqid == cpu_to_be32(1))
1594 static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1596 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1598 if (state->n_wronly)
1599 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1600 if (state->n_rdonly)
1601 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1603 set_bit(NFS_O_RDWR_STATE, &state->flags);
1604 set_bit(NFS_OPEN_STATE, &state->flags);
1607 static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1608 nfs4_stateid *stateid, fmode_t fmode)
1610 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1611 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1613 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1616 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1619 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1620 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1621 clear_bit(NFS_OPEN_STATE, &state->flags);
1623 if (stateid == NULL)
1625 /* Handle OPEN+OPEN_DOWNGRADE races */
1626 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1627 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1628 nfs_resync_open_stateid_locked(state);
1631 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1632 nfs4_stateid_copy(&state->stateid, stateid);
1633 nfs4_stateid_copy(&state->open_stateid, stateid);
1634 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
1636 nfs_state_log_update_open_stateid(state);
1639 static void nfs_clear_open_stateid(struct nfs4_state *state,
1640 nfs4_stateid *arg_stateid,
1641 nfs4_stateid *stateid, fmode_t fmode)
1643 write_seqlock(&state->seqlock);
1644 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1645 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1646 nfs_clear_open_stateid_locked(state, stateid, fmode);
1647 write_sequnlock(&state->seqlock);
1648 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1649 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1652 static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1653 const nfs4_stateid *stateid, nfs4_stateid *freeme)
1654 __must_hold(&state->owner->so_lock)
1655 __must_hold(&state->seqlock)
1663 if (nfs_stateid_is_sequential(state, stateid))
1668 /* Rely on seqids for serialisation with NFSv4.0 */
1669 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1672 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1673 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1675 * Ensure we process the state changes in the same order
1676 * in which the server processed them by delaying the
1677 * update of the stateid until we are in sequence.
1679 write_sequnlock(&state->seqlock);
1680 spin_unlock(&state->owner->so_lock);
1682 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
1684 if (!signal_pending(current)) {
1685 if (schedule_timeout(5*HZ) == 0)
1691 finish_wait(&state->waitq, &wait);
1693 spin_lock(&state->owner->so_lock);
1694 write_seqlock(&state->seqlock);
1697 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1698 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1699 nfs4_stateid_copy(freeme, &state->open_stateid);
1700 nfs_test_and_clear_all_open_stateid(state);
1703 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1704 nfs4_stateid_copy(&state->stateid, stateid);
1705 nfs4_stateid_copy(&state->open_stateid, stateid);
1706 trace_nfs4_open_stateid_update(state->inode, stateid, status);
1707 nfs_state_log_update_open_stateid(state);
1710 static void nfs_state_set_open_stateid(struct nfs4_state *state,
1711 const nfs4_stateid *open_stateid,
1713 nfs4_stateid *freeme)
1716 * Protect the call to nfs4_state_set_mode_locked and
1717 * serialise the stateid update
1719 write_seqlock(&state->seqlock);
1720 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1723 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1726 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1728 case FMODE_READ|FMODE_WRITE:
1729 set_bit(NFS_O_RDWR_STATE, &state->flags);
1731 set_bit(NFS_OPEN_STATE, &state->flags);
1732 write_sequnlock(&state->seqlock);
1735 static void nfs_state_clear_open_state_flags(struct nfs4_state *state)
1737 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1738 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1739 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1740 clear_bit(NFS_OPEN_STATE, &state->flags);
1743 static void nfs_state_set_delegation(struct nfs4_state *state,
1744 const nfs4_stateid *deleg_stateid,
1748 * Protect the call to nfs4_state_set_mode_locked and
1749 * serialise the stateid update
1751 write_seqlock(&state->seqlock);
1752 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1753 set_bit(NFS_DELEGATED_STATE, &state->flags);
1754 write_sequnlock(&state->seqlock);
1757 static void nfs_state_clear_delegation(struct nfs4_state *state)
1759 write_seqlock(&state->seqlock);
1760 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1761 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1762 write_sequnlock(&state->seqlock);
1765 int update_open_stateid(struct nfs4_state *state,
1766 const nfs4_stateid *open_stateid,
1767 const nfs4_stateid *delegation,
1770 struct nfs_server *server = NFS_SERVER(state->inode);
1771 struct nfs_client *clp = server->nfs_client;
1772 struct nfs_inode *nfsi = NFS_I(state->inode);
1773 struct nfs_delegation *deleg_cur;
1774 nfs4_stateid freeme = { };
1777 fmode &= (FMODE_READ|FMODE_WRITE);
1780 spin_lock(&state->owner->so_lock);
1781 if (open_stateid != NULL) {
1782 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1786 deleg_cur = nfs4_get_valid_delegation(state->inode);
1787 if (deleg_cur == NULL)
1790 spin_lock(&deleg_cur->lock);
1791 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1792 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1793 (deleg_cur->type & fmode) != fmode)
1794 goto no_delegation_unlock;
1796 if (delegation == NULL)
1797 delegation = &deleg_cur->stateid;
1798 else if (!nfs4_stateid_match_other(&deleg_cur->stateid, delegation))
1799 goto no_delegation_unlock;
1801 nfs_mark_delegation_referenced(deleg_cur);
1802 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
1804 no_delegation_unlock:
1805 spin_unlock(&deleg_cur->lock);
1808 update_open_stateflags(state, fmode);
1809 spin_unlock(&state->owner->so_lock);
1812 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1813 nfs4_schedule_state_manager(clp);
1814 if (freeme.type != 0)
1815 nfs4_test_and_free_stateid(server, &freeme,
1816 state->owner->so_cred);
1821 static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1822 const nfs4_stateid *stateid)
1824 struct nfs4_state *state = lsp->ls_state;
1827 spin_lock(&state->state_lock);
1828 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1830 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1832 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1835 spin_unlock(&state->state_lock);
1839 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1841 struct nfs_delegation *delegation;
1843 fmode &= FMODE_READ|FMODE_WRITE;
1845 delegation = nfs4_get_valid_delegation(inode);
1846 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1851 nfs4_inode_return_delegation(inode);
1854 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1856 struct nfs4_state *state = opendata->state;
1857 struct nfs_delegation *delegation;
1858 int open_mode = opendata->o_arg.open_flags;
1859 fmode_t fmode = opendata->o_arg.fmode;
1860 enum open_claim_type4 claim = opendata->o_arg.claim;
1861 nfs4_stateid stateid;
1865 spin_lock(&state->owner->so_lock);
1866 if (can_open_cached(state, fmode, open_mode, claim)) {
1867 update_open_stateflags(state, fmode);
1868 spin_unlock(&state->owner->so_lock);
1869 goto out_return_state;
1871 spin_unlock(&state->owner->so_lock);
1873 delegation = nfs4_get_valid_delegation(state->inode);
1874 if (!can_open_delegated(delegation, fmode, claim)) {
1878 /* Save the delegation */
1879 nfs4_stateid_copy(&stateid, &delegation->stateid);
1881 nfs_release_seqid(opendata->o_arg.seqid);
1882 if (!opendata->is_recover) {
1883 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1889 /* Try to update the stateid using the delegation */
1890 if (update_open_stateid(state, NULL, &stateid, fmode))
1891 goto out_return_state;
1894 return ERR_PTR(ret);
1896 refcount_inc(&state->count);
1901 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1903 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1904 struct nfs_delegation *delegation;
1905 int delegation_flags = 0;
1908 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1910 delegation_flags = delegation->flags;
1912 switch (data->o_arg.claim) {
1915 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1916 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1917 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1918 "returning a delegation for "
1919 "OPEN(CLAIM_DELEGATE_CUR)\n",
1923 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1924 nfs_inode_set_delegation(state->inode,
1925 data->owner->so_cred,
1926 data->o_res.delegation_type,
1927 &data->o_res.delegation,
1928 data->o_res.pagemod_limit);
1930 nfs_inode_reclaim_delegation(state->inode,
1931 data->owner->so_cred,
1932 data->o_res.delegation_type,
1933 &data->o_res.delegation,
1934 data->o_res.pagemod_limit);
1936 if (data->o_res.do_recall)
1937 nfs_async_inode_return_delegation(state->inode,
1938 &data->o_res.delegation);
1942 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1943 * and update the nfs4_state.
1945 static struct nfs4_state *
1946 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1948 struct inode *inode = data->state->inode;
1949 struct nfs4_state *state = data->state;
1952 if (!data->rpc_done) {
1953 if (data->rpc_status)
1954 return ERR_PTR(data->rpc_status);
1955 /* cached opens have already been processed */
1959 ret = nfs_refresh_inode(inode, &data->f_attr);
1961 return ERR_PTR(ret);
1963 if (data->o_res.delegation_type != 0)
1964 nfs4_opendata_check_deleg(data, state);
1966 if (!update_open_stateid(state, &data->o_res.stateid,
1967 NULL, data->o_arg.fmode))
1968 return ERR_PTR(-EAGAIN);
1969 refcount_inc(&state->count);
1974 static struct inode *
1975 nfs4_opendata_get_inode(struct nfs4_opendata *data)
1977 struct inode *inode;
1979 switch (data->o_arg.claim) {
1980 case NFS4_OPEN_CLAIM_NULL:
1981 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1982 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1983 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1984 return ERR_PTR(-EAGAIN);
1985 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1986 &data->f_attr, data->f_label);
1989 inode = d_inode(data->dentry);
1991 nfs_refresh_inode(inode, &data->f_attr);
1996 static struct nfs4_state *
1997 nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
1999 struct nfs4_state *state;
2000 struct inode *inode;
2002 inode = nfs4_opendata_get_inode(data);
2004 return ERR_CAST(inode);
2005 if (data->state != NULL && data->state->inode == inode) {
2006 state = data->state;
2007 refcount_inc(&state->count);
2009 state = nfs4_get_open_state(inode, data->owner);
2012 state = ERR_PTR(-ENOMEM);
2016 static struct nfs4_state *
2017 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2019 struct nfs4_state *state;
2021 if (!data->rpc_done) {
2022 state = nfs4_try_open_cached(data);
2023 trace_nfs4_cached_open(data->state);
2027 state = nfs4_opendata_find_nfs4_state(data);
2031 if (data->o_res.delegation_type != 0)
2032 nfs4_opendata_check_deleg(data, state);
2033 if (!update_open_stateid(state, &data->o_res.stateid,
2034 NULL, data->o_arg.fmode)) {
2035 nfs4_put_open_state(state);
2036 state = ERR_PTR(-EAGAIN);
2039 nfs_release_seqid(data->o_arg.seqid);
2043 static struct nfs4_state *
2044 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2046 struct nfs4_state *ret;
2048 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
2049 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
2051 ret = _nfs4_opendata_to_nfs4_state(data);
2052 nfs4_sequence_free_slot(&data->o_res.seq_res);
2056 static struct nfs_open_context *
2057 nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode)
2059 struct nfs_inode *nfsi = NFS_I(state->inode);
2060 struct nfs_open_context *ctx;
2063 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
2064 if (ctx->state != state)
2066 if ((ctx->mode & mode) != mode)
2068 if (!get_nfs_open_context(ctx))
2074 return ERR_PTR(-ENOENT);
2077 static struct nfs_open_context *
2078 nfs4_state_find_open_context(struct nfs4_state *state)
2080 struct nfs_open_context *ctx;
2082 ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE);
2085 ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE);
2088 return nfs4_state_find_open_context_mode(state, FMODE_READ);
2091 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
2092 struct nfs4_state *state, enum open_claim_type4 claim)
2094 struct nfs4_opendata *opendata;
2096 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
2097 NULL, claim, GFP_NOFS);
2098 if (opendata == NULL)
2099 return ERR_PTR(-ENOMEM);
2100 opendata->state = state;
2101 refcount_inc(&state->count);
2105 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
2108 struct nfs4_state *newstate;
2111 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
2113 opendata->o_arg.open_flags = 0;
2114 opendata->o_arg.fmode = fmode;
2115 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
2116 NFS_SB(opendata->dentry->d_sb),
2118 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
2119 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
2120 nfs4_init_opendata_res(opendata);
2121 ret = _nfs4_recover_proc_open(opendata);
2124 newstate = nfs4_opendata_to_nfs4_state(opendata);
2125 if (IS_ERR(newstate))
2126 return PTR_ERR(newstate);
2127 if (newstate != opendata->state)
2129 nfs4_close_state(newstate, fmode);
2133 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
2137 /* memory barrier prior to reading state->n_* */
2139 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2142 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2145 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2149 * We may have performed cached opens for all three recoveries.
2150 * Check if we need to update the current stateid.
2152 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
2153 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
2154 write_seqlock(&state->seqlock);
2155 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2156 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2157 write_sequnlock(&state->seqlock);
2164 * reclaim state on the server after a reboot.
2166 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2168 struct nfs_delegation *delegation;
2169 struct nfs4_opendata *opendata;
2170 fmode_t delegation_type = 0;
2173 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2174 NFS4_OPEN_CLAIM_PREVIOUS);
2175 if (IS_ERR(opendata))
2176 return PTR_ERR(opendata);
2178 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2179 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
2180 delegation_type = delegation->type;
2182 opendata->o_arg.u.delegation_type = delegation_type;
2183 status = nfs4_open_recover(opendata, state);
2184 nfs4_opendata_put(opendata);
2188 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2190 struct nfs_server *server = NFS_SERVER(state->inode);
2191 struct nfs4_exception exception = { };
2194 err = _nfs4_do_open_reclaim(ctx, state);
2195 trace_nfs4_open_reclaim(ctx, 0, err);
2196 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2198 if (err != -NFS4ERR_DELAY)
2200 nfs4_handle_exception(server, err, &exception);
2201 } while (exception.retry);
2205 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2207 struct nfs_open_context *ctx;
2210 ctx = nfs4_state_find_open_context(state);
2213 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2214 nfs_state_clear_open_state_flags(state);
2215 ret = nfs4_do_open_reclaim(ctx, state);
2216 put_nfs_open_context(ctx);
2220 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)
2224 printk(KERN_ERR "NFS: %s: unhandled error "
2225 "%d.\n", __func__, err);
2233 case -NFS4ERR_BADSESSION:
2234 case -NFS4ERR_BADSLOT:
2235 case -NFS4ERR_BAD_HIGH_SLOT:
2236 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2237 case -NFS4ERR_DEADSESSION:
2239 case -NFS4ERR_STALE_CLIENTID:
2240 case -NFS4ERR_STALE_STATEID:
2241 /* Don't recall a delegation if it was lost */
2242 nfs4_schedule_lease_recovery(server->nfs_client);
2244 case -NFS4ERR_MOVED:
2245 nfs4_schedule_migration_recovery(server);
2247 case -NFS4ERR_LEASE_MOVED:
2248 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2250 case -NFS4ERR_DELEG_REVOKED:
2251 case -NFS4ERR_ADMIN_REVOKED:
2252 case -NFS4ERR_EXPIRED:
2253 case -NFS4ERR_BAD_STATEID:
2254 case -NFS4ERR_OPENMODE:
2255 nfs_inode_find_state_and_recover(state->inode,
2257 nfs4_schedule_stateid_recovery(server, state);
2259 case -NFS4ERR_DELAY:
2260 case -NFS4ERR_GRACE:
2264 case -NFS4ERR_DENIED:
2266 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2268 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2275 int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2276 struct nfs4_state *state, const nfs4_stateid *stateid)
2278 struct nfs_server *server = NFS_SERVER(state->inode);
2279 struct nfs4_opendata *opendata;
2282 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2283 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2284 if (IS_ERR(opendata))
2285 return PTR_ERR(opendata);
2286 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
2287 if (!test_bit(NFS_O_RDWR_STATE, &state->flags)) {
2288 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2292 if (!test_bit(NFS_O_WRONLY_STATE, &state->flags)) {
2293 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2297 if (!test_bit(NFS_O_RDONLY_STATE, &state->flags)) {
2298 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2302 nfs_state_clear_delegation(state);
2304 nfs4_opendata_put(opendata);
2305 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
2308 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2310 struct nfs4_opendata *data = calldata;
2312 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2313 &data->c_arg.seq_args, &data->c_res.seq_res, task);
2316 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2318 struct nfs4_opendata *data = calldata;
2320 nfs40_sequence_done(task, &data->c_res.seq_res);
2322 data->rpc_status = task->tk_status;
2323 if (data->rpc_status == 0) {
2324 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
2325 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2326 renew_lease(data->o_res.server, data->timestamp);
2327 data->rpc_done = true;
2331 static void nfs4_open_confirm_release(void *calldata)
2333 struct nfs4_opendata *data = calldata;
2334 struct nfs4_state *state = NULL;
2336 /* If this request hasn't been cancelled, do nothing */
2337 if (!data->cancelled)
2339 /* In case of error, no cleanup! */
2340 if (!data->rpc_done)
2342 state = nfs4_opendata_to_nfs4_state(data);
2344 nfs4_close_state(state, data->o_arg.fmode);
2346 nfs4_opendata_put(data);
2349 static const struct rpc_call_ops nfs4_open_confirm_ops = {
2350 .rpc_call_prepare = nfs4_open_confirm_prepare,
2351 .rpc_call_done = nfs4_open_confirm_done,
2352 .rpc_release = nfs4_open_confirm_release,
2356 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2358 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2360 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
2361 struct rpc_task *task;
2362 struct rpc_message msg = {
2363 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2364 .rpc_argp = &data->c_arg,
2365 .rpc_resp = &data->c_res,
2366 .rpc_cred = data->owner->so_cred,
2368 struct rpc_task_setup task_setup_data = {
2369 .rpc_client = server->client,
2370 .rpc_message = &msg,
2371 .callback_ops = &nfs4_open_confirm_ops,
2372 .callback_data = data,
2373 .workqueue = nfsiod_workqueue,
2374 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
2378 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2380 kref_get(&data->kref);
2381 data->rpc_done = false;
2382 data->rpc_status = 0;
2383 data->timestamp = jiffies;
2384 task = rpc_run_task(&task_setup_data);
2386 return PTR_ERR(task);
2387 status = rpc_wait_for_completion_task(task);
2389 data->cancelled = true;
2392 status = data->rpc_status;
2397 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
2399 struct nfs4_opendata *data = calldata;
2400 struct nfs4_state_owner *sp = data->owner;
2401 struct nfs_client *clp = sp->so_server->nfs_client;
2402 enum open_claim_type4 claim = data->o_arg.claim;
2404 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
2407 * Check if we still need to send an OPEN call, or if we can use
2408 * a delegation instead.
2410 if (data->state != NULL) {
2411 struct nfs_delegation *delegation;
2413 if (can_open_cached(data->state, data->o_arg.fmode,
2414 data->o_arg.open_flags, claim))
2417 delegation = nfs4_get_valid_delegation(data->state->inode);
2418 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
2419 goto unlock_no_action;
2422 /* Update client id. */
2423 data->o_arg.clientid = clp->cl_clientid;
2427 case NFS4_OPEN_CLAIM_PREVIOUS:
2428 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2429 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
2430 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
2432 case NFS4_OPEN_CLAIM_FH:
2433 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
2435 data->timestamp = jiffies;
2436 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
2437 &data->o_arg.seq_args,
2438 &data->o_res.seq_res,
2440 nfs_release_seqid(data->o_arg.seqid);
2442 /* Set the create mode (note dependency on the session type) */
2443 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2444 if (data->o_arg.open_flags & O_EXCL) {
2445 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2446 if (nfs4_has_persistent_session(clp))
2447 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2448 else if (clp->cl_mvops->minor_version > 0)
2449 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2453 trace_nfs4_cached_open(data->state);
2456 task->tk_action = NULL;
2458 nfs4_sequence_done(task, &data->o_res.seq_res);
2461 static void nfs4_open_done(struct rpc_task *task, void *calldata)
2463 struct nfs4_opendata *data = calldata;
2465 data->rpc_status = task->tk_status;
2467 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
2470 if (task->tk_status == 0) {
2471 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2472 switch (data->o_res.f_attr->mode & S_IFMT) {
2476 data->rpc_status = -ELOOP;
2479 data->rpc_status = -EISDIR;
2482 data->rpc_status = -ENOTDIR;
2485 renew_lease(data->o_res.server, data->timestamp);
2486 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2487 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2489 data->rpc_done = true;
2492 static void nfs4_open_release(void *calldata)
2494 struct nfs4_opendata *data = calldata;
2495 struct nfs4_state *state = NULL;
2497 /* If this request hasn't been cancelled, do nothing */
2498 if (!data->cancelled)
2500 /* In case of error, no cleanup! */
2501 if (data->rpc_status != 0 || !data->rpc_done)
2503 /* In case we need an open_confirm, no cleanup! */
2504 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2506 state = nfs4_opendata_to_nfs4_state(data);
2508 nfs4_close_state(state, data->o_arg.fmode);
2510 nfs4_opendata_put(data);
2513 static const struct rpc_call_ops nfs4_open_ops = {
2514 .rpc_call_prepare = nfs4_open_prepare,
2515 .rpc_call_done = nfs4_open_done,
2516 .rpc_release = nfs4_open_release,
2519 static int nfs4_run_open_task(struct nfs4_opendata *data,
2520 struct nfs_open_context *ctx)
2522 struct inode *dir = d_inode(data->dir);
2523 struct nfs_server *server = NFS_SERVER(dir);
2524 struct nfs_openargs *o_arg = &data->o_arg;
2525 struct nfs_openres *o_res = &data->o_res;
2526 struct rpc_task *task;
2527 struct rpc_message msg = {
2528 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2531 .rpc_cred = data->owner->so_cred,
2533 struct rpc_task_setup task_setup_data = {
2534 .rpc_client = server->client,
2535 .rpc_message = &msg,
2536 .callback_ops = &nfs4_open_ops,
2537 .callback_data = data,
2538 .workqueue = nfsiod_workqueue,
2539 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
2543 kref_get(&data->kref);
2544 data->rpc_done = false;
2545 data->rpc_status = 0;
2546 data->cancelled = false;
2547 data->is_recover = false;
2549 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
2550 data->is_recover = true;
2551 task_setup_data.flags |= RPC_TASK_TIMEOUT;
2553 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
2554 pnfs_lgopen_prepare(data, ctx);
2556 task = rpc_run_task(&task_setup_data);
2558 return PTR_ERR(task);
2559 status = rpc_wait_for_completion_task(task);
2561 data->cancelled = true;
2564 status = data->rpc_status;
2570 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2572 struct inode *dir = d_inode(data->dir);
2573 struct nfs_openres *o_res = &data->o_res;
2576 status = nfs4_run_open_task(data, NULL);
2577 if (status != 0 || !data->rpc_done)
2580 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2582 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
2583 status = _nfs4_proc_open_confirm(data);
2589 * Additional permission checks in order to distinguish between an
2590 * open for read, and an open for execute. This works around the
2591 * fact that NFSv4 OPEN treats read and execute permissions as being
2593 * Note that in the non-execute case, we want to turn off permission
2594 * checking if we just created a new file (POSIX open() semantics).
2596 static int nfs4_opendata_access(const struct cred *cred,
2597 struct nfs4_opendata *opendata,
2598 struct nfs4_state *state, fmode_t fmode,
2601 struct nfs_access_entry cache;
2604 /* access call failed or for some reason the server doesn't
2605 * support any access modes -- defer access call until later */
2606 if (opendata->o_res.access_supported == 0)
2611 * Use openflags to check for exec, because fmode won't
2612 * always have FMODE_EXEC set when file open for exec.
2614 if (openflags & __FMODE_EXEC) {
2615 /* ONLY check for exec rights */
2616 if (S_ISDIR(state->inode->i_mode))
2617 mask = NFS4_ACCESS_LOOKUP;
2619 mask = NFS4_ACCESS_EXECUTE;
2620 } else if ((fmode & FMODE_READ) && !opendata->file_created)
2621 mask = NFS4_ACCESS_READ;
2624 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2625 nfs_access_add_cache(state->inode, &cache);
2627 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2628 if ((mask & ~cache.mask & flags) == 0)
2635 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2637 static int _nfs4_proc_open(struct nfs4_opendata *data,
2638 struct nfs_open_context *ctx)
2640 struct inode *dir = d_inode(data->dir);
2641 struct nfs_server *server = NFS_SERVER(dir);
2642 struct nfs_openargs *o_arg = &data->o_arg;
2643 struct nfs_openres *o_res = &data->o_res;
2646 status = nfs4_run_open_task(data, ctx);
2647 if (!data->rpc_done)
2650 if (status == -NFS4ERR_BADNAME &&
2651 !(o_arg->open_flags & O_CREAT))
2656 nfs_fattr_map_and_free_names(server, &data->f_attr);
2658 if (o_arg->open_flags & O_CREAT) {
2659 if (o_arg->open_flags & O_EXCL)
2660 data->file_created = true;
2661 else if (o_res->cinfo.before != o_res->cinfo.after)
2662 data->file_created = true;
2663 if (data->file_created ||
2664 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
2665 nfs4_update_changeattr(dir, &o_res->cinfo,
2666 o_res->f_attr->time_start,
2667 NFS_INO_INVALID_DATA);
2669 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2670 server->caps &= ~NFS_CAP_POSIX_LOCK;
2671 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2672 status = _nfs4_proc_open_confirm(data);
2676 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2677 nfs4_sequence_free_slot(&o_res->seq_res);
2678 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr,
2679 o_res->f_label, NULL);
2686 * reclaim state on the server after a network partition.
2687 * Assumes caller holds the appropriate lock
2689 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2691 struct nfs4_opendata *opendata;
2694 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2695 NFS4_OPEN_CLAIM_FH);
2696 if (IS_ERR(opendata))
2697 return PTR_ERR(opendata);
2698 ret = nfs4_open_recover(opendata, state);
2700 d_drop(ctx->dentry);
2701 nfs4_opendata_put(opendata);
2705 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2707 struct nfs_server *server = NFS_SERVER(state->inode);
2708 struct nfs4_exception exception = { };
2712 err = _nfs4_open_expired(ctx, state);
2713 trace_nfs4_open_expired(ctx, 0, err);
2714 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2719 case -NFS4ERR_GRACE:
2720 case -NFS4ERR_DELAY:
2721 nfs4_handle_exception(server, err, &exception);
2724 } while (exception.retry);
2729 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2731 struct nfs_open_context *ctx;
2734 ctx = nfs4_state_find_open_context(state);
2737 ret = nfs4_do_open_expired(ctx, state);
2738 put_nfs_open_context(ctx);
2742 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2743 const nfs4_stateid *stateid)
2745 nfs_remove_bad_delegation(state->inode, stateid);
2746 nfs_state_clear_delegation(state);
2749 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2751 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2752 nfs_finish_clear_delegation_stateid(state, NULL);
2755 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2757 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2758 nfs40_clear_delegation_stateid(state);
2759 nfs_state_clear_open_state_flags(state);
2760 return nfs4_open_expired(sp, state);
2763 static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2764 nfs4_stateid *stateid,
2765 const struct cred *cred)
2767 return -NFS4ERR_BAD_STATEID;
2770 #if defined(CONFIG_NFS_V4_1)
2771 static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2772 nfs4_stateid *stateid,
2773 const struct cred *cred)
2777 switch (stateid->type) {
2780 case NFS4_INVALID_STATEID_TYPE:
2781 case NFS4_SPECIAL_STATEID_TYPE:
2782 return -NFS4ERR_BAD_STATEID;
2783 case NFS4_REVOKED_STATEID_TYPE:
2787 status = nfs41_test_stateid(server, stateid, cred);
2789 case -NFS4ERR_EXPIRED:
2790 case -NFS4ERR_ADMIN_REVOKED:
2791 case -NFS4ERR_DELEG_REVOKED:
2797 /* Ack the revoked state to the server */
2798 nfs41_free_stateid(server, stateid, cred, true);
2799 return -NFS4ERR_EXPIRED;
2802 static int nfs41_check_delegation_stateid(struct nfs4_state *state)
2804 struct nfs_server *server = NFS_SERVER(state->inode);
2805 nfs4_stateid stateid;
2806 struct nfs_delegation *delegation;
2807 const struct cred *cred = NULL;
2808 int status, ret = NFS_OK;
2810 /* Get the delegation credential for use by test/free_stateid */
2812 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2813 if (delegation == NULL) {
2815 nfs_state_clear_delegation(state);
2819 spin_lock(&delegation->lock);
2820 nfs4_stateid_copy(&stateid, &delegation->stateid);
2822 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2823 &delegation->flags)) {
2824 spin_unlock(&delegation->lock);
2829 if (delegation->cred)
2830 cred = get_cred(delegation->cred);
2831 spin_unlock(&delegation->lock);
2833 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
2834 trace_nfs4_test_delegation_stateid(state, NULL, status);
2835 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
2836 nfs_finish_clear_delegation_stateid(state, &stateid);
2844 static void nfs41_delegation_recover_stateid(struct nfs4_state *state)
2848 if (test_bit(NFS_DELEGATED_STATE, &state->flags) &&
2849 nfs4_copy_delegation_stateid(state->inode, state->state,
2851 nfs4_stateid_match_other(&state->stateid, &tmp))
2852 nfs_state_set_delegation(state, &tmp, state->state);
2854 nfs_state_clear_delegation(state);
2858 * nfs41_check_expired_locks - possibly free a lock stateid
2860 * @state: NFSv4 state for an inode
2862 * Returns NFS_OK if recovery for this stateid is now finished.
2863 * Otherwise a negative NFS4ERR value is returned.
2865 static int nfs41_check_expired_locks(struct nfs4_state *state)
2867 int status, ret = NFS_OK;
2868 struct nfs4_lock_state *lsp, *prev = NULL;
2869 struct nfs_server *server = NFS_SERVER(state->inode);
2871 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2874 spin_lock(&state->state_lock);
2875 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2876 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2877 const struct cred *cred = lsp->ls_state->owner->so_cred;
2879 refcount_inc(&lsp->ls_count);
2880 spin_unlock(&state->state_lock);
2882 nfs4_put_lock_state(prev);
2885 status = nfs41_test_and_free_expired_stateid(server,
2888 trace_nfs4_test_lock_stateid(state, lsp, status);
2889 if (status == -NFS4ERR_EXPIRED ||
2890 status == -NFS4ERR_BAD_STATEID) {
2891 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
2892 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
2893 if (!recover_lost_locks)
2894 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2895 } else if (status != NFS_OK) {
2897 nfs4_put_lock_state(prev);
2900 spin_lock(&state->state_lock);
2903 spin_unlock(&state->state_lock);
2904 nfs4_put_lock_state(prev);
2910 * nfs41_check_open_stateid - possibly free an open stateid
2912 * @state: NFSv4 state for an inode
2914 * Returns NFS_OK if recovery for this stateid is now finished.
2915 * Otherwise a negative NFS4ERR value is returned.
2917 static int nfs41_check_open_stateid(struct nfs4_state *state)
2919 struct nfs_server *server = NFS_SERVER(state->inode);
2920 nfs4_stateid *stateid = &state->open_stateid;
2921 const struct cred *cred = state->owner->so_cred;
2924 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0)
2925 return -NFS4ERR_BAD_STATEID;
2926 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
2927 trace_nfs4_test_open_stateid(state, NULL, status);
2928 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
2929 nfs_state_clear_open_state_flags(state);
2930 stateid->type = NFS4_INVALID_STATEID_TYPE;
2933 if (nfs_open_stateid_recover_openmode(state))
2934 return -NFS4ERR_OPENMODE;
2938 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2942 status = nfs41_check_delegation_stateid(state);
2943 if (status != NFS_OK)
2945 nfs41_delegation_recover_stateid(state);
2947 status = nfs41_check_expired_locks(state);
2948 if (status != NFS_OK)
2950 status = nfs41_check_open_stateid(state);
2951 if (status != NFS_OK)
2952 status = nfs4_open_expired(sp, state);
2958 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2959 * fields corresponding to attributes that were used to store the verifier.
2960 * Make sure we clobber those fields in the later setattr call
2962 static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2963 struct iattr *sattr, struct nfs4_label **label)
2965 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2970 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
2971 attrset[i] = opendata->o_res.attrset[i];
2972 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
2973 attrset[i] &= ~bitmask[i];
2976 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
2977 sattr->ia_valid : 0;
2979 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
2980 if (sattr->ia_valid & ATTR_ATIME_SET)
2981 ret |= ATTR_ATIME_SET;
2986 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
2987 if (sattr->ia_valid & ATTR_MTIME_SET)
2988 ret |= ATTR_MTIME_SET;
2993 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
2998 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2999 int flags, struct nfs_open_context *ctx)
3001 struct nfs4_state_owner *sp = opendata->owner;
3002 struct nfs_server *server = sp->so_server;
3003 struct dentry *dentry;
3004 struct nfs4_state *state;
3005 fmode_t acc_mode = _nfs4_ctx_to_accessmode(ctx);
3006 struct inode *dir = d_inode(opendata->dir);
3007 unsigned long dir_verifier;
3011 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
3012 dir_verifier = nfs_save_change_attribute(dir);
3014 ret = _nfs4_proc_open(opendata, ctx);
3018 state = _nfs4_opendata_to_nfs4_state(opendata);
3019 ret = PTR_ERR(state);
3023 if (server->caps & NFS_CAP_POSIX_LOCK)
3024 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
3025 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
3026 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
3028 dentry = opendata->dentry;
3029 if (d_really_is_negative(dentry)) {
3030 struct dentry *alias;
3032 alias = d_exact_alias(dentry, state->inode);
3034 alias = d_splice_alias(igrab(state->inode), dentry);
3035 /* d_splice_alias() can't fail here - it's a non-directory */
3038 ctx->dentry = dentry = alias;
3042 switch(opendata->o_arg.claim) {
3045 case NFS4_OPEN_CLAIM_NULL:
3046 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
3047 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
3048 if (!opendata->rpc_done)
3050 if (opendata->o_res.delegation_type != 0)
3051 dir_verifier = nfs_save_change_attribute(dir);
3052 nfs_set_verifier(dentry, dir_verifier);
3055 /* Parse layoutget results before we check for access */
3056 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
3058 ret = nfs4_opendata_access(sp->so_cred, opendata, state,
3063 if (d_inode(dentry) == state->inode) {
3064 nfs_inode_attach_open_context(ctx);
3065 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
3066 nfs4_schedule_stateid_recovery(server, state);
3070 if (!opendata->cancelled)
3071 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
3076 * Returns a referenced nfs4_state
3078 static int _nfs4_do_open(struct inode *dir,
3079 struct nfs_open_context *ctx,
3081 const struct nfs4_open_createattrs *c,
3084 struct nfs4_state_owner *sp;
3085 struct nfs4_state *state = NULL;
3086 struct nfs_server *server = NFS_SERVER(dir);
3087 struct nfs4_opendata *opendata;
3088 struct dentry *dentry = ctx->dentry;
3089 const struct cred *cred = ctx->cred;
3090 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
3091 fmode_t fmode = _nfs4_ctx_to_openmode(ctx);
3092 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
3093 struct iattr *sattr = c->sattr;
3094 struct nfs4_label *label = c->label;
3095 struct nfs4_label *olabel = NULL;
3098 /* Protect against reboot recovery conflicts */
3100 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
3102 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
3105 status = nfs4_client_recover_expired_lease(server->nfs_client);
3107 goto err_put_state_owner;
3108 if (d_really_is_positive(dentry))
3109 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
3111 if (d_really_is_positive(dentry))
3112 claim = NFS4_OPEN_CLAIM_FH;
3113 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
3114 c, claim, GFP_KERNEL);
3115 if (opendata == NULL)
3116 goto err_put_state_owner;
3119 olabel = nfs4_label_alloc(server, GFP_KERNEL);
3120 if (IS_ERR(olabel)) {
3121 status = PTR_ERR(olabel);
3122 goto err_opendata_put;
3126 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
3127 if (!opendata->f_attr.mdsthreshold) {
3128 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
3129 if (!opendata->f_attr.mdsthreshold)
3130 goto err_free_label;
3132 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
3134 if (d_really_is_positive(dentry))
3135 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
3137 status = _nfs4_open_and_get_state(opendata, flags, ctx);
3139 goto err_free_label;
3142 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
3143 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
3144 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
3146 * send create attributes which was not set by open
3147 * with an extra setattr.
3149 if (attrs || label) {
3150 unsigned ia_old = sattr->ia_valid;
3152 sattr->ia_valid = attrs;
3153 nfs_fattr_init(opendata->o_res.f_attr);
3154 status = nfs4_do_setattr(state->inode, cred,
3155 opendata->o_res.f_attr, sattr,
3156 ctx, label, olabel);
3158 nfs_setattr_update_inode(state->inode, sattr,
3159 opendata->o_res.f_attr);
3160 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
3162 sattr->ia_valid = ia_old;
3165 if (opened && opendata->file_created)
3168 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
3169 *ctx_th = opendata->f_attr.mdsthreshold;
3170 opendata->f_attr.mdsthreshold = NULL;
3173 nfs4_label_free(olabel);
3175 nfs4_opendata_put(opendata);
3176 nfs4_put_state_owner(sp);
3179 nfs4_label_free(olabel);
3181 nfs4_opendata_put(opendata);
3182 err_put_state_owner:
3183 nfs4_put_state_owner(sp);
3189 static struct nfs4_state *nfs4_do_open(struct inode *dir,
3190 struct nfs_open_context *ctx,
3192 struct iattr *sattr,
3193 struct nfs4_label *label,
3196 struct nfs_server *server = NFS_SERVER(dir);
3197 struct nfs4_exception exception = {
3198 .interruptible = true,
3200 struct nfs4_state *res;
3201 struct nfs4_open_createattrs c = {
3205 [0] = (__u32)jiffies,
3206 [1] = (__u32)current->pid,
3212 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
3214 trace_nfs4_open_file(ctx, flags, status);
3217 /* NOTE: BAD_SEQID means the server and client disagree about the
3218 * book-keeping w.r.t. state-changing operations
3219 * (OPEN/CLOSE/LOCK/LOCKU...)
3220 * It is actually a sign of a bug on the client or on the server.
3222 * If we receive a BAD_SEQID error in the particular case of
3223 * doing an OPEN, we assume that nfs_increment_open_seqid() will
3224 * have unhashed the old state_owner for us, and that we can
3225 * therefore safely retry using a new one. We should still warn
3226 * the user though...
3228 if (status == -NFS4ERR_BAD_SEQID) {
3229 pr_warn_ratelimited("NFS: v4 server %s "
3230 " returned a bad sequence-id error!\n",
3231 NFS_SERVER(dir)->nfs_client->cl_hostname);
3232 exception.retry = 1;
3236 * BAD_STATEID on OPEN means that the server cancelled our
3237 * state before it received the OPEN_CONFIRM.
3238 * Recover by retrying the request as per the discussion
3239 * on Page 181 of RFC3530.
3241 if (status == -NFS4ERR_BAD_STATEID) {
3242 exception.retry = 1;
3245 if (status == -NFS4ERR_EXPIRED) {
3246 nfs4_schedule_lease_recovery(server->nfs_client);
3247 exception.retry = 1;
3250 if (status == -EAGAIN) {
3251 /* We must have found a delegation */
3252 exception.retry = 1;
3255 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3257 res = ERR_PTR(nfs4_handle_exception(server,
3258 status, &exception));
3259 } while (exception.retry);
3263 static int _nfs4_do_setattr(struct inode *inode,
3264 struct nfs_setattrargs *arg,
3265 struct nfs_setattrres *res,
3266 const struct cred *cred,
3267 struct nfs_open_context *ctx)
3269 struct nfs_server *server = NFS_SERVER(inode);
3270 struct rpc_message msg = {
3271 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3276 const struct cred *delegation_cred = NULL;
3277 unsigned long timestamp = jiffies;
3281 nfs_fattr_init(res->fattr);
3283 /* Servers should only apply open mode checks for file size changes */
3284 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
3286 nfs4_inode_make_writeable(inode);
3290 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
3291 /* Use that stateid */
3292 } else if (ctx != NULL && ctx->state) {
3293 struct nfs_lock_context *l_ctx;
3294 if (!nfs4_valid_open_stateid(ctx->state))
3296 l_ctx = nfs_get_lock_context(ctx);
3298 return PTR_ERR(l_ctx);
3299 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3300 &arg->stateid, &delegation_cred);
3301 nfs_put_lock_context(l_ctx);
3304 else if (status == -EAGAIN)
3308 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
3310 if (delegation_cred)
3311 msg.rpc_cred = delegation_cred;
3313 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3315 put_cred(delegation_cred);
3316 if (status == 0 && ctx != NULL)
3317 renew_lease(server, timestamp);
3318 trace_nfs4_setattr(inode, &arg->stateid, status);
3322 static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
3323 struct nfs_fattr *fattr, struct iattr *sattr,
3324 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
3325 struct nfs4_label *olabel)
3327 struct nfs_server *server = NFS_SERVER(inode);
3328 __u32 bitmask[NFS4_BITMASK_SZ];
3329 struct nfs4_state *state = ctx ? ctx->state : NULL;
3330 struct nfs_setattrargs arg = {
3331 .fh = NFS_FH(inode),
3337 struct nfs_setattrres res = {
3342 struct nfs4_exception exception = {
3345 .stateid = &arg.stateid,
3350 nfs4_bitmap_copy_adjust_setattr(bitmask,
3351 nfs4_bitmask(server, olabel),
3354 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
3356 case -NFS4ERR_OPENMODE:
3357 if (!(sattr->ia_valid & ATTR_SIZE)) {
3358 pr_warn_once("NFSv4: server %s is incorrectly "
3359 "applying open mode checks to "
3360 "a SETATTR that is not "
3361 "changing file size.\n",
3362 server->nfs_client->cl_hostname);
3364 if (state && !(state->state & FMODE_WRITE)) {
3366 if (sattr->ia_valid & ATTR_OPEN)
3371 err = nfs4_handle_exception(server, err, &exception);
3372 } while (exception.retry);
3378 nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3380 if (inode == NULL || !nfs_have_layout(inode))
3383 return pnfs_wait_on_layoutreturn(inode, task);
3387 * Update the seqid of an open stateid
3389 static void nfs4_sync_open_stateid(nfs4_stateid *dst,
3390 struct nfs4_state *state)
3397 if (!nfs4_valid_open_stateid(state))
3399 seq = read_seqbegin(&state->seqlock);
3400 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3401 nfs4_stateid_copy(dst, &state->open_stateid);
3402 if (read_seqretry(&state->seqlock, seq))
3406 seqid_open = state->open_stateid.seqid;
3407 if (read_seqretry(&state->seqlock, seq))
3410 dst_seqid = be32_to_cpu(dst->seqid);
3411 if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) < 0)
3412 dst->seqid = seqid_open;
3418 * Update the seqid of an open stateid after receiving
3419 * NFS4ERR_OLD_STATEID
3421 static bool nfs4_refresh_open_old_stateid(nfs4_stateid *dst,
3422 struct nfs4_state *state)
3427 int seq, status = -EAGAIN;
3432 if (!nfs4_valid_open_stateid(state))
3434 seq = read_seqbegin(&state->seqlock);
3435 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3436 if (read_seqretry(&state->seqlock, seq))
3441 write_seqlock(&state->seqlock);
3442 seqid_open = state->open_stateid.seqid;
3444 dst_seqid = be32_to_cpu(dst->seqid);
3446 /* Did another OPEN bump the state's seqid? try again: */
3447 if ((s32)(be32_to_cpu(seqid_open) - dst_seqid) > 0) {
3448 dst->seqid = seqid_open;
3449 write_sequnlock(&state->seqlock);
3454 /* server says we're behind but we haven't seen the update yet */
3455 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
3456 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
3457 write_sequnlock(&state->seqlock);
3458 trace_nfs4_close_stateid_update_wait(state->inode, dst, 0);
3460 if (signal_pending(current))
3463 if (schedule_timeout(5*HZ) != 0)
3466 finish_wait(&state->waitq, &wait);
3470 if (status == -EINTR)
3473 /* we slept the whole 5 seconds, we must have lost a seqid */
3474 dst->seqid = cpu_to_be32(dst_seqid + 1);
3482 struct nfs4_closedata {
3483 struct inode *inode;
3484 struct nfs4_state *state;
3485 struct nfs_closeargs arg;
3486 struct nfs_closeres res;
3488 struct nfs4_layoutreturn_args arg;
3489 struct nfs4_layoutreturn_res res;
3490 struct nfs4_xdr_opaque_data ld_private;
3494 struct nfs_fattr fattr;
3495 unsigned long timestamp;
3498 static void nfs4_free_closedata(void *data)
3500 struct nfs4_closedata *calldata = data;
3501 struct nfs4_state_owner *sp = calldata->state->owner;
3502 struct super_block *sb = calldata->state->inode->i_sb;
3504 if (calldata->lr.roc)
3505 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3506 calldata->res.lr_ret);
3507 nfs4_put_open_state(calldata->state);
3508 nfs_free_seqid(calldata->arg.seqid);
3509 nfs4_put_state_owner(sp);
3510 nfs_sb_deactive(sb);
3514 static void nfs4_close_done(struct rpc_task *task, void *data)
3516 struct nfs4_closedata *calldata = data;
3517 struct nfs4_state *state = calldata->state;
3518 struct nfs_server *server = NFS_SERVER(calldata->inode);
3519 nfs4_stateid *res_stateid = NULL;
3520 struct nfs4_exception exception = {
3522 .inode = calldata->inode,
3523 .stateid = &calldata->arg.stateid,
3526 dprintk("%s: begin!\n", __func__);
3527 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3529 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
3531 /* Handle Layoutreturn errors */
3532 if (pnfs_roc_done(task, &calldata->arg.lr_args, &calldata->res.lr_res,
3533 &calldata->res.lr_ret) == -EAGAIN)
3536 /* hmm. we are done with the inode, and in the process of freeing
3537 * the state_owner. we keep this around to process errors
3539 switch (task->tk_status) {
3541 res_stateid = &calldata->res.stateid;
3542 renew_lease(server, calldata->timestamp);
3544 case -NFS4ERR_ACCESS:
3545 if (calldata->arg.bitmask != NULL) {
3546 calldata->arg.bitmask = NULL;
3547 calldata->res.fattr = NULL;
3552 case -NFS4ERR_OLD_STATEID:
3553 /* Did we race with OPEN? */
3554 if (nfs4_refresh_open_old_stateid(&calldata->arg.stateid,
3558 case -NFS4ERR_ADMIN_REVOKED:
3559 case -NFS4ERR_STALE_STATEID:
3560 case -NFS4ERR_EXPIRED:
3561 nfs4_free_revoked_stateid(server,
3562 &calldata->arg.stateid,
3563 task->tk_msg.rpc_cred);
3565 case -NFS4ERR_BAD_STATEID:
3566 if (calldata->arg.fmode == 0)
3570 task->tk_status = nfs4_async_handle_exception(task,
3571 server, task->tk_status, &exception);
3572 if (exception.retry)
3575 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3576 res_stateid, calldata->arg.fmode);
3578 task->tk_status = 0;
3579 nfs_release_seqid(calldata->arg.seqid);
3580 nfs_refresh_inode(calldata->inode, &calldata->fattr);
3581 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
3584 task->tk_status = 0;
3585 rpc_restart_call_prepare(task);
3589 static void nfs4_close_prepare(struct rpc_task *task, void *data)
3591 struct nfs4_closedata *calldata = data;
3592 struct nfs4_state *state = calldata->state;
3593 struct inode *inode = calldata->inode;
3594 struct pnfs_layout_hdr *lo;
3595 bool is_rdonly, is_wronly, is_rdwr;
3598 dprintk("%s: begin!\n", __func__);
3599 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3602 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
3603 spin_lock(&state->owner->so_lock);
3604 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3605 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3606 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
3607 /* Calculate the change in open mode */
3608 calldata->arg.fmode = 0;
3609 if (state->n_rdwr == 0) {
3610 if (state->n_rdonly == 0)
3611 call_close |= is_rdonly;
3613 calldata->arg.fmode |= FMODE_READ;
3614 if (state->n_wronly == 0)
3615 call_close |= is_wronly;
3617 calldata->arg.fmode |= FMODE_WRITE;
3618 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3619 call_close |= is_rdwr;
3621 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3623 nfs4_sync_open_stateid(&calldata->arg.stateid, state);
3624 if (!nfs4_valid_open_stateid(state))
3626 spin_unlock(&state->owner->so_lock);
3629 /* Note: exit _without_ calling nfs4_close_done */
3633 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
3634 nfs_release_seqid(calldata->arg.seqid);
3638 lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3639 if (lo && !pnfs_layout_is_valid(lo)) {
3640 calldata->arg.lr_args = NULL;
3641 calldata->res.lr_res = NULL;
3644 if (calldata->arg.fmode == 0)
3645 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
3647 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
3648 /* Close-to-open cache consistency revalidation */
3649 if (!nfs4_have_delegation(inode, FMODE_READ)) {
3650 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3651 nfs4_bitmask_adjust(calldata->arg.bitmask, inode, NFS_SERVER(inode), NULL);
3653 calldata->arg.bitmask = NULL;
3656 calldata->arg.share_access =
3657 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3658 calldata->arg.fmode, 0);
3660 if (calldata->res.fattr == NULL)
3661 calldata->arg.bitmask = NULL;
3662 else if (calldata->arg.bitmask == NULL)
3663 calldata->res.fattr = NULL;
3664 calldata->timestamp = jiffies;
3665 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
3666 &calldata->arg.seq_args,
3667 &calldata->res.seq_res,
3669 nfs_release_seqid(calldata->arg.seqid);
3670 dprintk("%s: done!\n", __func__);
3673 task->tk_action = NULL;
3675 nfs4_sequence_done(task, &calldata->res.seq_res);
3678 static const struct rpc_call_ops nfs4_close_ops = {
3679 .rpc_call_prepare = nfs4_close_prepare,
3680 .rpc_call_done = nfs4_close_done,
3681 .rpc_release = nfs4_free_closedata,
3685 * It is possible for data to be read/written from a mem-mapped file
3686 * after the sys_close call (which hits the vfs layer as a flush).
3687 * This means that we can't safely call nfsv4 close on a file until
3688 * the inode is cleared. This in turn means that we are not good
3689 * NFSv4 citizens - we do not indicate to the server to update the file's
3690 * share state even when we are done with one of the three share
3691 * stateid's in the inode.
3693 * NOTE: Caller must be holding the sp->so_owner semaphore!
3695 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
3697 struct nfs_server *server = NFS_SERVER(state->inode);
3698 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
3699 struct nfs4_closedata *calldata;
3700 struct nfs4_state_owner *sp = state->owner;
3701 struct rpc_task *task;
3702 struct rpc_message msg = {
3703 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3704 .rpc_cred = state->owner->so_cred,
3706 struct rpc_task_setup task_setup_data = {
3707 .rpc_client = server->client,
3708 .rpc_message = &msg,
3709 .callback_ops = &nfs4_close_ops,
3710 .workqueue = nfsiod_workqueue,
3711 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
3713 int status = -ENOMEM;
3715 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3716 &task_setup_data.rpc_client, &msg);
3718 calldata = kzalloc(sizeof(*calldata), gfp_mask);
3719 if (calldata == NULL)
3721 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
3722 calldata->inode = state->inode;
3723 calldata->state = state;
3724 calldata->arg.fh = NFS_FH(state->inode);
3725 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3726 goto out_free_calldata;
3727 /* Serialization for the sequence id */
3728 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3729 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
3730 if (IS_ERR(calldata->arg.seqid))
3731 goto out_free_calldata;
3732 nfs_fattr_init(&calldata->fattr);
3733 calldata->arg.fmode = 0;
3734 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
3735 calldata->res.fattr = &calldata->fattr;
3736 calldata->res.seqid = calldata->arg.seqid;
3737 calldata->res.server = server;
3738 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3739 calldata->lr.roc = pnfs_roc(state->inode,
3740 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3741 if (calldata->lr.roc) {
3742 calldata->arg.lr_args = &calldata->lr.arg;
3743 calldata->res.lr_res = &calldata->lr.res;
3745 nfs_sb_active(calldata->inode->i_sb);
3747 msg.rpc_argp = &calldata->arg;
3748 msg.rpc_resp = &calldata->res;
3749 task_setup_data.callback_data = calldata;
3750 task = rpc_run_task(&task_setup_data);
3752 return PTR_ERR(task);
3755 status = rpc_wait_for_completion_task(task);
3761 nfs4_put_open_state(state);
3762 nfs4_put_state_owner(sp);
3766 static struct inode *
3767 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3768 int open_flags, struct iattr *attr, int *opened)
3770 struct nfs4_state *state;
3771 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3773 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
3775 /* Protect against concurrent sillydeletes */
3776 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
3778 nfs4_label_release_security(label);
3781 return ERR_CAST(state);
3782 return state->inode;
3785 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
3787 if (ctx->state == NULL)
3790 nfs4_close_sync(ctx->state, _nfs4_ctx_to_openmode(ctx));
3792 nfs4_close_state(ctx->state, _nfs4_ctx_to_openmode(ctx));
3795 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3796 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3797 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_XATTR_SUPPORT - 1UL)
3799 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3801 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
3802 struct nfs4_server_caps_arg args = {
3806 struct nfs4_server_caps_res res = {};
3807 struct rpc_message msg = {
3808 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
3815 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3816 FATTR4_WORD0_FH_EXPIRE_TYPE |
3817 FATTR4_WORD0_LINK_SUPPORT |
3818 FATTR4_WORD0_SYMLINK_SUPPORT |
3819 FATTR4_WORD0_ACLSUPPORT;
3821 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3823 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3825 /* Sanity check the server answers */
3826 switch (minorversion) {
3828 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3829 res.attr_bitmask[2] = 0;
3832 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3835 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3837 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
3838 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3839 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3840 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3841 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
3842 NFS_CAP_CTIME|NFS_CAP_MTIME|
3843 NFS_CAP_SECURITY_LABEL);
3844 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3845 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3846 server->caps |= NFS_CAP_ACLS;
3847 if (res.has_links != 0)
3848 server->caps |= NFS_CAP_HARDLINKS;
3849 if (res.has_symlinks != 0)
3850 server->caps |= NFS_CAP_SYMLINKS;
3851 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3852 server->caps |= NFS_CAP_FILEID;
3853 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3854 server->caps |= NFS_CAP_MODE;
3855 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3856 server->caps |= NFS_CAP_NLINK;
3857 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3858 server->caps |= NFS_CAP_OWNER;
3859 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3860 server->caps |= NFS_CAP_OWNER_GROUP;
3861 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3862 server->caps |= NFS_CAP_ATIME;
3863 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3864 server->caps |= NFS_CAP_CTIME;
3865 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3866 server->caps |= NFS_CAP_MTIME;
3867 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3868 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3869 server->caps |= NFS_CAP_SECURITY_LABEL;
3871 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3872 sizeof(server->attr_bitmask));
3873 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3875 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3876 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3877 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
3878 server->cache_consistency_bitmask[2] = 0;
3880 /* Avoid a regression due to buggy server */
3881 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3882 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
3883 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3884 sizeof(server->exclcreat_bitmask));
3886 server->acl_bitmask = res.acl_bitmask;
3887 server->fh_expire_type = res.fh_expire_type;
3893 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3895 struct nfs4_exception exception = {
3896 .interruptible = true,
3900 err = nfs4_handle_exception(server,
3901 _nfs4_server_capabilities(server, fhandle),
3903 } while (exception.retry);
3907 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3908 struct nfs_fsinfo *info)
3911 struct nfs4_lookup_root_arg args = {
3914 struct nfs4_lookup_res res = {
3916 .fattr = info->fattr,
3919 struct rpc_message msg = {
3920 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3925 bitmask[0] = nfs4_fattr_bitmap[0];
3926 bitmask[1] = nfs4_fattr_bitmap[1];
3928 * Process the label in the upcoming getfattr
3930 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3932 nfs_fattr_init(info->fattr);
3933 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3936 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3937 struct nfs_fsinfo *info)
3939 struct nfs4_exception exception = {
3940 .interruptible = true,
3944 err = _nfs4_lookup_root(server, fhandle, info);
3945 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
3948 case -NFS4ERR_WRONGSEC:
3951 err = nfs4_handle_exception(server, err, &exception);
3953 } while (exception.retry);
3958 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3959 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3961 struct rpc_auth_create_args auth_args = {
3962 .pseudoflavor = flavor,
3964 struct rpc_auth *auth;
3966 auth = rpcauth_create(&auth_args, server->client);
3969 return nfs4_lookup_root(server, fhandle, info);
3973 * Retry pseudoroot lookup with various security flavors. We do this when:
3975 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3976 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3978 * Returns zero on success, or a negative NFS4ERR value, or a
3979 * negative errno value.
3981 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3982 struct nfs_fsinfo *info)
3984 /* Per 3530bis 15.33.5 */
3985 static const rpc_authflavor_t flav_array[] = {
3989 RPC_AUTH_UNIX, /* courtesy */
3992 int status = -EPERM;
3995 if (server->auth_info.flavor_len > 0) {
3996 /* try each flavor specified by user */
3997 for (i = 0; i < server->auth_info.flavor_len; i++) {
3998 status = nfs4_lookup_root_sec(server, fhandle, info,
3999 server->auth_info.flavors[i]);
4000 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4005 /* no flavors specified by user, try default list */
4006 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
4007 status = nfs4_lookup_root_sec(server, fhandle, info,
4009 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4016 * -EACCES could mean that the user doesn't have correct permissions
4017 * to access the mount. It could also mean that we tried to mount
4018 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
4019 * existing mount programs don't handle -EACCES very well so it should
4020 * be mapped to -EPERM instead.
4022 if (status == -EACCES)
4028 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
4029 * @server: initialized nfs_server handle
4030 * @fhandle: we fill in the pseudo-fs root file handle
4031 * @info: we fill in an FSINFO struct
4032 * @auth_probe: probe the auth flavours
4034 * Returns zero on success, or a negative errno.
4036 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
4037 struct nfs_fsinfo *info,
4043 status = nfs4_lookup_root(server, fhandle, info);
4045 if (auth_probe || status == NFS4ERR_WRONGSEC)
4046 status = server->nfs_client->cl_mvops->find_root_sec(server,
4050 status = nfs4_server_capabilities(server, fhandle);
4052 status = nfs4_do_fsinfo(server, fhandle, info);
4054 return nfs4_map_errors(status);
4057 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
4058 struct nfs_fsinfo *info)
4061 struct nfs_fattr *fattr = info->fattr;
4062 struct nfs4_label *label = fattr->label;
4064 error = nfs4_server_capabilities(server, mntfh);
4066 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
4070 error = nfs4_proc_getattr(server, mntfh, fattr, label, NULL);
4072 dprintk("nfs4_get_root: getattr error = %d\n", -error);
4076 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
4077 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
4078 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
4085 * Get locations and (maybe) other attributes of a referral.
4086 * Note that we'll actually follow the referral later when
4087 * we detect fsid mismatch in inode revalidation
4089 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
4090 const struct qstr *name, struct nfs_fattr *fattr,
4091 struct nfs_fh *fhandle)
4093 int status = -ENOMEM;
4094 struct page *page = NULL;
4095 struct nfs4_fs_locations *locations = NULL;
4097 page = alloc_page(GFP_KERNEL);
4100 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4101 if (locations == NULL)
4104 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
4109 * If the fsid didn't change, this is a migration event, not a
4110 * referral. Cause us to drop into the exception handler, which
4111 * will kick off migration recovery.
4113 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
4114 dprintk("%s: server did not return a different fsid for"
4115 " a referral at %s\n", __func__, name->name);
4116 status = -NFS4ERR_MOVED;
4119 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
4120 nfs_fixup_referral_attributes(&locations->fattr);
4122 /* replace the lookup nfs_fattr with the locations nfs_fattr */
4123 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
4124 memset(fhandle, 0, sizeof(struct nfs_fh));
4132 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4133 struct nfs_fattr *fattr, struct nfs4_label *label,
4134 struct inode *inode)
4136 __u32 bitmask[NFS4_BITMASK_SZ];
4137 struct nfs4_getattr_arg args = {
4141 struct nfs4_getattr_res res = {
4146 struct rpc_message msg = {
4147 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4151 unsigned short task_flags = 0;
4153 /* Is this is an attribute revalidation, subject to softreval? */
4154 if (inode && (server->flags & NFS_MOUNT_SOFTREVAL))
4155 task_flags |= RPC_TASK_TIMEOUT;
4157 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode);
4159 nfs_fattr_init(fattr);
4160 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4161 return nfs4_do_call_sync(server->client, server, &msg,
4162 &args.seq_args, &res.seq_res, task_flags);
4165 int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4166 struct nfs_fattr *fattr, struct nfs4_label *label,
4167 struct inode *inode)
4169 struct nfs4_exception exception = {
4170 .interruptible = true,
4174 err = _nfs4_proc_getattr(server, fhandle, fattr, label, inode);
4175 trace_nfs4_getattr(server, fhandle, fattr, err);
4176 err = nfs4_handle_exception(server, err,
4178 } while (exception.retry);
4183 * The file is not closed if it is opened due to the a request to change
4184 * the size of the file. The open call will not be needed once the
4185 * VFS layer lookup-intents are implemented.
4187 * Close is called when the inode is destroyed.
4188 * If we haven't opened the file for O_WRONLY, we
4189 * need to in the size_change case to obtain a stateid.
4192 * Because OPEN is always done by name in nfsv4, it is
4193 * possible that we opened a different file by the same
4194 * name. We can recognize this race condition, but we
4195 * can't do anything about it besides returning an error.
4197 * This will be fixed with VFS changes (lookup-intent).
4200 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
4201 struct iattr *sattr)
4203 struct inode *inode = d_inode(dentry);
4204 const struct cred *cred = NULL;
4205 struct nfs_open_context *ctx = NULL;
4206 struct nfs4_label *label = NULL;
4209 if (pnfs_ld_layoutret_on_setattr(inode) &&
4210 sattr->ia_valid & ATTR_SIZE &&
4211 sattr->ia_size < i_size_read(inode))
4212 pnfs_commit_and_return_layout(inode);
4214 nfs_fattr_init(fattr);
4216 /* Deal with open(O_TRUNC) */
4217 if (sattr->ia_valid & ATTR_OPEN)
4218 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
4220 /* Optimization: if the end result is no change, don't RPC */
4221 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
4224 /* Search for an existing open(O_WRITE) file */
4225 if (sattr->ia_valid & ATTR_FILE) {
4227 ctx = nfs_file_open_context(sattr->ia_file);
4232 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4234 return PTR_ERR(label);
4236 /* Return any delegations if we're going to change ACLs */
4237 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
4238 nfs4_inode_make_writeable(inode);
4240 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
4242 nfs_setattr_update_inode(inode, sattr, fattr);
4243 nfs_setsecurity(inode, fattr, label);
4245 nfs4_label_free(label);
4249 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
4250 struct dentry *dentry, struct nfs_fh *fhandle,
4251 struct nfs_fattr *fattr, struct nfs4_label *label)
4253 struct nfs_server *server = NFS_SERVER(dir);
4255 struct nfs4_lookup_arg args = {
4256 .bitmask = server->attr_bitmask,
4257 .dir_fh = NFS_FH(dir),
4258 .name = &dentry->d_name,
4260 struct nfs4_lookup_res res = {
4266 struct rpc_message msg = {
4267 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4271 unsigned short task_flags = 0;
4273 /* Is this is an attribute revalidation, subject to softreval? */
4274 if (nfs_lookup_is_soft_revalidate(dentry))
4275 task_flags |= RPC_TASK_TIMEOUT;
4277 args.bitmask = nfs4_bitmask(server, label);
4279 nfs_fattr_init(fattr);
4281 dprintk("NFS call lookup %pd2\n", dentry);
4282 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4283 status = nfs4_do_call_sync(clnt, server, &msg,
4284 &args.seq_args, &res.seq_res, task_flags);
4285 dprintk("NFS reply lookup: %d\n", status);
4289 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
4291 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4292 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
4293 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4297 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
4298 struct dentry *dentry, struct nfs_fh *fhandle,
4299 struct nfs_fattr *fattr, struct nfs4_label *label)
4301 struct nfs4_exception exception = {
4302 .interruptible = true,
4304 struct rpc_clnt *client = *clnt;
4305 const struct qstr *name = &dentry->d_name;
4308 err = _nfs4_proc_lookup(client, dir, dentry, fhandle, fattr, label);
4309 trace_nfs4_lookup(dir, name, err);
4311 case -NFS4ERR_BADNAME:
4314 case -NFS4ERR_MOVED:
4315 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
4316 if (err == -NFS4ERR_MOVED)
4317 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4319 case -NFS4ERR_WRONGSEC:
4321 if (client != *clnt)
4323 client = nfs4_negotiate_security(client, dir, name);
4325 return PTR_ERR(client);
4327 exception.retry = 1;
4330 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4332 } while (exception.retry);
4337 else if (client != *clnt)
4338 rpc_shutdown_client(client);
4343 static int nfs4_proc_lookup(struct inode *dir, struct dentry *dentry,
4344 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4345 struct nfs4_label *label)
4348 struct rpc_clnt *client = NFS_CLIENT(dir);
4350 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr, label);
4351 if (client != NFS_CLIENT(dir)) {
4352 rpc_shutdown_client(client);
4353 nfs_fixup_secinfo_attributes(fattr);
4359 nfs4_proc_lookup_mountpoint(struct inode *dir, struct dentry *dentry,
4360 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4362 struct rpc_clnt *client = NFS_CLIENT(dir);
4365 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr, NULL);
4367 return ERR_PTR(status);
4368 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
4371 static int _nfs4_proc_lookupp(struct inode *inode,
4372 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4373 struct nfs4_label *label)
4375 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4376 struct nfs_server *server = NFS_SERVER(inode);
4378 struct nfs4_lookupp_arg args = {
4379 .bitmask = server->attr_bitmask,
4380 .fh = NFS_FH(inode),
4382 struct nfs4_lookupp_res res = {
4388 struct rpc_message msg = {
4389 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4393 unsigned short task_flags = 0;
4395 if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL)
4396 task_flags |= RPC_TASK_TIMEOUT;
4398 args.bitmask = nfs4_bitmask(server, label);
4400 nfs_fattr_init(fattr);
4402 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4403 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4404 &res.seq_res, task_flags);
4405 dprintk("NFS reply lookupp: %d\n", status);
4409 static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4410 struct nfs_fattr *fattr, struct nfs4_label *label)
4412 struct nfs4_exception exception = {
4413 .interruptible = true,
4417 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4418 trace_nfs4_lookupp(inode, err);
4419 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4421 } while (exception.retry);
4425 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4427 struct nfs_server *server = NFS_SERVER(inode);
4428 struct nfs4_accessargs args = {
4429 .fh = NFS_FH(inode),
4430 .access = entry->mask,
4432 struct nfs4_accessres res = {
4435 struct rpc_message msg = {
4436 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4439 .rpc_cred = entry->cred,
4443 if (!nfs4_have_delegation(inode, FMODE_READ)) {
4444 res.fattr = nfs_alloc_fattr();
4445 if (res.fattr == NULL)
4447 args.bitmask = server->cache_consistency_bitmask;
4449 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4451 nfs_access_set_mask(entry, res.access);
4453 nfs_refresh_inode(inode, res.fattr);
4455 nfs_free_fattr(res.fattr);
4459 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4461 struct nfs4_exception exception = {
4462 .interruptible = true,
4466 err = _nfs4_proc_access(inode, entry);
4467 trace_nfs4_access(inode, err);
4468 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4470 } while (exception.retry);
4475 * TODO: For the time being, we don't try to get any attributes
4476 * along with any of the zero-copy operations READ, READDIR,
4479 * In the case of the first three, we want to put the GETATTR
4480 * after the read-type operation -- this is because it is hard
4481 * to predict the length of a GETATTR response in v4, and thus
4482 * align the READ data correctly. This means that the GETATTR
4483 * may end up partially falling into the page cache, and we should
4484 * shift it into the 'tail' of the xdr_buf before processing.
4485 * To do this efficiently, we need to know the total length
4486 * of data received, which doesn't seem to be available outside
4489 * In the case of WRITE, we also want to put the GETATTR after
4490 * the operation -- in this case because we want to make sure
4491 * we get the post-operation mtime and size.
4493 * Both of these changes to the XDR layer would in fact be quite
4494 * minor, but I decided to leave them for a subsequent patch.
4496 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4497 unsigned int pgbase, unsigned int pglen)
4499 struct nfs4_readlink args = {
4500 .fh = NFS_FH(inode),
4505 struct nfs4_readlink_res res;
4506 struct rpc_message msg = {
4507 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4512 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
4515 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4516 unsigned int pgbase, unsigned int pglen)
4518 struct nfs4_exception exception = {
4519 .interruptible = true,
4523 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4524 trace_nfs4_readlink(inode, err);
4525 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4527 } while (exception.retry);
4532 * This is just for mknod. open(O_CREAT) will always do ->open_context().
4535 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
4538 struct nfs_server *server = NFS_SERVER(dir);
4539 struct nfs4_label l, *ilabel = NULL;
4540 struct nfs_open_context *ctx;
4541 struct nfs4_state *state;
4544 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
4546 return PTR_ERR(ctx);
4548 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4550 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4551 sattr->ia_mode &= ~current_umask();
4552 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
4553 if (IS_ERR(state)) {
4554 status = PTR_ERR(state);
4558 nfs4_label_release_security(ilabel);
4559 put_nfs_open_context(ctx);
4564 _nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
4566 struct nfs_server *server = NFS_SERVER(dir);
4567 struct nfs_removeargs args = {
4571 struct nfs_removeres res = {
4574 struct rpc_message msg = {
4575 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4579 unsigned long timestamp = jiffies;
4582 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
4584 spin_lock(&dir->i_lock);
4585 nfs4_update_changeattr_locked(dir, &res.cinfo, timestamp,
4586 NFS_INO_INVALID_DATA);
4587 /* Removing a directory decrements nlink in the parent */
4588 if (ftype == NF4DIR && dir->i_nlink > 2)
4589 nfs4_dec_nlink_locked(dir);
4590 spin_unlock(&dir->i_lock);
4595 static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4597 struct nfs4_exception exception = {
4598 .interruptible = true,
4600 struct inode *inode = d_inode(dentry);
4604 if (inode->i_nlink == 1)
4605 nfs4_inode_return_delegation(inode);
4607 nfs4_inode_make_writeable(inode);
4610 err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
4611 trace_nfs4_remove(dir, &dentry->d_name, err);
4612 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4614 } while (exception.retry);
4618 static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
4620 struct nfs4_exception exception = {
4621 .interruptible = true,
4626 err = _nfs4_proc_remove(dir, name, NF4DIR);
4627 trace_nfs4_remove(dir, name, err);
4628 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4630 } while (exception.retry);
4634 static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4635 struct dentry *dentry,
4636 struct inode *inode)
4638 struct nfs_removeargs *args = msg->rpc_argp;
4639 struct nfs_removeres *res = msg->rpc_resp;
4641 res->server = NFS_SB(dentry->d_sb);
4642 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
4643 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
4645 nfs_fattr_init(res->dir_attr);
4648 nfs4_inode_return_delegation(inode);
4651 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4653 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
4654 &data->args.seq_args,
4659 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
4661 struct nfs_unlinkdata *data = task->tk_calldata;
4662 struct nfs_removeres *res = &data->res;
4664 if (!nfs4_sequence_done(task, &res->seq_res))
4666 if (nfs4_async_handle_error(task, res->server, NULL,
4667 &data->timeout) == -EAGAIN)
4669 if (task->tk_status == 0)
4670 nfs4_update_changeattr(dir, &res->cinfo,
4671 res->dir_attr->time_start,
4672 NFS_INO_INVALID_DATA);
4676 static void nfs4_proc_rename_setup(struct rpc_message *msg,
4677 struct dentry *old_dentry,
4678 struct dentry *new_dentry)
4680 struct nfs_renameargs *arg = msg->rpc_argp;
4681 struct nfs_renameres *res = msg->rpc_resp;
4682 struct inode *old_inode = d_inode(old_dentry);
4683 struct inode *new_inode = d_inode(new_dentry);
4686 nfs4_inode_make_writeable(old_inode);
4688 nfs4_inode_return_delegation(new_inode);
4689 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
4690 res->server = NFS_SB(old_dentry->d_sb);
4691 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
4694 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4696 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
4697 &data->args.seq_args,
4702 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4703 struct inode *new_dir)
4705 struct nfs_renamedata *data = task->tk_calldata;
4706 struct nfs_renameres *res = &data->res;
4708 if (!nfs4_sequence_done(task, &res->seq_res))
4710 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
4713 if (task->tk_status == 0) {
4714 if (new_dir != old_dir) {
4715 /* Note: If we moved a directory, nlink will change */
4716 nfs4_update_changeattr(old_dir, &res->old_cinfo,
4717 res->old_fattr->time_start,
4718 NFS_INO_INVALID_OTHER |
4719 NFS_INO_INVALID_DATA);
4720 nfs4_update_changeattr(new_dir, &res->new_cinfo,
4721 res->new_fattr->time_start,
4722 NFS_INO_INVALID_OTHER |
4723 NFS_INO_INVALID_DATA);
4725 nfs4_update_changeattr(old_dir, &res->old_cinfo,
4726 res->old_fattr->time_start,
4727 NFS_INO_INVALID_DATA);
4732 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4734 struct nfs_server *server = NFS_SERVER(inode);
4735 __u32 bitmask[NFS4_BITMASK_SZ];
4736 struct nfs4_link_arg arg = {
4737 .fh = NFS_FH(inode),
4738 .dir_fh = NFS_FH(dir),
4742 struct nfs4_link_res res = {
4746 struct rpc_message msg = {
4747 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4751 int status = -ENOMEM;
4753 res.fattr = nfs_alloc_fattr();
4754 if (res.fattr == NULL)
4757 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4758 if (IS_ERR(res.label)) {
4759 status = PTR_ERR(res.label);
4763 nfs4_inode_make_writeable(inode);
4764 nfs4_bitmap_copy_adjust_setattr(bitmask, nfs4_bitmask(server, res.label), inode);
4766 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4768 nfs4_update_changeattr(dir, &res.cinfo, res.fattr->time_start,
4769 NFS_INO_INVALID_DATA);
4770 status = nfs_post_op_update_inode(inode, res.fattr);
4772 nfs_setsecurity(inode, res.fattr, res.label);
4776 nfs4_label_free(res.label);
4779 nfs_free_fattr(res.fattr);
4783 static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4785 struct nfs4_exception exception = {
4786 .interruptible = true,
4790 err = nfs4_handle_exception(NFS_SERVER(inode),
4791 _nfs4_proc_link(inode, dir, name),
4793 } while (exception.retry);
4797 struct nfs4_createdata {
4798 struct rpc_message msg;
4799 struct nfs4_create_arg arg;
4800 struct nfs4_create_res res;
4802 struct nfs_fattr fattr;
4803 struct nfs4_label *label;
4806 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
4807 const struct qstr *name, struct iattr *sattr, u32 ftype)
4809 struct nfs4_createdata *data;
4811 data = kzalloc(sizeof(*data), GFP_KERNEL);
4813 struct nfs_server *server = NFS_SERVER(dir);
4815 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4816 if (IS_ERR(data->label))
4819 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4820 data->msg.rpc_argp = &data->arg;
4821 data->msg.rpc_resp = &data->res;
4822 data->arg.dir_fh = NFS_FH(dir);
4823 data->arg.server = server;
4824 data->arg.name = name;
4825 data->arg.attrs = sattr;
4826 data->arg.ftype = ftype;
4827 data->arg.bitmask = nfs4_bitmask(server, data->label);
4828 data->arg.umask = current_umask();
4829 data->res.server = server;
4830 data->res.fh = &data->fh;
4831 data->res.fattr = &data->fattr;
4832 data->res.label = data->label;
4833 nfs_fattr_init(data->res.fattr);
4841 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4843 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
4844 &data->arg.seq_args, &data->res.seq_res, 1);
4846 spin_lock(&dir->i_lock);
4847 nfs4_update_changeattr_locked(dir, &data->res.dir_cinfo,
4848 data->res.fattr->time_start,
4849 NFS_INO_INVALID_DATA);
4850 /* Creating a directory bumps nlink in the parent */
4851 if (data->arg.ftype == NF4DIR)
4852 nfs4_inc_nlink_locked(dir);
4853 spin_unlock(&dir->i_lock);
4854 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
4859 static void nfs4_free_createdata(struct nfs4_createdata *data)
4861 nfs4_label_free(data->label);
4865 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4866 struct page *page, unsigned int len, struct iattr *sattr,
4867 struct nfs4_label *label)
4869 struct nfs4_createdata *data;
4870 int status = -ENAMETOOLONG;
4872 if (len > NFS4_MAXPATHLEN)
4876 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4880 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4881 data->arg.u.symlink.pages = &page;
4882 data->arg.u.symlink.len = len;
4883 data->arg.label = label;
4885 status = nfs4_do_create(dir, dentry, data);
4887 nfs4_free_createdata(data);
4892 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4893 struct page *page, unsigned int len, struct iattr *sattr)
4895 struct nfs4_exception exception = {
4896 .interruptible = true,
4898 struct nfs4_label l, *label = NULL;
4901 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4904 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4905 trace_nfs4_symlink(dir, &dentry->d_name, err);
4906 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4908 } while (exception.retry);
4910 nfs4_label_release_security(label);
4914 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4915 struct iattr *sattr, struct nfs4_label *label)
4917 struct nfs4_createdata *data;
4918 int status = -ENOMEM;
4920 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4924 data->arg.label = label;
4925 status = nfs4_do_create(dir, dentry, data);
4927 nfs4_free_createdata(data);
4932 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4933 struct iattr *sattr)
4935 struct nfs_server *server = NFS_SERVER(dir);
4936 struct nfs4_exception exception = {
4937 .interruptible = true,
4939 struct nfs4_label l, *label = NULL;
4942 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4944 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4945 sattr->ia_mode &= ~current_umask();
4947 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4948 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4949 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4951 } while (exception.retry);
4952 nfs4_label_release_security(label);
4957 static int _nfs4_proc_readdir(struct nfs_readdir_arg *nr_arg,
4958 struct nfs_readdir_res *nr_res)
4960 struct inode *dir = d_inode(nr_arg->dentry);
4961 struct nfs_server *server = NFS_SERVER(dir);
4962 struct nfs4_readdir_arg args = {
4964 .pages = nr_arg->pages,
4966 .count = nr_arg->page_len,
4967 .plus = nr_arg->plus,
4969 struct nfs4_readdir_res res;
4970 struct rpc_message msg = {
4971 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4974 .rpc_cred = nr_arg->cred,
4978 dprintk("%s: dentry = %pd2, cookie = %llu\n", __func__,
4979 nr_arg->dentry, (unsigned long long)nr_arg->cookie);
4980 if (!(server->caps & NFS_CAP_SECURITY_LABEL))
4981 args.bitmask = server->attr_bitmask_nl;
4983 args.bitmask = server->attr_bitmask;
4985 nfs4_setup_readdir(nr_arg->cookie, nr_arg->verf, nr_arg->dentry, &args);
4986 res.pgbase = args.pgbase;
4987 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
4990 memcpy(nr_res->verf, res.verifier.data, NFS4_VERIFIER_SIZE);
4991 status += args.pgbase;
4994 nfs_invalidate_atime(dir);
4996 dprintk("%s: returns %d\n", __func__, status);
5000 static int nfs4_proc_readdir(struct nfs_readdir_arg *arg,
5001 struct nfs_readdir_res *res)
5003 struct nfs4_exception exception = {
5004 .interruptible = true,
5008 err = _nfs4_proc_readdir(arg, res);
5009 trace_nfs4_readdir(d_inode(arg->dentry), err);
5010 err = nfs4_handle_exception(NFS_SERVER(d_inode(arg->dentry)),
5012 } while (exception.retry);
5016 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5017 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
5019 struct nfs4_createdata *data;
5020 int mode = sattr->ia_mode;
5021 int status = -ENOMEM;
5023 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
5028 data->arg.ftype = NF4FIFO;
5029 else if (S_ISBLK(mode)) {
5030 data->arg.ftype = NF4BLK;
5031 data->arg.u.device.specdata1 = MAJOR(rdev);
5032 data->arg.u.device.specdata2 = MINOR(rdev);
5034 else if (S_ISCHR(mode)) {
5035 data->arg.ftype = NF4CHR;
5036 data->arg.u.device.specdata1 = MAJOR(rdev);
5037 data->arg.u.device.specdata2 = MINOR(rdev);
5038 } else if (!S_ISSOCK(mode)) {
5043 data->arg.label = label;
5044 status = nfs4_do_create(dir, dentry, data);
5046 nfs4_free_createdata(data);
5051 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5052 struct iattr *sattr, dev_t rdev)
5054 struct nfs_server *server = NFS_SERVER(dir);
5055 struct nfs4_exception exception = {
5056 .interruptible = true,
5058 struct nfs4_label l, *label = NULL;
5061 label = nfs4_label_init_security(dir, dentry, sattr, &l);
5063 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
5064 sattr->ia_mode &= ~current_umask();
5066 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
5067 trace_nfs4_mknod(dir, &dentry->d_name, err);
5068 err = nfs4_handle_exception(NFS_SERVER(dir), err,
5070 } while (exception.retry);
5072 nfs4_label_release_security(label);
5077 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
5078 struct nfs_fsstat *fsstat)
5080 struct nfs4_statfs_arg args = {
5082 .bitmask = server->attr_bitmask,
5084 struct nfs4_statfs_res res = {
5087 struct rpc_message msg = {
5088 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
5093 nfs_fattr_init(fsstat->fattr);
5094 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5097 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
5099 struct nfs4_exception exception = {
5100 .interruptible = true,
5104 err = nfs4_handle_exception(server,
5105 _nfs4_proc_statfs(server, fhandle, fsstat),
5107 } while (exception.retry);
5111 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
5112 struct nfs_fsinfo *fsinfo)
5114 struct nfs4_fsinfo_arg args = {
5116 .bitmask = server->attr_bitmask,
5118 struct nfs4_fsinfo_res res = {
5121 struct rpc_message msg = {
5122 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
5127 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5130 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5132 struct nfs4_exception exception = {
5133 .interruptible = true,
5138 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
5139 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
5141 nfs4_set_lease_period(server->nfs_client, fsinfo->lease_time * HZ);
5144 err = nfs4_handle_exception(server, err, &exception);
5145 } while (exception.retry);
5149 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5153 nfs_fattr_init(fsinfo->fattr);
5154 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
5156 /* block layout checks this! */
5157 server->pnfs_blksize = fsinfo->blksize;
5158 set_pnfs_layoutdriver(server, fhandle, fsinfo);
5164 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5165 struct nfs_pathconf *pathconf)
5167 struct nfs4_pathconf_arg args = {
5169 .bitmask = server->attr_bitmask,
5171 struct nfs4_pathconf_res res = {
5172 .pathconf = pathconf,
5174 struct rpc_message msg = {
5175 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
5180 /* None of the pathconf attributes are mandatory to implement */
5181 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
5182 memset(pathconf, 0, sizeof(*pathconf));
5186 nfs_fattr_init(pathconf->fattr);
5187 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5190 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5191 struct nfs_pathconf *pathconf)
5193 struct nfs4_exception exception = {
5194 .interruptible = true,
5199 err = nfs4_handle_exception(server,
5200 _nfs4_proc_pathconf(server, fhandle, pathconf),
5202 } while (exception.retry);
5206 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
5207 const struct nfs_open_context *ctx,
5208 const struct nfs_lock_context *l_ctx,
5211 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
5213 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
5215 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
5216 const struct nfs_open_context *ctx,
5217 const struct nfs_lock_context *l_ctx,
5220 nfs4_stateid _current_stateid;
5222 /* If the current stateid represents a lost lock, then exit */
5223 if (nfs4_set_rw_stateid(&_current_stateid, ctx, l_ctx, fmode) == -EIO)
5225 return nfs4_stateid_match(stateid, &_current_stateid);
5228 static bool nfs4_error_stateid_expired(int err)
5231 case -NFS4ERR_DELEG_REVOKED:
5232 case -NFS4ERR_ADMIN_REVOKED:
5233 case -NFS4ERR_BAD_STATEID:
5234 case -NFS4ERR_STALE_STATEID:
5235 case -NFS4ERR_OLD_STATEID:
5236 case -NFS4ERR_OPENMODE:
5237 case -NFS4ERR_EXPIRED:
5243 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
5245 struct nfs_server *server = NFS_SERVER(hdr->inode);
5247 trace_nfs4_read(hdr, task->tk_status);
5248 if (task->tk_status < 0) {
5249 struct nfs4_exception exception = {
5250 .inode = hdr->inode,
5251 .state = hdr->args.context->state,
5252 .stateid = &hdr->args.stateid,
5254 task->tk_status = nfs4_async_handle_exception(task,
5255 server, task->tk_status, &exception);
5256 if (exception.retry) {
5257 rpc_restart_call_prepare(task);
5262 if (task->tk_status > 0)
5263 renew_lease(server, hdr->timestamp);
5267 static bool nfs4_read_stateid_changed(struct rpc_task *task,
5268 struct nfs_pgio_args *args)
5271 if (!nfs4_error_stateid_expired(task->tk_status) ||
5272 nfs4_stateid_is_current(&args->stateid,
5277 rpc_restart_call_prepare(task);
5281 static bool nfs4_read_plus_not_supported(struct rpc_task *task,
5282 struct nfs_pgio_header *hdr)
5284 struct nfs_server *server = NFS_SERVER(hdr->inode);
5285 struct rpc_message *msg = &task->tk_msg;
5287 if (msg->rpc_proc == &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS] &&
5288 server->caps & NFS_CAP_READ_PLUS && task->tk_status == -ENOTSUPP) {
5289 server->caps &= ~NFS_CAP_READ_PLUS;
5290 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5291 rpc_restart_call_prepare(task);
5297 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5299 dprintk("--> %s\n", __func__);
5301 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5303 if (nfs4_read_stateid_changed(task, &hdr->args))
5305 if (nfs4_read_plus_not_supported(task, hdr))
5307 if (task->tk_status > 0)
5308 nfs_invalidate_atime(hdr->inode);
5309 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5310 nfs4_read_done_cb(task, hdr);
5313 #if defined CONFIG_NFS_V4_2 && defined CONFIG_NFS_V4_2_READ_PLUS
5314 static void nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5315 struct rpc_message *msg)
5317 /* Note: We don't use READ_PLUS with pNFS yet */
5318 if (nfs_server_capable(hdr->inode, NFS_CAP_READ_PLUS) && !hdr->ds_clp)
5319 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS];
5322 static void nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5323 struct rpc_message *msg)
5326 #endif /* CONFIG_NFS_V4_2 */
5328 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5329 struct rpc_message *msg)
5331 hdr->timestamp = jiffies;
5332 if (!hdr->pgio_done_cb)
5333 hdr->pgio_done_cb = nfs4_read_done_cb;
5334 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5335 nfs42_read_plus_support(hdr, msg);
5336 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5339 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5340 struct nfs_pgio_header *hdr)
5342 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
5343 &hdr->args.seq_args,
5347 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5348 hdr->args.lock_context,
5349 hdr->rw_mode) == -EIO)
5351 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
5356 static int nfs4_write_done_cb(struct rpc_task *task,
5357 struct nfs_pgio_header *hdr)
5359 struct inode *inode = hdr->inode;
5361 trace_nfs4_write(hdr, task->tk_status);
5362 if (task->tk_status < 0) {
5363 struct nfs4_exception exception = {
5364 .inode = hdr->inode,
5365 .state = hdr->args.context->state,
5366 .stateid = &hdr->args.stateid,
5368 task->tk_status = nfs4_async_handle_exception(task,
5369 NFS_SERVER(inode), task->tk_status,
5371 if (exception.retry) {
5372 rpc_restart_call_prepare(task);
5376 if (task->tk_status >= 0) {
5377 renew_lease(NFS_SERVER(inode), hdr->timestamp);
5378 nfs_writeback_update_inode(hdr);
5383 static bool nfs4_write_stateid_changed(struct rpc_task *task,
5384 struct nfs_pgio_args *args)
5387 if (!nfs4_error_stateid_expired(task->tk_status) ||
5388 nfs4_stateid_is_current(&args->stateid,
5393 rpc_restart_call_prepare(task);
5397 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5399 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5401 if (nfs4_write_stateid_changed(task, &hdr->args))
5403 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5404 nfs4_write_done_cb(task, hdr);
5408 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
5410 /* Don't request attributes for pNFS or O_DIRECT writes */
5411 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
5413 /* Otherwise, request attributes if and only if we don't hold
5416 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
5419 static void nfs4_bitmask_adjust(__u32 *bitmask, struct inode *inode,
5420 struct nfs_server *server,
5421 struct nfs4_label *label)
5424 unsigned long cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
5426 if ((cache_validity & NFS_INO_INVALID_DATA) ||
5427 (cache_validity & NFS_INO_REVAL_PAGECACHE) ||
5428 (cache_validity & NFS_INO_REVAL_FORCED) ||
5429 (cache_validity & NFS_INO_INVALID_OTHER))
5430 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode);
5432 if (cache_validity & NFS_INO_INVALID_ATIME)
5433 bitmask[1] |= FATTR4_WORD1_TIME_ACCESS;
5434 if (cache_validity & NFS_INO_INVALID_OTHER)
5435 bitmask[1] |= FATTR4_WORD1_MODE | FATTR4_WORD1_OWNER |
5436 FATTR4_WORD1_OWNER_GROUP |
5437 FATTR4_WORD1_NUMLINKS;
5438 if (label && label->len && cache_validity & NFS_INO_INVALID_LABEL)
5439 bitmask[2] |= FATTR4_WORD2_SECURITY_LABEL;
5440 if (cache_validity & NFS_INO_INVALID_CHANGE)
5441 bitmask[0] |= FATTR4_WORD0_CHANGE;
5442 if (cache_validity & NFS_INO_INVALID_CTIME)
5443 bitmask[1] |= FATTR4_WORD1_TIME_METADATA;
5444 if (cache_validity & NFS_INO_INVALID_MTIME)
5445 bitmask[1] |= FATTR4_WORD1_TIME_MODIFY;
5446 if (cache_validity & NFS_INO_INVALID_SIZE)
5447 bitmask[0] |= FATTR4_WORD0_SIZE;
5448 if (cache_validity & NFS_INO_INVALID_BLOCKS)
5449 bitmask[1] |= FATTR4_WORD1_SPACE_USED;
5452 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
5453 struct rpc_message *msg,
5454 struct rpc_clnt **clnt)
5456 struct nfs_server *server = NFS_SERVER(hdr->inode);
5458 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5459 hdr->args.bitmask = NULL;
5460 hdr->res.fattr = NULL;
5462 hdr->args.bitmask = server->cache_consistency_bitmask;
5463 nfs4_bitmask_adjust(hdr->args.bitmask, hdr->inode, server, NULL);
5466 if (!hdr->pgio_done_cb)
5467 hdr->pgio_done_cb = nfs4_write_done_cb;
5468 hdr->res.server = server;
5469 hdr->timestamp = jiffies;
5471 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
5472 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5473 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
5476 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5478 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
5479 &data->args.seq_args,
5484 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
5486 struct inode *inode = data->inode;
5488 trace_nfs4_commit(data, task->tk_status);
5489 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5490 NULL, NULL) == -EAGAIN) {
5491 rpc_restart_call_prepare(task);
5497 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
5499 if (!nfs4_sequence_done(task, &data->res.seq_res))
5501 return data->commit_done_cb(task, data);
5504 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5505 struct rpc_clnt **clnt)
5507 struct nfs_server *server = NFS_SERVER(data->inode);
5509 if (data->commit_done_cb == NULL)
5510 data->commit_done_cb = nfs4_commit_done_cb;
5511 data->res.server = server;
5512 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
5513 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
5514 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
5517 static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5518 struct nfs_commitres *res)
5520 struct inode *dst_inode = file_inode(dst);
5521 struct nfs_server *server = NFS_SERVER(dst_inode);
5522 struct rpc_message msg = {
5523 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5528 args->fh = NFS_FH(dst_inode);
5529 return nfs4_call_sync(server->client, server, &msg,
5530 &args->seq_args, &res->seq_res, 1);
5533 int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5535 struct nfs_commitargs args = {
5539 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5540 struct nfs4_exception exception = { };
5544 status = _nfs4_proc_commit(dst, &args, res);
5545 status = nfs4_handle_exception(dst_server, status, &exception);
5546 } while (exception.retry);
5551 struct nfs4_renewdata {
5552 struct nfs_client *client;
5553 unsigned long timestamp;
5557 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5558 * standalone procedure for queueing an asynchronous RENEW.
5560 static void nfs4_renew_release(void *calldata)
5562 struct nfs4_renewdata *data = calldata;
5563 struct nfs_client *clp = data->client;
5565 if (refcount_read(&clp->cl_count) > 1)
5566 nfs4_schedule_state_renewal(clp);
5567 nfs_put_client(clp);
5571 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
5573 struct nfs4_renewdata *data = calldata;
5574 struct nfs_client *clp = data->client;
5575 unsigned long timestamp = data->timestamp;
5577 trace_nfs4_renew_async(clp, task->tk_status);
5578 switch (task->tk_status) {
5581 case -NFS4ERR_LEASE_MOVED:
5582 nfs4_schedule_lease_moved_recovery(clp);
5585 /* Unless we're shutting down, schedule state recovery! */
5586 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5588 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
5589 nfs4_schedule_lease_recovery(clp);
5592 nfs4_schedule_path_down_recovery(clp);
5594 do_renew_lease(clp, timestamp);
5597 static const struct rpc_call_ops nfs4_renew_ops = {
5598 .rpc_call_done = nfs4_renew_done,
5599 .rpc_release = nfs4_renew_release,
5602 static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
5604 struct rpc_message msg = {
5605 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5609 struct nfs4_renewdata *data;
5611 if (renew_flags == 0)
5613 if (!refcount_inc_not_zero(&clp->cl_count))
5615 data = kmalloc(sizeof(*data), GFP_NOFS);
5617 nfs_put_client(clp);
5621 data->timestamp = jiffies;
5622 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
5623 &nfs4_renew_ops, data);
5626 static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
5628 struct rpc_message msg = {
5629 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5633 unsigned long now = jiffies;
5636 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5639 do_renew_lease(clp, now);
5643 static inline int nfs4_server_supports_acls(struct nfs_server *server)
5645 return server->caps & NFS_CAP_ACLS;
5648 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5649 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
5652 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
5654 int nfs4_buf_to_pages_noslab(const void *buf, size_t buflen,
5655 struct page **pages)
5657 struct page *newpage, **spages;
5663 len = min_t(size_t, PAGE_SIZE, buflen);
5664 newpage = alloc_page(GFP_KERNEL);
5666 if (newpage == NULL)
5668 memcpy(page_address(newpage), buf, len);
5673 } while (buflen != 0);
5679 __free_page(spages[rc-1]);
5683 struct nfs4_cached_acl {
5689 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
5691 struct nfs_inode *nfsi = NFS_I(inode);
5693 spin_lock(&inode->i_lock);
5694 kfree(nfsi->nfs4_acl);
5695 nfsi->nfs4_acl = acl;
5696 spin_unlock(&inode->i_lock);
5699 static void nfs4_zap_acl_attr(struct inode *inode)
5701 nfs4_set_cached_acl(inode, NULL);
5704 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5706 struct nfs_inode *nfsi = NFS_I(inode);
5707 struct nfs4_cached_acl *acl;
5710 spin_lock(&inode->i_lock);
5711 acl = nfsi->nfs4_acl;
5714 if (buf == NULL) /* user is just asking for length */
5716 if (acl->cached == 0)
5718 ret = -ERANGE; /* see getxattr(2) man page */
5719 if (acl->len > buflen)
5721 memcpy(buf, acl->data, acl->len);
5725 spin_unlock(&inode->i_lock);
5729 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
5731 struct nfs4_cached_acl *acl;
5732 size_t buflen = sizeof(*acl) + acl_len;
5734 if (buflen <= PAGE_SIZE) {
5735 acl = kmalloc(buflen, GFP_KERNEL);
5739 _copy_from_pages(acl->data, pages, pgbase, acl_len);
5741 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5748 nfs4_set_cached_acl(inode, acl);
5752 * The getxattr API returns the required buffer length when called with a
5753 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5754 * the required buf. On a NULL buf, we send a page of data to the server
5755 * guessing that the ACL request can be serviced by a page. If so, we cache
5756 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5757 * the cache. If not so, we throw away the page, and cache the required
5758 * length. The next getxattr call will then produce another round trip to
5759 * the server, this time with the input buf of the required size.
5761 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5763 struct page **pages;
5764 struct nfs_getaclargs args = {
5765 .fh = NFS_FH(inode),
5768 struct nfs_getaclres res = {
5771 struct rpc_message msg = {
5772 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5776 unsigned int npages;
5777 int ret = -ENOMEM, i;
5778 struct nfs_server *server = NFS_SERVER(inode);
5781 buflen = server->rsize;
5783 npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
5784 pages = kmalloc_array(npages, sizeof(struct page *), GFP_NOFS);
5788 args.acl_pages = pages;
5790 for (i = 0; i < npages; i++) {
5791 pages[i] = alloc_page(GFP_KERNEL);
5796 /* for decoding across pages */
5797 res.acl_scratch = alloc_page(GFP_KERNEL);
5798 if (!res.acl_scratch)
5801 args.acl_len = npages * PAGE_SIZE;
5803 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
5804 __func__, buf, buflen, npages, args.acl_len);
5805 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5806 &msg, &args.seq_args, &res.seq_res, 0);
5810 /* Handle the case where the passed-in buffer is too short */
5811 if (res.acl_flags & NFS4_ACL_TRUNC) {
5812 /* Did the user only issue a request for the acl length? */
5818 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
5820 if (res.acl_len > buflen) {
5824 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
5829 for (i = 0; i < npages; i++)
5831 __free_page(pages[i]);
5832 if (res.acl_scratch)
5833 __free_page(res.acl_scratch);
5838 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5840 struct nfs4_exception exception = {
5841 .interruptible = true,
5845 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
5846 trace_nfs4_get_acl(inode, ret);
5849 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5850 } while (exception.retry);
5854 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5856 struct nfs_server *server = NFS_SERVER(inode);
5859 if (!nfs4_server_supports_acls(server))
5861 ret = nfs_revalidate_inode(server, inode);
5864 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5865 nfs_zap_acl_cache(inode);
5866 ret = nfs4_read_cached_acl(inode, buf, buflen);
5868 /* -ENOENT is returned if there is no ACL or if there is an ACL
5869 * but no cached acl data, just the acl length */
5871 return nfs4_get_acl_uncached(inode, buf, buflen);
5874 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5876 struct nfs_server *server = NFS_SERVER(inode);
5877 struct page *pages[NFS4ACL_MAXPAGES];
5878 struct nfs_setaclargs arg = {
5879 .fh = NFS_FH(inode),
5883 struct nfs_setaclres res;
5884 struct rpc_message msg = {
5885 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5889 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
5892 /* You can't remove system.nfs4_acl: */
5895 if (!nfs4_server_supports_acls(server))
5897 if (npages > ARRAY_SIZE(pages))
5899 i = nfs4_buf_to_pages_noslab(buf, buflen, arg.acl_pages);
5902 nfs4_inode_make_writeable(inode);
5903 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5906 * Free each page after tx, so the only ref left is
5907 * held by the network stack
5910 put_page(pages[i-1]);
5913 * Acl update can result in inode attribute update.
5914 * so mark the attribute cache invalid.
5916 spin_lock(&inode->i_lock);
5917 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
5918 NFS_INO_INVALID_CTIME |
5919 NFS_INO_REVAL_FORCED);
5920 spin_unlock(&inode->i_lock);
5921 nfs_access_zap_cache(inode);
5922 nfs_zap_acl_cache(inode);
5926 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5928 struct nfs4_exception exception = { };
5931 err = __nfs4_proc_set_acl(inode, buf, buflen);
5932 trace_nfs4_set_acl(inode, err);
5933 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5935 } while (exception.retry);
5939 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
5940 static int _nfs4_get_security_label(struct inode *inode, void *buf,
5943 struct nfs_server *server = NFS_SERVER(inode);
5944 struct nfs_fattr fattr;
5945 struct nfs4_label label = {0, 0, buflen, buf};
5947 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5948 struct nfs4_getattr_arg arg = {
5949 .fh = NFS_FH(inode),
5952 struct nfs4_getattr_res res = {
5957 struct rpc_message msg = {
5958 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
5964 nfs_fattr_init(&fattr);
5966 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
5969 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5974 static int nfs4_get_security_label(struct inode *inode, void *buf,
5977 struct nfs4_exception exception = {
5978 .interruptible = true,
5982 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5986 err = _nfs4_get_security_label(inode, buf, buflen);
5987 trace_nfs4_get_security_label(inode, err);
5988 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5990 } while (exception.retry);
5994 static int _nfs4_do_set_security_label(struct inode *inode,
5995 struct nfs4_label *ilabel,
5996 struct nfs_fattr *fattr,
5997 struct nfs4_label *olabel)
6000 struct iattr sattr = {0};
6001 struct nfs_server *server = NFS_SERVER(inode);
6002 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
6003 struct nfs_setattrargs arg = {
6004 .fh = NFS_FH(inode),
6010 struct nfs_setattrres res = {
6015 struct rpc_message msg = {
6016 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
6022 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
6024 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6026 dprintk("%s failed: %d\n", __func__, status);
6031 static int nfs4_do_set_security_label(struct inode *inode,
6032 struct nfs4_label *ilabel,
6033 struct nfs_fattr *fattr,
6034 struct nfs4_label *olabel)
6036 struct nfs4_exception exception = { };
6040 err = _nfs4_do_set_security_label(inode, ilabel,
6042 trace_nfs4_set_security_label(inode, err);
6043 err = nfs4_handle_exception(NFS_SERVER(inode), err,
6045 } while (exception.retry);
6050 nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
6052 struct nfs4_label ilabel, *olabel = NULL;
6053 struct nfs_fattr fattr;
6056 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
6059 nfs_fattr_init(&fattr);
6063 ilabel.label = (char *)buf;
6064 ilabel.len = buflen;
6066 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
6067 if (IS_ERR(olabel)) {
6068 status = -PTR_ERR(olabel);
6072 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
6074 nfs_setsecurity(inode, &fattr, olabel);
6076 nfs4_label_free(olabel);
6080 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
6083 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
6084 nfs4_verifier *bootverf)
6088 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
6089 /* An impossible timestamp guarantees this value
6090 * will never match a generated boot time. */
6091 verf[0] = cpu_to_be32(U32_MAX);
6092 verf[1] = cpu_to_be32(U32_MAX);
6094 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
6095 u64 ns = ktime_to_ns(nn->boot_time);
6097 verf[0] = cpu_to_be32(ns >> 32);
6098 verf[1] = cpu_to_be32(ns);
6100 memcpy(bootverf->data, verf, sizeof(bootverf->data));
6104 nfs4_get_uniquifier(struct nfs_client *clp, char *buf, size_t buflen)
6106 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
6107 struct nfs_netns_client *nn_clp = nn->nfs_client;
6114 id = rcu_dereference(nn_clp->identifier);
6116 strscpy(buf, id, buflen);
6120 if (nfs4_client_id_uniquifier[0] != '\0' && buf[0] == '\0')
6121 strscpy(buf, nfs4_client_id_uniquifier, buflen);
6127 nfs4_init_nonuniform_client_string(struct nfs_client *clp)
6129 char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6134 if (clp->cl_owner_id != NULL)
6139 strlen(clp->cl_rpcclient->cl_nodename) +
6141 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
6145 buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
6149 if (len > NFS4_OPAQUE_LIMIT + 1)
6153 * Since this string is allocated at mount time, and held until the
6154 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6155 * about a memory-reclaim deadlock.
6157 str = kmalloc(len, GFP_KERNEL);
6163 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
6164 clp->cl_rpcclient->cl_nodename, buf,
6165 rpc_peeraddr2str(clp->cl_rpcclient,
6168 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
6169 clp->cl_rpcclient->cl_nodename,
6170 rpc_peeraddr2str(clp->cl_rpcclient,
6174 clp->cl_owner_id = str;
6179 nfs4_init_uniform_client_string(struct nfs_client *clp)
6181 char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6186 if (clp->cl_owner_id != NULL)
6189 len = 10 + 10 + 1 + 10 + 1 +
6190 strlen(clp->cl_rpcclient->cl_nodename) + 1;
6192 buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
6196 if (len > NFS4_OPAQUE_LIMIT + 1)
6200 * Since this string is allocated at mount time, and held until the
6201 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6202 * about a memory-reclaim deadlock.
6204 str = kmalloc(len, GFP_KERNEL);
6209 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
6210 clp->rpc_ops->version, clp->cl_minorversion,
6211 buf, clp->cl_rpcclient->cl_nodename);
6213 scnprintf(str, len, "Linux NFSv%u.%u %s",
6214 clp->rpc_ops->version, clp->cl_minorversion,
6215 clp->cl_rpcclient->cl_nodename);
6216 clp->cl_owner_id = str;
6221 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
6222 * services. Advertise one based on the address family of the
6226 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
6228 if (strchr(clp->cl_ipaddr, ':') != NULL)
6229 return scnprintf(buf, len, "tcp6");
6231 return scnprintf(buf, len, "tcp");
6234 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
6236 struct nfs4_setclientid *sc = calldata;
6238 if (task->tk_status == 0)
6239 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
6242 static const struct rpc_call_ops nfs4_setclientid_ops = {
6243 .rpc_call_done = nfs4_setclientid_done,
6247 * nfs4_proc_setclientid - Negotiate client ID
6248 * @clp: state data structure
6249 * @program: RPC program for NFSv4 callback service
6250 * @port: IP port number for NFS4 callback service
6251 * @cred: credential to use for this call
6252 * @res: where to place the result
6254 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6256 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
6257 unsigned short port, const struct cred *cred,
6258 struct nfs4_setclientid_res *res)
6260 nfs4_verifier sc_verifier;
6261 struct nfs4_setclientid setclientid = {
6262 .sc_verifier = &sc_verifier,
6266 struct rpc_message msg = {
6267 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
6268 .rpc_argp = &setclientid,
6272 struct rpc_task_setup task_setup_data = {
6273 .rpc_client = clp->cl_rpcclient,
6274 .rpc_message = &msg,
6275 .callback_ops = &nfs4_setclientid_ops,
6276 .callback_data = &setclientid,
6277 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
6279 unsigned long now = jiffies;
6282 /* nfs_client_id4 */
6283 nfs4_init_boot_verifier(clp, &sc_verifier);
6285 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
6286 status = nfs4_init_uniform_client_string(clp);
6288 status = nfs4_init_nonuniform_client_string(clp);
6294 setclientid.sc_netid_len =
6295 nfs4_init_callback_netid(clp,
6296 setclientid.sc_netid,
6297 sizeof(setclientid.sc_netid));
6298 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
6299 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
6300 clp->cl_ipaddr, port >> 8, port & 255);
6302 dprintk("NFS call setclientid auth=%s, '%s'\n",
6303 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6306 status = nfs4_call_sync_custom(&task_setup_data);
6307 if (setclientid.sc_cred) {
6308 kfree(clp->cl_acceptor);
6309 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
6310 put_rpccred(setclientid.sc_cred);
6314 do_renew_lease(clp, now);
6316 trace_nfs4_setclientid(clp, status);
6317 dprintk("NFS reply setclientid: %d\n", status);
6322 * nfs4_proc_setclientid_confirm - Confirm client ID
6323 * @clp: state data structure
6324 * @arg: result of a previous SETCLIENTID
6325 * @cred: credential to use for this call
6327 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6329 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
6330 struct nfs4_setclientid_res *arg,
6331 const struct cred *cred)
6333 struct rpc_message msg = {
6334 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
6340 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
6341 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6343 status = rpc_call_sync(clp->cl_rpcclient, &msg,
6344 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
6345 trace_nfs4_setclientid_confirm(clp, status);
6346 dprintk("NFS reply setclientid_confirm: %d\n", status);
6350 struct nfs4_delegreturndata {
6351 struct nfs4_delegreturnargs args;
6352 struct nfs4_delegreturnres res;
6354 nfs4_stateid stateid;
6355 unsigned long timestamp;
6357 struct nfs4_layoutreturn_args arg;
6358 struct nfs4_layoutreturn_res res;
6359 struct nfs4_xdr_opaque_data ld_private;
6363 struct nfs_fattr fattr;
6365 struct inode *inode;
6368 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6370 struct nfs4_delegreturndata *data = calldata;
6371 struct nfs4_exception exception = {
6372 .inode = data->inode,
6373 .stateid = &data->stateid,
6376 if (!nfs4_sequence_done(task, &data->res.seq_res))
6379 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
6381 /* Handle Layoutreturn errors */
6382 if (pnfs_roc_done(task, &data->args.lr_args, &data->res.lr_res,
6383 &data->res.lr_ret) == -EAGAIN)
6386 switch (task->tk_status) {
6388 renew_lease(data->res.server, data->timestamp);
6390 case -NFS4ERR_ADMIN_REVOKED:
6391 case -NFS4ERR_DELEG_REVOKED:
6392 case -NFS4ERR_EXPIRED:
6393 nfs4_free_revoked_stateid(data->res.server,
6395 task->tk_msg.rpc_cred);
6397 case -NFS4ERR_BAD_STATEID:
6398 case -NFS4ERR_STALE_STATEID:
6400 task->tk_status = 0;
6402 case -NFS4ERR_OLD_STATEID:
6403 if (!nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6404 nfs4_stateid_seqid_inc(&data->stateid);
6405 if (data->args.bitmask) {
6406 data->args.bitmask = NULL;
6407 data->res.fattr = NULL;
6410 case -NFS4ERR_ACCESS:
6411 if (data->args.bitmask) {
6412 data->args.bitmask = NULL;
6413 data->res.fattr = NULL;
6418 task->tk_status = nfs4_async_handle_exception(task,
6419 data->res.server, task->tk_status,
6421 if (exception.retry)
6424 nfs_delegation_mark_returned(data->inode, data->args.stateid);
6425 data->rpc_status = task->tk_status;
6428 task->tk_status = 0;
6429 rpc_restart_call_prepare(task);
6432 static void nfs4_delegreturn_release(void *calldata)
6434 struct nfs4_delegreturndata *data = calldata;
6435 struct inode *inode = data->inode;
6438 pnfs_roc_release(&data->lr.arg, &data->lr.res,
6441 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
6442 nfs_iput_and_deactive(inode);
6447 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6449 struct nfs4_delegreturndata *d_data;
6450 struct pnfs_layout_hdr *lo;
6452 d_data = (struct nfs4_delegreturndata *)data;
6454 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task)) {
6455 nfs4_sequence_done(task, &d_data->res.seq_res);
6459 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6460 if (lo && !pnfs_layout_is_valid(lo)) {
6461 d_data->args.lr_args = NULL;
6462 d_data->res.lr_res = NULL;
6465 nfs4_setup_sequence(d_data->res.server->nfs_client,
6466 &d_data->args.seq_args,
6467 &d_data->res.seq_res,
6471 static const struct rpc_call_ops nfs4_delegreturn_ops = {
6472 .rpc_call_prepare = nfs4_delegreturn_prepare,
6473 .rpc_call_done = nfs4_delegreturn_done,
6474 .rpc_release = nfs4_delegreturn_release,
6477 static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6479 struct nfs4_delegreturndata *data;
6480 struct nfs_server *server = NFS_SERVER(inode);
6481 struct rpc_task *task;
6482 struct rpc_message msg = {
6483 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6486 struct rpc_task_setup task_setup_data = {
6487 .rpc_client = server->client,
6488 .rpc_message = &msg,
6489 .callback_ops = &nfs4_delegreturn_ops,
6490 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
6494 data = kzalloc(sizeof(*data), GFP_NOFS);
6497 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
6499 nfs4_state_protect(server->nfs_client,
6500 NFS_SP4_MACH_CRED_CLEANUP,
6501 &task_setup_data.rpc_client, &msg);
6503 data->args.fhandle = &data->fh;
6504 data->args.stateid = &data->stateid;
6505 data->args.bitmask = server->cache_consistency_bitmask;
6506 nfs4_bitmask_adjust(data->args.bitmask, inode, server, NULL);
6507 nfs_copy_fh(&data->fh, NFS_FH(inode));
6508 nfs4_stateid_copy(&data->stateid, stateid);
6509 data->res.fattr = &data->fattr;
6510 data->res.server = server;
6511 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
6512 data->lr.arg.ld_private = &data->lr.ld_private;
6513 nfs_fattr_init(data->res.fattr);
6514 data->timestamp = jiffies;
6515 data->rpc_status = 0;
6516 data->inode = nfs_igrab_and_active(inode);
6517 if (data->inode || issync) {
6518 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res,
6521 data->args.lr_args = &data->lr.arg;
6522 data->res.lr_res = &data->lr.res;
6526 task_setup_data.callback_data = data;
6527 msg.rpc_argp = &data->args;
6528 msg.rpc_resp = &data->res;
6529 task = rpc_run_task(&task_setup_data);
6531 return PTR_ERR(task);
6534 status = rpc_wait_for_completion_task(task);
6537 status = data->rpc_status;
6543 int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6545 struct nfs_server *server = NFS_SERVER(inode);
6546 struct nfs4_exception exception = { };
6549 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
6550 trace_nfs4_delegreturn(inode, stateid, err);
6552 case -NFS4ERR_STALE_STATEID:
6553 case -NFS4ERR_EXPIRED:
6557 err = nfs4_handle_exception(server, err, &exception);
6558 } while (exception.retry);
6562 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6564 struct inode *inode = state->inode;
6565 struct nfs_server *server = NFS_SERVER(inode);
6566 struct nfs_client *clp = server->nfs_client;
6567 struct nfs_lockt_args arg = {
6568 .fh = NFS_FH(inode),
6571 struct nfs_lockt_res res = {
6574 struct rpc_message msg = {
6575 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
6578 .rpc_cred = state->owner->so_cred,
6580 struct nfs4_lock_state *lsp;
6583 arg.lock_owner.clientid = clp->cl_clientid;
6584 status = nfs4_set_lock_state(state, request);
6587 lsp = request->fl_u.nfs4_fl.owner;
6588 arg.lock_owner.id = lsp->ls_seqid.owner_id;
6589 arg.lock_owner.s_dev = server->s_dev;
6590 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6593 request->fl_type = F_UNLCK;
6595 case -NFS4ERR_DENIED:
6598 request->fl_ops->fl_release_private(request);
6599 request->fl_ops = NULL;
6604 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6606 struct nfs4_exception exception = {
6607 .interruptible = true,
6612 err = _nfs4_proc_getlk(state, cmd, request);
6613 trace_nfs4_get_lock(request, state, cmd, err);
6614 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
6616 } while (exception.retry);
6621 * Update the seqid of a lock stateid after receiving
6622 * NFS4ERR_OLD_STATEID
6624 static bool nfs4_refresh_lock_old_stateid(nfs4_stateid *dst,
6625 struct nfs4_lock_state *lsp)
6627 struct nfs4_state *state = lsp->ls_state;
6630 spin_lock(&state->state_lock);
6631 if (!nfs4_stateid_match_other(dst, &lsp->ls_stateid))
6633 if (!nfs4_stateid_is_newer(&lsp->ls_stateid, dst))
6634 nfs4_stateid_seqid_inc(dst);
6636 dst->seqid = lsp->ls_stateid.seqid;
6639 spin_unlock(&state->state_lock);
6643 static bool nfs4_sync_lock_stateid(nfs4_stateid *dst,
6644 struct nfs4_lock_state *lsp)
6646 struct nfs4_state *state = lsp->ls_state;
6649 spin_lock(&state->state_lock);
6650 ret = !nfs4_stateid_match_other(dst, &lsp->ls_stateid);
6651 nfs4_stateid_copy(dst, &lsp->ls_stateid);
6652 spin_unlock(&state->state_lock);
6656 struct nfs4_unlockdata {
6657 struct nfs_locku_args arg;
6658 struct nfs_locku_res res;
6659 struct nfs4_lock_state *lsp;
6660 struct nfs_open_context *ctx;
6661 struct nfs_lock_context *l_ctx;
6662 struct file_lock fl;
6663 struct nfs_server *server;
6664 unsigned long timestamp;
6667 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6668 struct nfs_open_context *ctx,
6669 struct nfs4_lock_state *lsp,
6670 struct nfs_seqid *seqid)
6672 struct nfs4_unlockdata *p;
6673 struct nfs4_state *state = lsp->ls_state;
6674 struct inode *inode = state->inode;
6676 p = kzalloc(sizeof(*p), GFP_NOFS);
6679 p->arg.fh = NFS_FH(inode);
6681 p->arg.seqid = seqid;
6682 p->res.seqid = seqid;
6684 /* Ensure we don't close file until we're done freeing locks! */
6685 p->ctx = get_nfs_open_context(ctx);
6686 p->l_ctx = nfs_get_lock_context(ctx);
6687 locks_init_lock(&p->fl);
6688 locks_copy_lock(&p->fl, fl);
6689 p->server = NFS_SERVER(inode);
6690 spin_lock(&state->state_lock);
6691 nfs4_stateid_copy(&p->arg.stateid, &lsp->ls_stateid);
6692 spin_unlock(&state->state_lock);
6696 static void nfs4_locku_release_calldata(void *data)
6698 struct nfs4_unlockdata *calldata = data;
6699 nfs_free_seqid(calldata->arg.seqid);
6700 nfs4_put_lock_state(calldata->lsp);
6701 nfs_put_lock_context(calldata->l_ctx);
6702 put_nfs_open_context(calldata->ctx);
6706 static void nfs4_locku_done(struct rpc_task *task, void *data)
6708 struct nfs4_unlockdata *calldata = data;
6709 struct nfs4_exception exception = {
6710 .inode = calldata->lsp->ls_state->inode,
6711 .stateid = &calldata->arg.stateid,
6714 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6716 switch (task->tk_status) {
6718 renew_lease(calldata->server, calldata->timestamp);
6719 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
6720 if (nfs4_update_lock_stateid(calldata->lsp,
6721 &calldata->res.stateid))
6724 case -NFS4ERR_ADMIN_REVOKED:
6725 case -NFS4ERR_EXPIRED:
6726 nfs4_free_revoked_stateid(calldata->server,
6727 &calldata->arg.stateid,
6728 task->tk_msg.rpc_cred);
6730 case -NFS4ERR_BAD_STATEID:
6731 case -NFS4ERR_STALE_STATEID:
6732 if (nfs4_sync_lock_stateid(&calldata->arg.stateid,
6734 rpc_restart_call_prepare(task);
6736 case -NFS4ERR_OLD_STATEID:
6737 if (nfs4_refresh_lock_old_stateid(&calldata->arg.stateid,
6739 rpc_restart_call_prepare(task);
6742 task->tk_status = nfs4_async_handle_exception(task,
6743 calldata->server, task->tk_status,
6745 if (exception.retry)
6746 rpc_restart_call_prepare(task);
6748 nfs_release_seqid(calldata->arg.seqid);
6751 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
6753 struct nfs4_unlockdata *calldata = data;
6755 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6756 nfs_async_iocounter_wait(task, calldata->l_ctx))
6759 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
6761 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
6762 /* Note: exit _without_ running nfs4_locku_done */
6765 calldata->timestamp = jiffies;
6766 if (nfs4_setup_sequence(calldata->server->nfs_client,
6767 &calldata->arg.seq_args,
6768 &calldata->res.seq_res,
6770 nfs_release_seqid(calldata->arg.seqid);
6773 task->tk_action = NULL;
6775 nfs4_sequence_done(task, &calldata->res.seq_res);
6778 static const struct rpc_call_ops nfs4_locku_ops = {
6779 .rpc_call_prepare = nfs4_locku_prepare,
6780 .rpc_call_done = nfs4_locku_done,
6781 .rpc_release = nfs4_locku_release_calldata,
6784 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6785 struct nfs_open_context *ctx,
6786 struct nfs4_lock_state *lsp,
6787 struct nfs_seqid *seqid)
6789 struct nfs4_unlockdata *data;
6790 struct rpc_message msg = {
6791 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6792 .rpc_cred = ctx->cred,
6794 struct rpc_task_setup task_setup_data = {
6795 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
6796 .rpc_message = &msg,
6797 .callback_ops = &nfs4_locku_ops,
6798 .workqueue = nfsiod_workqueue,
6799 .flags = RPC_TASK_ASYNC,
6802 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6803 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6805 /* Ensure this is an unlock - when canceling a lock, the
6806 * canceled lock is passed in, and it won't be an unlock.
6808 fl->fl_type = F_UNLCK;
6809 if (fl->fl_flags & FL_CLOSE)
6810 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
6812 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6814 nfs_free_seqid(seqid);
6815 return ERR_PTR(-ENOMEM);
6818 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
6819 msg.rpc_argp = &data->arg;
6820 msg.rpc_resp = &data->res;
6821 task_setup_data.callback_data = data;
6822 return rpc_run_task(&task_setup_data);
6825 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6827 struct inode *inode = state->inode;
6828 struct nfs4_state_owner *sp = state->owner;
6829 struct nfs_inode *nfsi = NFS_I(inode);
6830 struct nfs_seqid *seqid;
6831 struct nfs4_lock_state *lsp;
6832 struct rpc_task *task;
6833 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6835 unsigned char fl_flags = request->fl_flags;
6837 status = nfs4_set_lock_state(state, request);
6838 /* Unlock _before_ we do the RPC call */
6839 request->fl_flags |= FL_EXISTS;
6840 /* Exclude nfs_delegation_claim_locks() */
6841 mutex_lock(&sp->so_delegreturn_mutex);
6842 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
6843 down_read(&nfsi->rwsem);
6844 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
6845 up_read(&nfsi->rwsem);
6846 mutex_unlock(&sp->so_delegreturn_mutex);
6849 up_read(&nfsi->rwsem);
6850 mutex_unlock(&sp->so_delegreturn_mutex);
6853 /* Is this a delegated lock? */
6854 lsp = request->fl_u.nfs4_fl.owner;
6855 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6857 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6858 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
6862 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
6863 status = PTR_ERR(task);
6866 status = rpc_wait_for_completion_task(task);
6869 request->fl_flags = fl_flags;
6870 trace_nfs4_unlock(request, state, F_SETLK, status);
6874 struct nfs4_lockdata {
6875 struct nfs_lock_args arg;
6876 struct nfs_lock_res res;
6877 struct nfs4_lock_state *lsp;
6878 struct nfs_open_context *ctx;
6879 struct file_lock fl;
6880 unsigned long timestamp;
6883 struct nfs_server *server;
6886 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
6887 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6890 struct nfs4_lockdata *p;
6891 struct inode *inode = lsp->ls_state->inode;
6892 struct nfs_server *server = NFS_SERVER(inode);
6893 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6895 p = kzalloc(sizeof(*p), gfp_mask);
6899 p->arg.fh = NFS_FH(inode);
6901 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
6902 if (IS_ERR(p->arg.open_seqid))
6904 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6905 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
6906 if (IS_ERR(p->arg.lock_seqid))
6907 goto out_free_seqid;
6908 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
6909 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
6910 p->arg.lock_owner.s_dev = server->s_dev;
6911 p->res.lock_seqid = p->arg.lock_seqid;
6914 p->ctx = get_nfs_open_context(ctx);
6915 locks_init_lock(&p->fl);
6916 locks_copy_lock(&p->fl, fl);
6919 nfs_free_seqid(p->arg.open_seqid);
6925 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6927 struct nfs4_lockdata *data = calldata;
6928 struct nfs4_state *state = data->lsp->ls_state;
6930 dprintk("%s: begin!\n", __func__);
6931 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
6933 /* Do we need to do an open_to_lock_owner? */
6934 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
6935 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
6936 goto out_release_lock_seqid;
6938 nfs4_stateid_copy(&data->arg.open_stateid,
6939 &state->open_stateid);
6940 data->arg.new_lock_owner = 1;
6941 data->res.open_seqid = data->arg.open_seqid;
6943 data->arg.new_lock_owner = 0;
6944 nfs4_stateid_copy(&data->arg.lock_stateid,
6945 &data->lsp->ls_stateid);
6947 if (!nfs4_valid_open_stateid(state)) {
6948 data->rpc_status = -EBADF;
6949 task->tk_action = NULL;
6950 goto out_release_open_seqid;
6952 data->timestamp = jiffies;
6953 if (nfs4_setup_sequence(data->server->nfs_client,
6954 &data->arg.seq_args,
6958 out_release_open_seqid:
6959 nfs_release_seqid(data->arg.open_seqid);
6960 out_release_lock_seqid:
6961 nfs_release_seqid(data->arg.lock_seqid);
6963 nfs4_sequence_done(task, &data->res.seq_res);
6964 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
6967 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6969 struct nfs4_lockdata *data = calldata;
6970 struct nfs4_lock_state *lsp = data->lsp;
6972 dprintk("%s: begin!\n", __func__);
6974 if (!nfs4_sequence_done(task, &data->res.seq_res))
6977 data->rpc_status = task->tk_status;
6978 switch (task->tk_status) {
6980 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
6982 if (data->arg.new_lock && !data->cancelled) {
6983 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
6984 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
6987 if (data->arg.new_lock_owner != 0) {
6988 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6989 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6990 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6991 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6994 case -NFS4ERR_BAD_STATEID:
6995 case -NFS4ERR_OLD_STATEID:
6996 case -NFS4ERR_STALE_STATEID:
6997 case -NFS4ERR_EXPIRED:
6998 if (data->arg.new_lock_owner != 0) {
6999 if (!nfs4_stateid_match(&data->arg.open_stateid,
7000 &lsp->ls_state->open_stateid))
7002 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
7007 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
7010 if (!data->cancelled)
7011 rpc_restart_call_prepare(task);
7015 static void nfs4_lock_release(void *calldata)
7017 struct nfs4_lockdata *data = calldata;
7019 dprintk("%s: begin!\n", __func__);
7020 nfs_free_seqid(data->arg.open_seqid);
7021 if (data->cancelled && data->rpc_status == 0) {
7022 struct rpc_task *task;
7023 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
7024 data->arg.lock_seqid);
7026 rpc_put_task_async(task);
7027 dprintk("%s: cancelling lock!\n", __func__);
7029 nfs_free_seqid(data->arg.lock_seqid);
7030 nfs4_put_lock_state(data->lsp);
7031 put_nfs_open_context(data->ctx);
7033 dprintk("%s: done!\n", __func__);
7036 static const struct rpc_call_ops nfs4_lock_ops = {
7037 .rpc_call_prepare = nfs4_lock_prepare,
7038 .rpc_call_done = nfs4_lock_done,
7039 .rpc_release = nfs4_lock_release,
7042 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
7045 case -NFS4ERR_ADMIN_REVOKED:
7046 case -NFS4ERR_EXPIRED:
7047 case -NFS4ERR_BAD_STATEID:
7048 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
7049 if (new_lock_owner != 0 ||
7050 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
7051 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
7053 case -NFS4ERR_STALE_STATEID:
7054 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
7055 nfs4_schedule_lease_recovery(server->nfs_client);
7059 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
7061 struct nfs4_lockdata *data;
7062 struct rpc_task *task;
7063 struct rpc_message msg = {
7064 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
7065 .rpc_cred = state->owner->so_cred,
7067 struct rpc_task_setup task_setup_data = {
7068 .rpc_client = NFS_CLIENT(state->inode),
7069 .rpc_message = &msg,
7070 .callback_ops = &nfs4_lock_ops,
7071 .workqueue = nfsiod_workqueue,
7072 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
7076 dprintk("%s: begin!\n", __func__);
7077 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
7078 fl->fl_u.nfs4_fl.owner,
7079 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
7083 data->arg.block = 1;
7084 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
7085 recovery_type > NFS_LOCK_NEW);
7086 msg.rpc_argp = &data->arg;
7087 msg.rpc_resp = &data->res;
7088 task_setup_data.callback_data = data;
7089 if (recovery_type > NFS_LOCK_NEW) {
7090 if (recovery_type == NFS_LOCK_RECLAIM)
7091 data->arg.reclaim = NFS_LOCK_RECLAIM;
7093 data->arg.new_lock = 1;
7094 task = rpc_run_task(&task_setup_data);
7096 return PTR_ERR(task);
7097 ret = rpc_wait_for_completion_task(task);
7099 ret = data->rpc_status;
7101 nfs4_handle_setlk_error(data->server, data->lsp,
7102 data->arg.new_lock_owner, ret);
7104 data->cancelled = true;
7105 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
7107 dprintk("%s: done, ret = %d!\n", __func__, ret);
7111 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
7113 struct nfs_server *server = NFS_SERVER(state->inode);
7114 struct nfs4_exception exception = {
7115 .inode = state->inode,
7120 /* Cache the lock if possible... */
7121 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7123 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
7124 if (err != -NFS4ERR_DELAY)
7126 nfs4_handle_exception(server, err, &exception);
7127 } while (exception.retry);
7131 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
7133 struct nfs_server *server = NFS_SERVER(state->inode);
7134 struct nfs4_exception exception = {
7135 .inode = state->inode,
7139 err = nfs4_set_lock_state(state, request);
7142 if (!recover_lost_locks) {
7143 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
7147 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7149 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
7153 case -NFS4ERR_GRACE:
7154 case -NFS4ERR_DELAY:
7155 nfs4_handle_exception(server, err, &exception);
7158 } while (exception.retry);
7163 #if defined(CONFIG_NFS_V4_1)
7164 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
7166 struct nfs4_lock_state *lsp;
7169 status = nfs4_set_lock_state(state, request);
7172 lsp = request->fl_u.nfs4_fl.owner;
7173 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
7174 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
7176 return nfs4_lock_expired(state, request);
7180 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7182 struct nfs_inode *nfsi = NFS_I(state->inode);
7183 struct nfs4_state_owner *sp = state->owner;
7184 unsigned char fl_flags = request->fl_flags;
7187 request->fl_flags |= FL_ACCESS;
7188 status = locks_lock_inode_wait(state->inode, request);
7191 mutex_lock(&sp->so_delegreturn_mutex);
7192 down_read(&nfsi->rwsem);
7193 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
7194 /* Yes: cache locks! */
7195 /* ...but avoid races with delegation recall... */
7196 request->fl_flags = fl_flags & ~FL_SLEEP;
7197 status = locks_lock_inode_wait(state->inode, request);
7198 up_read(&nfsi->rwsem);
7199 mutex_unlock(&sp->so_delegreturn_mutex);
7202 up_read(&nfsi->rwsem);
7203 mutex_unlock(&sp->so_delegreturn_mutex);
7204 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
7206 request->fl_flags = fl_flags;
7210 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7212 struct nfs4_exception exception = {
7214 .inode = state->inode,
7215 .interruptible = true,
7220 err = _nfs4_proc_setlk(state, cmd, request);
7221 if (err == -NFS4ERR_DENIED)
7223 err = nfs4_handle_exception(NFS_SERVER(state->inode),
7225 } while (exception.retry);
7229 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
7230 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
7233 nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
7234 struct file_lock *request)
7236 int status = -ERESTARTSYS;
7237 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
7239 while(!signalled()) {
7240 status = nfs4_proc_setlk(state, cmd, request);
7241 if ((status != -EAGAIN) || IS_SETLK(cmd))
7243 freezable_schedule_timeout_interruptible(timeout);
7245 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
7246 status = -ERESTARTSYS;
7251 #ifdef CONFIG_NFS_V4_1
7252 struct nfs4_lock_waiter {
7253 struct task_struct *task;
7254 struct inode *inode;
7255 struct nfs_lowner *owner;
7259 nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
7262 struct nfs4_lock_waiter *waiter = wait->private;
7264 /* NULL key means to wake up everyone */
7266 struct cb_notify_lock_args *cbnl = key;
7267 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
7268 *wowner = waiter->owner;
7270 /* Only wake if the callback was for the same owner. */
7271 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
7274 /* Make sure it's for the right inode */
7275 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
7279 /* override "private" so we can use default_wake_function */
7280 wait->private = waiter->task;
7281 ret = woken_wake_function(wait, mode, flags, key);
7283 list_del_init(&wait->entry);
7284 wait->private = waiter;
7289 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7291 int status = -ERESTARTSYS;
7292 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
7293 struct nfs_server *server = NFS_SERVER(state->inode);
7294 struct nfs_client *clp = server->nfs_client;
7295 wait_queue_head_t *q = &clp->cl_lock_waitq;
7296 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
7297 .id = lsp->ls_seqid.owner_id,
7298 .s_dev = server->s_dev };
7299 struct nfs4_lock_waiter waiter = { .task = current,
7300 .inode = state->inode,
7302 wait_queue_entry_t wait;
7304 /* Don't bother with waitqueue if we don't expect a callback */
7305 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
7306 return nfs4_retry_setlk_simple(state, cmd, request);
7309 wait.private = &waiter;
7310 wait.func = nfs4_wake_lock_waiter;
7312 while(!signalled()) {
7313 add_wait_queue(q, &wait);
7314 status = nfs4_proc_setlk(state, cmd, request);
7315 if ((status != -EAGAIN) || IS_SETLK(cmd)) {
7316 finish_wait(q, &wait);
7320 status = -ERESTARTSYS;
7321 freezer_do_not_count();
7322 wait_woken(&wait, TASK_INTERRUPTIBLE, NFS4_LOCK_MAXTIMEOUT);
7324 finish_wait(q, &wait);
7329 #else /* !CONFIG_NFS_V4_1 */
7331 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7333 return nfs4_retry_setlk_simple(state, cmd, request);
7338 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
7340 struct nfs_open_context *ctx;
7341 struct nfs4_state *state;
7344 /* verify open state */
7345 ctx = nfs_file_open_context(filp);
7348 if (IS_GETLK(cmd)) {
7350 return nfs4_proc_getlk(state, F_GETLK, request);
7354 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
7357 if (request->fl_type == F_UNLCK) {
7359 return nfs4_proc_unlck(state, cmd, request);
7366 if ((request->fl_flags & FL_POSIX) &&
7367 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
7371 * Don't rely on the VFS having checked the file open mode,
7372 * since it won't do this for flock() locks.
7374 switch (request->fl_type) {
7376 if (!(filp->f_mode & FMODE_READ))
7380 if (!(filp->f_mode & FMODE_WRITE))
7384 status = nfs4_set_lock_state(state, request);
7388 return nfs4_retry_setlk(state, cmd, request);
7391 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
7393 struct nfs_server *server = NFS_SERVER(state->inode);
7396 err = nfs4_set_lock_state(state, fl);
7400 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
7401 if (err != -NFS4ERR_DELAY)
7404 } while (err == -NFS4ERR_DELAY);
7405 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
7408 struct nfs_release_lockowner_data {
7409 struct nfs4_lock_state *lsp;
7410 struct nfs_server *server;
7411 struct nfs_release_lockowner_args args;
7412 struct nfs_release_lockowner_res res;
7413 unsigned long timestamp;
7416 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7418 struct nfs_release_lockowner_data *data = calldata;
7419 struct nfs_server *server = data->server;
7420 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7421 &data->res.seq_res, task);
7422 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7423 data->timestamp = jiffies;
7426 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7428 struct nfs_release_lockowner_data *data = calldata;
7429 struct nfs_server *server = data->server;
7431 nfs40_sequence_done(task, &data->res.seq_res);
7433 switch (task->tk_status) {
7435 renew_lease(server, data->timestamp);
7437 case -NFS4ERR_STALE_CLIENTID:
7438 case -NFS4ERR_EXPIRED:
7439 nfs4_schedule_lease_recovery(server->nfs_client);
7441 case -NFS4ERR_LEASE_MOVED:
7442 case -NFS4ERR_DELAY:
7443 if (nfs4_async_handle_error(task, server,
7444 NULL, NULL) == -EAGAIN)
7445 rpc_restart_call_prepare(task);
7449 static void nfs4_release_lockowner_release(void *calldata)
7451 struct nfs_release_lockowner_data *data = calldata;
7452 nfs4_free_lock_state(data->server, data->lsp);
7456 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
7457 .rpc_call_prepare = nfs4_release_lockowner_prepare,
7458 .rpc_call_done = nfs4_release_lockowner_done,
7459 .rpc_release = nfs4_release_lockowner_release,
7463 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
7465 struct nfs_release_lockowner_data *data;
7466 struct rpc_message msg = {
7467 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7470 if (server->nfs_client->cl_mvops->minor_version != 0)
7473 data = kmalloc(sizeof(*data), GFP_NOFS);
7477 data->server = server;
7478 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7479 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7480 data->args.lock_owner.s_dev = server->s_dev;
7482 msg.rpc_argp = &data->args;
7483 msg.rpc_resp = &data->res;
7484 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
7485 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
7488 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7490 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
7491 struct user_namespace *mnt_userns,
7492 struct dentry *unused, struct inode *inode,
7493 const char *key, const void *buf,
7494 size_t buflen, int flags)
7496 return nfs4_proc_set_acl(inode, buf, buflen);
7499 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
7500 struct dentry *unused, struct inode *inode,
7501 const char *key, void *buf, size_t buflen)
7503 return nfs4_proc_get_acl(inode, buf, buflen);
7506 static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
7508 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
7511 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
7513 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
7514 struct user_namespace *mnt_userns,
7515 struct dentry *unused, struct inode *inode,
7516 const char *key, const void *buf,
7517 size_t buflen, int flags)
7519 if (security_ismaclabel(key))
7520 return nfs4_set_security_label(inode, buf, buflen);
7525 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
7526 struct dentry *unused, struct inode *inode,
7527 const char *key, void *buf, size_t buflen)
7529 if (security_ismaclabel(key))
7530 return nfs4_get_security_label(inode, buf, buflen);
7535 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7539 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7540 len = security_inode_listsecurity(inode, list, list_len);
7541 if (len >= 0 && list_len && len > list_len)
7547 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7548 .prefix = XATTR_SECURITY_PREFIX,
7549 .get = nfs4_xattr_get_nfs4_label,
7550 .set = nfs4_xattr_set_nfs4_label,
7556 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7563 #ifdef CONFIG_NFS_V4_2
7564 static int nfs4_xattr_set_nfs4_user(const struct xattr_handler *handler,
7565 struct user_namespace *mnt_userns,
7566 struct dentry *unused, struct inode *inode,
7567 const char *key, const void *buf,
7568 size_t buflen, int flags)
7570 struct nfs_access_entry cache;
7573 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7577 * There is no mapping from the MAY_* flags to the NFS_ACCESS_XA*
7578 * flags right now. Handling of xattr operations use the normal
7579 * file read/write permissions.
7581 * Just in case the server has other ideas (which RFC 8276 allows),
7582 * do a cached access check for the XA* flags to possibly avoid
7583 * doing an RPC and getting EACCES back.
7585 if (!nfs_access_get_cached(inode, current_cred(), &cache, true)) {
7586 if (!(cache.mask & NFS_ACCESS_XAWRITE))
7591 ret = nfs42_proc_removexattr(inode, key);
7593 nfs4_xattr_cache_remove(inode, key);
7595 ret = nfs42_proc_setxattr(inode, key, buf, buflen, flags);
7597 nfs4_xattr_cache_add(inode, key, buf, NULL, buflen);
7603 static int nfs4_xattr_get_nfs4_user(const struct xattr_handler *handler,
7604 struct dentry *unused, struct inode *inode,
7605 const char *key, void *buf, size_t buflen)
7607 struct nfs_access_entry cache;
7610 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7613 if (!nfs_access_get_cached(inode, current_cred(), &cache, true)) {
7614 if (!(cache.mask & NFS_ACCESS_XAREAD))
7618 ret = nfs_revalidate_inode(NFS_SERVER(inode), inode);
7622 ret = nfs4_xattr_cache_get(inode, key, buf, buflen);
7623 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7626 ret = nfs42_proc_getxattr(inode, key, buf, buflen);
7632 nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7639 struct nfs_access_entry cache;
7641 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7644 if (!nfs_access_get_cached(inode, current_cred(), &cache, true)) {
7645 if (!(cache.mask & NFS_ACCESS_XALIST))
7649 ret = nfs_revalidate_inode(NFS_SERVER(inode), inode);
7653 ret = nfs4_xattr_cache_list(inode, list, list_len);
7654 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7659 buflen = list_len ? list_len : XATTR_LIST_MAX;
7660 buf = list_len ? list : NULL;
7664 ret = nfs42_proc_listxattrs(inode, buf, buflen,
7677 nfs4_xattr_cache_set_list(inode, list, size);
7685 nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7689 #endif /* CONFIG_NFS_V4_2 */
7692 * nfs_fhget will use either the mounted_on_fileid or the fileid
7694 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7696 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7697 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7698 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
7699 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
7702 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
7703 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
7704 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7708 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7709 const struct qstr *name,
7710 struct nfs4_fs_locations *fs_locations,
7713 struct nfs_server *server = NFS_SERVER(dir);
7715 struct nfs4_fs_locations_arg args = {
7716 .dir_fh = NFS_FH(dir),
7721 struct nfs4_fs_locations_res res = {
7722 .fs_locations = fs_locations,
7724 struct rpc_message msg = {
7725 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7731 dprintk("%s: start\n", __func__);
7733 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7734 bitmask[1] = nfs4_fattr_bitmap[1];
7736 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7737 * is not supported */
7738 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
7739 bitmask[0] &= ~FATTR4_WORD0_FILEID;
7741 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
7743 nfs_fattr_init(&fs_locations->fattr);
7744 fs_locations->server = server;
7745 fs_locations->nlocations = 0;
7746 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
7747 dprintk("%s: returned status = %d\n", __func__, status);
7751 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7752 const struct qstr *name,
7753 struct nfs4_fs_locations *fs_locations,
7756 struct nfs4_exception exception = {
7757 .interruptible = true,
7761 err = _nfs4_proc_fs_locations(client, dir, name,
7762 fs_locations, page);
7763 trace_nfs4_get_fs_locations(dir, name, err);
7764 err = nfs4_handle_exception(NFS_SERVER(dir), err,
7766 } while (exception.retry);
7771 * This operation also signals the server that this client is
7772 * performing migration recovery. The server can stop returning
7773 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7774 * appended to this compound to identify the client ID which is
7775 * performing recovery.
7777 static int _nfs40_proc_get_locations(struct inode *inode,
7778 struct nfs4_fs_locations *locations,
7779 struct page *page, const struct cred *cred)
7781 struct nfs_server *server = NFS_SERVER(inode);
7782 struct rpc_clnt *clnt = server->client;
7784 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7786 struct nfs4_fs_locations_arg args = {
7787 .clientid = server->nfs_client->cl_clientid,
7788 .fh = NFS_FH(inode),
7791 .migration = 1, /* skip LOOKUP */
7792 .renew = 1, /* append RENEW */
7794 struct nfs4_fs_locations_res res = {
7795 .fs_locations = locations,
7799 struct rpc_message msg = {
7800 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7805 unsigned long now = jiffies;
7808 nfs_fattr_init(&locations->fattr);
7809 locations->server = server;
7810 locations->nlocations = 0;
7812 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7813 status = nfs4_call_sync_sequence(clnt, server, &msg,
7814 &args.seq_args, &res.seq_res);
7818 renew_lease(server, now);
7822 #ifdef CONFIG_NFS_V4_1
7825 * This operation also signals the server that this client is
7826 * performing migration recovery. The server can stop asserting
7827 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7828 * performing this operation is identified in the SEQUENCE
7829 * operation in this compound.
7831 * When the client supports GETATTR(fs_locations_info), it can
7832 * be plumbed in here.
7834 static int _nfs41_proc_get_locations(struct inode *inode,
7835 struct nfs4_fs_locations *locations,
7836 struct page *page, const struct cred *cred)
7838 struct nfs_server *server = NFS_SERVER(inode);
7839 struct rpc_clnt *clnt = server->client;
7841 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7843 struct nfs4_fs_locations_arg args = {
7844 .fh = NFS_FH(inode),
7847 .migration = 1, /* skip LOOKUP */
7849 struct nfs4_fs_locations_res res = {
7850 .fs_locations = locations,
7853 struct rpc_message msg = {
7854 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7861 nfs_fattr_init(&locations->fattr);
7862 locations->server = server;
7863 locations->nlocations = 0;
7865 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7866 status = nfs4_call_sync_sequence(clnt, server, &msg,
7867 &args.seq_args, &res.seq_res);
7868 if (status == NFS4_OK &&
7869 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7870 status = -NFS4ERR_LEASE_MOVED;
7874 #endif /* CONFIG_NFS_V4_1 */
7877 * nfs4_proc_get_locations - discover locations for a migrated FSID
7878 * @inode: inode on FSID that is migrating
7879 * @locations: result of query
7881 * @cred: credential to use for this operation
7883 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7884 * operation failed, or a negative errno if a local error occurred.
7886 * On success, "locations" is filled in, but if the server has
7887 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7890 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7891 * from this client that require migration recovery.
7893 int nfs4_proc_get_locations(struct inode *inode,
7894 struct nfs4_fs_locations *locations,
7895 struct page *page, const struct cred *cred)
7897 struct nfs_server *server = NFS_SERVER(inode);
7898 struct nfs_client *clp = server->nfs_client;
7899 const struct nfs4_mig_recovery_ops *ops =
7900 clp->cl_mvops->mig_recovery_ops;
7901 struct nfs4_exception exception = {
7902 .interruptible = true,
7906 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7907 (unsigned long long)server->fsid.major,
7908 (unsigned long long)server->fsid.minor,
7910 nfs_display_fhandle(NFS_FH(inode), __func__);
7913 status = ops->get_locations(inode, locations, page, cred);
7914 if (status != -NFS4ERR_DELAY)
7916 nfs4_handle_exception(server, status, &exception);
7917 } while (exception.retry);
7922 * This operation also signals the server that this client is
7923 * performing "lease moved" recovery. The server can stop
7924 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7925 * is appended to this compound to identify the client ID which is
7926 * performing recovery.
7928 static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
7930 struct nfs_server *server = NFS_SERVER(inode);
7931 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7932 struct rpc_clnt *clnt = server->client;
7933 struct nfs4_fsid_present_arg args = {
7934 .fh = NFS_FH(inode),
7935 .clientid = clp->cl_clientid,
7936 .renew = 1, /* append RENEW */
7938 struct nfs4_fsid_present_res res = {
7941 struct rpc_message msg = {
7942 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7947 unsigned long now = jiffies;
7950 res.fh = nfs_alloc_fhandle();
7954 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7955 status = nfs4_call_sync_sequence(clnt, server, &msg,
7956 &args.seq_args, &res.seq_res);
7957 nfs_free_fhandle(res.fh);
7961 do_renew_lease(clp, now);
7965 #ifdef CONFIG_NFS_V4_1
7968 * This operation also signals the server that this client is
7969 * performing "lease moved" recovery. The server can stop asserting
7970 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7971 * this operation is identified in the SEQUENCE operation in this
7974 static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
7976 struct nfs_server *server = NFS_SERVER(inode);
7977 struct rpc_clnt *clnt = server->client;
7978 struct nfs4_fsid_present_arg args = {
7979 .fh = NFS_FH(inode),
7981 struct nfs4_fsid_present_res res = {
7983 struct rpc_message msg = {
7984 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7991 res.fh = nfs_alloc_fhandle();
7995 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7996 status = nfs4_call_sync_sequence(clnt, server, &msg,
7997 &args.seq_args, &res.seq_res);
7998 nfs_free_fhandle(res.fh);
7999 if (status == NFS4_OK &&
8000 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
8001 status = -NFS4ERR_LEASE_MOVED;
8005 #endif /* CONFIG_NFS_V4_1 */
8008 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
8009 * @inode: inode on FSID to check
8010 * @cred: credential to use for this operation
8012 * Server indicates whether the FSID is present, moved, or not
8013 * recognized. This operation is necessary to clear a LEASE_MOVED
8014 * condition for this client ID.
8016 * Returns NFS4_OK if the FSID is present on this server,
8017 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
8018 * NFS4ERR code if some error occurred on the server, or a
8019 * negative errno if a local failure occurred.
8021 int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
8023 struct nfs_server *server = NFS_SERVER(inode);
8024 struct nfs_client *clp = server->nfs_client;
8025 const struct nfs4_mig_recovery_ops *ops =
8026 clp->cl_mvops->mig_recovery_ops;
8027 struct nfs4_exception exception = {
8028 .interruptible = true,
8032 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
8033 (unsigned long long)server->fsid.major,
8034 (unsigned long long)server->fsid.minor,
8036 nfs_display_fhandle(NFS_FH(inode), __func__);
8039 status = ops->fsid_present(inode, cred);
8040 if (status != -NFS4ERR_DELAY)
8042 nfs4_handle_exception(server, status, &exception);
8043 } while (exception.retry);
8048 * If 'use_integrity' is true and the state managment nfs_client
8049 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
8050 * and the machine credential as per RFC3530bis and RFC5661 Security
8051 * Considerations sections. Otherwise, just use the user cred with the
8052 * filesystem's rpc_client.
8054 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
8057 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
8058 struct nfs_client *clp = NFS_SERVER(dir)->nfs_client;
8059 struct nfs4_secinfo_arg args = {
8060 .dir_fh = NFS_FH(dir),
8063 struct nfs4_secinfo_res res = {
8066 struct rpc_message msg = {
8067 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
8071 struct nfs4_call_sync_data data = {
8072 .seq_server = NFS_SERVER(dir),
8073 .seq_args = &args.seq_args,
8074 .seq_res = &res.seq_res,
8076 struct rpc_task_setup task_setup = {
8078 .rpc_message = &msg,
8079 .callback_ops = clp->cl_mvops->call_sync_ops,
8080 .callback_data = &data,
8081 .flags = RPC_TASK_NO_ROUND_ROBIN,
8083 const struct cred *cred = NULL;
8085 if (use_integrity) {
8086 clnt = clp->cl_rpcclient;
8087 task_setup.rpc_client = clnt;
8089 cred = nfs4_get_clid_cred(clp);
8090 msg.rpc_cred = cred;
8093 dprintk("NFS call secinfo %s\n", name->name);
8095 nfs4_state_protect(clp, NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
8096 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
8097 status = nfs4_call_sync_custom(&task_setup);
8099 dprintk("NFS reply secinfo: %d\n", status);
8105 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
8106 struct nfs4_secinfo_flavors *flavors)
8108 struct nfs4_exception exception = {
8109 .interruptible = true,
8113 err = -NFS4ERR_WRONGSEC;
8115 /* try to use integrity protection with machine cred */
8116 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
8117 err = _nfs4_proc_secinfo(dir, name, flavors, true);
8120 * if unable to use integrity protection, or SECINFO with
8121 * integrity protection returns NFS4ERR_WRONGSEC (which is
8122 * disallowed by spec, but exists in deployed servers) use
8123 * the current filesystem's rpc_client and the user cred.
8125 if (err == -NFS4ERR_WRONGSEC)
8126 err = _nfs4_proc_secinfo(dir, name, flavors, false);
8128 trace_nfs4_secinfo(dir, name, err);
8129 err = nfs4_handle_exception(NFS_SERVER(dir), err,
8131 } while (exception.retry);
8135 #ifdef CONFIG_NFS_V4_1
8137 * Check the exchange flags returned by the server for invalid flags, having
8138 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
8141 static int nfs4_check_cl_exchange_flags(u32 flags, u32 version)
8143 if (version >= 2 && (flags & ~EXCHGID4_2_FLAG_MASK_R))
8145 else if (version < 2 && (flags & ~EXCHGID4_FLAG_MASK_R))
8147 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
8148 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
8150 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
8154 return -NFS4ERR_INVAL;
8158 nfs41_same_server_scope(struct nfs41_server_scope *a,
8159 struct nfs41_server_scope *b)
8161 if (a->server_scope_sz != b->server_scope_sz)
8163 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
8167 nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
8169 struct nfs41_bind_conn_to_session_args *args = task->tk_msg.rpc_argp;
8170 struct nfs41_bind_conn_to_session_res *res = task->tk_msg.rpc_resp;
8171 struct nfs_client *clp = args->client;
8173 switch (task->tk_status) {
8174 case -NFS4ERR_BADSESSION:
8175 case -NFS4ERR_DEADSESSION:
8176 nfs4_schedule_session_recovery(clp->cl_session,
8179 if (args->dir == NFS4_CDFC4_FORE_OR_BOTH &&
8180 res->dir != NFS4_CDFS4_BOTH) {
8181 rpc_task_close_connection(task);
8182 if (args->retries++ < MAX_BIND_CONN_TO_SESSION_RETRIES)
8183 rpc_restart_call(task);
8187 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
8188 .rpc_call_done = nfs4_bind_one_conn_to_session_done,
8192 * nfs4_proc_bind_one_conn_to_session()
8194 * The 4.1 client currently uses the same TCP connection for the
8195 * fore and backchannel.
8198 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
8199 struct rpc_xprt *xprt,
8200 struct nfs_client *clp,
8201 const struct cred *cred)
8204 struct nfs41_bind_conn_to_session_args args = {
8206 .dir = NFS4_CDFC4_FORE_OR_BOTH,
8209 struct nfs41_bind_conn_to_session_res res;
8210 struct rpc_message msg = {
8212 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
8217 struct rpc_task_setup task_setup_data = {
8220 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
8221 .rpc_message = &msg,
8222 .flags = RPC_TASK_TIMEOUT,
8224 struct rpc_task *task;
8226 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
8227 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
8228 args.dir = NFS4_CDFC4_FORE;
8230 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
8231 if (xprt != rcu_access_pointer(clnt->cl_xprt))
8232 args.dir = NFS4_CDFC4_FORE;
8234 task = rpc_run_task(&task_setup_data);
8235 if (!IS_ERR(task)) {
8236 status = task->tk_status;
8239 status = PTR_ERR(task);
8240 trace_nfs4_bind_conn_to_session(clp, status);
8242 if (memcmp(res.sessionid.data,
8243 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
8244 dprintk("NFS: %s: Session ID mismatch\n", __func__);
8247 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
8248 dprintk("NFS: %s: Unexpected direction from server\n",
8252 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
8253 dprintk("NFS: %s: Server returned RDMA mode = true\n",
8262 struct rpc_bind_conn_calldata {
8263 struct nfs_client *clp;
8264 const struct cred *cred;
8268 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
8269 struct rpc_xprt *xprt,
8272 struct rpc_bind_conn_calldata *p = calldata;
8274 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
8277 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
8279 struct rpc_bind_conn_calldata data = {
8283 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
8284 nfs4_proc_bind_conn_to_session_callback, &data);
8288 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
8289 * and operations we'd like to see to enable certain features in the allow map
8291 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
8292 .how = SP4_MACH_CRED,
8293 .enforce.u.words = {
8294 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8295 1 << (OP_EXCHANGE_ID - 32) |
8296 1 << (OP_CREATE_SESSION - 32) |
8297 1 << (OP_DESTROY_SESSION - 32) |
8298 1 << (OP_DESTROY_CLIENTID - 32)
8301 [0] = 1 << (OP_CLOSE) |
8302 1 << (OP_OPEN_DOWNGRADE) |
8304 1 << (OP_DELEGRETURN) |
8306 [1] = 1 << (OP_SECINFO - 32) |
8307 1 << (OP_SECINFO_NO_NAME - 32) |
8308 1 << (OP_LAYOUTRETURN - 32) |
8309 1 << (OP_TEST_STATEID - 32) |
8310 1 << (OP_FREE_STATEID - 32) |
8311 1 << (OP_WRITE - 32)
8316 * Select the state protection mode for client `clp' given the server results
8317 * from exchange_id in `sp'.
8319 * Returns 0 on success, negative errno otherwise.
8321 static int nfs4_sp4_select_mode(struct nfs_client *clp,
8322 struct nfs41_state_protection *sp)
8324 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
8325 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8326 1 << (OP_EXCHANGE_ID - 32) |
8327 1 << (OP_CREATE_SESSION - 32) |
8328 1 << (OP_DESTROY_SESSION - 32) |
8329 1 << (OP_DESTROY_CLIENTID - 32)
8331 unsigned long flags = 0;
8335 if (sp->how == SP4_MACH_CRED) {
8336 /* Print state protect result */
8337 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
8338 for (i = 0; i <= LAST_NFS4_OP; i++) {
8339 if (test_bit(i, sp->enforce.u.longs))
8340 dfprintk(MOUNT, " enforce op %d\n", i);
8341 if (test_bit(i, sp->allow.u.longs))
8342 dfprintk(MOUNT, " allow op %d\n", i);
8345 /* make sure nothing is on enforce list that isn't supported */
8346 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
8347 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
8348 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
8355 * Minimal mode - state operations are allowed to use machine
8356 * credential. Note this already happens by default, so the
8357 * client doesn't have to do anything more than the negotiation.
8359 * NOTE: we don't care if EXCHANGE_ID is in the list -
8360 * we're already using the machine cred for exchange_id
8361 * and will never use a different cred.
8363 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
8364 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
8365 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
8366 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
8367 dfprintk(MOUNT, "sp4_mach_cred:\n");
8368 dfprintk(MOUNT, " minimal mode enabled\n");
8369 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
8371 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
8376 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
8377 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
8378 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
8379 test_bit(OP_LOCKU, sp->allow.u.longs)) {
8380 dfprintk(MOUNT, " cleanup mode enabled\n");
8381 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
8384 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
8385 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
8386 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
8389 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
8390 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
8391 dfprintk(MOUNT, " secinfo mode enabled\n");
8392 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
8395 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
8396 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
8397 dfprintk(MOUNT, " stateid mode enabled\n");
8398 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
8401 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
8402 dfprintk(MOUNT, " write mode enabled\n");
8403 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
8406 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
8407 dfprintk(MOUNT, " commit mode enabled\n");
8408 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
8412 clp->cl_sp4_flags = flags;
8416 struct nfs41_exchange_id_data {
8417 struct nfs41_exchange_id_res res;
8418 struct nfs41_exchange_id_args args;
8421 static void nfs4_exchange_id_release(void *data)
8423 struct nfs41_exchange_id_data *cdata =
8424 (struct nfs41_exchange_id_data *)data;
8426 nfs_put_client(cdata->args.client);
8427 kfree(cdata->res.impl_id);
8428 kfree(cdata->res.server_scope);
8429 kfree(cdata->res.server_owner);
8433 static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
8434 .rpc_release = nfs4_exchange_id_release,
8438 * _nfs4_proc_exchange_id()
8440 * Wrapper for EXCHANGE_ID operation.
8442 static struct rpc_task *
8443 nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
8444 u32 sp4_how, struct rpc_xprt *xprt)
8446 struct rpc_message msg = {
8447 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
8450 struct rpc_task_setup task_setup_data = {
8451 .rpc_client = clp->cl_rpcclient,
8452 .callback_ops = &nfs4_exchange_id_call_ops,
8453 .rpc_message = &msg,
8454 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
8456 struct nfs41_exchange_id_data *calldata;
8459 if (!refcount_inc_not_zero(&clp->cl_count))
8460 return ERR_PTR(-EIO);
8463 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8467 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
8469 status = nfs4_init_uniform_client_string(clp);
8473 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
8476 if (unlikely(calldata->res.server_owner == NULL))
8479 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
8481 if (unlikely(calldata->res.server_scope == NULL))
8482 goto out_server_owner;
8484 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
8485 if (unlikely(calldata->res.impl_id == NULL))
8486 goto out_server_scope;
8490 calldata->args.state_protect.how = SP4_NONE;
8494 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
8504 task_setup_data.rpc_xprt = xprt;
8505 task_setup_data.flags |= RPC_TASK_SOFTCONN;
8506 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
8507 sizeof(calldata->args.verifier.data));
8509 calldata->args.client = clp;
8510 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
8511 EXCHGID4_FLAG_BIND_PRINC_STATEID;
8512 #ifdef CONFIG_NFS_V4_1_MIGRATION
8513 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
8515 msg.rpc_argp = &calldata->args;
8516 msg.rpc_resp = &calldata->res;
8517 task_setup_data.callback_data = calldata;
8519 return rpc_run_task(&task_setup_data);
8522 kfree(calldata->res.impl_id);
8524 kfree(calldata->res.server_scope);
8526 kfree(calldata->res.server_owner);
8530 nfs_put_client(clp);
8531 return ERR_PTR(status);
8535 * _nfs4_proc_exchange_id()
8537 * Wrapper for EXCHANGE_ID operation.
8539 static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
8542 struct rpc_task *task;
8543 struct nfs41_exchange_id_args *argp;
8544 struct nfs41_exchange_id_res *resp;
8545 unsigned long now = jiffies;
8548 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
8550 return PTR_ERR(task);
8552 argp = task->tk_msg.rpc_argp;
8553 resp = task->tk_msg.rpc_resp;
8554 status = task->tk_status;
8558 status = nfs4_check_cl_exchange_flags(resp->flags,
8559 clp->cl_mvops->minor_version);
8563 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8567 do_renew_lease(clp, now);
8569 clp->cl_clientid = resp->clientid;
8570 clp->cl_exchange_flags = resp->flags;
8571 clp->cl_seqid = resp->seqid;
8572 /* Client ID is not confirmed */
8573 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8574 clear_bit(NFS4_SESSION_ESTABLISHED,
8575 &clp->cl_session->session_state);
8577 if (clp->cl_serverscope != NULL &&
8578 !nfs41_same_server_scope(clp->cl_serverscope,
8579 resp->server_scope)) {
8580 dprintk("%s: server_scope mismatch detected\n",
8582 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8585 swap(clp->cl_serverowner, resp->server_owner);
8586 swap(clp->cl_serverscope, resp->server_scope);
8587 swap(clp->cl_implid, resp->impl_id);
8589 /* Save the EXCHANGE_ID verifier session trunk tests */
8590 memcpy(clp->cl_confirm.data, argp->verifier.data,
8591 sizeof(clp->cl_confirm.data));
8593 trace_nfs4_exchange_id(clp, status);
8599 * nfs4_proc_exchange_id()
8601 * Returns zero, a negative errno, or a negative NFS4ERR status code.
8603 * Since the clientid has expired, all compounds using sessions
8604 * associated with the stale clientid will be returning
8605 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
8606 * be in some phase of session reset.
8608 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
8610 int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
8612 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8615 /* try SP4_MACH_CRED if krb5i/p */
8616 if (authflavor == RPC_AUTH_GSS_KRB5I ||
8617 authflavor == RPC_AUTH_GSS_KRB5P) {
8618 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
8624 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
8628 * nfs4_test_session_trunk
8630 * This is an add_xprt_test() test function called from
8631 * rpc_clnt_setup_test_and_add_xprt.
8633 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8634 * and is dereferrenced in nfs4_exchange_id_release
8636 * Upon success, add the new transport to the rpc_clnt
8638 * @clnt: struct rpc_clnt to get new transport
8639 * @xprt: the rpc_xprt to test
8640 * @data: call data for _nfs4_proc_exchange_id.
8642 void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
8645 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
8646 struct rpc_task *task;
8651 dprintk("--> %s try %s\n", __func__,
8652 xprt->address_strings[RPC_DISPLAY_ADDR]);
8654 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8656 /* Test connection for session trunking. Async exchange_id call */
8657 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8661 status = task->tk_status;
8663 status = nfs4_detect_session_trunking(adata->clp,
8664 task->tk_msg.rpc_resp, xprt);
8667 rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
8671 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
8673 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
8674 const struct cred *cred)
8676 struct rpc_message msg = {
8677 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8683 status = rpc_call_sync(clp->cl_rpcclient, &msg,
8684 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
8685 trace_nfs4_destroy_clientid(clp, status);
8687 dprintk("NFS: Got error %d from the server %s on "
8688 "DESTROY_CLIENTID.", status, clp->cl_hostname);
8692 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
8693 const struct cred *cred)
8698 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8699 ret = _nfs4_proc_destroy_clientid(clp, cred);
8701 case -NFS4ERR_DELAY:
8702 case -NFS4ERR_CLIENTID_BUSY:
8712 int nfs4_destroy_clientid(struct nfs_client *clp)
8714 const struct cred *cred;
8717 if (clp->cl_mvops->minor_version < 1)
8719 if (clp->cl_exchange_flags == 0)
8721 if (clp->cl_preserve_clid)
8723 cred = nfs4_get_clid_cred(clp);
8724 ret = nfs4_proc_destroy_clientid(clp, cred);
8728 case -NFS4ERR_STALE_CLIENTID:
8729 clp->cl_exchange_flags = 0;
8735 #endif /* CONFIG_NFS_V4_1 */
8737 struct nfs4_get_lease_time_data {
8738 struct nfs4_get_lease_time_args *args;
8739 struct nfs4_get_lease_time_res *res;
8740 struct nfs_client *clp;
8743 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
8746 struct nfs4_get_lease_time_data *data =
8747 (struct nfs4_get_lease_time_data *)calldata;
8749 dprintk("--> %s\n", __func__);
8750 /* just setup sequence, do not trigger session recovery
8751 since we're invoked within one */
8752 nfs4_setup_sequence(data->clp,
8753 &data->args->la_seq_args,
8754 &data->res->lr_seq_res,
8756 dprintk("<-- %s\n", __func__);
8760 * Called from nfs4_state_manager thread for session setup, so don't recover
8761 * from sequence operation or clientid errors.
8763 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8765 struct nfs4_get_lease_time_data *data =
8766 (struct nfs4_get_lease_time_data *)calldata;
8768 dprintk("--> %s\n", __func__);
8769 if (!nfs4_sequence_done(task, &data->res->lr_seq_res))
8771 switch (task->tk_status) {
8772 case -NFS4ERR_DELAY:
8773 case -NFS4ERR_GRACE:
8774 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
8775 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8776 task->tk_status = 0;
8778 case -NFS4ERR_RETRY_UNCACHED_REP:
8779 rpc_restart_call_prepare(task);
8782 dprintk("<-- %s\n", __func__);
8785 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
8786 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8787 .rpc_call_done = nfs4_get_lease_time_done,
8790 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8792 struct nfs4_get_lease_time_args args;
8793 struct nfs4_get_lease_time_res res = {
8794 .lr_fsinfo = fsinfo,
8796 struct nfs4_get_lease_time_data data = {
8801 struct rpc_message msg = {
8802 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8806 struct rpc_task_setup task_setup = {
8807 .rpc_client = clp->cl_rpcclient,
8808 .rpc_message = &msg,
8809 .callback_ops = &nfs4_get_lease_time_ops,
8810 .callback_data = &data,
8811 .flags = RPC_TASK_TIMEOUT,
8814 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
8815 return nfs4_call_sync_custom(&task_setup);
8818 #ifdef CONFIG_NFS_V4_1
8821 * Initialize the values to be used by the client in CREATE_SESSION
8822 * If nfs4_init_session set the fore channel request and response sizes,
8825 * Set the back channel max_resp_sz_cached to zero to force the client to
8826 * always set csa_cachethis to FALSE because the current implementation
8827 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8829 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8830 struct rpc_clnt *clnt)
8832 unsigned int max_rqst_sz, max_resp_sz;
8833 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
8834 unsigned int max_bc_slots = rpc_num_bc_slots(clnt);
8836 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8837 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8839 /* Fore channel attributes */
8840 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8841 args->fc_attrs.max_resp_sz = max_resp_sz;
8842 args->fc_attrs.max_ops = NFS4_MAX_OPS;
8843 args->fc_attrs.max_reqs = max_session_slots;
8845 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
8846 "max_ops=%u max_reqs=%u\n",
8848 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
8849 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
8851 /* Back channel attributes */
8852 args->bc_attrs.max_rqst_sz = max_bc_payload;
8853 args->bc_attrs.max_resp_sz = max_bc_payload;
8854 args->bc_attrs.max_resp_sz_cached = 0;
8855 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
8856 args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
8857 if (args->bc_attrs.max_reqs > max_bc_slots)
8858 args->bc_attrs.max_reqs = max_bc_slots;
8860 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8861 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8863 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8864 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8865 args->bc_attrs.max_reqs);
8868 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8869 struct nfs41_create_session_res *res)
8871 struct nfs4_channel_attrs *sent = &args->fc_attrs;
8872 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
8874 if (rcvd->max_resp_sz > sent->max_resp_sz)
8877 * Our requested max_ops is the minimum we need; we're not
8878 * prepared to break up compounds into smaller pieces than that.
8879 * So, no point even trying to continue if the server won't
8882 if (rcvd->max_ops < sent->max_ops)
8884 if (rcvd->max_reqs == 0)
8886 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8887 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
8891 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8892 struct nfs41_create_session_res *res)
8894 struct nfs4_channel_attrs *sent = &args->bc_attrs;
8895 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
8897 if (!(res->flags & SESSION4_BACK_CHAN))
8899 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8901 if (rcvd->max_resp_sz < sent->max_resp_sz)
8903 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8905 if (rcvd->max_ops > sent->max_ops)
8907 if (rcvd->max_reqs > sent->max_reqs)
8913 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
8914 struct nfs41_create_session_res *res)
8918 ret = nfs4_verify_fore_channel_attrs(args, res);
8921 return nfs4_verify_back_channel_attrs(args, res);
8924 static void nfs4_update_session(struct nfs4_session *session,
8925 struct nfs41_create_session_res *res)
8927 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
8928 /* Mark client id and session as being confirmed */
8929 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8930 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
8931 session->flags = res->flags;
8932 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
8933 if (res->flags & SESSION4_BACK_CHAN)
8934 memcpy(&session->bc_attrs, &res->bc_attrs,
8935 sizeof(session->bc_attrs));
8938 static int _nfs4_proc_create_session(struct nfs_client *clp,
8939 const struct cred *cred)
8941 struct nfs4_session *session = clp->cl_session;
8942 struct nfs41_create_session_args args = {
8944 .clientid = clp->cl_clientid,
8945 .seqid = clp->cl_seqid,
8946 .cb_program = NFS4_CALLBACK,
8948 struct nfs41_create_session_res res;
8950 struct rpc_message msg = {
8951 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8958 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
8959 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
8961 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
8962 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
8963 trace_nfs4_create_session(clp, status);
8966 case -NFS4ERR_STALE_CLIENTID:
8967 case -NFS4ERR_DELAY:
8976 /* Verify the session's negotiated channel_attrs values */
8977 status = nfs4_verify_channel_attrs(&args, &res);
8978 /* Increment the clientid slot sequence id */
8981 nfs4_update_session(session, &res);
8988 * Issues a CREATE_SESSION operation to the server.
8989 * It is the responsibility of the caller to verify the session is
8990 * expired before calling this routine.
8992 int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
8996 struct nfs4_session *session = clp->cl_session;
8998 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
9000 status = _nfs4_proc_create_session(clp, cred);
9004 /* Init or reset the session slot tables */
9005 status = nfs4_setup_session_slot_tables(session);
9006 dprintk("slot table setup returned %d\n", status);
9010 ptr = (unsigned *)&session->sess_id.data[0];
9011 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
9012 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
9014 dprintk("<-- %s\n", __func__);
9019 * Issue the over-the-wire RPC DESTROY_SESSION.
9020 * The caller must serialize access to this routine.
9022 int nfs4_proc_destroy_session(struct nfs4_session *session,
9023 const struct cred *cred)
9025 struct rpc_message msg = {
9026 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
9027 .rpc_argp = session,
9032 dprintk("--> nfs4_proc_destroy_session\n");
9034 /* session is still being setup */
9035 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
9038 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
9039 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
9040 trace_nfs4_destroy_session(session->clp, status);
9043 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
9044 "Session has been destroyed regardless...\n", status);
9046 dprintk("<-- nfs4_proc_destroy_session\n");
9051 * Renew the cl_session lease.
9053 struct nfs4_sequence_data {
9054 struct nfs_client *clp;
9055 struct nfs4_sequence_args args;
9056 struct nfs4_sequence_res res;
9059 static void nfs41_sequence_release(void *data)
9061 struct nfs4_sequence_data *calldata = data;
9062 struct nfs_client *clp = calldata->clp;
9064 if (refcount_read(&clp->cl_count) > 1)
9065 nfs4_schedule_state_renewal(clp);
9066 nfs_put_client(clp);
9070 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9072 switch(task->tk_status) {
9073 case -NFS4ERR_DELAY:
9074 rpc_delay(task, NFS4_POLL_RETRY_MAX);
9077 nfs4_schedule_lease_recovery(clp);
9082 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
9084 struct nfs4_sequence_data *calldata = data;
9085 struct nfs_client *clp = calldata->clp;
9087 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
9090 trace_nfs4_sequence(clp, task->tk_status);
9091 if (task->tk_status < 0) {
9092 dprintk("%s ERROR %d\n", __func__, task->tk_status);
9093 if (refcount_read(&clp->cl_count) == 1)
9096 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
9097 rpc_restart_call_prepare(task);
9101 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
9103 dprintk("<-- %s\n", __func__);
9106 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
9108 struct nfs4_sequence_data *calldata = data;
9109 struct nfs_client *clp = calldata->clp;
9110 struct nfs4_sequence_args *args;
9111 struct nfs4_sequence_res *res;
9113 args = task->tk_msg.rpc_argp;
9114 res = task->tk_msg.rpc_resp;
9116 nfs4_setup_sequence(clp, args, res, task);
9119 static const struct rpc_call_ops nfs41_sequence_ops = {
9120 .rpc_call_done = nfs41_sequence_call_done,
9121 .rpc_call_prepare = nfs41_sequence_prepare,
9122 .rpc_release = nfs41_sequence_release,
9125 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
9126 const struct cred *cred,
9127 struct nfs4_slot *slot,
9130 struct nfs4_sequence_data *calldata;
9131 struct rpc_message msg = {
9132 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
9135 struct rpc_task_setup task_setup_data = {
9136 .rpc_client = clp->cl_rpcclient,
9137 .rpc_message = &msg,
9138 .callback_ops = &nfs41_sequence_ops,
9139 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
9141 struct rpc_task *ret;
9143 ret = ERR_PTR(-EIO);
9144 if (!refcount_inc_not_zero(&clp->cl_count))
9147 ret = ERR_PTR(-ENOMEM);
9148 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
9149 if (calldata == NULL)
9151 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
9152 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
9153 msg.rpc_argp = &calldata->args;
9154 msg.rpc_resp = &calldata->res;
9155 calldata->clp = clp;
9156 task_setup_data.callback_data = calldata;
9158 ret = rpc_run_task(&task_setup_data);
9163 nfs_put_client(clp);
9165 nfs41_release_slot(slot);
9169 static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
9171 struct rpc_task *task;
9174 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
9176 task = _nfs41_proc_sequence(clp, cred, NULL, false);
9178 ret = PTR_ERR(task);
9180 rpc_put_task_async(task);
9181 dprintk("<-- %s status=%d\n", __func__, ret);
9185 static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
9187 struct rpc_task *task;
9190 task = _nfs41_proc_sequence(clp, cred, NULL, true);
9192 ret = PTR_ERR(task);
9195 ret = rpc_wait_for_completion_task(task);
9197 ret = task->tk_status;
9200 dprintk("<-- %s status=%d\n", __func__, ret);
9204 struct nfs4_reclaim_complete_data {
9205 struct nfs_client *clp;
9206 struct nfs41_reclaim_complete_args arg;
9207 struct nfs41_reclaim_complete_res res;
9210 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
9212 struct nfs4_reclaim_complete_data *calldata = data;
9214 nfs4_setup_sequence(calldata->clp,
9215 &calldata->arg.seq_args,
9216 &calldata->res.seq_res,
9220 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9222 switch(task->tk_status) {
9224 wake_up_all(&clp->cl_lock_waitq);
9226 case -NFS4ERR_COMPLETE_ALREADY:
9227 case -NFS4ERR_WRONG_CRED: /* What to do here? */
9229 case -NFS4ERR_DELAY:
9230 rpc_delay(task, NFS4_POLL_RETRY_MAX);
9232 case -NFS4ERR_RETRY_UNCACHED_REP:
9234 case -NFS4ERR_BADSESSION:
9235 case -NFS4ERR_DEADSESSION:
9236 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9239 nfs4_schedule_lease_recovery(clp);
9244 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
9246 struct nfs4_reclaim_complete_data *calldata = data;
9247 struct nfs_client *clp = calldata->clp;
9248 struct nfs4_sequence_res *res = &calldata->res.seq_res;
9250 dprintk("--> %s\n", __func__);
9251 if (!nfs41_sequence_done(task, res))
9254 trace_nfs4_reclaim_complete(clp, task->tk_status);
9255 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
9256 rpc_restart_call_prepare(task);
9259 dprintk("<-- %s\n", __func__);
9262 static void nfs4_free_reclaim_complete_data(void *data)
9264 struct nfs4_reclaim_complete_data *calldata = data;
9269 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
9270 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
9271 .rpc_call_done = nfs4_reclaim_complete_done,
9272 .rpc_release = nfs4_free_reclaim_complete_data,
9276 * Issue a global reclaim complete.
9278 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
9279 const struct cred *cred)
9281 struct nfs4_reclaim_complete_data *calldata;
9282 struct rpc_message msg = {
9283 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
9286 struct rpc_task_setup task_setup_data = {
9287 .rpc_client = clp->cl_rpcclient,
9288 .rpc_message = &msg,
9289 .callback_ops = &nfs4_reclaim_complete_call_ops,
9290 .flags = RPC_TASK_NO_ROUND_ROBIN,
9292 int status = -ENOMEM;
9294 dprintk("--> %s\n", __func__);
9295 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
9296 if (calldata == NULL)
9298 calldata->clp = clp;
9299 calldata->arg.one_fs = 0;
9301 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
9302 msg.rpc_argp = &calldata->arg;
9303 msg.rpc_resp = &calldata->res;
9304 task_setup_data.callback_data = calldata;
9305 status = nfs4_call_sync_custom(&task_setup_data);
9307 dprintk("<-- %s status=%d\n", __func__, status);
9312 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
9314 struct nfs4_layoutget *lgp = calldata;
9315 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
9317 dprintk("--> %s\n", __func__);
9318 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
9319 &lgp->res.seq_res, task);
9320 dprintk("<-- %s\n", __func__);
9323 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
9325 struct nfs4_layoutget *lgp = calldata;
9327 dprintk("--> %s\n", __func__);
9328 nfs41_sequence_process(task, &lgp->res.seq_res);
9329 dprintk("<-- %s\n", __func__);
9333 nfs4_layoutget_handle_exception(struct rpc_task *task,
9334 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
9336 struct inode *inode = lgp->args.inode;
9337 struct nfs_server *server = NFS_SERVER(inode);
9338 struct pnfs_layout_hdr *lo;
9339 int nfs4err = task->tk_status;
9340 int err, status = 0;
9343 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
9345 nfs4_sequence_free_slot(&lgp->res.seq_res);
9352 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
9353 * on the file. set tk_status to -ENODATA to tell upper layer to
9356 case -NFS4ERR_LAYOUTUNAVAILABLE:
9360 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
9361 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
9363 case -NFS4ERR_BADLAYOUT:
9364 status = -EOVERFLOW;
9367 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
9368 * (or clients) writing to the same RAID stripe except when
9369 * the minlength argument is 0 (see RFC5661 section 18.43.3).
9371 * Treat it like we would RECALLCONFLICT -- we retry for a little
9372 * while, and then eventually give up.
9374 case -NFS4ERR_LAYOUTTRYLATER:
9375 if (lgp->args.minlength == 0) {
9376 status = -EOVERFLOW;
9381 case -NFS4ERR_RECALLCONFLICT:
9382 status = -ERECALLCONFLICT;
9384 case -NFS4ERR_DELEG_REVOKED:
9385 case -NFS4ERR_ADMIN_REVOKED:
9386 case -NFS4ERR_EXPIRED:
9387 case -NFS4ERR_BAD_STATEID:
9388 exception->timeout = 0;
9389 spin_lock(&inode->i_lock);
9390 lo = NFS_I(inode)->layout;
9391 /* If the open stateid was bad, then recover it. */
9392 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
9393 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
9394 spin_unlock(&inode->i_lock);
9395 exception->state = lgp->args.ctx->state;
9396 exception->stateid = &lgp->args.stateid;
9401 * Mark the bad layout state as invalid, then retry
9403 pnfs_mark_layout_stateid_invalid(lo, &head);
9404 spin_unlock(&inode->i_lock);
9405 nfs_commit_inode(inode, 0);
9406 pnfs_free_lseg_list(&head);
9411 err = nfs4_handle_exception(server, nfs4err, exception);
9413 if (exception->retry)
9419 dprintk("<-- %s\n", __func__);
9423 size_t max_response_pages(struct nfs_server *server)
9425 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
9426 return nfs_page_array_len(0, max_resp_sz);
9429 static void nfs4_layoutget_release(void *calldata)
9431 struct nfs4_layoutget *lgp = calldata;
9433 dprintk("--> %s\n", __func__);
9434 nfs4_sequence_free_slot(&lgp->res.seq_res);
9435 pnfs_layoutget_free(lgp);
9436 dprintk("<-- %s\n", __func__);
9439 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
9440 .rpc_call_prepare = nfs4_layoutget_prepare,
9441 .rpc_call_done = nfs4_layoutget_done,
9442 .rpc_release = nfs4_layoutget_release,
9445 struct pnfs_layout_segment *
9446 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
9448 struct inode *inode = lgp->args.inode;
9449 struct nfs_server *server = NFS_SERVER(inode);
9450 struct rpc_task *task;
9451 struct rpc_message msg = {
9452 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
9453 .rpc_argp = &lgp->args,
9454 .rpc_resp = &lgp->res,
9455 .rpc_cred = lgp->cred,
9457 struct rpc_task_setup task_setup_data = {
9458 .rpc_client = server->client,
9459 .rpc_message = &msg,
9460 .callback_ops = &nfs4_layoutget_call_ops,
9461 .callback_data = lgp,
9462 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
9464 struct pnfs_layout_segment *lseg = NULL;
9465 struct nfs4_exception exception = {
9467 .timeout = *timeout,
9471 dprintk("--> %s\n", __func__);
9473 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
9474 pnfs_get_layout_hdr(NFS_I(inode)->layout);
9476 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
9478 task = rpc_run_task(&task_setup_data);
9480 status = rpc_wait_for_completion_task(task);
9484 if (task->tk_status < 0) {
9485 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
9486 *timeout = exception.timeout;
9487 } else if (lgp->res.layoutp->len == 0) {
9489 *timeout = nfs4_update_delay(&exception.timeout);
9491 lseg = pnfs_layout_process(lgp);
9493 trace_nfs4_layoutget(lgp->args.ctx,
9500 dprintk("<-- %s status=%d\n", __func__, status);
9502 return ERR_PTR(status);
9507 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
9509 struct nfs4_layoutreturn *lrp = calldata;
9511 dprintk("--> %s\n", __func__);
9512 nfs4_setup_sequence(lrp->clp,
9513 &lrp->args.seq_args,
9516 if (!pnfs_layout_is_valid(lrp->args.layout))
9520 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
9522 struct nfs4_layoutreturn *lrp = calldata;
9523 struct nfs_server *server;
9525 dprintk("--> %s\n", __func__);
9527 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
9531 * Was there an RPC level error? Assume the call succeeded,
9532 * and that we need to release the layout
9534 if (task->tk_rpc_status != 0 && RPC_WAS_SENT(task)) {
9535 lrp->res.lrs_present = 0;
9539 server = NFS_SERVER(lrp->args.inode);
9540 switch (task->tk_status) {
9541 case -NFS4ERR_OLD_STATEID:
9542 if (nfs4_layout_refresh_old_stateid(&lrp->args.stateid,
9548 task->tk_status = 0;
9552 case -NFS4ERR_DELAY:
9553 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
9557 dprintk("<-- %s\n", __func__);
9560 task->tk_status = 0;
9561 nfs4_sequence_free_slot(&lrp->res.seq_res);
9562 rpc_restart_call_prepare(task);
9565 static void nfs4_layoutreturn_release(void *calldata)
9567 struct nfs4_layoutreturn *lrp = calldata;
9568 struct pnfs_layout_hdr *lo = lrp->args.layout;
9570 dprintk("--> %s\n", __func__);
9571 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
9572 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
9573 nfs4_sequence_free_slot(&lrp->res.seq_res);
9574 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9575 lrp->ld_private.ops->free(&lrp->ld_private);
9576 pnfs_put_layout_hdr(lrp->args.layout);
9577 nfs_iput_and_deactive(lrp->inode);
9578 put_cred(lrp->cred);
9580 dprintk("<-- %s\n", __func__);
9583 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9584 .rpc_call_prepare = nfs4_layoutreturn_prepare,
9585 .rpc_call_done = nfs4_layoutreturn_done,
9586 .rpc_release = nfs4_layoutreturn_release,
9589 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
9591 struct rpc_task *task;
9592 struct rpc_message msg = {
9593 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9594 .rpc_argp = &lrp->args,
9595 .rpc_resp = &lrp->res,
9596 .rpc_cred = lrp->cred,
9598 struct rpc_task_setup task_setup_data = {
9599 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
9600 .rpc_message = &msg,
9601 .callback_ops = &nfs4_layoutreturn_call_ops,
9602 .callback_data = lrp,
9606 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9607 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9608 &task_setup_data.rpc_client, &msg);
9610 dprintk("--> %s\n", __func__);
9612 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
9614 nfs4_layoutreturn_release(lrp);
9617 task_setup_data.flags |= RPC_TASK_ASYNC;
9619 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1, 0);
9620 task = rpc_run_task(&task_setup_data);
9622 return PTR_ERR(task);
9624 status = task->tk_status;
9625 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
9626 dprintk("<-- %s status=%d\n", __func__, status);
9632 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
9633 struct pnfs_device *pdev,
9634 const struct cred *cred)
9636 struct nfs4_getdeviceinfo_args args = {
9638 .notify_types = NOTIFY_DEVICEID4_CHANGE |
9639 NOTIFY_DEVICEID4_DELETE,
9641 struct nfs4_getdeviceinfo_res res = {
9644 struct rpc_message msg = {
9645 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9652 dprintk("--> %s\n", __func__);
9653 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
9654 if (res.notification & ~args.notify_types)
9655 dprintk("%s: unsupported notification\n", __func__);
9656 if (res.notification != args.notify_types)
9659 trace_nfs4_getdeviceinfo(server, &pdev->dev_id, status);
9661 dprintk("<-- %s status=%d\n", __func__, status);
9666 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9667 struct pnfs_device *pdev,
9668 const struct cred *cred)
9670 struct nfs4_exception exception = { };
9674 err = nfs4_handle_exception(server,
9675 _nfs4_proc_getdeviceinfo(server, pdev, cred),
9677 } while (exception.retry);
9680 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9682 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9684 struct nfs4_layoutcommit_data *data = calldata;
9685 struct nfs_server *server = NFS_SERVER(data->args.inode);
9687 nfs4_setup_sequence(server->nfs_client,
9688 &data->args.seq_args,
9694 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9696 struct nfs4_layoutcommit_data *data = calldata;
9697 struct nfs_server *server = NFS_SERVER(data->args.inode);
9699 if (!nfs41_sequence_done(task, &data->res.seq_res))
9702 switch (task->tk_status) { /* Just ignore these failures */
9703 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9704 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9705 case -NFS4ERR_BADLAYOUT: /* no layout */
9706 case -NFS4ERR_GRACE: /* loca_recalim always false */
9707 task->tk_status = 0;
9712 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
9713 rpc_restart_call_prepare(task);
9719 static void nfs4_layoutcommit_release(void *calldata)
9721 struct nfs4_layoutcommit_data *data = calldata;
9723 pnfs_cleanup_layoutcommit(data);
9724 nfs_post_op_update_inode_force_wcc(data->args.inode,
9726 put_cred(data->cred);
9727 nfs_iput_and_deactive(data->inode);
9731 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9732 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9733 .rpc_call_done = nfs4_layoutcommit_done,
9734 .rpc_release = nfs4_layoutcommit_release,
9738 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
9740 struct rpc_message msg = {
9741 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9742 .rpc_argp = &data->args,
9743 .rpc_resp = &data->res,
9744 .rpc_cred = data->cred,
9746 struct rpc_task_setup task_setup_data = {
9747 .task = &data->task,
9748 .rpc_client = NFS_CLIENT(data->args.inode),
9749 .rpc_message = &msg,
9750 .callback_ops = &nfs4_layoutcommit_ops,
9751 .callback_data = data,
9753 struct rpc_task *task;
9756 dprintk("NFS: initiating layoutcommit call. sync %d "
9757 "lbw: %llu inode %lu\n", sync,
9758 data->args.lastbytewritten,
9759 data->args.inode->i_ino);
9762 data->inode = nfs_igrab_and_active(data->args.inode);
9763 if (data->inode == NULL) {
9764 nfs4_layoutcommit_release(data);
9767 task_setup_data.flags = RPC_TASK_ASYNC;
9769 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
9770 task = rpc_run_task(&task_setup_data);
9772 return PTR_ERR(task);
9774 status = task->tk_status;
9775 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
9776 dprintk("%s: status %d\n", __func__, status);
9782 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9783 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9786 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9787 struct nfs_fsinfo *info,
9788 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
9790 struct nfs41_secinfo_no_name_args args = {
9791 .style = SECINFO_STYLE_CURRENT_FH,
9793 struct nfs4_secinfo_res res = {
9796 struct rpc_message msg = {
9797 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9801 struct nfs4_call_sync_data data = {
9802 .seq_server = server,
9803 .seq_args = &args.seq_args,
9804 .seq_res = &res.seq_res,
9806 struct rpc_task_setup task_setup = {
9807 .rpc_client = server->client,
9808 .rpc_message = &msg,
9809 .callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
9810 .callback_data = &data,
9811 .flags = RPC_TASK_NO_ROUND_ROBIN,
9813 const struct cred *cred = NULL;
9816 if (use_integrity) {
9817 task_setup.rpc_client = server->nfs_client->cl_rpcclient;
9819 cred = nfs4_get_clid_cred(server->nfs_client);
9820 msg.rpc_cred = cred;
9823 dprintk("--> %s\n", __func__);
9824 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
9825 status = nfs4_call_sync_custom(&task_setup);
9826 dprintk("<-- %s status=%d\n", __func__, status);
9834 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9835 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9837 struct nfs4_exception exception = {
9838 .interruptible = true,
9842 /* first try using integrity protection */
9843 err = -NFS4ERR_WRONGSEC;
9845 /* try to use integrity protection with machine cred */
9846 if (_nfs4_is_integrity_protected(server->nfs_client))
9847 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9851 * if unable to use integrity protection, or SECINFO with
9852 * integrity protection returns NFS4ERR_WRONGSEC (which is
9853 * disallowed by spec, but exists in deployed servers) use
9854 * the current filesystem's rpc_client and the user cred.
9856 if (err == -NFS4ERR_WRONGSEC)
9857 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9862 case -NFS4ERR_WRONGSEC:
9866 err = nfs4_handle_exception(server, err, &exception);
9868 } while (exception.retry);
9874 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9875 struct nfs_fsinfo *info)
9879 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
9880 struct nfs4_secinfo_flavors *flavors;
9881 struct nfs4_secinfo4 *secinfo;
9884 page = alloc_page(GFP_KERNEL);
9890 flavors = page_address(page);
9891 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9894 * Fall back on "guess and check" method if
9895 * the server doesn't support SECINFO_NO_NAME
9897 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
9898 err = nfs4_find_root_sec(server, fhandle, info);
9904 for (i = 0; i < flavors->num_flavors; i++) {
9905 secinfo = &flavors->flavors[i];
9907 switch (secinfo->flavor) {
9911 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9912 &secinfo->flavor_info);
9915 flavor = RPC_AUTH_MAXFLAVOR;
9919 if (!nfs_auth_info_match(&server->auth_info, flavor))
9920 flavor = RPC_AUTH_MAXFLAVOR;
9922 if (flavor != RPC_AUTH_MAXFLAVOR) {
9923 err = nfs4_lookup_root_sec(server, fhandle,
9930 if (flavor == RPC_AUTH_MAXFLAVOR)
9941 static int _nfs41_test_stateid(struct nfs_server *server,
9942 nfs4_stateid *stateid,
9943 const struct cred *cred)
9946 struct nfs41_test_stateid_args args = {
9949 struct nfs41_test_stateid_res res;
9950 struct rpc_message msg = {
9951 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9956 struct rpc_clnt *rpc_client = server->client;
9958 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9961 dprintk("NFS call test_stateid %p\n", stateid);
9962 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
9963 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
9964 &args.seq_args, &res.seq_res);
9965 if (status != NFS_OK) {
9966 dprintk("NFS reply test_stateid: failed, %d\n", status);
9969 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
9973 static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9974 int err, struct nfs4_exception *exception)
9976 exception->retry = 0;
9978 case -NFS4ERR_DELAY:
9979 case -NFS4ERR_RETRY_UNCACHED_REP:
9980 nfs4_handle_exception(server, err, exception);
9982 case -NFS4ERR_BADSESSION:
9983 case -NFS4ERR_BADSLOT:
9984 case -NFS4ERR_BAD_HIGH_SLOT:
9985 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9986 case -NFS4ERR_DEADSESSION:
9987 nfs4_do_handle_exception(server, err, exception);
9992 * nfs41_test_stateid - perform a TEST_STATEID operation
9994 * @server: server / transport on which to perform the operation
9995 * @stateid: state ID to test
9998 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9999 * Otherwise a negative NFS4ERR value is returned if the operation
10000 * failed or the state ID is not currently valid.
10002 static int nfs41_test_stateid(struct nfs_server *server,
10003 nfs4_stateid *stateid,
10004 const struct cred *cred)
10006 struct nfs4_exception exception = {
10007 .interruptible = true,
10011 err = _nfs41_test_stateid(server, stateid, cred);
10012 nfs4_handle_delay_or_session_error(server, err, &exception);
10013 } while (exception.retry);
10017 struct nfs_free_stateid_data {
10018 struct nfs_server *server;
10019 struct nfs41_free_stateid_args args;
10020 struct nfs41_free_stateid_res res;
10023 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
10025 struct nfs_free_stateid_data *data = calldata;
10026 nfs4_setup_sequence(data->server->nfs_client,
10027 &data->args.seq_args,
10028 &data->res.seq_res,
10032 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
10034 struct nfs_free_stateid_data *data = calldata;
10036 nfs41_sequence_done(task, &data->res.seq_res);
10038 switch (task->tk_status) {
10039 case -NFS4ERR_DELAY:
10040 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
10041 rpc_restart_call_prepare(task);
10045 static void nfs41_free_stateid_release(void *calldata)
10050 static const struct rpc_call_ops nfs41_free_stateid_ops = {
10051 .rpc_call_prepare = nfs41_free_stateid_prepare,
10052 .rpc_call_done = nfs41_free_stateid_done,
10053 .rpc_release = nfs41_free_stateid_release,
10057 * nfs41_free_stateid - perform a FREE_STATEID operation
10059 * @server: server / transport on which to perform the operation
10060 * @stateid: state ID to release
10061 * @cred: credential
10062 * @privileged: set to true if this call needs to be privileged
10064 * Note: this function is always asynchronous.
10066 static int nfs41_free_stateid(struct nfs_server *server,
10067 const nfs4_stateid *stateid,
10068 const struct cred *cred,
10071 struct rpc_message msg = {
10072 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
10075 struct rpc_task_setup task_setup = {
10076 .rpc_client = server->client,
10077 .rpc_message = &msg,
10078 .callback_ops = &nfs41_free_stateid_ops,
10079 .flags = RPC_TASK_ASYNC,
10081 struct nfs_free_stateid_data *data;
10082 struct rpc_task *task;
10084 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
10085 &task_setup.rpc_client, &msg);
10087 dprintk("NFS call free_stateid %p\n", stateid);
10088 data = kmalloc(sizeof(*data), GFP_NOFS);
10091 data->server = server;
10092 nfs4_stateid_copy(&data->args.stateid, stateid);
10094 task_setup.callback_data = data;
10096 msg.rpc_argp = &data->args;
10097 msg.rpc_resp = &data->res;
10098 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
10099 task = rpc_run_task(&task_setup);
10101 return PTR_ERR(task);
10102 rpc_put_task(task);
10107 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
10109 const struct cred *cred = lsp->ls_state->owner->so_cred;
10111 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
10112 nfs4_free_lock_state(server, lsp);
10115 static bool nfs41_match_stateid(const nfs4_stateid *s1,
10116 const nfs4_stateid *s2)
10118 if (s1->type != s2->type)
10121 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
10124 if (s1->seqid == s2->seqid)
10127 return s1->seqid == 0 || s2->seqid == 0;
10130 #endif /* CONFIG_NFS_V4_1 */
10132 static bool nfs4_match_stateid(const nfs4_stateid *s1,
10133 const nfs4_stateid *s2)
10135 return nfs4_stateid_match(s1, s2);
10139 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
10140 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
10141 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
10142 .recover_open = nfs4_open_reclaim,
10143 .recover_lock = nfs4_lock_reclaim,
10144 .establish_clid = nfs4_init_clientid,
10145 .detect_trunking = nfs40_discover_server_trunking,
10148 #if defined(CONFIG_NFS_V4_1)
10149 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
10150 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
10151 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
10152 .recover_open = nfs4_open_reclaim,
10153 .recover_lock = nfs4_lock_reclaim,
10154 .establish_clid = nfs41_init_clientid,
10155 .reclaim_complete = nfs41_proc_reclaim_complete,
10156 .detect_trunking = nfs41_discover_server_trunking,
10158 #endif /* CONFIG_NFS_V4_1 */
10160 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
10161 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
10162 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
10163 .recover_open = nfs40_open_expired,
10164 .recover_lock = nfs4_lock_expired,
10165 .establish_clid = nfs4_init_clientid,
10168 #if defined(CONFIG_NFS_V4_1)
10169 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
10170 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
10171 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
10172 .recover_open = nfs41_open_expired,
10173 .recover_lock = nfs41_lock_expired,
10174 .establish_clid = nfs41_init_clientid,
10176 #endif /* CONFIG_NFS_V4_1 */
10178 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
10179 .sched_state_renewal = nfs4_proc_async_renew,
10180 .get_state_renewal_cred = nfs4_get_renew_cred,
10181 .renew_lease = nfs4_proc_renew,
10184 #if defined(CONFIG_NFS_V4_1)
10185 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
10186 .sched_state_renewal = nfs41_proc_async_sequence,
10187 .get_state_renewal_cred = nfs4_get_machine_cred,
10188 .renew_lease = nfs4_proc_sequence,
10192 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
10193 .get_locations = _nfs40_proc_get_locations,
10194 .fsid_present = _nfs40_proc_fsid_present,
10197 #if defined(CONFIG_NFS_V4_1)
10198 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
10199 .get_locations = _nfs41_proc_get_locations,
10200 .fsid_present = _nfs41_proc_fsid_present,
10202 #endif /* CONFIG_NFS_V4_1 */
10204 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
10205 .minor_version = 0,
10206 .init_caps = NFS_CAP_READDIRPLUS
10207 | NFS_CAP_ATOMIC_OPEN
10208 | NFS_CAP_POSIX_LOCK,
10209 .init_client = nfs40_init_client,
10210 .shutdown_client = nfs40_shutdown_client,
10211 .match_stateid = nfs4_match_stateid,
10212 .find_root_sec = nfs4_find_root_sec,
10213 .free_lock_state = nfs4_release_lockowner,
10214 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
10215 .alloc_seqid = nfs_alloc_seqid,
10216 .call_sync_ops = &nfs40_call_sync_ops,
10217 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
10218 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
10219 .state_renewal_ops = &nfs40_state_renewal_ops,
10220 .mig_recovery_ops = &nfs40_mig_recovery_ops,
10223 #if defined(CONFIG_NFS_V4_1)
10224 static struct nfs_seqid *
10225 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
10230 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
10231 .minor_version = 1,
10232 .init_caps = NFS_CAP_READDIRPLUS
10233 | NFS_CAP_ATOMIC_OPEN
10234 | NFS_CAP_POSIX_LOCK
10235 | NFS_CAP_STATEID_NFSV41
10236 | NFS_CAP_ATOMIC_OPEN_V1
10238 .init_client = nfs41_init_client,
10239 .shutdown_client = nfs41_shutdown_client,
10240 .match_stateid = nfs41_match_stateid,
10241 .find_root_sec = nfs41_find_root_sec,
10242 .free_lock_state = nfs41_free_lock_state,
10243 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
10244 .alloc_seqid = nfs_alloc_no_seqid,
10245 .session_trunk = nfs4_test_session_trunk,
10246 .call_sync_ops = &nfs41_call_sync_ops,
10247 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10248 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10249 .state_renewal_ops = &nfs41_state_renewal_ops,
10250 .mig_recovery_ops = &nfs41_mig_recovery_ops,
10254 #if defined(CONFIG_NFS_V4_2)
10255 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
10256 .minor_version = 2,
10257 .init_caps = NFS_CAP_READDIRPLUS
10258 | NFS_CAP_ATOMIC_OPEN
10259 | NFS_CAP_POSIX_LOCK
10260 | NFS_CAP_STATEID_NFSV41
10261 | NFS_CAP_ATOMIC_OPEN_V1
10265 | NFS_CAP_OFFLOAD_CANCEL
10266 | NFS_CAP_COPY_NOTIFY
10267 | NFS_CAP_DEALLOCATE
10269 | NFS_CAP_LAYOUTSTATS
10271 | NFS_CAP_LAYOUTERROR
10272 | NFS_CAP_READ_PLUS,
10273 .init_client = nfs41_init_client,
10274 .shutdown_client = nfs41_shutdown_client,
10275 .match_stateid = nfs41_match_stateid,
10276 .find_root_sec = nfs41_find_root_sec,
10277 .free_lock_state = nfs41_free_lock_state,
10278 .call_sync_ops = &nfs41_call_sync_ops,
10279 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
10280 .alloc_seqid = nfs_alloc_no_seqid,
10281 .session_trunk = nfs4_test_session_trunk,
10282 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10283 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10284 .state_renewal_ops = &nfs41_state_renewal_ops,
10285 .mig_recovery_ops = &nfs41_mig_recovery_ops,
10289 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
10290 [0] = &nfs_v4_0_minor_ops,
10291 #if defined(CONFIG_NFS_V4_1)
10292 [1] = &nfs_v4_1_minor_ops,
10294 #if defined(CONFIG_NFS_V4_2)
10295 [2] = &nfs_v4_2_minor_ops,
10299 static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
10301 ssize_t error, error2, error3;
10303 error = generic_listxattr(dentry, list, size);
10311 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
10320 error3 = nfs4_listxattr_nfs4_user(d_inode(dentry), list, size);
10324 return error + error2 + error3;
10327 static const struct inode_operations nfs4_dir_inode_operations = {
10328 .create = nfs_create,
10329 .lookup = nfs_lookup,
10330 .atomic_open = nfs_atomic_open,
10332 .unlink = nfs_unlink,
10333 .symlink = nfs_symlink,
10334 .mkdir = nfs_mkdir,
10335 .rmdir = nfs_rmdir,
10336 .mknod = nfs_mknod,
10337 .rename = nfs_rename,
10338 .permission = nfs_permission,
10339 .getattr = nfs_getattr,
10340 .setattr = nfs_setattr,
10341 .listxattr = nfs4_listxattr,
10344 static const struct inode_operations nfs4_file_inode_operations = {
10345 .permission = nfs_permission,
10346 .getattr = nfs_getattr,
10347 .setattr = nfs_setattr,
10348 .listxattr = nfs4_listxattr,
10351 const struct nfs_rpc_ops nfs_v4_clientops = {
10352 .version = 4, /* protocol version */
10353 .dentry_ops = &nfs4_dentry_operations,
10354 .dir_inode_ops = &nfs4_dir_inode_operations,
10355 .file_inode_ops = &nfs4_file_inode_operations,
10356 .file_ops = &nfs4_file_operations,
10357 .getroot = nfs4_proc_get_root,
10358 .submount = nfs4_submount,
10359 .try_get_tree = nfs4_try_get_tree,
10360 .getattr = nfs4_proc_getattr,
10361 .setattr = nfs4_proc_setattr,
10362 .lookup = nfs4_proc_lookup,
10363 .lookupp = nfs4_proc_lookupp,
10364 .access = nfs4_proc_access,
10365 .readlink = nfs4_proc_readlink,
10366 .create = nfs4_proc_create,
10367 .remove = nfs4_proc_remove,
10368 .unlink_setup = nfs4_proc_unlink_setup,
10369 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
10370 .unlink_done = nfs4_proc_unlink_done,
10371 .rename_setup = nfs4_proc_rename_setup,
10372 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
10373 .rename_done = nfs4_proc_rename_done,
10374 .link = nfs4_proc_link,
10375 .symlink = nfs4_proc_symlink,
10376 .mkdir = nfs4_proc_mkdir,
10377 .rmdir = nfs4_proc_rmdir,
10378 .readdir = nfs4_proc_readdir,
10379 .mknod = nfs4_proc_mknod,
10380 .statfs = nfs4_proc_statfs,
10381 .fsinfo = nfs4_proc_fsinfo,
10382 .pathconf = nfs4_proc_pathconf,
10383 .set_capabilities = nfs4_server_capabilities,
10384 .decode_dirent = nfs4_decode_dirent,
10385 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
10386 .read_setup = nfs4_proc_read_setup,
10387 .read_done = nfs4_read_done,
10388 .write_setup = nfs4_proc_write_setup,
10389 .write_done = nfs4_write_done,
10390 .commit_setup = nfs4_proc_commit_setup,
10391 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
10392 .commit_done = nfs4_commit_done,
10393 .lock = nfs4_proc_lock,
10394 .clear_acl_cache = nfs4_zap_acl_attr,
10395 .close_context = nfs4_close_context,
10396 .open_context = nfs4_atomic_open,
10397 .have_delegation = nfs4_have_delegation,
10398 .alloc_client = nfs4_alloc_client,
10399 .init_client = nfs4_init_client,
10400 .free_client = nfs4_free_client,
10401 .create_server = nfs4_create_server,
10402 .clone_server = nfs_clone_server,
10405 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
10406 .name = XATTR_NAME_NFSV4_ACL,
10407 .list = nfs4_xattr_list_nfs4_acl,
10408 .get = nfs4_xattr_get_nfs4_acl,
10409 .set = nfs4_xattr_set_nfs4_acl,
10412 #ifdef CONFIG_NFS_V4_2
10413 static const struct xattr_handler nfs4_xattr_nfs4_user_handler = {
10414 .prefix = XATTR_USER_PREFIX,
10415 .get = nfs4_xattr_get_nfs4_user,
10416 .set = nfs4_xattr_set_nfs4_user,
10420 const struct xattr_handler *nfs4_xattr_handlers[] = {
10421 &nfs4_xattr_nfs4_acl_handler,
10422 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
10423 &nfs4_xattr_nfs4_label_handler,
10425 #ifdef CONFIG_NFS_V4_2
10426 &nfs4_xattr_nfs4_user_handler,
10433 * c-basic-offset: 8