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"
66 #include "nfs4idmap.h"
67 #include "nfs4session.h"
71 #include "nfs4trace.h"
73 #define NFSDBG_FACILITY NFSDBG_PROC
75 #define NFS4_BITMASK_SZ 3
77 #define NFS4_POLL_RETRY_MIN (HZ/10)
78 #define NFS4_POLL_RETRY_MAX (15*HZ)
80 /* file attributes which can be mapped to nfs attributes */
81 #define NFS4_VALID_ATTRS (ATTR_MODE \
92 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
93 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
94 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
95 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label, struct inode *inode);
96 static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
97 struct nfs_fattr *fattr, struct iattr *sattr,
98 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
99 struct nfs4_label *olabel);
100 #ifdef CONFIG_NFS_V4_1
101 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
102 const struct cred *cred,
103 struct nfs4_slot *slot,
105 static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
106 const struct cred *);
107 static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
108 const struct cred *, bool);
111 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
112 static inline struct nfs4_label *
113 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
114 struct iattr *sattr, struct nfs4_label *label)
121 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
124 err = security_dentry_init_security(dentry, sattr->ia_mode,
125 &dentry->d_name, (void **)&label->label, &label->len);
132 nfs4_label_release_security(struct nfs4_label *label)
135 security_release_secctx(label->label, label->len);
137 static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
140 return server->attr_bitmask;
142 return server->attr_bitmask_nl;
145 static inline struct nfs4_label *
146 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
147 struct iattr *sattr, struct nfs4_label *l)
150 nfs4_label_release_security(struct nfs4_label *label)
153 nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
154 { return server->attr_bitmask; }
157 /* Prevent leaks of NFSv4 errors into userland */
158 static int nfs4_map_errors(int err)
163 case -NFS4ERR_RESOURCE:
164 case -NFS4ERR_LAYOUTTRYLATER:
165 case -NFS4ERR_RECALLCONFLICT:
167 case -NFS4ERR_WRONGSEC:
168 case -NFS4ERR_WRONG_CRED:
170 case -NFS4ERR_BADOWNER:
171 case -NFS4ERR_BADNAME:
173 case -NFS4ERR_SHARE_DENIED:
175 case -NFS4ERR_MINOR_VERS_MISMATCH:
176 return -EPROTONOSUPPORT;
177 case -NFS4ERR_FILE_OPEN:
180 dprintk("%s could not handle NFSv4 error %d\n",
188 * This is our standard bitmap for GETATTR requests.
190 const u32 nfs4_fattr_bitmap[3] = {
192 | FATTR4_WORD0_CHANGE
195 | FATTR4_WORD0_FILEID,
197 | FATTR4_WORD1_NUMLINKS
199 | FATTR4_WORD1_OWNER_GROUP
200 | FATTR4_WORD1_RAWDEV
201 | FATTR4_WORD1_SPACE_USED
202 | FATTR4_WORD1_TIME_ACCESS
203 | FATTR4_WORD1_TIME_METADATA
204 | FATTR4_WORD1_TIME_MODIFY
205 | FATTR4_WORD1_MOUNTED_ON_FILEID,
206 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
207 FATTR4_WORD2_SECURITY_LABEL
211 static const u32 nfs4_pnfs_open_bitmap[3] = {
213 | FATTR4_WORD0_CHANGE
216 | FATTR4_WORD0_FILEID,
218 | FATTR4_WORD1_NUMLINKS
220 | FATTR4_WORD1_OWNER_GROUP
221 | FATTR4_WORD1_RAWDEV
222 | FATTR4_WORD1_SPACE_USED
223 | FATTR4_WORD1_TIME_ACCESS
224 | FATTR4_WORD1_TIME_METADATA
225 | FATTR4_WORD1_TIME_MODIFY,
226 FATTR4_WORD2_MDSTHRESHOLD
227 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
228 | FATTR4_WORD2_SECURITY_LABEL
232 static const u32 nfs4_open_noattr_bitmap[3] = {
234 | FATTR4_WORD0_FILEID,
237 const u32 nfs4_statfs_bitmap[3] = {
238 FATTR4_WORD0_FILES_AVAIL
239 | FATTR4_WORD0_FILES_FREE
240 | FATTR4_WORD0_FILES_TOTAL,
241 FATTR4_WORD1_SPACE_AVAIL
242 | FATTR4_WORD1_SPACE_FREE
243 | FATTR4_WORD1_SPACE_TOTAL
246 const u32 nfs4_pathconf_bitmap[3] = {
248 | FATTR4_WORD0_MAXNAME,
252 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
253 | FATTR4_WORD0_MAXREAD
254 | FATTR4_WORD0_MAXWRITE
255 | FATTR4_WORD0_LEASE_TIME,
256 FATTR4_WORD1_TIME_DELTA
257 | FATTR4_WORD1_FS_LAYOUT_TYPES,
258 FATTR4_WORD2_LAYOUT_BLKSIZE
259 | FATTR4_WORD2_CLONE_BLKSIZE
260 | FATTR4_WORD2_XATTR_SUPPORT
263 const u32 nfs4_fs_locations_bitmap[3] = {
267 | FATTR4_WORD0_FILEID
268 | FATTR4_WORD0_FS_LOCATIONS,
270 | FATTR4_WORD1_OWNER_GROUP
271 | FATTR4_WORD1_RAWDEV
272 | FATTR4_WORD1_SPACE_USED
273 | FATTR4_WORD1_TIME_ACCESS
274 | FATTR4_WORD1_TIME_METADATA
275 | FATTR4_WORD1_TIME_MODIFY
276 | FATTR4_WORD1_MOUNTED_ON_FILEID,
279 static void nfs4_bitmap_copy_adjust(__u32 *dst, const __u32 *src,
282 unsigned long cache_validity;
284 memcpy(dst, src, NFS4_BITMASK_SZ*sizeof(*dst));
285 if (!inode || !nfs4_have_delegation(inode, FMODE_READ))
288 cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
289 if (!(cache_validity & NFS_INO_REVAL_FORCED))
290 cache_validity &= ~(NFS_INO_INVALID_CHANGE
291 | NFS_INO_INVALID_SIZE);
293 if (!(cache_validity & NFS_INO_INVALID_SIZE))
294 dst[0] &= ~FATTR4_WORD0_SIZE;
296 if (!(cache_validity & NFS_INO_INVALID_CHANGE))
297 dst[0] &= ~FATTR4_WORD0_CHANGE;
300 static void nfs4_bitmap_copy_adjust_setattr(__u32 *dst,
301 const __u32 *src, struct inode *inode)
303 nfs4_bitmap_copy_adjust(dst, src, inode);
306 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
307 struct nfs4_readdir_arg *readdir)
309 unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
313 readdir->cookie = cookie;
314 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
319 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
324 * NFSv4 servers do not return entries for '.' and '..'
325 * Therefore, we fake these entries here. We let '.'
326 * have cookie 0 and '..' have cookie 1. Note that
327 * when talking to the server, we always send cookie 0
330 start = p = kmap_atomic(*readdir->pages);
333 *p++ = xdr_one; /* next */
334 *p++ = xdr_zero; /* cookie, first word */
335 *p++ = xdr_one; /* cookie, second word */
336 *p++ = xdr_one; /* entry len */
337 memcpy(p, ".\0\0\0", 4); /* entry */
339 *p++ = xdr_one; /* bitmap length */
340 *p++ = htonl(attrs); /* bitmap */
341 *p++ = htonl(12); /* attribute buffer length */
342 *p++ = htonl(NF4DIR);
343 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
346 *p++ = xdr_one; /* next */
347 *p++ = xdr_zero; /* cookie, first word */
348 *p++ = xdr_two; /* cookie, second word */
349 *p++ = xdr_two; /* entry len */
350 memcpy(p, "..\0\0", 4); /* entry */
352 *p++ = xdr_one; /* bitmap length */
353 *p++ = htonl(attrs); /* bitmap */
354 *p++ = htonl(12); /* attribute buffer length */
355 *p++ = htonl(NF4DIR);
356 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
358 readdir->pgbase = (char *)p - (char *)start;
359 readdir->count -= readdir->pgbase;
360 kunmap_atomic(start);
363 static void nfs4_test_and_free_stateid(struct nfs_server *server,
364 nfs4_stateid *stateid,
365 const struct cred *cred)
367 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
369 ops->test_and_free_expired(server, stateid, cred);
372 static void __nfs4_free_revoked_stateid(struct nfs_server *server,
373 nfs4_stateid *stateid,
374 const struct cred *cred)
376 stateid->type = NFS4_REVOKED_STATEID_TYPE;
377 nfs4_test_and_free_stateid(server, stateid, cred);
380 static void nfs4_free_revoked_stateid(struct nfs_server *server,
381 const nfs4_stateid *stateid,
382 const struct cred *cred)
386 nfs4_stateid_copy(&tmp, stateid);
387 __nfs4_free_revoked_stateid(server, &tmp, cred);
390 static long nfs4_update_delay(long *timeout)
394 return NFS4_POLL_RETRY_MAX;
396 *timeout = NFS4_POLL_RETRY_MIN;
397 if (*timeout > NFS4_POLL_RETRY_MAX)
398 *timeout = NFS4_POLL_RETRY_MAX;
404 static int nfs4_delay_killable(long *timeout)
408 freezable_schedule_timeout_killable_unsafe(
409 nfs4_update_delay(timeout));
410 if (!__fatal_signal_pending(current))
415 static int nfs4_delay_interruptible(long *timeout)
419 freezable_schedule_timeout_interruptible_unsafe(nfs4_update_delay(timeout));
420 if (!signal_pending(current))
422 return __fatal_signal_pending(current) ? -EINTR :-ERESTARTSYS;
425 static int nfs4_delay(long *timeout, bool interruptible)
428 return nfs4_delay_interruptible(timeout);
429 return nfs4_delay_killable(timeout);
432 static const nfs4_stateid *
433 nfs4_recoverable_stateid(const nfs4_stateid *stateid)
437 switch (stateid->type) {
438 case NFS4_OPEN_STATEID_TYPE:
439 case NFS4_LOCK_STATEID_TYPE:
440 case NFS4_DELEGATION_STATEID_TYPE:
448 /* This is the error handling routine for processes that are allowed
451 static int nfs4_do_handle_exception(struct nfs_server *server,
452 int errorcode, struct nfs4_exception *exception)
454 struct nfs_client *clp = server->nfs_client;
455 struct nfs4_state *state = exception->state;
456 const nfs4_stateid *stateid;
457 struct inode *inode = exception->inode;
460 exception->delay = 0;
461 exception->recovering = 0;
462 exception->retry = 0;
464 stateid = nfs4_recoverable_stateid(exception->stateid);
465 if (stateid == NULL && state != NULL)
466 stateid = nfs4_recoverable_stateid(&state->stateid);
471 case -NFS4ERR_BADHANDLE:
473 if (inode != NULL && S_ISREG(inode->i_mode))
474 pnfs_destroy_layout(NFS_I(inode));
476 case -NFS4ERR_DELEG_REVOKED:
477 case -NFS4ERR_ADMIN_REVOKED:
478 case -NFS4ERR_EXPIRED:
479 case -NFS4ERR_BAD_STATEID:
480 case -NFS4ERR_PARTNER_NO_AUTH:
481 if (inode != NULL && stateid != NULL) {
482 nfs_inode_find_state_and_recover(inode,
484 goto wait_on_recovery;
487 case -NFS4ERR_OPENMODE:
491 err = nfs_async_inode_return_delegation(inode,
494 goto wait_on_recovery;
495 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
496 exception->retry = 1;
502 ret = nfs4_schedule_stateid_recovery(server, state);
505 goto wait_on_recovery;
506 case -NFS4ERR_STALE_STATEID:
507 case -NFS4ERR_STALE_CLIENTID:
508 nfs4_schedule_lease_recovery(clp);
509 goto wait_on_recovery;
511 ret = nfs4_schedule_migration_recovery(server);
514 goto wait_on_recovery;
515 case -NFS4ERR_LEASE_MOVED:
516 nfs4_schedule_lease_moved_recovery(clp);
517 goto wait_on_recovery;
518 #if defined(CONFIG_NFS_V4_1)
519 case -NFS4ERR_BADSESSION:
520 case -NFS4ERR_BADSLOT:
521 case -NFS4ERR_BAD_HIGH_SLOT:
522 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
523 case -NFS4ERR_DEADSESSION:
524 case -NFS4ERR_SEQ_FALSE_RETRY:
525 case -NFS4ERR_SEQ_MISORDERED:
526 /* Handled in nfs41_sequence_process() */
527 goto wait_on_recovery;
528 #endif /* defined(CONFIG_NFS_V4_1) */
529 case -NFS4ERR_FILE_OPEN:
530 if (exception->timeout > HZ) {
531 /* We have retried a decent amount, time to
539 nfs_inc_server_stats(server, NFSIOS_DELAY);
542 case -NFS4ERR_LAYOUTTRYLATER:
543 case -NFS4ERR_RECALLCONFLICT:
544 exception->delay = 1;
547 case -NFS4ERR_RETRY_UNCACHED_REP:
548 case -NFS4ERR_OLD_STATEID:
549 exception->retry = 1;
551 case -NFS4ERR_BADOWNER:
552 /* The following works around a Linux server bug! */
553 case -NFS4ERR_BADNAME:
554 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
555 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
556 exception->retry = 1;
557 printk(KERN_WARNING "NFS: v4 server %s "
558 "does not accept raw "
560 "Reenabling the idmapper.\n",
561 server->nfs_client->cl_hostname);
564 /* We failed to handle the error */
565 return nfs4_map_errors(ret);
567 exception->recovering = 1;
571 /* This is the error handling routine for processes that are allowed
574 int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
576 struct nfs_client *clp = server->nfs_client;
579 ret = nfs4_do_handle_exception(server, errorcode, exception);
580 if (exception->delay) {
581 ret = nfs4_delay(&exception->timeout,
582 exception->interruptible);
585 if (exception->recovering) {
586 ret = nfs4_wait_clnt_recover(clp);
587 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
594 exception->retry = 1;
599 nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
600 int errorcode, struct nfs4_exception *exception)
602 struct nfs_client *clp = server->nfs_client;
605 ret = nfs4_do_handle_exception(server, errorcode, exception);
606 if (exception->delay) {
607 rpc_delay(task, nfs4_update_delay(&exception->timeout));
610 if (exception->recovering) {
611 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
612 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
613 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
616 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
621 exception->retry = 1;
623 * For NFS4ERR_MOVED, the client transport will need to
624 * be recomputed after migration recovery has completed.
626 if (errorcode == -NFS4ERR_MOVED)
627 rpc_task_release_transport(task);
633 nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
634 struct nfs4_state *state, long *timeout)
636 struct nfs4_exception exception = {
640 if (task->tk_status >= 0)
643 exception.timeout = *timeout;
644 task->tk_status = nfs4_async_handle_exception(task, server,
647 if (exception.delay && timeout)
648 *timeout = exception.timeout;
655 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
656 * or 'false' otherwise.
658 static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
660 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
661 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
664 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
666 spin_lock(&clp->cl_lock);
667 if (time_before(clp->cl_last_renewal,timestamp))
668 clp->cl_last_renewal = timestamp;
669 spin_unlock(&clp->cl_lock);
672 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
674 struct nfs_client *clp = server->nfs_client;
676 if (!nfs4_has_session(clp))
677 do_renew_lease(clp, timestamp);
680 struct nfs4_call_sync_data {
681 const struct nfs_server *seq_server;
682 struct nfs4_sequence_args *seq_args;
683 struct nfs4_sequence_res *seq_res;
686 void nfs4_init_sequence(struct nfs4_sequence_args *args,
687 struct nfs4_sequence_res *res, int cache_reply,
690 args->sa_slot = NULL;
691 args->sa_cache_this = cache_reply;
692 args->sa_privileged = privileged;
697 static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
699 struct nfs4_slot *slot = res->sr_slot;
700 struct nfs4_slot_table *tbl;
703 spin_lock(&tbl->slot_tbl_lock);
704 if (!nfs41_wake_and_assign_slot(tbl, slot))
705 nfs4_free_slot(tbl, slot);
706 spin_unlock(&tbl->slot_tbl_lock);
711 static int nfs40_sequence_done(struct rpc_task *task,
712 struct nfs4_sequence_res *res)
714 if (res->sr_slot != NULL)
715 nfs40_sequence_free_slot(res);
719 #if defined(CONFIG_NFS_V4_1)
721 static void nfs41_release_slot(struct nfs4_slot *slot)
723 struct nfs4_session *session;
724 struct nfs4_slot_table *tbl;
725 bool send_new_highest_used_slotid = false;
730 session = tbl->session;
732 /* Bump the slot sequence number */
737 spin_lock(&tbl->slot_tbl_lock);
738 /* Be nice to the server: try to ensure that the last transmitted
739 * value for highest_user_slotid <= target_highest_slotid
741 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
742 send_new_highest_used_slotid = true;
744 if (nfs41_wake_and_assign_slot(tbl, slot)) {
745 send_new_highest_used_slotid = false;
748 nfs4_free_slot(tbl, slot);
750 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
751 send_new_highest_used_slotid = false;
753 spin_unlock(&tbl->slot_tbl_lock);
754 if (send_new_highest_used_slotid)
755 nfs41_notify_server(session->clp);
756 if (waitqueue_active(&tbl->slot_waitq))
757 wake_up_all(&tbl->slot_waitq);
760 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
762 nfs41_release_slot(res->sr_slot);
766 static void nfs4_slot_sequence_record_sent(struct nfs4_slot *slot,
769 if ((s32)(seqnr - slot->seq_nr_highest_sent) > 0)
770 slot->seq_nr_highest_sent = seqnr;
772 static void nfs4_slot_sequence_acked(struct nfs4_slot *slot,
775 slot->seq_nr_highest_sent = seqnr;
776 slot->seq_nr_last_acked = seqnr;
779 static void nfs4_probe_sequence(struct nfs_client *client, const struct cred *cred,
780 struct nfs4_slot *slot)
782 struct rpc_task *task = _nfs41_proc_sequence(client, cred, slot, true);
784 rpc_put_task_async(task);
787 static int nfs41_sequence_process(struct rpc_task *task,
788 struct nfs4_sequence_res *res)
790 struct nfs4_session *session;
791 struct nfs4_slot *slot = res->sr_slot;
792 struct nfs_client *clp;
798 /* don't increment the sequence number if the task wasn't sent */
799 if (!RPC_WAS_SENT(task) || slot->seq_done)
802 session = slot->table->session;
805 trace_nfs4_sequence_done(session, res);
807 status = res->sr_status;
808 if (task->tk_status == -NFS4ERR_DEADSESSION)
809 status = -NFS4ERR_DEADSESSION;
811 /* Check the SEQUENCE operation status */
814 /* Mark this sequence number as having been acked */
815 nfs4_slot_sequence_acked(slot, slot->seq_nr);
816 /* Update the slot's sequence and clientid lease timer */
818 do_renew_lease(clp, res->sr_timestamp);
819 /* Check sequence flags */
820 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
822 nfs41_update_target_slotid(slot->table, slot, res);
826 * sr_status remains 1 if an RPC level error occurred.
827 * The server may or may not have processed the sequence
830 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
834 /* The server detected a resend of the RPC call and
835 * returned NFS4ERR_DELAY as per Section 2.10.6.2
838 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
842 nfs4_slot_sequence_acked(slot, slot->seq_nr);
844 case -NFS4ERR_RETRY_UNCACHED_REP:
845 case -NFS4ERR_SEQ_FALSE_RETRY:
847 * The server thinks we tried to replay a request.
848 * Retry the call after bumping the sequence ID.
850 nfs4_slot_sequence_acked(slot, slot->seq_nr);
852 case -NFS4ERR_BADSLOT:
854 * The slot id we used was probably retired. Try again
855 * using a different slot id.
857 if (slot->slot_nr < slot->table->target_highest_slotid)
858 goto session_recover;
860 case -NFS4ERR_SEQ_MISORDERED:
861 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
863 * Were one or more calls using this slot interrupted?
864 * If the server never received the request, then our
865 * transmitted slot sequence number may be too high. However,
866 * if the server did receive the request then it might
867 * accidentally give us a reply with a mismatched operation.
868 * We can sort this out by sending a lone sequence operation
869 * to the server on the same slot.
871 if ((s32)(slot->seq_nr - slot->seq_nr_last_acked) > 1) {
873 if (task->tk_msg.rpc_proc != &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE]) {
874 nfs4_probe_sequence(clp, task->tk_msg.rpc_cred, slot);
881 * A retry might be sent while the original request is
882 * still in progress on the replier. The replier SHOULD
883 * deal with the issue by returning NFS4ERR_DELAY as the
884 * reply to SEQUENCE or CB_SEQUENCE operation, but
885 * implementations MAY return NFS4ERR_SEQ_MISORDERED.
887 * Restart the search after a delay.
889 slot->seq_nr = slot->seq_nr_highest_sent;
891 case -NFS4ERR_BADSESSION:
892 case -NFS4ERR_DEADSESSION:
893 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
894 goto session_recover;
896 /* Just update the slot sequence no. */
900 /* The session may be reset by one of the error handlers. */
901 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
905 nfs4_schedule_session_recovery(session, status);
906 dprintk("%s ERROR: %d Reset session\n", __func__, status);
907 nfs41_sequence_free_slot(res);
912 if (rpc_restart_call_prepare(task)) {
913 nfs41_sequence_free_slot(res);
919 if (!rpc_restart_call(task))
921 rpc_delay(task, NFS4_POLL_RETRY_MAX);
925 int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
927 if (!nfs41_sequence_process(task, res))
929 if (res->sr_slot != NULL)
930 nfs41_sequence_free_slot(res);
934 EXPORT_SYMBOL_GPL(nfs41_sequence_done);
936 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
938 if (res->sr_slot == NULL)
940 if (res->sr_slot->table->session != NULL)
941 return nfs41_sequence_process(task, res);
942 return nfs40_sequence_done(task, res);
945 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
947 if (res->sr_slot != NULL) {
948 if (res->sr_slot->table->session != NULL)
949 nfs41_sequence_free_slot(res);
951 nfs40_sequence_free_slot(res);
955 int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
957 if (res->sr_slot == NULL)
959 if (!res->sr_slot->table->session)
960 return nfs40_sequence_done(task, res);
961 return nfs41_sequence_done(task, res);
963 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
965 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
967 struct nfs4_call_sync_data *data = calldata;
969 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
971 nfs4_setup_sequence(data->seq_server->nfs_client,
972 data->seq_args, data->seq_res, task);
975 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
977 struct nfs4_call_sync_data *data = calldata;
979 nfs41_sequence_done(task, data->seq_res);
982 static const struct rpc_call_ops nfs41_call_sync_ops = {
983 .rpc_call_prepare = nfs41_call_sync_prepare,
984 .rpc_call_done = nfs41_call_sync_done,
987 #else /* !CONFIG_NFS_V4_1 */
989 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
991 return nfs40_sequence_done(task, res);
994 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
996 if (res->sr_slot != NULL)
997 nfs40_sequence_free_slot(res);
1000 int nfs4_sequence_done(struct rpc_task *task,
1001 struct nfs4_sequence_res *res)
1003 return nfs40_sequence_done(task, res);
1005 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
1007 #endif /* !CONFIG_NFS_V4_1 */
1009 static void nfs41_sequence_res_init(struct nfs4_sequence_res *res)
1011 res->sr_timestamp = jiffies;
1012 res->sr_status_flags = 0;
1017 void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
1018 struct nfs4_sequence_res *res,
1019 struct nfs4_slot *slot)
1023 slot->privileged = args->sa_privileged ? 1 : 0;
1024 args->sa_slot = slot;
1026 res->sr_slot = slot;
1029 int nfs4_setup_sequence(struct nfs_client *client,
1030 struct nfs4_sequence_args *args,
1031 struct nfs4_sequence_res *res,
1032 struct rpc_task *task)
1034 struct nfs4_session *session = nfs4_get_session(client);
1035 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
1036 struct nfs4_slot *slot;
1038 /* slot already allocated? */
1039 if (res->sr_slot != NULL)
1043 tbl = &session->fc_slot_table;
1045 spin_lock(&tbl->slot_tbl_lock);
1046 /* The state manager will wait until the slot table is empty */
1047 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
1050 slot = nfs4_alloc_slot(tbl);
1052 if (slot == ERR_PTR(-ENOMEM))
1053 goto out_sleep_timeout;
1056 spin_unlock(&tbl->slot_tbl_lock);
1058 nfs4_sequence_attach_slot(args, res, slot);
1060 trace_nfs4_setup_sequence(session, args);
1062 nfs41_sequence_res_init(res);
1063 rpc_call_start(task);
1066 /* Try again in 1/4 second */
1067 if (args->sa_privileged)
1068 rpc_sleep_on_priority_timeout(&tbl->slot_tbl_waitq, task,
1069 jiffies + (HZ >> 2), RPC_PRIORITY_PRIVILEGED);
1071 rpc_sleep_on_timeout(&tbl->slot_tbl_waitq, task,
1072 NULL, jiffies + (HZ >> 2));
1073 spin_unlock(&tbl->slot_tbl_lock);
1076 if (args->sa_privileged)
1077 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
1078 RPC_PRIORITY_PRIVILEGED);
1080 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
1081 spin_unlock(&tbl->slot_tbl_lock);
1084 EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
1086 static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1088 struct nfs4_call_sync_data *data = calldata;
1089 nfs4_setup_sequence(data->seq_server->nfs_client,
1090 data->seq_args, data->seq_res, task);
1093 static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1095 struct nfs4_call_sync_data *data = calldata;
1096 nfs4_sequence_done(task, data->seq_res);
1099 static const struct rpc_call_ops nfs40_call_sync_ops = {
1100 .rpc_call_prepare = nfs40_call_sync_prepare,
1101 .rpc_call_done = nfs40_call_sync_done,
1104 static int nfs4_call_sync_custom(struct rpc_task_setup *task_setup)
1107 struct rpc_task *task;
1109 task = rpc_run_task(task_setup);
1111 return PTR_ERR(task);
1113 ret = task->tk_status;
1118 static int nfs4_do_call_sync(struct rpc_clnt *clnt,
1119 struct nfs_server *server,
1120 struct rpc_message *msg,
1121 struct nfs4_sequence_args *args,
1122 struct nfs4_sequence_res *res,
1123 unsigned short task_flags)
1125 struct nfs_client *clp = server->nfs_client;
1126 struct nfs4_call_sync_data data = {
1127 .seq_server = server,
1131 struct rpc_task_setup task_setup = {
1134 .callback_ops = clp->cl_mvops->call_sync_ops,
1135 .callback_data = &data,
1136 .flags = task_flags,
1139 return nfs4_call_sync_custom(&task_setup);
1142 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
1143 struct nfs_server *server,
1144 struct rpc_message *msg,
1145 struct nfs4_sequence_args *args,
1146 struct nfs4_sequence_res *res)
1148 return nfs4_do_call_sync(clnt, server, msg, args, res, 0);
1152 int nfs4_call_sync(struct rpc_clnt *clnt,
1153 struct nfs_server *server,
1154 struct rpc_message *msg,
1155 struct nfs4_sequence_args *args,
1156 struct nfs4_sequence_res *res,
1159 nfs4_init_sequence(args, res, cache_reply, 0);
1160 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
1164 nfs4_inc_nlink_locked(struct inode *inode)
1166 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1171 nfs4_dec_nlink_locked(struct inode *inode)
1173 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1178 nfs4_update_changeattr_locked(struct inode *inode,
1179 struct nfs4_change_info *cinfo,
1180 unsigned long timestamp, unsigned long cache_validity)
1182 struct nfs_inode *nfsi = NFS_I(inode);
1184 nfsi->cache_validity |= NFS_INO_INVALID_CTIME
1185 | NFS_INO_INVALID_MTIME
1188 if (cinfo->atomic && cinfo->before == inode_peek_iversion_raw(inode)) {
1189 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1190 nfsi->attrtimeo_timestamp = jiffies;
1192 if (S_ISDIR(inode->i_mode)) {
1193 nfsi->cache_validity |= NFS_INO_INVALID_DATA;
1194 nfs_force_lookup_revalidate(inode);
1196 if (!NFS_PROTO(inode)->have_delegation(inode,
1198 nfsi->cache_validity |= NFS_INO_REVAL_PAGECACHE;
1201 if (cinfo->before != inode_peek_iversion_raw(inode))
1202 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1203 NFS_INO_INVALID_ACL |
1204 NFS_INO_INVALID_XATTR;
1206 inode_set_iversion_raw(inode, cinfo->after);
1207 nfsi->read_cache_jiffies = timestamp;
1208 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
1209 nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE;
1211 if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
1212 nfs_fscache_invalidate(inode);
1216 nfs4_update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1217 unsigned long timestamp, unsigned long cache_validity)
1219 spin_lock(&dir->i_lock);
1220 nfs4_update_changeattr_locked(dir, cinfo, timestamp, cache_validity);
1221 spin_unlock(&dir->i_lock);
1224 struct nfs4_open_createattrs {
1225 struct nfs4_label *label;
1226 struct iattr *sattr;
1227 const __u32 verf[2];
1230 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1231 int err, struct nfs4_exception *exception)
1235 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1237 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1238 exception->retry = 1;
1242 static fmode_t _nfs4_ctx_to_accessmode(const struct nfs_open_context *ctx)
1244 return ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
1247 static fmode_t _nfs4_ctx_to_openmode(const struct nfs_open_context *ctx)
1249 fmode_t ret = ctx->mode & (FMODE_READ|FMODE_WRITE);
1251 return (ctx->mode & FMODE_EXEC) ? FMODE_READ | ret : ret;
1255 nfs4_map_atomic_open_share(struct nfs_server *server,
1256 fmode_t fmode, int openflags)
1260 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1262 res = NFS4_SHARE_ACCESS_READ;
1265 res = NFS4_SHARE_ACCESS_WRITE;
1267 case FMODE_READ|FMODE_WRITE:
1268 res = NFS4_SHARE_ACCESS_BOTH;
1270 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1272 /* Want no delegation if we're using O_DIRECT */
1273 if (openflags & O_DIRECT)
1274 res |= NFS4_SHARE_WANT_NO_DELEG;
1279 static enum open_claim_type4
1280 nfs4_map_atomic_open_claim(struct nfs_server *server,
1281 enum open_claim_type4 claim)
1283 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1288 case NFS4_OPEN_CLAIM_FH:
1289 return NFS4_OPEN_CLAIM_NULL;
1290 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1291 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1292 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1293 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1297 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
1299 p->o_res.f_attr = &p->f_attr;
1300 p->o_res.f_label = p->f_label;
1301 p->o_res.seqid = p->o_arg.seqid;
1302 p->c_res.seqid = p->c_arg.seqid;
1303 p->o_res.server = p->o_arg.server;
1304 p->o_res.access_request = p->o_arg.access;
1305 nfs_fattr_init(&p->f_attr);
1306 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
1309 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
1310 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
1311 const struct nfs4_open_createattrs *c,
1312 enum open_claim_type4 claim,
1315 struct dentry *parent = dget_parent(dentry);
1316 struct inode *dir = d_inode(parent);
1317 struct nfs_server *server = NFS_SERVER(dir);
1318 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
1319 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
1320 struct nfs4_opendata *p;
1322 p = kzalloc(sizeof(*p), gfp_mask);
1326 p->f_label = nfs4_label_alloc(server, gfp_mask);
1327 if (IS_ERR(p->f_label))
1330 p->a_label = nfs4_label_alloc(server, gfp_mask);
1331 if (IS_ERR(p->a_label))
1334 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1335 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
1336 if (IS_ERR(p->o_arg.seqid))
1337 goto err_free_label;
1338 nfs_sb_active(dentry->d_sb);
1339 p->dentry = dget(dentry);
1342 atomic_inc(&sp->so_count);
1343 p->o_arg.open_flags = flags;
1344 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
1345 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1346 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1348 if (flags & O_CREAT) {
1349 p->o_arg.umask = current_umask();
1350 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1351 if (c->sattr != NULL && c->sattr->ia_valid != 0) {
1352 p->o_arg.u.attrs = &p->attrs;
1353 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
1355 memcpy(p->o_arg.u.verifier.data, c->verf,
1356 sizeof(p->o_arg.u.verifier.data));
1359 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1360 * will return permission denied for all bits until close */
1361 if (!(flags & O_EXCL)) {
1362 /* ask server to check for all possible rights as results
1364 switch (p->o_arg.claim) {
1367 case NFS4_OPEN_CLAIM_NULL:
1368 case NFS4_OPEN_CLAIM_FH:
1369 p->o_arg.access = NFS4_ACCESS_READ |
1370 NFS4_ACCESS_MODIFY |
1371 NFS4_ACCESS_EXTEND |
1372 NFS4_ACCESS_EXECUTE;
1373 #ifdef CONFIG_NFS_V4_2
1374 if (server->caps & NFS_CAP_XATTR)
1375 p->o_arg.access |= NFS4_ACCESS_XAREAD |
1376 NFS4_ACCESS_XAWRITE |
1381 p->o_arg.clientid = server->nfs_client->cl_clientid;
1382 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1383 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1384 p->o_arg.name = &dentry->d_name;
1385 p->o_arg.server = server;
1386 p->o_arg.bitmask = nfs4_bitmask(server, label);
1387 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1388 switch (p->o_arg.claim) {
1389 case NFS4_OPEN_CLAIM_NULL:
1390 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1391 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1392 p->o_arg.fh = NFS_FH(dir);
1394 case NFS4_OPEN_CLAIM_PREVIOUS:
1395 case NFS4_OPEN_CLAIM_FH:
1396 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1397 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1398 p->o_arg.fh = NFS_FH(d_inode(dentry));
1400 p->c_arg.fh = &p->o_res.fh;
1401 p->c_arg.stateid = &p->o_res.stateid;
1402 p->c_arg.seqid = p->o_arg.seqid;
1403 nfs4_init_opendata_res(p);
1404 kref_init(&p->kref);
1408 nfs4_label_free(p->a_label);
1410 nfs4_label_free(p->f_label);
1418 static void nfs4_opendata_free(struct kref *kref)
1420 struct nfs4_opendata *p = container_of(kref,
1421 struct nfs4_opendata, kref);
1422 struct super_block *sb = p->dentry->d_sb;
1424 nfs4_lgopen_release(p->lgp);
1425 nfs_free_seqid(p->o_arg.seqid);
1426 nfs4_sequence_free_slot(&p->o_res.seq_res);
1427 if (p->state != NULL)
1428 nfs4_put_open_state(p->state);
1429 nfs4_put_state_owner(p->owner);
1431 nfs4_label_free(p->a_label);
1432 nfs4_label_free(p->f_label);
1436 nfs_sb_deactive(sb);
1437 nfs_fattr_free_names(&p->f_attr);
1438 kfree(p->f_attr.mdsthreshold);
1442 static void nfs4_opendata_put(struct nfs4_opendata *p)
1445 kref_put(&p->kref, nfs4_opendata_free);
1448 static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1451 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1452 case FMODE_READ|FMODE_WRITE:
1453 return state->n_rdwr != 0;
1455 return state->n_wronly != 0;
1457 return state->n_rdonly != 0;
1463 static int can_open_cached(struct nfs4_state *state, fmode_t mode,
1464 int open_mode, enum open_claim_type4 claim)
1468 if (open_mode & (O_EXCL|O_TRUNC))
1471 case NFS4_OPEN_CLAIM_NULL:
1472 case NFS4_OPEN_CLAIM_FH:
1477 switch (mode & (FMODE_READ|FMODE_WRITE)) {
1479 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1480 && state->n_rdonly != 0;
1483 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1484 && state->n_wronly != 0;
1486 case FMODE_READ|FMODE_WRITE:
1487 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1488 && state->n_rdwr != 0;
1494 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1495 enum open_claim_type4 claim)
1497 if (delegation == NULL)
1499 if ((delegation->type & fmode) != fmode)
1502 case NFS4_OPEN_CLAIM_NULL:
1503 case NFS4_OPEN_CLAIM_FH:
1505 case NFS4_OPEN_CLAIM_PREVIOUS:
1506 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1512 nfs_mark_delegation_referenced(delegation);
1516 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1525 case FMODE_READ|FMODE_WRITE:
1528 nfs4_state_set_mode_locked(state, state->state | fmode);
1531 #ifdef CONFIG_NFS_V4_1
1532 static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1534 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1536 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1538 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1542 #endif /* CONFIG_NFS_V4_1 */
1544 static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1546 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1547 wake_up_all(&state->waitq);
1550 static void nfs_state_log_out_of_order_open_stateid(struct nfs4_state *state,
1551 const nfs4_stateid *stateid)
1553 u32 state_seqid = be32_to_cpu(state->open_stateid.seqid);
1554 u32 stateid_seqid = be32_to_cpu(stateid->seqid);
1556 if (stateid_seqid == state_seqid + 1U ||
1557 (stateid_seqid == 1U && state_seqid == 0xffffffffU))
1558 nfs_state_log_update_open_stateid(state);
1560 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1563 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
1565 struct nfs_client *clp = state->owner->so_server->nfs_client;
1566 bool need_recover = false;
1568 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1569 need_recover = true;
1570 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1571 need_recover = true;
1572 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1573 need_recover = true;
1575 nfs4_state_mark_reclaim_nograce(clp, state);
1579 * Check for whether or not the caller may update the open stateid
1580 * to the value passed in by stateid.
1582 * Note: This function relies heavily on the server implementing
1583 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1585 * i.e. The stateid seqids have to be initialised to 1, and
1586 * are then incremented on every state transition.
1588 static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1589 const nfs4_stateid *stateid)
1591 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0 ||
1592 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1593 if (stateid->seqid == cpu_to_be32(1))
1594 nfs_state_log_update_open_stateid(state);
1596 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1600 if (nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1601 nfs_state_log_out_of_order_open_stateid(state, stateid);
1607 static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1609 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1611 if (state->n_wronly)
1612 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1613 if (state->n_rdonly)
1614 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1616 set_bit(NFS_O_RDWR_STATE, &state->flags);
1617 set_bit(NFS_OPEN_STATE, &state->flags);
1620 static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1621 nfs4_stateid *stateid, fmode_t fmode)
1623 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1624 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1626 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1629 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1632 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1633 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1634 clear_bit(NFS_OPEN_STATE, &state->flags);
1636 if (stateid == NULL)
1638 /* Handle OPEN+OPEN_DOWNGRADE races */
1639 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1640 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1641 nfs_resync_open_stateid_locked(state);
1644 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1645 nfs4_stateid_copy(&state->stateid, stateid);
1646 nfs4_stateid_copy(&state->open_stateid, stateid);
1647 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
1649 nfs_state_log_update_open_stateid(state);
1652 static void nfs_clear_open_stateid(struct nfs4_state *state,
1653 nfs4_stateid *arg_stateid,
1654 nfs4_stateid *stateid, fmode_t fmode)
1656 write_seqlock(&state->seqlock);
1657 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1658 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1659 nfs_clear_open_stateid_locked(state, stateid, fmode);
1660 write_sequnlock(&state->seqlock);
1661 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1662 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1665 static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1666 const nfs4_stateid *stateid, nfs4_stateid *freeme)
1667 __must_hold(&state->owner->so_lock)
1668 __must_hold(&state->seqlock)
1676 if (!nfs_need_update_open_stateid(state, stateid))
1678 if (!test_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1682 /* Rely on seqids for serialisation with NFSv4.0 */
1683 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1686 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1688 * Ensure we process the state changes in the same order
1689 * in which the server processed them by delaying the
1690 * update of the stateid until we are in sequence.
1692 write_sequnlock(&state->seqlock);
1693 spin_unlock(&state->owner->so_lock);
1695 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
1696 if (!signal_pending(current)) {
1697 if (schedule_timeout(5*HZ) == 0)
1703 finish_wait(&state->waitq, &wait);
1705 spin_lock(&state->owner->so_lock);
1706 write_seqlock(&state->seqlock);
1709 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1710 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1711 nfs4_stateid_copy(freeme, &state->open_stateid);
1712 nfs_test_and_clear_all_open_stateid(state);
1715 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1716 nfs4_stateid_copy(&state->stateid, stateid);
1717 nfs4_stateid_copy(&state->open_stateid, stateid);
1718 trace_nfs4_open_stateid_update(state->inode, stateid, status);
1719 nfs_state_log_update_open_stateid(state);
1722 static void nfs_state_set_open_stateid(struct nfs4_state *state,
1723 const nfs4_stateid *open_stateid,
1725 nfs4_stateid *freeme)
1728 * Protect the call to nfs4_state_set_mode_locked and
1729 * serialise the stateid update
1731 write_seqlock(&state->seqlock);
1732 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1735 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1738 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1740 case FMODE_READ|FMODE_WRITE:
1741 set_bit(NFS_O_RDWR_STATE, &state->flags);
1743 set_bit(NFS_OPEN_STATE, &state->flags);
1744 write_sequnlock(&state->seqlock);
1747 static void nfs_state_clear_open_state_flags(struct nfs4_state *state)
1749 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1750 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1751 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1752 clear_bit(NFS_OPEN_STATE, &state->flags);
1755 static void nfs_state_set_delegation(struct nfs4_state *state,
1756 const nfs4_stateid *deleg_stateid,
1760 * Protect the call to nfs4_state_set_mode_locked and
1761 * serialise the stateid update
1763 write_seqlock(&state->seqlock);
1764 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1765 set_bit(NFS_DELEGATED_STATE, &state->flags);
1766 write_sequnlock(&state->seqlock);
1769 static void nfs_state_clear_delegation(struct nfs4_state *state)
1771 write_seqlock(&state->seqlock);
1772 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1773 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1774 write_sequnlock(&state->seqlock);
1777 int update_open_stateid(struct nfs4_state *state,
1778 const nfs4_stateid *open_stateid,
1779 const nfs4_stateid *delegation,
1782 struct nfs_server *server = NFS_SERVER(state->inode);
1783 struct nfs_client *clp = server->nfs_client;
1784 struct nfs_inode *nfsi = NFS_I(state->inode);
1785 struct nfs_delegation *deleg_cur;
1786 nfs4_stateid freeme = { };
1789 fmode &= (FMODE_READ|FMODE_WRITE);
1792 spin_lock(&state->owner->so_lock);
1793 if (open_stateid != NULL) {
1794 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1798 deleg_cur = nfs4_get_valid_delegation(state->inode);
1799 if (deleg_cur == NULL)
1802 spin_lock(&deleg_cur->lock);
1803 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1804 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1805 (deleg_cur->type & fmode) != fmode)
1806 goto no_delegation_unlock;
1808 if (delegation == NULL)
1809 delegation = &deleg_cur->stateid;
1810 else if (!nfs4_stateid_match_other(&deleg_cur->stateid, delegation))
1811 goto no_delegation_unlock;
1813 nfs_mark_delegation_referenced(deleg_cur);
1814 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
1816 no_delegation_unlock:
1817 spin_unlock(&deleg_cur->lock);
1820 update_open_stateflags(state, fmode);
1821 spin_unlock(&state->owner->so_lock);
1824 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1825 nfs4_schedule_state_manager(clp);
1826 if (freeme.type != 0)
1827 nfs4_test_and_free_stateid(server, &freeme,
1828 state->owner->so_cred);
1833 static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1834 const nfs4_stateid *stateid)
1836 struct nfs4_state *state = lsp->ls_state;
1839 spin_lock(&state->state_lock);
1840 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1842 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1844 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1847 spin_unlock(&state->state_lock);
1851 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1853 struct nfs_delegation *delegation;
1855 fmode &= FMODE_READ|FMODE_WRITE;
1857 delegation = nfs4_get_valid_delegation(inode);
1858 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1863 nfs4_inode_return_delegation(inode);
1866 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1868 struct nfs4_state *state = opendata->state;
1869 struct nfs_delegation *delegation;
1870 int open_mode = opendata->o_arg.open_flags;
1871 fmode_t fmode = opendata->o_arg.fmode;
1872 enum open_claim_type4 claim = opendata->o_arg.claim;
1873 nfs4_stateid stateid;
1877 spin_lock(&state->owner->so_lock);
1878 if (can_open_cached(state, fmode, open_mode, claim)) {
1879 update_open_stateflags(state, fmode);
1880 spin_unlock(&state->owner->so_lock);
1881 goto out_return_state;
1883 spin_unlock(&state->owner->so_lock);
1885 delegation = nfs4_get_valid_delegation(state->inode);
1886 if (!can_open_delegated(delegation, fmode, claim)) {
1890 /* Save the delegation */
1891 nfs4_stateid_copy(&stateid, &delegation->stateid);
1893 nfs_release_seqid(opendata->o_arg.seqid);
1894 if (!opendata->is_recover) {
1895 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1901 /* Try to update the stateid using the delegation */
1902 if (update_open_stateid(state, NULL, &stateid, fmode))
1903 goto out_return_state;
1906 return ERR_PTR(ret);
1908 refcount_inc(&state->count);
1913 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1915 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1916 struct nfs_delegation *delegation;
1917 int delegation_flags = 0;
1920 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1922 delegation_flags = delegation->flags;
1924 switch (data->o_arg.claim) {
1927 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1928 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1929 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1930 "returning a delegation for "
1931 "OPEN(CLAIM_DELEGATE_CUR)\n",
1935 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1936 nfs_inode_set_delegation(state->inode,
1937 data->owner->so_cred,
1938 data->o_res.delegation_type,
1939 &data->o_res.delegation,
1940 data->o_res.pagemod_limit);
1942 nfs_inode_reclaim_delegation(state->inode,
1943 data->owner->so_cred,
1944 data->o_res.delegation_type,
1945 &data->o_res.delegation,
1946 data->o_res.pagemod_limit);
1948 if (data->o_res.do_recall)
1949 nfs_async_inode_return_delegation(state->inode,
1950 &data->o_res.delegation);
1954 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1955 * and update the nfs4_state.
1957 static struct nfs4_state *
1958 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1960 struct inode *inode = data->state->inode;
1961 struct nfs4_state *state = data->state;
1964 if (!data->rpc_done) {
1965 if (data->rpc_status)
1966 return ERR_PTR(data->rpc_status);
1967 /* cached opens have already been processed */
1971 ret = nfs_refresh_inode(inode, &data->f_attr);
1973 return ERR_PTR(ret);
1975 if (data->o_res.delegation_type != 0)
1976 nfs4_opendata_check_deleg(data, state);
1978 if (!update_open_stateid(state, &data->o_res.stateid,
1979 NULL, data->o_arg.fmode))
1980 return ERR_PTR(-EAGAIN);
1981 refcount_inc(&state->count);
1986 static struct inode *
1987 nfs4_opendata_get_inode(struct nfs4_opendata *data)
1989 struct inode *inode;
1991 switch (data->o_arg.claim) {
1992 case NFS4_OPEN_CLAIM_NULL:
1993 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1994 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1995 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1996 return ERR_PTR(-EAGAIN);
1997 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1998 &data->f_attr, data->f_label);
2001 inode = d_inode(data->dentry);
2003 nfs_refresh_inode(inode, &data->f_attr);
2008 static struct nfs4_state *
2009 nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
2011 struct nfs4_state *state;
2012 struct inode *inode;
2014 inode = nfs4_opendata_get_inode(data);
2016 return ERR_CAST(inode);
2017 if (data->state != NULL && data->state->inode == inode) {
2018 state = data->state;
2019 refcount_inc(&state->count);
2021 state = nfs4_get_open_state(inode, data->owner);
2024 state = ERR_PTR(-ENOMEM);
2028 static struct nfs4_state *
2029 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2031 struct nfs4_state *state;
2033 if (!data->rpc_done) {
2034 state = nfs4_try_open_cached(data);
2035 trace_nfs4_cached_open(data->state);
2039 state = nfs4_opendata_find_nfs4_state(data);
2043 if (data->o_res.delegation_type != 0)
2044 nfs4_opendata_check_deleg(data, state);
2045 if (!update_open_stateid(state, &data->o_res.stateid,
2046 NULL, data->o_arg.fmode)) {
2047 nfs4_put_open_state(state);
2048 state = ERR_PTR(-EAGAIN);
2051 nfs_release_seqid(data->o_arg.seqid);
2055 static struct nfs4_state *
2056 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2058 struct nfs4_state *ret;
2060 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
2061 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
2063 ret = _nfs4_opendata_to_nfs4_state(data);
2064 nfs4_sequence_free_slot(&data->o_res.seq_res);
2068 static struct nfs_open_context *
2069 nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode)
2071 struct nfs_inode *nfsi = NFS_I(state->inode);
2072 struct nfs_open_context *ctx;
2075 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
2076 if (ctx->state != state)
2078 if ((ctx->mode & mode) != mode)
2080 if (!get_nfs_open_context(ctx))
2086 return ERR_PTR(-ENOENT);
2089 static struct nfs_open_context *
2090 nfs4_state_find_open_context(struct nfs4_state *state)
2092 struct nfs_open_context *ctx;
2094 ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE);
2097 ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE);
2100 return nfs4_state_find_open_context_mode(state, FMODE_READ);
2103 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
2104 struct nfs4_state *state, enum open_claim_type4 claim)
2106 struct nfs4_opendata *opendata;
2108 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
2109 NULL, claim, GFP_NOFS);
2110 if (opendata == NULL)
2111 return ERR_PTR(-ENOMEM);
2112 opendata->state = state;
2113 refcount_inc(&state->count);
2117 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
2120 struct nfs4_state *newstate;
2123 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
2125 opendata->o_arg.open_flags = 0;
2126 opendata->o_arg.fmode = fmode;
2127 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
2128 NFS_SB(opendata->dentry->d_sb),
2130 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
2131 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
2132 nfs4_init_opendata_res(opendata);
2133 ret = _nfs4_recover_proc_open(opendata);
2136 newstate = nfs4_opendata_to_nfs4_state(opendata);
2137 if (IS_ERR(newstate))
2138 return PTR_ERR(newstate);
2139 if (newstate != opendata->state)
2141 nfs4_close_state(newstate, fmode);
2145 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
2149 /* memory barrier prior to reading state->n_* */
2151 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2154 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2157 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2161 * We may have performed cached opens for all three recoveries.
2162 * Check if we need to update the current stateid.
2164 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
2165 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
2166 write_seqlock(&state->seqlock);
2167 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2168 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2169 write_sequnlock(&state->seqlock);
2176 * reclaim state on the server after a reboot.
2178 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2180 struct nfs_delegation *delegation;
2181 struct nfs4_opendata *opendata;
2182 fmode_t delegation_type = 0;
2185 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2186 NFS4_OPEN_CLAIM_PREVIOUS);
2187 if (IS_ERR(opendata))
2188 return PTR_ERR(opendata);
2190 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2191 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
2192 delegation_type = delegation->type;
2194 opendata->o_arg.u.delegation_type = delegation_type;
2195 status = nfs4_open_recover(opendata, state);
2196 nfs4_opendata_put(opendata);
2200 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2202 struct nfs_server *server = NFS_SERVER(state->inode);
2203 struct nfs4_exception exception = { };
2206 err = _nfs4_do_open_reclaim(ctx, state);
2207 trace_nfs4_open_reclaim(ctx, 0, err);
2208 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2210 if (err != -NFS4ERR_DELAY)
2212 nfs4_handle_exception(server, err, &exception);
2213 } while (exception.retry);
2217 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2219 struct nfs_open_context *ctx;
2222 ctx = nfs4_state_find_open_context(state);
2225 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2226 nfs_state_clear_open_state_flags(state);
2227 ret = nfs4_do_open_reclaim(ctx, state);
2228 put_nfs_open_context(ctx);
2232 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)
2236 printk(KERN_ERR "NFS: %s: unhandled error "
2237 "%d.\n", __func__, err);
2244 case -NFS4ERR_BADSESSION:
2245 case -NFS4ERR_BADSLOT:
2246 case -NFS4ERR_BAD_HIGH_SLOT:
2247 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2248 case -NFS4ERR_DEADSESSION:
2250 case -NFS4ERR_STALE_CLIENTID:
2251 case -NFS4ERR_STALE_STATEID:
2252 /* Don't recall a delegation if it was lost */
2253 nfs4_schedule_lease_recovery(server->nfs_client);
2255 case -NFS4ERR_MOVED:
2256 nfs4_schedule_migration_recovery(server);
2258 case -NFS4ERR_LEASE_MOVED:
2259 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2261 case -NFS4ERR_DELEG_REVOKED:
2262 case -NFS4ERR_ADMIN_REVOKED:
2263 case -NFS4ERR_EXPIRED:
2264 case -NFS4ERR_BAD_STATEID:
2265 case -NFS4ERR_OPENMODE:
2266 nfs_inode_find_state_and_recover(state->inode,
2268 nfs4_schedule_stateid_recovery(server, state);
2270 case -NFS4ERR_DELAY:
2271 case -NFS4ERR_GRACE:
2275 case -NFS4ERR_DENIED:
2277 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2279 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2286 int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2287 struct nfs4_state *state, const nfs4_stateid *stateid)
2289 struct nfs_server *server = NFS_SERVER(state->inode);
2290 struct nfs4_opendata *opendata;
2293 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2294 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2295 if (IS_ERR(opendata))
2296 return PTR_ERR(opendata);
2297 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
2298 if (!test_bit(NFS_O_RDWR_STATE, &state->flags)) {
2299 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2303 if (!test_bit(NFS_O_WRONLY_STATE, &state->flags)) {
2304 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2308 if (!test_bit(NFS_O_RDONLY_STATE, &state->flags)) {
2309 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2313 nfs_state_clear_delegation(state);
2315 nfs4_opendata_put(opendata);
2316 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
2319 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2321 struct nfs4_opendata *data = calldata;
2323 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2324 &data->c_arg.seq_args, &data->c_res.seq_res, task);
2327 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2329 struct nfs4_opendata *data = calldata;
2331 nfs40_sequence_done(task, &data->c_res.seq_res);
2333 data->rpc_status = task->tk_status;
2334 if (data->rpc_status == 0) {
2335 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
2336 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2337 renew_lease(data->o_res.server, data->timestamp);
2338 data->rpc_done = true;
2342 static void nfs4_open_confirm_release(void *calldata)
2344 struct nfs4_opendata *data = calldata;
2345 struct nfs4_state *state = NULL;
2347 /* If this request hasn't been cancelled, do nothing */
2348 if (!data->cancelled)
2350 /* In case of error, no cleanup! */
2351 if (!data->rpc_done)
2353 state = nfs4_opendata_to_nfs4_state(data);
2355 nfs4_close_state(state, data->o_arg.fmode);
2357 nfs4_opendata_put(data);
2360 static const struct rpc_call_ops nfs4_open_confirm_ops = {
2361 .rpc_call_prepare = nfs4_open_confirm_prepare,
2362 .rpc_call_done = nfs4_open_confirm_done,
2363 .rpc_release = nfs4_open_confirm_release,
2367 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2369 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2371 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
2372 struct rpc_task *task;
2373 struct rpc_message msg = {
2374 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2375 .rpc_argp = &data->c_arg,
2376 .rpc_resp = &data->c_res,
2377 .rpc_cred = data->owner->so_cred,
2379 struct rpc_task_setup task_setup_data = {
2380 .rpc_client = server->client,
2381 .rpc_message = &msg,
2382 .callback_ops = &nfs4_open_confirm_ops,
2383 .callback_data = data,
2384 .workqueue = nfsiod_workqueue,
2385 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
2389 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2391 kref_get(&data->kref);
2392 data->rpc_done = false;
2393 data->rpc_status = 0;
2394 data->timestamp = jiffies;
2395 task = rpc_run_task(&task_setup_data);
2397 return PTR_ERR(task);
2398 status = rpc_wait_for_completion_task(task);
2400 data->cancelled = true;
2403 status = data->rpc_status;
2408 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
2410 struct nfs4_opendata *data = calldata;
2411 struct nfs4_state_owner *sp = data->owner;
2412 struct nfs_client *clp = sp->so_server->nfs_client;
2413 enum open_claim_type4 claim = data->o_arg.claim;
2415 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
2418 * Check if we still need to send an OPEN call, or if we can use
2419 * a delegation instead.
2421 if (data->state != NULL) {
2422 struct nfs_delegation *delegation;
2424 if (can_open_cached(data->state, data->o_arg.fmode,
2425 data->o_arg.open_flags, claim))
2428 delegation = nfs4_get_valid_delegation(data->state->inode);
2429 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
2430 goto unlock_no_action;
2433 /* Update client id. */
2434 data->o_arg.clientid = clp->cl_clientid;
2438 case NFS4_OPEN_CLAIM_PREVIOUS:
2439 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2440 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
2441 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
2443 case NFS4_OPEN_CLAIM_FH:
2444 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
2446 data->timestamp = jiffies;
2447 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
2448 &data->o_arg.seq_args,
2449 &data->o_res.seq_res,
2451 nfs_release_seqid(data->o_arg.seqid);
2453 /* Set the create mode (note dependency on the session type) */
2454 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2455 if (data->o_arg.open_flags & O_EXCL) {
2456 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2457 if (nfs4_has_persistent_session(clp))
2458 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2459 else if (clp->cl_mvops->minor_version > 0)
2460 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2464 trace_nfs4_cached_open(data->state);
2467 task->tk_action = NULL;
2469 nfs4_sequence_done(task, &data->o_res.seq_res);
2472 static void nfs4_open_done(struct rpc_task *task, void *calldata)
2474 struct nfs4_opendata *data = calldata;
2476 data->rpc_status = task->tk_status;
2478 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
2481 if (task->tk_status == 0) {
2482 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2483 switch (data->o_res.f_attr->mode & S_IFMT) {
2487 data->rpc_status = -ELOOP;
2490 data->rpc_status = -EISDIR;
2493 data->rpc_status = -ENOTDIR;
2496 renew_lease(data->o_res.server, data->timestamp);
2497 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2498 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2500 data->rpc_done = true;
2503 static void nfs4_open_release(void *calldata)
2505 struct nfs4_opendata *data = calldata;
2506 struct nfs4_state *state = NULL;
2508 /* If this request hasn't been cancelled, do nothing */
2509 if (!data->cancelled)
2511 /* In case of error, no cleanup! */
2512 if (data->rpc_status != 0 || !data->rpc_done)
2514 /* In case we need an open_confirm, no cleanup! */
2515 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2517 state = nfs4_opendata_to_nfs4_state(data);
2519 nfs4_close_state(state, data->o_arg.fmode);
2521 nfs4_opendata_put(data);
2524 static const struct rpc_call_ops nfs4_open_ops = {
2525 .rpc_call_prepare = nfs4_open_prepare,
2526 .rpc_call_done = nfs4_open_done,
2527 .rpc_release = nfs4_open_release,
2530 static int nfs4_run_open_task(struct nfs4_opendata *data,
2531 struct nfs_open_context *ctx)
2533 struct inode *dir = d_inode(data->dir);
2534 struct nfs_server *server = NFS_SERVER(dir);
2535 struct nfs_openargs *o_arg = &data->o_arg;
2536 struct nfs_openres *o_res = &data->o_res;
2537 struct rpc_task *task;
2538 struct rpc_message msg = {
2539 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2542 .rpc_cred = data->owner->so_cred,
2544 struct rpc_task_setup task_setup_data = {
2545 .rpc_client = server->client,
2546 .rpc_message = &msg,
2547 .callback_ops = &nfs4_open_ops,
2548 .callback_data = data,
2549 .workqueue = nfsiod_workqueue,
2550 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
2554 kref_get(&data->kref);
2555 data->rpc_done = false;
2556 data->rpc_status = 0;
2557 data->cancelled = false;
2558 data->is_recover = false;
2560 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
2561 data->is_recover = true;
2562 task_setup_data.flags |= RPC_TASK_TIMEOUT;
2564 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
2565 pnfs_lgopen_prepare(data, ctx);
2567 task = rpc_run_task(&task_setup_data);
2569 return PTR_ERR(task);
2570 status = rpc_wait_for_completion_task(task);
2572 data->cancelled = true;
2575 status = data->rpc_status;
2581 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2583 struct inode *dir = d_inode(data->dir);
2584 struct nfs_openres *o_res = &data->o_res;
2587 status = nfs4_run_open_task(data, NULL);
2588 if (status != 0 || !data->rpc_done)
2591 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2593 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
2594 status = _nfs4_proc_open_confirm(data);
2600 * Additional permission checks in order to distinguish between an
2601 * open for read, and an open for execute. This works around the
2602 * fact that NFSv4 OPEN treats read and execute permissions as being
2604 * Note that in the non-execute case, we want to turn off permission
2605 * checking if we just created a new file (POSIX open() semantics).
2607 static int nfs4_opendata_access(const struct cred *cred,
2608 struct nfs4_opendata *opendata,
2609 struct nfs4_state *state, fmode_t fmode,
2612 struct nfs_access_entry cache;
2615 /* access call failed or for some reason the server doesn't
2616 * support any access modes -- defer access call until later */
2617 if (opendata->o_res.access_supported == 0)
2622 * Use openflags to check for exec, because fmode won't
2623 * always have FMODE_EXEC set when file open for exec.
2625 if (openflags & __FMODE_EXEC) {
2626 /* ONLY check for exec rights */
2627 if (S_ISDIR(state->inode->i_mode))
2628 mask = NFS4_ACCESS_LOOKUP;
2630 mask = NFS4_ACCESS_EXECUTE;
2631 } else if ((fmode & FMODE_READ) && !opendata->file_created)
2632 mask = NFS4_ACCESS_READ;
2635 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2636 nfs_access_add_cache(state->inode, &cache);
2638 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2639 if ((mask & ~cache.mask & flags) == 0)
2646 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2648 static int _nfs4_proc_open(struct nfs4_opendata *data,
2649 struct nfs_open_context *ctx)
2651 struct inode *dir = d_inode(data->dir);
2652 struct nfs_server *server = NFS_SERVER(dir);
2653 struct nfs_openargs *o_arg = &data->o_arg;
2654 struct nfs_openres *o_res = &data->o_res;
2657 status = nfs4_run_open_task(data, ctx);
2658 if (!data->rpc_done)
2661 if (status == -NFS4ERR_BADNAME &&
2662 !(o_arg->open_flags & O_CREAT))
2667 nfs_fattr_map_and_free_names(server, &data->f_attr);
2669 if (o_arg->open_flags & O_CREAT) {
2670 if (o_arg->open_flags & O_EXCL)
2671 data->file_created = true;
2672 else if (o_res->cinfo.before != o_res->cinfo.after)
2673 data->file_created = true;
2674 if (data->file_created ||
2675 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
2676 nfs4_update_changeattr(dir, &o_res->cinfo,
2677 o_res->f_attr->time_start,
2678 NFS_INO_INVALID_DATA);
2680 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2681 server->caps &= ~NFS_CAP_POSIX_LOCK;
2682 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2683 status = _nfs4_proc_open_confirm(data);
2687 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2688 nfs4_sequence_free_slot(&o_res->seq_res);
2689 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr,
2690 o_res->f_label, NULL);
2697 * reclaim state on the server after a network partition.
2698 * Assumes caller holds the appropriate lock
2700 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2702 struct nfs4_opendata *opendata;
2705 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2706 NFS4_OPEN_CLAIM_FH);
2707 if (IS_ERR(opendata))
2708 return PTR_ERR(opendata);
2709 ret = nfs4_open_recover(opendata, state);
2711 d_drop(ctx->dentry);
2712 nfs4_opendata_put(opendata);
2716 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2718 struct nfs_server *server = NFS_SERVER(state->inode);
2719 struct nfs4_exception exception = { };
2723 err = _nfs4_open_expired(ctx, state);
2724 trace_nfs4_open_expired(ctx, 0, err);
2725 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2730 case -NFS4ERR_GRACE:
2731 case -NFS4ERR_DELAY:
2732 nfs4_handle_exception(server, err, &exception);
2735 } while (exception.retry);
2740 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2742 struct nfs_open_context *ctx;
2745 ctx = nfs4_state_find_open_context(state);
2748 ret = nfs4_do_open_expired(ctx, state);
2749 put_nfs_open_context(ctx);
2753 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2754 const nfs4_stateid *stateid)
2756 nfs_remove_bad_delegation(state->inode, stateid);
2757 nfs_state_clear_delegation(state);
2760 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2762 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2763 nfs_finish_clear_delegation_stateid(state, NULL);
2766 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2768 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2769 nfs40_clear_delegation_stateid(state);
2770 nfs_state_clear_open_state_flags(state);
2771 return nfs4_open_expired(sp, state);
2774 static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2775 nfs4_stateid *stateid,
2776 const struct cred *cred)
2778 return -NFS4ERR_BAD_STATEID;
2781 #if defined(CONFIG_NFS_V4_1)
2782 static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2783 nfs4_stateid *stateid,
2784 const struct cred *cred)
2788 switch (stateid->type) {
2791 case NFS4_INVALID_STATEID_TYPE:
2792 case NFS4_SPECIAL_STATEID_TYPE:
2793 return -NFS4ERR_BAD_STATEID;
2794 case NFS4_REVOKED_STATEID_TYPE:
2798 status = nfs41_test_stateid(server, stateid, cred);
2800 case -NFS4ERR_EXPIRED:
2801 case -NFS4ERR_ADMIN_REVOKED:
2802 case -NFS4ERR_DELEG_REVOKED:
2808 /* Ack the revoked state to the server */
2809 nfs41_free_stateid(server, stateid, cred, true);
2810 return -NFS4ERR_EXPIRED;
2813 static int nfs41_check_delegation_stateid(struct nfs4_state *state)
2815 struct nfs_server *server = NFS_SERVER(state->inode);
2816 nfs4_stateid stateid;
2817 struct nfs_delegation *delegation;
2818 const struct cred *cred = NULL;
2819 int status, ret = NFS_OK;
2821 /* Get the delegation credential for use by test/free_stateid */
2823 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2824 if (delegation == NULL) {
2826 nfs_state_clear_delegation(state);
2830 spin_lock(&delegation->lock);
2831 nfs4_stateid_copy(&stateid, &delegation->stateid);
2833 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2834 &delegation->flags)) {
2835 spin_unlock(&delegation->lock);
2840 if (delegation->cred)
2841 cred = get_cred(delegation->cred);
2842 spin_unlock(&delegation->lock);
2844 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
2845 trace_nfs4_test_delegation_stateid(state, NULL, status);
2846 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
2847 nfs_finish_clear_delegation_stateid(state, &stateid);
2855 static void nfs41_delegation_recover_stateid(struct nfs4_state *state)
2859 if (test_bit(NFS_DELEGATED_STATE, &state->flags) &&
2860 nfs4_copy_delegation_stateid(state->inode, state->state,
2862 nfs4_stateid_match_other(&state->stateid, &tmp))
2863 nfs_state_set_delegation(state, &tmp, state->state);
2865 nfs_state_clear_delegation(state);
2869 * nfs41_check_expired_locks - possibly free a lock stateid
2871 * @state: NFSv4 state for an inode
2873 * Returns NFS_OK if recovery for this stateid is now finished.
2874 * Otherwise a negative NFS4ERR value is returned.
2876 static int nfs41_check_expired_locks(struct nfs4_state *state)
2878 int status, ret = NFS_OK;
2879 struct nfs4_lock_state *lsp, *prev = NULL;
2880 struct nfs_server *server = NFS_SERVER(state->inode);
2882 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2885 spin_lock(&state->state_lock);
2886 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2887 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2888 const struct cred *cred = lsp->ls_state->owner->so_cred;
2890 refcount_inc(&lsp->ls_count);
2891 spin_unlock(&state->state_lock);
2893 nfs4_put_lock_state(prev);
2896 status = nfs41_test_and_free_expired_stateid(server,
2899 trace_nfs4_test_lock_stateid(state, lsp, status);
2900 if (status == -NFS4ERR_EXPIRED ||
2901 status == -NFS4ERR_BAD_STATEID) {
2902 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
2903 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
2904 if (!recover_lost_locks)
2905 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2906 } else if (status != NFS_OK) {
2908 nfs4_put_lock_state(prev);
2911 spin_lock(&state->state_lock);
2914 spin_unlock(&state->state_lock);
2915 nfs4_put_lock_state(prev);
2921 * nfs41_check_open_stateid - possibly free an open stateid
2923 * @state: NFSv4 state for an inode
2925 * Returns NFS_OK if recovery for this stateid is now finished.
2926 * Otherwise a negative NFS4ERR value is returned.
2928 static int nfs41_check_open_stateid(struct nfs4_state *state)
2930 struct nfs_server *server = NFS_SERVER(state->inode);
2931 nfs4_stateid *stateid = &state->open_stateid;
2932 const struct cred *cred = state->owner->so_cred;
2935 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0)
2936 return -NFS4ERR_BAD_STATEID;
2937 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
2938 trace_nfs4_test_open_stateid(state, NULL, status);
2939 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
2940 nfs_state_clear_open_state_flags(state);
2941 stateid->type = NFS4_INVALID_STATEID_TYPE;
2944 if (nfs_open_stateid_recover_openmode(state))
2945 return -NFS4ERR_OPENMODE;
2949 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2953 status = nfs41_check_delegation_stateid(state);
2954 if (status != NFS_OK)
2956 nfs41_delegation_recover_stateid(state);
2958 status = nfs41_check_expired_locks(state);
2959 if (status != NFS_OK)
2961 status = nfs41_check_open_stateid(state);
2962 if (status != NFS_OK)
2963 status = nfs4_open_expired(sp, state);
2969 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2970 * fields corresponding to attributes that were used to store the verifier.
2971 * Make sure we clobber those fields in the later setattr call
2973 static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2974 struct iattr *sattr, struct nfs4_label **label)
2976 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2981 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
2982 attrset[i] = opendata->o_res.attrset[i];
2983 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
2984 attrset[i] &= ~bitmask[i];
2987 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
2988 sattr->ia_valid : 0;
2990 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
2991 if (sattr->ia_valid & ATTR_ATIME_SET)
2992 ret |= ATTR_ATIME_SET;
2997 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
2998 if (sattr->ia_valid & ATTR_MTIME_SET)
2999 ret |= ATTR_MTIME_SET;
3004 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
3009 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
3010 int flags, struct nfs_open_context *ctx)
3012 struct nfs4_state_owner *sp = opendata->owner;
3013 struct nfs_server *server = sp->so_server;
3014 struct dentry *dentry;
3015 struct nfs4_state *state;
3016 fmode_t acc_mode = _nfs4_ctx_to_accessmode(ctx);
3017 struct inode *dir = d_inode(opendata->dir);
3018 unsigned long dir_verifier;
3022 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
3023 dir_verifier = nfs_save_change_attribute(dir);
3025 ret = _nfs4_proc_open(opendata, ctx);
3029 state = _nfs4_opendata_to_nfs4_state(opendata);
3030 ret = PTR_ERR(state);
3034 if (server->caps & NFS_CAP_POSIX_LOCK)
3035 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
3036 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
3037 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
3039 dentry = opendata->dentry;
3040 if (d_really_is_negative(dentry)) {
3041 struct dentry *alias;
3043 alias = d_exact_alias(dentry, state->inode);
3045 alias = d_splice_alias(igrab(state->inode), dentry);
3046 /* d_splice_alias() can't fail here - it's a non-directory */
3049 ctx->dentry = dentry = alias;
3053 switch(opendata->o_arg.claim) {
3056 case NFS4_OPEN_CLAIM_NULL:
3057 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
3058 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
3059 if (!opendata->rpc_done)
3061 if (opendata->o_res.delegation_type != 0)
3062 dir_verifier = nfs_save_change_attribute(dir);
3063 nfs_set_verifier(dentry, dir_verifier);
3066 /* Parse layoutget results before we check for access */
3067 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
3069 ret = nfs4_opendata_access(sp->so_cred, opendata, state,
3074 if (d_inode(dentry) == state->inode) {
3075 nfs_inode_attach_open_context(ctx);
3076 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
3077 nfs4_schedule_stateid_recovery(server, state);
3081 if (!opendata->cancelled)
3082 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
3087 * Returns a referenced nfs4_state
3089 static int _nfs4_do_open(struct inode *dir,
3090 struct nfs_open_context *ctx,
3092 const struct nfs4_open_createattrs *c,
3095 struct nfs4_state_owner *sp;
3096 struct nfs4_state *state = NULL;
3097 struct nfs_server *server = NFS_SERVER(dir);
3098 struct nfs4_opendata *opendata;
3099 struct dentry *dentry = ctx->dentry;
3100 const struct cred *cred = ctx->cred;
3101 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
3102 fmode_t fmode = _nfs4_ctx_to_openmode(ctx);
3103 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
3104 struct iattr *sattr = c->sattr;
3105 struct nfs4_label *label = c->label;
3106 struct nfs4_label *olabel = NULL;
3109 /* Protect against reboot recovery conflicts */
3111 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
3113 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
3116 status = nfs4_client_recover_expired_lease(server->nfs_client);
3118 goto err_put_state_owner;
3119 if (d_really_is_positive(dentry))
3120 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
3122 if (d_really_is_positive(dentry))
3123 claim = NFS4_OPEN_CLAIM_FH;
3124 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
3125 c, claim, GFP_KERNEL);
3126 if (opendata == NULL)
3127 goto err_put_state_owner;
3130 olabel = nfs4_label_alloc(server, GFP_KERNEL);
3131 if (IS_ERR(olabel)) {
3132 status = PTR_ERR(olabel);
3133 goto err_opendata_put;
3137 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
3138 if (!opendata->f_attr.mdsthreshold) {
3139 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
3140 if (!opendata->f_attr.mdsthreshold)
3141 goto err_free_label;
3143 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
3145 if (d_really_is_positive(dentry))
3146 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
3148 status = _nfs4_open_and_get_state(opendata, flags, ctx);
3150 goto err_free_label;
3153 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
3154 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
3155 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
3157 * send create attributes which was not set by open
3158 * with an extra setattr.
3160 if (attrs || label) {
3161 unsigned ia_old = sattr->ia_valid;
3163 sattr->ia_valid = attrs;
3164 nfs_fattr_init(opendata->o_res.f_attr);
3165 status = nfs4_do_setattr(state->inode, cred,
3166 opendata->o_res.f_attr, sattr,
3167 ctx, label, olabel);
3169 nfs_setattr_update_inode(state->inode, sattr,
3170 opendata->o_res.f_attr);
3171 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
3173 sattr->ia_valid = ia_old;
3176 if (opened && opendata->file_created)
3179 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
3180 *ctx_th = opendata->f_attr.mdsthreshold;
3181 opendata->f_attr.mdsthreshold = NULL;
3184 nfs4_label_free(olabel);
3186 nfs4_opendata_put(opendata);
3187 nfs4_put_state_owner(sp);
3190 nfs4_label_free(olabel);
3192 nfs4_opendata_put(opendata);
3193 err_put_state_owner:
3194 nfs4_put_state_owner(sp);
3200 static struct nfs4_state *nfs4_do_open(struct inode *dir,
3201 struct nfs_open_context *ctx,
3203 struct iattr *sattr,
3204 struct nfs4_label *label,
3207 struct nfs_server *server = NFS_SERVER(dir);
3208 struct nfs4_exception exception = {
3209 .interruptible = true,
3211 struct nfs4_state *res;
3212 struct nfs4_open_createattrs c = {
3216 [0] = (__u32)jiffies,
3217 [1] = (__u32)current->pid,
3223 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
3225 trace_nfs4_open_file(ctx, flags, status);
3228 /* NOTE: BAD_SEQID means the server and client disagree about the
3229 * book-keeping w.r.t. state-changing operations
3230 * (OPEN/CLOSE/LOCK/LOCKU...)
3231 * It is actually a sign of a bug on the client or on the server.
3233 * If we receive a BAD_SEQID error in the particular case of
3234 * doing an OPEN, we assume that nfs_increment_open_seqid() will
3235 * have unhashed the old state_owner for us, and that we can
3236 * therefore safely retry using a new one. We should still warn
3237 * the user though...
3239 if (status == -NFS4ERR_BAD_SEQID) {
3240 pr_warn_ratelimited("NFS: v4 server %s "
3241 " returned a bad sequence-id error!\n",
3242 NFS_SERVER(dir)->nfs_client->cl_hostname);
3243 exception.retry = 1;
3247 * BAD_STATEID on OPEN means that the server cancelled our
3248 * state before it received the OPEN_CONFIRM.
3249 * Recover by retrying the request as per the discussion
3250 * on Page 181 of RFC3530.
3252 if (status == -NFS4ERR_BAD_STATEID) {
3253 exception.retry = 1;
3256 if (status == -NFS4ERR_EXPIRED) {
3257 nfs4_schedule_lease_recovery(server->nfs_client);
3258 exception.retry = 1;
3261 if (status == -EAGAIN) {
3262 /* We must have found a delegation */
3263 exception.retry = 1;
3266 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3268 res = ERR_PTR(nfs4_handle_exception(server,
3269 status, &exception));
3270 } while (exception.retry);
3274 static int _nfs4_do_setattr(struct inode *inode,
3275 struct nfs_setattrargs *arg,
3276 struct nfs_setattrres *res,
3277 const struct cred *cred,
3278 struct nfs_open_context *ctx)
3280 struct nfs_server *server = NFS_SERVER(inode);
3281 struct rpc_message msg = {
3282 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3287 const struct cred *delegation_cred = NULL;
3288 unsigned long timestamp = jiffies;
3292 nfs_fattr_init(res->fattr);
3294 /* Servers should only apply open mode checks for file size changes */
3295 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
3299 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
3300 /* Use that stateid */
3301 } else if (ctx != NULL && ctx->state) {
3302 struct nfs_lock_context *l_ctx;
3303 if (!nfs4_valid_open_stateid(ctx->state))
3305 l_ctx = nfs_get_lock_context(ctx);
3307 return PTR_ERR(l_ctx);
3308 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3309 &arg->stateid, &delegation_cred);
3310 nfs_put_lock_context(l_ctx);
3313 else if (status == -EAGAIN)
3317 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
3319 if (delegation_cred)
3320 msg.rpc_cred = delegation_cred;
3322 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3324 put_cred(delegation_cred);
3325 if (status == 0 && ctx != NULL)
3326 renew_lease(server, timestamp);
3327 trace_nfs4_setattr(inode, &arg->stateid, status);
3331 static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
3332 struct nfs_fattr *fattr, struct iattr *sattr,
3333 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
3334 struct nfs4_label *olabel)
3336 struct nfs_server *server = NFS_SERVER(inode);
3337 __u32 bitmask[NFS4_BITMASK_SZ];
3338 struct nfs4_state *state = ctx ? ctx->state : NULL;
3339 struct nfs_setattrargs arg = {
3340 .fh = NFS_FH(inode),
3346 struct nfs_setattrres res = {
3351 struct nfs4_exception exception = {
3354 .stateid = &arg.stateid,
3359 nfs4_bitmap_copy_adjust_setattr(bitmask,
3360 nfs4_bitmask(server, olabel),
3363 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
3365 case -NFS4ERR_OPENMODE:
3366 if (!(sattr->ia_valid & ATTR_SIZE)) {
3367 pr_warn_once("NFSv4: server %s is incorrectly "
3368 "applying open mode checks to "
3369 "a SETATTR that is not "
3370 "changing file size.\n",
3371 server->nfs_client->cl_hostname);
3373 if (state && !(state->state & FMODE_WRITE)) {
3375 if (sattr->ia_valid & ATTR_OPEN)
3380 err = nfs4_handle_exception(server, err, &exception);
3381 } while (exception.retry);
3387 nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3389 if (inode == NULL || !nfs_have_layout(inode))
3392 return pnfs_wait_on_layoutreturn(inode, task);
3396 * Update the seqid of an open stateid
3398 static void nfs4_sync_open_stateid(nfs4_stateid *dst,
3399 struct nfs4_state *state)
3406 if (!nfs4_valid_open_stateid(state))
3408 seq = read_seqbegin(&state->seqlock);
3409 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3410 nfs4_stateid_copy(dst, &state->open_stateid);
3411 if (read_seqretry(&state->seqlock, seq))
3415 seqid_open = state->open_stateid.seqid;
3416 if (read_seqretry(&state->seqlock, seq))
3419 dst_seqid = be32_to_cpu(dst->seqid);
3420 if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) < 0)
3421 dst->seqid = seqid_open;
3427 * Update the seqid of an open stateid after receiving
3428 * NFS4ERR_OLD_STATEID
3430 static bool nfs4_refresh_open_old_stateid(nfs4_stateid *dst,
3431 struct nfs4_state *state)
3440 if (!nfs4_valid_open_stateid(state))
3442 seq = read_seqbegin(&state->seqlock);
3443 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3444 if (read_seqretry(&state->seqlock, seq))
3448 seqid_open = state->open_stateid.seqid;
3449 if (read_seqretry(&state->seqlock, seq))
3452 dst_seqid = be32_to_cpu(dst->seqid);
3453 if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) >= 0)
3454 dst->seqid = cpu_to_be32(dst_seqid + 1);
3456 dst->seqid = seqid_open;
3464 struct nfs4_closedata {
3465 struct inode *inode;
3466 struct nfs4_state *state;
3467 struct nfs_closeargs arg;
3468 struct nfs_closeres res;
3470 struct nfs4_layoutreturn_args arg;
3471 struct nfs4_layoutreturn_res res;
3472 struct nfs4_xdr_opaque_data ld_private;
3476 struct nfs_fattr fattr;
3477 unsigned long timestamp;
3480 static void nfs4_free_closedata(void *data)
3482 struct nfs4_closedata *calldata = data;
3483 struct nfs4_state_owner *sp = calldata->state->owner;
3484 struct super_block *sb = calldata->state->inode->i_sb;
3486 if (calldata->lr.roc)
3487 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3488 calldata->res.lr_ret);
3489 nfs4_put_open_state(calldata->state);
3490 nfs_free_seqid(calldata->arg.seqid);
3491 nfs4_put_state_owner(sp);
3492 nfs_sb_deactive(sb);
3496 static void nfs4_close_done(struct rpc_task *task, void *data)
3498 struct nfs4_closedata *calldata = data;
3499 struct nfs4_state *state = calldata->state;
3500 struct nfs_server *server = NFS_SERVER(calldata->inode);
3501 nfs4_stateid *res_stateid = NULL;
3502 struct nfs4_exception exception = {
3504 .inode = calldata->inode,
3505 .stateid = &calldata->arg.stateid,
3508 dprintk("%s: begin!\n", __func__);
3509 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3511 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
3513 /* Handle Layoutreturn errors */
3514 if (pnfs_roc_done(task, calldata->inode,
3515 &calldata->arg.lr_args,
3516 &calldata->res.lr_res,
3517 &calldata->res.lr_ret) == -EAGAIN)
3520 /* hmm. we are done with the inode, and in the process of freeing
3521 * the state_owner. we keep this around to process errors
3523 switch (task->tk_status) {
3525 res_stateid = &calldata->res.stateid;
3526 renew_lease(server, calldata->timestamp);
3528 case -NFS4ERR_ACCESS:
3529 if (calldata->arg.bitmask != NULL) {
3530 calldata->arg.bitmask = NULL;
3531 calldata->res.fattr = NULL;
3536 case -NFS4ERR_OLD_STATEID:
3537 /* Did we race with OPEN? */
3538 if (nfs4_refresh_open_old_stateid(&calldata->arg.stateid,
3542 case -NFS4ERR_ADMIN_REVOKED:
3543 case -NFS4ERR_STALE_STATEID:
3544 case -NFS4ERR_EXPIRED:
3545 nfs4_free_revoked_stateid(server,
3546 &calldata->arg.stateid,
3547 task->tk_msg.rpc_cred);
3549 case -NFS4ERR_BAD_STATEID:
3550 if (calldata->arg.fmode == 0)
3554 task->tk_status = nfs4_async_handle_exception(task,
3555 server, task->tk_status, &exception);
3556 if (exception.retry)
3559 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3560 res_stateid, calldata->arg.fmode);
3562 task->tk_status = 0;
3563 nfs_release_seqid(calldata->arg.seqid);
3564 nfs_refresh_inode(calldata->inode, &calldata->fattr);
3565 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
3568 task->tk_status = 0;
3569 rpc_restart_call_prepare(task);
3573 static void nfs4_close_prepare(struct rpc_task *task, void *data)
3575 struct nfs4_closedata *calldata = data;
3576 struct nfs4_state *state = calldata->state;
3577 struct inode *inode = calldata->inode;
3578 struct pnfs_layout_hdr *lo;
3579 bool is_rdonly, is_wronly, is_rdwr;
3582 dprintk("%s: begin!\n", __func__);
3583 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3586 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
3587 spin_lock(&state->owner->so_lock);
3588 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3589 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3590 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
3591 /* Calculate the change in open mode */
3592 calldata->arg.fmode = 0;
3593 if (state->n_rdwr == 0) {
3594 if (state->n_rdonly == 0)
3595 call_close |= is_rdonly;
3597 calldata->arg.fmode |= FMODE_READ;
3598 if (state->n_wronly == 0)
3599 call_close |= is_wronly;
3601 calldata->arg.fmode |= FMODE_WRITE;
3602 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3603 call_close |= is_rdwr;
3605 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3607 nfs4_sync_open_stateid(&calldata->arg.stateid, state);
3608 if (!nfs4_valid_open_stateid(state))
3610 spin_unlock(&state->owner->so_lock);
3613 /* Note: exit _without_ calling nfs4_close_done */
3617 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
3618 nfs_release_seqid(calldata->arg.seqid);
3622 lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3623 if (lo && !pnfs_layout_is_valid(lo)) {
3624 calldata->arg.lr_args = NULL;
3625 calldata->res.lr_res = NULL;
3628 if (calldata->arg.fmode == 0)
3629 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
3631 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
3632 /* Close-to-open cache consistency revalidation */
3633 if (!nfs4_have_delegation(inode, FMODE_READ))
3634 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3636 calldata->arg.bitmask = NULL;
3639 calldata->arg.share_access =
3640 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3641 calldata->arg.fmode, 0);
3643 if (calldata->res.fattr == NULL)
3644 calldata->arg.bitmask = NULL;
3645 else if (calldata->arg.bitmask == NULL)
3646 calldata->res.fattr = NULL;
3647 calldata->timestamp = jiffies;
3648 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
3649 &calldata->arg.seq_args,
3650 &calldata->res.seq_res,
3652 nfs_release_seqid(calldata->arg.seqid);
3653 dprintk("%s: done!\n", __func__);
3656 task->tk_action = NULL;
3658 nfs4_sequence_done(task, &calldata->res.seq_res);
3661 static const struct rpc_call_ops nfs4_close_ops = {
3662 .rpc_call_prepare = nfs4_close_prepare,
3663 .rpc_call_done = nfs4_close_done,
3664 .rpc_release = nfs4_free_closedata,
3668 * It is possible for data to be read/written from a mem-mapped file
3669 * after the sys_close call (which hits the vfs layer as a flush).
3670 * This means that we can't safely call nfsv4 close on a file until
3671 * the inode is cleared. This in turn means that we are not good
3672 * NFSv4 citizens - we do not indicate to the server to update the file's
3673 * share state even when we are done with one of the three share
3674 * stateid's in the inode.
3676 * NOTE: Caller must be holding the sp->so_owner semaphore!
3678 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
3680 struct nfs_server *server = NFS_SERVER(state->inode);
3681 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
3682 struct nfs4_closedata *calldata;
3683 struct nfs4_state_owner *sp = state->owner;
3684 struct rpc_task *task;
3685 struct rpc_message msg = {
3686 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3687 .rpc_cred = state->owner->so_cred,
3689 struct rpc_task_setup task_setup_data = {
3690 .rpc_client = server->client,
3691 .rpc_message = &msg,
3692 .callback_ops = &nfs4_close_ops,
3693 .workqueue = nfsiod_workqueue,
3694 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
3696 int status = -ENOMEM;
3698 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3699 &task_setup_data.rpc_client, &msg);
3701 calldata = kzalloc(sizeof(*calldata), gfp_mask);
3702 if (calldata == NULL)
3704 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
3705 calldata->inode = state->inode;
3706 calldata->state = state;
3707 calldata->arg.fh = NFS_FH(state->inode);
3708 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3709 goto out_free_calldata;
3710 /* Serialization for the sequence id */
3711 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3712 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
3713 if (IS_ERR(calldata->arg.seqid))
3714 goto out_free_calldata;
3715 nfs_fattr_init(&calldata->fattr);
3716 calldata->arg.fmode = 0;
3717 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
3718 calldata->res.fattr = &calldata->fattr;
3719 calldata->res.seqid = calldata->arg.seqid;
3720 calldata->res.server = server;
3721 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3722 calldata->lr.roc = pnfs_roc(state->inode,
3723 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3724 if (calldata->lr.roc) {
3725 calldata->arg.lr_args = &calldata->lr.arg;
3726 calldata->res.lr_res = &calldata->lr.res;
3728 nfs_sb_active(calldata->inode->i_sb);
3730 msg.rpc_argp = &calldata->arg;
3731 msg.rpc_resp = &calldata->res;
3732 task_setup_data.callback_data = calldata;
3733 task = rpc_run_task(&task_setup_data);
3735 return PTR_ERR(task);
3738 status = rpc_wait_for_completion_task(task);
3744 nfs4_put_open_state(state);
3745 nfs4_put_state_owner(sp);
3749 static struct inode *
3750 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3751 int open_flags, struct iattr *attr, int *opened)
3753 struct nfs4_state *state;
3754 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3756 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
3758 /* Protect against concurrent sillydeletes */
3759 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
3761 nfs4_label_release_security(label);
3764 return ERR_CAST(state);
3765 return state->inode;
3768 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
3770 if (ctx->state == NULL)
3773 nfs4_close_sync(ctx->state, _nfs4_ctx_to_openmode(ctx));
3775 nfs4_close_state(ctx->state, _nfs4_ctx_to_openmode(ctx));
3778 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3779 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3780 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_XATTR_SUPPORT - 1UL)
3782 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3784 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
3785 struct nfs4_server_caps_arg args = {
3789 struct nfs4_server_caps_res res = {};
3790 struct rpc_message msg = {
3791 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
3798 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3799 FATTR4_WORD0_FH_EXPIRE_TYPE |
3800 FATTR4_WORD0_LINK_SUPPORT |
3801 FATTR4_WORD0_SYMLINK_SUPPORT |
3802 FATTR4_WORD0_ACLSUPPORT;
3804 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3806 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3808 /* Sanity check the server answers */
3809 switch (minorversion) {
3811 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3812 res.attr_bitmask[2] = 0;
3815 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3818 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3820 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
3821 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3822 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3823 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3824 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
3825 NFS_CAP_CTIME|NFS_CAP_MTIME|
3826 NFS_CAP_SECURITY_LABEL);
3827 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3828 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3829 server->caps |= NFS_CAP_ACLS;
3830 if (res.has_links != 0)
3831 server->caps |= NFS_CAP_HARDLINKS;
3832 if (res.has_symlinks != 0)
3833 server->caps |= NFS_CAP_SYMLINKS;
3834 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3835 server->caps |= NFS_CAP_FILEID;
3836 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3837 server->caps |= NFS_CAP_MODE;
3838 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3839 server->caps |= NFS_CAP_NLINK;
3840 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3841 server->caps |= NFS_CAP_OWNER;
3842 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3843 server->caps |= NFS_CAP_OWNER_GROUP;
3844 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3845 server->caps |= NFS_CAP_ATIME;
3846 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3847 server->caps |= NFS_CAP_CTIME;
3848 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3849 server->caps |= NFS_CAP_MTIME;
3850 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3851 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3852 server->caps |= NFS_CAP_SECURITY_LABEL;
3854 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3855 sizeof(server->attr_bitmask));
3856 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3858 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3859 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3860 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
3861 server->cache_consistency_bitmask[2] = 0;
3863 /* Avoid a regression due to buggy server */
3864 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3865 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
3866 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3867 sizeof(server->exclcreat_bitmask));
3869 server->acl_bitmask = res.acl_bitmask;
3870 server->fh_expire_type = res.fh_expire_type;
3876 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3878 struct nfs4_exception exception = {
3879 .interruptible = true,
3883 err = nfs4_handle_exception(server,
3884 _nfs4_server_capabilities(server, fhandle),
3886 } while (exception.retry);
3890 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3891 struct nfs_fsinfo *info)
3894 struct nfs4_lookup_root_arg args = {
3897 struct nfs4_lookup_res res = {
3899 .fattr = info->fattr,
3902 struct rpc_message msg = {
3903 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3908 bitmask[0] = nfs4_fattr_bitmap[0];
3909 bitmask[1] = nfs4_fattr_bitmap[1];
3911 * Process the label in the upcoming getfattr
3913 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3915 nfs_fattr_init(info->fattr);
3916 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3919 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3920 struct nfs_fsinfo *info)
3922 struct nfs4_exception exception = {
3923 .interruptible = true,
3927 err = _nfs4_lookup_root(server, fhandle, info);
3928 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
3931 case -NFS4ERR_WRONGSEC:
3934 err = nfs4_handle_exception(server, err, &exception);
3936 } while (exception.retry);
3941 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3942 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3944 struct rpc_auth_create_args auth_args = {
3945 .pseudoflavor = flavor,
3947 struct rpc_auth *auth;
3949 auth = rpcauth_create(&auth_args, server->client);
3952 return nfs4_lookup_root(server, fhandle, info);
3956 * Retry pseudoroot lookup with various security flavors. We do this when:
3958 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3959 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3961 * Returns zero on success, or a negative NFS4ERR value, or a
3962 * negative errno value.
3964 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3965 struct nfs_fsinfo *info)
3967 /* Per 3530bis 15.33.5 */
3968 static const rpc_authflavor_t flav_array[] = {
3972 RPC_AUTH_UNIX, /* courtesy */
3975 int status = -EPERM;
3978 if (server->auth_info.flavor_len > 0) {
3979 /* try each flavor specified by user */
3980 for (i = 0; i < server->auth_info.flavor_len; i++) {
3981 status = nfs4_lookup_root_sec(server, fhandle, info,
3982 server->auth_info.flavors[i]);
3983 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3988 /* no flavors specified by user, try default list */
3989 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3990 status = nfs4_lookup_root_sec(server, fhandle, info,
3992 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3999 * -EACCES could mean that the user doesn't have correct permissions
4000 * to access the mount. It could also mean that we tried to mount
4001 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
4002 * existing mount programs don't handle -EACCES very well so it should
4003 * be mapped to -EPERM instead.
4005 if (status == -EACCES)
4011 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
4012 * @server: initialized nfs_server handle
4013 * @fhandle: we fill in the pseudo-fs root file handle
4014 * @info: we fill in an FSINFO struct
4015 * @auth_probe: probe the auth flavours
4017 * Returns zero on success, or a negative errno.
4019 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
4020 struct nfs_fsinfo *info,
4026 status = nfs4_lookup_root(server, fhandle, info);
4028 if (auth_probe || status == NFS4ERR_WRONGSEC)
4029 status = server->nfs_client->cl_mvops->find_root_sec(server,
4033 status = nfs4_server_capabilities(server, fhandle);
4035 status = nfs4_do_fsinfo(server, fhandle, info);
4037 return nfs4_map_errors(status);
4040 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
4041 struct nfs_fsinfo *info)
4044 struct nfs_fattr *fattr = info->fattr;
4045 struct nfs4_label *label = fattr->label;
4047 error = nfs4_server_capabilities(server, mntfh);
4049 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
4053 error = nfs4_proc_getattr(server, mntfh, fattr, label, NULL);
4055 dprintk("nfs4_get_root: getattr error = %d\n", -error);
4059 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
4060 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
4061 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
4068 * Get locations and (maybe) other attributes of a referral.
4069 * Note that we'll actually follow the referral later when
4070 * we detect fsid mismatch in inode revalidation
4072 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
4073 const struct qstr *name, struct nfs_fattr *fattr,
4074 struct nfs_fh *fhandle)
4076 int status = -ENOMEM;
4077 struct page *page = NULL;
4078 struct nfs4_fs_locations *locations = NULL;
4080 page = alloc_page(GFP_KERNEL);
4083 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4084 if (locations == NULL)
4087 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
4092 * If the fsid didn't change, this is a migration event, not a
4093 * referral. Cause us to drop into the exception handler, which
4094 * will kick off migration recovery.
4096 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
4097 dprintk("%s: server did not return a different fsid for"
4098 " a referral at %s\n", __func__, name->name);
4099 status = -NFS4ERR_MOVED;
4102 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
4103 nfs_fixup_referral_attributes(&locations->fattr);
4105 /* replace the lookup nfs_fattr with the locations nfs_fattr */
4106 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
4107 memset(fhandle, 0, sizeof(struct nfs_fh));
4115 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4116 struct nfs_fattr *fattr, struct nfs4_label *label,
4117 struct inode *inode)
4119 __u32 bitmask[NFS4_BITMASK_SZ];
4120 struct nfs4_getattr_arg args = {
4124 struct nfs4_getattr_res res = {
4129 struct rpc_message msg = {
4130 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4134 unsigned short task_flags = 0;
4136 /* Is this is an attribute revalidation, subject to softreval? */
4137 if (inode && (server->flags & NFS_MOUNT_SOFTREVAL))
4138 task_flags |= RPC_TASK_TIMEOUT;
4140 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode);
4142 nfs_fattr_init(fattr);
4143 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4144 return nfs4_do_call_sync(server->client, server, &msg,
4145 &args.seq_args, &res.seq_res, task_flags);
4148 int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4149 struct nfs_fattr *fattr, struct nfs4_label *label,
4150 struct inode *inode)
4152 struct nfs4_exception exception = {
4153 .interruptible = true,
4157 err = _nfs4_proc_getattr(server, fhandle, fattr, label, inode);
4158 trace_nfs4_getattr(server, fhandle, fattr, err);
4159 err = nfs4_handle_exception(server, err,
4161 } while (exception.retry);
4166 * The file is not closed if it is opened due to the a request to change
4167 * the size of the file. The open call will not be needed once the
4168 * VFS layer lookup-intents are implemented.
4170 * Close is called when the inode is destroyed.
4171 * If we haven't opened the file for O_WRONLY, we
4172 * need to in the size_change case to obtain a stateid.
4175 * Because OPEN is always done by name in nfsv4, it is
4176 * possible that we opened a different file by the same
4177 * name. We can recognize this race condition, but we
4178 * can't do anything about it besides returning an error.
4180 * This will be fixed with VFS changes (lookup-intent).
4183 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
4184 struct iattr *sattr)
4186 struct inode *inode = d_inode(dentry);
4187 const struct cred *cred = NULL;
4188 struct nfs_open_context *ctx = NULL;
4189 struct nfs4_label *label = NULL;
4192 if (pnfs_ld_layoutret_on_setattr(inode) &&
4193 sattr->ia_valid & ATTR_SIZE &&
4194 sattr->ia_size < i_size_read(inode))
4195 pnfs_commit_and_return_layout(inode);
4197 nfs_fattr_init(fattr);
4199 /* Deal with open(O_TRUNC) */
4200 if (sattr->ia_valid & ATTR_OPEN)
4201 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
4203 /* Optimization: if the end result is no change, don't RPC */
4204 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
4207 /* Search for an existing open(O_WRITE) file */
4208 if (sattr->ia_valid & ATTR_FILE) {
4210 ctx = nfs_file_open_context(sattr->ia_file);
4215 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4217 return PTR_ERR(label);
4219 /* Return any delegations if we're going to change ACLs */
4220 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
4221 nfs4_inode_make_writeable(inode);
4223 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
4225 nfs_setattr_update_inode(inode, sattr, fattr);
4226 nfs_setsecurity(inode, fattr, label);
4228 nfs4_label_free(label);
4232 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
4233 struct dentry *dentry, struct nfs_fh *fhandle,
4234 struct nfs_fattr *fattr, struct nfs4_label *label)
4236 struct nfs_server *server = NFS_SERVER(dir);
4238 struct nfs4_lookup_arg args = {
4239 .bitmask = server->attr_bitmask,
4240 .dir_fh = NFS_FH(dir),
4241 .name = &dentry->d_name,
4243 struct nfs4_lookup_res res = {
4249 struct rpc_message msg = {
4250 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4254 unsigned short task_flags = 0;
4256 /* Is this is an attribute revalidation, subject to softreval? */
4257 if (nfs_lookup_is_soft_revalidate(dentry))
4258 task_flags |= RPC_TASK_TIMEOUT;
4260 args.bitmask = nfs4_bitmask(server, label);
4262 nfs_fattr_init(fattr);
4264 dprintk("NFS call lookup %pd2\n", dentry);
4265 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4266 status = nfs4_do_call_sync(clnt, server, &msg,
4267 &args.seq_args, &res.seq_res, task_flags);
4268 dprintk("NFS reply lookup: %d\n", status);
4272 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
4274 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4275 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
4276 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4280 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
4281 struct dentry *dentry, struct nfs_fh *fhandle,
4282 struct nfs_fattr *fattr, struct nfs4_label *label)
4284 struct nfs4_exception exception = {
4285 .interruptible = true,
4287 struct rpc_clnt *client = *clnt;
4288 const struct qstr *name = &dentry->d_name;
4291 err = _nfs4_proc_lookup(client, dir, dentry, fhandle, fattr, label);
4292 trace_nfs4_lookup(dir, name, err);
4294 case -NFS4ERR_BADNAME:
4297 case -NFS4ERR_MOVED:
4298 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
4299 if (err == -NFS4ERR_MOVED)
4300 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4302 case -NFS4ERR_WRONGSEC:
4304 if (client != *clnt)
4306 client = nfs4_negotiate_security(client, dir, name);
4308 return PTR_ERR(client);
4310 exception.retry = 1;
4313 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4315 } while (exception.retry);
4320 else if (client != *clnt)
4321 rpc_shutdown_client(client);
4326 static int nfs4_proc_lookup(struct inode *dir, struct dentry *dentry,
4327 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4328 struct nfs4_label *label)
4331 struct rpc_clnt *client = NFS_CLIENT(dir);
4333 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr, label);
4334 if (client != NFS_CLIENT(dir)) {
4335 rpc_shutdown_client(client);
4336 nfs_fixup_secinfo_attributes(fattr);
4342 nfs4_proc_lookup_mountpoint(struct inode *dir, struct dentry *dentry,
4343 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4345 struct rpc_clnt *client = NFS_CLIENT(dir);
4348 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr, NULL);
4350 return ERR_PTR(status);
4351 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
4354 static int _nfs4_proc_lookupp(struct inode *inode,
4355 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4356 struct nfs4_label *label)
4358 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4359 struct nfs_server *server = NFS_SERVER(inode);
4361 struct nfs4_lookupp_arg args = {
4362 .bitmask = server->attr_bitmask,
4363 .fh = NFS_FH(inode),
4365 struct nfs4_lookupp_res res = {
4371 struct rpc_message msg = {
4372 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4377 args.bitmask = nfs4_bitmask(server, label);
4379 nfs_fattr_init(fattr);
4381 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4382 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4384 dprintk("NFS reply lookupp: %d\n", status);
4388 static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4389 struct nfs_fattr *fattr, struct nfs4_label *label)
4391 struct nfs4_exception exception = {
4392 .interruptible = true,
4396 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4397 trace_nfs4_lookupp(inode, err);
4398 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4400 } while (exception.retry);
4404 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4406 struct nfs_server *server = NFS_SERVER(inode);
4407 struct nfs4_accessargs args = {
4408 .fh = NFS_FH(inode),
4409 .access = entry->mask,
4411 struct nfs4_accessres res = {
4414 struct rpc_message msg = {
4415 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4418 .rpc_cred = entry->cred,
4422 if (!nfs4_have_delegation(inode, FMODE_READ)) {
4423 res.fattr = nfs_alloc_fattr();
4424 if (res.fattr == NULL)
4426 args.bitmask = server->cache_consistency_bitmask;
4428 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4430 nfs_access_set_mask(entry, res.access);
4432 nfs_refresh_inode(inode, res.fattr);
4434 nfs_free_fattr(res.fattr);
4438 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4440 struct nfs4_exception exception = {
4441 .interruptible = true,
4445 err = _nfs4_proc_access(inode, entry);
4446 trace_nfs4_access(inode, err);
4447 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4449 } while (exception.retry);
4454 * TODO: For the time being, we don't try to get any attributes
4455 * along with any of the zero-copy operations READ, READDIR,
4458 * In the case of the first three, we want to put the GETATTR
4459 * after the read-type operation -- this is because it is hard
4460 * to predict the length of a GETATTR response in v4, and thus
4461 * align the READ data correctly. This means that the GETATTR
4462 * may end up partially falling into the page cache, and we should
4463 * shift it into the 'tail' of the xdr_buf before processing.
4464 * To do this efficiently, we need to know the total length
4465 * of data received, which doesn't seem to be available outside
4468 * In the case of WRITE, we also want to put the GETATTR after
4469 * the operation -- in this case because we want to make sure
4470 * we get the post-operation mtime and size.
4472 * Both of these changes to the XDR layer would in fact be quite
4473 * minor, but I decided to leave them for a subsequent patch.
4475 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4476 unsigned int pgbase, unsigned int pglen)
4478 struct nfs4_readlink args = {
4479 .fh = NFS_FH(inode),
4484 struct nfs4_readlink_res res;
4485 struct rpc_message msg = {
4486 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4491 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
4494 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4495 unsigned int pgbase, unsigned int pglen)
4497 struct nfs4_exception exception = {
4498 .interruptible = true,
4502 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4503 trace_nfs4_readlink(inode, err);
4504 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4506 } while (exception.retry);
4511 * This is just for mknod. open(O_CREAT) will always do ->open_context().
4514 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
4517 struct nfs_server *server = NFS_SERVER(dir);
4518 struct nfs4_label l, *ilabel = NULL;
4519 struct nfs_open_context *ctx;
4520 struct nfs4_state *state;
4523 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
4525 return PTR_ERR(ctx);
4527 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4529 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4530 sattr->ia_mode &= ~current_umask();
4531 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
4532 if (IS_ERR(state)) {
4533 status = PTR_ERR(state);
4537 nfs4_label_release_security(ilabel);
4538 put_nfs_open_context(ctx);
4543 _nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
4545 struct nfs_server *server = NFS_SERVER(dir);
4546 struct nfs_removeargs args = {
4550 struct nfs_removeres res = {
4553 struct rpc_message msg = {
4554 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4558 unsigned long timestamp = jiffies;
4561 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
4563 spin_lock(&dir->i_lock);
4564 nfs4_update_changeattr_locked(dir, &res.cinfo, timestamp,
4565 NFS_INO_INVALID_DATA);
4566 /* Removing a directory decrements nlink in the parent */
4567 if (ftype == NF4DIR && dir->i_nlink > 2)
4568 nfs4_dec_nlink_locked(dir);
4569 spin_unlock(&dir->i_lock);
4574 static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4576 struct nfs4_exception exception = {
4577 .interruptible = true,
4579 struct inode *inode = d_inode(dentry);
4583 if (inode->i_nlink == 1)
4584 nfs4_inode_return_delegation(inode);
4586 nfs4_inode_make_writeable(inode);
4589 err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
4590 trace_nfs4_remove(dir, &dentry->d_name, err);
4591 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4593 } while (exception.retry);
4597 static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
4599 struct nfs4_exception exception = {
4600 .interruptible = true,
4605 err = _nfs4_proc_remove(dir, name, NF4DIR);
4606 trace_nfs4_remove(dir, name, err);
4607 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4609 } while (exception.retry);
4613 static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4614 struct dentry *dentry,
4615 struct inode *inode)
4617 struct nfs_removeargs *args = msg->rpc_argp;
4618 struct nfs_removeres *res = msg->rpc_resp;
4620 res->server = NFS_SB(dentry->d_sb);
4621 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
4622 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
4624 nfs_fattr_init(res->dir_attr);
4627 nfs4_inode_return_delegation(inode);
4630 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4632 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
4633 &data->args.seq_args,
4638 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
4640 struct nfs_unlinkdata *data = task->tk_calldata;
4641 struct nfs_removeres *res = &data->res;
4643 if (!nfs4_sequence_done(task, &res->seq_res))
4645 if (nfs4_async_handle_error(task, res->server, NULL,
4646 &data->timeout) == -EAGAIN)
4648 if (task->tk_status == 0)
4649 nfs4_update_changeattr(dir, &res->cinfo,
4650 res->dir_attr->time_start,
4651 NFS_INO_INVALID_DATA);
4655 static void nfs4_proc_rename_setup(struct rpc_message *msg,
4656 struct dentry *old_dentry,
4657 struct dentry *new_dentry)
4659 struct nfs_renameargs *arg = msg->rpc_argp;
4660 struct nfs_renameres *res = msg->rpc_resp;
4661 struct inode *old_inode = d_inode(old_dentry);
4662 struct inode *new_inode = d_inode(new_dentry);
4665 nfs4_inode_make_writeable(old_inode);
4667 nfs4_inode_return_delegation(new_inode);
4668 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
4669 res->server = NFS_SB(old_dentry->d_sb);
4670 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
4673 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4675 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
4676 &data->args.seq_args,
4681 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4682 struct inode *new_dir)
4684 struct nfs_renamedata *data = task->tk_calldata;
4685 struct nfs_renameres *res = &data->res;
4687 if (!nfs4_sequence_done(task, &res->seq_res))
4689 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
4692 if (task->tk_status == 0) {
4693 if (new_dir != old_dir) {
4694 /* Note: If we moved a directory, nlink will change */
4695 nfs4_update_changeattr(old_dir, &res->old_cinfo,
4696 res->old_fattr->time_start,
4697 NFS_INO_INVALID_OTHER |
4698 NFS_INO_INVALID_DATA);
4699 nfs4_update_changeattr(new_dir, &res->new_cinfo,
4700 res->new_fattr->time_start,
4701 NFS_INO_INVALID_OTHER |
4702 NFS_INO_INVALID_DATA);
4704 nfs4_update_changeattr(old_dir, &res->old_cinfo,
4705 res->old_fattr->time_start,
4706 NFS_INO_INVALID_DATA);
4711 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4713 struct nfs_server *server = NFS_SERVER(inode);
4714 __u32 bitmask[NFS4_BITMASK_SZ];
4715 struct nfs4_link_arg arg = {
4716 .fh = NFS_FH(inode),
4717 .dir_fh = NFS_FH(dir),
4721 struct nfs4_link_res res = {
4725 struct rpc_message msg = {
4726 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4730 int status = -ENOMEM;
4732 res.fattr = nfs_alloc_fattr();
4733 if (res.fattr == NULL)
4736 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4737 if (IS_ERR(res.label)) {
4738 status = PTR_ERR(res.label);
4742 nfs4_inode_make_writeable(inode);
4743 nfs4_bitmap_copy_adjust_setattr(bitmask, nfs4_bitmask(server, res.label), inode);
4745 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4747 nfs4_update_changeattr(dir, &res.cinfo, res.fattr->time_start,
4748 NFS_INO_INVALID_DATA);
4749 status = nfs_post_op_update_inode(inode, res.fattr);
4751 nfs_setsecurity(inode, res.fattr, res.label);
4755 nfs4_label_free(res.label);
4758 nfs_free_fattr(res.fattr);
4762 static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4764 struct nfs4_exception exception = {
4765 .interruptible = true,
4769 err = nfs4_handle_exception(NFS_SERVER(inode),
4770 _nfs4_proc_link(inode, dir, name),
4772 } while (exception.retry);
4776 struct nfs4_createdata {
4777 struct rpc_message msg;
4778 struct nfs4_create_arg arg;
4779 struct nfs4_create_res res;
4781 struct nfs_fattr fattr;
4782 struct nfs4_label *label;
4785 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
4786 const struct qstr *name, struct iattr *sattr, u32 ftype)
4788 struct nfs4_createdata *data;
4790 data = kzalloc(sizeof(*data), GFP_KERNEL);
4792 struct nfs_server *server = NFS_SERVER(dir);
4794 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4795 if (IS_ERR(data->label))
4798 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4799 data->msg.rpc_argp = &data->arg;
4800 data->msg.rpc_resp = &data->res;
4801 data->arg.dir_fh = NFS_FH(dir);
4802 data->arg.server = server;
4803 data->arg.name = name;
4804 data->arg.attrs = sattr;
4805 data->arg.ftype = ftype;
4806 data->arg.bitmask = nfs4_bitmask(server, data->label);
4807 data->arg.umask = current_umask();
4808 data->res.server = server;
4809 data->res.fh = &data->fh;
4810 data->res.fattr = &data->fattr;
4811 data->res.label = data->label;
4812 nfs_fattr_init(data->res.fattr);
4820 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4822 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
4823 &data->arg.seq_args, &data->res.seq_res, 1);
4825 spin_lock(&dir->i_lock);
4826 nfs4_update_changeattr_locked(dir, &data->res.dir_cinfo,
4827 data->res.fattr->time_start,
4828 NFS_INO_INVALID_DATA);
4829 /* Creating a directory bumps nlink in the parent */
4830 if (data->arg.ftype == NF4DIR)
4831 nfs4_inc_nlink_locked(dir);
4832 spin_unlock(&dir->i_lock);
4833 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
4838 static void nfs4_free_createdata(struct nfs4_createdata *data)
4840 nfs4_label_free(data->label);
4844 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4845 struct page *page, unsigned int len, struct iattr *sattr,
4846 struct nfs4_label *label)
4848 struct nfs4_createdata *data;
4849 int status = -ENAMETOOLONG;
4851 if (len > NFS4_MAXPATHLEN)
4855 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4859 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4860 data->arg.u.symlink.pages = &page;
4861 data->arg.u.symlink.len = len;
4862 data->arg.label = label;
4864 status = nfs4_do_create(dir, dentry, data);
4866 nfs4_free_createdata(data);
4871 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4872 struct page *page, unsigned int len, struct iattr *sattr)
4874 struct nfs4_exception exception = {
4875 .interruptible = true,
4877 struct nfs4_label l, *label = NULL;
4880 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4883 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4884 trace_nfs4_symlink(dir, &dentry->d_name, err);
4885 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4887 } while (exception.retry);
4889 nfs4_label_release_security(label);
4893 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4894 struct iattr *sattr, struct nfs4_label *label)
4896 struct nfs4_createdata *data;
4897 int status = -ENOMEM;
4899 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4903 data->arg.label = label;
4904 status = nfs4_do_create(dir, dentry, data);
4906 nfs4_free_createdata(data);
4911 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4912 struct iattr *sattr)
4914 struct nfs_server *server = NFS_SERVER(dir);
4915 struct nfs4_exception exception = {
4916 .interruptible = true,
4918 struct nfs4_label l, *label = NULL;
4921 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4923 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4924 sattr->ia_mode &= ~current_umask();
4926 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4927 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4928 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4930 } while (exception.retry);
4931 nfs4_label_release_security(label);
4936 static int _nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
4937 u64 cookie, struct page **pages, unsigned int count, bool plus)
4939 struct inode *dir = d_inode(dentry);
4940 struct nfs4_readdir_arg args = {
4945 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
4948 struct nfs4_readdir_res res;
4949 struct rpc_message msg = {
4950 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4957 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4959 (unsigned long long)cookie);
4960 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
4961 res.pgbase = args.pgbase;
4962 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4964 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
4965 status += args.pgbase;
4968 nfs_invalidate_atime(dir);
4970 dprintk("%s: returns %d\n", __func__, status);
4974 static int nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
4975 u64 cookie, struct page **pages, unsigned int count, bool plus)
4977 struct nfs4_exception exception = {
4978 .interruptible = true,
4982 err = _nfs4_proc_readdir(dentry, cred, cookie,
4983 pages, count, plus);
4984 trace_nfs4_readdir(d_inode(dentry), err);
4985 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
4987 } while (exception.retry);
4991 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4992 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
4994 struct nfs4_createdata *data;
4995 int mode = sattr->ia_mode;
4996 int status = -ENOMEM;
4998 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
5003 data->arg.ftype = NF4FIFO;
5004 else if (S_ISBLK(mode)) {
5005 data->arg.ftype = NF4BLK;
5006 data->arg.u.device.specdata1 = MAJOR(rdev);
5007 data->arg.u.device.specdata2 = MINOR(rdev);
5009 else if (S_ISCHR(mode)) {
5010 data->arg.ftype = NF4CHR;
5011 data->arg.u.device.specdata1 = MAJOR(rdev);
5012 data->arg.u.device.specdata2 = MINOR(rdev);
5013 } else if (!S_ISSOCK(mode)) {
5018 data->arg.label = label;
5019 status = nfs4_do_create(dir, dentry, data);
5021 nfs4_free_createdata(data);
5026 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5027 struct iattr *sattr, dev_t rdev)
5029 struct nfs_server *server = NFS_SERVER(dir);
5030 struct nfs4_exception exception = {
5031 .interruptible = true,
5033 struct nfs4_label l, *label = NULL;
5036 label = nfs4_label_init_security(dir, dentry, sattr, &l);
5038 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
5039 sattr->ia_mode &= ~current_umask();
5041 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
5042 trace_nfs4_mknod(dir, &dentry->d_name, err);
5043 err = nfs4_handle_exception(NFS_SERVER(dir), err,
5045 } while (exception.retry);
5047 nfs4_label_release_security(label);
5052 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
5053 struct nfs_fsstat *fsstat)
5055 struct nfs4_statfs_arg args = {
5057 .bitmask = server->attr_bitmask,
5059 struct nfs4_statfs_res res = {
5062 struct rpc_message msg = {
5063 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
5068 nfs_fattr_init(fsstat->fattr);
5069 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5072 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
5074 struct nfs4_exception exception = {
5075 .interruptible = true,
5079 err = nfs4_handle_exception(server,
5080 _nfs4_proc_statfs(server, fhandle, fsstat),
5082 } while (exception.retry);
5086 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
5087 struct nfs_fsinfo *fsinfo)
5089 struct nfs4_fsinfo_arg args = {
5091 .bitmask = server->attr_bitmask,
5093 struct nfs4_fsinfo_res res = {
5096 struct rpc_message msg = {
5097 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
5102 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5105 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5107 struct nfs4_exception exception = {
5108 .interruptible = true,
5113 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
5114 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
5116 nfs4_set_lease_period(server->nfs_client, fsinfo->lease_time * HZ);
5119 err = nfs4_handle_exception(server, err, &exception);
5120 } while (exception.retry);
5124 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5128 nfs_fattr_init(fsinfo->fattr);
5129 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
5131 /* block layout checks this! */
5132 server->pnfs_blksize = fsinfo->blksize;
5133 set_pnfs_layoutdriver(server, fhandle, fsinfo);
5139 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5140 struct nfs_pathconf *pathconf)
5142 struct nfs4_pathconf_arg args = {
5144 .bitmask = server->attr_bitmask,
5146 struct nfs4_pathconf_res res = {
5147 .pathconf = pathconf,
5149 struct rpc_message msg = {
5150 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
5155 /* None of the pathconf attributes are mandatory to implement */
5156 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
5157 memset(pathconf, 0, sizeof(*pathconf));
5161 nfs_fattr_init(pathconf->fattr);
5162 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5165 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5166 struct nfs_pathconf *pathconf)
5168 struct nfs4_exception exception = {
5169 .interruptible = true,
5174 err = nfs4_handle_exception(server,
5175 _nfs4_proc_pathconf(server, fhandle, pathconf),
5177 } while (exception.retry);
5181 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
5182 const struct nfs_open_context *ctx,
5183 const struct nfs_lock_context *l_ctx,
5186 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
5188 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
5190 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
5191 const struct nfs_open_context *ctx,
5192 const struct nfs_lock_context *l_ctx,
5195 nfs4_stateid _current_stateid;
5197 /* If the current stateid represents a lost lock, then exit */
5198 if (nfs4_set_rw_stateid(&_current_stateid, ctx, l_ctx, fmode) == -EIO)
5200 return nfs4_stateid_match(stateid, &_current_stateid);
5203 static bool nfs4_error_stateid_expired(int err)
5206 case -NFS4ERR_DELEG_REVOKED:
5207 case -NFS4ERR_ADMIN_REVOKED:
5208 case -NFS4ERR_BAD_STATEID:
5209 case -NFS4ERR_STALE_STATEID:
5210 case -NFS4ERR_OLD_STATEID:
5211 case -NFS4ERR_OPENMODE:
5212 case -NFS4ERR_EXPIRED:
5218 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
5220 struct nfs_server *server = NFS_SERVER(hdr->inode);
5222 trace_nfs4_read(hdr, task->tk_status);
5223 if (task->tk_status < 0) {
5224 struct nfs4_exception exception = {
5225 .inode = hdr->inode,
5226 .state = hdr->args.context->state,
5227 .stateid = &hdr->args.stateid,
5229 task->tk_status = nfs4_async_handle_exception(task,
5230 server, task->tk_status, &exception);
5231 if (exception.retry) {
5232 rpc_restart_call_prepare(task);
5237 if (task->tk_status > 0)
5238 renew_lease(server, hdr->timestamp);
5242 static bool nfs4_read_stateid_changed(struct rpc_task *task,
5243 struct nfs_pgio_args *args)
5246 if (!nfs4_error_stateid_expired(task->tk_status) ||
5247 nfs4_stateid_is_current(&args->stateid,
5252 rpc_restart_call_prepare(task);
5256 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5259 dprintk("--> %s\n", __func__);
5261 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5263 if (nfs4_read_stateid_changed(task, &hdr->args))
5265 if (task->tk_status > 0)
5266 nfs_invalidate_atime(hdr->inode);
5267 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5268 nfs4_read_done_cb(task, hdr);
5271 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5272 struct rpc_message *msg)
5274 hdr->timestamp = jiffies;
5275 if (!hdr->pgio_done_cb)
5276 hdr->pgio_done_cb = nfs4_read_done_cb;
5277 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5278 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5281 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5282 struct nfs_pgio_header *hdr)
5284 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
5285 &hdr->args.seq_args,
5289 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5290 hdr->args.lock_context,
5291 hdr->rw_mode) == -EIO)
5293 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
5298 static int nfs4_write_done_cb(struct rpc_task *task,
5299 struct nfs_pgio_header *hdr)
5301 struct inode *inode = hdr->inode;
5303 trace_nfs4_write(hdr, task->tk_status);
5304 if (task->tk_status < 0) {
5305 struct nfs4_exception exception = {
5306 .inode = hdr->inode,
5307 .state = hdr->args.context->state,
5308 .stateid = &hdr->args.stateid,
5310 task->tk_status = nfs4_async_handle_exception(task,
5311 NFS_SERVER(inode), task->tk_status,
5313 if (exception.retry) {
5314 rpc_restart_call_prepare(task);
5318 if (task->tk_status >= 0) {
5319 renew_lease(NFS_SERVER(inode), hdr->timestamp);
5320 nfs_writeback_update_inode(hdr);
5325 static bool nfs4_write_stateid_changed(struct rpc_task *task,
5326 struct nfs_pgio_args *args)
5329 if (!nfs4_error_stateid_expired(task->tk_status) ||
5330 nfs4_stateid_is_current(&args->stateid,
5335 rpc_restart_call_prepare(task);
5339 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5341 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5343 if (nfs4_write_stateid_changed(task, &hdr->args))
5345 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5346 nfs4_write_done_cb(task, hdr);
5350 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
5352 /* Don't request attributes for pNFS or O_DIRECT writes */
5353 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
5355 /* Otherwise, request attributes if and only if we don't hold
5358 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
5361 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
5362 struct rpc_message *msg,
5363 struct rpc_clnt **clnt)
5365 struct nfs_server *server = NFS_SERVER(hdr->inode);
5367 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5368 hdr->args.bitmask = NULL;
5369 hdr->res.fattr = NULL;
5371 hdr->args.bitmask = server->cache_consistency_bitmask;
5373 if (!hdr->pgio_done_cb)
5374 hdr->pgio_done_cb = nfs4_write_done_cb;
5375 hdr->res.server = server;
5376 hdr->timestamp = jiffies;
5378 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
5379 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5380 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
5383 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5385 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
5386 &data->args.seq_args,
5391 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
5393 struct inode *inode = data->inode;
5395 trace_nfs4_commit(data, task->tk_status);
5396 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5397 NULL, NULL) == -EAGAIN) {
5398 rpc_restart_call_prepare(task);
5404 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
5406 if (!nfs4_sequence_done(task, &data->res.seq_res))
5408 return data->commit_done_cb(task, data);
5411 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5412 struct rpc_clnt **clnt)
5414 struct nfs_server *server = NFS_SERVER(data->inode);
5416 if (data->commit_done_cb == NULL)
5417 data->commit_done_cb = nfs4_commit_done_cb;
5418 data->res.server = server;
5419 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
5420 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
5421 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
5424 static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5425 struct nfs_commitres *res)
5427 struct inode *dst_inode = file_inode(dst);
5428 struct nfs_server *server = NFS_SERVER(dst_inode);
5429 struct rpc_message msg = {
5430 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5435 args->fh = NFS_FH(dst_inode);
5436 return nfs4_call_sync(server->client, server, &msg,
5437 &args->seq_args, &res->seq_res, 1);
5440 int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5442 struct nfs_commitargs args = {
5446 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5447 struct nfs4_exception exception = { };
5451 status = _nfs4_proc_commit(dst, &args, res);
5452 status = nfs4_handle_exception(dst_server, status, &exception);
5453 } while (exception.retry);
5458 struct nfs4_renewdata {
5459 struct nfs_client *client;
5460 unsigned long timestamp;
5464 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5465 * standalone procedure for queueing an asynchronous RENEW.
5467 static void nfs4_renew_release(void *calldata)
5469 struct nfs4_renewdata *data = calldata;
5470 struct nfs_client *clp = data->client;
5472 if (refcount_read(&clp->cl_count) > 1)
5473 nfs4_schedule_state_renewal(clp);
5474 nfs_put_client(clp);
5478 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
5480 struct nfs4_renewdata *data = calldata;
5481 struct nfs_client *clp = data->client;
5482 unsigned long timestamp = data->timestamp;
5484 trace_nfs4_renew_async(clp, task->tk_status);
5485 switch (task->tk_status) {
5488 case -NFS4ERR_LEASE_MOVED:
5489 nfs4_schedule_lease_moved_recovery(clp);
5492 /* Unless we're shutting down, schedule state recovery! */
5493 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5495 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
5496 nfs4_schedule_lease_recovery(clp);
5499 nfs4_schedule_path_down_recovery(clp);
5501 do_renew_lease(clp, timestamp);
5504 static const struct rpc_call_ops nfs4_renew_ops = {
5505 .rpc_call_done = nfs4_renew_done,
5506 .rpc_release = nfs4_renew_release,
5509 static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
5511 struct rpc_message msg = {
5512 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5516 struct nfs4_renewdata *data;
5518 if (renew_flags == 0)
5520 if (!refcount_inc_not_zero(&clp->cl_count))
5522 data = kmalloc(sizeof(*data), GFP_NOFS);
5524 nfs_put_client(clp);
5528 data->timestamp = jiffies;
5529 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
5530 &nfs4_renew_ops, data);
5533 static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
5535 struct rpc_message msg = {
5536 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5540 unsigned long now = jiffies;
5543 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5546 do_renew_lease(clp, now);
5550 static inline int nfs4_server_supports_acls(struct nfs_server *server)
5552 return server->caps & NFS_CAP_ACLS;
5555 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5556 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
5559 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
5561 int nfs4_buf_to_pages_noslab(const void *buf, size_t buflen,
5562 struct page **pages)
5564 struct page *newpage, **spages;
5570 len = min_t(size_t, PAGE_SIZE, buflen);
5571 newpage = alloc_page(GFP_KERNEL);
5573 if (newpage == NULL)
5575 memcpy(page_address(newpage), buf, len);
5580 } while (buflen != 0);
5586 __free_page(spages[rc-1]);
5590 struct nfs4_cached_acl {
5596 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
5598 struct nfs_inode *nfsi = NFS_I(inode);
5600 spin_lock(&inode->i_lock);
5601 kfree(nfsi->nfs4_acl);
5602 nfsi->nfs4_acl = acl;
5603 spin_unlock(&inode->i_lock);
5606 static void nfs4_zap_acl_attr(struct inode *inode)
5608 nfs4_set_cached_acl(inode, NULL);
5611 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5613 struct nfs_inode *nfsi = NFS_I(inode);
5614 struct nfs4_cached_acl *acl;
5617 spin_lock(&inode->i_lock);
5618 acl = nfsi->nfs4_acl;
5621 if (buf == NULL) /* user is just asking for length */
5623 if (acl->cached == 0)
5625 ret = -ERANGE; /* see getxattr(2) man page */
5626 if (acl->len > buflen)
5628 memcpy(buf, acl->data, acl->len);
5632 spin_unlock(&inode->i_lock);
5636 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
5638 struct nfs4_cached_acl *acl;
5639 size_t buflen = sizeof(*acl) + acl_len;
5641 if (buflen <= PAGE_SIZE) {
5642 acl = kmalloc(buflen, GFP_KERNEL);
5646 _copy_from_pages(acl->data, pages, pgbase, acl_len);
5648 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5655 nfs4_set_cached_acl(inode, acl);
5659 * The getxattr API returns the required buffer length when called with a
5660 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5661 * the required buf. On a NULL buf, we send a page of data to the server
5662 * guessing that the ACL request can be serviced by a page. If so, we cache
5663 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5664 * the cache. If not so, we throw away the page, and cache the required
5665 * length. The next getxattr call will then produce another round trip to
5666 * the server, this time with the input buf of the required size.
5668 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5670 struct page **pages;
5671 struct nfs_getaclargs args = {
5672 .fh = NFS_FH(inode),
5675 struct nfs_getaclres res = {
5678 struct rpc_message msg = {
5679 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5683 unsigned int npages;
5684 int ret = -ENOMEM, i;
5685 struct nfs_server *server = NFS_SERVER(inode);
5688 buflen = server->rsize;
5690 npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
5691 pages = kmalloc_array(npages, sizeof(struct page *), GFP_NOFS);
5695 args.acl_pages = pages;
5697 for (i = 0; i < npages; i++) {
5698 pages[i] = alloc_page(GFP_KERNEL);
5703 /* for decoding across pages */
5704 res.acl_scratch = alloc_page(GFP_KERNEL);
5705 if (!res.acl_scratch)
5708 args.acl_len = npages * PAGE_SIZE;
5710 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
5711 __func__, buf, buflen, npages, args.acl_len);
5712 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5713 &msg, &args.seq_args, &res.seq_res, 0);
5717 /* Handle the case where the passed-in buffer is too short */
5718 if (res.acl_flags & NFS4_ACL_TRUNC) {
5719 /* Did the user only issue a request for the acl length? */
5725 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
5727 if (res.acl_len > buflen) {
5731 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
5736 for (i = 0; i < npages; i++)
5738 __free_page(pages[i]);
5739 if (res.acl_scratch)
5740 __free_page(res.acl_scratch);
5745 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5747 struct nfs4_exception exception = {
5748 .interruptible = true,
5752 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
5753 trace_nfs4_get_acl(inode, ret);
5756 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5757 } while (exception.retry);
5761 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5763 struct nfs_server *server = NFS_SERVER(inode);
5766 if (!nfs4_server_supports_acls(server))
5768 ret = nfs_revalidate_inode(server, inode);
5771 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5772 nfs_zap_acl_cache(inode);
5773 ret = nfs4_read_cached_acl(inode, buf, buflen);
5775 /* -ENOENT is returned if there is no ACL or if there is an ACL
5776 * but no cached acl data, just the acl length */
5778 return nfs4_get_acl_uncached(inode, buf, buflen);
5781 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5783 struct nfs_server *server = NFS_SERVER(inode);
5784 struct page *pages[NFS4ACL_MAXPAGES];
5785 struct nfs_setaclargs arg = {
5786 .fh = NFS_FH(inode),
5790 struct nfs_setaclres res;
5791 struct rpc_message msg = {
5792 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5796 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
5799 if (!nfs4_server_supports_acls(server))
5801 if (npages > ARRAY_SIZE(pages))
5803 i = nfs4_buf_to_pages_noslab(buf, buflen, arg.acl_pages);
5806 nfs4_inode_make_writeable(inode);
5807 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5810 * Free each page after tx, so the only ref left is
5811 * held by the network stack
5814 put_page(pages[i-1]);
5817 * Acl update can result in inode attribute update.
5818 * so mark the attribute cache invalid.
5820 spin_lock(&inode->i_lock);
5821 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
5822 | NFS_INO_INVALID_CTIME
5823 | NFS_INO_REVAL_FORCED;
5824 spin_unlock(&inode->i_lock);
5825 nfs_access_zap_cache(inode);
5826 nfs_zap_acl_cache(inode);
5830 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5832 struct nfs4_exception exception = { };
5835 err = __nfs4_proc_set_acl(inode, buf, buflen);
5836 trace_nfs4_set_acl(inode, err);
5837 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5839 } while (exception.retry);
5843 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
5844 static int _nfs4_get_security_label(struct inode *inode, void *buf,
5847 struct nfs_server *server = NFS_SERVER(inode);
5848 struct nfs_fattr fattr;
5849 struct nfs4_label label = {0, 0, buflen, buf};
5851 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5852 struct nfs4_getattr_arg arg = {
5853 .fh = NFS_FH(inode),
5856 struct nfs4_getattr_res res = {
5861 struct rpc_message msg = {
5862 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
5868 nfs_fattr_init(&fattr);
5870 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
5873 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5878 static int nfs4_get_security_label(struct inode *inode, void *buf,
5881 struct nfs4_exception exception = {
5882 .interruptible = true,
5886 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5890 err = _nfs4_get_security_label(inode, buf, buflen);
5891 trace_nfs4_get_security_label(inode, err);
5892 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5894 } while (exception.retry);
5898 static int _nfs4_do_set_security_label(struct inode *inode,
5899 struct nfs4_label *ilabel,
5900 struct nfs_fattr *fattr,
5901 struct nfs4_label *olabel)
5904 struct iattr sattr = {0};
5905 struct nfs_server *server = NFS_SERVER(inode);
5906 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5907 struct nfs_setattrargs arg = {
5908 .fh = NFS_FH(inode),
5914 struct nfs_setattrres res = {
5919 struct rpc_message msg = {
5920 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5926 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
5928 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5930 dprintk("%s failed: %d\n", __func__, status);
5935 static int nfs4_do_set_security_label(struct inode *inode,
5936 struct nfs4_label *ilabel,
5937 struct nfs_fattr *fattr,
5938 struct nfs4_label *olabel)
5940 struct nfs4_exception exception = { };
5944 err = _nfs4_do_set_security_label(inode, ilabel,
5946 trace_nfs4_set_security_label(inode, err);
5947 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5949 } while (exception.retry);
5954 nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
5956 struct nfs4_label ilabel, *olabel = NULL;
5957 struct nfs_fattr fattr;
5960 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5963 nfs_fattr_init(&fattr);
5967 ilabel.label = (char *)buf;
5968 ilabel.len = buflen;
5970 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5971 if (IS_ERR(olabel)) {
5972 status = -PTR_ERR(olabel);
5976 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5978 nfs_setsecurity(inode, &fattr, olabel);
5980 nfs4_label_free(olabel);
5984 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5987 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5988 nfs4_verifier *bootverf)
5992 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5993 /* An impossible timestamp guarantees this value
5994 * will never match a generated boot time. */
5995 verf[0] = cpu_to_be32(U32_MAX);
5996 verf[1] = cpu_to_be32(U32_MAX);
5998 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
5999 u64 ns = ktime_to_ns(nn->boot_time);
6001 verf[0] = cpu_to_be32(ns >> 32);
6002 verf[1] = cpu_to_be32(ns);
6004 memcpy(bootverf->data, verf, sizeof(bootverf->data));
6008 nfs4_init_nonuniform_client_string(struct nfs_client *clp)
6013 if (clp->cl_owner_id != NULL)
6018 strlen(clp->cl_rpcclient->cl_nodename) +
6020 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
6023 if (nfs4_client_id_uniquifier[0] != '\0')
6024 len += strlen(nfs4_client_id_uniquifier) + 1;
6025 if (len > NFS4_OPAQUE_LIMIT + 1)
6029 * Since this string is allocated at mount time, and held until the
6030 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6031 * about a memory-reclaim deadlock.
6033 str = kmalloc(len, GFP_KERNEL);
6038 if (nfs4_client_id_uniquifier[0] != '\0')
6039 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
6040 clp->cl_rpcclient->cl_nodename,
6041 nfs4_client_id_uniquifier,
6042 rpc_peeraddr2str(clp->cl_rpcclient,
6045 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
6046 clp->cl_rpcclient->cl_nodename,
6047 rpc_peeraddr2str(clp->cl_rpcclient,
6051 clp->cl_owner_id = str;
6056 nfs4_init_uniquifier_client_string(struct nfs_client *clp)
6061 len = 10 + 10 + 1 + 10 + 1 +
6062 strlen(nfs4_client_id_uniquifier) + 1 +
6063 strlen(clp->cl_rpcclient->cl_nodename) + 1;
6065 if (len > NFS4_OPAQUE_LIMIT + 1)
6069 * Since this string is allocated at mount time, and held until the
6070 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6071 * about a memory-reclaim deadlock.
6073 str = kmalloc(len, GFP_KERNEL);
6077 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
6078 clp->rpc_ops->version, clp->cl_minorversion,
6079 nfs4_client_id_uniquifier,
6080 clp->cl_rpcclient->cl_nodename);
6081 clp->cl_owner_id = str;
6086 nfs4_init_uniform_client_string(struct nfs_client *clp)
6091 if (clp->cl_owner_id != NULL)
6094 if (nfs4_client_id_uniquifier[0] != '\0')
6095 return nfs4_init_uniquifier_client_string(clp);
6097 len = 10 + 10 + 1 + 10 + 1 +
6098 strlen(clp->cl_rpcclient->cl_nodename) + 1;
6100 if (len > NFS4_OPAQUE_LIMIT + 1)
6104 * Since this string is allocated at mount time, and held until the
6105 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6106 * about a memory-reclaim deadlock.
6108 str = kmalloc(len, GFP_KERNEL);
6112 scnprintf(str, len, "Linux NFSv%u.%u %s",
6113 clp->rpc_ops->version, clp->cl_minorversion,
6114 clp->cl_rpcclient->cl_nodename);
6115 clp->cl_owner_id = str;
6120 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
6121 * services. Advertise one based on the address family of the
6125 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
6127 if (strchr(clp->cl_ipaddr, ':') != NULL)
6128 return scnprintf(buf, len, "tcp6");
6130 return scnprintf(buf, len, "tcp");
6133 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
6135 struct nfs4_setclientid *sc = calldata;
6137 if (task->tk_status == 0)
6138 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
6141 static const struct rpc_call_ops nfs4_setclientid_ops = {
6142 .rpc_call_done = nfs4_setclientid_done,
6146 * nfs4_proc_setclientid - Negotiate client ID
6147 * @clp: state data structure
6148 * @program: RPC program for NFSv4 callback service
6149 * @port: IP port number for NFS4 callback service
6150 * @cred: credential to use for this call
6151 * @res: where to place the result
6153 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6155 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
6156 unsigned short port, const struct cred *cred,
6157 struct nfs4_setclientid_res *res)
6159 nfs4_verifier sc_verifier;
6160 struct nfs4_setclientid setclientid = {
6161 .sc_verifier = &sc_verifier,
6165 struct rpc_message msg = {
6166 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
6167 .rpc_argp = &setclientid,
6171 struct rpc_task_setup task_setup_data = {
6172 .rpc_client = clp->cl_rpcclient,
6173 .rpc_message = &msg,
6174 .callback_ops = &nfs4_setclientid_ops,
6175 .callback_data = &setclientid,
6176 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
6178 unsigned long now = jiffies;
6181 /* nfs_client_id4 */
6182 nfs4_init_boot_verifier(clp, &sc_verifier);
6184 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
6185 status = nfs4_init_uniform_client_string(clp);
6187 status = nfs4_init_nonuniform_client_string(clp);
6193 setclientid.sc_netid_len =
6194 nfs4_init_callback_netid(clp,
6195 setclientid.sc_netid,
6196 sizeof(setclientid.sc_netid));
6197 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
6198 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
6199 clp->cl_ipaddr, port >> 8, port & 255);
6201 dprintk("NFS call setclientid auth=%s, '%s'\n",
6202 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6205 status = nfs4_call_sync_custom(&task_setup_data);
6206 if (setclientid.sc_cred) {
6207 kfree(clp->cl_acceptor);
6208 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
6209 put_rpccred(setclientid.sc_cred);
6213 do_renew_lease(clp, now);
6215 trace_nfs4_setclientid(clp, status);
6216 dprintk("NFS reply setclientid: %d\n", status);
6221 * nfs4_proc_setclientid_confirm - Confirm client ID
6222 * @clp: state data structure
6223 * @arg: result of a previous SETCLIENTID
6224 * @cred: credential to use for this call
6226 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6228 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
6229 struct nfs4_setclientid_res *arg,
6230 const struct cred *cred)
6232 struct rpc_message msg = {
6233 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
6239 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
6240 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6242 status = rpc_call_sync(clp->cl_rpcclient, &msg,
6243 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
6244 trace_nfs4_setclientid_confirm(clp, status);
6245 dprintk("NFS reply setclientid_confirm: %d\n", status);
6249 struct nfs4_delegreturndata {
6250 struct nfs4_delegreturnargs args;
6251 struct nfs4_delegreturnres res;
6253 nfs4_stateid stateid;
6254 unsigned long timestamp;
6256 struct nfs4_layoutreturn_args arg;
6257 struct nfs4_layoutreturn_res res;
6258 struct nfs4_xdr_opaque_data ld_private;
6262 struct nfs_fattr fattr;
6264 struct inode *inode;
6267 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6269 struct nfs4_delegreturndata *data = calldata;
6270 struct nfs4_exception exception = {
6271 .inode = data->inode,
6272 .stateid = &data->stateid,
6275 if (!nfs4_sequence_done(task, &data->res.seq_res))
6278 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
6280 /* Handle Layoutreturn errors */
6281 if (pnfs_roc_done(task, data->inode,
6282 &data->args.lr_args,
6284 &data->res.lr_ret) == -EAGAIN)
6287 switch (task->tk_status) {
6289 renew_lease(data->res.server, data->timestamp);
6291 case -NFS4ERR_ADMIN_REVOKED:
6292 case -NFS4ERR_DELEG_REVOKED:
6293 case -NFS4ERR_EXPIRED:
6294 nfs4_free_revoked_stateid(data->res.server,
6296 task->tk_msg.rpc_cred);
6298 case -NFS4ERR_BAD_STATEID:
6299 case -NFS4ERR_STALE_STATEID:
6301 task->tk_status = 0;
6303 case -NFS4ERR_OLD_STATEID:
6304 if (!nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6305 nfs4_stateid_seqid_inc(&data->stateid);
6306 if (data->args.bitmask) {
6307 data->args.bitmask = NULL;
6308 data->res.fattr = NULL;
6311 case -NFS4ERR_ACCESS:
6312 if (data->args.bitmask) {
6313 data->args.bitmask = NULL;
6314 data->res.fattr = NULL;
6319 task->tk_status = nfs4_async_handle_exception(task,
6320 data->res.server, task->tk_status,
6322 if (exception.retry)
6325 nfs_delegation_mark_returned(data->inode, data->args.stateid);
6326 data->rpc_status = task->tk_status;
6329 task->tk_status = 0;
6330 rpc_restart_call_prepare(task);
6333 static void nfs4_delegreturn_release(void *calldata)
6335 struct nfs4_delegreturndata *data = calldata;
6336 struct inode *inode = data->inode;
6340 pnfs_roc_release(&data->lr.arg, &data->lr.res,
6342 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
6343 nfs_iput_and_deactive(inode);
6348 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6350 struct nfs4_delegreturndata *d_data;
6351 struct pnfs_layout_hdr *lo;
6353 d_data = (struct nfs4_delegreturndata *)data;
6355 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task)) {
6356 nfs4_sequence_done(task, &d_data->res.seq_res);
6360 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6361 if (lo && !pnfs_layout_is_valid(lo)) {
6362 d_data->args.lr_args = NULL;
6363 d_data->res.lr_res = NULL;
6366 nfs4_setup_sequence(d_data->res.server->nfs_client,
6367 &d_data->args.seq_args,
6368 &d_data->res.seq_res,
6372 static const struct rpc_call_ops nfs4_delegreturn_ops = {
6373 .rpc_call_prepare = nfs4_delegreturn_prepare,
6374 .rpc_call_done = nfs4_delegreturn_done,
6375 .rpc_release = nfs4_delegreturn_release,
6378 static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6380 struct nfs4_delegreturndata *data;
6381 struct nfs_server *server = NFS_SERVER(inode);
6382 struct rpc_task *task;
6383 struct rpc_message msg = {
6384 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6387 struct rpc_task_setup task_setup_data = {
6388 .rpc_client = server->client,
6389 .rpc_message = &msg,
6390 .callback_ops = &nfs4_delegreturn_ops,
6391 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
6395 data = kzalloc(sizeof(*data), GFP_NOFS);
6398 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
6400 nfs4_state_protect(server->nfs_client,
6401 NFS_SP4_MACH_CRED_CLEANUP,
6402 &task_setup_data.rpc_client, &msg);
6404 data->args.fhandle = &data->fh;
6405 data->args.stateid = &data->stateid;
6406 data->args.bitmask = server->cache_consistency_bitmask;
6407 nfs_copy_fh(&data->fh, NFS_FH(inode));
6408 nfs4_stateid_copy(&data->stateid, stateid);
6409 data->res.fattr = &data->fattr;
6410 data->res.server = server;
6411 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
6412 data->lr.arg.ld_private = &data->lr.ld_private;
6413 nfs_fattr_init(data->res.fattr);
6414 data->timestamp = jiffies;
6415 data->rpc_status = 0;
6416 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
6417 data->inode = nfs_igrab_and_active(inode);
6420 data->args.lr_args = &data->lr.arg;
6421 data->res.lr_res = &data->lr.res;
6423 } else if (data->lr.roc) {
6424 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
6425 data->lr.roc = false;
6428 task_setup_data.callback_data = data;
6429 msg.rpc_argp = &data->args;
6430 msg.rpc_resp = &data->res;
6431 task = rpc_run_task(&task_setup_data);
6433 return PTR_ERR(task);
6436 status = rpc_wait_for_completion_task(task);
6439 status = data->rpc_status;
6445 int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6447 struct nfs_server *server = NFS_SERVER(inode);
6448 struct nfs4_exception exception = { };
6451 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
6452 trace_nfs4_delegreturn(inode, stateid, err);
6454 case -NFS4ERR_STALE_STATEID:
6455 case -NFS4ERR_EXPIRED:
6459 err = nfs4_handle_exception(server, err, &exception);
6460 } while (exception.retry);
6464 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6466 struct inode *inode = state->inode;
6467 struct nfs_server *server = NFS_SERVER(inode);
6468 struct nfs_client *clp = server->nfs_client;
6469 struct nfs_lockt_args arg = {
6470 .fh = NFS_FH(inode),
6473 struct nfs_lockt_res res = {
6476 struct rpc_message msg = {
6477 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
6480 .rpc_cred = state->owner->so_cred,
6482 struct nfs4_lock_state *lsp;
6485 arg.lock_owner.clientid = clp->cl_clientid;
6486 status = nfs4_set_lock_state(state, request);
6489 lsp = request->fl_u.nfs4_fl.owner;
6490 arg.lock_owner.id = lsp->ls_seqid.owner_id;
6491 arg.lock_owner.s_dev = server->s_dev;
6492 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6495 request->fl_type = F_UNLCK;
6497 case -NFS4ERR_DENIED:
6500 request->fl_ops->fl_release_private(request);
6501 request->fl_ops = NULL;
6506 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6508 struct nfs4_exception exception = {
6509 .interruptible = true,
6514 err = _nfs4_proc_getlk(state, cmd, request);
6515 trace_nfs4_get_lock(request, state, cmd, err);
6516 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
6518 } while (exception.retry);
6523 * Update the seqid of a lock stateid after receiving
6524 * NFS4ERR_OLD_STATEID
6526 static bool nfs4_refresh_lock_old_stateid(nfs4_stateid *dst,
6527 struct nfs4_lock_state *lsp)
6529 struct nfs4_state *state = lsp->ls_state;
6532 spin_lock(&state->state_lock);
6533 if (!nfs4_stateid_match_other(dst, &lsp->ls_stateid))
6535 if (!nfs4_stateid_is_newer(&lsp->ls_stateid, dst))
6536 nfs4_stateid_seqid_inc(dst);
6538 dst->seqid = lsp->ls_stateid.seqid;
6541 spin_unlock(&state->state_lock);
6545 static bool nfs4_sync_lock_stateid(nfs4_stateid *dst,
6546 struct nfs4_lock_state *lsp)
6548 struct nfs4_state *state = lsp->ls_state;
6551 spin_lock(&state->state_lock);
6552 ret = !nfs4_stateid_match_other(dst, &lsp->ls_stateid);
6553 nfs4_stateid_copy(dst, &lsp->ls_stateid);
6554 spin_unlock(&state->state_lock);
6558 struct nfs4_unlockdata {
6559 struct nfs_locku_args arg;
6560 struct nfs_locku_res res;
6561 struct nfs4_lock_state *lsp;
6562 struct nfs_open_context *ctx;
6563 struct nfs_lock_context *l_ctx;
6564 struct file_lock fl;
6565 struct nfs_server *server;
6566 unsigned long timestamp;
6569 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6570 struct nfs_open_context *ctx,
6571 struct nfs4_lock_state *lsp,
6572 struct nfs_seqid *seqid)
6574 struct nfs4_unlockdata *p;
6575 struct nfs4_state *state = lsp->ls_state;
6576 struct inode *inode = state->inode;
6578 p = kzalloc(sizeof(*p), GFP_NOFS);
6581 p->arg.fh = NFS_FH(inode);
6583 p->arg.seqid = seqid;
6584 p->res.seqid = seqid;
6586 /* Ensure we don't close file until we're done freeing locks! */
6587 p->ctx = get_nfs_open_context(ctx);
6588 p->l_ctx = nfs_get_lock_context(ctx);
6589 locks_init_lock(&p->fl);
6590 locks_copy_lock(&p->fl, fl);
6591 p->server = NFS_SERVER(inode);
6592 spin_lock(&state->state_lock);
6593 nfs4_stateid_copy(&p->arg.stateid, &lsp->ls_stateid);
6594 spin_unlock(&state->state_lock);
6598 static void nfs4_locku_release_calldata(void *data)
6600 struct nfs4_unlockdata *calldata = data;
6601 nfs_free_seqid(calldata->arg.seqid);
6602 nfs4_put_lock_state(calldata->lsp);
6603 nfs_put_lock_context(calldata->l_ctx);
6604 put_nfs_open_context(calldata->ctx);
6608 static void nfs4_locku_done(struct rpc_task *task, void *data)
6610 struct nfs4_unlockdata *calldata = data;
6611 struct nfs4_exception exception = {
6612 .inode = calldata->lsp->ls_state->inode,
6613 .stateid = &calldata->arg.stateid,
6616 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6618 switch (task->tk_status) {
6620 renew_lease(calldata->server, calldata->timestamp);
6621 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
6622 if (nfs4_update_lock_stateid(calldata->lsp,
6623 &calldata->res.stateid))
6626 case -NFS4ERR_ADMIN_REVOKED:
6627 case -NFS4ERR_EXPIRED:
6628 nfs4_free_revoked_stateid(calldata->server,
6629 &calldata->arg.stateid,
6630 task->tk_msg.rpc_cred);
6632 case -NFS4ERR_BAD_STATEID:
6633 case -NFS4ERR_STALE_STATEID:
6634 if (nfs4_sync_lock_stateid(&calldata->arg.stateid,
6636 rpc_restart_call_prepare(task);
6638 case -NFS4ERR_OLD_STATEID:
6639 if (nfs4_refresh_lock_old_stateid(&calldata->arg.stateid,
6641 rpc_restart_call_prepare(task);
6644 task->tk_status = nfs4_async_handle_exception(task,
6645 calldata->server, task->tk_status,
6647 if (exception.retry)
6648 rpc_restart_call_prepare(task);
6650 nfs_release_seqid(calldata->arg.seqid);
6653 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
6655 struct nfs4_unlockdata *calldata = data;
6657 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6658 nfs_async_iocounter_wait(task, calldata->l_ctx))
6661 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
6663 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
6664 /* Note: exit _without_ running nfs4_locku_done */
6667 calldata->timestamp = jiffies;
6668 if (nfs4_setup_sequence(calldata->server->nfs_client,
6669 &calldata->arg.seq_args,
6670 &calldata->res.seq_res,
6672 nfs_release_seqid(calldata->arg.seqid);
6675 task->tk_action = NULL;
6677 nfs4_sequence_done(task, &calldata->res.seq_res);
6680 static const struct rpc_call_ops nfs4_locku_ops = {
6681 .rpc_call_prepare = nfs4_locku_prepare,
6682 .rpc_call_done = nfs4_locku_done,
6683 .rpc_release = nfs4_locku_release_calldata,
6686 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6687 struct nfs_open_context *ctx,
6688 struct nfs4_lock_state *lsp,
6689 struct nfs_seqid *seqid)
6691 struct nfs4_unlockdata *data;
6692 struct rpc_message msg = {
6693 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6694 .rpc_cred = ctx->cred,
6696 struct rpc_task_setup task_setup_data = {
6697 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
6698 .rpc_message = &msg,
6699 .callback_ops = &nfs4_locku_ops,
6700 .workqueue = nfsiod_workqueue,
6701 .flags = RPC_TASK_ASYNC,
6704 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6705 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6707 /* Ensure this is an unlock - when canceling a lock, the
6708 * canceled lock is passed in, and it won't be an unlock.
6710 fl->fl_type = F_UNLCK;
6711 if (fl->fl_flags & FL_CLOSE)
6712 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
6714 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6716 nfs_free_seqid(seqid);
6717 return ERR_PTR(-ENOMEM);
6720 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
6721 msg.rpc_argp = &data->arg;
6722 msg.rpc_resp = &data->res;
6723 task_setup_data.callback_data = data;
6724 return rpc_run_task(&task_setup_data);
6727 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6729 struct inode *inode = state->inode;
6730 struct nfs4_state_owner *sp = state->owner;
6731 struct nfs_inode *nfsi = NFS_I(inode);
6732 struct nfs_seqid *seqid;
6733 struct nfs4_lock_state *lsp;
6734 struct rpc_task *task;
6735 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6737 unsigned char fl_flags = request->fl_flags;
6739 status = nfs4_set_lock_state(state, request);
6740 /* Unlock _before_ we do the RPC call */
6741 request->fl_flags |= FL_EXISTS;
6742 /* Exclude nfs_delegation_claim_locks() */
6743 mutex_lock(&sp->so_delegreturn_mutex);
6744 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
6745 down_read(&nfsi->rwsem);
6746 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
6747 up_read(&nfsi->rwsem);
6748 mutex_unlock(&sp->so_delegreturn_mutex);
6751 up_read(&nfsi->rwsem);
6752 mutex_unlock(&sp->so_delegreturn_mutex);
6755 /* Is this a delegated lock? */
6756 lsp = request->fl_u.nfs4_fl.owner;
6757 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6759 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6760 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
6764 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
6765 status = PTR_ERR(task);
6768 status = rpc_wait_for_completion_task(task);
6771 request->fl_flags = fl_flags;
6772 trace_nfs4_unlock(request, state, F_SETLK, status);
6776 struct nfs4_lockdata {
6777 struct nfs_lock_args arg;
6778 struct nfs_lock_res res;
6779 struct nfs4_lock_state *lsp;
6780 struct nfs_open_context *ctx;
6781 struct file_lock fl;
6782 unsigned long timestamp;
6785 struct nfs_server *server;
6788 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
6789 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6792 struct nfs4_lockdata *p;
6793 struct inode *inode = lsp->ls_state->inode;
6794 struct nfs_server *server = NFS_SERVER(inode);
6795 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6797 p = kzalloc(sizeof(*p), gfp_mask);
6801 p->arg.fh = NFS_FH(inode);
6803 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
6804 if (IS_ERR(p->arg.open_seqid))
6806 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6807 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
6808 if (IS_ERR(p->arg.lock_seqid))
6809 goto out_free_seqid;
6810 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
6811 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
6812 p->arg.lock_owner.s_dev = server->s_dev;
6813 p->res.lock_seqid = p->arg.lock_seqid;
6816 p->ctx = get_nfs_open_context(ctx);
6817 locks_init_lock(&p->fl);
6818 locks_copy_lock(&p->fl, fl);
6821 nfs_free_seqid(p->arg.open_seqid);
6827 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6829 struct nfs4_lockdata *data = calldata;
6830 struct nfs4_state *state = data->lsp->ls_state;
6832 dprintk("%s: begin!\n", __func__);
6833 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
6835 /* Do we need to do an open_to_lock_owner? */
6836 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
6837 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
6838 goto out_release_lock_seqid;
6840 nfs4_stateid_copy(&data->arg.open_stateid,
6841 &state->open_stateid);
6842 data->arg.new_lock_owner = 1;
6843 data->res.open_seqid = data->arg.open_seqid;
6845 data->arg.new_lock_owner = 0;
6846 nfs4_stateid_copy(&data->arg.lock_stateid,
6847 &data->lsp->ls_stateid);
6849 if (!nfs4_valid_open_stateid(state)) {
6850 data->rpc_status = -EBADF;
6851 task->tk_action = NULL;
6852 goto out_release_open_seqid;
6854 data->timestamp = jiffies;
6855 if (nfs4_setup_sequence(data->server->nfs_client,
6856 &data->arg.seq_args,
6860 out_release_open_seqid:
6861 nfs_release_seqid(data->arg.open_seqid);
6862 out_release_lock_seqid:
6863 nfs_release_seqid(data->arg.lock_seqid);
6865 nfs4_sequence_done(task, &data->res.seq_res);
6866 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
6869 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6871 struct nfs4_lockdata *data = calldata;
6872 struct nfs4_lock_state *lsp = data->lsp;
6874 dprintk("%s: begin!\n", __func__);
6876 if (!nfs4_sequence_done(task, &data->res.seq_res))
6879 data->rpc_status = task->tk_status;
6880 switch (task->tk_status) {
6882 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
6884 if (data->arg.new_lock && !data->cancelled) {
6885 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
6886 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
6889 if (data->arg.new_lock_owner != 0) {
6890 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6891 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6892 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6893 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6896 case -NFS4ERR_BAD_STATEID:
6897 case -NFS4ERR_OLD_STATEID:
6898 case -NFS4ERR_STALE_STATEID:
6899 case -NFS4ERR_EXPIRED:
6900 if (data->arg.new_lock_owner != 0) {
6901 if (!nfs4_stateid_match(&data->arg.open_stateid,
6902 &lsp->ls_state->open_stateid))
6904 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6909 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
6912 if (!data->cancelled)
6913 rpc_restart_call_prepare(task);
6917 static void nfs4_lock_release(void *calldata)
6919 struct nfs4_lockdata *data = calldata;
6921 dprintk("%s: begin!\n", __func__);
6922 nfs_free_seqid(data->arg.open_seqid);
6923 if (data->cancelled && data->rpc_status == 0) {
6924 struct rpc_task *task;
6925 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6926 data->arg.lock_seqid);
6928 rpc_put_task_async(task);
6929 dprintk("%s: cancelling lock!\n", __func__);
6931 nfs_free_seqid(data->arg.lock_seqid);
6932 nfs4_put_lock_state(data->lsp);
6933 put_nfs_open_context(data->ctx);
6935 dprintk("%s: done!\n", __func__);
6938 static const struct rpc_call_ops nfs4_lock_ops = {
6939 .rpc_call_prepare = nfs4_lock_prepare,
6940 .rpc_call_done = nfs4_lock_done,
6941 .rpc_release = nfs4_lock_release,
6944 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6947 case -NFS4ERR_ADMIN_REVOKED:
6948 case -NFS4ERR_EXPIRED:
6949 case -NFS4ERR_BAD_STATEID:
6950 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6951 if (new_lock_owner != 0 ||
6952 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
6953 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
6955 case -NFS4ERR_STALE_STATEID:
6956 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6957 nfs4_schedule_lease_recovery(server->nfs_client);
6961 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
6963 struct nfs4_lockdata *data;
6964 struct rpc_task *task;
6965 struct rpc_message msg = {
6966 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6967 .rpc_cred = state->owner->so_cred,
6969 struct rpc_task_setup task_setup_data = {
6970 .rpc_client = NFS_CLIENT(state->inode),
6971 .rpc_message = &msg,
6972 .callback_ops = &nfs4_lock_ops,
6973 .workqueue = nfsiod_workqueue,
6974 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
6978 dprintk("%s: begin!\n", __func__);
6979 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
6980 fl->fl_u.nfs4_fl.owner,
6981 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
6985 data->arg.block = 1;
6986 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
6987 recovery_type > NFS_LOCK_NEW);
6988 msg.rpc_argp = &data->arg;
6989 msg.rpc_resp = &data->res;
6990 task_setup_data.callback_data = data;
6991 if (recovery_type > NFS_LOCK_NEW) {
6992 if (recovery_type == NFS_LOCK_RECLAIM)
6993 data->arg.reclaim = NFS_LOCK_RECLAIM;
6995 data->arg.new_lock = 1;
6996 task = rpc_run_task(&task_setup_data);
6998 return PTR_ERR(task);
6999 ret = rpc_wait_for_completion_task(task);
7001 ret = data->rpc_status;
7003 nfs4_handle_setlk_error(data->server, data->lsp,
7004 data->arg.new_lock_owner, ret);
7006 data->cancelled = true;
7008 dprintk("%s: done, ret = %d!\n", __func__, ret);
7009 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
7013 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
7015 struct nfs_server *server = NFS_SERVER(state->inode);
7016 struct nfs4_exception exception = {
7017 .inode = state->inode,
7022 /* Cache the lock if possible... */
7023 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7025 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
7026 if (err != -NFS4ERR_DELAY)
7028 nfs4_handle_exception(server, err, &exception);
7029 } while (exception.retry);
7033 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
7035 struct nfs_server *server = NFS_SERVER(state->inode);
7036 struct nfs4_exception exception = {
7037 .inode = state->inode,
7041 err = nfs4_set_lock_state(state, request);
7044 if (!recover_lost_locks) {
7045 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
7049 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7051 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
7055 case -NFS4ERR_GRACE:
7056 case -NFS4ERR_DELAY:
7057 nfs4_handle_exception(server, err, &exception);
7060 } while (exception.retry);
7065 #if defined(CONFIG_NFS_V4_1)
7066 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
7068 struct nfs4_lock_state *lsp;
7071 status = nfs4_set_lock_state(state, request);
7074 lsp = request->fl_u.nfs4_fl.owner;
7075 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
7076 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
7078 return nfs4_lock_expired(state, request);
7082 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7084 struct nfs_inode *nfsi = NFS_I(state->inode);
7085 struct nfs4_state_owner *sp = state->owner;
7086 unsigned char fl_flags = request->fl_flags;
7089 request->fl_flags |= FL_ACCESS;
7090 status = locks_lock_inode_wait(state->inode, request);
7093 mutex_lock(&sp->so_delegreturn_mutex);
7094 down_read(&nfsi->rwsem);
7095 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
7096 /* Yes: cache locks! */
7097 /* ...but avoid races with delegation recall... */
7098 request->fl_flags = fl_flags & ~FL_SLEEP;
7099 status = locks_lock_inode_wait(state->inode, request);
7100 up_read(&nfsi->rwsem);
7101 mutex_unlock(&sp->so_delegreturn_mutex);
7104 up_read(&nfsi->rwsem);
7105 mutex_unlock(&sp->so_delegreturn_mutex);
7106 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
7108 request->fl_flags = fl_flags;
7112 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7114 struct nfs4_exception exception = {
7116 .inode = state->inode,
7117 .interruptible = true,
7122 err = _nfs4_proc_setlk(state, cmd, request);
7123 if (err == -NFS4ERR_DENIED)
7125 err = nfs4_handle_exception(NFS_SERVER(state->inode),
7127 } while (exception.retry);
7131 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
7132 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
7135 nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
7136 struct file_lock *request)
7138 int status = -ERESTARTSYS;
7139 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
7141 while(!signalled()) {
7142 status = nfs4_proc_setlk(state, cmd, request);
7143 if ((status != -EAGAIN) || IS_SETLK(cmd))
7145 freezable_schedule_timeout_interruptible(timeout);
7147 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
7148 status = -ERESTARTSYS;
7153 #ifdef CONFIG_NFS_V4_1
7154 struct nfs4_lock_waiter {
7155 struct task_struct *task;
7156 struct inode *inode;
7157 struct nfs_lowner *owner;
7161 nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
7164 struct nfs4_lock_waiter *waiter = wait->private;
7166 /* NULL key means to wake up everyone */
7168 struct cb_notify_lock_args *cbnl = key;
7169 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
7170 *wowner = waiter->owner;
7172 /* Only wake if the callback was for the same owner. */
7173 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
7176 /* Make sure it's for the right inode */
7177 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
7181 /* override "private" so we can use default_wake_function */
7182 wait->private = waiter->task;
7183 ret = woken_wake_function(wait, mode, flags, key);
7185 list_del_init(&wait->entry);
7186 wait->private = waiter;
7191 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7193 int status = -ERESTARTSYS;
7194 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
7195 struct nfs_server *server = NFS_SERVER(state->inode);
7196 struct nfs_client *clp = server->nfs_client;
7197 wait_queue_head_t *q = &clp->cl_lock_waitq;
7198 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
7199 .id = lsp->ls_seqid.owner_id,
7200 .s_dev = server->s_dev };
7201 struct nfs4_lock_waiter waiter = { .task = current,
7202 .inode = state->inode,
7204 wait_queue_entry_t wait;
7206 /* Don't bother with waitqueue if we don't expect a callback */
7207 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
7208 return nfs4_retry_setlk_simple(state, cmd, request);
7211 wait.private = &waiter;
7212 wait.func = nfs4_wake_lock_waiter;
7214 while(!signalled()) {
7215 add_wait_queue(q, &wait);
7216 status = nfs4_proc_setlk(state, cmd, request);
7217 if ((status != -EAGAIN) || IS_SETLK(cmd)) {
7218 finish_wait(q, &wait);
7222 status = -ERESTARTSYS;
7223 freezer_do_not_count();
7224 wait_woken(&wait, TASK_INTERRUPTIBLE, NFS4_LOCK_MAXTIMEOUT);
7226 finish_wait(q, &wait);
7231 #else /* !CONFIG_NFS_V4_1 */
7233 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7235 return nfs4_retry_setlk_simple(state, cmd, request);
7240 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
7242 struct nfs_open_context *ctx;
7243 struct nfs4_state *state;
7246 /* verify open state */
7247 ctx = nfs_file_open_context(filp);
7250 if (IS_GETLK(cmd)) {
7252 return nfs4_proc_getlk(state, F_GETLK, request);
7256 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
7259 if (request->fl_type == F_UNLCK) {
7261 return nfs4_proc_unlck(state, cmd, request);
7268 if ((request->fl_flags & FL_POSIX) &&
7269 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
7273 * Don't rely on the VFS having checked the file open mode,
7274 * since it won't do this for flock() locks.
7276 switch (request->fl_type) {
7278 if (!(filp->f_mode & FMODE_READ))
7282 if (!(filp->f_mode & FMODE_WRITE))
7286 status = nfs4_set_lock_state(state, request);
7290 return nfs4_retry_setlk(state, cmd, request);
7293 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
7295 struct nfs_server *server = NFS_SERVER(state->inode);
7298 err = nfs4_set_lock_state(state, fl);
7301 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
7302 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
7305 struct nfs_release_lockowner_data {
7306 struct nfs4_lock_state *lsp;
7307 struct nfs_server *server;
7308 struct nfs_release_lockowner_args args;
7309 struct nfs_release_lockowner_res res;
7310 unsigned long timestamp;
7313 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7315 struct nfs_release_lockowner_data *data = calldata;
7316 struct nfs_server *server = data->server;
7317 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7318 &data->res.seq_res, task);
7319 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7320 data->timestamp = jiffies;
7323 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7325 struct nfs_release_lockowner_data *data = calldata;
7326 struct nfs_server *server = data->server;
7328 nfs40_sequence_done(task, &data->res.seq_res);
7330 switch (task->tk_status) {
7332 renew_lease(server, data->timestamp);
7334 case -NFS4ERR_STALE_CLIENTID:
7335 case -NFS4ERR_EXPIRED:
7336 nfs4_schedule_lease_recovery(server->nfs_client);
7338 case -NFS4ERR_LEASE_MOVED:
7339 case -NFS4ERR_DELAY:
7340 if (nfs4_async_handle_error(task, server,
7341 NULL, NULL) == -EAGAIN)
7342 rpc_restart_call_prepare(task);
7346 static void nfs4_release_lockowner_release(void *calldata)
7348 struct nfs_release_lockowner_data *data = calldata;
7349 nfs4_free_lock_state(data->server, data->lsp);
7353 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
7354 .rpc_call_prepare = nfs4_release_lockowner_prepare,
7355 .rpc_call_done = nfs4_release_lockowner_done,
7356 .rpc_release = nfs4_release_lockowner_release,
7360 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
7362 struct nfs_release_lockowner_data *data;
7363 struct rpc_message msg = {
7364 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7367 if (server->nfs_client->cl_mvops->minor_version != 0)
7370 data = kmalloc(sizeof(*data), GFP_NOFS);
7374 data->server = server;
7375 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7376 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7377 data->args.lock_owner.s_dev = server->s_dev;
7379 msg.rpc_argp = &data->args;
7380 msg.rpc_resp = &data->res;
7381 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
7382 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
7385 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7387 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
7388 struct dentry *unused, struct inode *inode,
7389 const char *key, const void *buf,
7390 size_t buflen, int flags)
7392 return nfs4_proc_set_acl(inode, buf, buflen);
7395 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
7396 struct dentry *unused, struct inode *inode,
7397 const char *key, void *buf, size_t buflen)
7399 return nfs4_proc_get_acl(inode, buf, buflen);
7402 static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
7404 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
7407 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
7409 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
7410 struct dentry *unused, struct inode *inode,
7411 const char *key, const void *buf,
7412 size_t buflen, int flags)
7414 if (security_ismaclabel(key))
7415 return nfs4_set_security_label(inode, buf, buflen);
7420 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
7421 struct dentry *unused, struct inode *inode,
7422 const char *key, void *buf, size_t buflen)
7424 if (security_ismaclabel(key))
7425 return nfs4_get_security_label(inode, buf, buflen);
7430 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7434 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7435 len = security_inode_listsecurity(inode, list, list_len);
7436 if (list_len && len > list_len)
7442 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7443 .prefix = XATTR_SECURITY_PREFIX,
7444 .get = nfs4_xattr_get_nfs4_label,
7445 .set = nfs4_xattr_set_nfs4_label,
7451 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7458 #ifdef CONFIG_NFS_V4_2
7459 static int nfs4_xattr_set_nfs4_user(const struct xattr_handler *handler,
7460 struct dentry *unused, struct inode *inode,
7461 const char *key, const void *buf,
7462 size_t buflen, int flags)
7464 struct nfs_access_entry cache;
7467 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7471 * There is no mapping from the MAY_* flags to the NFS_ACCESS_XA*
7472 * flags right now. Handling of xattr operations use the normal
7473 * file read/write permissions.
7475 * Just in case the server has other ideas (which RFC 8276 allows),
7476 * do a cached access check for the XA* flags to possibly avoid
7477 * doing an RPC and getting EACCES back.
7479 if (!nfs_access_get_cached(inode, current_cred(), &cache, true)) {
7480 if (!(cache.mask & NFS_ACCESS_XAWRITE))
7485 ret = nfs42_proc_removexattr(inode, key);
7487 nfs4_xattr_cache_remove(inode, key);
7489 ret = nfs42_proc_setxattr(inode, key, buf, buflen, flags);
7491 nfs4_xattr_cache_add(inode, key, buf, NULL, buflen);
7497 static int nfs4_xattr_get_nfs4_user(const struct xattr_handler *handler,
7498 struct dentry *unused, struct inode *inode,
7499 const char *key, void *buf, size_t buflen)
7501 struct nfs_access_entry cache;
7504 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7507 if (!nfs_access_get_cached(inode, current_cred(), &cache, true)) {
7508 if (!(cache.mask & NFS_ACCESS_XAREAD))
7512 ret = nfs_revalidate_inode(NFS_SERVER(inode), inode);
7516 ret = nfs4_xattr_cache_get(inode, key, buf, buflen);
7517 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7520 ret = nfs42_proc_getxattr(inode, key, buf, buflen);
7526 nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7533 struct nfs_access_entry cache;
7535 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7538 if (!nfs_access_get_cached(inode, current_cred(), &cache, true)) {
7539 if (!(cache.mask & NFS_ACCESS_XALIST))
7543 ret = nfs_revalidate_inode(NFS_SERVER(inode), inode);
7547 ret = nfs4_xattr_cache_list(inode, list, list_len);
7548 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7553 buflen = list_len ? list_len : XATTR_LIST_MAX;
7554 buf = list_len ? list : NULL;
7558 ret = nfs42_proc_listxattrs(inode, buf, buflen,
7571 nfs4_xattr_cache_set_list(inode, list, size);
7579 nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7583 #endif /* CONFIG_NFS_V4_2 */
7586 * nfs_fhget will use either the mounted_on_fileid or the fileid
7588 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7590 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7591 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7592 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
7593 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
7596 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
7597 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
7598 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7602 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7603 const struct qstr *name,
7604 struct nfs4_fs_locations *fs_locations,
7607 struct nfs_server *server = NFS_SERVER(dir);
7609 struct nfs4_fs_locations_arg args = {
7610 .dir_fh = NFS_FH(dir),
7615 struct nfs4_fs_locations_res res = {
7616 .fs_locations = fs_locations,
7618 struct rpc_message msg = {
7619 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7625 dprintk("%s: start\n", __func__);
7627 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7628 bitmask[1] = nfs4_fattr_bitmap[1];
7630 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7631 * is not supported */
7632 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
7633 bitmask[0] &= ~FATTR4_WORD0_FILEID;
7635 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
7637 nfs_fattr_init(&fs_locations->fattr);
7638 fs_locations->server = server;
7639 fs_locations->nlocations = 0;
7640 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
7641 dprintk("%s: returned status = %d\n", __func__, status);
7645 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7646 const struct qstr *name,
7647 struct nfs4_fs_locations *fs_locations,
7650 struct nfs4_exception exception = {
7651 .interruptible = true,
7655 err = _nfs4_proc_fs_locations(client, dir, name,
7656 fs_locations, page);
7657 trace_nfs4_get_fs_locations(dir, name, err);
7658 err = nfs4_handle_exception(NFS_SERVER(dir), err,
7660 } while (exception.retry);
7665 * This operation also signals the server that this client is
7666 * performing migration recovery. The server can stop returning
7667 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7668 * appended to this compound to identify the client ID which is
7669 * performing recovery.
7671 static int _nfs40_proc_get_locations(struct inode *inode,
7672 struct nfs4_fs_locations *locations,
7673 struct page *page, const struct cred *cred)
7675 struct nfs_server *server = NFS_SERVER(inode);
7676 struct rpc_clnt *clnt = server->client;
7678 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7680 struct nfs4_fs_locations_arg args = {
7681 .clientid = server->nfs_client->cl_clientid,
7682 .fh = NFS_FH(inode),
7685 .migration = 1, /* skip LOOKUP */
7686 .renew = 1, /* append RENEW */
7688 struct nfs4_fs_locations_res res = {
7689 .fs_locations = locations,
7693 struct rpc_message msg = {
7694 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7699 unsigned long now = jiffies;
7702 nfs_fattr_init(&locations->fattr);
7703 locations->server = server;
7704 locations->nlocations = 0;
7706 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7707 status = nfs4_call_sync_sequence(clnt, server, &msg,
7708 &args.seq_args, &res.seq_res);
7712 renew_lease(server, now);
7716 #ifdef CONFIG_NFS_V4_1
7719 * This operation also signals the server that this client is
7720 * performing migration recovery. The server can stop asserting
7721 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7722 * performing this operation is identified in the SEQUENCE
7723 * operation in this compound.
7725 * When the client supports GETATTR(fs_locations_info), it can
7726 * be plumbed in here.
7728 static int _nfs41_proc_get_locations(struct inode *inode,
7729 struct nfs4_fs_locations *locations,
7730 struct page *page, const struct cred *cred)
7732 struct nfs_server *server = NFS_SERVER(inode);
7733 struct rpc_clnt *clnt = server->client;
7735 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7737 struct nfs4_fs_locations_arg args = {
7738 .fh = NFS_FH(inode),
7741 .migration = 1, /* skip LOOKUP */
7743 struct nfs4_fs_locations_res res = {
7744 .fs_locations = locations,
7747 struct rpc_message msg = {
7748 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7755 nfs_fattr_init(&locations->fattr);
7756 locations->server = server;
7757 locations->nlocations = 0;
7759 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7760 status = nfs4_call_sync_sequence(clnt, server, &msg,
7761 &args.seq_args, &res.seq_res);
7762 if (status == NFS4_OK &&
7763 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7764 status = -NFS4ERR_LEASE_MOVED;
7768 #endif /* CONFIG_NFS_V4_1 */
7771 * nfs4_proc_get_locations - discover locations for a migrated FSID
7772 * @inode: inode on FSID that is migrating
7773 * @locations: result of query
7775 * @cred: credential to use for this operation
7777 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7778 * operation failed, or a negative errno if a local error occurred.
7780 * On success, "locations" is filled in, but if the server has
7781 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7784 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7785 * from this client that require migration recovery.
7787 int nfs4_proc_get_locations(struct inode *inode,
7788 struct nfs4_fs_locations *locations,
7789 struct page *page, const struct cred *cred)
7791 struct nfs_server *server = NFS_SERVER(inode);
7792 struct nfs_client *clp = server->nfs_client;
7793 const struct nfs4_mig_recovery_ops *ops =
7794 clp->cl_mvops->mig_recovery_ops;
7795 struct nfs4_exception exception = {
7796 .interruptible = true,
7800 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7801 (unsigned long long)server->fsid.major,
7802 (unsigned long long)server->fsid.minor,
7804 nfs_display_fhandle(NFS_FH(inode), __func__);
7807 status = ops->get_locations(inode, locations, page, cred);
7808 if (status != -NFS4ERR_DELAY)
7810 nfs4_handle_exception(server, status, &exception);
7811 } while (exception.retry);
7816 * This operation also signals the server that this client is
7817 * performing "lease moved" recovery. The server can stop
7818 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7819 * is appended to this compound to identify the client ID which is
7820 * performing recovery.
7822 static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
7824 struct nfs_server *server = NFS_SERVER(inode);
7825 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7826 struct rpc_clnt *clnt = server->client;
7827 struct nfs4_fsid_present_arg args = {
7828 .fh = NFS_FH(inode),
7829 .clientid = clp->cl_clientid,
7830 .renew = 1, /* append RENEW */
7832 struct nfs4_fsid_present_res res = {
7835 struct rpc_message msg = {
7836 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7841 unsigned long now = jiffies;
7844 res.fh = nfs_alloc_fhandle();
7848 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7849 status = nfs4_call_sync_sequence(clnt, server, &msg,
7850 &args.seq_args, &res.seq_res);
7851 nfs_free_fhandle(res.fh);
7855 do_renew_lease(clp, now);
7859 #ifdef CONFIG_NFS_V4_1
7862 * This operation also signals the server that this client is
7863 * performing "lease moved" recovery. The server can stop asserting
7864 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7865 * this operation is identified in the SEQUENCE operation in this
7868 static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
7870 struct nfs_server *server = NFS_SERVER(inode);
7871 struct rpc_clnt *clnt = server->client;
7872 struct nfs4_fsid_present_arg args = {
7873 .fh = NFS_FH(inode),
7875 struct nfs4_fsid_present_res res = {
7877 struct rpc_message msg = {
7878 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7885 res.fh = nfs_alloc_fhandle();
7889 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7890 status = nfs4_call_sync_sequence(clnt, server, &msg,
7891 &args.seq_args, &res.seq_res);
7892 nfs_free_fhandle(res.fh);
7893 if (status == NFS4_OK &&
7894 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7895 status = -NFS4ERR_LEASE_MOVED;
7899 #endif /* CONFIG_NFS_V4_1 */
7902 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7903 * @inode: inode on FSID to check
7904 * @cred: credential to use for this operation
7906 * Server indicates whether the FSID is present, moved, or not
7907 * recognized. This operation is necessary to clear a LEASE_MOVED
7908 * condition for this client ID.
7910 * Returns NFS4_OK if the FSID is present on this server,
7911 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7912 * NFS4ERR code if some error occurred on the server, or a
7913 * negative errno if a local failure occurred.
7915 int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
7917 struct nfs_server *server = NFS_SERVER(inode);
7918 struct nfs_client *clp = server->nfs_client;
7919 const struct nfs4_mig_recovery_ops *ops =
7920 clp->cl_mvops->mig_recovery_ops;
7921 struct nfs4_exception exception = {
7922 .interruptible = true,
7926 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7927 (unsigned long long)server->fsid.major,
7928 (unsigned long long)server->fsid.minor,
7930 nfs_display_fhandle(NFS_FH(inode), __func__);
7933 status = ops->fsid_present(inode, cred);
7934 if (status != -NFS4ERR_DELAY)
7936 nfs4_handle_exception(server, status, &exception);
7937 } while (exception.retry);
7942 * If 'use_integrity' is true and the state managment nfs_client
7943 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7944 * and the machine credential as per RFC3530bis and RFC5661 Security
7945 * Considerations sections. Otherwise, just use the user cred with the
7946 * filesystem's rpc_client.
7948 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
7951 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
7952 struct nfs_client *clp = NFS_SERVER(dir)->nfs_client;
7953 struct nfs4_secinfo_arg args = {
7954 .dir_fh = NFS_FH(dir),
7957 struct nfs4_secinfo_res res = {
7960 struct rpc_message msg = {
7961 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7965 struct nfs4_call_sync_data data = {
7966 .seq_server = NFS_SERVER(dir),
7967 .seq_args = &args.seq_args,
7968 .seq_res = &res.seq_res,
7970 struct rpc_task_setup task_setup = {
7972 .rpc_message = &msg,
7973 .callback_ops = clp->cl_mvops->call_sync_ops,
7974 .callback_data = &data,
7975 .flags = RPC_TASK_NO_ROUND_ROBIN,
7977 const struct cred *cred = NULL;
7979 if (use_integrity) {
7980 clnt = clp->cl_rpcclient;
7981 task_setup.rpc_client = clnt;
7983 cred = nfs4_get_clid_cred(clp);
7984 msg.rpc_cred = cred;
7987 dprintk("NFS call secinfo %s\n", name->name);
7989 nfs4_state_protect(clp, NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7990 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
7991 status = nfs4_call_sync_custom(&task_setup);
7993 dprintk("NFS reply secinfo: %d\n", status);
7999 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
8000 struct nfs4_secinfo_flavors *flavors)
8002 struct nfs4_exception exception = {
8003 .interruptible = true,
8007 err = -NFS4ERR_WRONGSEC;
8009 /* try to use integrity protection with machine cred */
8010 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
8011 err = _nfs4_proc_secinfo(dir, name, flavors, true);
8014 * if unable to use integrity protection, or SECINFO with
8015 * integrity protection returns NFS4ERR_WRONGSEC (which is
8016 * disallowed by spec, but exists in deployed servers) use
8017 * the current filesystem's rpc_client and the user cred.
8019 if (err == -NFS4ERR_WRONGSEC)
8020 err = _nfs4_proc_secinfo(dir, name, flavors, false);
8022 trace_nfs4_secinfo(dir, name, err);
8023 err = nfs4_handle_exception(NFS_SERVER(dir), err,
8025 } while (exception.retry);
8029 #ifdef CONFIG_NFS_V4_1
8031 * Check the exchange flags returned by the server for invalid flags, having
8032 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
8035 static int nfs4_check_cl_exchange_flags(u32 flags)
8037 if (flags & ~EXCHGID4_FLAG_MASK_R)
8039 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
8040 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
8042 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
8046 return -NFS4ERR_INVAL;
8050 nfs41_same_server_scope(struct nfs41_server_scope *a,
8051 struct nfs41_server_scope *b)
8053 if (a->server_scope_sz != b->server_scope_sz)
8055 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
8059 nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
8061 struct nfs41_bind_conn_to_session_args *args = task->tk_msg.rpc_argp;
8062 struct nfs41_bind_conn_to_session_res *res = task->tk_msg.rpc_resp;
8063 struct nfs_client *clp = args->client;
8065 switch (task->tk_status) {
8066 case -NFS4ERR_BADSESSION:
8067 case -NFS4ERR_DEADSESSION:
8068 nfs4_schedule_session_recovery(clp->cl_session,
8071 if (args->dir == NFS4_CDFC4_FORE_OR_BOTH &&
8072 res->dir != NFS4_CDFS4_BOTH) {
8073 rpc_task_close_connection(task);
8074 if (args->retries++ < MAX_BIND_CONN_TO_SESSION_RETRIES)
8075 rpc_restart_call(task);
8079 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
8080 .rpc_call_done = nfs4_bind_one_conn_to_session_done,
8084 * nfs4_proc_bind_one_conn_to_session()
8086 * The 4.1 client currently uses the same TCP connection for the
8087 * fore and backchannel.
8090 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
8091 struct rpc_xprt *xprt,
8092 struct nfs_client *clp,
8093 const struct cred *cred)
8096 struct nfs41_bind_conn_to_session_args args = {
8098 .dir = NFS4_CDFC4_FORE_OR_BOTH,
8101 struct nfs41_bind_conn_to_session_res res;
8102 struct rpc_message msg = {
8104 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
8109 struct rpc_task_setup task_setup_data = {
8112 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
8113 .rpc_message = &msg,
8114 .flags = RPC_TASK_TIMEOUT,
8116 struct rpc_task *task;
8118 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
8119 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
8120 args.dir = NFS4_CDFC4_FORE;
8122 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
8123 if (xprt != rcu_access_pointer(clnt->cl_xprt))
8124 args.dir = NFS4_CDFC4_FORE;
8126 task = rpc_run_task(&task_setup_data);
8127 if (!IS_ERR(task)) {
8128 status = task->tk_status;
8131 status = PTR_ERR(task);
8132 trace_nfs4_bind_conn_to_session(clp, status);
8134 if (memcmp(res.sessionid.data,
8135 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
8136 dprintk("NFS: %s: Session ID mismatch\n", __func__);
8139 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
8140 dprintk("NFS: %s: Unexpected direction from server\n",
8144 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
8145 dprintk("NFS: %s: Server returned RDMA mode = true\n",
8154 struct rpc_bind_conn_calldata {
8155 struct nfs_client *clp;
8156 const struct cred *cred;
8160 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
8161 struct rpc_xprt *xprt,
8164 struct rpc_bind_conn_calldata *p = calldata;
8166 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
8169 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
8171 struct rpc_bind_conn_calldata data = {
8175 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
8176 nfs4_proc_bind_conn_to_session_callback, &data);
8180 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
8181 * and operations we'd like to see to enable certain features in the allow map
8183 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
8184 .how = SP4_MACH_CRED,
8185 .enforce.u.words = {
8186 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8187 1 << (OP_EXCHANGE_ID - 32) |
8188 1 << (OP_CREATE_SESSION - 32) |
8189 1 << (OP_DESTROY_SESSION - 32) |
8190 1 << (OP_DESTROY_CLIENTID - 32)
8193 [0] = 1 << (OP_CLOSE) |
8194 1 << (OP_OPEN_DOWNGRADE) |
8196 1 << (OP_DELEGRETURN) |
8198 [1] = 1 << (OP_SECINFO - 32) |
8199 1 << (OP_SECINFO_NO_NAME - 32) |
8200 1 << (OP_LAYOUTRETURN - 32) |
8201 1 << (OP_TEST_STATEID - 32) |
8202 1 << (OP_FREE_STATEID - 32) |
8203 1 << (OP_WRITE - 32)
8208 * Select the state protection mode for client `clp' given the server results
8209 * from exchange_id in `sp'.
8211 * Returns 0 on success, negative errno otherwise.
8213 static int nfs4_sp4_select_mode(struct nfs_client *clp,
8214 struct nfs41_state_protection *sp)
8216 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
8217 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8218 1 << (OP_EXCHANGE_ID - 32) |
8219 1 << (OP_CREATE_SESSION - 32) |
8220 1 << (OP_DESTROY_SESSION - 32) |
8221 1 << (OP_DESTROY_CLIENTID - 32)
8223 unsigned long flags = 0;
8227 if (sp->how == SP4_MACH_CRED) {
8228 /* Print state protect result */
8229 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
8230 for (i = 0; i <= LAST_NFS4_OP; i++) {
8231 if (test_bit(i, sp->enforce.u.longs))
8232 dfprintk(MOUNT, " enforce op %d\n", i);
8233 if (test_bit(i, sp->allow.u.longs))
8234 dfprintk(MOUNT, " allow op %d\n", i);
8237 /* make sure nothing is on enforce list that isn't supported */
8238 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
8239 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
8240 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
8247 * Minimal mode - state operations are allowed to use machine
8248 * credential. Note this already happens by default, so the
8249 * client doesn't have to do anything more than the negotiation.
8251 * NOTE: we don't care if EXCHANGE_ID is in the list -
8252 * we're already using the machine cred for exchange_id
8253 * and will never use a different cred.
8255 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
8256 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
8257 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
8258 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
8259 dfprintk(MOUNT, "sp4_mach_cred:\n");
8260 dfprintk(MOUNT, " minimal mode enabled\n");
8261 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
8263 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
8268 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
8269 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
8270 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
8271 test_bit(OP_LOCKU, sp->allow.u.longs)) {
8272 dfprintk(MOUNT, " cleanup mode enabled\n");
8273 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
8276 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
8277 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
8278 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
8281 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
8282 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
8283 dfprintk(MOUNT, " secinfo mode enabled\n");
8284 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
8287 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
8288 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
8289 dfprintk(MOUNT, " stateid mode enabled\n");
8290 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
8293 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
8294 dfprintk(MOUNT, " write mode enabled\n");
8295 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
8298 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
8299 dfprintk(MOUNT, " commit mode enabled\n");
8300 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
8304 clp->cl_sp4_flags = flags;
8308 struct nfs41_exchange_id_data {
8309 struct nfs41_exchange_id_res res;
8310 struct nfs41_exchange_id_args args;
8313 static void nfs4_exchange_id_release(void *data)
8315 struct nfs41_exchange_id_data *cdata =
8316 (struct nfs41_exchange_id_data *)data;
8318 nfs_put_client(cdata->args.client);
8319 kfree(cdata->res.impl_id);
8320 kfree(cdata->res.server_scope);
8321 kfree(cdata->res.server_owner);
8325 static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
8326 .rpc_release = nfs4_exchange_id_release,
8330 * _nfs4_proc_exchange_id()
8332 * Wrapper for EXCHANGE_ID operation.
8334 static struct rpc_task *
8335 nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
8336 u32 sp4_how, struct rpc_xprt *xprt)
8338 struct rpc_message msg = {
8339 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
8342 struct rpc_task_setup task_setup_data = {
8343 .rpc_client = clp->cl_rpcclient,
8344 .callback_ops = &nfs4_exchange_id_call_ops,
8345 .rpc_message = &msg,
8346 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
8348 struct nfs41_exchange_id_data *calldata;
8351 if (!refcount_inc_not_zero(&clp->cl_count))
8352 return ERR_PTR(-EIO);
8355 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8359 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
8361 status = nfs4_init_uniform_client_string(clp);
8365 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
8368 if (unlikely(calldata->res.server_owner == NULL))
8371 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
8373 if (unlikely(calldata->res.server_scope == NULL))
8374 goto out_server_owner;
8376 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
8377 if (unlikely(calldata->res.impl_id == NULL))
8378 goto out_server_scope;
8382 calldata->args.state_protect.how = SP4_NONE;
8386 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
8396 task_setup_data.rpc_xprt = xprt;
8397 task_setup_data.flags |= RPC_TASK_SOFTCONN;
8398 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
8399 sizeof(calldata->args.verifier.data));
8401 calldata->args.client = clp;
8402 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
8403 EXCHGID4_FLAG_BIND_PRINC_STATEID;
8404 #ifdef CONFIG_NFS_V4_1_MIGRATION
8405 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
8407 msg.rpc_argp = &calldata->args;
8408 msg.rpc_resp = &calldata->res;
8409 task_setup_data.callback_data = calldata;
8411 return rpc_run_task(&task_setup_data);
8414 kfree(calldata->res.impl_id);
8416 kfree(calldata->res.server_scope);
8418 kfree(calldata->res.server_owner);
8422 nfs_put_client(clp);
8423 return ERR_PTR(status);
8427 * _nfs4_proc_exchange_id()
8429 * Wrapper for EXCHANGE_ID operation.
8431 static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
8434 struct rpc_task *task;
8435 struct nfs41_exchange_id_args *argp;
8436 struct nfs41_exchange_id_res *resp;
8437 unsigned long now = jiffies;
8440 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
8442 return PTR_ERR(task);
8444 argp = task->tk_msg.rpc_argp;
8445 resp = task->tk_msg.rpc_resp;
8446 status = task->tk_status;
8450 status = nfs4_check_cl_exchange_flags(resp->flags);
8454 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8458 do_renew_lease(clp, now);
8460 clp->cl_clientid = resp->clientid;
8461 clp->cl_exchange_flags = resp->flags;
8462 clp->cl_seqid = resp->seqid;
8463 /* Client ID is not confirmed */
8464 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8465 clear_bit(NFS4_SESSION_ESTABLISHED,
8466 &clp->cl_session->session_state);
8468 if (clp->cl_serverscope != NULL &&
8469 !nfs41_same_server_scope(clp->cl_serverscope,
8470 resp->server_scope)) {
8471 dprintk("%s: server_scope mismatch detected\n",
8473 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8476 swap(clp->cl_serverowner, resp->server_owner);
8477 swap(clp->cl_serverscope, resp->server_scope);
8478 swap(clp->cl_implid, resp->impl_id);
8480 /* Save the EXCHANGE_ID verifier session trunk tests */
8481 memcpy(clp->cl_confirm.data, argp->verifier.data,
8482 sizeof(clp->cl_confirm.data));
8484 trace_nfs4_exchange_id(clp, status);
8490 * nfs4_proc_exchange_id()
8492 * Returns zero, a negative errno, or a negative NFS4ERR status code.
8494 * Since the clientid has expired, all compounds using sessions
8495 * associated with the stale clientid will be returning
8496 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
8497 * be in some phase of session reset.
8499 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
8501 int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
8503 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8506 /* try SP4_MACH_CRED if krb5i/p */
8507 if (authflavor == RPC_AUTH_GSS_KRB5I ||
8508 authflavor == RPC_AUTH_GSS_KRB5P) {
8509 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
8515 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
8519 * nfs4_test_session_trunk
8521 * This is an add_xprt_test() test function called from
8522 * rpc_clnt_setup_test_and_add_xprt.
8524 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8525 * and is dereferrenced in nfs4_exchange_id_release
8527 * Upon success, add the new transport to the rpc_clnt
8529 * @clnt: struct rpc_clnt to get new transport
8530 * @xprt: the rpc_xprt to test
8531 * @data: call data for _nfs4_proc_exchange_id.
8533 void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
8536 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
8537 struct rpc_task *task;
8542 dprintk("--> %s try %s\n", __func__,
8543 xprt->address_strings[RPC_DISPLAY_ADDR]);
8545 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8547 /* Test connection for session trunking. Async exchange_id call */
8548 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8552 status = task->tk_status;
8554 status = nfs4_detect_session_trunking(adata->clp,
8555 task->tk_msg.rpc_resp, xprt);
8558 rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
8562 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
8564 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
8565 const struct cred *cred)
8567 struct rpc_message msg = {
8568 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8574 status = rpc_call_sync(clp->cl_rpcclient, &msg,
8575 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
8576 trace_nfs4_destroy_clientid(clp, status);
8578 dprintk("NFS: Got error %d from the server %s on "
8579 "DESTROY_CLIENTID.", status, clp->cl_hostname);
8583 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
8584 const struct cred *cred)
8589 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8590 ret = _nfs4_proc_destroy_clientid(clp, cred);
8592 case -NFS4ERR_DELAY:
8593 case -NFS4ERR_CLIENTID_BUSY:
8603 int nfs4_destroy_clientid(struct nfs_client *clp)
8605 const struct cred *cred;
8608 if (clp->cl_mvops->minor_version < 1)
8610 if (clp->cl_exchange_flags == 0)
8612 if (clp->cl_preserve_clid)
8614 cred = nfs4_get_clid_cred(clp);
8615 ret = nfs4_proc_destroy_clientid(clp, cred);
8619 case -NFS4ERR_STALE_CLIENTID:
8620 clp->cl_exchange_flags = 0;
8626 #endif /* CONFIG_NFS_V4_1 */
8628 struct nfs4_get_lease_time_data {
8629 struct nfs4_get_lease_time_args *args;
8630 struct nfs4_get_lease_time_res *res;
8631 struct nfs_client *clp;
8634 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
8637 struct nfs4_get_lease_time_data *data =
8638 (struct nfs4_get_lease_time_data *)calldata;
8640 dprintk("--> %s\n", __func__);
8641 /* just setup sequence, do not trigger session recovery
8642 since we're invoked within one */
8643 nfs4_setup_sequence(data->clp,
8644 &data->args->la_seq_args,
8645 &data->res->lr_seq_res,
8647 dprintk("<-- %s\n", __func__);
8651 * Called from nfs4_state_manager thread for session setup, so don't recover
8652 * from sequence operation or clientid errors.
8654 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8656 struct nfs4_get_lease_time_data *data =
8657 (struct nfs4_get_lease_time_data *)calldata;
8659 dprintk("--> %s\n", __func__);
8660 if (!nfs4_sequence_done(task, &data->res->lr_seq_res))
8662 switch (task->tk_status) {
8663 case -NFS4ERR_DELAY:
8664 case -NFS4ERR_GRACE:
8665 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
8666 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8667 task->tk_status = 0;
8669 case -NFS4ERR_RETRY_UNCACHED_REP:
8670 rpc_restart_call_prepare(task);
8673 dprintk("<-- %s\n", __func__);
8676 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
8677 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8678 .rpc_call_done = nfs4_get_lease_time_done,
8681 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8683 struct nfs4_get_lease_time_args args;
8684 struct nfs4_get_lease_time_res res = {
8685 .lr_fsinfo = fsinfo,
8687 struct nfs4_get_lease_time_data data = {
8692 struct rpc_message msg = {
8693 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8697 struct rpc_task_setup task_setup = {
8698 .rpc_client = clp->cl_rpcclient,
8699 .rpc_message = &msg,
8700 .callback_ops = &nfs4_get_lease_time_ops,
8701 .callback_data = &data,
8702 .flags = RPC_TASK_TIMEOUT,
8705 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
8706 return nfs4_call_sync_custom(&task_setup);
8709 #ifdef CONFIG_NFS_V4_1
8712 * Initialize the values to be used by the client in CREATE_SESSION
8713 * If nfs4_init_session set the fore channel request and response sizes,
8716 * Set the back channel max_resp_sz_cached to zero to force the client to
8717 * always set csa_cachethis to FALSE because the current implementation
8718 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8720 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8721 struct rpc_clnt *clnt)
8723 unsigned int max_rqst_sz, max_resp_sz;
8724 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
8725 unsigned int max_bc_slots = rpc_num_bc_slots(clnt);
8727 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8728 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8730 /* Fore channel attributes */
8731 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8732 args->fc_attrs.max_resp_sz = max_resp_sz;
8733 args->fc_attrs.max_ops = NFS4_MAX_OPS;
8734 args->fc_attrs.max_reqs = max_session_slots;
8736 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
8737 "max_ops=%u max_reqs=%u\n",
8739 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
8740 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
8742 /* Back channel attributes */
8743 args->bc_attrs.max_rqst_sz = max_bc_payload;
8744 args->bc_attrs.max_resp_sz = max_bc_payload;
8745 args->bc_attrs.max_resp_sz_cached = 0;
8746 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
8747 args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
8748 if (args->bc_attrs.max_reqs > max_bc_slots)
8749 args->bc_attrs.max_reqs = max_bc_slots;
8751 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8752 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8754 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8755 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8756 args->bc_attrs.max_reqs);
8759 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8760 struct nfs41_create_session_res *res)
8762 struct nfs4_channel_attrs *sent = &args->fc_attrs;
8763 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
8765 if (rcvd->max_resp_sz > sent->max_resp_sz)
8768 * Our requested max_ops is the minimum we need; we're not
8769 * prepared to break up compounds into smaller pieces than that.
8770 * So, no point even trying to continue if the server won't
8773 if (rcvd->max_ops < sent->max_ops)
8775 if (rcvd->max_reqs == 0)
8777 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8778 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
8782 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8783 struct nfs41_create_session_res *res)
8785 struct nfs4_channel_attrs *sent = &args->bc_attrs;
8786 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
8788 if (!(res->flags & SESSION4_BACK_CHAN))
8790 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8792 if (rcvd->max_resp_sz < sent->max_resp_sz)
8794 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8796 if (rcvd->max_ops > sent->max_ops)
8798 if (rcvd->max_reqs > sent->max_reqs)
8804 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
8805 struct nfs41_create_session_res *res)
8809 ret = nfs4_verify_fore_channel_attrs(args, res);
8812 return nfs4_verify_back_channel_attrs(args, res);
8815 static void nfs4_update_session(struct nfs4_session *session,
8816 struct nfs41_create_session_res *res)
8818 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
8819 /* Mark client id and session as being confirmed */
8820 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8821 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
8822 session->flags = res->flags;
8823 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
8824 if (res->flags & SESSION4_BACK_CHAN)
8825 memcpy(&session->bc_attrs, &res->bc_attrs,
8826 sizeof(session->bc_attrs));
8829 static int _nfs4_proc_create_session(struct nfs_client *clp,
8830 const struct cred *cred)
8832 struct nfs4_session *session = clp->cl_session;
8833 struct nfs41_create_session_args args = {
8835 .clientid = clp->cl_clientid,
8836 .seqid = clp->cl_seqid,
8837 .cb_program = NFS4_CALLBACK,
8839 struct nfs41_create_session_res res;
8841 struct rpc_message msg = {
8842 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8849 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
8850 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
8852 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
8853 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
8854 trace_nfs4_create_session(clp, status);
8857 case -NFS4ERR_STALE_CLIENTID:
8858 case -NFS4ERR_DELAY:
8867 /* Verify the session's negotiated channel_attrs values */
8868 status = nfs4_verify_channel_attrs(&args, &res);
8869 /* Increment the clientid slot sequence id */
8872 nfs4_update_session(session, &res);
8879 * Issues a CREATE_SESSION operation to the server.
8880 * It is the responsibility of the caller to verify the session is
8881 * expired before calling this routine.
8883 int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
8887 struct nfs4_session *session = clp->cl_session;
8889 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8891 status = _nfs4_proc_create_session(clp, cred);
8895 /* Init or reset the session slot tables */
8896 status = nfs4_setup_session_slot_tables(session);
8897 dprintk("slot table setup returned %d\n", status);
8901 ptr = (unsigned *)&session->sess_id.data[0];
8902 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8903 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
8905 dprintk("<-- %s\n", __func__);
8910 * Issue the over-the-wire RPC DESTROY_SESSION.
8911 * The caller must serialize access to this routine.
8913 int nfs4_proc_destroy_session(struct nfs4_session *session,
8914 const struct cred *cred)
8916 struct rpc_message msg = {
8917 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8918 .rpc_argp = session,
8923 dprintk("--> nfs4_proc_destroy_session\n");
8925 /* session is still being setup */
8926 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8929 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
8930 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
8931 trace_nfs4_destroy_session(session->clp, status);
8934 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
8935 "Session has been destroyed regardless...\n", status);
8937 dprintk("<-- nfs4_proc_destroy_session\n");
8942 * Renew the cl_session lease.
8944 struct nfs4_sequence_data {
8945 struct nfs_client *clp;
8946 struct nfs4_sequence_args args;
8947 struct nfs4_sequence_res res;
8950 static void nfs41_sequence_release(void *data)
8952 struct nfs4_sequence_data *calldata = data;
8953 struct nfs_client *clp = calldata->clp;
8955 if (refcount_read(&clp->cl_count) > 1)
8956 nfs4_schedule_state_renewal(clp);
8957 nfs_put_client(clp);
8961 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8963 switch(task->tk_status) {
8964 case -NFS4ERR_DELAY:
8965 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8968 nfs4_schedule_lease_recovery(clp);
8973 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
8975 struct nfs4_sequence_data *calldata = data;
8976 struct nfs_client *clp = calldata->clp;
8978 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8981 trace_nfs4_sequence(clp, task->tk_status);
8982 if (task->tk_status < 0) {
8983 dprintk("%s ERROR %d\n", __func__, task->tk_status);
8984 if (refcount_read(&clp->cl_count) == 1)
8987 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8988 rpc_restart_call_prepare(task);
8992 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
8994 dprintk("<-- %s\n", __func__);
8997 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8999 struct nfs4_sequence_data *calldata = data;
9000 struct nfs_client *clp = calldata->clp;
9001 struct nfs4_sequence_args *args;
9002 struct nfs4_sequence_res *res;
9004 args = task->tk_msg.rpc_argp;
9005 res = task->tk_msg.rpc_resp;
9007 nfs4_setup_sequence(clp, args, res, task);
9010 static const struct rpc_call_ops nfs41_sequence_ops = {
9011 .rpc_call_done = nfs41_sequence_call_done,
9012 .rpc_call_prepare = nfs41_sequence_prepare,
9013 .rpc_release = nfs41_sequence_release,
9016 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
9017 const struct cred *cred,
9018 struct nfs4_slot *slot,
9021 struct nfs4_sequence_data *calldata;
9022 struct rpc_message msg = {
9023 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
9026 struct rpc_task_setup task_setup_data = {
9027 .rpc_client = clp->cl_rpcclient,
9028 .rpc_message = &msg,
9029 .callback_ops = &nfs41_sequence_ops,
9030 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
9032 struct rpc_task *ret;
9034 ret = ERR_PTR(-EIO);
9035 if (!refcount_inc_not_zero(&clp->cl_count))
9038 ret = ERR_PTR(-ENOMEM);
9039 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
9040 if (calldata == NULL)
9042 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
9043 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
9044 msg.rpc_argp = &calldata->args;
9045 msg.rpc_resp = &calldata->res;
9046 calldata->clp = clp;
9047 task_setup_data.callback_data = calldata;
9049 ret = rpc_run_task(&task_setup_data);
9054 nfs_put_client(clp);
9056 nfs41_release_slot(slot);
9060 static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
9062 struct rpc_task *task;
9065 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
9067 task = _nfs41_proc_sequence(clp, cred, NULL, false);
9069 ret = PTR_ERR(task);
9071 rpc_put_task_async(task);
9072 dprintk("<-- %s status=%d\n", __func__, ret);
9076 static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
9078 struct rpc_task *task;
9081 task = _nfs41_proc_sequence(clp, cred, NULL, true);
9083 ret = PTR_ERR(task);
9086 ret = rpc_wait_for_completion_task(task);
9088 ret = task->tk_status;
9091 dprintk("<-- %s status=%d\n", __func__, ret);
9095 struct nfs4_reclaim_complete_data {
9096 struct nfs_client *clp;
9097 struct nfs41_reclaim_complete_args arg;
9098 struct nfs41_reclaim_complete_res res;
9101 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
9103 struct nfs4_reclaim_complete_data *calldata = data;
9105 nfs4_setup_sequence(calldata->clp,
9106 &calldata->arg.seq_args,
9107 &calldata->res.seq_res,
9111 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9113 switch(task->tk_status) {
9115 wake_up_all(&clp->cl_lock_waitq);
9117 case -NFS4ERR_COMPLETE_ALREADY:
9118 case -NFS4ERR_WRONG_CRED: /* What to do here? */
9120 case -NFS4ERR_DELAY:
9121 rpc_delay(task, NFS4_POLL_RETRY_MAX);
9123 case -NFS4ERR_RETRY_UNCACHED_REP:
9125 case -NFS4ERR_BADSESSION:
9126 case -NFS4ERR_DEADSESSION:
9127 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9130 nfs4_schedule_lease_recovery(clp);
9135 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
9137 struct nfs4_reclaim_complete_data *calldata = data;
9138 struct nfs_client *clp = calldata->clp;
9139 struct nfs4_sequence_res *res = &calldata->res.seq_res;
9141 dprintk("--> %s\n", __func__);
9142 if (!nfs41_sequence_done(task, res))
9145 trace_nfs4_reclaim_complete(clp, task->tk_status);
9146 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
9147 rpc_restart_call_prepare(task);
9150 dprintk("<-- %s\n", __func__);
9153 static void nfs4_free_reclaim_complete_data(void *data)
9155 struct nfs4_reclaim_complete_data *calldata = data;
9160 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
9161 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
9162 .rpc_call_done = nfs4_reclaim_complete_done,
9163 .rpc_release = nfs4_free_reclaim_complete_data,
9167 * Issue a global reclaim complete.
9169 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
9170 const struct cred *cred)
9172 struct nfs4_reclaim_complete_data *calldata;
9173 struct rpc_message msg = {
9174 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
9177 struct rpc_task_setup task_setup_data = {
9178 .rpc_client = clp->cl_rpcclient,
9179 .rpc_message = &msg,
9180 .callback_ops = &nfs4_reclaim_complete_call_ops,
9181 .flags = RPC_TASK_NO_ROUND_ROBIN,
9183 int status = -ENOMEM;
9185 dprintk("--> %s\n", __func__);
9186 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
9187 if (calldata == NULL)
9189 calldata->clp = clp;
9190 calldata->arg.one_fs = 0;
9192 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
9193 msg.rpc_argp = &calldata->arg;
9194 msg.rpc_resp = &calldata->res;
9195 task_setup_data.callback_data = calldata;
9196 status = nfs4_call_sync_custom(&task_setup_data);
9198 dprintk("<-- %s status=%d\n", __func__, status);
9203 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
9205 struct nfs4_layoutget *lgp = calldata;
9206 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
9208 dprintk("--> %s\n", __func__);
9209 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
9210 &lgp->res.seq_res, task);
9211 dprintk("<-- %s\n", __func__);
9214 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
9216 struct nfs4_layoutget *lgp = calldata;
9218 dprintk("--> %s\n", __func__);
9219 nfs41_sequence_process(task, &lgp->res.seq_res);
9220 dprintk("<-- %s\n", __func__);
9224 nfs4_layoutget_handle_exception(struct rpc_task *task,
9225 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
9227 struct inode *inode = lgp->args.inode;
9228 struct nfs_server *server = NFS_SERVER(inode);
9229 struct pnfs_layout_hdr *lo;
9230 int nfs4err = task->tk_status;
9231 int err, status = 0;
9234 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
9236 nfs4_sequence_free_slot(&lgp->res.seq_res);
9243 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
9244 * on the file. set tk_status to -ENODATA to tell upper layer to
9247 case -NFS4ERR_LAYOUTUNAVAILABLE:
9251 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
9252 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
9254 case -NFS4ERR_BADLAYOUT:
9255 status = -EOVERFLOW;
9258 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
9259 * (or clients) writing to the same RAID stripe except when
9260 * the minlength argument is 0 (see RFC5661 section 18.43.3).
9262 * Treat it like we would RECALLCONFLICT -- we retry for a little
9263 * while, and then eventually give up.
9265 case -NFS4ERR_LAYOUTTRYLATER:
9266 if (lgp->args.minlength == 0) {
9267 status = -EOVERFLOW;
9272 case -NFS4ERR_RECALLCONFLICT:
9273 status = -ERECALLCONFLICT;
9275 case -NFS4ERR_DELEG_REVOKED:
9276 case -NFS4ERR_ADMIN_REVOKED:
9277 case -NFS4ERR_EXPIRED:
9278 case -NFS4ERR_BAD_STATEID:
9279 exception->timeout = 0;
9280 spin_lock(&inode->i_lock);
9281 lo = NFS_I(inode)->layout;
9282 /* If the open stateid was bad, then recover it. */
9283 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
9284 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
9285 spin_unlock(&inode->i_lock);
9286 exception->state = lgp->args.ctx->state;
9287 exception->stateid = &lgp->args.stateid;
9292 * Mark the bad layout state as invalid, then retry
9294 pnfs_mark_layout_stateid_invalid(lo, &head);
9295 spin_unlock(&inode->i_lock);
9296 nfs_commit_inode(inode, 0);
9297 pnfs_free_lseg_list(&head);
9302 err = nfs4_handle_exception(server, nfs4err, exception);
9304 if (exception->retry)
9310 dprintk("<-- %s\n", __func__);
9314 size_t max_response_pages(struct nfs_server *server)
9316 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
9317 return nfs_page_array_len(0, max_resp_sz);
9320 static void nfs4_layoutget_release(void *calldata)
9322 struct nfs4_layoutget *lgp = calldata;
9324 dprintk("--> %s\n", __func__);
9325 nfs4_sequence_free_slot(&lgp->res.seq_res);
9326 pnfs_layoutget_free(lgp);
9327 dprintk("<-- %s\n", __func__);
9330 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
9331 .rpc_call_prepare = nfs4_layoutget_prepare,
9332 .rpc_call_done = nfs4_layoutget_done,
9333 .rpc_release = nfs4_layoutget_release,
9336 struct pnfs_layout_segment *
9337 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
9339 struct inode *inode = lgp->args.inode;
9340 struct nfs_server *server = NFS_SERVER(inode);
9341 struct rpc_task *task;
9342 struct rpc_message msg = {
9343 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
9344 .rpc_argp = &lgp->args,
9345 .rpc_resp = &lgp->res,
9346 .rpc_cred = lgp->cred,
9348 struct rpc_task_setup task_setup_data = {
9349 .rpc_client = server->client,
9350 .rpc_message = &msg,
9351 .callback_ops = &nfs4_layoutget_call_ops,
9352 .callback_data = lgp,
9353 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
9355 struct pnfs_layout_segment *lseg = NULL;
9356 struct nfs4_exception exception = {
9358 .timeout = *timeout,
9362 dprintk("--> %s\n", __func__);
9364 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
9365 pnfs_get_layout_hdr(NFS_I(inode)->layout);
9367 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
9369 task = rpc_run_task(&task_setup_data);
9371 status = rpc_wait_for_completion_task(task);
9375 if (task->tk_status < 0) {
9376 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
9377 *timeout = exception.timeout;
9378 } else if (lgp->res.layoutp->len == 0) {
9380 *timeout = nfs4_update_delay(&exception.timeout);
9382 lseg = pnfs_layout_process(lgp);
9384 trace_nfs4_layoutget(lgp->args.ctx,
9391 dprintk("<-- %s status=%d\n", __func__, status);
9393 return ERR_PTR(status);
9398 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
9400 struct nfs4_layoutreturn *lrp = calldata;
9402 dprintk("--> %s\n", __func__);
9403 nfs4_setup_sequence(lrp->clp,
9404 &lrp->args.seq_args,
9407 if (!pnfs_layout_is_valid(lrp->args.layout))
9411 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
9413 struct nfs4_layoutreturn *lrp = calldata;
9414 struct nfs_server *server;
9416 dprintk("--> %s\n", __func__);
9418 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
9422 * Was there an RPC level error? Assume the call succeeded,
9423 * and that we need to release the layout
9425 if (task->tk_rpc_status != 0 && RPC_WAS_SENT(task)) {
9426 lrp->res.lrs_present = 0;
9430 server = NFS_SERVER(lrp->args.inode);
9431 switch (task->tk_status) {
9432 case -NFS4ERR_OLD_STATEID:
9433 if (nfs4_layout_refresh_old_stateid(&lrp->args.stateid,
9439 task->tk_status = 0;
9443 case -NFS4ERR_DELAY:
9444 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
9448 dprintk("<-- %s\n", __func__);
9451 task->tk_status = 0;
9452 nfs4_sequence_free_slot(&lrp->res.seq_res);
9453 rpc_restart_call_prepare(task);
9456 static void nfs4_layoutreturn_release(void *calldata)
9458 struct nfs4_layoutreturn *lrp = calldata;
9459 struct pnfs_layout_hdr *lo = lrp->args.layout;
9461 dprintk("--> %s\n", __func__);
9462 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
9463 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
9464 nfs4_sequence_free_slot(&lrp->res.seq_res);
9465 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9466 lrp->ld_private.ops->free(&lrp->ld_private);
9467 pnfs_put_layout_hdr(lrp->args.layout);
9468 nfs_iput_and_deactive(lrp->inode);
9469 put_cred(lrp->cred);
9471 dprintk("<-- %s\n", __func__);
9474 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9475 .rpc_call_prepare = nfs4_layoutreturn_prepare,
9476 .rpc_call_done = nfs4_layoutreturn_done,
9477 .rpc_release = nfs4_layoutreturn_release,
9480 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
9482 struct rpc_task *task;
9483 struct rpc_message msg = {
9484 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9485 .rpc_argp = &lrp->args,
9486 .rpc_resp = &lrp->res,
9487 .rpc_cred = lrp->cred,
9489 struct rpc_task_setup task_setup_data = {
9490 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
9491 .rpc_message = &msg,
9492 .callback_ops = &nfs4_layoutreturn_call_ops,
9493 .callback_data = lrp,
9497 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9498 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9499 &task_setup_data.rpc_client, &msg);
9501 dprintk("--> %s\n", __func__);
9503 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
9505 nfs4_layoutreturn_release(lrp);
9508 task_setup_data.flags |= RPC_TASK_ASYNC;
9510 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1, 0);
9511 task = rpc_run_task(&task_setup_data);
9513 return PTR_ERR(task);
9515 status = task->tk_status;
9516 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
9517 dprintk("<-- %s status=%d\n", __func__, status);
9523 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
9524 struct pnfs_device *pdev,
9525 const struct cred *cred)
9527 struct nfs4_getdeviceinfo_args args = {
9529 .notify_types = NOTIFY_DEVICEID4_CHANGE |
9530 NOTIFY_DEVICEID4_DELETE,
9532 struct nfs4_getdeviceinfo_res res = {
9535 struct rpc_message msg = {
9536 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9543 dprintk("--> %s\n", __func__);
9544 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
9545 if (res.notification & ~args.notify_types)
9546 dprintk("%s: unsupported notification\n", __func__);
9547 if (res.notification != args.notify_types)
9550 dprintk("<-- %s status=%d\n", __func__, status);
9555 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9556 struct pnfs_device *pdev,
9557 const struct cred *cred)
9559 struct nfs4_exception exception = { };
9563 err = nfs4_handle_exception(server,
9564 _nfs4_proc_getdeviceinfo(server, pdev, cred),
9566 } while (exception.retry);
9569 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9571 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9573 struct nfs4_layoutcommit_data *data = calldata;
9574 struct nfs_server *server = NFS_SERVER(data->args.inode);
9576 nfs4_setup_sequence(server->nfs_client,
9577 &data->args.seq_args,
9583 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9585 struct nfs4_layoutcommit_data *data = calldata;
9586 struct nfs_server *server = NFS_SERVER(data->args.inode);
9588 if (!nfs41_sequence_done(task, &data->res.seq_res))
9591 switch (task->tk_status) { /* Just ignore these failures */
9592 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9593 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9594 case -NFS4ERR_BADLAYOUT: /* no layout */
9595 case -NFS4ERR_GRACE: /* loca_recalim always false */
9596 task->tk_status = 0;
9600 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
9601 rpc_restart_call_prepare(task);
9607 static void nfs4_layoutcommit_release(void *calldata)
9609 struct nfs4_layoutcommit_data *data = calldata;
9611 pnfs_cleanup_layoutcommit(data);
9612 nfs_post_op_update_inode_force_wcc(data->args.inode,
9614 put_cred(data->cred);
9615 nfs_iput_and_deactive(data->inode);
9619 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9620 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9621 .rpc_call_done = nfs4_layoutcommit_done,
9622 .rpc_release = nfs4_layoutcommit_release,
9626 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
9628 struct rpc_message msg = {
9629 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9630 .rpc_argp = &data->args,
9631 .rpc_resp = &data->res,
9632 .rpc_cred = data->cred,
9634 struct rpc_task_setup task_setup_data = {
9635 .task = &data->task,
9636 .rpc_client = NFS_CLIENT(data->args.inode),
9637 .rpc_message = &msg,
9638 .callback_ops = &nfs4_layoutcommit_ops,
9639 .callback_data = data,
9641 struct rpc_task *task;
9644 dprintk("NFS: initiating layoutcommit call. sync %d "
9645 "lbw: %llu inode %lu\n", sync,
9646 data->args.lastbytewritten,
9647 data->args.inode->i_ino);
9650 data->inode = nfs_igrab_and_active(data->args.inode);
9651 if (data->inode == NULL) {
9652 nfs4_layoutcommit_release(data);
9655 task_setup_data.flags = RPC_TASK_ASYNC;
9657 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
9658 task = rpc_run_task(&task_setup_data);
9660 return PTR_ERR(task);
9662 status = task->tk_status;
9663 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
9664 dprintk("%s: status %d\n", __func__, status);
9670 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9671 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9674 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9675 struct nfs_fsinfo *info,
9676 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
9678 struct nfs41_secinfo_no_name_args args = {
9679 .style = SECINFO_STYLE_CURRENT_FH,
9681 struct nfs4_secinfo_res res = {
9684 struct rpc_message msg = {
9685 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9689 struct rpc_clnt *clnt = server->client;
9690 struct nfs4_call_sync_data data = {
9691 .seq_server = server,
9692 .seq_args = &args.seq_args,
9693 .seq_res = &res.seq_res,
9695 struct rpc_task_setup task_setup = {
9696 .rpc_client = server->client,
9697 .rpc_message = &msg,
9698 .callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
9699 .callback_data = &data,
9700 .flags = RPC_TASK_NO_ROUND_ROBIN,
9702 const struct cred *cred = NULL;
9705 if (use_integrity) {
9706 clnt = server->nfs_client->cl_rpcclient;
9707 task_setup.rpc_client = clnt;
9709 cred = nfs4_get_clid_cred(server->nfs_client);
9710 msg.rpc_cred = cred;
9713 dprintk("--> %s\n", __func__);
9714 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
9715 status = nfs4_call_sync_custom(&task_setup);
9716 dprintk("<-- %s status=%d\n", __func__, status);
9724 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9725 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9727 struct nfs4_exception exception = {
9728 .interruptible = true,
9732 /* first try using integrity protection */
9733 err = -NFS4ERR_WRONGSEC;
9735 /* try to use integrity protection with machine cred */
9736 if (_nfs4_is_integrity_protected(server->nfs_client))
9737 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9741 * if unable to use integrity protection, or SECINFO with
9742 * integrity protection returns NFS4ERR_WRONGSEC (which is
9743 * disallowed by spec, but exists in deployed servers) use
9744 * the current filesystem's rpc_client and the user cred.
9746 if (err == -NFS4ERR_WRONGSEC)
9747 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9752 case -NFS4ERR_WRONGSEC:
9756 err = nfs4_handle_exception(server, err, &exception);
9758 } while (exception.retry);
9764 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9765 struct nfs_fsinfo *info)
9769 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
9770 struct nfs4_secinfo_flavors *flavors;
9771 struct nfs4_secinfo4 *secinfo;
9774 page = alloc_page(GFP_KERNEL);
9780 flavors = page_address(page);
9781 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9784 * Fall back on "guess and check" method if
9785 * the server doesn't support SECINFO_NO_NAME
9787 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
9788 err = nfs4_find_root_sec(server, fhandle, info);
9794 for (i = 0; i < flavors->num_flavors; i++) {
9795 secinfo = &flavors->flavors[i];
9797 switch (secinfo->flavor) {
9801 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9802 &secinfo->flavor_info);
9805 flavor = RPC_AUTH_MAXFLAVOR;
9809 if (!nfs_auth_info_match(&server->auth_info, flavor))
9810 flavor = RPC_AUTH_MAXFLAVOR;
9812 if (flavor != RPC_AUTH_MAXFLAVOR) {
9813 err = nfs4_lookup_root_sec(server, fhandle,
9820 if (flavor == RPC_AUTH_MAXFLAVOR)
9831 static int _nfs41_test_stateid(struct nfs_server *server,
9832 nfs4_stateid *stateid,
9833 const struct cred *cred)
9836 struct nfs41_test_stateid_args args = {
9839 struct nfs41_test_stateid_res res;
9840 struct rpc_message msg = {
9841 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9846 struct rpc_clnt *rpc_client = server->client;
9848 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9851 dprintk("NFS call test_stateid %p\n", stateid);
9852 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
9853 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
9854 &args.seq_args, &res.seq_res);
9855 if (status != NFS_OK) {
9856 dprintk("NFS reply test_stateid: failed, %d\n", status);
9859 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
9863 static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9864 int err, struct nfs4_exception *exception)
9866 exception->retry = 0;
9868 case -NFS4ERR_DELAY:
9869 case -NFS4ERR_RETRY_UNCACHED_REP:
9870 nfs4_handle_exception(server, err, exception);
9872 case -NFS4ERR_BADSESSION:
9873 case -NFS4ERR_BADSLOT:
9874 case -NFS4ERR_BAD_HIGH_SLOT:
9875 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9876 case -NFS4ERR_DEADSESSION:
9877 nfs4_do_handle_exception(server, err, exception);
9882 * nfs41_test_stateid - perform a TEST_STATEID operation
9884 * @server: server / transport on which to perform the operation
9885 * @stateid: state ID to test
9888 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9889 * Otherwise a negative NFS4ERR value is returned if the operation
9890 * failed or the state ID is not currently valid.
9892 static int nfs41_test_stateid(struct nfs_server *server,
9893 nfs4_stateid *stateid,
9894 const struct cred *cred)
9896 struct nfs4_exception exception = {
9897 .interruptible = true,
9901 err = _nfs41_test_stateid(server, stateid, cred);
9902 nfs4_handle_delay_or_session_error(server, err, &exception);
9903 } while (exception.retry);
9907 struct nfs_free_stateid_data {
9908 struct nfs_server *server;
9909 struct nfs41_free_stateid_args args;
9910 struct nfs41_free_stateid_res res;
9913 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9915 struct nfs_free_stateid_data *data = calldata;
9916 nfs4_setup_sequence(data->server->nfs_client,
9917 &data->args.seq_args,
9922 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9924 struct nfs_free_stateid_data *data = calldata;
9926 nfs41_sequence_done(task, &data->res.seq_res);
9928 switch (task->tk_status) {
9929 case -NFS4ERR_DELAY:
9930 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
9931 rpc_restart_call_prepare(task);
9935 static void nfs41_free_stateid_release(void *calldata)
9940 static const struct rpc_call_ops nfs41_free_stateid_ops = {
9941 .rpc_call_prepare = nfs41_free_stateid_prepare,
9942 .rpc_call_done = nfs41_free_stateid_done,
9943 .rpc_release = nfs41_free_stateid_release,
9947 * nfs41_free_stateid - perform a FREE_STATEID operation
9949 * @server: server / transport on which to perform the operation
9950 * @stateid: state ID to release
9952 * @privileged: set to true if this call needs to be privileged
9954 * Note: this function is always asynchronous.
9956 static int nfs41_free_stateid(struct nfs_server *server,
9957 const nfs4_stateid *stateid,
9958 const struct cred *cred,
9961 struct rpc_message msg = {
9962 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
9965 struct rpc_task_setup task_setup = {
9966 .rpc_client = server->client,
9967 .rpc_message = &msg,
9968 .callback_ops = &nfs41_free_stateid_ops,
9969 .flags = RPC_TASK_ASYNC,
9971 struct nfs_free_stateid_data *data;
9972 struct rpc_task *task;
9974 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9975 &task_setup.rpc_client, &msg);
9977 dprintk("NFS call free_stateid %p\n", stateid);
9978 data = kmalloc(sizeof(*data), GFP_NOFS);
9981 data->server = server;
9982 nfs4_stateid_copy(&data->args.stateid, stateid);
9984 task_setup.callback_data = data;
9986 msg.rpc_argp = &data->args;
9987 msg.rpc_resp = &data->res;
9988 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
9989 task = rpc_run_task(&task_setup);
9991 return PTR_ERR(task);
9997 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
9999 const struct cred *cred = lsp->ls_state->owner->so_cred;
10001 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
10002 nfs4_free_lock_state(server, lsp);
10005 static bool nfs41_match_stateid(const nfs4_stateid *s1,
10006 const nfs4_stateid *s2)
10008 if (s1->type != s2->type)
10011 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
10014 if (s1->seqid == s2->seqid)
10017 return s1->seqid == 0 || s2->seqid == 0;
10020 #endif /* CONFIG_NFS_V4_1 */
10022 static bool nfs4_match_stateid(const nfs4_stateid *s1,
10023 const nfs4_stateid *s2)
10025 return nfs4_stateid_match(s1, s2);
10029 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
10030 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
10031 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
10032 .recover_open = nfs4_open_reclaim,
10033 .recover_lock = nfs4_lock_reclaim,
10034 .establish_clid = nfs4_init_clientid,
10035 .detect_trunking = nfs40_discover_server_trunking,
10038 #if defined(CONFIG_NFS_V4_1)
10039 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
10040 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
10041 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
10042 .recover_open = nfs4_open_reclaim,
10043 .recover_lock = nfs4_lock_reclaim,
10044 .establish_clid = nfs41_init_clientid,
10045 .reclaim_complete = nfs41_proc_reclaim_complete,
10046 .detect_trunking = nfs41_discover_server_trunking,
10048 #endif /* CONFIG_NFS_V4_1 */
10050 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
10051 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
10052 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
10053 .recover_open = nfs40_open_expired,
10054 .recover_lock = nfs4_lock_expired,
10055 .establish_clid = nfs4_init_clientid,
10058 #if defined(CONFIG_NFS_V4_1)
10059 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
10060 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
10061 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
10062 .recover_open = nfs41_open_expired,
10063 .recover_lock = nfs41_lock_expired,
10064 .establish_clid = nfs41_init_clientid,
10066 #endif /* CONFIG_NFS_V4_1 */
10068 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
10069 .sched_state_renewal = nfs4_proc_async_renew,
10070 .get_state_renewal_cred = nfs4_get_renew_cred,
10071 .renew_lease = nfs4_proc_renew,
10074 #if defined(CONFIG_NFS_V4_1)
10075 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
10076 .sched_state_renewal = nfs41_proc_async_sequence,
10077 .get_state_renewal_cred = nfs4_get_machine_cred,
10078 .renew_lease = nfs4_proc_sequence,
10082 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
10083 .get_locations = _nfs40_proc_get_locations,
10084 .fsid_present = _nfs40_proc_fsid_present,
10087 #if defined(CONFIG_NFS_V4_1)
10088 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
10089 .get_locations = _nfs41_proc_get_locations,
10090 .fsid_present = _nfs41_proc_fsid_present,
10092 #endif /* CONFIG_NFS_V4_1 */
10094 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
10095 .minor_version = 0,
10096 .init_caps = NFS_CAP_READDIRPLUS
10097 | NFS_CAP_ATOMIC_OPEN
10098 | NFS_CAP_POSIX_LOCK,
10099 .init_client = nfs40_init_client,
10100 .shutdown_client = nfs40_shutdown_client,
10101 .match_stateid = nfs4_match_stateid,
10102 .find_root_sec = nfs4_find_root_sec,
10103 .free_lock_state = nfs4_release_lockowner,
10104 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
10105 .alloc_seqid = nfs_alloc_seqid,
10106 .call_sync_ops = &nfs40_call_sync_ops,
10107 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
10108 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
10109 .state_renewal_ops = &nfs40_state_renewal_ops,
10110 .mig_recovery_ops = &nfs40_mig_recovery_ops,
10113 #if defined(CONFIG_NFS_V4_1)
10114 static struct nfs_seqid *
10115 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
10120 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
10121 .minor_version = 1,
10122 .init_caps = NFS_CAP_READDIRPLUS
10123 | NFS_CAP_ATOMIC_OPEN
10124 | NFS_CAP_POSIX_LOCK
10125 | NFS_CAP_STATEID_NFSV41
10126 | NFS_CAP_ATOMIC_OPEN_V1
10128 .init_client = nfs41_init_client,
10129 .shutdown_client = nfs41_shutdown_client,
10130 .match_stateid = nfs41_match_stateid,
10131 .find_root_sec = nfs41_find_root_sec,
10132 .free_lock_state = nfs41_free_lock_state,
10133 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
10134 .alloc_seqid = nfs_alloc_no_seqid,
10135 .session_trunk = nfs4_test_session_trunk,
10136 .call_sync_ops = &nfs41_call_sync_ops,
10137 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10138 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10139 .state_renewal_ops = &nfs41_state_renewal_ops,
10140 .mig_recovery_ops = &nfs41_mig_recovery_ops,
10144 #if defined(CONFIG_NFS_V4_2)
10145 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
10146 .minor_version = 2,
10147 .init_caps = NFS_CAP_READDIRPLUS
10148 | NFS_CAP_ATOMIC_OPEN
10149 | NFS_CAP_POSIX_LOCK
10150 | NFS_CAP_STATEID_NFSV41
10151 | NFS_CAP_ATOMIC_OPEN_V1
10155 | NFS_CAP_OFFLOAD_CANCEL
10156 | NFS_CAP_COPY_NOTIFY
10157 | NFS_CAP_DEALLOCATE
10159 | NFS_CAP_LAYOUTSTATS
10161 | NFS_CAP_LAYOUTERROR,
10162 .init_client = nfs41_init_client,
10163 .shutdown_client = nfs41_shutdown_client,
10164 .match_stateid = nfs41_match_stateid,
10165 .find_root_sec = nfs41_find_root_sec,
10166 .free_lock_state = nfs41_free_lock_state,
10167 .call_sync_ops = &nfs41_call_sync_ops,
10168 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
10169 .alloc_seqid = nfs_alloc_no_seqid,
10170 .session_trunk = nfs4_test_session_trunk,
10171 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10172 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10173 .state_renewal_ops = &nfs41_state_renewal_ops,
10174 .mig_recovery_ops = &nfs41_mig_recovery_ops,
10178 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
10179 [0] = &nfs_v4_0_minor_ops,
10180 #if defined(CONFIG_NFS_V4_1)
10181 [1] = &nfs_v4_1_minor_ops,
10183 #if defined(CONFIG_NFS_V4_2)
10184 [2] = &nfs_v4_2_minor_ops,
10188 static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
10190 ssize_t error, error2, error3;
10192 error = generic_listxattr(dentry, list, size);
10200 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
10209 error3 = nfs4_listxattr_nfs4_user(d_inode(dentry), list, size);
10213 return error + error2 + error3;
10216 static const struct inode_operations nfs4_dir_inode_operations = {
10217 .create = nfs_create,
10218 .lookup = nfs_lookup,
10219 .atomic_open = nfs_atomic_open,
10221 .unlink = nfs_unlink,
10222 .symlink = nfs_symlink,
10223 .mkdir = nfs_mkdir,
10224 .rmdir = nfs_rmdir,
10225 .mknod = nfs_mknod,
10226 .rename = nfs_rename,
10227 .permission = nfs_permission,
10228 .getattr = nfs_getattr,
10229 .setattr = nfs_setattr,
10230 .listxattr = nfs4_listxattr,
10233 static const struct inode_operations nfs4_file_inode_operations = {
10234 .permission = nfs_permission,
10235 .getattr = nfs_getattr,
10236 .setattr = nfs_setattr,
10237 .listxattr = nfs4_listxattr,
10240 const struct nfs_rpc_ops nfs_v4_clientops = {
10241 .version = 4, /* protocol version */
10242 .dentry_ops = &nfs4_dentry_operations,
10243 .dir_inode_ops = &nfs4_dir_inode_operations,
10244 .file_inode_ops = &nfs4_file_inode_operations,
10245 .file_ops = &nfs4_file_operations,
10246 .getroot = nfs4_proc_get_root,
10247 .submount = nfs4_submount,
10248 .try_get_tree = nfs4_try_get_tree,
10249 .getattr = nfs4_proc_getattr,
10250 .setattr = nfs4_proc_setattr,
10251 .lookup = nfs4_proc_lookup,
10252 .lookupp = nfs4_proc_lookupp,
10253 .access = nfs4_proc_access,
10254 .readlink = nfs4_proc_readlink,
10255 .create = nfs4_proc_create,
10256 .remove = nfs4_proc_remove,
10257 .unlink_setup = nfs4_proc_unlink_setup,
10258 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
10259 .unlink_done = nfs4_proc_unlink_done,
10260 .rename_setup = nfs4_proc_rename_setup,
10261 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
10262 .rename_done = nfs4_proc_rename_done,
10263 .link = nfs4_proc_link,
10264 .symlink = nfs4_proc_symlink,
10265 .mkdir = nfs4_proc_mkdir,
10266 .rmdir = nfs4_proc_rmdir,
10267 .readdir = nfs4_proc_readdir,
10268 .mknod = nfs4_proc_mknod,
10269 .statfs = nfs4_proc_statfs,
10270 .fsinfo = nfs4_proc_fsinfo,
10271 .pathconf = nfs4_proc_pathconf,
10272 .set_capabilities = nfs4_server_capabilities,
10273 .decode_dirent = nfs4_decode_dirent,
10274 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
10275 .read_setup = nfs4_proc_read_setup,
10276 .read_done = nfs4_read_done,
10277 .write_setup = nfs4_proc_write_setup,
10278 .write_done = nfs4_write_done,
10279 .commit_setup = nfs4_proc_commit_setup,
10280 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
10281 .commit_done = nfs4_commit_done,
10282 .lock = nfs4_proc_lock,
10283 .clear_acl_cache = nfs4_zap_acl_attr,
10284 .close_context = nfs4_close_context,
10285 .open_context = nfs4_atomic_open,
10286 .have_delegation = nfs4_have_delegation,
10287 .alloc_client = nfs4_alloc_client,
10288 .init_client = nfs4_init_client,
10289 .free_client = nfs4_free_client,
10290 .create_server = nfs4_create_server,
10291 .clone_server = nfs_clone_server,
10294 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
10295 .name = XATTR_NAME_NFSV4_ACL,
10296 .list = nfs4_xattr_list_nfs4_acl,
10297 .get = nfs4_xattr_get_nfs4_acl,
10298 .set = nfs4_xattr_set_nfs4_acl,
10301 #ifdef CONFIG_NFS_V4_2
10302 static const struct xattr_handler nfs4_xattr_nfs4_user_handler = {
10303 .prefix = XATTR_USER_PREFIX,
10304 .get = nfs4_xattr_get_nfs4_user,
10305 .set = nfs4_xattr_set_nfs4_user,
10309 const struct xattr_handler *nfs4_xattr_handlers[] = {
10310 &nfs4_xattr_nfs4_acl_handler,
10311 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
10312 &nfs4_xattr_nfs4_label_handler,
10314 #ifdef CONFIG_NFS_V4_2
10315 &nfs4_xattr_nfs4_user_handler,
10322 * c-basic-offset: 8