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/file.h>
42 #include <linux/string.h>
43 #include <linux/ratelimit.h>
44 #include <linux/printk.h>
45 #include <linux/slab.h>
46 #include <linux/sunrpc/clnt.h>
47 #include <linux/nfs.h>
48 #include <linux/nfs4.h>
49 #include <linux/nfs_fs.h>
50 #include <linux/nfs_page.h>
51 #include <linux/nfs_mount.h>
52 #include <linux/namei.h>
53 #include <linux/mount.h>
54 #include <linux/module.h>
55 #include <linux/xattr.h>
56 #include <linux/utsname.h>
57 #include <linux/freezer.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_POLL_RETRY_MIN (HZ/10)
75 #define NFS4_POLL_RETRY_MAX (15*HZ)
77 /* file attributes which can be mapped to nfs attributes */
78 #define NFS4_VALID_ATTRS (ATTR_MODE \
89 static int _nfs4_proc_open(struct nfs4_opendata *data);
90 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
91 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
92 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
93 static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
94 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label);
95 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
96 struct nfs_fattr *fattr, struct iattr *sattr,
97 struct nfs4_state *state, struct nfs4_label *ilabel,
98 struct nfs4_label *olabel);
99 #ifdef CONFIG_NFS_V4_1
100 static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
102 static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
103 struct rpc_cred *, bool);
106 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
107 static inline struct nfs4_label *
108 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
109 struct iattr *sattr, struct nfs4_label *label)
116 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
119 err = security_dentry_init_security(dentry, sattr->ia_mode,
120 &dentry->d_name, (void **)&label->label, &label->len);
127 nfs4_label_release_security(struct nfs4_label *label)
130 security_release_secctx(label->label, label->len);
132 static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
135 return server->attr_bitmask;
137 return server->attr_bitmask_nl;
140 static inline struct nfs4_label *
141 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
142 struct iattr *sattr, struct nfs4_label *l)
145 nfs4_label_release_security(struct nfs4_label *label)
148 nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
149 { return server->attr_bitmask; }
152 /* Prevent leaks of NFSv4 errors into userland */
153 static int nfs4_map_errors(int err)
158 case -NFS4ERR_RESOURCE:
159 case -NFS4ERR_LAYOUTTRYLATER:
160 case -NFS4ERR_RECALLCONFLICT:
162 case -NFS4ERR_WRONGSEC:
163 case -NFS4ERR_WRONG_CRED:
165 case -NFS4ERR_BADOWNER:
166 case -NFS4ERR_BADNAME:
168 case -NFS4ERR_SHARE_DENIED:
170 case -NFS4ERR_MINOR_VERS_MISMATCH:
171 return -EPROTONOSUPPORT;
172 case -NFS4ERR_FILE_OPEN:
175 dprintk("%s could not handle NFSv4 error %d\n",
183 * This is our standard bitmap for GETATTR requests.
185 const u32 nfs4_fattr_bitmap[3] = {
187 | FATTR4_WORD0_CHANGE
190 | FATTR4_WORD0_FILEID,
192 | FATTR4_WORD1_NUMLINKS
194 | FATTR4_WORD1_OWNER_GROUP
195 | FATTR4_WORD1_RAWDEV
196 | FATTR4_WORD1_SPACE_USED
197 | FATTR4_WORD1_TIME_ACCESS
198 | FATTR4_WORD1_TIME_METADATA
199 | FATTR4_WORD1_TIME_MODIFY
200 | FATTR4_WORD1_MOUNTED_ON_FILEID,
201 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
202 FATTR4_WORD2_SECURITY_LABEL
206 static const u32 nfs4_pnfs_open_bitmap[3] = {
208 | FATTR4_WORD0_CHANGE
211 | FATTR4_WORD0_FILEID,
213 | FATTR4_WORD1_NUMLINKS
215 | FATTR4_WORD1_OWNER_GROUP
216 | FATTR4_WORD1_RAWDEV
217 | FATTR4_WORD1_SPACE_USED
218 | FATTR4_WORD1_TIME_ACCESS
219 | FATTR4_WORD1_TIME_METADATA
220 | FATTR4_WORD1_TIME_MODIFY,
221 FATTR4_WORD2_MDSTHRESHOLD
222 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
223 | FATTR4_WORD2_SECURITY_LABEL
227 static const u32 nfs4_open_noattr_bitmap[3] = {
229 | FATTR4_WORD0_CHANGE
230 | FATTR4_WORD0_FILEID,
233 const u32 nfs4_statfs_bitmap[3] = {
234 FATTR4_WORD0_FILES_AVAIL
235 | FATTR4_WORD0_FILES_FREE
236 | FATTR4_WORD0_FILES_TOTAL,
237 FATTR4_WORD1_SPACE_AVAIL
238 | FATTR4_WORD1_SPACE_FREE
239 | FATTR4_WORD1_SPACE_TOTAL
242 const u32 nfs4_pathconf_bitmap[3] = {
244 | FATTR4_WORD0_MAXNAME,
248 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
249 | FATTR4_WORD0_MAXREAD
250 | FATTR4_WORD0_MAXWRITE
251 | FATTR4_WORD0_LEASE_TIME,
252 FATTR4_WORD1_TIME_DELTA
253 | FATTR4_WORD1_FS_LAYOUT_TYPES,
254 FATTR4_WORD2_LAYOUT_BLKSIZE
255 | FATTR4_WORD2_CLONE_BLKSIZE
258 const u32 nfs4_fs_locations_bitmap[3] = {
260 | FATTR4_WORD0_CHANGE
263 | FATTR4_WORD0_FILEID
264 | FATTR4_WORD0_FS_LOCATIONS,
266 | FATTR4_WORD1_NUMLINKS
268 | FATTR4_WORD1_OWNER_GROUP
269 | FATTR4_WORD1_RAWDEV
270 | FATTR4_WORD1_SPACE_USED
271 | FATTR4_WORD1_TIME_ACCESS
272 | FATTR4_WORD1_TIME_METADATA
273 | FATTR4_WORD1_TIME_MODIFY
274 | FATTR4_WORD1_MOUNTED_ON_FILEID,
277 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
278 struct nfs4_readdir_arg *readdir)
283 readdir->cookie = cookie;
284 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
289 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
294 * NFSv4 servers do not return entries for '.' and '..'
295 * Therefore, we fake these entries here. We let '.'
296 * have cookie 0 and '..' have cookie 1. Note that
297 * when talking to the server, we always send cookie 0
300 start = p = kmap_atomic(*readdir->pages);
303 *p++ = xdr_one; /* next */
304 *p++ = xdr_zero; /* cookie, first word */
305 *p++ = xdr_one; /* cookie, second word */
306 *p++ = xdr_one; /* entry len */
307 memcpy(p, ".\0\0\0", 4); /* entry */
309 *p++ = xdr_one; /* bitmap length */
310 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
311 *p++ = htonl(8); /* attribute buffer length */
312 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
315 *p++ = xdr_one; /* next */
316 *p++ = xdr_zero; /* cookie, first word */
317 *p++ = xdr_two; /* cookie, second word */
318 *p++ = xdr_two; /* entry len */
319 memcpy(p, "..\0\0", 4); /* entry */
321 *p++ = xdr_one; /* bitmap length */
322 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
323 *p++ = htonl(8); /* attribute buffer length */
324 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
326 readdir->pgbase = (char *)p - (char *)start;
327 readdir->count -= readdir->pgbase;
328 kunmap_atomic(start);
331 static void nfs4_test_and_free_stateid(struct nfs_server *server,
332 nfs4_stateid *stateid,
333 struct rpc_cred *cred)
335 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
337 ops->test_and_free_expired(server, stateid, cred);
340 static void __nfs4_free_revoked_stateid(struct nfs_server *server,
341 nfs4_stateid *stateid,
342 struct rpc_cred *cred)
344 stateid->type = NFS4_REVOKED_STATEID_TYPE;
345 nfs4_test_and_free_stateid(server, stateid, cred);
348 static void nfs4_free_revoked_stateid(struct nfs_server *server,
349 const nfs4_stateid *stateid,
350 struct rpc_cred *cred)
354 nfs4_stateid_copy(&tmp, stateid);
355 __nfs4_free_revoked_stateid(server, &tmp, cred);
358 static long nfs4_update_delay(long *timeout)
362 return NFS4_POLL_RETRY_MAX;
364 *timeout = NFS4_POLL_RETRY_MIN;
365 if (*timeout > NFS4_POLL_RETRY_MAX)
366 *timeout = NFS4_POLL_RETRY_MAX;
372 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
378 freezable_schedule_timeout_killable_unsafe(
379 nfs4_update_delay(timeout));
380 if (fatal_signal_pending(current))
385 /* This is the error handling routine for processes that are allowed
388 static int nfs4_do_handle_exception(struct nfs_server *server,
389 int errorcode, struct nfs4_exception *exception)
391 struct nfs_client *clp = server->nfs_client;
392 struct nfs4_state *state = exception->state;
393 const nfs4_stateid *stateid = exception->stateid;
394 struct inode *inode = exception->inode;
397 exception->delay = 0;
398 exception->recovering = 0;
399 exception->retry = 0;
401 if (stateid == NULL && state != NULL)
402 stateid = &state->stateid;
407 case -NFS4ERR_DELEG_REVOKED:
408 case -NFS4ERR_ADMIN_REVOKED:
409 case -NFS4ERR_EXPIRED:
410 case -NFS4ERR_BAD_STATEID:
411 if (inode != NULL && stateid != NULL) {
412 nfs_inode_find_state_and_recover(inode,
414 goto wait_on_recovery;
416 case -NFS4ERR_OPENMODE:
420 err = nfs_async_inode_return_delegation(inode,
423 goto wait_on_recovery;
424 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
425 exception->retry = 1;
431 ret = nfs4_schedule_stateid_recovery(server, state);
434 goto wait_on_recovery;
435 case -NFS4ERR_STALE_STATEID:
436 case -NFS4ERR_STALE_CLIENTID:
437 nfs4_schedule_lease_recovery(clp);
438 goto wait_on_recovery;
440 ret = nfs4_schedule_migration_recovery(server);
443 goto wait_on_recovery;
444 case -NFS4ERR_LEASE_MOVED:
445 nfs4_schedule_lease_moved_recovery(clp);
446 goto wait_on_recovery;
447 #if defined(CONFIG_NFS_V4_1)
448 case -NFS4ERR_BADSESSION:
449 case -NFS4ERR_BADSLOT:
450 case -NFS4ERR_BAD_HIGH_SLOT:
451 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
452 case -NFS4ERR_DEADSESSION:
453 case -NFS4ERR_SEQ_FALSE_RETRY:
454 case -NFS4ERR_SEQ_MISORDERED:
455 dprintk("%s ERROR: %d Reset session\n", __func__,
457 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
458 goto wait_on_recovery;
459 #endif /* defined(CONFIG_NFS_V4_1) */
460 case -NFS4ERR_FILE_OPEN:
461 if (exception->timeout > HZ) {
462 /* We have retried a decent amount, time to
469 nfs_inc_server_stats(server, NFSIOS_DELAY);
471 case -NFS4ERR_LAYOUTTRYLATER:
472 case -NFS4ERR_RECALLCONFLICT:
473 exception->delay = 1;
476 case -NFS4ERR_RETRY_UNCACHED_REP:
477 case -NFS4ERR_OLD_STATEID:
478 exception->retry = 1;
480 case -NFS4ERR_BADOWNER:
481 /* The following works around a Linux server bug! */
482 case -NFS4ERR_BADNAME:
483 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
484 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
485 exception->retry = 1;
486 printk(KERN_WARNING "NFS: v4 server %s "
487 "does not accept raw "
489 "Reenabling the idmapper.\n",
490 server->nfs_client->cl_hostname);
493 /* We failed to handle the error */
494 return nfs4_map_errors(ret);
496 exception->recovering = 1;
500 /* This is the error handling routine for processes that are allowed
503 int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
505 struct nfs_client *clp = server->nfs_client;
508 ret = nfs4_do_handle_exception(server, errorcode, exception);
509 if (exception->delay) {
510 ret = nfs4_delay(server->client, &exception->timeout);
513 if (exception->recovering) {
514 ret = nfs4_wait_clnt_recover(clp);
515 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
522 exception->retry = 1;
527 nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
528 int errorcode, struct nfs4_exception *exception)
530 struct nfs_client *clp = server->nfs_client;
533 ret = nfs4_do_handle_exception(server, errorcode, exception);
534 if (exception->delay) {
535 rpc_delay(task, nfs4_update_delay(&exception->timeout));
538 if (exception->recovering) {
539 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
540 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
541 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
544 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
549 exception->retry = 1;
554 nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
555 struct nfs4_state *state, long *timeout)
557 struct nfs4_exception exception = {
561 if (task->tk_status >= 0)
564 exception.timeout = *timeout;
565 task->tk_status = nfs4_async_handle_exception(task, server,
568 if (exception.delay && timeout)
569 *timeout = exception.timeout;
576 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
577 * or 'false' otherwise.
579 static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
581 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
583 if (flavor == RPC_AUTH_GSS_KRB5I ||
584 flavor == RPC_AUTH_GSS_KRB5P)
590 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
592 spin_lock(&clp->cl_lock);
593 if (time_before(clp->cl_last_renewal,timestamp))
594 clp->cl_last_renewal = timestamp;
595 spin_unlock(&clp->cl_lock);
598 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
600 struct nfs_client *clp = server->nfs_client;
602 if (!nfs4_has_session(clp))
603 do_renew_lease(clp, timestamp);
606 struct nfs4_call_sync_data {
607 const struct nfs_server *seq_server;
608 struct nfs4_sequence_args *seq_args;
609 struct nfs4_sequence_res *seq_res;
612 void nfs4_init_sequence(struct nfs4_sequence_args *args,
613 struct nfs4_sequence_res *res, int cache_reply)
615 args->sa_slot = NULL;
616 args->sa_cache_this = cache_reply;
617 args->sa_privileged = 0;
622 static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
624 args->sa_privileged = 1;
627 int nfs40_setup_sequence(struct nfs4_slot_table *tbl,
628 struct nfs4_sequence_args *args,
629 struct nfs4_sequence_res *res,
630 struct rpc_task *task)
632 struct nfs4_slot *slot;
634 /* slot already allocated? */
635 if (res->sr_slot != NULL)
638 spin_lock(&tbl->slot_tbl_lock);
639 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
642 slot = nfs4_alloc_slot(tbl);
644 if (slot == ERR_PTR(-ENOMEM))
645 task->tk_timeout = HZ >> 2;
648 spin_unlock(&tbl->slot_tbl_lock);
650 slot->privileged = args->sa_privileged ? 1 : 0;
651 args->sa_slot = slot;
655 rpc_call_start(task);
659 if (args->sa_privileged)
660 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
661 NULL, RPC_PRIORITY_PRIVILEGED);
663 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
664 spin_unlock(&tbl->slot_tbl_lock);
667 EXPORT_SYMBOL_GPL(nfs40_setup_sequence);
669 static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
671 struct nfs4_slot *slot = res->sr_slot;
672 struct nfs4_slot_table *tbl;
675 spin_lock(&tbl->slot_tbl_lock);
676 if (!nfs41_wake_and_assign_slot(tbl, slot))
677 nfs4_free_slot(tbl, slot);
678 spin_unlock(&tbl->slot_tbl_lock);
683 static int nfs40_sequence_done(struct rpc_task *task,
684 struct nfs4_sequence_res *res)
686 if (res->sr_slot != NULL)
687 nfs40_sequence_free_slot(res);
691 #if defined(CONFIG_NFS_V4_1)
693 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
695 struct nfs4_session *session;
696 struct nfs4_slot_table *tbl;
697 struct nfs4_slot *slot = res->sr_slot;
698 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);
726 if (send_new_highest_used_slotid)
727 nfs41_notify_server(session->clp);
728 if (waitqueue_active(&tbl->slot_waitq))
729 wake_up_all(&tbl->slot_waitq);
732 static int nfs41_sequence_process(struct rpc_task *task,
733 struct nfs4_sequence_res *res)
735 struct nfs4_session *session;
736 struct nfs4_slot *slot = res->sr_slot;
737 struct nfs_client *clp;
738 bool interrupted = false;
743 /* don't increment the sequence number if the task wasn't sent */
744 if (!RPC_WAS_SENT(task))
747 session = slot->table->session;
749 if (slot->interrupted) {
750 slot->interrupted = 0;
754 trace_nfs4_sequence_done(session, res);
755 /* Check the SEQUENCE operation status */
756 switch (res->sr_status) {
758 /* Update the slot's sequence and clientid lease timer */
761 do_renew_lease(clp, res->sr_timestamp);
762 /* Check sequence flags */
763 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
765 nfs41_update_target_slotid(slot->table, slot, res);
769 * sr_status remains 1 if an RPC level error occurred.
770 * The server may or may not have processed the sequence
772 * Mark the slot as having hosted an interrupted RPC call.
774 slot->interrupted = 1;
777 /* The server detected a resend of the RPC call and
778 * returned NFS4ERR_DELAY as per Section 2.10.6.2
781 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
786 case -NFS4ERR_BADSLOT:
788 * The slot id we used was probably retired. Try again
789 * using a different slot id.
792 case -NFS4ERR_SEQ_MISORDERED:
794 * Was the last operation on this sequence interrupted?
795 * If so, retry after bumping the sequence number.
802 * Could this slot have been previously retired?
803 * If so, then the server may be expecting seq_nr = 1!
805 if (slot->seq_nr != 1) {
810 case -NFS4ERR_SEQ_FALSE_RETRY:
814 /* Just update the slot sequence no. */
818 /* The session may be reset by one of the error handlers. */
819 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
823 if (rpc_restart_call_prepare(task)) {
824 nfs41_sequence_free_slot(res);
830 if (!rpc_restart_call(task))
832 rpc_delay(task, NFS4_POLL_RETRY_MAX);
836 int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
838 if (!nfs41_sequence_process(task, res))
840 if (res->sr_slot != NULL)
841 nfs41_sequence_free_slot(res);
845 EXPORT_SYMBOL_GPL(nfs41_sequence_done);
847 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
849 if (res->sr_slot == NULL)
851 if (res->sr_slot->table->session != NULL)
852 return nfs41_sequence_process(task, res);
853 return nfs40_sequence_done(task, res);
856 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
858 if (res->sr_slot != NULL) {
859 if (res->sr_slot->table->session != NULL)
860 nfs41_sequence_free_slot(res);
862 nfs40_sequence_free_slot(res);
866 int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
868 if (res->sr_slot == NULL)
870 if (!res->sr_slot->table->session)
871 return nfs40_sequence_done(task, res);
872 return nfs41_sequence_done(task, res);
874 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
876 int nfs41_setup_sequence(struct nfs4_session *session,
877 struct nfs4_sequence_args *args,
878 struct nfs4_sequence_res *res,
879 struct rpc_task *task)
881 struct nfs4_slot *slot;
882 struct nfs4_slot_table *tbl;
884 dprintk("--> %s\n", __func__);
885 /* slot already allocated? */
886 if (res->sr_slot != NULL)
889 tbl = &session->fc_slot_table;
891 task->tk_timeout = 0;
893 spin_lock(&tbl->slot_tbl_lock);
894 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
895 !args->sa_privileged) {
896 /* The state manager will wait until the slot table is empty */
897 dprintk("%s session is draining\n", __func__);
901 slot = nfs4_alloc_slot(tbl);
903 /* If out of memory, try again in 1/4 second */
904 if (slot == ERR_PTR(-ENOMEM))
905 task->tk_timeout = HZ >> 2;
906 dprintk("<-- %s: no free slots\n", __func__);
909 spin_unlock(&tbl->slot_tbl_lock);
911 slot->privileged = args->sa_privileged ? 1 : 0;
912 args->sa_slot = slot;
914 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
915 slot->slot_nr, slot->seq_nr);
918 res->sr_timestamp = jiffies;
919 res->sr_status_flags = 0;
921 * sr_status is only set in decode_sequence, and so will remain
922 * set to 1 if an rpc level failure occurs.
925 trace_nfs4_setup_sequence(session, args);
927 rpc_call_start(task);
930 /* Privileged tasks are queued with top priority */
931 if (args->sa_privileged)
932 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
933 NULL, RPC_PRIORITY_PRIVILEGED);
935 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
936 spin_unlock(&tbl->slot_tbl_lock);
939 EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
941 static int nfs4_setup_sequence(const struct nfs_server *server,
942 struct nfs4_sequence_args *args,
943 struct nfs4_sequence_res *res,
944 struct rpc_task *task)
946 struct nfs4_session *session = nfs4_get_session(server);
950 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
953 dprintk("--> %s clp %p session %p sr_slot %u\n",
954 __func__, session->clp, session, res->sr_slot ?
955 res->sr_slot->slot_nr : NFS4_NO_SLOT);
957 ret = nfs41_setup_sequence(session, args, res, task);
959 dprintk("<-- %s status=%d\n", __func__, ret);
963 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
965 struct nfs4_call_sync_data *data = calldata;
966 struct nfs4_session *session = nfs4_get_session(data->seq_server);
968 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
970 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
973 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
975 struct nfs4_call_sync_data *data = calldata;
977 nfs41_sequence_done(task, data->seq_res);
980 static const struct rpc_call_ops nfs41_call_sync_ops = {
981 .rpc_call_prepare = nfs41_call_sync_prepare,
982 .rpc_call_done = nfs41_call_sync_done,
985 #else /* !CONFIG_NFS_V4_1 */
987 static int nfs4_setup_sequence(const struct nfs_server *server,
988 struct nfs4_sequence_args *args,
989 struct nfs4_sequence_res *res,
990 struct rpc_task *task)
992 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
996 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
998 return nfs40_sequence_done(task, res);
1001 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
1003 if (res->sr_slot != NULL)
1004 nfs40_sequence_free_slot(res);
1007 int nfs4_sequence_done(struct rpc_task *task,
1008 struct nfs4_sequence_res *res)
1010 return nfs40_sequence_done(task, res);
1012 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
1014 #endif /* !CONFIG_NFS_V4_1 */
1016 static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1018 struct nfs4_call_sync_data *data = calldata;
1019 nfs4_setup_sequence(data->seq_server,
1020 data->seq_args, data->seq_res, task);
1023 static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1025 struct nfs4_call_sync_data *data = calldata;
1026 nfs4_sequence_done(task, data->seq_res);
1029 static const struct rpc_call_ops nfs40_call_sync_ops = {
1030 .rpc_call_prepare = nfs40_call_sync_prepare,
1031 .rpc_call_done = nfs40_call_sync_done,
1034 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
1035 struct nfs_server *server,
1036 struct rpc_message *msg,
1037 struct nfs4_sequence_args *args,
1038 struct nfs4_sequence_res *res)
1041 struct rpc_task *task;
1042 struct nfs_client *clp = server->nfs_client;
1043 struct nfs4_call_sync_data data = {
1044 .seq_server = server,
1048 struct rpc_task_setup task_setup = {
1051 .callback_ops = clp->cl_mvops->call_sync_ops,
1052 .callback_data = &data
1055 task = rpc_run_task(&task_setup);
1057 ret = PTR_ERR(task);
1059 ret = task->tk_status;
1065 int nfs4_call_sync(struct rpc_clnt *clnt,
1066 struct nfs_server *server,
1067 struct rpc_message *msg,
1068 struct nfs4_sequence_args *args,
1069 struct nfs4_sequence_res *res,
1072 nfs4_init_sequence(args, res, cache_reply);
1073 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
1076 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
1078 struct nfs_inode *nfsi = NFS_I(dir);
1080 spin_lock(&dir->i_lock);
1081 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
1082 if (!cinfo->atomic || cinfo->before != dir->i_version)
1083 nfs_force_lookup_revalidate(dir);
1084 dir->i_version = cinfo->after;
1085 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
1086 nfs_fscache_invalidate(dir);
1087 spin_unlock(&dir->i_lock);
1090 struct nfs4_opendata {
1092 struct nfs_openargs o_arg;
1093 struct nfs_openres o_res;
1094 struct nfs_open_confirmargs c_arg;
1095 struct nfs_open_confirmres c_res;
1096 struct nfs4_string owner_name;
1097 struct nfs4_string group_name;
1098 struct nfs4_label *a_label;
1099 struct nfs_fattr f_attr;
1100 struct nfs4_label *f_label;
1102 struct dentry *dentry;
1103 struct nfs4_state_owner *owner;
1104 struct nfs4_state *state;
1106 unsigned long timestamp;
1107 unsigned int rpc_done : 1;
1108 unsigned int file_created : 1;
1109 unsigned int is_recover : 1;
1114 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1115 int err, struct nfs4_exception *exception)
1119 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1121 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1122 exception->retry = 1;
1127 nfs4_map_atomic_open_share(struct nfs_server *server,
1128 fmode_t fmode, int openflags)
1132 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1134 res = NFS4_SHARE_ACCESS_READ;
1137 res = NFS4_SHARE_ACCESS_WRITE;
1139 case FMODE_READ|FMODE_WRITE:
1140 res = NFS4_SHARE_ACCESS_BOTH;
1142 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1144 /* Want no delegation if we're using O_DIRECT */
1145 if (openflags & O_DIRECT)
1146 res |= NFS4_SHARE_WANT_NO_DELEG;
1151 static enum open_claim_type4
1152 nfs4_map_atomic_open_claim(struct nfs_server *server,
1153 enum open_claim_type4 claim)
1155 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1160 case NFS4_OPEN_CLAIM_FH:
1161 return NFS4_OPEN_CLAIM_NULL;
1162 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1163 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1164 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1165 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1169 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
1171 p->o_res.f_attr = &p->f_attr;
1172 p->o_res.f_label = p->f_label;
1173 p->o_res.seqid = p->o_arg.seqid;
1174 p->c_res.seqid = p->c_arg.seqid;
1175 p->o_res.server = p->o_arg.server;
1176 p->o_res.access_request = p->o_arg.access;
1177 nfs_fattr_init(&p->f_attr);
1178 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
1181 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
1182 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
1183 const struct iattr *attrs,
1184 struct nfs4_label *label,
1185 enum open_claim_type4 claim,
1188 struct dentry *parent = dget_parent(dentry);
1189 struct inode *dir = d_inode(parent);
1190 struct nfs_server *server = NFS_SERVER(dir);
1191 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
1192 struct nfs4_opendata *p;
1194 p = kzalloc(sizeof(*p), gfp_mask);
1198 p->f_label = nfs4_label_alloc(server, gfp_mask);
1199 if (IS_ERR(p->f_label))
1202 p->a_label = nfs4_label_alloc(server, gfp_mask);
1203 if (IS_ERR(p->a_label))
1206 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1207 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
1208 if (IS_ERR(p->o_arg.seqid))
1209 goto err_free_label;
1210 nfs_sb_active(dentry->d_sb);
1211 p->dentry = dget(dentry);
1214 atomic_inc(&sp->so_count);
1215 p->o_arg.open_flags = flags;
1216 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
1217 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1219 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1220 * will return permission denied for all bits until close */
1221 if (!(flags & O_EXCL)) {
1222 /* ask server to check for all possible rights as results
1224 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1225 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
1227 p->o_arg.clientid = server->nfs_client->cl_clientid;
1228 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1229 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1230 p->o_arg.name = &dentry->d_name;
1231 p->o_arg.server = server;
1232 p->o_arg.bitmask = nfs4_bitmask(server, label);
1233 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1234 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1235 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1236 switch (p->o_arg.claim) {
1237 case NFS4_OPEN_CLAIM_NULL:
1238 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1239 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1240 p->o_arg.fh = NFS_FH(dir);
1242 case NFS4_OPEN_CLAIM_PREVIOUS:
1243 case NFS4_OPEN_CLAIM_FH:
1244 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1245 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1246 p->o_arg.fh = NFS_FH(d_inode(dentry));
1248 if (attrs != NULL && attrs->ia_valid != 0) {
1251 p->o_arg.u.attrs = &p->attrs;
1252 memcpy(&p->attrs, attrs, sizeof(p->attrs));
1255 verf[1] = current->pid;
1256 memcpy(p->o_arg.u.verifier.data, verf,
1257 sizeof(p->o_arg.u.verifier.data));
1259 p->c_arg.fh = &p->o_res.fh;
1260 p->c_arg.stateid = &p->o_res.stateid;
1261 p->c_arg.seqid = p->o_arg.seqid;
1262 nfs4_init_opendata_res(p);
1263 kref_init(&p->kref);
1267 nfs4_label_free(p->a_label);
1269 nfs4_label_free(p->f_label);
1277 static void nfs4_opendata_free(struct kref *kref)
1279 struct nfs4_opendata *p = container_of(kref,
1280 struct nfs4_opendata, kref);
1281 struct super_block *sb = p->dentry->d_sb;
1283 nfs_free_seqid(p->o_arg.seqid);
1284 nfs4_sequence_free_slot(&p->o_res.seq_res);
1285 if (p->state != NULL)
1286 nfs4_put_open_state(p->state);
1287 nfs4_put_state_owner(p->owner);
1289 nfs4_label_free(p->a_label);
1290 nfs4_label_free(p->f_label);
1294 nfs_sb_deactive(sb);
1295 nfs_fattr_free_names(&p->f_attr);
1296 kfree(p->f_attr.mdsthreshold);
1300 static void nfs4_opendata_put(struct nfs4_opendata *p)
1303 kref_put(&p->kref, nfs4_opendata_free);
1306 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1310 ret = rpc_wait_for_completion_task(task);
1314 static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1317 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1318 case FMODE_READ|FMODE_WRITE:
1319 return state->n_rdwr != 0;
1321 return state->n_wronly != 0;
1323 return state->n_rdonly != 0;
1329 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
1333 if (open_mode & (O_EXCL|O_TRUNC))
1335 switch (mode & (FMODE_READ|FMODE_WRITE)) {
1337 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1338 && state->n_rdonly != 0;
1341 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1342 && state->n_wronly != 0;
1344 case FMODE_READ|FMODE_WRITE:
1345 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1346 && state->n_rdwr != 0;
1352 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1353 enum open_claim_type4 claim)
1355 if (delegation == NULL)
1357 if ((delegation->type & fmode) != fmode)
1359 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1362 case NFS4_OPEN_CLAIM_NULL:
1363 case NFS4_OPEN_CLAIM_FH:
1365 case NFS4_OPEN_CLAIM_PREVIOUS:
1366 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1371 nfs_mark_delegation_referenced(delegation);
1375 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1384 case FMODE_READ|FMODE_WRITE:
1387 nfs4_state_set_mode_locked(state, state->state | fmode);
1390 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
1392 struct nfs_client *clp = state->owner->so_server->nfs_client;
1393 bool need_recover = false;
1395 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1396 need_recover = true;
1397 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1398 need_recover = true;
1399 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1400 need_recover = true;
1402 nfs4_state_mark_reclaim_nograce(clp, state);
1405 static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1406 const nfs4_stateid *stateid, nfs4_stateid *freeme)
1408 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1410 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1411 nfs4_stateid_copy(freeme, &state->open_stateid);
1412 nfs_test_and_clear_all_open_stateid(state);
1415 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1420 static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1422 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1424 if (state->n_wronly)
1425 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1426 if (state->n_rdonly)
1427 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1429 set_bit(NFS_O_RDWR_STATE, &state->flags);
1430 set_bit(NFS_OPEN_STATE, &state->flags);
1433 static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1434 nfs4_stateid *arg_stateid,
1435 nfs4_stateid *stateid, fmode_t fmode)
1437 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1438 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1440 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1443 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1446 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1447 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1448 clear_bit(NFS_OPEN_STATE, &state->flags);
1450 if (stateid == NULL)
1452 /* Handle races with OPEN */
1453 if (!nfs4_stateid_match_other(arg_stateid, &state->open_stateid) ||
1454 (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1455 !nfs4_stateid_is_newer(stateid, &state->open_stateid))) {
1456 nfs_resync_open_stateid_locked(state);
1459 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1460 nfs4_stateid_copy(&state->stateid, stateid);
1461 nfs4_stateid_copy(&state->open_stateid, stateid);
1464 static void nfs_clear_open_stateid(struct nfs4_state *state,
1465 nfs4_stateid *arg_stateid,
1466 nfs4_stateid *stateid, fmode_t fmode)
1468 write_seqlock(&state->seqlock);
1469 nfs_clear_open_stateid_locked(state, arg_stateid, stateid, fmode);
1470 write_sequnlock(&state->seqlock);
1471 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1472 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1475 static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1476 const nfs4_stateid *stateid, fmode_t fmode,
1477 nfs4_stateid *freeme)
1481 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1484 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1486 case FMODE_READ|FMODE_WRITE:
1487 set_bit(NFS_O_RDWR_STATE, &state->flags);
1489 if (!nfs_need_update_open_stateid(state, stateid, freeme))
1491 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1492 nfs4_stateid_copy(&state->stateid, stateid);
1493 nfs4_stateid_copy(&state->open_stateid, stateid);
1496 static void __update_open_stateid(struct nfs4_state *state,
1497 const nfs4_stateid *open_stateid,
1498 const nfs4_stateid *deleg_stateid,
1500 nfs4_stateid *freeme)
1503 * Protect the call to nfs4_state_set_mode_locked and
1504 * serialise the stateid update
1506 spin_lock(&state->owner->so_lock);
1507 write_seqlock(&state->seqlock);
1508 if (deleg_stateid != NULL) {
1509 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1510 set_bit(NFS_DELEGATED_STATE, &state->flags);
1512 if (open_stateid != NULL)
1513 nfs_set_open_stateid_locked(state, open_stateid, fmode, freeme);
1514 write_sequnlock(&state->seqlock);
1515 update_open_stateflags(state, fmode);
1516 spin_unlock(&state->owner->so_lock);
1519 static int update_open_stateid(struct nfs4_state *state,
1520 const nfs4_stateid *open_stateid,
1521 const nfs4_stateid *delegation,
1524 struct nfs_server *server = NFS_SERVER(state->inode);
1525 struct nfs_client *clp = server->nfs_client;
1526 struct nfs_inode *nfsi = NFS_I(state->inode);
1527 struct nfs_delegation *deleg_cur;
1528 nfs4_stateid freeme = {0};
1531 fmode &= (FMODE_READ|FMODE_WRITE);
1534 deleg_cur = rcu_dereference(nfsi->delegation);
1535 if (deleg_cur == NULL)
1538 spin_lock(&deleg_cur->lock);
1539 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1540 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1541 (deleg_cur->type & fmode) != fmode)
1542 goto no_delegation_unlock;
1544 if (delegation == NULL)
1545 delegation = &deleg_cur->stateid;
1546 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
1547 goto no_delegation_unlock;
1549 nfs_mark_delegation_referenced(deleg_cur);
1550 __update_open_stateid(state, open_stateid, &deleg_cur->stateid,
1553 no_delegation_unlock:
1554 spin_unlock(&deleg_cur->lock);
1558 if (!ret && open_stateid != NULL) {
1559 __update_open_stateid(state, open_stateid, NULL, fmode, &freeme);
1562 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1563 nfs4_schedule_state_manager(clp);
1564 if (freeme.type != 0)
1565 nfs4_test_and_free_stateid(server, &freeme,
1566 state->owner->so_cred);
1571 static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1572 const nfs4_stateid *stateid)
1574 struct nfs4_state *state = lsp->ls_state;
1577 spin_lock(&state->state_lock);
1578 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1580 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1582 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1585 spin_unlock(&state->state_lock);
1589 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1591 struct nfs_delegation *delegation;
1594 delegation = rcu_dereference(NFS_I(inode)->delegation);
1595 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1600 nfs4_inode_return_delegation(inode);
1603 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1605 struct nfs4_state *state = opendata->state;
1606 struct nfs_inode *nfsi = NFS_I(state->inode);
1607 struct nfs_delegation *delegation;
1608 int open_mode = opendata->o_arg.open_flags;
1609 fmode_t fmode = opendata->o_arg.fmode;
1610 enum open_claim_type4 claim = opendata->o_arg.claim;
1611 nfs4_stateid stateid;
1615 spin_lock(&state->owner->so_lock);
1616 if (can_open_cached(state, fmode, open_mode)) {
1617 update_open_stateflags(state, fmode);
1618 spin_unlock(&state->owner->so_lock);
1619 goto out_return_state;
1621 spin_unlock(&state->owner->so_lock);
1623 delegation = rcu_dereference(nfsi->delegation);
1624 if (!can_open_delegated(delegation, fmode, claim)) {
1628 /* Save the delegation */
1629 nfs4_stateid_copy(&stateid, &delegation->stateid);
1631 nfs_release_seqid(opendata->o_arg.seqid);
1632 if (!opendata->is_recover) {
1633 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1639 /* Try to update the stateid using the delegation */
1640 if (update_open_stateid(state, NULL, &stateid, fmode))
1641 goto out_return_state;
1644 return ERR_PTR(ret);
1646 atomic_inc(&state->count);
1651 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1653 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1654 struct nfs_delegation *delegation;
1655 int delegation_flags = 0;
1658 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1660 delegation_flags = delegation->flags;
1662 switch (data->o_arg.claim) {
1665 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1666 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1667 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1668 "returning a delegation for "
1669 "OPEN(CLAIM_DELEGATE_CUR)\n",
1673 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1674 nfs_inode_set_delegation(state->inode,
1675 data->owner->so_cred,
1678 nfs_inode_reclaim_delegation(state->inode,
1679 data->owner->so_cred,
1684 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1685 * and update the nfs4_state.
1687 static struct nfs4_state *
1688 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1690 struct inode *inode = data->state->inode;
1691 struct nfs4_state *state = data->state;
1694 if (!data->rpc_done) {
1695 if (data->rpc_status) {
1696 ret = data->rpc_status;
1699 /* cached opens have already been processed */
1703 ret = nfs_refresh_inode(inode, &data->f_attr);
1707 if (data->o_res.delegation_type != 0)
1708 nfs4_opendata_check_deleg(data, state);
1710 update_open_stateid(state, &data->o_res.stateid, NULL,
1712 atomic_inc(&state->count);
1716 return ERR_PTR(ret);
1720 static struct nfs4_state *
1721 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1723 struct inode *inode;
1724 struct nfs4_state *state = NULL;
1727 if (!data->rpc_done) {
1728 state = nfs4_try_open_cached(data);
1729 trace_nfs4_cached_open(data->state);
1734 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1736 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
1737 ret = PTR_ERR(inode);
1741 state = nfs4_get_open_state(inode, data->owner);
1744 if (data->o_res.delegation_type != 0)
1745 nfs4_opendata_check_deleg(data, state);
1746 update_open_stateid(state, &data->o_res.stateid, NULL,
1750 nfs_release_seqid(data->o_arg.seqid);
1755 return ERR_PTR(ret);
1758 static struct nfs4_state *
1759 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1761 struct nfs4_state *ret;
1763 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1764 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1766 ret = _nfs4_opendata_to_nfs4_state(data);
1767 nfs4_sequence_free_slot(&data->o_res.seq_res);
1771 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1773 struct nfs_inode *nfsi = NFS_I(state->inode);
1774 struct nfs_open_context *ctx;
1776 spin_lock(&state->inode->i_lock);
1777 list_for_each_entry(ctx, &nfsi->open_files, list) {
1778 if (ctx->state != state)
1780 get_nfs_open_context(ctx);
1781 spin_unlock(&state->inode->i_lock);
1784 spin_unlock(&state->inode->i_lock);
1785 return ERR_PTR(-ENOENT);
1788 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1789 struct nfs4_state *state, enum open_claim_type4 claim)
1791 struct nfs4_opendata *opendata;
1793 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
1794 NULL, NULL, claim, GFP_NOFS);
1795 if (opendata == NULL)
1796 return ERR_PTR(-ENOMEM);
1797 opendata->state = state;
1798 atomic_inc(&state->count);
1802 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1805 struct nfs4_state *newstate;
1808 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
1810 opendata->o_arg.open_flags = 0;
1811 opendata->o_arg.fmode = fmode;
1812 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1813 NFS_SB(opendata->dentry->d_sb),
1815 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1816 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1817 nfs4_init_opendata_res(opendata);
1818 ret = _nfs4_recover_proc_open(opendata);
1821 newstate = nfs4_opendata_to_nfs4_state(opendata);
1822 if (IS_ERR(newstate))
1823 return PTR_ERR(newstate);
1824 if (newstate != opendata->state)
1826 nfs4_close_state(newstate, fmode);
1830 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1834 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1835 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1836 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1837 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1838 /* memory barrier prior to reading state->n_* */
1839 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1840 clear_bit(NFS_OPEN_STATE, &state->flags);
1842 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1845 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1848 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1852 * We may have performed cached opens for all three recoveries.
1853 * Check if we need to update the current stateid.
1855 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1856 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
1857 write_seqlock(&state->seqlock);
1858 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1859 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1860 write_sequnlock(&state->seqlock);
1867 * reclaim state on the server after a reboot.
1869 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1871 struct nfs_delegation *delegation;
1872 struct nfs4_opendata *opendata;
1873 fmode_t delegation_type = 0;
1876 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1877 NFS4_OPEN_CLAIM_PREVIOUS);
1878 if (IS_ERR(opendata))
1879 return PTR_ERR(opendata);
1881 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1882 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1883 delegation_type = delegation->type;
1885 opendata->o_arg.u.delegation_type = delegation_type;
1886 status = nfs4_open_recover(opendata, state);
1887 nfs4_opendata_put(opendata);
1891 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1893 struct nfs_server *server = NFS_SERVER(state->inode);
1894 struct nfs4_exception exception = { };
1897 err = _nfs4_do_open_reclaim(ctx, state);
1898 trace_nfs4_open_reclaim(ctx, 0, err);
1899 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1901 if (err != -NFS4ERR_DELAY)
1903 nfs4_handle_exception(server, err, &exception);
1904 } while (exception.retry);
1908 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1910 struct nfs_open_context *ctx;
1913 ctx = nfs4_state_find_open_context(state);
1916 ret = nfs4_do_open_reclaim(ctx, state);
1917 put_nfs_open_context(ctx);
1921 static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, int err)
1925 printk(KERN_ERR "NFS: %s: unhandled error "
1926 "%d.\n", __func__, err);
1932 case -NFS4ERR_BADSESSION:
1933 case -NFS4ERR_BADSLOT:
1934 case -NFS4ERR_BAD_HIGH_SLOT:
1935 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1936 case -NFS4ERR_DEADSESSION:
1937 set_bit(NFS_DELEGATED_STATE, &state->flags);
1938 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1940 case -NFS4ERR_STALE_CLIENTID:
1941 case -NFS4ERR_STALE_STATEID:
1942 set_bit(NFS_DELEGATED_STATE, &state->flags);
1943 /* Don't recall a delegation if it was lost */
1944 nfs4_schedule_lease_recovery(server->nfs_client);
1946 case -NFS4ERR_MOVED:
1947 nfs4_schedule_migration_recovery(server);
1949 case -NFS4ERR_LEASE_MOVED:
1950 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1952 case -NFS4ERR_DELEG_REVOKED:
1953 case -NFS4ERR_ADMIN_REVOKED:
1954 case -NFS4ERR_EXPIRED:
1955 case -NFS4ERR_BAD_STATEID:
1956 case -NFS4ERR_OPENMODE:
1957 nfs_inode_find_state_and_recover(state->inode,
1959 nfs4_schedule_stateid_recovery(server, state);
1961 case -NFS4ERR_DELAY:
1962 case -NFS4ERR_GRACE:
1963 set_bit(NFS_DELEGATED_STATE, &state->flags);
1967 case -NFS4ERR_DENIED:
1968 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1974 int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
1975 struct nfs4_state *state, const nfs4_stateid *stateid,
1978 struct nfs_server *server = NFS_SERVER(state->inode);
1979 struct nfs4_opendata *opendata;
1982 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1983 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1984 if (IS_ERR(opendata))
1985 return PTR_ERR(opendata);
1986 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1987 write_seqlock(&state->seqlock);
1988 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1989 write_sequnlock(&state->seqlock);
1990 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1991 switch (type & (FMODE_READ|FMODE_WRITE)) {
1992 case FMODE_READ|FMODE_WRITE:
1994 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1997 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2001 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2003 nfs4_opendata_put(opendata);
2004 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
2007 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2009 struct nfs4_opendata *data = calldata;
2011 nfs40_setup_sequence(data->o_arg.server->nfs_client->cl_slot_tbl,
2012 &data->c_arg.seq_args, &data->c_res.seq_res, task);
2015 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2017 struct nfs4_opendata *data = calldata;
2019 nfs40_sequence_done(task, &data->c_res.seq_res);
2021 data->rpc_status = task->tk_status;
2022 if (data->rpc_status == 0) {
2023 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
2024 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2025 renew_lease(data->o_res.server, data->timestamp);
2030 static void nfs4_open_confirm_release(void *calldata)
2032 struct nfs4_opendata *data = calldata;
2033 struct nfs4_state *state = NULL;
2035 /* If this request hasn't been cancelled, do nothing */
2036 if (data->cancelled == 0)
2038 /* In case of error, no cleanup! */
2039 if (!data->rpc_done)
2041 state = nfs4_opendata_to_nfs4_state(data);
2043 nfs4_close_state(state, data->o_arg.fmode);
2045 nfs4_opendata_put(data);
2048 static const struct rpc_call_ops nfs4_open_confirm_ops = {
2049 .rpc_call_prepare = nfs4_open_confirm_prepare,
2050 .rpc_call_done = nfs4_open_confirm_done,
2051 .rpc_release = nfs4_open_confirm_release,
2055 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2057 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2059 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
2060 struct rpc_task *task;
2061 struct rpc_message msg = {
2062 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2063 .rpc_argp = &data->c_arg,
2064 .rpc_resp = &data->c_res,
2065 .rpc_cred = data->owner->so_cred,
2067 struct rpc_task_setup task_setup_data = {
2068 .rpc_client = server->client,
2069 .rpc_message = &msg,
2070 .callback_ops = &nfs4_open_confirm_ops,
2071 .callback_data = data,
2072 .workqueue = nfsiod_workqueue,
2073 .flags = RPC_TASK_ASYNC,
2077 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
2078 kref_get(&data->kref);
2080 data->rpc_status = 0;
2081 data->timestamp = jiffies;
2082 if (data->is_recover)
2083 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
2084 task = rpc_run_task(&task_setup_data);
2086 return PTR_ERR(task);
2087 status = nfs4_wait_for_completion_rpc_task(task);
2089 data->cancelled = 1;
2092 status = data->rpc_status;
2097 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
2099 struct nfs4_opendata *data = calldata;
2100 struct nfs4_state_owner *sp = data->owner;
2101 struct nfs_client *clp = sp->so_server->nfs_client;
2102 enum open_claim_type4 claim = data->o_arg.claim;
2104 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
2107 * Check if we still need to send an OPEN call, or if we can use
2108 * a delegation instead.
2110 if (data->state != NULL) {
2111 struct nfs_delegation *delegation;
2113 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
2116 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
2117 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
2118 goto unlock_no_action;
2121 /* Update client id. */
2122 data->o_arg.clientid = clp->cl_clientid;
2126 case NFS4_OPEN_CLAIM_PREVIOUS:
2127 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2128 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
2129 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
2130 case NFS4_OPEN_CLAIM_FH:
2131 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
2132 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
2134 data->timestamp = jiffies;
2135 if (nfs4_setup_sequence(data->o_arg.server,
2136 &data->o_arg.seq_args,
2137 &data->o_res.seq_res,
2139 nfs_release_seqid(data->o_arg.seqid);
2141 /* Set the create mode (note dependency on the session type) */
2142 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2143 if (data->o_arg.open_flags & O_EXCL) {
2144 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2145 if (nfs4_has_persistent_session(clp))
2146 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2147 else if (clp->cl_mvops->minor_version > 0)
2148 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2152 trace_nfs4_cached_open(data->state);
2155 task->tk_action = NULL;
2157 nfs4_sequence_done(task, &data->o_res.seq_res);
2160 static void nfs4_open_done(struct rpc_task *task, void *calldata)
2162 struct nfs4_opendata *data = calldata;
2164 data->rpc_status = task->tk_status;
2166 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
2169 if (task->tk_status == 0) {
2170 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2171 switch (data->o_res.f_attr->mode & S_IFMT) {
2175 data->rpc_status = -ELOOP;
2178 data->rpc_status = -EISDIR;
2181 data->rpc_status = -ENOTDIR;
2184 renew_lease(data->o_res.server, data->timestamp);
2185 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2186 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2191 static void nfs4_open_release(void *calldata)
2193 struct nfs4_opendata *data = calldata;
2194 struct nfs4_state *state = NULL;
2196 /* If this request hasn't been cancelled, do nothing */
2197 if (data->cancelled == 0)
2199 /* In case of error, no cleanup! */
2200 if (data->rpc_status != 0 || !data->rpc_done)
2202 /* In case we need an open_confirm, no cleanup! */
2203 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2205 state = nfs4_opendata_to_nfs4_state(data);
2207 nfs4_close_state(state, data->o_arg.fmode);
2209 nfs4_opendata_put(data);
2212 static const struct rpc_call_ops nfs4_open_ops = {
2213 .rpc_call_prepare = nfs4_open_prepare,
2214 .rpc_call_done = nfs4_open_done,
2215 .rpc_release = nfs4_open_release,
2218 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
2220 struct inode *dir = d_inode(data->dir);
2221 struct nfs_server *server = NFS_SERVER(dir);
2222 struct nfs_openargs *o_arg = &data->o_arg;
2223 struct nfs_openres *o_res = &data->o_res;
2224 struct rpc_task *task;
2225 struct rpc_message msg = {
2226 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2229 .rpc_cred = data->owner->so_cred,
2231 struct rpc_task_setup task_setup_data = {
2232 .rpc_client = server->client,
2233 .rpc_message = &msg,
2234 .callback_ops = &nfs4_open_ops,
2235 .callback_data = data,
2236 .workqueue = nfsiod_workqueue,
2237 .flags = RPC_TASK_ASYNC,
2241 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
2242 kref_get(&data->kref);
2244 data->rpc_status = 0;
2245 data->cancelled = 0;
2246 data->is_recover = 0;
2248 nfs4_set_sequence_privileged(&o_arg->seq_args);
2249 data->is_recover = 1;
2251 task = rpc_run_task(&task_setup_data);
2253 return PTR_ERR(task);
2254 status = nfs4_wait_for_completion_rpc_task(task);
2256 data->cancelled = 1;
2259 status = data->rpc_status;
2265 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2267 struct inode *dir = d_inode(data->dir);
2268 struct nfs_openres *o_res = &data->o_res;
2271 status = nfs4_run_open_task(data, 1);
2272 if (status != 0 || !data->rpc_done)
2275 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2277 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2278 status = _nfs4_proc_open_confirm(data);
2287 * Additional permission checks in order to distinguish between an
2288 * open for read, and an open for execute. This works around the
2289 * fact that NFSv4 OPEN treats read and execute permissions as being
2291 * Note that in the non-execute case, we want to turn off permission
2292 * checking if we just created a new file (POSIX open() semantics).
2294 static int nfs4_opendata_access(struct rpc_cred *cred,
2295 struct nfs4_opendata *opendata,
2296 struct nfs4_state *state, fmode_t fmode,
2299 struct nfs_access_entry cache;
2302 /* access call failed or for some reason the server doesn't
2303 * support any access modes -- defer access call until later */
2304 if (opendata->o_res.access_supported == 0)
2309 * Use openflags to check for exec, because fmode won't
2310 * always have FMODE_EXEC set when file open for exec.
2312 if (openflags & __FMODE_EXEC) {
2313 /* ONLY check for exec rights */
2315 } else if ((fmode & FMODE_READ) && !opendata->file_created)
2319 cache.jiffies = jiffies;
2320 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2321 nfs_access_add_cache(state->inode, &cache);
2323 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
2326 /* even though OPEN succeeded, access is denied. Close the file */
2327 nfs4_close_state(state, fmode);
2332 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2334 static int _nfs4_proc_open(struct nfs4_opendata *data)
2336 struct inode *dir = d_inode(data->dir);
2337 struct nfs_server *server = NFS_SERVER(dir);
2338 struct nfs_openargs *o_arg = &data->o_arg;
2339 struct nfs_openres *o_res = &data->o_res;
2342 status = nfs4_run_open_task(data, 0);
2343 if (!data->rpc_done)
2346 if (status == -NFS4ERR_BADNAME &&
2347 !(o_arg->open_flags & O_CREAT))
2352 nfs_fattr_map_and_free_names(server, &data->f_attr);
2354 if (o_arg->open_flags & O_CREAT) {
2355 update_changeattr(dir, &o_res->cinfo);
2356 if (o_arg->open_flags & O_EXCL)
2357 data->file_created = 1;
2358 else if (o_res->cinfo.before != o_res->cinfo.after)
2359 data->file_created = 1;
2361 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2362 server->caps &= ~NFS_CAP_POSIX_LOCK;
2363 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2364 status = _nfs4_proc_open_confirm(data);
2368 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
2369 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
2373 static int nfs4_recover_expired_lease(struct nfs_server *server)
2375 return nfs4_client_recover_expired_lease(server->nfs_client);
2380 * reclaim state on the server after a network partition.
2381 * Assumes caller holds the appropriate lock
2383 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2385 struct nfs4_opendata *opendata;
2388 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2389 NFS4_OPEN_CLAIM_FH);
2390 if (IS_ERR(opendata))
2391 return PTR_ERR(opendata);
2392 ret = nfs4_open_recover(opendata, state);
2394 d_drop(ctx->dentry);
2395 nfs4_opendata_put(opendata);
2399 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2401 struct nfs_server *server = NFS_SERVER(state->inode);
2402 struct nfs4_exception exception = { };
2406 err = _nfs4_open_expired(ctx, state);
2407 trace_nfs4_open_expired(ctx, 0, err);
2408 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2413 case -NFS4ERR_GRACE:
2414 case -NFS4ERR_DELAY:
2415 nfs4_handle_exception(server, err, &exception);
2418 } while (exception.retry);
2423 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2425 struct nfs_open_context *ctx;
2428 ctx = nfs4_state_find_open_context(state);
2431 ret = nfs4_do_open_expired(ctx, state);
2432 put_nfs_open_context(ctx);
2436 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2437 const nfs4_stateid *stateid)
2439 nfs_remove_bad_delegation(state->inode, stateid);
2440 write_seqlock(&state->seqlock);
2441 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2442 write_sequnlock(&state->seqlock);
2443 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2446 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2448 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2449 nfs_finish_clear_delegation_stateid(state, NULL);
2452 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2454 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2455 nfs40_clear_delegation_stateid(state);
2456 return nfs4_open_expired(sp, state);
2459 static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2460 nfs4_stateid *stateid,
2461 struct rpc_cred *cred)
2463 return -NFS4ERR_BAD_STATEID;
2466 #if defined(CONFIG_NFS_V4_1)
2467 static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2468 nfs4_stateid *stateid,
2469 struct rpc_cred *cred)
2473 switch (stateid->type) {
2476 case NFS4_INVALID_STATEID_TYPE:
2477 case NFS4_SPECIAL_STATEID_TYPE:
2478 return -NFS4ERR_BAD_STATEID;
2479 case NFS4_REVOKED_STATEID_TYPE:
2483 status = nfs41_test_stateid(server, stateid, cred);
2485 case -NFS4ERR_EXPIRED:
2486 case -NFS4ERR_ADMIN_REVOKED:
2487 case -NFS4ERR_DELEG_REVOKED:
2493 /* Ack the revoked state to the server */
2494 nfs41_free_stateid(server, stateid, cred, true);
2495 return -NFS4ERR_EXPIRED;
2498 static void nfs41_check_delegation_stateid(struct nfs4_state *state)
2500 struct nfs_server *server = NFS_SERVER(state->inode);
2501 nfs4_stateid stateid;
2502 struct nfs_delegation *delegation;
2503 struct rpc_cred *cred;
2506 /* Get the delegation credential for use by test/free_stateid */
2508 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2509 if (delegation == NULL) {
2514 nfs4_stateid_copy(&stateid, &delegation->stateid);
2515 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
2517 nfs_finish_clear_delegation_stateid(state, &stateid);
2521 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags)) {
2526 cred = get_rpccred(delegation->cred);
2528 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
2529 trace_nfs4_test_delegation_stateid(state, NULL, status);
2530 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
2531 nfs_finish_clear_delegation_stateid(state, &stateid);
2537 * nfs41_check_expired_locks - possibly free a lock stateid
2539 * @state: NFSv4 state for an inode
2541 * Returns NFS_OK if recovery for this stateid is now finished.
2542 * Otherwise a negative NFS4ERR value is returned.
2544 static int nfs41_check_expired_locks(struct nfs4_state *state)
2546 int status, ret = NFS_OK;
2547 struct nfs4_lock_state *lsp;
2548 struct nfs_server *server = NFS_SERVER(state->inode);
2550 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2552 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2553 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2554 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2556 status = nfs41_test_and_free_expired_stateid(server,
2559 trace_nfs4_test_lock_stateid(state, lsp, status);
2560 if (status == -NFS4ERR_EXPIRED ||
2561 status == -NFS4ERR_BAD_STATEID) {
2562 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
2563 if (!recover_lost_locks)
2564 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2565 } else if (status != NFS_OK) {
2576 * nfs41_check_open_stateid - possibly free an open stateid
2578 * @state: NFSv4 state for an inode
2580 * Returns NFS_OK if recovery for this stateid is now finished.
2581 * Otherwise a negative NFS4ERR value is returned.
2583 static int nfs41_check_open_stateid(struct nfs4_state *state)
2585 struct nfs_server *server = NFS_SERVER(state->inode);
2586 nfs4_stateid *stateid = &state->open_stateid;
2587 struct rpc_cred *cred = state->owner->so_cred;
2590 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
2591 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2593 return -NFS4ERR_BAD_STATEID;
2595 /* If a state reset has been done, test_stateid is unneeded */
2596 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2597 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2598 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2599 return -NFS4ERR_BAD_STATEID;
2601 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
2602 trace_nfs4_test_open_stateid(state, NULL, status);
2603 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
2604 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2605 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2606 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2607 clear_bit(NFS_OPEN_STATE, &state->flags);
2612 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2616 nfs41_check_delegation_stateid(state);
2617 status = nfs41_check_expired_locks(state);
2618 if (status != NFS_OK)
2620 status = nfs41_check_open_stateid(state);
2621 if (status != NFS_OK)
2622 status = nfs4_open_expired(sp, state);
2628 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2629 * fields corresponding to attributes that were used to store the verifier.
2630 * Make sure we clobber those fields in the later setattr call
2632 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2633 struct iattr *sattr, struct nfs4_label **label)
2635 const u32 *attrset = opendata->o_res.attrset;
2637 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2638 !(sattr->ia_valid & ATTR_ATIME_SET))
2639 sattr->ia_valid |= ATTR_ATIME;
2641 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2642 !(sattr->ia_valid & ATTR_MTIME_SET))
2643 sattr->ia_valid |= ATTR_MTIME;
2645 /* Except MODE, it seems harmless of setting twice. */
2646 if ((attrset[1] & FATTR4_WORD1_MODE))
2647 sattr->ia_valid &= ~ATTR_MODE;
2649 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2653 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2656 struct nfs_open_context *ctx)
2658 struct nfs4_state_owner *sp = opendata->owner;
2659 struct nfs_server *server = sp->so_server;
2660 struct dentry *dentry;
2661 struct nfs4_state *state;
2665 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2667 ret = _nfs4_proc_open(opendata);
2671 state = nfs4_opendata_to_nfs4_state(opendata);
2672 ret = PTR_ERR(state);
2675 if (server->caps & NFS_CAP_POSIX_LOCK)
2676 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2677 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2678 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
2680 dentry = opendata->dentry;
2681 if (d_really_is_negative(dentry)) {
2682 struct dentry *alias;
2684 alias = d_exact_alias(dentry, state->inode);
2686 alias = d_splice_alias(igrab(state->inode), dentry);
2687 /* d_splice_alias() can't fail here - it's a non-directory */
2690 ctx->dentry = dentry = alias;
2692 nfs_set_verifier(dentry,
2693 nfs_save_change_attribute(d_inode(opendata->dir)));
2696 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2701 if (d_inode(dentry) == state->inode) {
2702 nfs_inode_attach_open_context(ctx);
2703 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2704 nfs4_schedule_stateid_recovery(server, state);
2711 * Returns a referenced nfs4_state
2713 static int _nfs4_do_open(struct inode *dir,
2714 struct nfs_open_context *ctx,
2716 struct iattr *sattr,
2717 struct nfs4_label *label,
2720 struct nfs4_state_owner *sp;
2721 struct nfs4_state *state = NULL;
2722 struct nfs_server *server = NFS_SERVER(dir);
2723 struct nfs4_opendata *opendata;
2724 struct dentry *dentry = ctx->dentry;
2725 struct rpc_cred *cred = ctx->cred;
2726 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2727 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
2728 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
2729 struct nfs4_label *olabel = NULL;
2732 /* Protect against reboot recovery conflicts */
2734 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2736 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2739 status = nfs4_recover_expired_lease(server);
2741 goto err_put_state_owner;
2742 if (d_really_is_positive(dentry))
2743 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
2745 if (d_really_is_positive(dentry))
2746 claim = NFS4_OPEN_CLAIM_FH;
2747 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
2748 label, claim, GFP_KERNEL);
2749 if (opendata == NULL)
2750 goto err_put_state_owner;
2753 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2754 if (IS_ERR(olabel)) {
2755 status = PTR_ERR(olabel);
2756 goto err_opendata_put;
2760 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2761 if (!opendata->f_attr.mdsthreshold) {
2762 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2763 if (!opendata->f_attr.mdsthreshold)
2764 goto err_free_label;
2766 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
2768 if (d_really_is_positive(dentry))
2769 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
2771 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
2773 goto err_free_label;
2776 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
2777 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
2778 nfs4_exclusive_attrset(opendata, sattr, &label);
2780 * send create attributes which was not set by open
2781 * with an extra setattr.
2783 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2784 nfs_fattr_init(opendata->o_res.f_attr);
2785 status = nfs4_do_setattr(state->inode, cred,
2786 opendata->o_res.f_attr, sattr,
2787 state, label, olabel);
2789 nfs_setattr_update_inode(state->inode, sattr,
2790 opendata->o_res.f_attr);
2791 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2795 if (opened && opendata->file_created)
2796 *opened |= FILE_CREATED;
2798 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
2799 *ctx_th = opendata->f_attr.mdsthreshold;
2800 opendata->f_attr.mdsthreshold = NULL;
2803 nfs4_label_free(olabel);
2805 nfs4_opendata_put(opendata);
2806 nfs4_put_state_owner(sp);
2809 nfs4_label_free(olabel);
2811 nfs4_opendata_put(opendata);
2812 err_put_state_owner:
2813 nfs4_put_state_owner(sp);
2819 static struct nfs4_state *nfs4_do_open(struct inode *dir,
2820 struct nfs_open_context *ctx,
2822 struct iattr *sattr,
2823 struct nfs4_label *label,
2826 struct nfs_server *server = NFS_SERVER(dir);
2827 struct nfs4_exception exception = { };
2828 struct nfs4_state *res;
2832 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
2834 trace_nfs4_open_file(ctx, flags, status);
2837 /* NOTE: BAD_SEQID means the server and client disagree about the
2838 * book-keeping w.r.t. state-changing operations
2839 * (OPEN/CLOSE/LOCK/LOCKU...)
2840 * It is actually a sign of a bug on the client or on the server.
2842 * If we receive a BAD_SEQID error in the particular case of
2843 * doing an OPEN, we assume that nfs_increment_open_seqid() will
2844 * have unhashed the old state_owner for us, and that we can
2845 * therefore safely retry using a new one. We should still warn
2846 * the user though...
2848 if (status == -NFS4ERR_BAD_SEQID) {
2849 pr_warn_ratelimited("NFS: v4 server %s "
2850 " returned a bad sequence-id error!\n",
2851 NFS_SERVER(dir)->nfs_client->cl_hostname);
2852 exception.retry = 1;
2856 * BAD_STATEID on OPEN means that the server cancelled our
2857 * state before it received the OPEN_CONFIRM.
2858 * Recover by retrying the request as per the discussion
2859 * on Page 181 of RFC3530.
2861 if (status == -NFS4ERR_BAD_STATEID) {
2862 exception.retry = 1;
2865 if (status == -EAGAIN) {
2866 /* We must have found a delegation */
2867 exception.retry = 1;
2870 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2872 res = ERR_PTR(nfs4_handle_exception(server,
2873 status, &exception));
2874 } while (exception.retry);
2878 static int _nfs4_do_setattr(struct inode *inode,
2879 struct nfs_setattrargs *arg,
2880 struct nfs_setattrres *res,
2881 struct rpc_cred *cred,
2882 struct nfs4_state *state)
2884 struct nfs_server *server = NFS_SERVER(inode);
2885 struct rpc_message msg = {
2886 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2891 struct rpc_cred *delegation_cred = NULL;
2892 unsigned long timestamp = jiffies;
2897 nfs_fattr_init(res->fattr);
2899 /* Servers should only apply open mode checks for file size changes */
2900 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
2901 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2903 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
2904 /* Use that stateid */
2905 } else if (truncate && state != NULL) {
2906 struct nfs_lockowner lockowner = {
2907 .l_owner = current->files,
2908 .l_pid = current->tgid,
2910 if (!nfs4_valid_open_stateid(state))
2912 if (nfs4_select_rw_stateid(state, FMODE_WRITE, &lockowner,
2913 &arg->stateid, &delegation_cred) == -EIO)
2916 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
2917 if (delegation_cred)
2918 msg.rpc_cred = delegation_cred;
2920 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
2922 put_rpccred(delegation_cred);
2923 if (status == 0 && state != NULL)
2924 renew_lease(server, timestamp);
2925 trace_nfs4_setattr(inode, &arg->stateid, status);
2929 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2930 struct nfs_fattr *fattr, struct iattr *sattr,
2931 struct nfs4_state *state, struct nfs4_label *ilabel,
2932 struct nfs4_label *olabel)
2934 struct nfs_server *server = NFS_SERVER(inode);
2935 struct nfs_setattrargs arg = {
2936 .fh = NFS_FH(inode),
2939 .bitmask = server->attr_bitmask,
2942 struct nfs_setattrres res = {
2947 struct nfs4_exception exception = {
2950 .stateid = &arg.stateid,
2954 arg.bitmask = nfs4_bitmask(server, ilabel);
2956 arg.bitmask = nfs4_bitmask(server, olabel);
2959 err = _nfs4_do_setattr(inode, &arg, &res, cred, state);
2961 case -NFS4ERR_OPENMODE:
2962 if (!(sattr->ia_valid & ATTR_SIZE)) {
2963 pr_warn_once("NFSv4: server %s is incorrectly "
2964 "applying open mode checks to "
2965 "a SETATTR that is not "
2966 "changing file size.\n",
2967 server->nfs_client->cl_hostname);
2969 if (state && !(state->state & FMODE_WRITE)) {
2971 if (sattr->ia_valid & ATTR_OPEN)
2976 err = nfs4_handle_exception(server, err, &exception);
2977 } while (exception.retry);
2983 nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
2985 if (inode == NULL || !nfs_have_layout(inode))
2988 return pnfs_wait_on_layoutreturn(inode, task);
2991 struct nfs4_closedata {
2992 struct inode *inode;
2993 struct nfs4_state *state;
2994 struct nfs_closeargs arg;
2995 struct nfs_closeres res;
2996 struct nfs_fattr fattr;
2997 unsigned long timestamp;
3002 static void nfs4_free_closedata(void *data)
3004 struct nfs4_closedata *calldata = data;
3005 struct nfs4_state_owner *sp = calldata->state->owner;
3006 struct super_block *sb = calldata->state->inode->i_sb;
3009 pnfs_roc_release(calldata->state->inode);
3010 nfs4_put_open_state(calldata->state);
3011 nfs_free_seqid(calldata->arg.seqid);
3012 nfs4_put_state_owner(sp);
3013 nfs_sb_deactive(sb);
3017 static void nfs4_close_done(struct rpc_task *task, void *data)
3019 struct nfs4_closedata *calldata = data;
3020 struct nfs4_state *state = calldata->state;
3021 struct nfs_server *server = NFS_SERVER(calldata->inode);
3022 nfs4_stateid *res_stateid = NULL;
3024 dprintk("%s: begin!\n", __func__);
3025 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3027 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
3028 /* hmm. we are done with the inode, and in the process of freeing
3029 * the state_owner. we keep this around to process errors
3031 switch (task->tk_status) {
3033 res_stateid = &calldata->res.stateid;
3035 pnfs_roc_set_barrier(state->inode,
3036 calldata->roc_barrier);
3037 renew_lease(server, calldata->timestamp);
3039 case -NFS4ERR_ADMIN_REVOKED:
3040 case -NFS4ERR_STALE_STATEID:
3041 case -NFS4ERR_EXPIRED:
3042 nfs4_free_revoked_stateid(server,
3043 &calldata->arg.stateid,
3044 task->tk_msg.rpc_cred);
3045 case -NFS4ERR_OLD_STATEID:
3046 case -NFS4ERR_BAD_STATEID:
3047 if (!nfs4_stateid_match(&calldata->arg.stateid,
3048 &state->open_stateid)) {
3049 rpc_restart_call_prepare(task);
3052 if (calldata->arg.fmode == 0)
3055 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
3056 rpc_restart_call_prepare(task);
3060 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3061 res_stateid, calldata->arg.fmode);
3063 nfs_release_seqid(calldata->arg.seqid);
3064 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
3065 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
3068 static void nfs4_close_prepare(struct rpc_task *task, void *data)
3070 struct nfs4_closedata *calldata = data;
3071 struct nfs4_state *state = calldata->state;
3072 struct inode *inode = calldata->inode;
3073 bool is_rdonly, is_wronly, is_rdwr;
3076 dprintk("%s: begin!\n", __func__);
3077 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3080 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
3081 spin_lock(&state->owner->so_lock);
3082 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3083 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3084 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
3085 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
3086 /* Calculate the change in open mode */
3087 calldata->arg.fmode = 0;
3088 if (state->n_rdwr == 0) {
3089 if (state->n_rdonly == 0)
3090 call_close |= is_rdonly;
3092 calldata->arg.fmode |= FMODE_READ;
3093 if (state->n_wronly == 0)
3094 call_close |= is_wronly;
3096 calldata->arg.fmode |= FMODE_WRITE;
3097 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3098 call_close |= is_rdwr;
3100 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3102 if (!nfs4_valid_open_stateid(state))
3104 spin_unlock(&state->owner->so_lock);
3107 /* Note: exit _without_ calling nfs4_close_done */
3111 if (nfs4_wait_on_layoutreturn(inode, task)) {
3112 nfs_release_seqid(calldata->arg.seqid);
3116 if (calldata->arg.fmode == 0)
3117 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
3119 pnfs_roc_get_barrier(inode, &calldata->roc_barrier);
3121 calldata->arg.share_access =
3122 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3123 calldata->arg.fmode, 0);
3125 nfs_fattr_init(calldata->res.fattr);
3126 calldata->timestamp = jiffies;
3127 if (nfs4_setup_sequence(NFS_SERVER(inode),
3128 &calldata->arg.seq_args,
3129 &calldata->res.seq_res,
3131 nfs_release_seqid(calldata->arg.seqid);
3132 dprintk("%s: done!\n", __func__);
3135 task->tk_action = NULL;
3137 nfs4_sequence_done(task, &calldata->res.seq_res);
3140 static const struct rpc_call_ops nfs4_close_ops = {
3141 .rpc_call_prepare = nfs4_close_prepare,
3142 .rpc_call_done = nfs4_close_done,
3143 .rpc_release = nfs4_free_closedata,
3146 static bool nfs4_roc(struct inode *inode)
3148 if (!nfs_have_layout(inode))
3150 return pnfs_roc(inode);
3154 * It is possible for data to be read/written from a mem-mapped file
3155 * after the sys_close call (which hits the vfs layer as a flush).
3156 * This means that we can't safely call nfsv4 close on a file until
3157 * the inode is cleared. This in turn means that we are not good
3158 * NFSv4 citizens - we do not indicate to the server to update the file's
3159 * share state even when we are done with one of the three share
3160 * stateid's in the inode.
3162 * NOTE: Caller must be holding the sp->so_owner semaphore!
3164 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
3166 struct nfs_server *server = NFS_SERVER(state->inode);
3167 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
3168 struct nfs4_closedata *calldata;
3169 struct nfs4_state_owner *sp = state->owner;
3170 struct rpc_task *task;
3171 struct rpc_message msg = {
3172 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3173 .rpc_cred = state->owner->so_cred,
3175 struct rpc_task_setup task_setup_data = {
3176 .rpc_client = server->client,
3177 .rpc_message = &msg,
3178 .callback_ops = &nfs4_close_ops,
3179 .workqueue = nfsiod_workqueue,
3180 .flags = RPC_TASK_ASYNC,
3182 int status = -ENOMEM;
3184 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3185 &task_setup_data.rpc_client, &msg);
3187 calldata = kzalloc(sizeof(*calldata), gfp_mask);
3188 if (calldata == NULL)
3190 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
3191 calldata->inode = state->inode;
3192 calldata->state = state;
3193 calldata->arg.fh = NFS_FH(state->inode);
3194 /* Serialization for the sequence id */
3195 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3196 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
3197 if (IS_ERR(calldata->arg.seqid))
3198 goto out_free_calldata;
3199 calldata->arg.fmode = 0;
3200 calldata->arg.bitmask = server->cache_consistency_bitmask;
3201 calldata->res.fattr = &calldata->fattr;
3202 calldata->res.seqid = calldata->arg.seqid;
3203 calldata->res.server = server;
3204 calldata->roc = nfs4_roc(state->inode);
3205 nfs_sb_active(calldata->inode->i_sb);
3207 msg.rpc_argp = &calldata->arg;
3208 msg.rpc_resp = &calldata->res;
3209 task_setup_data.callback_data = calldata;
3210 task = rpc_run_task(&task_setup_data);
3212 return PTR_ERR(task);
3215 status = rpc_wait_for_completion_task(task);
3221 nfs4_put_open_state(state);
3222 nfs4_put_state_owner(sp);
3226 static struct inode *
3227 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3228 int open_flags, struct iattr *attr, int *opened)
3230 struct nfs4_state *state;
3231 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3233 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
3235 /* Protect against concurrent sillydeletes */
3236 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
3238 nfs4_label_release_security(label);
3241 return ERR_CAST(state);
3242 return state->inode;
3245 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
3247 if (ctx->state == NULL)
3250 nfs4_close_sync(ctx->state, ctx->mode);
3252 nfs4_close_state(ctx->state, ctx->mode);
3255 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3256 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3257 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
3259 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3261 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
3262 struct nfs4_server_caps_arg args = {
3266 struct nfs4_server_caps_res res = {};
3267 struct rpc_message msg = {
3268 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
3274 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3275 FATTR4_WORD0_FH_EXPIRE_TYPE |
3276 FATTR4_WORD0_LINK_SUPPORT |
3277 FATTR4_WORD0_SYMLINK_SUPPORT |
3278 FATTR4_WORD0_ACLSUPPORT;
3280 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3282 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3284 /* Sanity check the server answers */
3285 switch (minorversion) {
3287 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3288 res.attr_bitmask[2] = 0;
3291 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3294 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3296 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
3297 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3298 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3299 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3300 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
3301 NFS_CAP_CTIME|NFS_CAP_MTIME|
3302 NFS_CAP_SECURITY_LABEL);
3303 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3304 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3305 server->caps |= NFS_CAP_ACLS;
3306 if (res.has_links != 0)
3307 server->caps |= NFS_CAP_HARDLINKS;
3308 if (res.has_symlinks != 0)
3309 server->caps |= NFS_CAP_SYMLINKS;
3310 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3311 server->caps |= NFS_CAP_FILEID;
3312 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3313 server->caps |= NFS_CAP_MODE;
3314 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3315 server->caps |= NFS_CAP_NLINK;
3316 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3317 server->caps |= NFS_CAP_OWNER;
3318 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3319 server->caps |= NFS_CAP_OWNER_GROUP;
3320 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3321 server->caps |= NFS_CAP_ATIME;
3322 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3323 server->caps |= NFS_CAP_CTIME;
3324 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3325 server->caps |= NFS_CAP_MTIME;
3326 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3327 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3328 server->caps |= NFS_CAP_SECURITY_LABEL;
3330 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3331 sizeof(server->attr_bitmask));
3332 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3334 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3335 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3336 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
3337 server->cache_consistency_bitmask[2] = 0;
3338 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3339 sizeof(server->exclcreat_bitmask));
3340 server->acl_bitmask = res.acl_bitmask;
3341 server->fh_expire_type = res.fh_expire_type;
3347 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3349 struct nfs4_exception exception = { };
3352 err = nfs4_handle_exception(server,
3353 _nfs4_server_capabilities(server, fhandle),
3355 } while (exception.retry);
3359 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3360 struct nfs_fsinfo *info)
3363 struct nfs4_lookup_root_arg args = {
3366 struct nfs4_lookup_res res = {
3368 .fattr = info->fattr,
3371 struct rpc_message msg = {
3372 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3377 bitmask[0] = nfs4_fattr_bitmap[0];
3378 bitmask[1] = nfs4_fattr_bitmap[1];
3380 * Process the label in the upcoming getfattr
3382 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3384 nfs_fattr_init(info->fattr);
3385 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3388 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3389 struct nfs_fsinfo *info)
3391 struct nfs4_exception exception = { };
3394 err = _nfs4_lookup_root(server, fhandle, info);
3395 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
3398 case -NFS4ERR_WRONGSEC:
3401 err = nfs4_handle_exception(server, err, &exception);
3403 } while (exception.retry);
3408 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3409 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3411 struct rpc_auth_create_args auth_args = {
3412 .pseudoflavor = flavor,
3414 struct rpc_auth *auth;
3417 auth = rpcauth_create(&auth_args, server->client);
3422 ret = nfs4_lookup_root(server, fhandle, info);
3428 * Retry pseudoroot lookup with various security flavors. We do this when:
3430 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3431 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3433 * Returns zero on success, or a negative NFS4ERR value, or a
3434 * negative errno value.
3436 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3437 struct nfs_fsinfo *info)
3439 /* Per 3530bis 15.33.5 */
3440 static const rpc_authflavor_t flav_array[] = {
3444 RPC_AUTH_UNIX, /* courtesy */
3447 int status = -EPERM;
3450 if (server->auth_info.flavor_len > 0) {
3451 /* try each flavor specified by user */
3452 for (i = 0; i < server->auth_info.flavor_len; i++) {
3453 status = nfs4_lookup_root_sec(server, fhandle, info,
3454 server->auth_info.flavors[i]);
3455 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3460 /* no flavors specified by user, try default list */
3461 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3462 status = nfs4_lookup_root_sec(server, fhandle, info,
3464 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3471 * -EACCESS could mean that the user doesn't have correct permissions
3472 * to access the mount. It could also mean that we tried to mount
3473 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3474 * existing mount programs don't handle -EACCES very well so it should
3475 * be mapped to -EPERM instead.
3477 if (status == -EACCES)
3483 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3484 * @server: initialized nfs_server handle
3485 * @fhandle: we fill in the pseudo-fs root file handle
3486 * @info: we fill in an FSINFO struct
3487 * @auth_probe: probe the auth flavours
3489 * Returns zero on success, or a negative errno.
3491 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
3492 struct nfs_fsinfo *info,
3498 status = nfs4_lookup_root(server, fhandle, info);
3500 if (auth_probe || status == NFS4ERR_WRONGSEC)
3501 status = server->nfs_client->cl_mvops->find_root_sec(server,
3505 status = nfs4_server_capabilities(server, fhandle);
3507 status = nfs4_do_fsinfo(server, fhandle, info);
3509 return nfs4_map_errors(status);
3512 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3513 struct nfs_fsinfo *info)
3516 struct nfs_fattr *fattr = info->fattr;
3517 struct nfs4_label *label = NULL;
3519 error = nfs4_server_capabilities(server, mntfh);
3521 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3525 label = nfs4_label_alloc(server, GFP_KERNEL);
3527 return PTR_ERR(label);
3529 error = nfs4_proc_getattr(server, mntfh, fattr, label);
3531 dprintk("nfs4_get_root: getattr error = %d\n", -error);
3532 goto err_free_label;
3535 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3536 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3537 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3540 nfs4_label_free(label);
3546 * Get locations and (maybe) other attributes of a referral.
3547 * Note that we'll actually follow the referral later when
3548 * we detect fsid mismatch in inode revalidation
3550 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3551 const struct qstr *name, struct nfs_fattr *fattr,
3552 struct nfs_fh *fhandle)
3554 int status = -ENOMEM;
3555 struct page *page = NULL;
3556 struct nfs4_fs_locations *locations = NULL;
3558 page = alloc_page(GFP_KERNEL);
3561 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3562 if (locations == NULL)
3565 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
3570 * If the fsid didn't change, this is a migration event, not a
3571 * referral. Cause us to drop into the exception handler, which
3572 * will kick off migration recovery.
3574 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
3575 dprintk("%s: server did not return a different fsid for"
3576 " a referral at %s\n", __func__, name->name);
3577 status = -NFS4ERR_MOVED;
3580 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3581 nfs_fixup_referral_attributes(&locations->fattr);
3583 /* replace the lookup nfs_fattr with the locations nfs_fattr */
3584 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
3585 memset(fhandle, 0, sizeof(struct nfs_fh));
3593 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3594 struct nfs_fattr *fattr, struct nfs4_label *label)
3596 struct nfs4_getattr_arg args = {
3598 .bitmask = server->attr_bitmask,
3600 struct nfs4_getattr_res res = {
3605 struct rpc_message msg = {
3606 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3611 args.bitmask = nfs4_bitmask(server, label);
3613 nfs_fattr_init(fattr);
3614 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3617 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3618 struct nfs_fattr *fattr, struct nfs4_label *label)
3620 struct nfs4_exception exception = { };
3623 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3624 trace_nfs4_getattr(server, fhandle, fattr, err);
3625 err = nfs4_handle_exception(server, err,
3627 } while (exception.retry);
3632 * The file is not closed if it is opened due to the a request to change
3633 * the size of the file. The open call will not be needed once the
3634 * VFS layer lookup-intents are implemented.
3636 * Close is called when the inode is destroyed.
3637 * If we haven't opened the file for O_WRONLY, we
3638 * need to in the size_change case to obtain a stateid.
3641 * Because OPEN is always done by name in nfsv4, it is
3642 * possible that we opened a different file by the same
3643 * name. We can recognize this race condition, but we
3644 * can't do anything about it besides returning an error.
3646 * This will be fixed with VFS changes (lookup-intent).
3649 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3650 struct iattr *sattr)
3652 struct inode *inode = d_inode(dentry);
3653 struct rpc_cred *cred = NULL;
3654 struct nfs4_state *state = NULL;
3655 struct nfs4_label *label = NULL;
3658 if (pnfs_ld_layoutret_on_setattr(inode) &&
3659 sattr->ia_valid & ATTR_SIZE &&
3660 sattr->ia_size < i_size_read(inode))
3661 pnfs_commit_and_return_layout(inode);
3663 nfs_fattr_init(fattr);
3665 /* Deal with open(O_TRUNC) */
3666 if (sattr->ia_valid & ATTR_OPEN)
3667 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
3669 /* Optimization: if the end result is no change, don't RPC */
3670 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
3673 /* Search for an existing open(O_WRITE) file */
3674 if (sattr->ia_valid & ATTR_FILE) {
3675 struct nfs_open_context *ctx;
3677 ctx = nfs_file_open_context(sattr->ia_file);
3684 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3686 return PTR_ERR(label);
3688 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
3690 nfs_setattr_update_inode(inode, sattr, fattr);
3691 nfs_setsecurity(inode, fattr, label);
3693 nfs4_label_free(label);
3697 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3698 const struct qstr *name, struct nfs_fh *fhandle,
3699 struct nfs_fattr *fattr, struct nfs4_label *label)
3701 struct nfs_server *server = NFS_SERVER(dir);
3703 struct nfs4_lookup_arg args = {
3704 .bitmask = server->attr_bitmask,
3705 .dir_fh = NFS_FH(dir),
3708 struct nfs4_lookup_res res = {
3714 struct rpc_message msg = {
3715 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3720 args.bitmask = nfs4_bitmask(server, label);
3722 nfs_fattr_init(fattr);
3724 dprintk("NFS call lookup %s\n", name->name);
3725 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
3726 dprintk("NFS reply lookup: %d\n", status);
3730 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
3732 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
3733 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
3734 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3738 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3739 const struct qstr *name, struct nfs_fh *fhandle,
3740 struct nfs_fattr *fattr, struct nfs4_label *label)
3742 struct nfs4_exception exception = { };
3743 struct rpc_clnt *client = *clnt;
3746 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
3747 trace_nfs4_lookup(dir, name, err);
3749 case -NFS4ERR_BADNAME:
3752 case -NFS4ERR_MOVED:
3753 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
3754 if (err == -NFS4ERR_MOVED)
3755 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3757 case -NFS4ERR_WRONGSEC:
3759 if (client != *clnt)
3761 client = nfs4_negotiate_security(client, dir, name);
3763 return PTR_ERR(client);
3765 exception.retry = 1;
3768 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3770 } while (exception.retry);
3775 else if (client != *clnt)
3776 rpc_shutdown_client(client);
3781 static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
3782 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3783 struct nfs4_label *label)
3786 struct rpc_clnt *client = NFS_CLIENT(dir);
3788 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
3789 if (client != NFS_CLIENT(dir)) {
3790 rpc_shutdown_client(client);
3791 nfs_fixup_secinfo_attributes(fattr);
3797 nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
3798 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3800 struct rpc_clnt *client = NFS_CLIENT(dir);
3803 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
3805 return ERR_PTR(status);
3806 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
3809 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3811 struct nfs_server *server = NFS_SERVER(inode);
3812 struct nfs4_accessargs args = {
3813 .fh = NFS_FH(inode),
3814 .bitmask = server->cache_consistency_bitmask,
3816 struct nfs4_accessres res = {
3819 struct rpc_message msg = {
3820 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3823 .rpc_cred = entry->cred,
3825 int mode = entry->mask;
3829 * Determine which access bits we want to ask for...
3831 if (mode & MAY_READ)
3832 args.access |= NFS4_ACCESS_READ;
3833 if (S_ISDIR(inode->i_mode)) {
3834 if (mode & MAY_WRITE)
3835 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3836 if (mode & MAY_EXEC)
3837 args.access |= NFS4_ACCESS_LOOKUP;
3839 if (mode & MAY_WRITE)
3840 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3841 if (mode & MAY_EXEC)
3842 args.access |= NFS4_ACCESS_EXECUTE;
3845 res.fattr = nfs_alloc_fattr();
3846 if (res.fattr == NULL)
3849 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3851 nfs_access_set_mask(entry, res.access);
3852 nfs_refresh_inode(inode, res.fattr);
3854 nfs_free_fattr(res.fattr);
3858 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3860 struct nfs4_exception exception = { };
3863 err = _nfs4_proc_access(inode, entry);
3864 trace_nfs4_access(inode, err);
3865 err = nfs4_handle_exception(NFS_SERVER(inode), err,
3867 } while (exception.retry);
3872 * TODO: For the time being, we don't try to get any attributes
3873 * along with any of the zero-copy operations READ, READDIR,
3876 * In the case of the first three, we want to put the GETATTR
3877 * after the read-type operation -- this is because it is hard
3878 * to predict the length of a GETATTR response in v4, and thus
3879 * align the READ data correctly. This means that the GETATTR
3880 * may end up partially falling into the page cache, and we should
3881 * shift it into the 'tail' of the xdr_buf before processing.
3882 * To do this efficiently, we need to know the total length
3883 * of data received, which doesn't seem to be available outside
3886 * In the case of WRITE, we also want to put the GETATTR after
3887 * the operation -- in this case because we want to make sure
3888 * we get the post-operation mtime and size.
3890 * Both of these changes to the XDR layer would in fact be quite
3891 * minor, but I decided to leave them for a subsequent patch.
3893 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3894 unsigned int pgbase, unsigned int pglen)
3896 struct nfs4_readlink args = {
3897 .fh = NFS_FH(inode),
3902 struct nfs4_readlink_res res;
3903 struct rpc_message msg = {
3904 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3909 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
3912 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3913 unsigned int pgbase, unsigned int pglen)
3915 struct nfs4_exception exception = { };
3918 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3919 trace_nfs4_readlink(inode, err);
3920 err = nfs4_handle_exception(NFS_SERVER(inode), err,
3922 } while (exception.retry);
3927 * This is just for mknod. open(O_CREAT) will always do ->open_context().
3930 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
3933 struct nfs4_label l, *ilabel = NULL;
3934 struct nfs_open_context *ctx;
3935 struct nfs4_state *state;
3938 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3940 return PTR_ERR(ctx);
3942 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3944 sattr->ia_mode &= ~current_umask();
3945 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
3946 if (IS_ERR(state)) {
3947 status = PTR_ERR(state);
3951 nfs4_label_release_security(ilabel);
3952 put_nfs_open_context(ctx);
3956 static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
3958 struct nfs_server *server = NFS_SERVER(dir);
3959 struct nfs_removeargs args = {
3963 struct nfs_removeres res = {
3966 struct rpc_message msg = {
3967 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3973 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
3975 update_changeattr(dir, &res.cinfo);
3979 static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
3981 struct nfs4_exception exception = { };
3984 err = _nfs4_proc_remove(dir, name);
3985 trace_nfs4_remove(dir, name, err);
3986 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3988 } while (exception.retry);
3992 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
3994 struct nfs_server *server = NFS_SERVER(dir);
3995 struct nfs_removeargs *args = msg->rpc_argp;
3996 struct nfs_removeres *res = msg->rpc_resp;
3998 res->server = server;
3999 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
4000 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
4002 nfs_fattr_init(res->dir_attr);
4005 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4007 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb),
4008 &data->args.seq_args,
4013 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
4015 struct nfs_unlinkdata *data = task->tk_calldata;
4016 struct nfs_removeres *res = &data->res;
4018 if (!nfs4_sequence_done(task, &res->seq_res))
4020 if (nfs4_async_handle_error(task, res->server, NULL,
4021 &data->timeout) == -EAGAIN)
4023 update_changeattr(dir, &res->cinfo);
4027 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4029 struct nfs_server *server = NFS_SERVER(dir);
4030 struct nfs_renameargs *arg = msg->rpc_argp;
4031 struct nfs_renameres *res = msg->rpc_resp;
4033 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
4034 res->server = server;
4035 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
4038 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4040 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
4041 &data->args.seq_args,
4046 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4047 struct inode *new_dir)
4049 struct nfs_renamedata *data = task->tk_calldata;
4050 struct nfs_renameres *res = &data->res;
4052 if (!nfs4_sequence_done(task, &res->seq_res))
4054 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
4057 update_changeattr(old_dir, &res->old_cinfo);
4058 update_changeattr(new_dir, &res->new_cinfo);
4062 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4064 struct nfs_server *server = NFS_SERVER(inode);
4065 struct nfs4_link_arg arg = {
4066 .fh = NFS_FH(inode),
4067 .dir_fh = NFS_FH(dir),
4069 .bitmask = server->attr_bitmask,
4071 struct nfs4_link_res res = {
4075 struct rpc_message msg = {
4076 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4080 int status = -ENOMEM;
4082 res.fattr = nfs_alloc_fattr();
4083 if (res.fattr == NULL)
4086 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4087 if (IS_ERR(res.label)) {
4088 status = PTR_ERR(res.label);
4091 arg.bitmask = nfs4_bitmask(server, res.label);
4093 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4095 update_changeattr(dir, &res.cinfo);
4096 status = nfs_post_op_update_inode(inode, res.fattr);
4098 nfs_setsecurity(inode, res.fattr, res.label);
4102 nfs4_label_free(res.label);
4105 nfs_free_fattr(res.fattr);
4109 static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4111 struct nfs4_exception exception = { };
4114 err = nfs4_handle_exception(NFS_SERVER(inode),
4115 _nfs4_proc_link(inode, dir, name),
4117 } while (exception.retry);
4121 struct nfs4_createdata {
4122 struct rpc_message msg;
4123 struct nfs4_create_arg arg;
4124 struct nfs4_create_res res;
4126 struct nfs_fattr fattr;
4127 struct nfs4_label *label;
4130 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
4131 const struct qstr *name, struct iattr *sattr, u32 ftype)
4133 struct nfs4_createdata *data;
4135 data = kzalloc(sizeof(*data), GFP_KERNEL);
4137 struct nfs_server *server = NFS_SERVER(dir);
4139 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4140 if (IS_ERR(data->label))
4143 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4144 data->msg.rpc_argp = &data->arg;
4145 data->msg.rpc_resp = &data->res;
4146 data->arg.dir_fh = NFS_FH(dir);
4147 data->arg.server = server;
4148 data->arg.name = name;
4149 data->arg.attrs = sattr;
4150 data->arg.ftype = ftype;
4151 data->arg.bitmask = nfs4_bitmask(server, data->label);
4152 data->res.server = server;
4153 data->res.fh = &data->fh;
4154 data->res.fattr = &data->fattr;
4155 data->res.label = data->label;
4156 nfs_fattr_init(data->res.fattr);
4164 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4166 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
4167 &data->arg.seq_args, &data->res.seq_res, 1);
4169 update_changeattr(dir, &data->res.dir_cinfo);
4170 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
4175 static void nfs4_free_createdata(struct nfs4_createdata *data)
4177 nfs4_label_free(data->label);
4181 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4182 struct page *page, unsigned int len, struct iattr *sattr,
4183 struct nfs4_label *label)
4185 struct nfs4_createdata *data;
4186 int status = -ENAMETOOLONG;
4188 if (len > NFS4_MAXPATHLEN)
4192 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4196 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4197 data->arg.u.symlink.pages = &page;
4198 data->arg.u.symlink.len = len;
4199 data->arg.label = label;
4201 status = nfs4_do_create(dir, dentry, data);
4203 nfs4_free_createdata(data);
4208 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4209 struct page *page, unsigned int len, struct iattr *sattr)
4211 struct nfs4_exception exception = { };
4212 struct nfs4_label l, *label = NULL;
4215 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4218 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4219 trace_nfs4_symlink(dir, &dentry->d_name, err);
4220 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4222 } while (exception.retry);
4224 nfs4_label_release_security(label);
4228 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4229 struct iattr *sattr, struct nfs4_label *label)
4231 struct nfs4_createdata *data;
4232 int status = -ENOMEM;
4234 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4238 data->arg.label = label;
4239 status = nfs4_do_create(dir, dentry, data);
4241 nfs4_free_createdata(data);
4246 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4247 struct iattr *sattr)
4249 struct nfs4_exception exception = { };
4250 struct nfs4_label l, *label = NULL;
4253 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4255 sattr->ia_mode &= ~current_umask();
4257 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4258 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4259 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4261 } while (exception.retry);
4262 nfs4_label_release_security(label);
4267 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
4268 u64 cookie, struct page **pages, unsigned int count, int plus)
4270 struct inode *dir = d_inode(dentry);
4271 struct nfs4_readdir_arg args = {
4276 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
4279 struct nfs4_readdir_res res;
4280 struct rpc_message msg = {
4281 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4288 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4290 (unsigned long long)cookie);
4291 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
4292 res.pgbase = args.pgbase;
4293 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4295 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
4296 status += args.pgbase;
4299 nfs_invalidate_atime(dir);
4301 dprintk("%s: returns %d\n", __func__, status);
4305 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
4306 u64 cookie, struct page **pages, unsigned int count, int plus)
4308 struct nfs4_exception exception = { };
4311 err = _nfs4_proc_readdir(dentry, cred, cookie,
4312 pages, count, plus);
4313 trace_nfs4_readdir(d_inode(dentry), err);
4314 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
4316 } while (exception.retry);
4320 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4321 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
4323 struct nfs4_createdata *data;
4324 int mode = sattr->ia_mode;
4325 int status = -ENOMEM;
4327 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4332 data->arg.ftype = NF4FIFO;
4333 else if (S_ISBLK(mode)) {
4334 data->arg.ftype = NF4BLK;
4335 data->arg.u.device.specdata1 = MAJOR(rdev);
4336 data->arg.u.device.specdata2 = MINOR(rdev);
4338 else if (S_ISCHR(mode)) {
4339 data->arg.ftype = NF4CHR;
4340 data->arg.u.device.specdata1 = MAJOR(rdev);
4341 data->arg.u.device.specdata2 = MINOR(rdev);
4342 } else if (!S_ISSOCK(mode)) {
4347 data->arg.label = label;
4348 status = nfs4_do_create(dir, dentry, data);
4350 nfs4_free_createdata(data);
4355 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4356 struct iattr *sattr, dev_t rdev)
4358 struct nfs4_exception exception = { };
4359 struct nfs4_label l, *label = NULL;
4362 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4364 sattr->ia_mode &= ~current_umask();
4366 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4367 trace_nfs4_mknod(dir, &dentry->d_name, err);
4368 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4370 } while (exception.retry);
4372 nfs4_label_release_security(label);
4377 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4378 struct nfs_fsstat *fsstat)
4380 struct nfs4_statfs_arg args = {
4382 .bitmask = server->attr_bitmask,
4384 struct nfs4_statfs_res res = {
4387 struct rpc_message msg = {
4388 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4393 nfs_fattr_init(fsstat->fattr);
4394 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4397 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4399 struct nfs4_exception exception = { };
4402 err = nfs4_handle_exception(server,
4403 _nfs4_proc_statfs(server, fhandle, fsstat),
4405 } while (exception.retry);
4409 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4410 struct nfs_fsinfo *fsinfo)
4412 struct nfs4_fsinfo_arg args = {
4414 .bitmask = server->attr_bitmask,
4416 struct nfs4_fsinfo_res res = {
4419 struct rpc_message msg = {
4420 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4425 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4428 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4430 struct nfs4_exception exception = { };
4431 unsigned long now = jiffies;
4435 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
4436 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
4438 nfs4_set_lease_period(server->nfs_client,
4439 fsinfo->lease_time * HZ,
4443 err = nfs4_handle_exception(server, err, &exception);
4444 } while (exception.retry);
4448 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4452 nfs_fattr_init(fsinfo->fattr);
4453 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
4455 /* block layout checks this! */
4456 server->pnfs_blksize = fsinfo->blksize;
4457 set_pnfs_layoutdriver(server, fhandle, fsinfo);
4463 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4464 struct nfs_pathconf *pathconf)
4466 struct nfs4_pathconf_arg args = {
4468 .bitmask = server->attr_bitmask,
4470 struct nfs4_pathconf_res res = {
4471 .pathconf = pathconf,
4473 struct rpc_message msg = {
4474 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4479 /* None of the pathconf attributes are mandatory to implement */
4480 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4481 memset(pathconf, 0, sizeof(*pathconf));
4485 nfs_fattr_init(pathconf->fattr);
4486 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4489 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4490 struct nfs_pathconf *pathconf)
4492 struct nfs4_exception exception = { };
4496 err = nfs4_handle_exception(server,
4497 _nfs4_proc_pathconf(server, fhandle, pathconf),
4499 } while (exception.retry);
4503 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
4504 const struct nfs_open_context *ctx,
4505 const struct nfs_lock_context *l_ctx,
4508 const struct nfs_lockowner *lockowner = NULL;
4511 lockowner = &l_ctx->lockowner;
4512 return nfs4_select_rw_stateid(ctx->state, fmode, lockowner, stateid, NULL);
4514 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4516 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4517 const struct nfs_open_context *ctx,
4518 const struct nfs_lock_context *l_ctx,
4521 nfs4_stateid current_stateid;
4523 /* If the current stateid represents a lost lock, then exit */
4524 if (nfs4_set_rw_stateid(¤t_stateid, ctx, l_ctx, fmode) == -EIO)
4526 return nfs4_stateid_match(stateid, ¤t_stateid);
4529 static bool nfs4_error_stateid_expired(int err)
4532 case -NFS4ERR_DELEG_REVOKED:
4533 case -NFS4ERR_ADMIN_REVOKED:
4534 case -NFS4ERR_BAD_STATEID:
4535 case -NFS4ERR_STALE_STATEID:
4536 case -NFS4ERR_OLD_STATEID:
4537 case -NFS4ERR_OPENMODE:
4538 case -NFS4ERR_EXPIRED:
4544 void __nfs4_read_done_cb(struct nfs_pgio_header *hdr)
4546 nfs_invalidate_atime(hdr->inode);
4549 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
4551 struct nfs_server *server = NFS_SERVER(hdr->inode);
4553 trace_nfs4_read(hdr, task->tk_status);
4554 if (task->tk_status < 0) {
4555 struct nfs4_exception exception = {
4556 .inode = hdr->inode,
4557 .state = hdr->args.context->state,
4558 .stateid = &hdr->args.stateid,
4560 task->tk_status = nfs4_async_handle_exception(task,
4561 server, task->tk_status, &exception);
4562 if (exception.retry) {
4563 rpc_restart_call_prepare(task);
4568 __nfs4_read_done_cb(hdr);
4569 if (task->tk_status > 0)
4570 renew_lease(server, hdr->timestamp);
4574 static bool nfs4_read_stateid_changed(struct rpc_task *task,
4575 struct nfs_pgio_args *args)
4578 if (!nfs4_error_stateid_expired(task->tk_status) ||
4579 nfs4_stateid_is_current(&args->stateid,
4584 rpc_restart_call_prepare(task);
4588 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
4591 dprintk("--> %s\n", __func__);
4593 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
4595 if (nfs4_read_stateid_changed(task, &hdr->args))
4597 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4598 nfs4_read_done_cb(task, hdr);
4601 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4602 struct rpc_message *msg)
4604 hdr->timestamp = jiffies;
4605 if (!hdr->pgio_done_cb)
4606 hdr->pgio_done_cb = nfs4_read_done_cb;
4607 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
4608 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
4611 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4612 struct nfs_pgio_header *hdr)
4614 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4615 &hdr->args.seq_args,
4619 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4620 hdr->args.lock_context,
4621 hdr->rw_ops->rw_mode) == -EIO)
4623 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
4628 static int nfs4_write_done_cb(struct rpc_task *task,
4629 struct nfs_pgio_header *hdr)
4631 struct inode *inode = hdr->inode;
4633 trace_nfs4_write(hdr, task->tk_status);
4634 if (task->tk_status < 0) {
4635 struct nfs4_exception exception = {
4636 .inode = hdr->inode,
4637 .state = hdr->args.context->state,
4638 .stateid = &hdr->args.stateid,
4640 task->tk_status = nfs4_async_handle_exception(task,
4641 NFS_SERVER(inode), task->tk_status,
4643 if (exception.retry) {
4644 rpc_restart_call_prepare(task);
4648 if (task->tk_status >= 0) {
4649 renew_lease(NFS_SERVER(inode), hdr->timestamp);
4650 nfs_writeback_update_inode(hdr);
4655 static bool nfs4_write_stateid_changed(struct rpc_task *task,
4656 struct nfs_pgio_args *args)
4659 if (!nfs4_error_stateid_expired(task->tk_status) ||
4660 nfs4_stateid_is_current(&args->stateid,
4665 rpc_restart_call_prepare(task);
4669 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
4671 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
4673 if (nfs4_write_stateid_changed(task, &hdr->args))
4675 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4676 nfs4_write_done_cb(task, hdr);
4680 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
4682 /* Don't request attributes for pNFS or O_DIRECT writes */
4683 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
4685 /* Otherwise, request attributes if and only if we don't hold
4688 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
4691 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4692 struct rpc_message *msg)
4694 struct nfs_server *server = NFS_SERVER(hdr->inode);
4696 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4697 hdr->args.bitmask = NULL;
4698 hdr->res.fattr = NULL;
4700 hdr->args.bitmask = server->cache_consistency_bitmask;
4702 if (!hdr->pgio_done_cb)
4703 hdr->pgio_done_cb = nfs4_write_done_cb;
4704 hdr->res.server = server;
4705 hdr->timestamp = jiffies;
4707 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
4708 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
4711 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4713 nfs4_setup_sequence(NFS_SERVER(data->inode),
4714 &data->args.seq_args,
4719 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
4721 struct inode *inode = data->inode;
4723 trace_nfs4_commit(data, task->tk_status);
4724 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4725 NULL, NULL) == -EAGAIN) {
4726 rpc_restart_call_prepare(task);
4732 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
4734 if (!nfs4_sequence_done(task, &data->res.seq_res))
4736 return data->commit_done_cb(task, data);
4739 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
4741 struct nfs_server *server = NFS_SERVER(data->inode);
4743 if (data->commit_done_cb == NULL)
4744 data->commit_done_cb = nfs4_commit_done_cb;
4745 data->res.server = server;
4746 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
4747 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4750 struct nfs4_renewdata {
4751 struct nfs_client *client;
4752 unsigned long timestamp;
4756 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4757 * standalone procedure for queueing an asynchronous RENEW.
4759 static void nfs4_renew_release(void *calldata)
4761 struct nfs4_renewdata *data = calldata;
4762 struct nfs_client *clp = data->client;
4764 if (atomic_read(&clp->cl_count) > 1)
4765 nfs4_schedule_state_renewal(clp);
4766 nfs_put_client(clp);
4770 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
4772 struct nfs4_renewdata *data = calldata;
4773 struct nfs_client *clp = data->client;
4774 unsigned long timestamp = data->timestamp;
4776 trace_nfs4_renew_async(clp, task->tk_status);
4777 switch (task->tk_status) {
4780 case -NFS4ERR_LEASE_MOVED:
4781 nfs4_schedule_lease_moved_recovery(clp);
4784 /* Unless we're shutting down, schedule state recovery! */
4785 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4787 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
4788 nfs4_schedule_lease_recovery(clp);
4791 nfs4_schedule_path_down_recovery(clp);
4793 do_renew_lease(clp, timestamp);
4796 static const struct rpc_call_ops nfs4_renew_ops = {
4797 .rpc_call_done = nfs4_renew_done,
4798 .rpc_release = nfs4_renew_release,
4801 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
4803 struct rpc_message msg = {
4804 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4808 struct nfs4_renewdata *data;
4810 if (renew_flags == 0)
4812 if (!atomic_inc_not_zero(&clp->cl_count))
4814 data = kmalloc(sizeof(*data), GFP_NOFS);
4818 data->timestamp = jiffies;
4819 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
4820 &nfs4_renew_ops, data);
4823 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
4825 struct rpc_message msg = {
4826 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4830 unsigned long now = jiffies;
4833 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4836 do_renew_lease(clp, now);
4840 static inline int nfs4_server_supports_acls(struct nfs_server *server)
4842 return server->caps & NFS_CAP_ACLS;
4845 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4846 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
4849 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
4851 static int buf_to_pages_noslab(const void *buf, size_t buflen,
4852 struct page **pages)
4854 struct page *newpage, **spages;
4860 len = min_t(size_t, PAGE_SIZE, buflen);
4861 newpage = alloc_page(GFP_KERNEL);
4863 if (newpage == NULL)
4865 memcpy(page_address(newpage), buf, len);
4870 } while (buflen != 0);
4876 __free_page(spages[rc-1]);
4880 struct nfs4_cached_acl {
4886 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
4888 struct nfs_inode *nfsi = NFS_I(inode);
4890 spin_lock(&inode->i_lock);
4891 kfree(nfsi->nfs4_acl);
4892 nfsi->nfs4_acl = acl;
4893 spin_unlock(&inode->i_lock);
4896 static void nfs4_zap_acl_attr(struct inode *inode)
4898 nfs4_set_cached_acl(inode, NULL);
4901 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4903 struct nfs_inode *nfsi = NFS_I(inode);
4904 struct nfs4_cached_acl *acl;
4907 spin_lock(&inode->i_lock);
4908 acl = nfsi->nfs4_acl;
4911 if (buf == NULL) /* user is just asking for length */
4913 if (acl->cached == 0)
4915 ret = -ERANGE; /* see getxattr(2) man page */
4916 if (acl->len > buflen)
4918 memcpy(buf, acl->data, acl->len);
4922 spin_unlock(&inode->i_lock);
4926 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
4928 struct nfs4_cached_acl *acl;
4929 size_t buflen = sizeof(*acl) + acl_len;
4931 if (buflen <= PAGE_SIZE) {
4932 acl = kmalloc(buflen, GFP_KERNEL);
4936 _copy_from_pages(acl->data, pages, pgbase, acl_len);
4938 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4945 nfs4_set_cached_acl(inode, acl);
4949 * The getxattr API returns the required buffer length when called with a
4950 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4951 * the required buf. On a NULL buf, we send a page of data to the server
4952 * guessing that the ACL request can be serviced by a page. If so, we cache
4953 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4954 * the cache. If not so, we throw away the page, and cache the required
4955 * length. The next getxattr call will then produce another round trip to
4956 * the server, this time with the input buf of the required size.
4958 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4960 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
4961 struct nfs_getaclargs args = {
4962 .fh = NFS_FH(inode),
4966 struct nfs_getaclres res = {
4969 struct rpc_message msg = {
4970 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4974 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4975 int ret = -ENOMEM, i;
4977 /* As long as we're doing a round trip to the server anyway,
4978 * let's be prepared for a page of acl data. */
4981 if (npages > ARRAY_SIZE(pages))
4984 for (i = 0; i < npages; i++) {
4985 pages[i] = alloc_page(GFP_KERNEL);
4990 /* for decoding across pages */
4991 res.acl_scratch = alloc_page(GFP_KERNEL);
4992 if (!res.acl_scratch)
4995 args.acl_len = npages * PAGE_SIZE;
4997 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
4998 __func__, buf, buflen, npages, args.acl_len);
4999 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5000 &msg, &args.seq_args, &res.seq_res, 0);
5004 /* Handle the case where the passed-in buffer is too short */
5005 if (res.acl_flags & NFS4_ACL_TRUNC) {
5006 /* Did the user only issue a request for the acl length? */
5012 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
5014 if (res.acl_len > buflen) {
5018 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
5023 for (i = 0; i < npages; i++)
5025 __free_page(pages[i]);
5026 if (res.acl_scratch)
5027 __free_page(res.acl_scratch);
5031 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5033 struct nfs4_exception exception = { };
5036 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
5037 trace_nfs4_get_acl(inode, ret);
5040 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5041 } while (exception.retry);
5045 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5047 struct nfs_server *server = NFS_SERVER(inode);
5050 if (!nfs4_server_supports_acls(server))
5052 ret = nfs_revalidate_inode(server, inode);
5055 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5056 nfs_zap_acl_cache(inode);
5057 ret = nfs4_read_cached_acl(inode, buf, buflen);
5059 /* -ENOENT is returned if there is no ACL or if there is an ACL
5060 * but no cached acl data, just the acl length */
5062 return nfs4_get_acl_uncached(inode, buf, buflen);
5065 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5067 struct nfs_server *server = NFS_SERVER(inode);
5068 struct page *pages[NFS4ACL_MAXPAGES];
5069 struct nfs_setaclargs arg = {
5070 .fh = NFS_FH(inode),
5074 struct nfs_setaclres res;
5075 struct rpc_message msg = {
5076 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5080 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
5083 if (!nfs4_server_supports_acls(server))
5085 if (npages > ARRAY_SIZE(pages))
5087 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
5090 nfs4_inode_return_delegation(inode);
5091 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5094 * Free each page after tx, so the only ref left is
5095 * held by the network stack
5098 put_page(pages[i-1]);
5101 * Acl update can result in inode attribute update.
5102 * so mark the attribute cache invalid.
5104 spin_lock(&inode->i_lock);
5105 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5106 spin_unlock(&inode->i_lock);
5107 nfs_access_zap_cache(inode);
5108 nfs_zap_acl_cache(inode);
5112 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5114 struct nfs4_exception exception = { };
5117 err = __nfs4_proc_set_acl(inode, buf, buflen);
5118 trace_nfs4_set_acl(inode, err);
5119 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5121 } while (exception.retry);
5125 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
5126 static int _nfs4_get_security_label(struct inode *inode, void *buf,
5129 struct nfs_server *server = NFS_SERVER(inode);
5130 struct nfs_fattr fattr;
5131 struct nfs4_label label = {0, 0, buflen, buf};
5133 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5134 struct nfs4_getattr_arg arg = {
5135 .fh = NFS_FH(inode),
5138 struct nfs4_getattr_res res = {
5143 struct rpc_message msg = {
5144 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
5150 nfs_fattr_init(&fattr);
5152 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
5155 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5157 if (buflen < label.len)
5162 static int nfs4_get_security_label(struct inode *inode, void *buf,
5165 struct nfs4_exception exception = { };
5168 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5172 err = _nfs4_get_security_label(inode, buf, buflen);
5173 trace_nfs4_get_security_label(inode, err);
5174 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5176 } while (exception.retry);
5180 static int _nfs4_do_set_security_label(struct inode *inode,
5181 struct nfs4_label *ilabel,
5182 struct nfs_fattr *fattr,
5183 struct nfs4_label *olabel)
5186 struct iattr sattr = {0};
5187 struct nfs_server *server = NFS_SERVER(inode);
5188 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5189 struct nfs_setattrargs arg = {
5190 .fh = NFS_FH(inode),
5196 struct nfs_setattrres res = {
5201 struct rpc_message msg = {
5202 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5208 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
5210 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5212 dprintk("%s failed: %d\n", __func__, status);
5217 static int nfs4_do_set_security_label(struct inode *inode,
5218 struct nfs4_label *ilabel,
5219 struct nfs_fattr *fattr,
5220 struct nfs4_label *olabel)
5222 struct nfs4_exception exception = { };
5226 err = _nfs4_do_set_security_label(inode, ilabel,
5228 trace_nfs4_set_security_label(inode, err);
5229 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5231 } while (exception.retry);
5236 nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
5238 struct nfs4_label ilabel, *olabel = NULL;
5239 struct nfs_fattr fattr;
5240 struct rpc_cred *cred;
5243 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5246 nfs_fattr_init(&fattr);
5250 ilabel.label = (char *)buf;
5251 ilabel.len = buflen;
5253 cred = rpc_lookup_cred();
5255 return PTR_ERR(cred);
5257 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5258 if (IS_ERR(olabel)) {
5259 status = -PTR_ERR(olabel);
5263 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5265 nfs_setsecurity(inode, &fattr, olabel);
5267 nfs4_label_free(olabel);
5272 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5275 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5276 nfs4_verifier *bootverf)
5280 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5281 /* An impossible timestamp guarantees this value
5282 * will never match a generated boot time. */
5284 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
5286 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
5287 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
5288 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
5290 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5294 nfs4_init_nonuniform_client_string(struct nfs_client *clp)
5299 if (clp->cl_owner_id != NULL)
5303 len = 14 + strlen(clp->cl_ipaddr) + 1 +
5304 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5306 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5310 if (len > NFS4_OPAQUE_LIMIT + 1)
5314 * Since this string is allocated at mount time, and held until the
5315 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5316 * about a memory-reclaim deadlock.
5318 str = kmalloc(len, GFP_KERNEL);
5323 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
5325 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5326 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
5329 clp->cl_owner_id = str;
5334 nfs4_init_uniquifier_client_string(struct nfs_client *clp)
5339 len = 10 + 10 + 1 + 10 + 1 +
5340 strlen(nfs4_client_id_uniquifier) + 1 +
5341 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5343 if (len > NFS4_OPAQUE_LIMIT + 1)
5347 * Since this string is allocated at mount time, and held until the
5348 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5349 * about a memory-reclaim deadlock.
5351 str = kmalloc(len, GFP_KERNEL);
5355 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
5356 clp->rpc_ops->version, clp->cl_minorversion,
5357 nfs4_client_id_uniquifier,
5358 clp->cl_rpcclient->cl_nodename);
5359 clp->cl_owner_id = str;
5364 nfs4_init_uniform_client_string(struct nfs_client *clp)
5369 if (clp->cl_owner_id != NULL)
5372 if (nfs4_client_id_uniquifier[0] != '\0')
5373 return nfs4_init_uniquifier_client_string(clp);
5375 len = 10 + 10 + 1 + 10 + 1 +
5376 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5378 if (len > NFS4_OPAQUE_LIMIT + 1)
5382 * Since this string is allocated at mount time, and held until the
5383 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5384 * about a memory-reclaim deadlock.
5386 str = kmalloc(len, GFP_KERNEL);
5390 scnprintf(str, len, "Linux NFSv%u.%u %s",
5391 clp->rpc_ops->version, clp->cl_minorversion,
5392 clp->cl_rpcclient->cl_nodename);
5393 clp->cl_owner_id = str;
5398 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5399 * services. Advertise one based on the address family of the
5403 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5405 if (strchr(clp->cl_ipaddr, ':') != NULL)
5406 return scnprintf(buf, len, "tcp6");
5408 return scnprintf(buf, len, "tcp");
5411 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5413 struct nfs4_setclientid *sc = calldata;
5415 if (task->tk_status == 0)
5416 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5419 static const struct rpc_call_ops nfs4_setclientid_ops = {
5420 .rpc_call_done = nfs4_setclientid_done,
5424 * nfs4_proc_setclientid - Negotiate client ID
5425 * @clp: state data structure
5426 * @program: RPC program for NFSv4 callback service
5427 * @port: IP port number for NFS4 callback service
5428 * @cred: RPC credential to use for this call
5429 * @res: where to place the result
5431 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5433 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5434 unsigned short port, struct rpc_cred *cred,
5435 struct nfs4_setclientid_res *res)
5437 nfs4_verifier sc_verifier;
5438 struct nfs4_setclientid setclientid = {
5439 .sc_verifier = &sc_verifier,
5443 struct rpc_message msg = {
5444 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5445 .rpc_argp = &setclientid,
5449 struct rpc_task *task;
5450 struct rpc_task_setup task_setup_data = {
5451 .rpc_client = clp->cl_rpcclient,
5452 .rpc_message = &msg,
5453 .callback_ops = &nfs4_setclientid_ops,
5454 .callback_data = &setclientid,
5455 .flags = RPC_TASK_TIMEOUT,
5459 /* nfs_client_id4 */
5460 nfs4_init_boot_verifier(clp, &sc_verifier);
5462 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5463 status = nfs4_init_uniform_client_string(clp);
5465 status = nfs4_init_nonuniform_client_string(clp);
5471 setclientid.sc_netid_len =
5472 nfs4_init_callback_netid(clp,
5473 setclientid.sc_netid,
5474 sizeof(setclientid.sc_netid));
5475 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
5476 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
5477 clp->cl_ipaddr, port >> 8, port & 255);
5479 dprintk("NFS call setclientid auth=%s, '%s'\n",
5480 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5482 task = rpc_run_task(&task_setup_data);
5484 status = PTR_ERR(task);
5487 status = task->tk_status;
5488 if (setclientid.sc_cred) {
5489 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5490 put_rpccred(setclientid.sc_cred);
5494 trace_nfs4_setclientid(clp, status);
5495 dprintk("NFS reply setclientid: %d\n", status);
5500 * nfs4_proc_setclientid_confirm - Confirm client ID
5501 * @clp: state data structure
5502 * @res: result of a previous SETCLIENTID
5503 * @cred: RPC credential to use for this call
5505 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5507 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
5508 struct nfs4_setclientid_res *arg,
5509 struct rpc_cred *cred)
5511 struct rpc_message msg = {
5512 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
5518 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5519 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5521 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5522 trace_nfs4_setclientid_confirm(clp, status);
5523 dprintk("NFS reply setclientid_confirm: %d\n", status);
5527 struct nfs4_delegreturndata {
5528 struct nfs4_delegreturnargs args;
5529 struct nfs4_delegreturnres res;
5531 nfs4_stateid stateid;
5532 unsigned long timestamp;
5533 struct nfs_fattr fattr;
5535 struct inode *inode;
5540 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5542 struct nfs4_delegreturndata *data = calldata;
5544 if (!nfs4_sequence_done(task, &data->res.seq_res))
5547 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
5548 switch (task->tk_status) {
5550 renew_lease(data->res.server, data->timestamp);
5551 case -NFS4ERR_ADMIN_REVOKED:
5552 case -NFS4ERR_DELEG_REVOKED:
5553 case -NFS4ERR_EXPIRED:
5554 nfs4_free_revoked_stateid(data->res.server,
5556 task->tk_msg.rpc_cred);
5557 case -NFS4ERR_BAD_STATEID:
5558 case -NFS4ERR_OLD_STATEID:
5559 case -NFS4ERR_STALE_STATEID:
5560 task->tk_status = 0;
5562 pnfs_roc_set_barrier(data->inode, data->roc_barrier);
5565 if (nfs4_async_handle_error(task, data->res.server,
5566 NULL, NULL) == -EAGAIN) {
5567 rpc_restart_call_prepare(task);
5571 data->rpc_status = task->tk_status;
5574 static void nfs4_delegreturn_release(void *calldata)
5576 struct nfs4_delegreturndata *data = calldata;
5577 struct inode *inode = data->inode;
5581 pnfs_roc_release(inode);
5582 nfs_iput_and_deactive(inode);
5587 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5589 struct nfs4_delegreturndata *d_data;
5591 d_data = (struct nfs4_delegreturndata *)data;
5593 if (nfs4_wait_on_layoutreturn(d_data->inode, task))
5597 pnfs_roc_get_barrier(d_data->inode, &d_data->roc_barrier);
5599 nfs4_setup_sequence(d_data->res.server,
5600 &d_data->args.seq_args,
5601 &d_data->res.seq_res,
5605 static const struct rpc_call_ops nfs4_delegreturn_ops = {
5606 .rpc_call_prepare = nfs4_delegreturn_prepare,
5607 .rpc_call_done = nfs4_delegreturn_done,
5608 .rpc_release = nfs4_delegreturn_release,
5611 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
5613 struct nfs4_delegreturndata *data;
5614 struct nfs_server *server = NFS_SERVER(inode);
5615 struct rpc_task *task;
5616 struct rpc_message msg = {
5617 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5620 struct rpc_task_setup task_setup_data = {
5621 .rpc_client = server->client,
5622 .rpc_message = &msg,
5623 .callback_ops = &nfs4_delegreturn_ops,
5624 .flags = RPC_TASK_ASYNC,
5628 data = kzalloc(sizeof(*data), GFP_NOFS);
5631 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
5633 nfs4_state_protect(server->nfs_client,
5634 NFS_SP4_MACH_CRED_CLEANUP,
5635 &task_setup_data.rpc_client, &msg);
5637 data->args.fhandle = &data->fh;
5638 data->args.stateid = &data->stateid;
5639 data->args.bitmask = server->cache_consistency_bitmask;
5640 nfs_copy_fh(&data->fh, NFS_FH(inode));
5641 nfs4_stateid_copy(&data->stateid, stateid);
5642 data->res.fattr = &data->fattr;
5643 data->res.server = server;
5644 nfs_fattr_init(data->res.fattr);
5645 data->timestamp = jiffies;
5646 data->rpc_status = 0;
5647 data->inode = nfs_igrab_and_active(inode);
5649 data->roc = nfs4_roc(inode);
5651 task_setup_data.callback_data = data;
5652 msg.rpc_argp = &data->args;
5653 msg.rpc_resp = &data->res;
5654 task = rpc_run_task(&task_setup_data);
5656 return PTR_ERR(task);
5659 status = nfs4_wait_for_completion_rpc_task(task);
5662 status = data->rpc_status;
5664 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5666 nfs_refresh_inode(inode, &data->fattr);
5672 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
5674 struct nfs_server *server = NFS_SERVER(inode);
5675 struct nfs4_exception exception = { };
5678 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
5679 trace_nfs4_delegreturn(inode, stateid, err);
5681 case -NFS4ERR_STALE_STATEID:
5682 case -NFS4ERR_EXPIRED:
5686 err = nfs4_handle_exception(server, err, &exception);
5687 } while (exception.retry);
5691 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5693 struct inode *inode = state->inode;
5694 struct nfs_server *server = NFS_SERVER(inode);
5695 struct nfs_client *clp = server->nfs_client;
5696 struct nfs_lockt_args arg = {
5697 .fh = NFS_FH(inode),
5700 struct nfs_lockt_res res = {
5703 struct rpc_message msg = {
5704 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5707 .rpc_cred = state->owner->so_cred,
5709 struct nfs4_lock_state *lsp;
5712 arg.lock_owner.clientid = clp->cl_clientid;
5713 status = nfs4_set_lock_state(state, request);
5716 lsp = request->fl_u.nfs4_fl.owner;
5717 arg.lock_owner.id = lsp->ls_seqid.owner_id;
5718 arg.lock_owner.s_dev = server->s_dev;
5719 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5722 request->fl_type = F_UNLCK;
5724 case -NFS4ERR_DENIED:
5727 request->fl_ops->fl_release_private(request);
5728 request->fl_ops = NULL;
5733 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5735 struct nfs4_exception exception = { };
5739 err = _nfs4_proc_getlk(state, cmd, request);
5740 trace_nfs4_get_lock(request, state, cmd, err);
5741 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
5743 } while (exception.retry);
5747 struct nfs4_unlockdata {
5748 struct nfs_locku_args arg;
5749 struct nfs_locku_res res;
5750 struct nfs4_lock_state *lsp;
5751 struct nfs_open_context *ctx;
5752 struct file_lock fl;
5753 struct nfs_server *server;
5754 unsigned long timestamp;
5757 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5758 struct nfs_open_context *ctx,
5759 struct nfs4_lock_state *lsp,
5760 struct nfs_seqid *seqid)
5762 struct nfs4_unlockdata *p;
5763 struct inode *inode = lsp->ls_state->inode;
5765 p = kzalloc(sizeof(*p), GFP_NOFS);
5768 p->arg.fh = NFS_FH(inode);
5770 p->arg.seqid = seqid;
5771 p->res.seqid = seqid;
5773 atomic_inc(&lsp->ls_count);
5774 /* Ensure we don't close file until we're done freeing locks! */
5775 p->ctx = get_nfs_open_context(ctx);
5776 memcpy(&p->fl, fl, sizeof(p->fl));
5777 p->server = NFS_SERVER(inode);
5781 static void nfs4_locku_release_calldata(void *data)
5783 struct nfs4_unlockdata *calldata = data;
5784 nfs_free_seqid(calldata->arg.seqid);
5785 nfs4_put_lock_state(calldata->lsp);
5786 put_nfs_open_context(calldata->ctx);
5790 static void nfs4_locku_done(struct rpc_task *task, void *data)
5792 struct nfs4_unlockdata *calldata = data;
5794 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5796 switch (task->tk_status) {
5798 renew_lease(calldata->server, calldata->timestamp);
5799 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
5800 if (nfs4_update_lock_stateid(calldata->lsp,
5801 &calldata->res.stateid))
5803 case -NFS4ERR_ADMIN_REVOKED:
5804 case -NFS4ERR_EXPIRED:
5805 nfs4_free_revoked_stateid(calldata->server,
5806 &calldata->arg.stateid,
5807 task->tk_msg.rpc_cred);
5808 case -NFS4ERR_BAD_STATEID:
5809 case -NFS4ERR_OLD_STATEID:
5810 case -NFS4ERR_STALE_STATEID:
5811 if (!nfs4_stateid_match(&calldata->arg.stateid,
5812 &calldata->lsp->ls_stateid))
5813 rpc_restart_call_prepare(task);
5816 if (nfs4_async_handle_error(task, calldata->server,
5817 NULL, NULL) == -EAGAIN)
5818 rpc_restart_call_prepare(task);
5820 nfs_release_seqid(calldata->arg.seqid);
5823 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
5825 struct nfs4_unlockdata *calldata = data;
5827 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
5829 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
5830 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
5831 /* Note: exit _without_ running nfs4_locku_done */
5834 calldata->timestamp = jiffies;
5835 if (nfs4_setup_sequence(calldata->server,
5836 &calldata->arg.seq_args,
5837 &calldata->res.seq_res,
5839 nfs_release_seqid(calldata->arg.seqid);
5842 task->tk_action = NULL;
5844 nfs4_sequence_done(task, &calldata->res.seq_res);
5847 static const struct rpc_call_ops nfs4_locku_ops = {
5848 .rpc_call_prepare = nfs4_locku_prepare,
5849 .rpc_call_done = nfs4_locku_done,
5850 .rpc_release = nfs4_locku_release_calldata,
5853 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5854 struct nfs_open_context *ctx,
5855 struct nfs4_lock_state *lsp,
5856 struct nfs_seqid *seqid)
5858 struct nfs4_unlockdata *data;
5859 struct rpc_message msg = {
5860 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5861 .rpc_cred = ctx->cred,
5863 struct rpc_task_setup task_setup_data = {
5864 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
5865 .rpc_message = &msg,
5866 .callback_ops = &nfs4_locku_ops,
5867 .workqueue = nfsiod_workqueue,
5868 .flags = RPC_TASK_ASYNC,
5871 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5872 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5874 /* Ensure this is an unlock - when canceling a lock, the
5875 * canceled lock is passed in, and it won't be an unlock.
5877 fl->fl_type = F_UNLCK;
5879 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5881 nfs_free_seqid(seqid);
5882 return ERR_PTR(-ENOMEM);
5885 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
5886 msg.rpc_argp = &data->arg;
5887 msg.rpc_resp = &data->res;
5888 task_setup_data.callback_data = data;
5889 return rpc_run_task(&task_setup_data);
5892 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5894 struct inode *inode = state->inode;
5895 struct nfs4_state_owner *sp = state->owner;
5896 struct nfs_inode *nfsi = NFS_I(inode);
5897 struct nfs_seqid *seqid;
5898 struct nfs4_lock_state *lsp;
5899 struct rpc_task *task;
5900 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
5902 unsigned char fl_flags = request->fl_flags;
5904 status = nfs4_set_lock_state(state, request);
5905 /* Unlock _before_ we do the RPC call */
5906 request->fl_flags |= FL_EXISTS;
5907 /* Exclude nfs_delegation_claim_locks() */
5908 mutex_lock(&sp->so_delegreturn_mutex);
5909 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
5910 down_read(&nfsi->rwsem);
5911 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
5912 up_read(&nfsi->rwsem);
5913 mutex_unlock(&sp->so_delegreturn_mutex);
5916 up_read(&nfsi->rwsem);
5917 mutex_unlock(&sp->so_delegreturn_mutex);
5920 /* Is this a delegated lock? */
5921 lsp = request->fl_u.nfs4_fl.owner;
5922 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5924 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
5925 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
5929 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
5930 status = PTR_ERR(task);
5933 status = nfs4_wait_for_completion_rpc_task(task);
5936 request->fl_flags = fl_flags;
5937 trace_nfs4_unlock(request, state, F_SETLK, status);
5941 struct nfs4_lockdata {
5942 struct nfs_lock_args arg;
5943 struct nfs_lock_res res;
5944 struct nfs4_lock_state *lsp;
5945 struct nfs_open_context *ctx;
5946 struct file_lock fl;
5947 unsigned long timestamp;
5950 struct nfs_server *server;
5953 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
5954 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5957 struct nfs4_lockdata *p;
5958 struct inode *inode = lsp->ls_state->inode;
5959 struct nfs_server *server = NFS_SERVER(inode);
5960 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
5962 p = kzalloc(sizeof(*p), gfp_mask);
5966 p->arg.fh = NFS_FH(inode);
5968 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
5969 if (IS_ERR(p->arg.open_seqid))
5971 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
5972 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
5973 if (IS_ERR(p->arg.lock_seqid))
5974 goto out_free_seqid;
5975 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
5976 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
5977 p->arg.lock_owner.s_dev = server->s_dev;
5978 p->res.lock_seqid = p->arg.lock_seqid;
5981 atomic_inc(&lsp->ls_count);
5982 p->ctx = get_nfs_open_context(ctx);
5983 get_file(fl->fl_file);
5984 memcpy(&p->fl, fl, sizeof(p->fl));
5987 nfs_free_seqid(p->arg.open_seqid);
5993 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5995 struct nfs4_lockdata *data = calldata;
5996 struct nfs4_state *state = data->lsp->ls_state;
5998 dprintk("%s: begin!\n", __func__);
5999 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
6001 /* Do we need to do an open_to_lock_owner? */
6002 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
6003 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
6004 goto out_release_lock_seqid;
6006 nfs4_stateid_copy(&data->arg.open_stateid,
6007 &state->open_stateid);
6008 data->arg.new_lock_owner = 1;
6009 data->res.open_seqid = data->arg.open_seqid;
6011 data->arg.new_lock_owner = 0;
6012 nfs4_stateid_copy(&data->arg.lock_stateid,
6013 &data->lsp->ls_stateid);
6015 if (!nfs4_valid_open_stateid(state)) {
6016 data->rpc_status = -EBADF;
6017 task->tk_action = NULL;
6018 goto out_release_open_seqid;
6020 data->timestamp = jiffies;
6021 if (nfs4_setup_sequence(data->server,
6022 &data->arg.seq_args,
6026 out_release_open_seqid:
6027 nfs_release_seqid(data->arg.open_seqid);
6028 out_release_lock_seqid:
6029 nfs_release_seqid(data->arg.lock_seqid);
6031 nfs4_sequence_done(task, &data->res.seq_res);
6032 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
6035 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6037 struct nfs4_lockdata *data = calldata;
6038 struct nfs4_lock_state *lsp = data->lsp;
6040 dprintk("%s: begin!\n", __func__);
6042 if (!nfs4_sequence_done(task, &data->res.seq_res))
6045 data->rpc_status = task->tk_status;
6046 switch (task->tk_status) {
6048 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
6050 if (data->arg.new_lock) {
6051 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
6052 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
6053 rpc_restart_call_prepare(task);
6057 if (data->arg.new_lock_owner != 0) {
6058 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6059 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6060 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6061 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6062 rpc_restart_call_prepare(task);
6064 case -NFS4ERR_BAD_STATEID:
6065 case -NFS4ERR_OLD_STATEID:
6066 case -NFS4ERR_STALE_STATEID:
6067 case -NFS4ERR_EXPIRED:
6068 if (data->arg.new_lock_owner != 0) {
6069 if (!nfs4_stateid_match(&data->arg.open_stateid,
6070 &lsp->ls_state->open_stateid))
6071 rpc_restart_call_prepare(task);
6072 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6074 rpc_restart_call_prepare(task);
6076 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
6079 static void nfs4_lock_release(void *calldata)
6081 struct nfs4_lockdata *data = calldata;
6083 dprintk("%s: begin!\n", __func__);
6084 nfs_free_seqid(data->arg.open_seqid);
6085 if (data->cancelled != 0) {
6086 struct rpc_task *task;
6087 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6088 data->arg.lock_seqid);
6090 rpc_put_task_async(task);
6091 dprintk("%s: cancelling lock!\n", __func__);
6093 nfs_free_seqid(data->arg.lock_seqid);
6094 nfs4_put_lock_state(data->lsp);
6095 put_nfs_open_context(data->ctx);
6096 fput(data->fl.fl_file);
6098 dprintk("%s: done!\n", __func__);
6101 static const struct rpc_call_ops nfs4_lock_ops = {
6102 .rpc_call_prepare = nfs4_lock_prepare,
6103 .rpc_call_done = nfs4_lock_done,
6104 .rpc_release = nfs4_lock_release,
6107 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6110 case -NFS4ERR_ADMIN_REVOKED:
6111 case -NFS4ERR_EXPIRED:
6112 case -NFS4ERR_BAD_STATEID:
6113 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6114 if (new_lock_owner != 0 ||
6115 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
6116 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
6118 case -NFS4ERR_STALE_STATEID:
6119 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6120 nfs4_schedule_lease_recovery(server->nfs_client);
6124 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
6126 struct nfs4_lockdata *data;
6127 struct rpc_task *task;
6128 struct rpc_message msg = {
6129 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6130 .rpc_cred = state->owner->so_cred,
6132 struct rpc_task_setup task_setup_data = {
6133 .rpc_client = NFS_CLIENT(state->inode),
6134 .rpc_message = &msg,
6135 .callback_ops = &nfs4_lock_ops,
6136 .workqueue = nfsiod_workqueue,
6137 .flags = RPC_TASK_ASYNC,
6141 dprintk("%s: begin!\n", __func__);
6142 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
6143 fl->fl_u.nfs4_fl.owner,
6144 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
6148 data->arg.block = 1;
6149 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
6150 msg.rpc_argp = &data->arg;
6151 msg.rpc_resp = &data->res;
6152 task_setup_data.callback_data = data;
6153 if (recovery_type > NFS_LOCK_NEW) {
6154 if (recovery_type == NFS_LOCK_RECLAIM)
6155 data->arg.reclaim = NFS_LOCK_RECLAIM;
6156 nfs4_set_sequence_privileged(&data->arg.seq_args);
6158 data->arg.new_lock = 1;
6159 task = rpc_run_task(&task_setup_data);
6161 return PTR_ERR(task);
6162 ret = nfs4_wait_for_completion_rpc_task(task);
6164 ret = data->rpc_status;
6166 nfs4_handle_setlk_error(data->server, data->lsp,
6167 data->arg.new_lock_owner, ret);
6169 data->cancelled = 1;
6171 dprintk("%s: done, ret = %d!\n", __func__, ret);
6172 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
6176 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6178 struct nfs_server *server = NFS_SERVER(state->inode);
6179 struct nfs4_exception exception = {
6180 .inode = state->inode,
6185 /* Cache the lock if possible... */
6186 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6188 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
6189 if (err != -NFS4ERR_DELAY)
6191 nfs4_handle_exception(server, err, &exception);
6192 } while (exception.retry);
6196 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6198 struct nfs_server *server = NFS_SERVER(state->inode);
6199 struct nfs4_exception exception = {
6200 .inode = state->inode,
6204 err = nfs4_set_lock_state(state, request);
6207 if (!recover_lost_locks) {
6208 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6212 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6214 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
6218 case -NFS4ERR_GRACE:
6219 case -NFS4ERR_DELAY:
6220 nfs4_handle_exception(server, err, &exception);
6223 } while (exception.retry);
6228 #if defined(CONFIG_NFS_V4_1)
6229 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6231 struct nfs4_lock_state *lsp;
6234 status = nfs4_set_lock_state(state, request);
6237 lsp = request->fl_u.nfs4_fl.owner;
6238 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6239 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6241 status = nfs4_lock_expired(state, request);
6246 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6248 struct nfs_inode *nfsi = NFS_I(state->inode);
6249 struct nfs4_state_owner *sp = state->owner;
6250 unsigned char fl_flags = request->fl_flags;
6253 request->fl_flags |= FL_ACCESS;
6254 status = locks_lock_inode_wait(state->inode, request);
6257 mutex_lock(&sp->so_delegreturn_mutex);
6258 down_read(&nfsi->rwsem);
6259 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
6260 /* Yes: cache locks! */
6261 /* ...but avoid races with delegation recall... */
6262 request->fl_flags = fl_flags & ~FL_SLEEP;
6263 status = locks_lock_inode_wait(state->inode, request);
6264 up_read(&nfsi->rwsem);
6265 mutex_unlock(&sp->so_delegreturn_mutex);
6268 up_read(&nfsi->rwsem);
6269 mutex_unlock(&sp->so_delegreturn_mutex);
6270 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
6272 request->fl_flags = fl_flags;
6276 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6278 struct nfs4_exception exception = {
6280 .inode = state->inode,
6285 err = _nfs4_proc_setlk(state, cmd, request);
6286 if (err == -NFS4ERR_DENIED)
6288 err = nfs4_handle_exception(NFS_SERVER(state->inode),
6290 } while (exception.retry);
6294 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6295 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6298 nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6299 struct file_lock *request)
6301 int status = -ERESTARTSYS;
6302 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6304 while(!signalled()) {
6305 status = nfs4_proc_setlk(state, cmd, request);
6306 if ((status != -EAGAIN) || IS_SETLK(cmd))
6308 freezable_schedule_timeout_interruptible(timeout);
6310 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6311 status = -ERESTARTSYS;
6316 #ifdef CONFIG_NFS_V4_1
6317 struct nfs4_lock_waiter {
6318 struct task_struct *task;
6319 struct inode *inode;
6320 struct nfs_lowner *owner;
6325 nfs4_wake_lock_waiter(wait_queue_t *wait, unsigned int mode, int flags, void *key)
6328 struct cb_notify_lock_args *cbnl = key;
6329 struct nfs4_lock_waiter *waiter = wait->private;
6330 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6331 *wowner = waiter->owner;
6333 /* Only wake if the callback was for the same owner */
6334 if (lowner->clientid != wowner->clientid ||
6335 lowner->id != wowner->id ||
6336 lowner->s_dev != wowner->s_dev)
6339 /* Make sure it's for the right inode */
6340 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6343 waiter->notified = true;
6345 /* override "private" so we can use default_wake_function */
6346 wait->private = waiter->task;
6347 ret = autoremove_wake_function(wait, mode, flags, key);
6348 wait->private = waiter;
6353 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6355 int status = -ERESTARTSYS;
6356 unsigned long flags;
6357 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6358 struct nfs_server *server = NFS_SERVER(state->inode);
6359 struct nfs_client *clp = server->nfs_client;
6360 wait_queue_head_t *q = &clp->cl_lock_waitq;
6361 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6362 .id = lsp->ls_seqid.owner_id,
6363 .s_dev = server->s_dev };
6364 struct nfs4_lock_waiter waiter = { .task = current,
6365 .inode = state->inode,
6367 .notified = false };
6370 /* Don't bother with waitqueue if we don't expect a callback */
6371 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6372 return nfs4_retry_setlk_simple(state, cmd, request);
6375 wait.private = &waiter;
6376 wait.func = nfs4_wake_lock_waiter;
6377 add_wait_queue(q, &wait);
6379 while(!signalled()) {
6380 status = nfs4_proc_setlk(state, cmd, request);
6381 if ((status != -EAGAIN) || IS_SETLK(cmd))
6384 status = -ERESTARTSYS;
6385 spin_lock_irqsave(&q->lock, flags);
6386 if (waiter.notified) {
6387 spin_unlock_irqrestore(&q->lock, flags);
6390 set_current_state(TASK_INTERRUPTIBLE);
6391 spin_unlock_irqrestore(&q->lock, flags);
6393 freezable_schedule_timeout_interruptible(NFS4_LOCK_MAXTIMEOUT);
6396 finish_wait(q, &wait);
6399 #else /* !CONFIG_NFS_V4_1 */
6401 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6403 return nfs4_retry_setlk_simple(state, cmd, request);
6408 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6410 struct nfs_open_context *ctx;
6411 struct nfs4_state *state;
6414 /* verify open state */
6415 ctx = nfs_file_open_context(filp);
6418 if (request->fl_start < 0 || request->fl_end < 0)
6421 if (IS_GETLK(cmd)) {
6423 return nfs4_proc_getlk(state, F_GETLK, request);
6427 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6430 if (request->fl_type == F_UNLCK) {
6432 return nfs4_proc_unlck(state, cmd, request);
6439 if ((request->fl_flags & FL_POSIX) &&
6440 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6444 * Don't rely on the VFS having checked the file open mode,
6445 * since it won't do this for flock() locks.
6447 switch (request->fl_type) {
6449 if (!(filp->f_mode & FMODE_READ))
6453 if (!(filp->f_mode & FMODE_WRITE))
6457 status = nfs4_set_lock_state(state, request);
6461 return nfs4_retry_setlk(state, cmd, request);
6464 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
6466 struct nfs_server *server = NFS_SERVER(state->inode);
6469 err = nfs4_set_lock_state(state, fl);
6472 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
6473 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
6476 struct nfs_release_lockowner_data {
6477 struct nfs4_lock_state *lsp;
6478 struct nfs_server *server;
6479 struct nfs_release_lockowner_args args;
6480 struct nfs_release_lockowner_res res;
6481 unsigned long timestamp;
6484 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6486 struct nfs_release_lockowner_data *data = calldata;
6487 struct nfs_server *server = data->server;
6488 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
6489 &data->args.seq_args, &data->res.seq_res, task);
6490 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6491 data->timestamp = jiffies;
6494 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6496 struct nfs_release_lockowner_data *data = calldata;
6497 struct nfs_server *server = data->server;
6499 nfs40_sequence_done(task, &data->res.seq_res);
6501 switch (task->tk_status) {
6503 renew_lease(server, data->timestamp);
6505 case -NFS4ERR_STALE_CLIENTID:
6506 case -NFS4ERR_EXPIRED:
6507 nfs4_schedule_lease_recovery(server->nfs_client);
6509 case -NFS4ERR_LEASE_MOVED:
6510 case -NFS4ERR_DELAY:
6511 if (nfs4_async_handle_error(task, server,
6512 NULL, NULL) == -EAGAIN)
6513 rpc_restart_call_prepare(task);
6517 static void nfs4_release_lockowner_release(void *calldata)
6519 struct nfs_release_lockowner_data *data = calldata;
6520 nfs4_free_lock_state(data->server, data->lsp);
6524 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
6525 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6526 .rpc_call_done = nfs4_release_lockowner_done,
6527 .rpc_release = nfs4_release_lockowner_release,
6531 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
6533 struct nfs_release_lockowner_data *data;
6534 struct rpc_message msg = {
6535 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6538 if (server->nfs_client->cl_mvops->minor_version != 0)
6541 data = kmalloc(sizeof(*data), GFP_NOFS);
6545 data->server = server;
6546 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6547 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6548 data->args.lock_owner.s_dev = server->s_dev;
6550 msg.rpc_argp = &data->args;
6551 msg.rpc_resp = &data->res;
6552 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
6553 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
6556 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6558 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
6559 struct dentry *unused, struct inode *inode,
6560 const char *key, const void *buf,
6561 size_t buflen, int flags)
6563 return nfs4_proc_set_acl(inode, buf, buflen);
6566 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
6567 struct dentry *unused, struct inode *inode,
6568 const char *key, void *buf, size_t buflen)
6570 return nfs4_proc_get_acl(inode, buf, buflen);
6573 static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
6575 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
6578 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
6580 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
6581 struct dentry *unused, struct inode *inode,
6582 const char *key, const void *buf,
6583 size_t buflen, int flags)
6585 if (security_ismaclabel(key))
6586 return nfs4_set_security_label(inode, buf, buflen);
6591 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
6592 struct dentry *unused, struct inode *inode,
6593 const char *key, void *buf, size_t buflen)
6595 if (security_ismaclabel(key))
6596 return nfs4_get_security_label(inode, buf, buflen);
6601 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6605 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6606 len = security_inode_listsecurity(inode, list, list_len);
6607 if (list_len && len > list_len)
6613 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6614 .prefix = XATTR_SECURITY_PREFIX,
6615 .get = nfs4_xattr_get_nfs4_label,
6616 .set = nfs4_xattr_set_nfs4_label,
6622 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6630 * nfs_fhget will use either the mounted_on_fileid or the fileid
6632 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6634 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6635 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6636 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
6637 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
6640 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
6641 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
6642 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6646 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6647 const struct qstr *name,
6648 struct nfs4_fs_locations *fs_locations,
6651 struct nfs_server *server = NFS_SERVER(dir);
6653 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6655 struct nfs4_fs_locations_arg args = {
6656 .dir_fh = NFS_FH(dir),
6661 struct nfs4_fs_locations_res res = {
6662 .fs_locations = fs_locations,
6664 struct rpc_message msg = {
6665 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6671 dprintk("%s: start\n", __func__);
6673 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6674 * is not supported */
6675 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6676 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6678 bitmask[0] |= FATTR4_WORD0_FILEID;
6680 nfs_fattr_init(&fs_locations->fattr);
6681 fs_locations->server = server;
6682 fs_locations->nlocations = 0;
6683 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
6684 dprintk("%s: returned status = %d\n", __func__, status);
6688 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6689 const struct qstr *name,
6690 struct nfs4_fs_locations *fs_locations,
6693 struct nfs4_exception exception = { };
6696 err = _nfs4_proc_fs_locations(client, dir, name,
6697 fs_locations, page);
6698 trace_nfs4_get_fs_locations(dir, name, err);
6699 err = nfs4_handle_exception(NFS_SERVER(dir), err,
6701 } while (exception.retry);
6706 * This operation also signals the server that this client is
6707 * performing migration recovery. The server can stop returning
6708 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6709 * appended to this compound to identify the client ID which is
6710 * performing recovery.
6712 static int _nfs40_proc_get_locations(struct inode *inode,
6713 struct nfs4_fs_locations *locations,
6714 struct page *page, struct rpc_cred *cred)
6716 struct nfs_server *server = NFS_SERVER(inode);
6717 struct rpc_clnt *clnt = server->client;
6719 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6721 struct nfs4_fs_locations_arg args = {
6722 .clientid = server->nfs_client->cl_clientid,
6723 .fh = NFS_FH(inode),
6726 .migration = 1, /* skip LOOKUP */
6727 .renew = 1, /* append RENEW */
6729 struct nfs4_fs_locations_res res = {
6730 .fs_locations = locations,
6734 struct rpc_message msg = {
6735 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6740 unsigned long now = jiffies;
6743 nfs_fattr_init(&locations->fattr);
6744 locations->server = server;
6745 locations->nlocations = 0;
6747 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6748 nfs4_set_sequence_privileged(&args.seq_args);
6749 status = nfs4_call_sync_sequence(clnt, server, &msg,
6750 &args.seq_args, &res.seq_res);
6754 renew_lease(server, now);
6758 #ifdef CONFIG_NFS_V4_1
6761 * This operation also signals the server that this client is
6762 * performing migration recovery. The server can stop asserting
6763 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6764 * performing this operation is identified in the SEQUENCE
6765 * operation in this compound.
6767 * When the client supports GETATTR(fs_locations_info), it can
6768 * be plumbed in here.
6770 static int _nfs41_proc_get_locations(struct inode *inode,
6771 struct nfs4_fs_locations *locations,
6772 struct page *page, struct rpc_cred *cred)
6774 struct nfs_server *server = NFS_SERVER(inode);
6775 struct rpc_clnt *clnt = server->client;
6777 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6779 struct nfs4_fs_locations_arg args = {
6780 .fh = NFS_FH(inode),
6783 .migration = 1, /* skip LOOKUP */
6785 struct nfs4_fs_locations_res res = {
6786 .fs_locations = locations,
6789 struct rpc_message msg = {
6790 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6797 nfs_fattr_init(&locations->fattr);
6798 locations->server = server;
6799 locations->nlocations = 0;
6801 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6802 nfs4_set_sequence_privileged(&args.seq_args);
6803 status = nfs4_call_sync_sequence(clnt, server, &msg,
6804 &args.seq_args, &res.seq_res);
6805 if (status == NFS4_OK &&
6806 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6807 status = -NFS4ERR_LEASE_MOVED;
6811 #endif /* CONFIG_NFS_V4_1 */
6814 * nfs4_proc_get_locations - discover locations for a migrated FSID
6815 * @inode: inode on FSID that is migrating
6816 * @locations: result of query
6818 * @cred: credential to use for this operation
6820 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6821 * operation failed, or a negative errno if a local error occurred.
6823 * On success, "locations" is filled in, but if the server has
6824 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6827 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6828 * from this client that require migration recovery.
6830 int nfs4_proc_get_locations(struct inode *inode,
6831 struct nfs4_fs_locations *locations,
6832 struct page *page, struct rpc_cred *cred)
6834 struct nfs_server *server = NFS_SERVER(inode);
6835 struct nfs_client *clp = server->nfs_client;
6836 const struct nfs4_mig_recovery_ops *ops =
6837 clp->cl_mvops->mig_recovery_ops;
6838 struct nfs4_exception exception = { };
6841 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6842 (unsigned long long)server->fsid.major,
6843 (unsigned long long)server->fsid.minor,
6845 nfs_display_fhandle(NFS_FH(inode), __func__);
6848 status = ops->get_locations(inode, locations, page, cred);
6849 if (status != -NFS4ERR_DELAY)
6851 nfs4_handle_exception(server, status, &exception);
6852 } while (exception.retry);
6857 * This operation also signals the server that this client is
6858 * performing "lease moved" recovery. The server can stop
6859 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6860 * is appended to this compound to identify the client ID which is
6861 * performing recovery.
6863 static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6865 struct nfs_server *server = NFS_SERVER(inode);
6866 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6867 struct rpc_clnt *clnt = server->client;
6868 struct nfs4_fsid_present_arg args = {
6869 .fh = NFS_FH(inode),
6870 .clientid = clp->cl_clientid,
6871 .renew = 1, /* append RENEW */
6873 struct nfs4_fsid_present_res res = {
6876 struct rpc_message msg = {
6877 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6882 unsigned long now = jiffies;
6885 res.fh = nfs_alloc_fhandle();
6889 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6890 nfs4_set_sequence_privileged(&args.seq_args);
6891 status = nfs4_call_sync_sequence(clnt, server, &msg,
6892 &args.seq_args, &res.seq_res);
6893 nfs_free_fhandle(res.fh);
6897 do_renew_lease(clp, now);
6901 #ifdef CONFIG_NFS_V4_1
6904 * This operation also signals the server that this client is
6905 * performing "lease moved" recovery. The server can stop asserting
6906 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6907 * this operation is identified in the SEQUENCE operation in this
6910 static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6912 struct nfs_server *server = NFS_SERVER(inode);
6913 struct rpc_clnt *clnt = server->client;
6914 struct nfs4_fsid_present_arg args = {
6915 .fh = NFS_FH(inode),
6917 struct nfs4_fsid_present_res res = {
6919 struct rpc_message msg = {
6920 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6927 res.fh = nfs_alloc_fhandle();
6931 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6932 nfs4_set_sequence_privileged(&args.seq_args);
6933 status = nfs4_call_sync_sequence(clnt, server, &msg,
6934 &args.seq_args, &res.seq_res);
6935 nfs_free_fhandle(res.fh);
6936 if (status == NFS4_OK &&
6937 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6938 status = -NFS4ERR_LEASE_MOVED;
6942 #endif /* CONFIG_NFS_V4_1 */
6945 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6946 * @inode: inode on FSID to check
6947 * @cred: credential to use for this operation
6949 * Server indicates whether the FSID is present, moved, or not
6950 * recognized. This operation is necessary to clear a LEASE_MOVED
6951 * condition for this client ID.
6953 * Returns NFS4_OK if the FSID is present on this server,
6954 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6955 * NFS4ERR code if some error occurred on the server, or a
6956 * negative errno if a local failure occurred.
6958 int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6960 struct nfs_server *server = NFS_SERVER(inode);
6961 struct nfs_client *clp = server->nfs_client;
6962 const struct nfs4_mig_recovery_ops *ops =
6963 clp->cl_mvops->mig_recovery_ops;
6964 struct nfs4_exception exception = { };
6967 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6968 (unsigned long long)server->fsid.major,
6969 (unsigned long long)server->fsid.minor,
6971 nfs_display_fhandle(NFS_FH(inode), __func__);
6974 status = ops->fsid_present(inode, cred);
6975 if (status != -NFS4ERR_DELAY)
6977 nfs4_handle_exception(server, status, &exception);
6978 } while (exception.retry);
6983 * If 'use_integrity' is true and the state managment nfs_client
6984 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
6985 * and the machine credential as per RFC3530bis and RFC5661 Security
6986 * Considerations sections. Otherwise, just use the user cred with the
6987 * filesystem's rpc_client.
6989 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
6992 struct nfs4_secinfo_arg args = {
6993 .dir_fh = NFS_FH(dir),
6996 struct nfs4_secinfo_res res = {
6999 struct rpc_message msg = {
7000 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7004 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
7005 struct rpc_cred *cred = NULL;
7007 if (use_integrity) {
7008 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
7009 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7010 msg.rpc_cred = cred;
7013 dprintk("NFS call secinfo %s\n", name->name);
7015 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7016 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7018 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7020 dprintk("NFS reply secinfo: %d\n", status);
7028 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7029 struct nfs4_secinfo_flavors *flavors)
7031 struct nfs4_exception exception = { };
7034 err = -NFS4ERR_WRONGSEC;
7036 /* try to use integrity protection with machine cred */
7037 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7038 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7041 * if unable to use integrity protection, or SECINFO with
7042 * integrity protection returns NFS4ERR_WRONGSEC (which is
7043 * disallowed by spec, but exists in deployed servers) use
7044 * the current filesystem's rpc_client and the user cred.
7046 if (err == -NFS4ERR_WRONGSEC)
7047 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7049 trace_nfs4_secinfo(dir, name, err);
7050 err = nfs4_handle_exception(NFS_SERVER(dir), err,
7052 } while (exception.retry);
7056 #ifdef CONFIG_NFS_V4_1
7058 * Check the exchange flags returned by the server for invalid flags, having
7059 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7062 static int nfs4_check_cl_exchange_flags(u32 flags)
7064 if (flags & ~EXCHGID4_FLAG_MASK_R)
7066 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7067 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7069 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7073 return -NFS4ERR_INVAL;
7077 nfs41_same_server_scope(struct nfs41_server_scope *a,
7078 struct nfs41_server_scope *b)
7080 if (a->server_scope_sz == b->server_scope_sz &&
7081 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
7088 nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7092 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7093 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7097 * nfs4_proc_bind_one_conn_to_session()
7099 * The 4.1 client currently uses the same TCP connection for the
7100 * fore and backchannel.
7103 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7104 struct rpc_xprt *xprt,
7105 struct nfs_client *clp,
7106 struct rpc_cred *cred)
7109 struct nfs41_bind_conn_to_session_args args = {
7111 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7113 struct nfs41_bind_conn_to_session_res res;
7114 struct rpc_message msg = {
7116 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
7121 struct rpc_task_setup task_setup_data = {
7124 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
7125 .rpc_message = &msg,
7126 .flags = RPC_TASK_TIMEOUT,
7128 struct rpc_task *task;
7130 dprintk("--> %s\n", __func__);
7132 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7133 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7134 args.dir = NFS4_CDFC4_FORE;
7136 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7137 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7138 args.dir = NFS4_CDFC4_FORE;
7140 task = rpc_run_task(&task_setup_data);
7141 if (!IS_ERR(task)) {
7142 status = task->tk_status;
7145 status = PTR_ERR(task);
7146 trace_nfs4_bind_conn_to_session(clp, status);
7148 if (memcmp(res.sessionid.data,
7149 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7150 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7154 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
7155 dprintk("NFS: %s: Unexpected direction from server\n",
7160 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
7161 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7168 dprintk("<-- %s status= %d\n", __func__, status);
7172 struct rpc_bind_conn_calldata {
7173 struct nfs_client *clp;
7174 struct rpc_cred *cred;
7178 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7179 struct rpc_xprt *xprt,
7182 struct rpc_bind_conn_calldata *p = calldata;
7184 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7187 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7189 struct rpc_bind_conn_calldata data = {
7193 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7194 nfs4_proc_bind_conn_to_session_callback, &data);
7198 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7199 * and operations we'd like to see to enable certain features in the allow map
7201 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7202 .how = SP4_MACH_CRED,
7203 .enforce.u.words = {
7204 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7205 1 << (OP_EXCHANGE_ID - 32) |
7206 1 << (OP_CREATE_SESSION - 32) |
7207 1 << (OP_DESTROY_SESSION - 32) |
7208 1 << (OP_DESTROY_CLIENTID - 32)
7211 [0] = 1 << (OP_CLOSE) |
7212 1 << (OP_OPEN_DOWNGRADE) |
7214 1 << (OP_DELEGRETURN) |
7216 [1] = 1 << (OP_SECINFO - 32) |
7217 1 << (OP_SECINFO_NO_NAME - 32) |
7218 1 << (OP_LAYOUTRETURN - 32) |
7219 1 << (OP_TEST_STATEID - 32) |
7220 1 << (OP_FREE_STATEID - 32) |
7221 1 << (OP_WRITE - 32)
7226 * Select the state protection mode for client `clp' given the server results
7227 * from exchange_id in `sp'.
7229 * Returns 0 on success, negative errno otherwise.
7231 static int nfs4_sp4_select_mode(struct nfs_client *clp,
7232 struct nfs41_state_protection *sp)
7234 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7235 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7236 1 << (OP_EXCHANGE_ID - 32) |
7237 1 << (OP_CREATE_SESSION - 32) |
7238 1 << (OP_DESTROY_SESSION - 32) |
7239 1 << (OP_DESTROY_CLIENTID - 32)
7243 if (sp->how == SP4_MACH_CRED) {
7244 /* Print state protect result */
7245 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7246 for (i = 0; i <= LAST_NFS4_OP; i++) {
7247 if (test_bit(i, sp->enforce.u.longs))
7248 dfprintk(MOUNT, " enforce op %d\n", i);
7249 if (test_bit(i, sp->allow.u.longs))
7250 dfprintk(MOUNT, " allow op %d\n", i);
7253 /* make sure nothing is on enforce list that isn't supported */
7254 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7255 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7256 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7262 * Minimal mode - state operations are allowed to use machine
7263 * credential. Note this already happens by default, so the
7264 * client doesn't have to do anything more than the negotiation.
7266 * NOTE: we don't care if EXCHANGE_ID is in the list -
7267 * we're already using the machine cred for exchange_id
7268 * and will never use a different cred.
7270 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7271 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7272 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7273 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7274 dfprintk(MOUNT, "sp4_mach_cred:\n");
7275 dfprintk(MOUNT, " minimal mode enabled\n");
7276 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
7278 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7282 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
7283 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7284 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
7285 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7286 dfprintk(MOUNT, " cleanup mode enabled\n");
7287 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
7290 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7291 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7292 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
7293 &clp->cl_sp4_flags);
7296 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7297 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7298 dfprintk(MOUNT, " secinfo mode enabled\n");
7299 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
7302 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7303 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7304 dfprintk(MOUNT, " stateid mode enabled\n");
7305 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
7308 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7309 dfprintk(MOUNT, " write mode enabled\n");
7310 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
7313 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7314 dfprintk(MOUNT, " commit mode enabled\n");
7315 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
7322 struct nfs41_exchange_id_data {
7323 struct nfs41_exchange_id_res res;
7324 struct nfs41_exchange_id_args args;
7325 struct rpc_xprt *xprt;
7329 static void nfs4_exchange_id_done(struct rpc_task *task, void *data)
7331 struct nfs41_exchange_id_data *cdata =
7332 (struct nfs41_exchange_id_data *)data;
7333 struct nfs_client *clp = cdata->args.client;
7334 int status = task->tk_status;
7336 trace_nfs4_exchange_id(clp, status);
7339 status = nfs4_check_cl_exchange_flags(cdata->res.flags);
7341 if (cdata->xprt && status == 0) {
7342 status = nfs4_detect_session_trunking(clp, &cdata->res,
7348 status = nfs4_sp4_select_mode(clp, &cdata->res.state_protect);
7351 clp->cl_clientid = cdata->res.clientid;
7352 clp->cl_exchange_flags = cdata->res.flags;
7353 /* Client ID is not confirmed */
7354 if (!(cdata->res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
7355 clear_bit(NFS4_SESSION_ESTABLISHED,
7356 &clp->cl_session->session_state);
7357 clp->cl_seqid = cdata->res.seqid;
7360 kfree(clp->cl_serverowner);
7361 clp->cl_serverowner = cdata->res.server_owner;
7362 cdata->res.server_owner = NULL;
7364 /* use the most recent implementation id */
7365 kfree(clp->cl_implid);
7366 clp->cl_implid = cdata->res.impl_id;
7367 cdata->res.impl_id = NULL;
7369 if (clp->cl_serverscope != NULL &&
7370 !nfs41_same_server_scope(clp->cl_serverscope,
7371 cdata->res.server_scope)) {
7372 dprintk("%s: server_scope mismatch detected\n",
7374 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
7375 kfree(clp->cl_serverscope);
7376 clp->cl_serverscope = NULL;
7379 if (clp->cl_serverscope == NULL) {
7380 clp->cl_serverscope = cdata->res.server_scope;
7381 cdata->res.server_scope = NULL;
7383 /* Save the EXCHANGE_ID verifier session trunk tests */
7384 memcpy(clp->cl_confirm.data, cdata->args.verifier->data,
7385 sizeof(clp->cl_confirm.data));
7388 cdata->rpc_status = status;
7392 static void nfs4_exchange_id_release(void *data)
7394 struct nfs41_exchange_id_data *cdata =
7395 (struct nfs41_exchange_id_data *)data;
7397 nfs_put_client(cdata->args.client);
7399 xprt_put(cdata->xprt);
7400 rpc_clnt_xprt_switch_put(cdata->args.client->cl_rpcclient);
7402 kfree(cdata->res.impl_id);
7403 kfree(cdata->res.server_scope);
7404 kfree(cdata->res.server_owner);
7408 static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7409 .rpc_call_done = nfs4_exchange_id_done,
7410 .rpc_release = nfs4_exchange_id_release,
7414 * _nfs4_proc_exchange_id()
7416 * Wrapper for EXCHANGE_ID operation.
7418 static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
7419 u32 sp4_how, struct rpc_xprt *xprt)
7421 nfs4_verifier verifier;
7422 struct rpc_message msg = {
7423 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
7426 struct rpc_task_setup task_setup_data = {
7427 .rpc_client = clp->cl_rpcclient,
7428 .callback_ops = &nfs4_exchange_id_call_ops,
7429 .rpc_message = &msg,
7430 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7432 struct nfs41_exchange_id_data *calldata;
7433 struct rpc_task *task;
7436 if (!atomic_inc_not_zero(&clp->cl_count))
7440 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7445 nfs4_init_boot_verifier(clp, &verifier);
7447 status = nfs4_init_uniform_client_string(clp);
7451 dprintk("NFS call exchange_id auth=%s, '%s'\n",
7452 clp->cl_rpcclient->cl_auth->au_ops->au_name,
7455 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7458 if (unlikely(calldata->res.server_owner == NULL))
7461 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
7463 if (unlikely(calldata->res.server_scope == NULL))
7464 goto out_server_owner;
7466 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7467 if (unlikely(calldata->res.impl_id == NULL))
7468 goto out_server_scope;
7472 calldata->args.state_protect.how = SP4_NONE;
7476 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
7486 calldata->xprt = xprt;
7487 task_setup_data.rpc_xprt = xprt;
7488 task_setup_data.flags =
7489 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC;
7490 calldata->args.verifier = &clp->cl_confirm;
7492 calldata->args.verifier = &verifier;
7494 calldata->args.client = clp;
7495 #ifdef CONFIG_NFS_V4_1_MIGRATION
7496 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7497 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7498 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7500 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7501 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7503 msg.rpc_argp = &calldata->args;
7504 msg.rpc_resp = &calldata->res;
7505 task_setup_data.callback_data = calldata;
7507 task = rpc_run_task(&task_setup_data);
7509 status = PTR_ERR(task);
7514 status = rpc_wait_for_completion_task(task);
7516 status = calldata->rpc_status;
7517 } else /* session trunking test */
7518 status = calldata->rpc_status;
7522 if (clp->cl_implid != NULL)
7523 dprintk("NFS reply exchange_id: Server Implementation ID: "
7524 "domain: %s, name: %s, date: %llu,%u\n",
7525 clp->cl_implid->domain, clp->cl_implid->name,
7526 clp->cl_implid->date.seconds,
7527 clp->cl_implid->date.nseconds);
7528 dprintk("NFS reply exchange_id: %d\n", status);
7532 kfree(calldata->res.impl_id);
7534 kfree(calldata->res.server_scope);
7536 kfree(calldata->res.server_owner);
7543 * nfs4_proc_exchange_id()
7545 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7547 * Since the clientid has expired, all compounds using sessions
7548 * associated with the stale clientid will be returning
7549 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7550 * be in some phase of session reset.
7552 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7554 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7556 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7559 /* try SP4_MACH_CRED if krb5i/p */
7560 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7561 authflavor == RPC_AUTH_GSS_KRB5P) {
7562 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED, NULL);
7568 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE, NULL);
7572 * nfs4_test_session_trunk
7574 * This is an add_xprt_test() test function called from
7575 * rpc_clnt_setup_test_and_add_xprt.
7577 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7578 * and is dereferrenced in nfs4_exchange_id_release
7580 * Upon success, add the new transport to the rpc_clnt
7582 * @clnt: struct rpc_clnt to get new transport
7583 * @xprt: the rpc_xprt to test
7584 * @data: call data for _nfs4_proc_exchange_id.
7586 int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7589 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
7592 dprintk("--> %s try %s\n", __func__,
7593 xprt->address_strings[RPC_DISPLAY_ADDR]);
7595 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7597 /* Test connection for session trunking. Async exchange_id call */
7598 return _nfs4_proc_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7600 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7602 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7603 struct rpc_cred *cred)
7605 struct rpc_message msg = {
7606 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7612 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7613 trace_nfs4_destroy_clientid(clp, status);
7615 dprintk("NFS: Got error %d from the server %s on "
7616 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7620 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7621 struct rpc_cred *cred)
7626 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7627 ret = _nfs4_proc_destroy_clientid(clp, cred);
7629 case -NFS4ERR_DELAY:
7630 case -NFS4ERR_CLIENTID_BUSY:
7640 int nfs4_destroy_clientid(struct nfs_client *clp)
7642 struct rpc_cred *cred;
7645 if (clp->cl_mvops->minor_version < 1)
7647 if (clp->cl_exchange_flags == 0)
7649 if (clp->cl_preserve_clid)
7651 cred = nfs4_get_clid_cred(clp);
7652 ret = nfs4_proc_destroy_clientid(clp, cred);
7657 case -NFS4ERR_STALE_CLIENTID:
7658 clp->cl_exchange_flags = 0;
7664 struct nfs4_get_lease_time_data {
7665 struct nfs4_get_lease_time_args *args;
7666 struct nfs4_get_lease_time_res *res;
7667 struct nfs_client *clp;
7670 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7673 struct nfs4_get_lease_time_data *data =
7674 (struct nfs4_get_lease_time_data *)calldata;
7676 dprintk("--> %s\n", __func__);
7677 /* just setup sequence, do not trigger session recovery
7678 since we're invoked within one */
7679 nfs41_setup_sequence(data->clp->cl_session,
7680 &data->args->la_seq_args,
7681 &data->res->lr_seq_res,
7683 dprintk("<-- %s\n", __func__);
7687 * Called from nfs4_state_manager thread for session setup, so don't recover
7688 * from sequence operation or clientid errors.
7690 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7692 struct nfs4_get_lease_time_data *data =
7693 (struct nfs4_get_lease_time_data *)calldata;
7695 dprintk("--> %s\n", __func__);
7696 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7698 switch (task->tk_status) {
7699 case -NFS4ERR_DELAY:
7700 case -NFS4ERR_GRACE:
7701 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7702 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7703 task->tk_status = 0;
7705 case -NFS4ERR_RETRY_UNCACHED_REP:
7706 rpc_restart_call_prepare(task);
7709 dprintk("<-- %s\n", __func__);
7712 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
7713 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7714 .rpc_call_done = nfs4_get_lease_time_done,
7717 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7719 struct rpc_task *task;
7720 struct nfs4_get_lease_time_args args;
7721 struct nfs4_get_lease_time_res res = {
7722 .lr_fsinfo = fsinfo,
7724 struct nfs4_get_lease_time_data data = {
7729 struct rpc_message msg = {
7730 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7734 struct rpc_task_setup task_setup = {
7735 .rpc_client = clp->cl_rpcclient,
7736 .rpc_message = &msg,
7737 .callback_ops = &nfs4_get_lease_time_ops,
7738 .callback_data = &data,
7739 .flags = RPC_TASK_TIMEOUT,
7743 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
7744 nfs4_set_sequence_privileged(&args.la_seq_args);
7745 dprintk("--> %s\n", __func__);
7746 task = rpc_run_task(&task_setup);
7749 status = PTR_ERR(task);
7751 status = task->tk_status;
7754 dprintk("<-- %s return %d\n", __func__, status);
7760 * Initialize the values to be used by the client in CREATE_SESSION
7761 * If nfs4_init_session set the fore channel request and response sizes,
7764 * Set the back channel max_resp_sz_cached to zero to force the client to
7765 * always set csa_cachethis to FALSE because the current implementation
7766 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7768 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7769 struct rpc_clnt *clnt)
7771 unsigned int max_rqst_sz, max_resp_sz;
7772 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
7774 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7775 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7777 /* Fore channel attributes */
7778 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7779 args->fc_attrs.max_resp_sz = max_resp_sz;
7780 args->fc_attrs.max_ops = NFS4_MAX_OPS;
7781 args->fc_attrs.max_reqs = max_session_slots;
7783 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
7784 "max_ops=%u max_reqs=%u\n",
7786 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
7787 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
7789 /* Back channel attributes */
7790 args->bc_attrs.max_rqst_sz = max_bc_payload;
7791 args->bc_attrs.max_resp_sz = max_bc_payload;
7792 args->bc_attrs.max_resp_sz_cached = 0;
7793 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
7794 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
7796 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7797 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7799 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7800 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7801 args->bc_attrs.max_reqs);
7804 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7805 struct nfs41_create_session_res *res)
7807 struct nfs4_channel_attrs *sent = &args->fc_attrs;
7808 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
7810 if (rcvd->max_resp_sz > sent->max_resp_sz)
7813 * Our requested max_ops is the minimum we need; we're not
7814 * prepared to break up compounds into smaller pieces than that.
7815 * So, no point even trying to continue if the server won't
7818 if (rcvd->max_ops < sent->max_ops)
7820 if (rcvd->max_reqs == 0)
7822 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7823 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
7827 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7828 struct nfs41_create_session_res *res)
7830 struct nfs4_channel_attrs *sent = &args->bc_attrs;
7831 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
7833 if (!(res->flags & SESSION4_BACK_CHAN))
7835 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7837 if (rcvd->max_resp_sz < sent->max_resp_sz)
7839 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7841 if (rcvd->max_ops > sent->max_ops)
7843 if (rcvd->max_reqs > sent->max_reqs)
7849 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
7850 struct nfs41_create_session_res *res)
7854 ret = nfs4_verify_fore_channel_attrs(args, res);
7857 return nfs4_verify_back_channel_attrs(args, res);
7860 static void nfs4_update_session(struct nfs4_session *session,
7861 struct nfs41_create_session_res *res)
7863 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
7864 /* Mark client id and session as being confirmed */
7865 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7866 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
7867 session->flags = res->flags;
7868 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
7869 if (res->flags & SESSION4_BACK_CHAN)
7870 memcpy(&session->bc_attrs, &res->bc_attrs,
7871 sizeof(session->bc_attrs));
7874 static int _nfs4_proc_create_session(struct nfs_client *clp,
7875 struct rpc_cred *cred)
7877 struct nfs4_session *session = clp->cl_session;
7878 struct nfs41_create_session_args args = {
7880 .clientid = clp->cl_clientid,
7881 .seqid = clp->cl_seqid,
7882 .cb_program = NFS4_CALLBACK,
7884 struct nfs41_create_session_res res;
7886 struct rpc_message msg = {
7887 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7894 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
7895 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
7897 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7898 trace_nfs4_create_session(clp, status);
7901 case -NFS4ERR_STALE_CLIENTID:
7902 case -NFS4ERR_DELAY:
7911 /* Verify the session's negotiated channel_attrs values */
7912 status = nfs4_verify_channel_attrs(&args, &res);
7913 /* Increment the clientid slot sequence id */
7916 nfs4_update_session(session, &res);
7923 * Issues a CREATE_SESSION operation to the server.
7924 * It is the responsibility of the caller to verify the session is
7925 * expired before calling this routine.
7927 int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
7931 struct nfs4_session *session = clp->cl_session;
7933 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7935 status = _nfs4_proc_create_session(clp, cred);
7939 /* Init or reset the session slot tables */
7940 status = nfs4_setup_session_slot_tables(session);
7941 dprintk("slot table setup returned %d\n", status);
7945 ptr = (unsigned *)&session->sess_id.data[0];
7946 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7947 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
7949 dprintk("<-- %s\n", __func__);
7954 * Issue the over-the-wire RPC DESTROY_SESSION.
7955 * The caller must serialize access to this routine.
7957 int nfs4_proc_destroy_session(struct nfs4_session *session,
7958 struct rpc_cred *cred)
7960 struct rpc_message msg = {
7961 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
7962 .rpc_argp = session,
7967 dprintk("--> nfs4_proc_destroy_session\n");
7969 /* session is still being setup */
7970 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
7973 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7974 trace_nfs4_destroy_session(session->clp, status);
7977 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
7978 "Session has been destroyed regardless...\n", status);
7980 dprintk("<-- nfs4_proc_destroy_session\n");
7985 * Renew the cl_session lease.
7987 struct nfs4_sequence_data {
7988 struct nfs_client *clp;
7989 struct nfs4_sequence_args args;
7990 struct nfs4_sequence_res res;
7993 static void nfs41_sequence_release(void *data)
7995 struct nfs4_sequence_data *calldata = data;
7996 struct nfs_client *clp = calldata->clp;
7998 if (atomic_read(&clp->cl_count) > 1)
7999 nfs4_schedule_state_renewal(clp);
8000 nfs_put_client(clp);
8004 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8006 switch(task->tk_status) {
8007 case -NFS4ERR_DELAY:
8008 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8011 nfs4_schedule_lease_recovery(clp);
8016 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
8018 struct nfs4_sequence_data *calldata = data;
8019 struct nfs_client *clp = calldata->clp;
8021 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8024 trace_nfs4_sequence(clp, task->tk_status);
8025 if (task->tk_status < 0) {
8026 dprintk("%s ERROR %d\n", __func__, task->tk_status);
8027 if (atomic_read(&clp->cl_count) == 1)
8030 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8031 rpc_restart_call_prepare(task);
8035 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
8037 dprintk("<-- %s\n", __func__);
8040 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8042 struct nfs4_sequence_data *calldata = data;
8043 struct nfs_client *clp = calldata->clp;
8044 struct nfs4_sequence_args *args;
8045 struct nfs4_sequence_res *res;
8047 args = task->tk_msg.rpc_argp;
8048 res = task->tk_msg.rpc_resp;
8050 nfs41_setup_sequence(clp->cl_session, args, res, task);
8053 static const struct rpc_call_ops nfs41_sequence_ops = {
8054 .rpc_call_done = nfs41_sequence_call_done,
8055 .rpc_call_prepare = nfs41_sequence_prepare,
8056 .rpc_release = nfs41_sequence_release,
8059 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8060 struct rpc_cred *cred,
8063 struct nfs4_sequence_data *calldata;
8064 struct rpc_message msg = {
8065 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8068 struct rpc_task_setup task_setup_data = {
8069 .rpc_client = clp->cl_rpcclient,
8070 .rpc_message = &msg,
8071 .callback_ops = &nfs41_sequence_ops,
8072 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
8075 if (!atomic_inc_not_zero(&clp->cl_count))
8076 return ERR_PTR(-EIO);
8077 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8078 if (calldata == NULL) {
8079 nfs_put_client(clp);
8080 return ERR_PTR(-ENOMEM);
8082 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
8084 nfs4_set_sequence_privileged(&calldata->args);
8085 msg.rpc_argp = &calldata->args;
8086 msg.rpc_resp = &calldata->res;
8087 calldata->clp = clp;
8088 task_setup_data.callback_data = calldata;
8090 return rpc_run_task(&task_setup_data);
8093 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
8095 struct rpc_task *task;
8098 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
8100 task = _nfs41_proc_sequence(clp, cred, false);
8102 ret = PTR_ERR(task);
8104 rpc_put_task_async(task);
8105 dprintk("<-- %s status=%d\n", __func__, ret);
8109 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8111 struct rpc_task *task;
8114 task = _nfs41_proc_sequence(clp, cred, true);
8116 ret = PTR_ERR(task);
8119 ret = rpc_wait_for_completion_task(task);
8121 ret = task->tk_status;
8124 dprintk("<-- %s status=%d\n", __func__, ret);
8128 struct nfs4_reclaim_complete_data {
8129 struct nfs_client *clp;
8130 struct nfs41_reclaim_complete_args arg;
8131 struct nfs41_reclaim_complete_res res;
8134 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8136 struct nfs4_reclaim_complete_data *calldata = data;
8138 nfs41_setup_sequence(calldata->clp->cl_session,
8139 &calldata->arg.seq_args,
8140 &calldata->res.seq_res,
8144 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8146 switch(task->tk_status) {
8148 case -NFS4ERR_COMPLETE_ALREADY:
8149 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8151 case -NFS4ERR_DELAY:
8152 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8154 case -NFS4ERR_RETRY_UNCACHED_REP:
8157 nfs4_schedule_lease_recovery(clp);
8162 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8164 struct nfs4_reclaim_complete_data *calldata = data;
8165 struct nfs_client *clp = calldata->clp;
8166 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8168 dprintk("--> %s\n", __func__);
8169 if (!nfs41_sequence_done(task, res))
8172 trace_nfs4_reclaim_complete(clp, task->tk_status);
8173 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8174 rpc_restart_call_prepare(task);
8177 dprintk("<-- %s\n", __func__);
8180 static void nfs4_free_reclaim_complete_data(void *data)
8182 struct nfs4_reclaim_complete_data *calldata = data;
8187 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8188 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8189 .rpc_call_done = nfs4_reclaim_complete_done,
8190 .rpc_release = nfs4_free_reclaim_complete_data,
8194 * Issue a global reclaim complete.
8196 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8197 struct rpc_cred *cred)
8199 struct nfs4_reclaim_complete_data *calldata;
8200 struct rpc_task *task;
8201 struct rpc_message msg = {
8202 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
8205 struct rpc_task_setup task_setup_data = {
8206 .rpc_client = clp->cl_rpcclient,
8207 .rpc_message = &msg,
8208 .callback_ops = &nfs4_reclaim_complete_call_ops,
8209 .flags = RPC_TASK_ASYNC,
8211 int status = -ENOMEM;
8213 dprintk("--> %s\n", __func__);
8214 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8215 if (calldata == NULL)
8217 calldata->clp = clp;
8218 calldata->arg.one_fs = 0;
8220 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
8221 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
8222 msg.rpc_argp = &calldata->arg;
8223 msg.rpc_resp = &calldata->res;
8224 task_setup_data.callback_data = calldata;
8225 task = rpc_run_task(&task_setup_data);
8227 status = PTR_ERR(task);
8230 status = nfs4_wait_for_completion_rpc_task(task);
8232 status = task->tk_status;
8236 dprintk("<-- %s status=%d\n", __func__, status);
8241 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8243 struct nfs4_layoutget *lgp = calldata;
8244 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
8245 struct nfs4_session *session = nfs4_get_session(server);
8247 dprintk("--> %s\n", __func__);
8248 nfs41_setup_sequence(session, &lgp->args.seq_args,
8249 &lgp->res.seq_res, task);
8250 dprintk("<-- %s\n", __func__);
8253 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8255 struct nfs4_layoutget *lgp = calldata;
8257 dprintk("--> %s\n", __func__);
8258 nfs41_sequence_process(task, &lgp->res.seq_res);
8259 dprintk("<-- %s\n", __func__);
8263 nfs4_layoutget_handle_exception(struct rpc_task *task,
8264 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8266 struct inode *inode = lgp->args.inode;
8267 struct nfs_server *server = NFS_SERVER(inode);
8268 struct pnfs_layout_hdr *lo;
8269 int nfs4err = task->tk_status;
8270 int err, status = 0;
8273 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
8280 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8281 * on the file. set tk_status to -ENODATA to tell upper layer to
8284 case -NFS4ERR_LAYOUTUNAVAILABLE:
8288 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8289 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8291 case -NFS4ERR_BADLAYOUT:
8292 status = -EOVERFLOW;
8295 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
8296 * (or clients) writing to the same RAID stripe except when
8297 * the minlength argument is 0 (see RFC5661 section 18.43.3).
8299 * Treat it like we would RECALLCONFLICT -- we retry for a little
8300 * while, and then eventually give up.
8302 case -NFS4ERR_LAYOUTTRYLATER:
8303 if (lgp->args.minlength == 0) {
8304 status = -EOVERFLOW;
8309 case -NFS4ERR_RECALLCONFLICT:
8310 status = -ERECALLCONFLICT;
8312 case -NFS4ERR_DELEG_REVOKED:
8313 case -NFS4ERR_ADMIN_REVOKED:
8314 case -NFS4ERR_EXPIRED:
8315 case -NFS4ERR_BAD_STATEID:
8316 exception->timeout = 0;
8317 spin_lock(&inode->i_lock);
8318 lo = NFS_I(inode)->layout;
8319 /* If the open stateid was bad, then recover it. */
8320 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8321 nfs4_stateid_match_other(&lgp->args.stateid,
8322 &lgp->args.ctx->state->stateid)) {
8323 spin_unlock(&inode->i_lock);
8324 exception->state = lgp->args.ctx->state;
8325 exception->stateid = &lgp->args.stateid;
8330 * Mark the bad layout state as invalid, then retry
8332 pnfs_mark_layout_stateid_invalid(lo, &head);
8333 spin_unlock(&inode->i_lock);
8334 pnfs_free_lseg_list(&head);
8339 err = nfs4_handle_exception(server, nfs4err, exception);
8341 if (exception->retry)
8347 dprintk("<-- %s\n", __func__);
8351 static size_t max_response_pages(struct nfs_server *server)
8353 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8354 return nfs_page_array_len(0, max_resp_sz);
8357 static void nfs4_free_pages(struct page **pages, size_t size)
8364 for (i = 0; i < size; i++) {
8367 __free_page(pages[i]);
8372 static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8374 struct page **pages;
8377 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8379 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8383 for (i = 0; i < size; i++) {
8384 pages[i] = alloc_page(gfp_flags);
8386 dprintk("%s: failed to allocate page\n", __func__);
8387 nfs4_free_pages(pages, size);
8395 static void nfs4_layoutget_release(void *calldata)
8397 struct nfs4_layoutget *lgp = calldata;
8398 struct inode *inode = lgp->args.inode;
8399 struct nfs_server *server = NFS_SERVER(inode);
8400 size_t max_pages = max_response_pages(server);
8402 dprintk("--> %s\n", __func__);
8403 nfs4_free_pages(lgp->args.layout.pages, max_pages);
8404 pnfs_put_layout_hdr(NFS_I(inode)->layout);
8405 put_nfs_open_context(lgp->args.ctx);
8407 dprintk("<-- %s\n", __func__);
8410 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8411 .rpc_call_prepare = nfs4_layoutget_prepare,
8412 .rpc_call_done = nfs4_layoutget_done,
8413 .rpc_release = nfs4_layoutget_release,
8416 struct pnfs_layout_segment *
8417 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
8419 struct inode *inode = lgp->args.inode;
8420 struct nfs_server *server = NFS_SERVER(inode);
8421 size_t max_pages = max_response_pages(server);
8422 struct rpc_task *task;
8423 struct rpc_message msg = {
8424 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8425 .rpc_argp = &lgp->args,
8426 .rpc_resp = &lgp->res,
8427 .rpc_cred = lgp->cred,
8429 struct rpc_task_setup task_setup_data = {
8430 .rpc_client = server->client,
8431 .rpc_message = &msg,
8432 .callback_ops = &nfs4_layoutget_call_ops,
8433 .callback_data = lgp,
8434 .flags = RPC_TASK_ASYNC,
8436 struct pnfs_layout_segment *lseg = NULL;
8437 struct nfs4_exception exception = {
8439 .timeout = *timeout,
8443 dprintk("--> %s\n", __func__);
8445 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8446 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8448 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8449 if (!lgp->args.layout.pages) {
8450 nfs4_layoutget_release(lgp);
8451 return ERR_PTR(-ENOMEM);
8453 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8455 lgp->res.layoutp = &lgp->args.layout;
8456 lgp->res.seq_res.sr_slot = NULL;
8457 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
8459 task = rpc_run_task(&task_setup_data);
8461 return ERR_CAST(task);
8462 status = nfs4_wait_for_completion_rpc_task(task);
8464 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8465 *timeout = exception.timeout;
8468 trace_nfs4_layoutget(lgp->args.ctx,
8474 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8475 if (status == 0 && lgp->res.layoutp->len)
8476 lseg = pnfs_layout_process(lgp);
8477 nfs4_sequence_free_slot(&lgp->res.seq_res);
8479 dprintk("<-- %s status=%d\n", __func__, status);
8481 return ERR_PTR(status);
8486 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8488 struct nfs4_layoutreturn *lrp = calldata;
8490 dprintk("--> %s\n", __func__);
8491 nfs41_setup_sequence(lrp->clp->cl_session,
8492 &lrp->args.seq_args,
8497 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8499 struct nfs4_layoutreturn *lrp = calldata;
8500 struct nfs_server *server;
8502 dprintk("--> %s\n", __func__);
8504 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
8507 server = NFS_SERVER(lrp->args.inode);
8508 switch (task->tk_status) {
8510 task->tk_status = 0;
8513 case -NFS4ERR_DELAY:
8514 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
8516 nfs4_sequence_free_slot(&lrp->res.seq_res);
8517 rpc_restart_call_prepare(task);
8520 dprintk("<-- %s\n", __func__);
8523 static void nfs4_layoutreturn_release(void *calldata)
8525 struct nfs4_layoutreturn *lrp = calldata;
8526 struct pnfs_layout_hdr *lo = lrp->args.layout;
8529 dprintk("--> %s\n", __func__);
8530 spin_lock(&lo->plh_inode->i_lock);
8531 if (lrp->res.lrs_present) {
8532 pnfs_mark_matching_lsegs_invalid(lo, &freeme,
8534 be32_to_cpu(lrp->args.stateid.seqid));
8535 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
8537 pnfs_mark_layout_stateid_invalid(lo, &freeme);
8538 pnfs_clear_layoutreturn_waitbit(lo);
8539 spin_unlock(&lo->plh_inode->i_lock);
8540 nfs4_sequence_free_slot(&lrp->res.seq_res);
8541 pnfs_free_lseg_list(&freeme);
8542 pnfs_put_layout_hdr(lrp->args.layout);
8543 nfs_iput_and_deactive(lrp->inode);
8545 dprintk("<-- %s\n", __func__);
8548 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8549 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8550 .rpc_call_done = nfs4_layoutreturn_done,
8551 .rpc_release = nfs4_layoutreturn_release,
8554 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
8556 struct rpc_task *task;
8557 struct rpc_message msg = {
8558 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8559 .rpc_argp = &lrp->args,
8560 .rpc_resp = &lrp->res,
8561 .rpc_cred = lrp->cred,
8563 struct rpc_task_setup task_setup_data = {
8564 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
8565 .rpc_message = &msg,
8566 .callback_ops = &nfs4_layoutreturn_call_ops,
8567 .callback_data = lrp,
8571 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8572 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8573 &task_setup_data.rpc_client, &msg);
8575 dprintk("--> %s\n", __func__);
8577 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8579 nfs4_layoutreturn_release(lrp);
8582 task_setup_data.flags |= RPC_TASK_ASYNC;
8584 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
8585 task = rpc_run_task(&task_setup_data);
8587 return PTR_ERR(task);
8589 status = task->tk_status;
8590 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
8591 dprintk("<-- %s status=%d\n", __func__, status);
8597 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
8598 struct pnfs_device *pdev,
8599 struct rpc_cred *cred)
8601 struct nfs4_getdeviceinfo_args args = {
8603 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8604 NOTIFY_DEVICEID4_DELETE,
8606 struct nfs4_getdeviceinfo_res res = {
8609 struct rpc_message msg = {
8610 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8617 dprintk("--> %s\n", __func__);
8618 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
8619 if (res.notification & ~args.notify_types)
8620 dprintk("%s: unsupported notification\n", __func__);
8621 if (res.notification != args.notify_types)
8624 dprintk("<-- %s status=%d\n", __func__, status);
8629 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8630 struct pnfs_device *pdev,
8631 struct rpc_cred *cred)
8633 struct nfs4_exception exception = { };
8637 err = nfs4_handle_exception(server,
8638 _nfs4_proc_getdeviceinfo(server, pdev, cred),
8640 } while (exception.retry);
8643 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8645 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8647 struct nfs4_layoutcommit_data *data = calldata;
8648 struct nfs_server *server = NFS_SERVER(data->args.inode);
8649 struct nfs4_session *session = nfs4_get_session(server);
8651 nfs41_setup_sequence(session,
8652 &data->args.seq_args,
8658 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8660 struct nfs4_layoutcommit_data *data = calldata;
8661 struct nfs_server *server = NFS_SERVER(data->args.inode);
8663 if (!nfs41_sequence_done(task, &data->res.seq_res))
8666 switch (task->tk_status) { /* Just ignore these failures */
8667 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8668 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8669 case -NFS4ERR_BADLAYOUT: /* no layout */
8670 case -NFS4ERR_GRACE: /* loca_recalim always false */
8671 task->tk_status = 0;
8675 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
8676 rpc_restart_call_prepare(task);
8682 static void nfs4_layoutcommit_release(void *calldata)
8684 struct nfs4_layoutcommit_data *data = calldata;
8686 pnfs_cleanup_layoutcommit(data);
8687 nfs_post_op_update_inode_force_wcc(data->args.inode,
8689 put_rpccred(data->cred);
8690 nfs_iput_and_deactive(data->inode);
8694 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8695 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8696 .rpc_call_done = nfs4_layoutcommit_done,
8697 .rpc_release = nfs4_layoutcommit_release,
8701 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
8703 struct rpc_message msg = {
8704 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8705 .rpc_argp = &data->args,
8706 .rpc_resp = &data->res,
8707 .rpc_cred = data->cred,
8709 struct rpc_task_setup task_setup_data = {
8710 .task = &data->task,
8711 .rpc_client = NFS_CLIENT(data->args.inode),
8712 .rpc_message = &msg,
8713 .callback_ops = &nfs4_layoutcommit_ops,
8714 .callback_data = data,
8716 struct rpc_task *task;
8719 dprintk("NFS: initiating layoutcommit call. sync %d "
8720 "lbw: %llu inode %lu\n", sync,
8721 data->args.lastbytewritten,
8722 data->args.inode->i_ino);
8725 data->inode = nfs_igrab_and_active(data->args.inode);
8726 if (data->inode == NULL) {
8727 nfs4_layoutcommit_release(data);
8730 task_setup_data.flags = RPC_TASK_ASYNC;
8732 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
8733 task = rpc_run_task(&task_setup_data);
8735 return PTR_ERR(task);
8737 status = task->tk_status;
8738 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
8739 dprintk("%s: status %d\n", __func__, status);
8745 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8746 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8749 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8750 struct nfs_fsinfo *info,
8751 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
8753 struct nfs41_secinfo_no_name_args args = {
8754 .style = SECINFO_STYLE_CURRENT_FH,
8756 struct nfs4_secinfo_res res = {
8759 struct rpc_message msg = {
8760 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8764 struct rpc_clnt *clnt = server->client;
8765 struct rpc_cred *cred = NULL;
8768 if (use_integrity) {
8769 clnt = server->nfs_client->cl_rpcclient;
8770 cred = nfs4_get_clid_cred(server->nfs_client);
8771 msg.rpc_cred = cred;
8774 dprintk("--> %s\n", __func__);
8775 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8777 dprintk("<-- %s status=%d\n", __func__, status);
8786 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8787 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8789 struct nfs4_exception exception = { };
8792 /* first try using integrity protection */
8793 err = -NFS4ERR_WRONGSEC;
8795 /* try to use integrity protection with machine cred */
8796 if (_nfs4_is_integrity_protected(server->nfs_client))
8797 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8801 * if unable to use integrity protection, or SECINFO with
8802 * integrity protection returns NFS4ERR_WRONGSEC (which is
8803 * disallowed by spec, but exists in deployed servers) use
8804 * the current filesystem's rpc_client and the user cred.
8806 if (err == -NFS4ERR_WRONGSEC)
8807 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8812 case -NFS4ERR_WRONGSEC:
8816 err = nfs4_handle_exception(server, err, &exception);
8818 } while (exception.retry);
8824 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8825 struct nfs_fsinfo *info)
8829 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
8830 struct nfs4_secinfo_flavors *flavors;
8831 struct nfs4_secinfo4 *secinfo;
8834 page = alloc_page(GFP_KERNEL);
8840 flavors = page_address(page);
8841 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8844 * Fall back on "guess and check" method if
8845 * the server doesn't support SECINFO_NO_NAME
8847 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
8848 err = nfs4_find_root_sec(server, fhandle, info);
8854 for (i = 0; i < flavors->num_flavors; i++) {
8855 secinfo = &flavors->flavors[i];
8857 switch (secinfo->flavor) {
8861 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8862 &secinfo->flavor_info);
8865 flavor = RPC_AUTH_MAXFLAVOR;
8869 if (!nfs_auth_info_match(&server->auth_info, flavor))
8870 flavor = RPC_AUTH_MAXFLAVOR;
8872 if (flavor != RPC_AUTH_MAXFLAVOR) {
8873 err = nfs4_lookup_root_sec(server, fhandle,
8880 if (flavor == RPC_AUTH_MAXFLAVOR)
8891 static int _nfs41_test_stateid(struct nfs_server *server,
8892 nfs4_stateid *stateid,
8893 struct rpc_cred *cred)
8896 struct nfs41_test_stateid_args args = {
8899 struct nfs41_test_stateid_res res;
8900 struct rpc_message msg = {
8901 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8906 struct rpc_clnt *rpc_client = server->client;
8908 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8911 dprintk("NFS call test_stateid %p\n", stateid);
8912 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
8913 nfs4_set_sequence_privileged(&args.seq_args);
8914 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
8915 &args.seq_args, &res.seq_res);
8916 if (status != NFS_OK) {
8917 dprintk("NFS reply test_stateid: failed, %d\n", status);
8920 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
8924 static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
8925 int err, struct nfs4_exception *exception)
8927 exception->retry = 0;
8929 case -NFS4ERR_DELAY:
8930 nfs4_handle_exception(server, err, exception);
8932 case -NFS4ERR_BADSESSION:
8933 case -NFS4ERR_BADSLOT:
8934 case -NFS4ERR_BAD_HIGH_SLOT:
8935 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8936 case -NFS4ERR_DEADSESSION:
8937 nfs4_do_handle_exception(server, err, exception);
8942 * nfs41_test_stateid - perform a TEST_STATEID operation
8944 * @server: server / transport on which to perform the operation
8945 * @stateid: state ID to test
8948 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8949 * Otherwise a negative NFS4ERR value is returned if the operation
8950 * failed or the state ID is not currently valid.
8952 static int nfs41_test_stateid(struct nfs_server *server,
8953 nfs4_stateid *stateid,
8954 struct rpc_cred *cred)
8956 struct nfs4_exception exception = { };
8959 err = _nfs41_test_stateid(server, stateid, cred);
8960 nfs4_handle_delay_or_session_error(server, err, &exception);
8961 } while (exception.retry);
8965 struct nfs_free_stateid_data {
8966 struct nfs_server *server;
8967 struct nfs41_free_stateid_args args;
8968 struct nfs41_free_stateid_res res;
8971 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
8973 struct nfs_free_stateid_data *data = calldata;
8974 nfs41_setup_sequence(nfs4_get_session(data->server),
8975 &data->args.seq_args,
8980 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
8982 struct nfs_free_stateid_data *data = calldata;
8984 nfs41_sequence_done(task, &data->res.seq_res);
8986 switch (task->tk_status) {
8987 case -NFS4ERR_DELAY:
8988 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
8989 rpc_restart_call_prepare(task);
8993 static void nfs41_free_stateid_release(void *calldata)
8998 static const struct rpc_call_ops nfs41_free_stateid_ops = {
8999 .rpc_call_prepare = nfs41_free_stateid_prepare,
9000 .rpc_call_done = nfs41_free_stateid_done,
9001 .rpc_release = nfs41_free_stateid_release,
9004 static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
9005 const nfs4_stateid *stateid,
9006 struct rpc_cred *cred,
9009 struct rpc_message msg = {
9010 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
9013 struct rpc_task_setup task_setup = {
9014 .rpc_client = server->client,
9015 .rpc_message = &msg,
9016 .callback_ops = &nfs41_free_stateid_ops,
9017 .flags = RPC_TASK_ASYNC,
9019 struct nfs_free_stateid_data *data;
9021 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9022 &task_setup.rpc_client, &msg);
9024 dprintk("NFS call free_stateid %p\n", stateid);
9025 data = kmalloc(sizeof(*data), GFP_NOFS);
9027 return ERR_PTR(-ENOMEM);
9028 data->server = server;
9029 nfs4_stateid_copy(&data->args.stateid, stateid);
9031 task_setup.callback_data = data;
9033 msg.rpc_argp = &data->args;
9034 msg.rpc_resp = &data->res;
9035 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
9037 nfs4_set_sequence_privileged(&data->args.seq_args);
9039 return rpc_run_task(&task_setup);
9043 * nfs41_free_stateid - perform a FREE_STATEID operation
9045 * @server: server / transport on which to perform the operation
9046 * @stateid: state ID to release
9048 * @is_recovery: set to true if this call needs to be privileged
9050 * Note: this function is always asynchronous.
9052 static int nfs41_free_stateid(struct nfs_server *server,
9053 const nfs4_stateid *stateid,
9054 struct rpc_cred *cred,
9057 struct rpc_task *task;
9059 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
9061 return PTR_ERR(task);
9067 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
9069 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
9071 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
9072 nfs4_free_lock_state(server, lsp);
9075 static bool nfs41_match_stateid(const nfs4_stateid *s1,
9076 const nfs4_stateid *s2)
9078 if (s1->type != s2->type)
9081 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
9084 if (s1->seqid == s2->seqid)
9086 if (s1->seqid == 0 || s2->seqid == 0)
9092 #endif /* CONFIG_NFS_V4_1 */
9094 static bool nfs4_match_stateid(const nfs4_stateid *s1,
9095 const nfs4_stateid *s2)
9097 return nfs4_stateid_match(s1, s2);
9101 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
9102 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9103 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9104 .recover_open = nfs4_open_reclaim,
9105 .recover_lock = nfs4_lock_reclaim,
9106 .establish_clid = nfs4_init_clientid,
9107 .detect_trunking = nfs40_discover_server_trunking,
9110 #if defined(CONFIG_NFS_V4_1)
9111 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
9112 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9113 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9114 .recover_open = nfs4_open_reclaim,
9115 .recover_lock = nfs4_lock_reclaim,
9116 .establish_clid = nfs41_init_clientid,
9117 .reclaim_complete = nfs41_proc_reclaim_complete,
9118 .detect_trunking = nfs41_discover_server_trunking,
9120 #endif /* CONFIG_NFS_V4_1 */
9122 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
9123 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9124 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
9125 .recover_open = nfs40_open_expired,
9126 .recover_lock = nfs4_lock_expired,
9127 .establish_clid = nfs4_init_clientid,
9130 #if defined(CONFIG_NFS_V4_1)
9131 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
9132 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9133 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
9134 .recover_open = nfs41_open_expired,
9135 .recover_lock = nfs41_lock_expired,
9136 .establish_clid = nfs41_init_clientid,
9138 #endif /* CONFIG_NFS_V4_1 */
9140 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
9141 .sched_state_renewal = nfs4_proc_async_renew,
9142 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
9143 .renew_lease = nfs4_proc_renew,
9146 #if defined(CONFIG_NFS_V4_1)
9147 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
9148 .sched_state_renewal = nfs41_proc_async_sequence,
9149 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
9150 .renew_lease = nfs4_proc_sequence,
9154 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
9155 .get_locations = _nfs40_proc_get_locations,
9156 .fsid_present = _nfs40_proc_fsid_present,
9159 #if defined(CONFIG_NFS_V4_1)
9160 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
9161 .get_locations = _nfs41_proc_get_locations,
9162 .fsid_present = _nfs41_proc_fsid_present,
9164 #endif /* CONFIG_NFS_V4_1 */
9166 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9168 .init_caps = NFS_CAP_READDIRPLUS
9169 | NFS_CAP_ATOMIC_OPEN
9170 | NFS_CAP_POSIX_LOCK,
9171 .init_client = nfs40_init_client,
9172 .shutdown_client = nfs40_shutdown_client,
9173 .match_stateid = nfs4_match_stateid,
9174 .find_root_sec = nfs4_find_root_sec,
9175 .free_lock_state = nfs4_release_lockowner,
9176 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
9177 .alloc_seqid = nfs_alloc_seqid,
9178 .call_sync_ops = &nfs40_call_sync_ops,
9179 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9180 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9181 .state_renewal_ops = &nfs40_state_renewal_ops,
9182 .mig_recovery_ops = &nfs40_mig_recovery_ops,
9185 #if defined(CONFIG_NFS_V4_1)
9186 static struct nfs_seqid *
9187 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9192 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9194 .init_caps = NFS_CAP_READDIRPLUS
9195 | NFS_CAP_ATOMIC_OPEN
9196 | NFS_CAP_POSIX_LOCK
9197 | NFS_CAP_STATEID_NFSV41
9198 | NFS_CAP_ATOMIC_OPEN_V1,
9199 .init_client = nfs41_init_client,
9200 .shutdown_client = nfs41_shutdown_client,
9201 .match_stateid = nfs41_match_stateid,
9202 .find_root_sec = nfs41_find_root_sec,
9203 .free_lock_state = nfs41_free_lock_state,
9204 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
9205 .alloc_seqid = nfs_alloc_no_seqid,
9206 .session_trunk = nfs4_test_session_trunk,
9207 .call_sync_ops = &nfs41_call_sync_ops,
9208 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9209 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9210 .state_renewal_ops = &nfs41_state_renewal_ops,
9211 .mig_recovery_ops = &nfs41_mig_recovery_ops,
9215 #if defined(CONFIG_NFS_V4_2)
9216 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9218 .init_caps = NFS_CAP_READDIRPLUS
9219 | NFS_CAP_ATOMIC_OPEN
9220 | NFS_CAP_POSIX_LOCK
9221 | NFS_CAP_STATEID_NFSV41
9222 | NFS_CAP_ATOMIC_OPEN_V1
9225 | NFS_CAP_DEALLOCATE
9227 | NFS_CAP_LAYOUTSTATS
9229 .init_client = nfs41_init_client,
9230 .shutdown_client = nfs41_shutdown_client,
9231 .match_stateid = nfs41_match_stateid,
9232 .find_root_sec = nfs41_find_root_sec,
9233 .free_lock_state = nfs41_free_lock_state,
9234 .call_sync_ops = &nfs41_call_sync_ops,
9235 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
9236 .alloc_seqid = nfs_alloc_no_seqid,
9237 .session_trunk = nfs4_test_session_trunk,
9238 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9239 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9240 .state_renewal_ops = &nfs41_state_renewal_ops,
9241 .mig_recovery_ops = &nfs41_mig_recovery_ops,
9245 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9246 [0] = &nfs_v4_0_minor_ops,
9247 #if defined(CONFIG_NFS_V4_1)
9248 [1] = &nfs_v4_1_minor_ops,
9250 #if defined(CONFIG_NFS_V4_2)
9251 [2] = &nfs_v4_2_minor_ops,
9255 static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
9257 ssize_t error, error2;
9259 error = generic_listxattr(dentry, list, size);
9267 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9270 return error + error2;
9273 static const struct inode_operations nfs4_dir_inode_operations = {
9274 .create = nfs_create,
9275 .lookup = nfs_lookup,
9276 .atomic_open = nfs_atomic_open,
9278 .unlink = nfs_unlink,
9279 .symlink = nfs_symlink,
9283 .rename = nfs_rename,
9284 .permission = nfs_permission,
9285 .getattr = nfs_getattr,
9286 .setattr = nfs_setattr,
9287 .getxattr = generic_getxattr,
9288 .setxattr = generic_setxattr,
9289 .listxattr = nfs4_listxattr,
9290 .removexattr = generic_removexattr,
9293 static const struct inode_operations nfs4_file_inode_operations = {
9294 .permission = nfs_permission,
9295 .getattr = nfs_getattr,
9296 .setattr = nfs_setattr,
9297 .getxattr = generic_getxattr,
9298 .setxattr = generic_setxattr,
9299 .listxattr = nfs4_listxattr,
9300 .removexattr = generic_removexattr,
9303 const struct nfs_rpc_ops nfs_v4_clientops = {
9304 .version = 4, /* protocol version */
9305 .dentry_ops = &nfs4_dentry_operations,
9306 .dir_inode_ops = &nfs4_dir_inode_operations,
9307 .file_inode_ops = &nfs4_file_inode_operations,
9308 .file_ops = &nfs4_file_operations,
9309 .getroot = nfs4_proc_get_root,
9310 .submount = nfs4_submount,
9311 .try_mount = nfs4_try_mount,
9312 .getattr = nfs4_proc_getattr,
9313 .setattr = nfs4_proc_setattr,
9314 .lookup = nfs4_proc_lookup,
9315 .access = nfs4_proc_access,
9316 .readlink = nfs4_proc_readlink,
9317 .create = nfs4_proc_create,
9318 .remove = nfs4_proc_remove,
9319 .unlink_setup = nfs4_proc_unlink_setup,
9320 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
9321 .unlink_done = nfs4_proc_unlink_done,
9322 .rename_setup = nfs4_proc_rename_setup,
9323 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
9324 .rename_done = nfs4_proc_rename_done,
9325 .link = nfs4_proc_link,
9326 .symlink = nfs4_proc_symlink,
9327 .mkdir = nfs4_proc_mkdir,
9328 .rmdir = nfs4_proc_remove,
9329 .readdir = nfs4_proc_readdir,
9330 .mknod = nfs4_proc_mknod,
9331 .statfs = nfs4_proc_statfs,
9332 .fsinfo = nfs4_proc_fsinfo,
9333 .pathconf = nfs4_proc_pathconf,
9334 .set_capabilities = nfs4_server_capabilities,
9335 .decode_dirent = nfs4_decode_dirent,
9336 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
9337 .read_setup = nfs4_proc_read_setup,
9338 .read_done = nfs4_read_done,
9339 .write_setup = nfs4_proc_write_setup,
9340 .write_done = nfs4_write_done,
9341 .commit_setup = nfs4_proc_commit_setup,
9342 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
9343 .commit_done = nfs4_commit_done,
9344 .lock = nfs4_proc_lock,
9345 .clear_acl_cache = nfs4_zap_acl_attr,
9346 .close_context = nfs4_close_context,
9347 .open_context = nfs4_atomic_open,
9348 .have_delegation = nfs4_have_delegation,
9349 .return_delegation = nfs4_inode_return_delegation,
9350 .alloc_client = nfs4_alloc_client,
9351 .init_client = nfs4_init_client,
9352 .free_client = nfs4_free_client,
9353 .create_server = nfs4_create_server,
9354 .clone_server = nfs_clone_server,
9357 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
9358 .name = XATTR_NAME_NFSV4_ACL,
9359 .list = nfs4_xattr_list_nfs4_acl,
9360 .get = nfs4_xattr_get_nfs4_acl,
9361 .set = nfs4_xattr_set_nfs4_acl,
9364 const struct xattr_handler *nfs4_xattr_handlers[] = {
9365 &nfs4_xattr_nfs4_acl_handler,
9366 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
9367 &nfs4_xattr_nfs4_label_handler,