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"
70 #include "nfs4trace.h"
72 #define NFSDBG_FACILITY NFSDBG_PROC
74 #define NFS4_BITMASK_SZ 3
76 #define NFS4_POLL_RETRY_MIN (HZ/10)
77 #define NFS4_POLL_RETRY_MAX (15*HZ)
79 /* file attributes which can be mapped to nfs attributes */
80 #define NFS4_VALID_ATTRS (ATTR_MODE \
91 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
92 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
93 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
94 static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label, struct inode *inode);
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
262 const u32 nfs4_fs_locations_bitmap[3] = {
266 | FATTR4_WORD0_FILEID
267 | FATTR4_WORD0_FS_LOCATIONS,
269 | FATTR4_WORD1_OWNER_GROUP
270 | FATTR4_WORD1_RAWDEV
271 | FATTR4_WORD1_SPACE_USED
272 | FATTR4_WORD1_TIME_ACCESS
273 | FATTR4_WORD1_TIME_METADATA
274 | FATTR4_WORD1_TIME_MODIFY
275 | FATTR4_WORD1_MOUNTED_ON_FILEID,
278 static void nfs4_bitmap_copy_adjust(__u32 *dst, const __u32 *src,
281 unsigned long cache_validity;
283 memcpy(dst, src, NFS4_BITMASK_SZ*sizeof(*dst));
284 if (!inode || !nfs4_have_delegation(inode, FMODE_READ))
287 cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
288 if (!(cache_validity & NFS_INO_REVAL_FORCED))
289 cache_validity &= ~(NFS_INO_INVALID_CHANGE
290 | NFS_INO_INVALID_SIZE);
292 if (!(cache_validity & NFS_INO_INVALID_SIZE))
293 dst[0] &= ~FATTR4_WORD0_SIZE;
295 if (!(cache_validity & NFS_INO_INVALID_CHANGE))
296 dst[0] &= ~FATTR4_WORD0_CHANGE;
299 static void nfs4_bitmap_copy_adjust_setattr(__u32 *dst,
300 const __u32 *src, struct inode *inode)
302 nfs4_bitmap_copy_adjust(dst, src, inode);
305 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
306 struct nfs4_readdir_arg *readdir)
308 unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
312 readdir->cookie = cookie;
313 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
318 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
323 * NFSv4 servers do not return entries for '.' and '..'
324 * Therefore, we fake these entries here. We let '.'
325 * have cookie 0 and '..' have cookie 1. Note that
326 * when talking to the server, we always send cookie 0
329 start = p = kmap_atomic(*readdir->pages);
332 *p++ = xdr_one; /* next */
333 *p++ = xdr_zero; /* cookie, first word */
334 *p++ = xdr_one; /* cookie, second word */
335 *p++ = xdr_one; /* entry len */
336 memcpy(p, ".\0\0\0", 4); /* entry */
338 *p++ = xdr_one; /* bitmap length */
339 *p++ = htonl(attrs); /* bitmap */
340 *p++ = htonl(12); /* attribute buffer length */
341 *p++ = htonl(NF4DIR);
342 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
345 *p++ = xdr_one; /* next */
346 *p++ = xdr_zero; /* cookie, first word */
347 *p++ = xdr_two; /* cookie, second word */
348 *p++ = xdr_two; /* entry len */
349 memcpy(p, "..\0\0", 4); /* entry */
351 *p++ = xdr_one; /* bitmap length */
352 *p++ = htonl(attrs); /* bitmap */
353 *p++ = htonl(12); /* attribute buffer length */
354 *p++ = htonl(NF4DIR);
355 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
357 readdir->pgbase = (char *)p - (char *)start;
358 readdir->count -= readdir->pgbase;
359 kunmap_atomic(start);
362 static void nfs4_test_and_free_stateid(struct nfs_server *server,
363 nfs4_stateid *stateid,
364 const struct cred *cred)
366 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
368 ops->test_and_free_expired(server, stateid, cred);
371 static void __nfs4_free_revoked_stateid(struct nfs_server *server,
372 nfs4_stateid *stateid,
373 const struct cred *cred)
375 stateid->type = NFS4_REVOKED_STATEID_TYPE;
376 nfs4_test_and_free_stateid(server, stateid, cred);
379 static void nfs4_free_revoked_stateid(struct nfs_server *server,
380 const nfs4_stateid *stateid,
381 const struct cred *cred)
385 nfs4_stateid_copy(&tmp, stateid);
386 __nfs4_free_revoked_stateid(server, &tmp, cred);
389 static long nfs4_update_delay(long *timeout)
393 return NFS4_POLL_RETRY_MAX;
395 *timeout = NFS4_POLL_RETRY_MIN;
396 if (*timeout > NFS4_POLL_RETRY_MAX)
397 *timeout = NFS4_POLL_RETRY_MAX;
403 static int nfs4_delay_killable(long *timeout)
407 freezable_schedule_timeout_killable_unsafe(
408 nfs4_update_delay(timeout));
409 if (!__fatal_signal_pending(current))
414 static int nfs4_delay_interruptible(long *timeout)
418 freezable_schedule_timeout_interruptible(nfs4_update_delay(timeout));
419 if (!signal_pending(current))
421 return __fatal_signal_pending(current) ? -EINTR :-ERESTARTSYS;
424 static int nfs4_delay(long *timeout, bool interruptible)
427 return nfs4_delay_interruptible(timeout);
428 return nfs4_delay_killable(timeout);
431 static const nfs4_stateid *
432 nfs4_recoverable_stateid(const nfs4_stateid *stateid)
436 switch (stateid->type) {
437 case NFS4_OPEN_STATEID_TYPE:
438 case NFS4_LOCK_STATEID_TYPE:
439 case NFS4_DELEGATION_STATEID_TYPE:
447 /* This is the error handling routine for processes that are allowed
450 static int nfs4_do_handle_exception(struct nfs_server *server,
451 int errorcode, struct nfs4_exception *exception)
453 struct nfs_client *clp = server->nfs_client;
454 struct nfs4_state *state = exception->state;
455 const nfs4_stateid *stateid;
456 struct inode *inode = exception->inode;
459 exception->delay = 0;
460 exception->recovering = 0;
461 exception->retry = 0;
463 stateid = nfs4_recoverable_stateid(exception->stateid);
464 if (stateid == NULL && state != NULL)
465 stateid = nfs4_recoverable_stateid(&state->stateid);
470 case -NFS4ERR_BADHANDLE:
472 if (inode != NULL && S_ISREG(inode->i_mode))
473 pnfs_destroy_layout(NFS_I(inode));
475 case -NFS4ERR_DELEG_REVOKED:
476 case -NFS4ERR_ADMIN_REVOKED:
477 case -NFS4ERR_EXPIRED:
478 case -NFS4ERR_BAD_STATEID:
479 if (inode != NULL && stateid != NULL) {
480 nfs_inode_find_state_and_recover(inode,
482 goto wait_on_recovery;
485 case -NFS4ERR_OPENMODE:
489 err = nfs_async_inode_return_delegation(inode,
492 goto wait_on_recovery;
493 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
494 exception->retry = 1;
500 ret = nfs4_schedule_stateid_recovery(server, state);
503 goto wait_on_recovery;
504 case -NFS4ERR_STALE_STATEID:
505 case -NFS4ERR_STALE_CLIENTID:
506 nfs4_schedule_lease_recovery(clp);
507 goto wait_on_recovery;
509 ret = nfs4_schedule_migration_recovery(server);
512 goto wait_on_recovery;
513 case -NFS4ERR_LEASE_MOVED:
514 nfs4_schedule_lease_moved_recovery(clp);
515 goto wait_on_recovery;
516 #if defined(CONFIG_NFS_V4_1)
517 case -NFS4ERR_BADSESSION:
518 case -NFS4ERR_BADSLOT:
519 case -NFS4ERR_BAD_HIGH_SLOT:
520 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
521 case -NFS4ERR_DEADSESSION:
522 case -NFS4ERR_SEQ_FALSE_RETRY:
523 case -NFS4ERR_SEQ_MISORDERED:
524 dprintk("%s ERROR: %d Reset session\n", __func__,
526 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
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 int nfs41_sequence_process(struct rpc_task *task,
780 struct nfs4_sequence_res *res)
782 struct nfs4_session *session;
783 struct nfs4_slot *slot = res->sr_slot;
784 struct nfs_client *clp;
789 /* don't increment the sequence number if the task wasn't sent */
790 if (!RPC_WAS_SENT(task) || slot->seq_done)
793 session = slot->table->session;
795 trace_nfs4_sequence_done(session, res);
796 /* Check the SEQUENCE operation status */
797 switch (res->sr_status) {
799 /* Mark this sequence number as having been acked */
800 nfs4_slot_sequence_acked(slot, slot->seq_nr);
801 /* Update the slot's sequence and clientid lease timer */
804 do_renew_lease(clp, res->sr_timestamp);
805 /* Check sequence flags */
806 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
808 nfs41_update_target_slotid(slot->table, slot, res);
812 * sr_status remains 1 if an RPC level error occurred.
813 * The server may or may not have processed the sequence
816 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
820 /* The server detected a resend of the RPC call and
821 * returned NFS4ERR_DELAY as per Section 2.10.6.2
824 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
828 nfs4_slot_sequence_acked(slot, slot->seq_nr);
830 case -NFS4ERR_RETRY_UNCACHED_REP:
831 case -NFS4ERR_SEQ_FALSE_RETRY:
833 * The server thinks we tried to replay a request.
834 * Retry the call after bumping the sequence ID.
836 nfs4_slot_sequence_acked(slot, slot->seq_nr);
838 case -NFS4ERR_BADSLOT:
840 * The slot id we used was probably retired. Try again
841 * using a different slot id.
843 if (slot->slot_nr < slot->table->target_highest_slotid)
844 goto session_recover;
846 case -NFS4ERR_SEQ_MISORDERED:
847 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
849 * Were one or more calls using this slot interrupted?
850 * If the server never received the request, then our
851 * transmitted slot sequence number may be too high.
853 if ((s32)(slot->seq_nr - slot->seq_nr_last_acked) > 1) {
859 * A retry might be sent while the original request is
860 * still in progress on the replier. The replier SHOULD
861 * deal with the issue by returning NFS4ERR_DELAY as the
862 * reply to SEQUENCE or CB_SEQUENCE operation, but
863 * implementations MAY return NFS4ERR_SEQ_MISORDERED.
865 * Restart the search after a delay.
867 slot->seq_nr = slot->seq_nr_highest_sent;
870 /* Just update the slot sequence no. */
874 /* The session may be reset by one of the error handlers. */
875 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
879 nfs4_schedule_session_recovery(session, res->sr_status);
884 if (rpc_restart_call_prepare(task)) {
885 nfs41_sequence_free_slot(res);
891 if (!rpc_restart_call(task))
893 rpc_delay(task, NFS4_POLL_RETRY_MAX);
897 int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
899 if (!nfs41_sequence_process(task, res))
901 if (res->sr_slot != NULL)
902 nfs41_sequence_free_slot(res);
906 EXPORT_SYMBOL_GPL(nfs41_sequence_done);
908 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
910 if (res->sr_slot == NULL)
912 if (res->sr_slot->table->session != NULL)
913 return nfs41_sequence_process(task, res);
914 return nfs40_sequence_done(task, res);
917 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
919 if (res->sr_slot != NULL) {
920 if (res->sr_slot->table->session != NULL)
921 nfs41_sequence_free_slot(res);
923 nfs40_sequence_free_slot(res);
927 int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
929 if (res->sr_slot == NULL)
931 if (!res->sr_slot->table->session)
932 return nfs40_sequence_done(task, res);
933 return nfs41_sequence_done(task, res);
935 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
937 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
939 struct nfs4_call_sync_data *data = calldata;
941 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
943 nfs4_setup_sequence(data->seq_server->nfs_client,
944 data->seq_args, data->seq_res, task);
947 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
949 struct nfs4_call_sync_data *data = calldata;
951 nfs41_sequence_done(task, data->seq_res);
954 static const struct rpc_call_ops nfs41_call_sync_ops = {
955 .rpc_call_prepare = nfs41_call_sync_prepare,
956 .rpc_call_done = nfs41_call_sync_done,
959 #else /* !CONFIG_NFS_V4_1 */
961 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
963 return nfs40_sequence_done(task, res);
966 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
968 if (res->sr_slot != NULL)
969 nfs40_sequence_free_slot(res);
972 int nfs4_sequence_done(struct rpc_task *task,
973 struct nfs4_sequence_res *res)
975 return nfs40_sequence_done(task, res);
977 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
979 #endif /* !CONFIG_NFS_V4_1 */
981 static void nfs41_sequence_res_init(struct nfs4_sequence_res *res)
983 res->sr_timestamp = jiffies;
984 res->sr_status_flags = 0;
989 void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
990 struct nfs4_sequence_res *res,
991 struct nfs4_slot *slot)
995 slot->privileged = args->sa_privileged ? 1 : 0;
996 args->sa_slot = slot;
1001 int nfs4_setup_sequence(struct nfs_client *client,
1002 struct nfs4_sequence_args *args,
1003 struct nfs4_sequence_res *res,
1004 struct rpc_task *task)
1006 struct nfs4_session *session = nfs4_get_session(client);
1007 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
1008 struct nfs4_slot *slot;
1010 /* slot already allocated? */
1011 if (res->sr_slot != NULL)
1015 tbl = &session->fc_slot_table;
1017 spin_lock(&tbl->slot_tbl_lock);
1018 /* The state manager will wait until the slot table is empty */
1019 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
1022 slot = nfs4_alloc_slot(tbl);
1024 if (slot == ERR_PTR(-ENOMEM))
1025 goto out_sleep_timeout;
1028 spin_unlock(&tbl->slot_tbl_lock);
1030 nfs4_sequence_attach_slot(args, res, slot);
1032 trace_nfs4_setup_sequence(session, args);
1034 nfs41_sequence_res_init(res);
1035 rpc_call_start(task);
1038 /* Try again in 1/4 second */
1039 if (args->sa_privileged)
1040 rpc_sleep_on_priority_timeout(&tbl->slot_tbl_waitq, task,
1041 jiffies + (HZ >> 2), RPC_PRIORITY_PRIVILEGED);
1043 rpc_sleep_on_timeout(&tbl->slot_tbl_waitq, task,
1044 NULL, jiffies + (HZ >> 2));
1045 spin_unlock(&tbl->slot_tbl_lock);
1048 if (args->sa_privileged)
1049 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
1050 RPC_PRIORITY_PRIVILEGED);
1052 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
1053 spin_unlock(&tbl->slot_tbl_lock);
1056 EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
1058 static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1060 struct nfs4_call_sync_data *data = calldata;
1061 nfs4_setup_sequence(data->seq_server->nfs_client,
1062 data->seq_args, data->seq_res, task);
1065 static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1067 struct nfs4_call_sync_data *data = calldata;
1068 nfs4_sequence_done(task, data->seq_res);
1071 static const struct rpc_call_ops nfs40_call_sync_ops = {
1072 .rpc_call_prepare = nfs40_call_sync_prepare,
1073 .rpc_call_done = nfs40_call_sync_done,
1076 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
1077 struct nfs_server *server,
1078 struct rpc_message *msg,
1079 struct nfs4_sequence_args *args,
1080 struct nfs4_sequence_res *res)
1083 struct rpc_task *task;
1084 struct nfs_client *clp = server->nfs_client;
1085 struct nfs4_call_sync_data data = {
1086 .seq_server = server,
1090 struct rpc_task_setup task_setup = {
1093 .callback_ops = clp->cl_mvops->call_sync_ops,
1094 .callback_data = &data
1097 task = rpc_run_task(&task_setup);
1099 ret = PTR_ERR(task);
1101 ret = task->tk_status;
1107 int nfs4_call_sync(struct rpc_clnt *clnt,
1108 struct nfs_server *server,
1109 struct rpc_message *msg,
1110 struct nfs4_sequence_args *args,
1111 struct nfs4_sequence_res *res,
1114 nfs4_init_sequence(args, res, cache_reply, 0);
1115 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
1119 nfs4_inc_nlink_locked(struct inode *inode)
1121 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1126 nfs4_dec_nlink_locked(struct inode *inode)
1128 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1133 update_changeattr_locked(struct inode *dir, struct nfs4_change_info *cinfo,
1134 unsigned long timestamp, unsigned long cache_validity)
1136 struct nfs_inode *nfsi = NFS_I(dir);
1138 nfsi->cache_validity |= NFS_INO_INVALID_CTIME
1139 | NFS_INO_INVALID_MTIME
1140 | NFS_INO_INVALID_DATA
1142 if (cinfo->atomic && cinfo->before == inode_peek_iversion_raw(dir)) {
1143 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1144 nfsi->attrtimeo_timestamp = jiffies;
1146 nfs_force_lookup_revalidate(dir);
1147 if (cinfo->before != inode_peek_iversion_raw(dir))
1148 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1149 NFS_INO_INVALID_ACL;
1151 inode_set_iversion_raw(dir, cinfo->after);
1152 nfsi->read_cache_jiffies = timestamp;
1153 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
1154 nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE;
1155 nfs_fscache_invalidate(dir);
1159 update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1160 unsigned long timestamp, unsigned long cache_validity)
1162 spin_lock(&dir->i_lock);
1163 update_changeattr_locked(dir, cinfo, timestamp, cache_validity);
1164 spin_unlock(&dir->i_lock);
1167 struct nfs4_open_createattrs {
1168 struct nfs4_label *label;
1169 struct iattr *sattr;
1170 const __u32 verf[2];
1173 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1174 int err, struct nfs4_exception *exception)
1178 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1180 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1181 exception->retry = 1;
1185 static fmode_t _nfs4_ctx_to_accessmode(const struct nfs_open_context *ctx)
1187 return ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
1190 static fmode_t _nfs4_ctx_to_openmode(const struct nfs_open_context *ctx)
1192 fmode_t ret = ctx->mode & (FMODE_READ|FMODE_WRITE);
1194 return (ctx->mode & FMODE_EXEC) ? FMODE_READ | ret : ret;
1198 nfs4_map_atomic_open_share(struct nfs_server *server,
1199 fmode_t fmode, int openflags)
1203 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1205 res = NFS4_SHARE_ACCESS_READ;
1208 res = NFS4_SHARE_ACCESS_WRITE;
1210 case FMODE_READ|FMODE_WRITE:
1211 res = NFS4_SHARE_ACCESS_BOTH;
1213 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1215 /* Want no delegation if we're using O_DIRECT */
1216 if (openflags & O_DIRECT)
1217 res |= NFS4_SHARE_WANT_NO_DELEG;
1222 static enum open_claim_type4
1223 nfs4_map_atomic_open_claim(struct nfs_server *server,
1224 enum open_claim_type4 claim)
1226 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1231 case NFS4_OPEN_CLAIM_FH:
1232 return NFS4_OPEN_CLAIM_NULL;
1233 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1234 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1235 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1236 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1240 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
1242 p->o_res.f_attr = &p->f_attr;
1243 p->o_res.f_label = p->f_label;
1244 p->o_res.seqid = p->o_arg.seqid;
1245 p->c_res.seqid = p->c_arg.seqid;
1246 p->o_res.server = p->o_arg.server;
1247 p->o_res.access_request = p->o_arg.access;
1248 nfs_fattr_init(&p->f_attr);
1249 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
1252 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
1253 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
1254 const struct nfs4_open_createattrs *c,
1255 enum open_claim_type4 claim,
1258 struct dentry *parent = dget_parent(dentry);
1259 struct inode *dir = d_inode(parent);
1260 struct nfs_server *server = NFS_SERVER(dir);
1261 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
1262 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
1263 struct nfs4_opendata *p;
1265 p = kzalloc(sizeof(*p), gfp_mask);
1269 p->f_label = nfs4_label_alloc(server, gfp_mask);
1270 if (IS_ERR(p->f_label))
1273 p->a_label = nfs4_label_alloc(server, gfp_mask);
1274 if (IS_ERR(p->a_label))
1277 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1278 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
1279 if (IS_ERR(p->o_arg.seqid))
1280 goto err_free_label;
1281 nfs_sb_active(dentry->d_sb);
1282 p->dentry = dget(dentry);
1285 atomic_inc(&sp->so_count);
1286 p->o_arg.open_flags = flags;
1287 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
1288 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1289 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1291 if (flags & O_CREAT) {
1292 p->o_arg.umask = current_umask();
1293 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1294 if (c->sattr != NULL && c->sattr->ia_valid != 0) {
1295 p->o_arg.u.attrs = &p->attrs;
1296 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
1298 memcpy(p->o_arg.u.verifier.data, c->verf,
1299 sizeof(p->o_arg.u.verifier.data));
1302 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1303 * will return permission denied for all bits until close */
1304 if (!(flags & O_EXCL)) {
1305 /* ask server to check for all possible rights as results
1307 switch (p->o_arg.claim) {
1310 case NFS4_OPEN_CLAIM_NULL:
1311 case NFS4_OPEN_CLAIM_FH:
1312 p->o_arg.access = NFS4_ACCESS_READ |
1313 NFS4_ACCESS_MODIFY |
1314 NFS4_ACCESS_EXTEND |
1315 NFS4_ACCESS_EXECUTE;
1318 p->o_arg.clientid = server->nfs_client->cl_clientid;
1319 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1320 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1321 p->o_arg.name = &dentry->d_name;
1322 p->o_arg.server = server;
1323 p->o_arg.bitmask = nfs4_bitmask(server, label);
1324 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1325 switch (p->o_arg.claim) {
1326 case NFS4_OPEN_CLAIM_NULL:
1327 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1328 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1329 p->o_arg.fh = NFS_FH(dir);
1331 case NFS4_OPEN_CLAIM_PREVIOUS:
1332 case NFS4_OPEN_CLAIM_FH:
1333 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1334 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1335 p->o_arg.fh = NFS_FH(d_inode(dentry));
1337 p->c_arg.fh = &p->o_res.fh;
1338 p->c_arg.stateid = &p->o_res.stateid;
1339 p->c_arg.seqid = p->o_arg.seqid;
1340 nfs4_init_opendata_res(p);
1341 kref_init(&p->kref);
1345 nfs4_label_free(p->a_label);
1347 nfs4_label_free(p->f_label);
1355 static void nfs4_opendata_free(struct kref *kref)
1357 struct nfs4_opendata *p = container_of(kref,
1358 struct nfs4_opendata, kref);
1359 struct super_block *sb = p->dentry->d_sb;
1361 nfs4_lgopen_release(p->lgp);
1362 nfs_free_seqid(p->o_arg.seqid);
1363 nfs4_sequence_free_slot(&p->o_res.seq_res);
1364 if (p->state != NULL)
1365 nfs4_put_open_state(p->state);
1366 nfs4_put_state_owner(p->owner);
1368 nfs4_label_free(p->a_label);
1369 nfs4_label_free(p->f_label);
1373 nfs_sb_deactive(sb);
1374 nfs_fattr_free_names(&p->f_attr);
1375 kfree(p->f_attr.mdsthreshold);
1379 static void nfs4_opendata_put(struct nfs4_opendata *p)
1382 kref_put(&p->kref, nfs4_opendata_free);
1385 static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1388 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1389 case FMODE_READ|FMODE_WRITE:
1390 return state->n_rdwr != 0;
1392 return state->n_wronly != 0;
1394 return state->n_rdonly != 0;
1400 static int can_open_cached(struct nfs4_state *state, fmode_t mode,
1401 int open_mode, enum open_claim_type4 claim)
1405 if (open_mode & (O_EXCL|O_TRUNC))
1408 case NFS4_OPEN_CLAIM_NULL:
1409 case NFS4_OPEN_CLAIM_FH:
1414 switch (mode & (FMODE_READ|FMODE_WRITE)) {
1416 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1417 && state->n_rdonly != 0;
1420 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1421 && state->n_wronly != 0;
1423 case FMODE_READ|FMODE_WRITE:
1424 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1425 && state->n_rdwr != 0;
1431 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1432 enum open_claim_type4 claim)
1434 if (delegation == NULL)
1436 if ((delegation->type & fmode) != fmode)
1438 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1441 case NFS4_OPEN_CLAIM_NULL:
1442 case NFS4_OPEN_CLAIM_FH:
1444 case NFS4_OPEN_CLAIM_PREVIOUS:
1445 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1451 nfs_mark_delegation_referenced(delegation);
1455 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1464 case FMODE_READ|FMODE_WRITE:
1467 nfs4_state_set_mode_locked(state, state->state | fmode);
1470 #ifdef CONFIG_NFS_V4_1
1471 static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1473 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1475 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1477 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1481 #endif /* CONFIG_NFS_V4_1 */
1483 static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1485 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1486 wake_up_all(&state->waitq);
1489 static void nfs_state_log_out_of_order_open_stateid(struct nfs4_state *state,
1490 const nfs4_stateid *stateid)
1492 u32 state_seqid = be32_to_cpu(state->open_stateid.seqid);
1493 u32 stateid_seqid = be32_to_cpu(stateid->seqid);
1495 if (stateid_seqid == state_seqid + 1U ||
1496 (stateid_seqid == 1U && state_seqid == 0xffffffffU))
1497 nfs_state_log_update_open_stateid(state);
1499 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1502 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
1504 struct nfs_client *clp = state->owner->so_server->nfs_client;
1505 bool need_recover = false;
1507 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1508 need_recover = true;
1509 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1510 need_recover = true;
1511 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1512 need_recover = true;
1514 nfs4_state_mark_reclaim_nograce(clp, state);
1518 * Check for whether or not the caller may update the open stateid
1519 * to the value passed in by stateid.
1521 * Note: This function relies heavily on the server implementing
1522 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1524 * i.e. The stateid seqids have to be initialised to 1, and
1525 * are then incremented on every state transition.
1527 static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1528 const nfs4_stateid *stateid)
1530 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0 ||
1531 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1532 if (stateid->seqid == cpu_to_be32(1))
1533 nfs_state_log_update_open_stateid(state);
1535 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1539 if (nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1540 nfs_state_log_out_of_order_open_stateid(state, stateid);
1546 static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1548 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1550 if (state->n_wronly)
1551 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1552 if (state->n_rdonly)
1553 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1555 set_bit(NFS_O_RDWR_STATE, &state->flags);
1556 set_bit(NFS_OPEN_STATE, &state->flags);
1559 static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1560 nfs4_stateid *stateid, fmode_t fmode)
1562 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1563 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1565 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1568 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1571 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1572 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1573 clear_bit(NFS_OPEN_STATE, &state->flags);
1575 if (stateid == NULL)
1577 /* Handle OPEN+OPEN_DOWNGRADE races */
1578 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1579 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1580 nfs_resync_open_stateid_locked(state);
1583 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1584 nfs4_stateid_copy(&state->stateid, stateid);
1585 nfs4_stateid_copy(&state->open_stateid, stateid);
1586 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
1588 nfs_state_log_update_open_stateid(state);
1591 static void nfs_clear_open_stateid(struct nfs4_state *state,
1592 nfs4_stateid *arg_stateid,
1593 nfs4_stateid *stateid, fmode_t fmode)
1595 write_seqlock(&state->seqlock);
1596 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1597 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1598 nfs_clear_open_stateid_locked(state, stateid, fmode);
1599 write_sequnlock(&state->seqlock);
1600 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1601 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1604 static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1605 const nfs4_stateid *stateid, nfs4_stateid *freeme)
1606 __must_hold(&state->owner->so_lock)
1607 __must_hold(&state->seqlock)
1615 if (!nfs_need_update_open_stateid(state, stateid))
1617 if (!test_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1621 /* Rely on seqids for serialisation with NFSv4.0 */
1622 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1625 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1627 * Ensure we process the state changes in the same order
1628 * in which the server processed them by delaying the
1629 * update of the stateid until we are in sequence.
1631 write_sequnlock(&state->seqlock);
1632 spin_unlock(&state->owner->so_lock);
1634 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
1635 if (!signal_pending(current)) {
1636 if (schedule_timeout(5*HZ) == 0)
1642 finish_wait(&state->waitq, &wait);
1644 spin_lock(&state->owner->so_lock);
1645 write_seqlock(&state->seqlock);
1648 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1649 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1650 nfs4_stateid_copy(freeme, &state->open_stateid);
1651 nfs_test_and_clear_all_open_stateid(state);
1654 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1655 nfs4_stateid_copy(&state->stateid, stateid);
1656 nfs4_stateid_copy(&state->open_stateid, stateid);
1657 trace_nfs4_open_stateid_update(state->inode, stateid, status);
1658 nfs_state_log_update_open_stateid(state);
1661 static void nfs_state_set_open_stateid(struct nfs4_state *state,
1662 const nfs4_stateid *open_stateid,
1664 nfs4_stateid *freeme)
1667 * Protect the call to nfs4_state_set_mode_locked and
1668 * serialise the stateid update
1670 write_seqlock(&state->seqlock);
1671 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1674 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1677 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1679 case FMODE_READ|FMODE_WRITE:
1680 set_bit(NFS_O_RDWR_STATE, &state->flags);
1682 set_bit(NFS_OPEN_STATE, &state->flags);
1683 write_sequnlock(&state->seqlock);
1686 static void nfs_state_set_delegation(struct nfs4_state *state,
1687 const nfs4_stateid *deleg_stateid,
1691 * Protect the call to nfs4_state_set_mode_locked and
1692 * serialise the stateid update
1694 write_seqlock(&state->seqlock);
1695 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1696 set_bit(NFS_DELEGATED_STATE, &state->flags);
1697 write_sequnlock(&state->seqlock);
1700 static void nfs_state_clear_delegation(struct nfs4_state *state)
1702 write_seqlock(&state->seqlock);
1703 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1704 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1705 write_sequnlock(&state->seqlock);
1708 static int update_open_stateid(struct nfs4_state *state,
1709 const nfs4_stateid *open_stateid,
1710 const nfs4_stateid *delegation,
1713 struct nfs_server *server = NFS_SERVER(state->inode);
1714 struct nfs_client *clp = server->nfs_client;
1715 struct nfs_inode *nfsi = NFS_I(state->inode);
1716 struct nfs_delegation *deleg_cur;
1717 nfs4_stateid freeme = { };
1720 fmode &= (FMODE_READ|FMODE_WRITE);
1723 spin_lock(&state->owner->so_lock);
1724 if (open_stateid != NULL) {
1725 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1729 deleg_cur = rcu_dereference(nfsi->delegation);
1730 if (deleg_cur == NULL)
1733 spin_lock(&deleg_cur->lock);
1734 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1735 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1736 (deleg_cur->type & fmode) != fmode)
1737 goto no_delegation_unlock;
1739 if (delegation == NULL)
1740 delegation = &deleg_cur->stateid;
1741 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
1742 goto no_delegation_unlock;
1744 nfs_mark_delegation_referenced(deleg_cur);
1745 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
1747 no_delegation_unlock:
1748 spin_unlock(&deleg_cur->lock);
1751 update_open_stateflags(state, fmode);
1752 spin_unlock(&state->owner->so_lock);
1755 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1756 nfs4_schedule_state_manager(clp);
1757 if (freeme.type != 0)
1758 nfs4_test_and_free_stateid(server, &freeme,
1759 state->owner->so_cred);
1764 static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1765 const nfs4_stateid *stateid)
1767 struct nfs4_state *state = lsp->ls_state;
1770 spin_lock(&state->state_lock);
1771 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1773 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1775 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1778 spin_unlock(&state->state_lock);
1782 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1784 struct nfs_delegation *delegation;
1786 fmode &= FMODE_READ|FMODE_WRITE;
1788 delegation = rcu_dereference(NFS_I(inode)->delegation);
1789 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1794 nfs4_inode_return_delegation(inode);
1797 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1799 struct nfs4_state *state = opendata->state;
1800 struct nfs_inode *nfsi = NFS_I(state->inode);
1801 struct nfs_delegation *delegation;
1802 int open_mode = opendata->o_arg.open_flags;
1803 fmode_t fmode = opendata->o_arg.fmode;
1804 enum open_claim_type4 claim = opendata->o_arg.claim;
1805 nfs4_stateid stateid;
1809 spin_lock(&state->owner->so_lock);
1810 if (can_open_cached(state, fmode, open_mode, claim)) {
1811 update_open_stateflags(state, fmode);
1812 spin_unlock(&state->owner->so_lock);
1813 goto out_return_state;
1815 spin_unlock(&state->owner->so_lock);
1817 delegation = rcu_dereference(nfsi->delegation);
1818 if (!can_open_delegated(delegation, fmode, claim)) {
1822 /* Save the delegation */
1823 nfs4_stateid_copy(&stateid, &delegation->stateid);
1825 nfs_release_seqid(opendata->o_arg.seqid);
1826 if (!opendata->is_recover) {
1827 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1833 /* Try to update the stateid using the delegation */
1834 if (update_open_stateid(state, NULL, &stateid, fmode))
1835 goto out_return_state;
1838 return ERR_PTR(ret);
1840 refcount_inc(&state->count);
1845 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1847 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1848 struct nfs_delegation *delegation;
1849 int delegation_flags = 0;
1852 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1854 delegation_flags = delegation->flags;
1856 switch (data->o_arg.claim) {
1859 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1860 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1861 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1862 "returning a delegation for "
1863 "OPEN(CLAIM_DELEGATE_CUR)\n",
1867 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1868 nfs_inode_set_delegation(state->inode,
1869 data->owner->so_cred,
1870 data->o_res.delegation_type,
1871 &data->o_res.delegation,
1872 data->o_res.pagemod_limit);
1874 nfs_inode_reclaim_delegation(state->inode,
1875 data->owner->so_cred,
1876 data->o_res.delegation_type,
1877 &data->o_res.delegation,
1878 data->o_res.pagemod_limit);
1880 if (data->o_res.do_recall)
1881 nfs_async_inode_return_delegation(state->inode,
1882 &data->o_res.delegation);
1886 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1887 * and update the nfs4_state.
1889 static struct nfs4_state *
1890 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1892 struct inode *inode = data->state->inode;
1893 struct nfs4_state *state = data->state;
1896 if (!data->rpc_done) {
1897 if (data->rpc_status)
1898 return ERR_PTR(data->rpc_status);
1899 /* cached opens have already been processed */
1903 ret = nfs_refresh_inode(inode, &data->f_attr);
1905 return ERR_PTR(ret);
1907 if (data->o_res.delegation_type != 0)
1908 nfs4_opendata_check_deleg(data, state);
1910 update_open_stateid(state, &data->o_res.stateid, NULL,
1912 refcount_inc(&state->count);
1917 static struct inode *
1918 nfs4_opendata_get_inode(struct nfs4_opendata *data)
1920 struct inode *inode;
1922 switch (data->o_arg.claim) {
1923 case NFS4_OPEN_CLAIM_NULL:
1924 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1925 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1926 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1927 return ERR_PTR(-EAGAIN);
1928 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1929 &data->f_attr, data->f_label);
1932 inode = d_inode(data->dentry);
1934 nfs_refresh_inode(inode, &data->f_attr);
1939 static struct nfs4_state *
1940 nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
1942 struct nfs4_state *state;
1943 struct inode *inode;
1945 inode = nfs4_opendata_get_inode(data);
1947 return ERR_CAST(inode);
1948 if (data->state != NULL && data->state->inode == inode) {
1949 state = data->state;
1950 refcount_inc(&state->count);
1952 state = nfs4_get_open_state(inode, data->owner);
1955 state = ERR_PTR(-ENOMEM);
1959 static struct nfs4_state *
1960 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1962 struct nfs4_state *state;
1964 if (!data->rpc_done) {
1965 state = nfs4_try_open_cached(data);
1966 trace_nfs4_cached_open(data->state);
1970 state = nfs4_opendata_find_nfs4_state(data);
1974 if (data->o_res.delegation_type != 0)
1975 nfs4_opendata_check_deleg(data, state);
1976 update_open_stateid(state, &data->o_res.stateid, NULL,
1979 nfs_release_seqid(data->o_arg.seqid);
1983 static struct nfs4_state *
1984 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1986 struct nfs4_state *ret;
1988 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1989 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1991 ret = _nfs4_opendata_to_nfs4_state(data);
1992 nfs4_sequence_free_slot(&data->o_res.seq_res);
1996 static struct nfs_open_context *
1997 nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode)
1999 struct nfs_inode *nfsi = NFS_I(state->inode);
2000 struct nfs_open_context *ctx;
2003 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
2004 if (ctx->state != state)
2006 if ((ctx->mode & mode) != mode)
2008 if (!get_nfs_open_context(ctx))
2014 return ERR_PTR(-ENOENT);
2017 static struct nfs_open_context *
2018 nfs4_state_find_open_context(struct nfs4_state *state)
2020 struct nfs_open_context *ctx;
2022 ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE);
2025 ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE);
2028 return nfs4_state_find_open_context_mode(state, FMODE_READ);
2031 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
2032 struct nfs4_state *state, enum open_claim_type4 claim)
2034 struct nfs4_opendata *opendata;
2036 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
2037 NULL, claim, GFP_NOFS);
2038 if (opendata == NULL)
2039 return ERR_PTR(-ENOMEM);
2040 opendata->state = state;
2041 refcount_inc(&state->count);
2045 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
2048 struct nfs4_state *newstate;
2051 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
2053 opendata->o_arg.open_flags = 0;
2054 opendata->o_arg.fmode = fmode;
2055 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
2056 NFS_SB(opendata->dentry->d_sb),
2058 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
2059 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
2060 nfs4_init_opendata_res(opendata);
2061 ret = _nfs4_recover_proc_open(opendata);
2064 newstate = nfs4_opendata_to_nfs4_state(opendata);
2065 if (IS_ERR(newstate))
2066 return PTR_ERR(newstate);
2067 if (newstate != opendata->state)
2069 nfs4_close_state(newstate, fmode);
2073 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
2077 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
2078 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2079 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2080 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2081 /* memory barrier prior to reading state->n_* */
2082 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2083 clear_bit(NFS_OPEN_STATE, &state->flags);
2085 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2088 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2091 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2095 * We may have performed cached opens for all three recoveries.
2096 * Check if we need to update the current stateid.
2098 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
2099 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
2100 write_seqlock(&state->seqlock);
2101 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2102 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2103 write_sequnlock(&state->seqlock);
2110 * reclaim state on the server after a reboot.
2112 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2114 struct nfs_delegation *delegation;
2115 struct nfs4_opendata *opendata;
2116 fmode_t delegation_type = 0;
2119 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2120 NFS4_OPEN_CLAIM_PREVIOUS);
2121 if (IS_ERR(opendata))
2122 return PTR_ERR(opendata);
2124 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2125 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
2126 delegation_type = delegation->type;
2128 opendata->o_arg.u.delegation_type = delegation_type;
2129 status = nfs4_open_recover(opendata, state);
2130 nfs4_opendata_put(opendata);
2134 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2136 struct nfs_server *server = NFS_SERVER(state->inode);
2137 struct nfs4_exception exception = { };
2140 err = _nfs4_do_open_reclaim(ctx, state);
2141 trace_nfs4_open_reclaim(ctx, 0, err);
2142 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2144 if (err != -NFS4ERR_DELAY)
2146 nfs4_handle_exception(server, err, &exception);
2147 } while (exception.retry);
2151 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2153 struct nfs_open_context *ctx;
2156 ctx = nfs4_state_find_open_context(state);
2159 ret = nfs4_do_open_reclaim(ctx, state);
2160 put_nfs_open_context(ctx);
2164 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)
2168 printk(KERN_ERR "NFS: %s: unhandled error "
2169 "%d.\n", __func__, err);
2175 case -NFS4ERR_BADSESSION:
2176 case -NFS4ERR_BADSLOT:
2177 case -NFS4ERR_BAD_HIGH_SLOT:
2178 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2179 case -NFS4ERR_DEADSESSION:
2180 set_bit(NFS_DELEGATED_STATE, &state->flags);
2181 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
2183 case -NFS4ERR_STALE_CLIENTID:
2184 case -NFS4ERR_STALE_STATEID:
2185 set_bit(NFS_DELEGATED_STATE, &state->flags);
2186 /* Don't recall a delegation if it was lost */
2187 nfs4_schedule_lease_recovery(server->nfs_client);
2189 case -NFS4ERR_MOVED:
2190 nfs4_schedule_migration_recovery(server);
2192 case -NFS4ERR_LEASE_MOVED:
2193 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2195 case -NFS4ERR_DELEG_REVOKED:
2196 case -NFS4ERR_ADMIN_REVOKED:
2197 case -NFS4ERR_EXPIRED:
2198 case -NFS4ERR_BAD_STATEID:
2199 case -NFS4ERR_OPENMODE:
2200 nfs_inode_find_state_and_recover(state->inode,
2202 nfs4_schedule_stateid_recovery(server, state);
2204 case -NFS4ERR_DELAY:
2205 case -NFS4ERR_GRACE:
2206 set_bit(NFS_DELEGATED_STATE, &state->flags);
2210 case -NFS4ERR_DENIED:
2212 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2214 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2221 int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2222 struct nfs4_state *state, const nfs4_stateid *stateid,
2225 struct nfs_server *server = NFS_SERVER(state->inode);
2226 struct nfs4_opendata *opendata;
2229 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2230 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2231 if (IS_ERR(opendata))
2232 return PTR_ERR(opendata);
2233 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
2234 nfs_state_clear_delegation(state);
2235 switch (type & (FMODE_READ|FMODE_WRITE)) {
2236 case FMODE_READ|FMODE_WRITE:
2238 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2241 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2246 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2248 nfs4_opendata_put(opendata);
2249 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
2252 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2254 struct nfs4_opendata *data = calldata;
2256 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2257 &data->c_arg.seq_args, &data->c_res.seq_res, task);
2260 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2262 struct nfs4_opendata *data = calldata;
2264 nfs40_sequence_done(task, &data->c_res.seq_res);
2266 data->rpc_status = task->tk_status;
2267 if (data->rpc_status == 0) {
2268 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
2269 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2270 renew_lease(data->o_res.server, data->timestamp);
2271 data->rpc_done = true;
2275 static void nfs4_open_confirm_release(void *calldata)
2277 struct nfs4_opendata *data = calldata;
2278 struct nfs4_state *state = NULL;
2280 /* If this request hasn't been cancelled, do nothing */
2281 if (!data->cancelled)
2283 /* In case of error, no cleanup! */
2284 if (!data->rpc_done)
2286 state = nfs4_opendata_to_nfs4_state(data);
2288 nfs4_close_state(state, data->o_arg.fmode);
2290 nfs4_opendata_put(data);
2293 static const struct rpc_call_ops nfs4_open_confirm_ops = {
2294 .rpc_call_prepare = nfs4_open_confirm_prepare,
2295 .rpc_call_done = nfs4_open_confirm_done,
2296 .rpc_release = nfs4_open_confirm_release,
2300 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2302 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2304 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
2305 struct rpc_task *task;
2306 struct rpc_message msg = {
2307 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2308 .rpc_argp = &data->c_arg,
2309 .rpc_resp = &data->c_res,
2310 .rpc_cred = data->owner->so_cred,
2312 struct rpc_task_setup task_setup_data = {
2313 .rpc_client = server->client,
2314 .rpc_message = &msg,
2315 .callback_ops = &nfs4_open_confirm_ops,
2316 .callback_data = data,
2317 .workqueue = nfsiod_workqueue,
2318 .flags = RPC_TASK_ASYNC,
2322 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2324 kref_get(&data->kref);
2325 data->rpc_done = false;
2326 data->rpc_status = 0;
2327 data->timestamp = jiffies;
2328 task = rpc_run_task(&task_setup_data);
2330 return PTR_ERR(task);
2331 status = rpc_wait_for_completion_task(task);
2333 data->cancelled = true;
2336 status = data->rpc_status;
2341 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
2343 struct nfs4_opendata *data = calldata;
2344 struct nfs4_state_owner *sp = data->owner;
2345 struct nfs_client *clp = sp->so_server->nfs_client;
2346 enum open_claim_type4 claim = data->o_arg.claim;
2348 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
2351 * Check if we still need to send an OPEN call, or if we can use
2352 * a delegation instead.
2354 if (data->state != NULL) {
2355 struct nfs_delegation *delegation;
2357 if (can_open_cached(data->state, data->o_arg.fmode,
2358 data->o_arg.open_flags, claim))
2361 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
2362 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
2363 goto unlock_no_action;
2366 /* Update client id. */
2367 data->o_arg.clientid = clp->cl_clientid;
2371 case NFS4_OPEN_CLAIM_PREVIOUS:
2372 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2373 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
2374 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
2376 case NFS4_OPEN_CLAIM_FH:
2377 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
2379 data->timestamp = jiffies;
2380 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
2381 &data->o_arg.seq_args,
2382 &data->o_res.seq_res,
2384 nfs_release_seqid(data->o_arg.seqid);
2386 /* Set the create mode (note dependency on the session type) */
2387 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2388 if (data->o_arg.open_flags & O_EXCL) {
2389 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2390 if (nfs4_has_persistent_session(clp))
2391 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2392 else if (clp->cl_mvops->minor_version > 0)
2393 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2397 trace_nfs4_cached_open(data->state);
2400 task->tk_action = NULL;
2402 nfs4_sequence_done(task, &data->o_res.seq_res);
2405 static void nfs4_open_done(struct rpc_task *task, void *calldata)
2407 struct nfs4_opendata *data = calldata;
2409 data->rpc_status = task->tk_status;
2411 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
2414 if (task->tk_status == 0) {
2415 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2416 switch (data->o_res.f_attr->mode & S_IFMT) {
2420 data->rpc_status = -ELOOP;
2423 data->rpc_status = -EISDIR;
2426 data->rpc_status = -ENOTDIR;
2429 renew_lease(data->o_res.server, data->timestamp);
2430 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2431 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2433 data->rpc_done = true;
2436 static void nfs4_open_release(void *calldata)
2438 struct nfs4_opendata *data = calldata;
2439 struct nfs4_state *state = NULL;
2441 /* If this request hasn't been cancelled, do nothing */
2442 if (!data->cancelled)
2444 /* In case of error, no cleanup! */
2445 if (data->rpc_status != 0 || !data->rpc_done)
2447 /* In case we need an open_confirm, no cleanup! */
2448 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2450 state = nfs4_opendata_to_nfs4_state(data);
2452 nfs4_close_state(state, data->o_arg.fmode);
2454 nfs4_opendata_put(data);
2457 static const struct rpc_call_ops nfs4_open_ops = {
2458 .rpc_call_prepare = nfs4_open_prepare,
2459 .rpc_call_done = nfs4_open_done,
2460 .rpc_release = nfs4_open_release,
2463 static int nfs4_run_open_task(struct nfs4_opendata *data,
2464 struct nfs_open_context *ctx)
2466 struct inode *dir = d_inode(data->dir);
2467 struct nfs_server *server = NFS_SERVER(dir);
2468 struct nfs_openargs *o_arg = &data->o_arg;
2469 struct nfs_openres *o_res = &data->o_res;
2470 struct rpc_task *task;
2471 struct rpc_message msg = {
2472 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2475 .rpc_cred = data->owner->so_cred,
2477 struct rpc_task_setup task_setup_data = {
2478 .rpc_client = server->client,
2479 .rpc_message = &msg,
2480 .callback_ops = &nfs4_open_ops,
2481 .callback_data = data,
2482 .workqueue = nfsiod_workqueue,
2483 .flags = RPC_TASK_ASYNC,
2487 kref_get(&data->kref);
2488 data->rpc_done = false;
2489 data->rpc_status = 0;
2490 data->cancelled = false;
2491 data->is_recover = false;
2493 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
2494 data->is_recover = true;
2496 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
2497 pnfs_lgopen_prepare(data, ctx);
2499 task = rpc_run_task(&task_setup_data);
2501 return PTR_ERR(task);
2502 status = rpc_wait_for_completion_task(task);
2504 data->cancelled = true;
2507 status = data->rpc_status;
2513 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2515 struct inode *dir = d_inode(data->dir);
2516 struct nfs_openres *o_res = &data->o_res;
2519 status = nfs4_run_open_task(data, NULL);
2520 if (status != 0 || !data->rpc_done)
2523 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2525 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
2526 status = _nfs4_proc_open_confirm(data);
2532 * Additional permission checks in order to distinguish between an
2533 * open for read, and an open for execute. This works around the
2534 * fact that NFSv4 OPEN treats read and execute permissions as being
2536 * Note that in the non-execute case, we want to turn off permission
2537 * checking if we just created a new file (POSIX open() semantics).
2539 static int nfs4_opendata_access(const struct cred *cred,
2540 struct nfs4_opendata *opendata,
2541 struct nfs4_state *state, fmode_t fmode,
2544 struct nfs_access_entry cache;
2547 /* access call failed or for some reason the server doesn't
2548 * support any access modes -- defer access call until later */
2549 if (opendata->o_res.access_supported == 0)
2554 * Use openflags to check for exec, because fmode won't
2555 * always have FMODE_EXEC set when file open for exec.
2557 if (openflags & __FMODE_EXEC) {
2558 /* ONLY check for exec rights */
2559 if (S_ISDIR(state->inode->i_mode))
2560 mask = NFS4_ACCESS_LOOKUP;
2562 mask = NFS4_ACCESS_EXECUTE;
2563 } else if ((fmode & FMODE_READ) && !opendata->file_created)
2564 mask = NFS4_ACCESS_READ;
2567 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2568 nfs_access_add_cache(state->inode, &cache);
2570 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2571 if ((mask & ~cache.mask & flags) == 0)
2578 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2580 static int _nfs4_proc_open(struct nfs4_opendata *data,
2581 struct nfs_open_context *ctx)
2583 struct inode *dir = d_inode(data->dir);
2584 struct nfs_server *server = NFS_SERVER(dir);
2585 struct nfs_openargs *o_arg = &data->o_arg;
2586 struct nfs_openres *o_res = &data->o_res;
2589 status = nfs4_run_open_task(data, ctx);
2590 if (!data->rpc_done)
2593 if (status == -NFS4ERR_BADNAME &&
2594 !(o_arg->open_flags & O_CREAT))
2599 nfs_fattr_map_and_free_names(server, &data->f_attr);
2601 if (o_arg->open_flags & O_CREAT) {
2602 if (o_arg->open_flags & O_EXCL)
2603 data->file_created = true;
2604 else if (o_res->cinfo.before != o_res->cinfo.after)
2605 data->file_created = true;
2606 if (data->file_created ||
2607 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
2608 update_changeattr(dir, &o_res->cinfo,
2609 o_res->f_attr->time_start, 0);
2611 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2612 server->caps &= ~NFS_CAP_POSIX_LOCK;
2613 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2614 status = _nfs4_proc_open_confirm(data);
2618 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2619 nfs4_sequence_free_slot(&o_res->seq_res);
2620 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr,
2621 o_res->f_label, NULL);
2628 * reclaim state on the server after a network partition.
2629 * Assumes caller holds the appropriate lock
2631 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2633 struct nfs4_opendata *opendata;
2636 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2637 NFS4_OPEN_CLAIM_FH);
2638 if (IS_ERR(opendata))
2639 return PTR_ERR(opendata);
2640 ret = nfs4_open_recover(opendata, state);
2642 d_drop(ctx->dentry);
2643 nfs4_opendata_put(opendata);
2647 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2649 struct nfs_server *server = NFS_SERVER(state->inode);
2650 struct nfs4_exception exception = { };
2654 err = _nfs4_open_expired(ctx, state);
2655 trace_nfs4_open_expired(ctx, 0, err);
2656 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2661 case -NFS4ERR_GRACE:
2662 case -NFS4ERR_DELAY:
2663 nfs4_handle_exception(server, err, &exception);
2666 } while (exception.retry);
2671 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2673 struct nfs_open_context *ctx;
2676 ctx = nfs4_state_find_open_context(state);
2679 ret = nfs4_do_open_expired(ctx, state);
2680 put_nfs_open_context(ctx);
2684 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2685 const nfs4_stateid *stateid)
2687 nfs_remove_bad_delegation(state->inode, stateid);
2688 nfs_state_clear_delegation(state);
2691 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2693 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2694 nfs_finish_clear_delegation_stateid(state, NULL);
2697 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2699 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2700 nfs40_clear_delegation_stateid(state);
2701 return nfs4_open_expired(sp, state);
2704 static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2705 nfs4_stateid *stateid,
2706 const struct cred *cred)
2708 return -NFS4ERR_BAD_STATEID;
2711 #if defined(CONFIG_NFS_V4_1)
2712 static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2713 nfs4_stateid *stateid,
2714 const struct cred *cred)
2718 switch (stateid->type) {
2721 case NFS4_INVALID_STATEID_TYPE:
2722 case NFS4_SPECIAL_STATEID_TYPE:
2723 return -NFS4ERR_BAD_STATEID;
2724 case NFS4_REVOKED_STATEID_TYPE:
2728 status = nfs41_test_stateid(server, stateid, cred);
2730 case -NFS4ERR_EXPIRED:
2731 case -NFS4ERR_ADMIN_REVOKED:
2732 case -NFS4ERR_DELEG_REVOKED:
2738 /* Ack the revoked state to the server */
2739 nfs41_free_stateid(server, stateid, cred, true);
2740 return -NFS4ERR_EXPIRED;
2743 static void nfs41_check_delegation_stateid(struct nfs4_state *state)
2745 struct nfs_server *server = NFS_SERVER(state->inode);
2746 nfs4_stateid stateid;
2747 struct nfs_delegation *delegation;
2748 const struct cred *cred = NULL;
2751 /* Get the delegation credential for use by test/free_stateid */
2753 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2754 if (delegation == NULL) {
2756 nfs_state_clear_delegation(state);
2760 nfs4_stateid_copy(&stateid, &delegation->stateid);
2761 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
2763 nfs_state_clear_delegation(state);
2767 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2768 &delegation->flags)) {
2773 if (delegation->cred)
2774 cred = get_cred(delegation->cred);
2776 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
2777 trace_nfs4_test_delegation_stateid(state, NULL, status);
2778 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
2779 nfs_finish_clear_delegation_stateid(state, &stateid);
2781 if (delegation->cred)
2786 * nfs41_check_expired_locks - possibly free a lock stateid
2788 * @state: NFSv4 state for an inode
2790 * Returns NFS_OK if recovery for this stateid is now finished.
2791 * Otherwise a negative NFS4ERR value is returned.
2793 static int nfs41_check_expired_locks(struct nfs4_state *state)
2795 int status, ret = NFS_OK;
2796 struct nfs4_lock_state *lsp, *prev = NULL;
2797 struct nfs_server *server = NFS_SERVER(state->inode);
2799 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2802 spin_lock(&state->state_lock);
2803 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2804 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2805 const struct cred *cred = lsp->ls_state->owner->so_cred;
2807 refcount_inc(&lsp->ls_count);
2808 spin_unlock(&state->state_lock);
2810 nfs4_put_lock_state(prev);
2813 status = nfs41_test_and_free_expired_stateid(server,
2816 trace_nfs4_test_lock_stateid(state, lsp, status);
2817 if (status == -NFS4ERR_EXPIRED ||
2818 status == -NFS4ERR_BAD_STATEID) {
2819 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
2820 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
2821 if (!recover_lost_locks)
2822 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2823 } else if (status != NFS_OK) {
2825 nfs4_put_lock_state(prev);
2828 spin_lock(&state->state_lock);
2831 spin_unlock(&state->state_lock);
2832 nfs4_put_lock_state(prev);
2838 * nfs41_check_open_stateid - possibly free an open stateid
2840 * @state: NFSv4 state for an inode
2842 * Returns NFS_OK if recovery for this stateid is now finished.
2843 * Otherwise a negative NFS4ERR value is returned.
2845 static int nfs41_check_open_stateid(struct nfs4_state *state)
2847 struct nfs_server *server = NFS_SERVER(state->inode);
2848 nfs4_stateid *stateid = &state->open_stateid;
2849 const struct cred *cred = state->owner->so_cred;
2852 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
2853 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2854 if (nfs4_have_delegation(state->inode, state->state))
2856 return -NFS4ERR_OPENMODE;
2858 return -NFS4ERR_BAD_STATEID;
2860 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
2861 trace_nfs4_test_open_stateid(state, NULL, status);
2862 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
2863 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2864 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2865 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2866 clear_bit(NFS_OPEN_STATE, &state->flags);
2867 stateid->type = NFS4_INVALID_STATEID_TYPE;
2870 if (nfs_open_stateid_recover_openmode(state))
2871 return -NFS4ERR_OPENMODE;
2875 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2879 nfs41_check_delegation_stateid(state);
2880 status = nfs41_check_expired_locks(state);
2881 if (status != NFS_OK)
2883 status = nfs41_check_open_stateid(state);
2884 if (status != NFS_OK)
2885 status = nfs4_open_expired(sp, state);
2891 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2892 * fields corresponding to attributes that were used to store the verifier.
2893 * Make sure we clobber those fields in the later setattr call
2895 static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2896 struct iattr *sattr, struct nfs4_label **label)
2898 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2903 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
2904 attrset[i] = opendata->o_res.attrset[i];
2905 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
2906 attrset[i] &= ~bitmask[i];
2909 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
2910 sattr->ia_valid : 0;
2912 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
2913 if (sattr->ia_valid & ATTR_ATIME_SET)
2914 ret |= ATTR_ATIME_SET;
2919 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
2920 if (sattr->ia_valid & ATTR_MTIME_SET)
2921 ret |= ATTR_MTIME_SET;
2926 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
2931 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2932 int flags, struct nfs_open_context *ctx)
2934 struct nfs4_state_owner *sp = opendata->owner;
2935 struct nfs_server *server = sp->so_server;
2936 struct dentry *dentry;
2937 struct nfs4_state *state;
2938 fmode_t acc_mode = _nfs4_ctx_to_accessmode(ctx);
2942 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2944 ret = _nfs4_proc_open(opendata, ctx);
2948 state = _nfs4_opendata_to_nfs4_state(opendata);
2949 ret = PTR_ERR(state);
2953 if (server->caps & NFS_CAP_POSIX_LOCK)
2954 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2955 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2956 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
2958 dentry = opendata->dentry;
2959 if (d_really_is_negative(dentry)) {
2960 struct dentry *alias;
2962 alias = d_exact_alias(dentry, state->inode);
2964 alias = d_splice_alias(igrab(state->inode), dentry);
2965 /* d_splice_alias() can't fail here - it's a non-directory */
2968 ctx->dentry = dentry = alias;
2970 nfs_set_verifier(dentry,
2971 nfs_save_change_attribute(d_inode(opendata->dir)));
2974 /* Parse layoutget results before we check for access */
2975 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
2977 ret = nfs4_opendata_access(sp->so_cred, opendata, state,
2982 if (d_inode(dentry) == state->inode) {
2983 nfs_inode_attach_open_context(ctx);
2984 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2985 nfs4_schedule_stateid_recovery(server, state);
2989 if (!opendata->cancelled)
2990 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
2995 * Returns a referenced nfs4_state
2997 static int _nfs4_do_open(struct inode *dir,
2998 struct nfs_open_context *ctx,
3000 const struct nfs4_open_createattrs *c,
3003 struct nfs4_state_owner *sp;
3004 struct nfs4_state *state = NULL;
3005 struct nfs_server *server = NFS_SERVER(dir);
3006 struct nfs4_opendata *opendata;
3007 struct dentry *dentry = ctx->dentry;
3008 const struct cred *cred = ctx->cred;
3009 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
3010 fmode_t fmode = _nfs4_ctx_to_openmode(ctx);
3011 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
3012 struct iattr *sattr = c->sattr;
3013 struct nfs4_label *label = c->label;
3014 struct nfs4_label *olabel = NULL;
3017 /* Protect against reboot recovery conflicts */
3019 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
3021 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
3024 status = nfs4_client_recover_expired_lease(server->nfs_client);
3026 goto err_put_state_owner;
3027 if (d_really_is_positive(dentry))
3028 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
3030 if (d_really_is_positive(dentry))
3031 claim = NFS4_OPEN_CLAIM_FH;
3032 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
3033 c, claim, GFP_KERNEL);
3034 if (opendata == NULL)
3035 goto err_put_state_owner;
3038 olabel = nfs4_label_alloc(server, GFP_KERNEL);
3039 if (IS_ERR(olabel)) {
3040 status = PTR_ERR(olabel);
3041 goto err_opendata_put;
3045 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
3046 if (!opendata->f_attr.mdsthreshold) {
3047 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
3048 if (!opendata->f_attr.mdsthreshold)
3049 goto err_free_label;
3051 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
3053 if (d_really_is_positive(dentry))
3054 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
3056 status = _nfs4_open_and_get_state(opendata, flags, ctx);
3058 goto err_free_label;
3061 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
3062 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
3063 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
3065 * send create attributes which was not set by open
3066 * with an extra setattr.
3068 if (attrs || label) {
3069 unsigned ia_old = sattr->ia_valid;
3071 sattr->ia_valid = attrs;
3072 nfs_fattr_init(opendata->o_res.f_attr);
3073 status = nfs4_do_setattr(state->inode, cred,
3074 opendata->o_res.f_attr, sattr,
3075 ctx, label, olabel);
3077 nfs_setattr_update_inode(state->inode, sattr,
3078 opendata->o_res.f_attr);
3079 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
3081 sattr->ia_valid = ia_old;
3084 if (opened && opendata->file_created)
3087 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
3088 *ctx_th = opendata->f_attr.mdsthreshold;
3089 opendata->f_attr.mdsthreshold = NULL;
3092 nfs4_label_free(olabel);
3094 nfs4_opendata_put(opendata);
3095 nfs4_put_state_owner(sp);
3098 nfs4_label_free(olabel);
3100 nfs4_opendata_put(opendata);
3101 err_put_state_owner:
3102 nfs4_put_state_owner(sp);
3108 static struct nfs4_state *nfs4_do_open(struct inode *dir,
3109 struct nfs_open_context *ctx,
3111 struct iattr *sattr,
3112 struct nfs4_label *label,
3115 struct nfs_server *server = NFS_SERVER(dir);
3116 struct nfs4_exception exception = {
3117 .interruptible = true,
3119 struct nfs4_state *res;
3120 struct nfs4_open_createattrs c = {
3124 [0] = (__u32)jiffies,
3125 [1] = (__u32)current->pid,
3131 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
3133 trace_nfs4_open_file(ctx, flags, status);
3136 /* NOTE: BAD_SEQID means the server and client disagree about the
3137 * book-keeping w.r.t. state-changing operations
3138 * (OPEN/CLOSE/LOCK/LOCKU...)
3139 * It is actually a sign of a bug on the client or on the server.
3141 * If we receive a BAD_SEQID error in the particular case of
3142 * doing an OPEN, we assume that nfs_increment_open_seqid() will
3143 * have unhashed the old state_owner for us, and that we can
3144 * therefore safely retry using a new one. We should still warn
3145 * the user though...
3147 if (status == -NFS4ERR_BAD_SEQID) {
3148 pr_warn_ratelimited("NFS: v4 server %s "
3149 " returned a bad sequence-id error!\n",
3150 NFS_SERVER(dir)->nfs_client->cl_hostname);
3151 exception.retry = 1;
3155 * BAD_STATEID on OPEN means that the server cancelled our
3156 * state before it received the OPEN_CONFIRM.
3157 * Recover by retrying the request as per the discussion
3158 * on Page 181 of RFC3530.
3160 if (status == -NFS4ERR_BAD_STATEID) {
3161 exception.retry = 1;
3164 if (status == -EAGAIN) {
3165 /* We must have found a delegation */
3166 exception.retry = 1;
3169 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3171 res = ERR_PTR(nfs4_handle_exception(server,
3172 status, &exception));
3173 } while (exception.retry);
3177 static int _nfs4_do_setattr(struct inode *inode,
3178 struct nfs_setattrargs *arg,
3179 struct nfs_setattrres *res,
3180 const struct cred *cred,
3181 struct nfs_open_context *ctx)
3183 struct nfs_server *server = NFS_SERVER(inode);
3184 struct rpc_message msg = {
3185 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3190 const struct cred *delegation_cred = NULL;
3191 unsigned long timestamp = jiffies;
3195 nfs_fattr_init(res->fattr);
3197 /* Servers should only apply open mode checks for file size changes */
3198 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
3202 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
3203 /* Use that stateid */
3204 } else if (ctx != NULL) {
3205 struct nfs_lock_context *l_ctx;
3206 if (!nfs4_valid_open_stateid(ctx->state))
3208 l_ctx = nfs_get_lock_context(ctx);
3210 return PTR_ERR(l_ctx);
3211 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3212 &arg->stateid, &delegation_cred);
3213 nfs_put_lock_context(l_ctx);
3218 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
3220 if (delegation_cred)
3221 msg.rpc_cred = delegation_cred;
3223 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3225 put_cred(delegation_cred);
3226 if (status == 0 && ctx != NULL)
3227 renew_lease(server, timestamp);
3228 trace_nfs4_setattr(inode, &arg->stateid, status);
3232 static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
3233 struct nfs_fattr *fattr, struct iattr *sattr,
3234 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
3235 struct nfs4_label *olabel)
3237 struct nfs_server *server = NFS_SERVER(inode);
3238 __u32 bitmask[NFS4_BITMASK_SZ];
3239 struct nfs4_state *state = ctx ? ctx->state : NULL;
3240 struct nfs_setattrargs arg = {
3241 .fh = NFS_FH(inode),
3247 struct nfs_setattrres res = {
3252 struct nfs4_exception exception = {
3255 .stateid = &arg.stateid,
3260 nfs4_bitmap_copy_adjust_setattr(bitmask,
3261 nfs4_bitmask(server, olabel),
3264 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
3266 case -NFS4ERR_OPENMODE:
3267 if (!(sattr->ia_valid & ATTR_SIZE)) {
3268 pr_warn_once("NFSv4: server %s is incorrectly "
3269 "applying open mode checks to "
3270 "a SETATTR that is not "
3271 "changing file size.\n",
3272 server->nfs_client->cl_hostname);
3274 if (state && !(state->state & FMODE_WRITE)) {
3276 if (sattr->ia_valid & ATTR_OPEN)
3281 err = nfs4_handle_exception(server, err, &exception);
3282 } while (exception.retry);
3288 nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3290 if (inode == NULL || !nfs_have_layout(inode))
3293 return pnfs_wait_on_layoutreturn(inode, task);
3296 struct nfs4_closedata {
3297 struct inode *inode;
3298 struct nfs4_state *state;
3299 struct nfs_closeargs arg;
3300 struct nfs_closeres res;
3302 struct nfs4_layoutreturn_args arg;
3303 struct nfs4_layoutreturn_res res;
3304 struct nfs4_xdr_opaque_data ld_private;
3308 struct nfs_fattr fattr;
3309 unsigned long timestamp;
3312 static void nfs4_free_closedata(void *data)
3314 struct nfs4_closedata *calldata = data;
3315 struct nfs4_state_owner *sp = calldata->state->owner;
3316 struct super_block *sb = calldata->state->inode->i_sb;
3318 if (calldata->lr.roc)
3319 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3320 calldata->res.lr_ret);
3321 nfs4_put_open_state(calldata->state);
3322 nfs_free_seqid(calldata->arg.seqid);
3323 nfs4_put_state_owner(sp);
3324 nfs_sb_deactive(sb);
3328 static void nfs4_close_done(struct rpc_task *task, void *data)
3330 struct nfs4_closedata *calldata = data;
3331 struct nfs4_state *state = calldata->state;
3332 struct nfs_server *server = NFS_SERVER(calldata->inode);
3333 nfs4_stateid *res_stateid = NULL;
3334 struct nfs4_exception exception = {
3336 .inode = calldata->inode,
3337 .stateid = &calldata->arg.stateid,
3340 dprintk("%s: begin!\n", __func__);
3341 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3343 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
3345 /* Handle Layoutreturn errors */
3346 if (calldata->arg.lr_args && task->tk_status != 0) {
3347 switch (calldata->res.lr_ret) {
3349 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3352 calldata->arg.lr_args = NULL;
3353 calldata->res.lr_res = NULL;
3355 case -NFS4ERR_OLD_STATEID:
3356 if (nfs4_layoutreturn_refresh_stateid(&calldata->arg.lr_args->stateid,
3357 &calldata->arg.lr_args->range,
3361 case -NFS4ERR_ADMIN_REVOKED:
3362 case -NFS4ERR_DELEG_REVOKED:
3363 case -NFS4ERR_EXPIRED:
3364 case -NFS4ERR_BAD_STATEID:
3365 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3366 case -NFS4ERR_WRONG_CRED:
3367 calldata->arg.lr_args = NULL;
3368 calldata->res.lr_res = NULL;
3373 /* hmm. we are done with the inode, and in the process of freeing
3374 * the state_owner. we keep this around to process errors
3376 switch (task->tk_status) {
3378 res_stateid = &calldata->res.stateid;
3379 renew_lease(server, calldata->timestamp);
3381 case -NFS4ERR_ACCESS:
3382 if (calldata->arg.bitmask != NULL) {
3383 calldata->arg.bitmask = NULL;
3384 calldata->res.fattr = NULL;
3389 case -NFS4ERR_OLD_STATEID:
3390 /* Did we race with OPEN? */
3391 if (nfs4_refresh_open_stateid(&calldata->arg.stateid,
3395 case -NFS4ERR_ADMIN_REVOKED:
3396 case -NFS4ERR_STALE_STATEID:
3397 case -NFS4ERR_EXPIRED:
3398 nfs4_free_revoked_stateid(server,
3399 &calldata->arg.stateid,
3400 task->tk_msg.rpc_cred);
3402 case -NFS4ERR_BAD_STATEID:
3405 task->tk_status = nfs4_async_handle_exception(task,
3406 server, task->tk_status, &exception);
3407 if (exception.retry)
3410 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3411 res_stateid, calldata->arg.fmode);
3413 task->tk_status = 0;
3414 nfs_release_seqid(calldata->arg.seqid);
3415 nfs_refresh_inode(calldata->inode, &calldata->fattr);
3416 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
3419 calldata->res.lr_ret = 0;
3421 task->tk_status = 0;
3422 rpc_restart_call_prepare(task);
3426 static void nfs4_close_prepare(struct rpc_task *task, void *data)
3428 struct nfs4_closedata *calldata = data;
3429 struct nfs4_state *state = calldata->state;
3430 struct inode *inode = calldata->inode;
3431 struct pnfs_layout_hdr *lo;
3432 bool is_rdonly, is_wronly, is_rdwr;
3435 dprintk("%s: begin!\n", __func__);
3436 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3439 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
3440 spin_lock(&state->owner->so_lock);
3441 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3442 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3443 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
3444 /* Calculate the change in open mode */
3445 calldata->arg.fmode = 0;
3446 if (state->n_rdwr == 0) {
3447 if (state->n_rdonly == 0)
3448 call_close |= is_rdonly;
3450 calldata->arg.fmode |= FMODE_READ;
3451 if (state->n_wronly == 0)
3452 call_close |= is_wronly;
3454 calldata->arg.fmode |= FMODE_WRITE;
3455 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3456 call_close |= is_rdwr;
3458 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3460 if (!nfs4_valid_open_stateid(state) ||
3461 !nfs4_refresh_open_stateid(&calldata->arg.stateid, state))
3463 spin_unlock(&state->owner->so_lock);
3466 /* Note: exit _without_ calling nfs4_close_done */
3470 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
3471 nfs_release_seqid(calldata->arg.seqid);
3475 lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3476 if (lo && !pnfs_layout_is_valid(lo)) {
3477 calldata->arg.lr_args = NULL;
3478 calldata->res.lr_res = NULL;
3481 if (calldata->arg.fmode == 0)
3482 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
3484 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
3485 /* Close-to-open cache consistency revalidation */
3486 if (!nfs4_have_delegation(inode, FMODE_READ))
3487 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3489 calldata->arg.bitmask = NULL;
3492 calldata->arg.share_access =
3493 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3494 calldata->arg.fmode, 0);
3496 if (calldata->res.fattr == NULL)
3497 calldata->arg.bitmask = NULL;
3498 else if (calldata->arg.bitmask == NULL)
3499 calldata->res.fattr = NULL;
3500 calldata->timestamp = jiffies;
3501 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
3502 &calldata->arg.seq_args,
3503 &calldata->res.seq_res,
3505 nfs_release_seqid(calldata->arg.seqid);
3506 dprintk("%s: done!\n", __func__);
3509 task->tk_action = NULL;
3511 nfs4_sequence_done(task, &calldata->res.seq_res);
3514 static const struct rpc_call_ops nfs4_close_ops = {
3515 .rpc_call_prepare = nfs4_close_prepare,
3516 .rpc_call_done = nfs4_close_done,
3517 .rpc_release = nfs4_free_closedata,
3521 * It is possible for data to be read/written from a mem-mapped file
3522 * after the sys_close call (which hits the vfs layer as a flush).
3523 * This means that we can't safely call nfsv4 close on a file until
3524 * the inode is cleared. This in turn means that we are not good
3525 * NFSv4 citizens - we do not indicate to the server to update the file's
3526 * share state even when we are done with one of the three share
3527 * stateid's in the inode.
3529 * NOTE: Caller must be holding the sp->so_owner semaphore!
3531 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
3533 struct nfs_server *server = NFS_SERVER(state->inode);
3534 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
3535 struct nfs4_closedata *calldata;
3536 struct nfs4_state_owner *sp = state->owner;
3537 struct rpc_task *task;
3538 struct rpc_message msg = {
3539 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3540 .rpc_cred = state->owner->so_cred,
3542 struct rpc_task_setup task_setup_data = {
3543 .rpc_client = server->client,
3544 .rpc_message = &msg,
3545 .callback_ops = &nfs4_close_ops,
3546 .workqueue = nfsiod_workqueue,
3547 .flags = RPC_TASK_ASYNC,
3549 int status = -ENOMEM;
3551 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3552 &task_setup_data.rpc_client, &msg);
3554 calldata = kzalloc(sizeof(*calldata), gfp_mask);
3555 if (calldata == NULL)
3557 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
3558 calldata->inode = state->inode;
3559 calldata->state = state;
3560 calldata->arg.fh = NFS_FH(state->inode);
3561 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3562 goto out_free_calldata;
3563 /* Serialization for the sequence id */
3564 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3565 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
3566 if (IS_ERR(calldata->arg.seqid))
3567 goto out_free_calldata;
3568 nfs_fattr_init(&calldata->fattr);
3569 calldata->arg.fmode = 0;
3570 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
3571 calldata->res.fattr = &calldata->fattr;
3572 calldata->res.seqid = calldata->arg.seqid;
3573 calldata->res.server = server;
3574 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3575 calldata->lr.roc = pnfs_roc(state->inode,
3576 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3577 if (calldata->lr.roc) {
3578 calldata->arg.lr_args = &calldata->lr.arg;
3579 calldata->res.lr_res = &calldata->lr.res;
3581 nfs_sb_active(calldata->inode->i_sb);
3583 msg.rpc_argp = &calldata->arg;
3584 msg.rpc_resp = &calldata->res;
3585 task_setup_data.callback_data = calldata;
3586 task = rpc_run_task(&task_setup_data);
3588 return PTR_ERR(task);
3591 status = rpc_wait_for_completion_task(task);
3597 nfs4_put_open_state(state);
3598 nfs4_put_state_owner(sp);
3602 static struct inode *
3603 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3604 int open_flags, struct iattr *attr, int *opened)
3606 struct nfs4_state *state;
3607 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3609 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
3611 /* Protect against concurrent sillydeletes */
3612 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
3614 nfs4_label_release_security(label);
3617 return ERR_CAST(state);
3618 return state->inode;
3621 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
3623 if (ctx->state == NULL)
3626 nfs4_close_sync(ctx->state, _nfs4_ctx_to_openmode(ctx));
3628 nfs4_close_state(ctx->state, _nfs4_ctx_to_openmode(ctx));
3631 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3632 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3633 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
3635 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3637 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
3638 struct nfs4_server_caps_arg args = {
3642 struct nfs4_server_caps_res res = {};
3643 struct rpc_message msg = {
3644 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
3651 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3652 FATTR4_WORD0_FH_EXPIRE_TYPE |
3653 FATTR4_WORD0_LINK_SUPPORT |
3654 FATTR4_WORD0_SYMLINK_SUPPORT |
3655 FATTR4_WORD0_ACLSUPPORT;
3657 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3659 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3661 /* Sanity check the server answers */
3662 switch (minorversion) {
3664 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3665 res.attr_bitmask[2] = 0;
3668 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3671 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3673 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
3674 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3675 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3676 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3677 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
3678 NFS_CAP_CTIME|NFS_CAP_MTIME|
3679 NFS_CAP_SECURITY_LABEL);
3680 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3681 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3682 server->caps |= NFS_CAP_ACLS;
3683 if (res.has_links != 0)
3684 server->caps |= NFS_CAP_HARDLINKS;
3685 if (res.has_symlinks != 0)
3686 server->caps |= NFS_CAP_SYMLINKS;
3687 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3688 server->caps |= NFS_CAP_FILEID;
3689 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3690 server->caps |= NFS_CAP_MODE;
3691 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3692 server->caps |= NFS_CAP_NLINK;
3693 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3694 server->caps |= NFS_CAP_OWNER;
3695 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3696 server->caps |= NFS_CAP_OWNER_GROUP;
3697 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3698 server->caps |= NFS_CAP_ATIME;
3699 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3700 server->caps |= NFS_CAP_CTIME;
3701 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3702 server->caps |= NFS_CAP_MTIME;
3703 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3704 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3705 server->caps |= NFS_CAP_SECURITY_LABEL;
3707 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3708 sizeof(server->attr_bitmask));
3709 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3711 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3712 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3713 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
3714 server->cache_consistency_bitmask[2] = 0;
3716 /* Avoid a regression due to buggy server */
3717 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3718 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
3719 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3720 sizeof(server->exclcreat_bitmask));
3722 server->acl_bitmask = res.acl_bitmask;
3723 server->fh_expire_type = res.fh_expire_type;
3729 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3731 struct nfs4_exception exception = {
3732 .interruptible = true,
3736 err = nfs4_handle_exception(server,
3737 _nfs4_server_capabilities(server, fhandle),
3739 } while (exception.retry);
3743 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3744 struct nfs_fsinfo *info)
3747 struct nfs4_lookup_root_arg args = {
3750 struct nfs4_lookup_res res = {
3752 .fattr = info->fattr,
3755 struct rpc_message msg = {
3756 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3761 bitmask[0] = nfs4_fattr_bitmap[0];
3762 bitmask[1] = nfs4_fattr_bitmap[1];
3764 * Process the label in the upcoming getfattr
3766 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3768 nfs_fattr_init(info->fattr);
3769 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3772 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3773 struct nfs_fsinfo *info)
3775 struct nfs4_exception exception = {
3776 .interruptible = true,
3780 err = _nfs4_lookup_root(server, fhandle, info);
3781 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
3784 case -NFS4ERR_WRONGSEC:
3787 err = nfs4_handle_exception(server, err, &exception);
3789 } while (exception.retry);
3794 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3795 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3797 struct rpc_auth_create_args auth_args = {
3798 .pseudoflavor = flavor,
3800 struct rpc_auth *auth;
3802 auth = rpcauth_create(&auth_args, server->client);
3805 return nfs4_lookup_root(server, fhandle, info);
3809 * Retry pseudoroot lookup with various security flavors. We do this when:
3811 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3812 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3814 * Returns zero on success, or a negative NFS4ERR value, or a
3815 * negative errno value.
3817 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3818 struct nfs_fsinfo *info)
3820 /* Per 3530bis 15.33.5 */
3821 static const rpc_authflavor_t flav_array[] = {
3825 RPC_AUTH_UNIX, /* courtesy */
3828 int status = -EPERM;
3831 if (server->auth_info.flavor_len > 0) {
3832 /* try each flavor specified by user */
3833 for (i = 0; i < server->auth_info.flavor_len; i++) {
3834 status = nfs4_lookup_root_sec(server, fhandle, info,
3835 server->auth_info.flavors[i]);
3836 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3841 /* no flavors specified by user, try default list */
3842 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3843 status = nfs4_lookup_root_sec(server, fhandle, info,
3845 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3852 * -EACCES could mean that the user doesn't have correct permissions
3853 * to access the mount. It could also mean that we tried to mount
3854 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3855 * existing mount programs don't handle -EACCES very well so it should
3856 * be mapped to -EPERM instead.
3858 if (status == -EACCES)
3864 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3865 * @server: initialized nfs_server handle
3866 * @fhandle: we fill in the pseudo-fs root file handle
3867 * @info: we fill in an FSINFO struct
3868 * @auth_probe: probe the auth flavours
3870 * Returns zero on success, or a negative errno.
3872 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
3873 struct nfs_fsinfo *info,
3879 status = nfs4_lookup_root(server, fhandle, info);
3881 if (auth_probe || status == NFS4ERR_WRONGSEC)
3882 status = server->nfs_client->cl_mvops->find_root_sec(server,
3886 status = nfs4_server_capabilities(server, fhandle);
3888 status = nfs4_do_fsinfo(server, fhandle, info);
3890 return nfs4_map_errors(status);
3893 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3894 struct nfs_fsinfo *info)
3897 struct nfs_fattr *fattr = info->fattr;
3898 struct nfs4_label *label = NULL;
3900 error = nfs4_server_capabilities(server, mntfh);
3902 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3906 label = nfs4_label_alloc(server, GFP_KERNEL);
3908 return PTR_ERR(label);
3910 error = nfs4_proc_getattr(server, mntfh, fattr, label, NULL);
3912 dprintk("nfs4_get_root: getattr error = %d\n", -error);
3913 goto err_free_label;
3916 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3917 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3918 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3921 nfs4_label_free(label);
3927 * Get locations and (maybe) other attributes of a referral.
3928 * Note that we'll actually follow the referral later when
3929 * we detect fsid mismatch in inode revalidation
3931 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3932 const struct qstr *name, struct nfs_fattr *fattr,
3933 struct nfs_fh *fhandle)
3935 int status = -ENOMEM;
3936 struct page *page = NULL;
3937 struct nfs4_fs_locations *locations = NULL;
3939 page = alloc_page(GFP_KERNEL);
3942 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3943 if (locations == NULL)
3946 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
3951 * If the fsid didn't change, this is a migration event, not a
3952 * referral. Cause us to drop into the exception handler, which
3953 * will kick off migration recovery.
3955 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
3956 dprintk("%s: server did not return a different fsid for"
3957 " a referral at %s\n", __func__, name->name);
3958 status = -NFS4ERR_MOVED;
3961 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3962 nfs_fixup_referral_attributes(&locations->fattr);
3964 /* replace the lookup nfs_fattr with the locations nfs_fattr */
3965 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
3966 memset(fhandle, 0, sizeof(struct nfs_fh));
3974 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3975 struct nfs_fattr *fattr, struct nfs4_label *label,
3976 struct inode *inode)
3978 __u32 bitmask[NFS4_BITMASK_SZ];
3979 struct nfs4_getattr_arg args = {
3983 struct nfs4_getattr_res res = {
3988 struct rpc_message msg = {
3989 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3994 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode);
3996 nfs_fattr_init(fattr);
3997 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4000 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4001 struct nfs_fattr *fattr, struct nfs4_label *label,
4002 struct inode *inode)
4004 struct nfs4_exception exception = {
4005 .interruptible = true,
4009 err = _nfs4_proc_getattr(server, fhandle, fattr, label, inode);
4010 trace_nfs4_getattr(server, fhandle, fattr, err);
4011 err = nfs4_handle_exception(server, err,
4013 } while (exception.retry);
4018 * The file is not closed if it is opened due to the a request to change
4019 * the size of the file. The open call will not be needed once the
4020 * VFS layer lookup-intents are implemented.
4022 * Close is called when the inode is destroyed.
4023 * If we haven't opened the file for O_WRONLY, we
4024 * need to in the size_change case to obtain a stateid.
4027 * Because OPEN is always done by name in nfsv4, it is
4028 * possible that we opened a different file by the same
4029 * name. We can recognize this race condition, but we
4030 * can't do anything about it besides returning an error.
4032 * This will be fixed with VFS changes (lookup-intent).
4035 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
4036 struct iattr *sattr)
4038 struct inode *inode = d_inode(dentry);
4039 const struct cred *cred = NULL;
4040 struct nfs_open_context *ctx = NULL;
4041 struct nfs4_label *label = NULL;
4044 if (pnfs_ld_layoutret_on_setattr(inode) &&
4045 sattr->ia_valid & ATTR_SIZE &&
4046 sattr->ia_size < i_size_read(inode))
4047 pnfs_commit_and_return_layout(inode);
4049 nfs_fattr_init(fattr);
4051 /* Deal with open(O_TRUNC) */
4052 if (sattr->ia_valid & ATTR_OPEN)
4053 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
4055 /* Optimization: if the end result is no change, don't RPC */
4056 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
4059 /* Search for an existing open(O_WRITE) file */
4060 if (sattr->ia_valid & ATTR_FILE) {
4062 ctx = nfs_file_open_context(sattr->ia_file);
4067 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4069 return PTR_ERR(label);
4071 /* Return any delegations if we're going to change ACLs */
4072 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
4073 nfs4_inode_make_writeable(inode);
4075 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
4077 nfs_setattr_update_inode(inode, sattr, fattr);
4078 nfs_setsecurity(inode, fattr, label);
4080 nfs4_label_free(label);
4084 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
4085 const struct qstr *name, struct nfs_fh *fhandle,
4086 struct nfs_fattr *fattr, struct nfs4_label *label)
4088 struct nfs_server *server = NFS_SERVER(dir);
4090 struct nfs4_lookup_arg args = {
4091 .bitmask = server->attr_bitmask,
4092 .dir_fh = NFS_FH(dir),
4095 struct nfs4_lookup_res res = {
4101 struct rpc_message msg = {
4102 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4107 args.bitmask = nfs4_bitmask(server, label);
4109 nfs_fattr_init(fattr);
4111 dprintk("NFS call lookup %s\n", name->name);
4112 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
4113 dprintk("NFS reply lookup: %d\n", status);
4117 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
4119 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4120 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
4121 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4125 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
4126 const struct qstr *name, struct nfs_fh *fhandle,
4127 struct nfs_fattr *fattr, struct nfs4_label *label)
4129 struct nfs4_exception exception = {
4130 .interruptible = true,
4132 struct rpc_clnt *client = *clnt;
4135 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
4136 trace_nfs4_lookup(dir, name, err);
4138 case -NFS4ERR_BADNAME:
4141 case -NFS4ERR_MOVED:
4142 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
4143 if (err == -NFS4ERR_MOVED)
4144 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4146 case -NFS4ERR_WRONGSEC:
4148 if (client != *clnt)
4150 client = nfs4_negotiate_security(client, dir, name);
4152 return PTR_ERR(client);
4154 exception.retry = 1;
4157 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4159 } while (exception.retry);
4164 else if (client != *clnt)
4165 rpc_shutdown_client(client);
4170 static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
4171 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4172 struct nfs4_label *label)
4175 struct rpc_clnt *client = NFS_CLIENT(dir);
4177 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
4178 if (client != NFS_CLIENT(dir)) {
4179 rpc_shutdown_client(client);
4180 nfs_fixup_secinfo_attributes(fattr);
4186 nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
4187 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4189 struct rpc_clnt *client = NFS_CLIENT(dir);
4192 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
4194 return ERR_PTR(status);
4195 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
4198 static int _nfs4_proc_lookupp(struct inode *inode,
4199 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4200 struct nfs4_label *label)
4202 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4203 struct nfs_server *server = NFS_SERVER(inode);
4205 struct nfs4_lookupp_arg args = {
4206 .bitmask = server->attr_bitmask,
4207 .fh = NFS_FH(inode),
4209 struct nfs4_lookupp_res res = {
4215 struct rpc_message msg = {
4216 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4221 args.bitmask = nfs4_bitmask(server, label);
4223 nfs_fattr_init(fattr);
4225 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4226 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4228 dprintk("NFS reply lookupp: %d\n", status);
4232 static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4233 struct nfs_fattr *fattr, struct nfs4_label *label)
4235 struct nfs4_exception exception = {
4236 .interruptible = true,
4240 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4241 trace_nfs4_lookupp(inode, err);
4242 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4244 } while (exception.retry);
4248 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4250 struct nfs_server *server = NFS_SERVER(inode);
4251 struct nfs4_accessargs args = {
4252 .fh = NFS_FH(inode),
4253 .access = entry->mask,
4255 struct nfs4_accessres res = {
4258 struct rpc_message msg = {
4259 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4262 .rpc_cred = entry->cred,
4266 if (!nfs4_have_delegation(inode, FMODE_READ)) {
4267 res.fattr = nfs_alloc_fattr();
4268 if (res.fattr == NULL)
4270 args.bitmask = server->cache_consistency_bitmask;
4272 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4274 nfs_access_set_mask(entry, res.access);
4276 nfs_refresh_inode(inode, res.fattr);
4278 nfs_free_fattr(res.fattr);
4282 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4284 struct nfs4_exception exception = {
4285 .interruptible = true,
4289 err = _nfs4_proc_access(inode, entry);
4290 trace_nfs4_access(inode, err);
4291 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4293 } while (exception.retry);
4298 * TODO: For the time being, we don't try to get any attributes
4299 * along with any of the zero-copy operations READ, READDIR,
4302 * In the case of the first three, we want to put the GETATTR
4303 * after the read-type operation -- this is because it is hard
4304 * to predict the length of a GETATTR response in v4, and thus
4305 * align the READ data correctly. This means that the GETATTR
4306 * may end up partially falling into the page cache, and we should
4307 * shift it into the 'tail' of the xdr_buf before processing.
4308 * To do this efficiently, we need to know the total length
4309 * of data received, which doesn't seem to be available outside
4312 * In the case of WRITE, we also want to put the GETATTR after
4313 * the operation -- in this case because we want to make sure
4314 * we get the post-operation mtime and size.
4316 * Both of these changes to the XDR layer would in fact be quite
4317 * minor, but I decided to leave them for a subsequent patch.
4319 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4320 unsigned int pgbase, unsigned int pglen)
4322 struct nfs4_readlink args = {
4323 .fh = NFS_FH(inode),
4328 struct nfs4_readlink_res res;
4329 struct rpc_message msg = {
4330 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4335 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
4338 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4339 unsigned int pgbase, unsigned int pglen)
4341 struct nfs4_exception exception = {
4342 .interruptible = true,
4346 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4347 trace_nfs4_readlink(inode, err);
4348 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4350 } while (exception.retry);
4355 * This is just for mknod. open(O_CREAT) will always do ->open_context().
4358 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
4361 struct nfs_server *server = NFS_SERVER(dir);
4362 struct nfs4_label l, *ilabel = NULL;
4363 struct nfs_open_context *ctx;
4364 struct nfs4_state *state;
4367 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
4369 return PTR_ERR(ctx);
4371 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4373 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4374 sattr->ia_mode &= ~current_umask();
4375 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
4376 if (IS_ERR(state)) {
4377 status = PTR_ERR(state);
4381 nfs4_label_release_security(ilabel);
4382 put_nfs_open_context(ctx);
4387 _nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
4389 struct nfs_server *server = NFS_SERVER(dir);
4390 struct nfs_removeargs args = {
4394 struct nfs_removeres res = {
4397 struct rpc_message msg = {
4398 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4402 unsigned long timestamp = jiffies;
4405 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
4407 spin_lock(&dir->i_lock);
4408 update_changeattr_locked(dir, &res.cinfo, timestamp, 0);
4409 /* Removing a directory decrements nlink in the parent */
4410 if (ftype == NF4DIR && dir->i_nlink > 2)
4411 nfs4_dec_nlink_locked(dir);
4412 spin_unlock(&dir->i_lock);
4417 static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4419 struct nfs4_exception exception = {
4420 .interruptible = true,
4422 struct inode *inode = d_inode(dentry);
4426 if (inode->i_nlink == 1)
4427 nfs4_inode_return_delegation(inode);
4429 nfs4_inode_make_writeable(inode);
4432 err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
4433 trace_nfs4_remove(dir, &dentry->d_name, err);
4434 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4436 } while (exception.retry);
4440 static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
4442 struct nfs4_exception exception = {
4443 .interruptible = true,
4448 err = _nfs4_proc_remove(dir, name, NF4DIR);
4449 trace_nfs4_remove(dir, name, err);
4450 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4452 } while (exception.retry);
4456 static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4457 struct dentry *dentry,
4458 struct inode *inode)
4460 struct nfs_removeargs *args = msg->rpc_argp;
4461 struct nfs_removeres *res = msg->rpc_resp;
4463 res->server = NFS_SB(dentry->d_sb);
4464 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
4465 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
4467 nfs_fattr_init(res->dir_attr);
4470 nfs4_inode_return_delegation(inode);
4473 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4475 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
4476 &data->args.seq_args,
4481 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
4483 struct nfs_unlinkdata *data = task->tk_calldata;
4484 struct nfs_removeres *res = &data->res;
4486 if (!nfs4_sequence_done(task, &res->seq_res))
4488 if (nfs4_async_handle_error(task, res->server, NULL,
4489 &data->timeout) == -EAGAIN)
4491 if (task->tk_status == 0)
4492 update_changeattr(dir, &res->cinfo,
4493 res->dir_attr->time_start, 0);
4497 static void nfs4_proc_rename_setup(struct rpc_message *msg,
4498 struct dentry *old_dentry,
4499 struct dentry *new_dentry)
4501 struct nfs_renameargs *arg = msg->rpc_argp;
4502 struct nfs_renameres *res = msg->rpc_resp;
4503 struct inode *old_inode = d_inode(old_dentry);
4504 struct inode *new_inode = d_inode(new_dentry);
4507 nfs4_inode_make_writeable(old_inode);
4509 nfs4_inode_return_delegation(new_inode);
4510 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
4511 res->server = NFS_SB(old_dentry->d_sb);
4512 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
4515 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4517 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
4518 &data->args.seq_args,
4523 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4524 struct inode *new_dir)
4526 struct nfs_renamedata *data = task->tk_calldata;
4527 struct nfs_renameres *res = &data->res;
4529 if (!nfs4_sequence_done(task, &res->seq_res))
4531 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
4534 if (task->tk_status == 0) {
4535 if (new_dir != old_dir) {
4536 /* Note: If we moved a directory, nlink will change */
4537 update_changeattr(old_dir, &res->old_cinfo,
4538 res->old_fattr->time_start,
4539 NFS_INO_INVALID_OTHER);
4540 update_changeattr(new_dir, &res->new_cinfo,
4541 res->new_fattr->time_start,
4542 NFS_INO_INVALID_OTHER);
4544 update_changeattr(old_dir, &res->old_cinfo,
4545 res->old_fattr->time_start,
4551 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4553 struct nfs_server *server = NFS_SERVER(inode);
4554 __u32 bitmask[NFS4_BITMASK_SZ];
4555 struct nfs4_link_arg arg = {
4556 .fh = NFS_FH(inode),
4557 .dir_fh = NFS_FH(dir),
4561 struct nfs4_link_res res = {
4565 struct rpc_message msg = {
4566 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4570 int status = -ENOMEM;
4572 res.fattr = nfs_alloc_fattr();
4573 if (res.fattr == NULL)
4576 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4577 if (IS_ERR(res.label)) {
4578 status = PTR_ERR(res.label);
4582 nfs4_inode_make_writeable(inode);
4583 nfs4_bitmap_copy_adjust_setattr(bitmask, nfs4_bitmask(server, res.label), inode);
4585 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4587 update_changeattr(dir, &res.cinfo, res.fattr->time_start, 0);
4588 status = nfs_post_op_update_inode(inode, res.fattr);
4590 nfs_setsecurity(inode, res.fattr, res.label);
4594 nfs4_label_free(res.label);
4597 nfs_free_fattr(res.fattr);
4601 static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4603 struct nfs4_exception exception = {
4604 .interruptible = true,
4608 err = nfs4_handle_exception(NFS_SERVER(inode),
4609 _nfs4_proc_link(inode, dir, name),
4611 } while (exception.retry);
4615 struct nfs4_createdata {
4616 struct rpc_message msg;
4617 struct nfs4_create_arg arg;
4618 struct nfs4_create_res res;
4620 struct nfs_fattr fattr;
4621 struct nfs4_label *label;
4624 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
4625 const struct qstr *name, struct iattr *sattr, u32 ftype)
4627 struct nfs4_createdata *data;
4629 data = kzalloc(sizeof(*data), GFP_KERNEL);
4631 struct nfs_server *server = NFS_SERVER(dir);
4633 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4634 if (IS_ERR(data->label))
4637 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4638 data->msg.rpc_argp = &data->arg;
4639 data->msg.rpc_resp = &data->res;
4640 data->arg.dir_fh = NFS_FH(dir);
4641 data->arg.server = server;
4642 data->arg.name = name;
4643 data->arg.attrs = sattr;
4644 data->arg.ftype = ftype;
4645 data->arg.bitmask = nfs4_bitmask(server, data->label);
4646 data->arg.umask = current_umask();
4647 data->res.server = server;
4648 data->res.fh = &data->fh;
4649 data->res.fattr = &data->fattr;
4650 data->res.label = data->label;
4651 nfs_fattr_init(data->res.fattr);
4659 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4661 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
4662 &data->arg.seq_args, &data->res.seq_res, 1);
4664 spin_lock(&dir->i_lock);
4665 update_changeattr_locked(dir, &data->res.dir_cinfo,
4666 data->res.fattr->time_start, 0);
4667 /* Creating a directory bumps nlink in the parent */
4668 if (data->arg.ftype == NF4DIR)
4669 nfs4_inc_nlink_locked(dir);
4670 spin_unlock(&dir->i_lock);
4671 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
4676 static void nfs4_free_createdata(struct nfs4_createdata *data)
4678 nfs4_label_free(data->label);
4682 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4683 struct page *page, unsigned int len, struct iattr *sattr,
4684 struct nfs4_label *label)
4686 struct nfs4_createdata *data;
4687 int status = -ENAMETOOLONG;
4689 if (len > NFS4_MAXPATHLEN)
4693 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4697 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4698 data->arg.u.symlink.pages = &page;
4699 data->arg.u.symlink.len = len;
4700 data->arg.label = label;
4702 status = nfs4_do_create(dir, dentry, data);
4704 nfs4_free_createdata(data);
4709 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4710 struct page *page, unsigned int len, struct iattr *sattr)
4712 struct nfs4_exception exception = {
4713 .interruptible = true,
4715 struct nfs4_label l, *label = NULL;
4718 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4721 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4722 trace_nfs4_symlink(dir, &dentry->d_name, err);
4723 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4725 } while (exception.retry);
4727 nfs4_label_release_security(label);
4731 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4732 struct iattr *sattr, struct nfs4_label *label)
4734 struct nfs4_createdata *data;
4735 int status = -ENOMEM;
4737 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4741 data->arg.label = label;
4742 status = nfs4_do_create(dir, dentry, data);
4744 nfs4_free_createdata(data);
4749 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4750 struct iattr *sattr)
4752 struct nfs_server *server = NFS_SERVER(dir);
4753 struct nfs4_exception exception = {
4754 .interruptible = true,
4756 struct nfs4_label l, *label = NULL;
4759 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4761 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4762 sattr->ia_mode &= ~current_umask();
4764 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4765 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4766 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4768 } while (exception.retry);
4769 nfs4_label_release_security(label);
4774 static int _nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
4775 u64 cookie, struct page **pages, unsigned int count, bool plus)
4777 struct inode *dir = d_inode(dentry);
4778 struct nfs4_readdir_arg args = {
4783 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
4786 struct nfs4_readdir_res res;
4787 struct rpc_message msg = {
4788 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4795 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4797 (unsigned long long)cookie);
4798 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
4799 res.pgbase = args.pgbase;
4800 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4802 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
4803 status += args.pgbase;
4806 nfs_invalidate_atime(dir);
4808 dprintk("%s: returns %d\n", __func__, status);
4812 static int nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
4813 u64 cookie, struct page **pages, unsigned int count, bool plus)
4815 struct nfs4_exception exception = {
4816 .interruptible = true,
4820 err = _nfs4_proc_readdir(dentry, cred, cookie,
4821 pages, count, plus);
4822 trace_nfs4_readdir(d_inode(dentry), err);
4823 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
4825 } while (exception.retry);
4829 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4830 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
4832 struct nfs4_createdata *data;
4833 int mode = sattr->ia_mode;
4834 int status = -ENOMEM;
4836 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4841 data->arg.ftype = NF4FIFO;
4842 else if (S_ISBLK(mode)) {
4843 data->arg.ftype = NF4BLK;
4844 data->arg.u.device.specdata1 = MAJOR(rdev);
4845 data->arg.u.device.specdata2 = MINOR(rdev);
4847 else if (S_ISCHR(mode)) {
4848 data->arg.ftype = NF4CHR;
4849 data->arg.u.device.specdata1 = MAJOR(rdev);
4850 data->arg.u.device.specdata2 = MINOR(rdev);
4851 } else if (!S_ISSOCK(mode)) {
4856 data->arg.label = label;
4857 status = nfs4_do_create(dir, dentry, data);
4859 nfs4_free_createdata(data);
4864 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4865 struct iattr *sattr, dev_t rdev)
4867 struct nfs_server *server = NFS_SERVER(dir);
4868 struct nfs4_exception exception = {
4869 .interruptible = true,
4871 struct nfs4_label l, *label = NULL;
4874 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4876 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4877 sattr->ia_mode &= ~current_umask();
4879 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4880 trace_nfs4_mknod(dir, &dentry->d_name, err);
4881 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4883 } while (exception.retry);
4885 nfs4_label_release_security(label);
4890 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4891 struct nfs_fsstat *fsstat)
4893 struct nfs4_statfs_arg args = {
4895 .bitmask = server->attr_bitmask,
4897 struct nfs4_statfs_res res = {
4900 struct rpc_message msg = {
4901 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4906 nfs_fattr_init(fsstat->fattr);
4907 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4910 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4912 struct nfs4_exception exception = {
4913 .interruptible = true,
4917 err = nfs4_handle_exception(server,
4918 _nfs4_proc_statfs(server, fhandle, fsstat),
4920 } while (exception.retry);
4924 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4925 struct nfs_fsinfo *fsinfo)
4927 struct nfs4_fsinfo_arg args = {
4929 .bitmask = server->attr_bitmask,
4931 struct nfs4_fsinfo_res res = {
4934 struct rpc_message msg = {
4935 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4940 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4943 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4945 struct nfs4_exception exception = {
4946 .interruptible = true,
4948 unsigned long now = jiffies;
4952 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
4953 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
4955 nfs4_set_lease_period(server->nfs_client,
4956 fsinfo->lease_time * HZ,
4960 err = nfs4_handle_exception(server, err, &exception);
4961 } while (exception.retry);
4965 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4969 nfs_fattr_init(fsinfo->fattr);
4970 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
4972 /* block layout checks this! */
4973 server->pnfs_blksize = fsinfo->blksize;
4974 set_pnfs_layoutdriver(server, fhandle, fsinfo);
4980 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4981 struct nfs_pathconf *pathconf)
4983 struct nfs4_pathconf_arg args = {
4985 .bitmask = server->attr_bitmask,
4987 struct nfs4_pathconf_res res = {
4988 .pathconf = pathconf,
4990 struct rpc_message msg = {
4991 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4996 /* None of the pathconf attributes are mandatory to implement */
4997 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4998 memset(pathconf, 0, sizeof(*pathconf));
5002 nfs_fattr_init(pathconf->fattr);
5003 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5006 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5007 struct nfs_pathconf *pathconf)
5009 struct nfs4_exception exception = {
5010 .interruptible = true,
5015 err = nfs4_handle_exception(server,
5016 _nfs4_proc_pathconf(server, fhandle, pathconf),
5018 } while (exception.retry);
5022 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
5023 const struct nfs_open_context *ctx,
5024 const struct nfs_lock_context *l_ctx,
5027 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
5029 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
5031 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
5032 const struct nfs_open_context *ctx,
5033 const struct nfs_lock_context *l_ctx,
5036 nfs4_stateid current_stateid;
5038 /* If the current stateid represents a lost lock, then exit */
5039 if (nfs4_set_rw_stateid(¤t_stateid, ctx, l_ctx, fmode) == -EIO)
5041 return nfs4_stateid_match(stateid, ¤t_stateid);
5044 static bool nfs4_error_stateid_expired(int err)
5047 case -NFS4ERR_DELEG_REVOKED:
5048 case -NFS4ERR_ADMIN_REVOKED:
5049 case -NFS4ERR_BAD_STATEID:
5050 case -NFS4ERR_STALE_STATEID:
5051 case -NFS4ERR_OLD_STATEID:
5052 case -NFS4ERR_OPENMODE:
5053 case -NFS4ERR_EXPIRED:
5059 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
5061 struct nfs_server *server = NFS_SERVER(hdr->inode);
5063 trace_nfs4_read(hdr, task->tk_status);
5064 if (task->tk_status < 0) {
5065 struct nfs4_exception exception = {
5066 .inode = hdr->inode,
5067 .state = hdr->args.context->state,
5068 .stateid = &hdr->args.stateid,
5070 task->tk_status = nfs4_async_handle_exception(task,
5071 server, task->tk_status, &exception);
5072 if (exception.retry) {
5073 rpc_restart_call_prepare(task);
5078 if (task->tk_status > 0)
5079 renew_lease(server, hdr->timestamp);
5083 static bool nfs4_read_stateid_changed(struct rpc_task *task,
5084 struct nfs_pgio_args *args)
5087 if (!nfs4_error_stateid_expired(task->tk_status) ||
5088 nfs4_stateid_is_current(&args->stateid,
5093 rpc_restart_call_prepare(task);
5097 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5100 dprintk("--> %s\n", __func__);
5102 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5104 if (nfs4_read_stateid_changed(task, &hdr->args))
5106 if (task->tk_status > 0)
5107 nfs_invalidate_atime(hdr->inode);
5108 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5109 nfs4_read_done_cb(task, hdr);
5112 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5113 struct rpc_message *msg)
5115 hdr->timestamp = jiffies;
5116 if (!hdr->pgio_done_cb)
5117 hdr->pgio_done_cb = nfs4_read_done_cb;
5118 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5119 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5122 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5123 struct nfs_pgio_header *hdr)
5125 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
5126 &hdr->args.seq_args,
5130 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5131 hdr->args.lock_context,
5132 hdr->rw_mode) == -EIO)
5134 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
5139 static int nfs4_write_done_cb(struct rpc_task *task,
5140 struct nfs_pgio_header *hdr)
5142 struct inode *inode = hdr->inode;
5144 trace_nfs4_write(hdr, task->tk_status);
5145 if (task->tk_status < 0) {
5146 struct nfs4_exception exception = {
5147 .inode = hdr->inode,
5148 .state = hdr->args.context->state,
5149 .stateid = &hdr->args.stateid,
5151 task->tk_status = nfs4_async_handle_exception(task,
5152 NFS_SERVER(inode), task->tk_status,
5154 if (exception.retry) {
5155 rpc_restart_call_prepare(task);
5159 if (task->tk_status >= 0) {
5160 renew_lease(NFS_SERVER(inode), hdr->timestamp);
5161 nfs_writeback_update_inode(hdr);
5166 static bool nfs4_write_stateid_changed(struct rpc_task *task,
5167 struct nfs_pgio_args *args)
5170 if (!nfs4_error_stateid_expired(task->tk_status) ||
5171 nfs4_stateid_is_current(&args->stateid,
5176 rpc_restart_call_prepare(task);
5180 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5182 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5184 if (nfs4_write_stateid_changed(task, &hdr->args))
5186 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5187 nfs4_write_done_cb(task, hdr);
5191 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
5193 /* Don't request attributes for pNFS or O_DIRECT writes */
5194 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
5196 /* Otherwise, request attributes if and only if we don't hold
5199 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
5202 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
5203 struct rpc_message *msg,
5204 struct rpc_clnt **clnt)
5206 struct nfs_server *server = NFS_SERVER(hdr->inode);
5208 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5209 hdr->args.bitmask = NULL;
5210 hdr->res.fattr = NULL;
5212 hdr->args.bitmask = server->cache_consistency_bitmask;
5214 if (!hdr->pgio_done_cb)
5215 hdr->pgio_done_cb = nfs4_write_done_cb;
5216 hdr->res.server = server;
5217 hdr->timestamp = jiffies;
5219 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
5220 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1, 0);
5221 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
5224 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5226 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
5227 &data->args.seq_args,
5232 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
5234 struct inode *inode = data->inode;
5236 trace_nfs4_commit(data, task->tk_status);
5237 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5238 NULL, NULL) == -EAGAIN) {
5239 rpc_restart_call_prepare(task);
5245 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
5247 if (!nfs4_sequence_done(task, &data->res.seq_res))
5249 return data->commit_done_cb(task, data);
5252 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5253 struct rpc_clnt **clnt)
5255 struct nfs_server *server = NFS_SERVER(data->inode);
5257 if (data->commit_done_cb == NULL)
5258 data->commit_done_cb = nfs4_commit_done_cb;
5259 data->res.server = server;
5260 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
5261 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
5262 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
5265 static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5266 struct nfs_commitres *res)
5268 struct inode *dst_inode = file_inode(dst);
5269 struct nfs_server *server = NFS_SERVER(dst_inode);
5270 struct rpc_message msg = {
5271 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5276 args->fh = NFS_FH(dst_inode);
5277 return nfs4_call_sync(server->client, server, &msg,
5278 &args->seq_args, &res->seq_res, 1);
5281 int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5283 struct nfs_commitargs args = {
5287 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5288 struct nfs4_exception exception = { };
5292 status = _nfs4_proc_commit(dst, &args, res);
5293 status = nfs4_handle_exception(dst_server, status, &exception);
5294 } while (exception.retry);
5299 struct nfs4_renewdata {
5300 struct nfs_client *client;
5301 unsigned long timestamp;
5305 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5306 * standalone procedure for queueing an asynchronous RENEW.
5308 static void nfs4_renew_release(void *calldata)
5310 struct nfs4_renewdata *data = calldata;
5311 struct nfs_client *clp = data->client;
5313 if (refcount_read(&clp->cl_count) > 1)
5314 nfs4_schedule_state_renewal(clp);
5315 nfs_put_client(clp);
5319 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
5321 struct nfs4_renewdata *data = calldata;
5322 struct nfs_client *clp = data->client;
5323 unsigned long timestamp = data->timestamp;
5325 trace_nfs4_renew_async(clp, task->tk_status);
5326 switch (task->tk_status) {
5329 case -NFS4ERR_LEASE_MOVED:
5330 nfs4_schedule_lease_moved_recovery(clp);
5333 /* Unless we're shutting down, schedule state recovery! */
5334 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5336 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
5337 nfs4_schedule_lease_recovery(clp);
5340 nfs4_schedule_path_down_recovery(clp);
5342 do_renew_lease(clp, timestamp);
5345 static const struct rpc_call_ops nfs4_renew_ops = {
5346 .rpc_call_done = nfs4_renew_done,
5347 .rpc_release = nfs4_renew_release,
5350 static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
5352 struct rpc_message msg = {
5353 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5357 struct nfs4_renewdata *data;
5359 if (renew_flags == 0)
5361 if (!refcount_inc_not_zero(&clp->cl_count))
5363 data = kmalloc(sizeof(*data), GFP_NOFS);
5365 nfs_put_client(clp);
5369 data->timestamp = jiffies;
5370 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
5371 &nfs4_renew_ops, data);
5374 static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
5376 struct rpc_message msg = {
5377 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5381 unsigned long now = jiffies;
5384 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5387 do_renew_lease(clp, now);
5391 static inline int nfs4_server_supports_acls(struct nfs_server *server)
5393 return server->caps & NFS_CAP_ACLS;
5396 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5397 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
5400 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
5402 static int buf_to_pages_noslab(const void *buf, size_t buflen,
5403 struct page **pages)
5405 struct page *newpage, **spages;
5411 len = min_t(size_t, PAGE_SIZE, buflen);
5412 newpage = alloc_page(GFP_KERNEL);
5414 if (newpage == NULL)
5416 memcpy(page_address(newpage), buf, len);
5421 } while (buflen != 0);
5427 __free_page(spages[rc-1]);
5431 struct nfs4_cached_acl {
5437 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
5439 struct nfs_inode *nfsi = NFS_I(inode);
5441 spin_lock(&inode->i_lock);
5442 kfree(nfsi->nfs4_acl);
5443 nfsi->nfs4_acl = acl;
5444 spin_unlock(&inode->i_lock);
5447 static void nfs4_zap_acl_attr(struct inode *inode)
5449 nfs4_set_cached_acl(inode, NULL);
5452 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5454 struct nfs_inode *nfsi = NFS_I(inode);
5455 struct nfs4_cached_acl *acl;
5458 spin_lock(&inode->i_lock);
5459 acl = nfsi->nfs4_acl;
5462 if (buf == NULL) /* user is just asking for length */
5464 if (acl->cached == 0)
5466 ret = -ERANGE; /* see getxattr(2) man page */
5467 if (acl->len > buflen)
5469 memcpy(buf, acl->data, acl->len);
5473 spin_unlock(&inode->i_lock);
5477 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
5479 struct nfs4_cached_acl *acl;
5480 size_t buflen = sizeof(*acl) + acl_len;
5482 if (buflen <= PAGE_SIZE) {
5483 acl = kmalloc(buflen, GFP_KERNEL);
5487 _copy_from_pages(acl->data, pages, pgbase, acl_len);
5489 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5496 nfs4_set_cached_acl(inode, acl);
5500 * The getxattr API returns the required buffer length when called with a
5501 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5502 * the required buf. On a NULL buf, we send a page of data to the server
5503 * guessing that the ACL request can be serviced by a page. If so, we cache
5504 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5505 * the cache. If not so, we throw away the page, and cache the required
5506 * length. The next getxattr call will then produce another round trip to
5507 * the server, this time with the input buf of the required size.
5509 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5511 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
5512 struct nfs_getaclargs args = {
5513 .fh = NFS_FH(inode),
5517 struct nfs_getaclres res = {
5520 struct rpc_message msg = {
5521 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5525 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
5526 int ret = -ENOMEM, i;
5528 if (npages > ARRAY_SIZE(pages))
5531 for (i = 0; i < npages; i++) {
5532 pages[i] = alloc_page(GFP_KERNEL);
5537 /* for decoding across pages */
5538 res.acl_scratch = alloc_page(GFP_KERNEL);
5539 if (!res.acl_scratch)
5542 args.acl_len = npages * PAGE_SIZE;
5544 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
5545 __func__, buf, buflen, npages, args.acl_len);
5546 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5547 &msg, &args.seq_args, &res.seq_res, 0);
5551 /* Handle the case where the passed-in buffer is too short */
5552 if (res.acl_flags & NFS4_ACL_TRUNC) {
5553 /* Did the user only issue a request for the acl length? */
5559 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
5561 if (res.acl_len > buflen) {
5565 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
5570 for (i = 0; i < npages; i++)
5572 __free_page(pages[i]);
5573 if (res.acl_scratch)
5574 __free_page(res.acl_scratch);
5578 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5580 struct nfs4_exception exception = {
5581 .interruptible = true,
5585 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
5586 trace_nfs4_get_acl(inode, ret);
5589 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5590 } while (exception.retry);
5594 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5596 struct nfs_server *server = NFS_SERVER(inode);
5599 if (!nfs4_server_supports_acls(server))
5601 ret = nfs_revalidate_inode(server, inode);
5604 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5605 nfs_zap_acl_cache(inode);
5606 ret = nfs4_read_cached_acl(inode, buf, buflen);
5608 /* -ENOENT is returned if there is no ACL or if there is an ACL
5609 * but no cached acl data, just the acl length */
5611 return nfs4_get_acl_uncached(inode, buf, buflen);
5614 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5616 struct nfs_server *server = NFS_SERVER(inode);
5617 struct page *pages[NFS4ACL_MAXPAGES];
5618 struct nfs_setaclargs arg = {
5619 .fh = NFS_FH(inode),
5623 struct nfs_setaclres res;
5624 struct rpc_message msg = {
5625 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5629 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
5632 if (!nfs4_server_supports_acls(server))
5634 if (npages > ARRAY_SIZE(pages))
5636 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
5639 nfs4_inode_make_writeable(inode);
5640 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5643 * Free each page after tx, so the only ref left is
5644 * held by the network stack
5647 put_page(pages[i-1]);
5650 * Acl update can result in inode attribute update.
5651 * so mark the attribute cache invalid.
5653 spin_lock(&inode->i_lock);
5654 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
5655 | NFS_INO_INVALID_CTIME
5656 | NFS_INO_REVAL_FORCED;
5657 spin_unlock(&inode->i_lock);
5658 nfs_access_zap_cache(inode);
5659 nfs_zap_acl_cache(inode);
5663 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5665 struct nfs4_exception exception = { };
5668 err = __nfs4_proc_set_acl(inode, buf, buflen);
5669 trace_nfs4_set_acl(inode, err);
5670 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5672 } while (exception.retry);
5676 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
5677 static int _nfs4_get_security_label(struct inode *inode, void *buf,
5680 struct nfs_server *server = NFS_SERVER(inode);
5681 struct nfs_fattr fattr;
5682 struct nfs4_label label = {0, 0, buflen, buf};
5684 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5685 struct nfs4_getattr_arg arg = {
5686 .fh = NFS_FH(inode),
5689 struct nfs4_getattr_res res = {
5694 struct rpc_message msg = {
5695 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
5701 nfs_fattr_init(&fattr);
5703 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
5706 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5708 if (buflen < label.len)
5713 static int nfs4_get_security_label(struct inode *inode, void *buf,
5716 struct nfs4_exception exception = {
5717 .interruptible = true,
5721 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5725 err = _nfs4_get_security_label(inode, buf, buflen);
5726 trace_nfs4_get_security_label(inode, err);
5727 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5729 } while (exception.retry);
5733 static int _nfs4_do_set_security_label(struct inode *inode,
5734 struct nfs4_label *ilabel,
5735 struct nfs_fattr *fattr,
5736 struct nfs4_label *olabel)
5739 struct iattr sattr = {0};
5740 struct nfs_server *server = NFS_SERVER(inode);
5741 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5742 struct nfs_setattrargs arg = {
5743 .fh = NFS_FH(inode),
5749 struct nfs_setattrres res = {
5754 struct rpc_message msg = {
5755 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5761 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
5763 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5765 dprintk("%s failed: %d\n", __func__, status);
5770 static int nfs4_do_set_security_label(struct inode *inode,
5771 struct nfs4_label *ilabel,
5772 struct nfs_fattr *fattr,
5773 struct nfs4_label *olabel)
5775 struct nfs4_exception exception = { };
5779 err = _nfs4_do_set_security_label(inode, ilabel,
5781 trace_nfs4_set_security_label(inode, err);
5782 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5784 } while (exception.retry);
5789 nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
5791 struct nfs4_label ilabel, *olabel = NULL;
5792 struct nfs_fattr fattr;
5795 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5798 nfs_fattr_init(&fattr);
5802 ilabel.label = (char *)buf;
5803 ilabel.len = buflen;
5805 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5806 if (IS_ERR(olabel)) {
5807 status = -PTR_ERR(olabel);
5811 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5813 nfs_setsecurity(inode, &fattr, olabel);
5815 nfs4_label_free(olabel);
5819 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5822 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5823 nfs4_verifier *bootverf)
5827 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5828 /* An impossible timestamp guarantees this value
5829 * will never match a generated boot time. */
5830 verf[0] = cpu_to_be32(U32_MAX);
5831 verf[1] = cpu_to_be32(U32_MAX);
5833 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
5834 u64 ns = ktime_to_ns(nn->boot_time);
5836 verf[0] = cpu_to_be32(ns >> 32);
5837 verf[1] = cpu_to_be32(ns);
5839 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5843 nfs4_init_nonuniform_client_string(struct nfs_client *clp)
5848 if (clp->cl_owner_id != NULL)
5853 strlen(clp->cl_rpcclient->cl_nodename) +
5855 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5858 if (nfs4_client_id_uniquifier[0] != '\0')
5859 len += strlen(nfs4_client_id_uniquifier) + 1;
5860 if (len > NFS4_OPAQUE_LIMIT + 1)
5864 * Since this string is allocated at mount time, and held until the
5865 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5866 * about a memory-reclaim deadlock.
5868 str = kmalloc(len, GFP_KERNEL);
5873 if (nfs4_client_id_uniquifier[0] != '\0')
5874 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
5875 clp->cl_rpcclient->cl_nodename,
5876 nfs4_client_id_uniquifier,
5877 rpc_peeraddr2str(clp->cl_rpcclient,
5880 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
5881 clp->cl_rpcclient->cl_nodename,
5882 rpc_peeraddr2str(clp->cl_rpcclient,
5886 clp->cl_owner_id = str;
5891 nfs4_init_uniquifier_client_string(struct nfs_client *clp)
5896 len = 10 + 10 + 1 + 10 + 1 +
5897 strlen(nfs4_client_id_uniquifier) + 1 +
5898 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5900 if (len > NFS4_OPAQUE_LIMIT + 1)
5904 * Since this string is allocated at mount time, and held until the
5905 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5906 * about a memory-reclaim deadlock.
5908 str = kmalloc(len, GFP_KERNEL);
5912 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
5913 clp->rpc_ops->version, clp->cl_minorversion,
5914 nfs4_client_id_uniquifier,
5915 clp->cl_rpcclient->cl_nodename);
5916 clp->cl_owner_id = str;
5921 nfs4_init_uniform_client_string(struct nfs_client *clp)
5926 if (clp->cl_owner_id != NULL)
5929 if (nfs4_client_id_uniquifier[0] != '\0')
5930 return nfs4_init_uniquifier_client_string(clp);
5932 len = 10 + 10 + 1 + 10 + 1 +
5933 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5935 if (len > NFS4_OPAQUE_LIMIT + 1)
5939 * Since this string is allocated at mount time, and held until the
5940 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5941 * about a memory-reclaim deadlock.
5943 str = kmalloc(len, GFP_KERNEL);
5947 scnprintf(str, len, "Linux NFSv%u.%u %s",
5948 clp->rpc_ops->version, clp->cl_minorversion,
5949 clp->cl_rpcclient->cl_nodename);
5950 clp->cl_owner_id = str;
5955 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5956 * services. Advertise one based on the address family of the
5960 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5962 if (strchr(clp->cl_ipaddr, ':') != NULL)
5963 return scnprintf(buf, len, "tcp6");
5965 return scnprintf(buf, len, "tcp");
5968 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5970 struct nfs4_setclientid *sc = calldata;
5972 if (task->tk_status == 0)
5973 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5976 static const struct rpc_call_ops nfs4_setclientid_ops = {
5977 .rpc_call_done = nfs4_setclientid_done,
5981 * nfs4_proc_setclientid - Negotiate client ID
5982 * @clp: state data structure
5983 * @program: RPC program for NFSv4 callback service
5984 * @port: IP port number for NFS4 callback service
5985 * @cred: credential to use for this call
5986 * @res: where to place the result
5988 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5990 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5991 unsigned short port, const struct cred *cred,
5992 struct nfs4_setclientid_res *res)
5994 nfs4_verifier sc_verifier;
5995 struct nfs4_setclientid setclientid = {
5996 .sc_verifier = &sc_verifier,
6000 struct rpc_message msg = {
6001 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
6002 .rpc_argp = &setclientid,
6006 struct rpc_task *task;
6007 struct rpc_task_setup task_setup_data = {
6008 .rpc_client = clp->cl_rpcclient,
6009 .rpc_message = &msg,
6010 .callback_ops = &nfs4_setclientid_ops,
6011 .callback_data = &setclientid,
6012 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
6016 /* nfs_client_id4 */
6017 nfs4_init_boot_verifier(clp, &sc_verifier);
6019 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
6020 status = nfs4_init_uniform_client_string(clp);
6022 status = nfs4_init_nonuniform_client_string(clp);
6028 setclientid.sc_netid_len =
6029 nfs4_init_callback_netid(clp,
6030 setclientid.sc_netid,
6031 sizeof(setclientid.sc_netid));
6032 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
6033 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
6034 clp->cl_ipaddr, port >> 8, port & 255);
6036 dprintk("NFS call setclientid auth=%s, '%s'\n",
6037 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6039 task = rpc_run_task(&task_setup_data);
6041 status = PTR_ERR(task);
6044 status = task->tk_status;
6045 if (setclientid.sc_cred) {
6046 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
6047 put_rpccred(setclientid.sc_cred);
6051 trace_nfs4_setclientid(clp, status);
6052 dprintk("NFS reply setclientid: %d\n", status);
6057 * nfs4_proc_setclientid_confirm - Confirm client ID
6058 * @clp: state data structure
6059 * @arg: result of a previous SETCLIENTID
6060 * @cred: credential to use for this call
6062 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6064 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
6065 struct nfs4_setclientid_res *arg,
6066 const struct cred *cred)
6068 struct rpc_message msg = {
6069 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
6075 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
6076 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6078 status = rpc_call_sync(clp->cl_rpcclient, &msg,
6079 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
6080 trace_nfs4_setclientid_confirm(clp, status);
6081 dprintk("NFS reply setclientid_confirm: %d\n", status);
6085 struct nfs4_delegreturndata {
6086 struct nfs4_delegreturnargs args;
6087 struct nfs4_delegreturnres res;
6089 nfs4_stateid stateid;
6090 unsigned long timestamp;
6092 struct nfs4_layoutreturn_args arg;
6093 struct nfs4_layoutreturn_res res;
6094 struct nfs4_xdr_opaque_data ld_private;
6098 struct nfs_fattr fattr;
6100 struct inode *inode;
6103 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6105 struct nfs4_delegreturndata *data = calldata;
6106 struct nfs4_exception exception = {
6107 .inode = data->inode,
6108 .stateid = &data->stateid,
6111 if (!nfs4_sequence_done(task, &data->res.seq_res))
6114 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
6116 /* Handle Layoutreturn errors */
6117 if (data->args.lr_args && task->tk_status != 0) {
6118 switch(data->res.lr_ret) {
6120 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
6123 data->args.lr_args = NULL;
6124 data->res.lr_res = NULL;
6126 case -NFS4ERR_OLD_STATEID:
6127 if (nfs4_layoutreturn_refresh_stateid(&data->args.lr_args->stateid,
6128 &data->args.lr_args->range,
6132 case -NFS4ERR_ADMIN_REVOKED:
6133 case -NFS4ERR_DELEG_REVOKED:
6134 case -NFS4ERR_EXPIRED:
6135 case -NFS4ERR_BAD_STATEID:
6136 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
6137 case -NFS4ERR_WRONG_CRED:
6138 data->args.lr_args = NULL;
6139 data->res.lr_res = NULL;
6144 switch (task->tk_status) {
6146 renew_lease(data->res.server, data->timestamp);
6148 case -NFS4ERR_ADMIN_REVOKED:
6149 case -NFS4ERR_DELEG_REVOKED:
6150 case -NFS4ERR_EXPIRED:
6151 nfs4_free_revoked_stateid(data->res.server,
6153 task->tk_msg.rpc_cred);
6155 case -NFS4ERR_BAD_STATEID:
6156 case -NFS4ERR_STALE_STATEID:
6157 task->tk_status = 0;
6159 case -NFS4ERR_OLD_STATEID:
6160 if (nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6162 task->tk_status = 0;
6164 case -NFS4ERR_ACCESS:
6165 if (data->args.bitmask) {
6166 data->args.bitmask = NULL;
6167 data->res.fattr = NULL;
6172 task->tk_status = nfs4_async_handle_exception(task,
6173 data->res.server, task->tk_status,
6175 if (exception.retry)
6178 data->rpc_status = task->tk_status;
6181 data->res.lr_ret = 0;
6183 task->tk_status = 0;
6184 rpc_restart_call_prepare(task);
6187 static void nfs4_delegreturn_release(void *calldata)
6189 struct nfs4_delegreturndata *data = calldata;
6190 struct inode *inode = data->inode;
6194 pnfs_roc_release(&data->lr.arg, &data->lr.res,
6196 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
6197 nfs_iput_and_deactive(inode);
6202 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6204 struct nfs4_delegreturndata *d_data;
6205 struct pnfs_layout_hdr *lo;
6207 d_data = (struct nfs4_delegreturndata *)data;
6209 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
6212 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6213 if (lo && !pnfs_layout_is_valid(lo)) {
6214 d_data->args.lr_args = NULL;
6215 d_data->res.lr_res = NULL;
6218 nfs4_setup_sequence(d_data->res.server->nfs_client,
6219 &d_data->args.seq_args,
6220 &d_data->res.seq_res,
6224 static const struct rpc_call_ops nfs4_delegreturn_ops = {
6225 .rpc_call_prepare = nfs4_delegreturn_prepare,
6226 .rpc_call_done = nfs4_delegreturn_done,
6227 .rpc_release = nfs4_delegreturn_release,
6230 static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6232 struct nfs4_delegreturndata *data;
6233 struct nfs_server *server = NFS_SERVER(inode);
6234 struct rpc_task *task;
6235 struct rpc_message msg = {
6236 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6239 struct rpc_task_setup task_setup_data = {
6240 .rpc_client = server->client,
6241 .rpc_message = &msg,
6242 .callback_ops = &nfs4_delegreturn_ops,
6243 .flags = RPC_TASK_ASYNC,
6247 data = kzalloc(sizeof(*data), GFP_NOFS);
6250 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
6252 nfs4_state_protect(server->nfs_client,
6253 NFS_SP4_MACH_CRED_CLEANUP,
6254 &task_setup_data.rpc_client, &msg);
6256 data->args.fhandle = &data->fh;
6257 data->args.stateid = &data->stateid;
6258 data->args.bitmask = server->cache_consistency_bitmask;
6259 nfs_copy_fh(&data->fh, NFS_FH(inode));
6260 nfs4_stateid_copy(&data->stateid, stateid);
6261 data->res.fattr = &data->fattr;
6262 data->res.server = server;
6263 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
6264 data->lr.arg.ld_private = &data->lr.ld_private;
6265 nfs_fattr_init(data->res.fattr);
6266 data->timestamp = jiffies;
6267 data->rpc_status = 0;
6268 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
6269 data->inode = nfs_igrab_and_active(inode);
6272 data->args.lr_args = &data->lr.arg;
6273 data->res.lr_res = &data->lr.res;
6275 } else if (data->lr.roc) {
6276 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
6277 data->lr.roc = false;
6280 task_setup_data.callback_data = data;
6281 msg.rpc_argp = &data->args;
6282 msg.rpc_resp = &data->res;
6283 task = rpc_run_task(&task_setup_data);
6285 return PTR_ERR(task);
6288 status = rpc_wait_for_completion_task(task);
6291 status = data->rpc_status;
6297 int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6299 struct nfs_server *server = NFS_SERVER(inode);
6300 struct nfs4_exception exception = { };
6303 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
6304 trace_nfs4_delegreturn(inode, stateid, err);
6306 case -NFS4ERR_STALE_STATEID:
6307 case -NFS4ERR_EXPIRED:
6311 err = nfs4_handle_exception(server, err, &exception);
6312 } while (exception.retry);
6316 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6318 struct inode *inode = state->inode;
6319 struct nfs_server *server = NFS_SERVER(inode);
6320 struct nfs_client *clp = server->nfs_client;
6321 struct nfs_lockt_args arg = {
6322 .fh = NFS_FH(inode),
6325 struct nfs_lockt_res res = {
6328 struct rpc_message msg = {
6329 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
6332 .rpc_cred = state->owner->so_cred,
6334 struct nfs4_lock_state *lsp;
6337 arg.lock_owner.clientid = clp->cl_clientid;
6338 status = nfs4_set_lock_state(state, request);
6341 lsp = request->fl_u.nfs4_fl.owner;
6342 arg.lock_owner.id = lsp->ls_seqid.owner_id;
6343 arg.lock_owner.s_dev = server->s_dev;
6344 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6347 request->fl_type = F_UNLCK;
6349 case -NFS4ERR_DENIED:
6352 request->fl_ops->fl_release_private(request);
6353 request->fl_ops = NULL;
6358 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6360 struct nfs4_exception exception = {
6361 .interruptible = true,
6366 err = _nfs4_proc_getlk(state, cmd, request);
6367 trace_nfs4_get_lock(request, state, cmd, err);
6368 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
6370 } while (exception.retry);
6374 struct nfs4_unlockdata {
6375 struct nfs_locku_args arg;
6376 struct nfs_locku_res res;
6377 struct nfs4_lock_state *lsp;
6378 struct nfs_open_context *ctx;
6379 struct nfs_lock_context *l_ctx;
6380 struct file_lock fl;
6381 struct nfs_server *server;
6382 unsigned long timestamp;
6385 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6386 struct nfs_open_context *ctx,
6387 struct nfs4_lock_state *lsp,
6388 struct nfs_seqid *seqid)
6390 struct nfs4_unlockdata *p;
6391 struct inode *inode = lsp->ls_state->inode;
6393 p = kzalloc(sizeof(*p), GFP_NOFS);
6396 p->arg.fh = NFS_FH(inode);
6398 p->arg.seqid = seqid;
6399 p->res.seqid = seqid;
6401 /* Ensure we don't close file until we're done freeing locks! */
6402 p->ctx = get_nfs_open_context(ctx);
6403 p->l_ctx = nfs_get_lock_context(ctx);
6404 locks_init_lock(&p->fl);
6405 locks_copy_lock(&p->fl, fl);
6406 p->server = NFS_SERVER(inode);
6410 static void nfs4_locku_release_calldata(void *data)
6412 struct nfs4_unlockdata *calldata = data;
6413 nfs_free_seqid(calldata->arg.seqid);
6414 nfs4_put_lock_state(calldata->lsp);
6415 nfs_put_lock_context(calldata->l_ctx);
6416 put_nfs_open_context(calldata->ctx);
6420 static void nfs4_locku_done(struct rpc_task *task, void *data)
6422 struct nfs4_unlockdata *calldata = data;
6423 struct nfs4_exception exception = {
6424 .inode = calldata->lsp->ls_state->inode,
6425 .stateid = &calldata->arg.stateid,
6428 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6430 switch (task->tk_status) {
6432 renew_lease(calldata->server, calldata->timestamp);
6433 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
6434 if (nfs4_update_lock_stateid(calldata->lsp,
6435 &calldata->res.stateid))
6438 case -NFS4ERR_ADMIN_REVOKED:
6439 case -NFS4ERR_EXPIRED:
6440 nfs4_free_revoked_stateid(calldata->server,
6441 &calldata->arg.stateid,
6442 task->tk_msg.rpc_cred);
6444 case -NFS4ERR_BAD_STATEID:
6445 case -NFS4ERR_OLD_STATEID:
6446 case -NFS4ERR_STALE_STATEID:
6447 if (!nfs4_stateid_match(&calldata->arg.stateid,
6448 &calldata->lsp->ls_stateid))
6449 rpc_restart_call_prepare(task);
6452 task->tk_status = nfs4_async_handle_exception(task,
6453 calldata->server, task->tk_status,
6455 if (exception.retry)
6456 rpc_restart_call_prepare(task);
6458 nfs_release_seqid(calldata->arg.seqid);
6461 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
6463 struct nfs4_unlockdata *calldata = data;
6465 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6466 nfs_async_iocounter_wait(task, calldata->l_ctx))
6469 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
6471 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
6472 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
6473 /* Note: exit _without_ running nfs4_locku_done */
6476 calldata->timestamp = jiffies;
6477 if (nfs4_setup_sequence(calldata->server->nfs_client,
6478 &calldata->arg.seq_args,
6479 &calldata->res.seq_res,
6481 nfs_release_seqid(calldata->arg.seqid);
6484 task->tk_action = NULL;
6486 nfs4_sequence_done(task, &calldata->res.seq_res);
6489 static const struct rpc_call_ops nfs4_locku_ops = {
6490 .rpc_call_prepare = nfs4_locku_prepare,
6491 .rpc_call_done = nfs4_locku_done,
6492 .rpc_release = nfs4_locku_release_calldata,
6495 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6496 struct nfs_open_context *ctx,
6497 struct nfs4_lock_state *lsp,
6498 struct nfs_seqid *seqid)
6500 struct nfs4_unlockdata *data;
6501 struct rpc_message msg = {
6502 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6503 .rpc_cred = ctx->cred,
6505 struct rpc_task_setup task_setup_data = {
6506 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
6507 .rpc_message = &msg,
6508 .callback_ops = &nfs4_locku_ops,
6509 .workqueue = nfsiod_workqueue,
6510 .flags = RPC_TASK_ASYNC,
6513 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6514 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6516 /* Ensure this is an unlock - when canceling a lock, the
6517 * canceled lock is passed in, and it won't be an unlock.
6519 fl->fl_type = F_UNLCK;
6520 if (fl->fl_flags & FL_CLOSE)
6521 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
6523 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6525 nfs_free_seqid(seqid);
6526 return ERR_PTR(-ENOMEM);
6529 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
6530 msg.rpc_argp = &data->arg;
6531 msg.rpc_resp = &data->res;
6532 task_setup_data.callback_data = data;
6533 return rpc_run_task(&task_setup_data);
6536 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6538 struct inode *inode = state->inode;
6539 struct nfs4_state_owner *sp = state->owner;
6540 struct nfs_inode *nfsi = NFS_I(inode);
6541 struct nfs_seqid *seqid;
6542 struct nfs4_lock_state *lsp;
6543 struct rpc_task *task;
6544 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6546 unsigned char fl_flags = request->fl_flags;
6548 status = nfs4_set_lock_state(state, request);
6549 /* Unlock _before_ we do the RPC call */
6550 request->fl_flags |= FL_EXISTS;
6551 /* Exclude nfs_delegation_claim_locks() */
6552 mutex_lock(&sp->so_delegreturn_mutex);
6553 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
6554 down_read(&nfsi->rwsem);
6555 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
6556 up_read(&nfsi->rwsem);
6557 mutex_unlock(&sp->so_delegreturn_mutex);
6560 up_read(&nfsi->rwsem);
6561 mutex_unlock(&sp->so_delegreturn_mutex);
6564 /* Is this a delegated lock? */
6565 lsp = request->fl_u.nfs4_fl.owner;
6566 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6568 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6569 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
6573 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
6574 status = PTR_ERR(task);
6577 status = rpc_wait_for_completion_task(task);
6580 request->fl_flags = fl_flags;
6581 trace_nfs4_unlock(request, state, F_SETLK, status);
6585 struct nfs4_lockdata {
6586 struct nfs_lock_args arg;
6587 struct nfs_lock_res res;
6588 struct nfs4_lock_state *lsp;
6589 struct nfs_open_context *ctx;
6590 struct file_lock fl;
6591 unsigned long timestamp;
6594 struct nfs_server *server;
6597 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
6598 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6601 struct nfs4_lockdata *p;
6602 struct inode *inode = lsp->ls_state->inode;
6603 struct nfs_server *server = NFS_SERVER(inode);
6604 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6606 p = kzalloc(sizeof(*p), gfp_mask);
6610 p->arg.fh = NFS_FH(inode);
6612 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
6613 if (IS_ERR(p->arg.open_seqid))
6615 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6616 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
6617 if (IS_ERR(p->arg.lock_seqid))
6618 goto out_free_seqid;
6619 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
6620 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
6621 p->arg.lock_owner.s_dev = server->s_dev;
6622 p->res.lock_seqid = p->arg.lock_seqid;
6625 p->ctx = get_nfs_open_context(ctx);
6626 locks_init_lock(&p->fl);
6627 locks_copy_lock(&p->fl, fl);
6630 nfs_free_seqid(p->arg.open_seqid);
6636 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6638 struct nfs4_lockdata *data = calldata;
6639 struct nfs4_state *state = data->lsp->ls_state;
6641 dprintk("%s: begin!\n", __func__);
6642 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
6644 /* Do we need to do an open_to_lock_owner? */
6645 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
6646 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
6647 goto out_release_lock_seqid;
6649 nfs4_stateid_copy(&data->arg.open_stateid,
6650 &state->open_stateid);
6651 data->arg.new_lock_owner = 1;
6652 data->res.open_seqid = data->arg.open_seqid;
6654 data->arg.new_lock_owner = 0;
6655 nfs4_stateid_copy(&data->arg.lock_stateid,
6656 &data->lsp->ls_stateid);
6658 if (!nfs4_valid_open_stateid(state)) {
6659 data->rpc_status = -EBADF;
6660 task->tk_action = NULL;
6661 goto out_release_open_seqid;
6663 data->timestamp = jiffies;
6664 if (nfs4_setup_sequence(data->server->nfs_client,
6665 &data->arg.seq_args,
6669 out_release_open_seqid:
6670 nfs_release_seqid(data->arg.open_seqid);
6671 out_release_lock_seqid:
6672 nfs_release_seqid(data->arg.lock_seqid);
6674 nfs4_sequence_done(task, &data->res.seq_res);
6675 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
6678 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6680 struct nfs4_lockdata *data = calldata;
6681 struct nfs4_lock_state *lsp = data->lsp;
6683 dprintk("%s: begin!\n", __func__);
6685 if (!nfs4_sequence_done(task, &data->res.seq_res))
6688 data->rpc_status = task->tk_status;
6689 switch (task->tk_status) {
6691 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
6693 if (data->arg.new_lock && !data->cancelled) {
6694 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
6695 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
6698 if (data->arg.new_lock_owner != 0) {
6699 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6700 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6701 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6702 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6705 case -NFS4ERR_BAD_STATEID:
6706 case -NFS4ERR_OLD_STATEID:
6707 case -NFS4ERR_STALE_STATEID:
6708 case -NFS4ERR_EXPIRED:
6709 if (data->arg.new_lock_owner != 0) {
6710 if (!nfs4_stateid_match(&data->arg.open_stateid,
6711 &lsp->ls_state->open_stateid))
6713 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6718 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
6721 if (!data->cancelled)
6722 rpc_restart_call_prepare(task);
6726 static void nfs4_lock_release(void *calldata)
6728 struct nfs4_lockdata *data = calldata;
6730 dprintk("%s: begin!\n", __func__);
6731 nfs_free_seqid(data->arg.open_seqid);
6732 if (data->cancelled && data->rpc_status == 0) {
6733 struct rpc_task *task;
6734 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6735 data->arg.lock_seqid);
6737 rpc_put_task_async(task);
6738 dprintk("%s: cancelling lock!\n", __func__);
6740 nfs_free_seqid(data->arg.lock_seqid);
6741 nfs4_put_lock_state(data->lsp);
6742 put_nfs_open_context(data->ctx);
6744 dprintk("%s: done!\n", __func__);
6747 static const struct rpc_call_ops nfs4_lock_ops = {
6748 .rpc_call_prepare = nfs4_lock_prepare,
6749 .rpc_call_done = nfs4_lock_done,
6750 .rpc_release = nfs4_lock_release,
6753 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6756 case -NFS4ERR_ADMIN_REVOKED:
6757 case -NFS4ERR_EXPIRED:
6758 case -NFS4ERR_BAD_STATEID:
6759 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6760 if (new_lock_owner != 0 ||
6761 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
6762 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
6764 case -NFS4ERR_STALE_STATEID:
6765 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6766 nfs4_schedule_lease_recovery(server->nfs_client);
6770 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
6772 struct nfs4_lockdata *data;
6773 struct rpc_task *task;
6774 struct rpc_message msg = {
6775 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6776 .rpc_cred = state->owner->so_cred,
6778 struct rpc_task_setup task_setup_data = {
6779 .rpc_client = NFS_CLIENT(state->inode),
6780 .rpc_message = &msg,
6781 .callback_ops = &nfs4_lock_ops,
6782 .workqueue = nfsiod_workqueue,
6783 .flags = RPC_TASK_ASYNC,
6787 dprintk("%s: begin!\n", __func__);
6788 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
6789 fl->fl_u.nfs4_fl.owner,
6790 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
6794 data->arg.block = 1;
6795 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
6796 recovery_type > NFS_LOCK_NEW);
6797 msg.rpc_argp = &data->arg;
6798 msg.rpc_resp = &data->res;
6799 task_setup_data.callback_data = data;
6800 if (recovery_type > NFS_LOCK_NEW) {
6801 if (recovery_type == NFS_LOCK_RECLAIM)
6802 data->arg.reclaim = NFS_LOCK_RECLAIM;
6804 data->arg.new_lock = 1;
6805 task = rpc_run_task(&task_setup_data);
6807 return PTR_ERR(task);
6808 ret = rpc_wait_for_completion_task(task);
6810 ret = data->rpc_status;
6812 nfs4_handle_setlk_error(data->server, data->lsp,
6813 data->arg.new_lock_owner, ret);
6815 data->cancelled = true;
6817 dprintk("%s: done, ret = %d!\n", __func__, ret);
6818 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
6822 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6824 struct nfs_server *server = NFS_SERVER(state->inode);
6825 struct nfs4_exception exception = {
6826 .inode = state->inode,
6831 /* Cache the lock if possible... */
6832 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6834 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
6835 if (err != -NFS4ERR_DELAY)
6837 nfs4_handle_exception(server, err, &exception);
6838 } while (exception.retry);
6842 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6844 struct nfs_server *server = NFS_SERVER(state->inode);
6845 struct nfs4_exception exception = {
6846 .inode = state->inode,
6850 err = nfs4_set_lock_state(state, request);
6853 if (!recover_lost_locks) {
6854 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6858 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6860 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
6864 case -NFS4ERR_GRACE:
6865 case -NFS4ERR_DELAY:
6866 nfs4_handle_exception(server, err, &exception);
6869 } while (exception.retry);
6874 #if defined(CONFIG_NFS_V4_1)
6875 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6877 struct nfs4_lock_state *lsp;
6880 status = nfs4_set_lock_state(state, request);
6883 lsp = request->fl_u.nfs4_fl.owner;
6884 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6885 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6887 return nfs4_lock_expired(state, request);
6891 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6893 struct nfs_inode *nfsi = NFS_I(state->inode);
6894 struct nfs4_state_owner *sp = state->owner;
6895 unsigned char fl_flags = request->fl_flags;
6898 request->fl_flags |= FL_ACCESS;
6899 status = locks_lock_inode_wait(state->inode, request);
6902 mutex_lock(&sp->so_delegreturn_mutex);
6903 down_read(&nfsi->rwsem);
6904 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
6905 /* Yes: cache locks! */
6906 /* ...but avoid races with delegation recall... */
6907 request->fl_flags = fl_flags & ~FL_SLEEP;
6908 status = locks_lock_inode_wait(state->inode, request);
6909 up_read(&nfsi->rwsem);
6910 mutex_unlock(&sp->so_delegreturn_mutex);
6913 up_read(&nfsi->rwsem);
6914 mutex_unlock(&sp->so_delegreturn_mutex);
6915 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
6917 request->fl_flags = fl_flags;
6921 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6923 struct nfs4_exception exception = {
6925 .inode = state->inode,
6926 .interruptible = true,
6931 err = _nfs4_proc_setlk(state, cmd, request);
6932 if (err == -NFS4ERR_DENIED)
6934 err = nfs4_handle_exception(NFS_SERVER(state->inode),
6936 } while (exception.retry);
6940 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6941 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6944 nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6945 struct file_lock *request)
6947 int status = -ERESTARTSYS;
6948 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6950 while(!signalled()) {
6951 status = nfs4_proc_setlk(state, cmd, request);
6952 if ((status != -EAGAIN) || IS_SETLK(cmd))
6954 freezable_schedule_timeout_interruptible(timeout);
6956 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6957 status = -ERESTARTSYS;
6962 #ifdef CONFIG_NFS_V4_1
6963 struct nfs4_lock_waiter {
6964 struct task_struct *task;
6965 struct inode *inode;
6966 struct nfs_lowner *owner;
6970 nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
6973 struct nfs4_lock_waiter *waiter = wait->private;
6975 /* NULL key means to wake up everyone */
6977 struct cb_notify_lock_args *cbnl = key;
6978 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6979 *wowner = waiter->owner;
6981 /* Only wake if the callback was for the same owner. */
6982 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
6985 /* Make sure it's for the right inode */
6986 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6990 /* override "private" so we can use default_wake_function */
6991 wait->private = waiter->task;
6992 ret = woken_wake_function(wait, mode, flags, key);
6994 list_del_init(&wait->entry);
6995 wait->private = waiter;
7000 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7002 int status = -ERESTARTSYS;
7003 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
7004 struct nfs_server *server = NFS_SERVER(state->inode);
7005 struct nfs_client *clp = server->nfs_client;
7006 wait_queue_head_t *q = &clp->cl_lock_waitq;
7007 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
7008 .id = lsp->ls_seqid.owner_id,
7009 .s_dev = server->s_dev };
7010 struct nfs4_lock_waiter waiter = { .task = current,
7011 .inode = state->inode,
7013 wait_queue_entry_t wait;
7015 /* Don't bother with waitqueue if we don't expect a callback */
7016 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
7017 return nfs4_retry_setlk_simple(state, cmd, request);
7020 wait.private = &waiter;
7021 wait.func = nfs4_wake_lock_waiter;
7023 while(!signalled()) {
7024 add_wait_queue(q, &wait);
7025 status = nfs4_proc_setlk(state, cmd, request);
7026 if ((status != -EAGAIN) || IS_SETLK(cmd)) {
7027 finish_wait(q, &wait);
7031 status = -ERESTARTSYS;
7032 freezer_do_not_count();
7033 wait_woken(&wait, TASK_INTERRUPTIBLE, NFS4_LOCK_MAXTIMEOUT);
7035 finish_wait(q, &wait);
7040 #else /* !CONFIG_NFS_V4_1 */
7042 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7044 return nfs4_retry_setlk_simple(state, cmd, request);
7049 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
7051 struct nfs_open_context *ctx;
7052 struct nfs4_state *state;
7055 /* verify open state */
7056 ctx = nfs_file_open_context(filp);
7059 if (IS_GETLK(cmd)) {
7061 return nfs4_proc_getlk(state, F_GETLK, request);
7065 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
7068 if (request->fl_type == F_UNLCK) {
7070 return nfs4_proc_unlck(state, cmd, request);
7077 if ((request->fl_flags & FL_POSIX) &&
7078 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
7082 * Don't rely on the VFS having checked the file open mode,
7083 * since it won't do this for flock() locks.
7085 switch (request->fl_type) {
7087 if (!(filp->f_mode & FMODE_READ))
7091 if (!(filp->f_mode & FMODE_WRITE))
7095 status = nfs4_set_lock_state(state, request);
7099 return nfs4_retry_setlk(state, cmd, request);
7102 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
7104 struct nfs_server *server = NFS_SERVER(state->inode);
7107 err = nfs4_set_lock_state(state, fl);
7110 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
7111 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
7114 struct nfs_release_lockowner_data {
7115 struct nfs4_lock_state *lsp;
7116 struct nfs_server *server;
7117 struct nfs_release_lockowner_args args;
7118 struct nfs_release_lockowner_res res;
7119 unsigned long timestamp;
7122 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7124 struct nfs_release_lockowner_data *data = calldata;
7125 struct nfs_server *server = data->server;
7126 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7127 &data->res.seq_res, task);
7128 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7129 data->timestamp = jiffies;
7132 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7134 struct nfs_release_lockowner_data *data = calldata;
7135 struct nfs_server *server = data->server;
7137 nfs40_sequence_done(task, &data->res.seq_res);
7139 switch (task->tk_status) {
7141 renew_lease(server, data->timestamp);
7143 case -NFS4ERR_STALE_CLIENTID:
7144 case -NFS4ERR_EXPIRED:
7145 nfs4_schedule_lease_recovery(server->nfs_client);
7147 case -NFS4ERR_LEASE_MOVED:
7148 case -NFS4ERR_DELAY:
7149 if (nfs4_async_handle_error(task, server,
7150 NULL, NULL) == -EAGAIN)
7151 rpc_restart_call_prepare(task);
7155 static void nfs4_release_lockowner_release(void *calldata)
7157 struct nfs_release_lockowner_data *data = calldata;
7158 nfs4_free_lock_state(data->server, data->lsp);
7162 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
7163 .rpc_call_prepare = nfs4_release_lockowner_prepare,
7164 .rpc_call_done = nfs4_release_lockowner_done,
7165 .rpc_release = nfs4_release_lockowner_release,
7169 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
7171 struct nfs_release_lockowner_data *data;
7172 struct rpc_message msg = {
7173 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7176 if (server->nfs_client->cl_mvops->minor_version != 0)
7179 data = kmalloc(sizeof(*data), GFP_NOFS);
7183 data->server = server;
7184 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7185 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7186 data->args.lock_owner.s_dev = server->s_dev;
7188 msg.rpc_argp = &data->args;
7189 msg.rpc_resp = &data->res;
7190 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
7191 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
7194 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7196 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
7197 struct dentry *unused, struct inode *inode,
7198 const char *key, const void *buf,
7199 size_t buflen, int flags)
7201 return nfs4_proc_set_acl(inode, buf, buflen);
7204 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
7205 struct dentry *unused, struct inode *inode,
7206 const char *key, void *buf, size_t buflen)
7208 return nfs4_proc_get_acl(inode, buf, buflen);
7211 static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
7213 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
7216 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
7218 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
7219 struct dentry *unused, struct inode *inode,
7220 const char *key, const void *buf,
7221 size_t buflen, int flags)
7223 if (security_ismaclabel(key))
7224 return nfs4_set_security_label(inode, buf, buflen);
7229 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
7230 struct dentry *unused, struct inode *inode,
7231 const char *key, void *buf, size_t buflen)
7233 if (security_ismaclabel(key))
7234 return nfs4_get_security_label(inode, buf, buflen);
7239 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7243 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7244 len = security_inode_listsecurity(inode, list, list_len);
7245 if (list_len && len > list_len)
7251 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7252 .prefix = XATTR_SECURITY_PREFIX,
7253 .get = nfs4_xattr_get_nfs4_label,
7254 .set = nfs4_xattr_set_nfs4_label,
7260 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7268 * nfs_fhget will use either the mounted_on_fileid or the fileid
7270 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7272 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7273 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7274 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
7275 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
7278 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
7279 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
7280 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7284 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7285 const struct qstr *name,
7286 struct nfs4_fs_locations *fs_locations,
7289 struct nfs_server *server = NFS_SERVER(dir);
7291 struct nfs4_fs_locations_arg args = {
7292 .dir_fh = NFS_FH(dir),
7297 struct nfs4_fs_locations_res res = {
7298 .fs_locations = fs_locations,
7300 struct rpc_message msg = {
7301 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7307 dprintk("%s: start\n", __func__);
7309 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7310 bitmask[1] = nfs4_fattr_bitmap[1];
7312 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7313 * is not supported */
7314 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
7315 bitmask[0] &= ~FATTR4_WORD0_FILEID;
7317 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
7319 nfs_fattr_init(&fs_locations->fattr);
7320 fs_locations->server = server;
7321 fs_locations->nlocations = 0;
7322 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
7323 dprintk("%s: returned status = %d\n", __func__, status);
7327 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7328 const struct qstr *name,
7329 struct nfs4_fs_locations *fs_locations,
7332 struct nfs4_exception exception = {
7333 .interruptible = true,
7337 err = _nfs4_proc_fs_locations(client, dir, name,
7338 fs_locations, page);
7339 trace_nfs4_get_fs_locations(dir, name, err);
7340 err = nfs4_handle_exception(NFS_SERVER(dir), err,
7342 } while (exception.retry);
7347 * This operation also signals the server that this client is
7348 * performing migration recovery. The server can stop returning
7349 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7350 * appended to this compound to identify the client ID which is
7351 * performing recovery.
7353 static int _nfs40_proc_get_locations(struct inode *inode,
7354 struct nfs4_fs_locations *locations,
7355 struct page *page, const struct cred *cred)
7357 struct nfs_server *server = NFS_SERVER(inode);
7358 struct rpc_clnt *clnt = server->client;
7360 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7362 struct nfs4_fs_locations_arg args = {
7363 .clientid = server->nfs_client->cl_clientid,
7364 .fh = NFS_FH(inode),
7367 .migration = 1, /* skip LOOKUP */
7368 .renew = 1, /* append RENEW */
7370 struct nfs4_fs_locations_res res = {
7371 .fs_locations = locations,
7375 struct rpc_message msg = {
7376 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7381 unsigned long now = jiffies;
7384 nfs_fattr_init(&locations->fattr);
7385 locations->server = server;
7386 locations->nlocations = 0;
7388 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7389 status = nfs4_call_sync_sequence(clnt, server, &msg,
7390 &args.seq_args, &res.seq_res);
7394 renew_lease(server, now);
7398 #ifdef CONFIG_NFS_V4_1
7401 * This operation also signals the server that this client is
7402 * performing migration recovery. The server can stop asserting
7403 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7404 * performing this operation is identified in the SEQUENCE
7405 * operation in this compound.
7407 * When the client supports GETATTR(fs_locations_info), it can
7408 * be plumbed in here.
7410 static int _nfs41_proc_get_locations(struct inode *inode,
7411 struct nfs4_fs_locations *locations,
7412 struct page *page, const struct cred *cred)
7414 struct nfs_server *server = NFS_SERVER(inode);
7415 struct rpc_clnt *clnt = server->client;
7417 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7419 struct nfs4_fs_locations_arg args = {
7420 .fh = NFS_FH(inode),
7423 .migration = 1, /* skip LOOKUP */
7425 struct nfs4_fs_locations_res res = {
7426 .fs_locations = locations,
7429 struct rpc_message msg = {
7430 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7437 nfs_fattr_init(&locations->fattr);
7438 locations->server = server;
7439 locations->nlocations = 0;
7441 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7442 status = nfs4_call_sync_sequence(clnt, server, &msg,
7443 &args.seq_args, &res.seq_res);
7444 if (status == NFS4_OK &&
7445 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7446 status = -NFS4ERR_LEASE_MOVED;
7450 #endif /* CONFIG_NFS_V4_1 */
7453 * nfs4_proc_get_locations - discover locations for a migrated FSID
7454 * @inode: inode on FSID that is migrating
7455 * @locations: result of query
7457 * @cred: credential to use for this operation
7459 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7460 * operation failed, or a negative errno if a local error occurred.
7462 * On success, "locations" is filled in, but if the server has
7463 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7466 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7467 * from this client that require migration recovery.
7469 int nfs4_proc_get_locations(struct inode *inode,
7470 struct nfs4_fs_locations *locations,
7471 struct page *page, const struct cred *cred)
7473 struct nfs_server *server = NFS_SERVER(inode);
7474 struct nfs_client *clp = server->nfs_client;
7475 const struct nfs4_mig_recovery_ops *ops =
7476 clp->cl_mvops->mig_recovery_ops;
7477 struct nfs4_exception exception = {
7478 .interruptible = true,
7482 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7483 (unsigned long long)server->fsid.major,
7484 (unsigned long long)server->fsid.minor,
7486 nfs_display_fhandle(NFS_FH(inode), __func__);
7489 status = ops->get_locations(inode, locations, page, cred);
7490 if (status != -NFS4ERR_DELAY)
7492 nfs4_handle_exception(server, status, &exception);
7493 } while (exception.retry);
7498 * This operation also signals the server that this client is
7499 * performing "lease moved" recovery. The server can stop
7500 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7501 * is appended to this compound to identify the client ID which is
7502 * performing recovery.
7504 static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
7506 struct nfs_server *server = NFS_SERVER(inode);
7507 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7508 struct rpc_clnt *clnt = server->client;
7509 struct nfs4_fsid_present_arg args = {
7510 .fh = NFS_FH(inode),
7511 .clientid = clp->cl_clientid,
7512 .renew = 1, /* append RENEW */
7514 struct nfs4_fsid_present_res res = {
7517 struct rpc_message msg = {
7518 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7523 unsigned long now = jiffies;
7526 res.fh = nfs_alloc_fhandle();
7530 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7531 status = nfs4_call_sync_sequence(clnt, server, &msg,
7532 &args.seq_args, &res.seq_res);
7533 nfs_free_fhandle(res.fh);
7537 do_renew_lease(clp, now);
7541 #ifdef CONFIG_NFS_V4_1
7544 * This operation also signals the server that this client is
7545 * performing "lease moved" recovery. The server can stop asserting
7546 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7547 * this operation is identified in the SEQUENCE operation in this
7550 static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
7552 struct nfs_server *server = NFS_SERVER(inode);
7553 struct rpc_clnt *clnt = server->client;
7554 struct nfs4_fsid_present_arg args = {
7555 .fh = NFS_FH(inode),
7557 struct nfs4_fsid_present_res res = {
7559 struct rpc_message msg = {
7560 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7567 res.fh = nfs_alloc_fhandle();
7571 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7572 status = nfs4_call_sync_sequence(clnt, server, &msg,
7573 &args.seq_args, &res.seq_res);
7574 nfs_free_fhandle(res.fh);
7575 if (status == NFS4_OK &&
7576 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7577 status = -NFS4ERR_LEASE_MOVED;
7581 #endif /* CONFIG_NFS_V4_1 */
7584 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7585 * @inode: inode on FSID to check
7586 * @cred: credential to use for this operation
7588 * Server indicates whether the FSID is present, moved, or not
7589 * recognized. This operation is necessary to clear a LEASE_MOVED
7590 * condition for this client ID.
7592 * Returns NFS4_OK if the FSID is present on this server,
7593 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7594 * NFS4ERR code if some error occurred on the server, or a
7595 * negative errno if a local failure occurred.
7597 int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
7599 struct nfs_server *server = NFS_SERVER(inode);
7600 struct nfs_client *clp = server->nfs_client;
7601 const struct nfs4_mig_recovery_ops *ops =
7602 clp->cl_mvops->mig_recovery_ops;
7603 struct nfs4_exception exception = {
7604 .interruptible = true,
7608 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7609 (unsigned long long)server->fsid.major,
7610 (unsigned long long)server->fsid.minor,
7612 nfs_display_fhandle(NFS_FH(inode), __func__);
7615 status = ops->fsid_present(inode, cred);
7616 if (status != -NFS4ERR_DELAY)
7618 nfs4_handle_exception(server, status, &exception);
7619 } while (exception.retry);
7624 * If 'use_integrity' is true and the state managment nfs_client
7625 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7626 * and the machine credential as per RFC3530bis and RFC5661 Security
7627 * Considerations sections. Otherwise, just use the user cred with the
7628 * filesystem's rpc_client.
7630 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
7633 struct nfs4_secinfo_arg args = {
7634 .dir_fh = NFS_FH(dir),
7637 struct nfs4_secinfo_res res = {
7640 struct rpc_message msg = {
7641 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7645 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
7646 const struct cred *cred = NULL;
7648 if (use_integrity) {
7649 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
7650 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7651 msg.rpc_cred = cred;
7654 dprintk("NFS call secinfo %s\n", name->name);
7656 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7657 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7659 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7660 &res.seq_res, RPC_TASK_NO_ROUND_ROBIN);
7661 dprintk("NFS reply secinfo: %d\n", status);
7668 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7669 struct nfs4_secinfo_flavors *flavors)
7671 struct nfs4_exception exception = {
7672 .interruptible = true,
7676 err = -NFS4ERR_WRONGSEC;
7678 /* try to use integrity protection with machine cred */
7679 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7680 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7683 * if unable to use integrity protection, or SECINFO with
7684 * integrity protection returns NFS4ERR_WRONGSEC (which is
7685 * disallowed by spec, but exists in deployed servers) use
7686 * the current filesystem's rpc_client and the user cred.
7688 if (err == -NFS4ERR_WRONGSEC)
7689 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7691 trace_nfs4_secinfo(dir, name, err);
7692 err = nfs4_handle_exception(NFS_SERVER(dir), err,
7694 } while (exception.retry);
7698 #ifdef CONFIG_NFS_V4_1
7700 * Check the exchange flags returned by the server for invalid flags, having
7701 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7704 static int nfs4_check_cl_exchange_flags(u32 flags)
7706 if (flags & ~EXCHGID4_FLAG_MASK_R)
7708 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7709 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7711 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7715 return -NFS4ERR_INVAL;
7719 nfs41_same_server_scope(struct nfs41_server_scope *a,
7720 struct nfs41_server_scope *b)
7722 if (a->server_scope_sz != b->server_scope_sz)
7724 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
7728 nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7732 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7733 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7737 * nfs4_proc_bind_one_conn_to_session()
7739 * The 4.1 client currently uses the same TCP connection for the
7740 * fore and backchannel.
7743 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7744 struct rpc_xprt *xprt,
7745 struct nfs_client *clp,
7746 const struct cred *cred)
7749 struct nfs41_bind_conn_to_session_args args = {
7751 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7753 struct nfs41_bind_conn_to_session_res res;
7754 struct rpc_message msg = {
7756 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
7761 struct rpc_task_setup task_setup_data = {
7764 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
7765 .rpc_message = &msg,
7766 .flags = RPC_TASK_TIMEOUT,
7768 struct rpc_task *task;
7770 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7771 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7772 args.dir = NFS4_CDFC4_FORE;
7774 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7775 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7776 args.dir = NFS4_CDFC4_FORE;
7778 task = rpc_run_task(&task_setup_data);
7779 if (!IS_ERR(task)) {
7780 status = task->tk_status;
7783 status = PTR_ERR(task);
7784 trace_nfs4_bind_conn_to_session(clp, status);
7786 if (memcmp(res.sessionid.data,
7787 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7788 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7791 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
7792 dprintk("NFS: %s: Unexpected direction from server\n",
7796 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
7797 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7806 struct rpc_bind_conn_calldata {
7807 struct nfs_client *clp;
7808 const struct cred *cred;
7812 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7813 struct rpc_xprt *xprt,
7816 struct rpc_bind_conn_calldata *p = calldata;
7818 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7821 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
7823 struct rpc_bind_conn_calldata data = {
7827 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7828 nfs4_proc_bind_conn_to_session_callback, &data);
7832 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7833 * and operations we'd like to see to enable certain features in the allow map
7835 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7836 .how = SP4_MACH_CRED,
7837 .enforce.u.words = {
7838 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7839 1 << (OP_EXCHANGE_ID - 32) |
7840 1 << (OP_CREATE_SESSION - 32) |
7841 1 << (OP_DESTROY_SESSION - 32) |
7842 1 << (OP_DESTROY_CLIENTID - 32)
7845 [0] = 1 << (OP_CLOSE) |
7846 1 << (OP_OPEN_DOWNGRADE) |
7848 1 << (OP_DELEGRETURN) |
7850 [1] = 1 << (OP_SECINFO - 32) |
7851 1 << (OP_SECINFO_NO_NAME - 32) |
7852 1 << (OP_LAYOUTRETURN - 32) |
7853 1 << (OP_TEST_STATEID - 32) |
7854 1 << (OP_FREE_STATEID - 32) |
7855 1 << (OP_WRITE - 32)
7860 * Select the state protection mode for client `clp' given the server results
7861 * from exchange_id in `sp'.
7863 * Returns 0 on success, negative errno otherwise.
7865 static int nfs4_sp4_select_mode(struct nfs_client *clp,
7866 struct nfs41_state_protection *sp)
7868 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7869 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7870 1 << (OP_EXCHANGE_ID - 32) |
7871 1 << (OP_CREATE_SESSION - 32) |
7872 1 << (OP_DESTROY_SESSION - 32) |
7873 1 << (OP_DESTROY_CLIENTID - 32)
7875 unsigned long flags = 0;
7879 if (sp->how == SP4_MACH_CRED) {
7880 /* Print state protect result */
7881 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7882 for (i = 0; i <= LAST_NFS4_OP; i++) {
7883 if (test_bit(i, sp->enforce.u.longs))
7884 dfprintk(MOUNT, " enforce op %d\n", i);
7885 if (test_bit(i, sp->allow.u.longs))
7886 dfprintk(MOUNT, " allow op %d\n", i);
7889 /* make sure nothing is on enforce list that isn't supported */
7890 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7891 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7892 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7899 * Minimal mode - state operations are allowed to use machine
7900 * credential. Note this already happens by default, so the
7901 * client doesn't have to do anything more than the negotiation.
7903 * NOTE: we don't care if EXCHANGE_ID is in the list -
7904 * we're already using the machine cred for exchange_id
7905 * and will never use a different cred.
7907 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7908 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7909 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7910 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7911 dfprintk(MOUNT, "sp4_mach_cred:\n");
7912 dfprintk(MOUNT, " minimal mode enabled\n");
7913 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
7915 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7920 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
7921 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7922 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
7923 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7924 dfprintk(MOUNT, " cleanup mode enabled\n");
7925 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
7928 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7929 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7930 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
7933 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7934 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7935 dfprintk(MOUNT, " secinfo mode enabled\n");
7936 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
7939 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7940 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7941 dfprintk(MOUNT, " stateid mode enabled\n");
7942 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
7945 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7946 dfprintk(MOUNT, " write mode enabled\n");
7947 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
7950 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7951 dfprintk(MOUNT, " commit mode enabled\n");
7952 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
7956 clp->cl_sp4_flags = flags;
7960 struct nfs41_exchange_id_data {
7961 struct nfs41_exchange_id_res res;
7962 struct nfs41_exchange_id_args args;
7965 static void nfs4_exchange_id_release(void *data)
7967 struct nfs41_exchange_id_data *cdata =
7968 (struct nfs41_exchange_id_data *)data;
7970 nfs_put_client(cdata->args.client);
7971 kfree(cdata->res.impl_id);
7972 kfree(cdata->res.server_scope);
7973 kfree(cdata->res.server_owner);
7977 static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7978 .rpc_release = nfs4_exchange_id_release,
7982 * _nfs4_proc_exchange_id()
7984 * Wrapper for EXCHANGE_ID operation.
7986 static struct rpc_task *
7987 nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
7988 u32 sp4_how, struct rpc_xprt *xprt)
7990 struct rpc_message msg = {
7991 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
7994 struct rpc_task_setup task_setup_data = {
7995 .rpc_client = clp->cl_rpcclient,
7996 .callback_ops = &nfs4_exchange_id_call_ops,
7997 .rpc_message = &msg,
7998 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
8000 struct nfs41_exchange_id_data *calldata;
8003 if (!refcount_inc_not_zero(&clp->cl_count))
8004 return ERR_PTR(-EIO);
8007 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8011 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
8013 status = nfs4_init_uniform_client_string(clp);
8017 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
8020 if (unlikely(calldata->res.server_owner == NULL))
8023 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
8025 if (unlikely(calldata->res.server_scope == NULL))
8026 goto out_server_owner;
8028 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
8029 if (unlikely(calldata->res.impl_id == NULL))
8030 goto out_server_scope;
8034 calldata->args.state_protect.how = SP4_NONE;
8038 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
8048 task_setup_data.rpc_xprt = xprt;
8049 task_setup_data.flags |= RPC_TASK_SOFTCONN;
8050 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
8051 sizeof(calldata->args.verifier.data));
8053 calldata->args.client = clp;
8054 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
8055 EXCHGID4_FLAG_BIND_PRINC_STATEID;
8056 #ifdef CONFIG_NFS_V4_1_MIGRATION
8057 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
8059 msg.rpc_argp = &calldata->args;
8060 msg.rpc_resp = &calldata->res;
8061 task_setup_data.callback_data = calldata;
8063 return rpc_run_task(&task_setup_data);
8066 kfree(calldata->res.impl_id);
8068 kfree(calldata->res.server_scope);
8070 kfree(calldata->res.server_owner);
8074 nfs_put_client(clp);
8075 return ERR_PTR(status);
8079 * _nfs4_proc_exchange_id()
8081 * Wrapper for EXCHANGE_ID operation.
8083 static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
8086 struct rpc_task *task;
8087 struct nfs41_exchange_id_args *argp;
8088 struct nfs41_exchange_id_res *resp;
8091 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
8093 return PTR_ERR(task);
8095 argp = task->tk_msg.rpc_argp;
8096 resp = task->tk_msg.rpc_resp;
8097 status = task->tk_status;
8101 status = nfs4_check_cl_exchange_flags(resp->flags);
8105 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8109 clp->cl_clientid = resp->clientid;
8110 clp->cl_exchange_flags = resp->flags;
8111 clp->cl_seqid = resp->seqid;
8112 /* Client ID is not confirmed */
8113 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8114 clear_bit(NFS4_SESSION_ESTABLISHED,
8115 &clp->cl_session->session_state);
8117 if (clp->cl_serverscope != NULL &&
8118 !nfs41_same_server_scope(clp->cl_serverscope,
8119 resp->server_scope)) {
8120 dprintk("%s: server_scope mismatch detected\n",
8122 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8125 swap(clp->cl_serverowner, resp->server_owner);
8126 swap(clp->cl_serverscope, resp->server_scope);
8127 swap(clp->cl_implid, resp->impl_id);
8129 /* Save the EXCHANGE_ID verifier session trunk tests */
8130 memcpy(clp->cl_confirm.data, argp->verifier.data,
8131 sizeof(clp->cl_confirm.data));
8133 trace_nfs4_exchange_id(clp, status);
8139 * nfs4_proc_exchange_id()
8141 * Returns zero, a negative errno, or a negative NFS4ERR status code.
8143 * Since the clientid has expired, all compounds using sessions
8144 * associated with the stale clientid will be returning
8145 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
8146 * be in some phase of session reset.
8148 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
8150 int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
8152 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8155 /* try SP4_MACH_CRED if krb5i/p */
8156 if (authflavor == RPC_AUTH_GSS_KRB5I ||
8157 authflavor == RPC_AUTH_GSS_KRB5P) {
8158 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
8164 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
8168 * nfs4_test_session_trunk
8170 * This is an add_xprt_test() test function called from
8171 * rpc_clnt_setup_test_and_add_xprt.
8173 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8174 * and is dereferrenced in nfs4_exchange_id_release
8176 * Upon success, add the new transport to the rpc_clnt
8178 * @clnt: struct rpc_clnt to get new transport
8179 * @xprt: the rpc_xprt to test
8180 * @data: call data for _nfs4_proc_exchange_id.
8182 void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
8185 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
8186 struct rpc_task *task;
8191 dprintk("--> %s try %s\n", __func__,
8192 xprt->address_strings[RPC_DISPLAY_ADDR]);
8194 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8196 /* Test connection for session trunking. Async exchange_id call */
8197 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8201 status = task->tk_status;
8203 status = nfs4_detect_session_trunking(adata->clp,
8204 task->tk_msg.rpc_resp, xprt);
8207 rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
8211 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
8213 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
8214 const struct cred *cred)
8216 struct rpc_message msg = {
8217 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8223 status = rpc_call_sync(clp->cl_rpcclient, &msg,
8224 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
8225 trace_nfs4_destroy_clientid(clp, status);
8227 dprintk("NFS: Got error %d from the server %s on "
8228 "DESTROY_CLIENTID.", status, clp->cl_hostname);
8232 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
8233 const struct cred *cred)
8238 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8239 ret = _nfs4_proc_destroy_clientid(clp, cred);
8241 case -NFS4ERR_DELAY:
8242 case -NFS4ERR_CLIENTID_BUSY:
8252 int nfs4_destroy_clientid(struct nfs_client *clp)
8254 const struct cred *cred;
8257 if (clp->cl_mvops->minor_version < 1)
8259 if (clp->cl_exchange_flags == 0)
8261 if (clp->cl_preserve_clid)
8263 cred = nfs4_get_clid_cred(clp);
8264 ret = nfs4_proc_destroy_clientid(clp, cred);
8268 case -NFS4ERR_STALE_CLIENTID:
8269 clp->cl_exchange_flags = 0;
8275 #endif /* CONFIG_NFS_V4_1 */
8277 struct nfs4_get_lease_time_data {
8278 struct nfs4_get_lease_time_args *args;
8279 struct nfs4_get_lease_time_res *res;
8280 struct nfs_client *clp;
8283 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
8286 struct nfs4_get_lease_time_data *data =
8287 (struct nfs4_get_lease_time_data *)calldata;
8289 dprintk("--> %s\n", __func__);
8290 /* just setup sequence, do not trigger session recovery
8291 since we're invoked within one */
8292 nfs4_setup_sequence(data->clp,
8293 &data->args->la_seq_args,
8294 &data->res->lr_seq_res,
8296 dprintk("<-- %s\n", __func__);
8300 * Called from nfs4_state_manager thread for session setup, so don't recover
8301 * from sequence operation or clientid errors.
8303 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8305 struct nfs4_get_lease_time_data *data =
8306 (struct nfs4_get_lease_time_data *)calldata;
8308 dprintk("--> %s\n", __func__);
8309 if (!nfs4_sequence_done(task, &data->res->lr_seq_res))
8311 switch (task->tk_status) {
8312 case -NFS4ERR_DELAY:
8313 case -NFS4ERR_GRACE:
8314 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
8315 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8316 task->tk_status = 0;
8318 case -NFS4ERR_RETRY_UNCACHED_REP:
8319 rpc_restart_call_prepare(task);
8322 dprintk("<-- %s\n", __func__);
8325 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
8326 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8327 .rpc_call_done = nfs4_get_lease_time_done,
8330 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8332 struct rpc_task *task;
8333 struct nfs4_get_lease_time_args args;
8334 struct nfs4_get_lease_time_res res = {
8335 .lr_fsinfo = fsinfo,
8337 struct nfs4_get_lease_time_data data = {
8342 struct rpc_message msg = {
8343 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8347 struct rpc_task_setup task_setup = {
8348 .rpc_client = clp->cl_rpcclient,
8349 .rpc_message = &msg,
8350 .callback_ops = &nfs4_get_lease_time_ops,
8351 .callback_data = &data,
8352 .flags = RPC_TASK_TIMEOUT,
8356 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
8357 task = rpc_run_task(&task_setup);
8360 return PTR_ERR(task);
8362 status = task->tk_status;
8367 #ifdef CONFIG_NFS_V4_1
8370 * Initialize the values to be used by the client in CREATE_SESSION
8371 * If nfs4_init_session set the fore channel request and response sizes,
8374 * Set the back channel max_resp_sz_cached to zero to force the client to
8375 * always set csa_cachethis to FALSE because the current implementation
8376 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8378 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8379 struct rpc_clnt *clnt)
8381 unsigned int max_rqst_sz, max_resp_sz;
8382 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
8383 unsigned int max_bc_slots = rpc_num_bc_slots(clnt);
8385 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8386 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8388 /* Fore channel attributes */
8389 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8390 args->fc_attrs.max_resp_sz = max_resp_sz;
8391 args->fc_attrs.max_ops = NFS4_MAX_OPS;
8392 args->fc_attrs.max_reqs = max_session_slots;
8394 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
8395 "max_ops=%u max_reqs=%u\n",
8397 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
8398 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
8400 /* Back channel attributes */
8401 args->bc_attrs.max_rqst_sz = max_bc_payload;
8402 args->bc_attrs.max_resp_sz = max_bc_payload;
8403 args->bc_attrs.max_resp_sz_cached = 0;
8404 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
8405 args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
8406 if (args->bc_attrs.max_reqs > max_bc_slots)
8407 args->bc_attrs.max_reqs = max_bc_slots;
8409 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8410 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8412 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8413 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8414 args->bc_attrs.max_reqs);
8417 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8418 struct nfs41_create_session_res *res)
8420 struct nfs4_channel_attrs *sent = &args->fc_attrs;
8421 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
8423 if (rcvd->max_resp_sz > sent->max_resp_sz)
8426 * Our requested max_ops is the minimum we need; we're not
8427 * prepared to break up compounds into smaller pieces than that.
8428 * So, no point even trying to continue if the server won't
8431 if (rcvd->max_ops < sent->max_ops)
8433 if (rcvd->max_reqs == 0)
8435 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8436 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
8440 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8441 struct nfs41_create_session_res *res)
8443 struct nfs4_channel_attrs *sent = &args->bc_attrs;
8444 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
8446 if (!(res->flags & SESSION4_BACK_CHAN))
8448 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8450 if (rcvd->max_resp_sz < sent->max_resp_sz)
8452 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8454 if (rcvd->max_ops > sent->max_ops)
8456 if (rcvd->max_reqs > sent->max_reqs)
8462 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
8463 struct nfs41_create_session_res *res)
8467 ret = nfs4_verify_fore_channel_attrs(args, res);
8470 return nfs4_verify_back_channel_attrs(args, res);
8473 static void nfs4_update_session(struct nfs4_session *session,
8474 struct nfs41_create_session_res *res)
8476 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
8477 /* Mark client id and session as being confirmed */
8478 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8479 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
8480 session->flags = res->flags;
8481 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
8482 if (res->flags & SESSION4_BACK_CHAN)
8483 memcpy(&session->bc_attrs, &res->bc_attrs,
8484 sizeof(session->bc_attrs));
8487 static int _nfs4_proc_create_session(struct nfs_client *clp,
8488 const struct cred *cred)
8490 struct nfs4_session *session = clp->cl_session;
8491 struct nfs41_create_session_args args = {
8493 .clientid = clp->cl_clientid,
8494 .seqid = clp->cl_seqid,
8495 .cb_program = NFS4_CALLBACK,
8497 struct nfs41_create_session_res res;
8499 struct rpc_message msg = {
8500 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8507 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
8508 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
8510 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
8511 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
8512 trace_nfs4_create_session(clp, status);
8515 case -NFS4ERR_STALE_CLIENTID:
8516 case -NFS4ERR_DELAY:
8525 /* Verify the session's negotiated channel_attrs values */
8526 status = nfs4_verify_channel_attrs(&args, &res);
8527 /* Increment the clientid slot sequence id */
8530 nfs4_update_session(session, &res);
8537 * Issues a CREATE_SESSION operation to the server.
8538 * It is the responsibility of the caller to verify the session is
8539 * expired before calling this routine.
8541 int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
8545 struct nfs4_session *session = clp->cl_session;
8547 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8549 status = _nfs4_proc_create_session(clp, cred);
8553 /* Init or reset the session slot tables */
8554 status = nfs4_setup_session_slot_tables(session);
8555 dprintk("slot table setup returned %d\n", status);
8559 ptr = (unsigned *)&session->sess_id.data[0];
8560 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8561 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
8563 dprintk("<-- %s\n", __func__);
8568 * Issue the over-the-wire RPC DESTROY_SESSION.
8569 * The caller must serialize access to this routine.
8571 int nfs4_proc_destroy_session(struct nfs4_session *session,
8572 const struct cred *cred)
8574 struct rpc_message msg = {
8575 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8576 .rpc_argp = session,
8581 dprintk("--> nfs4_proc_destroy_session\n");
8583 /* session is still being setup */
8584 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8587 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
8588 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
8589 trace_nfs4_destroy_session(session->clp, status);
8592 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
8593 "Session has been destroyed regardless...\n", status);
8595 dprintk("<-- nfs4_proc_destroy_session\n");
8600 * Renew the cl_session lease.
8602 struct nfs4_sequence_data {
8603 struct nfs_client *clp;
8604 struct nfs4_sequence_args args;
8605 struct nfs4_sequence_res res;
8608 static void nfs41_sequence_release(void *data)
8610 struct nfs4_sequence_data *calldata = data;
8611 struct nfs_client *clp = calldata->clp;
8613 if (refcount_read(&clp->cl_count) > 1)
8614 nfs4_schedule_state_renewal(clp);
8615 nfs_put_client(clp);
8619 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8621 switch(task->tk_status) {
8622 case -NFS4ERR_DELAY:
8623 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8626 nfs4_schedule_lease_recovery(clp);
8631 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
8633 struct nfs4_sequence_data *calldata = data;
8634 struct nfs_client *clp = calldata->clp;
8636 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8639 trace_nfs4_sequence(clp, task->tk_status);
8640 if (task->tk_status < 0) {
8641 dprintk("%s ERROR %d\n", __func__, task->tk_status);
8642 if (refcount_read(&clp->cl_count) == 1)
8645 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8646 rpc_restart_call_prepare(task);
8650 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
8652 dprintk("<-- %s\n", __func__);
8655 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8657 struct nfs4_sequence_data *calldata = data;
8658 struct nfs_client *clp = calldata->clp;
8659 struct nfs4_sequence_args *args;
8660 struct nfs4_sequence_res *res;
8662 args = task->tk_msg.rpc_argp;
8663 res = task->tk_msg.rpc_resp;
8665 nfs4_setup_sequence(clp, args, res, task);
8668 static const struct rpc_call_ops nfs41_sequence_ops = {
8669 .rpc_call_done = nfs41_sequence_call_done,
8670 .rpc_call_prepare = nfs41_sequence_prepare,
8671 .rpc_release = nfs41_sequence_release,
8674 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8675 const struct cred *cred,
8676 struct nfs4_slot *slot,
8679 struct nfs4_sequence_data *calldata;
8680 struct rpc_message msg = {
8681 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8684 struct rpc_task_setup task_setup_data = {
8685 .rpc_client = clp->cl_rpcclient,
8686 .rpc_message = &msg,
8687 .callback_ops = &nfs41_sequence_ops,
8688 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
8690 struct rpc_task *ret;
8692 ret = ERR_PTR(-EIO);
8693 if (!refcount_inc_not_zero(&clp->cl_count))
8696 ret = ERR_PTR(-ENOMEM);
8697 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8698 if (calldata == NULL)
8700 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
8701 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
8702 msg.rpc_argp = &calldata->args;
8703 msg.rpc_resp = &calldata->res;
8704 calldata->clp = clp;
8705 task_setup_data.callback_data = calldata;
8707 ret = rpc_run_task(&task_setup_data);
8712 nfs_put_client(clp);
8714 nfs41_release_slot(slot);
8718 static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
8720 struct rpc_task *task;
8723 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
8725 task = _nfs41_proc_sequence(clp, cred, NULL, false);
8727 ret = PTR_ERR(task);
8729 rpc_put_task_async(task);
8730 dprintk("<-- %s status=%d\n", __func__, ret);
8734 static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
8736 struct rpc_task *task;
8739 task = _nfs41_proc_sequence(clp, cred, NULL, true);
8741 ret = PTR_ERR(task);
8744 ret = rpc_wait_for_completion_task(task);
8746 ret = task->tk_status;
8749 dprintk("<-- %s status=%d\n", __func__, ret);
8753 struct nfs4_reclaim_complete_data {
8754 struct nfs_client *clp;
8755 struct nfs41_reclaim_complete_args arg;
8756 struct nfs41_reclaim_complete_res res;
8759 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8761 struct nfs4_reclaim_complete_data *calldata = data;
8763 nfs4_setup_sequence(calldata->clp,
8764 &calldata->arg.seq_args,
8765 &calldata->res.seq_res,
8769 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8771 switch(task->tk_status) {
8773 wake_up_all(&clp->cl_lock_waitq);
8775 case -NFS4ERR_COMPLETE_ALREADY:
8776 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8778 case -NFS4ERR_DELAY:
8779 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8781 case -NFS4ERR_RETRY_UNCACHED_REP:
8783 case -NFS4ERR_BADSESSION:
8784 case -NFS4ERR_DEADSESSION:
8785 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8786 nfs4_schedule_session_recovery(clp->cl_session,
8790 nfs4_schedule_lease_recovery(clp);
8795 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8797 struct nfs4_reclaim_complete_data *calldata = data;
8798 struct nfs_client *clp = calldata->clp;
8799 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8801 dprintk("--> %s\n", __func__);
8802 if (!nfs41_sequence_done(task, res))
8805 trace_nfs4_reclaim_complete(clp, task->tk_status);
8806 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8807 rpc_restart_call_prepare(task);
8810 dprintk("<-- %s\n", __func__);
8813 static void nfs4_free_reclaim_complete_data(void *data)
8815 struct nfs4_reclaim_complete_data *calldata = data;
8820 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8821 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8822 .rpc_call_done = nfs4_reclaim_complete_done,
8823 .rpc_release = nfs4_free_reclaim_complete_data,
8827 * Issue a global reclaim complete.
8829 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8830 const struct cred *cred)
8832 struct nfs4_reclaim_complete_data *calldata;
8833 struct rpc_task *task;
8834 struct rpc_message msg = {
8835 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
8838 struct rpc_task_setup task_setup_data = {
8839 .rpc_client = clp->cl_rpcclient,
8840 .rpc_message = &msg,
8841 .callback_ops = &nfs4_reclaim_complete_call_ops,
8842 .flags = RPC_TASK_ASYNC | RPC_TASK_NO_ROUND_ROBIN,
8844 int status = -ENOMEM;
8846 dprintk("--> %s\n", __func__);
8847 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8848 if (calldata == NULL)
8850 calldata->clp = clp;
8851 calldata->arg.one_fs = 0;
8853 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
8854 msg.rpc_argp = &calldata->arg;
8855 msg.rpc_resp = &calldata->res;
8856 task_setup_data.callback_data = calldata;
8857 task = rpc_run_task(&task_setup_data);
8859 status = PTR_ERR(task);
8862 status = rpc_wait_for_completion_task(task);
8864 status = task->tk_status;
8867 dprintk("<-- %s status=%d\n", __func__, status);
8872 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8874 struct nfs4_layoutget *lgp = calldata;
8875 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
8877 dprintk("--> %s\n", __func__);
8878 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
8879 &lgp->res.seq_res, task);
8880 dprintk("<-- %s\n", __func__);
8883 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8885 struct nfs4_layoutget *lgp = calldata;
8887 dprintk("--> %s\n", __func__);
8888 nfs41_sequence_process(task, &lgp->res.seq_res);
8889 dprintk("<-- %s\n", __func__);
8893 nfs4_layoutget_handle_exception(struct rpc_task *task,
8894 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8896 struct inode *inode = lgp->args.inode;
8897 struct nfs_server *server = NFS_SERVER(inode);
8898 struct pnfs_layout_hdr *lo;
8899 int nfs4err = task->tk_status;
8900 int err, status = 0;
8903 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
8905 nfs4_sequence_free_slot(&lgp->res.seq_res);
8912 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8913 * on the file. set tk_status to -ENODATA to tell upper layer to
8916 case -NFS4ERR_LAYOUTUNAVAILABLE:
8920 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8921 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8923 case -NFS4ERR_BADLAYOUT:
8924 status = -EOVERFLOW;
8927 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
8928 * (or clients) writing to the same RAID stripe except when
8929 * the minlength argument is 0 (see RFC5661 section 18.43.3).
8931 * Treat it like we would RECALLCONFLICT -- we retry for a little
8932 * while, and then eventually give up.
8934 case -NFS4ERR_LAYOUTTRYLATER:
8935 if (lgp->args.minlength == 0) {
8936 status = -EOVERFLOW;
8941 case -NFS4ERR_RECALLCONFLICT:
8942 status = -ERECALLCONFLICT;
8944 case -NFS4ERR_DELEG_REVOKED:
8945 case -NFS4ERR_ADMIN_REVOKED:
8946 case -NFS4ERR_EXPIRED:
8947 case -NFS4ERR_BAD_STATEID:
8948 exception->timeout = 0;
8949 spin_lock(&inode->i_lock);
8950 lo = NFS_I(inode)->layout;
8951 /* If the open stateid was bad, then recover it. */
8952 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8953 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
8954 spin_unlock(&inode->i_lock);
8955 exception->state = lgp->args.ctx->state;
8956 exception->stateid = &lgp->args.stateid;
8961 * Mark the bad layout state as invalid, then retry
8963 pnfs_mark_layout_stateid_invalid(lo, &head);
8964 spin_unlock(&inode->i_lock);
8965 nfs_commit_inode(inode, 0);
8966 pnfs_free_lseg_list(&head);
8971 err = nfs4_handle_exception(server, nfs4err, exception);
8973 if (exception->retry)
8979 dprintk("<-- %s\n", __func__);
8983 size_t max_response_pages(struct nfs_server *server)
8985 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8986 return nfs_page_array_len(0, max_resp_sz);
8989 static void nfs4_layoutget_release(void *calldata)
8991 struct nfs4_layoutget *lgp = calldata;
8993 dprintk("--> %s\n", __func__);
8994 nfs4_sequence_free_slot(&lgp->res.seq_res);
8995 pnfs_layoutget_free(lgp);
8996 dprintk("<-- %s\n", __func__);
8999 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
9000 .rpc_call_prepare = nfs4_layoutget_prepare,
9001 .rpc_call_done = nfs4_layoutget_done,
9002 .rpc_release = nfs4_layoutget_release,
9005 struct pnfs_layout_segment *
9006 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
9008 struct inode *inode = lgp->args.inode;
9009 struct nfs_server *server = NFS_SERVER(inode);
9010 struct rpc_task *task;
9011 struct rpc_message msg = {
9012 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
9013 .rpc_argp = &lgp->args,
9014 .rpc_resp = &lgp->res,
9015 .rpc_cred = lgp->cred,
9017 struct rpc_task_setup task_setup_data = {
9018 .rpc_client = server->client,
9019 .rpc_message = &msg,
9020 .callback_ops = &nfs4_layoutget_call_ops,
9021 .callback_data = lgp,
9022 .flags = RPC_TASK_ASYNC,
9024 struct pnfs_layout_segment *lseg = NULL;
9025 struct nfs4_exception exception = {
9027 .timeout = *timeout,
9031 dprintk("--> %s\n", __func__);
9033 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
9034 pnfs_get_layout_hdr(NFS_I(inode)->layout);
9036 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
9038 task = rpc_run_task(&task_setup_data);
9040 return ERR_CAST(task);
9041 status = rpc_wait_for_completion_task(task);
9045 if (task->tk_status < 0) {
9046 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
9047 *timeout = exception.timeout;
9048 } else if (lgp->res.layoutp->len == 0) {
9050 *timeout = nfs4_update_delay(&exception.timeout);
9052 lseg = pnfs_layout_process(lgp);
9054 trace_nfs4_layoutget(lgp->args.ctx,
9061 dprintk("<-- %s status=%d\n", __func__, status);
9063 return ERR_PTR(status);
9068 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
9070 struct nfs4_layoutreturn *lrp = calldata;
9072 dprintk("--> %s\n", __func__);
9073 nfs4_setup_sequence(lrp->clp,
9074 &lrp->args.seq_args,
9077 if (!pnfs_layout_is_valid(lrp->args.layout))
9081 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
9083 struct nfs4_layoutreturn *lrp = calldata;
9084 struct nfs_server *server;
9086 dprintk("--> %s\n", __func__);
9088 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
9091 server = NFS_SERVER(lrp->args.inode);
9092 switch (task->tk_status) {
9093 case -NFS4ERR_OLD_STATEID:
9094 if (nfs4_layoutreturn_refresh_stateid(&lrp->args.stateid,
9100 task->tk_status = 0;
9104 case -NFS4ERR_DELAY:
9105 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
9109 dprintk("<-- %s\n", __func__);
9112 task->tk_status = 0;
9113 nfs4_sequence_free_slot(&lrp->res.seq_res);
9114 rpc_restart_call_prepare(task);
9117 static void nfs4_layoutreturn_release(void *calldata)
9119 struct nfs4_layoutreturn *lrp = calldata;
9120 struct pnfs_layout_hdr *lo = lrp->args.layout;
9122 dprintk("--> %s\n", __func__);
9123 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
9124 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
9125 nfs4_sequence_free_slot(&lrp->res.seq_res);
9126 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9127 lrp->ld_private.ops->free(&lrp->ld_private);
9128 pnfs_put_layout_hdr(lrp->args.layout);
9129 nfs_iput_and_deactive(lrp->inode);
9131 dprintk("<-- %s\n", __func__);
9134 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9135 .rpc_call_prepare = nfs4_layoutreturn_prepare,
9136 .rpc_call_done = nfs4_layoutreturn_done,
9137 .rpc_release = nfs4_layoutreturn_release,
9140 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
9142 struct rpc_task *task;
9143 struct rpc_message msg = {
9144 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9145 .rpc_argp = &lrp->args,
9146 .rpc_resp = &lrp->res,
9147 .rpc_cred = lrp->cred,
9149 struct rpc_task_setup task_setup_data = {
9150 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
9151 .rpc_message = &msg,
9152 .callback_ops = &nfs4_layoutreturn_call_ops,
9153 .callback_data = lrp,
9157 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9158 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9159 &task_setup_data.rpc_client, &msg);
9161 dprintk("--> %s\n", __func__);
9163 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
9165 nfs4_layoutreturn_release(lrp);
9168 task_setup_data.flags |= RPC_TASK_ASYNC;
9170 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1, 0);
9171 task = rpc_run_task(&task_setup_data);
9173 return PTR_ERR(task);
9175 status = task->tk_status;
9176 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
9177 dprintk("<-- %s status=%d\n", __func__, status);
9183 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
9184 struct pnfs_device *pdev,
9185 const struct cred *cred)
9187 struct nfs4_getdeviceinfo_args args = {
9189 .notify_types = NOTIFY_DEVICEID4_CHANGE |
9190 NOTIFY_DEVICEID4_DELETE,
9192 struct nfs4_getdeviceinfo_res res = {
9195 struct rpc_message msg = {
9196 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9203 dprintk("--> %s\n", __func__);
9204 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
9205 if (res.notification & ~args.notify_types)
9206 dprintk("%s: unsupported notification\n", __func__);
9207 if (res.notification != args.notify_types)
9210 dprintk("<-- %s status=%d\n", __func__, status);
9215 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9216 struct pnfs_device *pdev,
9217 const struct cred *cred)
9219 struct nfs4_exception exception = { };
9223 err = nfs4_handle_exception(server,
9224 _nfs4_proc_getdeviceinfo(server, pdev, cred),
9226 } while (exception.retry);
9229 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9231 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9233 struct nfs4_layoutcommit_data *data = calldata;
9234 struct nfs_server *server = NFS_SERVER(data->args.inode);
9236 nfs4_setup_sequence(server->nfs_client,
9237 &data->args.seq_args,
9243 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9245 struct nfs4_layoutcommit_data *data = calldata;
9246 struct nfs_server *server = NFS_SERVER(data->args.inode);
9248 if (!nfs41_sequence_done(task, &data->res.seq_res))
9251 switch (task->tk_status) { /* Just ignore these failures */
9252 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9253 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9254 case -NFS4ERR_BADLAYOUT: /* no layout */
9255 case -NFS4ERR_GRACE: /* loca_recalim always false */
9256 task->tk_status = 0;
9260 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
9261 rpc_restart_call_prepare(task);
9267 static void nfs4_layoutcommit_release(void *calldata)
9269 struct nfs4_layoutcommit_data *data = calldata;
9271 pnfs_cleanup_layoutcommit(data);
9272 nfs_post_op_update_inode_force_wcc(data->args.inode,
9274 put_cred(data->cred);
9275 nfs_iput_and_deactive(data->inode);
9279 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9280 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9281 .rpc_call_done = nfs4_layoutcommit_done,
9282 .rpc_release = nfs4_layoutcommit_release,
9286 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
9288 struct rpc_message msg = {
9289 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9290 .rpc_argp = &data->args,
9291 .rpc_resp = &data->res,
9292 .rpc_cred = data->cred,
9294 struct rpc_task_setup task_setup_data = {
9295 .task = &data->task,
9296 .rpc_client = NFS_CLIENT(data->args.inode),
9297 .rpc_message = &msg,
9298 .callback_ops = &nfs4_layoutcommit_ops,
9299 .callback_data = data,
9301 struct rpc_task *task;
9304 dprintk("NFS: initiating layoutcommit call. sync %d "
9305 "lbw: %llu inode %lu\n", sync,
9306 data->args.lastbytewritten,
9307 data->args.inode->i_ino);
9310 data->inode = nfs_igrab_and_active(data->args.inode);
9311 if (data->inode == NULL) {
9312 nfs4_layoutcommit_release(data);
9315 task_setup_data.flags = RPC_TASK_ASYNC;
9317 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
9318 task = rpc_run_task(&task_setup_data);
9320 return PTR_ERR(task);
9322 status = task->tk_status;
9323 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
9324 dprintk("%s: status %d\n", __func__, status);
9330 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9331 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9334 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9335 struct nfs_fsinfo *info,
9336 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
9338 struct nfs41_secinfo_no_name_args args = {
9339 .style = SECINFO_STYLE_CURRENT_FH,
9341 struct nfs4_secinfo_res res = {
9344 struct rpc_message msg = {
9345 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9349 struct rpc_clnt *clnt = server->client;
9350 const struct cred *cred = NULL;
9353 if (use_integrity) {
9354 clnt = server->nfs_client->cl_rpcclient;
9355 cred = nfs4_get_clid_cred(server->nfs_client);
9356 msg.rpc_cred = cred;
9359 dprintk("--> %s\n", __func__);
9360 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
9361 &res.seq_res, RPC_TASK_NO_ROUND_ROBIN);
9362 dprintk("<-- %s status=%d\n", __func__, status);
9370 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9371 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9373 struct nfs4_exception exception = {
9374 .interruptible = true,
9378 /* first try using integrity protection */
9379 err = -NFS4ERR_WRONGSEC;
9381 /* try to use integrity protection with machine cred */
9382 if (_nfs4_is_integrity_protected(server->nfs_client))
9383 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9387 * if unable to use integrity protection, or SECINFO with
9388 * integrity protection returns NFS4ERR_WRONGSEC (which is
9389 * disallowed by spec, but exists in deployed servers) use
9390 * the current filesystem's rpc_client and the user cred.
9392 if (err == -NFS4ERR_WRONGSEC)
9393 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9398 case -NFS4ERR_WRONGSEC:
9402 err = nfs4_handle_exception(server, err, &exception);
9404 } while (exception.retry);
9410 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9411 struct nfs_fsinfo *info)
9415 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
9416 struct nfs4_secinfo_flavors *flavors;
9417 struct nfs4_secinfo4 *secinfo;
9420 page = alloc_page(GFP_KERNEL);
9426 flavors = page_address(page);
9427 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9430 * Fall back on "guess and check" method if
9431 * the server doesn't support SECINFO_NO_NAME
9433 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
9434 err = nfs4_find_root_sec(server, fhandle, info);
9440 for (i = 0; i < flavors->num_flavors; i++) {
9441 secinfo = &flavors->flavors[i];
9443 switch (secinfo->flavor) {
9447 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9448 &secinfo->flavor_info);
9451 flavor = RPC_AUTH_MAXFLAVOR;
9455 if (!nfs_auth_info_match(&server->auth_info, flavor))
9456 flavor = RPC_AUTH_MAXFLAVOR;
9458 if (flavor != RPC_AUTH_MAXFLAVOR) {
9459 err = nfs4_lookup_root_sec(server, fhandle,
9466 if (flavor == RPC_AUTH_MAXFLAVOR)
9477 static int _nfs41_test_stateid(struct nfs_server *server,
9478 nfs4_stateid *stateid,
9479 const struct cred *cred)
9482 struct nfs41_test_stateid_args args = {
9485 struct nfs41_test_stateid_res res;
9486 struct rpc_message msg = {
9487 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9492 struct rpc_clnt *rpc_client = server->client;
9494 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9497 dprintk("NFS call test_stateid %p\n", stateid);
9498 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
9499 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
9500 &args.seq_args, &res.seq_res);
9501 if (status != NFS_OK) {
9502 dprintk("NFS reply test_stateid: failed, %d\n", status);
9505 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
9509 static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9510 int err, struct nfs4_exception *exception)
9512 exception->retry = 0;
9514 case -NFS4ERR_DELAY:
9515 case -NFS4ERR_RETRY_UNCACHED_REP:
9516 nfs4_handle_exception(server, err, exception);
9518 case -NFS4ERR_BADSESSION:
9519 case -NFS4ERR_BADSLOT:
9520 case -NFS4ERR_BAD_HIGH_SLOT:
9521 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9522 case -NFS4ERR_DEADSESSION:
9523 nfs4_do_handle_exception(server, err, exception);
9528 * nfs41_test_stateid - perform a TEST_STATEID operation
9530 * @server: server / transport on which to perform the operation
9531 * @stateid: state ID to test
9534 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9535 * Otherwise a negative NFS4ERR value is returned if the operation
9536 * failed or the state ID is not currently valid.
9538 static int nfs41_test_stateid(struct nfs_server *server,
9539 nfs4_stateid *stateid,
9540 const struct cred *cred)
9542 struct nfs4_exception exception = {
9543 .interruptible = true,
9547 err = _nfs41_test_stateid(server, stateid, cred);
9548 nfs4_handle_delay_or_session_error(server, err, &exception);
9549 } while (exception.retry);
9553 struct nfs_free_stateid_data {
9554 struct nfs_server *server;
9555 struct nfs41_free_stateid_args args;
9556 struct nfs41_free_stateid_res res;
9559 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9561 struct nfs_free_stateid_data *data = calldata;
9562 nfs4_setup_sequence(data->server->nfs_client,
9563 &data->args.seq_args,
9568 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9570 struct nfs_free_stateid_data *data = calldata;
9572 nfs41_sequence_done(task, &data->res.seq_res);
9574 switch (task->tk_status) {
9575 case -NFS4ERR_DELAY:
9576 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
9577 rpc_restart_call_prepare(task);
9581 static void nfs41_free_stateid_release(void *calldata)
9586 static const struct rpc_call_ops nfs41_free_stateid_ops = {
9587 .rpc_call_prepare = nfs41_free_stateid_prepare,
9588 .rpc_call_done = nfs41_free_stateid_done,
9589 .rpc_release = nfs41_free_stateid_release,
9593 * nfs41_free_stateid - perform a FREE_STATEID operation
9595 * @server: server / transport on which to perform the operation
9596 * @stateid: state ID to release
9598 * @privileged: set to true if this call needs to be privileged
9600 * Note: this function is always asynchronous.
9602 static int nfs41_free_stateid(struct nfs_server *server,
9603 const nfs4_stateid *stateid,
9604 const struct cred *cred,
9607 struct rpc_message msg = {
9608 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
9611 struct rpc_task_setup task_setup = {
9612 .rpc_client = server->client,
9613 .rpc_message = &msg,
9614 .callback_ops = &nfs41_free_stateid_ops,
9615 .flags = RPC_TASK_ASYNC,
9617 struct nfs_free_stateid_data *data;
9618 struct rpc_task *task;
9620 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9621 &task_setup.rpc_client, &msg);
9623 dprintk("NFS call free_stateid %p\n", stateid);
9624 data = kmalloc(sizeof(*data), GFP_NOFS);
9627 data->server = server;
9628 nfs4_stateid_copy(&data->args.stateid, stateid);
9630 task_setup.callback_data = data;
9632 msg.rpc_argp = &data->args;
9633 msg.rpc_resp = &data->res;
9634 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
9635 task = rpc_run_task(&task_setup);
9637 return PTR_ERR(task);
9643 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
9645 const struct cred *cred = lsp->ls_state->owner->so_cred;
9647 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
9648 nfs4_free_lock_state(server, lsp);
9651 static bool nfs41_match_stateid(const nfs4_stateid *s1,
9652 const nfs4_stateid *s2)
9654 if (s1->type != s2->type)
9657 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
9660 if (s1->seqid == s2->seqid)
9663 return s1->seqid == 0 || s2->seqid == 0;
9666 #endif /* CONFIG_NFS_V4_1 */
9668 static bool nfs4_match_stateid(const nfs4_stateid *s1,
9669 const nfs4_stateid *s2)
9671 return nfs4_stateid_match(s1, s2);
9675 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
9676 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9677 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9678 .recover_open = nfs4_open_reclaim,
9679 .recover_lock = nfs4_lock_reclaim,
9680 .establish_clid = nfs4_init_clientid,
9681 .detect_trunking = nfs40_discover_server_trunking,
9684 #if defined(CONFIG_NFS_V4_1)
9685 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
9686 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9687 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9688 .recover_open = nfs4_open_reclaim,
9689 .recover_lock = nfs4_lock_reclaim,
9690 .establish_clid = nfs41_init_clientid,
9691 .reclaim_complete = nfs41_proc_reclaim_complete,
9692 .detect_trunking = nfs41_discover_server_trunking,
9694 #endif /* CONFIG_NFS_V4_1 */
9696 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
9697 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9698 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
9699 .recover_open = nfs40_open_expired,
9700 .recover_lock = nfs4_lock_expired,
9701 .establish_clid = nfs4_init_clientid,
9704 #if defined(CONFIG_NFS_V4_1)
9705 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
9706 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9707 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
9708 .recover_open = nfs41_open_expired,
9709 .recover_lock = nfs41_lock_expired,
9710 .establish_clid = nfs41_init_clientid,
9712 #endif /* CONFIG_NFS_V4_1 */
9714 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
9715 .sched_state_renewal = nfs4_proc_async_renew,
9716 .get_state_renewal_cred = nfs4_get_renew_cred,
9717 .renew_lease = nfs4_proc_renew,
9720 #if defined(CONFIG_NFS_V4_1)
9721 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
9722 .sched_state_renewal = nfs41_proc_async_sequence,
9723 .get_state_renewal_cred = nfs4_get_machine_cred,
9724 .renew_lease = nfs4_proc_sequence,
9728 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
9729 .get_locations = _nfs40_proc_get_locations,
9730 .fsid_present = _nfs40_proc_fsid_present,
9733 #if defined(CONFIG_NFS_V4_1)
9734 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
9735 .get_locations = _nfs41_proc_get_locations,
9736 .fsid_present = _nfs41_proc_fsid_present,
9738 #endif /* CONFIG_NFS_V4_1 */
9740 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9742 .init_caps = NFS_CAP_READDIRPLUS
9743 | NFS_CAP_ATOMIC_OPEN
9744 | NFS_CAP_POSIX_LOCK,
9745 .init_client = nfs40_init_client,
9746 .shutdown_client = nfs40_shutdown_client,
9747 .match_stateid = nfs4_match_stateid,
9748 .find_root_sec = nfs4_find_root_sec,
9749 .free_lock_state = nfs4_release_lockowner,
9750 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
9751 .alloc_seqid = nfs_alloc_seqid,
9752 .call_sync_ops = &nfs40_call_sync_ops,
9753 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9754 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9755 .state_renewal_ops = &nfs40_state_renewal_ops,
9756 .mig_recovery_ops = &nfs40_mig_recovery_ops,
9759 #if defined(CONFIG_NFS_V4_1)
9760 static struct nfs_seqid *
9761 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9766 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9768 .init_caps = NFS_CAP_READDIRPLUS
9769 | NFS_CAP_ATOMIC_OPEN
9770 | NFS_CAP_POSIX_LOCK
9771 | NFS_CAP_STATEID_NFSV41
9772 | NFS_CAP_ATOMIC_OPEN_V1
9774 .init_client = nfs41_init_client,
9775 .shutdown_client = nfs41_shutdown_client,
9776 .match_stateid = nfs41_match_stateid,
9777 .find_root_sec = nfs41_find_root_sec,
9778 .free_lock_state = nfs41_free_lock_state,
9779 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
9780 .alloc_seqid = nfs_alloc_no_seqid,
9781 .session_trunk = nfs4_test_session_trunk,
9782 .call_sync_ops = &nfs41_call_sync_ops,
9783 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9784 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9785 .state_renewal_ops = &nfs41_state_renewal_ops,
9786 .mig_recovery_ops = &nfs41_mig_recovery_ops,
9790 #if defined(CONFIG_NFS_V4_2)
9791 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9793 .init_caps = NFS_CAP_READDIRPLUS
9794 | NFS_CAP_ATOMIC_OPEN
9795 | NFS_CAP_POSIX_LOCK
9796 | NFS_CAP_STATEID_NFSV41
9797 | NFS_CAP_ATOMIC_OPEN_V1
9801 | NFS_CAP_OFFLOAD_CANCEL
9802 | NFS_CAP_DEALLOCATE
9804 | NFS_CAP_LAYOUTSTATS
9806 | NFS_CAP_LAYOUTERROR,
9807 .init_client = nfs41_init_client,
9808 .shutdown_client = nfs41_shutdown_client,
9809 .match_stateid = nfs41_match_stateid,
9810 .find_root_sec = nfs41_find_root_sec,
9811 .free_lock_state = nfs41_free_lock_state,
9812 .call_sync_ops = &nfs41_call_sync_ops,
9813 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
9814 .alloc_seqid = nfs_alloc_no_seqid,
9815 .session_trunk = nfs4_test_session_trunk,
9816 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9817 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9818 .state_renewal_ops = &nfs41_state_renewal_ops,
9819 .mig_recovery_ops = &nfs41_mig_recovery_ops,
9823 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9824 [0] = &nfs_v4_0_minor_ops,
9825 #if defined(CONFIG_NFS_V4_1)
9826 [1] = &nfs_v4_1_minor_ops,
9828 #if defined(CONFIG_NFS_V4_2)
9829 [2] = &nfs_v4_2_minor_ops,
9833 static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
9835 ssize_t error, error2;
9837 error = generic_listxattr(dentry, list, size);
9845 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9848 return error + error2;
9851 static const struct inode_operations nfs4_dir_inode_operations = {
9852 .create = nfs_create,
9853 .lookup = nfs_lookup,
9854 .atomic_open = nfs_atomic_open,
9856 .unlink = nfs_unlink,
9857 .symlink = nfs_symlink,
9861 .rename = nfs_rename,
9862 .permission = nfs_permission,
9863 .getattr = nfs_getattr,
9864 .setattr = nfs_setattr,
9865 .listxattr = nfs4_listxattr,
9868 static const struct inode_operations nfs4_file_inode_operations = {
9869 .permission = nfs_permission,
9870 .getattr = nfs_getattr,
9871 .setattr = nfs_setattr,
9872 .listxattr = nfs4_listxattr,
9875 const struct nfs_rpc_ops nfs_v4_clientops = {
9876 .version = 4, /* protocol version */
9877 .dentry_ops = &nfs4_dentry_operations,
9878 .dir_inode_ops = &nfs4_dir_inode_operations,
9879 .file_inode_ops = &nfs4_file_inode_operations,
9880 .file_ops = &nfs4_file_operations,
9881 .getroot = nfs4_proc_get_root,
9882 .submount = nfs4_submount,
9883 .try_mount = nfs4_try_mount,
9884 .getattr = nfs4_proc_getattr,
9885 .setattr = nfs4_proc_setattr,
9886 .lookup = nfs4_proc_lookup,
9887 .lookupp = nfs4_proc_lookupp,
9888 .access = nfs4_proc_access,
9889 .readlink = nfs4_proc_readlink,
9890 .create = nfs4_proc_create,
9891 .remove = nfs4_proc_remove,
9892 .unlink_setup = nfs4_proc_unlink_setup,
9893 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
9894 .unlink_done = nfs4_proc_unlink_done,
9895 .rename_setup = nfs4_proc_rename_setup,
9896 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
9897 .rename_done = nfs4_proc_rename_done,
9898 .link = nfs4_proc_link,
9899 .symlink = nfs4_proc_symlink,
9900 .mkdir = nfs4_proc_mkdir,
9901 .rmdir = nfs4_proc_rmdir,
9902 .readdir = nfs4_proc_readdir,
9903 .mknod = nfs4_proc_mknod,
9904 .statfs = nfs4_proc_statfs,
9905 .fsinfo = nfs4_proc_fsinfo,
9906 .pathconf = nfs4_proc_pathconf,
9907 .set_capabilities = nfs4_server_capabilities,
9908 .decode_dirent = nfs4_decode_dirent,
9909 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
9910 .read_setup = nfs4_proc_read_setup,
9911 .read_done = nfs4_read_done,
9912 .write_setup = nfs4_proc_write_setup,
9913 .write_done = nfs4_write_done,
9914 .commit_setup = nfs4_proc_commit_setup,
9915 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
9916 .commit_done = nfs4_commit_done,
9917 .lock = nfs4_proc_lock,
9918 .clear_acl_cache = nfs4_zap_acl_attr,
9919 .close_context = nfs4_close_context,
9920 .open_context = nfs4_atomic_open,
9921 .have_delegation = nfs4_have_delegation,
9922 .alloc_client = nfs4_alloc_client,
9923 .init_client = nfs4_init_client,
9924 .free_client = nfs4_free_client,
9925 .create_server = nfs4_create_server,
9926 .clone_server = nfs_clone_server,
9929 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
9930 .name = XATTR_NAME_NFSV4_ACL,
9931 .list = nfs4_xattr_list_nfs4_acl,
9932 .get = nfs4_xattr_get_nfs4_acl,
9933 .set = nfs4_xattr_set_nfs4_acl,
9936 const struct xattr_handler *nfs4_xattr_handlers[] = {
9937 &nfs4_xattr_nfs4_acl_handler,
9938 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
9939 &nfs4_xattr_nfs4_label_handler,