4 * Client-side procedure declarations for NFSv4.
6 * Copyright (c) 2002 The Regents of the University of Michigan.
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <linux/delay.h>
40 #include <linux/errno.h>
41 #include <linux/string.h>
42 #include <linux/ratelimit.h>
43 #include <linux/printk.h>
44 #include <linux/slab.h>
45 #include <linux/sunrpc/clnt.h>
46 #include <linux/nfs.h>
47 #include <linux/nfs4.h>
48 #include <linux/nfs_fs.h>
49 #include <linux/nfs_page.h>
50 #include <linux/nfs_mount.h>
51 #include <linux/namei.h>
52 #include <linux/mount.h>
53 #include <linux/module.h>
54 #include <linux/xattr.h>
55 #include <linux/utsname.h>
56 #include <linux/freezer.h>
57 #include <linux/iversion.h>
60 #include "delegation.h"
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_clear_open_state_flags(struct nfs4_state *state)
1688 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1689 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1690 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1691 clear_bit(NFS_OPEN_STATE, &state->flags);
1694 static void nfs_state_set_delegation(struct nfs4_state *state,
1695 const nfs4_stateid *deleg_stateid,
1699 * Protect the call to nfs4_state_set_mode_locked and
1700 * serialise the stateid update
1702 write_seqlock(&state->seqlock);
1703 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1704 set_bit(NFS_DELEGATED_STATE, &state->flags);
1705 write_sequnlock(&state->seqlock);
1708 static void nfs_state_clear_delegation(struct nfs4_state *state)
1710 write_seqlock(&state->seqlock);
1711 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1712 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1713 write_sequnlock(&state->seqlock);
1716 static int update_open_stateid(struct nfs4_state *state,
1717 const nfs4_stateid *open_stateid,
1718 const nfs4_stateid *delegation,
1721 struct nfs_server *server = NFS_SERVER(state->inode);
1722 struct nfs_client *clp = server->nfs_client;
1723 struct nfs_inode *nfsi = NFS_I(state->inode);
1724 struct nfs_delegation *deleg_cur;
1725 nfs4_stateid freeme = { };
1728 fmode &= (FMODE_READ|FMODE_WRITE);
1731 spin_lock(&state->owner->so_lock);
1732 if (open_stateid != NULL) {
1733 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1737 deleg_cur = rcu_dereference(nfsi->delegation);
1738 if (deleg_cur == NULL)
1741 spin_lock(&deleg_cur->lock);
1742 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1743 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1744 (deleg_cur->type & fmode) != fmode)
1745 goto no_delegation_unlock;
1747 if (delegation == NULL)
1748 delegation = &deleg_cur->stateid;
1749 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
1750 goto no_delegation_unlock;
1752 nfs_mark_delegation_referenced(deleg_cur);
1753 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
1755 no_delegation_unlock:
1756 spin_unlock(&deleg_cur->lock);
1759 update_open_stateflags(state, fmode);
1760 spin_unlock(&state->owner->so_lock);
1763 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1764 nfs4_schedule_state_manager(clp);
1765 if (freeme.type != 0)
1766 nfs4_test_and_free_stateid(server, &freeme,
1767 state->owner->so_cred);
1772 static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1773 const nfs4_stateid *stateid)
1775 struct nfs4_state *state = lsp->ls_state;
1778 spin_lock(&state->state_lock);
1779 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1781 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1783 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1786 spin_unlock(&state->state_lock);
1790 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1792 struct nfs_delegation *delegation;
1794 fmode &= FMODE_READ|FMODE_WRITE;
1796 delegation = rcu_dereference(NFS_I(inode)->delegation);
1797 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1802 nfs4_inode_return_delegation(inode);
1805 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1807 struct nfs4_state *state = opendata->state;
1808 struct nfs_inode *nfsi = NFS_I(state->inode);
1809 struct nfs_delegation *delegation;
1810 int open_mode = opendata->o_arg.open_flags;
1811 fmode_t fmode = opendata->o_arg.fmode;
1812 enum open_claim_type4 claim = opendata->o_arg.claim;
1813 nfs4_stateid stateid;
1817 spin_lock(&state->owner->so_lock);
1818 if (can_open_cached(state, fmode, open_mode, claim)) {
1819 update_open_stateflags(state, fmode);
1820 spin_unlock(&state->owner->so_lock);
1821 goto out_return_state;
1823 spin_unlock(&state->owner->so_lock);
1825 delegation = rcu_dereference(nfsi->delegation);
1826 if (!can_open_delegated(delegation, fmode, claim)) {
1830 /* Save the delegation */
1831 nfs4_stateid_copy(&stateid, &delegation->stateid);
1833 nfs_release_seqid(opendata->o_arg.seqid);
1834 if (!opendata->is_recover) {
1835 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1841 /* Try to update the stateid using the delegation */
1842 if (update_open_stateid(state, NULL, &stateid, fmode))
1843 goto out_return_state;
1846 return ERR_PTR(ret);
1848 refcount_inc(&state->count);
1853 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1855 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1856 struct nfs_delegation *delegation;
1857 int delegation_flags = 0;
1860 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1862 delegation_flags = delegation->flags;
1864 switch (data->o_arg.claim) {
1867 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1868 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1869 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1870 "returning a delegation for "
1871 "OPEN(CLAIM_DELEGATE_CUR)\n",
1875 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1876 nfs_inode_set_delegation(state->inode,
1877 data->owner->so_cred,
1878 data->o_res.delegation_type,
1879 &data->o_res.delegation,
1880 data->o_res.pagemod_limit);
1882 nfs_inode_reclaim_delegation(state->inode,
1883 data->owner->so_cred,
1884 data->o_res.delegation_type,
1885 &data->o_res.delegation,
1886 data->o_res.pagemod_limit);
1888 if (data->o_res.do_recall)
1889 nfs_async_inode_return_delegation(state->inode,
1890 &data->o_res.delegation);
1894 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1895 * and update the nfs4_state.
1897 static struct nfs4_state *
1898 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1900 struct inode *inode = data->state->inode;
1901 struct nfs4_state *state = data->state;
1904 if (!data->rpc_done) {
1905 if (data->rpc_status)
1906 return ERR_PTR(data->rpc_status);
1907 /* cached opens have already been processed */
1911 ret = nfs_refresh_inode(inode, &data->f_attr);
1913 return ERR_PTR(ret);
1915 if (data->o_res.delegation_type != 0)
1916 nfs4_opendata_check_deleg(data, state);
1918 if (!update_open_stateid(state, &data->o_res.stateid,
1919 NULL, data->o_arg.fmode))
1920 return ERR_PTR(-EAGAIN);
1921 refcount_inc(&state->count);
1926 static struct inode *
1927 nfs4_opendata_get_inode(struct nfs4_opendata *data)
1929 struct inode *inode;
1931 switch (data->o_arg.claim) {
1932 case NFS4_OPEN_CLAIM_NULL:
1933 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1934 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1935 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1936 return ERR_PTR(-EAGAIN);
1937 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1938 &data->f_attr, data->f_label);
1941 inode = d_inode(data->dentry);
1943 nfs_refresh_inode(inode, &data->f_attr);
1948 static struct nfs4_state *
1949 nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
1951 struct nfs4_state *state;
1952 struct inode *inode;
1954 inode = nfs4_opendata_get_inode(data);
1956 return ERR_CAST(inode);
1957 if (data->state != NULL && data->state->inode == inode) {
1958 state = data->state;
1959 refcount_inc(&state->count);
1961 state = nfs4_get_open_state(inode, data->owner);
1964 state = ERR_PTR(-ENOMEM);
1968 static struct nfs4_state *
1969 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1971 struct nfs4_state *state;
1973 if (!data->rpc_done) {
1974 state = nfs4_try_open_cached(data);
1975 trace_nfs4_cached_open(data->state);
1979 state = nfs4_opendata_find_nfs4_state(data);
1983 if (data->o_res.delegation_type != 0)
1984 nfs4_opendata_check_deleg(data, state);
1985 if (!update_open_stateid(state, &data->o_res.stateid,
1986 NULL, data->o_arg.fmode)) {
1987 nfs4_put_open_state(state);
1988 state = ERR_PTR(-EAGAIN);
1991 nfs_release_seqid(data->o_arg.seqid);
1995 static struct nfs4_state *
1996 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1998 struct nfs4_state *ret;
2000 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
2001 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
2003 ret = _nfs4_opendata_to_nfs4_state(data);
2004 nfs4_sequence_free_slot(&data->o_res.seq_res);
2008 static struct nfs_open_context *
2009 nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode)
2011 struct nfs_inode *nfsi = NFS_I(state->inode);
2012 struct nfs_open_context *ctx;
2015 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
2016 if (ctx->state != state)
2018 if ((ctx->mode & mode) != mode)
2020 if (!get_nfs_open_context(ctx))
2026 return ERR_PTR(-ENOENT);
2029 static struct nfs_open_context *
2030 nfs4_state_find_open_context(struct nfs4_state *state)
2032 struct nfs_open_context *ctx;
2034 ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE);
2037 ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE);
2040 return nfs4_state_find_open_context_mode(state, FMODE_READ);
2043 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
2044 struct nfs4_state *state, enum open_claim_type4 claim)
2046 struct nfs4_opendata *opendata;
2048 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
2049 NULL, claim, GFP_NOFS);
2050 if (opendata == NULL)
2051 return ERR_PTR(-ENOMEM);
2052 opendata->state = state;
2053 refcount_inc(&state->count);
2057 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
2060 struct nfs4_state *newstate;
2063 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
2065 opendata->o_arg.open_flags = 0;
2066 opendata->o_arg.fmode = fmode;
2067 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
2068 NFS_SB(opendata->dentry->d_sb),
2070 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
2071 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
2072 nfs4_init_opendata_res(opendata);
2073 ret = _nfs4_recover_proc_open(opendata);
2076 newstate = nfs4_opendata_to_nfs4_state(opendata);
2077 if (IS_ERR(newstate))
2078 return PTR_ERR(newstate);
2079 if (newstate != opendata->state)
2081 nfs4_close_state(newstate, fmode);
2085 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
2089 /* memory barrier prior to reading state->n_* */
2091 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2094 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2097 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2101 * We may have performed cached opens for all three recoveries.
2102 * Check if we need to update the current stateid.
2104 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
2105 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
2106 write_seqlock(&state->seqlock);
2107 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2108 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2109 write_sequnlock(&state->seqlock);
2116 * reclaim state on the server after a reboot.
2118 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2120 struct nfs_delegation *delegation;
2121 struct nfs4_opendata *opendata;
2122 fmode_t delegation_type = 0;
2125 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2126 NFS4_OPEN_CLAIM_PREVIOUS);
2127 if (IS_ERR(opendata))
2128 return PTR_ERR(opendata);
2130 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2131 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
2132 delegation_type = delegation->type;
2134 opendata->o_arg.u.delegation_type = delegation_type;
2135 status = nfs4_open_recover(opendata, state);
2136 nfs4_opendata_put(opendata);
2140 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2142 struct nfs_server *server = NFS_SERVER(state->inode);
2143 struct nfs4_exception exception = { };
2146 err = _nfs4_do_open_reclaim(ctx, state);
2147 trace_nfs4_open_reclaim(ctx, 0, err);
2148 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2150 if (err != -NFS4ERR_DELAY)
2152 nfs4_handle_exception(server, err, &exception);
2153 } while (exception.retry);
2157 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2159 struct nfs_open_context *ctx;
2162 ctx = nfs4_state_find_open_context(state);
2165 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2166 nfs_state_clear_open_state_flags(state);
2167 ret = nfs4_do_open_reclaim(ctx, state);
2168 put_nfs_open_context(ctx);
2172 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)
2176 printk(KERN_ERR "NFS: %s: unhandled error "
2177 "%d.\n", __func__, err);
2183 case -NFS4ERR_BADSESSION:
2184 case -NFS4ERR_BADSLOT:
2185 case -NFS4ERR_BAD_HIGH_SLOT:
2186 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2187 case -NFS4ERR_DEADSESSION:
2188 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
2190 case -NFS4ERR_STALE_CLIENTID:
2191 case -NFS4ERR_STALE_STATEID:
2192 /* Don't recall a delegation if it was lost */
2193 nfs4_schedule_lease_recovery(server->nfs_client);
2195 case -NFS4ERR_MOVED:
2196 nfs4_schedule_migration_recovery(server);
2198 case -NFS4ERR_LEASE_MOVED:
2199 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2201 case -NFS4ERR_DELEG_REVOKED:
2202 case -NFS4ERR_ADMIN_REVOKED:
2203 case -NFS4ERR_EXPIRED:
2204 case -NFS4ERR_BAD_STATEID:
2205 case -NFS4ERR_OPENMODE:
2206 nfs_inode_find_state_and_recover(state->inode,
2208 nfs4_schedule_stateid_recovery(server, state);
2210 case -NFS4ERR_DELAY:
2211 case -NFS4ERR_GRACE:
2215 case -NFS4ERR_DENIED:
2217 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2219 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2226 int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2227 struct nfs4_state *state, const nfs4_stateid *stateid)
2229 struct nfs_server *server = NFS_SERVER(state->inode);
2230 struct nfs4_opendata *opendata;
2233 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2234 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2235 if (IS_ERR(opendata))
2236 return PTR_ERR(opendata);
2237 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
2238 if (!test_bit(NFS_O_RDWR_STATE, &state->flags)) {
2239 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2243 if (!test_bit(NFS_O_WRONLY_STATE, &state->flags)) {
2244 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2248 if (!test_bit(NFS_O_RDONLY_STATE, &state->flags)) {
2249 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2253 nfs_state_clear_delegation(state);
2255 nfs4_opendata_put(opendata);
2256 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
2259 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2261 struct nfs4_opendata *data = calldata;
2263 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2264 &data->c_arg.seq_args, &data->c_res.seq_res, task);
2267 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2269 struct nfs4_opendata *data = calldata;
2271 nfs40_sequence_done(task, &data->c_res.seq_res);
2273 data->rpc_status = task->tk_status;
2274 if (data->rpc_status == 0) {
2275 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
2276 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2277 renew_lease(data->o_res.server, data->timestamp);
2278 data->rpc_done = true;
2282 static void nfs4_open_confirm_release(void *calldata)
2284 struct nfs4_opendata *data = calldata;
2285 struct nfs4_state *state = NULL;
2287 /* If this request hasn't been cancelled, do nothing */
2288 if (!data->cancelled)
2290 /* In case of error, no cleanup! */
2291 if (!data->rpc_done)
2293 state = nfs4_opendata_to_nfs4_state(data);
2295 nfs4_close_state(state, data->o_arg.fmode);
2297 nfs4_opendata_put(data);
2300 static const struct rpc_call_ops nfs4_open_confirm_ops = {
2301 .rpc_call_prepare = nfs4_open_confirm_prepare,
2302 .rpc_call_done = nfs4_open_confirm_done,
2303 .rpc_release = nfs4_open_confirm_release,
2307 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2309 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2311 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
2312 struct rpc_task *task;
2313 struct rpc_message msg = {
2314 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2315 .rpc_argp = &data->c_arg,
2316 .rpc_resp = &data->c_res,
2317 .rpc_cred = data->owner->so_cred,
2319 struct rpc_task_setup task_setup_data = {
2320 .rpc_client = server->client,
2321 .rpc_message = &msg,
2322 .callback_ops = &nfs4_open_confirm_ops,
2323 .callback_data = data,
2324 .workqueue = nfsiod_workqueue,
2325 .flags = RPC_TASK_ASYNC,
2329 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2331 kref_get(&data->kref);
2332 data->rpc_done = false;
2333 data->rpc_status = 0;
2334 data->timestamp = jiffies;
2335 task = rpc_run_task(&task_setup_data);
2337 return PTR_ERR(task);
2338 status = rpc_wait_for_completion_task(task);
2340 data->cancelled = true;
2343 status = data->rpc_status;
2348 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
2350 struct nfs4_opendata *data = calldata;
2351 struct nfs4_state_owner *sp = data->owner;
2352 struct nfs_client *clp = sp->so_server->nfs_client;
2353 enum open_claim_type4 claim = data->o_arg.claim;
2355 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
2358 * Check if we still need to send an OPEN call, or if we can use
2359 * a delegation instead.
2361 if (data->state != NULL) {
2362 struct nfs_delegation *delegation;
2364 if (can_open_cached(data->state, data->o_arg.fmode,
2365 data->o_arg.open_flags, claim))
2368 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
2369 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
2370 goto unlock_no_action;
2373 /* Update client id. */
2374 data->o_arg.clientid = clp->cl_clientid;
2378 case NFS4_OPEN_CLAIM_PREVIOUS:
2379 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2380 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
2381 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
2383 case NFS4_OPEN_CLAIM_FH:
2384 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
2386 data->timestamp = jiffies;
2387 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
2388 &data->o_arg.seq_args,
2389 &data->o_res.seq_res,
2391 nfs_release_seqid(data->o_arg.seqid);
2393 /* Set the create mode (note dependency on the session type) */
2394 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2395 if (data->o_arg.open_flags & O_EXCL) {
2396 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2397 if (nfs4_has_persistent_session(clp))
2398 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2399 else if (clp->cl_mvops->minor_version > 0)
2400 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2404 trace_nfs4_cached_open(data->state);
2407 task->tk_action = NULL;
2409 nfs4_sequence_done(task, &data->o_res.seq_res);
2412 static void nfs4_open_done(struct rpc_task *task, void *calldata)
2414 struct nfs4_opendata *data = calldata;
2416 data->rpc_status = task->tk_status;
2418 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
2421 if (task->tk_status == 0) {
2422 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2423 switch (data->o_res.f_attr->mode & S_IFMT) {
2427 data->rpc_status = -ELOOP;
2430 data->rpc_status = -EISDIR;
2433 data->rpc_status = -ENOTDIR;
2436 renew_lease(data->o_res.server, data->timestamp);
2437 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2438 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2440 data->rpc_done = true;
2443 static void nfs4_open_release(void *calldata)
2445 struct nfs4_opendata *data = calldata;
2446 struct nfs4_state *state = NULL;
2448 /* If this request hasn't been cancelled, do nothing */
2449 if (!data->cancelled)
2451 /* In case of error, no cleanup! */
2452 if (data->rpc_status != 0 || !data->rpc_done)
2454 /* In case we need an open_confirm, no cleanup! */
2455 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2457 state = nfs4_opendata_to_nfs4_state(data);
2459 nfs4_close_state(state, data->o_arg.fmode);
2461 nfs4_opendata_put(data);
2464 static const struct rpc_call_ops nfs4_open_ops = {
2465 .rpc_call_prepare = nfs4_open_prepare,
2466 .rpc_call_done = nfs4_open_done,
2467 .rpc_release = nfs4_open_release,
2470 static int nfs4_run_open_task(struct nfs4_opendata *data,
2471 struct nfs_open_context *ctx)
2473 struct inode *dir = d_inode(data->dir);
2474 struct nfs_server *server = NFS_SERVER(dir);
2475 struct nfs_openargs *o_arg = &data->o_arg;
2476 struct nfs_openres *o_res = &data->o_res;
2477 struct rpc_task *task;
2478 struct rpc_message msg = {
2479 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2482 .rpc_cred = data->owner->so_cred,
2484 struct rpc_task_setup task_setup_data = {
2485 .rpc_client = server->client,
2486 .rpc_message = &msg,
2487 .callback_ops = &nfs4_open_ops,
2488 .callback_data = data,
2489 .workqueue = nfsiod_workqueue,
2490 .flags = RPC_TASK_ASYNC,
2494 kref_get(&data->kref);
2495 data->rpc_done = false;
2496 data->rpc_status = 0;
2497 data->cancelled = false;
2498 data->is_recover = false;
2500 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
2501 data->is_recover = true;
2503 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
2504 pnfs_lgopen_prepare(data, ctx);
2506 task = rpc_run_task(&task_setup_data);
2508 return PTR_ERR(task);
2509 status = rpc_wait_for_completion_task(task);
2511 data->cancelled = true;
2514 status = data->rpc_status;
2520 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2522 struct inode *dir = d_inode(data->dir);
2523 struct nfs_openres *o_res = &data->o_res;
2526 status = nfs4_run_open_task(data, NULL);
2527 if (status != 0 || !data->rpc_done)
2530 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2532 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
2533 status = _nfs4_proc_open_confirm(data);
2539 * Additional permission checks in order to distinguish between an
2540 * open for read, and an open for execute. This works around the
2541 * fact that NFSv4 OPEN treats read and execute permissions as being
2543 * Note that in the non-execute case, we want to turn off permission
2544 * checking if we just created a new file (POSIX open() semantics).
2546 static int nfs4_opendata_access(const struct cred *cred,
2547 struct nfs4_opendata *opendata,
2548 struct nfs4_state *state, fmode_t fmode,
2551 struct nfs_access_entry cache;
2554 /* access call failed or for some reason the server doesn't
2555 * support any access modes -- defer access call until later */
2556 if (opendata->o_res.access_supported == 0)
2561 * Use openflags to check for exec, because fmode won't
2562 * always have FMODE_EXEC set when file open for exec.
2564 if (openflags & __FMODE_EXEC) {
2565 /* ONLY check for exec rights */
2566 if (S_ISDIR(state->inode->i_mode))
2567 mask = NFS4_ACCESS_LOOKUP;
2569 mask = NFS4_ACCESS_EXECUTE;
2570 } else if ((fmode & FMODE_READ) && !opendata->file_created)
2571 mask = NFS4_ACCESS_READ;
2574 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2575 nfs_access_add_cache(state->inode, &cache);
2577 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2578 if ((mask & ~cache.mask & flags) == 0)
2585 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2587 static int _nfs4_proc_open(struct nfs4_opendata *data,
2588 struct nfs_open_context *ctx)
2590 struct inode *dir = d_inode(data->dir);
2591 struct nfs_server *server = NFS_SERVER(dir);
2592 struct nfs_openargs *o_arg = &data->o_arg;
2593 struct nfs_openres *o_res = &data->o_res;
2596 status = nfs4_run_open_task(data, ctx);
2597 if (!data->rpc_done)
2600 if (status == -NFS4ERR_BADNAME &&
2601 !(o_arg->open_flags & O_CREAT))
2606 nfs_fattr_map_and_free_names(server, &data->f_attr);
2608 if (o_arg->open_flags & O_CREAT) {
2609 if (o_arg->open_flags & O_EXCL)
2610 data->file_created = true;
2611 else if (o_res->cinfo.before != o_res->cinfo.after)
2612 data->file_created = true;
2613 if (data->file_created ||
2614 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
2615 update_changeattr(dir, &o_res->cinfo,
2616 o_res->f_attr->time_start, 0);
2618 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2619 server->caps &= ~NFS_CAP_POSIX_LOCK;
2620 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2621 status = _nfs4_proc_open_confirm(data);
2625 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2626 nfs4_sequence_free_slot(&o_res->seq_res);
2627 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr,
2628 o_res->f_label, NULL);
2635 * reclaim state on the server after a network partition.
2636 * Assumes caller holds the appropriate lock
2638 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2640 struct nfs4_opendata *opendata;
2643 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2644 NFS4_OPEN_CLAIM_FH);
2645 if (IS_ERR(opendata))
2646 return PTR_ERR(opendata);
2647 ret = nfs4_open_recover(opendata, state);
2649 d_drop(ctx->dentry);
2650 nfs4_opendata_put(opendata);
2654 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2656 struct nfs_server *server = NFS_SERVER(state->inode);
2657 struct nfs4_exception exception = { };
2661 err = _nfs4_open_expired(ctx, state);
2662 trace_nfs4_open_expired(ctx, 0, err);
2663 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2668 case -NFS4ERR_GRACE:
2669 case -NFS4ERR_DELAY:
2670 nfs4_handle_exception(server, err, &exception);
2673 } while (exception.retry);
2678 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2680 struct nfs_open_context *ctx;
2683 ctx = nfs4_state_find_open_context(state);
2686 ret = nfs4_do_open_expired(ctx, state);
2687 put_nfs_open_context(ctx);
2691 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2692 const nfs4_stateid *stateid)
2694 nfs_remove_bad_delegation(state->inode, stateid);
2695 nfs_state_clear_delegation(state);
2698 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2700 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2701 nfs_finish_clear_delegation_stateid(state, NULL);
2704 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2706 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2707 nfs40_clear_delegation_stateid(state);
2708 nfs_state_clear_open_state_flags(state);
2709 return nfs4_open_expired(sp, state);
2712 static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2713 nfs4_stateid *stateid,
2714 const struct cred *cred)
2716 return -NFS4ERR_BAD_STATEID;
2719 #if defined(CONFIG_NFS_V4_1)
2720 static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2721 nfs4_stateid *stateid,
2722 const struct cred *cred)
2726 switch (stateid->type) {
2729 case NFS4_INVALID_STATEID_TYPE:
2730 case NFS4_SPECIAL_STATEID_TYPE:
2731 return -NFS4ERR_BAD_STATEID;
2732 case NFS4_REVOKED_STATEID_TYPE:
2736 status = nfs41_test_stateid(server, stateid, cred);
2738 case -NFS4ERR_EXPIRED:
2739 case -NFS4ERR_ADMIN_REVOKED:
2740 case -NFS4ERR_DELEG_REVOKED:
2746 /* Ack the revoked state to the server */
2747 nfs41_free_stateid(server, stateid, cred, true);
2748 return -NFS4ERR_EXPIRED;
2751 static int nfs41_check_delegation_stateid(struct nfs4_state *state)
2753 struct nfs_server *server = NFS_SERVER(state->inode);
2754 nfs4_stateid stateid;
2755 struct nfs_delegation *delegation;
2756 const struct cred *cred = NULL;
2757 int status, ret = NFS_OK;
2759 /* Get the delegation credential for use by test/free_stateid */
2761 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2762 if (delegation == NULL) {
2764 nfs_state_clear_delegation(state);
2768 nfs4_stateid_copy(&stateid, &delegation->stateid);
2770 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2771 &delegation->flags)) {
2776 if (delegation->cred)
2777 cred = get_cred(delegation->cred);
2779 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
2780 trace_nfs4_test_delegation_stateid(state, NULL, status);
2781 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
2782 nfs_finish_clear_delegation_stateid(state, &stateid);
2790 static void nfs41_delegation_recover_stateid(struct nfs4_state *state)
2794 if (test_bit(NFS_DELEGATED_STATE, &state->flags) &&
2795 nfs4_copy_delegation_stateid(state->inode, state->state,
2797 nfs4_stateid_match_other(&state->stateid, &tmp))
2798 nfs_state_set_delegation(state, &tmp, state->state);
2800 nfs_state_clear_delegation(state);
2804 * nfs41_check_expired_locks - possibly free a lock stateid
2806 * @state: NFSv4 state for an inode
2808 * Returns NFS_OK if recovery for this stateid is now finished.
2809 * Otherwise a negative NFS4ERR value is returned.
2811 static int nfs41_check_expired_locks(struct nfs4_state *state)
2813 int status, ret = NFS_OK;
2814 struct nfs4_lock_state *lsp, *prev = NULL;
2815 struct nfs_server *server = NFS_SERVER(state->inode);
2817 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2820 spin_lock(&state->state_lock);
2821 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2822 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2823 const struct cred *cred = lsp->ls_state->owner->so_cred;
2825 refcount_inc(&lsp->ls_count);
2826 spin_unlock(&state->state_lock);
2828 nfs4_put_lock_state(prev);
2831 status = nfs41_test_and_free_expired_stateid(server,
2834 trace_nfs4_test_lock_stateid(state, lsp, status);
2835 if (status == -NFS4ERR_EXPIRED ||
2836 status == -NFS4ERR_BAD_STATEID) {
2837 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
2838 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
2839 if (!recover_lost_locks)
2840 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2841 } else if (status != NFS_OK) {
2843 nfs4_put_lock_state(prev);
2846 spin_lock(&state->state_lock);
2849 spin_unlock(&state->state_lock);
2850 nfs4_put_lock_state(prev);
2856 * nfs41_check_open_stateid - possibly free an open stateid
2858 * @state: NFSv4 state for an inode
2860 * Returns NFS_OK if recovery for this stateid is now finished.
2861 * Otherwise a negative NFS4ERR value is returned.
2863 static int nfs41_check_open_stateid(struct nfs4_state *state)
2865 struct nfs_server *server = NFS_SERVER(state->inode);
2866 nfs4_stateid *stateid = &state->open_stateid;
2867 const struct cred *cred = state->owner->so_cred;
2870 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0)
2871 return -NFS4ERR_BAD_STATEID;
2872 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
2873 trace_nfs4_test_open_stateid(state, NULL, status);
2874 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
2875 nfs_state_clear_open_state_flags(state);
2876 stateid->type = NFS4_INVALID_STATEID_TYPE;
2879 if (nfs_open_stateid_recover_openmode(state))
2880 return -NFS4ERR_OPENMODE;
2884 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2888 status = nfs41_check_delegation_stateid(state);
2889 if (status != NFS_OK)
2891 nfs41_delegation_recover_stateid(state);
2893 status = nfs41_check_expired_locks(state);
2894 if (status != NFS_OK)
2896 status = nfs41_check_open_stateid(state);
2897 if (status != NFS_OK)
2898 status = nfs4_open_expired(sp, state);
2904 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2905 * fields corresponding to attributes that were used to store the verifier.
2906 * Make sure we clobber those fields in the later setattr call
2908 static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2909 struct iattr *sattr, struct nfs4_label **label)
2911 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2916 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
2917 attrset[i] = opendata->o_res.attrset[i];
2918 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
2919 attrset[i] &= ~bitmask[i];
2922 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
2923 sattr->ia_valid : 0;
2925 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
2926 if (sattr->ia_valid & ATTR_ATIME_SET)
2927 ret |= ATTR_ATIME_SET;
2932 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
2933 if (sattr->ia_valid & ATTR_MTIME_SET)
2934 ret |= ATTR_MTIME_SET;
2939 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
2944 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2945 int flags, struct nfs_open_context *ctx)
2947 struct nfs4_state_owner *sp = opendata->owner;
2948 struct nfs_server *server = sp->so_server;
2949 struct dentry *dentry;
2950 struct nfs4_state *state;
2951 fmode_t acc_mode = _nfs4_ctx_to_accessmode(ctx);
2955 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2957 ret = _nfs4_proc_open(opendata, ctx);
2961 state = _nfs4_opendata_to_nfs4_state(opendata);
2962 ret = PTR_ERR(state);
2966 if (server->caps & NFS_CAP_POSIX_LOCK)
2967 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2968 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2969 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
2971 dentry = opendata->dentry;
2972 if (d_really_is_negative(dentry)) {
2973 struct dentry *alias;
2975 alias = d_exact_alias(dentry, state->inode);
2977 alias = d_splice_alias(igrab(state->inode), dentry);
2978 /* d_splice_alias() can't fail here - it's a non-directory */
2981 ctx->dentry = dentry = alias;
2983 nfs_set_verifier(dentry,
2984 nfs_save_change_attribute(d_inode(opendata->dir)));
2987 /* Parse layoutget results before we check for access */
2988 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
2990 ret = nfs4_opendata_access(sp->so_cred, opendata, state,
2995 if (d_inode(dentry) == state->inode) {
2996 nfs_inode_attach_open_context(ctx);
2997 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2998 nfs4_schedule_stateid_recovery(server, state);
3002 if (!opendata->cancelled)
3003 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
3008 * Returns a referenced nfs4_state
3010 static int _nfs4_do_open(struct inode *dir,
3011 struct nfs_open_context *ctx,
3013 const struct nfs4_open_createattrs *c,
3016 struct nfs4_state_owner *sp;
3017 struct nfs4_state *state = NULL;
3018 struct nfs_server *server = NFS_SERVER(dir);
3019 struct nfs4_opendata *opendata;
3020 struct dentry *dentry = ctx->dentry;
3021 const struct cred *cred = ctx->cred;
3022 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
3023 fmode_t fmode = _nfs4_ctx_to_openmode(ctx);
3024 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
3025 struct iattr *sattr = c->sattr;
3026 struct nfs4_label *label = c->label;
3027 struct nfs4_label *olabel = NULL;
3030 /* Protect against reboot recovery conflicts */
3032 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
3034 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
3037 status = nfs4_client_recover_expired_lease(server->nfs_client);
3039 goto err_put_state_owner;
3040 if (d_really_is_positive(dentry))
3041 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
3043 if (d_really_is_positive(dentry))
3044 claim = NFS4_OPEN_CLAIM_FH;
3045 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
3046 c, claim, GFP_KERNEL);
3047 if (opendata == NULL)
3048 goto err_put_state_owner;
3051 olabel = nfs4_label_alloc(server, GFP_KERNEL);
3052 if (IS_ERR(olabel)) {
3053 status = PTR_ERR(olabel);
3054 goto err_opendata_put;
3058 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
3059 if (!opendata->f_attr.mdsthreshold) {
3060 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
3061 if (!opendata->f_attr.mdsthreshold)
3062 goto err_free_label;
3064 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
3066 if (d_really_is_positive(dentry))
3067 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
3069 status = _nfs4_open_and_get_state(opendata, flags, ctx);
3071 goto err_free_label;
3074 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
3075 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
3076 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
3078 * send create attributes which was not set by open
3079 * with an extra setattr.
3081 if (attrs || label) {
3082 unsigned ia_old = sattr->ia_valid;
3084 sattr->ia_valid = attrs;
3085 nfs_fattr_init(opendata->o_res.f_attr);
3086 status = nfs4_do_setattr(state->inode, cred,
3087 opendata->o_res.f_attr, sattr,
3088 ctx, label, olabel);
3090 nfs_setattr_update_inode(state->inode, sattr,
3091 opendata->o_res.f_attr);
3092 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
3094 sattr->ia_valid = ia_old;
3097 if (opened && opendata->file_created)
3100 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
3101 *ctx_th = opendata->f_attr.mdsthreshold;
3102 opendata->f_attr.mdsthreshold = NULL;
3105 nfs4_label_free(olabel);
3107 nfs4_opendata_put(opendata);
3108 nfs4_put_state_owner(sp);
3111 nfs4_label_free(olabel);
3113 nfs4_opendata_put(opendata);
3114 err_put_state_owner:
3115 nfs4_put_state_owner(sp);
3121 static struct nfs4_state *nfs4_do_open(struct inode *dir,
3122 struct nfs_open_context *ctx,
3124 struct iattr *sattr,
3125 struct nfs4_label *label,
3128 struct nfs_server *server = NFS_SERVER(dir);
3129 struct nfs4_exception exception = {
3130 .interruptible = true,
3132 struct nfs4_state *res;
3133 struct nfs4_open_createattrs c = {
3137 [0] = (__u32)jiffies,
3138 [1] = (__u32)current->pid,
3144 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
3146 trace_nfs4_open_file(ctx, flags, status);
3149 /* NOTE: BAD_SEQID means the server and client disagree about the
3150 * book-keeping w.r.t. state-changing operations
3151 * (OPEN/CLOSE/LOCK/LOCKU...)
3152 * It is actually a sign of a bug on the client or on the server.
3154 * If we receive a BAD_SEQID error in the particular case of
3155 * doing an OPEN, we assume that nfs_increment_open_seqid() will
3156 * have unhashed the old state_owner for us, and that we can
3157 * therefore safely retry using a new one. We should still warn
3158 * the user though...
3160 if (status == -NFS4ERR_BAD_SEQID) {
3161 pr_warn_ratelimited("NFS: v4 server %s "
3162 " returned a bad sequence-id error!\n",
3163 NFS_SERVER(dir)->nfs_client->cl_hostname);
3164 exception.retry = 1;
3168 * BAD_STATEID on OPEN means that the server cancelled our
3169 * state before it received the OPEN_CONFIRM.
3170 * Recover by retrying the request as per the discussion
3171 * on Page 181 of RFC3530.
3173 if (status == -NFS4ERR_BAD_STATEID) {
3174 exception.retry = 1;
3177 if (status == -EAGAIN) {
3178 /* We must have found a delegation */
3179 exception.retry = 1;
3182 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3184 res = ERR_PTR(nfs4_handle_exception(server,
3185 status, &exception));
3186 } while (exception.retry);
3190 static int _nfs4_do_setattr(struct inode *inode,
3191 struct nfs_setattrargs *arg,
3192 struct nfs_setattrres *res,
3193 const struct cred *cred,
3194 struct nfs_open_context *ctx)
3196 struct nfs_server *server = NFS_SERVER(inode);
3197 struct rpc_message msg = {
3198 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3203 const struct cred *delegation_cred = NULL;
3204 unsigned long timestamp = jiffies;
3208 nfs_fattr_init(res->fattr);
3210 /* Servers should only apply open mode checks for file size changes */
3211 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
3215 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
3216 /* Use that stateid */
3217 } else if (ctx != NULL && ctx->state) {
3218 struct nfs_lock_context *l_ctx;
3219 if (!nfs4_valid_open_stateid(ctx->state))
3221 l_ctx = nfs_get_lock_context(ctx);
3223 return PTR_ERR(l_ctx);
3224 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3225 &arg->stateid, &delegation_cred);
3226 nfs_put_lock_context(l_ctx);
3231 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
3233 if (delegation_cred)
3234 msg.rpc_cred = delegation_cred;
3236 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3238 put_cred(delegation_cred);
3239 if (status == 0 && ctx != NULL)
3240 renew_lease(server, timestamp);
3241 trace_nfs4_setattr(inode, &arg->stateid, status);
3245 static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
3246 struct nfs_fattr *fattr, struct iattr *sattr,
3247 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
3248 struct nfs4_label *olabel)
3250 struct nfs_server *server = NFS_SERVER(inode);
3251 __u32 bitmask[NFS4_BITMASK_SZ];
3252 struct nfs4_state *state = ctx ? ctx->state : NULL;
3253 struct nfs_setattrargs arg = {
3254 .fh = NFS_FH(inode),
3260 struct nfs_setattrres res = {
3265 struct nfs4_exception exception = {
3268 .stateid = &arg.stateid,
3273 nfs4_bitmap_copy_adjust_setattr(bitmask,
3274 nfs4_bitmask(server, olabel),
3277 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
3279 case -NFS4ERR_OPENMODE:
3280 if (!(sattr->ia_valid & ATTR_SIZE)) {
3281 pr_warn_once("NFSv4: server %s is incorrectly "
3282 "applying open mode checks to "
3283 "a SETATTR that is not "
3284 "changing file size.\n",
3285 server->nfs_client->cl_hostname);
3287 if (state && !(state->state & FMODE_WRITE)) {
3289 if (sattr->ia_valid & ATTR_OPEN)
3294 err = nfs4_handle_exception(server, err, &exception);
3295 } while (exception.retry);
3301 nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3303 if (inode == NULL || !nfs_have_layout(inode))
3306 return pnfs_wait_on_layoutreturn(inode, task);
3309 struct nfs4_closedata {
3310 struct inode *inode;
3311 struct nfs4_state *state;
3312 struct nfs_closeargs arg;
3313 struct nfs_closeres res;
3315 struct nfs4_layoutreturn_args arg;
3316 struct nfs4_layoutreturn_res res;
3317 struct nfs4_xdr_opaque_data ld_private;
3321 struct nfs_fattr fattr;
3322 unsigned long timestamp;
3325 static void nfs4_free_closedata(void *data)
3327 struct nfs4_closedata *calldata = data;
3328 struct nfs4_state_owner *sp = calldata->state->owner;
3329 struct super_block *sb = calldata->state->inode->i_sb;
3331 if (calldata->lr.roc)
3332 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3333 calldata->res.lr_ret);
3334 nfs4_put_open_state(calldata->state);
3335 nfs_free_seqid(calldata->arg.seqid);
3336 nfs4_put_state_owner(sp);
3337 nfs_sb_deactive(sb);
3341 static void nfs4_close_done(struct rpc_task *task, void *data)
3343 struct nfs4_closedata *calldata = data;
3344 struct nfs4_state *state = calldata->state;
3345 struct nfs_server *server = NFS_SERVER(calldata->inode);
3346 nfs4_stateid *res_stateid = NULL;
3347 struct nfs4_exception exception = {
3349 .inode = calldata->inode,
3350 .stateid = &calldata->arg.stateid,
3353 dprintk("%s: begin!\n", __func__);
3354 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3356 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
3358 /* Handle Layoutreturn errors */
3359 if (calldata->arg.lr_args && task->tk_status != 0) {
3360 switch (calldata->res.lr_ret) {
3362 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3365 calldata->arg.lr_args = NULL;
3366 calldata->res.lr_res = NULL;
3368 case -NFS4ERR_OLD_STATEID:
3369 if (nfs4_layoutreturn_refresh_stateid(&calldata->arg.lr_args->stateid,
3370 &calldata->arg.lr_args->range,
3374 case -NFS4ERR_ADMIN_REVOKED:
3375 case -NFS4ERR_DELEG_REVOKED:
3376 case -NFS4ERR_EXPIRED:
3377 case -NFS4ERR_BAD_STATEID:
3378 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3379 case -NFS4ERR_WRONG_CRED:
3380 calldata->arg.lr_args = NULL;
3381 calldata->res.lr_res = NULL;
3386 /* hmm. we are done with the inode, and in the process of freeing
3387 * the state_owner. we keep this around to process errors
3389 switch (task->tk_status) {
3391 res_stateid = &calldata->res.stateid;
3392 renew_lease(server, calldata->timestamp);
3394 case -NFS4ERR_ACCESS:
3395 if (calldata->arg.bitmask != NULL) {
3396 calldata->arg.bitmask = NULL;
3397 calldata->res.fattr = NULL;
3402 case -NFS4ERR_OLD_STATEID:
3403 /* Did we race with OPEN? */
3404 if (nfs4_refresh_open_stateid(&calldata->arg.stateid,
3408 case -NFS4ERR_ADMIN_REVOKED:
3409 case -NFS4ERR_STALE_STATEID:
3410 case -NFS4ERR_EXPIRED:
3411 nfs4_free_revoked_stateid(server,
3412 &calldata->arg.stateid,
3413 task->tk_msg.rpc_cred);
3415 case -NFS4ERR_BAD_STATEID:
3418 task->tk_status = nfs4_async_handle_exception(task,
3419 server, task->tk_status, &exception);
3420 if (exception.retry)
3423 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3424 res_stateid, calldata->arg.fmode);
3426 task->tk_status = 0;
3427 nfs_release_seqid(calldata->arg.seqid);
3428 nfs_refresh_inode(calldata->inode, &calldata->fattr);
3429 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
3432 calldata->res.lr_ret = 0;
3434 task->tk_status = 0;
3435 rpc_restart_call_prepare(task);
3439 static void nfs4_close_prepare(struct rpc_task *task, void *data)
3441 struct nfs4_closedata *calldata = data;
3442 struct nfs4_state *state = calldata->state;
3443 struct inode *inode = calldata->inode;
3444 struct pnfs_layout_hdr *lo;
3445 bool is_rdonly, is_wronly, is_rdwr;
3448 dprintk("%s: begin!\n", __func__);
3449 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3452 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
3453 spin_lock(&state->owner->so_lock);
3454 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3455 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3456 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
3457 /* Calculate the change in open mode */
3458 calldata->arg.fmode = 0;
3459 if (state->n_rdwr == 0) {
3460 if (state->n_rdonly == 0)
3461 call_close |= is_rdonly;
3463 calldata->arg.fmode |= FMODE_READ;
3464 if (state->n_wronly == 0)
3465 call_close |= is_wronly;
3467 calldata->arg.fmode |= FMODE_WRITE;
3468 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3469 call_close |= is_rdwr;
3471 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3473 if (!nfs4_valid_open_stateid(state) ||
3474 !nfs4_refresh_open_stateid(&calldata->arg.stateid, state))
3476 spin_unlock(&state->owner->so_lock);
3479 /* Note: exit _without_ calling nfs4_close_done */
3483 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
3484 nfs_release_seqid(calldata->arg.seqid);
3488 lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3489 if (lo && !pnfs_layout_is_valid(lo)) {
3490 calldata->arg.lr_args = NULL;
3491 calldata->res.lr_res = NULL;
3494 if (calldata->arg.fmode == 0)
3495 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
3497 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
3498 /* Close-to-open cache consistency revalidation */
3499 if (!nfs4_have_delegation(inode, FMODE_READ))
3500 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3502 calldata->arg.bitmask = NULL;
3505 calldata->arg.share_access =
3506 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3507 calldata->arg.fmode, 0);
3509 if (calldata->res.fattr == NULL)
3510 calldata->arg.bitmask = NULL;
3511 else if (calldata->arg.bitmask == NULL)
3512 calldata->res.fattr = NULL;
3513 calldata->timestamp = jiffies;
3514 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
3515 &calldata->arg.seq_args,
3516 &calldata->res.seq_res,
3518 nfs_release_seqid(calldata->arg.seqid);
3519 dprintk("%s: done!\n", __func__);
3522 task->tk_action = NULL;
3524 nfs4_sequence_done(task, &calldata->res.seq_res);
3527 static const struct rpc_call_ops nfs4_close_ops = {
3528 .rpc_call_prepare = nfs4_close_prepare,
3529 .rpc_call_done = nfs4_close_done,
3530 .rpc_release = nfs4_free_closedata,
3534 * It is possible for data to be read/written from a mem-mapped file
3535 * after the sys_close call (which hits the vfs layer as a flush).
3536 * This means that we can't safely call nfsv4 close on a file until
3537 * the inode is cleared. This in turn means that we are not good
3538 * NFSv4 citizens - we do not indicate to the server to update the file's
3539 * share state even when we are done with one of the three share
3540 * stateid's in the inode.
3542 * NOTE: Caller must be holding the sp->so_owner semaphore!
3544 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
3546 struct nfs_server *server = NFS_SERVER(state->inode);
3547 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
3548 struct nfs4_closedata *calldata;
3549 struct nfs4_state_owner *sp = state->owner;
3550 struct rpc_task *task;
3551 struct rpc_message msg = {
3552 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3553 .rpc_cred = state->owner->so_cred,
3555 struct rpc_task_setup task_setup_data = {
3556 .rpc_client = server->client,
3557 .rpc_message = &msg,
3558 .callback_ops = &nfs4_close_ops,
3559 .workqueue = nfsiod_workqueue,
3560 .flags = RPC_TASK_ASYNC,
3562 int status = -ENOMEM;
3564 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3565 &task_setup_data.rpc_client, &msg);
3567 calldata = kzalloc(sizeof(*calldata), gfp_mask);
3568 if (calldata == NULL)
3570 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
3571 calldata->inode = state->inode;
3572 calldata->state = state;
3573 calldata->arg.fh = NFS_FH(state->inode);
3574 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3575 goto out_free_calldata;
3576 /* Serialization for the sequence id */
3577 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3578 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
3579 if (IS_ERR(calldata->arg.seqid))
3580 goto out_free_calldata;
3581 nfs_fattr_init(&calldata->fattr);
3582 calldata->arg.fmode = 0;
3583 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
3584 calldata->res.fattr = &calldata->fattr;
3585 calldata->res.seqid = calldata->arg.seqid;
3586 calldata->res.server = server;
3587 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3588 calldata->lr.roc = pnfs_roc(state->inode,
3589 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3590 if (calldata->lr.roc) {
3591 calldata->arg.lr_args = &calldata->lr.arg;
3592 calldata->res.lr_res = &calldata->lr.res;
3594 nfs_sb_active(calldata->inode->i_sb);
3596 msg.rpc_argp = &calldata->arg;
3597 msg.rpc_resp = &calldata->res;
3598 task_setup_data.callback_data = calldata;
3599 task = rpc_run_task(&task_setup_data);
3601 return PTR_ERR(task);
3604 status = rpc_wait_for_completion_task(task);
3610 nfs4_put_open_state(state);
3611 nfs4_put_state_owner(sp);
3615 static struct inode *
3616 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3617 int open_flags, struct iattr *attr, int *opened)
3619 struct nfs4_state *state;
3620 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3622 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
3624 /* Protect against concurrent sillydeletes */
3625 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
3627 nfs4_label_release_security(label);
3630 return ERR_CAST(state);
3631 return state->inode;
3634 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
3636 if (ctx->state == NULL)
3639 nfs4_close_sync(ctx->state, _nfs4_ctx_to_openmode(ctx));
3641 nfs4_close_state(ctx->state, _nfs4_ctx_to_openmode(ctx));
3644 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3645 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3646 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
3648 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3650 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
3651 struct nfs4_server_caps_arg args = {
3655 struct nfs4_server_caps_res res = {};
3656 struct rpc_message msg = {
3657 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
3664 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3665 FATTR4_WORD0_FH_EXPIRE_TYPE |
3666 FATTR4_WORD0_LINK_SUPPORT |
3667 FATTR4_WORD0_SYMLINK_SUPPORT |
3668 FATTR4_WORD0_ACLSUPPORT;
3670 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3672 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3674 /* Sanity check the server answers */
3675 switch (minorversion) {
3677 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3678 res.attr_bitmask[2] = 0;
3681 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3684 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3686 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
3687 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3688 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3689 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3690 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
3691 NFS_CAP_CTIME|NFS_CAP_MTIME|
3692 NFS_CAP_SECURITY_LABEL);
3693 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3694 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3695 server->caps |= NFS_CAP_ACLS;
3696 if (res.has_links != 0)
3697 server->caps |= NFS_CAP_HARDLINKS;
3698 if (res.has_symlinks != 0)
3699 server->caps |= NFS_CAP_SYMLINKS;
3700 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3701 server->caps |= NFS_CAP_FILEID;
3702 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3703 server->caps |= NFS_CAP_MODE;
3704 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3705 server->caps |= NFS_CAP_NLINK;
3706 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3707 server->caps |= NFS_CAP_OWNER;
3708 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3709 server->caps |= NFS_CAP_OWNER_GROUP;
3710 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3711 server->caps |= NFS_CAP_ATIME;
3712 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3713 server->caps |= NFS_CAP_CTIME;
3714 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3715 server->caps |= NFS_CAP_MTIME;
3716 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3717 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3718 server->caps |= NFS_CAP_SECURITY_LABEL;
3720 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3721 sizeof(server->attr_bitmask));
3722 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3724 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3725 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3726 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
3727 server->cache_consistency_bitmask[2] = 0;
3729 /* Avoid a regression due to buggy server */
3730 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3731 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
3732 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3733 sizeof(server->exclcreat_bitmask));
3735 server->acl_bitmask = res.acl_bitmask;
3736 server->fh_expire_type = res.fh_expire_type;
3742 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3744 struct nfs4_exception exception = {
3745 .interruptible = true,
3749 err = nfs4_handle_exception(server,
3750 _nfs4_server_capabilities(server, fhandle),
3752 } while (exception.retry);
3756 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3757 struct nfs_fsinfo *info)
3760 struct nfs4_lookup_root_arg args = {
3763 struct nfs4_lookup_res res = {
3765 .fattr = info->fattr,
3768 struct rpc_message msg = {
3769 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3774 bitmask[0] = nfs4_fattr_bitmap[0];
3775 bitmask[1] = nfs4_fattr_bitmap[1];
3777 * Process the label in the upcoming getfattr
3779 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3781 nfs_fattr_init(info->fattr);
3782 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3785 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3786 struct nfs_fsinfo *info)
3788 struct nfs4_exception exception = {
3789 .interruptible = true,
3793 err = _nfs4_lookup_root(server, fhandle, info);
3794 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
3797 case -NFS4ERR_WRONGSEC:
3800 err = nfs4_handle_exception(server, err, &exception);
3802 } while (exception.retry);
3807 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3808 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3810 struct rpc_auth_create_args auth_args = {
3811 .pseudoflavor = flavor,
3813 struct rpc_auth *auth;
3815 auth = rpcauth_create(&auth_args, server->client);
3818 return nfs4_lookup_root(server, fhandle, info);
3822 * Retry pseudoroot lookup with various security flavors. We do this when:
3824 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3825 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3827 * Returns zero on success, or a negative NFS4ERR value, or a
3828 * negative errno value.
3830 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3831 struct nfs_fsinfo *info)
3833 /* Per 3530bis 15.33.5 */
3834 static const rpc_authflavor_t flav_array[] = {
3838 RPC_AUTH_UNIX, /* courtesy */
3841 int status = -EPERM;
3844 if (server->auth_info.flavor_len > 0) {
3845 /* try each flavor specified by user */
3846 for (i = 0; i < server->auth_info.flavor_len; i++) {
3847 status = nfs4_lookup_root_sec(server, fhandle, info,
3848 server->auth_info.flavors[i]);
3849 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3854 /* no flavors specified by user, try default list */
3855 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3856 status = nfs4_lookup_root_sec(server, fhandle, info,
3858 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3865 * -EACCES could mean that the user doesn't have correct permissions
3866 * to access the mount. It could also mean that we tried to mount
3867 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3868 * existing mount programs don't handle -EACCES very well so it should
3869 * be mapped to -EPERM instead.
3871 if (status == -EACCES)
3877 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3878 * @server: initialized nfs_server handle
3879 * @fhandle: we fill in the pseudo-fs root file handle
3880 * @info: we fill in an FSINFO struct
3881 * @auth_probe: probe the auth flavours
3883 * Returns zero on success, or a negative errno.
3885 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
3886 struct nfs_fsinfo *info,
3892 status = nfs4_lookup_root(server, fhandle, info);
3894 if (auth_probe || status == NFS4ERR_WRONGSEC)
3895 status = server->nfs_client->cl_mvops->find_root_sec(server,
3899 status = nfs4_server_capabilities(server, fhandle);
3901 status = nfs4_do_fsinfo(server, fhandle, info);
3903 return nfs4_map_errors(status);
3906 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3907 struct nfs_fsinfo *info)
3910 struct nfs_fattr *fattr = info->fattr;
3911 struct nfs4_label *label = NULL;
3913 error = nfs4_server_capabilities(server, mntfh);
3915 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3919 label = nfs4_label_alloc(server, GFP_KERNEL);
3921 return PTR_ERR(label);
3923 error = nfs4_proc_getattr(server, mntfh, fattr, label, NULL);
3925 dprintk("nfs4_get_root: getattr error = %d\n", -error);
3926 goto err_free_label;
3929 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3930 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3931 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3934 nfs4_label_free(label);
3940 * Get locations and (maybe) other attributes of a referral.
3941 * Note that we'll actually follow the referral later when
3942 * we detect fsid mismatch in inode revalidation
3944 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3945 const struct qstr *name, struct nfs_fattr *fattr,
3946 struct nfs_fh *fhandle)
3948 int status = -ENOMEM;
3949 struct page *page = NULL;
3950 struct nfs4_fs_locations *locations = NULL;
3952 page = alloc_page(GFP_KERNEL);
3955 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3956 if (locations == NULL)
3959 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
3964 * If the fsid didn't change, this is a migration event, not a
3965 * referral. Cause us to drop into the exception handler, which
3966 * will kick off migration recovery.
3968 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
3969 dprintk("%s: server did not return a different fsid for"
3970 " a referral at %s\n", __func__, name->name);
3971 status = -NFS4ERR_MOVED;
3974 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3975 nfs_fixup_referral_attributes(&locations->fattr);
3977 /* replace the lookup nfs_fattr with the locations nfs_fattr */
3978 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
3979 memset(fhandle, 0, sizeof(struct nfs_fh));
3987 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3988 struct nfs_fattr *fattr, struct nfs4_label *label,
3989 struct inode *inode)
3991 __u32 bitmask[NFS4_BITMASK_SZ];
3992 struct nfs4_getattr_arg args = {
3996 struct nfs4_getattr_res res = {
4001 struct rpc_message msg = {
4002 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4007 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode);
4009 nfs_fattr_init(fattr);
4010 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4013 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4014 struct nfs_fattr *fattr, struct nfs4_label *label,
4015 struct inode *inode)
4017 struct nfs4_exception exception = {
4018 .interruptible = true,
4022 err = _nfs4_proc_getattr(server, fhandle, fattr, label, inode);
4023 trace_nfs4_getattr(server, fhandle, fattr, err);
4024 err = nfs4_handle_exception(server, err,
4026 } while (exception.retry);
4031 * The file is not closed if it is opened due to the a request to change
4032 * the size of the file. The open call will not be needed once the
4033 * VFS layer lookup-intents are implemented.
4035 * Close is called when the inode is destroyed.
4036 * If we haven't opened the file for O_WRONLY, we
4037 * need to in the size_change case to obtain a stateid.
4040 * Because OPEN is always done by name in nfsv4, it is
4041 * possible that we opened a different file by the same
4042 * name. We can recognize this race condition, but we
4043 * can't do anything about it besides returning an error.
4045 * This will be fixed with VFS changes (lookup-intent).
4048 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
4049 struct iattr *sattr)
4051 struct inode *inode = d_inode(dentry);
4052 const struct cred *cred = NULL;
4053 struct nfs_open_context *ctx = NULL;
4054 struct nfs4_label *label = NULL;
4057 if (pnfs_ld_layoutret_on_setattr(inode) &&
4058 sattr->ia_valid & ATTR_SIZE &&
4059 sattr->ia_size < i_size_read(inode))
4060 pnfs_commit_and_return_layout(inode);
4062 nfs_fattr_init(fattr);
4064 /* Deal with open(O_TRUNC) */
4065 if (sattr->ia_valid & ATTR_OPEN)
4066 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
4068 /* Optimization: if the end result is no change, don't RPC */
4069 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
4072 /* Search for an existing open(O_WRITE) file */
4073 if (sattr->ia_valid & ATTR_FILE) {
4075 ctx = nfs_file_open_context(sattr->ia_file);
4080 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4082 return PTR_ERR(label);
4084 /* Return any delegations if we're going to change ACLs */
4085 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
4086 nfs4_inode_make_writeable(inode);
4088 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
4090 nfs_setattr_update_inode(inode, sattr, fattr);
4091 nfs_setsecurity(inode, fattr, label);
4093 nfs4_label_free(label);
4097 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
4098 const struct qstr *name, struct nfs_fh *fhandle,
4099 struct nfs_fattr *fattr, struct nfs4_label *label)
4101 struct nfs_server *server = NFS_SERVER(dir);
4103 struct nfs4_lookup_arg args = {
4104 .bitmask = server->attr_bitmask,
4105 .dir_fh = NFS_FH(dir),
4108 struct nfs4_lookup_res res = {
4114 struct rpc_message msg = {
4115 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4120 args.bitmask = nfs4_bitmask(server, label);
4122 nfs_fattr_init(fattr);
4124 dprintk("NFS call lookup %s\n", name->name);
4125 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
4126 dprintk("NFS reply lookup: %d\n", status);
4130 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
4132 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4133 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
4134 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4138 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
4139 const struct qstr *name, struct nfs_fh *fhandle,
4140 struct nfs_fattr *fattr, struct nfs4_label *label)
4142 struct nfs4_exception exception = {
4143 .interruptible = true,
4145 struct rpc_clnt *client = *clnt;
4148 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
4149 trace_nfs4_lookup(dir, name, err);
4151 case -NFS4ERR_BADNAME:
4154 case -NFS4ERR_MOVED:
4155 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
4156 if (err == -NFS4ERR_MOVED)
4157 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4159 case -NFS4ERR_WRONGSEC:
4161 if (client != *clnt)
4163 client = nfs4_negotiate_security(client, dir, name);
4165 return PTR_ERR(client);
4167 exception.retry = 1;
4170 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4172 } while (exception.retry);
4177 else if (client != *clnt)
4178 rpc_shutdown_client(client);
4183 static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
4184 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4185 struct nfs4_label *label)
4188 struct rpc_clnt *client = NFS_CLIENT(dir);
4190 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
4191 if (client != NFS_CLIENT(dir)) {
4192 rpc_shutdown_client(client);
4193 nfs_fixup_secinfo_attributes(fattr);
4199 nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
4200 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4202 struct rpc_clnt *client = NFS_CLIENT(dir);
4205 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
4207 return ERR_PTR(status);
4208 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
4211 static int _nfs4_proc_lookupp(struct inode *inode,
4212 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4213 struct nfs4_label *label)
4215 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4216 struct nfs_server *server = NFS_SERVER(inode);
4218 struct nfs4_lookupp_arg args = {
4219 .bitmask = server->attr_bitmask,
4220 .fh = NFS_FH(inode),
4222 struct nfs4_lookupp_res res = {
4228 struct rpc_message msg = {
4229 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4234 args.bitmask = nfs4_bitmask(server, label);
4236 nfs_fattr_init(fattr);
4238 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4239 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4241 dprintk("NFS reply lookupp: %d\n", status);
4245 static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4246 struct nfs_fattr *fattr, struct nfs4_label *label)
4248 struct nfs4_exception exception = {
4249 .interruptible = true,
4253 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4254 trace_nfs4_lookupp(inode, err);
4255 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4257 } while (exception.retry);
4261 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4263 struct nfs_server *server = NFS_SERVER(inode);
4264 struct nfs4_accessargs args = {
4265 .fh = NFS_FH(inode),
4266 .access = entry->mask,
4268 struct nfs4_accessres res = {
4271 struct rpc_message msg = {
4272 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4275 .rpc_cred = entry->cred,
4279 if (!nfs4_have_delegation(inode, FMODE_READ)) {
4280 res.fattr = nfs_alloc_fattr();
4281 if (res.fattr == NULL)
4283 args.bitmask = server->cache_consistency_bitmask;
4285 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4287 nfs_access_set_mask(entry, res.access);
4289 nfs_refresh_inode(inode, res.fattr);
4291 nfs_free_fattr(res.fattr);
4295 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4297 struct nfs4_exception exception = {
4298 .interruptible = true,
4302 err = _nfs4_proc_access(inode, entry);
4303 trace_nfs4_access(inode, err);
4304 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4306 } while (exception.retry);
4311 * TODO: For the time being, we don't try to get any attributes
4312 * along with any of the zero-copy operations READ, READDIR,
4315 * In the case of the first three, we want to put the GETATTR
4316 * after the read-type operation -- this is because it is hard
4317 * to predict the length of a GETATTR response in v4, and thus
4318 * align the READ data correctly. This means that the GETATTR
4319 * may end up partially falling into the page cache, and we should
4320 * shift it into the 'tail' of the xdr_buf before processing.
4321 * To do this efficiently, we need to know the total length
4322 * of data received, which doesn't seem to be available outside
4325 * In the case of WRITE, we also want to put the GETATTR after
4326 * the operation -- in this case because we want to make sure
4327 * we get the post-operation mtime and size.
4329 * Both of these changes to the XDR layer would in fact be quite
4330 * minor, but I decided to leave them for a subsequent patch.
4332 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4333 unsigned int pgbase, unsigned int pglen)
4335 struct nfs4_readlink args = {
4336 .fh = NFS_FH(inode),
4341 struct nfs4_readlink_res res;
4342 struct rpc_message msg = {
4343 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4348 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
4351 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4352 unsigned int pgbase, unsigned int pglen)
4354 struct nfs4_exception exception = {
4355 .interruptible = true,
4359 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4360 trace_nfs4_readlink(inode, err);
4361 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4363 } while (exception.retry);
4368 * This is just for mknod. open(O_CREAT) will always do ->open_context().
4371 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
4374 struct nfs_server *server = NFS_SERVER(dir);
4375 struct nfs4_label l, *ilabel = NULL;
4376 struct nfs_open_context *ctx;
4377 struct nfs4_state *state;
4380 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
4382 return PTR_ERR(ctx);
4384 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4386 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4387 sattr->ia_mode &= ~current_umask();
4388 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
4389 if (IS_ERR(state)) {
4390 status = PTR_ERR(state);
4394 nfs4_label_release_security(ilabel);
4395 put_nfs_open_context(ctx);
4400 _nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
4402 struct nfs_server *server = NFS_SERVER(dir);
4403 struct nfs_removeargs args = {
4407 struct nfs_removeres res = {
4410 struct rpc_message msg = {
4411 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4415 unsigned long timestamp = jiffies;
4418 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
4420 spin_lock(&dir->i_lock);
4421 update_changeattr_locked(dir, &res.cinfo, timestamp, 0);
4422 /* Removing a directory decrements nlink in the parent */
4423 if (ftype == NF4DIR && dir->i_nlink > 2)
4424 nfs4_dec_nlink_locked(dir);
4425 spin_unlock(&dir->i_lock);
4430 static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4432 struct nfs4_exception exception = {
4433 .interruptible = true,
4435 struct inode *inode = d_inode(dentry);
4439 if (inode->i_nlink == 1)
4440 nfs4_inode_return_delegation(inode);
4442 nfs4_inode_make_writeable(inode);
4445 err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
4446 trace_nfs4_remove(dir, &dentry->d_name, err);
4447 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4449 } while (exception.retry);
4453 static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
4455 struct nfs4_exception exception = {
4456 .interruptible = true,
4461 err = _nfs4_proc_remove(dir, name, NF4DIR);
4462 trace_nfs4_remove(dir, name, err);
4463 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4465 } while (exception.retry);
4469 static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4470 struct dentry *dentry,
4471 struct inode *inode)
4473 struct nfs_removeargs *args = msg->rpc_argp;
4474 struct nfs_removeres *res = msg->rpc_resp;
4476 res->server = NFS_SB(dentry->d_sb);
4477 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
4478 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
4480 nfs_fattr_init(res->dir_attr);
4483 nfs4_inode_return_delegation(inode);
4486 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4488 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
4489 &data->args.seq_args,
4494 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
4496 struct nfs_unlinkdata *data = task->tk_calldata;
4497 struct nfs_removeres *res = &data->res;
4499 if (!nfs4_sequence_done(task, &res->seq_res))
4501 if (nfs4_async_handle_error(task, res->server, NULL,
4502 &data->timeout) == -EAGAIN)
4504 if (task->tk_status == 0)
4505 update_changeattr(dir, &res->cinfo,
4506 res->dir_attr->time_start, 0);
4510 static void nfs4_proc_rename_setup(struct rpc_message *msg,
4511 struct dentry *old_dentry,
4512 struct dentry *new_dentry)
4514 struct nfs_renameargs *arg = msg->rpc_argp;
4515 struct nfs_renameres *res = msg->rpc_resp;
4516 struct inode *old_inode = d_inode(old_dentry);
4517 struct inode *new_inode = d_inode(new_dentry);
4520 nfs4_inode_make_writeable(old_inode);
4522 nfs4_inode_return_delegation(new_inode);
4523 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
4524 res->server = NFS_SB(old_dentry->d_sb);
4525 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
4528 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4530 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
4531 &data->args.seq_args,
4536 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4537 struct inode *new_dir)
4539 struct nfs_renamedata *data = task->tk_calldata;
4540 struct nfs_renameres *res = &data->res;
4542 if (!nfs4_sequence_done(task, &res->seq_res))
4544 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
4547 if (task->tk_status == 0) {
4548 if (new_dir != old_dir) {
4549 /* Note: If we moved a directory, nlink will change */
4550 update_changeattr(old_dir, &res->old_cinfo,
4551 res->old_fattr->time_start,
4552 NFS_INO_INVALID_OTHER);
4553 update_changeattr(new_dir, &res->new_cinfo,
4554 res->new_fattr->time_start,
4555 NFS_INO_INVALID_OTHER);
4557 update_changeattr(old_dir, &res->old_cinfo,
4558 res->old_fattr->time_start,
4564 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4566 struct nfs_server *server = NFS_SERVER(inode);
4567 __u32 bitmask[NFS4_BITMASK_SZ];
4568 struct nfs4_link_arg arg = {
4569 .fh = NFS_FH(inode),
4570 .dir_fh = NFS_FH(dir),
4574 struct nfs4_link_res res = {
4578 struct rpc_message msg = {
4579 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4583 int status = -ENOMEM;
4585 res.fattr = nfs_alloc_fattr();
4586 if (res.fattr == NULL)
4589 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4590 if (IS_ERR(res.label)) {
4591 status = PTR_ERR(res.label);
4595 nfs4_inode_make_writeable(inode);
4596 nfs4_bitmap_copy_adjust_setattr(bitmask, nfs4_bitmask(server, res.label), inode);
4598 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4600 update_changeattr(dir, &res.cinfo, res.fattr->time_start, 0);
4601 status = nfs_post_op_update_inode(inode, res.fattr);
4603 nfs_setsecurity(inode, res.fattr, res.label);
4607 nfs4_label_free(res.label);
4610 nfs_free_fattr(res.fattr);
4614 static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4616 struct nfs4_exception exception = {
4617 .interruptible = true,
4621 err = nfs4_handle_exception(NFS_SERVER(inode),
4622 _nfs4_proc_link(inode, dir, name),
4624 } while (exception.retry);
4628 struct nfs4_createdata {
4629 struct rpc_message msg;
4630 struct nfs4_create_arg arg;
4631 struct nfs4_create_res res;
4633 struct nfs_fattr fattr;
4634 struct nfs4_label *label;
4637 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
4638 const struct qstr *name, struct iattr *sattr, u32 ftype)
4640 struct nfs4_createdata *data;
4642 data = kzalloc(sizeof(*data), GFP_KERNEL);
4644 struct nfs_server *server = NFS_SERVER(dir);
4646 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4647 if (IS_ERR(data->label))
4650 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4651 data->msg.rpc_argp = &data->arg;
4652 data->msg.rpc_resp = &data->res;
4653 data->arg.dir_fh = NFS_FH(dir);
4654 data->arg.server = server;
4655 data->arg.name = name;
4656 data->arg.attrs = sattr;
4657 data->arg.ftype = ftype;
4658 data->arg.bitmask = nfs4_bitmask(server, data->label);
4659 data->arg.umask = current_umask();
4660 data->res.server = server;
4661 data->res.fh = &data->fh;
4662 data->res.fattr = &data->fattr;
4663 data->res.label = data->label;
4664 nfs_fattr_init(data->res.fattr);
4672 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4674 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
4675 &data->arg.seq_args, &data->res.seq_res, 1);
4677 spin_lock(&dir->i_lock);
4678 update_changeattr_locked(dir, &data->res.dir_cinfo,
4679 data->res.fattr->time_start, 0);
4680 /* Creating a directory bumps nlink in the parent */
4681 if (data->arg.ftype == NF4DIR)
4682 nfs4_inc_nlink_locked(dir);
4683 spin_unlock(&dir->i_lock);
4684 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
4689 static void nfs4_free_createdata(struct nfs4_createdata *data)
4691 nfs4_label_free(data->label);
4695 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4696 struct page *page, unsigned int len, struct iattr *sattr,
4697 struct nfs4_label *label)
4699 struct nfs4_createdata *data;
4700 int status = -ENAMETOOLONG;
4702 if (len > NFS4_MAXPATHLEN)
4706 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4710 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4711 data->arg.u.symlink.pages = &page;
4712 data->arg.u.symlink.len = len;
4713 data->arg.label = label;
4715 status = nfs4_do_create(dir, dentry, data);
4717 nfs4_free_createdata(data);
4722 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4723 struct page *page, unsigned int len, struct iattr *sattr)
4725 struct nfs4_exception exception = {
4726 .interruptible = true,
4728 struct nfs4_label l, *label = NULL;
4731 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4734 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4735 trace_nfs4_symlink(dir, &dentry->d_name, err);
4736 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4738 } while (exception.retry);
4740 nfs4_label_release_security(label);
4744 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4745 struct iattr *sattr, struct nfs4_label *label)
4747 struct nfs4_createdata *data;
4748 int status = -ENOMEM;
4750 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4754 data->arg.label = label;
4755 status = nfs4_do_create(dir, dentry, data);
4757 nfs4_free_createdata(data);
4762 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4763 struct iattr *sattr)
4765 struct nfs_server *server = NFS_SERVER(dir);
4766 struct nfs4_exception exception = {
4767 .interruptible = true,
4769 struct nfs4_label l, *label = NULL;
4772 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4774 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4775 sattr->ia_mode &= ~current_umask();
4777 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4778 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4779 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4781 } while (exception.retry);
4782 nfs4_label_release_security(label);
4787 static int _nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
4788 u64 cookie, struct page **pages, unsigned int count, bool plus)
4790 struct inode *dir = d_inode(dentry);
4791 struct nfs4_readdir_arg args = {
4796 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
4799 struct nfs4_readdir_res res;
4800 struct rpc_message msg = {
4801 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4808 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4810 (unsigned long long)cookie);
4811 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
4812 res.pgbase = args.pgbase;
4813 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4815 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
4816 status += args.pgbase;
4819 nfs_invalidate_atime(dir);
4821 dprintk("%s: returns %d\n", __func__, status);
4825 static int nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
4826 u64 cookie, struct page **pages, unsigned int count, bool plus)
4828 struct nfs4_exception exception = {
4829 .interruptible = true,
4833 err = _nfs4_proc_readdir(dentry, cred, cookie,
4834 pages, count, plus);
4835 trace_nfs4_readdir(d_inode(dentry), err);
4836 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
4838 } while (exception.retry);
4842 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4843 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
4845 struct nfs4_createdata *data;
4846 int mode = sattr->ia_mode;
4847 int status = -ENOMEM;
4849 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4854 data->arg.ftype = NF4FIFO;
4855 else if (S_ISBLK(mode)) {
4856 data->arg.ftype = NF4BLK;
4857 data->arg.u.device.specdata1 = MAJOR(rdev);
4858 data->arg.u.device.specdata2 = MINOR(rdev);
4860 else if (S_ISCHR(mode)) {
4861 data->arg.ftype = NF4CHR;
4862 data->arg.u.device.specdata1 = MAJOR(rdev);
4863 data->arg.u.device.specdata2 = MINOR(rdev);
4864 } else if (!S_ISSOCK(mode)) {
4869 data->arg.label = label;
4870 status = nfs4_do_create(dir, dentry, data);
4872 nfs4_free_createdata(data);
4877 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4878 struct iattr *sattr, dev_t rdev)
4880 struct nfs_server *server = NFS_SERVER(dir);
4881 struct nfs4_exception exception = {
4882 .interruptible = true,
4884 struct nfs4_label l, *label = NULL;
4887 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4889 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4890 sattr->ia_mode &= ~current_umask();
4892 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4893 trace_nfs4_mknod(dir, &dentry->d_name, err);
4894 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4896 } while (exception.retry);
4898 nfs4_label_release_security(label);
4903 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4904 struct nfs_fsstat *fsstat)
4906 struct nfs4_statfs_arg args = {
4908 .bitmask = server->attr_bitmask,
4910 struct nfs4_statfs_res res = {
4913 struct rpc_message msg = {
4914 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4919 nfs_fattr_init(fsstat->fattr);
4920 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4923 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4925 struct nfs4_exception exception = {
4926 .interruptible = true,
4930 err = nfs4_handle_exception(server,
4931 _nfs4_proc_statfs(server, fhandle, fsstat),
4933 } while (exception.retry);
4937 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4938 struct nfs_fsinfo *fsinfo)
4940 struct nfs4_fsinfo_arg args = {
4942 .bitmask = server->attr_bitmask,
4944 struct nfs4_fsinfo_res res = {
4947 struct rpc_message msg = {
4948 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4953 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4956 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4958 struct nfs4_exception exception = {
4959 .interruptible = true,
4961 unsigned long now = jiffies;
4965 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
4966 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
4968 nfs4_set_lease_period(server->nfs_client,
4969 fsinfo->lease_time * HZ,
4973 err = nfs4_handle_exception(server, err, &exception);
4974 } while (exception.retry);
4978 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4982 nfs_fattr_init(fsinfo->fattr);
4983 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
4985 /* block layout checks this! */
4986 server->pnfs_blksize = fsinfo->blksize;
4987 set_pnfs_layoutdriver(server, fhandle, fsinfo);
4993 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4994 struct nfs_pathconf *pathconf)
4996 struct nfs4_pathconf_arg args = {
4998 .bitmask = server->attr_bitmask,
5000 struct nfs4_pathconf_res res = {
5001 .pathconf = pathconf,
5003 struct rpc_message msg = {
5004 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
5009 /* None of the pathconf attributes are mandatory to implement */
5010 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
5011 memset(pathconf, 0, sizeof(*pathconf));
5015 nfs_fattr_init(pathconf->fattr);
5016 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5019 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5020 struct nfs_pathconf *pathconf)
5022 struct nfs4_exception exception = {
5023 .interruptible = true,
5028 err = nfs4_handle_exception(server,
5029 _nfs4_proc_pathconf(server, fhandle, pathconf),
5031 } while (exception.retry);
5035 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
5036 const struct nfs_open_context *ctx,
5037 const struct nfs_lock_context *l_ctx,
5040 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
5042 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
5044 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
5045 const struct nfs_open_context *ctx,
5046 const struct nfs_lock_context *l_ctx,
5049 nfs4_stateid current_stateid;
5051 /* If the current stateid represents a lost lock, then exit */
5052 if (nfs4_set_rw_stateid(¤t_stateid, ctx, l_ctx, fmode) == -EIO)
5054 return nfs4_stateid_match(stateid, ¤t_stateid);
5057 static bool nfs4_error_stateid_expired(int err)
5060 case -NFS4ERR_DELEG_REVOKED:
5061 case -NFS4ERR_ADMIN_REVOKED:
5062 case -NFS4ERR_BAD_STATEID:
5063 case -NFS4ERR_STALE_STATEID:
5064 case -NFS4ERR_OLD_STATEID:
5065 case -NFS4ERR_OPENMODE:
5066 case -NFS4ERR_EXPIRED:
5072 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
5074 struct nfs_server *server = NFS_SERVER(hdr->inode);
5076 trace_nfs4_read(hdr, task->tk_status);
5077 if (task->tk_status < 0) {
5078 struct nfs4_exception exception = {
5079 .inode = hdr->inode,
5080 .state = hdr->args.context->state,
5081 .stateid = &hdr->args.stateid,
5083 task->tk_status = nfs4_async_handle_exception(task,
5084 server, task->tk_status, &exception);
5085 if (exception.retry) {
5086 rpc_restart_call_prepare(task);
5091 if (task->tk_status > 0)
5092 renew_lease(server, hdr->timestamp);
5096 static bool nfs4_read_stateid_changed(struct rpc_task *task,
5097 struct nfs_pgio_args *args)
5100 if (!nfs4_error_stateid_expired(task->tk_status) ||
5101 nfs4_stateid_is_current(&args->stateid,
5106 rpc_restart_call_prepare(task);
5110 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5113 dprintk("--> %s\n", __func__);
5115 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5117 if (nfs4_read_stateid_changed(task, &hdr->args))
5119 if (task->tk_status > 0)
5120 nfs_invalidate_atime(hdr->inode);
5121 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5122 nfs4_read_done_cb(task, hdr);
5125 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5126 struct rpc_message *msg)
5128 hdr->timestamp = jiffies;
5129 if (!hdr->pgio_done_cb)
5130 hdr->pgio_done_cb = nfs4_read_done_cb;
5131 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5132 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5135 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5136 struct nfs_pgio_header *hdr)
5138 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
5139 &hdr->args.seq_args,
5143 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5144 hdr->args.lock_context,
5145 hdr->rw_mode) == -EIO)
5147 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
5152 static int nfs4_write_done_cb(struct rpc_task *task,
5153 struct nfs_pgio_header *hdr)
5155 struct inode *inode = hdr->inode;
5157 trace_nfs4_write(hdr, task->tk_status);
5158 if (task->tk_status < 0) {
5159 struct nfs4_exception exception = {
5160 .inode = hdr->inode,
5161 .state = hdr->args.context->state,
5162 .stateid = &hdr->args.stateid,
5164 task->tk_status = nfs4_async_handle_exception(task,
5165 NFS_SERVER(inode), task->tk_status,
5167 if (exception.retry) {
5168 rpc_restart_call_prepare(task);
5172 if (task->tk_status >= 0) {
5173 renew_lease(NFS_SERVER(inode), hdr->timestamp);
5174 nfs_writeback_update_inode(hdr);
5179 static bool nfs4_write_stateid_changed(struct rpc_task *task,
5180 struct nfs_pgio_args *args)
5183 if (!nfs4_error_stateid_expired(task->tk_status) ||
5184 nfs4_stateid_is_current(&args->stateid,
5189 rpc_restart_call_prepare(task);
5193 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5195 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5197 if (nfs4_write_stateid_changed(task, &hdr->args))
5199 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5200 nfs4_write_done_cb(task, hdr);
5204 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
5206 /* Don't request attributes for pNFS or O_DIRECT writes */
5207 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
5209 /* Otherwise, request attributes if and only if we don't hold
5212 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
5215 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
5216 struct rpc_message *msg,
5217 struct rpc_clnt **clnt)
5219 struct nfs_server *server = NFS_SERVER(hdr->inode);
5221 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5222 hdr->args.bitmask = NULL;
5223 hdr->res.fattr = NULL;
5225 hdr->args.bitmask = server->cache_consistency_bitmask;
5227 if (!hdr->pgio_done_cb)
5228 hdr->pgio_done_cb = nfs4_write_done_cb;
5229 hdr->res.server = server;
5230 hdr->timestamp = jiffies;
5232 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
5233 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1, 0);
5234 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
5237 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5239 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
5240 &data->args.seq_args,
5245 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
5247 struct inode *inode = data->inode;
5249 trace_nfs4_commit(data, task->tk_status);
5250 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5251 NULL, NULL) == -EAGAIN) {
5252 rpc_restart_call_prepare(task);
5258 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
5260 if (!nfs4_sequence_done(task, &data->res.seq_res))
5262 return data->commit_done_cb(task, data);
5265 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5266 struct rpc_clnt **clnt)
5268 struct nfs_server *server = NFS_SERVER(data->inode);
5270 if (data->commit_done_cb == NULL)
5271 data->commit_done_cb = nfs4_commit_done_cb;
5272 data->res.server = server;
5273 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
5274 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
5275 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
5278 static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5279 struct nfs_commitres *res)
5281 struct inode *dst_inode = file_inode(dst);
5282 struct nfs_server *server = NFS_SERVER(dst_inode);
5283 struct rpc_message msg = {
5284 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5289 args->fh = NFS_FH(dst_inode);
5290 return nfs4_call_sync(server->client, server, &msg,
5291 &args->seq_args, &res->seq_res, 1);
5294 int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5296 struct nfs_commitargs args = {
5300 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5301 struct nfs4_exception exception = { };
5305 status = _nfs4_proc_commit(dst, &args, res);
5306 status = nfs4_handle_exception(dst_server, status, &exception);
5307 } while (exception.retry);
5312 struct nfs4_renewdata {
5313 struct nfs_client *client;
5314 unsigned long timestamp;
5318 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5319 * standalone procedure for queueing an asynchronous RENEW.
5321 static void nfs4_renew_release(void *calldata)
5323 struct nfs4_renewdata *data = calldata;
5324 struct nfs_client *clp = data->client;
5326 if (refcount_read(&clp->cl_count) > 1)
5327 nfs4_schedule_state_renewal(clp);
5328 nfs_put_client(clp);
5332 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
5334 struct nfs4_renewdata *data = calldata;
5335 struct nfs_client *clp = data->client;
5336 unsigned long timestamp = data->timestamp;
5338 trace_nfs4_renew_async(clp, task->tk_status);
5339 switch (task->tk_status) {
5342 case -NFS4ERR_LEASE_MOVED:
5343 nfs4_schedule_lease_moved_recovery(clp);
5346 /* Unless we're shutting down, schedule state recovery! */
5347 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5349 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
5350 nfs4_schedule_lease_recovery(clp);
5353 nfs4_schedule_path_down_recovery(clp);
5355 do_renew_lease(clp, timestamp);
5358 static const struct rpc_call_ops nfs4_renew_ops = {
5359 .rpc_call_done = nfs4_renew_done,
5360 .rpc_release = nfs4_renew_release,
5363 static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
5365 struct rpc_message msg = {
5366 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5370 struct nfs4_renewdata *data;
5372 if (renew_flags == 0)
5374 if (!refcount_inc_not_zero(&clp->cl_count))
5376 data = kmalloc(sizeof(*data), GFP_NOFS);
5378 nfs_put_client(clp);
5382 data->timestamp = jiffies;
5383 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
5384 &nfs4_renew_ops, data);
5387 static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
5389 struct rpc_message msg = {
5390 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5394 unsigned long now = jiffies;
5397 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5400 do_renew_lease(clp, now);
5404 static inline int nfs4_server_supports_acls(struct nfs_server *server)
5406 return server->caps & NFS_CAP_ACLS;
5409 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5410 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
5413 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
5415 static int buf_to_pages_noslab(const void *buf, size_t buflen,
5416 struct page **pages)
5418 struct page *newpage, **spages;
5424 len = min_t(size_t, PAGE_SIZE, buflen);
5425 newpage = alloc_page(GFP_KERNEL);
5427 if (newpage == NULL)
5429 memcpy(page_address(newpage), buf, len);
5434 } while (buflen != 0);
5440 __free_page(spages[rc-1]);
5444 struct nfs4_cached_acl {
5450 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
5452 struct nfs_inode *nfsi = NFS_I(inode);
5454 spin_lock(&inode->i_lock);
5455 kfree(nfsi->nfs4_acl);
5456 nfsi->nfs4_acl = acl;
5457 spin_unlock(&inode->i_lock);
5460 static void nfs4_zap_acl_attr(struct inode *inode)
5462 nfs4_set_cached_acl(inode, NULL);
5465 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5467 struct nfs_inode *nfsi = NFS_I(inode);
5468 struct nfs4_cached_acl *acl;
5471 spin_lock(&inode->i_lock);
5472 acl = nfsi->nfs4_acl;
5475 if (buf == NULL) /* user is just asking for length */
5477 if (acl->cached == 0)
5479 ret = -ERANGE; /* see getxattr(2) man page */
5480 if (acl->len > buflen)
5482 memcpy(buf, acl->data, acl->len);
5486 spin_unlock(&inode->i_lock);
5490 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
5492 struct nfs4_cached_acl *acl;
5493 size_t buflen = sizeof(*acl) + acl_len;
5495 if (buflen <= PAGE_SIZE) {
5496 acl = kmalloc(buflen, GFP_KERNEL);
5500 _copy_from_pages(acl->data, pages, pgbase, acl_len);
5502 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5509 nfs4_set_cached_acl(inode, acl);
5513 * The getxattr API returns the required buffer length when called with a
5514 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5515 * the required buf. On a NULL buf, we send a page of data to the server
5516 * guessing that the ACL request can be serviced by a page. If so, we cache
5517 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5518 * the cache. If not so, we throw away the page, and cache the required
5519 * length. The next getxattr call will then produce another round trip to
5520 * the server, this time with the input buf of the required size.
5522 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5524 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
5525 struct nfs_getaclargs args = {
5526 .fh = NFS_FH(inode),
5530 struct nfs_getaclres res = {
5533 struct rpc_message msg = {
5534 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5538 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
5539 int ret = -ENOMEM, i;
5541 if (npages > ARRAY_SIZE(pages))
5544 for (i = 0; i < npages; i++) {
5545 pages[i] = alloc_page(GFP_KERNEL);
5550 /* for decoding across pages */
5551 res.acl_scratch = alloc_page(GFP_KERNEL);
5552 if (!res.acl_scratch)
5555 args.acl_len = npages * PAGE_SIZE;
5557 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
5558 __func__, buf, buflen, npages, args.acl_len);
5559 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5560 &msg, &args.seq_args, &res.seq_res, 0);
5564 /* Handle the case where the passed-in buffer is too short */
5565 if (res.acl_flags & NFS4_ACL_TRUNC) {
5566 /* Did the user only issue a request for the acl length? */
5572 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
5574 if (res.acl_len > buflen) {
5578 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
5583 for (i = 0; i < npages; i++)
5585 __free_page(pages[i]);
5586 if (res.acl_scratch)
5587 __free_page(res.acl_scratch);
5591 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5593 struct nfs4_exception exception = {
5594 .interruptible = true,
5598 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
5599 trace_nfs4_get_acl(inode, ret);
5602 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5603 } while (exception.retry);
5607 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5609 struct nfs_server *server = NFS_SERVER(inode);
5612 if (!nfs4_server_supports_acls(server))
5614 ret = nfs_revalidate_inode(server, inode);
5617 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5618 nfs_zap_acl_cache(inode);
5619 ret = nfs4_read_cached_acl(inode, buf, buflen);
5621 /* -ENOENT is returned if there is no ACL or if there is an ACL
5622 * but no cached acl data, just the acl length */
5624 return nfs4_get_acl_uncached(inode, buf, buflen);
5627 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5629 struct nfs_server *server = NFS_SERVER(inode);
5630 struct page *pages[NFS4ACL_MAXPAGES];
5631 struct nfs_setaclargs arg = {
5632 .fh = NFS_FH(inode),
5636 struct nfs_setaclres res;
5637 struct rpc_message msg = {
5638 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5642 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
5645 if (!nfs4_server_supports_acls(server))
5647 if (npages > ARRAY_SIZE(pages))
5649 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
5652 nfs4_inode_make_writeable(inode);
5653 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5656 * Free each page after tx, so the only ref left is
5657 * held by the network stack
5660 put_page(pages[i-1]);
5663 * Acl update can result in inode attribute update.
5664 * so mark the attribute cache invalid.
5666 spin_lock(&inode->i_lock);
5667 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
5668 | NFS_INO_INVALID_CTIME
5669 | NFS_INO_REVAL_FORCED;
5670 spin_unlock(&inode->i_lock);
5671 nfs_access_zap_cache(inode);
5672 nfs_zap_acl_cache(inode);
5676 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5678 struct nfs4_exception exception = { };
5681 err = __nfs4_proc_set_acl(inode, buf, buflen);
5682 trace_nfs4_set_acl(inode, err);
5683 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5685 } while (exception.retry);
5689 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
5690 static int _nfs4_get_security_label(struct inode *inode, void *buf,
5693 struct nfs_server *server = NFS_SERVER(inode);
5694 struct nfs_fattr fattr;
5695 struct nfs4_label label = {0, 0, buflen, buf};
5697 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5698 struct nfs4_getattr_arg arg = {
5699 .fh = NFS_FH(inode),
5702 struct nfs4_getattr_res res = {
5707 struct rpc_message msg = {
5708 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
5714 nfs_fattr_init(&fattr);
5716 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
5719 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5721 if (buflen < label.len)
5726 static int nfs4_get_security_label(struct inode *inode, void *buf,
5729 struct nfs4_exception exception = {
5730 .interruptible = true,
5734 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5738 err = _nfs4_get_security_label(inode, buf, buflen);
5739 trace_nfs4_get_security_label(inode, err);
5740 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5742 } while (exception.retry);
5746 static int _nfs4_do_set_security_label(struct inode *inode,
5747 struct nfs4_label *ilabel,
5748 struct nfs_fattr *fattr,
5749 struct nfs4_label *olabel)
5752 struct iattr sattr = {0};
5753 struct nfs_server *server = NFS_SERVER(inode);
5754 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5755 struct nfs_setattrargs arg = {
5756 .fh = NFS_FH(inode),
5762 struct nfs_setattrres res = {
5767 struct rpc_message msg = {
5768 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5774 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
5776 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5778 dprintk("%s failed: %d\n", __func__, status);
5783 static int nfs4_do_set_security_label(struct inode *inode,
5784 struct nfs4_label *ilabel,
5785 struct nfs_fattr *fattr,
5786 struct nfs4_label *olabel)
5788 struct nfs4_exception exception = { };
5792 err = _nfs4_do_set_security_label(inode, ilabel,
5794 trace_nfs4_set_security_label(inode, err);
5795 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5797 } while (exception.retry);
5802 nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
5804 struct nfs4_label ilabel, *olabel = NULL;
5805 struct nfs_fattr fattr;
5808 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5811 nfs_fattr_init(&fattr);
5815 ilabel.label = (char *)buf;
5816 ilabel.len = buflen;
5818 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5819 if (IS_ERR(olabel)) {
5820 status = -PTR_ERR(olabel);
5824 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5826 nfs_setsecurity(inode, &fattr, olabel);
5828 nfs4_label_free(olabel);
5832 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5835 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5836 nfs4_verifier *bootverf)
5840 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5841 /* An impossible timestamp guarantees this value
5842 * will never match a generated boot time. */
5843 verf[0] = cpu_to_be32(U32_MAX);
5844 verf[1] = cpu_to_be32(U32_MAX);
5846 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
5847 u64 ns = ktime_to_ns(nn->boot_time);
5849 verf[0] = cpu_to_be32(ns >> 32);
5850 verf[1] = cpu_to_be32(ns);
5852 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5856 nfs4_init_nonuniform_client_string(struct nfs_client *clp)
5861 if (clp->cl_owner_id != NULL)
5866 strlen(clp->cl_rpcclient->cl_nodename) +
5868 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5871 if (nfs4_client_id_uniquifier[0] != '\0')
5872 len += strlen(nfs4_client_id_uniquifier) + 1;
5873 if (len > NFS4_OPAQUE_LIMIT + 1)
5877 * Since this string is allocated at mount time, and held until the
5878 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5879 * about a memory-reclaim deadlock.
5881 str = kmalloc(len, GFP_KERNEL);
5886 if (nfs4_client_id_uniquifier[0] != '\0')
5887 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
5888 clp->cl_rpcclient->cl_nodename,
5889 nfs4_client_id_uniquifier,
5890 rpc_peeraddr2str(clp->cl_rpcclient,
5893 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
5894 clp->cl_rpcclient->cl_nodename,
5895 rpc_peeraddr2str(clp->cl_rpcclient,
5899 clp->cl_owner_id = str;
5904 nfs4_init_uniquifier_client_string(struct nfs_client *clp)
5909 len = 10 + 10 + 1 + 10 + 1 +
5910 strlen(nfs4_client_id_uniquifier) + 1 +
5911 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5913 if (len > NFS4_OPAQUE_LIMIT + 1)
5917 * Since this string is allocated at mount time, and held until the
5918 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5919 * about a memory-reclaim deadlock.
5921 str = kmalloc(len, GFP_KERNEL);
5925 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
5926 clp->rpc_ops->version, clp->cl_minorversion,
5927 nfs4_client_id_uniquifier,
5928 clp->cl_rpcclient->cl_nodename);
5929 clp->cl_owner_id = str;
5934 nfs4_init_uniform_client_string(struct nfs_client *clp)
5939 if (clp->cl_owner_id != NULL)
5942 if (nfs4_client_id_uniquifier[0] != '\0')
5943 return nfs4_init_uniquifier_client_string(clp);
5945 len = 10 + 10 + 1 + 10 + 1 +
5946 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5948 if (len > NFS4_OPAQUE_LIMIT + 1)
5952 * Since this string is allocated at mount time, and held until the
5953 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5954 * about a memory-reclaim deadlock.
5956 str = kmalloc(len, GFP_KERNEL);
5960 scnprintf(str, len, "Linux NFSv%u.%u %s",
5961 clp->rpc_ops->version, clp->cl_minorversion,
5962 clp->cl_rpcclient->cl_nodename);
5963 clp->cl_owner_id = str;
5968 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5969 * services. Advertise one based on the address family of the
5973 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5975 if (strchr(clp->cl_ipaddr, ':') != NULL)
5976 return scnprintf(buf, len, "tcp6");
5978 return scnprintf(buf, len, "tcp");
5981 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5983 struct nfs4_setclientid *sc = calldata;
5985 if (task->tk_status == 0)
5986 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5989 static const struct rpc_call_ops nfs4_setclientid_ops = {
5990 .rpc_call_done = nfs4_setclientid_done,
5994 * nfs4_proc_setclientid - Negotiate client ID
5995 * @clp: state data structure
5996 * @program: RPC program for NFSv4 callback service
5997 * @port: IP port number for NFS4 callback service
5998 * @cred: credential to use for this call
5999 * @res: where to place the result
6001 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6003 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
6004 unsigned short port, const struct cred *cred,
6005 struct nfs4_setclientid_res *res)
6007 nfs4_verifier sc_verifier;
6008 struct nfs4_setclientid setclientid = {
6009 .sc_verifier = &sc_verifier,
6013 struct rpc_message msg = {
6014 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
6015 .rpc_argp = &setclientid,
6019 struct rpc_task *task;
6020 struct rpc_task_setup task_setup_data = {
6021 .rpc_client = clp->cl_rpcclient,
6022 .rpc_message = &msg,
6023 .callback_ops = &nfs4_setclientid_ops,
6024 .callback_data = &setclientid,
6025 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
6029 /* nfs_client_id4 */
6030 nfs4_init_boot_verifier(clp, &sc_verifier);
6032 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
6033 status = nfs4_init_uniform_client_string(clp);
6035 status = nfs4_init_nonuniform_client_string(clp);
6041 setclientid.sc_netid_len =
6042 nfs4_init_callback_netid(clp,
6043 setclientid.sc_netid,
6044 sizeof(setclientid.sc_netid));
6045 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
6046 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
6047 clp->cl_ipaddr, port >> 8, port & 255);
6049 dprintk("NFS call setclientid auth=%s, '%s'\n",
6050 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6052 task = rpc_run_task(&task_setup_data);
6054 status = PTR_ERR(task);
6057 status = task->tk_status;
6058 if (setclientid.sc_cred) {
6059 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
6060 put_rpccred(setclientid.sc_cred);
6064 trace_nfs4_setclientid(clp, status);
6065 dprintk("NFS reply setclientid: %d\n", status);
6070 * nfs4_proc_setclientid_confirm - Confirm client ID
6071 * @clp: state data structure
6072 * @arg: result of a previous SETCLIENTID
6073 * @cred: credential to use for this call
6075 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6077 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
6078 struct nfs4_setclientid_res *arg,
6079 const struct cred *cred)
6081 struct rpc_message msg = {
6082 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
6088 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
6089 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6091 status = rpc_call_sync(clp->cl_rpcclient, &msg,
6092 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
6093 trace_nfs4_setclientid_confirm(clp, status);
6094 dprintk("NFS reply setclientid_confirm: %d\n", status);
6098 struct nfs4_delegreturndata {
6099 struct nfs4_delegreturnargs args;
6100 struct nfs4_delegreturnres res;
6102 nfs4_stateid stateid;
6103 unsigned long timestamp;
6105 struct nfs4_layoutreturn_args arg;
6106 struct nfs4_layoutreturn_res res;
6107 struct nfs4_xdr_opaque_data ld_private;
6111 struct nfs_fattr fattr;
6113 struct inode *inode;
6116 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6118 struct nfs4_delegreturndata *data = calldata;
6119 struct nfs4_exception exception = {
6120 .inode = data->inode,
6121 .stateid = &data->stateid,
6124 if (!nfs4_sequence_done(task, &data->res.seq_res))
6127 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
6129 /* Handle Layoutreturn errors */
6130 if (data->args.lr_args && task->tk_status != 0) {
6131 switch(data->res.lr_ret) {
6133 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
6136 data->args.lr_args = NULL;
6137 data->res.lr_res = NULL;
6139 case -NFS4ERR_OLD_STATEID:
6140 if (nfs4_layoutreturn_refresh_stateid(&data->args.lr_args->stateid,
6141 &data->args.lr_args->range,
6145 case -NFS4ERR_ADMIN_REVOKED:
6146 case -NFS4ERR_DELEG_REVOKED:
6147 case -NFS4ERR_EXPIRED:
6148 case -NFS4ERR_BAD_STATEID:
6149 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
6150 case -NFS4ERR_WRONG_CRED:
6151 data->args.lr_args = NULL;
6152 data->res.lr_res = NULL;
6157 switch (task->tk_status) {
6159 renew_lease(data->res.server, data->timestamp);
6161 case -NFS4ERR_ADMIN_REVOKED:
6162 case -NFS4ERR_DELEG_REVOKED:
6163 case -NFS4ERR_EXPIRED:
6164 nfs4_free_revoked_stateid(data->res.server,
6166 task->tk_msg.rpc_cred);
6168 case -NFS4ERR_BAD_STATEID:
6169 case -NFS4ERR_STALE_STATEID:
6170 task->tk_status = 0;
6172 case -NFS4ERR_OLD_STATEID:
6173 if (nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6175 task->tk_status = 0;
6177 case -NFS4ERR_ACCESS:
6178 if (data->args.bitmask) {
6179 data->args.bitmask = NULL;
6180 data->res.fattr = NULL;
6185 task->tk_status = nfs4_async_handle_exception(task,
6186 data->res.server, task->tk_status,
6188 if (exception.retry)
6191 data->rpc_status = task->tk_status;
6194 data->res.lr_ret = 0;
6196 task->tk_status = 0;
6197 rpc_restart_call_prepare(task);
6200 static void nfs4_delegreturn_release(void *calldata)
6202 struct nfs4_delegreturndata *data = calldata;
6203 struct inode *inode = data->inode;
6207 pnfs_roc_release(&data->lr.arg, &data->lr.res,
6209 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
6210 nfs_iput_and_deactive(inode);
6215 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6217 struct nfs4_delegreturndata *d_data;
6218 struct pnfs_layout_hdr *lo;
6220 d_data = (struct nfs4_delegreturndata *)data;
6222 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
6225 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6226 if (lo && !pnfs_layout_is_valid(lo)) {
6227 d_data->args.lr_args = NULL;
6228 d_data->res.lr_res = NULL;
6231 nfs4_setup_sequence(d_data->res.server->nfs_client,
6232 &d_data->args.seq_args,
6233 &d_data->res.seq_res,
6237 static const struct rpc_call_ops nfs4_delegreturn_ops = {
6238 .rpc_call_prepare = nfs4_delegreturn_prepare,
6239 .rpc_call_done = nfs4_delegreturn_done,
6240 .rpc_release = nfs4_delegreturn_release,
6243 static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6245 struct nfs4_delegreturndata *data;
6246 struct nfs_server *server = NFS_SERVER(inode);
6247 struct rpc_task *task;
6248 struct rpc_message msg = {
6249 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6252 struct rpc_task_setup task_setup_data = {
6253 .rpc_client = server->client,
6254 .rpc_message = &msg,
6255 .callback_ops = &nfs4_delegreturn_ops,
6256 .flags = RPC_TASK_ASYNC,
6260 data = kzalloc(sizeof(*data), GFP_NOFS);
6263 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
6265 nfs4_state_protect(server->nfs_client,
6266 NFS_SP4_MACH_CRED_CLEANUP,
6267 &task_setup_data.rpc_client, &msg);
6269 data->args.fhandle = &data->fh;
6270 data->args.stateid = &data->stateid;
6271 data->args.bitmask = server->cache_consistency_bitmask;
6272 nfs_copy_fh(&data->fh, NFS_FH(inode));
6273 nfs4_stateid_copy(&data->stateid, stateid);
6274 data->res.fattr = &data->fattr;
6275 data->res.server = server;
6276 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
6277 data->lr.arg.ld_private = &data->lr.ld_private;
6278 nfs_fattr_init(data->res.fattr);
6279 data->timestamp = jiffies;
6280 data->rpc_status = 0;
6281 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
6282 data->inode = nfs_igrab_and_active(inode);
6285 data->args.lr_args = &data->lr.arg;
6286 data->res.lr_res = &data->lr.res;
6288 } else if (data->lr.roc) {
6289 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
6290 data->lr.roc = false;
6293 task_setup_data.callback_data = data;
6294 msg.rpc_argp = &data->args;
6295 msg.rpc_resp = &data->res;
6296 task = rpc_run_task(&task_setup_data);
6298 return PTR_ERR(task);
6301 status = rpc_wait_for_completion_task(task);
6304 status = data->rpc_status;
6310 int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6312 struct nfs_server *server = NFS_SERVER(inode);
6313 struct nfs4_exception exception = { };
6316 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
6317 trace_nfs4_delegreturn(inode, stateid, err);
6319 case -NFS4ERR_STALE_STATEID:
6320 case -NFS4ERR_EXPIRED:
6324 err = nfs4_handle_exception(server, err, &exception);
6325 } while (exception.retry);
6329 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6331 struct inode *inode = state->inode;
6332 struct nfs_server *server = NFS_SERVER(inode);
6333 struct nfs_client *clp = server->nfs_client;
6334 struct nfs_lockt_args arg = {
6335 .fh = NFS_FH(inode),
6338 struct nfs_lockt_res res = {
6341 struct rpc_message msg = {
6342 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
6345 .rpc_cred = state->owner->so_cred,
6347 struct nfs4_lock_state *lsp;
6350 arg.lock_owner.clientid = clp->cl_clientid;
6351 status = nfs4_set_lock_state(state, request);
6354 lsp = request->fl_u.nfs4_fl.owner;
6355 arg.lock_owner.id = lsp->ls_seqid.owner_id;
6356 arg.lock_owner.s_dev = server->s_dev;
6357 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6360 request->fl_type = F_UNLCK;
6362 case -NFS4ERR_DENIED:
6365 request->fl_ops->fl_release_private(request);
6366 request->fl_ops = NULL;
6371 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6373 struct nfs4_exception exception = {
6374 .interruptible = true,
6379 err = _nfs4_proc_getlk(state, cmd, request);
6380 trace_nfs4_get_lock(request, state, cmd, err);
6381 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
6383 } while (exception.retry);
6387 struct nfs4_unlockdata {
6388 struct nfs_locku_args arg;
6389 struct nfs_locku_res res;
6390 struct nfs4_lock_state *lsp;
6391 struct nfs_open_context *ctx;
6392 struct nfs_lock_context *l_ctx;
6393 struct file_lock fl;
6394 struct nfs_server *server;
6395 unsigned long timestamp;
6398 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6399 struct nfs_open_context *ctx,
6400 struct nfs4_lock_state *lsp,
6401 struct nfs_seqid *seqid)
6403 struct nfs4_unlockdata *p;
6404 struct inode *inode = lsp->ls_state->inode;
6406 p = kzalloc(sizeof(*p), GFP_NOFS);
6409 p->arg.fh = NFS_FH(inode);
6411 p->arg.seqid = seqid;
6412 p->res.seqid = seqid;
6414 /* Ensure we don't close file until we're done freeing locks! */
6415 p->ctx = get_nfs_open_context(ctx);
6416 p->l_ctx = nfs_get_lock_context(ctx);
6417 locks_init_lock(&p->fl);
6418 locks_copy_lock(&p->fl, fl);
6419 p->server = NFS_SERVER(inode);
6423 static void nfs4_locku_release_calldata(void *data)
6425 struct nfs4_unlockdata *calldata = data;
6426 nfs_free_seqid(calldata->arg.seqid);
6427 nfs4_put_lock_state(calldata->lsp);
6428 nfs_put_lock_context(calldata->l_ctx);
6429 put_nfs_open_context(calldata->ctx);
6433 static void nfs4_locku_done(struct rpc_task *task, void *data)
6435 struct nfs4_unlockdata *calldata = data;
6436 struct nfs4_exception exception = {
6437 .inode = calldata->lsp->ls_state->inode,
6438 .stateid = &calldata->arg.stateid,
6441 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6443 switch (task->tk_status) {
6445 renew_lease(calldata->server, calldata->timestamp);
6446 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
6447 if (nfs4_update_lock_stateid(calldata->lsp,
6448 &calldata->res.stateid))
6451 case -NFS4ERR_ADMIN_REVOKED:
6452 case -NFS4ERR_EXPIRED:
6453 nfs4_free_revoked_stateid(calldata->server,
6454 &calldata->arg.stateid,
6455 task->tk_msg.rpc_cred);
6457 case -NFS4ERR_BAD_STATEID:
6458 case -NFS4ERR_OLD_STATEID:
6459 case -NFS4ERR_STALE_STATEID:
6460 if (!nfs4_stateid_match(&calldata->arg.stateid,
6461 &calldata->lsp->ls_stateid))
6462 rpc_restart_call_prepare(task);
6465 task->tk_status = nfs4_async_handle_exception(task,
6466 calldata->server, task->tk_status,
6468 if (exception.retry)
6469 rpc_restart_call_prepare(task);
6471 nfs_release_seqid(calldata->arg.seqid);
6474 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
6476 struct nfs4_unlockdata *calldata = data;
6478 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6479 nfs_async_iocounter_wait(task, calldata->l_ctx))
6482 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
6484 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
6485 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
6486 /* Note: exit _without_ running nfs4_locku_done */
6489 calldata->timestamp = jiffies;
6490 if (nfs4_setup_sequence(calldata->server->nfs_client,
6491 &calldata->arg.seq_args,
6492 &calldata->res.seq_res,
6494 nfs_release_seqid(calldata->arg.seqid);
6497 task->tk_action = NULL;
6499 nfs4_sequence_done(task, &calldata->res.seq_res);
6502 static const struct rpc_call_ops nfs4_locku_ops = {
6503 .rpc_call_prepare = nfs4_locku_prepare,
6504 .rpc_call_done = nfs4_locku_done,
6505 .rpc_release = nfs4_locku_release_calldata,
6508 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6509 struct nfs_open_context *ctx,
6510 struct nfs4_lock_state *lsp,
6511 struct nfs_seqid *seqid)
6513 struct nfs4_unlockdata *data;
6514 struct rpc_message msg = {
6515 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6516 .rpc_cred = ctx->cred,
6518 struct rpc_task_setup task_setup_data = {
6519 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
6520 .rpc_message = &msg,
6521 .callback_ops = &nfs4_locku_ops,
6522 .workqueue = nfsiod_workqueue,
6523 .flags = RPC_TASK_ASYNC,
6526 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6527 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6529 /* Ensure this is an unlock - when canceling a lock, the
6530 * canceled lock is passed in, and it won't be an unlock.
6532 fl->fl_type = F_UNLCK;
6533 if (fl->fl_flags & FL_CLOSE)
6534 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
6536 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6538 nfs_free_seqid(seqid);
6539 return ERR_PTR(-ENOMEM);
6542 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
6543 msg.rpc_argp = &data->arg;
6544 msg.rpc_resp = &data->res;
6545 task_setup_data.callback_data = data;
6546 return rpc_run_task(&task_setup_data);
6549 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6551 struct inode *inode = state->inode;
6552 struct nfs4_state_owner *sp = state->owner;
6553 struct nfs_inode *nfsi = NFS_I(inode);
6554 struct nfs_seqid *seqid;
6555 struct nfs4_lock_state *lsp;
6556 struct rpc_task *task;
6557 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6559 unsigned char fl_flags = request->fl_flags;
6561 status = nfs4_set_lock_state(state, request);
6562 /* Unlock _before_ we do the RPC call */
6563 request->fl_flags |= FL_EXISTS;
6564 /* Exclude nfs_delegation_claim_locks() */
6565 mutex_lock(&sp->so_delegreturn_mutex);
6566 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
6567 down_read(&nfsi->rwsem);
6568 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
6569 up_read(&nfsi->rwsem);
6570 mutex_unlock(&sp->so_delegreturn_mutex);
6573 up_read(&nfsi->rwsem);
6574 mutex_unlock(&sp->so_delegreturn_mutex);
6577 /* Is this a delegated lock? */
6578 lsp = request->fl_u.nfs4_fl.owner;
6579 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6581 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6582 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
6586 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
6587 status = PTR_ERR(task);
6590 status = rpc_wait_for_completion_task(task);
6593 request->fl_flags = fl_flags;
6594 trace_nfs4_unlock(request, state, F_SETLK, status);
6598 struct nfs4_lockdata {
6599 struct nfs_lock_args arg;
6600 struct nfs_lock_res res;
6601 struct nfs4_lock_state *lsp;
6602 struct nfs_open_context *ctx;
6603 struct file_lock fl;
6604 unsigned long timestamp;
6607 struct nfs_server *server;
6610 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
6611 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6614 struct nfs4_lockdata *p;
6615 struct inode *inode = lsp->ls_state->inode;
6616 struct nfs_server *server = NFS_SERVER(inode);
6617 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6619 p = kzalloc(sizeof(*p), gfp_mask);
6623 p->arg.fh = NFS_FH(inode);
6625 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
6626 if (IS_ERR(p->arg.open_seqid))
6628 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6629 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
6630 if (IS_ERR(p->arg.lock_seqid))
6631 goto out_free_seqid;
6632 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
6633 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
6634 p->arg.lock_owner.s_dev = server->s_dev;
6635 p->res.lock_seqid = p->arg.lock_seqid;
6638 p->ctx = get_nfs_open_context(ctx);
6639 locks_init_lock(&p->fl);
6640 locks_copy_lock(&p->fl, fl);
6643 nfs_free_seqid(p->arg.open_seqid);
6649 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6651 struct nfs4_lockdata *data = calldata;
6652 struct nfs4_state *state = data->lsp->ls_state;
6654 dprintk("%s: begin!\n", __func__);
6655 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
6657 /* Do we need to do an open_to_lock_owner? */
6658 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
6659 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
6660 goto out_release_lock_seqid;
6662 nfs4_stateid_copy(&data->arg.open_stateid,
6663 &state->open_stateid);
6664 data->arg.new_lock_owner = 1;
6665 data->res.open_seqid = data->arg.open_seqid;
6667 data->arg.new_lock_owner = 0;
6668 nfs4_stateid_copy(&data->arg.lock_stateid,
6669 &data->lsp->ls_stateid);
6671 if (!nfs4_valid_open_stateid(state)) {
6672 data->rpc_status = -EBADF;
6673 task->tk_action = NULL;
6674 goto out_release_open_seqid;
6676 data->timestamp = jiffies;
6677 if (nfs4_setup_sequence(data->server->nfs_client,
6678 &data->arg.seq_args,
6682 out_release_open_seqid:
6683 nfs_release_seqid(data->arg.open_seqid);
6684 out_release_lock_seqid:
6685 nfs_release_seqid(data->arg.lock_seqid);
6687 nfs4_sequence_done(task, &data->res.seq_res);
6688 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
6691 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6693 struct nfs4_lockdata *data = calldata;
6694 struct nfs4_lock_state *lsp = data->lsp;
6696 dprintk("%s: begin!\n", __func__);
6698 if (!nfs4_sequence_done(task, &data->res.seq_res))
6701 data->rpc_status = task->tk_status;
6702 switch (task->tk_status) {
6704 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
6706 if (data->arg.new_lock && !data->cancelled) {
6707 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
6708 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
6711 if (data->arg.new_lock_owner != 0) {
6712 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6713 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6714 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6715 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6718 case -NFS4ERR_BAD_STATEID:
6719 case -NFS4ERR_OLD_STATEID:
6720 case -NFS4ERR_STALE_STATEID:
6721 case -NFS4ERR_EXPIRED:
6722 if (data->arg.new_lock_owner != 0) {
6723 if (!nfs4_stateid_match(&data->arg.open_stateid,
6724 &lsp->ls_state->open_stateid))
6726 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6731 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
6734 if (!data->cancelled)
6735 rpc_restart_call_prepare(task);
6739 static void nfs4_lock_release(void *calldata)
6741 struct nfs4_lockdata *data = calldata;
6743 dprintk("%s: begin!\n", __func__);
6744 nfs_free_seqid(data->arg.open_seqid);
6745 if (data->cancelled && data->rpc_status == 0) {
6746 struct rpc_task *task;
6747 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6748 data->arg.lock_seqid);
6750 rpc_put_task_async(task);
6751 dprintk("%s: cancelling lock!\n", __func__);
6753 nfs_free_seqid(data->arg.lock_seqid);
6754 nfs4_put_lock_state(data->lsp);
6755 put_nfs_open_context(data->ctx);
6757 dprintk("%s: done!\n", __func__);
6760 static const struct rpc_call_ops nfs4_lock_ops = {
6761 .rpc_call_prepare = nfs4_lock_prepare,
6762 .rpc_call_done = nfs4_lock_done,
6763 .rpc_release = nfs4_lock_release,
6766 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6769 case -NFS4ERR_ADMIN_REVOKED:
6770 case -NFS4ERR_EXPIRED:
6771 case -NFS4ERR_BAD_STATEID:
6772 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6773 if (new_lock_owner != 0 ||
6774 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
6775 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
6777 case -NFS4ERR_STALE_STATEID:
6778 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6779 nfs4_schedule_lease_recovery(server->nfs_client);
6783 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
6785 struct nfs4_lockdata *data;
6786 struct rpc_task *task;
6787 struct rpc_message msg = {
6788 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6789 .rpc_cred = state->owner->so_cred,
6791 struct rpc_task_setup task_setup_data = {
6792 .rpc_client = NFS_CLIENT(state->inode),
6793 .rpc_message = &msg,
6794 .callback_ops = &nfs4_lock_ops,
6795 .workqueue = nfsiod_workqueue,
6796 .flags = RPC_TASK_ASYNC,
6800 dprintk("%s: begin!\n", __func__);
6801 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
6802 fl->fl_u.nfs4_fl.owner,
6803 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
6807 data->arg.block = 1;
6808 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
6809 recovery_type > NFS_LOCK_NEW);
6810 msg.rpc_argp = &data->arg;
6811 msg.rpc_resp = &data->res;
6812 task_setup_data.callback_data = data;
6813 if (recovery_type > NFS_LOCK_NEW) {
6814 if (recovery_type == NFS_LOCK_RECLAIM)
6815 data->arg.reclaim = NFS_LOCK_RECLAIM;
6817 data->arg.new_lock = 1;
6818 task = rpc_run_task(&task_setup_data);
6820 return PTR_ERR(task);
6821 ret = rpc_wait_for_completion_task(task);
6823 ret = data->rpc_status;
6825 nfs4_handle_setlk_error(data->server, data->lsp,
6826 data->arg.new_lock_owner, ret);
6828 data->cancelled = true;
6830 dprintk("%s: done, ret = %d!\n", __func__, ret);
6831 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
6835 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6837 struct nfs_server *server = NFS_SERVER(state->inode);
6838 struct nfs4_exception exception = {
6839 .inode = state->inode,
6844 /* Cache the lock if possible... */
6845 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6847 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
6848 if (err != -NFS4ERR_DELAY)
6850 nfs4_handle_exception(server, err, &exception);
6851 } while (exception.retry);
6855 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6857 struct nfs_server *server = NFS_SERVER(state->inode);
6858 struct nfs4_exception exception = {
6859 .inode = state->inode,
6863 err = nfs4_set_lock_state(state, request);
6866 if (!recover_lost_locks) {
6867 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6871 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6873 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
6877 case -NFS4ERR_GRACE:
6878 case -NFS4ERR_DELAY:
6879 nfs4_handle_exception(server, err, &exception);
6882 } while (exception.retry);
6887 #if defined(CONFIG_NFS_V4_1)
6888 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6890 struct nfs4_lock_state *lsp;
6893 status = nfs4_set_lock_state(state, request);
6896 lsp = request->fl_u.nfs4_fl.owner;
6897 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6898 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6900 return nfs4_lock_expired(state, request);
6904 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6906 struct nfs_inode *nfsi = NFS_I(state->inode);
6907 struct nfs4_state_owner *sp = state->owner;
6908 unsigned char fl_flags = request->fl_flags;
6911 request->fl_flags |= FL_ACCESS;
6912 status = locks_lock_inode_wait(state->inode, request);
6915 mutex_lock(&sp->so_delegreturn_mutex);
6916 down_read(&nfsi->rwsem);
6917 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
6918 /* Yes: cache locks! */
6919 /* ...but avoid races with delegation recall... */
6920 request->fl_flags = fl_flags & ~FL_SLEEP;
6921 status = locks_lock_inode_wait(state->inode, request);
6922 up_read(&nfsi->rwsem);
6923 mutex_unlock(&sp->so_delegreturn_mutex);
6926 up_read(&nfsi->rwsem);
6927 mutex_unlock(&sp->so_delegreturn_mutex);
6928 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
6930 request->fl_flags = fl_flags;
6934 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6936 struct nfs4_exception exception = {
6938 .inode = state->inode,
6939 .interruptible = true,
6944 err = _nfs4_proc_setlk(state, cmd, request);
6945 if (err == -NFS4ERR_DENIED)
6947 err = nfs4_handle_exception(NFS_SERVER(state->inode),
6949 } while (exception.retry);
6953 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6954 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6957 nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6958 struct file_lock *request)
6960 int status = -ERESTARTSYS;
6961 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6963 while(!signalled()) {
6964 status = nfs4_proc_setlk(state, cmd, request);
6965 if ((status != -EAGAIN) || IS_SETLK(cmd))
6967 freezable_schedule_timeout_interruptible(timeout);
6969 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6970 status = -ERESTARTSYS;
6975 #ifdef CONFIG_NFS_V4_1
6976 struct nfs4_lock_waiter {
6977 struct task_struct *task;
6978 struct inode *inode;
6979 struct nfs_lowner *owner;
6983 nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
6986 struct nfs4_lock_waiter *waiter = wait->private;
6988 /* NULL key means to wake up everyone */
6990 struct cb_notify_lock_args *cbnl = key;
6991 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6992 *wowner = waiter->owner;
6994 /* Only wake if the callback was for the same owner. */
6995 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
6998 /* Make sure it's for the right inode */
6999 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
7003 /* override "private" so we can use default_wake_function */
7004 wait->private = waiter->task;
7005 ret = woken_wake_function(wait, mode, flags, key);
7007 list_del_init(&wait->entry);
7008 wait->private = waiter;
7013 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7015 int status = -ERESTARTSYS;
7016 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
7017 struct nfs_server *server = NFS_SERVER(state->inode);
7018 struct nfs_client *clp = server->nfs_client;
7019 wait_queue_head_t *q = &clp->cl_lock_waitq;
7020 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
7021 .id = lsp->ls_seqid.owner_id,
7022 .s_dev = server->s_dev };
7023 struct nfs4_lock_waiter waiter = { .task = current,
7024 .inode = state->inode,
7026 wait_queue_entry_t wait;
7028 /* Don't bother with waitqueue if we don't expect a callback */
7029 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
7030 return nfs4_retry_setlk_simple(state, cmd, request);
7033 wait.private = &waiter;
7034 wait.func = nfs4_wake_lock_waiter;
7036 while(!signalled()) {
7037 add_wait_queue(q, &wait);
7038 status = nfs4_proc_setlk(state, cmd, request);
7039 if ((status != -EAGAIN) || IS_SETLK(cmd)) {
7040 finish_wait(q, &wait);
7044 status = -ERESTARTSYS;
7045 freezer_do_not_count();
7046 wait_woken(&wait, TASK_INTERRUPTIBLE, NFS4_LOCK_MAXTIMEOUT);
7048 finish_wait(q, &wait);
7053 #else /* !CONFIG_NFS_V4_1 */
7055 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7057 return nfs4_retry_setlk_simple(state, cmd, request);
7062 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
7064 struct nfs_open_context *ctx;
7065 struct nfs4_state *state;
7068 /* verify open state */
7069 ctx = nfs_file_open_context(filp);
7072 if (IS_GETLK(cmd)) {
7074 return nfs4_proc_getlk(state, F_GETLK, request);
7078 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
7081 if (request->fl_type == F_UNLCK) {
7083 return nfs4_proc_unlck(state, cmd, request);
7090 if ((request->fl_flags & FL_POSIX) &&
7091 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
7095 * Don't rely on the VFS having checked the file open mode,
7096 * since it won't do this for flock() locks.
7098 switch (request->fl_type) {
7100 if (!(filp->f_mode & FMODE_READ))
7104 if (!(filp->f_mode & FMODE_WRITE))
7108 status = nfs4_set_lock_state(state, request);
7112 return nfs4_retry_setlk(state, cmd, request);
7115 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
7117 struct nfs_server *server = NFS_SERVER(state->inode);
7120 err = nfs4_set_lock_state(state, fl);
7123 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
7124 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
7127 struct nfs_release_lockowner_data {
7128 struct nfs4_lock_state *lsp;
7129 struct nfs_server *server;
7130 struct nfs_release_lockowner_args args;
7131 struct nfs_release_lockowner_res res;
7132 unsigned long timestamp;
7135 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7137 struct nfs_release_lockowner_data *data = calldata;
7138 struct nfs_server *server = data->server;
7139 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7140 &data->res.seq_res, task);
7141 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7142 data->timestamp = jiffies;
7145 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7147 struct nfs_release_lockowner_data *data = calldata;
7148 struct nfs_server *server = data->server;
7150 nfs40_sequence_done(task, &data->res.seq_res);
7152 switch (task->tk_status) {
7154 renew_lease(server, data->timestamp);
7156 case -NFS4ERR_STALE_CLIENTID:
7157 case -NFS4ERR_EXPIRED:
7158 nfs4_schedule_lease_recovery(server->nfs_client);
7160 case -NFS4ERR_LEASE_MOVED:
7161 case -NFS4ERR_DELAY:
7162 if (nfs4_async_handle_error(task, server,
7163 NULL, NULL) == -EAGAIN)
7164 rpc_restart_call_prepare(task);
7168 static void nfs4_release_lockowner_release(void *calldata)
7170 struct nfs_release_lockowner_data *data = calldata;
7171 nfs4_free_lock_state(data->server, data->lsp);
7175 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
7176 .rpc_call_prepare = nfs4_release_lockowner_prepare,
7177 .rpc_call_done = nfs4_release_lockowner_done,
7178 .rpc_release = nfs4_release_lockowner_release,
7182 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
7184 struct nfs_release_lockowner_data *data;
7185 struct rpc_message msg = {
7186 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7189 if (server->nfs_client->cl_mvops->minor_version != 0)
7192 data = kmalloc(sizeof(*data), GFP_NOFS);
7196 data->server = server;
7197 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7198 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7199 data->args.lock_owner.s_dev = server->s_dev;
7201 msg.rpc_argp = &data->args;
7202 msg.rpc_resp = &data->res;
7203 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
7204 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
7207 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7209 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
7210 struct dentry *unused, struct inode *inode,
7211 const char *key, const void *buf,
7212 size_t buflen, int flags)
7214 return nfs4_proc_set_acl(inode, buf, buflen);
7217 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
7218 struct dentry *unused, struct inode *inode,
7219 const char *key, void *buf, size_t buflen)
7221 return nfs4_proc_get_acl(inode, buf, buflen);
7224 static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
7226 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
7229 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
7231 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
7232 struct dentry *unused, struct inode *inode,
7233 const char *key, const void *buf,
7234 size_t buflen, int flags)
7236 if (security_ismaclabel(key))
7237 return nfs4_set_security_label(inode, buf, buflen);
7242 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
7243 struct dentry *unused, struct inode *inode,
7244 const char *key, void *buf, size_t buflen)
7246 if (security_ismaclabel(key))
7247 return nfs4_get_security_label(inode, buf, buflen);
7252 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7256 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7257 len = security_inode_listsecurity(inode, list, list_len);
7258 if (list_len && len > list_len)
7264 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7265 .prefix = XATTR_SECURITY_PREFIX,
7266 .get = nfs4_xattr_get_nfs4_label,
7267 .set = nfs4_xattr_set_nfs4_label,
7273 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7281 * nfs_fhget will use either the mounted_on_fileid or the fileid
7283 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7285 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7286 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7287 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
7288 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
7291 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
7292 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
7293 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7297 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7298 const struct qstr *name,
7299 struct nfs4_fs_locations *fs_locations,
7302 struct nfs_server *server = NFS_SERVER(dir);
7304 struct nfs4_fs_locations_arg args = {
7305 .dir_fh = NFS_FH(dir),
7310 struct nfs4_fs_locations_res res = {
7311 .fs_locations = fs_locations,
7313 struct rpc_message msg = {
7314 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7320 dprintk("%s: start\n", __func__);
7322 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7323 bitmask[1] = nfs4_fattr_bitmap[1];
7325 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7326 * is not supported */
7327 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
7328 bitmask[0] &= ~FATTR4_WORD0_FILEID;
7330 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
7332 nfs_fattr_init(&fs_locations->fattr);
7333 fs_locations->server = server;
7334 fs_locations->nlocations = 0;
7335 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
7336 dprintk("%s: returned status = %d\n", __func__, status);
7340 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7341 const struct qstr *name,
7342 struct nfs4_fs_locations *fs_locations,
7345 struct nfs4_exception exception = {
7346 .interruptible = true,
7350 err = _nfs4_proc_fs_locations(client, dir, name,
7351 fs_locations, page);
7352 trace_nfs4_get_fs_locations(dir, name, err);
7353 err = nfs4_handle_exception(NFS_SERVER(dir), err,
7355 } while (exception.retry);
7360 * This operation also signals the server that this client is
7361 * performing migration recovery. The server can stop returning
7362 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7363 * appended to this compound to identify the client ID which is
7364 * performing recovery.
7366 static int _nfs40_proc_get_locations(struct inode *inode,
7367 struct nfs4_fs_locations *locations,
7368 struct page *page, const struct cred *cred)
7370 struct nfs_server *server = NFS_SERVER(inode);
7371 struct rpc_clnt *clnt = server->client;
7373 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7375 struct nfs4_fs_locations_arg args = {
7376 .clientid = server->nfs_client->cl_clientid,
7377 .fh = NFS_FH(inode),
7380 .migration = 1, /* skip LOOKUP */
7381 .renew = 1, /* append RENEW */
7383 struct nfs4_fs_locations_res res = {
7384 .fs_locations = locations,
7388 struct rpc_message msg = {
7389 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7394 unsigned long now = jiffies;
7397 nfs_fattr_init(&locations->fattr);
7398 locations->server = server;
7399 locations->nlocations = 0;
7401 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7402 status = nfs4_call_sync_sequence(clnt, server, &msg,
7403 &args.seq_args, &res.seq_res);
7407 renew_lease(server, now);
7411 #ifdef CONFIG_NFS_V4_1
7414 * This operation also signals the server that this client is
7415 * performing migration recovery. The server can stop asserting
7416 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7417 * performing this operation is identified in the SEQUENCE
7418 * operation in this compound.
7420 * When the client supports GETATTR(fs_locations_info), it can
7421 * be plumbed in here.
7423 static int _nfs41_proc_get_locations(struct inode *inode,
7424 struct nfs4_fs_locations *locations,
7425 struct page *page, const struct cred *cred)
7427 struct nfs_server *server = NFS_SERVER(inode);
7428 struct rpc_clnt *clnt = server->client;
7430 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7432 struct nfs4_fs_locations_arg args = {
7433 .fh = NFS_FH(inode),
7436 .migration = 1, /* skip LOOKUP */
7438 struct nfs4_fs_locations_res res = {
7439 .fs_locations = locations,
7442 struct rpc_message msg = {
7443 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7450 nfs_fattr_init(&locations->fattr);
7451 locations->server = server;
7452 locations->nlocations = 0;
7454 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7455 status = nfs4_call_sync_sequence(clnt, server, &msg,
7456 &args.seq_args, &res.seq_res);
7457 if (status == NFS4_OK &&
7458 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7459 status = -NFS4ERR_LEASE_MOVED;
7463 #endif /* CONFIG_NFS_V4_1 */
7466 * nfs4_proc_get_locations - discover locations for a migrated FSID
7467 * @inode: inode on FSID that is migrating
7468 * @locations: result of query
7470 * @cred: credential to use for this operation
7472 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7473 * operation failed, or a negative errno if a local error occurred.
7475 * On success, "locations" is filled in, but if the server has
7476 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7479 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7480 * from this client that require migration recovery.
7482 int nfs4_proc_get_locations(struct inode *inode,
7483 struct nfs4_fs_locations *locations,
7484 struct page *page, const struct cred *cred)
7486 struct nfs_server *server = NFS_SERVER(inode);
7487 struct nfs_client *clp = server->nfs_client;
7488 const struct nfs4_mig_recovery_ops *ops =
7489 clp->cl_mvops->mig_recovery_ops;
7490 struct nfs4_exception exception = {
7491 .interruptible = true,
7495 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7496 (unsigned long long)server->fsid.major,
7497 (unsigned long long)server->fsid.minor,
7499 nfs_display_fhandle(NFS_FH(inode), __func__);
7502 status = ops->get_locations(inode, locations, page, cred);
7503 if (status != -NFS4ERR_DELAY)
7505 nfs4_handle_exception(server, status, &exception);
7506 } while (exception.retry);
7511 * This operation also signals the server that this client is
7512 * performing "lease moved" recovery. The server can stop
7513 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7514 * is appended to this compound to identify the client ID which is
7515 * performing recovery.
7517 static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
7519 struct nfs_server *server = NFS_SERVER(inode);
7520 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7521 struct rpc_clnt *clnt = server->client;
7522 struct nfs4_fsid_present_arg args = {
7523 .fh = NFS_FH(inode),
7524 .clientid = clp->cl_clientid,
7525 .renew = 1, /* append RENEW */
7527 struct nfs4_fsid_present_res res = {
7530 struct rpc_message msg = {
7531 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7536 unsigned long now = jiffies;
7539 res.fh = nfs_alloc_fhandle();
7543 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7544 status = nfs4_call_sync_sequence(clnt, server, &msg,
7545 &args.seq_args, &res.seq_res);
7546 nfs_free_fhandle(res.fh);
7550 do_renew_lease(clp, now);
7554 #ifdef CONFIG_NFS_V4_1
7557 * This operation also signals the server that this client is
7558 * performing "lease moved" recovery. The server can stop asserting
7559 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7560 * this operation is identified in the SEQUENCE operation in this
7563 static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
7565 struct nfs_server *server = NFS_SERVER(inode);
7566 struct rpc_clnt *clnt = server->client;
7567 struct nfs4_fsid_present_arg args = {
7568 .fh = NFS_FH(inode),
7570 struct nfs4_fsid_present_res res = {
7572 struct rpc_message msg = {
7573 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7580 res.fh = nfs_alloc_fhandle();
7584 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7585 status = nfs4_call_sync_sequence(clnt, server, &msg,
7586 &args.seq_args, &res.seq_res);
7587 nfs_free_fhandle(res.fh);
7588 if (status == NFS4_OK &&
7589 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7590 status = -NFS4ERR_LEASE_MOVED;
7594 #endif /* CONFIG_NFS_V4_1 */
7597 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7598 * @inode: inode on FSID to check
7599 * @cred: credential to use for this operation
7601 * Server indicates whether the FSID is present, moved, or not
7602 * recognized. This operation is necessary to clear a LEASE_MOVED
7603 * condition for this client ID.
7605 * Returns NFS4_OK if the FSID is present on this server,
7606 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7607 * NFS4ERR code if some error occurred on the server, or a
7608 * negative errno if a local failure occurred.
7610 int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
7612 struct nfs_server *server = NFS_SERVER(inode);
7613 struct nfs_client *clp = server->nfs_client;
7614 const struct nfs4_mig_recovery_ops *ops =
7615 clp->cl_mvops->mig_recovery_ops;
7616 struct nfs4_exception exception = {
7617 .interruptible = true,
7621 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7622 (unsigned long long)server->fsid.major,
7623 (unsigned long long)server->fsid.minor,
7625 nfs_display_fhandle(NFS_FH(inode), __func__);
7628 status = ops->fsid_present(inode, cred);
7629 if (status != -NFS4ERR_DELAY)
7631 nfs4_handle_exception(server, status, &exception);
7632 } while (exception.retry);
7637 * If 'use_integrity' is true and the state managment nfs_client
7638 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7639 * and the machine credential as per RFC3530bis and RFC5661 Security
7640 * Considerations sections. Otherwise, just use the user cred with the
7641 * filesystem's rpc_client.
7643 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
7646 struct nfs4_secinfo_arg args = {
7647 .dir_fh = NFS_FH(dir),
7650 struct nfs4_secinfo_res res = {
7653 struct rpc_message msg = {
7654 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7658 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
7659 const struct cred *cred = NULL;
7661 if (use_integrity) {
7662 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
7663 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7664 msg.rpc_cred = cred;
7667 dprintk("NFS call secinfo %s\n", name->name);
7669 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7670 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7672 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7673 &res.seq_res, RPC_TASK_NO_ROUND_ROBIN);
7674 dprintk("NFS reply secinfo: %d\n", status);
7681 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7682 struct nfs4_secinfo_flavors *flavors)
7684 struct nfs4_exception exception = {
7685 .interruptible = true,
7689 err = -NFS4ERR_WRONGSEC;
7691 /* try to use integrity protection with machine cred */
7692 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7693 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7696 * if unable to use integrity protection, or SECINFO with
7697 * integrity protection returns NFS4ERR_WRONGSEC (which is
7698 * disallowed by spec, but exists in deployed servers) use
7699 * the current filesystem's rpc_client and the user cred.
7701 if (err == -NFS4ERR_WRONGSEC)
7702 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7704 trace_nfs4_secinfo(dir, name, err);
7705 err = nfs4_handle_exception(NFS_SERVER(dir), err,
7707 } while (exception.retry);
7711 #ifdef CONFIG_NFS_V4_1
7713 * Check the exchange flags returned by the server for invalid flags, having
7714 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7717 static int nfs4_check_cl_exchange_flags(u32 flags)
7719 if (flags & ~EXCHGID4_FLAG_MASK_R)
7721 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7722 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7724 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7728 return -NFS4ERR_INVAL;
7732 nfs41_same_server_scope(struct nfs41_server_scope *a,
7733 struct nfs41_server_scope *b)
7735 if (a->server_scope_sz != b->server_scope_sz)
7737 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
7741 nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7745 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7746 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7750 * nfs4_proc_bind_one_conn_to_session()
7752 * The 4.1 client currently uses the same TCP connection for the
7753 * fore and backchannel.
7756 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7757 struct rpc_xprt *xprt,
7758 struct nfs_client *clp,
7759 const struct cred *cred)
7762 struct nfs41_bind_conn_to_session_args args = {
7764 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7766 struct nfs41_bind_conn_to_session_res res;
7767 struct rpc_message msg = {
7769 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
7774 struct rpc_task_setup task_setup_data = {
7777 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
7778 .rpc_message = &msg,
7779 .flags = RPC_TASK_TIMEOUT,
7781 struct rpc_task *task;
7783 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7784 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7785 args.dir = NFS4_CDFC4_FORE;
7787 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7788 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7789 args.dir = NFS4_CDFC4_FORE;
7791 task = rpc_run_task(&task_setup_data);
7792 if (!IS_ERR(task)) {
7793 status = task->tk_status;
7796 status = PTR_ERR(task);
7797 trace_nfs4_bind_conn_to_session(clp, status);
7799 if (memcmp(res.sessionid.data,
7800 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7801 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7804 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
7805 dprintk("NFS: %s: Unexpected direction from server\n",
7809 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
7810 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7819 struct rpc_bind_conn_calldata {
7820 struct nfs_client *clp;
7821 const struct cred *cred;
7825 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7826 struct rpc_xprt *xprt,
7829 struct rpc_bind_conn_calldata *p = calldata;
7831 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7834 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
7836 struct rpc_bind_conn_calldata data = {
7840 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7841 nfs4_proc_bind_conn_to_session_callback, &data);
7845 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7846 * and operations we'd like to see to enable certain features in the allow map
7848 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7849 .how = SP4_MACH_CRED,
7850 .enforce.u.words = {
7851 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7852 1 << (OP_EXCHANGE_ID - 32) |
7853 1 << (OP_CREATE_SESSION - 32) |
7854 1 << (OP_DESTROY_SESSION - 32) |
7855 1 << (OP_DESTROY_CLIENTID - 32)
7858 [0] = 1 << (OP_CLOSE) |
7859 1 << (OP_OPEN_DOWNGRADE) |
7861 1 << (OP_DELEGRETURN) |
7863 [1] = 1 << (OP_SECINFO - 32) |
7864 1 << (OP_SECINFO_NO_NAME - 32) |
7865 1 << (OP_LAYOUTRETURN - 32) |
7866 1 << (OP_TEST_STATEID - 32) |
7867 1 << (OP_FREE_STATEID - 32) |
7868 1 << (OP_WRITE - 32)
7873 * Select the state protection mode for client `clp' given the server results
7874 * from exchange_id in `sp'.
7876 * Returns 0 on success, negative errno otherwise.
7878 static int nfs4_sp4_select_mode(struct nfs_client *clp,
7879 struct nfs41_state_protection *sp)
7881 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7882 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7883 1 << (OP_EXCHANGE_ID - 32) |
7884 1 << (OP_CREATE_SESSION - 32) |
7885 1 << (OP_DESTROY_SESSION - 32) |
7886 1 << (OP_DESTROY_CLIENTID - 32)
7888 unsigned long flags = 0;
7892 if (sp->how == SP4_MACH_CRED) {
7893 /* Print state protect result */
7894 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7895 for (i = 0; i <= LAST_NFS4_OP; i++) {
7896 if (test_bit(i, sp->enforce.u.longs))
7897 dfprintk(MOUNT, " enforce op %d\n", i);
7898 if (test_bit(i, sp->allow.u.longs))
7899 dfprintk(MOUNT, " allow op %d\n", i);
7902 /* make sure nothing is on enforce list that isn't supported */
7903 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7904 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7905 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7912 * Minimal mode - state operations are allowed to use machine
7913 * credential. Note this already happens by default, so the
7914 * client doesn't have to do anything more than the negotiation.
7916 * NOTE: we don't care if EXCHANGE_ID is in the list -
7917 * we're already using the machine cred for exchange_id
7918 * and will never use a different cred.
7920 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7921 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7922 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7923 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7924 dfprintk(MOUNT, "sp4_mach_cred:\n");
7925 dfprintk(MOUNT, " minimal mode enabled\n");
7926 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
7928 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7933 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
7934 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7935 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
7936 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7937 dfprintk(MOUNT, " cleanup mode enabled\n");
7938 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
7941 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7942 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7943 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
7946 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7947 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7948 dfprintk(MOUNT, " secinfo mode enabled\n");
7949 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
7952 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7953 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7954 dfprintk(MOUNT, " stateid mode enabled\n");
7955 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
7958 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7959 dfprintk(MOUNT, " write mode enabled\n");
7960 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
7963 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7964 dfprintk(MOUNT, " commit mode enabled\n");
7965 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
7969 clp->cl_sp4_flags = flags;
7973 struct nfs41_exchange_id_data {
7974 struct nfs41_exchange_id_res res;
7975 struct nfs41_exchange_id_args args;
7978 static void nfs4_exchange_id_release(void *data)
7980 struct nfs41_exchange_id_data *cdata =
7981 (struct nfs41_exchange_id_data *)data;
7983 nfs_put_client(cdata->args.client);
7984 kfree(cdata->res.impl_id);
7985 kfree(cdata->res.server_scope);
7986 kfree(cdata->res.server_owner);
7990 static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7991 .rpc_release = nfs4_exchange_id_release,
7995 * _nfs4_proc_exchange_id()
7997 * Wrapper for EXCHANGE_ID operation.
7999 static struct rpc_task *
8000 nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
8001 u32 sp4_how, struct rpc_xprt *xprt)
8003 struct rpc_message msg = {
8004 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
8007 struct rpc_task_setup task_setup_data = {
8008 .rpc_client = clp->cl_rpcclient,
8009 .callback_ops = &nfs4_exchange_id_call_ops,
8010 .rpc_message = &msg,
8011 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
8013 struct nfs41_exchange_id_data *calldata;
8016 if (!refcount_inc_not_zero(&clp->cl_count))
8017 return ERR_PTR(-EIO);
8020 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8024 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
8026 status = nfs4_init_uniform_client_string(clp);
8030 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
8033 if (unlikely(calldata->res.server_owner == NULL))
8036 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
8038 if (unlikely(calldata->res.server_scope == NULL))
8039 goto out_server_owner;
8041 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
8042 if (unlikely(calldata->res.impl_id == NULL))
8043 goto out_server_scope;
8047 calldata->args.state_protect.how = SP4_NONE;
8051 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
8061 task_setup_data.rpc_xprt = xprt;
8062 task_setup_data.flags |= RPC_TASK_SOFTCONN;
8063 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
8064 sizeof(calldata->args.verifier.data));
8066 calldata->args.client = clp;
8067 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
8068 EXCHGID4_FLAG_BIND_PRINC_STATEID;
8069 #ifdef CONFIG_NFS_V4_1_MIGRATION
8070 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
8072 msg.rpc_argp = &calldata->args;
8073 msg.rpc_resp = &calldata->res;
8074 task_setup_data.callback_data = calldata;
8076 return rpc_run_task(&task_setup_data);
8079 kfree(calldata->res.impl_id);
8081 kfree(calldata->res.server_scope);
8083 kfree(calldata->res.server_owner);
8087 nfs_put_client(clp);
8088 return ERR_PTR(status);
8092 * _nfs4_proc_exchange_id()
8094 * Wrapper for EXCHANGE_ID operation.
8096 static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
8099 struct rpc_task *task;
8100 struct nfs41_exchange_id_args *argp;
8101 struct nfs41_exchange_id_res *resp;
8104 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
8106 return PTR_ERR(task);
8108 argp = task->tk_msg.rpc_argp;
8109 resp = task->tk_msg.rpc_resp;
8110 status = task->tk_status;
8114 status = nfs4_check_cl_exchange_flags(resp->flags);
8118 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8122 clp->cl_clientid = resp->clientid;
8123 clp->cl_exchange_flags = resp->flags;
8124 clp->cl_seqid = resp->seqid;
8125 /* Client ID is not confirmed */
8126 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8127 clear_bit(NFS4_SESSION_ESTABLISHED,
8128 &clp->cl_session->session_state);
8130 if (clp->cl_serverscope != NULL &&
8131 !nfs41_same_server_scope(clp->cl_serverscope,
8132 resp->server_scope)) {
8133 dprintk("%s: server_scope mismatch detected\n",
8135 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8138 swap(clp->cl_serverowner, resp->server_owner);
8139 swap(clp->cl_serverscope, resp->server_scope);
8140 swap(clp->cl_implid, resp->impl_id);
8142 /* Save the EXCHANGE_ID verifier session trunk tests */
8143 memcpy(clp->cl_confirm.data, argp->verifier.data,
8144 sizeof(clp->cl_confirm.data));
8146 trace_nfs4_exchange_id(clp, status);
8152 * nfs4_proc_exchange_id()
8154 * Returns zero, a negative errno, or a negative NFS4ERR status code.
8156 * Since the clientid has expired, all compounds using sessions
8157 * associated with the stale clientid will be returning
8158 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
8159 * be in some phase of session reset.
8161 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
8163 int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
8165 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8168 /* try SP4_MACH_CRED if krb5i/p */
8169 if (authflavor == RPC_AUTH_GSS_KRB5I ||
8170 authflavor == RPC_AUTH_GSS_KRB5P) {
8171 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
8177 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
8181 * nfs4_test_session_trunk
8183 * This is an add_xprt_test() test function called from
8184 * rpc_clnt_setup_test_and_add_xprt.
8186 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8187 * and is dereferrenced in nfs4_exchange_id_release
8189 * Upon success, add the new transport to the rpc_clnt
8191 * @clnt: struct rpc_clnt to get new transport
8192 * @xprt: the rpc_xprt to test
8193 * @data: call data for _nfs4_proc_exchange_id.
8195 void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
8198 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
8199 struct rpc_task *task;
8204 dprintk("--> %s try %s\n", __func__,
8205 xprt->address_strings[RPC_DISPLAY_ADDR]);
8207 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8209 /* Test connection for session trunking. Async exchange_id call */
8210 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8214 status = task->tk_status;
8216 status = nfs4_detect_session_trunking(adata->clp,
8217 task->tk_msg.rpc_resp, xprt);
8220 rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
8224 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
8226 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
8227 const struct cred *cred)
8229 struct rpc_message msg = {
8230 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8236 status = rpc_call_sync(clp->cl_rpcclient, &msg,
8237 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
8238 trace_nfs4_destroy_clientid(clp, status);
8240 dprintk("NFS: Got error %d from the server %s on "
8241 "DESTROY_CLIENTID.", status, clp->cl_hostname);
8245 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
8246 const struct cred *cred)
8251 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8252 ret = _nfs4_proc_destroy_clientid(clp, cred);
8254 case -NFS4ERR_DELAY:
8255 case -NFS4ERR_CLIENTID_BUSY:
8265 int nfs4_destroy_clientid(struct nfs_client *clp)
8267 const struct cred *cred;
8270 if (clp->cl_mvops->minor_version < 1)
8272 if (clp->cl_exchange_flags == 0)
8274 if (clp->cl_preserve_clid)
8276 cred = nfs4_get_clid_cred(clp);
8277 ret = nfs4_proc_destroy_clientid(clp, cred);
8281 case -NFS4ERR_STALE_CLIENTID:
8282 clp->cl_exchange_flags = 0;
8288 #endif /* CONFIG_NFS_V4_1 */
8290 struct nfs4_get_lease_time_data {
8291 struct nfs4_get_lease_time_args *args;
8292 struct nfs4_get_lease_time_res *res;
8293 struct nfs_client *clp;
8296 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
8299 struct nfs4_get_lease_time_data *data =
8300 (struct nfs4_get_lease_time_data *)calldata;
8302 dprintk("--> %s\n", __func__);
8303 /* just setup sequence, do not trigger session recovery
8304 since we're invoked within one */
8305 nfs4_setup_sequence(data->clp,
8306 &data->args->la_seq_args,
8307 &data->res->lr_seq_res,
8309 dprintk("<-- %s\n", __func__);
8313 * Called from nfs4_state_manager thread for session setup, so don't recover
8314 * from sequence operation or clientid errors.
8316 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8318 struct nfs4_get_lease_time_data *data =
8319 (struct nfs4_get_lease_time_data *)calldata;
8321 dprintk("--> %s\n", __func__);
8322 if (!nfs4_sequence_done(task, &data->res->lr_seq_res))
8324 switch (task->tk_status) {
8325 case -NFS4ERR_DELAY:
8326 case -NFS4ERR_GRACE:
8327 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
8328 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8329 task->tk_status = 0;
8331 case -NFS4ERR_RETRY_UNCACHED_REP:
8332 rpc_restart_call_prepare(task);
8335 dprintk("<-- %s\n", __func__);
8338 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
8339 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8340 .rpc_call_done = nfs4_get_lease_time_done,
8343 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8345 struct rpc_task *task;
8346 struct nfs4_get_lease_time_args args;
8347 struct nfs4_get_lease_time_res res = {
8348 .lr_fsinfo = fsinfo,
8350 struct nfs4_get_lease_time_data data = {
8355 struct rpc_message msg = {
8356 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8360 struct rpc_task_setup task_setup = {
8361 .rpc_client = clp->cl_rpcclient,
8362 .rpc_message = &msg,
8363 .callback_ops = &nfs4_get_lease_time_ops,
8364 .callback_data = &data,
8365 .flags = RPC_TASK_TIMEOUT,
8369 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
8370 task = rpc_run_task(&task_setup);
8373 return PTR_ERR(task);
8375 status = task->tk_status;
8380 #ifdef CONFIG_NFS_V4_1
8383 * Initialize the values to be used by the client in CREATE_SESSION
8384 * If nfs4_init_session set the fore channel request and response sizes,
8387 * Set the back channel max_resp_sz_cached to zero to force the client to
8388 * always set csa_cachethis to FALSE because the current implementation
8389 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8391 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8392 struct rpc_clnt *clnt)
8394 unsigned int max_rqst_sz, max_resp_sz;
8395 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
8396 unsigned int max_bc_slots = rpc_num_bc_slots(clnt);
8398 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8399 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8401 /* Fore channel attributes */
8402 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8403 args->fc_attrs.max_resp_sz = max_resp_sz;
8404 args->fc_attrs.max_ops = NFS4_MAX_OPS;
8405 args->fc_attrs.max_reqs = max_session_slots;
8407 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
8408 "max_ops=%u max_reqs=%u\n",
8410 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
8411 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
8413 /* Back channel attributes */
8414 args->bc_attrs.max_rqst_sz = max_bc_payload;
8415 args->bc_attrs.max_resp_sz = max_bc_payload;
8416 args->bc_attrs.max_resp_sz_cached = 0;
8417 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
8418 args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
8419 if (args->bc_attrs.max_reqs > max_bc_slots)
8420 args->bc_attrs.max_reqs = max_bc_slots;
8422 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8423 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8425 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8426 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8427 args->bc_attrs.max_reqs);
8430 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8431 struct nfs41_create_session_res *res)
8433 struct nfs4_channel_attrs *sent = &args->fc_attrs;
8434 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
8436 if (rcvd->max_resp_sz > sent->max_resp_sz)
8439 * Our requested max_ops is the minimum we need; we're not
8440 * prepared to break up compounds into smaller pieces than that.
8441 * So, no point even trying to continue if the server won't
8444 if (rcvd->max_ops < sent->max_ops)
8446 if (rcvd->max_reqs == 0)
8448 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8449 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
8453 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8454 struct nfs41_create_session_res *res)
8456 struct nfs4_channel_attrs *sent = &args->bc_attrs;
8457 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
8459 if (!(res->flags & SESSION4_BACK_CHAN))
8461 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8463 if (rcvd->max_resp_sz < sent->max_resp_sz)
8465 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8467 if (rcvd->max_ops > sent->max_ops)
8469 if (rcvd->max_reqs > sent->max_reqs)
8475 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
8476 struct nfs41_create_session_res *res)
8480 ret = nfs4_verify_fore_channel_attrs(args, res);
8483 return nfs4_verify_back_channel_attrs(args, res);
8486 static void nfs4_update_session(struct nfs4_session *session,
8487 struct nfs41_create_session_res *res)
8489 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
8490 /* Mark client id and session as being confirmed */
8491 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8492 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
8493 session->flags = res->flags;
8494 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
8495 if (res->flags & SESSION4_BACK_CHAN)
8496 memcpy(&session->bc_attrs, &res->bc_attrs,
8497 sizeof(session->bc_attrs));
8500 static int _nfs4_proc_create_session(struct nfs_client *clp,
8501 const struct cred *cred)
8503 struct nfs4_session *session = clp->cl_session;
8504 struct nfs41_create_session_args args = {
8506 .clientid = clp->cl_clientid,
8507 .seqid = clp->cl_seqid,
8508 .cb_program = NFS4_CALLBACK,
8510 struct nfs41_create_session_res res;
8512 struct rpc_message msg = {
8513 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8520 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
8521 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
8523 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
8524 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
8525 trace_nfs4_create_session(clp, status);
8528 case -NFS4ERR_STALE_CLIENTID:
8529 case -NFS4ERR_DELAY:
8538 /* Verify the session's negotiated channel_attrs values */
8539 status = nfs4_verify_channel_attrs(&args, &res);
8540 /* Increment the clientid slot sequence id */
8543 nfs4_update_session(session, &res);
8550 * Issues a CREATE_SESSION operation to the server.
8551 * It is the responsibility of the caller to verify the session is
8552 * expired before calling this routine.
8554 int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
8558 struct nfs4_session *session = clp->cl_session;
8560 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8562 status = _nfs4_proc_create_session(clp, cred);
8566 /* Init or reset the session slot tables */
8567 status = nfs4_setup_session_slot_tables(session);
8568 dprintk("slot table setup returned %d\n", status);
8572 ptr = (unsigned *)&session->sess_id.data[0];
8573 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8574 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
8576 dprintk("<-- %s\n", __func__);
8581 * Issue the over-the-wire RPC DESTROY_SESSION.
8582 * The caller must serialize access to this routine.
8584 int nfs4_proc_destroy_session(struct nfs4_session *session,
8585 const struct cred *cred)
8587 struct rpc_message msg = {
8588 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8589 .rpc_argp = session,
8594 dprintk("--> nfs4_proc_destroy_session\n");
8596 /* session is still being setup */
8597 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8600 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
8601 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
8602 trace_nfs4_destroy_session(session->clp, status);
8605 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
8606 "Session has been destroyed regardless...\n", status);
8608 dprintk("<-- nfs4_proc_destroy_session\n");
8613 * Renew the cl_session lease.
8615 struct nfs4_sequence_data {
8616 struct nfs_client *clp;
8617 struct nfs4_sequence_args args;
8618 struct nfs4_sequence_res res;
8621 static void nfs41_sequence_release(void *data)
8623 struct nfs4_sequence_data *calldata = data;
8624 struct nfs_client *clp = calldata->clp;
8626 if (refcount_read(&clp->cl_count) > 1)
8627 nfs4_schedule_state_renewal(clp);
8628 nfs_put_client(clp);
8632 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8634 switch(task->tk_status) {
8635 case -NFS4ERR_DELAY:
8636 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8639 nfs4_schedule_lease_recovery(clp);
8644 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
8646 struct nfs4_sequence_data *calldata = data;
8647 struct nfs_client *clp = calldata->clp;
8649 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8652 trace_nfs4_sequence(clp, task->tk_status);
8653 if (task->tk_status < 0) {
8654 dprintk("%s ERROR %d\n", __func__, task->tk_status);
8655 if (refcount_read(&clp->cl_count) == 1)
8658 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8659 rpc_restart_call_prepare(task);
8663 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
8665 dprintk("<-- %s\n", __func__);
8668 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8670 struct nfs4_sequence_data *calldata = data;
8671 struct nfs_client *clp = calldata->clp;
8672 struct nfs4_sequence_args *args;
8673 struct nfs4_sequence_res *res;
8675 args = task->tk_msg.rpc_argp;
8676 res = task->tk_msg.rpc_resp;
8678 nfs4_setup_sequence(clp, args, res, task);
8681 static const struct rpc_call_ops nfs41_sequence_ops = {
8682 .rpc_call_done = nfs41_sequence_call_done,
8683 .rpc_call_prepare = nfs41_sequence_prepare,
8684 .rpc_release = nfs41_sequence_release,
8687 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8688 const struct cred *cred,
8689 struct nfs4_slot *slot,
8692 struct nfs4_sequence_data *calldata;
8693 struct rpc_message msg = {
8694 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8697 struct rpc_task_setup task_setup_data = {
8698 .rpc_client = clp->cl_rpcclient,
8699 .rpc_message = &msg,
8700 .callback_ops = &nfs41_sequence_ops,
8701 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
8703 struct rpc_task *ret;
8705 ret = ERR_PTR(-EIO);
8706 if (!refcount_inc_not_zero(&clp->cl_count))
8709 ret = ERR_PTR(-ENOMEM);
8710 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8711 if (calldata == NULL)
8713 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
8714 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
8715 msg.rpc_argp = &calldata->args;
8716 msg.rpc_resp = &calldata->res;
8717 calldata->clp = clp;
8718 task_setup_data.callback_data = calldata;
8720 ret = rpc_run_task(&task_setup_data);
8725 nfs_put_client(clp);
8727 nfs41_release_slot(slot);
8731 static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
8733 struct rpc_task *task;
8736 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
8738 task = _nfs41_proc_sequence(clp, cred, NULL, false);
8740 ret = PTR_ERR(task);
8742 rpc_put_task_async(task);
8743 dprintk("<-- %s status=%d\n", __func__, ret);
8747 static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
8749 struct rpc_task *task;
8752 task = _nfs41_proc_sequence(clp, cred, NULL, true);
8754 ret = PTR_ERR(task);
8757 ret = rpc_wait_for_completion_task(task);
8759 ret = task->tk_status;
8762 dprintk("<-- %s status=%d\n", __func__, ret);
8766 struct nfs4_reclaim_complete_data {
8767 struct nfs_client *clp;
8768 struct nfs41_reclaim_complete_args arg;
8769 struct nfs41_reclaim_complete_res res;
8772 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8774 struct nfs4_reclaim_complete_data *calldata = data;
8776 nfs4_setup_sequence(calldata->clp,
8777 &calldata->arg.seq_args,
8778 &calldata->res.seq_res,
8782 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8784 switch(task->tk_status) {
8786 wake_up_all(&clp->cl_lock_waitq);
8788 case -NFS4ERR_COMPLETE_ALREADY:
8789 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8791 case -NFS4ERR_DELAY:
8792 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8794 case -NFS4ERR_RETRY_UNCACHED_REP:
8796 case -NFS4ERR_BADSESSION:
8797 case -NFS4ERR_DEADSESSION:
8798 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8799 nfs4_schedule_session_recovery(clp->cl_session,
8803 nfs4_schedule_lease_recovery(clp);
8808 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8810 struct nfs4_reclaim_complete_data *calldata = data;
8811 struct nfs_client *clp = calldata->clp;
8812 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8814 dprintk("--> %s\n", __func__);
8815 if (!nfs41_sequence_done(task, res))
8818 trace_nfs4_reclaim_complete(clp, task->tk_status);
8819 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8820 rpc_restart_call_prepare(task);
8823 dprintk("<-- %s\n", __func__);
8826 static void nfs4_free_reclaim_complete_data(void *data)
8828 struct nfs4_reclaim_complete_data *calldata = data;
8833 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8834 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8835 .rpc_call_done = nfs4_reclaim_complete_done,
8836 .rpc_release = nfs4_free_reclaim_complete_data,
8840 * Issue a global reclaim complete.
8842 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8843 const struct cred *cred)
8845 struct nfs4_reclaim_complete_data *calldata;
8846 struct rpc_task *task;
8847 struct rpc_message msg = {
8848 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
8851 struct rpc_task_setup task_setup_data = {
8852 .rpc_client = clp->cl_rpcclient,
8853 .rpc_message = &msg,
8854 .callback_ops = &nfs4_reclaim_complete_call_ops,
8855 .flags = RPC_TASK_ASYNC | RPC_TASK_NO_ROUND_ROBIN,
8857 int status = -ENOMEM;
8859 dprintk("--> %s\n", __func__);
8860 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8861 if (calldata == NULL)
8863 calldata->clp = clp;
8864 calldata->arg.one_fs = 0;
8866 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
8867 msg.rpc_argp = &calldata->arg;
8868 msg.rpc_resp = &calldata->res;
8869 task_setup_data.callback_data = calldata;
8870 task = rpc_run_task(&task_setup_data);
8872 status = PTR_ERR(task);
8875 status = rpc_wait_for_completion_task(task);
8877 status = task->tk_status;
8880 dprintk("<-- %s status=%d\n", __func__, status);
8885 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8887 struct nfs4_layoutget *lgp = calldata;
8888 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
8890 dprintk("--> %s\n", __func__);
8891 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
8892 &lgp->res.seq_res, task);
8893 dprintk("<-- %s\n", __func__);
8896 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8898 struct nfs4_layoutget *lgp = calldata;
8900 dprintk("--> %s\n", __func__);
8901 nfs41_sequence_process(task, &lgp->res.seq_res);
8902 dprintk("<-- %s\n", __func__);
8906 nfs4_layoutget_handle_exception(struct rpc_task *task,
8907 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8909 struct inode *inode = lgp->args.inode;
8910 struct nfs_server *server = NFS_SERVER(inode);
8911 struct pnfs_layout_hdr *lo;
8912 int nfs4err = task->tk_status;
8913 int err, status = 0;
8916 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
8918 nfs4_sequence_free_slot(&lgp->res.seq_res);
8925 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8926 * on the file. set tk_status to -ENODATA to tell upper layer to
8929 case -NFS4ERR_LAYOUTUNAVAILABLE:
8933 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8934 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8936 case -NFS4ERR_BADLAYOUT:
8937 status = -EOVERFLOW;
8940 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
8941 * (or clients) writing to the same RAID stripe except when
8942 * the minlength argument is 0 (see RFC5661 section 18.43.3).
8944 * Treat it like we would RECALLCONFLICT -- we retry for a little
8945 * while, and then eventually give up.
8947 case -NFS4ERR_LAYOUTTRYLATER:
8948 if (lgp->args.minlength == 0) {
8949 status = -EOVERFLOW;
8954 case -NFS4ERR_RECALLCONFLICT:
8955 status = -ERECALLCONFLICT;
8957 case -NFS4ERR_DELEG_REVOKED:
8958 case -NFS4ERR_ADMIN_REVOKED:
8959 case -NFS4ERR_EXPIRED:
8960 case -NFS4ERR_BAD_STATEID:
8961 exception->timeout = 0;
8962 spin_lock(&inode->i_lock);
8963 lo = NFS_I(inode)->layout;
8964 /* If the open stateid was bad, then recover it. */
8965 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8966 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
8967 spin_unlock(&inode->i_lock);
8968 exception->state = lgp->args.ctx->state;
8969 exception->stateid = &lgp->args.stateid;
8974 * Mark the bad layout state as invalid, then retry
8976 pnfs_mark_layout_stateid_invalid(lo, &head);
8977 spin_unlock(&inode->i_lock);
8978 nfs_commit_inode(inode, 0);
8979 pnfs_free_lseg_list(&head);
8984 err = nfs4_handle_exception(server, nfs4err, exception);
8986 if (exception->retry)
8992 dprintk("<-- %s\n", __func__);
8996 size_t max_response_pages(struct nfs_server *server)
8998 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8999 return nfs_page_array_len(0, max_resp_sz);
9002 static void nfs4_layoutget_release(void *calldata)
9004 struct nfs4_layoutget *lgp = calldata;
9006 dprintk("--> %s\n", __func__);
9007 nfs4_sequence_free_slot(&lgp->res.seq_res);
9008 pnfs_layoutget_free(lgp);
9009 dprintk("<-- %s\n", __func__);
9012 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
9013 .rpc_call_prepare = nfs4_layoutget_prepare,
9014 .rpc_call_done = nfs4_layoutget_done,
9015 .rpc_release = nfs4_layoutget_release,
9018 struct pnfs_layout_segment *
9019 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
9021 struct inode *inode = lgp->args.inode;
9022 struct nfs_server *server = NFS_SERVER(inode);
9023 struct rpc_task *task;
9024 struct rpc_message msg = {
9025 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
9026 .rpc_argp = &lgp->args,
9027 .rpc_resp = &lgp->res,
9028 .rpc_cred = lgp->cred,
9030 struct rpc_task_setup task_setup_data = {
9031 .rpc_client = server->client,
9032 .rpc_message = &msg,
9033 .callback_ops = &nfs4_layoutget_call_ops,
9034 .callback_data = lgp,
9035 .flags = RPC_TASK_ASYNC,
9037 struct pnfs_layout_segment *lseg = NULL;
9038 struct nfs4_exception exception = {
9040 .timeout = *timeout,
9044 dprintk("--> %s\n", __func__);
9046 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
9047 pnfs_get_layout_hdr(NFS_I(inode)->layout);
9049 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
9051 task = rpc_run_task(&task_setup_data);
9053 return ERR_CAST(task);
9054 status = rpc_wait_for_completion_task(task);
9058 if (task->tk_status < 0) {
9059 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
9060 *timeout = exception.timeout;
9061 } else if (lgp->res.layoutp->len == 0) {
9063 *timeout = nfs4_update_delay(&exception.timeout);
9065 lseg = pnfs_layout_process(lgp);
9067 trace_nfs4_layoutget(lgp->args.ctx,
9074 dprintk("<-- %s status=%d\n", __func__, status);
9076 return ERR_PTR(status);
9081 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
9083 struct nfs4_layoutreturn *lrp = calldata;
9085 dprintk("--> %s\n", __func__);
9086 nfs4_setup_sequence(lrp->clp,
9087 &lrp->args.seq_args,
9090 if (!pnfs_layout_is_valid(lrp->args.layout))
9094 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
9096 struct nfs4_layoutreturn *lrp = calldata;
9097 struct nfs_server *server;
9099 dprintk("--> %s\n", __func__);
9101 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
9104 server = NFS_SERVER(lrp->args.inode);
9105 switch (task->tk_status) {
9106 case -NFS4ERR_OLD_STATEID:
9107 if (nfs4_layoutreturn_refresh_stateid(&lrp->args.stateid,
9113 task->tk_status = 0;
9117 case -NFS4ERR_DELAY:
9118 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
9122 dprintk("<-- %s\n", __func__);
9125 task->tk_status = 0;
9126 nfs4_sequence_free_slot(&lrp->res.seq_res);
9127 rpc_restart_call_prepare(task);
9130 static void nfs4_layoutreturn_release(void *calldata)
9132 struct nfs4_layoutreturn *lrp = calldata;
9133 struct pnfs_layout_hdr *lo = lrp->args.layout;
9135 dprintk("--> %s\n", __func__);
9136 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
9137 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
9138 nfs4_sequence_free_slot(&lrp->res.seq_res);
9139 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9140 lrp->ld_private.ops->free(&lrp->ld_private);
9141 pnfs_put_layout_hdr(lrp->args.layout);
9142 nfs_iput_and_deactive(lrp->inode);
9144 dprintk("<-- %s\n", __func__);
9147 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9148 .rpc_call_prepare = nfs4_layoutreturn_prepare,
9149 .rpc_call_done = nfs4_layoutreturn_done,
9150 .rpc_release = nfs4_layoutreturn_release,
9153 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
9155 struct rpc_task *task;
9156 struct rpc_message msg = {
9157 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9158 .rpc_argp = &lrp->args,
9159 .rpc_resp = &lrp->res,
9160 .rpc_cred = lrp->cred,
9162 struct rpc_task_setup task_setup_data = {
9163 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
9164 .rpc_message = &msg,
9165 .callback_ops = &nfs4_layoutreturn_call_ops,
9166 .callback_data = lrp,
9170 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9171 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9172 &task_setup_data.rpc_client, &msg);
9174 dprintk("--> %s\n", __func__);
9176 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
9178 nfs4_layoutreturn_release(lrp);
9181 task_setup_data.flags |= RPC_TASK_ASYNC;
9183 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1, 0);
9184 task = rpc_run_task(&task_setup_data);
9186 return PTR_ERR(task);
9188 status = task->tk_status;
9189 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
9190 dprintk("<-- %s status=%d\n", __func__, status);
9196 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
9197 struct pnfs_device *pdev,
9198 const struct cred *cred)
9200 struct nfs4_getdeviceinfo_args args = {
9202 .notify_types = NOTIFY_DEVICEID4_CHANGE |
9203 NOTIFY_DEVICEID4_DELETE,
9205 struct nfs4_getdeviceinfo_res res = {
9208 struct rpc_message msg = {
9209 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9216 dprintk("--> %s\n", __func__);
9217 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
9218 if (res.notification & ~args.notify_types)
9219 dprintk("%s: unsupported notification\n", __func__);
9220 if (res.notification != args.notify_types)
9223 dprintk("<-- %s status=%d\n", __func__, status);
9228 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9229 struct pnfs_device *pdev,
9230 const struct cred *cred)
9232 struct nfs4_exception exception = { };
9236 err = nfs4_handle_exception(server,
9237 _nfs4_proc_getdeviceinfo(server, pdev, cred),
9239 } while (exception.retry);
9242 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9244 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9246 struct nfs4_layoutcommit_data *data = calldata;
9247 struct nfs_server *server = NFS_SERVER(data->args.inode);
9249 nfs4_setup_sequence(server->nfs_client,
9250 &data->args.seq_args,
9256 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9258 struct nfs4_layoutcommit_data *data = calldata;
9259 struct nfs_server *server = NFS_SERVER(data->args.inode);
9261 if (!nfs41_sequence_done(task, &data->res.seq_res))
9264 switch (task->tk_status) { /* Just ignore these failures */
9265 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9266 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9267 case -NFS4ERR_BADLAYOUT: /* no layout */
9268 case -NFS4ERR_GRACE: /* loca_recalim always false */
9269 task->tk_status = 0;
9273 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
9274 rpc_restart_call_prepare(task);
9280 static void nfs4_layoutcommit_release(void *calldata)
9282 struct nfs4_layoutcommit_data *data = calldata;
9284 pnfs_cleanup_layoutcommit(data);
9285 nfs_post_op_update_inode_force_wcc(data->args.inode,
9287 put_cred(data->cred);
9288 nfs_iput_and_deactive(data->inode);
9292 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9293 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9294 .rpc_call_done = nfs4_layoutcommit_done,
9295 .rpc_release = nfs4_layoutcommit_release,
9299 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
9301 struct rpc_message msg = {
9302 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9303 .rpc_argp = &data->args,
9304 .rpc_resp = &data->res,
9305 .rpc_cred = data->cred,
9307 struct rpc_task_setup task_setup_data = {
9308 .task = &data->task,
9309 .rpc_client = NFS_CLIENT(data->args.inode),
9310 .rpc_message = &msg,
9311 .callback_ops = &nfs4_layoutcommit_ops,
9312 .callback_data = data,
9314 struct rpc_task *task;
9317 dprintk("NFS: initiating layoutcommit call. sync %d "
9318 "lbw: %llu inode %lu\n", sync,
9319 data->args.lastbytewritten,
9320 data->args.inode->i_ino);
9323 data->inode = nfs_igrab_and_active(data->args.inode);
9324 if (data->inode == NULL) {
9325 nfs4_layoutcommit_release(data);
9328 task_setup_data.flags = RPC_TASK_ASYNC;
9330 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
9331 task = rpc_run_task(&task_setup_data);
9333 return PTR_ERR(task);
9335 status = task->tk_status;
9336 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
9337 dprintk("%s: status %d\n", __func__, status);
9343 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9344 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9347 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9348 struct nfs_fsinfo *info,
9349 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
9351 struct nfs41_secinfo_no_name_args args = {
9352 .style = SECINFO_STYLE_CURRENT_FH,
9354 struct nfs4_secinfo_res res = {
9357 struct rpc_message msg = {
9358 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9362 struct rpc_clnt *clnt = server->client;
9363 const struct cred *cred = NULL;
9366 if (use_integrity) {
9367 clnt = server->nfs_client->cl_rpcclient;
9368 cred = nfs4_get_clid_cred(server->nfs_client);
9369 msg.rpc_cred = cred;
9372 dprintk("--> %s\n", __func__);
9373 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
9374 &res.seq_res, RPC_TASK_NO_ROUND_ROBIN);
9375 dprintk("<-- %s status=%d\n", __func__, status);
9383 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9384 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9386 struct nfs4_exception exception = {
9387 .interruptible = true,
9391 /* first try using integrity protection */
9392 err = -NFS4ERR_WRONGSEC;
9394 /* try to use integrity protection with machine cred */
9395 if (_nfs4_is_integrity_protected(server->nfs_client))
9396 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9400 * if unable to use integrity protection, or SECINFO with
9401 * integrity protection returns NFS4ERR_WRONGSEC (which is
9402 * disallowed by spec, but exists in deployed servers) use
9403 * the current filesystem's rpc_client and the user cred.
9405 if (err == -NFS4ERR_WRONGSEC)
9406 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9411 case -NFS4ERR_WRONGSEC:
9415 err = nfs4_handle_exception(server, err, &exception);
9417 } while (exception.retry);
9423 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9424 struct nfs_fsinfo *info)
9428 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
9429 struct nfs4_secinfo_flavors *flavors;
9430 struct nfs4_secinfo4 *secinfo;
9433 page = alloc_page(GFP_KERNEL);
9439 flavors = page_address(page);
9440 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9443 * Fall back on "guess and check" method if
9444 * the server doesn't support SECINFO_NO_NAME
9446 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
9447 err = nfs4_find_root_sec(server, fhandle, info);
9453 for (i = 0; i < flavors->num_flavors; i++) {
9454 secinfo = &flavors->flavors[i];
9456 switch (secinfo->flavor) {
9460 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9461 &secinfo->flavor_info);
9464 flavor = RPC_AUTH_MAXFLAVOR;
9468 if (!nfs_auth_info_match(&server->auth_info, flavor))
9469 flavor = RPC_AUTH_MAXFLAVOR;
9471 if (flavor != RPC_AUTH_MAXFLAVOR) {
9472 err = nfs4_lookup_root_sec(server, fhandle,
9479 if (flavor == RPC_AUTH_MAXFLAVOR)
9490 static int _nfs41_test_stateid(struct nfs_server *server,
9491 nfs4_stateid *stateid,
9492 const struct cred *cred)
9495 struct nfs41_test_stateid_args args = {
9498 struct nfs41_test_stateid_res res;
9499 struct rpc_message msg = {
9500 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9505 struct rpc_clnt *rpc_client = server->client;
9507 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9510 dprintk("NFS call test_stateid %p\n", stateid);
9511 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
9512 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
9513 &args.seq_args, &res.seq_res);
9514 if (status != NFS_OK) {
9515 dprintk("NFS reply test_stateid: failed, %d\n", status);
9518 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
9522 static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9523 int err, struct nfs4_exception *exception)
9525 exception->retry = 0;
9527 case -NFS4ERR_DELAY:
9528 case -NFS4ERR_RETRY_UNCACHED_REP:
9529 nfs4_handle_exception(server, err, exception);
9531 case -NFS4ERR_BADSESSION:
9532 case -NFS4ERR_BADSLOT:
9533 case -NFS4ERR_BAD_HIGH_SLOT:
9534 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9535 case -NFS4ERR_DEADSESSION:
9536 nfs4_do_handle_exception(server, err, exception);
9541 * nfs41_test_stateid - perform a TEST_STATEID operation
9543 * @server: server / transport on which to perform the operation
9544 * @stateid: state ID to test
9547 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9548 * Otherwise a negative NFS4ERR value is returned if the operation
9549 * failed or the state ID is not currently valid.
9551 static int nfs41_test_stateid(struct nfs_server *server,
9552 nfs4_stateid *stateid,
9553 const struct cred *cred)
9555 struct nfs4_exception exception = {
9556 .interruptible = true,
9560 err = _nfs41_test_stateid(server, stateid, cred);
9561 nfs4_handle_delay_or_session_error(server, err, &exception);
9562 } while (exception.retry);
9566 struct nfs_free_stateid_data {
9567 struct nfs_server *server;
9568 struct nfs41_free_stateid_args args;
9569 struct nfs41_free_stateid_res res;
9572 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9574 struct nfs_free_stateid_data *data = calldata;
9575 nfs4_setup_sequence(data->server->nfs_client,
9576 &data->args.seq_args,
9581 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9583 struct nfs_free_stateid_data *data = calldata;
9585 nfs41_sequence_done(task, &data->res.seq_res);
9587 switch (task->tk_status) {
9588 case -NFS4ERR_DELAY:
9589 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
9590 rpc_restart_call_prepare(task);
9594 static void nfs41_free_stateid_release(void *calldata)
9599 static const struct rpc_call_ops nfs41_free_stateid_ops = {
9600 .rpc_call_prepare = nfs41_free_stateid_prepare,
9601 .rpc_call_done = nfs41_free_stateid_done,
9602 .rpc_release = nfs41_free_stateid_release,
9606 * nfs41_free_stateid - perform a FREE_STATEID operation
9608 * @server: server / transport on which to perform the operation
9609 * @stateid: state ID to release
9611 * @privileged: set to true if this call needs to be privileged
9613 * Note: this function is always asynchronous.
9615 static int nfs41_free_stateid(struct nfs_server *server,
9616 const nfs4_stateid *stateid,
9617 const struct cred *cred,
9620 struct rpc_message msg = {
9621 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
9624 struct rpc_task_setup task_setup = {
9625 .rpc_client = server->client,
9626 .rpc_message = &msg,
9627 .callback_ops = &nfs41_free_stateid_ops,
9628 .flags = RPC_TASK_ASYNC,
9630 struct nfs_free_stateid_data *data;
9631 struct rpc_task *task;
9633 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9634 &task_setup.rpc_client, &msg);
9636 dprintk("NFS call free_stateid %p\n", stateid);
9637 data = kmalloc(sizeof(*data), GFP_NOFS);
9640 data->server = server;
9641 nfs4_stateid_copy(&data->args.stateid, stateid);
9643 task_setup.callback_data = data;
9645 msg.rpc_argp = &data->args;
9646 msg.rpc_resp = &data->res;
9647 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
9648 task = rpc_run_task(&task_setup);
9650 return PTR_ERR(task);
9656 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
9658 const struct cred *cred = lsp->ls_state->owner->so_cred;
9660 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
9661 nfs4_free_lock_state(server, lsp);
9664 static bool nfs41_match_stateid(const nfs4_stateid *s1,
9665 const nfs4_stateid *s2)
9667 if (s1->type != s2->type)
9670 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
9673 if (s1->seqid == s2->seqid)
9676 return s1->seqid == 0 || s2->seqid == 0;
9679 #endif /* CONFIG_NFS_V4_1 */
9681 static bool nfs4_match_stateid(const nfs4_stateid *s1,
9682 const nfs4_stateid *s2)
9684 return nfs4_stateid_match(s1, s2);
9688 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
9689 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9690 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9691 .recover_open = nfs4_open_reclaim,
9692 .recover_lock = nfs4_lock_reclaim,
9693 .establish_clid = nfs4_init_clientid,
9694 .detect_trunking = nfs40_discover_server_trunking,
9697 #if defined(CONFIG_NFS_V4_1)
9698 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
9699 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9700 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9701 .recover_open = nfs4_open_reclaim,
9702 .recover_lock = nfs4_lock_reclaim,
9703 .establish_clid = nfs41_init_clientid,
9704 .reclaim_complete = nfs41_proc_reclaim_complete,
9705 .detect_trunking = nfs41_discover_server_trunking,
9707 #endif /* CONFIG_NFS_V4_1 */
9709 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
9710 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9711 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
9712 .recover_open = nfs40_open_expired,
9713 .recover_lock = nfs4_lock_expired,
9714 .establish_clid = nfs4_init_clientid,
9717 #if defined(CONFIG_NFS_V4_1)
9718 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
9719 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9720 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
9721 .recover_open = nfs41_open_expired,
9722 .recover_lock = nfs41_lock_expired,
9723 .establish_clid = nfs41_init_clientid,
9725 #endif /* CONFIG_NFS_V4_1 */
9727 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
9728 .sched_state_renewal = nfs4_proc_async_renew,
9729 .get_state_renewal_cred = nfs4_get_renew_cred,
9730 .renew_lease = nfs4_proc_renew,
9733 #if defined(CONFIG_NFS_V4_1)
9734 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
9735 .sched_state_renewal = nfs41_proc_async_sequence,
9736 .get_state_renewal_cred = nfs4_get_machine_cred,
9737 .renew_lease = nfs4_proc_sequence,
9741 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
9742 .get_locations = _nfs40_proc_get_locations,
9743 .fsid_present = _nfs40_proc_fsid_present,
9746 #if defined(CONFIG_NFS_V4_1)
9747 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
9748 .get_locations = _nfs41_proc_get_locations,
9749 .fsid_present = _nfs41_proc_fsid_present,
9751 #endif /* CONFIG_NFS_V4_1 */
9753 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9755 .init_caps = NFS_CAP_READDIRPLUS
9756 | NFS_CAP_ATOMIC_OPEN
9757 | NFS_CAP_POSIX_LOCK,
9758 .init_client = nfs40_init_client,
9759 .shutdown_client = nfs40_shutdown_client,
9760 .match_stateid = nfs4_match_stateid,
9761 .find_root_sec = nfs4_find_root_sec,
9762 .free_lock_state = nfs4_release_lockowner,
9763 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
9764 .alloc_seqid = nfs_alloc_seqid,
9765 .call_sync_ops = &nfs40_call_sync_ops,
9766 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9767 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9768 .state_renewal_ops = &nfs40_state_renewal_ops,
9769 .mig_recovery_ops = &nfs40_mig_recovery_ops,
9772 #if defined(CONFIG_NFS_V4_1)
9773 static struct nfs_seqid *
9774 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9779 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9781 .init_caps = NFS_CAP_READDIRPLUS
9782 | NFS_CAP_ATOMIC_OPEN
9783 | NFS_CAP_POSIX_LOCK
9784 | NFS_CAP_STATEID_NFSV41
9785 | NFS_CAP_ATOMIC_OPEN_V1
9787 .init_client = nfs41_init_client,
9788 .shutdown_client = nfs41_shutdown_client,
9789 .match_stateid = nfs41_match_stateid,
9790 .find_root_sec = nfs41_find_root_sec,
9791 .free_lock_state = nfs41_free_lock_state,
9792 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
9793 .alloc_seqid = nfs_alloc_no_seqid,
9794 .session_trunk = nfs4_test_session_trunk,
9795 .call_sync_ops = &nfs41_call_sync_ops,
9796 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9797 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9798 .state_renewal_ops = &nfs41_state_renewal_ops,
9799 .mig_recovery_ops = &nfs41_mig_recovery_ops,
9803 #if defined(CONFIG_NFS_V4_2)
9804 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9806 .init_caps = NFS_CAP_READDIRPLUS
9807 | NFS_CAP_ATOMIC_OPEN
9808 | NFS_CAP_POSIX_LOCK
9809 | NFS_CAP_STATEID_NFSV41
9810 | NFS_CAP_ATOMIC_OPEN_V1
9814 | NFS_CAP_OFFLOAD_CANCEL
9815 | NFS_CAP_DEALLOCATE
9817 | NFS_CAP_LAYOUTSTATS
9819 | NFS_CAP_LAYOUTERROR,
9820 .init_client = nfs41_init_client,
9821 .shutdown_client = nfs41_shutdown_client,
9822 .match_stateid = nfs41_match_stateid,
9823 .find_root_sec = nfs41_find_root_sec,
9824 .free_lock_state = nfs41_free_lock_state,
9825 .call_sync_ops = &nfs41_call_sync_ops,
9826 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
9827 .alloc_seqid = nfs_alloc_no_seqid,
9828 .session_trunk = nfs4_test_session_trunk,
9829 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9830 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9831 .state_renewal_ops = &nfs41_state_renewal_ops,
9832 .mig_recovery_ops = &nfs41_mig_recovery_ops,
9836 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9837 [0] = &nfs_v4_0_minor_ops,
9838 #if defined(CONFIG_NFS_V4_1)
9839 [1] = &nfs_v4_1_minor_ops,
9841 #if defined(CONFIG_NFS_V4_2)
9842 [2] = &nfs_v4_2_minor_ops,
9846 static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
9848 ssize_t error, error2;
9850 error = generic_listxattr(dentry, list, size);
9858 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9861 return error + error2;
9864 static const struct inode_operations nfs4_dir_inode_operations = {
9865 .create = nfs_create,
9866 .lookup = nfs_lookup,
9867 .atomic_open = nfs_atomic_open,
9869 .unlink = nfs_unlink,
9870 .symlink = nfs_symlink,
9874 .rename = nfs_rename,
9875 .permission = nfs_permission,
9876 .getattr = nfs_getattr,
9877 .setattr = nfs_setattr,
9878 .listxattr = nfs4_listxattr,
9881 static const struct inode_operations nfs4_file_inode_operations = {
9882 .permission = nfs_permission,
9883 .getattr = nfs_getattr,
9884 .setattr = nfs_setattr,
9885 .listxattr = nfs4_listxattr,
9888 const struct nfs_rpc_ops nfs_v4_clientops = {
9889 .version = 4, /* protocol version */
9890 .dentry_ops = &nfs4_dentry_operations,
9891 .dir_inode_ops = &nfs4_dir_inode_operations,
9892 .file_inode_ops = &nfs4_file_inode_operations,
9893 .file_ops = &nfs4_file_operations,
9894 .getroot = nfs4_proc_get_root,
9895 .submount = nfs4_submount,
9896 .try_mount = nfs4_try_mount,
9897 .getattr = nfs4_proc_getattr,
9898 .setattr = nfs4_proc_setattr,
9899 .lookup = nfs4_proc_lookup,
9900 .lookupp = nfs4_proc_lookupp,
9901 .access = nfs4_proc_access,
9902 .readlink = nfs4_proc_readlink,
9903 .create = nfs4_proc_create,
9904 .remove = nfs4_proc_remove,
9905 .unlink_setup = nfs4_proc_unlink_setup,
9906 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
9907 .unlink_done = nfs4_proc_unlink_done,
9908 .rename_setup = nfs4_proc_rename_setup,
9909 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
9910 .rename_done = nfs4_proc_rename_done,
9911 .link = nfs4_proc_link,
9912 .symlink = nfs4_proc_symlink,
9913 .mkdir = nfs4_proc_mkdir,
9914 .rmdir = nfs4_proc_rmdir,
9915 .readdir = nfs4_proc_readdir,
9916 .mknod = nfs4_proc_mknod,
9917 .statfs = nfs4_proc_statfs,
9918 .fsinfo = nfs4_proc_fsinfo,
9919 .pathconf = nfs4_proc_pathconf,
9920 .set_capabilities = nfs4_server_capabilities,
9921 .decode_dirent = nfs4_decode_dirent,
9922 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
9923 .read_setup = nfs4_proc_read_setup,
9924 .read_done = nfs4_read_done,
9925 .write_setup = nfs4_proc_write_setup,
9926 .write_done = nfs4_write_done,
9927 .commit_setup = nfs4_proc_commit_setup,
9928 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
9929 .commit_done = nfs4_commit_done,
9930 .lock = nfs4_proc_lock,
9931 .clear_acl_cache = nfs4_zap_acl_attr,
9932 .close_context = nfs4_close_context,
9933 .open_context = nfs4_atomic_open,
9934 .have_delegation = nfs4_have_delegation,
9935 .alloc_client = nfs4_alloc_client,
9936 .init_client = nfs4_init_client,
9937 .free_client = nfs4_free_client,
9938 .create_server = nfs4_create_server,
9939 .clone_server = nfs_clone_server,
9942 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
9943 .name = XATTR_NAME_NFSV4_ACL,
9944 .list = nfs4_xattr_list_nfs4_acl,
9945 .get = nfs4_xattr_get_nfs4_acl,
9946 .set = nfs4_xattr_set_nfs4_acl,
9949 const struct xattr_handler *nfs4_xattr_handlers[] = {
9950 &nfs4_xattr_nfs4_acl_handler,
9951 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
9952 &nfs4_xattr_nfs4_label_handler,