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, struct rpc_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 struct rpc_cred *cred,
103 struct nfs4_slot *slot,
105 static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
107 static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
108 struct rpc_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 struct rpc_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 struct rpc_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 struct rpc_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(struct rpc_clnt *clnt, long *timeout)
409 freezable_schedule_timeout_killable_unsafe(
410 nfs4_update_delay(timeout));
411 if (fatal_signal_pending(current))
416 /* This is the error handling routine for processes that are allowed
419 static int nfs4_do_handle_exception(struct nfs_server *server,
420 int errorcode, struct nfs4_exception *exception)
422 struct nfs_client *clp = server->nfs_client;
423 struct nfs4_state *state = exception->state;
424 const nfs4_stateid *stateid = exception->stateid;
425 struct inode *inode = exception->inode;
428 exception->delay = 0;
429 exception->recovering = 0;
430 exception->retry = 0;
432 if (stateid == NULL && state != NULL)
433 stateid = &state->stateid;
438 case -NFS4ERR_BADHANDLE:
440 if (inode != NULL && S_ISREG(inode->i_mode))
441 pnfs_destroy_layout(NFS_I(inode));
443 case -NFS4ERR_DELEG_REVOKED:
444 case -NFS4ERR_ADMIN_REVOKED:
445 case -NFS4ERR_EXPIRED:
446 case -NFS4ERR_BAD_STATEID:
447 if (inode != NULL && stateid != NULL) {
448 nfs_inode_find_state_and_recover(inode,
450 goto wait_on_recovery;
453 case -NFS4ERR_OPENMODE:
457 err = nfs_async_inode_return_delegation(inode,
460 goto wait_on_recovery;
461 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
462 exception->retry = 1;
468 ret = nfs4_schedule_stateid_recovery(server, state);
471 goto wait_on_recovery;
472 case -NFS4ERR_STALE_STATEID:
473 case -NFS4ERR_STALE_CLIENTID:
474 nfs4_schedule_lease_recovery(clp);
475 goto wait_on_recovery;
477 ret = nfs4_schedule_migration_recovery(server);
480 goto wait_on_recovery;
481 case -NFS4ERR_LEASE_MOVED:
482 nfs4_schedule_lease_moved_recovery(clp);
483 goto wait_on_recovery;
484 #if defined(CONFIG_NFS_V4_1)
485 case -NFS4ERR_BADSESSION:
486 case -NFS4ERR_BADSLOT:
487 case -NFS4ERR_BAD_HIGH_SLOT:
488 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
489 case -NFS4ERR_DEADSESSION:
490 case -NFS4ERR_SEQ_FALSE_RETRY:
491 case -NFS4ERR_SEQ_MISORDERED:
492 dprintk("%s ERROR: %d Reset session\n", __func__,
494 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
495 goto wait_on_recovery;
496 #endif /* defined(CONFIG_NFS_V4_1) */
497 case -NFS4ERR_FILE_OPEN:
498 if (exception->timeout > HZ) {
499 /* We have retried a decent amount, time to
507 nfs_inc_server_stats(server, NFSIOS_DELAY);
510 case -NFS4ERR_LAYOUTTRYLATER:
511 case -NFS4ERR_RECALLCONFLICT:
512 exception->delay = 1;
515 case -NFS4ERR_RETRY_UNCACHED_REP:
516 case -NFS4ERR_OLD_STATEID:
517 exception->retry = 1;
519 case -NFS4ERR_BADOWNER:
520 /* The following works around a Linux server bug! */
521 case -NFS4ERR_BADNAME:
522 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
523 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
524 exception->retry = 1;
525 printk(KERN_WARNING "NFS: v4 server %s "
526 "does not accept raw "
528 "Reenabling the idmapper.\n",
529 server->nfs_client->cl_hostname);
532 /* We failed to handle the error */
533 return nfs4_map_errors(ret);
535 exception->recovering = 1;
539 /* This is the error handling routine for processes that are allowed
542 int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
544 struct nfs_client *clp = server->nfs_client;
547 ret = nfs4_do_handle_exception(server, errorcode, exception);
548 if (exception->delay) {
549 ret = nfs4_delay(server->client, &exception->timeout);
552 if (exception->recovering) {
553 if (exception->task_is_privileged)
555 ret = nfs4_wait_clnt_recover(clp);
556 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
563 exception->retry = 1;
568 nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
569 int errorcode, struct nfs4_exception *exception)
571 struct nfs_client *clp = server->nfs_client;
574 ret = nfs4_do_handle_exception(server, errorcode, exception);
575 if (exception->delay) {
576 rpc_delay(task, nfs4_update_delay(&exception->timeout));
579 if (exception->recovering) {
580 if (exception->task_is_privileged)
582 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
583 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
584 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
587 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
592 exception->retry = 1;
594 * For NFS4ERR_MOVED, the client transport will need to
595 * be recomputed after migration recovery has completed.
597 if (errorcode == -NFS4ERR_MOVED)
598 rpc_task_release_transport(task);
604 nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
605 struct nfs4_state *state, long *timeout)
607 struct nfs4_exception exception = {
611 if (task->tk_status >= 0)
614 exception.timeout = *timeout;
615 task->tk_status = nfs4_async_handle_exception(task, server,
618 if (exception.delay && timeout)
619 *timeout = exception.timeout;
626 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
627 * or 'false' otherwise.
629 static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
631 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
632 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
635 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
637 spin_lock(&clp->cl_lock);
638 if (time_before(clp->cl_last_renewal,timestamp))
639 clp->cl_last_renewal = timestamp;
640 spin_unlock(&clp->cl_lock);
643 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
645 struct nfs_client *clp = server->nfs_client;
647 if (!nfs4_has_session(clp))
648 do_renew_lease(clp, timestamp);
651 struct nfs4_call_sync_data {
652 const struct nfs_server *seq_server;
653 struct nfs4_sequence_args *seq_args;
654 struct nfs4_sequence_res *seq_res;
657 void nfs4_init_sequence(struct nfs4_sequence_args *args,
658 struct nfs4_sequence_res *res, int cache_reply,
661 args->sa_slot = NULL;
662 args->sa_cache_this = cache_reply;
663 args->sa_privileged = privileged;
668 static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
670 struct nfs4_slot *slot = res->sr_slot;
671 struct nfs4_slot_table *tbl;
674 spin_lock(&tbl->slot_tbl_lock);
675 if (!nfs41_wake_and_assign_slot(tbl, slot))
676 nfs4_free_slot(tbl, slot);
677 spin_unlock(&tbl->slot_tbl_lock);
682 static int nfs40_sequence_done(struct rpc_task *task,
683 struct nfs4_sequence_res *res)
685 if (res->sr_slot != NULL)
686 nfs40_sequence_free_slot(res);
690 #if defined(CONFIG_NFS_V4_1)
692 static void nfs41_release_slot(struct nfs4_slot *slot)
694 struct nfs4_session *session;
695 struct nfs4_slot_table *tbl;
696 bool send_new_highest_used_slotid = false;
701 session = tbl->session;
703 /* Bump the slot sequence number */
708 spin_lock(&tbl->slot_tbl_lock);
709 /* Be nice to the server: try to ensure that the last transmitted
710 * value for highest_user_slotid <= target_highest_slotid
712 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
713 send_new_highest_used_slotid = true;
715 if (nfs41_wake_and_assign_slot(tbl, slot)) {
716 send_new_highest_used_slotid = false;
719 nfs4_free_slot(tbl, slot);
721 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
722 send_new_highest_used_slotid = false;
724 spin_unlock(&tbl->slot_tbl_lock);
725 if (send_new_highest_used_slotid)
726 nfs41_notify_server(session->clp);
727 if (waitqueue_active(&tbl->slot_waitq))
728 wake_up_all(&tbl->slot_waitq);
731 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
733 nfs41_release_slot(res->sr_slot);
737 static int nfs41_sequence_process(struct rpc_task *task,
738 struct nfs4_sequence_res *res)
740 struct nfs4_session *session;
741 struct nfs4_slot *slot = res->sr_slot;
742 struct nfs_client *clp;
743 bool interrupted = false;
748 /* don't increment the sequence number if the task wasn't sent */
749 if (!RPC_WAS_SENT(task))
752 session = slot->table->session;
754 if (slot->interrupted) {
755 if (res->sr_status != -NFS4ERR_DELAY)
756 slot->interrupted = 0;
760 trace_nfs4_sequence_done(session, res);
761 /* Check the SEQUENCE operation status */
762 switch (res->sr_status) {
764 /* Update the slot's sequence and clientid lease timer */
767 do_renew_lease(clp, res->sr_timestamp);
768 /* Check sequence flags */
769 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
771 nfs41_update_target_slotid(slot->table, slot, res);
775 * sr_status remains 1 if an RPC level error occurred.
776 * The server may or may not have processed the sequence
778 * Mark the slot as having hosted an interrupted RPC call.
780 slot->interrupted = 1;
783 /* The server detected a resend of the RPC call and
784 * returned NFS4ERR_DELAY as per Section 2.10.6.2
787 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
792 case -NFS4ERR_RETRY_UNCACHED_REP:
793 case -NFS4ERR_SEQ_FALSE_RETRY:
795 * The server thinks we tried to replay a request.
796 * Retry the call after bumping the sequence ID.
799 case -NFS4ERR_BADSLOT:
801 * The slot id we used was probably retired. Try again
802 * using a different slot id.
804 if (slot->slot_nr < slot->table->target_highest_slotid)
805 goto session_recover;
807 case -NFS4ERR_SEQ_MISORDERED:
809 * Was the last operation on this sequence interrupted?
810 * If so, retry after bumping the sequence number.
815 * Could this slot have been previously retired?
816 * If so, then the server may be expecting seq_nr = 1!
818 if (slot->seq_nr != 1) {
822 goto session_recover;
824 /* Just update the slot sequence no. */
828 /* The session may be reset by one of the error handlers. */
829 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
833 nfs4_schedule_session_recovery(session, res->sr_status);
838 if (rpc_restart_call_prepare(task)) {
839 nfs41_sequence_free_slot(res);
845 if (!rpc_restart_call(task))
847 rpc_delay(task, NFS4_POLL_RETRY_MAX);
851 int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
853 if (!nfs41_sequence_process(task, res))
855 if (res->sr_slot != NULL)
856 nfs41_sequence_free_slot(res);
860 EXPORT_SYMBOL_GPL(nfs41_sequence_done);
862 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
864 if (res->sr_slot == NULL)
866 if (res->sr_slot->table->session != NULL)
867 return nfs41_sequence_process(task, res);
868 return nfs40_sequence_done(task, res);
871 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
873 if (res->sr_slot != NULL) {
874 if (res->sr_slot->table->session != NULL)
875 nfs41_sequence_free_slot(res);
877 nfs40_sequence_free_slot(res);
881 int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
883 if (res->sr_slot == NULL)
885 if (!res->sr_slot->table->session)
886 return nfs40_sequence_done(task, res);
887 return nfs41_sequence_done(task, res);
889 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
891 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
893 struct nfs4_call_sync_data *data = calldata;
895 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
897 nfs4_setup_sequence(data->seq_server->nfs_client,
898 data->seq_args, data->seq_res, task);
901 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
903 struct nfs4_call_sync_data *data = calldata;
905 nfs41_sequence_done(task, data->seq_res);
908 static const struct rpc_call_ops nfs41_call_sync_ops = {
909 .rpc_call_prepare = nfs41_call_sync_prepare,
910 .rpc_call_done = nfs41_call_sync_done,
914 nfs4_sequence_process_interrupted(struct nfs_client *client,
915 struct nfs4_slot *slot, struct rpc_cred *cred)
917 struct rpc_task *task;
919 task = _nfs41_proc_sequence(client, cred, slot, true);
921 rpc_put_task_async(task);
924 #else /* !CONFIG_NFS_V4_1 */
926 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
928 return nfs40_sequence_done(task, res);
931 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
933 if (res->sr_slot != NULL)
934 nfs40_sequence_free_slot(res);
937 int nfs4_sequence_done(struct rpc_task *task,
938 struct nfs4_sequence_res *res)
940 return nfs40_sequence_done(task, res);
942 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
945 nfs4_sequence_process_interrupted(struct nfs_client *client,
946 struct nfs4_slot *slot, struct rpc_cred *cred)
949 slot->interrupted = 0;
952 #endif /* !CONFIG_NFS_V4_1 */
954 static void nfs41_sequence_res_init(struct nfs4_sequence_res *res)
956 res->sr_timestamp = jiffies;
957 res->sr_status_flags = 0;
962 void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
963 struct nfs4_sequence_res *res,
964 struct nfs4_slot *slot)
968 slot->privileged = args->sa_privileged ? 1 : 0;
969 args->sa_slot = slot;
974 int nfs4_setup_sequence(struct nfs_client *client,
975 struct nfs4_sequence_args *args,
976 struct nfs4_sequence_res *res,
977 struct rpc_task *task)
979 struct nfs4_session *session = nfs4_get_session(client);
980 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
981 struct nfs4_slot *slot;
983 /* slot already allocated? */
984 if (res->sr_slot != NULL)
988 tbl = &session->fc_slot_table;
989 task->tk_timeout = 0;
993 spin_lock(&tbl->slot_tbl_lock);
994 /* The state manager will wait until the slot table is empty */
995 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
998 slot = nfs4_alloc_slot(tbl);
1000 /* Try again in 1/4 second */
1001 if (slot == ERR_PTR(-ENOMEM))
1002 task->tk_timeout = HZ >> 2;
1005 spin_unlock(&tbl->slot_tbl_lock);
1007 if (likely(!slot->interrupted))
1009 nfs4_sequence_process_interrupted(client,
1010 slot, task->tk_msg.rpc_cred);
1013 nfs4_sequence_attach_slot(args, res, slot);
1015 trace_nfs4_setup_sequence(session, args);
1017 nfs41_sequence_res_init(res);
1018 rpc_call_start(task);
1022 if (args->sa_privileged)
1023 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
1024 NULL, RPC_PRIORITY_PRIVILEGED);
1026 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
1027 spin_unlock(&tbl->slot_tbl_lock);
1030 EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
1032 static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1034 struct nfs4_call_sync_data *data = calldata;
1035 nfs4_setup_sequence(data->seq_server->nfs_client,
1036 data->seq_args, data->seq_res, task);
1039 static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1041 struct nfs4_call_sync_data *data = calldata;
1042 nfs4_sequence_done(task, data->seq_res);
1045 static const struct rpc_call_ops nfs40_call_sync_ops = {
1046 .rpc_call_prepare = nfs40_call_sync_prepare,
1047 .rpc_call_done = nfs40_call_sync_done,
1050 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
1051 struct nfs_server *server,
1052 struct rpc_message *msg,
1053 struct nfs4_sequence_args *args,
1054 struct nfs4_sequence_res *res)
1057 struct rpc_task *task;
1058 struct nfs_client *clp = server->nfs_client;
1059 struct nfs4_call_sync_data data = {
1060 .seq_server = server,
1064 struct rpc_task_setup task_setup = {
1067 .callback_ops = clp->cl_mvops->call_sync_ops,
1068 .callback_data = &data
1071 task = rpc_run_task(&task_setup);
1073 ret = PTR_ERR(task);
1075 ret = task->tk_status;
1081 int nfs4_call_sync(struct rpc_clnt *clnt,
1082 struct nfs_server *server,
1083 struct rpc_message *msg,
1084 struct nfs4_sequence_args *args,
1085 struct nfs4_sequence_res *res,
1088 nfs4_init_sequence(args, res, cache_reply, 0);
1089 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
1093 nfs4_inc_nlink_locked(struct inode *inode)
1095 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1100 nfs4_dec_nlink_locked(struct inode *inode)
1102 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1107 update_changeattr_locked(struct inode *dir, struct nfs4_change_info *cinfo,
1108 unsigned long timestamp, unsigned long cache_validity)
1110 struct nfs_inode *nfsi = NFS_I(dir);
1112 nfsi->cache_validity |= NFS_INO_INVALID_CTIME
1113 | NFS_INO_INVALID_MTIME
1114 | NFS_INO_INVALID_DATA
1116 if (cinfo->atomic && cinfo->before == inode_peek_iversion_raw(dir)) {
1117 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1118 nfsi->attrtimeo_timestamp = jiffies;
1120 nfs_force_lookup_revalidate(dir);
1121 if (cinfo->before != inode_peek_iversion_raw(dir))
1122 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1123 NFS_INO_INVALID_ACL;
1125 inode_set_iversion_raw(dir, cinfo->after);
1126 nfsi->read_cache_jiffies = timestamp;
1127 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
1128 nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE;
1129 nfs_fscache_invalidate(dir);
1133 update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1134 unsigned long timestamp, unsigned long cache_validity)
1136 spin_lock(&dir->i_lock);
1137 update_changeattr_locked(dir, cinfo, timestamp, cache_validity);
1138 spin_unlock(&dir->i_lock);
1141 struct nfs4_open_createattrs {
1142 struct nfs4_label *label;
1143 struct iattr *sattr;
1144 const __u32 verf[2];
1147 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1148 int err, struct nfs4_exception *exception)
1152 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1154 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1155 exception->retry = 1;
1160 nfs4_map_atomic_open_share(struct nfs_server *server,
1161 fmode_t fmode, int openflags)
1165 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1167 res = NFS4_SHARE_ACCESS_READ;
1170 res = NFS4_SHARE_ACCESS_WRITE;
1172 case FMODE_READ|FMODE_WRITE:
1173 res = NFS4_SHARE_ACCESS_BOTH;
1175 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1177 /* Want no delegation if we're using O_DIRECT */
1178 if (openflags & O_DIRECT)
1179 res |= NFS4_SHARE_WANT_NO_DELEG;
1184 static enum open_claim_type4
1185 nfs4_map_atomic_open_claim(struct nfs_server *server,
1186 enum open_claim_type4 claim)
1188 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1193 case NFS4_OPEN_CLAIM_FH:
1194 return NFS4_OPEN_CLAIM_NULL;
1195 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1196 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1197 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1198 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1202 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
1204 p->o_res.f_attr = &p->f_attr;
1205 p->o_res.f_label = p->f_label;
1206 p->o_res.seqid = p->o_arg.seqid;
1207 p->c_res.seqid = p->c_arg.seqid;
1208 p->o_res.server = p->o_arg.server;
1209 p->o_res.access_request = p->o_arg.access;
1210 nfs_fattr_init(&p->f_attr);
1211 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
1214 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
1215 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
1216 const struct nfs4_open_createattrs *c,
1217 enum open_claim_type4 claim,
1220 struct dentry *parent = dget_parent(dentry);
1221 struct inode *dir = d_inode(parent);
1222 struct nfs_server *server = NFS_SERVER(dir);
1223 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
1224 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
1225 struct nfs4_opendata *p;
1227 p = kzalloc(sizeof(*p), gfp_mask);
1231 p->f_label = nfs4_label_alloc(server, gfp_mask);
1232 if (IS_ERR(p->f_label))
1235 p->a_label = nfs4_label_alloc(server, gfp_mask);
1236 if (IS_ERR(p->a_label))
1239 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1240 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
1241 if (IS_ERR(p->o_arg.seqid))
1242 goto err_free_label;
1243 nfs_sb_active(dentry->d_sb);
1244 p->dentry = dget(dentry);
1247 atomic_inc(&sp->so_count);
1248 p->o_arg.open_flags = flags;
1249 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
1250 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1251 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1253 if (flags & O_CREAT) {
1254 p->o_arg.umask = current_umask();
1255 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1256 if (c->sattr != NULL && c->sattr->ia_valid != 0) {
1257 p->o_arg.u.attrs = &p->attrs;
1258 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
1260 memcpy(p->o_arg.u.verifier.data, c->verf,
1261 sizeof(p->o_arg.u.verifier.data));
1264 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1265 * will return permission denied for all bits until close */
1266 if (!(flags & O_EXCL)) {
1267 /* ask server to check for all possible rights as results
1269 switch (p->o_arg.claim) {
1272 case NFS4_OPEN_CLAIM_NULL:
1273 case NFS4_OPEN_CLAIM_FH:
1274 p->o_arg.access = NFS4_ACCESS_READ |
1275 NFS4_ACCESS_MODIFY |
1276 NFS4_ACCESS_EXTEND |
1277 NFS4_ACCESS_EXECUTE;
1280 p->o_arg.clientid = server->nfs_client->cl_clientid;
1281 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1282 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1283 p->o_arg.name = &dentry->d_name;
1284 p->o_arg.server = server;
1285 p->o_arg.bitmask = nfs4_bitmask(server, label);
1286 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1287 switch (p->o_arg.claim) {
1288 case NFS4_OPEN_CLAIM_NULL:
1289 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1290 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1291 p->o_arg.fh = NFS_FH(dir);
1293 case NFS4_OPEN_CLAIM_PREVIOUS:
1294 case NFS4_OPEN_CLAIM_FH:
1295 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1296 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1297 p->o_arg.fh = NFS_FH(d_inode(dentry));
1299 p->c_arg.fh = &p->o_res.fh;
1300 p->c_arg.stateid = &p->o_res.stateid;
1301 p->c_arg.seqid = p->o_arg.seqid;
1302 nfs4_init_opendata_res(p);
1303 kref_init(&p->kref);
1307 nfs4_label_free(p->a_label);
1309 nfs4_label_free(p->f_label);
1317 static void nfs4_opendata_free(struct kref *kref)
1319 struct nfs4_opendata *p = container_of(kref,
1320 struct nfs4_opendata, kref);
1321 struct super_block *sb = p->dentry->d_sb;
1323 nfs4_lgopen_release(p->lgp);
1324 nfs_free_seqid(p->o_arg.seqid);
1325 nfs4_sequence_free_slot(&p->o_res.seq_res);
1326 if (p->state != NULL)
1327 nfs4_put_open_state(p->state);
1328 nfs4_put_state_owner(p->owner);
1330 nfs4_label_free(p->a_label);
1331 nfs4_label_free(p->f_label);
1335 nfs_sb_deactive(sb);
1336 nfs_fattr_free_names(&p->f_attr);
1337 kfree(p->f_attr.mdsthreshold);
1341 static void nfs4_opendata_put(struct nfs4_opendata *p)
1344 kref_put(&p->kref, nfs4_opendata_free);
1347 static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1350 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1351 case FMODE_READ|FMODE_WRITE:
1352 return state->n_rdwr != 0;
1354 return state->n_wronly != 0;
1356 return state->n_rdonly != 0;
1362 static int can_open_cached(struct nfs4_state *state, fmode_t mode,
1363 int open_mode, enum open_claim_type4 claim)
1367 if (open_mode & (O_EXCL|O_TRUNC))
1370 case NFS4_OPEN_CLAIM_NULL:
1371 case NFS4_OPEN_CLAIM_FH:
1376 switch (mode & (FMODE_READ|FMODE_WRITE)) {
1378 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1379 && state->n_rdonly != 0;
1382 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1383 && state->n_wronly != 0;
1385 case FMODE_READ|FMODE_WRITE:
1386 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1387 && state->n_rdwr != 0;
1393 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1394 enum open_claim_type4 claim)
1396 if (delegation == NULL)
1398 if ((delegation->type & fmode) != fmode)
1401 case NFS4_OPEN_CLAIM_NULL:
1402 case NFS4_OPEN_CLAIM_FH:
1404 case NFS4_OPEN_CLAIM_PREVIOUS:
1405 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1411 nfs_mark_delegation_referenced(delegation);
1415 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1424 case FMODE_READ|FMODE_WRITE:
1427 nfs4_state_set_mode_locked(state, state->state | fmode);
1430 #ifdef CONFIG_NFS_V4_1
1431 static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1433 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1435 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1437 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1441 #endif /* CONFIG_NFS_V4_1 */
1443 static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1445 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1446 wake_up_all(&state->waitq);
1449 static void nfs_state_log_out_of_order_open_stateid(struct nfs4_state *state,
1450 const nfs4_stateid *stateid)
1452 u32 state_seqid = be32_to_cpu(state->open_stateid.seqid);
1453 u32 stateid_seqid = be32_to_cpu(stateid->seqid);
1455 if (stateid_seqid == state_seqid + 1U ||
1456 (stateid_seqid == 1U && state_seqid == 0xffffffffU))
1457 nfs_state_log_update_open_stateid(state);
1459 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1462 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
1464 struct nfs_client *clp = state->owner->so_server->nfs_client;
1465 bool need_recover = false;
1467 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1468 need_recover = true;
1469 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1470 need_recover = true;
1471 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1472 need_recover = true;
1474 nfs4_state_mark_reclaim_nograce(clp, state);
1478 * Check for whether or not the caller may update the open stateid
1479 * to the value passed in by stateid.
1481 * Note: This function relies heavily on the server implementing
1482 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1484 * i.e. The stateid seqids have to be initialised to 1, and
1485 * are then incremented on every state transition.
1487 static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1488 const nfs4_stateid *stateid)
1490 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0 ||
1491 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1492 if (stateid->seqid == cpu_to_be32(1))
1493 nfs_state_log_update_open_stateid(state);
1495 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1499 if (nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1500 nfs_state_log_out_of_order_open_stateid(state, stateid);
1506 static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1508 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1510 if (state->n_wronly)
1511 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1512 if (state->n_rdonly)
1513 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1515 set_bit(NFS_O_RDWR_STATE, &state->flags);
1516 set_bit(NFS_OPEN_STATE, &state->flags);
1519 static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1520 nfs4_stateid *stateid, fmode_t fmode)
1522 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1523 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1525 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1528 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1531 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1532 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1533 clear_bit(NFS_OPEN_STATE, &state->flags);
1535 if (stateid == NULL)
1537 /* Handle OPEN+OPEN_DOWNGRADE races */
1538 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1539 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1540 nfs_resync_open_stateid_locked(state);
1543 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1544 nfs4_stateid_copy(&state->stateid, stateid);
1545 nfs4_stateid_copy(&state->open_stateid, stateid);
1546 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
1548 nfs_state_log_update_open_stateid(state);
1551 static void nfs_clear_open_stateid(struct nfs4_state *state,
1552 nfs4_stateid *arg_stateid,
1553 nfs4_stateid *stateid, fmode_t fmode)
1555 write_seqlock(&state->seqlock);
1556 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1557 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1558 nfs_clear_open_stateid_locked(state, stateid, fmode);
1559 write_sequnlock(&state->seqlock);
1560 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1561 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1564 static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1565 const nfs4_stateid *stateid, nfs4_stateid *freeme)
1571 if (!nfs_need_update_open_stateid(state, stateid))
1573 if (!test_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1577 /* Rely on seqids for serialisation with NFSv4.0 */
1578 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1581 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1583 * Ensure we process the state changes in the same order
1584 * in which the server processed them by delaying the
1585 * update of the stateid until we are in sequence.
1587 write_sequnlock(&state->seqlock);
1588 spin_unlock(&state->owner->so_lock);
1590 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
1591 if (!signal_pending(current)) {
1592 if (schedule_timeout(5*HZ) == 0)
1598 finish_wait(&state->waitq, &wait);
1600 spin_lock(&state->owner->so_lock);
1601 write_seqlock(&state->seqlock);
1604 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1605 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1606 nfs4_stateid_copy(freeme, &state->open_stateid);
1607 nfs_test_and_clear_all_open_stateid(state);
1610 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1611 nfs4_stateid_copy(&state->stateid, stateid);
1612 nfs4_stateid_copy(&state->open_stateid, stateid);
1613 trace_nfs4_open_stateid_update(state->inode, stateid, status);
1614 nfs_state_log_update_open_stateid(state);
1617 static void nfs_state_set_open_stateid(struct nfs4_state *state,
1618 const nfs4_stateid *open_stateid,
1620 nfs4_stateid *freeme)
1623 * Protect the call to nfs4_state_set_mode_locked and
1624 * serialise the stateid update
1626 write_seqlock(&state->seqlock);
1627 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1630 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1633 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1635 case FMODE_READ|FMODE_WRITE:
1636 set_bit(NFS_O_RDWR_STATE, &state->flags);
1638 set_bit(NFS_OPEN_STATE, &state->flags);
1639 write_sequnlock(&state->seqlock);
1642 static void nfs_state_set_delegation(struct nfs4_state *state,
1643 const nfs4_stateid *deleg_stateid,
1647 * Protect the call to nfs4_state_set_mode_locked and
1648 * serialise the stateid update
1650 write_seqlock(&state->seqlock);
1651 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1652 set_bit(NFS_DELEGATED_STATE, &state->flags);
1653 write_sequnlock(&state->seqlock);
1656 static void nfs_state_clear_delegation(struct nfs4_state *state)
1658 write_seqlock(&state->seqlock);
1659 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1660 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1661 write_sequnlock(&state->seqlock);
1664 static int update_open_stateid(struct nfs4_state *state,
1665 const nfs4_stateid *open_stateid,
1666 const nfs4_stateid *delegation,
1669 struct nfs_server *server = NFS_SERVER(state->inode);
1670 struct nfs_client *clp = server->nfs_client;
1671 struct nfs_inode *nfsi = NFS_I(state->inode);
1672 struct nfs_delegation *deleg_cur;
1673 nfs4_stateid freeme = { };
1676 fmode &= (FMODE_READ|FMODE_WRITE);
1679 spin_lock(&state->owner->so_lock);
1680 if (open_stateid != NULL) {
1681 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1685 deleg_cur = rcu_dereference(nfsi->delegation);
1686 if (deleg_cur == NULL)
1689 spin_lock(&deleg_cur->lock);
1690 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1691 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1692 (deleg_cur->type & fmode) != fmode)
1693 goto no_delegation_unlock;
1695 if (delegation == NULL)
1696 delegation = &deleg_cur->stateid;
1697 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
1698 goto no_delegation_unlock;
1700 nfs_mark_delegation_referenced(deleg_cur);
1701 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
1703 no_delegation_unlock:
1704 spin_unlock(&deleg_cur->lock);
1707 update_open_stateflags(state, fmode);
1708 spin_unlock(&state->owner->so_lock);
1711 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1712 nfs4_schedule_state_manager(clp);
1713 if (freeme.type != 0)
1714 nfs4_test_and_free_stateid(server, &freeme,
1715 state->owner->so_cred);
1720 static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1721 const nfs4_stateid *stateid)
1723 struct nfs4_state *state = lsp->ls_state;
1726 spin_lock(&state->state_lock);
1727 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1729 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1731 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1734 spin_unlock(&state->state_lock);
1738 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1740 struct nfs_delegation *delegation;
1742 fmode &= FMODE_READ|FMODE_WRITE;
1744 delegation = rcu_dereference(NFS_I(inode)->delegation);
1745 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1750 nfs4_inode_return_delegation(inode);
1753 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1755 struct nfs4_state *state = opendata->state;
1756 struct nfs_delegation *delegation;
1757 int open_mode = opendata->o_arg.open_flags;
1758 fmode_t fmode = opendata->o_arg.fmode;
1759 enum open_claim_type4 claim = opendata->o_arg.claim;
1760 nfs4_stateid stateid;
1764 spin_lock(&state->owner->so_lock);
1765 if (can_open_cached(state, fmode, open_mode, claim)) {
1766 update_open_stateflags(state, fmode);
1767 spin_unlock(&state->owner->so_lock);
1768 goto out_return_state;
1770 spin_unlock(&state->owner->so_lock);
1772 delegation = nfs4_get_valid_delegation(state->inode);
1773 if (!can_open_delegated(delegation, fmode, claim)) {
1777 /* Save the delegation */
1778 nfs4_stateid_copy(&stateid, &delegation->stateid);
1780 nfs_release_seqid(opendata->o_arg.seqid);
1781 if (!opendata->is_recover) {
1782 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1788 /* Try to update the stateid using the delegation */
1789 if (update_open_stateid(state, NULL, &stateid, fmode))
1790 goto out_return_state;
1793 return ERR_PTR(ret);
1795 atomic_inc(&state->count);
1800 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1802 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1803 struct nfs_delegation *delegation;
1804 int delegation_flags = 0;
1807 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1809 delegation_flags = delegation->flags;
1811 switch (data->o_arg.claim) {
1814 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1815 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1816 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1817 "returning a delegation for "
1818 "OPEN(CLAIM_DELEGATE_CUR)\n",
1822 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1823 nfs_inode_set_delegation(state->inode,
1824 data->owner->so_cred,
1825 data->o_res.delegation_type,
1826 &data->o_res.delegation,
1827 data->o_res.pagemod_limit);
1829 nfs_inode_reclaim_delegation(state->inode,
1830 data->owner->so_cred,
1831 data->o_res.delegation_type,
1832 &data->o_res.delegation,
1833 data->o_res.pagemod_limit);
1835 if (data->o_res.do_recall)
1836 nfs_async_inode_return_delegation(state->inode,
1837 &data->o_res.delegation);
1841 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1842 * and update the nfs4_state.
1844 static struct nfs4_state *
1845 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1847 struct inode *inode = data->state->inode;
1848 struct nfs4_state *state = data->state;
1851 if (!data->rpc_done) {
1852 if (data->rpc_status)
1853 return ERR_PTR(data->rpc_status);
1854 /* cached opens have already been processed */
1858 ret = nfs_refresh_inode(inode, &data->f_attr);
1860 return ERR_PTR(ret);
1862 if (data->o_res.delegation_type != 0)
1863 nfs4_opendata_check_deleg(data, state);
1865 update_open_stateid(state, &data->o_res.stateid, NULL,
1867 atomic_inc(&state->count);
1872 static struct inode *
1873 nfs4_opendata_get_inode(struct nfs4_opendata *data)
1875 struct inode *inode;
1877 switch (data->o_arg.claim) {
1878 case NFS4_OPEN_CLAIM_NULL:
1879 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1880 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1881 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1882 return ERR_PTR(-EAGAIN);
1883 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1884 &data->f_attr, data->f_label);
1887 inode = d_inode(data->dentry);
1889 nfs_refresh_inode(inode, &data->f_attr);
1894 static struct nfs4_state *
1895 nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
1897 struct nfs4_state *state;
1898 struct inode *inode;
1900 inode = nfs4_opendata_get_inode(data);
1902 return ERR_CAST(inode);
1903 if (data->state != NULL && data->state->inode == inode) {
1904 state = data->state;
1905 atomic_inc(&state->count);
1907 state = nfs4_get_open_state(inode, data->owner);
1910 state = ERR_PTR(-ENOMEM);
1914 static struct nfs4_state *
1915 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1917 struct nfs4_state *state;
1919 if (!data->rpc_done) {
1920 state = nfs4_try_open_cached(data);
1921 trace_nfs4_cached_open(data->state);
1925 state = nfs4_opendata_find_nfs4_state(data);
1929 if (data->o_res.delegation_type != 0)
1930 nfs4_opendata_check_deleg(data, state);
1931 update_open_stateid(state, &data->o_res.stateid, NULL,
1934 nfs_release_seqid(data->o_arg.seqid);
1938 static struct nfs4_state *
1939 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1941 struct nfs4_state *ret;
1943 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1944 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1946 ret = _nfs4_opendata_to_nfs4_state(data);
1947 nfs4_sequence_free_slot(&data->o_res.seq_res);
1951 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1953 struct nfs_inode *nfsi = NFS_I(state->inode);
1954 struct nfs_open_context *ctx;
1956 spin_lock(&state->inode->i_lock);
1957 list_for_each_entry(ctx, &nfsi->open_files, list) {
1958 if (ctx->state != state)
1960 get_nfs_open_context(ctx);
1961 spin_unlock(&state->inode->i_lock);
1964 spin_unlock(&state->inode->i_lock);
1965 return ERR_PTR(-ENOENT);
1968 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1969 struct nfs4_state *state, enum open_claim_type4 claim)
1971 struct nfs4_opendata *opendata;
1973 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
1974 NULL, claim, GFP_NOFS);
1975 if (opendata == NULL)
1976 return ERR_PTR(-ENOMEM);
1977 opendata->state = state;
1978 atomic_inc(&state->count);
1982 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1985 struct nfs4_state *newstate;
1988 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
1990 opendata->o_arg.open_flags = 0;
1991 opendata->o_arg.fmode = fmode;
1992 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1993 NFS_SB(opendata->dentry->d_sb),
1995 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1996 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1997 nfs4_init_opendata_res(opendata);
1998 ret = _nfs4_recover_proc_open(opendata);
2001 newstate = nfs4_opendata_to_nfs4_state(opendata);
2002 if (IS_ERR(newstate))
2003 return PTR_ERR(newstate);
2004 if (newstate != opendata->state)
2006 nfs4_close_state(newstate, fmode);
2010 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
2014 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
2015 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2016 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2017 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2018 /* memory barrier prior to reading state->n_* */
2019 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2020 clear_bit(NFS_OPEN_STATE, &state->flags);
2022 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2025 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2028 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2032 * We may have performed cached opens for all three recoveries.
2033 * Check if we need to update the current stateid.
2035 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
2036 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
2037 write_seqlock(&state->seqlock);
2038 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2039 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2040 write_sequnlock(&state->seqlock);
2047 * reclaim state on the server after a reboot.
2049 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2051 struct nfs_delegation *delegation;
2052 struct nfs4_opendata *opendata;
2053 fmode_t delegation_type = 0;
2056 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2057 NFS4_OPEN_CLAIM_PREVIOUS);
2058 if (IS_ERR(opendata))
2059 return PTR_ERR(opendata);
2061 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2062 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
2063 delegation_type = delegation->type;
2065 opendata->o_arg.u.delegation_type = delegation_type;
2066 status = nfs4_open_recover(opendata, state);
2067 nfs4_opendata_put(opendata);
2071 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2073 struct nfs_server *server = NFS_SERVER(state->inode);
2074 struct nfs4_exception exception = { };
2077 err = _nfs4_do_open_reclaim(ctx, state);
2078 trace_nfs4_open_reclaim(ctx, 0, err);
2079 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2081 if (err != -NFS4ERR_DELAY)
2083 nfs4_handle_exception(server, err, &exception);
2084 } while (exception.retry);
2088 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2090 struct nfs_open_context *ctx;
2093 ctx = nfs4_state_find_open_context(state);
2096 ret = nfs4_do_open_reclaim(ctx, state);
2097 put_nfs_open_context(ctx);
2101 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)
2105 printk(KERN_ERR "NFS: %s: unhandled error "
2106 "%d.\n", __func__, err);
2112 case -NFS4ERR_BADSESSION:
2113 case -NFS4ERR_BADSLOT:
2114 case -NFS4ERR_BAD_HIGH_SLOT:
2115 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2116 case -NFS4ERR_DEADSESSION:
2117 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
2119 case -NFS4ERR_STALE_CLIENTID:
2120 case -NFS4ERR_STALE_STATEID:
2121 /* Don't recall a delegation if it was lost */
2122 nfs4_schedule_lease_recovery(server->nfs_client);
2124 case -NFS4ERR_MOVED:
2125 nfs4_schedule_migration_recovery(server);
2127 case -NFS4ERR_LEASE_MOVED:
2128 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2130 case -NFS4ERR_DELEG_REVOKED:
2131 case -NFS4ERR_ADMIN_REVOKED:
2132 case -NFS4ERR_EXPIRED:
2133 case -NFS4ERR_BAD_STATEID:
2134 case -NFS4ERR_OPENMODE:
2135 nfs_inode_find_state_and_recover(state->inode,
2137 nfs4_schedule_stateid_recovery(server, state);
2139 case -NFS4ERR_DELAY:
2140 case -NFS4ERR_GRACE:
2144 case -NFS4ERR_DENIED:
2146 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2148 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2155 int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2156 struct nfs4_state *state, const nfs4_stateid *stateid)
2158 struct nfs_server *server = NFS_SERVER(state->inode);
2159 struct nfs4_opendata *opendata;
2162 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2163 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2164 if (IS_ERR(opendata))
2165 return PTR_ERR(opendata);
2166 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
2167 if (!test_bit(NFS_O_RDWR_STATE, &state->flags)) {
2168 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2172 if (!test_bit(NFS_O_WRONLY_STATE, &state->flags)) {
2173 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2177 if (!test_bit(NFS_O_RDONLY_STATE, &state->flags)) {
2178 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2182 nfs_state_clear_delegation(state);
2184 nfs4_opendata_put(opendata);
2185 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
2188 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2190 struct nfs4_opendata *data = calldata;
2192 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2193 &data->c_arg.seq_args, &data->c_res.seq_res, task);
2196 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2198 struct nfs4_opendata *data = calldata;
2200 nfs40_sequence_done(task, &data->c_res.seq_res);
2202 data->rpc_status = task->tk_status;
2203 if (data->rpc_status == 0) {
2204 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
2205 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2206 renew_lease(data->o_res.server, data->timestamp);
2207 data->rpc_done = true;
2211 static void nfs4_open_confirm_release(void *calldata)
2213 struct nfs4_opendata *data = calldata;
2214 struct nfs4_state *state = NULL;
2216 /* If this request hasn't been cancelled, do nothing */
2217 if (!data->cancelled)
2219 /* In case of error, no cleanup! */
2220 if (!data->rpc_done)
2222 state = nfs4_opendata_to_nfs4_state(data);
2224 nfs4_close_state(state, data->o_arg.fmode);
2226 nfs4_opendata_put(data);
2229 static const struct rpc_call_ops nfs4_open_confirm_ops = {
2230 .rpc_call_prepare = nfs4_open_confirm_prepare,
2231 .rpc_call_done = nfs4_open_confirm_done,
2232 .rpc_release = nfs4_open_confirm_release,
2236 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2238 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2240 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
2241 struct rpc_task *task;
2242 struct rpc_message msg = {
2243 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2244 .rpc_argp = &data->c_arg,
2245 .rpc_resp = &data->c_res,
2246 .rpc_cred = data->owner->so_cred,
2248 struct rpc_task_setup task_setup_data = {
2249 .rpc_client = server->client,
2250 .rpc_message = &msg,
2251 .callback_ops = &nfs4_open_confirm_ops,
2252 .callback_data = data,
2253 .workqueue = nfsiod_workqueue,
2254 .flags = RPC_TASK_ASYNC,
2258 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2260 kref_get(&data->kref);
2261 data->rpc_done = false;
2262 data->rpc_status = 0;
2263 data->timestamp = jiffies;
2264 task = rpc_run_task(&task_setup_data);
2266 return PTR_ERR(task);
2267 status = rpc_wait_for_completion_task(task);
2269 data->cancelled = true;
2272 status = data->rpc_status;
2277 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
2279 struct nfs4_opendata *data = calldata;
2280 struct nfs4_state_owner *sp = data->owner;
2281 struct nfs_client *clp = sp->so_server->nfs_client;
2282 enum open_claim_type4 claim = data->o_arg.claim;
2284 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
2287 * Check if we still need to send an OPEN call, or if we can use
2288 * a delegation instead.
2290 if (data->state != NULL) {
2291 struct nfs_delegation *delegation;
2293 if (can_open_cached(data->state, data->o_arg.fmode,
2294 data->o_arg.open_flags, claim))
2297 delegation = nfs4_get_valid_delegation(data->state->inode);
2298 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
2299 goto unlock_no_action;
2302 /* Update client id. */
2303 data->o_arg.clientid = clp->cl_clientid;
2307 case NFS4_OPEN_CLAIM_PREVIOUS:
2308 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2309 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
2310 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
2312 case NFS4_OPEN_CLAIM_FH:
2313 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
2315 data->timestamp = jiffies;
2316 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
2317 &data->o_arg.seq_args,
2318 &data->o_res.seq_res,
2320 nfs_release_seqid(data->o_arg.seqid);
2322 /* Set the create mode (note dependency on the session type) */
2323 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2324 if (data->o_arg.open_flags & O_EXCL) {
2325 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2326 if (nfs4_has_persistent_session(clp))
2327 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2328 else if (clp->cl_mvops->minor_version > 0)
2329 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2333 trace_nfs4_cached_open(data->state);
2336 task->tk_action = NULL;
2338 nfs4_sequence_done(task, &data->o_res.seq_res);
2341 static void nfs4_open_done(struct rpc_task *task, void *calldata)
2343 struct nfs4_opendata *data = calldata;
2345 data->rpc_status = task->tk_status;
2347 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
2350 if (task->tk_status == 0) {
2351 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2352 switch (data->o_res.f_attr->mode & S_IFMT) {
2356 data->rpc_status = -ELOOP;
2359 data->rpc_status = -EISDIR;
2362 data->rpc_status = -ENOTDIR;
2365 renew_lease(data->o_res.server, data->timestamp);
2366 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2367 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2369 data->rpc_done = true;
2372 static void nfs4_open_release(void *calldata)
2374 struct nfs4_opendata *data = calldata;
2375 struct nfs4_state *state = NULL;
2377 /* If this request hasn't been cancelled, do nothing */
2378 if (!data->cancelled)
2380 /* In case of error, no cleanup! */
2381 if (data->rpc_status != 0 || !data->rpc_done)
2383 /* In case we need an open_confirm, no cleanup! */
2384 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2386 state = nfs4_opendata_to_nfs4_state(data);
2388 nfs4_close_state(state, data->o_arg.fmode);
2390 nfs4_opendata_put(data);
2393 static const struct rpc_call_ops nfs4_open_ops = {
2394 .rpc_call_prepare = nfs4_open_prepare,
2395 .rpc_call_done = nfs4_open_done,
2396 .rpc_release = nfs4_open_release,
2399 static int nfs4_run_open_task(struct nfs4_opendata *data,
2400 struct nfs_open_context *ctx)
2402 struct inode *dir = d_inode(data->dir);
2403 struct nfs_server *server = NFS_SERVER(dir);
2404 struct nfs_openargs *o_arg = &data->o_arg;
2405 struct nfs_openres *o_res = &data->o_res;
2406 struct rpc_task *task;
2407 struct rpc_message msg = {
2408 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2411 .rpc_cred = data->owner->so_cred,
2413 struct rpc_task_setup task_setup_data = {
2414 .rpc_client = server->client,
2415 .rpc_message = &msg,
2416 .callback_ops = &nfs4_open_ops,
2417 .callback_data = data,
2418 .workqueue = nfsiod_workqueue,
2419 .flags = RPC_TASK_ASYNC,
2423 kref_get(&data->kref);
2424 data->rpc_done = false;
2425 data->rpc_status = 0;
2426 data->cancelled = false;
2427 data->is_recover = false;
2429 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
2430 data->is_recover = true;
2432 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
2433 pnfs_lgopen_prepare(data, ctx);
2435 task = rpc_run_task(&task_setup_data);
2437 return PTR_ERR(task);
2438 status = rpc_wait_for_completion_task(task);
2440 data->cancelled = true;
2443 status = data->rpc_status;
2449 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2451 struct inode *dir = d_inode(data->dir);
2452 struct nfs_openres *o_res = &data->o_res;
2455 status = nfs4_run_open_task(data, NULL);
2456 if (status != 0 || !data->rpc_done)
2459 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2461 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
2462 status = _nfs4_proc_open_confirm(data);
2468 * Additional permission checks in order to distinguish between an
2469 * open for read, and an open for execute. This works around the
2470 * fact that NFSv4 OPEN treats read and execute permissions as being
2472 * Note that in the non-execute case, we want to turn off permission
2473 * checking if we just created a new file (POSIX open() semantics).
2475 static int nfs4_opendata_access(struct rpc_cred *cred,
2476 struct nfs4_opendata *opendata,
2477 struct nfs4_state *state, fmode_t fmode,
2480 struct nfs_access_entry cache;
2483 /* access call failed or for some reason the server doesn't
2484 * support any access modes -- defer access call until later */
2485 if (opendata->o_res.access_supported == 0)
2490 * Use openflags to check for exec, because fmode won't
2491 * always have FMODE_EXEC set when file open for exec.
2493 if (openflags & __FMODE_EXEC) {
2494 /* ONLY check for exec rights */
2495 if (S_ISDIR(state->inode->i_mode))
2496 mask = NFS4_ACCESS_LOOKUP;
2498 mask = NFS4_ACCESS_EXECUTE;
2499 } else if ((fmode & FMODE_READ) && !opendata->file_created)
2500 mask = NFS4_ACCESS_READ;
2503 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2504 nfs_access_add_cache(state->inode, &cache);
2506 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2507 if ((mask & ~cache.mask & flags) == 0)
2514 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2516 static int _nfs4_proc_open(struct nfs4_opendata *data,
2517 struct nfs_open_context *ctx)
2519 struct inode *dir = d_inode(data->dir);
2520 struct nfs_server *server = NFS_SERVER(dir);
2521 struct nfs_openargs *o_arg = &data->o_arg;
2522 struct nfs_openres *o_res = &data->o_res;
2525 status = nfs4_run_open_task(data, ctx);
2526 if (!data->rpc_done)
2529 if (status == -NFS4ERR_BADNAME &&
2530 !(o_arg->open_flags & O_CREAT))
2535 nfs_fattr_map_and_free_names(server, &data->f_attr);
2537 if (o_arg->open_flags & O_CREAT) {
2538 if (o_arg->open_flags & O_EXCL)
2539 data->file_created = true;
2540 else if (o_res->cinfo.before != o_res->cinfo.after)
2541 data->file_created = true;
2542 if (data->file_created ||
2543 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
2544 update_changeattr(dir, &o_res->cinfo,
2545 o_res->f_attr->time_start, 0);
2547 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2548 server->caps &= ~NFS_CAP_POSIX_LOCK;
2549 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2550 status = _nfs4_proc_open_confirm(data);
2554 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2555 nfs4_sequence_free_slot(&o_res->seq_res);
2556 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr,
2557 o_res->f_label, NULL);
2564 * reclaim state on the server after a network partition.
2565 * Assumes caller holds the appropriate lock
2567 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2569 struct nfs4_opendata *opendata;
2572 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2573 NFS4_OPEN_CLAIM_FH);
2574 if (IS_ERR(opendata))
2575 return PTR_ERR(opendata);
2576 ret = nfs4_open_recover(opendata, state);
2578 d_drop(ctx->dentry);
2579 nfs4_opendata_put(opendata);
2583 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2585 struct nfs_server *server = NFS_SERVER(state->inode);
2586 struct nfs4_exception exception = { };
2590 err = _nfs4_open_expired(ctx, state);
2591 trace_nfs4_open_expired(ctx, 0, err);
2592 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2597 case -NFS4ERR_GRACE:
2598 case -NFS4ERR_DELAY:
2599 nfs4_handle_exception(server, err, &exception);
2602 } while (exception.retry);
2607 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2609 struct nfs_open_context *ctx;
2612 ctx = nfs4_state_find_open_context(state);
2615 ret = nfs4_do_open_expired(ctx, state);
2616 put_nfs_open_context(ctx);
2620 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2621 const nfs4_stateid *stateid)
2623 nfs_remove_bad_delegation(state->inode, stateid);
2624 nfs_state_clear_delegation(state);
2627 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2629 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2630 nfs_finish_clear_delegation_stateid(state, NULL);
2633 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2635 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2636 nfs40_clear_delegation_stateid(state);
2637 return nfs4_open_expired(sp, state);
2640 static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2641 nfs4_stateid *stateid,
2642 struct rpc_cred *cred)
2644 return -NFS4ERR_BAD_STATEID;
2647 #if defined(CONFIG_NFS_V4_1)
2648 static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2649 nfs4_stateid *stateid,
2650 struct rpc_cred *cred)
2654 switch (stateid->type) {
2657 case NFS4_INVALID_STATEID_TYPE:
2658 case NFS4_SPECIAL_STATEID_TYPE:
2659 return -NFS4ERR_BAD_STATEID;
2660 case NFS4_REVOKED_STATEID_TYPE:
2664 status = nfs41_test_stateid(server, stateid, cred);
2666 case -NFS4ERR_EXPIRED:
2667 case -NFS4ERR_ADMIN_REVOKED:
2668 case -NFS4ERR_DELEG_REVOKED:
2674 /* Ack the revoked state to the server */
2675 nfs41_free_stateid(server, stateid, cred, true);
2676 return -NFS4ERR_EXPIRED;
2679 static void nfs41_check_delegation_stateid(struct nfs4_state *state)
2681 struct nfs_server *server = NFS_SERVER(state->inode);
2682 nfs4_stateid stateid;
2683 struct nfs_delegation *delegation;
2684 struct rpc_cred *cred;
2687 /* Get the delegation credential for use by test/free_stateid */
2689 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2690 if (delegation == NULL) {
2692 nfs_state_clear_delegation(state);
2696 nfs4_stateid_copy(&stateid, &delegation->stateid);
2697 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
2699 nfs_state_clear_delegation(state);
2703 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2704 &delegation->flags)) {
2709 cred = get_rpccred(delegation->cred);
2711 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
2712 trace_nfs4_test_delegation_stateid(state, NULL, status);
2713 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
2714 nfs_finish_clear_delegation_stateid(state, &stateid);
2720 * nfs41_check_expired_locks - possibly free a lock stateid
2722 * @state: NFSv4 state for an inode
2724 * Returns NFS_OK if recovery for this stateid is now finished.
2725 * Otherwise a negative NFS4ERR value is returned.
2727 static int nfs41_check_expired_locks(struct nfs4_state *state)
2729 int status, ret = NFS_OK;
2730 struct nfs4_lock_state *lsp, *prev = NULL;
2731 struct nfs_server *server = NFS_SERVER(state->inode);
2733 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2736 spin_lock(&state->state_lock);
2737 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2738 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2739 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2741 refcount_inc(&lsp->ls_count);
2742 spin_unlock(&state->state_lock);
2744 nfs4_put_lock_state(prev);
2747 status = nfs41_test_and_free_expired_stateid(server,
2750 trace_nfs4_test_lock_stateid(state, lsp, status);
2751 if (status == -NFS4ERR_EXPIRED ||
2752 status == -NFS4ERR_BAD_STATEID) {
2753 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
2754 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
2755 if (!recover_lost_locks)
2756 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2757 } else if (status != NFS_OK) {
2759 nfs4_put_lock_state(prev);
2762 spin_lock(&state->state_lock);
2765 spin_unlock(&state->state_lock);
2766 nfs4_put_lock_state(prev);
2772 * nfs41_check_open_stateid - possibly free an open stateid
2774 * @state: NFSv4 state for an inode
2776 * Returns NFS_OK if recovery for this stateid is now finished.
2777 * Otherwise a negative NFS4ERR value is returned.
2779 static int nfs41_check_open_stateid(struct nfs4_state *state)
2781 struct nfs_server *server = NFS_SERVER(state->inode);
2782 nfs4_stateid *stateid = &state->open_stateid;
2783 struct rpc_cred *cred = state->owner->so_cred;
2786 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
2787 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2788 if (nfs4_have_delegation(state->inode, state->state))
2790 return -NFS4ERR_OPENMODE;
2792 return -NFS4ERR_BAD_STATEID;
2794 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
2795 trace_nfs4_test_open_stateid(state, NULL, status);
2796 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
2797 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2798 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2799 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2800 clear_bit(NFS_OPEN_STATE, &state->flags);
2801 stateid->type = NFS4_INVALID_STATEID_TYPE;
2804 if (nfs_open_stateid_recover_openmode(state))
2805 return -NFS4ERR_OPENMODE;
2809 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2813 nfs41_check_delegation_stateid(state);
2814 status = nfs41_check_expired_locks(state);
2815 if (status != NFS_OK)
2817 status = nfs41_check_open_stateid(state);
2818 if (status != NFS_OK)
2819 status = nfs4_open_expired(sp, state);
2825 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2826 * fields corresponding to attributes that were used to store the verifier.
2827 * Make sure we clobber those fields in the later setattr call
2829 static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2830 struct iattr *sattr, struct nfs4_label **label)
2832 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2837 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
2838 attrset[i] = opendata->o_res.attrset[i];
2839 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
2840 attrset[i] &= ~bitmask[i];
2843 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
2844 sattr->ia_valid : 0;
2846 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
2847 if (sattr->ia_valid & ATTR_ATIME_SET)
2848 ret |= ATTR_ATIME_SET;
2853 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
2854 if (sattr->ia_valid & ATTR_MTIME_SET)
2855 ret |= ATTR_MTIME_SET;
2860 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
2865 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2868 struct nfs_open_context *ctx)
2870 struct nfs4_state_owner *sp = opendata->owner;
2871 struct nfs_server *server = sp->so_server;
2872 struct dentry *dentry;
2873 struct nfs4_state *state;
2877 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2879 ret = _nfs4_proc_open(opendata, ctx);
2883 state = _nfs4_opendata_to_nfs4_state(opendata);
2884 ret = PTR_ERR(state);
2888 if (server->caps & NFS_CAP_POSIX_LOCK)
2889 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2890 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2891 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
2893 dentry = opendata->dentry;
2894 if (d_really_is_negative(dentry)) {
2895 struct dentry *alias;
2897 alias = d_exact_alias(dentry, state->inode);
2899 alias = d_splice_alias(igrab(state->inode), dentry);
2900 /* d_splice_alias() can't fail here - it's a non-directory */
2903 ctx->dentry = dentry = alias;
2905 nfs_set_verifier(dentry,
2906 nfs_save_change_attribute(d_inode(opendata->dir)));
2909 /* Parse layoutget results before we check for access */
2910 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
2912 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2916 if (d_inode(dentry) == state->inode) {
2917 nfs_inode_attach_open_context(ctx);
2918 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2919 nfs4_schedule_stateid_recovery(server, state);
2923 if (!opendata->cancelled) {
2924 if (opendata->lgp) {
2925 nfs4_lgopen_release(opendata->lgp);
2926 opendata->lgp = NULL;
2928 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
2934 * Returns a referenced nfs4_state
2936 static int _nfs4_do_open(struct inode *dir,
2937 struct nfs_open_context *ctx,
2939 const struct nfs4_open_createattrs *c,
2942 struct nfs4_state_owner *sp;
2943 struct nfs4_state *state = NULL;
2944 struct nfs_server *server = NFS_SERVER(dir);
2945 struct nfs4_opendata *opendata;
2946 struct dentry *dentry = ctx->dentry;
2947 struct rpc_cred *cred = ctx->cred;
2948 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2949 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
2950 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
2951 struct iattr *sattr = c->sattr;
2952 struct nfs4_label *label = c->label;
2953 struct nfs4_label *olabel = NULL;
2956 /* Protect against reboot recovery conflicts */
2958 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2960 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2963 status = nfs4_client_recover_expired_lease(server->nfs_client);
2965 goto err_put_state_owner;
2966 if (d_really_is_positive(dentry))
2967 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
2969 if (d_really_is_positive(dentry))
2970 claim = NFS4_OPEN_CLAIM_FH;
2971 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
2972 c, claim, GFP_KERNEL);
2973 if (opendata == NULL)
2974 goto err_put_state_owner;
2977 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2978 if (IS_ERR(olabel)) {
2979 status = PTR_ERR(olabel);
2980 goto err_opendata_put;
2984 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2985 if (!opendata->f_attr.mdsthreshold) {
2986 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2987 if (!opendata->f_attr.mdsthreshold)
2988 goto err_free_label;
2990 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
2992 if (d_really_is_positive(dentry))
2993 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
2995 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
2997 goto err_free_label;
3000 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
3001 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
3002 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
3004 * send create attributes which was not set by open
3005 * with an extra setattr.
3007 if (attrs || label) {
3008 unsigned ia_old = sattr->ia_valid;
3010 sattr->ia_valid = attrs;
3011 nfs_fattr_init(opendata->o_res.f_attr);
3012 status = nfs4_do_setattr(state->inode, cred,
3013 opendata->o_res.f_attr, sattr,
3014 ctx, label, olabel);
3016 nfs_setattr_update_inode(state->inode, sattr,
3017 opendata->o_res.f_attr);
3018 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
3020 sattr->ia_valid = ia_old;
3023 if (opened && opendata->file_created)
3026 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
3027 *ctx_th = opendata->f_attr.mdsthreshold;
3028 opendata->f_attr.mdsthreshold = NULL;
3031 nfs4_label_free(olabel);
3033 nfs4_opendata_put(opendata);
3034 nfs4_put_state_owner(sp);
3037 nfs4_label_free(olabel);
3039 nfs4_opendata_put(opendata);
3040 err_put_state_owner:
3041 nfs4_put_state_owner(sp);
3047 static struct nfs4_state *nfs4_do_open(struct inode *dir,
3048 struct nfs_open_context *ctx,
3050 struct iattr *sattr,
3051 struct nfs4_label *label,
3054 struct nfs_server *server = NFS_SERVER(dir);
3055 struct nfs4_exception exception = { };
3056 struct nfs4_state *res;
3057 struct nfs4_open_createattrs c = {
3061 [0] = (__u32)jiffies,
3062 [1] = (__u32)current->pid,
3068 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
3070 trace_nfs4_open_file(ctx, flags, status);
3073 /* NOTE: BAD_SEQID means the server and client disagree about the
3074 * book-keeping w.r.t. state-changing operations
3075 * (OPEN/CLOSE/LOCK/LOCKU...)
3076 * It is actually a sign of a bug on the client or on the server.
3078 * If we receive a BAD_SEQID error in the particular case of
3079 * doing an OPEN, we assume that nfs_increment_open_seqid() will
3080 * have unhashed the old state_owner for us, and that we can
3081 * therefore safely retry using a new one. We should still warn
3082 * the user though...
3084 if (status == -NFS4ERR_BAD_SEQID) {
3085 pr_warn_ratelimited("NFS: v4 server %s "
3086 " returned a bad sequence-id error!\n",
3087 NFS_SERVER(dir)->nfs_client->cl_hostname);
3088 exception.retry = 1;
3092 * BAD_STATEID on OPEN means that the server cancelled our
3093 * state before it received the OPEN_CONFIRM.
3094 * Recover by retrying the request as per the discussion
3095 * on Page 181 of RFC3530.
3097 if (status == -NFS4ERR_BAD_STATEID) {
3098 exception.retry = 1;
3101 if (status == -NFS4ERR_EXPIRED) {
3102 nfs4_schedule_lease_recovery(server->nfs_client);
3103 exception.retry = 1;
3106 if (status == -EAGAIN) {
3107 /* We must have found a delegation */
3108 exception.retry = 1;
3111 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3113 res = ERR_PTR(nfs4_handle_exception(server,
3114 status, &exception));
3115 } while (exception.retry);
3119 static int _nfs4_do_setattr(struct inode *inode,
3120 struct nfs_setattrargs *arg,
3121 struct nfs_setattrres *res,
3122 struct rpc_cred *cred,
3123 struct nfs_open_context *ctx)
3125 struct nfs_server *server = NFS_SERVER(inode);
3126 struct rpc_message msg = {
3127 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3132 struct rpc_cred *delegation_cred = NULL;
3133 unsigned long timestamp = jiffies;
3137 nfs_fattr_init(res->fattr);
3139 /* Servers should only apply open mode checks for file size changes */
3140 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
3142 nfs4_inode_make_writeable(inode);
3146 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
3147 /* Use that stateid */
3148 } else if (ctx != NULL && ctx->state) {
3149 struct nfs_lock_context *l_ctx;
3150 if (!nfs4_valid_open_stateid(ctx->state))
3152 l_ctx = nfs_get_lock_context(ctx);
3154 return PTR_ERR(l_ctx);
3155 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3156 &arg->stateid, &delegation_cred);
3157 nfs_put_lock_context(l_ctx);
3162 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
3164 if (delegation_cred)
3165 msg.rpc_cred = delegation_cred;
3167 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3169 put_rpccred(delegation_cred);
3170 if (status == 0 && ctx != NULL)
3171 renew_lease(server, timestamp);
3172 trace_nfs4_setattr(inode, &arg->stateid, status);
3176 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
3177 struct nfs_fattr *fattr, struct iattr *sattr,
3178 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
3179 struct nfs4_label *olabel)
3181 struct nfs_server *server = NFS_SERVER(inode);
3182 __u32 bitmask[NFS4_BITMASK_SZ];
3183 struct nfs4_state *state = ctx ? ctx->state : NULL;
3184 struct nfs_setattrargs arg = {
3185 .fh = NFS_FH(inode),
3191 struct nfs_setattrres res = {
3196 struct nfs4_exception exception = {
3199 .stateid = &arg.stateid,
3204 nfs4_bitmap_copy_adjust_setattr(bitmask,
3205 nfs4_bitmask(server, olabel),
3208 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
3210 case -NFS4ERR_OPENMODE:
3211 if (!(sattr->ia_valid & ATTR_SIZE)) {
3212 pr_warn_once("NFSv4: server %s is incorrectly "
3213 "applying open mode checks to "
3214 "a SETATTR that is not "
3215 "changing file size.\n",
3216 server->nfs_client->cl_hostname);
3218 if (state && !(state->state & FMODE_WRITE)) {
3220 if (sattr->ia_valid & ATTR_OPEN)
3225 err = nfs4_handle_exception(server, err, &exception);
3226 } while (exception.retry);
3232 nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3234 if (inode == NULL || !nfs_have_layout(inode))
3237 return pnfs_wait_on_layoutreturn(inode, task);
3240 struct nfs4_closedata {
3241 struct inode *inode;
3242 struct nfs4_state *state;
3243 struct nfs_closeargs arg;
3244 struct nfs_closeres res;
3246 struct nfs4_layoutreturn_args arg;
3247 struct nfs4_layoutreturn_res res;
3248 struct nfs4_xdr_opaque_data ld_private;
3252 struct nfs_fattr fattr;
3253 unsigned long timestamp;
3256 static void nfs4_free_closedata(void *data)
3258 struct nfs4_closedata *calldata = data;
3259 struct nfs4_state_owner *sp = calldata->state->owner;
3260 struct super_block *sb = calldata->state->inode->i_sb;
3262 if (calldata->lr.roc)
3263 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3264 calldata->res.lr_ret);
3265 nfs4_put_open_state(calldata->state);
3266 nfs_free_seqid(calldata->arg.seqid);
3267 nfs4_put_state_owner(sp);
3268 nfs_sb_deactive(sb);
3272 static void nfs4_close_done(struct rpc_task *task, void *data)
3274 struct nfs4_closedata *calldata = data;
3275 struct nfs4_state *state = calldata->state;
3276 struct nfs_server *server = NFS_SERVER(calldata->inode);
3277 nfs4_stateid *res_stateid = NULL;
3278 struct nfs4_exception exception = {
3280 .inode = calldata->inode,
3281 .stateid = &calldata->arg.stateid,
3284 dprintk("%s: begin!\n", __func__);
3285 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3287 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
3289 /* Handle Layoutreturn errors */
3290 if (calldata->arg.lr_args && task->tk_status != 0) {
3291 switch (calldata->res.lr_ret) {
3293 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3296 calldata->arg.lr_args = NULL;
3297 calldata->res.lr_res = NULL;
3299 case -NFS4ERR_OLD_STATEID:
3300 if (nfs4_layoutreturn_refresh_stateid(&calldata->arg.lr_args->stateid,
3301 &calldata->arg.lr_args->range,
3305 case -NFS4ERR_ADMIN_REVOKED:
3306 case -NFS4ERR_DELEG_REVOKED:
3307 case -NFS4ERR_EXPIRED:
3308 case -NFS4ERR_BAD_STATEID:
3309 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3310 case -NFS4ERR_WRONG_CRED:
3311 calldata->arg.lr_args = NULL;
3312 calldata->res.lr_res = NULL;
3317 /* hmm. we are done with the inode, and in the process of freeing
3318 * the state_owner. we keep this around to process errors
3320 switch (task->tk_status) {
3322 res_stateid = &calldata->res.stateid;
3323 renew_lease(server, calldata->timestamp);
3325 case -NFS4ERR_ACCESS:
3326 if (calldata->arg.bitmask != NULL) {
3327 calldata->arg.bitmask = NULL;
3328 calldata->res.fattr = NULL;
3333 case -NFS4ERR_OLD_STATEID:
3334 /* Did we race with OPEN? */
3335 if (nfs4_refresh_open_stateid(&calldata->arg.stateid,
3339 case -NFS4ERR_ADMIN_REVOKED:
3340 case -NFS4ERR_STALE_STATEID:
3341 case -NFS4ERR_EXPIRED:
3342 nfs4_free_revoked_stateid(server,
3343 &calldata->arg.stateid,
3344 task->tk_msg.rpc_cred);
3346 case -NFS4ERR_BAD_STATEID:
3349 task->tk_status = nfs4_async_handle_exception(task,
3350 server, task->tk_status, &exception);
3351 if (exception.retry)
3354 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3355 res_stateid, calldata->arg.fmode);
3357 task->tk_status = 0;
3358 nfs_release_seqid(calldata->arg.seqid);
3359 nfs_refresh_inode(calldata->inode, &calldata->fattr);
3360 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
3363 calldata->res.lr_ret = 0;
3365 task->tk_status = 0;
3366 rpc_restart_call_prepare(task);
3370 static void nfs4_close_prepare(struct rpc_task *task, void *data)
3372 struct nfs4_closedata *calldata = data;
3373 struct nfs4_state *state = calldata->state;
3374 struct inode *inode = calldata->inode;
3375 struct pnfs_layout_hdr *lo;
3376 bool is_rdonly, is_wronly, is_rdwr;
3379 dprintk("%s: begin!\n", __func__);
3380 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3383 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
3384 spin_lock(&state->owner->so_lock);
3385 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3386 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3387 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
3388 /* Calculate the change in open mode */
3389 calldata->arg.fmode = 0;
3390 if (state->n_rdwr == 0) {
3391 if (state->n_rdonly == 0)
3392 call_close |= is_rdonly;
3394 calldata->arg.fmode |= FMODE_READ;
3395 if (state->n_wronly == 0)
3396 call_close |= is_wronly;
3398 calldata->arg.fmode |= FMODE_WRITE;
3399 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3400 call_close |= is_rdwr;
3402 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3404 if (!nfs4_valid_open_stateid(state) ||
3405 !nfs4_refresh_open_stateid(&calldata->arg.stateid, state))
3407 spin_unlock(&state->owner->so_lock);
3410 /* Note: exit _without_ calling nfs4_close_done */
3414 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
3415 nfs_release_seqid(calldata->arg.seqid);
3419 lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3420 if (lo && !pnfs_layout_is_valid(lo)) {
3421 calldata->arg.lr_args = NULL;
3422 calldata->res.lr_res = NULL;
3425 if (calldata->arg.fmode == 0)
3426 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
3428 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
3429 /* Close-to-open cache consistency revalidation */
3430 if (!nfs4_have_delegation(inode, FMODE_READ))
3431 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3433 calldata->arg.bitmask = NULL;
3436 calldata->arg.share_access =
3437 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3438 calldata->arg.fmode, 0);
3440 if (calldata->res.fattr == NULL)
3441 calldata->arg.bitmask = NULL;
3442 else if (calldata->arg.bitmask == NULL)
3443 calldata->res.fattr = NULL;
3444 calldata->timestamp = jiffies;
3445 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
3446 &calldata->arg.seq_args,
3447 &calldata->res.seq_res,
3449 nfs_release_seqid(calldata->arg.seqid);
3450 dprintk("%s: done!\n", __func__);
3453 task->tk_action = NULL;
3455 nfs4_sequence_done(task, &calldata->res.seq_res);
3458 static const struct rpc_call_ops nfs4_close_ops = {
3459 .rpc_call_prepare = nfs4_close_prepare,
3460 .rpc_call_done = nfs4_close_done,
3461 .rpc_release = nfs4_free_closedata,
3465 * It is possible for data to be read/written from a mem-mapped file
3466 * after the sys_close call (which hits the vfs layer as a flush).
3467 * This means that we can't safely call nfsv4 close on a file until
3468 * the inode is cleared. This in turn means that we are not good
3469 * NFSv4 citizens - we do not indicate to the server to update the file's
3470 * share state even when we are done with one of the three share
3471 * stateid's in the inode.
3473 * NOTE: Caller must be holding the sp->so_owner semaphore!
3475 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
3477 struct nfs_server *server = NFS_SERVER(state->inode);
3478 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
3479 struct nfs4_closedata *calldata;
3480 struct nfs4_state_owner *sp = state->owner;
3481 struct rpc_task *task;
3482 struct rpc_message msg = {
3483 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3484 .rpc_cred = state->owner->so_cred,
3486 struct rpc_task_setup task_setup_data = {
3487 .rpc_client = server->client,
3488 .rpc_message = &msg,
3489 .callback_ops = &nfs4_close_ops,
3490 .workqueue = nfsiod_workqueue,
3491 .flags = RPC_TASK_ASYNC,
3493 int status = -ENOMEM;
3495 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3496 &task_setup_data.rpc_client, &msg);
3498 calldata = kzalloc(sizeof(*calldata), gfp_mask);
3499 if (calldata == NULL)
3501 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
3502 calldata->inode = state->inode;
3503 calldata->state = state;
3504 calldata->arg.fh = NFS_FH(state->inode);
3505 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3506 goto out_free_calldata;
3507 /* Serialization for the sequence id */
3508 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3509 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
3510 if (IS_ERR(calldata->arg.seqid))
3511 goto out_free_calldata;
3512 nfs_fattr_init(&calldata->fattr);
3513 calldata->arg.fmode = 0;
3514 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
3515 calldata->res.fattr = &calldata->fattr;
3516 calldata->res.seqid = calldata->arg.seqid;
3517 calldata->res.server = server;
3518 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3519 calldata->lr.roc = pnfs_roc(state->inode,
3520 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3521 if (calldata->lr.roc) {
3522 calldata->arg.lr_args = &calldata->lr.arg;
3523 calldata->res.lr_res = &calldata->lr.res;
3525 nfs_sb_active(calldata->inode->i_sb);
3527 msg.rpc_argp = &calldata->arg;
3528 msg.rpc_resp = &calldata->res;
3529 task_setup_data.callback_data = calldata;
3530 task = rpc_run_task(&task_setup_data);
3532 return PTR_ERR(task);
3535 status = rpc_wait_for_completion_task(task);
3541 nfs4_put_open_state(state);
3542 nfs4_put_state_owner(sp);
3546 static struct inode *
3547 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3548 int open_flags, struct iattr *attr, int *opened)
3550 struct nfs4_state *state;
3551 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3553 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
3555 /* Protect against concurrent sillydeletes */
3556 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
3558 nfs4_label_release_security(label);
3561 return ERR_CAST(state);
3562 return state->inode;
3565 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
3567 if (ctx->state == NULL)
3570 nfs4_close_sync(ctx->state, ctx->mode);
3572 nfs4_close_state(ctx->state, ctx->mode);
3575 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3576 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3577 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
3579 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3581 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
3582 struct nfs4_server_caps_arg args = {
3586 struct nfs4_server_caps_res res = {};
3587 struct rpc_message msg = {
3588 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
3595 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3596 FATTR4_WORD0_FH_EXPIRE_TYPE |
3597 FATTR4_WORD0_LINK_SUPPORT |
3598 FATTR4_WORD0_SYMLINK_SUPPORT |
3599 FATTR4_WORD0_ACLSUPPORT;
3601 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3603 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3605 /* Sanity check the server answers */
3606 switch (minorversion) {
3608 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3609 res.attr_bitmask[2] = 0;
3612 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3615 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3617 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
3618 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3619 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3620 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3621 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
3622 NFS_CAP_CTIME|NFS_CAP_MTIME|
3623 NFS_CAP_SECURITY_LABEL);
3624 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3625 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3626 server->caps |= NFS_CAP_ACLS;
3627 if (res.has_links != 0)
3628 server->caps |= NFS_CAP_HARDLINKS;
3629 if (res.has_symlinks != 0)
3630 server->caps |= NFS_CAP_SYMLINKS;
3631 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3632 server->caps |= NFS_CAP_FILEID;
3633 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3634 server->caps |= NFS_CAP_MODE;
3635 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3636 server->caps |= NFS_CAP_NLINK;
3637 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3638 server->caps |= NFS_CAP_OWNER;
3639 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3640 server->caps |= NFS_CAP_OWNER_GROUP;
3641 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3642 server->caps |= NFS_CAP_ATIME;
3643 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3644 server->caps |= NFS_CAP_CTIME;
3645 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3646 server->caps |= NFS_CAP_MTIME;
3647 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3648 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3649 server->caps |= NFS_CAP_SECURITY_LABEL;
3651 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3652 sizeof(server->attr_bitmask));
3653 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3655 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3656 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3657 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
3658 server->cache_consistency_bitmask[2] = 0;
3660 /* Avoid a regression due to buggy server */
3661 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3662 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
3663 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3664 sizeof(server->exclcreat_bitmask));
3666 server->acl_bitmask = res.acl_bitmask;
3667 server->fh_expire_type = res.fh_expire_type;
3673 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3675 struct nfs4_exception exception = { };
3678 err = nfs4_handle_exception(server,
3679 _nfs4_server_capabilities(server, fhandle),
3681 } while (exception.retry);
3685 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3686 struct nfs_fsinfo *info)
3689 struct nfs4_lookup_root_arg args = {
3692 struct nfs4_lookup_res res = {
3694 .fattr = info->fattr,
3697 struct rpc_message msg = {
3698 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3703 bitmask[0] = nfs4_fattr_bitmap[0];
3704 bitmask[1] = nfs4_fattr_bitmap[1];
3706 * Process the label in the upcoming getfattr
3708 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3710 nfs_fattr_init(info->fattr);
3711 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3714 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3715 struct nfs_fsinfo *info)
3717 struct nfs4_exception exception = { };
3720 err = _nfs4_lookup_root(server, fhandle, info);
3721 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
3724 case -NFS4ERR_WRONGSEC:
3727 err = nfs4_handle_exception(server, err, &exception);
3729 } while (exception.retry);
3734 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3735 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3737 struct rpc_auth_create_args auth_args = {
3738 .pseudoflavor = flavor,
3740 struct rpc_auth *auth;
3742 auth = rpcauth_create(&auth_args, server->client);
3745 return nfs4_lookup_root(server, fhandle, info);
3749 * Retry pseudoroot lookup with various security flavors. We do this when:
3751 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3752 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3754 * Returns zero on success, or a negative NFS4ERR value, or a
3755 * negative errno value.
3757 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3758 struct nfs_fsinfo *info)
3760 /* Per 3530bis 15.33.5 */
3761 static const rpc_authflavor_t flav_array[] = {
3765 RPC_AUTH_UNIX, /* courtesy */
3768 int status = -EPERM;
3771 if (server->auth_info.flavor_len > 0) {
3772 /* try each flavor specified by user */
3773 for (i = 0; i < server->auth_info.flavor_len; i++) {
3774 status = nfs4_lookup_root_sec(server, fhandle, info,
3775 server->auth_info.flavors[i]);
3776 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3781 /* no flavors specified by user, try default list */
3782 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3783 status = nfs4_lookup_root_sec(server, fhandle, info,
3785 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3792 * -EACCESS could mean that the user doesn't have correct permissions
3793 * to access the mount. It could also mean that we tried to mount
3794 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3795 * existing mount programs don't handle -EACCES very well so it should
3796 * be mapped to -EPERM instead.
3798 if (status == -EACCES)
3804 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3805 * @server: initialized nfs_server handle
3806 * @fhandle: we fill in the pseudo-fs root file handle
3807 * @info: we fill in an FSINFO struct
3808 * @auth_probe: probe the auth flavours
3810 * Returns zero on success, or a negative errno.
3812 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
3813 struct nfs_fsinfo *info,
3819 status = nfs4_lookup_root(server, fhandle, info);
3821 if (auth_probe || status == NFS4ERR_WRONGSEC)
3822 status = server->nfs_client->cl_mvops->find_root_sec(server,
3826 status = nfs4_server_capabilities(server, fhandle);
3828 status = nfs4_do_fsinfo(server, fhandle, info);
3830 return nfs4_map_errors(status);
3833 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3834 struct nfs_fsinfo *info)
3837 struct nfs_fattr *fattr = info->fattr;
3838 struct nfs4_label *label = NULL;
3840 error = nfs4_server_capabilities(server, mntfh);
3842 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3846 label = nfs4_label_alloc(server, GFP_KERNEL);
3848 return PTR_ERR(label);
3850 error = nfs4_proc_getattr(server, mntfh, fattr, label, NULL);
3852 dprintk("nfs4_get_root: getattr error = %d\n", -error);
3853 goto err_free_label;
3856 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3857 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3858 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3861 nfs4_label_free(label);
3867 * Get locations and (maybe) other attributes of a referral.
3868 * Note that we'll actually follow the referral later when
3869 * we detect fsid mismatch in inode revalidation
3871 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3872 const struct qstr *name, struct nfs_fattr *fattr,
3873 struct nfs_fh *fhandle)
3875 int status = -ENOMEM;
3876 struct page *page = NULL;
3877 struct nfs4_fs_locations *locations = NULL;
3879 page = alloc_page(GFP_KERNEL);
3882 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3883 if (locations == NULL)
3886 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
3891 * If the fsid didn't change, this is a migration event, not a
3892 * referral. Cause us to drop into the exception handler, which
3893 * will kick off migration recovery.
3895 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
3896 dprintk("%s: server did not return a different fsid for"
3897 " a referral at %s\n", __func__, name->name);
3898 status = -NFS4ERR_MOVED;
3901 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3902 nfs_fixup_referral_attributes(&locations->fattr);
3904 /* replace the lookup nfs_fattr with the locations nfs_fattr */
3905 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
3906 memset(fhandle, 0, sizeof(struct nfs_fh));
3914 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3915 struct nfs_fattr *fattr, struct nfs4_label *label,
3916 struct inode *inode)
3918 __u32 bitmask[NFS4_BITMASK_SZ];
3919 struct nfs4_getattr_arg args = {
3923 struct nfs4_getattr_res res = {
3928 struct rpc_message msg = {
3929 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3934 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode);
3936 nfs_fattr_init(fattr);
3937 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3940 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3941 struct nfs_fattr *fattr, struct nfs4_label *label,
3942 struct inode *inode)
3944 struct nfs4_exception exception = { };
3947 err = _nfs4_proc_getattr(server, fhandle, fattr, label, inode);
3948 trace_nfs4_getattr(server, fhandle, fattr, err);
3949 err = nfs4_handle_exception(server, err,
3951 } while (exception.retry);
3956 * The file is not closed if it is opened due to the a request to change
3957 * the size of the file. The open call will not be needed once the
3958 * VFS layer lookup-intents are implemented.
3960 * Close is called when the inode is destroyed.
3961 * If we haven't opened the file for O_WRONLY, we
3962 * need to in the size_change case to obtain a stateid.
3965 * Because OPEN is always done by name in nfsv4, it is
3966 * possible that we opened a different file by the same
3967 * name. We can recognize this race condition, but we
3968 * can't do anything about it besides returning an error.
3970 * This will be fixed with VFS changes (lookup-intent).
3973 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3974 struct iattr *sattr)
3976 struct inode *inode = d_inode(dentry);
3977 struct rpc_cred *cred = NULL;
3978 struct nfs_open_context *ctx = NULL;
3979 struct nfs4_label *label = NULL;
3982 if (pnfs_ld_layoutret_on_setattr(inode) &&
3983 sattr->ia_valid & ATTR_SIZE &&
3984 sattr->ia_size < i_size_read(inode))
3985 pnfs_commit_and_return_layout(inode);
3987 nfs_fattr_init(fattr);
3989 /* Deal with open(O_TRUNC) */
3990 if (sattr->ia_valid & ATTR_OPEN)
3991 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
3993 /* Optimization: if the end result is no change, don't RPC */
3994 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
3997 /* Search for an existing open(O_WRITE) file */
3998 if (sattr->ia_valid & ATTR_FILE) {
4000 ctx = nfs_file_open_context(sattr->ia_file);
4005 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4007 return PTR_ERR(label);
4009 /* Return any delegations if we're going to change ACLs */
4010 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
4011 nfs4_inode_make_writeable(inode);
4013 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
4015 nfs_setattr_update_inode(inode, sattr, fattr);
4016 nfs_setsecurity(inode, fattr, label);
4018 nfs4_label_free(label);
4022 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
4023 const struct qstr *name, struct nfs_fh *fhandle,
4024 struct nfs_fattr *fattr, struct nfs4_label *label)
4026 struct nfs_server *server = NFS_SERVER(dir);
4028 struct nfs4_lookup_arg args = {
4029 .bitmask = server->attr_bitmask,
4030 .dir_fh = NFS_FH(dir),
4033 struct nfs4_lookup_res res = {
4039 struct rpc_message msg = {
4040 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4045 args.bitmask = nfs4_bitmask(server, label);
4047 nfs_fattr_init(fattr);
4049 dprintk("NFS call lookup %s\n", name->name);
4050 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
4051 dprintk("NFS reply lookup: %d\n", status);
4055 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
4057 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4058 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
4059 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4063 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
4064 const struct qstr *name, struct nfs_fh *fhandle,
4065 struct nfs_fattr *fattr, struct nfs4_label *label)
4067 struct nfs4_exception exception = { };
4068 struct rpc_clnt *client = *clnt;
4071 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
4072 trace_nfs4_lookup(dir, name, err);
4074 case -NFS4ERR_BADNAME:
4077 case -NFS4ERR_MOVED:
4078 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
4079 if (err == -NFS4ERR_MOVED)
4080 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4082 case -NFS4ERR_WRONGSEC:
4084 if (client != *clnt)
4086 client = nfs4_negotiate_security(client, dir, name);
4088 return PTR_ERR(client);
4090 exception.retry = 1;
4093 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4095 } while (exception.retry);
4100 else if (client != *clnt)
4101 rpc_shutdown_client(client);
4106 static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
4107 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4108 struct nfs4_label *label)
4111 struct rpc_clnt *client = NFS_CLIENT(dir);
4113 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
4114 if (client != NFS_CLIENT(dir)) {
4115 rpc_shutdown_client(client);
4116 nfs_fixup_secinfo_attributes(fattr);
4122 nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
4123 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4125 struct rpc_clnt *client = NFS_CLIENT(dir);
4128 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
4130 return ERR_PTR(status);
4131 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
4134 static int _nfs4_proc_lookupp(struct inode *inode,
4135 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4136 struct nfs4_label *label)
4138 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4139 struct nfs_server *server = NFS_SERVER(inode);
4141 struct nfs4_lookupp_arg args = {
4142 .bitmask = server->attr_bitmask,
4143 .fh = NFS_FH(inode),
4145 struct nfs4_lookupp_res res = {
4151 struct rpc_message msg = {
4152 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4157 args.bitmask = nfs4_bitmask(server, label);
4159 nfs_fattr_init(fattr);
4161 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4162 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4164 dprintk("NFS reply lookupp: %d\n", status);
4168 static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4169 struct nfs_fattr *fattr, struct nfs4_label *label)
4171 struct nfs4_exception exception = { };
4174 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4175 trace_nfs4_lookupp(inode, err);
4176 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4178 } while (exception.retry);
4182 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4184 struct nfs_server *server = NFS_SERVER(inode);
4185 struct nfs4_accessargs args = {
4186 .fh = NFS_FH(inode),
4187 .access = entry->mask,
4189 struct nfs4_accessres res = {
4192 struct rpc_message msg = {
4193 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4196 .rpc_cred = entry->cred,
4200 if (!nfs4_have_delegation(inode, FMODE_READ)) {
4201 res.fattr = nfs_alloc_fattr();
4202 if (res.fattr == NULL)
4204 args.bitmask = server->cache_consistency_bitmask;
4207 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4209 nfs_access_set_mask(entry, res.access);
4211 nfs_refresh_inode(inode, res.fattr);
4213 nfs_free_fattr(res.fattr);
4217 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4219 struct nfs4_exception exception = { };
4222 err = _nfs4_proc_access(inode, entry);
4223 trace_nfs4_access(inode, err);
4224 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4226 } while (exception.retry);
4231 * TODO: For the time being, we don't try to get any attributes
4232 * along with any of the zero-copy operations READ, READDIR,
4235 * In the case of the first three, we want to put the GETATTR
4236 * after the read-type operation -- this is because it is hard
4237 * to predict the length of a GETATTR response in v4, and thus
4238 * align the READ data correctly. This means that the GETATTR
4239 * may end up partially falling into the page cache, and we should
4240 * shift it into the 'tail' of the xdr_buf before processing.
4241 * To do this efficiently, we need to know the total length
4242 * of data received, which doesn't seem to be available outside
4245 * In the case of WRITE, we also want to put the GETATTR after
4246 * the operation -- in this case because we want to make sure
4247 * we get the post-operation mtime and size.
4249 * Both of these changes to the XDR layer would in fact be quite
4250 * minor, but I decided to leave them for a subsequent patch.
4252 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4253 unsigned int pgbase, unsigned int pglen)
4255 struct nfs4_readlink args = {
4256 .fh = NFS_FH(inode),
4261 struct nfs4_readlink_res res;
4262 struct rpc_message msg = {
4263 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4268 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
4271 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4272 unsigned int pgbase, unsigned int pglen)
4274 struct nfs4_exception exception = { };
4277 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4278 trace_nfs4_readlink(inode, err);
4279 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4281 } while (exception.retry);
4286 * This is just for mknod. open(O_CREAT) will always do ->open_context().
4289 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
4292 struct nfs_server *server = NFS_SERVER(dir);
4293 struct nfs4_label l, *ilabel = NULL;
4294 struct nfs_open_context *ctx;
4295 struct nfs4_state *state;
4298 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
4300 return PTR_ERR(ctx);
4302 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4304 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4305 sattr->ia_mode &= ~current_umask();
4306 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
4307 if (IS_ERR(state)) {
4308 status = PTR_ERR(state);
4312 nfs4_label_release_security(ilabel);
4313 put_nfs_open_context(ctx);
4318 _nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
4320 struct nfs_server *server = NFS_SERVER(dir);
4321 struct nfs_removeargs args = {
4325 struct nfs_removeres res = {
4328 struct rpc_message msg = {
4329 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4333 unsigned long timestamp = jiffies;
4336 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
4338 spin_lock(&dir->i_lock);
4339 update_changeattr_locked(dir, &res.cinfo, timestamp, 0);
4340 /* Removing a directory decrements nlink in the parent */
4341 if (ftype == NF4DIR && dir->i_nlink > 2)
4342 nfs4_dec_nlink_locked(dir);
4343 spin_unlock(&dir->i_lock);
4348 static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4350 struct nfs4_exception exception = { };
4351 struct inode *inode = d_inode(dentry);
4355 if (inode->i_nlink == 1)
4356 nfs4_inode_return_delegation(inode);
4358 nfs4_inode_make_writeable(inode);
4361 err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
4362 trace_nfs4_remove(dir, &dentry->d_name, err);
4363 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4365 } while (exception.retry);
4369 static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
4371 struct nfs4_exception exception = { };
4375 err = _nfs4_proc_remove(dir, name, NF4DIR);
4376 trace_nfs4_remove(dir, name, err);
4377 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4379 } while (exception.retry);
4383 static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4384 struct dentry *dentry,
4385 struct inode *inode)
4387 struct nfs_removeargs *args = msg->rpc_argp;
4388 struct nfs_removeres *res = msg->rpc_resp;
4390 res->server = NFS_SB(dentry->d_sb);
4391 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
4392 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
4394 nfs_fattr_init(res->dir_attr);
4397 nfs4_inode_return_delegation(inode);
4400 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4402 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
4403 &data->args.seq_args,
4408 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
4410 struct nfs_unlinkdata *data = task->tk_calldata;
4411 struct nfs_removeres *res = &data->res;
4413 if (!nfs4_sequence_done(task, &res->seq_res))
4415 if (nfs4_async_handle_error(task, res->server, NULL,
4416 &data->timeout) == -EAGAIN)
4418 if (task->tk_status == 0)
4419 update_changeattr(dir, &res->cinfo,
4420 res->dir_attr->time_start, 0);
4424 static void nfs4_proc_rename_setup(struct rpc_message *msg,
4425 struct dentry *old_dentry,
4426 struct dentry *new_dentry)
4428 struct nfs_renameargs *arg = msg->rpc_argp;
4429 struct nfs_renameres *res = msg->rpc_resp;
4430 struct inode *old_inode = d_inode(old_dentry);
4431 struct inode *new_inode = d_inode(new_dentry);
4434 nfs4_inode_make_writeable(old_inode);
4436 nfs4_inode_return_delegation(new_inode);
4437 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
4438 res->server = NFS_SB(old_dentry->d_sb);
4439 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
4442 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4444 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
4445 &data->args.seq_args,
4450 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4451 struct inode *new_dir)
4453 struct nfs_renamedata *data = task->tk_calldata;
4454 struct nfs_renameres *res = &data->res;
4456 if (!nfs4_sequence_done(task, &res->seq_res))
4458 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
4461 if (task->tk_status == 0) {
4462 if (new_dir != old_dir) {
4463 /* Note: If we moved a directory, nlink will change */
4464 update_changeattr(old_dir, &res->old_cinfo,
4465 res->old_fattr->time_start,
4466 NFS_INO_INVALID_OTHER);
4467 update_changeattr(new_dir, &res->new_cinfo,
4468 res->new_fattr->time_start,
4469 NFS_INO_INVALID_OTHER);
4471 update_changeattr(old_dir, &res->old_cinfo,
4472 res->old_fattr->time_start,
4478 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4480 struct nfs_server *server = NFS_SERVER(inode);
4481 __u32 bitmask[NFS4_BITMASK_SZ];
4482 struct nfs4_link_arg arg = {
4483 .fh = NFS_FH(inode),
4484 .dir_fh = NFS_FH(dir),
4488 struct nfs4_link_res res = {
4492 struct rpc_message msg = {
4493 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4497 int status = -ENOMEM;
4499 res.fattr = nfs_alloc_fattr();
4500 if (res.fattr == NULL)
4503 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4504 if (IS_ERR(res.label)) {
4505 status = PTR_ERR(res.label);
4509 nfs4_inode_make_writeable(inode);
4510 nfs4_bitmap_copy_adjust_setattr(bitmask, nfs4_bitmask(server, res.label), inode);
4512 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4514 update_changeattr(dir, &res.cinfo, res.fattr->time_start, 0);
4515 status = nfs_post_op_update_inode(inode, res.fattr);
4517 nfs_setsecurity(inode, res.fattr, res.label);
4521 nfs4_label_free(res.label);
4524 nfs_free_fattr(res.fattr);
4528 static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4530 struct nfs4_exception exception = { };
4533 err = nfs4_handle_exception(NFS_SERVER(inode),
4534 _nfs4_proc_link(inode, dir, name),
4536 } while (exception.retry);
4540 struct nfs4_createdata {
4541 struct rpc_message msg;
4542 struct nfs4_create_arg arg;
4543 struct nfs4_create_res res;
4545 struct nfs_fattr fattr;
4546 struct nfs4_label *label;
4549 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
4550 const struct qstr *name, struct iattr *sattr, u32 ftype)
4552 struct nfs4_createdata *data;
4554 data = kzalloc(sizeof(*data), GFP_KERNEL);
4556 struct nfs_server *server = NFS_SERVER(dir);
4558 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4559 if (IS_ERR(data->label))
4562 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4563 data->msg.rpc_argp = &data->arg;
4564 data->msg.rpc_resp = &data->res;
4565 data->arg.dir_fh = NFS_FH(dir);
4566 data->arg.server = server;
4567 data->arg.name = name;
4568 data->arg.attrs = sattr;
4569 data->arg.ftype = ftype;
4570 data->arg.bitmask = nfs4_bitmask(server, data->label);
4571 data->arg.umask = current_umask();
4572 data->res.server = server;
4573 data->res.fh = &data->fh;
4574 data->res.fattr = &data->fattr;
4575 data->res.label = data->label;
4576 nfs_fattr_init(data->res.fattr);
4584 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4586 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
4587 &data->arg.seq_args, &data->res.seq_res, 1);
4589 spin_lock(&dir->i_lock);
4590 update_changeattr_locked(dir, &data->res.dir_cinfo,
4591 data->res.fattr->time_start, 0);
4592 /* Creating a directory bumps nlink in the parent */
4593 if (data->arg.ftype == NF4DIR)
4594 nfs4_inc_nlink_locked(dir);
4595 spin_unlock(&dir->i_lock);
4596 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
4601 static void nfs4_free_createdata(struct nfs4_createdata *data)
4603 nfs4_label_free(data->label);
4607 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4608 struct page *page, unsigned int len, struct iattr *sattr,
4609 struct nfs4_label *label)
4611 struct nfs4_createdata *data;
4612 int status = -ENAMETOOLONG;
4614 if (len > NFS4_MAXPATHLEN)
4618 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4622 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4623 data->arg.u.symlink.pages = &page;
4624 data->arg.u.symlink.len = len;
4625 data->arg.label = label;
4627 status = nfs4_do_create(dir, dentry, data);
4629 nfs4_free_createdata(data);
4634 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4635 struct page *page, unsigned int len, struct iattr *sattr)
4637 struct nfs4_exception exception = { };
4638 struct nfs4_label l, *label = NULL;
4641 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4644 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4645 trace_nfs4_symlink(dir, &dentry->d_name, err);
4646 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4648 } while (exception.retry);
4650 nfs4_label_release_security(label);
4654 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4655 struct iattr *sattr, struct nfs4_label *label)
4657 struct nfs4_createdata *data;
4658 int status = -ENOMEM;
4660 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4664 data->arg.label = label;
4665 status = nfs4_do_create(dir, dentry, data);
4667 nfs4_free_createdata(data);
4672 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4673 struct iattr *sattr)
4675 struct nfs_server *server = NFS_SERVER(dir);
4676 struct nfs4_exception exception = { };
4677 struct nfs4_label l, *label = NULL;
4680 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4682 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4683 sattr->ia_mode &= ~current_umask();
4685 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4686 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4687 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4689 } while (exception.retry);
4690 nfs4_label_release_security(label);
4695 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
4696 u64 cookie, struct page **pages, unsigned int count, bool plus)
4698 struct inode *dir = d_inode(dentry);
4699 struct nfs_server *server = NFS_SERVER(dir);
4700 struct nfs4_readdir_arg args = {
4707 struct nfs4_readdir_res res;
4708 struct rpc_message msg = {
4709 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4716 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4718 (unsigned long long)cookie);
4719 if (!(server->caps & NFS_CAP_SECURITY_LABEL))
4720 args.bitmask = server->attr_bitmask_nl;
4722 args.bitmask = server->attr_bitmask;
4724 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
4725 res.pgbase = args.pgbase;
4726 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
4729 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
4730 status += args.pgbase;
4733 nfs_invalidate_atime(dir);
4735 dprintk("%s: returns %d\n", __func__, status);
4739 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
4740 u64 cookie, struct page **pages, unsigned int count, bool plus)
4742 struct nfs4_exception exception = { };
4745 err = _nfs4_proc_readdir(dentry, cred, cookie,
4746 pages, count, plus);
4747 trace_nfs4_readdir(d_inode(dentry), err);
4748 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
4750 } while (exception.retry);
4754 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4755 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
4757 struct nfs4_createdata *data;
4758 int mode = sattr->ia_mode;
4759 int status = -ENOMEM;
4761 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4766 data->arg.ftype = NF4FIFO;
4767 else if (S_ISBLK(mode)) {
4768 data->arg.ftype = NF4BLK;
4769 data->arg.u.device.specdata1 = MAJOR(rdev);
4770 data->arg.u.device.specdata2 = MINOR(rdev);
4772 else if (S_ISCHR(mode)) {
4773 data->arg.ftype = NF4CHR;
4774 data->arg.u.device.specdata1 = MAJOR(rdev);
4775 data->arg.u.device.specdata2 = MINOR(rdev);
4776 } else if (!S_ISSOCK(mode)) {
4781 data->arg.label = label;
4782 status = nfs4_do_create(dir, dentry, data);
4784 nfs4_free_createdata(data);
4789 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4790 struct iattr *sattr, dev_t rdev)
4792 struct nfs_server *server = NFS_SERVER(dir);
4793 struct nfs4_exception exception = { };
4794 struct nfs4_label l, *label = NULL;
4797 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4799 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4800 sattr->ia_mode &= ~current_umask();
4802 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4803 trace_nfs4_mknod(dir, &dentry->d_name, err);
4804 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4806 } while (exception.retry);
4808 nfs4_label_release_security(label);
4813 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4814 struct nfs_fsstat *fsstat)
4816 struct nfs4_statfs_arg args = {
4818 .bitmask = server->attr_bitmask,
4820 struct nfs4_statfs_res res = {
4823 struct rpc_message msg = {
4824 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4829 nfs_fattr_init(fsstat->fattr);
4830 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4833 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4835 struct nfs4_exception exception = { };
4838 err = nfs4_handle_exception(server,
4839 _nfs4_proc_statfs(server, fhandle, fsstat),
4841 } while (exception.retry);
4845 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4846 struct nfs_fsinfo *fsinfo)
4848 struct nfs4_fsinfo_arg args = {
4850 .bitmask = server->attr_bitmask,
4852 struct nfs4_fsinfo_res res = {
4855 struct rpc_message msg = {
4856 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4861 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4864 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4866 struct nfs4_exception exception = { };
4867 unsigned long now = jiffies;
4871 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
4872 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
4874 nfs4_set_lease_period(server->nfs_client,
4875 fsinfo->lease_time * HZ,
4879 err = nfs4_handle_exception(server, err, &exception);
4880 } while (exception.retry);
4884 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4888 nfs_fattr_init(fsinfo->fattr);
4889 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
4891 /* block layout checks this! */
4892 server->pnfs_blksize = fsinfo->blksize;
4893 set_pnfs_layoutdriver(server, fhandle, fsinfo);
4899 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4900 struct nfs_pathconf *pathconf)
4902 struct nfs4_pathconf_arg args = {
4904 .bitmask = server->attr_bitmask,
4906 struct nfs4_pathconf_res res = {
4907 .pathconf = pathconf,
4909 struct rpc_message msg = {
4910 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4915 /* None of the pathconf attributes are mandatory to implement */
4916 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4917 memset(pathconf, 0, sizeof(*pathconf));
4921 nfs_fattr_init(pathconf->fattr);
4922 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4925 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4926 struct nfs_pathconf *pathconf)
4928 struct nfs4_exception exception = { };
4932 err = nfs4_handle_exception(server,
4933 _nfs4_proc_pathconf(server, fhandle, pathconf),
4935 } while (exception.retry);
4939 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
4940 const struct nfs_open_context *ctx,
4941 const struct nfs_lock_context *l_ctx,
4944 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
4946 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4948 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4949 const struct nfs_open_context *ctx,
4950 const struct nfs_lock_context *l_ctx,
4953 nfs4_stateid current_stateid;
4955 /* If the current stateid represents a lost lock, then exit */
4956 if (nfs4_set_rw_stateid(¤t_stateid, ctx, l_ctx, fmode) == -EIO)
4958 return nfs4_stateid_match(stateid, ¤t_stateid);
4961 static bool nfs4_error_stateid_expired(int err)
4964 case -NFS4ERR_DELEG_REVOKED:
4965 case -NFS4ERR_ADMIN_REVOKED:
4966 case -NFS4ERR_BAD_STATEID:
4967 case -NFS4ERR_STALE_STATEID:
4968 case -NFS4ERR_OLD_STATEID:
4969 case -NFS4ERR_OPENMODE:
4970 case -NFS4ERR_EXPIRED:
4976 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
4978 struct nfs_server *server = NFS_SERVER(hdr->inode);
4980 trace_nfs4_read(hdr, task->tk_status);
4981 if (task->tk_status < 0) {
4982 struct nfs4_exception exception = {
4983 .inode = hdr->inode,
4984 .state = hdr->args.context->state,
4985 .stateid = &hdr->args.stateid,
4987 task->tk_status = nfs4_async_handle_exception(task,
4988 server, task->tk_status, &exception);
4989 if (exception.retry) {
4990 rpc_restart_call_prepare(task);
4995 if (task->tk_status > 0)
4996 renew_lease(server, hdr->timestamp);
5000 static bool nfs4_read_stateid_changed(struct rpc_task *task,
5001 struct nfs_pgio_args *args)
5004 if (!nfs4_error_stateid_expired(task->tk_status) ||
5005 nfs4_stateid_is_current(&args->stateid,
5010 rpc_restart_call_prepare(task);
5014 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5017 dprintk("--> %s\n", __func__);
5019 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5021 if (nfs4_read_stateid_changed(task, &hdr->args))
5023 if (task->tk_status > 0)
5024 nfs_invalidate_atime(hdr->inode);
5025 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5026 nfs4_read_done_cb(task, hdr);
5029 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5030 struct rpc_message *msg)
5032 hdr->timestamp = jiffies;
5033 if (!hdr->pgio_done_cb)
5034 hdr->pgio_done_cb = nfs4_read_done_cb;
5035 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5036 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5039 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5040 struct nfs_pgio_header *hdr)
5042 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
5043 &hdr->args.seq_args,
5047 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5048 hdr->args.lock_context,
5049 hdr->rw_mode) == -EIO)
5051 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
5056 static int nfs4_write_done_cb(struct rpc_task *task,
5057 struct nfs_pgio_header *hdr)
5059 struct inode *inode = hdr->inode;
5061 trace_nfs4_write(hdr, task->tk_status);
5062 if (task->tk_status < 0) {
5063 struct nfs4_exception exception = {
5064 .inode = hdr->inode,
5065 .state = hdr->args.context->state,
5066 .stateid = &hdr->args.stateid,
5068 task->tk_status = nfs4_async_handle_exception(task,
5069 NFS_SERVER(inode), task->tk_status,
5071 if (exception.retry) {
5072 rpc_restart_call_prepare(task);
5076 if (task->tk_status >= 0) {
5077 renew_lease(NFS_SERVER(inode), hdr->timestamp);
5078 nfs_writeback_update_inode(hdr);
5083 static bool nfs4_write_stateid_changed(struct rpc_task *task,
5084 struct nfs_pgio_args *args)
5087 if (!nfs4_error_stateid_expired(task->tk_status) ||
5088 nfs4_stateid_is_current(&args->stateid,
5093 rpc_restart_call_prepare(task);
5097 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5099 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5101 if (nfs4_write_stateid_changed(task, &hdr->args))
5103 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5104 nfs4_write_done_cb(task, hdr);
5108 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
5110 /* Don't request attributes for pNFS or O_DIRECT writes */
5111 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
5113 /* Otherwise, request attributes if and only if we don't hold
5116 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
5119 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
5120 struct rpc_message *msg,
5121 struct rpc_clnt **clnt)
5123 struct nfs_server *server = NFS_SERVER(hdr->inode);
5125 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5126 hdr->args.bitmask = NULL;
5127 hdr->res.fattr = NULL;
5129 hdr->args.bitmask = server->cache_consistency_bitmask;
5131 if (!hdr->pgio_done_cb)
5132 hdr->pgio_done_cb = nfs4_write_done_cb;
5133 hdr->res.server = server;
5134 hdr->timestamp = jiffies;
5136 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
5137 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5138 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
5141 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5143 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
5144 &data->args.seq_args,
5149 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
5151 struct inode *inode = data->inode;
5153 trace_nfs4_commit(data, task->tk_status);
5154 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5155 NULL, NULL) == -EAGAIN) {
5156 rpc_restart_call_prepare(task);
5162 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
5164 if (!nfs4_sequence_done(task, &data->res.seq_res))
5166 return data->commit_done_cb(task, data);
5169 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5170 struct rpc_clnt **clnt)
5172 struct nfs_server *server = NFS_SERVER(data->inode);
5174 if (data->commit_done_cb == NULL)
5175 data->commit_done_cb = nfs4_commit_done_cb;
5176 data->res.server = server;
5177 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
5178 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
5179 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
5182 static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5183 struct nfs_commitres *res)
5185 struct inode *dst_inode = file_inode(dst);
5186 struct nfs_server *server = NFS_SERVER(dst_inode);
5187 struct rpc_message msg = {
5188 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5193 args->fh = NFS_FH(dst_inode);
5194 return nfs4_call_sync(server->client, server, &msg,
5195 &args->seq_args, &res->seq_res, 1);
5198 int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5200 struct nfs_commitargs args = {
5204 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5205 struct nfs4_exception exception = { };
5209 status = _nfs4_proc_commit(dst, &args, res);
5210 status = nfs4_handle_exception(dst_server, status, &exception);
5211 } while (exception.retry);
5216 struct nfs4_renewdata {
5217 struct nfs_client *client;
5218 unsigned long timestamp;
5222 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5223 * standalone procedure for queueing an asynchronous RENEW.
5225 static void nfs4_renew_release(void *calldata)
5227 struct nfs4_renewdata *data = calldata;
5228 struct nfs_client *clp = data->client;
5230 if (refcount_read(&clp->cl_count) > 1)
5231 nfs4_schedule_state_renewal(clp);
5232 nfs_put_client(clp);
5236 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
5238 struct nfs4_renewdata *data = calldata;
5239 struct nfs_client *clp = data->client;
5240 unsigned long timestamp = data->timestamp;
5242 trace_nfs4_renew_async(clp, task->tk_status);
5243 switch (task->tk_status) {
5246 case -NFS4ERR_LEASE_MOVED:
5247 nfs4_schedule_lease_moved_recovery(clp);
5250 /* Unless we're shutting down, schedule state recovery! */
5251 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5253 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
5254 nfs4_schedule_lease_recovery(clp);
5257 nfs4_schedule_path_down_recovery(clp);
5259 do_renew_lease(clp, timestamp);
5262 static const struct rpc_call_ops nfs4_renew_ops = {
5263 .rpc_call_done = nfs4_renew_done,
5264 .rpc_release = nfs4_renew_release,
5267 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
5269 struct rpc_message msg = {
5270 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5274 struct nfs4_renewdata *data;
5276 if (renew_flags == 0)
5278 if (!refcount_inc_not_zero(&clp->cl_count))
5280 data = kmalloc(sizeof(*data), GFP_NOFS);
5282 nfs_put_client(clp);
5286 data->timestamp = jiffies;
5287 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
5288 &nfs4_renew_ops, data);
5291 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
5293 struct rpc_message msg = {
5294 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5298 unsigned long now = jiffies;
5301 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5304 do_renew_lease(clp, now);
5308 static inline int nfs4_server_supports_acls(struct nfs_server *server)
5310 return server->caps & NFS_CAP_ACLS;
5313 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5314 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
5317 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
5319 static int buf_to_pages_noslab(const void *buf, size_t buflen,
5320 struct page **pages)
5322 struct page *newpage, **spages;
5328 len = min_t(size_t, PAGE_SIZE, buflen);
5329 newpage = alloc_page(GFP_KERNEL);
5331 if (newpage == NULL)
5333 memcpy(page_address(newpage), buf, len);
5338 } while (buflen != 0);
5344 __free_page(spages[rc-1]);
5348 struct nfs4_cached_acl {
5354 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
5356 struct nfs_inode *nfsi = NFS_I(inode);
5358 spin_lock(&inode->i_lock);
5359 kfree(nfsi->nfs4_acl);
5360 nfsi->nfs4_acl = acl;
5361 spin_unlock(&inode->i_lock);
5364 static void nfs4_zap_acl_attr(struct inode *inode)
5366 nfs4_set_cached_acl(inode, NULL);
5369 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5371 struct nfs_inode *nfsi = NFS_I(inode);
5372 struct nfs4_cached_acl *acl;
5375 spin_lock(&inode->i_lock);
5376 acl = nfsi->nfs4_acl;
5379 if (buf == NULL) /* user is just asking for length */
5381 if (acl->cached == 0)
5383 ret = -ERANGE; /* see getxattr(2) man page */
5384 if (acl->len > buflen)
5386 memcpy(buf, acl->data, acl->len);
5390 spin_unlock(&inode->i_lock);
5394 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
5396 struct nfs4_cached_acl *acl;
5397 size_t buflen = sizeof(*acl) + acl_len;
5399 if (buflen <= PAGE_SIZE) {
5400 acl = kmalloc(buflen, GFP_KERNEL);
5404 _copy_from_pages(acl->data, pages, pgbase, acl_len);
5406 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5413 nfs4_set_cached_acl(inode, acl);
5417 * The getxattr API returns the required buffer length when called with a
5418 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5419 * the required buf. On a NULL buf, we send a page of data to the server
5420 * guessing that the ACL request can be serviced by a page. If so, we cache
5421 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5422 * the cache. If not so, we throw away the page, and cache the required
5423 * length. The next getxattr call will then produce another round trip to
5424 * the server, this time with the input buf of the required size.
5426 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5428 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
5429 struct nfs_getaclargs args = {
5430 .fh = NFS_FH(inode),
5434 struct nfs_getaclres res = {
5437 struct rpc_message msg = {
5438 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5442 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
5443 int ret = -ENOMEM, i;
5445 if (npages > ARRAY_SIZE(pages))
5448 for (i = 0; i < npages; i++) {
5449 pages[i] = alloc_page(GFP_KERNEL);
5454 /* for decoding across pages */
5455 res.acl_scratch = alloc_page(GFP_KERNEL);
5456 if (!res.acl_scratch)
5459 args.acl_len = npages * PAGE_SIZE;
5461 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
5462 __func__, buf, buflen, npages, args.acl_len);
5463 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5464 &msg, &args.seq_args, &res.seq_res, 0);
5468 /* Handle the case where the passed-in buffer is too short */
5469 if (res.acl_flags & NFS4_ACL_TRUNC) {
5470 /* Did the user only issue a request for the acl length? */
5476 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
5478 if (res.acl_len > buflen) {
5482 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
5487 for (i = 0; i < npages; i++)
5489 __free_page(pages[i]);
5490 if (res.acl_scratch)
5491 __free_page(res.acl_scratch);
5495 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5497 struct nfs4_exception exception = { };
5500 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
5501 trace_nfs4_get_acl(inode, ret);
5504 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5505 } while (exception.retry);
5509 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5511 struct nfs_server *server = NFS_SERVER(inode);
5514 if (!nfs4_server_supports_acls(server))
5516 ret = nfs_revalidate_inode(server, inode);
5519 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5520 nfs_zap_acl_cache(inode);
5521 ret = nfs4_read_cached_acl(inode, buf, buflen);
5523 /* -ENOENT is returned if there is no ACL or if there is an ACL
5524 * but no cached acl data, just the acl length */
5526 return nfs4_get_acl_uncached(inode, buf, buflen);
5529 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5531 struct nfs_server *server = NFS_SERVER(inode);
5532 struct page *pages[NFS4ACL_MAXPAGES];
5533 struct nfs_setaclargs arg = {
5534 .fh = NFS_FH(inode),
5538 struct nfs_setaclres res;
5539 struct rpc_message msg = {
5540 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5544 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
5547 /* You can't remove system.nfs4_acl: */
5550 if (!nfs4_server_supports_acls(server))
5552 if (npages > ARRAY_SIZE(pages))
5554 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
5557 nfs4_inode_make_writeable(inode);
5558 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5561 * Free each page after tx, so the only ref left is
5562 * held by the network stack
5565 put_page(pages[i-1]);
5568 * Acl update can result in inode attribute update.
5569 * so mark the attribute cache invalid.
5571 spin_lock(&inode->i_lock);
5572 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
5573 | NFS_INO_INVALID_CTIME
5574 | NFS_INO_REVAL_FORCED;
5575 spin_unlock(&inode->i_lock);
5576 nfs_access_zap_cache(inode);
5577 nfs_zap_acl_cache(inode);
5581 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5583 struct nfs4_exception exception = { };
5586 err = __nfs4_proc_set_acl(inode, buf, buflen);
5587 trace_nfs4_set_acl(inode, err);
5588 if (err == -NFS4ERR_BADOWNER || err == -NFS4ERR_BADNAME) {
5590 * no need to retry since the kernel
5591 * isn't involved in encoding the ACEs.
5596 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5598 } while (exception.retry);
5602 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
5603 static int _nfs4_get_security_label(struct inode *inode, void *buf,
5606 struct nfs_server *server = NFS_SERVER(inode);
5607 struct nfs_fattr fattr;
5608 struct nfs4_label label = {0, 0, buflen, buf};
5610 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5611 struct nfs4_getattr_arg arg = {
5612 .fh = NFS_FH(inode),
5615 struct nfs4_getattr_res res = {
5620 struct rpc_message msg = {
5621 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
5627 nfs_fattr_init(&fattr);
5629 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
5632 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5637 static int nfs4_get_security_label(struct inode *inode, void *buf,
5640 struct nfs4_exception exception = { };
5643 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5647 err = _nfs4_get_security_label(inode, buf, buflen);
5648 trace_nfs4_get_security_label(inode, err);
5649 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5651 } while (exception.retry);
5655 static int _nfs4_do_set_security_label(struct inode *inode,
5656 struct nfs4_label *ilabel,
5657 struct nfs_fattr *fattr,
5658 struct nfs4_label *olabel)
5661 struct iattr sattr = {0};
5662 struct nfs_server *server = NFS_SERVER(inode);
5663 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5664 struct nfs_setattrargs arg = {
5665 .fh = NFS_FH(inode),
5671 struct nfs_setattrres res = {
5676 struct rpc_message msg = {
5677 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5683 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
5685 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5687 dprintk("%s failed: %d\n", __func__, status);
5692 static int nfs4_do_set_security_label(struct inode *inode,
5693 struct nfs4_label *ilabel,
5694 struct nfs_fattr *fattr,
5695 struct nfs4_label *olabel)
5697 struct nfs4_exception exception = { };
5701 err = _nfs4_do_set_security_label(inode, ilabel,
5703 trace_nfs4_set_security_label(inode, err);
5704 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5706 } while (exception.retry);
5711 nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
5713 struct nfs4_label ilabel, *olabel = NULL;
5714 struct nfs_fattr fattr;
5715 struct rpc_cred *cred;
5718 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5721 nfs_fattr_init(&fattr);
5725 ilabel.label = (char *)buf;
5726 ilabel.len = buflen;
5728 cred = rpc_lookup_cred();
5730 return PTR_ERR(cred);
5732 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5733 if (IS_ERR(olabel)) {
5734 status = -PTR_ERR(olabel);
5738 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5740 nfs_setsecurity(inode, &fattr, olabel);
5742 nfs4_label_free(olabel);
5747 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5750 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5751 nfs4_verifier *bootverf)
5755 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5756 /* An impossible timestamp guarantees this value
5757 * will never match a generated boot time. */
5758 verf[0] = cpu_to_be32(U32_MAX);
5759 verf[1] = cpu_to_be32(U32_MAX);
5761 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
5762 u64 ns = ktime_to_ns(nn->boot_time);
5764 verf[0] = cpu_to_be32(ns >> 32);
5765 verf[1] = cpu_to_be32(ns);
5767 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5771 nfs4_init_nonuniform_client_string(struct nfs_client *clp)
5776 if (clp->cl_owner_id != NULL)
5781 strlen(clp->cl_rpcclient->cl_nodename) +
5783 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5786 if (nfs4_client_id_uniquifier[0] != '\0')
5787 len += strlen(nfs4_client_id_uniquifier) + 1;
5788 if (len > NFS4_OPAQUE_LIMIT + 1)
5792 * Since this string is allocated at mount time, and held until the
5793 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5794 * about a memory-reclaim deadlock.
5796 str = kmalloc(len, GFP_KERNEL);
5801 if (nfs4_client_id_uniquifier[0] != '\0')
5802 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
5803 clp->cl_rpcclient->cl_nodename,
5804 nfs4_client_id_uniquifier,
5805 rpc_peeraddr2str(clp->cl_rpcclient,
5808 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
5809 clp->cl_rpcclient->cl_nodename,
5810 rpc_peeraddr2str(clp->cl_rpcclient,
5814 clp->cl_owner_id = str;
5819 nfs4_init_uniquifier_client_string(struct nfs_client *clp)
5824 len = 10 + 10 + 1 + 10 + 1 +
5825 strlen(nfs4_client_id_uniquifier) + 1 +
5826 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5828 if (len > NFS4_OPAQUE_LIMIT + 1)
5832 * Since this string is allocated at mount time, and held until the
5833 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5834 * about a memory-reclaim deadlock.
5836 str = kmalloc(len, GFP_KERNEL);
5840 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
5841 clp->rpc_ops->version, clp->cl_minorversion,
5842 nfs4_client_id_uniquifier,
5843 clp->cl_rpcclient->cl_nodename);
5844 clp->cl_owner_id = str;
5849 nfs4_init_uniform_client_string(struct nfs_client *clp)
5854 if (clp->cl_owner_id != NULL)
5857 if (nfs4_client_id_uniquifier[0] != '\0')
5858 return nfs4_init_uniquifier_client_string(clp);
5860 len = 10 + 10 + 1 + 10 + 1 +
5861 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5863 if (len > NFS4_OPAQUE_LIMIT + 1)
5867 * Since this string is allocated at mount time, and held until the
5868 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5869 * about a memory-reclaim deadlock.
5871 str = kmalloc(len, GFP_KERNEL);
5875 scnprintf(str, len, "Linux NFSv%u.%u %s",
5876 clp->rpc_ops->version, clp->cl_minorversion,
5877 clp->cl_rpcclient->cl_nodename);
5878 clp->cl_owner_id = str;
5883 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5884 * services. Advertise one based on the address family of the
5888 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5890 if (strchr(clp->cl_ipaddr, ':') != NULL)
5891 return scnprintf(buf, len, "tcp6");
5893 return scnprintf(buf, len, "tcp");
5896 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5898 struct nfs4_setclientid *sc = calldata;
5900 if (task->tk_status == 0)
5901 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5904 static const struct rpc_call_ops nfs4_setclientid_ops = {
5905 .rpc_call_done = nfs4_setclientid_done,
5909 * nfs4_proc_setclientid - Negotiate client ID
5910 * @clp: state data structure
5911 * @program: RPC program for NFSv4 callback service
5912 * @port: IP port number for NFS4 callback service
5913 * @cred: RPC credential to use for this call
5914 * @res: where to place the result
5916 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5918 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5919 unsigned short port, struct rpc_cred *cred,
5920 struct nfs4_setclientid_res *res)
5922 nfs4_verifier sc_verifier;
5923 struct nfs4_setclientid setclientid = {
5924 .sc_verifier = &sc_verifier,
5928 struct rpc_message msg = {
5929 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5930 .rpc_argp = &setclientid,
5934 struct rpc_task *task;
5935 struct rpc_task_setup task_setup_data = {
5936 .rpc_client = clp->cl_rpcclient,
5937 .rpc_message = &msg,
5938 .callback_ops = &nfs4_setclientid_ops,
5939 .callback_data = &setclientid,
5940 .flags = RPC_TASK_TIMEOUT,
5944 /* nfs_client_id4 */
5945 nfs4_init_boot_verifier(clp, &sc_verifier);
5947 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5948 status = nfs4_init_uniform_client_string(clp);
5950 status = nfs4_init_nonuniform_client_string(clp);
5956 setclientid.sc_netid_len =
5957 nfs4_init_callback_netid(clp,
5958 setclientid.sc_netid,
5959 sizeof(setclientid.sc_netid));
5960 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
5961 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
5962 clp->cl_ipaddr, port >> 8, port & 255);
5964 dprintk("NFS call setclientid auth=%s, '%s'\n",
5965 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5967 task = rpc_run_task(&task_setup_data);
5969 status = PTR_ERR(task);
5972 status = task->tk_status;
5973 if (setclientid.sc_cred) {
5974 kfree(clp->cl_acceptor);
5975 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5976 put_rpccred(setclientid.sc_cred);
5980 trace_nfs4_setclientid(clp, status);
5981 dprintk("NFS reply setclientid: %d\n", status);
5986 * nfs4_proc_setclientid_confirm - Confirm client ID
5987 * @clp: state data structure
5988 * @res: result of a previous SETCLIENTID
5989 * @cred: RPC credential to use for this call
5991 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5993 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
5994 struct nfs4_setclientid_res *arg,
5995 struct rpc_cred *cred)
5997 struct rpc_message msg = {
5998 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
6004 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
6005 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6007 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
6008 trace_nfs4_setclientid_confirm(clp, status);
6009 dprintk("NFS reply setclientid_confirm: %d\n", status);
6013 struct nfs4_delegreturndata {
6014 struct nfs4_delegreturnargs args;
6015 struct nfs4_delegreturnres res;
6017 nfs4_stateid stateid;
6018 unsigned long timestamp;
6020 struct nfs4_layoutreturn_args arg;
6021 struct nfs4_layoutreturn_res res;
6022 struct nfs4_xdr_opaque_data ld_private;
6026 struct nfs_fattr fattr;
6028 struct inode *inode;
6031 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6033 struct nfs4_delegreturndata *data = calldata;
6034 struct nfs4_exception exception = {
6035 .inode = data->inode,
6036 .stateid = &data->stateid,
6037 .task_is_privileged = data->args.seq_args.sa_privileged,
6040 if (!nfs4_sequence_done(task, &data->res.seq_res))
6043 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
6045 /* Handle Layoutreturn errors */
6046 if (data->args.lr_args && task->tk_status != 0) {
6047 switch(data->res.lr_ret) {
6049 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
6052 data->args.lr_args = NULL;
6053 data->res.lr_res = NULL;
6055 case -NFS4ERR_OLD_STATEID:
6056 if (nfs4_layoutreturn_refresh_stateid(&data->args.lr_args->stateid,
6057 &data->args.lr_args->range,
6061 case -NFS4ERR_ADMIN_REVOKED:
6062 case -NFS4ERR_DELEG_REVOKED:
6063 case -NFS4ERR_EXPIRED:
6064 case -NFS4ERR_BAD_STATEID:
6065 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
6066 case -NFS4ERR_WRONG_CRED:
6067 data->args.lr_args = NULL;
6068 data->res.lr_res = NULL;
6073 switch (task->tk_status) {
6075 renew_lease(data->res.server, data->timestamp);
6077 case -NFS4ERR_ADMIN_REVOKED:
6078 case -NFS4ERR_DELEG_REVOKED:
6079 case -NFS4ERR_EXPIRED:
6080 nfs4_free_revoked_stateid(data->res.server,
6082 task->tk_msg.rpc_cred);
6084 case -NFS4ERR_BAD_STATEID:
6085 case -NFS4ERR_STALE_STATEID:
6086 task->tk_status = 0;
6088 case -NFS4ERR_OLD_STATEID:
6089 if (nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6091 task->tk_status = 0;
6093 case -NFS4ERR_ACCESS:
6094 if (data->args.bitmask) {
6095 data->args.bitmask = NULL;
6096 data->res.fattr = NULL;
6101 task->tk_status = nfs4_async_handle_exception(task,
6102 data->res.server, task->tk_status,
6104 if (exception.retry)
6107 data->rpc_status = task->tk_status;
6110 data->res.lr_ret = 0;
6112 task->tk_status = 0;
6113 rpc_restart_call_prepare(task);
6116 static void nfs4_delegreturn_release(void *calldata)
6118 struct nfs4_delegreturndata *data = calldata;
6119 struct inode *inode = data->inode;
6123 pnfs_roc_release(&data->lr.arg, &data->lr.res,
6125 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
6126 nfs_iput_and_deactive(inode);
6131 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6133 struct nfs4_delegreturndata *d_data;
6134 struct pnfs_layout_hdr *lo;
6136 d_data = (struct nfs4_delegreturndata *)data;
6138 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task)) {
6139 nfs4_sequence_done(task, &d_data->res.seq_res);
6143 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6144 if (lo && !pnfs_layout_is_valid(lo)) {
6145 d_data->args.lr_args = NULL;
6146 d_data->res.lr_res = NULL;
6149 nfs4_setup_sequence(d_data->res.server->nfs_client,
6150 &d_data->args.seq_args,
6151 &d_data->res.seq_res,
6155 static const struct rpc_call_ops nfs4_delegreturn_ops = {
6156 .rpc_call_prepare = nfs4_delegreturn_prepare,
6157 .rpc_call_done = nfs4_delegreturn_done,
6158 .rpc_release = nfs4_delegreturn_release,
6161 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
6163 struct nfs4_delegreturndata *data;
6164 struct nfs_server *server = NFS_SERVER(inode);
6165 struct rpc_task *task;
6166 struct rpc_message msg = {
6167 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6170 struct rpc_task_setup task_setup_data = {
6171 .rpc_client = server->client,
6172 .rpc_message = &msg,
6173 .callback_ops = &nfs4_delegreturn_ops,
6174 .flags = RPC_TASK_ASYNC,
6178 data = kzalloc(sizeof(*data), GFP_NOFS);
6182 nfs4_state_protect(server->nfs_client,
6183 NFS_SP4_MACH_CRED_CLEANUP,
6184 &task_setup_data.rpc_client, &msg);
6186 data->args.fhandle = &data->fh;
6187 data->args.stateid = &data->stateid;
6188 data->args.bitmask = server->cache_consistency_bitmask;
6189 nfs_copy_fh(&data->fh, NFS_FH(inode));
6190 nfs4_stateid_copy(&data->stateid, stateid);
6191 data->res.fattr = &data->fattr;
6192 data->res.server = server;
6193 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
6194 data->lr.arg.ld_private = &data->lr.ld_private;
6195 nfs_fattr_init(data->res.fattr);
6196 data->timestamp = jiffies;
6197 data->rpc_status = 0;
6198 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
6199 data->inode = nfs_igrab_and_active(inode);
6202 data->args.lr_args = &data->lr.arg;
6203 data->res.lr_res = &data->lr.res;
6205 } else if (data->lr.roc) {
6206 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
6207 data->lr.roc = false;
6211 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1,
6214 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1,
6216 task_setup_data.callback_data = data;
6217 msg.rpc_argp = &data->args;
6218 msg.rpc_resp = &data->res;
6219 task = rpc_run_task(&task_setup_data);
6221 return PTR_ERR(task);
6224 status = rpc_wait_for_completion_task(task);
6227 status = data->rpc_status;
6233 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
6235 struct nfs_server *server = NFS_SERVER(inode);
6236 struct nfs4_exception exception = { };
6239 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
6240 trace_nfs4_delegreturn(inode, stateid, err);
6242 case -NFS4ERR_STALE_STATEID:
6243 case -NFS4ERR_EXPIRED:
6247 err = nfs4_handle_exception(server, err, &exception);
6248 } while (exception.retry);
6252 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6254 struct inode *inode = state->inode;
6255 struct nfs_server *server = NFS_SERVER(inode);
6256 struct nfs_client *clp = server->nfs_client;
6257 struct nfs_lockt_args arg = {
6258 .fh = NFS_FH(inode),
6261 struct nfs_lockt_res res = {
6264 struct rpc_message msg = {
6265 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
6268 .rpc_cred = state->owner->so_cred,
6270 struct nfs4_lock_state *lsp;
6273 arg.lock_owner.clientid = clp->cl_clientid;
6274 status = nfs4_set_lock_state(state, request);
6277 lsp = request->fl_u.nfs4_fl.owner;
6278 arg.lock_owner.id = lsp->ls_seqid.owner_id;
6279 arg.lock_owner.s_dev = server->s_dev;
6280 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6283 request->fl_type = F_UNLCK;
6285 case -NFS4ERR_DENIED:
6288 request->fl_ops->fl_release_private(request);
6289 request->fl_ops = NULL;
6294 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6296 struct nfs4_exception exception = { };
6300 err = _nfs4_proc_getlk(state, cmd, request);
6301 trace_nfs4_get_lock(request, state, cmd, err);
6302 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
6304 } while (exception.retry);
6308 struct nfs4_unlockdata {
6309 struct nfs_locku_args arg;
6310 struct nfs_locku_res res;
6311 struct nfs4_lock_state *lsp;
6312 struct nfs_open_context *ctx;
6313 struct nfs_lock_context *l_ctx;
6314 struct file_lock fl;
6315 struct nfs_server *server;
6316 unsigned long timestamp;
6319 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6320 struct nfs_open_context *ctx,
6321 struct nfs4_lock_state *lsp,
6322 struct nfs_seqid *seqid)
6324 struct nfs4_unlockdata *p;
6325 struct inode *inode = lsp->ls_state->inode;
6327 p = kzalloc(sizeof(*p), GFP_NOFS);
6330 p->arg.fh = NFS_FH(inode);
6332 p->arg.seqid = seqid;
6333 p->res.seqid = seqid;
6335 refcount_inc(&lsp->ls_count);
6336 /* Ensure we don't close file until we're done freeing locks! */
6337 p->ctx = get_nfs_open_context(ctx);
6338 p->l_ctx = nfs_get_lock_context(ctx);
6339 memcpy(&p->fl, fl, sizeof(p->fl));
6340 p->server = NFS_SERVER(inode);
6344 static void nfs4_locku_release_calldata(void *data)
6346 struct nfs4_unlockdata *calldata = data;
6347 nfs_free_seqid(calldata->arg.seqid);
6348 nfs4_put_lock_state(calldata->lsp);
6349 nfs_put_lock_context(calldata->l_ctx);
6350 put_nfs_open_context(calldata->ctx);
6354 static void nfs4_locku_done(struct rpc_task *task, void *data)
6356 struct nfs4_unlockdata *calldata = data;
6357 struct nfs4_exception exception = {
6358 .inode = calldata->lsp->ls_state->inode,
6359 .stateid = &calldata->arg.stateid,
6362 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6364 switch (task->tk_status) {
6366 renew_lease(calldata->server, calldata->timestamp);
6367 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
6368 if (nfs4_update_lock_stateid(calldata->lsp,
6369 &calldata->res.stateid))
6372 case -NFS4ERR_ADMIN_REVOKED:
6373 case -NFS4ERR_EXPIRED:
6374 nfs4_free_revoked_stateid(calldata->server,
6375 &calldata->arg.stateid,
6376 task->tk_msg.rpc_cred);
6378 case -NFS4ERR_BAD_STATEID:
6379 case -NFS4ERR_OLD_STATEID:
6380 case -NFS4ERR_STALE_STATEID:
6381 if (!nfs4_stateid_match(&calldata->arg.stateid,
6382 &calldata->lsp->ls_stateid))
6383 rpc_restart_call_prepare(task);
6386 task->tk_status = nfs4_async_handle_exception(task,
6387 calldata->server, task->tk_status,
6389 if (exception.retry)
6390 rpc_restart_call_prepare(task);
6392 nfs_release_seqid(calldata->arg.seqid);
6395 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
6397 struct nfs4_unlockdata *calldata = data;
6399 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6400 nfs_async_iocounter_wait(task, calldata->l_ctx))
6403 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
6405 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
6406 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
6407 /* Note: exit _without_ running nfs4_locku_done */
6410 calldata->timestamp = jiffies;
6411 if (nfs4_setup_sequence(calldata->server->nfs_client,
6412 &calldata->arg.seq_args,
6413 &calldata->res.seq_res,
6415 nfs_release_seqid(calldata->arg.seqid);
6418 task->tk_action = NULL;
6420 nfs4_sequence_done(task, &calldata->res.seq_res);
6423 static const struct rpc_call_ops nfs4_locku_ops = {
6424 .rpc_call_prepare = nfs4_locku_prepare,
6425 .rpc_call_done = nfs4_locku_done,
6426 .rpc_release = nfs4_locku_release_calldata,
6429 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6430 struct nfs_open_context *ctx,
6431 struct nfs4_lock_state *lsp,
6432 struct nfs_seqid *seqid)
6434 struct nfs4_unlockdata *data;
6435 struct rpc_message msg = {
6436 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6437 .rpc_cred = ctx->cred,
6439 struct rpc_task_setup task_setup_data = {
6440 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
6441 .rpc_message = &msg,
6442 .callback_ops = &nfs4_locku_ops,
6443 .workqueue = nfsiod_workqueue,
6444 .flags = RPC_TASK_ASYNC,
6447 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6448 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6450 /* Ensure this is an unlock - when canceling a lock, the
6451 * canceled lock is passed in, and it won't be an unlock.
6453 fl->fl_type = F_UNLCK;
6454 if (fl->fl_flags & FL_CLOSE)
6455 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
6457 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6459 nfs_free_seqid(seqid);
6460 return ERR_PTR(-ENOMEM);
6463 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
6464 msg.rpc_argp = &data->arg;
6465 msg.rpc_resp = &data->res;
6466 task_setup_data.callback_data = data;
6467 return rpc_run_task(&task_setup_data);
6470 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6472 struct inode *inode = state->inode;
6473 struct nfs4_state_owner *sp = state->owner;
6474 struct nfs_inode *nfsi = NFS_I(inode);
6475 struct nfs_seqid *seqid;
6476 struct nfs4_lock_state *lsp;
6477 struct rpc_task *task;
6478 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6480 unsigned char fl_flags = request->fl_flags;
6482 status = nfs4_set_lock_state(state, request);
6483 /* Unlock _before_ we do the RPC call */
6484 request->fl_flags |= FL_EXISTS;
6485 /* Exclude nfs_delegation_claim_locks() */
6486 mutex_lock(&sp->so_delegreturn_mutex);
6487 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
6488 down_read(&nfsi->rwsem);
6489 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
6490 up_read(&nfsi->rwsem);
6491 mutex_unlock(&sp->so_delegreturn_mutex);
6494 up_read(&nfsi->rwsem);
6495 mutex_unlock(&sp->so_delegreturn_mutex);
6498 /* Is this a delegated lock? */
6499 lsp = request->fl_u.nfs4_fl.owner;
6500 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6502 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6503 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
6507 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
6508 status = PTR_ERR(task);
6511 status = rpc_wait_for_completion_task(task);
6514 request->fl_flags = fl_flags;
6515 trace_nfs4_unlock(request, state, F_SETLK, status);
6519 struct nfs4_lockdata {
6520 struct nfs_lock_args arg;
6521 struct nfs_lock_res res;
6522 struct nfs4_lock_state *lsp;
6523 struct nfs_open_context *ctx;
6524 struct file_lock fl;
6525 unsigned long timestamp;
6528 struct nfs_server *server;
6531 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
6532 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6535 struct nfs4_lockdata *p;
6536 struct inode *inode = lsp->ls_state->inode;
6537 struct nfs_server *server = NFS_SERVER(inode);
6538 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6540 p = kzalloc(sizeof(*p), gfp_mask);
6544 p->arg.fh = NFS_FH(inode);
6546 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
6547 if (IS_ERR(p->arg.open_seqid))
6549 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6550 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
6551 if (IS_ERR(p->arg.lock_seqid))
6552 goto out_free_seqid;
6553 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
6554 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
6555 p->arg.lock_owner.s_dev = server->s_dev;
6556 p->res.lock_seqid = p->arg.lock_seqid;
6559 refcount_inc(&lsp->ls_count);
6560 p->ctx = get_nfs_open_context(ctx);
6561 memcpy(&p->fl, fl, sizeof(p->fl));
6564 nfs_free_seqid(p->arg.open_seqid);
6570 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6572 struct nfs4_lockdata *data = calldata;
6573 struct nfs4_state *state = data->lsp->ls_state;
6575 dprintk("%s: begin!\n", __func__);
6576 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
6578 /* Do we need to do an open_to_lock_owner? */
6579 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
6580 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
6581 goto out_release_lock_seqid;
6583 nfs4_stateid_copy(&data->arg.open_stateid,
6584 &state->open_stateid);
6585 data->arg.new_lock_owner = 1;
6586 data->res.open_seqid = data->arg.open_seqid;
6588 data->arg.new_lock_owner = 0;
6589 nfs4_stateid_copy(&data->arg.lock_stateid,
6590 &data->lsp->ls_stateid);
6592 if (!nfs4_valid_open_stateid(state)) {
6593 data->rpc_status = -EBADF;
6594 task->tk_action = NULL;
6595 goto out_release_open_seqid;
6597 data->timestamp = jiffies;
6598 if (nfs4_setup_sequence(data->server->nfs_client,
6599 &data->arg.seq_args,
6603 out_release_open_seqid:
6604 nfs_release_seqid(data->arg.open_seqid);
6605 out_release_lock_seqid:
6606 nfs_release_seqid(data->arg.lock_seqid);
6608 nfs4_sequence_done(task, &data->res.seq_res);
6609 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
6612 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6614 struct nfs4_lockdata *data = calldata;
6615 struct nfs4_lock_state *lsp = data->lsp;
6617 dprintk("%s: begin!\n", __func__);
6619 if (!nfs4_sequence_done(task, &data->res.seq_res))
6622 data->rpc_status = task->tk_status;
6623 switch (task->tk_status) {
6625 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
6627 if (data->arg.new_lock && !data->cancelled) {
6628 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
6629 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
6632 if (data->arg.new_lock_owner != 0) {
6633 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6634 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6635 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6636 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6639 case -NFS4ERR_BAD_STATEID:
6640 case -NFS4ERR_OLD_STATEID:
6641 case -NFS4ERR_STALE_STATEID:
6642 case -NFS4ERR_EXPIRED:
6643 if (data->arg.new_lock_owner != 0) {
6644 if (!nfs4_stateid_match(&data->arg.open_stateid,
6645 &lsp->ls_state->open_stateid))
6647 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6652 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
6655 if (!data->cancelled)
6656 rpc_restart_call_prepare(task);
6660 static void nfs4_lock_release(void *calldata)
6662 struct nfs4_lockdata *data = calldata;
6664 dprintk("%s: begin!\n", __func__);
6665 nfs_free_seqid(data->arg.open_seqid);
6666 if (data->cancelled && data->rpc_status == 0) {
6667 struct rpc_task *task;
6668 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6669 data->arg.lock_seqid);
6671 rpc_put_task_async(task);
6672 dprintk("%s: cancelling lock!\n", __func__);
6674 nfs_free_seqid(data->arg.lock_seqid);
6675 nfs4_put_lock_state(data->lsp);
6676 put_nfs_open_context(data->ctx);
6678 dprintk("%s: done!\n", __func__);
6681 static const struct rpc_call_ops nfs4_lock_ops = {
6682 .rpc_call_prepare = nfs4_lock_prepare,
6683 .rpc_call_done = nfs4_lock_done,
6684 .rpc_release = nfs4_lock_release,
6687 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6690 case -NFS4ERR_ADMIN_REVOKED:
6691 case -NFS4ERR_EXPIRED:
6692 case -NFS4ERR_BAD_STATEID:
6693 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6694 if (new_lock_owner != 0 ||
6695 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
6696 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
6698 case -NFS4ERR_STALE_STATEID:
6699 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6700 nfs4_schedule_lease_recovery(server->nfs_client);
6704 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
6706 struct nfs4_lockdata *data;
6707 struct rpc_task *task;
6708 struct rpc_message msg = {
6709 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6710 .rpc_cred = state->owner->so_cred,
6712 struct rpc_task_setup task_setup_data = {
6713 .rpc_client = NFS_CLIENT(state->inode),
6714 .rpc_message = &msg,
6715 .callback_ops = &nfs4_lock_ops,
6716 .workqueue = nfsiod_workqueue,
6717 .flags = RPC_TASK_ASYNC,
6721 dprintk("%s: begin!\n", __func__);
6722 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
6723 fl->fl_u.nfs4_fl.owner,
6724 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
6728 data->arg.block = 1;
6729 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
6730 recovery_type > NFS_LOCK_NEW);
6731 msg.rpc_argp = &data->arg;
6732 msg.rpc_resp = &data->res;
6733 task_setup_data.callback_data = data;
6734 if (recovery_type > NFS_LOCK_NEW) {
6735 if (recovery_type == NFS_LOCK_RECLAIM)
6736 data->arg.reclaim = NFS_LOCK_RECLAIM;
6738 data->arg.new_lock = 1;
6739 task = rpc_run_task(&task_setup_data);
6741 return PTR_ERR(task);
6742 ret = rpc_wait_for_completion_task(task);
6744 ret = data->rpc_status;
6746 nfs4_handle_setlk_error(data->server, data->lsp,
6747 data->arg.new_lock_owner, ret);
6749 data->cancelled = true;
6750 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
6752 dprintk("%s: done, ret = %d!\n", __func__, ret);
6756 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6758 struct nfs_server *server = NFS_SERVER(state->inode);
6759 struct nfs4_exception exception = {
6760 .inode = state->inode,
6765 /* Cache the lock if possible... */
6766 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6768 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
6769 if (err != -NFS4ERR_DELAY)
6771 nfs4_handle_exception(server, err, &exception);
6772 } while (exception.retry);
6776 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6778 struct nfs_server *server = NFS_SERVER(state->inode);
6779 struct nfs4_exception exception = {
6780 .inode = state->inode,
6784 err = nfs4_set_lock_state(state, request);
6787 if (!recover_lost_locks) {
6788 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6792 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6794 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
6798 case -NFS4ERR_GRACE:
6799 case -NFS4ERR_DELAY:
6800 nfs4_handle_exception(server, err, &exception);
6803 } while (exception.retry);
6808 #if defined(CONFIG_NFS_V4_1)
6809 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6811 struct nfs4_lock_state *lsp;
6814 status = nfs4_set_lock_state(state, request);
6817 lsp = request->fl_u.nfs4_fl.owner;
6818 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6819 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6821 return nfs4_lock_expired(state, request);
6825 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6827 struct nfs_inode *nfsi = NFS_I(state->inode);
6828 struct nfs4_state_owner *sp = state->owner;
6829 unsigned char fl_flags = request->fl_flags;
6832 request->fl_flags |= FL_ACCESS;
6833 status = locks_lock_inode_wait(state->inode, request);
6836 mutex_lock(&sp->so_delegreturn_mutex);
6837 down_read(&nfsi->rwsem);
6838 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
6839 /* Yes: cache locks! */
6840 /* ...but avoid races with delegation recall... */
6841 request->fl_flags = fl_flags & ~FL_SLEEP;
6842 status = locks_lock_inode_wait(state->inode, request);
6843 up_read(&nfsi->rwsem);
6844 mutex_unlock(&sp->so_delegreturn_mutex);
6847 up_read(&nfsi->rwsem);
6848 mutex_unlock(&sp->so_delegreturn_mutex);
6849 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
6851 request->fl_flags = fl_flags;
6855 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6857 struct nfs4_exception exception = {
6859 .inode = state->inode,
6864 err = _nfs4_proc_setlk(state, cmd, request);
6865 if (err == -NFS4ERR_DENIED)
6867 err = nfs4_handle_exception(NFS_SERVER(state->inode),
6869 } while (exception.retry);
6873 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6874 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6877 nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6878 struct file_lock *request)
6880 int status = -ERESTARTSYS;
6881 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6883 while(!signalled()) {
6884 status = nfs4_proc_setlk(state, cmd, request);
6885 if ((status != -EAGAIN) || IS_SETLK(cmd))
6887 freezable_schedule_timeout_interruptible(timeout);
6889 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6890 status = -ERESTARTSYS;
6895 #ifdef CONFIG_NFS_V4_1
6896 struct nfs4_lock_waiter {
6897 struct task_struct *task;
6898 struct inode *inode;
6899 struct nfs_lowner *owner;
6903 nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
6906 struct nfs4_lock_waiter *waiter = wait->private;
6908 /* NULL key means to wake up everyone */
6910 struct cb_notify_lock_args *cbnl = key;
6911 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6912 *wowner = waiter->owner;
6914 /* Only wake if the callback was for the same owner. */
6915 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
6918 /* Make sure it's for the right inode */
6919 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6923 /* override "private" so we can use default_wake_function */
6924 wait->private = waiter->task;
6925 ret = woken_wake_function(wait, mode, flags, key);
6927 list_del_init(&wait->entry);
6928 wait->private = waiter;
6933 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6935 int status = -ERESTARTSYS;
6936 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6937 struct nfs_server *server = NFS_SERVER(state->inode);
6938 struct nfs_client *clp = server->nfs_client;
6939 wait_queue_head_t *q = &clp->cl_lock_waitq;
6940 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6941 .id = lsp->ls_seqid.owner_id,
6942 .s_dev = server->s_dev };
6943 struct nfs4_lock_waiter waiter = { .task = current,
6944 .inode = state->inode,
6946 wait_queue_entry_t wait;
6948 /* Don't bother with waitqueue if we don't expect a callback */
6949 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6950 return nfs4_retry_setlk_simple(state, cmd, request);
6953 wait.private = &waiter;
6954 wait.func = nfs4_wake_lock_waiter;
6956 while(!signalled()) {
6957 add_wait_queue(q, &wait);
6958 status = nfs4_proc_setlk(state, cmd, request);
6959 if ((status != -EAGAIN) || IS_SETLK(cmd)) {
6960 finish_wait(q, &wait);
6964 status = -ERESTARTSYS;
6965 freezer_do_not_count();
6966 wait_woken(&wait, TASK_INTERRUPTIBLE, NFS4_LOCK_MAXTIMEOUT);
6968 finish_wait(q, &wait);
6973 #else /* !CONFIG_NFS_V4_1 */
6975 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6977 return nfs4_retry_setlk_simple(state, cmd, request);
6982 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6984 struct nfs_open_context *ctx;
6985 struct nfs4_state *state;
6988 /* verify open state */
6989 ctx = nfs_file_open_context(filp);
6992 if (IS_GETLK(cmd)) {
6994 return nfs4_proc_getlk(state, F_GETLK, request);
6998 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
7001 if (request->fl_type == F_UNLCK) {
7003 return nfs4_proc_unlck(state, cmd, request);
7010 if ((request->fl_flags & FL_POSIX) &&
7011 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
7015 * Don't rely on the VFS having checked the file open mode,
7016 * since it won't do this for flock() locks.
7018 switch (request->fl_type) {
7020 if (!(filp->f_mode & FMODE_READ))
7024 if (!(filp->f_mode & FMODE_WRITE))
7028 status = nfs4_set_lock_state(state, request);
7032 return nfs4_retry_setlk(state, cmd, request);
7035 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
7037 struct nfs_server *server = NFS_SERVER(state->inode);
7040 err = nfs4_set_lock_state(state, fl);
7044 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
7045 if (err != -NFS4ERR_DELAY)
7048 } while (err == -NFS4ERR_DELAY);
7049 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
7052 struct nfs_release_lockowner_data {
7053 struct nfs4_lock_state *lsp;
7054 struct nfs_server *server;
7055 struct nfs_release_lockowner_args args;
7056 struct nfs_release_lockowner_res res;
7057 unsigned long timestamp;
7060 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7062 struct nfs_release_lockowner_data *data = calldata;
7063 struct nfs_server *server = data->server;
7064 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7065 &data->res.seq_res, task);
7066 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7067 data->timestamp = jiffies;
7070 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7072 struct nfs_release_lockowner_data *data = calldata;
7073 struct nfs_server *server = data->server;
7075 nfs40_sequence_done(task, &data->res.seq_res);
7077 switch (task->tk_status) {
7079 renew_lease(server, data->timestamp);
7081 case -NFS4ERR_STALE_CLIENTID:
7082 case -NFS4ERR_EXPIRED:
7083 nfs4_schedule_lease_recovery(server->nfs_client);
7085 case -NFS4ERR_LEASE_MOVED:
7086 case -NFS4ERR_DELAY:
7087 if (nfs4_async_handle_error(task, server,
7088 NULL, NULL) == -EAGAIN)
7089 rpc_restart_call_prepare(task);
7093 static void nfs4_release_lockowner_release(void *calldata)
7095 struct nfs_release_lockowner_data *data = calldata;
7096 nfs4_free_lock_state(data->server, data->lsp);
7100 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
7101 .rpc_call_prepare = nfs4_release_lockowner_prepare,
7102 .rpc_call_done = nfs4_release_lockowner_done,
7103 .rpc_release = nfs4_release_lockowner_release,
7107 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
7109 struct nfs_release_lockowner_data *data;
7110 struct rpc_message msg = {
7111 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7114 if (server->nfs_client->cl_mvops->minor_version != 0)
7117 data = kmalloc(sizeof(*data), GFP_NOFS);
7121 data->server = server;
7122 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7123 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7124 data->args.lock_owner.s_dev = server->s_dev;
7126 msg.rpc_argp = &data->args;
7127 msg.rpc_resp = &data->res;
7128 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
7129 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
7132 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7134 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
7135 struct dentry *unused, struct inode *inode,
7136 const char *key, const void *buf,
7137 size_t buflen, int flags)
7139 return nfs4_proc_set_acl(inode, buf, buflen);
7142 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
7143 struct dentry *unused, struct inode *inode,
7144 const char *key, void *buf, size_t buflen)
7146 return nfs4_proc_get_acl(inode, buf, buflen);
7149 static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
7151 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
7154 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
7156 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
7157 struct dentry *unused, struct inode *inode,
7158 const char *key, const void *buf,
7159 size_t buflen, int flags)
7161 if (security_ismaclabel(key))
7162 return nfs4_set_security_label(inode, buf, buflen);
7167 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
7168 struct dentry *unused, struct inode *inode,
7169 const char *key, void *buf, size_t buflen)
7171 if (security_ismaclabel(key))
7172 return nfs4_get_security_label(inode, buf, buflen);
7177 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7181 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7182 len = security_inode_listsecurity(inode, list, list_len);
7183 if (list_len && len > list_len)
7189 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7190 .prefix = XATTR_SECURITY_PREFIX,
7191 .get = nfs4_xattr_get_nfs4_label,
7192 .set = nfs4_xattr_set_nfs4_label,
7198 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7206 * nfs_fhget will use either the mounted_on_fileid or the fileid
7208 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7210 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7211 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7212 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
7213 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
7216 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
7217 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
7218 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7222 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7223 const struct qstr *name,
7224 struct nfs4_fs_locations *fs_locations,
7227 struct nfs_server *server = NFS_SERVER(dir);
7229 struct nfs4_fs_locations_arg args = {
7230 .dir_fh = NFS_FH(dir),
7235 struct nfs4_fs_locations_res res = {
7236 .fs_locations = fs_locations,
7238 struct rpc_message msg = {
7239 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7245 dprintk("%s: start\n", __func__);
7247 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7248 bitmask[1] = nfs4_fattr_bitmap[1];
7250 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7251 * is not supported */
7252 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
7253 bitmask[0] &= ~FATTR4_WORD0_FILEID;
7255 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
7257 nfs_fattr_init(&fs_locations->fattr);
7258 fs_locations->server = server;
7259 fs_locations->nlocations = 0;
7260 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
7261 dprintk("%s: returned status = %d\n", __func__, status);
7265 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7266 const struct qstr *name,
7267 struct nfs4_fs_locations *fs_locations,
7270 struct nfs4_exception exception = { };
7273 err = _nfs4_proc_fs_locations(client, dir, name,
7274 fs_locations, page);
7275 trace_nfs4_get_fs_locations(dir, name, err);
7276 err = nfs4_handle_exception(NFS_SERVER(dir), err,
7278 } while (exception.retry);
7283 * This operation also signals the server that this client is
7284 * performing migration recovery. The server can stop returning
7285 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7286 * appended to this compound to identify the client ID which is
7287 * performing recovery.
7289 static int _nfs40_proc_get_locations(struct inode *inode,
7290 struct nfs4_fs_locations *locations,
7291 struct page *page, struct rpc_cred *cred)
7293 struct nfs_server *server = NFS_SERVER(inode);
7294 struct rpc_clnt *clnt = server->client;
7296 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7298 struct nfs4_fs_locations_arg args = {
7299 .clientid = server->nfs_client->cl_clientid,
7300 .fh = NFS_FH(inode),
7303 .migration = 1, /* skip LOOKUP */
7304 .renew = 1, /* append RENEW */
7306 struct nfs4_fs_locations_res res = {
7307 .fs_locations = locations,
7311 struct rpc_message msg = {
7312 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7317 unsigned long now = jiffies;
7320 nfs_fattr_init(&locations->fattr);
7321 locations->server = server;
7322 locations->nlocations = 0;
7324 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7325 status = nfs4_call_sync_sequence(clnt, server, &msg,
7326 &args.seq_args, &res.seq_res);
7330 renew_lease(server, now);
7334 #ifdef CONFIG_NFS_V4_1
7337 * This operation also signals the server that this client is
7338 * performing migration recovery. The server can stop asserting
7339 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7340 * performing this operation is identified in the SEQUENCE
7341 * operation in this compound.
7343 * When the client supports GETATTR(fs_locations_info), it can
7344 * be plumbed in here.
7346 static int _nfs41_proc_get_locations(struct inode *inode,
7347 struct nfs4_fs_locations *locations,
7348 struct page *page, struct rpc_cred *cred)
7350 struct nfs_server *server = NFS_SERVER(inode);
7351 struct rpc_clnt *clnt = server->client;
7353 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7355 struct nfs4_fs_locations_arg args = {
7356 .fh = NFS_FH(inode),
7359 .migration = 1, /* skip LOOKUP */
7361 struct nfs4_fs_locations_res res = {
7362 .fs_locations = locations,
7365 struct rpc_message msg = {
7366 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7373 nfs_fattr_init(&locations->fattr);
7374 locations->server = server;
7375 locations->nlocations = 0;
7377 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7378 status = nfs4_call_sync_sequence(clnt, server, &msg,
7379 &args.seq_args, &res.seq_res);
7380 if (status == NFS4_OK &&
7381 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7382 status = -NFS4ERR_LEASE_MOVED;
7386 #endif /* CONFIG_NFS_V4_1 */
7389 * nfs4_proc_get_locations - discover locations for a migrated FSID
7390 * @inode: inode on FSID that is migrating
7391 * @locations: result of query
7393 * @cred: credential to use for this operation
7395 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7396 * operation failed, or a negative errno if a local error occurred.
7398 * On success, "locations" is filled in, but if the server has
7399 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7402 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7403 * from this client that require migration recovery.
7405 int nfs4_proc_get_locations(struct inode *inode,
7406 struct nfs4_fs_locations *locations,
7407 struct page *page, struct rpc_cred *cred)
7409 struct nfs_server *server = NFS_SERVER(inode);
7410 struct nfs_client *clp = server->nfs_client;
7411 const struct nfs4_mig_recovery_ops *ops =
7412 clp->cl_mvops->mig_recovery_ops;
7413 struct nfs4_exception exception = { };
7416 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7417 (unsigned long long)server->fsid.major,
7418 (unsigned long long)server->fsid.minor,
7420 nfs_display_fhandle(NFS_FH(inode), __func__);
7423 status = ops->get_locations(inode, locations, page, cred);
7424 if (status != -NFS4ERR_DELAY)
7426 nfs4_handle_exception(server, status, &exception);
7427 } while (exception.retry);
7432 * This operation also signals the server that this client is
7433 * performing "lease moved" recovery. The server can stop
7434 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7435 * is appended to this compound to identify the client ID which is
7436 * performing recovery.
7438 static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7440 struct nfs_server *server = NFS_SERVER(inode);
7441 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7442 struct rpc_clnt *clnt = server->client;
7443 struct nfs4_fsid_present_arg args = {
7444 .fh = NFS_FH(inode),
7445 .clientid = clp->cl_clientid,
7446 .renew = 1, /* append RENEW */
7448 struct nfs4_fsid_present_res res = {
7451 struct rpc_message msg = {
7452 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7457 unsigned long now = jiffies;
7460 res.fh = nfs_alloc_fhandle();
7464 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7465 status = nfs4_call_sync_sequence(clnt, server, &msg,
7466 &args.seq_args, &res.seq_res);
7467 nfs_free_fhandle(res.fh);
7471 do_renew_lease(clp, now);
7475 #ifdef CONFIG_NFS_V4_1
7478 * This operation also signals the server that this client is
7479 * performing "lease moved" recovery. The server can stop asserting
7480 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7481 * this operation is identified in the SEQUENCE operation in this
7484 static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7486 struct nfs_server *server = NFS_SERVER(inode);
7487 struct rpc_clnt *clnt = server->client;
7488 struct nfs4_fsid_present_arg args = {
7489 .fh = NFS_FH(inode),
7491 struct nfs4_fsid_present_res res = {
7493 struct rpc_message msg = {
7494 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7501 res.fh = nfs_alloc_fhandle();
7505 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7506 status = nfs4_call_sync_sequence(clnt, server, &msg,
7507 &args.seq_args, &res.seq_res);
7508 nfs_free_fhandle(res.fh);
7509 if (status == NFS4_OK &&
7510 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7511 status = -NFS4ERR_LEASE_MOVED;
7515 #endif /* CONFIG_NFS_V4_1 */
7518 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7519 * @inode: inode on FSID to check
7520 * @cred: credential to use for this operation
7522 * Server indicates whether the FSID is present, moved, or not
7523 * recognized. This operation is necessary to clear a LEASE_MOVED
7524 * condition for this client ID.
7526 * Returns NFS4_OK if the FSID is present on this server,
7527 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7528 * NFS4ERR code if some error occurred on the server, or a
7529 * negative errno if a local failure occurred.
7531 int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7533 struct nfs_server *server = NFS_SERVER(inode);
7534 struct nfs_client *clp = server->nfs_client;
7535 const struct nfs4_mig_recovery_ops *ops =
7536 clp->cl_mvops->mig_recovery_ops;
7537 struct nfs4_exception exception = { };
7540 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7541 (unsigned long long)server->fsid.major,
7542 (unsigned long long)server->fsid.minor,
7544 nfs_display_fhandle(NFS_FH(inode), __func__);
7547 status = ops->fsid_present(inode, cred);
7548 if (status != -NFS4ERR_DELAY)
7550 nfs4_handle_exception(server, status, &exception);
7551 } while (exception.retry);
7556 * If 'use_integrity' is true and the state managment nfs_client
7557 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7558 * and the machine credential as per RFC3530bis and RFC5661 Security
7559 * Considerations sections. Otherwise, just use the user cred with the
7560 * filesystem's rpc_client.
7562 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
7565 struct nfs4_secinfo_arg args = {
7566 .dir_fh = NFS_FH(dir),
7569 struct nfs4_secinfo_res res = {
7572 struct rpc_message msg = {
7573 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7577 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
7578 struct rpc_cred *cred = NULL;
7580 if (use_integrity) {
7581 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
7582 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7583 msg.rpc_cred = cred;
7586 dprintk("NFS call secinfo %s\n", name->name);
7588 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7589 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7591 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7593 dprintk("NFS reply secinfo: %d\n", status);
7601 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7602 struct nfs4_secinfo_flavors *flavors)
7604 struct nfs4_exception exception = { };
7607 err = -NFS4ERR_WRONGSEC;
7609 /* try to use integrity protection with machine cred */
7610 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7611 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7614 * if unable to use integrity protection, or SECINFO with
7615 * integrity protection returns NFS4ERR_WRONGSEC (which is
7616 * disallowed by spec, but exists in deployed servers) use
7617 * the current filesystem's rpc_client and the user cred.
7619 if (err == -NFS4ERR_WRONGSEC)
7620 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7622 trace_nfs4_secinfo(dir, name, err);
7623 err = nfs4_handle_exception(NFS_SERVER(dir), err,
7625 } while (exception.retry);
7629 #ifdef CONFIG_NFS_V4_1
7631 * Check the exchange flags returned by the server for invalid flags, having
7632 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7635 static int nfs4_check_cl_exchange_flags(u32 flags, u32 version)
7637 if (version >= 2 && (flags & ~EXCHGID4_2_FLAG_MASK_R))
7639 else if (version < 2 && (flags & ~EXCHGID4_FLAG_MASK_R))
7641 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7642 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7644 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7648 return -NFS4ERR_INVAL;
7652 nfs41_same_server_scope(struct nfs41_server_scope *a,
7653 struct nfs41_server_scope *b)
7655 if (a->server_scope_sz != b->server_scope_sz)
7657 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
7661 nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7665 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7666 .rpc_call_done = nfs4_bind_one_conn_to_session_done,
7670 * nfs4_proc_bind_one_conn_to_session()
7672 * The 4.1 client currently uses the same TCP connection for the
7673 * fore and backchannel.
7676 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7677 struct rpc_xprt *xprt,
7678 struct nfs_client *clp,
7679 struct rpc_cred *cred)
7682 struct nfs41_bind_conn_to_session_args args = {
7684 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7686 struct nfs41_bind_conn_to_session_res res;
7687 struct rpc_message msg = {
7689 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
7694 struct rpc_task_setup task_setup_data = {
7697 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
7698 .rpc_message = &msg,
7699 .flags = RPC_TASK_TIMEOUT,
7701 struct rpc_task *task;
7703 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7704 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7705 args.dir = NFS4_CDFC4_FORE;
7707 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7708 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7709 args.dir = NFS4_CDFC4_FORE;
7711 task = rpc_run_task(&task_setup_data);
7712 if (!IS_ERR(task)) {
7713 status = task->tk_status;
7716 status = PTR_ERR(task);
7717 trace_nfs4_bind_conn_to_session(clp, status);
7719 if (memcmp(res.sessionid.data,
7720 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7721 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7724 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
7725 dprintk("NFS: %s: Unexpected direction from server\n",
7729 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
7730 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7739 struct rpc_bind_conn_calldata {
7740 struct nfs_client *clp;
7741 struct rpc_cred *cred;
7745 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7746 struct rpc_xprt *xprt,
7749 struct rpc_bind_conn_calldata *p = calldata;
7751 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7754 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7756 struct rpc_bind_conn_calldata data = {
7760 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7761 nfs4_proc_bind_conn_to_session_callback, &data);
7765 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7766 * and operations we'd like to see to enable certain features in the allow map
7768 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7769 .how = SP4_MACH_CRED,
7770 .enforce.u.words = {
7771 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7772 1 << (OP_EXCHANGE_ID - 32) |
7773 1 << (OP_CREATE_SESSION - 32) |
7774 1 << (OP_DESTROY_SESSION - 32) |
7775 1 << (OP_DESTROY_CLIENTID - 32)
7778 [0] = 1 << (OP_CLOSE) |
7779 1 << (OP_OPEN_DOWNGRADE) |
7781 1 << (OP_DELEGRETURN) |
7783 [1] = 1 << (OP_SECINFO - 32) |
7784 1 << (OP_SECINFO_NO_NAME - 32) |
7785 1 << (OP_LAYOUTRETURN - 32) |
7786 1 << (OP_TEST_STATEID - 32) |
7787 1 << (OP_FREE_STATEID - 32) |
7788 1 << (OP_WRITE - 32)
7793 * Select the state protection mode for client `clp' given the server results
7794 * from exchange_id in `sp'.
7796 * Returns 0 on success, negative errno otherwise.
7798 static int nfs4_sp4_select_mode(struct nfs_client *clp,
7799 struct nfs41_state_protection *sp)
7801 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7802 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7803 1 << (OP_EXCHANGE_ID - 32) |
7804 1 << (OP_CREATE_SESSION - 32) |
7805 1 << (OP_DESTROY_SESSION - 32) |
7806 1 << (OP_DESTROY_CLIENTID - 32)
7808 unsigned long flags = 0;
7812 if (sp->how == SP4_MACH_CRED) {
7813 /* Print state protect result */
7814 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7815 for (i = 0; i <= LAST_NFS4_OP; i++) {
7816 if (test_bit(i, sp->enforce.u.longs))
7817 dfprintk(MOUNT, " enforce op %d\n", i);
7818 if (test_bit(i, sp->allow.u.longs))
7819 dfprintk(MOUNT, " allow op %d\n", i);
7822 /* make sure nothing is on enforce list that isn't supported */
7823 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7824 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7825 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7832 * Minimal mode - state operations are allowed to use machine
7833 * credential. Note this already happens by default, so the
7834 * client doesn't have to do anything more than the negotiation.
7836 * NOTE: we don't care if EXCHANGE_ID is in the list -
7837 * we're already using the machine cred for exchange_id
7838 * and will never use a different cred.
7840 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7841 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7842 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7843 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7844 dfprintk(MOUNT, "sp4_mach_cred:\n");
7845 dfprintk(MOUNT, " minimal mode enabled\n");
7846 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
7848 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7853 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
7854 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7855 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
7856 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7857 dfprintk(MOUNT, " cleanup mode enabled\n");
7858 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
7861 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7862 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7863 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
7866 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7867 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7868 dfprintk(MOUNT, " secinfo mode enabled\n");
7869 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
7872 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7873 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7874 dfprintk(MOUNT, " stateid mode enabled\n");
7875 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
7878 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7879 dfprintk(MOUNT, " write mode enabled\n");
7880 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
7883 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7884 dfprintk(MOUNT, " commit mode enabled\n");
7885 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
7889 clp->cl_sp4_flags = flags;
7893 struct nfs41_exchange_id_data {
7894 struct nfs41_exchange_id_res res;
7895 struct nfs41_exchange_id_args args;
7898 static void nfs4_exchange_id_release(void *data)
7900 struct nfs41_exchange_id_data *cdata =
7901 (struct nfs41_exchange_id_data *)data;
7903 nfs_put_client(cdata->args.client);
7904 kfree(cdata->res.impl_id);
7905 kfree(cdata->res.server_scope);
7906 kfree(cdata->res.server_owner);
7910 static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7911 .rpc_release = nfs4_exchange_id_release,
7915 * _nfs4_proc_exchange_id()
7917 * Wrapper for EXCHANGE_ID operation.
7919 static struct rpc_task *
7920 nfs4_run_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
7921 u32 sp4_how, struct rpc_xprt *xprt)
7923 struct rpc_message msg = {
7924 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
7927 struct rpc_task_setup task_setup_data = {
7928 .rpc_client = clp->cl_rpcclient,
7929 .callback_ops = &nfs4_exchange_id_call_ops,
7930 .rpc_message = &msg,
7931 .flags = RPC_TASK_TIMEOUT,
7933 struct nfs41_exchange_id_data *calldata;
7936 if (!refcount_inc_not_zero(&clp->cl_count))
7937 return ERR_PTR(-EIO);
7940 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7944 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
7946 status = nfs4_init_uniform_client_string(clp);
7950 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7953 if (unlikely(calldata->res.server_owner == NULL))
7956 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
7958 if (unlikely(calldata->res.server_scope == NULL))
7959 goto out_server_owner;
7961 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7962 if (unlikely(calldata->res.impl_id == NULL))
7963 goto out_server_scope;
7967 calldata->args.state_protect.how = SP4_NONE;
7971 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
7981 task_setup_data.rpc_xprt = xprt;
7982 task_setup_data.flags |= RPC_TASK_SOFTCONN;
7983 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
7984 sizeof(calldata->args.verifier.data));
7986 calldata->args.client = clp;
7987 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7988 EXCHGID4_FLAG_BIND_PRINC_STATEID;
7989 #ifdef CONFIG_NFS_V4_1_MIGRATION
7990 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
7992 msg.rpc_argp = &calldata->args;
7993 msg.rpc_resp = &calldata->res;
7994 task_setup_data.callback_data = calldata;
7996 return rpc_run_task(&task_setup_data);
7999 kfree(calldata->res.impl_id);
8001 kfree(calldata->res.server_scope);
8003 kfree(calldata->res.server_owner);
8007 nfs_put_client(clp);
8008 return ERR_PTR(status);
8012 * _nfs4_proc_exchange_id()
8014 * Wrapper for EXCHANGE_ID operation.
8016 static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
8019 struct rpc_task *task;
8020 struct nfs41_exchange_id_args *argp;
8021 struct nfs41_exchange_id_res *resp;
8024 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
8026 return PTR_ERR(task);
8028 argp = task->tk_msg.rpc_argp;
8029 resp = task->tk_msg.rpc_resp;
8030 status = task->tk_status;
8034 status = nfs4_check_cl_exchange_flags(resp->flags,
8035 clp->cl_mvops->minor_version);
8039 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8043 clp->cl_clientid = resp->clientid;
8044 clp->cl_exchange_flags = resp->flags;
8045 clp->cl_seqid = resp->seqid;
8046 /* Client ID is not confirmed */
8047 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8048 clear_bit(NFS4_SESSION_ESTABLISHED,
8049 &clp->cl_session->session_state);
8051 if (clp->cl_serverscope != NULL &&
8052 !nfs41_same_server_scope(clp->cl_serverscope,
8053 resp->server_scope)) {
8054 dprintk("%s: server_scope mismatch detected\n",
8056 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8059 swap(clp->cl_serverowner, resp->server_owner);
8060 swap(clp->cl_serverscope, resp->server_scope);
8061 swap(clp->cl_implid, resp->impl_id);
8063 /* Save the EXCHANGE_ID verifier session trunk tests */
8064 memcpy(clp->cl_confirm.data, argp->verifier.data,
8065 sizeof(clp->cl_confirm.data));
8067 trace_nfs4_exchange_id(clp, status);
8073 * nfs4_proc_exchange_id()
8075 * Returns zero, a negative errno, or a negative NFS4ERR status code.
8077 * Since the clientid has expired, all compounds using sessions
8078 * associated with the stale clientid will be returning
8079 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
8080 * be in some phase of session reset.
8082 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
8084 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
8086 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8089 /* try SP4_MACH_CRED if krb5i/p */
8090 if (authflavor == RPC_AUTH_GSS_KRB5I ||
8091 authflavor == RPC_AUTH_GSS_KRB5P) {
8092 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
8098 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
8102 * nfs4_test_session_trunk
8104 * This is an add_xprt_test() test function called from
8105 * rpc_clnt_setup_test_and_add_xprt.
8107 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8108 * and is dereferrenced in nfs4_exchange_id_release
8110 * Upon success, add the new transport to the rpc_clnt
8112 * @clnt: struct rpc_clnt to get new transport
8113 * @xprt: the rpc_xprt to test
8114 * @data: call data for _nfs4_proc_exchange_id.
8116 int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
8119 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
8120 struct rpc_task *task;
8125 dprintk("--> %s try %s\n", __func__,
8126 xprt->address_strings[RPC_DISPLAY_ADDR]);
8128 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8130 /* Test connection for session trunking. Async exchange_id call */
8131 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8133 return PTR_ERR(task);
8135 status = task->tk_status;
8137 status = nfs4_detect_session_trunking(adata->clp,
8138 task->tk_msg.rpc_resp, xprt);
8143 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
8145 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
8146 struct rpc_cred *cred)
8148 struct rpc_message msg = {
8149 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8155 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
8156 trace_nfs4_destroy_clientid(clp, status);
8158 dprintk("NFS: Got error %d from the server %s on "
8159 "DESTROY_CLIENTID.", status, clp->cl_hostname);
8163 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
8164 struct rpc_cred *cred)
8169 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8170 ret = _nfs4_proc_destroy_clientid(clp, cred);
8172 case -NFS4ERR_DELAY:
8173 case -NFS4ERR_CLIENTID_BUSY:
8183 int nfs4_destroy_clientid(struct nfs_client *clp)
8185 struct rpc_cred *cred;
8188 if (clp->cl_mvops->minor_version < 1)
8190 if (clp->cl_exchange_flags == 0)
8192 if (clp->cl_preserve_clid)
8194 cred = nfs4_get_clid_cred(clp);
8195 ret = nfs4_proc_destroy_clientid(clp, cred);
8200 case -NFS4ERR_STALE_CLIENTID:
8201 clp->cl_exchange_flags = 0;
8207 struct nfs4_get_lease_time_data {
8208 struct nfs4_get_lease_time_args *args;
8209 struct nfs4_get_lease_time_res *res;
8210 struct nfs_client *clp;
8213 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
8216 struct nfs4_get_lease_time_data *data =
8217 (struct nfs4_get_lease_time_data *)calldata;
8219 dprintk("--> %s\n", __func__);
8220 /* just setup sequence, do not trigger session recovery
8221 since we're invoked within one */
8222 nfs4_setup_sequence(data->clp,
8223 &data->args->la_seq_args,
8224 &data->res->lr_seq_res,
8226 dprintk("<-- %s\n", __func__);
8230 * Called from nfs4_state_manager thread for session setup, so don't recover
8231 * from sequence operation or clientid errors.
8233 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8235 struct nfs4_get_lease_time_data *data =
8236 (struct nfs4_get_lease_time_data *)calldata;
8238 dprintk("--> %s\n", __func__);
8239 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
8241 switch (task->tk_status) {
8242 case -NFS4ERR_DELAY:
8243 case -NFS4ERR_GRACE:
8244 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
8245 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8246 task->tk_status = 0;
8248 case -NFS4ERR_RETRY_UNCACHED_REP:
8249 rpc_restart_call_prepare(task);
8252 dprintk("<-- %s\n", __func__);
8255 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
8256 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8257 .rpc_call_done = nfs4_get_lease_time_done,
8260 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8262 struct rpc_task *task;
8263 struct nfs4_get_lease_time_args args;
8264 struct nfs4_get_lease_time_res res = {
8265 .lr_fsinfo = fsinfo,
8267 struct nfs4_get_lease_time_data data = {
8272 struct rpc_message msg = {
8273 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8277 struct rpc_task_setup task_setup = {
8278 .rpc_client = clp->cl_rpcclient,
8279 .rpc_message = &msg,
8280 .callback_ops = &nfs4_get_lease_time_ops,
8281 .callback_data = &data,
8282 .flags = RPC_TASK_TIMEOUT,
8286 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
8287 task = rpc_run_task(&task_setup);
8290 return PTR_ERR(task);
8292 status = task->tk_status;
8298 * Initialize the values to be used by the client in CREATE_SESSION
8299 * If nfs4_init_session set the fore channel request and response sizes,
8302 * Set the back channel max_resp_sz_cached to zero to force the client to
8303 * always set csa_cachethis to FALSE because the current implementation
8304 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8306 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8307 struct rpc_clnt *clnt)
8309 unsigned int max_rqst_sz, max_resp_sz;
8310 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
8312 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8313 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8315 /* Fore channel attributes */
8316 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8317 args->fc_attrs.max_resp_sz = max_resp_sz;
8318 args->fc_attrs.max_ops = NFS4_MAX_OPS;
8319 args->fc_attrs.max_reqs = max_session_slots;
8321 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
8322 "max_ops=%u max_reqs=%u\n",
8324 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
8325 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
8327 /* Back channel attributes */
8328 args->bc_attrs.max_rqst_sz = max_bc_payload;
8329 args->bc_attrs.max_resp_sz = max_bc_payload;
8330 args->bc_attrs.max_resp_sz_cached = 0;
8331 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
8332 args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
8334 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8335 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8337 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8338 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8339 args->bc_attrs.max_reqs);
8342 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8343 struct nfs41_create_session_res *res)
8345 struct nfs4_channel_attrs *sent = &args->fc_attrs;
8346 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
8348 if (rcvd->max_resp_sz > sent->max_resp_sz)
8351 * Our requested max_ops is the minimum we need; we're not
8352 * prepared to break up compounds into smaller pieces than that.
8353 * So, no point even trying to continue if the server won't
8356 if (rcvd->max_ops < sent->max_ops)
8358 if (rcvd->max_reqs == 0)
8360 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8361 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
8365 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8366 struct nfs41_create_session_res *res)
8368 struct nfs4_channel_attrs *sent = &args->bc_attrs;
8369 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
8371 if (!(res->flags & SESSION4_BACK_CHAN))
8373 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8375 if (rcvd->max_resp_sz < sent->max_resp_sz)
8377 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8379 if (rcvd->max_ops > sent->max_ops)
8381 if (rcvd->max_reqs > sent->max_reqs)
8387 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
8388 struct nfs41_create_session_res *res)
8392 ret = nfs4_verify_fore_channel_attrs(args, res);
8395 return nfs4_verify_back_channel_attrs(args, res);
8398 static void nfs4_update_session(struct nfs4_session *session,
8399 struct nfs41_create_session_res *res)
8401 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
8402 /* Mark client id and session as being confirmed */
8403 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8404 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
8405 session->flags = res->flags;
8406 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
8407 if (res->flags & SESSION4_BACK_CHAN)
8408 memcpy(&session->bc_attrs, &res->bc_attrs,
8409 sizeof(session->bc_attrs));
8412 static int _nfs4_proc_create_session(struct nfs_client *clp,
8413 struct rpc_cred *cred)
8415 struct nfs4_session *session = clp->cl_session;
8416 struct nfs41_create_session_args args = {
8418 .clientid = clp->cl_clientid,
8419 .seqid = clp->cl_seqid,
8420 .cb_program = NFS4_CALLBACK,
8422 struct nfs41_create_session_res res;
8424 struct rpc_message msg = {
8425 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8432 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
8433 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
8435 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
8436 trace_nfs4_create_session(clp, status);
8439 case -NFS4ERR_STALE_CLIENTID:
8440 case -NFS4ERR_DELAY:
8449 /* Verify the session's negotiated channel_attrs values */
8450 status = nfs4_verify_channel_attrs(&args, &res);
8451 /* Increment the clientid slot sequence id */
8454 nfs4_update_session(session, &res);
8461 * Issues a CREATE_SESSION operation to the server.
8462 * It is the responsibility of the caller to verify the session is
8463 * expired before calling this routine.
8465 int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
8469 struct nfs4_session *session = clp->cl_session;
8471 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8473 status = _nfs4_proc_create_session(clp, cred);
8477 /* Init or reset the session slot tables */
8478 status = nfs4_setup_session_slot_tables(session);
8479 dprintk("slot table setup returned %d\n", status);
8483 ptr = (unsigned *)&session->sess_id.data[0];
8484 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8485 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
8487 dprintk("<-- %s\n", __func__);
8492 * Issue the over-the-wire RPC DESTROY_SESSION.
8493 * The caller must serialize access to this routine.
8495 int nfs4_proc_destroy_session(struct nfs4_session *session,
8496 struct rpc_cred *cred)
8498 struct rpc_message msg = {
8499 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8500 .rpc_argp = session,
8505 dprintk("--> nfs4_proc_destroy_session\n");
8507 /* session is still being setup */
8508 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8511 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
8512 trace_nfs4_destroy_session(session->clp, status);
8515 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
8516 "Session has been destroyed regardless...\n", status);
8518 dprintk("<-- nfs4_proc_destroy_session\n");
8523 * Renew the cl_session lease.
8525 struct nfs4_sequence_data {
8526 struct nfs_client *clp;
8527 struct nfs4_sequence_args args;
8528 struct nfs4_sequence_res res;
8531 static void nfs41_sequence_release(void *data)
8533 struct nfs4_sequence_data *calldata = data;
8534 struct nfs_client *clp = calldata->clp;
8536 if (refcount_read(&clp->cl_count) > 1)
8537 nfs4_schedule_state_renewal(clp);
8538 nfs_put_client(clp);
8542 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8544 switch(task->tk_status) {
8545 case -NFS4ERR_DELAY:
8546 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8549 nfs4_schedule_lease_recovery(clp);
8554 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
8556 struct nfs4_sequence_data *calldata = data;
8557 struct nfs_client *clp = calldata->clp;
8559 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8562 trace_nfs4_sequence(clp, task->tk_status);
8563 if (task->tk_status < 0) {
8564 dprintk("%s ERROR %d\n", __func__, task->tk_status);
8565 if (refcount_read(&clp->cl_count) == 1)
8568 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8569 rpc_restart_call_prepare(task);
8573 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
8575 dprintk("<-- %s\n", __func__);
8578 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8580 struct nfs4_sequence_data *calldata = data;
8581 struct nfs_client *clp = calldata->clp;
8582 struct nfs4_sequence_args *args;
8583 struct nfs4_sequence_res *res;
8585 args = task->tk_msg.rpc_argp;
8586 res = task->tk_msg.rpc_resp;
8588 nfs4_setup_sequence(clp, args, res, task);
8591 static const struct rpc_call_ops nfs41_sequence_ops = {
8592 .rpc_call_done = nfs41_sequence_call_done,
8593 .rpc_call_prepare = nfs41_sequence_prepare,
8594 .rpc_release = nfs41_sequence_release,
8597 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8598 struct rpc_cred *cred,
8599 struct nfs4_slot *slot,
8602 struct nfs4_sequence_data *calldata;
8603 struct rpc_message msg = {
8604 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8607 struct rpc_task_setup task_setup_data = {
8608 .rpc_client = clp->cl_rpcclient,
8609 .rpc_message = &msg,
8610 .callback_ops = &nfs41_sequence_ops,
8611 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
8613 struct rpc_task *ret;
8615 ret = ERR_PTR(-EIO);
8616 if (!refcount_inc_not_zero(&clp->cl_count))
8619 ret = ERR_PTR(-ENOMEM);
8620 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8621 if (calldata == NULL)
8623 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
8624 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
8625 msg.rpc_argp = &calldata->args;
8626 msg.rpc_resp = &calldata->res;
8627 calldata->clp = clp;
8628 task_setup_data.callback_data = calldata;
8630 ret = rpc_run_task(&task_setup_data);
8635 nfs_put_client(clp);
8637 nfs41_release_slot(slot);
8641 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
8643 struct rpc_task *task;
8646 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
8648 task = _nfs41_proc_sequence(clp, cred, NULL, false);
8650 ret = PTR_ERR(task);
8652 rpc_put_task_async(task);
8653 dprintk("<-- %s status=%d\n", __func__, ret);
8657 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8659 struct rpc_task *task;
8662 task = _nfs41_proc_sequence(clp, cred, NULL, true);
8664 ret = PTR_ERR(task);
8667 ret = rpc_wait_for_completion_task(task);
8669 ret = task->tk_status;
8672 dprintk("<-- %s status=%d\n", __func__, ret);
8676 struct nfs4_reclaim_complete_data {
8677 struct nfs_client *clp;
8678 struct nfs41_reclaim_complete_args arg;
8679 struct nfs41_reclaim_complete_res res;
8682 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8684 struct nfs4_reclaim_complete_data *calldata = data;
8686 nfs4_setup_sequence(calldata->clp,
8687 &calldata->arg.seq_args,
8688 &calldata->res.seq_res,
8692 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8694 switch(task->tk_status) {
8696 wake_up_all(&clp->cl_lock_waitq);
8698 case -NFS4ERR_COMPLETE_ALREADY:
8699 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8701 case -NFS4ERR_DELAY:
8702 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8704 case -NFS4ERR_RETRY_UNCACHED_REP:
8706 dprintk("%s: failed to reclaim complete error %d for server %s, retrying\n",
8707 __func__, task->tk_status, clp->cl_hostname);
8709 case -NFS4ERR_BADSESSION:
8710 case -NFS4ERR_DEADSESSION:
8711 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8712 nfs4_schedule_session_recovery(clp->cl_session,
8716 nfs4_schedule_lease_recovery(clp);
8721 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8723 struct nfs4_reclaim_complete_data *calldata = data;
8724 struct nfs_client *clp = calldata->clp;
8725 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8727 dprintk("--> %s\n", __func__);
8728 if (!nfs41_sequence_done(task, res))
8731 trace_nfs4_reclaim_complete(clp, task->tk_status);
8732 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8733 rpc_restart_call_prepare(task);
8736 dprintk("<-- %s\n", __func__);
8739 static void nfs4_free_reclaim_complete_data(void *data)
8741 struct nfs4_reclaim_complete_data *calldata = data;
8746 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8747 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8748 .rpc_call_done = nfs4_reclaim_complete_done,
8749 .rpc_release = nfs4_free_reclaim_complete_data,
8753 * Issue a global reclaim complete.
8755 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8756 struct rpc_cred *cred)
8758 struct nfs4_reclaim_complete_data *calldata;
8759 struct rpc_task *task;
8760 struct rpc_message msg = {
8761 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
8764 struct rpc_task_setup task_setup_data = {
8765 .rpc_client = clp->cl_rpcclient,
8766 .rpc_message = &msg,
8767 .callback_ops = &nfs4_reclaim_complete_call_ops,
8768 .flags = RPC_TASK_ASYNC,
8770 int status = -ENOMEM;
8772 dprintk("--> %s\n", __func__);
8773 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8774 if (calldata == NULL)
8776 calldata->clp = clp;
8777 calldata->arg.one_fs = 0;
8779 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
8780 msg.rpc_argp = &calldata->arg;
8781 msg.rpc_resp = &calldata->res;
8782 task_setup_data.callback_data = calldata;
8783 task = rpc_run_task(&task_setup_data);
8785 status = PTR_ERR(task);
8788 status = rpc_wait_for_completion_task(task);
8790 status = task->tk_status;
8793 dprintk("<-- %s status=%d\n", __func__, status);
8798 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8800 struct nfs4_layoutget *lgp = calldata;
8801 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
8803 dprintk("--> %s\n", __func__);
8804 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
8805 &lgp->res.seq_res, task);
8806 dprintk("<-- %s\n", __func__);
8809 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8811 struct nfs4_layoutget *lgp = calldata;
8813 dprintk("--> %s\n", __func__);
8814 nfs41_sequence_process(task, &lgp->res.seq_res);
8815 dprintk("<-- %s\n", __func__);
8819 nfs4_layoutget_handle_exception(struct rpc_task *task,
8820 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8822 struct inode *inode = lgp->args.inode;
8823 struct nfs_server *server = NFS_SERVER(inode);
8824 struct pnfs_layout_hdr *lo;
8825 int nfs4err = task->tk_status;
8826 int err, status = 0;
8829 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
8831 nfs4_sequence_free_slot(&lgp->res.seq_res);
8838 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8839 * on the file. set tk_status to -ENODATA to tell upper layer to
8842 case -NFS4ERR_LAYOUTUNAVAILABLE:
8846 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8847 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8849 case -NFS4ERR_BADLAYOUT:
8850 status = -EOVERFLOW;
8853 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
8854 * (or clients) writing to the same RAID stripe except when
8855 * the minlength argument is 0 (see RFC5661 section 18.43.3).
8857 * Treat it like we would RECALLCONFLICT -- we retry for a little
8858 * while, and then eventually give up.
8860 case -NFS4ERR_LAYOUTTRYLATER:
8861 if (lgp->args.minlength == 0) {
8862 status = -EOVERFLOW;
8867 case -NFS4ERR_RECALLCONFLICT:
8868 status = -ERECALLCONFLICT;
8870 case -NFS4ERR_DELEG_REVOKED:
8871 case -NFS4ERR_ADMIN_REVOKED:
8872 case -NFS4ERR_EXPIRED:
8873 case -NFS4ERR_BAD_STATEID:
8874 exception->timeout = 0;
8875 spin_lock(&inode->i_lock);
8876 lo = NFS_I(inode)->layout;
8877 /* If the open stateid was bad, then recover it. */
8878 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8879 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
8880 spin_unlock(&inode->i_lock);
8881 exception->state = lgp->args.ctx->state;
8882 exception->stateid = &lgp->args.stateid;
8887 * Mark the bad layout state as invalid, then retry
8889 pnfs_mark_layout_stateid_invalid(lo, &head);
8890 spin_unlock(&inode->i_lock);
8891 nfs_commit_inode(inode, 0);
8892 pnfs_free_lseg_list(&head);
8897 err = nfs4_handle_exception(server, nfs4err, exception);
8899 if (exception->retry)
8905 dprintk("<-- %s\n", __func__);
8909 size_t max_response_pages(struct nfs_server *server)
8911 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8912 return nfs_page_array_len(0, max_resp_sz);
8915 static void nfs4_layoutget_release(void *calldata)
8917 struct nfs4_layoutget *lgp = calldata;
8919 dprintk("--> %s\n", __func__);
8920 nfs4_sequence_free_slot(&lgp->res.seq_res);
8921 pnfs_layoutget_free(lgp);
8922 dprintk("<-- %s\n", __func__);
8925 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8926 .rpc_call_prepare = nfs4_layoutget_prepare,
8927 .rpc_call_done = nfs4_layoutget_done,
8928 .rpc_release = nfs4_layoutget_release,
8931 struct pnfs_layout_segment *
8932 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
8934 struct inode *inode = lgp->args.inode;
8935 struct nfs_server *server = NFS_SERVER(inode);
8936 struct rpc_task *task;
8937 struct rpc_message msg = {
8938 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8939 .rpc_argp = &lgp->args,
8940 .rpc_resp = &lgp->res,
8941 .rpc_cred = lgp->cred,
8943 struct rpc_task_setup task_setup_data = {
8944 .rpc_client = server->client,
8945 .rpc_message = &msg,
8946 .callback_ops = &nfs4_layoutget_call_ops,
8947 .callback_data = lgp,
8948 .flags = RPC_TASK_ASYNC,
8950 struct pnfs_layout_segment *lseg = NULL;
8951 struct nfs4_exception exception = {
8953 .timeout = *timeout,
8957 dprintk("--> %s\n", __func__);
8959 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8960 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8962 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
8964 task = rpc_run_task(&task_setup_data);
8966 return ERR_CAST(task);
8967 status = rpc_wait_for_completion_task(task);
8971 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8972 if (task->tk_status < 0 || lgp->res.layoutp->len == 0) {
8973 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8974 *timeout = exception.timeout;
8976 lseg = pnfs_layout_process(lgp);
8978 trace_nfs4_layoutget(lgp->args.ctx,
8985 dprintk("<-- %s status=%d\n", __func__, status);
8987 return ERR_PTR(status);
8992 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8994 struct nfs4_layoutreturn *lrp = calldata;
8996 dprintk("--> %s\n", __func__);
8997 nfs4_setup_sequence(lrp->clp,
8998 &lrp->args.seq_args,
9001 if (!pnfs_layout_is_valid(lrp->args.layout))
9005 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
9007 struct nfs4_layoutreturn *lrp = calldata;
9008 struct nfs_server *server;
9010 dprintk("--> %s\n", __func__);
9012 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
9015 server = NFS_SERVER(lrp->args.inode);
9016 switch (task->tk_status) {
9017 case -NFS4ERR_OLD_STATEID:
9018 if (nfs4_layoutreturn_refresh_stateid(&lrp->args.stateid,
9024 task->tk_status = 0;
9028 case -NFS4ERR_DELAY:
9029 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
9033 dprintk("<-- %s\n", __func__);
9036 task->tk_status = 0;
9037 nfs4_sequence_free_slot(&lrp->res.seq_res);
9038 rpc_restart_call_prepare(task);
9041 static void nfs4_layoutreturn_release(void *calldata)
9043 struct nfs4_layoutreturn *lrp = calldata;
9044 struct pnfs_layout_hdr *lo = lrp->args.layout;
9046 dprintk("--> %s\n", __func__);
9047 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
9048 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
9049 nfs4_sequence_free_slot(&lrp->res.seq_res);
9050 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9051 lrp->ld_private.ops->free(&lrp->ld_private);
9052 pnfs_put_layout_hdr(lrp->args.layout);
9053 nfs_iput_and_deactive(lrp->inode);
9055 dprintk("<-- %s\n", __func__);
9058 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9059 .rpc_call_prepare = nfs4_layoutreturn_prepare,
9060 .rpc_call_done = nfs4_layoutreturn_done,
9061 .rpc_release = nfs4_layoutreturn_release,
9064 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
9066 struct rpc_task *task;
9067 struct rpc_message msg = {
9068 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9069 .rpc_argp = &lrp->args,
9070 .rpc_resp = &lrp->res,
9071 .rpc_cred = lrp->cred,
9073 struct rpc_task_setup task_setup_data = {
9074 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
9075 .rpc_message = &msg,
9076 .callback_ops = &nfs4_layoutreturn_call_ops,
9077 .callback_data = lrp,
9081 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9082 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9083 &task_setup_data.rpc_client, &msg);
9085 dprintk("--> %s\n", __func__);
9086 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
9089 nfs4_layoutreturn_release(lrp);
9092 task_setup_data.flags |= RPC_TASK_ASYNC;
9095 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1,
9098 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1,
9100 task = rpc_run_task(&task_setup_data);
9102 return PTR_ERR(task);
9104 status = task->tk_status;
9105 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
9106 dprintk("<-- %s status=%d\n", __func__, status);
9112 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
9113 struct pnfs_device *pdev,
9114 struct rpc_cred *cred)
9116 struct nfs4_getdeviceinfo_args args = {
9118 .notify_types = NOTIFY_DEVICEID4_CHANGE |
9119 NOTIFY_DEVICEID4_DELETE,
9121 struct nfs4_getdeviceinfo_res res = {
9124 struct rpc_message msg = {
9125 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9132 dprintk("--> %s\n", __func__);
9133 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
9134 if (res.notification & ~args.notify_types)
9135 dprintk("%s: unsupported notification\n", __func__);
9136 if (res.notification != args.notify_types)
9139 dprintk("<-- %s status=%d\n", __func__, status);
9144 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9145 struct pnfs_device *pdev,
9146 struct rpc_cred *cred)
9148 struct nfs4_exception exception = { };
9152 err = nfs4_handle_exception(server,
9153 _nfs4_proc_getdeviceinfo(server, pdev, cred),
9155 } while (exception.retry);
9158 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9160 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9162 struct nfs4_layoutcommit_data *data = calldata;
9163 struct nfs_server *server = NFS_SERVER(data->args.inode);
9165 nfs4_setup_sequence(server->nfs_client,
9166 &data->args.seq_args,
9172 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9174 struct nfs4_layoutcommit_data *data = calldata;
9175 struct nfs_server *server = NFS_SERVER(data->args.inode);
9177 if (!nfs41_sequence_done(task, &data->res.seq_res))
9180 switch (task->tk_status) { /* Just ignore these failures */
9181 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9182 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9183 case -NFS4ERR_BADLAYOUT: /* no layout */
9184 case -NFS4ERR_GRACE: /* loca_recalim always false */
9185 task->tk_status = 0;
9189 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
9190 rpc_restart_call_prepare(task);
9196 static void nfs4_layoutcommit_release(void *calldata)
9198 struct nfs4_layoutcommit_data *data = calldata;
9200 pnfs_cleanup_layoutcommit(data);
9201 nfs_post_op_update_inode_force_wcc(data->args.inode,
9203 put_rpccred(data->cred);
9204 nfs_iput_and_deactive(data->inode);
9208 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9209 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9210 .rpc_call_done = nfs4_layoutcommit_done,
9211 .rpc_release = nfs4_layoutcommit_release,
9215 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
9217 struct rpc_message msg = {
9218 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9219 .rpc_argp = &data->args,
9220 .rpc_resp = &data->res,
9221 .rpc_cred = data->cred,
9223 struct rpc_task_setup task_setup_data = {
9224 .task = &data->task,
9225 .rpc_client = NFS_CLIENT(data->args.inode),
9226 .rpc_message = &msg,
9227 .callback_ops = &nfs4_layoutcommit_ops,
9228 .callback_data = data,
9230 struct rpc_task *task;
9233 dprintk("NFS: initiating layoutcommit call. sync %d "
9234 "lbw: %llu inode %lu\n", sync,
9235 data->args.lastbytewritten,
9236 data->args.inode->i_ino);
9239 data->inode = nfs_igrab_and_active(data->args.inode);
9240 if (data->inode == NULL) {
9241 nfs4_layoutcommit_release(data);
9244 task_setup_data.flags = RPC_TASK_ASYNC;
9246 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
9247 task = rpc_run_task(&task_setup_data);
9249 return PTR_ERR(task);
9251 status = task->tk_status;
9252 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
9253 dprintk("%s: status %d\n", __func__, status);
9259 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9260 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9263 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9264 struct nfs_fsinfo *info,
9265 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
9267 struct nfs41_secinfo_no_name_args args = {
9268 .style = SECINFO_STYLE_CURRENT_FH,
9270 struct nfs4_secinfo_res res = {
9273 struct rpc_message msg = {
9274 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9278 struct rpc_clnt *clnt = server->client;
9279 struct rpc_cred *cred = NULL;
9282 if (use_integrity) {
9283 clnt = server->nfs_client->cl_rpcclient;
9284 cred = nfs4_get_clid_cred(server->nfs_client);
9285 msg.rpc_cred = cred;
9288 dprintk("--> %s\n", __func__);
9289 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
9291 dprintk("<-- %s status=%d\n", __func__, status);
9300 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9301 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9303 struct nfs4_exception exception = { };
9306 /* first try using integrity protection */
9307 err = -NFS4ERR_WRONGSEC;
9309 /* try to use integrity protection with machine cred */
9310 if (_nfs4_is_integrity_protected(server->nfs_client))
9311 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9315 * if unable to use integrity protection, or SECINFO with
9316 * integrity protection returns NFS4ERR_WRONGSEC (which is
9317 * disallowed by spec, but exists in deployed servers) use
9318 * the current filesystem's rpc_client and the user cred.
9320 if (err == -NFS4ERR_WRONGSEC)
9321 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9326 case -NFS4ERR_WRONGSEC:
9330 err = nfs4_handle_exception(server, err, &exception);
9332 } while (exception.retry);
9338 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9339 struct nfs_fsinfo *info)
9343 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
9344 struct nfs4_secinfo_flavors *flavors;
9345 struct nfs4_secinfo4 *secinfo;
9348 page = alloc_page(GFP_KERNEL);
9354 flavors = page_address(page);
9355 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9358 * Fall back on "guess and check" method if
9359 * the server doesn't support SECINFO_NO_NAME
9361 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
9362 err = nfs4_find_root_sec(server, fhandle, info);
9368 for (i = 0; i < flavors->num_flavors; i++) {
9369 secinfo = &flavors->flavors[i];
9371 switch (secinfo->flavor) {
9375 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9376 &secinfo->flavor_info);
9379 flavor = RPC_AUTH_MAXFLAVOR;
9383 if (!nfs_auth_info_match(&server->auth_info, flavor))
9384 flavor = RPC_AUTH_MAXFLAVOR;
9386 if (flavor != RPC_AUTH_MAXFLAVOR) {
9387 err = nfs4_lookup_root_sec(server, fhandle,
9394 if (flavor == RPC_AUTH_MAXFLAVOR)
9405 static int _nfs41_test_stateid(struct nfs_server *server,
9406 nfs4_stateid *stateid,
9407 struct rpc_cred *cred)
9410 struct nfs41_test_stateid_args args = {
9413 struct nfs41_test_stateid_res res;
9414 struct rpc_message msg = {
9415 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9420 struct rpc_clnt *rpc_client = server->client;
9422 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9425 dprintk("NFS call test_stateid %p\n", stateid);
9426 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
9427 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
9428 &args.seq_args, &res.seq_res);
9429 if (status != NFS_OK) {
9430 dprintk("NFS reply test_stateid: failed, %d\n", status);
9433 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
9437 static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9438 int err, struct nfs4_exception *exception)
9440 exception->retry = 0;
9442 case -NFS4ERR_DELAY:
9443 case -NFS4ERR_RETRY_UNCACHED_REP:
9444 nfs4_handle_exception(server, err, exception);
9446 case -NFS4ERR_BADSESSION:
9447 case -NFS4ERR_BADSLOT:
9448 case -NFS4ERR_BAD_HIGH_SLOT:
9449 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9450 case -NFS4ERR_DEADSESSION:
9451 nfs4_do_handle_exception(server, err, exception);
9456 * nfs41_test_stateid - perform a TEST_STATEID operation
9458 * @server: server / transport on which to perform the operation
9459 * @stateid: state ID to test
9462 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9463 * Otherwise a negative NFS4ERR value is returned if the operation
9464 * failed or the state ID is not currently valid.
9466 static int nfs41_test_stateid(struct nfs_server *server,
9467 nfs4_stateid *stateid,
9468 struct rpc_cred *cred)
9470 struct nfs4_exception exception = { };
9473 err = _nfs41_test_stateid(server, stateid, cred);
9474 nfs4_handle_delay_or_session_error(server, err, &exception);
9475 } while (exception.retry);
9479 struct nfs_free_stateid_data {
9480 struct nfs_server *server;
9481 struct nfs41_free_stateid_args args;
9482 struct nfs41_free_stateid_res res;
9485 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9487 struct nfs_free_stateid_data *data = calldata;
9488 nfs4_setup_sequence(data->server->nfs_client,
9489 &data->args.seq_args,
9494 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9496 struct nfs_free_stateid_data *data = calldata;
9498 nfs41_sequence_done(task, &data->res.seq_res);
9500 switch (task->tk_status) {
9501 case -NFS4ERR_DELAY:
9502 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
9503 rpc_restart_call_prepare(task);
9507 static void nfs41_free_stateid_release(void *calldata)
9512 static const struct rpc_call_ops nfs41_free_stateid_ops = {
9513 .rpc_call_prepare = nfs41_free_stateid_prepare,
9514 .rpc_call_done = nfs41_free_stateid_done,
9515 .rpc_release = nfs41_free_stateid_release,
9519 * nfs41_free_stateid - perform a FREE_STATEID operation
9521 * @server: server / transport on which to perform the operation
9522 * @stateid: state ID to release
9524 * @is_recovery: set to true if this call needs to be privileged
9526 * Note: this function is always asynchronous.
9528 static int nfs41_free_stateid(struct nfs_server *server,
9529 const nfs4_stateid *stateid,
9530 struct rpc_cred *cred,
9533 struct rpc_message msg = {
9534 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
9537 struct rpc_task_setup task_setup = {
9538 .rpc_client = server->client,
9539 .rpc_message = &msg,
9540 .callback_ops = &nfs41_free_stateid_ops,
9541 .flags = RPC_TASK_ASYNC,
9543 struct nfs_free_stateid_data *data;
9544 struct rpc_task *task;
9546 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9547 &task_setup.rpc_client, &msg);
9549 dprintk("NFS call free_stateid %p\n", stateid);
9550 data = kmalloc(sizeof(*data), GFP_NOFS);
9553 data->server = server;
9554 nfs4_stateid_copy(&data->args.stateid, stateid);
9556 task_setup.callback_data = data;
9558 msg.rpc_argp = &data->args;
9559 msg.rpc_resp = &data->res;
9560 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
9561 task = rpc_run_task(&task_setup);
9563 return PTR_ERR(task);
9569 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
9571 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
9573 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
9574 nfs4_free_lock_state(server, lsp);
9577 static bool nfs41_match_stateid(const nfs4_stateid *s1,
9578 const nfs4_stateid *s2)
9580 if (s1->type != s2->type)
9583 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
9586 if (s1->seqid == s2->seqid)
9589 return s1->seqid == 0 || s2->seqid == 0;
9592 #endif /* CONFIG_NFS_V4_1 */
9594 static bool nfs4_match_stateid(const nfs4_stateid *s1,
9595 const nfs4_stateid *s2)
9597 return nfs4_stateid_match(s1, s2);
9601 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
9602 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9603 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9604 .recover_open = nfs4_open_reclaim,
9605 .recover_lock = nfs4_lock_reclaim,
9606 .establish_clid = nfs4_init_clientid,
9607 .detect_trunking = nfs40_discover_server_trunking,
9610 #if defined(CONFIG_NFS_V4_1)
9611 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
9612 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9613 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9614 .recover_open = nfs4_open_reclaim,
9615 .recover_lock = nfs4_lock_reclaim,
9616 .establish_clid = nfs41_init_clientid,
9617 .reclaim_complete = nfs41_proc_reclaim_complete,
9618 .detect_trunking = nfs41_discover_server_trunking,
9620 #endif /* CONFIG_NFS_V4_1 */
9622 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
9623 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9624 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
9625 .recover_open = nfs40_open_expired,
9626 .recover_lock = nfs4_lock_expired,
9627 .establish_clid = nfs4_init_clientid,
9630 #if defined(CONFIG_NFS_V4_1)
9631 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
9632 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9633 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
9634 .recover_open = nfs41_open_expired,
9635 .recover_lock = nfs41_lock_expired,
9636 .establish_clid = nfs41_init_clientid,
9638 #endif /* CONFIG_NFS_V4_1 */
9640 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
9641 .sched_state_renewal = nfs4_proc_async_renew,
9642 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
9643 .renew_lease = nfs4_proc_renew,
9646 #if defined(CONFIG_NFS_V4_1)
9647 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
9648 .sched_state_renewal = nfs41_proc_async_sequence,
9649 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
9650 .renew_lease = nfs4_proc_sequence,
9654 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
9655 .get_locations = _nfs40_proc_get_locations,
9656 .fsid_present = _nfs40_proc_fsid_present,
9659 #if defined(CONFIG_NFS_V4_1)
9660 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
9661 .get_locations = _nfs41_proc_get_locations,
9662 .fsid_present = _nfs41_proc_fsid_present,
9664 #endif /* CONFIG_NFS_V4_1 */
9666 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9668 .init_caps = NFS_CAP_READDIRPLUS
9669 | NFS_CAP_ATOMIC_OPEN
9670 | NFS_CAP_POSIX_LOCK,
9671 .init_client = nfs40_init_client,
9672 .shutdown_client = nfs40_shutdown_client,
9673 .match_stateid = nfs4_match_stateid,
9674 .find_root_sec = nfs4_find_root_sec,
9675 .free_lock_state = nfs4_release_lockowner,
9676 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
9677 .alloc_seqid = nfs_alloc_seqid,
9678 .call_sync_ops = &nfs40_call_sync_ops,
9679 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9680 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9681 .state_renewal_ops = &nfs40_state_renewal_ops,
9682 .mig_recovery_ops = &nfs40_mig_recovery_ops,
9685 #if defined(CONFIG_NFS_V4_1)
9686 static struct nfs_seqid *
9687 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9692 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9694 .init_caps = NFS_CAP_READDIRPLUS
9695 | NFS_CAP_ATOMIC_OPEN
9696 | NFS_CAP_POSIX_LOCK
9697 | NFS_CAP_STATEID_NFSV41
9698 | NFS_CAP_ATOMIC_OPEN_V1
9700 .init_client = nfs41_init_client,
9701 .shutdown_client = nfs41_shutdown_client,
9702 .match_stateid = nfs41_match_stateid,
9703 .find_root_sec = nfs41_find_root_sec,
9704 .free_lock_state = nfs41_free_lock_state,
9705 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
9706 .alloc_seqid = nfs_alloc_no_seqid,
9707 .session_trunk = nfs4_test_session_trunk,
9708 .call_sync_ops = &nfs41_call_sync_ops,
9709 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9710 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9711 .state_renewal_ops = &nfs41_state_renewal_ops,
9712 .mig_recovery_ops = &nfs41_mig_recovery_ops,
9716 #if defined(CONFIG_NFS_V4_2)
9717 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9719 .init_caps = NFS_CAP_READDIRPLUS
9720 | NFS_CAP_ATOMIC_OPEN
9721 | NFS_CAP_POSIX_LOCK
9722 | NFS_CAP_STATEID_NFSV41
9723 | NFS_CAP_ATOMIC_OPEN_V1
9727 | NFS_CAP_OFFLOAD_CANCEL
9728 | NFS_CAP_DEALLOCATE
9730 | NFS_CAP_LAYOUTSTATS
9732 .init_client = nfs41_init_client,
9733 .shutdown_client = nfs41_shutdown_client,
9734 .match_stateid = nfs41_match_stateid,
9735 .find_root_sec = nfs41_find_root_sec,
9736 .free_lock_state = nfs41_free_lock_state,
9737 .call_sync_ops = &nfs41_call_sync_ops,
9738 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
9739 .alloc_seqid = nfs_alloc_no_seqid,
9740 .session_trunk = nfs4_test_session_trunk,
9741 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9742 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9743 .state_renewal_ops = &nfs41_state_renewal_ops,
9744 .mig_recovery_ops = &nfs41_mig_recovery_ops,
9748 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9749 [0] = &nfs_v4_0_minor_ops,
9750 #if defined(CONFIG_NFS_V4_1)
9751 [1] = &nfs_v4_1_minor_ops,
9753 #if defined(CONFIG_NFS_V4_2)
9754 [2] = &nfs_v4_2_minor_ops,
9758 static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
9760 ssize_t error, error2;
9762 error = generic_listxattr(dentry, list, size);
9770 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9773 return error + error2;
9776 static const struct inode_operations nfs4_dir_inode_operations = {
9777 .create = nfs_create,
9778 .lookup = nfs_lookup,
9779 .atomic_open = nfs_atomic_open,
9781 .unlink = nfs_unlink,
9782 .symlink = nfs_symlink,
9786 .rename = nfs_rename,
9787 .permission = nfs_permission,
9788 .getattr = nfs_getattr,
9789 .setattr = nfs_setattr,
9790 .listxattr = nfs4_listxattr,
9793 static const struct inode_operations nfs4_file_inode_operations = {
9794 .permission = nfs_permission,
9795 .getattr = nfs_getattr,
9796 .setattr = nfs_setattr,
9797 .listxattr = nfs4_listxattr,
9800 const struct nfs_rpc_ops nfs_v4_clientops = {
9801 .version = 4, /* protocol version */
9802 .dentry_ops = &nfs4_dentry_operations,
9803 .dir_inode_ops = &nfs4_dir_inode_operations,
9804 .file_inode_ops = &nfs4_file_inode_operations,
9805 .file_ops = &nfs4_file_operations,
9806 .getroot = nfs4_proc_get_root,
9807 .submount = nfs4_submount,
9808 .try_mount = nfs4_try_mount,
9809 .getattr = nfs4_proc_getattr,
9810 .setattr = nfs4_proc_setattr,
9811 .lookup = nfs4_proc_lookup,
9812 .lookupp = nfs4_proc_lookupp,
9813 .access = nfs4_proc_access,
9814 .readlink = nfs4_proc_readlink,
9815 .create = nfs4_proc_create,
9816 .remove = nfs4_proc_remove,
9817 .unlink_setup = nfs4_proc_unlink_setup,
9818 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
9819 .unlink_done = nfs4_proc_unlink_done,
9820 .rename_setup = nfs4_proc_rename_setup,
9821 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
9822 .rename_done = nfs4_proc_rename_done,
9823 .link = nfs4_proc_link,
9824 .symlink = nfs4_proc_symlink,
9825 .mkdir = nfs4_proc_mkdir,
9826 .rmdir = nfs4_proc_rmdir,
9827 .readdir = nfs4_proc_readdir,
9828 .mknod = nfs4_proc_mknod,
9829 .statfs = nfs4_proc_statfs,
9830 .fsinfo = nfs4_proc_fsinfo,
9831 .pathconf = nfs4_proc_pathconf,
9832 .set_capabilities = nfs4_server_capabilities,
9833 .decode_dirent = nfs4_decode_dirent,
9834 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
9835 .read_setup = nfs4_proc_read_setup,
9836 .read_done = nfs4_read_done,
9837 .write_setup = nfs4_proc_write_setup,
9838 .write_done = nfs4_write_done,
9839 .commit_setup = nfs4_proc_commit_setup,
9840 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
9841 .commit_done = nfs4_commit_done,
9842 .lock = nfs4_proc_lock,
9843 .clear_acl_cache = nfs4_zap_acl_attr,
9844 .close_context = nfs4_close_context,
9845 .open_context = nfs4_atomic_open,
9846 .have_delegation = nfs4_have_delegation,
9847 .alloc_client = nfs4_alloc_client,
9848 .init_client = nfs4_init_client,
9849 .free_client = nfs4_free_client,
9850 .create_server = nfs4_create_server,
9851 .clone_server = nfs_clone_server,
9854 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
9855 .name = XATTR_NAME_NFSV4_ACL,
9856 .list = nfs4_xattr_list_nfs4_acl,
9857 .get = nfs4_xattr_get_nfs4_acl,
9858 .set = nfs4_xattr_set_nfs4_acl,
9861 const struct xattr_handler *nfs4_xattr_handlers[] = {
9862 &nfs4_xattr_nfs4_acl_handler,
9863 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
9864 &nfs4_xattr_nfs4_label_handler,