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);
2184 case -NFS4ERR_BADSESSION:
2185 case -NFS4ERR_BADSLOT:
2186 case -NFS4ERR_BAD_HIGH_SLOT:
2187 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2188 case -NFS4ERR_DEADSESSION:
2189 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
2191 case -NFS4ERR_STALE_CLIENTID:
2192 case -NFS4ERR_STALE_STATEID:
2193 /* Don't recall a delegation if it was lost */
2194 nfs4_schedule_lease_recovery(server->nfs_client);
2196 case -NFS4ERR_MOVED:
2197 nfs4_schedule_migration_recovery(server);
2199 case -NFS4ERR_LEASE_MOVED:
2200 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2202 case -NFS4ERR_DELEG_REVOKED:
2203 case -NFS4ERR_ADMIN_REVOKED:
2204 case -NFS4ERR_EXPIRED:
2205 case -NFS4ERR_BAD_STATEID:
2206 case -NFS4ERR_OPENMODE:
2207 nfs_inode_find_state_and_recover(state->inode,
2209 nfs4_schedule_stateid_recovery(server, state);
2211 case -NFS4ERR_DELAY:
2212 case -NFS4ERR_GRACE:
2216 case -NFS4ERR_DENIED:
2218 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2220 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2227 int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2228 struct nfs4_state *state, const nfs4_stateid *stateid)
2230 struct nfs_server *server = NFS_SERVER(state->inode);
2231 struct nfs4_opendata *opendata;
2234 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2235 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2236 if (IS_ERR(opendata))
2237 return PTR_ERR(opendata);
2238 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
2239 if (!test_bit(NFS_O_RDWR_STATE, &state->flags)) {
2240 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2244 if (!test_bit(NFS_O_WRONLY_STATE, &state->flags)) {
2245 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2249 if (!test_bit(NFS_O_RDONLY_STATE, &state->flags)) {
2250 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2254 nfs_state_clear_delegation(state);
2256 nfs4_opendata_put(opendata);
2257 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
2260 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2262 struct nfs4_opendata *data = calldata;
2264 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2265 &data->c_arg.seq_args, &data->c_res.seq_res, task);
2268 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2270 struct nfs4_opendata *data = calldata;
2272 nfs40_sequence_done(task, &data->c_res.seq_res);
2274 data->rpc_status = task->tk_status;
2275 if (data->rpc_status == 0) {
2276 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
2277 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2278 renew_lease(data->o_res.server, data->timestamp);
2279 data->rpc_done = true;
2283 static void nfs4_open_confirm_release(void *calldata)
2285 struct nfs4_opendata *data = calldata;
2286 struct nfs4_state *state = NULL;
2288 /* If this request hasn't been cancelled, do nothing */
2289 if (!data->cancelled)
2291 /* In case of error, no cleanup! */
2292 if (!data->rpc_done)
2294 state = nfs4_opendata_to_nfs4_state(data);
2296 nfs4_close_state(state, data->o_arg.fmode);
2298 nfs4_opendata_put(data);
2301 static const struct rpc_call_ops nfs4_open_confirm_ops = {
2302 .rpc_call_prepare = nfs4_open_confirm_prepare,
2303 .rpc_call_done = nfs4_open_confirm_done,
2304 .rpc_release = nfs4_open_confirm_release,
2308 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2310 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2312 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
2313 struct rpc_task *task;
2314 struct rpc_message msg = {
2315 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2316 .rpc_argp = &data->c_arg,
2317 .rpc_resp = &data->c_res,
2318 .rpc_cred = data->owner->so_cred,
2320 struct rpc_task_setup task_setup_data = {
2321 .rpc_client = server->client,
2322 .rpc_message = &msg,
2323 .callback_ops = &nfs4_open_confirm_ops,
2324 .callback_data = data,
2325 .workqueue = nfsiod_workqueue,
2326 .flags = RPC_TASK_ASYNC,
2330 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2332 kref_get(&data->kref);
2333 data->rpc_done = false;
2334 data->rpc_status = 0;
2335 data->timestamp = jiffies;
2336 task = rpc_run_task(&task_setup_data);
2338 return PTR_ERR(task);
2339 status = rpc_wait_for_completion_task(task);
2341 data->cancelled = true;
2344 status = data->rpc_status;
2349 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
2351 struct nfs4_opendata *data = calldata;
2352 struct nfs4_state_owner *sp = data->owner;
2353 struct nfs_client *clp = sp->so_server->nfs_client;
2354 enum open_claim_type4 claim = data->o_arg.claim;
2356 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
2359 * Check if we still need to send an OPEN call, or if we can use
2360 * a delegation instead.
2362 if (data->state != NULL) {
2363 struct nfs_delegation *delegation;
2365 if (can_open_cached(data->state, data->o_arg.fmode,
2366 data->o_arg.open_flags, claim))
2369 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
2370 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
2371 goto unlock_no_action;
2374 /* Update client id. */
2375 data->o_arg.clientid = clp->cl_clientid;
2379 case NFS4_OPEN_CLAIM_PREVIOUS:
2380 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2381 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
2382 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
2384 case NFS4_OPEN_CLAIM_FH:
2385 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
2387 data->timestamp = jiffies;
2388 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
2389 &data->o_arg.seq_args,
2390 &data->o_res.seq_res,
2392 nfs_release_seqid(data->o_arg.seqid);
2394 /* Set the create mode (note dependency on the session type) */
2395 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2396 if (data->o_arg.open_flags & O_EXCL) {
2397 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2398 if (nfs4_has_persistent_session(clp))
2399 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2400 else if (clp->cl_mvops->minor_version > 0)
2401 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2405 trace_nfs4_cached_open(data->state);
2408 task->tk_action = NULL;
2410 nfs4_sequence_done(task, &data->o_res.seq_res);
2413 static void nfs4_open_done(struct rpc_task *task, void *calldata)
2415 struct nfs4_opendata *data = calldata;
2417 data->rpc_status = task->tk_status;
2419 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
2422 if (task->tk_status == 0) {
2423 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2424 switch (data->o_res.f_attr->mode & S_IFMT) {
2428 data->rpc_status = -ELOOP;
2431 data->rpc_status = -EISDIR;
2434 data->rpc_status = -ENOTDIR;
2437 renew_lease(data->o_res.server, data->timestamp);
2438 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2439 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2441 data->rpc_done = true;
2444 static void nfs4_open_release(void *calldata)
2446 struct nfs4_opendata *data = calldata;
2447 struct nfs4_state *state = NULL;
2449 /* If this request hasn't been cancelled, do nothing */
2450 if (!data->cancelled)
2452 /* In case of error, no cleanup! */
2453 if (data->rpc_status != 0 || !data->rpc_done)
2455 /* In case we need an open_confirm, no cleanup! */
2456 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2458 state = nfs4_opendata_to_nfs4_state(data);
2460 nfs4_close_state(state, data->o_arg.fmode);
2462 nfs4_opendata_put(data);
2465 static const struct rpc_call_ops nfs4_open_ops = {
2466 .rpc_call_prepare = nfs4_open_prepare,
2467 .rpc_call_done = nfs4_open_done,
2468 .rpc_release = nfs4_open_release,
2471 static int nfs4_run_open_task(struct nfs4_opendata *data,
2472 struct nfs_open_context *ctx)
2474 struct inode *dir = d_inode(data->dir);
2475 struct nfs_server *server = NFS_SERVER(dir);
2476 struct nfs_openargs *o_arg = &data->o_arg;
2477 struct nfs_openres *o_res = &data->o_res;
2478 struct rpc_task *task;
2479 struct rpc_message msg = {
2480 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2483 .rpc_cred = data->owner->so_cred,
2485 struct rpc_task_setup task_setup_data = {
2486 .rpc_client = server->client,
2487 .rpc_message = &msg,
2488 .callback_ops = &nfs4_open_ops,
2489 .callback_data = data,
2490 .workqueue = nfsiod_workqueue,
2491 .flags = RPC_TASK_ASYNC,
2495 kref_get(&data->kref);
2496 data->rpc_done = false;
2497 data->rpc_status = 0;
2498 data->cancelled = false;
2499 data->is_recover = false;
2501 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
2502 data->is_recover = true;
2503 task_setup_data.flags |= RPC_TASK_TIMEOUT;
2505 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
2506 pnfs_lgopen_prepare(data, ctx);
2508 task = rpc_run_task(&task_setup_data);
2510 return PTR_ERR(task);
2511 status = rpc_wait_for_completion_task(task);
2513 data->cancelled = true;
2516 status = data->rpc_status;
2522 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2524 struct inode *dir = d_inode(data->dir);
2525 struct nfs_openres *o_res = &data->o_res;
2528 status = nfs4_run_open_task(data, NULL);
2529 if (status != 0 || !data->rpc_done)
2532 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2534 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
2535 status = _nfs4_proc_open_confirm(data);
2541 * Additional permission checks in order to distinguish between an
2542 * open for read, and an open for execute. This works around the
2543 * fact that NFSv4 OPEN treats read and execute permissions as being
2545 * Note that in the non-execute case, we want to turn off permission
2546 * checking if we just created a new file (POSIX open() semantics).
2548 static int nfs4_opendata_access(const struct cred *cred,
2549 struct nfs4_opendata *opendata,
2550 struct nfs4_state *state, fmode_t fmode,
2553 struct nfs_access_entry cache;
2556 /* access call failed or for some reason the server doesn't
2557 * support any access modes -- defer access call until later */
2558 if (opendata->o_res.access_supported == 0)
2563 * Use openflags to check for exec, because fmode won't
2564 * always have FMODE_EXEC set when file open for exec.
2566 if (openflags & __FMODE_EXEC) {
2567 /* ONLY check for exec rights */
2568 if (S_ISDIR(state->inode->i_mode))
2569 mask = NFS4_ACCESS_LOOKUP;
2571 mask = NFS4_ACCESS_EXECUTE;
2572 } else if ((fmode & FMODE_READ) && !opendata->file_created)
2573 mask = NFS4_ACCESS_READ;
2576 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2577 nfs_access_add_cache(state->inode, &cache);
2579 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2580 if ((mask & ~cache.mask & flags) == 0)
2587 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2589 static int _nfs4_proc_open(struct nfs4_opendata *data,
2590 struct nfs_open_context *ctx)
2592 struct inode *dir = d_inode(data->dir);
2593 struct nfs_server *server = NFS_SERVER(dir);
2594 struct nfs_openargs *o_arg = &data->o_arg;
2595 struct nfs_openres *o_res = &data->o_res;
2598 status = nfs4_run_open_task(data, ctx);
2599 if (!data->rpc_done)
2602 if (status == -NFS4ERR_BADNAME &&
2603 !(o_arg->open_flags & O_CREAT))
2608 nfs_fattr_map_and_free_names(server, &data->f_attr);
2610 if (o_arg->open_flags & O_CREAT) {
2611 if (o_arg->open_flags & O_EXCL)
2612 data->file_created = true;
2613 else if (o_res->cinfo.before != o_res->cinfo.after)
2614 data->file_created = true;
2615 if (data->file_created ||
2616 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
2617 update_changeattr(dir, &o_res->cinfo,
2618 o_res->f_attr->time_start, 0);
2620 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2621 server->caps &= ~NFS_CAP_POSIX_LOCK;
2622 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2623 status = _nfs4_proc_open_confirm(data);
2627 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2628 nfs4_sequence_free_slot(&o_res->seq_res);
2629 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr,
2630 o_res->f_label, NULL);
2637 * reclaim state on the server after a network partition.
2638 * Assumes caller holds the appropriate lock
2640 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2642 struct nfs4_opendata *opendata;
2645 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2646 NFS4_OPEN_CLAIM_FH);
2647 if (IS_ERR(opendata))
2648 return PTR_ERR(opendata);
2649 ret = nfs4_open_recover(opendata, state);
2651 d_drop(ctx->dentry);
2652 nfs4_opendata_put(opendata);
2656 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2658 struct nfs_server *server = NFS_SERVER(state->inode);
2659 struct nfs4_exception exception = { };
2663 err = _nfs4_open_expired(ctx, state);
2664 trace_nfs4_open_expired(ctx, 0, err);
2665 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2670 case -NFS4ERR_GRACE:
2671 case -NFS4ERR_DELAY:
2672 nfs4_handle_exception(server, err, &exception);
2675 } while (exception.retry);
2680 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2682 struct nfs_open_context *ctx;
2685 ctx = nfs4_state_find_open_context(state);
2688 ret = nfs4_do_open_expired(ctx, state);
2689 put_nfs_open_context(ctx);
2693 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2694 const nfs4_stateid *stateid)
2696 nfs_remove_bad_delegation(state->inode, stateid);
2697 nfs_state_clear_delegation(state);
2700 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2702 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2703 nfs_finish_clear_delegation_stateid(state, NULL);
2706 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2708 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2709 nfs40_clear_delegation_stateid(state);
2710 nfs_state_clear_open_state_flags(state);
2711 return nfs4_open_expired(sp, state);
2714 static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2715 nfs4_stateid *stateid,
2716 const struct cred *cred)
2718 return -NFS4ERR_BAD_STATEID;
2721 #if defined(CONFIG_NFS_V4_1)
2722 static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2723 nfs4_stateid *stateid,
2724 const struct cred *cred)
2728 switch (stateid->type) {
2731 case NFS4_INVALID_STATEID_TYPE:
2732 case NFS4_SPECIAL_STATEID_TYPE:
2733 return -NFS4ERR_BAD_STATEID;
2734 case NFS4_REVOKED_STATEID_TYPE:
2738 status = nfs41_test_stateid(server, stateid, cred);
2740 case -NFS4ERR_EXPIRED:
2741 case -NFS4ERR_ADMIN_REVOKED:
2742 case -NFS4ERR_DELEG_REVOKED:
2748 /* Ack the revoked state to the server */
2749 nfs41_free_stateid(server, stateid, cred, true);
2750 return -NFS4ERR_EXPIRED;
2753 static int nfs41_check_delegation_stateid(struct nfs4_state *state)
2755 struct nfs_server *server = NFS_SERVER(state->inode);
2756 nfs4_stateid stateid;
2757 struct nfs_delegation *delegation;
2758 const struct cred *cred = NULL;
2759 int status, ret = NFS_OK;
2761 /* Get the delegation credential for use by test/free_stateid */
2763 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2764 if (delegation == NULL) {
2766 nfs_state_clear_delegation(state);
2770 nfs4_stateid_copy(&stateid, &delegation->stateid);
2772 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2773 &delegation->flags)) {
2778 if (delegation->cred)
2779 cred = get_cred(delegation->cred);
2781 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
2782 trace_nfs4_test_delegation_stateid(state, NULL, status);
2783 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
2784 nfs_finish_clear_delegation_stateid(state, &stateid);
2792 static void nfs41_delegation_recover_stateid(struct nfs4_state *state)
2796 if (test_bit(NFS_DELEGATED_STATE, &state->flags) &&
2797 nfs4_copy_delegation_stateid(state->inode, state->state,
2799 nfs4_stateid_match_other(&state->stateid, &tmp))
2800 nfs_state_set_delegation(state, &tmp, state->state);
2802 nfs_state_clear_delegation(state);
2806 * nfs41_check_expired_locks - possibly free a lock stateid
2808 * @state: NFSv4 state for an inode
2810 * Returns NFS_OK if recovery for this stateid is now finished.
2811 * Otherwise a negative NFS4ERR value is returned.
2813 static int nfs41_check_expired_locks(struct nfs4_state *state)
2815 int status, ret = NFS_OK;
2816 struct nfs4_lock_state *lsp, *prev = NULL;
2817 struct nfs_server *server = NFS_SERVER(state->inode);
2819 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2822 spin_lock(&state->state_lock);
2823 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2824 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2825 const struct cred *cred = lsp->ls_state->owner->so_cred;
2827 refcount_inc(&lsp->ls_count);
2828 spin_unlock(&state->state_lock);
2830 nfs4_put_lock_state(prev);
2833 status = nfs41_test_and_free_expired_stateid(server,
2836 trace_nfs4_test_lock_stateid(state, lsp, status);
2837 if (status == -NFS4ERR_EXPIRED ||
2838 status == -NFS4ERR_BAD_STATEID) {
2839 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
2840 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
2841 if (!recover_lost_locks)
2842 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2843 } else if (status != NFS_OK) {
2845 nfs4_put_lock_state(prev);
2848 spin_lock(&state->state_lock);
2851 spin_unlock(&state->state_lock);
2852 nfs4_put_lock_state(prev);
2858 * nfs41_check_open_stateid - possibly free an open stateid
2860 * @state: NFSv4 state for an inode
2862 * Returns NFS_OK if recovery for this stateid is now finished.
2863 * Otherwise a negative NFS4ERR value is returned.
2865 static int nfs41_check_open_stateid(struct nfs4_state *state)
2867 struct nfs_server *server = NFS_SERVER(state->inode);
2868 nfs4_stateid *stateid = &state->open_stateid;
2869 const struct cred *cred = state->owner->so_cred;
2872 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0)
2873 return -NFS4ERR_BAD_STATEID;
2874 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
2875 trace_nfs4_test_open_stateid(state, NULL, status);
2876 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
2877 nfs_state_clear_open_state_flags(state);
2878 stateid->type = NFS4_INVALID_STATEID_TYPE;
2881 if (nfs_open_stateid_recover_openmode(state))
2882 return -NFS4ERR_OPENMODE;
2886 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2890 status = nfs41_check_delegation_stateid(state);
2891 if (status != NFS_OK)
2893 nfs41_delegation_recover_stateid(state);
2895 status = nfs41_check_expired_locks(state);
2896 if (status != NFS_OK)
2898 status = nfs41_check_open_stateid(state);
2899 if (status != NFS_OK)
2900 status = nfs4_open_expired(sp, state);
2906 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2907 * fields corresponding to attributes that were used to store the verifier.
2908 * Make sure we clobber those fields in the later setattr call
2910 static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2911 struct iattr *sattr, struct nfs4_label **label)
2913 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2918 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
2919 attrset[i] = opendata->o_res.attrset[i];
2920 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
2921 attrset[i] &= ~bitmask[i];
2924 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
2925 sattr->ia_valid : 0;
2927 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
2928 if (sattr->ia_valid & ATTR_ATIME_SET)
2929 ret |= ATTR_ATIME_SET;
2934 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
2935 if (sattr->ia_valid & ATTR_MTIME_SET)
2936 ret |= ATTR_MTIME_SET;
2941 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
2946 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2947 int flags, struct nfs_open_context *ctx)
2949 struct nfs4_state_owner *sp = opendata->owner;
2950 struct nfs_server *server = sp->so_server;
2951 struct dentry *dentry;
2952 struct nfs4_state *state;
2953 fmode_t acc_mode = _nfs4_ctx_to_accessmode(ctx);
2957 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2959 ret = _nfs4_proc_open(opendata, ctx);
2963 state = _nfs4_opendata_to_nfs4_state(opendata);
2964 ret = PTR_ERR(state);
2968 if (server->caps & NFS_CAP_POSIX_LOCK)
2969 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2970 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2971 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
2973 dentry = opendata->dentry;
2974 if (d_really_is_negative(dentry)) {
2975 struct dentry *alias;
2977 alias = d_exact_alias(dentry, state->inode);
2979 alias = d_splice_alias(igrab(state->inode), dentry);
2980 /* d_splice_alias() can't fail here - it's a non-directory */
2983 ctx->dentry = dentry = alias;
2985 nfs_set_verifier(dentry,
2986 nfs_save_change_attribute(d_inode(opendata->dir)));
2989 /* Parse layoutget results before we check for access */
2990 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
2992 ret = nfs4_opendata_access(sp->so_cred, opendata, state,
2997 if (d_inode(dentry) == state->inode) {
2998 nfs_inode_attach_open_context(ctx);
2999 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
3000 nfs4_schedule_stateid_recovery(server, state);
3004 if (!opendata->cancelled)
3005 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
3010 * Returns a referenced nfs4_state
3012 static int _nfs4_do_open(struct inode *dir,
3013 struct nfs_open_context *ctx,
3015 const struct nfs4_open_createattrs *c,
3018 struct nfs4_state_owner *sp;
3019 struct nfs4_state *state = NULL;
3020 struct nfs_server *server = NFS_SERVER(dir);
3021 struct nfs4_opendata *opendata;
3022 struct dentry *dentry = ctx->dentry;
3023 const struct cred *cred = ctx->cred;
3024 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
3025 fmode_t fmode = _nfs4_ctx_to_openmode(ctx);
3026 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
3027 struct iattr *sattr = c->sattr;
3028 struct nfs4_label *label = c->label;
3029 struct nfs4_label *olabel = NULL;
3032 /* Protect against reboot recovery conflicts */
3034 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
3036 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
3039 status = nfs4_client_recover_expired_lease(server->nfs_client);
3041 goto err_put_state_owner;
3042 if (d_really_is_positive(dentry))
3043 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
3045 if (d_really_is_positive(dentry))
3046 claim = NFS4_OPEN_CLAIM_FH;
3047 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
3048 c, claim, GFP_KERNEL);
3049 if (opendata == NULL)
3050 goto err_put_state_owner;
3053 olabel = nfs4_label_alloc(server, GFP_KERNEL);
3054 if (IS_ERR(olabel)) {
3055 status = PTR_ERR(olabel);
3056 goto err_opendata_put;
3060 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
3061 if (!opendata->f_attr.mdsthreshold) {
3062 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
3063 if (!opendata->f_attr.mdsthreshold)
3064 goto err_free_label;
3066 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
3068 if (d_really_is_positive(dentry))
3069 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
3071 status = _nfs4_open_and_get_state(opendata, flags, ctx);
3073 goto err_free_label;
3076 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
3077 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
3078 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
3080 * send create attributes which was not set by open
3081 * with an extra setattr.
3083 if (attrs || label) {
3084 unsigned ia_old = sattr->ia_valid;
3086 sattr->ia_valid = attrs;
3087 nfs_fattr_init(opendata->o_res.f_attr);
3088 status = nfs4_do_setattr(state->inode, cred,
3089 opendata->o_res.f_attr, sattr,
3090 ctx, label, olabel);
3092 nfs_setattr_update_inode(state->inode, sattr,
3093 opendata->o_res.f_attr);
3094 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
3096 sattr->ia_valid = ia_old;
3099 if (opened && opendata->file_created)
3102 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
3103 *ctx_th = opendata->f_attr.mdsthreshold;
3104 opendata->f_attr.mdsthreshold = NULL;
3107 nfs4_label_free(olabel);
3109 nfs4_opendata_put(opendata);
3110 nfs4_put_state_owner(sp);
3113 nfs4_label_free(olabel);
3115 nfs4_opendata_put(opendata);
3116 err_put_state_owner:
3117 nfs4_put_state_owner(sp);
3123 static struct nfs4_state *nfs4_do_open(struct inode *dir,
3124 struct nfs_open_context *ctx,
3126 struct iattr *sattr,
3127 struct nfs4_label *label,
3130 struct nfs_server *server = NFS_SERVER(dir);
3131 struct nfs4_exception exception = {
3132 .interruptible = true,
3134 struct nfs4_state *res;
3135 struct nfs4_open_createattrs c = {
3139 [0] = (__u32)jiffies,
3140 [1] = (__u32)current->pid,
3146 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
3148 trace_nfs4_open_file(ctx, flags, status);
3151 /* NOTE: BAD_SEQID means the server and client disagree about the
3152 * book-keeping w.r.t. state-changing operations
3153 * (OPEN/CLOSE/LOCK/LOCKU...)
3154 * It is actually a sign of a bug on the client or on the server.
3156 * If we receive a BAD_SEQID error in the particular case of
3157 * doing an OPEN, we assume that nfs_increment_open_seqid() will
3158 * have unhashed the old state_owner for us, and that we can
3159 * therefore safely retry using a new one. We should still warn
3160 * the user though...
3162 if (status == -NFS4ERR_BAD_SEQID) {
3163 pr_warn_ratelimited("NFS: v4 server %s "
3164 " returned a bad sequence-id error!\n",
3165 NFS_SERVER(dir)->nfs_client->cl_hostname);
3166 exception.retry = 1;
3170 * BAD_STATEID on OPEN means that the server cancelled our
3171 * state before it received the OPEN_CONFIRM.
3172 * Recover by retrying the request as per the discussion
3173 * on Page 181 of RFC3530.
3175 if (status == -NFS4ERR_BAD_STATEID) {
3176 exception.retry = 1;
3179 if (status == -EAGAIN) {
3180 /* We must have found a delegation */
3181 exception.retry = 1;
3184 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3186 res = ERR_PTR(nfs4_handle_exception(server,
3187 status, &exception));
3188 } while (exception.retry);
3192 static int _nfs4_do_setattr(struct inode *inode,
3193 struct nfs_setattrargs *arg,
3194 struct nfs_setattrres *res,
3195 const struct cred *cred,
3196 struct nfs_open_context *ctx)
3198 struct nfs_server *server = NFS_SERVER(inode);
3199 struct rpc_message msg = {
3200 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3205 const struct cred *delegation_cred = NULL;
3206 unsigned long timestamp = jiffies;
3210 nfs_fattr_init(res->fattr);
3212 /* Servers should only apply open mode checks for file size changes */
3213 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
3217 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
3218 /* Use that stateid */
3219 } else if (ctx != NULL && ctx->state) {
3220 struct nfs_lock_context *l_ctx;
3221 if (!nfs4_valid_open_stateid(ctx->state))
3223 l_ctx = nfs_get_lock_context(ctx);
3225 return PTR_ERR(l_ctx);
3226 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3227 &arg->stateid, &delegation_cred);
3228 nfs_put_lock_context(l_ctx);
3233 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
3235 if (delegation_cred)
3236 msg.rpc_cred = delegation_cred;
3238 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3240 put_cred(delegation_cred);
3241 if (status == 0 && ctx != NULL)
3242 renew_lease(server, timestamp);
3243 trace_nfs4_setattr(inode, &arg->stateid, status);
3247 static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
3248 struct nfs_fattr *fattr, struct iattr *sattr,
3249 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
3250 struct nfs4_label *olabel)
3252 struct nfs_server *server = NFS_SERVER(inode);
3253 __u32 bitmask[NFS4_BITMASK_SZ];
3254 struct nfs4_state *state = ctx ? ctx->state : NULL;
3255 struct nfs_setattrargs arg = {
3256 .fh = NFS_FH(inode),
3262 struct nfs_setattrres res = {
3267 struct nfs4_exception exception = {
3270 .stateid = &arg.stateid,
3275 nfs4_bitmap_copy_adjust_setattr(bitmask,
3276 nfs4_bitmask(server, olabel),
3279 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
3281 case -NFS4ERR_OPENMODE:
3282 if (!(sattr->ia_valid & ATTR_SIZE)) {
3283 pr_warn_once("NFSv4: server %s is incorrectly "
3284 "applying open mode checks to "
3285 "a SETATTR that is not "
3286 "changing file size.\n",
3287 server->nfs_client->cl_hostname);
3289 if (state && !(state->state & FMODE_WRITE)) {
3291 if (sattr->ia_valid & ATTR_OPEN)
3296 err = nfs4_handle_exception(server, err, &exception);
3297 } while (exception.retry);
3303 nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3305 if (inode == NULL || !nfs_have_layout(inode))
3308 return pnfs_wait_on_layoutreturn(inode, task);
3311 struct nfs4_closedata {
3312 struct inode *inode;
3313 struct nfs4_state *state;
3314 struct nfs_closeargs arg;
3315 struct nfs_closeres res;
3317 struct nfs4_layoutreturn_args arg;
3318 struct nfs4_layoutreturn_res res;
3319 struct nfs4_xdr_opaque_data ld_private;
3323 struct nfs_fattr fattr;
3324 unsigned long timestamp;
3327 static void nfs4_free_closedata(void *data)
3329 struct nfs4_closedata *calldata = data;
3330 struct nfs4_state_owner *sp = calldata->state->owner;
3331 struct super_block *sb = calldata->state->inode->i_sb;
3333 if (calldata->lr.roc)
3334 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3335 calldata->res.lr_ret);
3336 nfs4_put_open_state(calldata->state);
3337 nfs_free_seqid(calldata->arg.seqid);
3338 nfs4_put_state_owner(sp);
3339 nfs_sb_deactive(sb);
3343 static void nfs4_close_done(struct rpc_task *task, void *data)
3345 struct nfs4_closedata *calldata = data;
3346 struct nfs4_state *state = calldata->state;
3347 struct nfs_server *server = NFS_SERVER(calldata->inode);
3348 nfs4_stateid *res_stateid = NULL;
3349 struct nfs4_exception exception = {
3351 .inode = calldata->inode,
3352 .stateid = &calldata->arg.stateid,
3355 dprintk("%s: begin!\n", __func__);
3356 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3358 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
3360 /* Handle Layoutreturn errors */
3361 if (calldata->arg.lr_args && task->tk_status != 0) {
3362 switch (calldata->res.lr_ret) {
3364 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3367 calldata->arg.lr_args = NULL;
3368 calldata->res.lr_res = NULL;
3370 case -NFS4ERR_OLD_STATEID:
3371 if (nfs4_layoutreturn_refresh_stateid(&calldata->arg.lr_args->stateid,
3372 &calldata->arg.lr_args->range,
3376 case -NFS4ERR_ADMIN_REVOKED:
3377 case -NFS4ERR_DELEG_REVOKED:
3378 case -NFS4ERR_EXPIRED:
3379 case -NFS4ERR_BAD_STATEID:
3380 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3381 case -NFS4ERR_WRONG_CRED:
3382 calldata->arg.lr_args = NULL;
3383 calldata->res.lr_res = NULL;
3388 /* hmm. we are done with the inode, and in the process of freeing
3389 * the state_owner. we keep this around to process errors
3391 switch (task->tk_status) {
3393 res_stateid = &calldata->res.stateid;
3394 renew_lease(server, calldata->timestamp);
3396 case -NFS4ERR_ACCESS:
3397 if (calldata->arg.bitmask != NULL) {
3398 calldata->arg.bitmask = NULL;
3399 calldata->res.fattr = NULL;
3404 case -NFS4ERR_OLD_STATEID:
3405 /* Did we race with OPEN? */
3406 if (nfs4_refresh_open_stateid(&calldata->arg.stateid,
3410 case -NFS4ERR_ADMIN_REVOKED:
3411 case -NFS4ERR_STALE_STATEID:
3412 case -NFS4ERR_EXPIRED:
3413 nfs4_free_revoked_stateid(server,
3414 &calldata->arg.stateid,
3415 task->tk_msg.rpc_cred);
3417 case -NFS4ERR_BAD_STATEID:
3420 task->tk_status = nfs4_async_handle_exception(task,
3421 server, task->tk_status, &exception);
3422 if (exception.retry)
3425 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3426 res_stateid, calldata->arg.fmode);
3428 task->tk_status = 0;
3429 nfs_release_seqid(calldata->arg.seqid);
3430 nfs_refresh_inode(calldata->inode, &calldata->fattr);
3431 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
3434 calldata->res.lr_ret = 0;
3436 task->tk_status = 0;
3437 rpc_restart_call_prepare(task);
3441 static void nfs4_close_prepare(struct rpc_task *task, void *data)
3443 struct nfs4_closedata *calldata = data;
3444 struct nfs4_state *state = calldata->state;
3445 struct inode *inode = calldata->inode;
3446 struct pnfs_layout_hdr *lo;
3447 bool is_rdonly, is_wronly, is_rdwr;
3450 dprintk("%s: begin!\n", __func__);
3451 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3454 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
3455 spin_lock(&state->owner->so_lock);
3456 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3457 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3458 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
3459 /* Calculate the change in open mode */
3460 calldata->arg.fmode = 0;
3461 if (state->n_rdwr == 0) {
3462 if (state->n_rdonly == 0)
3463 call_close |= is_rdonly;
3465 calldata->arg.fmode |= FMODE_READ;
3466 if (state->n_wronly == 0)
3467 call_close |= is_wronly;
3469 calldata->arg.fmode |= FMODE_WRITE;
3470 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3471 call_close |= is_rdwr;
3473 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3475 if (!nfs4_valid_open_stateid(state) ||
3476 !nfs4_refresh_open_stateid(&calldata->arg.stateid, state))
3478 spin_unlock(&state->owner->so_lock);
3481 /* Note: exit _without_ calling nfs4_close_done */
3485 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
3486 nfs_release_seqid(calldata->arg.seqid);
3490 lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3491 if (lo && !pnfs_layout_is_valid(lo)) {
3492 calldata->arg.lr_args = NULL;
3493 calldata->res.lr_res = NULL;
3496 if (calldata->arg.fmode == 0)
3497 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
3499 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
3500 /* Close-to-open cache consistency revalidation */
3501 if (!nfs4_have_delegation(inode, FMODE_READ))
3502 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3504 calldata->arg.bitmask = NULL;
3507 calldata->arg.share_access =
3508 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3509 calldata->arg.fmode, 0);
3511 if (calldata->res.fattr == NULL)
3512 calldata->arg.bitmask = NULL;
3513 else if (calldata->arg.bitmask == NULL)
3514 calldata->res.fattr = NULL;
3515 calldata->timestamp = jiffies;
3516 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
3517 &calldata->arg.seq_args,
3518 &calldata->res.seq_res,
3520 nfs_release_seqid(calldata->arg.seqid);
3521 dprintk("%s: done!\n", __func__);
3524 task->tk_action = NULL;
3526 nfs4_sequence_done(task, &calldata->res.seq_res);
3529 static const struct rpc_call_ops nfs4_close_ops = {
3530 .rpc_call_prepare = nfs4_close_prepare,
3531 .rpc_call_done = nfs4_close_done,
3532 .rpc_release = nfs4_free_closedata,
3536 * It is possible for data to be read/written from a mem-mapped file
3537 * after the sys_close call (which hits the vfs layer as a flush).
3538 * This means that we can't safely call nfsv4 close on a file until
3539 * the inode is cleared. This in turn means that we are not good
3540 * NFSv4 citizens - we do not indicate to the server to update the file's
3541 * share state even when we are done with one of the three share
3542 * stateid's in the inode.
3544 * NOTE: Caller must be holding the sp->so_owner semaphore!
3546 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
3548 struct nfs_server *server = NFS_SERVER(state->inode);
3549 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
3550 struct nfs4_closedata *calldata;
3551 struct nfs4_state_owner *sp = state->owner;
3552 struct rpc_task *task;
3553 struct rpc_message msg = {
3554 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3555 .rpc_cred = state->owner->so_cred,
3557 struct rpc_task_setup task_setup_data = {
3558 .rpc_client = server->client,
3559 .rpc_message = &msg,
3560 .callback_ops = &nfs4_close_ops,
3561 .workqueue = nfsiod_workqueue,
3562 .flags = RPC_TASK_ASYNC,
3564 int status = -ENOMEM;
3566 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3567 &task_setup_data.rpc_client, &msg);
3569 calldata = kzalloc(sizeof(*calldata), gfp_mask);
3570 if (calldata == NULL)
3572 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
3573 calldata->inode = state->inode;
3574 calldata->state = state;
3575 calldata->arg.fh = NFS_FH(state->inode);
3576 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3577 goto out_free_calldata;
3578 /* Serialization for the sequence id */
3579 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3580 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
3581 if (IS_ERR(calldata->arg.seqid))
3582 goto out_free_calldata;
3583 nfs_fattr_init(&calldata->fattr);
3584 calldata->arg.fmode = 0;
3585 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
3586 calldata->res.fattr = &calldata->fattr;
3587 calldata->res.seqid = calldata->arg.seqid;
3588 calldata->res.server = server;
3589 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3590 calldata->lr.roc = pnfs_roc(state->inode,
3591 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3592 if (calldata->lr.roc) {
3593 calldata->arg.lr_args = &calldata->lr.arg;
3594 calldata->res.lr_res = &calldata->lr.res;
3596 nfs_sb_active(calldata->inode->i_sb);
3598 msg.rpc_argp = &calldata->arg;
3599 msg.rpc_resp = &calldata->res;
3600 task_setup_data.callback_data = calldata;
3601 task = rpc_run_task(&task_setup_data);
3603 return PTR_ERR(task);
3606 status = rpc_wait_for_completion_task(task);
3612 nfs4_put_open_state(state);
3613 nfs4_put_state_owner(sp);
3617 static struct inode *
3618 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3619 int open_flags, struct iattr *attr, int *opened)
3621 struct nfs4_state *state;
3622 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3624 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
3626 /* Protect against concurrent sillydeletes */
3627 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
3629 nfs4_label_release_security(label);
3632 return ERR_CAST(state);
3633 return state->inode;
3636 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
3638 if (ctx->state == NULL)
3641 nfs4_close_sync(ctx->state, _nfs4_ctx_to_openmode(ctx));
3643 nfs4_close_state(ctx->state, _nfs4_ctx_to_openmode(ctx));
3646 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3647 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3648 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
3650 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3652 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
3653 struct nfs4_server_caps_arg args = {
3657 struct nfs4_server_caps_res res = {};
3658 struct rpc_message msg = {
3659 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
3666 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3667 FATTR4_WORD0_FH_EXPIRE_TYPE |
3668 FATTR4_WORD0_LINK_SUPPORT |
3669 FATTR4_WORD0_SYMLINK_SUPPORT |
3670 FATTR4_WORD0_ACLSUPPORT;
3672 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3674 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3676 /* Sanity check the server answers */
3677 switch (minorversion) {
3679 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3680 res.attr_bitmask[2] = 0;
3683 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3686 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3688 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
3689 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3690 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3691 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3692 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
3693 NFS_CAP_CTIME|NFS_CAP_MTIME|
3694 NFS_CAP_SECURITY_LABEL);
3695 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3696 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3697 server->caps |= NFS_CAP_ACLS;
3698 if (res.has_links != 0)
3699 server->caps |= NFS_CAP_HARDLINKS;
3700 if (res.has_symlinks != 0)
3701 server->caps |= NFS_CAP_SYMLINKS;
3702 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3703 server->caps |= NFS_CAP_FILEID;
3704 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3705 server->caps |= NFS_CAP_MODE;
3706 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3707 server->caps |= NFS_CAP_NLINK;
3708 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3709 server->caps |= NFS_CAP_OWNER;
3710 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3711 server->caps |= NFS_CAP_OWNER_GROUP;
3712 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3713 server->caps |= NFS_CAP_ATIME;
3714 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3715 server->caps |= NFS_CAP_CTIME;
3716 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3717 server->caps |= NFS_CAP_MTIME;
3718 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3719 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3720 server->caps |= NFS_CAP_SECURITY_LABEL;
3722 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3723 sizeof(server->attr_bitmask));
3724 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3726 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3727 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3728 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
3729 server->cache_consistency_bitmask[2] = 0;
3731 /* Avoid a regression due to buggy server */
3732 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3733 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
3734 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3735 sizeof(server->exclcreat_bitmask));
3737 server->acl_bitmask = res.acl_bitmask;
3738 server->fh_expire_type = res.fh_expire_type;
3744 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3746 struct nfs4_exception exception = {
3747 .interruptible = true,
3751 err = nfs4_handle_exception(server,
3752 _nfs4_server_capabilities(server, fhandle),
3754 } while (exception.retry);
3758 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3759 struct nfs_fsinfo *info)
3762 struct nfs4_lookup_root_arg args = {
3765 struct nfs4_lookup_res res = {
3767 .fattr = info->fattr,
3770 struct rpc_message msg = {
3771 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3776 bitmask[0] = nfs4_fattr_bitmap[0];
3777 bitmask[1] = nfs4_fattr_bitmap[1];
3779 * Process the label in the upcoming getfattr
3781 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3783 nfs_fattr_init(info->fattr);
3784 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3787 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3788 struct nfs_fsinfo *info)
3790 struct nfs4_exception exception = {
3791 .interruptible = true,
3795 err = _nfs4_lookup_root(server, fhandle, info);
3796 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
3799 case -NFS4ERR_WRONGSEC:
3802 err = nfs4_handle_exception(server, err, &exception);
3804 } while (exception.retry);
3809 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3810 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3812 struct rpc_auth_create_args auth_args = {
3813 .pseudoflavor = flavor,
3815 struct rpc_auth *auth;
3817 auth = rpcauth_create(&auth_args, server->client);
3820 return nfs4_lookup_root(server, fhandle, info);
3824 * Retry pseudoroot lookup with various security flavors. We do this when:
3826 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3827 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3829 * Returns zero on success, or a negative NFS4ERR value, or a
3830 * negative errno value.
3832 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3833 struct nfs_fsinfo *info)
3835 /* Per 3530bis 15.33.5 */
3836 static const rpc_authflavor_t flav_array[] = {
3840 RPC_AUTH_UNIX, /* courtesy */
3843 int status = -EPERM;
3846 if (server->auth_info.flavor_len > 0) {
3847 /* try each flavor specified by user */
3848 for (i = 0; i < server->auth_info.flavor_len; i++) {
3849 status = nfs4_lookup_root_sec(server, fhandle, info,
3850 server->auth_info.flavors[i]);
3851 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3856 /* no flavors specified by user, try default list */
3857 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3858 status = nfs4_lookup_root_sec(server, fhandle, info,
3860 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3867 * -EACCES could mean that the user doesn't have correct permissions
3868 * to access the mount. It could also mean that we tried to mount
3869 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3870 * existing mount programs don't handle -EACCES very well so it should
3871 * be mapped to -EPERM instead.
3873 if (status == -EACCES)
3879 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3880 * @server: initialized nfs_server handle
3881 * @fhandle: we fill in the pseudo-fs root file handle
3882 * @info: we fill in an FSINFO struct
3883 * @auth_probe: probe the auth flavours
3885 * Returns zero on success, or a negative errno.
3887 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
3888 struct nfs_fsinfo *info,
3894 status = nfs4_lookup_root(server, fhandle, info);
3896 if (auth_probe || status == NFS4ERR_WRONGSEC)
3897 status = server->nfs_client->cl_mvops->find_root_sec(server,
3901 status = nfs4_server_capabilities(server, fhandle);
3903 status = nfs4_do_fsinfo(server, fhandle, info);
3905 return nfs4_map_errors(status);
3908 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3909 struct nfs_fsinfo *info)
3912 struct nfs_fattr *fattr = info->fattr;
3913 struct nfs4_label *label = NULL;
3915 error = nfs4_server_capabilities(server, mntfh);
3917 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3921 label = nfs4_label_alloc(server, GFP_KERNEL);
3923 return PTR_ERR(label);
3925 error = nfs4_proc_getattr(server, mntfh, fattr, label, NULL);
3927 dprintk("nfs4_get_root: getattr error = %d\n", -error);
3928 goto err_free_label;
3931 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3932 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3933 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3936 nfs4_label_free(label);
3942 * Get locations and (maybe) other attributes of a referral.
3943 * Note that we'll actually follow the referral later when
3944 * we detect fsid mismatch in inode revalidation
3946 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3947 const struct qstr *name, struct nfs_fattr *fattr,
3948 struct nfs_fh *fhandle)
3950 int status = -ENOMEM;
3951 struct page *page = NULL;
3952 struct nfs4_fs_locations *locations = NULL;
3954 page = alloc_page(GFP_KERNEL);
3957 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3958 if (locations == NULL)
3961 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
3966 * If the fsid didn't change, this is a migration event, not a
3967 * referral. Cause us to drop into the exception handler, which
3968 * will kick off migration recovery.
3970 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
3971 dprintk("%s: server did not return a different fsid for"
3972 " a referral at %s\n", __func__, name->name);
3973 status = -NFS4ERR_MOVED;
3976 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3977 nfs_fixup_referral_attributes(&locations->fattr);
3979 /* replace the lookup nfs_fattr with the locations nfs_fattr */
3980 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
3981 memset(fhandle, 0, sizeof(struct nfs_fh));
3989 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3990 struct nfs_fattr *fattr, struct nfs4_label *label,
3991 struct inode *inode)
3993 __u32 bitmask[NFS4_BITMASK_SZ];
3994 struct nfs4_getattr_arg args = {
3998 struct nfs4_getattr_res res = {
4003 struct rpc_message msg = {
4004 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4009 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode);
4011 nfs_fattr_init(fattr);
4012 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4015 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4016 struct nfs_fattr *fattr, struct nfs4_label *label,
4017 struct inode *inode)
4019 struct nfs4_exception exception = {
4020 .interruptible = true,
4024 err = _nfs4_proc_getattr(server, fhandle, fattr, label, inode);
4025 trace_nfs4_getattr(server, fhandle, fattr, err);
4026 err = nfs4_handle_exception(server, err,
4028 } while (exception.retry);
4033 * The file is not closed if it is opened due to the a request to change
4034 * the size of the file. The open call will not be needed once the
4035 * VFS layer lookup-intents are implemented.
4037 * Close is called when the inode is destroyed.
4038 * If we haven't opened the file for O_WRONLY, we
4039 * need to in the size_change case to obtain a stateid.
4042 * Because OPEN is always done by name in nfsv4, it is
4043 * possible that we opened a different file by the same
4044 * name. We can recognize this race condition, but we
4045 * can't do anything about it besides returning an error.
4047 * This will be fixed with VFS changes (lookup-intent).
4050 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
4051 struct iattr *sattr)
4053 struct inode *inode = d_inode(dentry);
4054 const struct cred *cred = NULL;
4055 struct nfs_open_context *ctx = NULL;
4056 struct nfs4_label *label = NULL;
4059 if (pnfs_ld_layoutret_on_setattr(inode) &&
4060 sattr->ia_valid & ATTR_SIZE &&
4061 sattr->ia_size < i_size_read(inode))
4062 pnfs_commit_and_return_layout(inode);
4064 nfs_fattr_init(fattr);
4066 /* Deal with open(O_TRUNC) */
4067 if (sattr->ia_valid & ATTR_OPEN)
4068 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
4070 /* Optimization: if the end result is no change, don't RPC */
4071 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
4074 /* Search for an existing open(O_WRITE) file */
4075 if (sattr->ia_valid & ATTR_FILE) {
4077 ctx = nfs_file_open_context(sattr->ia_file);
4082 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4084 return PTR_ERR(label);
4086 /* Return any delegations if we're going to change ACLs */
4087 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
4088 nfs4_inode_make_writeable(inode);
4090 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
4092 nfs_setattr_update_inode(inode, sattr, fattr);
4093 nfs_setsecurity(inode, fattr, label);
4095 nfs4_label_free(label);
4099 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
4100 const struct qstr *name, struct nfs_fh *fhandle,
4101 struct nfs_fattr *fattr, struct nfs4_label *label)
4103 struct nfs_server *server = NFS_SERVER(dir);
4105 struct nfs4_lookup_arg args = {
4106 .bitmask = server->attr_bitmask,
4107 .dir_fh = NFS_FH(dir),
4110 struct nfs4_lookup_res res = {
4116 struct rpc_message msg = {
4117 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4122 args.bitmask = nfs4_bitmask(server, label);
4124 nfs_fattr_init(fattr);
4126 dprintk("NFS call lookup %s\n", name->name);
4127 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
4128 dprintk("NFS reply lookup: %d\n", status);
4132 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
4134 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4135 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
4136 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4140 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
4141 const struct qstr *name, struct nfs_fh *fhandle,
4142 struct nfs_fattr *fattr, struct nfs4_label *label)
4144 struct nfs4_exception exception = {
4145 .interruptible = true,
4147 struct rpc_clnt *client = *clnt;
4150 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
4151 trace_nfs4_lookup(dir, name, err);
4153 case -NFS4ERR_BADNAME:
4156 case -NFS4ERR_MOVED:
4157 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
4158 if (err == -NFS4ERR_MOVED)
4159 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4161 case -NFS4ERR_WRONGSEC:
4163 if (client != *clnt)
4165 client = nfs4_negotiate_security(client, dir, name);
4167 return PTR_ERR(client);
4169 exception.retry = 1;
4172 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4174 } while (exception.retry);
4179 else if (client != *clnt)
4180 rpc_shutdown_client(client);
4185 static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
4186 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4187 struct nfs4_label *label)
4190 struct rpc_clnt *client = NFS_CLIENT(dir);
4192 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
4193 if (client != NFS_CLIENT(dir)) {
4194 rpc_shutdown_client(client);
4195 nfs_fixup_secinfo_attributes(fattr);
4201 nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
4202 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4204 struct rpc_clnt *client = NFS_CLIENT(dir);
4207 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
4209 return ERR_PTR(status);
4210 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
4213 static int _nfs4_proc_lookupp(struct inode *inode,
4214 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4215 struct nfs4_label *label)
4217 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4218 struct nfs_server *server = NFS_SERVER(inode);
4220 struct nfs4_lookupp_arg args = {
4221 .bitmask = server->attr_bitmask,
4222 .fh = NFS_FH(inode),
4224 struct nfs4_lookupp_res res = {
4230 struct rpc_message msg = {
4231 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4236 args.bitmask = nfs4_bitmask(server, label);
4238 nfs_fattr_init(fattr);
4240 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4241 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4243 dprintk("NFS reply lookupp: %d\n", status);
4247 static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4248 struct nfs_fattr *fattr, struct nfs4_label *label)
4250 struct nfs4_exception exception = {
4251 .interruptible = true,
4255 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4256 trace_nfs4_lookupp(inode, err);
4257 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4259 } while (exception.retry);
4263 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4265 struct nfs_server *server = NFS_SERVER(inode);
4266 struct nfs4_accessargs args = {
4267 .fh = NFS_FH(inode),
4268 .access = entry->mask,
4270 struct nfs4_accessres res = {
4273 struct rpc_message msg = {
4274 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4277 .rpc_cred = entry->cred,
4281 if (!nfs4_have_delegation(inode, FMODE_READ)) {
4282 res.fattr = nfs_alloc_fattr();
4283 if (res.fattr == NULL)
4285 args.bitmask = server->cache_consistency_bitmask;
4287 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4289 nfs_access_set_mask(entry, res.access);
4291 nfs_refresh_inode(inode, res.fattr);
4293 nfs_free_fattr(res.fattr);
4297 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4299 struct nfs4_exception exception = {
4300 .interruptible = true,
4304 err = _nfs4_proc_access(inode, entry);
4305 trace_nfs4_access(inode, err);
4306 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4308 } while (exception.retry);
4313 * TODO: For the time being, we don't try to get any attributes
4314 * along with any of the zero-copy operations READ, READDIR,
4317 * In the case of the first three, we want to put the GETATTR
4318 * after the read-type operation -- this is because it is hard
4319 * to predict the length of a GETATTR response in v4, and thus
4320 * align the READ data correctly. This means that the GETATTR
4321 * may end up partially falling into the page cache, and we should
4322 * shift it into the 'tail' of the xdr_buf before processing.
4323 * To do this efficiently, we need to know the total length
4324 * of data received, which doesn't seem to be available outside
4327 * In the case of WRITE, we also want to put the GETATTR after
4328 * the operation -- in this case because we want to make sure
4329 * we get the post-operation mtime and size.
4331 * Both of these changes to the XDR layer would in fact be quite
4332 * minor, but I decided to leave them for a subsequent patch.
4334 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4335 unsigned int pgbase, unsigned int pglen)
4337 struct nfs4_readlink args = {
4338 .fh = NFS_FH(inode),
4343 struct nfs4_readlink_res res;
4344 struct rpc_message msg = {
4345 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4350 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
4353 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4354 unsigned int pgbase, unsigned int pglen)
4356 struct nfs4_exception exception = {
4357 .interruptible = true,
4361 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4362 trace_nfs4_readlink(inode, err);
4363 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4365 } while (exception.retry);
4370 * This is just for mknod. open(O_CREAT) will always do ->open_context().
4373 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
4376 struct nfs_server *server = NFS_SERVER(dir);
4377 struct nfs4_label l, *ilabel = NULL;
4378 struct nfs_open_context *ctx;
4379 struct nfs4_state *state;
4382 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
4384 return PTR_ERR(ctx);
4386 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4388 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4389 sattr->ia_mode &= ~current_umask();
4390 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
4391 if (IS_ERR(state)) {
4392 status = PTR_ERR(state);
4396 nfs4_label_release_security(ilabel);
4397 put_nfs_open_context(ctx);
4402 _nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
4404 struct nfs_server *server = NFS_SERVER(dir);
4405 struct nfs_removeargs args = {
4409 struct nfs_removeres res = {
4412 struct rpc_message msg = {
4413 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4417 unsigned long timestamp = jiffies;
4420 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
4422 spin_lock(&dir->i_lock);
4423 update_changeattr_locked(dir, &res.cinfo, timestamp, 0);
4424 /* Removing a directory decrements nlink in the parent */
4425 if (ftype == NF4DIR && dir->i_nlink > 2)
4426 nfs4_dec_nlink_locked(dir);
4427 spin_unlock(&dir->i_lock);
4432 static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4434 struct nfs4_exception exception = {
4435 .interruptible = true,
4437 struct inode *inode = d_inode(dentry);
4441 if (inode->i_nlink == 1)
4442 nfs4_inode_return_delegation(inode);
4444 nfs4_inode_make_writeable(inode);
4447 err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
4448 trace_nfs4_remove(dir, &dentry->d_name, err);
4449 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4451 } while (exception.retry);
4455 static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
4457 struct nfs4_exception exception = {
4458 .interruptible = true,
4463 err = _nfs4_proc_remove(dir, name, NF4DIR);
4464 trace_nfs4_remove(dir, name, err);
4465 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4467 } while (exception.retry);
4471 static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4472 struct dentry *dentry,
4473 struct inode *inode)
4475 struct nfs_removeargs *args = msg->rpc_argp;
4476 struct nfs_removeres *res = msg->rpc_resp;
4478 res->server = NFS_SB(dentry->d_sb);
4479 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
4480 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
4482 nfs_fattr_init(res->dir_attr);
4485 nfs4_inode_return_delegation(inode);
4488 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4490 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
4491 &data->args.seq_args,
4496 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
4498 struct nfs_unlinkdata *data = task->tk_calldata;
4499 struct nfs_removeres *res = &data->res;
4501 if (!nfs4_sequence_done(task, &res->seq_res))
4503 if (nfs4_async_handle_error(task, res->server, NULL,
4504 &data->timeout) == -EAGAIN)
4506 if (task->tk_status == 0)
4507 update_changeattr(dir, &res->cinfo,
4508 res->dir_attr->time_start, 0);
4512 static void nfs4_proc_rename_setup(struct rpc_message *msg,
4513 struct dentry *old_dentry,
4514 struct dentry *new_dentry)
4516 struct nfs_renameargs *arg = msg->rpc_argp;
4517 struct nfs_renameres *res = msg->rpc_resp;
4518 struct inode *old_inode = d_inode(old_dentry);
4519 struct inode *new_inode = d_inode(new_dentry);
4522 nfs4_inode_make_writeable(old_inode);
4524 nfs4_inode_return_delegation(new_inode);
4525 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
4526 res->server = NFS_SB(old_dentry->d_sb);
4527 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
4530 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4532 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
4533 &data->args.seq_args,
4538 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4539 struct inode *new_dir)
4541 struct nfs_renamedata *data = task->tk_calldata;
4542 struct nfs_renameres *res = &data->res;
4544 if (!nfs4_sequence_done(task, &res->seq_res))
4546 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
4549 if (task->tk_status == 0) {
4550 if (new_dir != old_dir) {
4551 /* Note: If we moved a directory, nlink will change */
4552 update_changeattr(old_dir, &res->old_cinfo,
4553 res->old_fattr->time_start,
4554 NFS_INO_INVALID_OTHER);
4555 update_changeattr(new_dir, &res->new_cinfo,
4556 res->new_fattr->time_start,
4557 NFS_INO_INVALID_OTHER);
4559 update_changeattr(old_dir, &res->old_cinfo,
4560 res->old_fattr->time_start,
4566 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4568 struct nfs_server *server = NFS_SERVER(inode);
4569 __u32 bitmask[NFS4_BITMASK_SZ];
4570 struct nfs4_link_arg arg = {
4571 .fh = NFS_FH(inode),
4572 .dir_fh = NFS_FH(dir),
4576 struct nfs4_link_res res = {
4580 struct rpc_message msg = {
4581 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4585 int status = -ENOMEM;
4587 res.fattr = nfs_alloc_fattr();
4588 if (res.fattr == NULL)
4591 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4592 if (IS_ERR(res.label)) {
4593 status = PTR_ERR(res.label);
4597 nfs4_inode_make_writeable(inode);
4598 nfs4_bitmap_copy_adjust_setattr(bitmask, nfs4_bitmask(server, res.label), inode);
4600 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4602 update_changeattr(dir, &res.cinfo, res.fattr->time_start, 0);
4603 status = nfs_post_op_update_inode(inode, res.fattr);
4605 nfs_setsecurity(inode, res.fattr, res.label);
4609 nfs4_label_free(res.label);
4612 nfs_free_fattr(res.fattr);
4616 static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4618 struct nfs4_exception exception = {
4619 .interruptible = true,
4623 err = nfs4_handle_exception(NFS_SERVER(inode),
4624 _nfs4_proc_link(inode, dir, name),
4626 } while (exception.retry);
4630 struct nfs4_createdata {
4631 struct rpc_message msg;
4632 struct nfs4_create_arg arg;
4633 struct nfs4_create_res res;
4635 struct nfs_fattr fattr;
4636 struct nfs4_label *label;
4639 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
4640 const struct qstr *name, struct iattr *sattr, u32 ftype)
4642 struct nfs4_createdata *data;
4644 data = kzalloc(sizeof(*data), GFP_KERNEL);
4646 struct nfs_server *server = NFS_SERVER(dir);
4648 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4649 if (IS_ERR(data->label))
4652 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4653 data->msg.rpc_argp = &data->arg;
4654 data->msg.rpc_resp = &data->res;
4655 data->arg.dir_fh = NFS_FH(dir);
4656 data->arg.server = server;
4657 data->arg.name = name;
4658 data->arg.attrs = sattr;
4659 data->arg.ftype = ftype;
4660 data->arg.bitmask = nfs4_bitmask(server, data->label);
4661 data->arg.umask = current_umask();
4662 data->res.server = server;
4663 data->res.fh = &data->fh;
4664 data->res.fattr = &data->fattr;
4665 data->res.label = data->label;
4666 nfs_fattr_init(data->res.fattr);
4674 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4676 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
4677 &data->arg.seq_args, &data->res.seq_res, 1);
4679 spin_lock(&dir->i_lock);
4680 update_changeattr_locked(dir, &data->res.dir_cinfo,
4681 data->res.fattr->time_start, 0);
4682 /* Creating a directory bumps nlink in the parent */
4683 if (data->arg.ftype == NF4DIR)
4684 nfs4_inc_nlink_locked(dir);
4685 spin_unlock(&dir->i_lock);
4686 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
4691 static void nfs4_free_createdata(struct nfs4_createdata *data)
4693 nfs4_label_free(data->label);
4697 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4698 struct page *page, unsigned int len, struct iattr *sattr,
4699 struct nfs4_label *label)
4701 struct nfs4_createdata *data;
4702 int status = -ENAMETOOLONG;
4704 if (len > NFS4_MAXPATHLEN)
4708 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4712 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4713 data->arg.u.symlink.pages = &page;
4714 data->arg.u.symlink.len = len;
4715 data->arg.label = label;
4717 status = nfs4_do_create(dir, dentry, data);
4719 nfs4_free_createdata(data);
4724 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4725 struct page *page, unsigned int len, struct iattr *sattr)
4727 struct nfs4_exception exception = {
4728 .interruptible = true,
4730 struct nfs4_label l, *label = NULL;
4733 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4736 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4737 trace_nfs4_symlink(dir, &dentry->d_name, err);
4738 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4740 } while (exception.retry);
4742 nfs4_label_release_security(label);
4746 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4747 struct iattr *sattr, struct nfs4_label *label)
4749 struct nfs4_createdata *data;
4750 int status = -ENOMEM;
4752 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4756 data->arg.label = label;
4757 status = nfs4_do_create(dir, dentry, data);
4759 nfs4_free_createdata(data);
4764 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4765 struct iattr *sattr)
4767 struct nfs_server *server = NFS_SERVER(dir);
4768 struct nfs4_exception exception = {
4769 .interruptible = true,
4771 struct nfs4_label l, *label = NULL;
4774 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4776 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4777 sattr->ia_mode &= ~current_umask();
4779 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4780 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4781 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4783 } while (exception.retry);
4784 nfs4_label_release_security(label);
4789 static int _nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
4790 u64 cookie, struct page **pages, unsigned int count, bool plus)
4792 struct inode *dir = d_inode(dentry);
4793 struct nfs4_readdir_arg args = {
4798 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
4801 struct nfs4_readdir_res res;
4802 struct rpc_message msg = {
4803 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4810 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4812 (unsigned long long)cookie);
4813 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
4814 res.pgbase = args.pgbase;
4815 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4817 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
4818 status += args.pgbase;
4821 nfs_invalidate_atime(dir);
4823 dprintk("%s: returns %d\n", __func__, status);
4827 static int nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
4828 u64 cookie, struct page **pages, unsigned int count, bool plus)
4830 struct nfs4_exception exception = {
4831 .interruptible = true,
4835 err = _nfs4_proc_readdir(dentry, cred, cookie,
4836 pages, count, plus);
4837 trace_nfs4_readdir(d_inode(dentry), err);
4838 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
4840 } while (exception.retry);
4844 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4845 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
4847 struct nfs4_createdata *data;
4848 int mode = sattr->ia_mode;
4849 int status = -ENOMEM;
4851 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4856 data->arg.ftype = NF4FIFO;
4857 else if (S_ISBLK(mode)) {
4858 data->arg.ftype = NF4BLK;
4859 data->arg.u.device.specdata1 = MAJOR(rdev);
4860 data->arg.u.device.specdata2 = MINOR(rdev);
4862 else if (S_ISCHR(mode)) {
4863 data->arg.ftype = NF4CHR;
4864 data->arg.u.device.specdata1 = MAJOR(rdev);
4865 data->arg.u.device.specdata2 = MINOR(rdev);
4866 } else if (!S_ISSOCK(mode)) {
4871 data->arg.label = label;
4872 status = nfs4_do_create(dir, dentry, data);
4874 nfs4_free_createdata(data);
4879 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4880 struct iattr *sattr, dev_t rdev)
4882 struct nfs_server *server = NFS_SERVER(dir);
4883 struct nfs4_exception exception = {
4884 .interruptible = true,
4886 struct nfs4_label l, *label = NULL;
4889 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4891 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4892 sattr->ia_mode &= ~current_umask();
4894 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4895 trace_nfs4_mknod(dir, &dentry->d_name, err);
4896 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4898 } while (exception.retry);
4900 nfs4_label_release_security(label);
4905 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4906 struct nfs_fsstat *fsstat)
4908 struct nfs4_statfs_arg args = {
4910 .bitmask = server->attr_bitmask,
4912 struct nfs4_statfs_res res = {
4915 struct rpc_message msg = {
4916 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4921 nfs_fattr_init(fsstat->fattr);
4922 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4925 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4927 struct nfs4_exception exception = {
4928 .interruptible = true,
4932 err = nfs4_handle_exception(server,
4933 _nfs4_proc_statfs(server, fhandle, fsstat),
4935 } while (exception.retry);
4939 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4940 struct nfs_fsinfo *fsinfo)
4942 struct nfs4_fsinfo_arg args = {
4944 .bitmask = server->attr_bitmask,
4946 struct nfs4_fsinfo_res res = {
4949 struct rpc_message msg = {
4950 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4955 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4958 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4960 struct nfs4_exception exception = {
4961 .interruptible = true,
4963 unsigned long now = jiffies;
4967 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
4968 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
4970 nfs4_set_lease_period(server->nfs_client,
4971 fsinfo->lease_time * HZ,
4975 err = nfs4_handle_exception(server, err, &exception);
4976 } while (exception.retry);
4980 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4984 nfs_fattr_init(fsinfo->fattr);
4985 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
4987 /* block layout checks this! */
4988 server->pnfs_blksize = fsinfo->blksize;
4989 set_pnfs_layoutdriver(server, fhandle, fsinfo);
4995 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4996 struct nfs_pathconf *pathconf)
4998 struct nfs4_pathconf_arg args = {
5000 .bitmask = server->attr_bitmask,
5002 struct nfs4_pathconf_res res = {
5003 .pathconf = pathconf,
5005 struct rpc_message msg = {
5006 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
5011 /* None of the pathconf attributes are mandatory to implement */
5012 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
5013 memset(pathconf, 0, sizeof(*pathconf));
5017 nfs_fattr_init(pathconf->fattr);
5018 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5021 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5022 struct nfs_pathconf *pathconf)
5024 struct nfs4_exception exception = {
5025 .interruptible = true,
5030 err = nfs4_handle_exception(server,
5031 _nfs4_proc_pathconf(server, fhandle, pathconf),
5033 } while (exception.retry);
5037 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
5038 const struct nfs_open_context *ctx,
5039 const struct nfs_lock_context *l_ctx,
5042 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
5044 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
5046 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
5047 const struct nfs_open_context *ctx,
5048 const struct nfs_lock_context *l_ctx,
5051 nfs4_stateid current_stateid;
5053 /* If the current stateid represents a lost lock, then exit */
5054 if (nfs4_set_rw_stateid(¤t_stateid, ctx, l_ctx, fmode) == -EIO)
5056 return nfs4_stateid_match(stateid, ¤t_stateid);
5059 static bool nfs4_error_stateid_expired(int err)
5062 case -NFS4ERR_DELEG_REVOKED:
5063 case -NFS4ERR_ADMIN_REVOKED:
5064 case -NFS4ERR_BAD_STATEID:
5065 case -NFS4ERR_STALE_STATEID:
5066 case -NFS4ERR_OLD_STATEID:
5067 case -NFS4ERR_OPENMODE:
5068 case -NFS4ERR_EXPIRED:
5074 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
5076 struct nfs_server *server = NFS_SERVER(hdr->inode);
5078 trace_nfs4_read(hdr, task->tk_status);
5079 if (task->tk_status < 0) {
5080 struct nfs4_exception exception = {
5081 .inode = hdr->inode,
5082 .state = hdr->args.context->state,
5083 .stateid = &hdr->args.stateid,
5085 task->tk_status = nfs4_async_handle_exception(task,
5086 server, task->tk_status, &exception);
5087 if (exception.retry) {
5088 rpc_restart_call_prepare(task);
5093 if (task->tk_status > 0)
5094 renew_lease(server, hdr->timestamp);
5098 static bool nfs4_read_stateid_changed(struct rpc_task *task,
5099 struct nfs_pgio_args *args)
5102 if (!nfs4_error_stateid_expired(task->tk_status) ||
5103 nfs4_stateid_is_current(&args->stateid,
5108 rpc_restart_call_prepare(task);
5112 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5115 dprintk("--> %s\n", __func__);
5117 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5119 if (nfs4_read_stateid_changed(task, &hdr->args))
5121 if (task->tk_status > 0)
5122 nfs_invalidate_atime(hdr->inode);
5123 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5124 nfs4_read_done_cb(task, hdr);
5127 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5128 struct rpc_message *msg)
5130 hdr->timestamp = jiffies;
5131 if (!hdr->pgio_done_cb)
5132 hdr->pgio_done_cb = nfs4_read_done_cb;
5133 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5134 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5137 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5138 struct nfs_pgio_header *hdr)
5140 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
5141 &hdr->args.seq_args,
5145 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5146 hdr->args.lock_context,
5147 hdr->rw_mode) == -EIO)
5149 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
5154 static int nfs4_write_done_cb(struct rpc_task *task,
5155 struct nfs_pgio_header *hdr)
5157 struct inode *inode = hdr->inode;
5159 trace_nfs4_write(hdr, task->tk_status);
5160 if (task->tk_status < 0) {
5161 struct nfs4_exception exception = {
5162 .inode = hdr->inode,
5163 .state = hdr->args.context->state,
5164 .stateid = &hdr->args.stateid,
5166 task->tk_status = nfs4_async_handle_exception(task,
5167 NFS_SERVER(inode), task->tk_status,
5169 if (exception.retry) {
5170 rpc_restart_call_prepare(task);
5174 if (task->tk_status >= 0) {
5175 renew_lease(NFS_SERVER(inode), hdr->timestamp);
5176 nfs_writeback_update_inode(hdr);
5181 static bool nfs4_write_stateid_changed(struct rpc_task *task,
5182 struct nfs_pgio_args *args)
5185 if (!nfs4_error_stateid_expired(task->tk_status) ||
5186 nfs4_stateid_is_current(&args->stateid,
5191 rpc_restart_call_prepare(task);
5195 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5197 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5199 if (nfs4_write_stateid_changed(task, &hdr->args))
5201 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5202 nfs4_write_done_cb(task, hdr);
5206 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
5208 /* Don't request attributes for pNFS or O_DIRECT writes */
5209 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
5211 /* Otherwise, request attributes if and only if we don't hold
5214 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
5217 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
5218 struct rpc_message *msg,
5219 struct rpc_clnt **clnt)
5221 struct nfs_server *server = NFS_SERVER(hdr->inode);
5223 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5224 hdr->args.bitmask = NULL;
5225 hdr->res.fattr = NULL;
5227 hdr->args.bitmask = server->cache_consistency_bitmask;
5229 if (!hdr->pgio_done_cb)
5230 hdr->pgio_done_cb = nfs4_write_done_cb;
5231 hdr->res.server = server;
5232 hdr->timestamp = jiffies;
5234 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
5235 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1, 0);
5236 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
5239 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5241 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
5242 &data->args.seq_args,
5247 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
5249 struct inode *inode = data->inode;
5251 trace_nfs4_commit(data, task->tk_status);
5252 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5253 NULL, NULL) == -EAGAIN) {
5254 rpc_restart_call_prepare(task);
5260 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
5262 if (!nfs4_sequence_done(task, &data->res.seq_res))
5264 return data->commit_done_cb(task, data);
5267 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5268 struct rpc_clnt **clnt)
5270 struct nfs_server *server = NFS_SERVER(data->inode);
5272 if (data->commit_done_cb == NULL)
5273 data->commit_done_cb = nfs4_commit_done_cb;
5274 data->res.server = server;
5275 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
5276 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
5277 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
5280 static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5281 struct nfs_commitres *res)
5283 struct inode *dst_inode = file_inode(dst);
5284 struct nfs_server *server = NFS_SERVER(dst_inode);
5285 struct rpc_message msg = {
5286 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5291 args->fh = NFS_FH(dst_inode);
5292 return nfs4_call_sync(server->client, server, &msg,
5293 &args->seq_args, &res->seq_res, 1);
5296 int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5298 struct nfs_commitargs args = {
5302 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5303 struct nfs4_exception exception = { };
5307 status = _nfs4_proc_commit(dst, &args, res);
5308 status = nfs4_handle_exception(dst_server, status, &exception);
5309 } while (exception.retry);
5314 struct nfs4_renewdata {
5315 struct nfs_client *client;
5316 unsigned long timestamp;
5320 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5321 * standalone procedure for queueing an asynchronous RENEW.
5323 static void nfs4_renew_release(void *calldata)
5325 struct nfs4_renewdata *data = calldata;
5326 struct nfs_client *clp = data->client;
5328 if (refcount_read(&clp->cl_count) > 1)
5329 nfs4_schedule_state_renewal(clp);
5330 nfs_put_client(clp);
5334 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
5336 struct nfs4_renewdata *data = calldata;
5337 struct nfs_client *clp = data->client;
5338 unsigned long timestamp = data->timestamp;
5340 trace_nfs4_renew_async(clp, task->tk_status);
5341 switch (task->tk_status) {
5344 case -NFS4ERR_LEASE_MOVED:
5345 nfs4_schedule_lease_moved_recovery(clp);
5348 /* Unless we're shutting down, schedule state recovery! */
5349 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5351 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
5352 nfs4_schedule_lease_recovery(clp);
5355 nfs4_schedule_path_down_recovery(clp);
5357 do_renew_lease(clp, timestamp);
5360 static const struct rpc_call_ops nfs4_renew_ops = {
5361 .rpc_call_done = nfs4_renew_done,
5362 .rpc_release = nfs4_renew_release,
5365 static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
5367 struct rpc_message msg = {
5368 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5372 struct nfs4_renewdata *data;
5374 if (renew_flags == 0)
5376 if (!refcount_inc_not_zero(&clp->cl_count))
5378 data = kmalloc(sizeof(*data), GFP_NOFS);
5380 nfs_put_client(clp);
5384 data->timestamp = jiffies;
5385 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
5386 &nfs4_renew_ops, data);
5389 static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
5391 struct rpc_message msg = {
5392 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5396 unsigned long now = jiffies;
5399 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5402 do_renew_lease(clp, now);
5406 static inline int nfs4_server_supports_acls(struct nfs_server *server)
5408 return server->caps & NFS_CAP_ACLS;
5411 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5412 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
5415 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
5417 static int buf_to_pages_noslab(const void *buf, size_t buflen,
5418 struct page **pages)
5420 struct page *newpage, **spages;
5426 len = min_t(size_t, PAGE_SIZE, buflen);
5427 newpage = alloc_page(GFP_KERNEL);
5429 if (newpage == NULL)
5431 memcpy(page_address(newpage), buf, len);
5436 } while (buflen != 0);
5442 __free_page(spages[rc-1]);
5446 struct nfs4_cached_acl {
5452 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
5454 struct nfs_inode *nfsi = NFS_I(inode);
5456 spin_lock(&inode->i_lock);
5457 kfree(nfsi->nfs4_acl);
5458 nfsi->nfs4_acl = acl;
5459 spin_unlock(&inode->i_lock);
5462 static void nfs4_zap_acl_attr(struct inode *inode)
5464 nfs4_set_cached_acl(inode, NULL);
5467 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5469 struct nfs_inode *nfsi = NFS_I(inode);
5470 struct nfs4_cached_acl *acl;
5473 spin_lock(&inode->i_lock);
5474 acl = nfsi->nfs4_acl;
5477 if (buf == NULL) /* user is just asking for length */
5479 if (acl->cached == 0)
5481 ret = -ERANGE; /* see getxattr(2) man page */
5482 if (acl->len > buflen)
5484 memcpy(buf, acl->data, acl->len);
5488 spin_unlock(&inode->i_lock);
5492 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
5494 struct nfs4_cached_acl *acl;
5495 size_t buflen = sizeof(*acl) + acl_len;
5497 if (buflen <= PAGE_SIZE) {
5498 acl = kmalloc(buflen, GFP_KERNEL);
5502 _copy_from_pages(acl->data, pages, pgbase, acl_len);
5504 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5511 nfs4_set_cached_acl(inode, acl);
5515 * The getxattr API returns the required buffer length when called with a
5516 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5517 * the required buf. On a NULL buf, we send a page of data to the server
5518 * guessing that the ACL request can be serviced by a page. If so, we cache
5519 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5520 * the cache. If not so, we throw away the page, and cache the required
5521 * length. The next getxattr call will then produce another round trip to
5522 * the server, this time with the input buf of the required size.
5524 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5526 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
5527 struct nfs_getaclargs args = {
5528 .fh = NFS_FH(inode),
5532 struct nfs_getaclres res = {
5535 struct rpc_message msg = {
5536 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5540 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
5541 int ret = -ENOMEM, i;
5543 if (npages > ARRAY_SIZE(pages))
5546 for (i = 0; i < npages; i++) {
5547 pages[i] = alloc_page(GFP_KERNEL);
5552 /* for decoding across pages */
5553 res.acl_scratch = alloc_page(GFP_KERNEL);
5554 if (!res.acl_scratch)
5557 args.acl_len = npages * PAGE_SIZE;
5559 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
5560 __func__, buf, buflen, npages, args.acl_len);
5561 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5562 &msg, &args.seq_args, &res.seq_res, 0);
5566 /* Handle the case where the passed-in buffer is too short */
5567 if (res.acl_flags & NFS4_ACL_TRUNC) {
5568 /* Did the user only issue a request for the acl length? */
5574 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
5576 if (res.acl_len > buflen) {
5580 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
5585 for (i = 0; i < npages; i++)
5587 __free_page(pages[i]);
5588 if (res.acl_scratch)
5589 __free_page(res.acl_scratch);
5593 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5595 struct nfs4_exception exception = {
5596 .interruptible = true,
5600 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
5601 trace_nfs4_get_acl(inode, ret);
5604 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5605 } while (exception.retry);
5609 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5611 struct nfs_server *server = NFS_SERVER(inode);
5614 if (!nfs4_server_supports_acls(server))
5616 ret = nfs_revalidate_inode(server, inode);
5619 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5620 nfs_zap_acl_cache(inode);
5621 ret = nfs4_read_cached_acl(inode, buf, buflen);
5623 /* -ENOENT is returned if there is no ACL or if there is an ACL
5624 * but no cached acl data, just the acl length */
5626 return nfs4_get_acl_uncached(inode, buf, buflen);
5629 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5631 struct nfs_server *server = NFS_SERVER(inode);
5632 struct page *pages[NFS4ACL_MAXPAGES];
5633 struct nfs_setaclargs arg = {
5634 .fh = NFS_FH(inode),
5638 struct nfs_setaclres res;
5639 struct rpc_message msg = {
5640 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5644 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
5647 if (!nfs4_server_supports_acls(server))
5649 if (npages > ARRAY_SIZE(pages))
5651 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
5654 nfs4_inode_make_writeable(inode);
5655 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5658 * Free each page after tx, so the only ref left is
5659 * held by the network stack
5662 put_page(pages[i-1]);
5665 * Acl update can result in inode attribute update.
5666 * so mark the attribute cache invalid.
5668 spin_lock(&inode->i_lock);
5669 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
5670 | NFS_INO_INVALID_CTIME
5671 | NFS_INO_REVAL_FORCED;
5672 spin_unlock(&inode->i_lock);
5673 nfs_access_zap_cache(inode);
5674 nfs_zap_acl_cache(inode);
5678 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5680 struct nfs4_exception exception = { };
5683 err = __nfs4_proc_set_acl(inode, buf, buflen);
5684 trace_nfs4_set_acl(inode, err);
5685 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5687 } while (exception.retry);
5691 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
5692 static int _nfs4_get_security_label(struct inode *inode, void *buf,
5695 struct nfs_server *server = NFS_SERVER(inode);
5696 struct nfs_fattr fattr;
5697 struct nfs4_label label = {0, 0, buflen, buf};
5699 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5700 struct nfs4_getattr_arg arg = {
5701 .fh = NFS_FH(inode),
5704 struct nfs4_getattr_res res = {
5709 struct rpc_message msg = {
5710 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
5716 nfs_fattr_init(&fattr);
5718 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
5721 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5723 if (buflen < label.len)
5728 static int nfs4_get_security_label(struct inode *inode, void *buf,
5731 struct nfs4_exception exception = {
5732 .interruptible = true,
5736 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5740 err = _nfs4_get_security_label(inode, buf, buflen);
5741 trace_nfs4_get_security_label(inode, err);
5742 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5744 } while (exception.retry);
5748 static int _nfs4_do_set_security_label(struct inode *inode,
5749 struct nfs4_label *ilabel,
5750 struct nfs_fattr *fattr,
5751 struct nfs4_label *olabel)
5754 struct iattr sattr = {0};
5755 struct nfs_server *server = NFS_SERVER(inode);
5756 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5757 struct nfs_setattrargs arg = {
5758 .fh = NFS_FH(inode),
5764 struct nfs_setattrres res = {
5769 struct rpc_message msg = {
5770 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5776 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
5778 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5780 dprintk("%s failed: %d\n", __func__, status);
5785 static int nfs4_do_set_security_label(struct inode *inode,
5786 struct nfs4_label *ilabel,
5787 struct nfs_fattr *fattr,
5788 struct nfs4_label *olabel)
5790 struct nfs4_exception exception = { };
5794 err = _nfs4_do_set_security_label(inode, ilabel,
5796 trace_nfs4_set_security_label(inode, err);
5797 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5799 } while (exception.retry);
5804 nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
5806 struct nfs4_label ilabel, *olabel = NULL;
5807 struct nfs_fattr fattr;
5810 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5813 nfs_fattr_init(&fattr);
5817 ilabel.label = (char *)buf;
5818 ilabel.len = buflen;
5820 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5821 if (IS_ERR(olabel)) {
5822 status = -PTR_ERR(olabel);
5826 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5828 nfs_setsecurity(inode, &fattr, olabel);
5830 nfs4_label_free(olabel);
5834 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5837 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5838 nfs4_verifier *bootverf)
5842 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5843 /* An impossible timestamp guarantees this value
5844 * will never match a generated boot time. */
5845 verf[0] = cpu_to_be32(U32_MAX);
5846 verf[1] = cpu_to_be32(U32_MAX);
5848 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
5849 u64 ns = ktime_to_ns(nn->boot_time);
5851 verf[0] = cpu_to_be32(ns >> 32);
5852 verf[1] = cpu_to_be32(ns);
5854 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5858 nfs4_init_nonuniform_client_string(struct nfs_client *clp)
5863 if (clp->cl_owner_id != NULL)
5868 strlen(clp->cl_rpcclient->cl_nodename) +
5870 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5873 if (nfs4_client_id_uniquifier[0] != '\0')
5874 len += strlen(nfs4_client_id_uniquifier) + 1;
5875 if (len > NFS4_OPAQUE_LIMIT + 1)
5879 * Since this string is allocated at mount time, and held until the
5880 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5881 * about a memory-reclaim deadlock.
5883 str = kmalloc(len, GFP_KERNEL);
5888 if (nfs4_client_id_uniquifier[0] != '\0')
5889 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
5890 clp->cl_rpcclient->cl_nodename,
5891 nfs4_client_id_uniquifier,
5892 rpc_peeraddr2str(clp->cl_rpcclient,
5895 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
5896 clp->cl_rpcclient->cl_nodename,
5897 rpc_peeraddr2str(clp->cl_rpcclient,
5901 clp->cl_owner_id = str;
5906 nfs4_init_uniquifier_client_string(struct nfs_client *clp)
5911 len = 10 + 10 + 1 + 10 + 1 +
5912 strlen(nfs4_client_id_uniquifier) + 1 +
5913 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5915 if (len > NFS4_OPAQUE_LIMIT + 1)
5919 * Since this string is allocated at mount time, and held until the
5920 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5921 * about a memory-reclaim deadlock.
5923 str = kmalloc(len, GFP_KERNEL);
5927 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
5928 clp->rpc_ops->version, clp->cl_minorversion,
5929 nfs4_client_id_uniquifier,
5930 clp->cl_rpcclient->cl_nodename);
5931 clp->cl_owner_id = str;
5936 nfs4_init_uniform_client_string(struct nfs_client *clp)
5941 if (clp->cl_owner_id != NULL)
5944 if (nfs4_client_id_uniquifier[0] != '\0')
5945 return nfs4_init_uniquifier_client_string(clp);
5947 len = 10 + 10 + 1 + 10 + 1 +
5948 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5950 if (len > NFS4_OPAQUE_LIMIT + 1)
5954 * Since this string is allocated at mount time, and held until the
5955 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5956 * about a memory-reclaim deadlock.
5958 str = kmalloc(len, GFP_KERNEL);
5962 scnprintf(str, len, "Linux NFSv%u.%u %s",
5963 clp->rpc_ops->version, clp->cl_minorversion,
5964 clp->cl_rpcclient->cl_nodename);
5965 clp->cl_owner_id = str;
5970 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5971 * services. Advertise one based on the address family of the
5975 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5977 if (strchr(clp->cl_ipaddr, ':') != NULL)
5978 return scnprintf(buf, len, "tcp6");
5980 return scnprintf(buf, len, "tcp");
5983 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5985 struct nfs4_setclientid *sc = calldata;
5987 if (task->tk_status == 0)
5988 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5991 static const struct rpc_call_ops nfs4_setclientid_ops = {
5992 .rpc_call_done = nfs4_setclientid_done,
5996 * nfs4_proc_setclientid - Negotiate client ID
5997 * @clp: state data structure
5998 * @program: RPC program for NFSv4 callback service
5999 * @port: IP port number for NFS4 callback service
6000 * @cred: credential to use for this call
6001 * @res: where to place the result
6003 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6005 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
6006 unsigned short port, const struct cred *cred,
6007 struct nfs4_setclientid_res *res)
6009 nfs4_verifier sc_verifier;
6010 struct nfs4_setclientid setclientid = {
6011 .sc_verifier = &sc_verifier,
6015 struct rpc_message msg = {
6016 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
6017 .rpc_argp = &setclientid,
6021 struct rpc_task *task;
6022 struct rpc_task_setup task_setup_data = {
6023 .rpc_client = clp->cl_rpcclient,
6024 .rpc_message = &msg,
6025 .callback_ops = &nfs4_setclientid_ops,
6026 .callback_data = &setclientid,
6027 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
6031 /* nfs_client_id4 */
6032 nfs4_init_boot_verifier(clp, &sc_verifier);
6034 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
6035 status = nfs4_init_uniform_client_string(clp);
6037 status = nfs4_init_nonuniform_client_string(clp);
6043 setclientid.sc_netid_len =
6044 nfs4_init_callback_netid(clp,
6045 setclientid.sc_netid,
6046 sizeof(setclientid.sc_netid));
6047 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
6048 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
6049 clp->cl_ipaddr, port >> 8, port & 255);
6051 dprintk("NFS call setclientid auth=%s, '%s'\n",
6052 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6054 task = rpc_run_task(&task_setup_data);
6056 status = PTR_ERR(task);
6059 status = task->tk_status;
6060 if (setclientid.sc_cred) {
6061 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
6062 put_rpccred(setclientid.sc_cred);
6066 trace_nfs4_setclientid(clp, status);
6067 dprintk("NFS reply setclientid: %d\n", status);
6072 * nfs4_proc_setclientid_confirm - Confirm client ID
6073 * @clp: state data structure
6074 * @arg: result of a previous SETCLIENTID
6075 * @cred: credential to use for this call
6077 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6079 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
6080 struct nfs4_setclientid_res *arg,
6081 const struct cred *cred)
6083 struct rpc_message msg = {
6084 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
6090 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
6091 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6093 status = rpc_call_sync(clp->cl_rpcclient, &msg,
6094 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
6095 trace_nfs4_setclientid_confirm(clp, status);
6096 dprintk("NFS reply setclientid_confirm: %d\n", status);
6100 struct nfs4_delegreturndata {
6101 struct nfs4_delegreturnargs args;
6102 struct nfs4_delegreturnres res;
6104 nfs4_stateid stateid;
6105 unsigned long timestamp;
6107 struct nfs4_layoutreturn_args arg;
6108 struct nfs4_layoutreturn_res res;
6109 struct nfs4_xdr_opaque_data ld_private;
6113 struct nfs_fattr fattr;
6115 struct inode *inode;
6118 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6120 struct nfs4_delegreturndata *data = calldata;
6121 struct nfs4_exception exception = {
6122 .inode = data->inode,
6123 .stateid = &data->stateid,
6126 if (!nfs4_sequence_done(task, &data->res.seq_res))
6129 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
6131 /* Handle Layoutreturn errors */
6132 if (data->args.lr_args && task->tk_status != 0) {
6133 switch(data->res.lr_ret) {
6135 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
6138 data->args.lr_args = NULL;
6139 data->res.lr_res = NULL;
6141 case -NFS4ERR_OLD_STATEID:
6142 if (nfs4_layoutreturn_refresh_stateid(&data->args.lr_args->stateid,
6143 &data->args.lr_args->range,
6147 case -NFS4ERR_ADMIN_REVOKED:
6148 case -NFS4ERR_DELEG_REVOKED:
6149 case -NFS4ERR_EXPIRED:
6150 case -NFS4ERR_BAD_STATEID:
6151 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
6152 case -NFS4ERR_WRONG_CRED:
6153 data->args.lr_args = NULL;
6154 data->res.lr_res = NULL;
6159 switch (task->tk_status) {
6161 renew_lease(data->res.server, data->timestamp);
6163 case -NFS4ERR_ADMIN_REVOKED:
6164 case -NFS4ERR_DELEG_REVOKED:
6165 case -NFS4ERR_EXPIRED:
6166 nfs4_free_revoked_stateid(data->res.server,
6168 task->tk_msg.rpc_cred);
6170 case -NFS4ERR_BAD_STATEID:
6171 case -NFS4ERR_STALE_STATEID:
6172 task->tk_status = 0;
6174 case -NFS4ERR_OLD_STATEID:
6175 if (nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6177 task->tk_status = 0;
6179 case -NFS4ERR_ACCESS:
6180 if (data->args.bitmask) {
6181 data->args.bitmask = NULL;
6182 data->res.fattr = NULL;
6187 task->tk_status = nfs4_async_handle_exception(task,
6188 data->res.server, task->tk_status,
6190 if (exception.retry)
6193 data->rpc_status = task->tk_status;
6196 data->res.lr_ret = 0;
6198 task->tk_status = 0;
6199 rpc_restart_call_prepare(task);
6202 static void nfs4_delegreturn_release(void *calldata)
6204 struct nfs4_delegreturndata *data = calldata;
6205 struct inode *inode = data->inode;
6209 pnfs_roc_release(&data->lr.arg, &data->lr.res,
6211 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
6212 nfs_iput_and_deactive(inode);
6217 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6219 struct nfs4_delegreturndata *d_data;
6220 struct pnfs_layout_hdr *lo;
6222 d_data = (struct nfs4_delegreturndata *)data;
6224 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
6227 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6228 if (lo && !pnfs_layout_is_valid(lo)) {
6229 d_data->args.lr_args = NULL;
6230 d_data->res.lr_res = NULL;
6233 nfs4_setup_sequence(d_data->res.server->nfs_client,
6234 &d_data->args.seq_args,
6235 &d_data->res.seq_res,
6239 static const struct rpc_call_ops nfs4_delegreturn_ops = {
6240 .rpc_call_prepare = nfs4_delegreturn_prepare,
6241 .rpc_call_done = nfs4_delegreturn_done,
6242 .rpc_release = nfs4_delegreturn_release,
6245 static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6247 struct nfs4_delegreturndata *data;
6248 struct nfs_server *server = NFS_SERVER(inode);
6249 struct rpc_task *task;
6250 struct rpc_message msg = {
6251 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6254 struct rpc_task_setup task_setup_data = {
6255 .rpc_client = server->client,
6256 .rpc_message = &msg,
6257 .callback_ops = &nfs4_delegreturn_ops,
6258 .flags = RPC_TASK_ASYNC,
6262 data = kzalloc(sizeof(*data), GFP_NOFS);
6265 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
6267 nfs4_state_protect(server->nfs_client,
6268 NFS_SP4_MACH_CRED_CLEANUP,
6269 &task_setup_data.rpc_client, &msg);
6271 data->args.fhandle = &data->fh;
6272 data->args.stateid = &data->stateid;
6273 data->args.bitmask = server->cache_consistency_bitmask;
6274 nfs_copy_fh(&data->fh, NFS_FH(inode));
6275 nfs4_stateid_copy(&data->stateid, stateid);
6276 data->res.fattr = &data->fattr;
6277 data->res.server = server;
6278 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
6279 data->lr.arg.ld_private = &data->lr.ld_private;
6280 nfs_fattr_init(data->res.fattr);
6281 data->timestamp = jiffies;
6282 data->rpc_status = 0;
6283 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
6284 data->inode = nfs_igrab_and_active(inode);
6287 data->args.lr_args = &data->lr.arg;
6288 data->res.lr_res = &data->lr.res;
6290 } else if (data->lr.roc) {
6291 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
6292 data->lr.roc = false;
6295 task_setup_data.callback_data = data;
6296 msg.rpc_argp = &data->args;
6297 msg.rpc_resp = &data->res;
6298 task = rpc_run_task(&task_setup_data);
6300 return PTR_ERR(task);
6303 status = rpc_wait_for_completion_task(task);
6306 status = data->rpc_status;
6312 int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6314 struct nfs_server *server = NFS_SERVER(inode);
6315 struct nfs4_exception exception = { };
6318 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
6319 trace_nfs4_delegreturn(inode, stateid, err);
6321 case -NFS4ERR_STALE_STATEID:
6322 case -NFS4ERR_EXPIRED:
6326 err = nfs4_handle_exception(server, err, &exception);
6327 } while (exception.retry);
6331 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6333 struct inode *inode = state->inode;
6334 struct nfs_server *server = NFS_SERVER(inode);
6335 struct nfs_client *clp = server->nfs_client;
6336 struct nfs_lockt_args arg = {
6337 .fh = NFS_FH(inode),
6340 struct nfs_lockt_res res = {
6343 struct rpc_message msg = {
6344 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
6347 .rpc_cred = state->owner->so_cred,
6349 struct nfs4_lock_state *lsp;
6352 arg.lock_owner.clientid = clp->cl_clientid;
6353 status = nfs4_set_lock_state(state, request);
6356 lsp = request->fl_u.nfs4_fl.owner;
6357 arg.lock_owner.id = lsp->ls_seqid.owner_id;
6358 arg.lock_owner.s_dev = server->s_dev;
6359 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6362 request->fl_type = F_UNLCK;
6364 case -NFS4ERR_DENIED:
6367 request->fl_ops->fl_release_private(request);
6368 request->fl_ops = NULL;
6373 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6375 struct nfs4_exception exception = {
6376 .interruptible = true,
6381 err = _nfs4_proc_getlk(state, cmd, request);
6382 trace_nfs4_get_lock(request, state, cmd, err);
6383 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
6385 } while (exception.retry);
6389 struct nfs4_unlockdata {
6390 struct nfs_locku_args arg;
6391 struct nfs_locku_res res;
6392 struct nfs4_lock_state *lsp;
6393 struct nfs_open_context *ctx;
6394 struct nfs_lock_context *l_ctx;
6395 struct file_lock fl;
6396 struct nfs_server *server;
6397 unsigned long timestamp;
6400 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6401 struct nfs_open_context *ctx,
6402 struct nfs4_lock_state *lsp,
6403 struct nfs_seqid *seqid)
6405 struct nfs4_unlockdata *p;
6406 struct inode *inode = lsp->ls_state->inode;
6408 p = kzalloc(sizeof(*p), GFP_NOFS);
6411 p->arg.fh = NFS_FH(inode);
6413 p->arg.seqid = seqid;
6414 p->res.seqid = seqid;
6416 /* Ensure we don't close file until we're done freeing locks! */
6417 p->ctx = get_nfs_open_context(ctx);
6418 p->l_ctx = nfs_get_lock_context(ctx);
6419 locks_init_lock(&p->fl);
6420 locks_copy_lock(&p->fl, fl);
6421 p->server = NFS_SERVER(inode);
6425 static void nfs4_locku_release_calldata(void *data)
6427 struct nfs4_unlockdata *calldata = data;
6428 nfs_free_seqid(calldata->arg.seqid);
6429 nfs4_put_lock_state(calldata->lsp);
6430 nfs_put_lock_context(calldata->l_ctx);
6431 put_nfs_open_context(calldata->ctx);
6435 static void nfs4_locku_done(struct rpc_task *task, void *data)
6437 struct nfs4_unlockdata *calldata = data;
6438 struct nfs4_exception exception = {
6439 .inode = calldata->lsp->ls_state->inode,
6440 .stateid = &calldata->arg.stateid,
6443 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6445 switch (task->tk_status) {
6447 renew_lease(calldata->server, calldata->timestamp);
6448 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
6449 if (nfs4_update_lock_stateid(calldata->lsp,
6450 &calldata->res.stateid))
6453 case -NFS4ERR_ADMIN_REVOKED:
6454 case -NFS4ERR_EXPIRED:
6455 nfs4_free_revoked_stateid(calldata->server,
6456 &calldata->arg.stateid,
6457 task->tk_msg.rpc_cred);
6459 case -NFS4ERR_BAD_STATEID:
6460 case -NFS4ERR_OLD_STATEID:
6461 case -NFS4ERR_STALE_STATEID:
6462 if (!nfs4_stateid_match(&calldata->arg.stateid,
6463 &calldata->lsp->ls_stateid))
6464 rpc_restart_call_prepare(task);
6467 task->tk_status = nfs4_async_handle_exception(task,
6468 calldata->server, task->tk_status,
6470 if (exception.retry)
6471 rpc_restart_call_prepare(task);
6473 nfs_release_seqid(calldata->arg.seqid);
6476 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
6478 struct nfs4_unlockdata *calldata = data;
6480 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6481 nfs_async_iocounter_wait(task, calldata->l_ctx))
6484 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
6486 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
6487 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
6488 /* Note: exit _without_ running nfs4_locku_done */
6491 calldata->timestamp = jiffies;
6492 if (nfs4_setup_sequence(calldata->server->nfs_client,
6493 &calldata->arg.seq_args,
6494 &calldata->res.seq_res,
6496 nfs_release_seqid(calldata->arg.seqid);
6499 task->tk_action = NULL;
6501 nfs4_sequence_done(task, &calldata->res.seq_res);
6504 static const struct rpc_call_ops nfs4_locku_ops = {
6505 .rpc_call_prepare = nfs4_locku_prepare,
6506 .rpc_call_done = nfs4_locku_done,
6507 .rpc_release = nfs4_locku_release_calldata,
6510 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6511 struct nfs_open_context *ctx,
6512 struct nfs4_lock_state *lsp,
6513 struct nfs_seqid *seqid)
6515 struct nfs4_unlockdata *data;
6516 struct rpc_message msg = {
6517 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6518 .rpc_cred = ctx->cred,
6520 struct rpc_task_setup task_setup_data = {
6521 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
6522 .rpc_message = &msg,
6523 .callback_ops = &nfs4_locku_ops,
6524 .workqueue = nfsiod_workqueue,
6525 .flags = RPC_TASK_ASYNC,
6528 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6529 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6531 /* Ensure this is an unlock - when canceling a lock, the
6532 * canceled lock is passed in, and it won't be an unlock.
6534 fl->fl_type = F_UNLCK;
6535 if (fl->fl_flags & FL_CLOSE)
6536 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
6538 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6540 nfs_free_seqid(seqid);
6541 return ERR_PTR(-ENOMEM);
6544 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
6545 msg.rpc_argp = &data->arg;
6546 msg.rpc_resp = &data->res;
6547 task_setup_data.callback_data = data;
6548 return rpc_run_task(&task_setup_data);
6551 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6553 struct inode *inode = state->inode;
6554 struct nfs4_state_owner *sp = state->owner;
6555 struct nfs_inode *nfsi = NFS_I(inode);
6556 struct nfs_seqid *seqid;
6557 struct nfs4_lock_state *lsp;
6558 struct rpc_task *task;
6559 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6561 unsigned char fl_flags = request->fl_flags;
6563 status = nfs4_set_lock_state(state, request);
6564 /* Unlock _before_ we do the RPC call */
6565 request->fl_flags |= FL_EXISTS;
6566 /* Exclude nfs_delegation_claim_locks() */
6567 mutex_lock(&sp->so_delegreturn_mutex);
6568 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
6569 down_read(&nfsi->rwsem);
6570 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
6571 up_read(&nfsi->rwsem);
6572 mutex_unlock(&sp->so_delegreturn_mutex);
6575 up_read(&nfsi->rwsem);
6576 mutex_unlock(&sp->so_delegreturn_mutex);
6579 /* Is this a delegated lock? */
6580 lsp = request->fl_u.nfs4_fl.owner;
6581 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6583 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6584 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
6588 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
6589 status = PTR_ERR(task);
6592 status = rpc_wait_for_completion_task(task);
6595 request->fl_flags = fl_flags;
6596 trace_nfs4_unlock(request, state, F_SETLK, status);
6600 struct nfs4_lockdata {
6601 struct nfs_lock_args arg;
6602 struct nfs_lock_res res;
6603 struct nfs4_lock_state *lsp;
6604 struct nfs_open_context *ctx;
6605 struct file_lock fl;
6606 unsigned long timestamp;
6609 struct nfs_server *server;
6612 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
6613 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6616 struct nfs4_lockdata *p;
6617 struct inode *inode = lsp->ls_state->inode;
6618 struct nfs_server *server = NFS_SERVER(inode);
6619 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6621 p = kzalloc(sizeof(*p), gfp_mask);
6625 p->arg.fh = NFS_FH(inode);
6627 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
6628 if (IS_ERR(p->arg.open_seqid))
6630 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6631 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
6632 if (IS_ERR(p->arg.lock_seqid))
6633 goto out_free_seqid;
6634 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
6635 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
6636 p->arg.lock_owner.s_dev = server->s_dev;
6637 p->res.lock_seqid = p->arg.lock_seqid;
6640 p->ctx = get_nfs_open_context(ctx);
6641 locks_init_lock(&p->fl);
6642 locks_copy_lock(&p->fl, fl);
6645 nfs_free_seqid(p->arg.open_seqid);
6651 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6653 struct nfs4_lockdata *data = calldata;
6654 struct nfs4_state *state = data->lsp->ls_state;
6656 dprintk("%s: begin!\n", __func__);
6657 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
6659 /* Do we need to do an open_to_lock_owner? */
6660 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
6661 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
6662 goto out_release_lock_seqid;
6664 nfs4_stateid_copy(&data->arg.open_stateid,
6665 &state->open_stateid);
6666 data->arg.new_lock_owner = 1;
6667 data->res.open_seqid = data->arg.open_seqid;
6669 data->arg.new_lock_owner = 0;
6670 nfs4_stateid_copy(&data->arg.lock_stateid,
6671 &data->lsp->ls_stateid);
6673 if (!nfs4_valid_open_stateid(state)) {
6674 data->rpc_status = -EBADF;
6675 task->tk_action = NULL;
6676 goto out_release_open_seqid;
6678 data->timestamp = jiffies;
6679 if (nfs4_setup_sequence(data->server->nfs_client,
6680 &data->arg.seq_args,
6684 out_release_open_seqid:
6685 nfs_release_seqid(data->arg.open_seqid);
6686 out_release_lock_seqid:
6687 nfs_release_seqid(data->arg.lock_seqid);
6689 nfs4_sequence_done(task, &data->res.seq_res);
6690 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
6693 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6695 struct nfs4_lockdata *data = calldata;
6696 struct nfs4_lock_state *lsp = data->lsp;
6698 dprintk("%s: begin!\n", __func__);
6700 if (!nfs4_sequence_done(task, &data->res.seq_res))
6703 data->rpc_status = task->tk_status;
6704 switch (task->tk_status) {
6706 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
6708 if (data->arg.new_lock && !data->cancelled) {
6709 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
6710 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
6713 if (data->arg.new_lock_owner != 0) {
6714 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6715 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6716 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6717 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6720 case -NFS4ERR_BAD_STATEID:
6721 case -NFS4ERR_OLD_STATEID:
6722 case -NFS4ERR_STALE_STATEID:
6723 case -NFS4ERR_EXPIRED:
6724 if (data->arg.new_lock_owner != 0) {
6725 if (!nfs4_stateid_match(&data->arg.open_stateid,
6726 &lsp->ls_state->open_stateid))
6728 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6733 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
6736 if (!data->cancelled)
6737 rpc_restart_call_prepare(task);
6741 static void nfs4_lock_release(void *calldata)
6743 struct nfs4_lockdata *data = calldata;
6745 dprintk("%s: begin!\n", __func__);
6746 nfs_free_seqid(data->arg.open_seqid);
6747 if (data->cancelled && data->rpc_status == 0) {
6748 struct rpc_task *task;
6749 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6750 data->arg.lock_seqid);
6752 rpc_put_task_async(task);
6753 dprintk("%s: cancelling lock!\n", __func__);
6755 nfs_free_seqid(data->arg.lock_seqid);
6756 nfs4_put_lock_state(data->lsp);
6757 put_nfs_open_context(data->ctx);
6759 dprintk("%s: done!\n", __func__);
6762 static const struct rpc_call_ops nfs4_lock_ops = {
6763 .rpc_call_prepare = nfs4_lock_prepare,
6764 .rpc_call_done = nfs4_lock_done,
6765 .rpc_release = nfs4_lock_release,
6768 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6771 case -NFS4ERR_ADMIN_REVOKED:
6772 case -NFS4ERR_EXPIRED:
6773 case -NFS4ERR_BAD_STATEID:
6774 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6775 if (new_lock_owner != 0 ||
6776 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
6777 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
6779 case -NFS4ERR_STALE_STATEID:
6780 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6781 nfs4_schedule_lease_recovery(server->nfs_client);
6785 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
6787 struct nfs4_lockdata *data;
6788 struct rpc_task *task;
6789 struct rpc_message msg = {
6790 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6791 .rpc_cred = state->owner->so_cred,
6793 struct rpc_task_setup task_setup_data = {
6794 .rpc_client = NFS_CLIENT(state->inode),
6795 .rpc_message = &msg,
6796 .callback_ops = &nfs4_lock_ops,
6797 .workqueue = nfsiod_workqueue,
6798 .flags = RPC_TASK_ASYNC,
6802 dprintk("%s: begin!\n", __func__);
6803 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
6804 fl->fl_u.nfs4_fl.owner,
6805 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
6809 data->arg.block = 1;
6810 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
6811 recovery_type > NFS_LOCK_NEW);
6812 msg.rpc_argp = &data->arg;
6813 msg.rpc_resp = &data->res;
6814 task_setup_data.callback_data = data;
6815 if (recovery_type > NFS_LOCK_NEW) {
6816 if (recovery_type == NFS_LOCK_RECLAIM)
6817 data->arg.reclaim = NFS_LOCK_RECLAIM;
6819 data->arg.new_lock = 1;
6820 task = rpc_run_task(&task_setup_data);
6822 return PTR_ERR(task);
6823 ret = rpc_wait_for_completion_task(task);
6825 ret = data->rpc_status;
6827 nfs4_handle_setlk_error(data->server, data->lsp,
6828 data->arg.new_lock_owner, ret);
6830 data->cancelled = true;
6832 dprintk("%s: done, ret = %d!\n", __func__, ret);
6833 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
6837 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6839 struct nfs_server *server = NFS_SERVER(state->inode);
6840 struct nfs4_exception exception = {
6841 .inode = state->inode,
6846 /* Cache the lock if possible... */
6847 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6849 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
6850 if (err != -NFS4ERR_DELAY)
6852 nfs4_handle_exception(server, err, &exception);
6853 } while (exception.retry);
6857 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6859 struct nfs_server *server = NFS_SERVER(state->inode);
6860 struct nfs4_exception exception = {
6861 .inode = state->inode,
6865 err = nfs4_set_lock_state(state, request);
6868 if (!recover_lost_locks) {
6869 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6873 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6875 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
6879 case -NFS4ERR_GRACE:
6880 case -NFS4ERR_DELAY:
6881 nfs4_handle_exception(server, err, &exception);
6884 } while (exception.retry);
6889 #if defined(CONFIG_NFS_V4_1)
6890 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6892 struct nfs4_lock_state *lsp;
6895 status = nfs4_set_lock_state(state, request);
6898 lsp = request->fl_u.nfs4_fl.owner;
6899 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6900 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6902 return nfs4_lock_expired(state, request);
6906 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6908 struct nfs_inode *nfsi = NFS_I(state->inode);
6909 struct nfs4_state_owner *sp = state->owner;
6910 unsigned char fl_flags = request->fl_flags;
6913 request->fl_flags |= FL_ACCESS;
6914 status = locks_lock_inode_wait(state->inode, request);
6917 mutex_lock(&sp->so_delegreturn_mutex);
6918 down_read(&nfsi->rwsem);
6919 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
6920 /* Yes: cache locks! */
6921 /* ...but avoid races with delegation recall... */
6922 request->fl_flags = fl_flags & ~FL_SLEEP;
6923 status = locks_lock_inode_wait(state->inode, request);
6924 up_read(&nfsi->rwsem);
6925 mutex_unlock(&sp->so_delegreturn_mutex);
6928 up_read(&nfsi->rwsem);
6929 mutex_unlock(&sp->so_delegreturn_mutex);
6930 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
6932 request->fl_flags = fl_flags;
6936 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6938 struct nfs4_exception exception = {
6940 .inode = state->inode,
6941 .interruptible = true,
6946 err = _nfs4_proc_setlk(state, cmd, request);
6947 if (err == -NFS4ERR_DENIED)
6949 err = nfs4_handle_exception(NFS_SERVER(state->inode),
6951 } while (exception.retry);
6955 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6956 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6959 nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6960 struct file_lock *request)
6962 int status = -ERESTARTSYS;
6963 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6965 while(!signalled()) {
6966 status = nfs4_proc_setlk(state, cmd, request);
6967 if ((status != -EAGAIN) || IS_SETLK(cmd))
6969 freezable_schedule_timeout_interruptible(timeout);
6971 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6972 status = -ERESTARTSYS;
6977 #ifdef CONFIG_NFS_V4_1
6978 struct nfs4_lock_waiter {
6979 struct task_struct *task;
6980 struct inode *inode;
6981 struct nfs_lowner *owner;
6985 nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
6988 struct nfs4_lock_waiter *waiter = wait->private;
6990 /* NULL key means to wake up everyone */
6992 struct cb_notify_lock_args *cbnl = key;
6993 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6994 *wowner = waiter->owner;
6996 /* Only wake if the callback was for the same owner. */
6997 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
7000 /* Make sure it's for the right inode */
7001 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
7005 /* override "private" so we can use default_wake_function */
7006 wait->private = waiter->task;
7007 ret = woken_wake_function(wait, mode, flags, key);
7009 list_del_init(&wait->entry);
7010 wait->private = waiter;
7015 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7017 int status = -ERESTARTSYS;
7018 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
7019 struct nfs_server *server = NFS_SERVER(state->inode);
7020 struct nfs_client *clp = server->nfs_client;
7021 wait_queue_head_t *q = &clp->cl_lock_waitq;
7022 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
7023 .id = lsp->ls_seqid.owner_id,
7024 .s_dev = server->s_dev };
7025 struct nfs4_lock_waiter waiter = { .task = current,
7026 .inode = state->inode,
7028 wait_queue_entry_t wait;
7030 /* Don't bother with waitqueue if we don't expect a callback */
7031 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
7032 return nfs4_retry_setlk_simple(state, cmd, request);
7035 wait.private = &waiter;
7036 wait.func = nfs4_wake_lock_waiter;
7038 while(!signalled()) {
7039 add_wait_queue(q, &wait);
7040 status = nfs4_proc_setlk(state, cmd, request);
7041 if ((status != -EAGAIN) || IS_SETLK(cmd)) {
7042 finish_wait(q, &wait);
7046 status = -ERESTARTSYS;
7047 freezer_do_not_count();
7048 wait_woken(&wait, TASK_INTERRUPTIBLE, NFS4_LOCK_MAXTIMEOUT);
7050 finish_wait(q, &wait);
7055 #else /* !CONFIG_NFS_V4_1 */
7057 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7059 return nfs4_retry_setlk_simple(state, cmd, request);
7064 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
7066 struct nfs_open_context *ctx;
7067 struct nfs4_state *state;
7070 /* verify open state */
7071 ctx = nfs_file_open_context(filp);
7074 if (IS_GETLK(cmd)) {
7076 return nfs4_proc_getlk(state, F_GETLK, request);
7080 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
7083 if (request->fl_type == F_UNLCK) {
7085 return nfs4_proc_unlck(state, cmd, request);
7092 if ((request->fl_flags & FL_POSIX) &&
7093 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
7097 * Don't rely on the VFS having checked the file open mode,
7098 * since it won't do this for flock() locks.
7100 switch (request->fl_type) {
7102 if (!(filp->f_mode & FMODE_READ))
7106 if (!(filp->f_mode & FMODE_WRITE))
7110 status = nfs4_set_lock_state(state, request);
7114 return nfs4_retry_setlk(state, cmd, request);
7117 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
7119 struct nfs_server *server = NFS_SERVER(state->inode);
7122 err = nfs4_set_lock_state(state, fl);
7125 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
7126 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
7129 struct nfs_release_lockowner_data {
7130 struct nfs4_lock_state *lsp;
7131 struct nfs_server *server;
7132 struct nfs_release_lockowner_args args;
7133 struct nfs_release_lockowner_res res;
7134 unsigned long timestamp;
7137 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7139 struct nfs_release_lockowner_data *data = calldata;
7140 struct nfs_server *server = data->server;
7141 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7142 &data->res.seq_res, task);
7143 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7144 data->timestamp = jiffies;
7147 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7149 struct nfs_release_lockowner_data *data = calldata;
7150 struct nfs_server *server = data->server;
7152 nfs40_sequence_done(task, &data->res.seq_res);
7154 switch (task->tk_status) {
7156 renew_lease(server, data->timestamp);
7158 case -NFS4ERR_STALE_CLIENTID:
7159 case -NFS4ERR_EXPIRED:
7160 nfs4_schedule_lease_recovery(server->nfs_client);
7162 case -NFS4ERR_LEASE_MOVED:
7163 case -NFS4ERR_DELAY:
7164 if (nfs4_async_handle_error(task, server,
7165 NULL, NULL) == -EAGAIN)
7166 rpc_restart_call_prepare(task);
7170 static void nfs4_release_lockowner_release(void *calldata)
7172 struct nfs_release_lockowner_data *data = calldata;
7173 nfs4_free_lock_state(data->server, data->lsp);
7177 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
7178 .rpc_call_prepare = nfs4_release_lockowner_prepare,
7179 .rpc_call_done = nfs4_release_lockowner_done,
7180 .rpc_release = nfs4_release_lockowner_release,
7184 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
7186 struct nfs_release_lockowner_data *data;
7187 struct rpc_message msg = {
7188 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7191 if (server->nfs_client->cl_mvops->minor_version != 0)
7194 data = kmalloc(sizeof(*data), GFP_NOFS);
7198 data->server = server;
7199 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7200 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7201 data->args.lock_owner.s_dev = server->s_dev;
7203 msg.rpc_argp = &data->args;
7204 msg.rpc_resp = &data->res;
7205 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
7206 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
7209 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7211 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
7212 struct dentry *unused, struct inode *inode,
7213 const char *key, const void *buf,
7214 size_t buflen, int flags)
7216 return nfs4_proc_set_acl(inode, buf, buflen);
7219 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
7220 struct dentry *unused, struct inode *inode,
7221 const char *key, void *buf, size_t buflen)
7223 return nfs4_proc_get_acl(inode, buf, buflen);
7226 static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
7228 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
7231 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
7233 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
7234 struct dentry *unused, struct inode *inode,
7235 const char *key, const void *buf,
7236 size_t buflen, int flags)
7238 if (security_ismaclabel(key))
7239 return nfs4_set_security_label(inode, buf, buflen);
7244 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
7245 struct dentry *unused, struct inode *inode,
7246 const char *key, void *buf, size_t buflen)
7248 if (security_ismaclabel(key))
7249 return nfs4_get_security_label(inode, buf, buflen);
7254 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7258 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7259 len = security_inode_listsecurity(inode, list, list_len);
7260 if (list_len && len > list_len)
7266 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7267 .prefix = XATTR_SECURITY_PREFIX,
7268 .get = nfs4_xattr_get_nfs4_label,
7269 .set = nfs4_xattr_set_nfs4_label,
7275 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7283 * nfs_fhget will use either the mounted_on_fileid or the fileid
7285 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7287 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7288 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7289 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
7290 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
7293 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
7294 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
7295 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7299 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7300 const struct qstr *name,
7301 struct nfs4_fs_locations *fs_locations,
7304 struct nfs_server *server = NFS_SERVER(dir);
7306 struct nfs4_fs_locations_arg args = {
7307 .dir_fh = NFS_FH(dir),
7312 struct nfs4_fs_locations_res res = {
7313 .fs_locations = fs_locations,
7315 struct rpc_message msg = {
7316 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7322 dprintk("%s: start\n", __func__);
7324 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7325 bitmask[1] = nfs4_fattr_bitmap[1];
7327 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7328 * is not supported */
7329 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
7330 bitmask[0] &= ~FATTR4_WORD0_FILEID;
7332 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
7334 nfs_fattr_init(&fs_locations->fattr);
7335 fs_locations->server = server;
7336 fs_locations->nlocations = 0;
7337 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
7338 dprintk("%s: returned status = %d\n", __func__, status);
7342 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7343 const struct qstr *name,
7344 struct nfs4_fs_locations *fs_locations,
7347 struct nfs4_exception exception = {
7348 .interruptible = true,
7352 err = _nfs4_proc_fs_locations(client, dir, name,
7353 fs_locations, page);
7354 trace_nfs4_get_fs_locations(dir, name, err);
7355 err = nfs4_handle_exception(NFS_SERVER(dir), err,
7357 } while (exception.retry);
7362 * This operation also signals the server that this client is
7363 * performing migration recovery. The server can stop returning
7364 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7365 * appended to this compound to identify the client ID which is
7366 * performing recovery.
7368 static int _nfs40_proc_get_locations(struct inode *inode,
7369 struct nfs4_fs_locations *locations,
7370 struct page *page, const struct cred *cred)
7372 struct nfs_server *server = NFS_SERVER(inode);
7373 struct rpc_clnt *clnt = server->client;
7375 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7377 struct nfs4_fs_locations_arg args = {
7378 .clientid = server->nfs_client->cl_clientid,
7379 .fh = NFS_FH(inode),
7382 .migration = 1, /* skip LOOKUP */
7383 .renew = 1, /* append RENEW */
7385 struct nfs4_fs_locations_res res = {
7386 .fs_locations = locations,
7390 struct rpc_message msg = {
7391 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7396 unsigned long now = jiffies;
7399 nfs_fattr_init(&locations->fattr);
7400 locations->server = server;
7401 locations->nlocations = 0;
7403 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7404 status = nfs4_call_sync_sequence(clnt, server, &msg,
7405 &args.seq_args, &res.seq_res);
7409 renew_lease(server, now);
7413 #ifdef CONFIG_NFS_V4_1
7416 * This operation also signals the server that this client is
7417 * performing migration recovery. The server can stop asserting
7418 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7419 * performing this operation is identified in the SEQUENCE
7420 * operation in this compound.
7422 * When the client supports GETATTR(fs_locations_info), it can
7423 * be plumbed in here.
7425 static int _nfs41_proc_get_locations(struct inode *inode,
7426 struct nfs4_fs_locations *locations,
7427 struct page *page, const struct cred *cred)
7429 struct nfs_server *server = NFS_SERVER(inode);
7430 struct rpc_clnt *clnt = server->client;
7432 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7434 struct nfs4_fs_locations_arg args = {
7435 .fh = NFS_FH(inode),
7438 .migration = 1, /* skip LOOKUP */
7440 struct nfs4_fs_locations_res res = {
7441 .fs_locations = locations,
7444 struct rpc_message msg = {
7445 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7452 nfs_fattr_init(&locations->fattr);
7453 locations->server = server;
7454 locations->nlocations = 0;
7456 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7457 status = nfs4_call_sync_sequence(clnt, server, &msg,
7458 &args.seq_args, &res.seq_res);
7459 if (status == NFS4_OK &&
7460 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7461 status = -NFS4ERR_LEASE_MOVED;
7465 #endif /* CONFIG_NFS_V4_1 */
7468 * nfs4_proc_get_locations - discover locations for a migrated FSID
7469 * @inode: inode on FSID that is migrating
7470 * @locations: result of query
7472 * @cred: credential to use for this operation
7474 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7475 * operation failed, or a negative errno if a local error occurred.
7477 * On success, "locations" is filled in, but if the server has
7478 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7481 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7482 * from this client that require migration recovery.
7484 int nfs4_proc_get_locations(struct inode *inode,
7485 struct nfs4_fs_locations *locations,
7486 struct page *page, const struct cred *cred)
7488 struct nfs_server *server = NFS_SERVER(inode);
7489 struct nfs_client *clp = server->nfs_client;
7490 const struct nfs4_mig_recovery_ops *ops =
7491 clp->cl_mvops->mig_recovery_ops;
7492 struct nfs4_exception exception = {
7493 .interruptible = true,
7497 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7498 (unsigned long long)server->fsid.major,
7499 (unsigned long long)server->fsid.minor,
7501 nfs_display_fhandle(NFS_FH(inode), __func__);
7504 status = ops->get_locations(inode, locations, page, cred);
7505 if (status != -NFS4ERR_DELAY)
7507 nfs4_handle_exception(server, status, &exception);
7508 } while (exception.retry);
7513 * This operation also signals the server that this client is
7514 * performing "lease moved" recovery. The server can stop
7515 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7516 * is appended to this compound to identify the client ID which is
7517 * performing recovery.
7519 static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
7521 struct nfs_server *server = NFS_SERVER(inode);
7522 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7523 struct rpc_clnt *clnt = server->client;
7524 struct nfs4_fsid_present_arg args = {
7525 .fh = NFS_FH(inode),
7526 .clientid = clp->cl_clientid,
7527 .renew = 1, /* append RENEW */
7529 struct nfs4_fsid_present_res res = {
7532 struct rpc_message msg = {
7533 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7538 unsigned long now = jiffies;
7541 res.fh = nfs_alloc_fhandle();
7545 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7546 status = nfs4_call_sync_sequence(clnt, server, &msg,
7547 &args.seq_args, &res.seq_res);
7548 nfs_free_fhandle(res.fh);
7552 do_renew_lease(clp, now);
7556 #ifdef CONFIG_NFS_V4_1
7559 * This operation also signals the server that this client is
7560 * performing "lease moved" recovery. The server can stop asserting
7561 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7562 * this operation is identified in the SEQUENCE operation in this
7565 static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
7567 struct nfs_server *server = NFS_SERVER(inode);
7568 struct rpc_clnt *clnt = server->client;
7569 struct nfs4_fsid_present_arg args = {
7570 .fh = NFS_FH(inode),
7572 struct nfs4_fsid_present_res res = {
7574 struct rpc_message msg = {
7575 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7582 res.fh = nfs_alloc_fhandle();
7586 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7587 status = nfs4_call_sync_sequence(clnt, server, &msg,
7588 &args.seq_args, &res.seq_res);
7589 nfs_free_fhandle(res.fh);
7590 if (status == NFS4_OK &&
7591 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7592 status = -NFS4ERR_LEASE_MOVED;
7596 #endif /* CONFIG_NFS_V4_1 */
7599 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7600 * @inode: inode on FSID to check
7601 * @cred: credential to use for this operation
7603 * Server indicates whether the FSID is present, moved, or not
7604 * recognized. This operation is necessary to clear a LEASE_MOVED
7605 * condition for this client ID.
7607 * Returns NFS4_OK if the FSID is present on this server,
7608 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7609 * NFS4ERR code if some error occurred on the server, or a
7610 * negative errno if a local failure occurred.
7612 int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
7614 struct nfs_server *server = NFS_SERVER(inode);
7615 struct nfs_client *clp = server->nfs_client;
7616 const struct nfs4_mig_recovery_ops *ops =
7617 clp->cl_mvops->mig_recovery_ops;
7618 struct nfs4_exception exception = {
7619 .interruptible = true,
7623 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7624 (unsigned long long)server->fsid.major,
7625 (unsigned long long)server->fsid.minor,
7627 nfs_display_fhandle(NFS_FH(inode), __func__);
7630 status = ops->fsid_present(inode, cred);
7631 if (status != -NFS4ERR_DELAY)
7633 nfs4_handle_exception(server, status, &exception);
7634 } while (exception.retry);
7639 * If 'use_integrity' is true and the state managment nfs_client
7640 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7641 * and the machine credential as per RFC3530bis and RFC5661 Security
7642 * Considerations sections. Otherwise, just use the user cred with the
7643 * filesystem's rpc_client.
7645 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
7648 struct nfs4_secinfo_arg args = {
7649 .dir_fh = NFS_FH(dir),
7652 struct nfs4_secinfo_res res = {
7655 struct rpc_message msg = {
7656 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7660 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
7661 const struct cred *cred = NULL;
7663 if (use_integrity) {
7664 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
7665 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7666 msg.rpc_cred = cred;
7669 dprintk("NFS call secinfo %s\n", name->name);
7671 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7672 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7674 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7675 &res.seq_res, RPC_TASK_NO_ROUND_ROBIN);
7676 dprintk("NFS reply secinfo: %d\n", status);
7683 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7684 struct nfs4_secinfo_flavors *flavors)
7686 struct nfs4_exception exception = {
7687 .interruptible = true,
7691 err = -NFS4ERR_WRONGSEC;
7693 /* try to use integrity protection with machine cred */
7694 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7695 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7698 * if unable to use integrity protection, or SECINFO with
7699 * integrity protection returns NFS4ERR_WRONGSEC (which is
7700 * disallowed by spec, but exists in deployed servers) use
7701 * the current filesystem's rpc_client and the user cred.
7703 if (err == -NFS4ERR_WRONGSEC)
7704 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7706 trace_nfs4_secinfo(dir, name, err);
7707 err = nfs4_handle_exception(NFS_SERVER(dir), err,
7709 } while (exception.retry);
7713 #ifdef CONFIG_NFS_V4_1
7715 * Check the exchange flags returned by the server for invalid flags, having
7716 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7719 static int nfs4_check_cl_exchange_flags(u32 flags)
7721 if (flags & ~EXCHGID4_FLAG_MASK_R)
7723 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7724 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7726 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7730 return -NFS4ERR_INVAL;
7734 nfs41_same_server_scope(struct nfs41_server_scope *a,
7735 struct nfs41_server_scope *b)
7737 if (a->server_scope_sz != b->server_scope_sz)
7739 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
7743 nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7747 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7748 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7752 * nfs4_proc_bind_one_conn_to_session()
7754 * The 4.1 client currently uses the same TCP connection for the
7755 * fore and backchannel.
7758 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7759 struct rpc_xprt *xprt,
7760 struct nfs_client *clp,
7761 const struct cred *cred)
7764 struct nfs41_bind_conn_to_session_args args = {
7766 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7768 struct nfs41_bind_conn_to_session_res res;
7769 struct rpc_message msg = {
7771 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
7776 struct rpc_task_setup task_setup_data = {
7779 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
7780 .rpc_message = &msg,
7781 .flags = RPC_TASK_TIMEOUT,
7783 struct rpc_task *task;
7785 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7786 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7787 args.dir = NFS4_CDFC4_FORE;
7789 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7790 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7791 args.dir = NFS4_CDFC4_FORE;
7793 task = rpc_run_task(&task_setup_data);
7794 if (!IS_ERR(task)) {
7795 status = task->tk_status;
7798 status = PTR_ERR(task);
7799 trace_nfs4_bind_conn_to_session(clp, status);
7801 if (memcmp(res.sessionid.data,
7802 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7803 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7806 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
7807 dprintk("NFS: %s: Unexpected direction from server\n",
7811 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
7812 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7821 struct rpc_bind_conn_calldata {
7822 struct nfs_client *clp;
7823 const struct cred *cred;
7827 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7828 struct rpc_xprt *xprt,
7831 struct rpc_bind_conn_calldata *p = calldata;
7833 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7836 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
7838 struct rpc_bind_conn_calldata data = {
7842 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7843 nfs4_proc_bind_conn_to_session_callback, &data);
7847 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7848 * and operations we'd like to see to enable certain features in the allow map
7850 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7851 .how = SP4_MACH_CRED,
7852 .enforce.u.words = {
7853 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7854 1 << (OP_EXCHANGE_ID - 32) |
7855 1 << (OP_CREATE_SESSION - 32) |
7856 1 << (OP_DESTROY_SESSION - 32) |
7857 1 << (OP_DESTROY_CLIENTID - 32)
7860 [0] = 1 << (OP_CLOSE) |
7861 1 << (OP_OPEN_DOWNGRADE) |
7863 1 << (OP_DELEGRETURN) |
7865 [1] = 1 << (OP_SECINFO - 32) |
7866 1 << (OP_SECINFO_NO_NAME - 32) |
7867 1 << (OP_LAYOUTRETURN - 32) |
7868 1 << (OP_TEST_STATEID - 32) |
7869 1 << (OP_FREE_STATEID - 32) |
7870 1 << (OP_WRITE - 32)
7875 * Select the state protection mode for client `clp' given the server results
7876 * from exchange_id in `sp'.
7878 * Returns 0 on success, negative errno otherwise.
7880 static int nfs4_sp4_select_mode(struct nfs_client *clp,
7881 struct nfs41_state_protection *sp)
7883 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7884 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7885 1 << (OP_EXCHANGE_ID - 32) |
7886 1 << (OP_CREATE_SESSION - 32) |
7887 1 << (OP_DESTROY_SESSION - 32) |
7888 1 << (OP_DESTROY_CLIENTID - 32)
7890 unsigned long flags = 0;
7894 if (sp->how == SP4_MACH_CRED) {
7895 /* Print state protect result */
7896 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7897 for (i = 0; i <= LAST_NFS4_OP; i++) {
7898 if (test_bit(i, sp->enforce.u.longs))
7899 dfprintk(MOUNT, " enforce op %d\n", i);
7900 if (test_bit(i, sp->allow.u.longs))
7901 dfprintk(MOUNT, " allow op %d\n", i);
7904 /* make sure nothing is on enforce list that isn't supported */
7905 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7906 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7907 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7914 * Minimal mode - state operations are allowed to use machine
7915 * credential. Note this already happens by default, so the
7916 * client doesn't have to do anything more than the negotiation.
7918 * NOTE: we don't care if EXCHANGE_ID is in the list -
7919 * we're already using the machine cred for exchange_id
7920 * and will never use a different cred.
7922 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7923 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7924 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7925 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7926 dfprintk(MOUNT, "sp4_mach_cred:\n");
7927 dfprintk(MOUNT, " minimal mode enabled\n");
7928 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
7930 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7935 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
7936 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7937 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
7938 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7939 dfprintk(MOUNT, " cleanup mode enabled\n");
7940 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
7943 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7944 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7945 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
7948 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7949 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7950 dfprintk(MOUNT, " secinfo mode enabled\n");
7951 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
7954 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7955 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7956 dfprintk(MOUNT, " stateid mode enabled\n");
7957 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
7960 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7961 dfprintk(MOUNT, " write mode enabled\n");
7962 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
7965 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7966 dfprintk(MOUNT, " commit mode enabled\n");
7967 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
7971 clp->cl_sp4_flags = flags;
7975 struct nfs41_exchange_id_data {
7976 struct nfs41_exchange_id_res res;
7977 struct nfs41_exchange_id_args args;
7980 static void nfs4_exchange_id_release(void *data)
7982 struct nfs41_exchange_id_data *cdata =
7983 (struct nfs41_exchange_id_data *)data;
7985 nfs_put_client(cdata->args.client);
7986 kfree(cdata->res.impl_id);
7987 kfree(cdata->res.server_scope);
7988 kfree(cdata->res.server_owner);
7992 static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7993 .rpc_release = nfs4_exchange_id_release,
7997 * _nfs4_proc_exchange_id()
7999 * Wrapper for EXCHANGE_ID operation.
8001 static struct rpc_task *
8002 nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
8003 u32 sp4_how, struct rpc_xprt *xprt)
8005 struct rpc_message msg = {
8006 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
8009 struct rpc_task_setup task_setup_data = {
8010 .rpc_client = clp->cl_rpcclient,
8011 .callback_ops = &nfs4_exchange_id_call_ops,
8012 .rpc_message = &msg,
8013 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
8015 struct nfs41_exchange_id_data *calldata;
8018 if (!refcount_inc_not_zero(&clp->cl_count))
8019 return ERR_PTR(-EIO);
8022 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8026 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
8028 status = nfs4_init_uniform_client_string(clp);
8032 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
8035 if (unlikely(calldata->res.server_owner == NULL))
8038 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
8040 if (unlikely(calldata->res.server_scope == NULL))
8041 goto out_server_owner;
8043 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
8044 if (unlikely(calldata->res.impl_id == NULL))
8045 goto out_server_scope;
8049 calldata->args.state_protect.how = SP4_NONE;
8053 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
8063 task_setup_data.rpc_xprt = xprt;
8064 task_setup_data.flags |= RPC_TASK_SOFTCONN;
8065 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
8066 sizeof(calldata->args.verifier.data));
8068 calldata->args.client = clp;
8069 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
8070 EXCHGID4_FLAG_BIND_PRINC_STATEID;
8071 #ifdef CONFIG_NFS_V4_1_MIGRATION
8072 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
8074 msg.rpc_argp = &calldata->args;
8075 msg.rpc_resp = &calldata->res;
8076 task_setup_data.callback_data = calldata;
8078 return rpc_run_task(&task_setup_data);
8081 kfree(calldata->res.impl_id);
8083 kfree(calldata->res.server_scope);
8085 kfree(calldata->res.server_owner);
8089 nfs_put_client(clp);
8090 return ERR_PTR(status);
8094 * _nfs4_proc_exchange_id()
8096 * Wrapper for EXCHANGE_ID operation.
8098 static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
8101 struct rpc_task *task;
8102 struct nfs41_exchange_id_args *argp;
8103 struct nfs41_exchange_id_res *resp;
8106 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
8108 return PTR_ERR(task);
8110 argp = task->tk_msg.rpc_argp;
8111 resp = task->tk_msg.rpc_resp;
8112 status = task->tk_status;
8116 status = nfs4_check_cl_exchange_flags(resp->flags);
8120 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8124 clp->cl_clientid = resp->clientid;
8125 clp->cl_exchange_flags = resp->flags;
8126 clp->cl_seqid = resp->seqid;
8127 /* Client ID is not confirmed */
8128 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8129 clear_bit(NFS4_SESSION_ESTABLISHED,
8130 &clp->cl_session->session_state);
8132 if (clp->cl_serverscope != NULL &&
8133 !nfs41_same_server_scope(clp->cl_serverscope,
8134 resp->server_scope)) {
8135 dprintk("%s: server_scope mismatch detected\n",
8137 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8140 swap(clp->cl_serverowner, resp->server_owner);
8141 swap(clp->cl_serverscope, resp->server_scope);
8142 swap(clp->cl_implid, resp->impl_id);
8144 /* Save the EXCHANGE_ID verifier session trunk tests */
8145 memcpy(clp->cl_confirm.data, argp->verifier.data,
8146 sizeof(clp->cl_confirm.data));
8148 trace_nfs4_exchange_id(clp, status);
8154 * nfs4_proc_exchange_id()
8156 * Returns zero, a negative errno, or a negative NFS4ERR status code.
8158 * Since the clientid has expired, all compounds using sessions
8159 * associated with the stale clientid will be returning
8160 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
8161 * be in some phase of session reset.
8163 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
8165 int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
8167 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8170 /* try SP4_MACH_CRED if krb5i/p */
8171 if (authflavor == RPC_AUTH_GSS_KRB5I ||
8172 authflavor == RPC_AUTH_GSS_KRB5P) {
8173 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
8179 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
8183 * nfs4_test_session_trunk
8185 * This is an add_xprt_test() test function called from
8186 * rpc_clnt_setup_test_and_add_xprt.
8188 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8189 * and is dereferrenced in nfs4_exchange_id_release
8191 * Upon success, add the new transport to the rpc_clnt
8193 * @clnt: struct rpc_clnt to get new transport
8194 * @xprt: the rpc_xprt to test
8195 * @data: call data for _nfs4_proc_exchange_id.
8197 void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
8200 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
8201 struct rpc_task *task;
8206 dprintk("--> %s try %s\n", __func__,
8207 xprt->address_strings[RPC_DISPLAY_ADDR]);
8209 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8211 /* Test connection for session trunking. Async exchange_id call */
8212 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8216 status = task->tk_status;
8218 status = nfs4_detect_session_trunking(adata->clp,
8219 task->tk_msg.rpc_resp, xprt);
8222 rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
8226 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
8228 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
8229 const struct cred *cred)
8231 struct rpc_message msg = {
8232 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8238 status = rpc_call_sync(clp->cl_rpcclient, &msg,
8239 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
8240 trace_nfs4_destroy_clientid(clp, status);
8242 dprintk("NFS: Got error %d from the server %s on "
8243 "DESTROY_CLIENTID.", status, clp->cl_hostname);
8247 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
8248 const struct cred *cred)
8253 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8254 ret = _nfs4_proc_destroy_clientid(clp, cred);
8256 case -NFS4ERR_DELAY:
8257 case -NFS4ERR_CLIENTID_BUSY:
8267 int nfs4_destroy_clientid(struct nfs_client *clp)
8269 const struct cred *cred;
8272 if (clp->cl_mvops->minor_version < 1)
8274 if (clp->cl_exchange_flags == 0)
8276 if (clp->cl_preserve_clid)
8278 cred = nfs4_get_clid_cred(clp);
8279 ret = nfs4_proc_destroy_clientid(clp, cred);
8283 case -NFS4ERR_STALE_CLIENTID:
8284 clp->cl_exchange_flags = 0;
8290 #endif /* CONFIG_NFS_V4_1 */
8292 struct nfs4_get_lease_time_data {
8293 struct nfs4_get_lease_time_args *args;
8294 struct nfs4_get_lease_time_res *res;
8295 struct nfs_client *clp;
8298 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
8301 struct nfs4_get_lease_time_data *data =
8302 (struct nfs4_get_lease_time_data *)calldata;
8304 dprintk("--> %s\n", __func__);
8305 /* just setup sequence, do not trigger session recovery
8306 since we're invoked within one */
8307 nfs4_setup_sequence(data->clp,
8308 &data->args->la_seq_args,
8309 &data->res->lr_seq_res,
8311 dprintk("<-- %s\n", __func__);
8315 * Called from nfs4_state_manager thread for session setup, so don't recover
8316 * from sequence operation or clientid errors.
8318 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8320 struct nfs4_get_lease_time_data *data =
8321 (struct nfs4_get_lease_time_data *)calldata;
8323 dprintk("--> %s\n", __func__);
8324 if (!nfs4_sequence_done(task, &data->res->lr_seq_res))
8326 switch (task->tk_status) {
8327 case -NFS4ERR_DELAY:
8328 case -NFS4ERR_GRACE:
8329 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
8330 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8331 task->tk_status = 0;
8333 case -NFS4ERR_RETRY_UNCACHED_REP:
8334 rpc_restart_call_prepare(task);
8337 dprintk("<-- %s\n", __func__);
8340 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
8341 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8342 .rpc_call_done = nfs4_get_lease_time_done,
8345 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8347 struct rpc_task *task;
8348 struct nfs4_get_lease_time_args args;
8349 struct nfs4_get_lease_time_res res = {
8350 .lr_fsinfo = fsinfo,
8352 struct nfs4_get_lease_time_data data = {
8357 struct rpc_message msg = {
8358 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8362 struct rpc_task_setup task_setup = {
8363 .rpc_client = clp->cl_rpcclient,
8364 .rpc_message = &msg,
8365 .callback_ops = &nfs4_get_lease_time_ops,
8366 .callback_data = &data,
8367 .flags = RPC_TASK_TIMEOUT,
8371 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
8372 task = rpc_run_task(&task_setup);
8375 return PTR_ERR(task);
8377 status = task->tk_status;
8382 #ifdef CONFIG_NFS_V4_1
8385 * Initialize the values to be used by the client in CREATE_SESSION
8386 * If nfs4_init_session set the fore channel request and response sizes,
8389 * Set the back channel max_resp_sz_cached to zero to force the client to
8390 * always set csa_cachethis to FALSE because the current implementation
8391 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8393 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8394 struct rpc_clnt *clnt)
8396 unsigned int max_rqst_sz, max_resp_sz;
8397 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
8398 unsigned int max_bc_slots = rpc_num_bc_slots(clnt);
8400 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8401 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8403 /* Fore channel attributes */
8404 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8405 args->fc_attrs.max_resp_sz = max_resp_sz;
8406 args->fc_attrs.max_ops = NFS4_MAX_OPS;
8407 args->fc_attrs.max_reqs = max_session_slots;
8409 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
8410 "max_ops=%u max_reqs=%u\n",
8412 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
8413 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
8415 /* Back channel attributes */
8416 args->bc_attrs.max_rqst_sz = max_bc_payload;
8417 args->bc_attrs.max_resp_sz = max_bc_payload;
8418 args->bc_attrs.max_resp_sz_cached = 0;
8419 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
8420 args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
8421 if (args->bc_attrs.max_reqs > max_bc_slots)
8422 args->bc_attrs.max_reqs = max_bc_slots;
8424 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8425 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8427 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8428 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8429 args->bc_attrs.max_reqs);
8432 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8433 struct nfs41_create_session_res *res)
8435 struct nfs4_channel_attrs *sent = &args->fc_attrs;
8436 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
8438 if (rcvd->max_resp_sz > sent->max_resp_sz)
8441 * Our requested max_ops is the minimum we need; we're not
8442 * prepared to break up compounds into smaller pieces than that.
8443 * So, no point even trying to continue if the server won't
8446 if (rcvd->max_ops < sent->max_ops)
8448 if (rcvd->max_reqs == 0)
8450 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8451 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
8455 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8456 struct nfs41_create_session_res *res)
8458 struct nfs4_channel_attrs *sent = &args->bc_attrs;
8459 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
8461 if (!(res->flags & SESSION4_BACK_CHAN))
8463 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8465 if (rcvd->max_resp_sz < sent->max_resp_sz)
8467 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8469 if (rcvd->max_ops > sent->max_ops)
8471 if (rcvd->max_reqs > sent->max_reqs)
8477 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
8478 struct nfs41_create_session_res *res)
8482 ret = nfs4_verify_fore_channel_attrs(args, res);
8485 return nfs4_verify_back_channel_attrs(args, res);
8488 static void nfs4_update_session(struct nfs4_session *session,
8489 struct nfs41_create_session_res *res)
8491 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
8492 /* Mark client id and session as being confirmed */
8493 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8494 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
8495 session->flags = res->flags;
8496 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
8497 if (res->flags & SESSION4_BACK_CHAN)
8498 memcpy(&session->bc_attrs, &res->bc_attrs,
8499 sizeof(session->bc_attrs));
8502 static int _nfs4_proc_create_session(struct nfs_client *clp,
8503 const struct cred *cred)
8505 struct nfs4_session *session = clp->cl_session;
8506 struct nfs41_create_session_args args = {
8508 .clientid = clp->cl_clientid,
8509 .seqid = clp->cl_seqid,
8510 .cb_program = NFS4_CALLBACK,
8512 struct nfs41_create_session_res res;
8514 struct rpc_message msg = {
8515 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8522 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
8523 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
8525 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
8526 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
8527 trace_nfs4_create_session(clp, status);
8530 case -NFS4ERR_STALE_CLIENTID:
8531 case -NFS4ERR_DELAY:
8540 /* Verify the session's negotiated channel_attrs values */
8541 status = nfs4_verify_channel_attrs(&args, &res);
8542 /* Increment the clientid slot sequence id */
8545 nfs4_update_session(session, &res);
8552 * Issues a CREATE_SESSION operation to the server.
8553 * It is the responsibility of the caller to verify the session is
8554 * expired before calling this routine.
8556 int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
8560 struct nfs4_session *session = clp->cl_session;
8562 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8564 status = _nfs4_proc_create_session(clp, cred);
8568 /* Init or reset the session slot tables */
8569 status = nfs4_setup_session_slot_tables(session);
8570 dprintk("slot table setup returned %d\n", status);
8574 ptr = (unsigned *)&session->sess_id.data[0];
8575 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8576 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
8578 dprintk("<-- %s\n", __func__);
8583 * Issue the over-the-wire RPC DESTROY_SESSION.
8584 * The caller must serialize access to this routine.
8586 int nfs4_proc_destroy_session(struct nfs4_session *session,
8587 const struct cred *cred)
8589 struct rpc_message msg = {
8590 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8591 .rpc_argp = session,
8596 dprintk("--> nfs4_proc_destroy_session\n");
8598 /* session is still being setup */
8599 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8602 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
8603 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
8604 trace_nfs4_destroy_session(session->clp, status);
8607 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
8608 "Session has been destroyed regardless...\n", status);
8610 dprintk("<-- nfs4_proc_destroy_session\n");
8615 * Renew the cl_session lease.
8617 struct nfs4_sequence_data {
8618 struct nfs_client *clp;
8619 struct nfs4_sequence_args args;
8620 struct nfs4_sequence_res res;
8623 static void nfs41_sequence_release(void *data)
8625 struct nfs4_sequence_data *calldata = data;
8626 struct nfs_client *clp = calldata->clp;
8628 if (refcount_read(&clp->cl_count) > 1)
8629 nfs4_schedule_state_renewal(clp);
8630 nfs_put_client(clp);
8634 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8636 switch(task->tk_status) {
8637 case -NFS4ERR_DELAY:
8638 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8641 nfs4_schedule_lease_recovery(clp);
8646 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
8648 struct nfs4_sequence_data *calldata = data;
8649 struct nfs_client *clp = calldata->clp;
8651 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8654 trace_nfs4_sequence(clp, task->tk_status);
8655 if (task->tk_status < 0) {
8656 dprintk("%s ERROR %d\n", __func__, task->tk_status);
8657 if (refcount_read(&clp->cl_count) == 1)
8660 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8661 rpc_restart_call_prepare(task);
8665 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
8667 dprintk("<-- %s\n", __func__);
8670 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8672 struct nfs4_sequence_data *calldata = data;
8673 struct nfs_client *clp = calldata->clp;
8674 struct nfs4_sequence_args *args;
8675 struct nfs4_sequence_res *res;
8677 args = task->tk_msg.rpc_argp;
8678 res = task->tk_msg.rpc_resp;
8680 nfs4_setup_sequence(clp, args, res, task);
8683 static const struct rpc_call_ops nfs41_sequence_ops = {
8684 .rpc_call_done = nfs41_sequence_call_done,
8685 .rpc_call_prepare = nfs41_sequence_prepare,
8686 .rpc_release = nfs41_sequence_release,
8689 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8690 const struct cred *cred,
8691 struct nfs4_slot *slot,
8694 struct nfs4_sequence_data *calldata;
8695 struct rpc_message msg = {
8696 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8699 struct rpc_task_setup task_setup_data = {
8700 .rpc_client = clp->cl_rpcclient,
8701 .rpc_message = &msg,
8702 .callback_ops = &nfs41_sequence_ops,
8703 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
8705 struct rpc_task *ret;
8707 ret = ERR_PTR(-EIO);
8708 if (!refcount_inc_not_zero(&clp->cl_count))
8711 ret = ERR_PTR(-ENOMEM);
8712 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8713 if (calldata == NULL)
8715 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
8716 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
8717 msg.rpc_argp = &calldata->args;
8718 msg.rpc_resp = &calldata->res;
8719 calldata->clp = clp;
8720 task_setup_data.callback_data = calldata;
8722 ret = rpc_run_task(&task_setup_data);
8727 nfs_put_client(clp);
8729 nfs41_release_slot(slot);
8733 static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
8735 struct rpc_task *task;
8738 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
8740 task = _nfs41_proc_sequence(clp, cred, NULL, false);
8742 ret = PTR_ERR(task);
8744 rpc_put_task_async(task);
8745 dprintk("<-- %s status=%d\n", __func__, ret);
8749 static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
8751 struct rpc_task *task;
8754 task = _nfs41_proc_sequence(clp, cred, NULL, true);
8756 ret = PTR_ERR(task);
8759 ret = rpc_wait_for_completion_task(task);
8761 ret = task->tk_status;
8764 dprintk("<-- %s status=%d\n", __func__, ret);
8768 struct nfs4_reclaim_complete_data {
8769 struct nfs_client *clp;
8770 struct nfs41_reclaim_complete_args arg;
8771 struct nfs41_reclaim_complete_res res;
8774 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8776 struct nfs4_reclaim_complete_data *calldata = data;
8778 nfs4_setup_sequence(calldata->clp,
8779 &calldata->arg.seq_args,
8780 &calldata->res.seq_res,
8784 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8786 switch(task->tk_status) {
8788 wake_up_all(&clp->cl_lock_waitq);
8790 case -NFS4ERR_COMPLETE_ALREADY:
8791 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8793 case -NFS4ERR_DELAY:
8794 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8796 case -NFS4ERR_RETRY_UNCACHED_REP:
8798 case -NFS4ERR_BADSESSION:
8799 case -NFS4ERR_DEADSESSION:
8800 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8801 nfs4_schedule_session_recovery(clp->cl_session,
8805 nfs4_schedule_lease_recovery(clp);
8810 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8812 struct nfs4_reclaim_complete_data *calldata = data;
8813 struct nfs_client *clp = calldata->clp;
8814 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8816 dprintk("--> %s\n", __func__);
8817 if (!nfs41_sequence_done(task, res))
8820 trace_nfs4_reclaim_complete(clp, task->tk_status);
8821 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8822 rpc_restart_call_prepare(task);
8825 dprintk("<-- %s\n", __func__);
8828 static void nfs4_free_reclaim_complete_data(void *data)
8830 struct nfs4_reclaim_complete_data *calldata = data;
8835 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8836 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8837 .rpc_call_done = nfs4_reclaim_complete_done,
8838 .rpc_release = nfs4_free_reclaim_complete_data,
8842 * Issue a global reclaim complete.
8844 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8845 const struct cred *cred)
8847 struct nfs4_reclaim_complete_data *calldata;
8848 struct rpc_task *task;
8849 struct rpc_message msg = {
8850 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
8853 struct rpc_task_setup task_setup_data = {
8854 .rpc_client = clp->cl_rpcclient,
8855 .rpc_message = &msg,
8856 .callback_ops = &nfs4_reclaim_complete_call_ops,
8857 .flags = RPC_TASK_ASYNC | RPC_TASK_NO_ROUND_ROBIN,
8859 int status = -ENOMEM;
8861 dprintk("--> %s\n", __func__);
8862 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8863 if (calldata == NULL)
8865 calldata->clp = clp;
8866 calldata->arg.one_fs = 0;
8868 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
8869 msg.rpc_argp = &calldata->arg;
8870 msg.rpc_resp = &calldata->res;
8871 task_setup_data.callback_data = calldata;
8872 task = rpc_run_task(&task_setup_data);
8874 status = PTR_ERR(task);
8877 status = rpc_wait_for_completion_task(task);
8879 status = task->tk_status;
8882 dprintk("<-- %s status=%d\n", __func__, status);
8887 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8889 struct nfs4_layoutget *lgp = calldata;
8890 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
8892 dprintk("--> %s\n", __func__);
8893 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
8894 &lgp->res.seq_res, task);
8895 dprintk("<-- %s\n", __func__);
8898 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8900 struct nfs4_layoutget *lgp = calldata;
8902 dprintk("--> %s\n", __func__);
8903 nfs41_sequence_process(task, &lgp->res.seq_res);
8904 dprintk("<-- %s\n", __func__);
8908 nfs4_layoutget_handle_exception(struct rpc_task *task,
8909 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8911 struct inode *inode = lgp->args.inode;
8912 struct nfs_server *server = NFS_SERVER(inode);
8913 struct pnfs_layout_hdr *lo;
8914 int nfs4err = task->tk_status;
8915 int err, status = 0;
8918 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
8920 nfs4_sequence_free_slot(&lgp->res.seq_res);
8927 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8928 * on the file. set tk_status to -ENODATA to tell upper layer to
8931 case -NFS4ERR_LAYOUTUNAVAILABLE:
8935 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8936 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8938 case -NFS4ERR_BADLAYOUT:
8939 status = -EOVERFLOW;
8942 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
8943 * (or clients) writing to the same RAID stripe except when
8944 * the minlength argument is 0 (see RFC5661 section 18.43.3).
8946 * Treat it like we would RECALLCONFLICT -- we retry for a little
8947 * while, and then eventually give up.
8949 case -NFS4ERR_LAYOUTTRYLATER:
8950 if (lgp->args.minlength == 0) {
8951 status = -EOVERFLOW;
8956 case -NFS4ERR_RECALLCONFLICT:
8957 status = -ERECALLCONFLICT;
8959 case -NFS4ERR_DELEG_REVOKED:
8960 case -NFS4ERR_ADMIN_REVOKED:
8961 case -NFS4ERR_EXPIRED:
8962 case -NFS4ERR_BAD_STATEID:
8963 exception->timeout = 0;
8964 spin_lock(&inode->i_lock);
8965 lo = NFS_I(inode)->layout;
8966 /* If the open stateid was bad, then recover it. */
8967 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8968 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
8969 spin_unlock(&inode->i_lock);
8970 exception->state = lgp->args.ctx->state;
8971 exception->stateid = &lgp->args.stateid;
8976 * Mark the bad layout state as invalid, then retry
8978 pnfs_mark_layout_stateid_invalid(lo, &head);
8979 spin_unlock(&inode->i_lock);
8980 nfs_commit_inode(inode, 0);
8981 pnfs_free_lseg_list(&head);
8986 err = nfs4_handle_exception(server, nfs4err, exception);
8988 if (exception->retry)
8994 dprintk("<-- %s\n", __func__);
8998 size_t max_response_pages(struct nfs_server *server)
9000 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
9001 return nfs_page_array_len(0, max_resp_sz);
9004 static void nfs4_layoutget_release(void *calldata)
9006 struct nfs4_layoutget *lgp = calldata;
9008 dprintk("--> %s\n", __func__);
9009 nfs4_sequence_free_slot(&lgp->res.seq_res);
9010 pnfs_layoutget_free(lgp);
9011 dprintk("<-- %s\n", __func__);
9014 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
9015 .rpc_call_prepare = nfs4_layoutget_prepare,
9016 .rpc_call_done = nfs4_layoutget_done,
9017 .rpc_release = nfs4_layoutget_release,
9020 struct pnfs_layout_segment *
9021 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
9023 struct inode *inode = lgp->args.inode;
9024 struct nfs_server *server = NFS_SERVER(inode);
9025 struct rpc_task *task;
9026 struct rpc_message msg = {
9027 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
9028 .rpc_argp = &lgp->args,
9029 .rpc_resp = &lgp->res,
9030 .rpc_cred = lgp->cred,
9032 struct rpc_task_setup task_setup_data = {
9033 .rpc_client = server->client,
9034 .rpc_message = &msg,
9035 .callback_ops = &nfs4_layoutget_call_ops,
9036 .callback_data = lgp,
9037 .flags = RPC_TASK_ASYNC,
9039 struct pnfs_layout_segment *lseg = NULL;
9040 struct nfs4_exception exception = {
9042 .timeout = *timeout,
9046 dprintk("--> %s\n", __func__);
9048 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
9049 pnfs_get_layout_hdr(NFS_I(inode)->layout);
9051 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
9053 task = rpc_run_task(&task_setup_data);
9055 return ERR_CAST(task);
9056 status = rpc_wait_for_completion_task(task);
9060 if (task->tk_status < 0) {
9061 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
9062 *timeout = exception.timeout;
9063 } else if (lgp->res.layoutp->len == 0) {
9065 *timeout = nfs4_update_delay(&exception.timeout);
9067 lseg = pnfs_layout_process(lgp);
9069 trace_nfs4_layoutget(lgp->args.ctx,
9076 dprintk("<-- %s status=%d\n", __func__, status);
9078 return ERR_PTR(status);
9083 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
9085 struct nfs4_layoutreturn *lrp = calldata;
9087 dprintk("--> %s\n", __func__);
9088 nfs4_setup_sequence(lrp->clp,
9089 &lrp->args.seq_args,
9092 if (!pnfs_layout_is_valid(lrp->args.layout))
9096 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
9098 struct nfs4_layoutreturn *lrp = calldata;
9099 struct nfs_server *server;
9101 dprintk("--> %s\n", __func__);
9103 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
9106 server = NFS_SERVER(lrp->args.inode);
9107 switch (task->tk_status) {
9108 case -NFS4ERR_OLD_STATEID:
9109 if (nfs4_layoutreturn_refresh_stateid(&lrp->args.stateid,
9115 task->tk_status = 0;
9119 case -NFS4ERR_DELAY:
9120 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
9124 dprintk("<-- %s\n", __func__);
9127 task->tk_status = 0;
9128 nfs4_sequence_free_slot(&lrp->res.seq_res);
9129 rpc_restart_call_prepare(task);
9132 static void nfs4_layoutreturn_release(void *calldata)
9134 struct nfs4_layoutreturn *lrp = calldata;
9135 struct pnfs_layout_hdr *lo = lrp->args.layout;
9137 dprintk("--> %s\n", __func__);
9138 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
9139 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
9140 nfs4_sequence_free_slot(&lrp->res.seq_res);
9141 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9142 lrp->ld_private.ops->free(&lrp->ld_private);
9143 pnfs_put_layout_hdr(lrp->args.layout);
9144 nfs_iput_and_deactive(lrp->inode);
9146 dprintk("<-- %s\n", __func__);
9149 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9150 .rpc_call_prepare = nfs4_layoutreturn_prepare,
9151 .rpc_call_done = nfs4_layoutreturn_done,
9152 .rpc_release = nfs4_layoutreturn_release,
9155 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
9157 struct rpc_task *task;
9158 struct rpc_message msg = {
9159 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9160 .rpc_argp = &lrp->args,
9161 .rpc_resp = &lrp->res,
9162 .rpc_cred = lrp->cred,
9164 struct rpc_task_setup task_setup_data = {
9165 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
9166 .rpc_message = &msg,
9167 .callback_ops = &nfs4_layoutreturn_call_ops,
9168 .callback_data = lrp,
9172 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9173 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9174 &task_setup_data.rpc_client, &msg);
9176 dprintk("--> %s\n", __func__);
9178 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
9180 nfs4_layoutreturn_release(lrp);
9183 task_setup_data.flags |= RPC_TASK_ASYNC;
9185 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1, 0);
9186 task = rpc_run_task(&task_setup_data);
9188 return PTR_ERR(task);
9190 status = task->tk_status;
9191 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
9192 dprintk("<-- %s status=%d\n", __func__, status);
9198 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
9199 struct pnfs_device *pdev,
9200 const struct cred *cred)
9202 struct nfs4_getdeviceinfo_args args = {
9204 .notify_types = NOTIFY_DEVICEID4_CHANGE |
9205 NOTIFY_DEVICEID4_DELETE,
9207 struct nfs4_getdeviceinfo_res res = {
9210 struct rpc_message msg = {
9211 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9218 dprintk("--> %s\n", __func__);
9219 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
9220 if (res.notification & ~args.notify_types)
9221 dprintk("%s: unsupported notification\n", __func__);
9222 if (res.notification != args.notify_types)
9225 dprintk("<-- %s status=%d\n", __func__, status);
9230 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9231 struct pnfs_device *pdev,
9232 const struct cred *cred)
9234 struct nfs4_exception exception = { };
9238 err = nfs4_handle_exception(server,
9239 _nfs4_proc_getdeviceinfo(server, pdev, cred),
9241 } while (exception.retry);
9244 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9246 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9248 struct nfs4_layoutcommit_data *data = calldata;
9249 struct nfs_server *server = NFS_SERVER(data->args.inode);
9251 nfs4_setup_sequence(server->nfs_client,
9252 &data->args.seq_args,
9258 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9260 struct nfs4_layoutcommit_data *data = calldata;
9261 struct nfs_server *server = NFS_SERVER(data->args.inode);
9263 if (!nfs41_sequence_done(task, &data->res.seq_res))
9266 switch (task->tk_status) { /* Just ignore these failures */
9267 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9268 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9269 case -NFS4ERR_BADLAYOUT: /* no layout */
9270 case -NFS4ERR_GRACE: /* loca_recalim always false */
9271 task->tk_status = 0;
9275 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
9276 rpc_restart_call_prepare(task);
9282 static void nfs4_layoutcommit_release(void *calldata)
9284 struct nfs4_layoutcommit_data *data = calldata;
9286 pnfs_cleanup_layoutcommit(data);
9287 nfs_post_op_update_inode_force_wcc(data->args.inode,
9289 put_cred(data->cred);
9290 nfs_iput_and_deactive(data->inode);
9294 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9295 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9296 .rpc_call_done = nfs4_layoutcommit_done,
9297 .rpc_release = nfs4_layoutcommit_release,
9301 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
9303 struct rpc_message msg = {
9304 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9305 .rpc_argp = &data->args,
9306 .rpc_resp = &data->res,
9307 .rpc_cred = data->cred,
9309 struct rpc_task_setup task_setup_data = {
9310 .task = &data->task,
9311 .rpc_client = NFS_CLIENT(data->args.inode),
9312 .rpc_message = &msg,
9313 .callback_ops = &nfs4_layoutcommit_ops,
9314 .callback_data = data,
9316 struct rpc_task *task;
9319 dprintk("NFS: initiating layoutcommit call. sync %d "
9320 "lbw: %llu inode %lu\n", sync,
9321 data->args.lastbytewritten,
9322 data->args.inode->i_ino);
9325 data->inode = nfs_igrab_and_active(data->args.inode);
9326 if (data->inode == NULL) {
9327 nfs4_layoutcommit_release(data);
9330 task_setup_data.flags = RPC_TASK_ASYNC;
9332 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
9333 task = rpc_run_task(&task_setup_data);
9335 return PTR_ERR(task);
9337 status = task->tk_status;
9338 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
9339 dprintk("%s: status %d\n", __func__, status);
9345 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9346 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9349 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9350 struct nfs_fsinfo *info,
9351 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
9353 struct nfs41_secinfo_no_name_args args = {
9354 .style = SECINFO_STYLE_CURRENT_FH,
9356 struct nfs4_secinfo_res res = {
9359 struct rpc_message msg = {
9360 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9364 struct rpc_clnt *clnt = server->client;
9365 const struct cred *cred = NULL;
9368 if (use_integrity) {
9369 clnt = server->nfs_client->cl_rpcclient;
9370 cred = nfs4_get_clid_cred(server->nfs_client);
9371 msg.rpc_cred = cred;
9374 dprintk("--> %s\n", __func__);
9375 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
9376 &res.seq_res, RPC_TASK_NO_ROUND_ROBIN);
9377 dprintk("<-- %s status=%d\n", __func__, status);
9385 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9386 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9388 struct nfs4_exception exception = {
9389 .interruptible = true,
9393 /* first try using integrity protection */
9394 err = -NFS4ERR_WRONGSEC;
9396 /* try to use integrity protection with machine cred */
9397 if (_nfs4_is_integrity_protected(server->nfs_client))
9398 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9402 * if unable to use integrity protection, or SECINFO with
9403 * integrity protection returns NFS4ERR_WRONGSEC (which is
9404 * disallowed by spec, but exists in deployed servers) use
9405 * the current filesystem's rpc_client and the user cred.
9407 if (err == -NFS4ERR_WRONGSEC)
9408 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9413 case -NFS4ERR_WRONGSEC:
9417 err = nfs4_handle_exception(server, err, &exception);
9419 } while (exception.retry);
9425 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9426 struct nfs_fsinfo *info)
9430 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
9431 struct nfs4_secinfo_flavors *flavors;
9432 struct nfs4_secinfo4 *secinfo;
9435 page = alloc_page(GFP_KERNEL);
9441 flavors = page_address(page);
9442 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9445 * Fall back on "guess and check" method if
9446 * the server doesn't support SECINFO_NO_NAME
9448 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
9449 err = nfs4_find_root_sec(server, fhandle, info);
9455 for (i = 0; i < flavors->num_flavors; i++) {
9456 secinfo = &flavors->flavors[i];
9458 switch (secinfo->flavor) {
9462 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9463 &secinfo->flavor_info);
9466 flavor = RPC_AUTH_MAXFLAVOR;
9470 if (!nfs_auth_info_match(&server->auth_info, flavor))
9471 flavor = RPC_AUTH_MAXFLAVOR;
9473 if (flavor != RPC_AUTH_MAXFLAVOR) {
9474 err = nfs4_lookup_root_sec(server, fhandle,
9481 if (flavor == RPC_AUTH_MAXFLAVOR)
9492 static int _nfs41_test_stateid(struct nfs_server *server,
9493 nfs4_stateid *stateid,
9494 const struct cred *cred)
9497 struct nfs41_test_stateid_args args = {
9500 struct nfs41_test_stateid_res res;
9501 struct rpc_message msg = {
9502 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9507 struct rpc_clnt *rpc_client = server->client;
9509 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9512 dprintk("NFS call test_stateid %p\n", stateid);
9513 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
9514 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
9515 &args.seq_args, &res.seq_res);
9516 if (status != NFS_OK) {
9517 dprintk("NFS reply test_stateid: failed, %d\n", status);
9520 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
9524 static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9525 int err, struct nfs4_exception *exception)
9527 exception->retry = 0;
9529 case -NFS4ERR_DELAY:
9530 case -NFS4ERR_RETRY_UNCACHED_REP:
9531 nfs4_handle_exception(server, err, exception);
9533 case -NFS4ERR_BADSESSION:
9534 case -NFS4ERR_BADSLOT:
9535 case -NFS4ERR_BAD_HIGH_SLOT:
9536 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9537 case -NFS4ERR_DEADSESSION:
9538 nfs4_do_handle_exception(server, err, exception);
9543 * nfs41_test_stateid - perform a TEST_STATEID operation
9545 * @server: server / transport on which to perform the operation
9546 * @stateid: state ID to test
9549 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9550 * Otherwise a negative NFS4ERR value is returned if the operation
9551 * failed or the state ID is not currently valid.
9553 static int nfs41_test_stateid(struct nfs_server *server,
9554 nfs4_stateid *stateid,
9555 const struct cred *cred)
9557 struct nfs4_exception exception = {
9558 .interruptible = true,
9562 err = _nfs41_test_stateid(server, stateid, cred);
9563 nfs4_handle_delay_or_session_error(server, err, &exception);
9564 } while (exception.retry);
9568 struct nfs_free_stateid_data {
9569 struct nfs_server *server;
9570 struct nfs41_free_stateid_args args;
9571 struct nfs41_free_stateid_res res;
9574 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9576 struct nfs_free_stateid_data *data = calldata;
9577 nfs4_setup_sequence(data->server->nfs_client,
9578 &data->args.seq_args,
9583 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9585 struct nfs_free_stateid_data *data = calldata;
9587 nfs41_sequence_done(task, &data->res.seq_res);
9589 switch (task->tk_status) {
9590 case -NFS4ERR_DELAY:
9591 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
9592 rpc_restart_call_prepare(task);
9596 static void nfs41_free_stateid_release(void *calldata)
9601 static const struct rpc_call_ops nfs41_free_stateid_ops = {
9602 .rpc_call_prepare = nfs41_free_stateid_prepare,
9603 .rpc_call_done = nfs41_free_stateid_done,
9604 .rpc_release = nfs41_free_stateid_release,
9608 * nfs41_free_stateid - perform a FREE_STATEID operation
9610 * @server: server / transport on which to perform the operation
9611 * @stateid: state ID to release
9613 * @privileged: set to true if this call needs to be privileged
9615 * Note: this function is always asynchronous.
9617 static int nfs41_free_stateid(struct nfs_server *server,
9618 const nfs4_stateid *stateid,
9619 const struct cred *cred,
9622 struct rpc_message msg = {
9623 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
9626 struct rpc_task_setup task_setup = {
9627 .rpc_client = server->client,
9628 .rpc_message = &msg,
9629 .callback_ops = &nfs41_free_stateid_ops,
9630 .flags = RPC_TASK_ASYNC,
9632 struct nfs_free_stateid_data *data;
9633 struct rpc_task *task;
9635 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9636 &task_setup.rpc_client, &msg);
9638 dprintk("NFS call free_stateid %p\n", stateid);
9639 data = kmalloc(sizeof(*data), GFP_NOFS);
9642 data->server = server;
9643 nfs4_stateid_copy(&data->args.stateid, stateid);
9645 task_setup.callback_data = data;
9647 msg.rpc_argp = &data->args;
9648 msg.rpc_resp = &data->res;
9649 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
9650 task = rpc_run_task(&task_setup);
9652 return PTR_ERR(task);
9658 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
9660 const struct cred *cred = lsp->ls_state->owner->so_cred;
9662 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
9663 nfs4_free_lock_state(server, lsp);
9666 static bool nfs41_match_stateid(const nfs4_stateid *s1,
9667 const nfs4_stateid *s2)
9669 if (s1->type != s2->type)
9672 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
9675 if (s1->seqid == s2->seqid)
9678 return s1->seqid == 0 || s2->seqid == 0;
9681 #endif /* CONFIG_NFS_V4_1 */
9683 static bool nfs4_match_stateid(const nfs4_stateid *s1,
9684 const nfs4_stateid *s2)
9686 return nfs4_stateid_match(s1, s2);
9690 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
9691 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9692 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9693 .recover_open = nfs4_open_reclaim,
9694 .recover_lock = nfs4_lock_reclaim,
9695 .establish_clid = nfs4_init_clientid,
9696 .detect_trunking = nfs40_discover_server_trunking,
9699 #if defined(CONFIG_NFS_V4_1)
9700 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
9701 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9702 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9703 .recover_open = nfs4_open_reclaim,
9704 .recover_lock = nfs4_lock_reclaim,
9705 .establish_clid = nfs41_init_clientid,
9706 .reclaim_complete = nfs41_proc_reclaim_complete,
9707 .detect_trunking = nfs41_discover_server_trunking,
9709 #endif /* CONFIG_NFS_V4_1 */
9711 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
9712 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9713 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
9714 .recover_open = nfs40_open_expired,
9715 .recover_lock = nfs4_lock_expired,
9716 .establish_clid = nfs4_init_clientid,
9719 #if defined(CONFIG_NFS_V4_1)
9720 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
9721 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9722 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
9723 .recover_open = nfs41_open_expired,
9724 .recover_lock = nfs41_lock_expired,
9725 .establish_clid = nfs41_init_clientid,
9727 #endif /* CONFIG_NFS_V4_1 */
9729 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
9730 .sched_state_renewal = nfs4_proc_async_renew,
9731 .get_state_renewal_cred = nfs4_get_renew_cred,
9732 .renew_lease = nfs4_proc_renew,
9735 #if defined(CONFIG_NFS_V4_1)
9736 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
9737 .sched_state_renewal = nfs41_proc_async_sequence,
9738 .get_state_renewal_cred = nfs4_get_machine_cred,
9739 .renew_lease = nfs4_proc_sequence,
9743 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
9744 .get_locations = _nfs40_proc_get_locations,
9745 .fsid_present = _nfs40_proc_fsid_present,
9748 #if defined(CONFIG_NFS_V4_1)
9749 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
9750 .get_locations = _nfs41_proc_get_locations,
9751 .fsid_present = _nfs41_proc_fsid_present,
9753 #endif /* CONFIG_NFS_V4_1 */
9755 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9757 .init_caps = NFS_CAP_READDIRPLUS
9758 | NFS_CAP_ATOMIC_OPEN
9759 | NFS_CAP_POSIX_LOCK,
9760 .init_client = nfs40_init_client,
9761 .shutdown_client = nfs40_shutdown_client,
9762 .match_stateid = nfs4_match_stateid,
9763 .find_root_sec = nfs4_find_root_sec,
9764 .free_lock_state = nfs4_release_lockowner,
9765 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
9766 .alloc_seqid = nfs_alloc_seqid,
9767 .call_sync_ops = &nfs40_call_sync_ops,
9768 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9769 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9770 .state_renewal_ops = &nfs40_state_renewal_ops,
9771 .mig_recovery_ops = &nfs40_mig_recovery_ops,
9774 #if defined(CONFIG_NFS_V4_1)
9775 static struct nfs_seqid *
9776 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9781 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9783 .init_caps = NFS_CAP_READDIRPLUS
9784 | NFS_CAP_ATOMIC_OPEN
9785 | NFS_CAP_POSIX_LOCK
9786 | NFS_CAP_STATEID_NFSV41
9787 | NFS_CAP_ATOMIC_OPEN_V1
9789 .init_client = nfs41_init_client,
9790 .shutdown_client = nfs41_shutdown_client,
9791 .match_stateid = nfs41_match_stateid,
9792 .find_root_sec = nfs41_find_root_sec,
9793 .free_lock_state = nfs41_free_lock_state,
9794 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
9795 .alloc_seqid = nfs_alloc_no_seqid,
9796 .session_trunk = nfs4_test_session_trunk,
9797 .call_sync_ops = &nfs41_call_sync_ops,
9798 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9799 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9800 .state_renewal_ops = &nfs41_state_renewal_ops,
9801 .mig_recovery_ops = &nfs41_mig_recovery_ops,
9805 #if defined(CONFIG_NFS_V4_2)
9806 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9808 .init_caps = NFS_CAP_READDIRPLUS
9809 | NFS_CAP_ATOMIC_OPEN
9810 | NFS_CAP_POSIX_LOCK
9811 | NFS_CAP_STATEID_NFSV41
9812 | NFS_CAP_ATOMIC_OPEN_V1
9816 | NFS_CAP_OFFLOAD_CANCEL
9817 | NFS_CAP_DEALLOCATE
9819 | NFS_CAP_LAYOUTSTATS
9821 | NFS_CAP_LAYOUTERROR,
9822 .init_client = nfs41_init_client,
9823 .shutdown_client = nfs41_shutdown_client,
9824 .match_stateid = nfs41_match_stateid,
9825 .find_root_sec = nfs41_find_root_sec,
9826 .free_lock_state = nfs41_free_lock_state,
9827 .call_sync_ops = &nfs41_call_sync_ops,
9828 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
9829 .alloc_seqid = nfs_alloc_no_seqid,
9830 .session_trunk = nfs4_test_session_trunk,
9831 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9832 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9833 .state_renewal_ops = &nfs41_state_renewal_ops,
9834 .mig_recovery_ops = &nfs41_mig_recovery_ops,
9838 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9839 [0] = &nfs_v4_0_minor_ops,
9840 #if defined(CONFIG_NFS_V4_1)
9841 [1] = &nfs_v4_1_minor_ops,
9843 #if defined(CONFIG_NFS_V4_2)
9844 [2] = &nfs_v4_2_minor_ops,
9848 static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
9850 ssize_t error, error2;
9852 error = generic_listxattr(dentry, list, size);
9860 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9863 return error + error2;
9866 static const struct inode_operations nfs4_dir_inode_operations = {
9867 .create = nfs_create,
9868 .lookup = nfs_lookup,
9869 .atomic_open = nfs_atomic_open,
9871 .unlink = nfs_unlink,
9872 .symlink = nfs_symlink,
9876 .rename = nfs_rename,
9877 .permission = nfs_permission,
9878 .getattr = nfs_getattr,
9879 .setattr = nfs_setattr,
9880 .listxattr = nfs4_listxattr,
9883 static const struct inode_operations nfs4_file_inode_operations = {
9884 .permission = nfs_permission,
9885 .getattr = nfs_getattr,
9886 .setattr = nfs_setattr,
9887 .listxattr = nfs4_listxattr,
9890 const struct nfs_rpc_ops nfs_v4_clientops = {
9891 .version = 4, /* protocol version */
9892 .dentry_ops = &nfs4_dentry_operations,
9893 .dir_inode_ops = &nfs4_dir_inode_operations,
9894 .file_inode_ops = &nfs4_file_inode_operations,
9895 .file_ops = &nfs4_file_operations,
9896 .getroot = nfs4_proc_get_root,
9897 .submount = nfs4_submount,
9898 .try_mount = nfs4_try_mount,
9899 .getattr = nfs4_proc_getattr,
9900 .setattr = nfs4_proc_setattr,
9901 .lookup = nfs4_proc_lookup,
9902 .lookupp = nfs4_proc_lookupp,
9903 .access = nfs4_proc_access,
9904 .readlink = nfs4_proc_readlink,
9905 .create = nfs4_proc_create,
9906 .remove = nfs4_proc_remove,
9907 .unlink_setup = nfs4_proc_unlink_setup,
9908 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
9909 .unlink_done = nfs4_proc_unlink_done,
9910 .rename_setup = nfs4_proc_rename_setup,
9911 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
9912 .rename_done = nfs4_proc_rename_done,
9913 .link = nfs4_proc_link,
9914 .symlink = nfs4_proc_symlink,
9915 .mkdir = nfs4_proc_mkdir,
9916 .rmdir = nfs4_proc_rmdir,
9917 .readdir = nfs4_proc_readdir,
9918 .mknod = nfs4_proc_mknod,
9919 .statfs = nfs4_proc_statfs,
9920 .fsinfo = nfs4_proc_fsinfo,
9921 .pathconf = nfs4_proc_pathconf,
9922 .set_capabilities = nfs4_server_capabilities,
9923 .decode_dirent = nfs4_decode_dirent,
9924 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
9925 .read_setup = nfs4_proc_read_setup,
9926 .read_done = nfs4_read_done,
9927 .write_setup = nfs4_proc_write_setup,
9928 .write_done = nfs4_write_done,
9929 .commit_setup = nfs4_proc_commit_setup,
9930 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
9931 .commit_done = nfs4_commit_done,
9932 .lock = nfs4_proc_lock,
9933 .clear_acl_cache = nfs4_zap_acl_attr,
9934 .close_context = nfs4_close_context,
9935 .open_context = nfs4_atomic_open,
9936 .have_delegation = nfs4_have_delegation,
9937 .alloc_client = nfs4_alloc_client,
9938 .init_client = nfs4_init_client,
9939 .free_client = nfs4_free_client,
9940 .create_server = nfs4_create_server,
9941 .clone_server = nfs_clone_server,
9944 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
9945 .name = XATTR_NAME_NFSV4_ACL,
9946 .list = nfs4_xattr_list_nfs4_acl,
9947 .get = nfs4_xattr_get_nfs4_acl,
9948 .set = nfs4_xattr_set_nfs4_acl,
9951 const struct xattr_handler *nfs4_xattr_handlers[] = {
9952 &nfs4_xattr_nfs4_acl_handler,
9953 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
9954 &nfs4_xattr_nfs4_label_handler,