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 *, nfs4_stateid *,
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 long nfs4_update_delay(long *timeout)
335 return NFS4_POLL_RETRY_MAX;
337 *timeout = NFS4_POLL_RETRY_MIN;
338 if (*timeout > NFS4_POLL_RETRY_MAX)
339 *timeout = NFS4_POLL_RETRY_MAX;
345 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
351 freezable_schedule_timeout_killable_unsafe(
352 nfs4_update_delay(timeout));
353 if (fatal_signal_pending(current))
358 /* This is the error handling routine for processes that are allowed
361 static int nfs4_do_handle_exception(struct nfs_server *server,
362 int errorcode, struct nfs4_exception *exception)
364 struct nfs_client *clp = server->nfs_client;
365 struct nfs4_state *state = exception->state;
366 struct inode *inode = exception->inode;
369 exception->delay = 0;
370 exception->recovering = 0;
371 exception->retry = 0;
375 case -NFS4ERR_OPENMODE:
376 case -NFS4ERR_DELEG_REVOKED:
377 case -NFS4ERR_ADMIN_REVOKED:
378 case -NFS4ERR_BAD_STATEID:
379 if (inode && nfs_async_inode_return_delegation(inode,
381 goto wait_on_recovery;
384 ret = nfs4_schedule_stateid_recovery(server, state);
387 goto wait_on_recovery;
388 case -NFS4ERR_EXPIRED:
390 ret = nfs4_schedule_stateid_recovery(server, state);
394 case -NFS4ERR_STALE_STATEID:
395 case -NFS4ERR_STALE_CLIENTID:
396 nfs4_schedule_lease_recovery(clp);
397 goto wait_on_recovery;
399 ret = nfs4_schedule_migration_recovery(server);
402 goto wait_on_recovery;
403 case -NFS4ERR_LEASE_MOVED:
404 nfs4_schedule_lease_moved_recovery(clp);
405 goto wait_on_recovery;
406 #if defined(CONFIG_NFS_V4_1)
407 case -NFS4ERR_BADSESSION:
408 case -NFS4ERR_BADSLOT:
409 case -NFS4ERR_BAD_HIGH_SLOT:
410 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
411 case -NFS4ERR_DEADSESSION:
412 case -NFS4ERR_SEQ_FALSE_RETRY:
413 case -NFS4ERR_SEQ_MISORDERED:
414 dprintk("%s ERROR: %d Reset session\n", __func__,
416 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
417 goto wait_on_recovery;
418 #endif /* defined(CONFIG_NFS_V4_1) */
419 case -NFS4ERR_FILE_OPEN:
420 if (exception->timeout > HZ) {
421 /* We have retried a decent amount, time to
428 nfs_inc_server_stats(server, NFSIOS_DELAY);
430 exception->delay = 1;
433 case -NFS4ERR_RETRY_UNCACHED_REP:
434 case -NFS4ERR_OLD_STATEID:
435 exception->retry = 1;
437 case -NFS4ERR_BADOWNER:
438 /* The following works around a Linux server bug! */
439 case -NFS4ERR_BADNAME:
440 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
441 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
442 exception->retry = 1;
443 printk(KERN_WARNING "NFS: v4 server %s "
444 "does not accept raw "
446 "Reenabling the idmapper.\n",
447 server->nfs_client->cl_hostname);
450 /* We failed to handle the error */
451 return nfs4_map_errors(ret);
453 exception->recovering = 1;
457 /* This is the error handling routine for processes that are allowed
460 int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
462 struct nfs_client *clp = server->nfs_client;
465 ret = nfs4_do_handle_exception(server, errorcode, exception);
466 if (exception->delay) {
467 ret = nfs4_delay(server->client, &exception->timeout);
470 if (exception->recovering) {
471 ret = nfs4_wait_clnt_recover(clp);
472 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
479 exception->retry = 1;
484 nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
485 int errorcode, struct nfs4_exception *exception)
487 struct nfs_client *clp = server->nfs_client;
490 ret = nfs4_do_handle_exception(server, errorcode, exception);
491 if (exception->delay) {
492 rpc_delay(task, nfs4_update_delay(&exception->timeout));
495 if (exception->recovering) {
496 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
497 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
498 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
501 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
506 exception->retry = 1;
511 nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
512 struct nfs4_state *state, long *timeout)
514 struct nfs4_exception exception = {
518 if (task->tk_status >= 0)
521 exception.timeout = *timeout;
522 task->tk_status = nfs4_async_handle_exception(task, server,
525 if (exception.delay && timeout)
526 *timeout = exception.timeout;
533 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
534 * or 'false' otherwise.
536 static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
538 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
540 if (flavor == RPC_AUTH_GSS_KRB5I ||
541 flavor == RPC_AUTH_GSS_KRB5P)
547 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
549 spin_lock(&clp->cl_lock);
550 if (time_before(clp->cl_last_renewal,timestamp))
551 clp->cl_last_renewal = timestamp;
552 spin_unlock(&clp->cl_lock);
555 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
557 struct nfs_client *clp = server->nfs_client;
559 if (!nfs4_has_session(clp))
560 do_renew_lease(clp, timestamp);
563 struct nfs4_call_sync_data {
564 const struct nfs_server *seq_server;
565 struct nfs4_sequence_args *seq_args;
566 struct nfs4_sequence_res *seq_res;
569 void nfs4_init_sequence(struct nfs4_sequence_args *args,
570 struct nfs4_sequence_res *res, int cache_reply)
572 args->sa_slot = NULL;
573 args->sa_cache_this = cache_reply;
574 args->sa_privileged = 0;
579 static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
581 args->sa_privileged = 1;
584 int nfs40_setup_sequence(struct nfs4_slot_table *tbl,
585 struct nfs4_sequence_args *args,
586 struct nfs4_sequence_res *res,
587 struct rpc_task *task)
589 struct nfs4_slot *slot;
591 /* slot already allocated? */
592 if (res->sr_slot != NULL)
595 spin_lock(&tbl->slot_tbl_lock);
596 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
599 slot = nfs4_alloc_slot(tbl);
601 if (slot == ERR_PTR(-ENOMEM))
602 task->tk_timeout = HZ >> 2;
605 spin_unlock(&tbl->slot_tbl_lock);
607 args->sa_slot = slot;
611 rpc_call_start(task);
615 if (args->sa_privileged)
616 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
617 NULL, RPC_PRIORITY_PRIVILEGED);
619 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
620 spin_unlock(&tbl->slot_tbl_lock);
623 EXPORT_SYMBOL_GPL(nfs40_setup_sequence);
625 static int nfs40_sequence_done(struct rpc_task *task,
626 struct nfs4_sequence_res *res)
628 struct nfs4_slot *slot = res->sr_slot;
629 struct nfs4_slot_table *tbl;
635 spin_lock(&tbl->slot_tbl_lock);
636 if (!nfs41_wake_and_assign_slot(tbl, slot))
637 nfs4_free_slot(tbl, slot);
638 spin_unlock(&tbl->slot_tbl_lock);
645 #if defined(CONFIG_NFS_V4_1)
647 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
649 struct nfs4_session *session;
650 struct nfs4_slot_table *tbl;
651 struct nfs4_slot *slot = res->sr_slot;
652 bool send_new_highest_used_slotid = false;
655 session = tbl->session;
657 spin_lock(&tbl->slot_tbl_lock);
658 /* Be nice to the server: try to ensure that the last transmitted
659 * value for highest_user_slotid <= target_highest_slotid
661 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
662 send_new_highest_used_slotid = true;
664 if (nfs41_wake_and_assign_slot(tbl, slot)) {
665 send_new_highest_used_slotid = false;
668 nfs4_free_slot(tbl, slot);
670 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
671 send_new_highest_used_slotid = false;
673 spin_unlock(&tbl->slot_tbl_lock);
675 if (send_new_highest_used_slotid)
676 nfs41_notify_server(session->clp);
679 int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
681 struct nfs4_session *session;
682 struct nfs4_slot *slot = res->sr_slot;
683 struct nfs_client *clp;
684 bool interrupted = false;
689 /* don't increment the sequence number if the task wasn't sent */
690 if (!RPC_WAS_SENT(task))
693 session = slot->table->session;
695 if (slot->interrupted) {
696 slot->interrupted = 0;
700 trace_nfs4_sequence_done(session, res);
701 /* Check the SEQUENCE operation status */
702 switch (res->sr_status) {
704 /* Update the slot's sequence and clientid lease timer */
707 do_renew_lease(clp, res->sr_timestamp);
708 /* Check sequence flags */
709 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
710 nfs41_update_target_slotid(slot->table, slot, res);
714 * sr_status remains 1 if an RPC level error occurred.
715 * The server may or may not have processed the sequence
717 * Mark the slot as having hosted an interrupted RPC call.
719 slot->interrupted = 1;
722 /* The server detected a resend of the RPC call and
723 * returned NFS4ERR_DELAY as per Section 2.10.6.2
726 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
731 case -NFS4ERR_BADSLOT:
733 * The slot id we used was probably retired. Try again
734 * using a different slot id.
737 case -NFS4ERR_SEQ_MISORDERED:
739 * Was the last operation on this sequence interrupted?
740 * If so, retry after bumping the sequence number.
747 * Could this slot have been previously retired?
748 * If so, then the server may be expecting seq_nr = 1!
750 if (slot->seq_nr != 1) {
755 case -NFS4ERR_SEQ_FALSE_RETRY:
759 /* Just update the slot sequence no. */
763 /* The session may be reset by one of the error handlers. */
764 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
765 nfs41_sequence_free_slot(res);
769 if (rpc_restart_call_prepare(task)) {
775 if (!rpc_restart_call(task))
777 rpc_delay(task, NFS4_POLL_RETRY_MAX);
780 EXPORT_SYMBOL_GPL(nfs41_sequence_done);
782 int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
784 if (res->sr_slot == NULL)
786 if (!res->sr_slot->table->session)
787 return nfs40_sequence_done(task, res);
788 return nfs41_sequence_done(task, res);
790 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
792 int nfs41_setup_sequence(struct nfs4_session *session,
793 struct nfs4_sequence_args *args,
794 struct nfs4_sequence_res *res,
795 struct rpc_task *task)
797 struct nfs4_slot *slot;
798 struct nfs4_slot_table *tbl;
800 dprintk("--> %s\n", __func__);
801 /* slot already allocated? */
802 if (res->sr_slot != NULL)
805 tbl = &session->fc_slot_table;
807 task->tk_timeout = 0;
809 spin_lock(&tbl->slot_tbl_lock);
810 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
811 !args->sa_privileged) {
812 /* The state manager will wait until the slot table is empty */
813 dprintk("%s session is draining\n", __func__);
817 slot = nfs4_alloc_slot(tbl);
819 /* If out of memory, try again in 1/4 second */
820 if (slot == ERR_PTR(-ENOMEM))
821 task->tk_timeout = HZ >> 2;
822 dprintk("<-- %s: no free slots\n", __func__);
825 spin_unlock(&tbl->slot_tbl_lock);
827 args->sa_slot = slot;
829 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
830 slot->slot_nr, slot->seq_nr);
833 res->sr_timestamp = jiffies;
834 res->sr_status_flags = 0;
836 * sr_status is only set in decode_sequence, and so will remain
837 * set to 1 if an rpc level failure occurs.
840 trace_nfs4_setup_sequence(session, args);
842 rpc_call_start(task);
845 /* Privileged tasks are queued with top priority */
846 if (args->sa_privileged)
847 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
848 NULL, RPC_PRIORITY_PRIVILEGED);
850 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
851 spin_unlock(&tbl->slot_tbl_lock);
854 EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
856 static int nfs4_setup_sequence(const struct nfs_server *server,
857 struct nfs4_sequence_args *args,
858 struct nfs4_sequence_res *res,
859 struct rpc_task *task)
861 struct nfs4_session *session = nfs4_get_session(server);
865 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
868 dprintk("--> %s clp %p session %p sr_slot %u\n",
869 __func__, session->clp, session, res->sr_slot ?
870 res->sr_slot->slot_nr : NFS4_NO_SLOT);
872 ret = nfs41_setup_sequence(session, args, res, task);
874 dprintk("<-- %s status=%d\n", __func__, ret);
878 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
880 struct nfs4_call_sync_data *data = calldata;
881 struct nfs4_session *session = nfs4_get_session(data->seq_server);
883 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
885 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
888 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
890 struct nfs4_call_sync_data *data = calldata;
892 nfs41_sequence_done(task, data->seq_res);
895 static const struct rpc_call_ops nfs41_call_sync_ops = {
896 .rpc_call_prepare = nfs41_call_sync_prepare,
897 .rpc_call_done = nfs41_call_sync_done,
900 #else /* !CONFIG_NFS_V4_1 */
902 static int nfs4_setup_sequence(const struct nfs_server *server,
903 struct nfs4_sequence_args *args,
904 struct nfs4_sequence_res *res,
905 struct rpc_task *task)
907 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
911 int nfs4_sequence_done(struct rpc_task *task,
912 struct nfs4_sequence_res *res)
914 return nfs40_sequence_done(task, res);
916 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
918 #endif /* !CONFIG_NFS_V4_1 */
920 static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
922 struct nfs4_call_sync_data *data = calldata;
923 nfs4_setup_sequence(data->seq_server,
924 data->seq_args, data->seq_res, task);
927 static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
929 struct nfs4_call_sync_data *data = calldata;
930 nfs4_sequence_done(task, data->seq_res);
933 static const struct rpc_call_ops nfs40_call_sync_ops = {
934 .rpc_call_prepare = nfs40_call_sync_prepare,
935 .rpc_call_done = nfs40_call_sync_done,
938 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
939 struct nfs_server *server,
940 struct rpc_message *msg,
941 struct nfs4_sequence_args *args,
942 struct nfs4_sequence_res *res)
945 struct rpc_task *task;
946 struct nfs_client *clp = server->nfs_client;
947 struct nfs4_call_sync_data data = {
948 .seq_server = server,
952 struct rpc_task_setup task_setup = {
955 .callback_ops = clp->cl_mvops->call_sync_ops,
956 .callback_data = &data
959 task = rpc_run_task(&task_setup);
963 ret = task->tk_status;
969 int nfs4_call_sync(struct rpc_clnt *clnt,
970 struct nfs_server *server,
971 struct rpc_message *msg,
972 struct nfs4_sequence_args *args,
973 struct nfs4_sequence_res *res,
976 nfs4_init_sequence(args, res, cache_reply);
977 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
980 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
982 struct nfs_inode *nfsi = NFS_I(dir);
984 spin_lock(&dir->i_lock);
985 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
986 if (!cinfo->atomic || cinfo->before != dir->i_version)
987 nfs_force_lookup_revalidate(dir);
988 dir->i_version = cinfo->after;
989 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
990 nfs_fscache_invalidate(dir);
991 spin_unlock(&dir->i_lock);
994 struct nfs4_opendata {
996 struct nfs_openargs o_arg;
997 struct nfs_openres o_res;
998 struct nfs_open_confirmargs c_arg;
999 struct nfs_open_confirmres c_res;
1000 struct nfs4_string owner_name;
1001 struct nfs4_string group_name;
1002 struct nfs4_label *a_label;
1003 struct nfs_fattr f_attr;
1004 struct nfs4_label *f_label;
1006 struct dentry *dentry;
1007 struct nfs4_state_owner *owner;
1008 struct nfs4_state *state;
1010 unsigned long timestamp;
1011 unsigned int rpc_done : 1;
1012 unsigned int file_created : 1;
1013 unsigned int is_recover : 1;
1018 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1019 int err, struct nfs4_exception *exception)
1023 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1025 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1026 exception->retry = 1;
1031 nfs4_map_atomic_open_share(struct nfs_server *server,
1032 fmode_t fmode, int openflags)
1036 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1038 res = NFS4_SHARE_ACCESS_READ;
1041 res = NFS4_SHARE_ACCESS_WRITE;
1043 case FMODE_READ|FMODE_WRITE:
1044 res = NFS4_SHARE_ACCESS_BOTH;
1046 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1048 /* Want no delegation if we're using O_DIRECT */
1049 if (openflags & O_DIRECT)
1050 res |= NFS4_SHARE_WANT_NO_DELEG;
1055 static enum open_claim_type4
1056 nfs4_map_atomic_open_claim(struct nfs_server *server,
1057 enum open_claim_type4 claim)
1059 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1064 case NFS4_OPEN_CLAIM_FH:
1065 return NFS4_OPEN_CLAIM_NULL;
1066 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1067 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1068 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1069 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1073 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
1075 p->o_res.f_attr = &p->f_attr;
1076 p->o_res.f_label = p->f_label;
1077 p->o_res.seqid = p->o_arg.seqid;
1078 p->c_res.seqid = p->c_arg.seqid;
1079 p->o_res.server = p->o_arg.server;
1080 p->o_res.access_request = p->o_arg.access;
1081 nfs_fattr_init(&p->f_attr);
1082 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
1085 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
1086 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
1087 const struct iattr *attrs,
1088 struct nfs4_label *label,
1089 enum open_claim_type4 claim,
1092 struct dentry *parent = dget_parent(dentry);
1093 struct inode *dir = d_inode(parent);
1094 struct nfs_server *server = NFS_SERVER(dir);
1095 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
1096 struct nfs4_opendata *p;
1098 p = kzalloc(sizeof(*p), gfp_mask);
1102 p->f_label = nfs4_label_alloc(server, gfp_mask);
1103 if (IS_ERR(p->f_label))
1106 p->a_label = nfs4_label_alloc(server, gfp_mask);
1107 if (IS_ERR(p->a_label))
1110 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1111 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
1112 if (IS_ERR(p->o_arg.seqid))
1113 goto err_free_label;
1114 nfs_sb_active(dentry->d_sb);
1115 p->dentry = dget(dentry);
1118 atomic_inc(&sp->so_count);
1119 p->o_arg.open_flags = flags;
1120 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
1121 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1123 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1124 * will return permission denied for all bits until close */
1125 if (!(flags & O_EXCL)) {
1126 /* ask server to check for all possible rights as results
1128 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1129 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
1131 p->o_arg.clientid = server->nfs_client->cl_clientid;
1132 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1133 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1134 p->o_arg.name = &dentry->d_name;
1135 p->o_arg.server = server;
1136 p->o_arg.bitmask = nfs4_bitmask(server, label);
1137 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1138 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1139 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1140 switch (p->o_arg.claim) {
1141 case NFS4_OPEN_CLAIM_NULL:
1142 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1143 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1144 p->o_arg.fh = NFS_FH(dir);
1146 case NFS4_OPEN_CLAIM_PREVIOUS:
1147 case NFS4_OPEN_CLAIM_FH:
1148 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1149 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1150 p->o_arg.fh = NFS_FH(d_inode(dentry));
1152 if (attrs != NULL && attrs->ia_valid != 0) {
1155 p->o_arg.u.attrs = &p->attrs;
1156 memcpy(&p->attrs, attrs, sizeof(p->attrs));
1159 verf[1] = current->pid;
1160 memcpy(p->o_arg.u.verifier.data, verf,
1161 sizeof(p->o_arg.u.verifier.data));
1163 p->c_arg.fh = &p->o_res.fh;
1164 p->c_arg.stateid = &p->o_res.stateid;
1165 p->c_arg.seqid = p->o_arg.seqid;
1166 nfs4_init_opendata_res(p);
1167 kref_init(&p->kref);
1171 nfs4_label_free(p->a_label);
1173 nfs4_label_free(p->f_label);
1181 static void nfs4_opendata_free(struct kref *kref)
1183 struct nfs4_opendata *p = container_of(kref,
1184 struct nfs4_opendata, kref);
1185 struct super_block *sb = p->dentry->d_sb;
1187 nfs_free_seqid(p->o_arg.seqid);
1188 if (p->state != NULL)
1189 nfs4_put_open_state(p->state);
1190 nfs4_put_state_owner(p->owner);
1192 nfs4_label_free(p->a_label);
1193 nfs4_label_free(p->f_label);
1197 nfs_sb_deactive(sb);
1198 nfs_fattr_free_names(&p->f_attr);
1199 kfree(p->f_attr.mdsthreshold);
1203 static void nfs4_opendata_put(struct nfs4_opendata *p)
1206 kref_put(&p->kref, nfs4_opendata_free);
1209 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1213 ret = rpc_wait_for_completion_task(task);
1217 static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1220 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1221 case FMODE_READ|FMODE_WRITE:
1222 return state->n_rdwr != 0;
1224 return state->n_wronly != 0;
1226 return state->n_rdonly != 0;
1232 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
1236 if (open_mode & (O_EXCL|O_TRUNC))
1238 switch (mode & (FMODE_READ|FMODE_WRITE)) {
1240 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1241 && state->n_rdonly != 0;
1244 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1245 && state->n_wronly != 0;
1247 case FMODE_READ|FMODE_WRITE:
1248 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1249 && state->n_rdwr != 0;
1255 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1256 enum open_claim_type4 claim)
1258 if (delegation == NULL)
1260 if ((delegation->type & fmode) != fmode)
1262 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1265 case NFS4_OPEN_CLAIM_NULL:
1266 case NFS4_OPEN_CLAIM_FH:
1268 case NFS4_OPEN_CLAIM_PREVIOUS:
1269 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1274 nfs_mark_delegation_referenced(delegation);
1278 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1287 case FMODE_READ|FMODE_WRITE:
1290 nfs4_state_set_mode_locked(state, state->state | fmode);
1293 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
1295 struct nfs_client *clp = state->owner->so_server->nfs_client;
1296 bool need_recover = false;
1298 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1299 need_recover = true;
1300 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1301 need_recover = true;
1302 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1303 need_recover = true;
1305 nfs4_state_mark_reclaim_nograce(clp, state);
1308 static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1309 nfs4_stateid *stateid)
1311 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1313 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1314 nfs_test_and_clear_all_open_stateid(state);
1317 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1322 static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1324 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1326 if (state->n_wronly)
1327 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1328 if (state->n_rdonly)
1329 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1331 set_bit(NFS_O_RDWR_STATE, &state->flags);
1332 set_bit(NFS_OPEN_STATE, &state->flags);
1335 static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1336 nfs4_stateid *arg_stateid,
1337 nfs4_stateid *stateid, fmode_t fmode)
1339 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1340 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1342 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1345 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1348 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1349 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1350 clear_bit(NFS_OPEN_STATE, &state->flags);
1352 if (stateid == NULL)
1354 /* Handle races with OPEN */
1355 if (!nfs4_stateid_match_other(arg_stateid, &state->open_stateid) ||
1356 (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1357 !nfs4_stateid_is_newer(stateid, &state->open_stateid))) {
1358 nfs_resync_open_stateid_locked(state);
1361 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1362 nfs4_stateid_copy(&state->stateid, stateid);
1363 nfs4_stateid_copy(&state->open_stateid, stateid);
1366 static void nfs_clear_open_stateid(struct nfs4_state *state,
1367 nfs4_stateid *arg_stateid,
1368 nfs4_stateid *stateid, fmode_t fmode)
1370 write_seqlock(&state->seqlock);
1371 nfs_clear_open_stateid_locked(state, arg_stateid, stateid, fmode);
1372 write_sequnlock(&state->seqlock);
1373 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1374 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1377 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1381 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1384 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1386 case FMODE_READ|FMODE_WRITE:
1387 set_bit(NFS_O_RDWR_STATE, &state->flags);
1389 if (!nfs_need_update_open_stateid(state, stateid))
1391 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1392 nfs4_stateid_copy(&state->stateid, stateid);
1393 nfs4_stateid_copy(&state->open_stateid, stateid);
1396 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
1399 * Protect the call to nfs4_state_set_mode_locked and
1400 * serialise the stateid update
1402 spin_lock(&state->owner->so_lock);
1403 write_seqlock(&state->seqlock);
1404 if (deleg_stateid != NULL) {
1405 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1406 set_bit(NFS_DELEGATED_STATE, &state->flags);
1408 if (open_stateid != NULL)
1409 nfs_set_open_stateid_locked(state, open_stateid, fmode);
1410 write_sequnlock(&state->seqlock);
1411 update_open_stateflags(state, fmode);
1412 spin_unlock(&state->owner->so_lock);
1415 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
1417 struct nfs_inode *nfsi = NFS_I(state->inode);
1418 struct nfs_delegation *deleg_cur;
1421 fmode &= (FMODE_READ|FMODE_WRITE);
1424 deleg_cur = rcu_dereference(nfsi->delegation);
1425 if (deleg_cur == NULL)
1428 spin_lock(&deleg_cur->lock);
1429 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1430 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1431 (deleg_cur->type & fmode) != fmode)
1432 goto no_delegation_unlock;
1434 if (delegation == NULL)
1435 delegation = &deleg_cur->stateid;
1436 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
1437 goto no_delegation_unlock;
1439 nfs_mark_delegation_referenced(deleg_cur);
1440 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
1442 no_delegation_unlock:
1443 spin_unlock(&deleg_cur->lock);
1447 if (!ret && open_stateid != NULL) {
1448 __update_open_stateid(state, open_stateid, NULL, fmode);
1451 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1452 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1457 static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1458 const nfs4_stateid *stateid)
1460 struct nfs4_state *state = lsp->ls_state;
1463 spin_lock(&state->state_lock);
1464 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1466 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1468 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1471 spin_unlock(&state->state_lock);
1475 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1477 struct nfs_delegation *delegation;
1480 delegation = rcu_dereference(NFS_I(inode)->delegation);
1481 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1486 nfs4_inode_return_delegation(inode);
1489 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1491 struct nfs4_state *state = opendata->state;
1492 struct nfs_inode *nfsi = NFS_I(state->inode);
1493 struct nfs_delegation *delegation;
1494 int open_mode = opendata->o_arg.open_flags;
1495 fmode_t fmode = opendata->o_arg.fmode;
1496 enum open_claim_type4 claim = opendata->o_arg.claim;
1497 nfs4_stateid stateid;
1501 spin_lock(&state->owner->so_lock);
1502 if (can_open_cached(state, fmode, open_mode)) {
1503 update_open_stateflags(state, fmode);
1504 spin_unlock(&state->owner->so_lock);
1505 goto out_return_state;
1507 spin_unlock(&state->owner->so_lock);
1509 delegation = rcu_dereference(nfsi->delegation);
1510 if (!can_open_delegated(delegation, fmode, claim)) {
1514 /* Save the delegation */
1515 nfs4_stateid_copy(&stateid, &delegation->stateid);
1517 nfs_release_seqid(opendata->o_arg.seqid);
1518 if (!opendata->is_recover) {
1519 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1525 /* Try to update the stateid using the delegation */
1526 if (update_open_stateid(state, NULL, &stateid, fmode))
1527 goto out_return_state;
1530 return ERR_PTR(ret);
1532 atomic_inc(&state->count);
1537 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1539 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1540 struct nfs_delegation *delegation;
1541 int delegation_flags = 0;
1544 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1546 delegation_flags = delegation->flags;
1548 switch (data->o_arg.claim) {
1551 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1552 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1553 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1554 "returning a delegation for "
1555 "OPEN(CLAIM_DELEGATE_CUR)\n",
1559 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1560 nfs_inode_set_delegation(state->inode,
1561 data->owner->so_cred,
1564 nfs_inode_reclaim_delegation(state->inode,
1565 data->owner->so_cred,
1570 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1571 * and update the nfs4_state.
1573 static struct nfs4_state *
1574 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1576 struct inode *inode = data->state->inode;
1577 struct nfs4_state *state = data->state;
1580 if (!data->rpc_done) {
1581 if (data->rpc_status) {
1582 ret = data->rpc_status;
1585 /* cached opens have already been processed */
1589 ret = nfs_refresh_inode(inode, &data->f_attr);
1593 if (data->o_res.delegation_type != 0)
1594 nfs4_opendata_check_deleg(data, state);
1596 update_open_stateid(state, &data->o_res.stateid, NULL,
1598 atomic_inc(&state->count);
1602 return ERR_PTR(ret);
1606 static struct nfs4_state *
1607 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1609 struct inode *inode;
1610 struct nfs4_state *state = NULL;
1613 if (!data->rpc_done) {
1614 state = nfs4_try_open_cached(data);
1615 trace_nfs4_cached_open(data->state);
1620 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1622 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
1623 ret = PTR_ERR(inode);
1627 state = nfs4_get_open_state(inode, data->owner);
1630 if (data->o_res.delegation_type != 0)
1631 nfs4_opendata_check_deleg(data, state);
1632 update_open_stateid(state, &data->o_res.stateid, NULL,
1636 nfs_release_seqid(data->o_arg.seqid);
1641 return ERR_PTR(ret);
1644 static struct nfs4_state *
1645 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1647 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1648 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1649 return _nfs4_opendata_to_nfs4_state(data);
1652 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1654 struct nfs_inode *nfsi = NFS_I(state->inode);
1655 struct nfs_open_context *ctx;
1657 spin_lock(&state->inode->i_lock);
1658 list_for_each_entry(ctx, &nfsi->open_files, list) {
1659 if (ctx->state != state)
1661 get_nfs_open_context(ctx);
1662 spin_unlock(&state->inode->i_lock);
1665 spin_unlock(&state->inode->i_lock);
1666 return ERR_PTR(-ENOENT);
1669 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1670 struct nfs4_state *state, enum open_claim_type4 claim)
1672 struct nfs4_opendata *opendata;
1674 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
1675 NULL, NULL, claim, GFP_NOFS);
1676 if (opendata == NULL)
1677 return ERR_PTR(-ENOMEM);
1678 opendata->state = state;
1679 atomic_inc(&state->count);
1683 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1686 struct nfs4_state *newstate;
1689 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
1691 opendata->o_arg.open_flags = 0;
1692 opendata->o_arg.fmode = fmode;
1693 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1694 NFS_SB(opendata->dentry->d_sb),
1696 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1697 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1698 nfs4_init_opendata_res(opendata);
1699 ret = _nfs4_recover_proc_open(opendata);
1702 newstate = nfs4_opendata_to_nfs4_state(opendata);
1703 if (IS_ERR(newstate))
1704 return PTR_ERR(newstate);
1705 if (newstate != opendata->state)
1707 nfs4_close_state(newstate, fmode);
1711 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1715 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1716 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1717 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1718 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1719 /* memory barrier prior to reading state->n_* */
1720 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1721 clear_bit(NFS_OPEN_STATE, &state->flags);
1723 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1726 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1729 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1733 * We may have performed cached opens for all three recoveries.
1734 * Check if we need to update the current stateid.
1736 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1737 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
1738 write_seqlock(&state->seqlock);
1739 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1740 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1741 write_sequnlock(&state->seqlock);
1748 * reclaim state on the server after a reboot.
1750 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1752 struct nfs_delegation *delegation;
1753 struct nfs4_opendata *opendata;
1754 fmode_t delegation_type = 0;
1757 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1758 NFS4_OPEN_CLAIM_PREVIOUS);
1759 if (IS_ERR(opendata))
1760 return PTR_ERR(opendata);
1762 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1763 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1764 delegation_type = delegation->type;
1766 opendata->o_arg.u.delegation_type = delegation_type;
1767 status = nfs4_open_recover(opendata, state);
1768 nfs4_opendata_put(opendata);
1772 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1774 struct nfs_server *server = NFS_SERVER(state->inode);
1775 struct nfs4_exception exception = { };
1778 err = _nfs4_do_open_reclaim(ctx, state);
1779 trace_nfs4_open_reclaim(ctx, 0, err);
1780 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1782 if (err != -NFS4ERR_DELAY)
1784 nfs4_handle_exception(server, err, &exception);
1785 } while (exception.retry);
1789 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1791 struct nfs_open_context *ctx;
1794 ctx = nfs4_state_find_open_context(state);
1797 ret = nfs4_do_open_reclaim(ctx, state);
1798 put_nfs_open_context(ctx);
1802 static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, int err)
1806 printk(KERN_ERR "NFS: %s: unhandled error "
1807 "%d.\n", __func__, err);
1813 case -NFS4ERR_BADSESSION:
1814 case -NFS4ERR_BADSLOT:
1815 case -NFS4ERR_BAD_HIGH_SLOT:
1816 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1817 case -NFS4ERR_DEADSESSION:
1818 set_bit(NFS_DELEGATED_STATE, &state->flags);
1819 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1821 case -NFS4ERR_STALE_CLIENTID:
1822 case -NFS4ERR_STALE_STATEID:
1823 set_bit(NFS_DELEGATED_STATE, &state->flags);
1824 case -NFS4ERR_EXPIRED:
1825 /* Don't recall a delegation if it was lost */
1826 nfs4_schedule_lease_recovery(server->nfs_client);
1828 case -NFS4ERR_MOVED:
1829 nfs4_schedule_migration_recovery(server);
1831 case -NFS4ERR_LEASE_MOVED:
1832 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1834 case -NFS4ERR_DELEG_REVOKED:
1835 case -NFS4ERR_ADMIN_REVOKED:
1836 case -NFS4ERR_BAD_STATEID:
1837 case -NFS4ERR_OPENMODE:
1838 nfs_inode_find_state_and_recover(state->inode,
1840 nfs4_schedule_stateid_recovery(server, state);
1842 case -NFS4ERR_DELAY:
1843 case -NFS4ERR_GRACE:
1844 set_bit(NFS_DELEGATED_STATE, &state->flags);
1848 case -NFS4ERR_DENIED:
1849 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1855 int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
1856 struct nfs4_state *state, const nfs4_stateid *stateid,
1859 struct nfs_server *server = NFS_SERVER(state->inode);
1860 struct nfs4_opendata *opendata;
1863 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1864 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1865 if (IS_ERR(opendata))
1866 return PTR_ERR(opendata);
1867 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1868 write_seqlock(&state->seqlock);
1869 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1870 write_sequnlock(&state->seqlock);
1871 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1872 switch (type & (FMODE_READ|FMODE_WRITE)) {
1873 case FMODE_READ|FMODE_WRITE:
1875 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1878 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1882 err = nfs4_open_recover_helper(opendata, FMODE_READ);
1884 nfs4_opendata_put(opendata);
1885 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1888 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1890 struct nfs4_opendata *data = calldata;
1892 nfs40_setup_sequence(data->o_arg.server->nfs_client->cl_slot_tbl,
1893 &data->c_arg.seq_args, &data->c_res.seq_res, task);
1896 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1898 struct nfs4_opendata *data = calldata;
1900 nfs40_sequence_done(task, &data->c_res.seq_res);
1902 data->rpc_status = task->tk_status;
1903 if (data->rpc_status == 0) {
1904 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
1905 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1906 renew_lease(data->o_res.server, data->timestamp);
1911 static void nfs4_open_confirm_release(void *calldata)
1913 struct nfs4_opendata *data = calldata;
1914 struct nfs4_state *state = NULL;
1916 /* If this request hasn't been cancelled, do nothing */
1917 if (data->cancelled == 0)
1919 /* In case of error, no cleanup! */
1920 if (!data->rpc_done)
1922 state = nfs4_opendata_to_nfs4_state(data);
1924 nfs4_close_state(state, data->o_arg.fmode);
1926 nfs4_opendata_put(data);
1929 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1930 .rpc_call_prepare = nfs4_open_confirm_prepare,
1931 .rpc_call_done = nfs4_open_confirm_done,
1932 .rpc_release = nfs4_open_confirm_release,
1936 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1938 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1940 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
1941 struct rpc_task *task;
1942 struct rpc_message msg = {
1943 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1944 .rpc_argp = &data->c_arg,
1945 .rpc_resp = &data->c_res,
1946 .rpc_cred = data->owner->so_cred,
1948 struct rpc_task_setup task_setup_data = {
1949 .rpc_client = server->client,
1950 .rpc_message = &msg,
1951 .callback_ops = &nfs4_open_confirm_ops,
1952 .callback_data = data,
1953 .workqueue = nfsiod_workqueue,
1954 .flags = RPC_TASK_ASYNC,
1958 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
1959 kref_get(&data->kref);
1961 data->rpc_status = 0;
1962 data->timestamp = jiffies;
1963 if (data->is_recover)
1964 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
1965 task = rpc_run_task(&task_setup_data);
1967 return PTR_ERR(task);
1968 status = nfs4_wait_for_completion_rpc_task(task);
1970 data->cancelled = 1;
1973 status = data->rpc_status;
1978 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1980 struct nfs4_opendata *data = calldata;
1981 struct nfs4_state_owner *sp = data->owner;
1982 struct nfs_client *clp = sp->so_server->nfs_client;
1983 enum open_claim_type4 claim = data->o_arg.claim;
1985 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1988 * Check if we still need to send an OPEN call, or if we can use
1989 * a delegation instead.
1991 if (data->state != NULL) {
1992 struct nfs_delegation *delegation;
1994 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1997 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1998 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
1999 goto unlock_no_action;
2002 /* Update client id. */
2003 data->o_arg.clientid = clp->cl_clientid;
2007 case NFS4_OPEN_CLAIM_PREVIOUS:
2008 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2009 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
2010 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
2011 case NFS4_OPEN_CLAIM_FH:
2012 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
2013 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
2015 data->timestamp = jiffies;
2016 if (nfs4_setup_sequence(data->o_arg.server,
2017 &data->o_arg.seq_args,
2018 &data->o_res.seq_res,
2020 nfs_release_seqid(data->o_arg.seqid);
2022 /* Set the create mode (note dependency on the session type) */
2023 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2024 if (data->o_arg.open_flags & O_EXCL) {
2025 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2026 if (nfs4_has_persistent_session(clp))
2027 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2028 else if (clp->cl_mvops->minor_version > 0)
2029 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2033 trace_nfs4_cached_open(data->state);
2036 task->tk_action = NULL;
2038 nfs4_sequence_done(task, &data->o_res.seq_res);
2041 static void nfs4_open_done(struct rpc_task *task, void *calldata)
2043 struct nfs4_opendata *data = calldata;
2045 data->rpc_status = task->tk_status;
2047 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
2050 if (task->tk_status == 0) {
2051 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2052 switch (data->o_res.f_attr->mode & S_IFMT) {
2056 data->rpc_status = -ELOOP;
2059 data->rpc_status = -EISDIR;
2062 data->rpc_status = -ENOTDIR;
2065 renew_lease(data->o_res.server, data->timestamp);
2066 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2067 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2072 static void nfs4_open_release(void *calldata)
2074 struct nfs4_opendata *data = calldata;
2075 struct nfs4_state *state = NULL;
2077 /* If this request hasn't been cancelled, do nothing */
2078 if (data->cancelled == 0)
2080 /* In case of error, no cleanup! */
2081 if (data->rpc_status != 0 || !data->rpc_done)
2083 /* In case we need an open_confirm, no cleanup! */
2084 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2086 state = nfs4_opendata_to_nfs4_state(data);
2088 nfs4_close_state(state, data->o_arg.fmode);
2090 nfs4_opendata_put(data);
2093 static const struct rpc_call_ops nfs4_open_ops = {
2094 .rpc_call_prepare = nfs4_open_prepare,
2095 .rpc_call_done = nfs4_open_done,
2096 .rpc_release = nfs4_open_release,
2099 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
2101 struct inode *dir = d_inode(data->dir);
2102 struct nfs_server *server = NFS_SERVER(dir);
2103 struct nfs_openargs *o_arg = &data->o_arg;
2104 struct nfs_openres *o_res = &data->o_res;
2105 struct rpc_task *task;
2106 struct rpc_message msg = {
2107 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2110 .rpc_cred = data->owner->so_cred,
2112 struct rpc_task_setup task_setup_data = {
2113 .rpc_client = server->client,
2114 .rpc_message = &msg,
2115 .callback_ops = &nfs4_open_ops,
2116 .callback_data = data,
2117 .workqueue = nfsiod_workqueue,
2118 .flags = RPC_TASK_ASYNC,
2122 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
2123 kref_get(&data->kref);
2125 data->rpc_status = 0;
2126 data->cancelled = 0;
2127 data->is_recover = 0;
2129 nfs4_set_sequence_privileged(&o_arg->seq_args);
2130 data->is_recover = 1;
2132 task = rpc_run_task(&task_setup_data);
2134 return PTR_ERR(task);
2135 status = nfs4_wait_for_completion_rpc_task(task);
2137 data->cancelled = 1;
2140 status = data->rpc_status;
2146 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2148 struct inode *dir = d_inode(data->dir);
2149 struct nfs_openres *o_res = &data->o_res;
2152 status = nfs4_run_open_task(data, 1);
2153 if (status != 0 || !data->rpc_done)
2156 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2158 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2159 status = _nfs4_proc_open_confirm(data);
2168 * Additional permission checks in order to distinguish between an
2169 * open for read, and an open for execute. This works around the
2170 * fact that NFSv4 OPEN treats read and execute permissions as being
2172 * Note that in the non-execute case, we want to turn off permission
2173 * checking if we just created a new file (POSIX open() semantics).
2175 static int nfs4_opendata_access(struct rpc_cred *cred,
2176 struct nfs4_opendata *opendata,
2177 struct nfs4_state *state, fmode_t fmode,
2180 struct nfs_access_entry cache;
2183 /* access call failed or for some reason the server doesn't
2184 * support any access modes -- defer access call until later */
2185 if (opendata->o_res.access_supported == 0)
2190 * Use openflags to check for exec, because fmode won't
2191 * always have FMODE_EXEC set when file open for exec.
2193 if (openflags & __FMODE_EXEC) {
2194 /* ONLY check for exec rights */
2196 } else if ((fmode & FMODE_READ) && !opendata->file_created)
2200 cache.jiffies = jiffies;
2201 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2202 nfs_access_add_cache(state->inode, &cache);
2204 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
2207 /* even though OPEN succeeded, access is denied. Close the file */
2208 nfs4_close_state(state, fmode);
2213 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2215 static int _nfs4_proc_open(struct nfs4_opendata *data)
2217 struct inode *dir = d_inode(data->dir);
2218 struct nfs_server *server = NFS_SERVER(dir);
2219 struct nfs_openargs *o_arg = &data->o_arg;
2220 struct nfs_openres *o_res = &data->o_res;
2223 status = nfs4_run_open_task(data, 0);
2224 if (!data->rpc_done)
2227 if (status == -NFS4ERR_BADNAME &&
2228 !(o_arg->open_flags & O_CREAT))
2233 nfs_fattr_map_and_free_names(server, &data->f_attr);
2235 if (o_arg->open_flags & O_CREAT) {
2236 update_changeattr(dir, &o_res->cinfo);
2237 if (o_arg->open_flags & O_EXCL)
2238 data->file_created = 1;
2239 else if (o_res->cinfo.before != o_res->cinfo.after)
2240 data->file_created = 1;
2242 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2243 server->caps &= ~NFS_CAP_POSIX_LOCK;
2244 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2245 status = _nfs4_proc_open_confirm(data);
2249 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
2250 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
2254 static int nfs4_recover_expired_lease(struct nfs_server *server)
2256 return nfs4_client_recover_expired_lease(server->nfs_client);
2261 * reclaim state on the server after a network partition.
2262 * Assumes caller holds the appropriate lock
2264 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2266 struct nfs4_opendata *opendata;
2269 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2270 NFS4_OPEN_CLAIM_FH);
2271 if (IS_ERR(opendata))
2272 return PTR_ERR(opendata);
2273 ret = nfs4_open_recover(opendata, state);
2275 d_drop(ctx->dentry);
2276 nfs4_opendata_put(opendata);
2280 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2282 struct nfs_server *server = NFS_SERVER(state->inode);
2283 struct nfs4_exception exception = { };
2287 err = _nfs4_open_expired(ctx, state);
2288 trace_nfs4_open_expired(ctx, 0, err);
2289 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2294 case -NFS4ERR_GRACE:
2295 case -NFS4ERR_DELAY:
2296 nfs4_handle_exception(server, err, &exception);
2299 } while (exception.retry);
2304 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2306 struct nfs_open_context *ctx;
2309 ctx = nfs4_state_find_open_context(state);
2312 ret = nfs4_do_open_expired(ctx, state);
2313 put_nfs_open_context(ctx);
2317 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state)
2319 nfs_remove_bad_delegation(state->inode);
2320 write_seqlock(&state->seqlock);
2321 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2322 write_sequnlock(&state->seqlock);
2323 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2326 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2328 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2329 nfs_finish_clear_delegation_stateid(state);
2332 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2334 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2335 nfs40_clear_delegation_stateid(state);
2336 return nfs4_open_expired(sp, state);
2339 #if defined(CONFIG_NFS_V4_1)
2340 static void nfs41_check_delegation_stateid(struct nfs4_state *state)
2342 struct nfs_server *server = NFS_SERVER(state->inode);
2343 nfs4_stateid stateid;
2344 struct nfs_delegation *delegation;
2345 struct rpc_cred *cred;
2348 /* Get the delegation credential for use by test/free_stateid */
2350 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2351 if (delegation == NULL) {
2356 nfs4_stateid_copy(&stateid, &delegation->stateid);
2357 cred = get_rpccred(delegation->cred);
2359 status = nfs41_test_stateid(server, &stateid, cred);
2360 trace_nfs4_test_delegation_stateid(state, NULL, status);
2362 if (status != NFS_OK) {
2363 /* Free the stateid unless the server explicitly
2364 * informs us the stateid is unrecognized. */
2365 if (status != -NFS4ERR_BAD_STATEID)
2366 nfs41_free_stateid(server, &stateid, cred);
2367 nfs_finish_clear_delegation_stateid(state);
2374 * nfs41_check_open_stateid - possibly free an open stateid
2376 * @state: NFSv4 state for an inode
2378 * Returns NFS_OK if recovery for this stateid is now finished.
2379 * Otherwise a negative NFS4ERR value is returned.
2381 static int nfs41_check_open_stateid(struct nfs4_state *state)
2383 struct nfs_server *server = NFS_SERVER(state->inode);
2384 nfs4_stateid *stateid = &state->open_stateid;
2385 struct rpc_cred *cred = state->owner->so_cred;
2388 /* If a state reset has been done, test_stateid is unneeded */
2389 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2390 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2391 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2392 return -NFS4ERR_BAD_STATEID;
2394 status = nfs41_test_stateid(server, stateid, cred);
2395 trace_nfs4_test_open_stateid(state, NULL, status);
2396 if (status != NFS_OK) {
2397 /* Free the stateid unless the server explicitly
2398 * informs us the stateid is unrecognized. */
2399 if (status != -NFS4ERR_BAD_STATEID)
2400 nfs41_free_stateid(server, stateid, cred);
2402 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2403 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2404 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2405 clear_bit(NFS_OPEN_STATE, &state->flags);
2410 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2414 nfs41_check_delegation_stateid(state);
2415 status = nfs41_check_open_stateid(state);
2416 if (status != NFS_OK)
2417 status = nfs4_open_expired(sp, state);
2423 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2424 * fields corresponding to attributes that were used to store the verifier.
2425 * Make sure we clobber those fields in the later setattr call
2427 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2428 struct iattr *sattr, struct nfs4_label **label)
2430 const u32 *attrset = opendata->o_res.attrset;
2432 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2433 !(sattr->ia_valid & ATTR_ATIME_SET))
2434 sattr->ia_valid |= ATTR_ATIME;
2436 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2437 !(sattr->ia_valid & ATTR_MTIME_SET))
2438 sattr->ia_valid |= ATTR_MTIME;
2440 /* Except MODE, it seems harmless of setting twice. */
2441 if ((attrset[1] & FATTR4_WORD1_MODE))
2442 sattr->ia_valid &= ~ATTR_MODE;
2444 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2448 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2451 struct nfs_open_context *ctx)
2453 struct nfs4_state_owner *sp = opendata->owner;
2454 struct nfs_server *server = sp->so_server;
2455 struct dentry *dentry;
2456 struct nfs4_state *state;
2460 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2462 ret = _nfs4_proc_open(opendata);
2466 state = nfs4_opendata_to_nfs4_state(opendata);
2467 ret = PTR_ERR(state);
2470 if (server->caps & NFS_CAP_POSIX_LOCK)
2471 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2473 dentry = opendata->dentry;
2474 if (d_really_is_negative(dentry)) {
2475 struct dentry *alias;
2477 alias = d_exact_alias(dentry, state->inode);
2479 alias = d_splice_alias(igrab(state->inode), dentry);
2480 /* d_splice_alias() can't fail here - it's a non-directory */
2483 ctx->dentry = dentry = alias;
2485 nfs_set_verifier(dentry,
2486 nfs_save_change_attribute(d_inode(opendata->dir)));
2489 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2494 if (d_inode(dentry) == state->inode) {
2495 nfs_inode_attach_open_context(ctx);
2496 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2497 nfs4_schedule_stateid_recovery(server, state);
2504 * Returns a referenced nfs4_state
2506 static int _nfs4_do_open(struct inode *dir,
2507 struct nfs_open_context *ctx,
2509 struct iattr *sattr,
2510 struct nfs4_label *label,
2513 struct nfs4_state_owner *sp;
2514 struct nfs4_state *state = NULL;
2515 struct nfs_server *server = NFS_SERVER(dir);
2516 struct nfs4_opendata *opendata;
2517 struct dentry *dentry = ctx->dentry;
2518 struct rpc_cred *cred = ctx->cred;
2519 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2520 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
2521 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
2522 struct nfs4_label *olabel = NULL;
2525 /* Protect against reboot recovery conflicts */
2527 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2529 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2532 status = nfs4_recover_expired_lease(server);
2534 goto err_put_state_owner;
2535 if (d_really_is_positive(dentry))
2536 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
2538 if (d_really_is_positive(dentry))
2539 claim = NFS4_OPEN_CLAIM_FH;
2540 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
2541 label, claim, GFP_KERNEL);
2542 if (opendata == NULL)
2543 goto err_put_state_owner;
2546 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2547 if (IS_ERR(olabel)) {
2548 status = PTR_ERR(olabel);
2549 goto err_opendata_put;
2553 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2554 if (!opendata->f_attr.mdsthreshold) {
2555 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2556 if (!opendata->f_attr.mdsthreshold)
2557 goto err_free_label;
2559 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
2561 if (d_really_is_positive(dentry))
2562 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
2564 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
2566 goto err_free_label;
2569 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
2570 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
2571 nfs4_exclusive_attrset(opendata, sattr, &label);
2573 * send create attributes which was not set by open
2574 * with an extra setattr.
2576 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2577 nfs_fattr_init(opendata->o_res.f_attr);
2578 status = nfs4_do_setattr(state->inode, cred,
2579 opendata->o_res.f_attr, sattr,
2580 state, label, olabel);
2582 nfs_setattr_update_inode(state->inode, sattr,
2583 opendata->o_res.f_attr);
2584 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2588 if (opened && opendata->file_created)
2589 *opened |= FILE_CREATED;
2591 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
2592 *ctx_th = opendata->f_attr.mdsthreshold;
2593 opendata->f_attr.mdsthreshold = NULL;
2596 nfs4_label_free(olabel);
2598 nfs4_opendata_put(opendata);
2599 nfs4_put_state_owner(sp);
2602 nfs4_label_free(olabel);
2604 nfs4_opendata_put(opendata);
2605 err_put_state_owner:
2606 nfs4_put_state_owner(sp);
2612 static struct nfs4_state *nfs4_do_open(struct inode *dir,
2613 struct nfs_open_context *ctx,
2615 struct iattr *sattr,
2616 struct nfs4_label *label,
2619 struct nfs_server *server = NFS_SERVER(dir);
2620 struct nfs4_exception exception = { };
2621 struct nfs4_state *res;
2625 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
2627 trace_nfs4_open_file(ctx, flags, status);
2630 /* NOTE: BAD_SEQID means the server and client disagree about the
2631 * book-keeping w.r.t. state-changing operations
2632 * (OPEN/CLOSE/LOCK/LOCKU...)
2633 * It is actually a sign of a bug on the client or on the server.
2635 * If we receive a BAD_SEQID error in the particular case of
2636 * doing an OPEN, we assume that nfs_increment_open_seqid() will
2637 * have unhashed the old state_owner for us, and that we can
2638 * therefore safely retry using a new one. We should still warn
2639 * the user though...
2641 if (status == -NFS4ERR_BAD_SEQID) {
2642 pr_warn_ratelimited("NFS: v4 server %s "
2643 " returned a bad sequence-id error!\n",
2644 NFS_SERVER(dir)->nfs_client->cl_hostname);
2645 exception.retry = 1;
2649 * BAD_STATEID on OPEN means that the server cancelled our
2650 * state before it received the OPEN_CONFIRM.
2651 * Recover by retrying the request as per the discussion
2652 * on Page 181 of RFC3530.
2654 if (status == -NFS4ERR_BAD_STATEID) {
2655 exception.retry = 1;
2658 if (status == -EAGAIN) {
2659 /* We must have found a delegation */
2660 exception.retry = 1;
2663 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2665 res = ERR_PTR(nfs4_handle_exception(server,
2666 status, &exception));
2667 } while (exception.retry);
2671 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2672 struct nfs_fattr *fattr, struct iattr *sattr,
2673 struct nfs4_state *state, struct nfs4_label *ilabel,
2674 struct nfs4_label *olabel)
2676 struct nfs_server *server = NFS_SERVER(inode);
2677 struct nfs_setattrargs arg = {
2678 .fh = NFS_FH(inode),
2681 .bitmask = server->attr_bitmask,
2684 struct nfs_setattrres res = {
2689 struct rpc_message msg = {
2690 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2695 unsigned long timestamp = jiffies;
2700 arg.bitmask = nfs4_bitmask(server, ilabel);
2702 arg.bitmask = nfs4_bitmask(server, olabel);
2704 nfs_fattr_init(fattr);
2706 /* Servers should only apply open mode checks for file size changes */
2707 truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false;
2708 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2710 if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) {
2711 /* Use that stateid */
2712 } else if (truncate && state != NULL) {
2713 struct nfs_lockowner lockowner = {
2714 .l_owner = current->files,
2715 .l_pid = current->tgid,
2717 if (!nfs4_valid_open_stateid(state))
2719 if (nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
2720 &lockowner) == -EIO)
2723 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
2725 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2726 if (status == 0 && state != NULL)
2727 renew_lease(server, timestamp);
2728 trace_nfs4_setattr(inode, &arg.stateid, status);
2732 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2733 struct nfs_fattr *fattr, struct iattr *sattr,
2734 struct nfs4_state *state, struct nfs4_label *ilabel,
2735 struct nfs4_label *olabel)
2737 struct nfs_server *server = NFS_SERVER(inode);
2738 struct nfs4_exception exception = {
2744 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state, ilabel, olabel);
2746 case -NFS4ERR_OPENMODE:
2747 if (!(sattr->ia_valid & ATTR_SIZE)) {
2748 pr_warn_once("NFSv4: server %s is incorrectly "
2749 "applying open mode checks to "
2750 "a SETATTR that is not "
2751 "changing file size.\n",
2752 server->nfs_client->cl_hostname);
2754 if (state && !(state->state & FMODE_WRITE)) {
2756 if (sattr->ia_valid & ATTR_OPEN)
2761 err = nfs4_handle_exception(server, err, &exception);
2762 } while (exception.retry);
2768 nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
2770 if (inode == NULL || !nfs_have_layout(inode))
2773 return pnfs_wait_on_layoutreturn(inode, task);
2776 struct nfs4_closedata {
2777 struct inode *inode;
2778 struct nfs4_state *state;
2779 struct nfs_closeargs arg;
2780 struct nfs_closeres res;
2781 struct nfs_fattr fattr;
2782 unsigned long timestamp;
2787 static void nfs4_free_closedata(void *data)
2789 struct nfs4_closedata *calldata = data;
2790 struct nfs4_state_owner *sp = calldata->state->owner;
2791 struct super_block *sb = calldata->state->inode->i_sb;
2794 pnfs_roc_release(calldata->state->inode);
2795 nfs4_put_open_state(calldata->state);
2796 nfs_free_seqid(calldata->arg.seqid);
2797 nfs4_put_state_owner(sp);
2798 nfs_sb_deactive(sb);
2802 static void nfs4_close_done(struct rpc_task *task, void *data)
2804 struct nfs4_closedata *calldata = data;
2805 struct nfs4_state *state = calldata->state;
2806 struct nfs_server *server = NFS_SERVER(calldata->inode);
2807 nfs4_stateid *res_stateid = NULL;
2809 dprintk("%s: begin!\n", __func__);
2810 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2812 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
2813 /* hmm. we are done with the inode, and in the process of freeing
2814 * the state_owner. we keep this around to process errors
2816 switch (task->tk_status) {
2818 res_stateid = &calldata->res.stateid;
2820 pnfs_roc_set_barrier(state->inode,
2821 calldata->roc_barrier);
2822 renew_lease(server, calldata->timestamp);
2824 case -NFS4ERR_ADMIN_REVOKED:
2825 case -NFS4ERR_STALE_STATEID:
2826 case -NFS4ERR_OLD_STATEID:
2827 case -NFS4ERR_BAD_STATEID:
2828 case -NFS4ERR_EXPIRED:
2829 if (!nfs4_stateid_match(&calldata->arg.stateid,
2830 &state->open_stateid)) {
2831 rpc_restart_call_prepare(task);
2834 if (calldata->arg.fmode == 0)
2837 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
2838 rpc_restart_call_prepare(task);
2842 nfs_clear_open_stateid(state, &calldata->arg.stateid,
2843 res_stateid, calldata->arg.fmode);
2845 nfs_release_seqid(calldata->arg.seqid);
2846 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
2847 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
2850 static void nfs4_close_prepare(struct rpc_task *task, void *data)
2852 struct nfs4_closedata *calldata = data;
2853 struct nfs4_state *state = calldata->state;
2854 struct inode *inode = calldata->inode;
2855 bool is_rdonly, is_wronly, is_rdwr;
2858 dprintk("%s: begin!\n", __func__);
2859 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
2862 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2863 spin_lock(&state->owner->so_lock);
2864 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
2865 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
2866 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
2867 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
2868 /* Calculate the change in open mode */
2869 calldata->arg.fmode = 0;
2870 if (state->n_rdwr == 0) {
2871 if (state->n_rdonly == 0)
2872 call_close |= is_rdonly;
2874 calldata->arg.fmode |= FMODE_READ;
2875 if (state->n_wronly == 0)
2876 call_close |= is_wronly;
2878 calldata->arg.fmode |= FMODE_WRITE;
2880 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
2882 if (calldata->arg.fmode == 0)
2883 call_close |= is_rdwr;
2885 if (!nfs4_valid_open_stateid(state))
2887 spin_unlock(&state->owner->so_lock);
2890 /* Note: exit _without_ calling nfs4_close_done */
2894 if (nfs4_wait_on_layoutreturn(inode, task)) {
2895 nfs_release_seqid(calldata->arg.seqid);
2899 if (calldata->arg.fmode == 0)
2900 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
2902 pnfs_roc_get_barrier(inode, &calldata->roc_barrier);
2904 calldata->arg.share_access =
2905 nfs4_map_atomic_open_share(NFS_SERVER(inode),
2906 calldata->arg.fmode, 0);
2908 nfs_fattr_init(calldata->res.fattr);
2909 calldata->timestamp = jiffies;
2910 if (nfs4_setup_sequence(NFS_SERVER(inode),
2911 &calldata->arg.seq_args,
2912 &calldata->res.seq_res,
2914 nfs_release_seqid(calldata->arg.seqid);
2915 dprintk("%s: done!\n", __func__);
2918 task->tk_action = NULL;
2920 nfs4_sequence_done(task, &calldata->res.seq_res);
2923 static const struct rpc_call_ops nfs4_close_ops = {
2924 .rpc_call_prepare = nfs4_close_prepare,
2925 .rpc_call_done = nfs4_close_done,
2926 .rpc_release = nfs4_free_closedata,
2929 static bool nfs4_roc(struct inode *inode)
2931 if (!nfs_have_layout(inode))
2933 return pnfs_roc(inode);
2937 * It is possible for data to be read/written from a mem-mapped file
2938 * after the sys_close call (which hits the vfs layer as a flush).
2939 * This means that we can't safely call nfsv4 close on a file until
2940 * the inode is cleared. This in turn means that we are not good
2941 * NFSv4 citizens - we do not indicate to the server to update the file's
2942 * share state even when we are done with one of the three share
2943 * stateid's in the inode.
2945 * NOTE: Caller must be holding the sp->so_owner semaphore!
2947 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
2949 struct nfs_server *server = NFS_SERVER(state->inode);
2950 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
2951 struct nfs4_closedata *calldata;
2952 struct nfs4_state_owner *sp = state->owner;
2953 struct rpc_task *task;
2954 struct rpc_message msg = {
2955 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2956 .rpc_cred = state->owner->so_cred,
2958 struct rpc_task_setup task_setup_data = {
2959 .rpc_client = server->client,
2960 .rpc_message = &msg,
2961 .callback_ops = &nfs4_close_ops,
2962 .workqueue = nfsiod_workqueue,
2963 .flags = RPC_TASK_ASYNC,
2965 int status = -ENOMEM;
2967 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
2968 &task_setup_data.rpc_client, &msg);
2970 calldata = kzalloc(sizeof(*calldata), gfp_mask);
2971 if (calldata == NULL)
2973 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
2974 calldata->inode = state->inode;
2975 calldata->state = state;
2976 calldata->arg.fh = NFS_FH(state->inode);
2977 /* Serialization for the sequence id */
2978 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
2979 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
2980 if (IS_ERR(calldata->arg.seqid))
2981 goto out_free_calldata;
2982 calldata->arg.fmode = 0;
2983 calldata->arg.bitmask = server->cache_consistency_bitmask;
2984 calldata->res.fattr = &calldata->fattr;
2985 calldata->res.seqid = calldata->arg.seqid;
2986 calldata->res.server = server;
2987 calldata->roc = nfs4_roc(state->inode);
2988 nfs_sb_active(calldata->inode->i_sb);
2990 msg.rpc_argp = &calldata->arg;
2991 msg.rpc_resp = &calldata->res;
2992 task_setup_data.callback_data = calldata;
2993 task = rpc_run_task(&task_setup_data);
2995 return PTR_ERR(task);
2998 status = rpc_wait_for_completion_task(task);
3004 nfs4_put_open_state(state);
3005 nfs4_put_state_owner(sp);
3009 static struct inode *
3010 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3011 int open_flags, struct iattr *attr, int *opened)
3013 struct nfs4_state *state;
3014 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3016 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
3018 /* Protect against concurrent sillydeletes */
3019 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
3021 nfs4_label_release_security(label);
3024 return ERR_CAST(state);
3025 return state->inode;
3028 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
3030 if (ctx->state == NULL)
3033 nfs4_close_sync(ctx->state, ctx->mode);
3035 nfs4_close_state(ctx->state, ctx->mode);
3038 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3039 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3040 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
3042 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3044 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
3045 struct nfs4_server_caps_arg args = {
3049 struct nfs4_server_caps_res res = {};
3050 struct rpc_message msg = {
3051 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
3057 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3058 FATTR4_WORD0_FH_EXPIRE_TYPE |
3059 FATTR4_WORD0_LINK_SUPPORT |
3060 FATTR4_WORD0_SYMLINK_SUPPORT |
3061 FATTR4_WORD0_ACLSUPPORT;
3063 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3065 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3067 /* Sanity check the server answers */
3068 switch (minorversion) {
3070 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3071 res.attr_bitmask[2] = 0;
3074 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3077 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3079 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
3080 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3081 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3082 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3083 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
3084 NFS_CAP_CTIME|NFS_CAP_MTIME|
3085 NFS_CAP_SECURITY_LABEL);
3086 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3087 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3088 server->caps |= NFS_CAP_ACLS;
3089 if (res.has_links != 0)
3090 server->caps |= NFS_CAP_HARDLINKS;
3091 if (res.has_symlinks != 0)
3092 server->caps |= NFS_CAP_SYMLINKS;
3093 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3094 server->caps |= NFS_CAP_FILEID;
3095 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3096 server->caps |= NFS_CAP_MODE;
3097 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3098 server->caps |= NFS_CAP_NLINK;
3099 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3100 server->caps |= NFS_CAP_OWNER;
3101 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3102 server->caps |= NFS_CAP_OWNER_GROUP;
3103 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3104 server->caps |= NFS_CAP_ATIME;
3105 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3106 server->caps |= NFS_CAP_CTIME;
3107 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3108 server->caps |= NFS_CAP_MTIME;
3109 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3110 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3111 server->caps |= NFS_CAP_SECURITY_LABEL;
3113 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3114 sizeof(server->attr_bitmask));
3115 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3117 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3118 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3119 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
3120 server->cache_consistency_bitmask[2] = 0;
3121 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3122 sizeof(server->exclcreat_bitmask));
3123 server->acl_bitmask = res.acl_bitmask;
3124 server->fh_expire_type = res.fh_expire_type;
3130 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3132 struct nfs4_exception exception = { };
3135 err = nfs4_handle_exception(server,
3136 _nfs4_server_capabilities(server, fhandle),
3138 } while (exception.retry);
3142 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3143 struct nfs_fsinfo *info)
3146 struct nfs4_lookup_root_arg args = {
3149 struct nfs4_lookup_res res = {
3151 .fattr = info->fattr,
3154 struct rpc_message msg = {
3155 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3160 bitmask[0] = nfs4_fattr_bitmap[0];
3161 bitmask[1] = nfs4_fattr_bitmap[1];
3163 * Process the label in the upcoming getfattr
3165 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3167 nfs_fattr_init(info->fattr);
3168 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3171 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3172 struct nfs_fsinfo *info)
3174 struct nfs4_exception exception = { };
3177 err = _nfs4_lookup_root(server, fhandle, info);
3178 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
3181 case -NFS4ERR_WRONGSEC:
3184 err = nfs4_handle_exception(server, err, &exception);
3186 } while (exception.retry);
3191 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3192 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3194 struct rpc_auth_create_args auth_args = {
3195 .pseudoflavor = flavor,
3197 struct rpc_auth *auth;
3200 auth = rpcauth_create(&auth_args, server->client);
3205 ret = nfs4_lookup_root(server, fhandle, info);
3211 * Retry pseudoroot lookup with various security flavors. We do this when:
3213 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3214 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3216 * Returns zero on success, or a negative NFS4ERR value, or a
3217 * negative errno value.
3219 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3220 struct nfs_fsinfo *info)
3222 /* Per 3530bis 15.33.5 */
3223 static const rpc_authflavor_t flav_array[] = {
3227 RPC_AUTH_UNIX, /* courtesy */
3230 int status = -EPERM;
3233 if (server->auth_info.flavor_len > 0) {
3234 /* try each flavor specified by user */
3235 for (i = 0; i < server->auth_info.flavor_len; i++) {
3236 status = nfs4_lookup_root_sec(server, fhandle, info,
3237 server->auth_info.flavors[i]);
3238 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3243 /* no flavors specified by user, try default list */
3244 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3245 status = nfs4_lookup_root_sec(server, fhandle, info,
3247 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3254 * -EACCESS could mean that the user doesn't have correct permissions
3255 * to access the mount. It could also mean that we tried to mount
3256 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3257 * existing mount programs don't handle -EACCES very well so it should
3258 * be mapped to -EPERM instead.
3260 if (status == -EACCES)
3265 static int nfs4_do_find_root_sec(struct nfs_server *server,
3266 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
3268 int mv = server->nfs_client->cl_minorversion;
3269 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
3273 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3274 * @server: initialized nfs_server handle
3275 * @fhandle: we fill in the pseudo-fs root file handle
3276 * @info: we fill in an FSINFO struct
3277 * @auth_probe: probe the auth flavours
3279 * Returns zero on success, or a negative errno.
3281 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
3282 struct nfs_fsinfo *info,
3288 status = nfs4_lookup_root(server, fhandle, info);
3290 if (auth_probe || status == NFS4ERR_WRONGSEC)
3291 status = nfs4_do_find_root_sec(server, fhandle, info);
3294 status = nfs4_server_capabilities(server, fhandle);
3296 status = nfs4_do_fsinfo(server, fhandle, info);
3298 return nfs4_map_errors(status);
3301 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3302 struct nfs_fsinfo *info)
3305 struct nfs_fattr *fattr = info->fattr;
3306 struct nfs4_label *label = NULL;
3308 error = nfs4_server_capabilities(server, mntfh);
3310 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3314 label = nfs4_label_alloc(server, GFP_KERNEL);
3316 return PTR_ERR(label);
3318 error = nfs4_proc_getattr(server, mntfh, fattr, label);
3320 dprintk("nfs4_get_root: getattr error = %d\n", -error);
3321 goto err_free_label;
3324 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3325 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3326 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3329 nfs4_label_free(label);
3335 * Get locations and (maybe) other attributes of a referral.
3336 * Note that we'll actually follow the referral later when
3337 * we detect fsid mismatch in inode revalidation
3339 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3340 const struct qstr *name, struct nfs_fattr *fattr,
3341 struct nfs_fh *fhandle)
3343 int status = -ENOMEM;
3344 struct page *page = NULL;
3345 struct nfs4_fs_locations *locations = NULL;
3347 page = alloc_page(GFP_KERNEL);
3350 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3351 if (locations == NULL)
3354 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
3359 * If the fsid didn't change, this is a migration event, not a
3360 * referral. Cause us to drop into the exception handler, which
3361 * will kick off migration recovery.
3363 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
3364 dprintk("%s: server did not return a different fsid for"
3365 " a referral at %s\n", __func__, name->name);
3366 status = -NFS4ERR_MOVED;
3369 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3370 nfs_fixup_referral_attributes(&locations->fattr);
3372 /* replace the lookup nfs_fattr with the locations nfs_fattr */
3373 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
3374 memset(fhandle, 0, sizeof(struct nfs_fh));
3382 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3383 struct nfs_fattr *fattr, struct nfs4_label *label)
3385 struct nfs4_getattr_arg args = {
3387 .bitmask = server->attr_bitmask,
3389 struct nfs4_getattr_res res = {
3394 struct rpc_message msg = {
3395 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3400 args.bitmask = nfs4_bitmask(server, label);
3402 nfs_fattr_init(fattr);
3403 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3406 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3407 struct nfs_fattr *fattr, struct nfs4_label *label)
3409 struct nfs4_exception exception = { };
3412 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3413 trace_nfs4_getattr(server, fhandle, fattr, err);
3414 err = nfs4_handle_exception(server, err,
3416 } while (exception.retry);
3421 * The file is not closed if it is opened due to the a request to change
3422 * the size of the file. The open call will not be needed once the
3423 * VFS layer lookup-intents are implemented.
3425 * Close is called when the inode is destroyed.
3426 * If we haven't opened the file for O_WRONLY, we
3427 * need to in the size_change case to obtain a stateid.
3430 * Because OPEN is always done by name in nfsv4, it is
3431 * possible that we opened a different file by the same
3432 * name. We can recognize this race condition, but we
3433 * can't do anything about it besides returning an error.
3435 * This will be fixed with VFS changes (lookup-intent).
3438 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3439 struct iattr *sattr)
3441 struct inode *inode = d_inode(dentry);
3442 struct rpc_cred *cred = NULL;
3443 struct nfs4_state *state = NULL;
3444 struct nfs4_label *label = NULL;
3447 if (pnfs_ld_layoutret_on_setattr(inode) &&
3448 sattr->ia_valid & ATTR_SIZE &&
3449 sattr->ia_size < i_size_read(inode))
3450 pnfs_commit_and_return_layout(inode);
3452 nfs_fattr_init(fattr);
3454 /* Deal with open(O_TRUNC) */
3455 if (sattr->ia_valid & ATTR_OPEN)
3456 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
3458 /* Optimization: if the end result is no change, don't RPC */
3459 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
3462 /* Search for an existing open(O_WRITE) file */
3463 if (sattr->ia_valid & ATTR_FILE) {
3464 struct nfs_open_context *ctx;
3466 ctx = nfs_file_open_context(sattr->ia_file);
3473 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3475 return PTR_ERR(label);
3477 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
3479 nfs_setattr_update_inode(inode, sattr, fattr);
3480 nfs_setsecurity(inode, fattr, label);
3482 nfs4_label_free(label);
3486 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3487 const struct qstr *name, struct nfs_fh *fhandle,
3488 struct nfs_fattr *fattr, struct nfs4_label *label)
3490 struct nfs_server *server = NFS_SERVER(dir);
3492 struct nfs4_lookup_arg args = {
3493 .bitmask = server->attr_bitmask,
3494 .dir_fh = NFS_FH(dir),
3497 struct nfs4_lookup_res res = {
3503 struct rpc_message msg = {
3504 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3509 args.bitmask = nfs4_bitmask(server, label);
3511 nfs_fattr_init(fattr);
3513 dprintk("NFS call lookup %s\n", name->name);
3514 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
3515 dprintk("NFS reply lookup: %d\n", status);
3519 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
3521 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
3522 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
3523 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3527 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3528 struct qstr *name, struct nfs_fh *fhandle,
3529 struct nfs_fattr *fattr, struct nfs4_label *label)
3531 struct nfs4_exception exception = { };
3532 struct rpc_clnt *client = *clnt;
3535 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
3536 trace_nfs4_lookup(dir, name, err);
3538 case -NFS4ERR_BADNAME:
3541 case -NFS4ERR_MOVED:
3542 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
3543 if (err == -NFS4ERR_MOVED)
3544 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3546 case -NFS4ERR_WRONGSEC:
3548 if (client != *clnt)
3550 client = nfs4_negotiate_security(client, dir, name);
3552 return PTR_ERR(client);
3554 exception.retry = 1;
3557 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3559 } while (exception.retry);
3564 else if (client != *clnt)
3565 rpc_shutdown_client(client);
3570 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
3571 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3572 struct nfs4_label *label)
3575 struct rpc_clnt *client = NFS_CLIENT(dir);
3577 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
3578 if (client != NFS_CLIENT(dir)) {
3579 rpc_shutdown_client(client);
3580 nfs_fixup_secinfo_attributes(fattr);
3586 nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
3587 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3589 struct rpc_clnt *client = NFS_CLIENT(dir);
3592 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
3594 return ERR_PTR(status);
3595 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
3598 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3600 struct nfs_server *server = NFS_SERVER(inode);
3601 struct nfs4_accessargs args = {
3602 .fh = NFS_FH(inode),
3603 .bitmask = server->cache_consistency_bitmask,
3605 struct nfs4_accessres res = {
3608 struct rpc_message msg = {
3609 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3612 .rpc_cred = entry->cred,
3614 int mode = entry->mask;
3618 * Determine which access bits we want to ask for...
3620 if (mode & MAY_READ)
3621 args.access |= NFS4_ACCESS_READ;
3622 if (S_ISDIR(inode->i_mode)) {
3623 if (mode & MAY_WRITE)
3624 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3625 if (mode & MAY_EXEC)
3626 args.access |= NFS4_ACCESS_LOOKUP;
3628 if (mode & MAY_WRITE)
3629 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3630 if (mode & MAY_EXEC)
3631 args.access |= NFS4_ACCESS_EXECUTE;
3634 res.fattr = nfs_alloc_fattr();
3635 if (res.fattr == NULL)
3638 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3640 nfs_access_set_mask(entry, res.access);
3641 nfs_refresh_inode(inode, res.fattr);
3643 nfs_free_fattr(res.fattr);
3647 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3649 struct nfs4_exception exception = { };
3652 err = _nfs4_proc_access(inode, entry);
3653 trace_nfs4_access(inode, err);
3654 err = nfs4_handle_exception(NFS_SERVER(inode), err,
3656 } while (exception.retry);
3661 * TODO: For the time being, we don't try to get any attributes
3662 * along with any of the zero-copy operations READ, READDIR,
3665 * In the case of the first three, we want to put the GETATTR
3666 * after the read-type operation -- this is because it is hard
3667 * to predict the length of a GETATTR response in v4, and thus
3668 * align the READ data correctly. This means that the GETATTR
3669 * may end up partially falling into the page cache, and we should
3670 * shift it into the 'tail' of the xdr_buf before processing.
3671 * To do this efficiently, we need to know the total length
3672 * of data received, which doesn't seem to be available outside
3675 * In the case of WRITE, we also want to put the GETATTR after
3676 * the operation -- in this case because we want to make sure
3677 * we get the post-operation mtime and size.
3679 * Both of these changes to the XDR layer would in fact be quite
3680 * minor, but I decided to leave them for a subsequent patch.
3682 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3683 unsigned int pgbase, unsigned int pglen)
3685 struct nfs4_readlink args = {
3686 .fh = NFS_FH(inode),
3691 struct nfs4_readlink_res res;
3692 struct rpc_message msg = {
3693 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3698 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
3701 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3702 unsigned int pgbase, unsigned int pglen)
3704 struct nfs4_exception exception = { };
3707 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3708 trace_nfs4_readlink(inode, err);
3709 err = nfs4_handle_exception(NFS_SERVER(inode), err,
3711 } while (exception.retry);
3716 * This is just for mknod. open(O_CREAT) will always do ->open_context().
3719 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
3722 struct nfs4_label l, *ilabel = NULL;
3723 struct nfs_open_context *ctx;
3724 struct nfs4_state *state;
3727 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3729 return PTR_ERR(ctx);
3731 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3733 sattr->ia_mode &= ~current_umask();
3734 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
3735 if (IS_ERR(state)) {
3736 status = PTR_ERR(state);
3740 nfs4_label_release_security(ilabel);
3741 put_nfs_open_context(ctx);
3745 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3747 struct nfs_server *server = NFS_SERVER(dir);
3748 struct nfs_removeargs args = {
3752 struct nfs_removeres res = {
3755 struct rpc_message msg = {
3756 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3762 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
3764 update_changeattr(dir, &res.cinfo);
3768 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3770 struct nfs4_exception exception = { };
3773 err = _nfs4_proc_remove(dir, name);
3774 trace_nfs4_remove(dir, name, err);
3775 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3777 } while (exception.retry);
3781 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
3783 struct nfs_server *server = NFS_SERVER(dir);
3784 struct nfs_removeargs *args = msg->rpc_argp;
3785 struct nfs_removeres *res = msg->rpc_resp;
3787 res->server = server;
3788 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
3789 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
3791 nfs_fattr_init(res->dir_attr);
3794 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3796 nfs4_setup_sequence(NFS_SERVER(data->dir),
3797 &data->args.seq_args,
3802 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
3804 struct nfs_unlinkdata *data = task->tk_calldata;
3805 struct nfs_removeres *res = &data->res;
3807 if (!nfs4_sequence_done(task, &res->seq_res))
3809 if (nfs4_async_handle_error(task, res->server, NULL,
3810 &data->timeout) == -EAGAIN)
3812 update_changeattr(dir, &res->cinfo);
3816 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3818 struct nfs_server *server = NFS_SERVER(dir);
3819 struct nfs_renameargs *arg = msg->rpc_argp;
3820 struct nfs_renameres *res = msg->rpc_resp;
3822 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
3823 res->server = server;
3824 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
3827 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3829 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3830 &data->args.seq_args,
3835 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3836 struct inode *new_dir)
3838 struct nfs_renamedata *data = task->tk_calldata;
3839 struct nfs_renameres *res = &data->res;
3841 if (!nfs4_sequence_done(task, &res->seq_res))
3843 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
3846 update_changeattr(old_dir, &res->old_cinfo);
3847 update_changeattr(new_dir, &res->new_cinfo);
3851 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3853 struct nfs_server *server = NFS_SERVER(inode);
3854 struct nfs4_link_arg arg = {
3855 .fh = NFS_FH(inode),
3856 .dir_fh = NFS_FH(dir),
3858 .bitmask = server->attr_bitmask,
3860 struct nfs4_link_res res = {
3864 struct rpc_message msg = {
3865 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3869 int status = -ENOMEM;
3871 res.fattr = nfs_alloc_fattr();
3872 if (res.fattr == NULL)
3875 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3876 if (IS_ERR(res.label)) {
3877 status = PTR_ERR(res.label);
3880 arg.bitmask = nfs4_bitmask(server, res.label);
3882 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3884 update_changeattr(dir, &res.cinfo);
3885 status = nfs_post_op_update_inode(inode, res.fattr);
3887 nfs_setsecurity(inode, res.fattr, res.label);
3891 nfs4_label_free(res.label);
3894 nfs_free_fattr(res.fattr);
3898 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3900 struct nfs4_exception exception = { };
3903 err = nfs4_handle_exception(NFS_SERVER(inode),
3904 _nfs4_proc_link(inode, dir, name),
3906 } while (exception.retry);
3910 struct nfs4_createdata {
3911 struct rpc_message msg;
3912 struct nfs4_create_arg arg;
3913 struct nfs4_create_res res;
3915 struct nfs_fattr fattr;
3916 struct nfs4_label *label;
3919 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3920 struct qstr *name, struct iattr *sattr, u32 ftype)
3922 struct nfs4_createdata *data;
3924 data = kzalloc(sizeof(*data), GFP_KERNEL);
3926 struct nfs_server *server = NFS_SERVER(dir);
3928 data->label = nfs4_label_alloc(server, GFP_KERNEL);
3929 if (IS_ERR(data->label))
3932 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3933 data->msg.rpc_argp = &data->arg;
3934 data->msg.rpc_resp = &data->res;
3935 data->arg.dir_fh = NFS_FH(dir);
3936 data->arg.server = server;
3937 data->arg.name = name;
3938 data->arg.attrs = sattr;
3939 data->arg.ftype = ftype;
3940 data->arg.bitmask = nfs4_bitmask(server, data->label);
3941 data->res.server = server;
3942 data->res.fh = &data->fh;
3943 data->res.fattr = &data->fattr;
3944 data->res.label = data->label;
3945 nfs_fattr_init(data->res.fattr);
3953 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3955 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
3956 &data->arg.seq_args, &data->res.seq_res, 1);
3958 update_changeattr(dir, &data->res.dir_cinfo);
3959 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
3964 static void nfs4_free_createdata(struct nfs4_createdata *data)
3966 nfs4_label_free(data->label);
3970 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3971 struct page *page, unsigned int len, struct iattr *sattr,
3972 struct nfs4_label *label)
3974 struct nfs4_createdata *data;
3975 int status = -ENAMETOOLONG;
3977 if (len > NFS4_MAXPATHLEN)
3981 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3985 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3986 data->arg.u.symlink.pages = &page;
3987 data->arg.u.symlink.len = len;
3988 data->arg.label = label;
3990 status = nfs4_do_create(dir, dentry, data);
3992 nfs4_free_createdata(data);
3997 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3998 struct page *page, unsigned int len, struct iattr *sattr)
4000 struct nfs4_exception exception = { };
4001 struct nfs4_label l, *label = NULL;
4004 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4007 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4008 trace_nfs4_symlink(dir, &dentry->d_name, err);
4009 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4011 } while (exception.retry);
4013 nfs4_label_release_security(label);
4017 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4018 struct iattr *sattr, struct nfs4_label *label)
4020 struct nfs4_createdata *data;
4021 int status = -ENOMEM;
4023 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4027 data->arg.label = label;
4028 status = nfs4_do_create(dir, dentry, data);
4030 nfs4_free_createdata(data);
4035 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4036 struct iattr *sattr)
4038 struct nfs4_exception exception = { };
4039 struct nfs4_label l, *label = NULL;
4042 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4044 sattr->ia_mode &= ~current_umask();
4046 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4047 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4048 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4050 } while (exception.retry);
4051 nfs4_label_release_security(label);
4056 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
4057 u64 cookie, struct page **pages, unsigned int count, int plus)
4059 struct inode *dir = d_inode(dentry);
4060 struct nfs4_readdir_arg args = {
4065 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
4068 struct nfs4_readdir_res res;
4069 struct rpc_message msg = {
4070 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4077 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4079 (unsigned long long)cookie);
4080 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
4081 res.pgbase = args.pgbase;
4082 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4084 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
4085 status += args.pgbase;
4088 nfs_invalidate_atime(dir);
4090 dprintk("%s: returns %d\n", __func__, status);
4094 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
4095 u64 cookie, struct page **pages, unsigned int count, int plus)
4097 struct nfs4_exception exception = { };
4100 err = _nfs4_proc_readdir(dentry, cred, cookie,
4101 pages, count, plus);
4102 trace_nfs4_readdir(d_inode(dentry), err);
4103 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
4105 } while (exception.retry);
4109 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4110 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
4112 struct nfs4_createdata *data;
4113 int mode = sattr->ia_mode;
4114 int status = -ENOMEM;
4116 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4121 data->arg.ftype = NF4FIFO;
4122 else if (S_ISBLK(mode)) {
4123 data->arg.ftype = NF4BLK;
4124 data->arg.u.device.specdata1 = MAJOR(rdev);
4125 data->arg.u.device.specdata2 = MINOR(rdev);
4127 else if (S_ISCHR(mode)) {
4128 data->arg.ftype = NF4CHR;
4129 data->arg.u.device.specdata1 = MAJOR(rdev);
4130 data->arg.u.device.specdata2 = MINOR(rdev);
4131 } else if (!S_ISSOCK(mode)) {
4136 data->arg.label = label;
4137 status = nfs4_do_create(dir, dentry, data);
4139 nfs4_free_createdata(data);
4144 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4145 struct iattr *sattr, dev_t rdev)
4147 struct nfs4_exception exception = { };
4148 struct nfs4_label l, *label = NULL;
4151 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4153 sattr->ia_mode &= ~current_umask();
4155 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4156 trace_nfs4_mknod(dir, &dentry->d_name, err);
4157 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4159 } while (exception.retry);
4161 nfs4_label_release_security(label);
4166 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4167 struct nfs_fsstat *fsstat)
4169 struct nfs4_statfs_arg args = {
4171 .bitmask = server->attr_bitmask,
4173 struct nfs4_statfs_res res = {
4176 struct rpc_message msg = {
4177 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4182 nfs_fattr_init(fsstat->fattr);
4183 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4186 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4188 struct nfs4_exception exception = { };
4191 err = nfs4_handle_exception(server,
4192 _nfs4_proc_statfs(server, fhandle, fsstat),
4194 } while (exception.retry);
4198 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4199 struct nfs_fsinfo *fsinfo)
4201 struct nfs4_fsinfo_arg args = {
4203 .bitmask = server->attr_bitmask,
4205 struct nfs4_fsinfo_res res = {
4208 struct rpc_message msg = {
4209 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4214 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4217 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4219 struct nfs4_exception exception = { };
4220 unsigned long now = jiffies;
4224 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
4225 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
4227 struct nfs_client *clp = server->nfs_client;
4229 spin_lock(&clp->cl_lock);
4230 clp->cl_lease_time = fsinfo->lease_time * HZ;
4231 clp->cl_last_renewal = now;
4232 spin_unlock(&clp->cl_lock);
4235 err = nfs4_handle_exception(server, err, &exception);
4236 } while (exception.retry);
4240 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4244 nfs_fattr_init(fsinfo->fattr);
4245 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
4247 /* block layout checks this! */
4248 server->pnfs_blksize = fsinfo->blksize;
4249 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
4255 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4256 struct nfs_pathconf *pathconf)
4258 struct nfs4_pathconf_arg args = {
4260 .bitmask = server->attr_bitmask,
4262 struct nfs4_pathconf_res res = {
4263 .pathconf = pathconf,
4265 struct rpc_message msg = {
4266 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4271 /* None of the pathconf attributes are mandatory to implement */
4272 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4273 memset(pathconf, 0, sizeof(*pathconf));
4277 nfs_fattr_init(pathconf->fattr);
4278 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4281 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4282 struct nfs_pathconf *pathconf)
4284 struct nfs4_exception exception = { };
4288 err = nfs4_handle_exception(server,
4289 _nfs4_proc_pathconf(server, fhandle, pathconf),
4291 } while (exception.retry);
4295 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
4296 const struct nfs_open_context *ctx,
4297 const struct nfs_lock_context *l_ctx,
4300 const struct nfs_lockowner *lockowner = NULL;
4303 lockowner = &l_ctx->lockowner;
4304 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
4306 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4308 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4309 const struct nfs_open_context *ctx,
4310 const struct nfs_lock_context *l_ctx,
4313 nfs4_stateid current_stateid;
4315 /* If the current stateid represents a lost lock, then exit */
4316 if (nfs4_set_rw_stateid(¤t_stateid, ctx, l_ctx, fmode) == -EIO)
4318 return nfs4_stateid_match(stateid, ¤t_stateid);
4321 static bool nfs4_error_stateid_expired(int err)
4324 case -NFS4ERR_DELEG_REVOKED:
4325 case -NFS4ERR_ADMIN_REVOKED:
4326 case -NFS4ERR_BAD_STATEID:
4327 case -NFS4ERR_STALE_STATEID:
4328 case -NFS4ERR_OLD_STATEID:
4329 case -NFS4ERR_OPENMODE:
4330 case -NFS4ERR_EXPIRED:
4336 void __nfs4_read_done_cb(struct nfs_pgio_header *hdr)
4338 nfs_invalidate_atime(hdr->inode);
4341 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
4343 struct nfs_server *server = NFS_SERVER(hdr->inode);
4345 trace_nfs4_read(hdr, task->tk_status);
4346 if (nfs4_async_handle_error(task, server,
4347 hdr->args.context->state,
4349 rpc_restart_call_prepare(task);
4353 __nfs4_read_done_cb(hdr);
4354 if (task->tk_status > 0)
4355 renew_lease(server, hdr->timestamp);
4359 static bool nfs4_read_stateid_changed(struct rpc_task *task,
4360 struct nfs_pgio_args *args)
4363 if (!nfs4_error_stateid_expired(task->tk_status) ||
4364 nfs4_stateid_is_current(&args->stateid,
4369 rpc_restart_call_prepare(task);
4373 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
4376 dprintk("--> %s\n", __func__);
4378 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
4380 if (nfs4_read_stateid_changed(task, &hdr->args))
4382 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4383 nfs4_read_done_cb(task, hdr);
4386 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4387 struct rpc_message *msg)
4389 hdr->timestamp = jiffies;
4390 hdr->pgio_done_cb = nfs4_read_done_cb;
4391 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
4392 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
4395 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4396 struct nfs_pgio_header *hdr)
4398 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4399 &hdr->args.seq_args,
4403 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4404 hdr->args.lock_context,
4405 hdr->rw_ops->rw_mode) == -EIO)
4407 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
4412 static int nfs4_write_done_cb(struct rpc_task *task,
4413 struct nfs_pgio_header *hdr)
4415 struct inode *inode = hdr->inode;
4417 trace_nfs4_write(hdr, task->tk_status);
4418 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4419 hdr->args.context->state,
4421 rpc_restart_call_prepare(task);
4424 if (task->tk_status >= 0) {
4425 renew_lease(NFS_SERVER(inode), hdr->timestamp);
4426 nfs_writeback_update_inode(hdr);
4431 static bool nfs4_write_stateid_changed(struct rpc_task *task,
4432 struct nfs_pgio_args *args)
4435 if (!nfs4_error_stateid_expired(task->tk_status) ||
4436 nfs4_stateid_is_current(&args->stateid,
4441 rpc_restart_call_prepare(task);
4445 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
4447 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
4449 if (nfs4_write_stateid_changed(task, &hdr->args))
4451 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4452 nfs4_write_done_cb(task, hdr);
4456 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
4458 /* Don't request attributes for pNFS or O_DIRECT writes */
4459 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
4461 /* Otherwise, request attributes if and only if we don't hold
4464 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
4467 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4468 struct rpc_message *msg)
4470 struct nfs_server *server = NFS_SERVER(hdr->inode);
4472 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4473 hdr->args.bitmask = NULL;
4474 hdr->res.fattr = NULL;
4476 hdr->args.bitmask = server->cache_consistency_bitmask;
4478 if (!hdr->pgio_done_cb)
4479 hdr->pgio_done_cb = nfs4_write_done_cb;
4480 hdr->res.server = server;
4481 hdr->timestamp = jiffies;
4483 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
4484 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
4487 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4489 nfs4_setup_sequence(NFS_SERVER(data->inode),
4490 &data->args.seq_args,
4495 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
4497 struct inode *inode = data->inode;
4499 trace_nfs4_commit(data, task->tk_status);
4500 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4501 NULL, NULL) == -EAGAIN) {
4502 rpc_restart_call_prepare(task);
4508 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
4510 if (!nfs4_sequence_done(task, &data->res.seq_res))
4512 return data->commit_done_cb(task, data);
4515 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
4517 struct nfs_server *server = NFS_SERVER(data->inode);
4519 if (data->commit_done_cb == NULL)
4520 data->commit_done_cb = nfs4_commit_done_cb;
4521 data->res.server = server;
4522 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
4523 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4526 struct nfs4_renewdata {
4527 struct nfs_client *client;
4528 unsigned long timestamp;
4532 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4533 * standalone procedure for queueing an asynchronous RENEW.
4535 static void nfs4_renew_release(void *calldata)
4537 struct nfs4_renewdata *data = calldata;
4538 struct nfs_client *clp = data->client;
4540 if (atomic_read(&clp->cl_count) > 1)
4541 nfs4_schedule_state_renewal(clp);
4542 nfs_put_client(clp);
4546 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
4548 struct nfs4_renewdata *data = calldata;
4549 struct nfs_client *clp = data->client;
4550 unsigned long timestamp = data->timestamp;
4552 trace_nfs4_renew_async(clp, task->tk_status);
4553 switch (task->tk_status) {
4556 case -NFS4ERR_LEASE_MOVED:
4557 nfs4_schedule_lease_moved_recovery(clp);
4560 /* Unless we're shutting down, schedule state recovery! */
4561 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4563 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
4564 nfs4_schedule_lease_recovery(clp);
4567 nfs4_schedule_path_down_recovery(clp);
4569 do_renew_lease(clp, timestamp);
4572 static const struct rpc_call_ops nfs4_renew_ops = {
4573 .rpc_call_done = nfs4_renew_done,
4574 .rpc_release = nfs4_renew_release,
4577 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
4579 struct rpc_message msg = {
4580 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4584 struct nfs4_renewdata *data;
4586 if (renew_flags == 0)
4588 if (!atomic_inc_not_zero(&clp->cl_count))
4590 data = kmalloc(sizeof(*data), GFP_NOFS);
4594 data->timestamp = jiffies;
4595 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
4596 &nfs4_renew_ops, data);
4599 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
4601 struct rpc_message msg = {
4602 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4606 unsigned long now = jiffies;
4609 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4612 do_renew_lease(clp, now);
4616 static inline int nfs4_server_supports_acls(struct nfs_server *server)
4618 return server->caps & NFS_CAP_ACLS;
4621 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4622 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
4625 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
4627 static int buf_to_pages_noslab(const void *buf, size_t buflen,
4628 struct page **pages)
4630 struct page *newpage, **spages;
4636 len = min_t(size_t, PAGE_SIZE, buflen);
4637 newpage = alloc_page(GFP_KERNEL);
4639 if (newpage == NULL)
4641 memcpy(page_address(newpage), buf, len);
4646 } while (buflen != 0);
4652 __free_page(spages[rc-1]);
4656 struct nfs4_cached_acl {
4662 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
4664 struct nfs_inode *nfsi = NFS_I(inode);
4666 spin_lock(&inode->i_lock);
4667 kfree(nfsi->nfs4_acl);
4668 nfsi->nfs4_acl = acl;
4669 spin_unlock(&inode->i_lock);
4672 static void nfs4_zap_acl_attr(struct inode *inode)
4674 nfs4_set_cached_acl(inode, NULL);
4677 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4679 struct nfs_inode *nfsi = NFS_I(inode);
4680 struct nfs4_cached_acl *acl;
4683 spin_lock(&inode->i_lock);
4684 acl = nfsi->nfs4_acl;
4687 if (buf == NULL) /* user is just asking for length */
4689 if (acl->cached == 0)
4691 ret = -ERANGE; /* see getxattr(2) man page */
4692 if (acl->len > buflen)
4694 memcpy(buf, acl->data, acl->len);
4698 spin_unlock(&inode->i_lock);
4702 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
4704 struct nfs4_cached_acl *acl;
4705 size_t buflen = sizeof(*acl) + acl_len;
4707 if (buflen <= PAGE_SIZE) {
4708 acl = kmalloc(buflen, GFP_KERNEL);
4712 _copy_from_pages(acl->data, pages, pgbase, acl_len);
4714 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4721 nfs4_set_cached_acl(inode, acl);
4725 * The getxattr API returns the required buffer length when called with a
4726 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4727 * the required buf. On a NULL buf, we send a page of data to the server
4728 * guessing that the ACL request can be serviced by a page. If so, we cache
4729 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4730 * the cache. If not so, we throw away the page, and cache the required
4731 * length. The next getxattr call will then produce another round trip to
4732 * the server, this time with the input buf of the required size.
4734 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4736 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
4737 struct nfs_getaclargs args = {
4738 .fh = NFS_FH(inode),
4742 struct nfs_getaclres res = {
4745 struct rpc_message msg = {
4746 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4750 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4751 int ret = -ENOMEM, i;
4753 /* As long as we're doing a round trip to the server anyway,
4754 * let's be prepared for a page of acl data. */
4757 if (npages > ARRAY_SIZE(pages))
4760 for (i = 0; i < npages; i++) {
4761 pages[i] = alloc_page(GFP_KERNEL);
4766 /* for decoding across pages */
4767 res.acl_scratch = alloc_page(GFP_KERNEL);
4768 if (!res.acl_scratch)
4771 args.acl_len = npages * PAGE_SIZE;
4773 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
4774 __func__, buf, buflen, npages, args.acl_len);
4775 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4776 &msg, &args.seq_args, &res.seq_res, 0);
4780 /* Handle the case where the passed-in buffer is too short */
4781 if (res.acl_flags & NFS4_ACL_TRUNC) {
4782 /* Did the user only issue a request for the acl length? */
4788 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
4790 if (res.acl_len > buflen) {
4794 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
4799 for (i = 0; i < npages; i++)
4801 __free_page(pages[i]);
4802 if (res.acl_scratch)
4803 __free_page(res.acl_scratch);
4807 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4809 struct nfs4_exception exception = { };
4812 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
4813 trace_nfs4_get_acl(inode, ret);
4816 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4817 } while (exception.retry);
4821 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4823 struct nfs_server *server = NFS_SERVER(inode);
4826 if (!nfs4_server_supports_acls(server))
4828 ret = nfs_revalidate_inode(server, inode);
4831 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4832 nfs_zap_acl_cache(inode);
4833 ret = nfs4_read_cached_acl(inode, buf, buflen);
4835 /* -ENOENT is returned if there is no ACL or if there is an ACL
4836 * but no cached acl data, just the acl length */
4838 return nfs4_get_acl_uncached(inode, buf, buflen);
4841 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4843 struct nfs_server *server = NFS_SERVER(inode);
4844 struct page *pages[NFS4ACL_MAXPAGES];
4845 struct nfs_setaclargs arg = {
4846 .fh = NFS_FH(inode),
4850 struct nfs_setaclres res;
4851 struct rpc_message msg = {
4852 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4856 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4859 if (!nfs4_server_supports_acls(server))
4861 if (npages > ARRAY_SIZE(pages))
4863 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
4866 nfs4_inode_return_delegation(inode);
4867 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4870 * Free each page after tx, so the only ref left is
4871 * held by the network stack
4874 put_page(pages[i-1]);
4877 * Acl update can result in inode attribute update.
4878 * so mark the attribute cache invalid.
4880 spin_lock(&inode->i_lock);
4881 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4882 spin_unlock(&inode->i_lock);
4883 nfs_access_zap_cache(inode);
4884 nfs_zap_acl_cache(inode);
4888 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4890 struct nfs4_exception exception = { };
4893 err = __nfs4_proc_set_acl(inode, buf, buflen);
4894 trace_nfs4_set_acl(inode, err);
4895 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4897 } while (exception.retry);
4901 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
4902 static int _nfs4_get_security_label(struct inode *inode, void *buf,
4905 struct nfs_server *server = NFS_SERVER(inode);
4906 struct nfs_fattr fattr;
4907 struct nfs4_label label = {0, 0, buflen, buf};
4909 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4910 struct nfs4_getattr_arg arg = {
4911 .fh = NFS_FH(inode),
4914 struct nfs4_getattr_res res = {
4919 struct rpc_message msg = {
4920 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4926 nfs_fattr_init(&fattr);
4928 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
4931 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
4933 if (buflen < label.len)
4938 static int nfs4_get_security_label(struct inode *inode, void *buf,
4941 struct nfs4_exception exception = { };
4944 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4948 err = _nfs4_get_security_label(inode, buf, buflen);
4949 trace_nfs4_get_security_label(inode, err);
4950 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4952 } while (exception.retry);
4956 static int _nfs4_do_set_security_label(struct inode *inode,
4957 struct nfs4_label *ilabel,
4958 struct nfs_fattr *fattr,
4959 struct nfs4_label *olabel)
4962 struct iattr sattr = {0};
4963 struct nfs_server *server = NFS_SERVER(inode);
4964 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4965 struct nfs_setattrargs arg = {
4966 .fh = NFS_FH(inode),
4972 struct nfs_setattrres res = {
4977 struct rpc_message msg = {
4978 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
4984 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
4986 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4988 dprintk("%s failed: %d\n", __func__, status);
4993 static int nfs4_do_set_security_label(struct inode *inode,
4994 struct nfs4_label *ilabel,
4995 struct nfs_fattr *fattr,
4996 struct nfs4_label *olabel)
4998 struct nfs4_exception exception = { };
5002 err = _nfs4_do_set_security_label(inode, ilabel,
5004 trace_nfs4_set_security_label(inode, err);
5005 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5007 } while (exception.retry);
5012 nfs4_set_security_label(struct dentry *dentry, const void *buf, size_t buflen)
5014 struct nfs4_label ilabel, *olabel = NULL;
5015 struct nfs_fattr fattr;
5016 struct rpc_cred *cred;
5017 struct inode *inode = d_inode(dentry);
5020 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5023 nfs_fattr_init(&fattr);
5027 ilabel.label = (char *)buf;
5028 ilabel.len = buflen;
5030 cred = rpc_lookup_cred();
5032 return PTR_ERR(cred);
5034 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5035 if (IS_ERR(olabel)) {
5036 status = -PTR_ERR(olabel);
5040 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5042 nfs_setsecurity(inode, &fattr, olabel);
5044 nfs4_label_free(olabel);
5049 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5052 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5053 nfs4_verifier *bootverf)
5057 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5058 /* An impossible timestamp guarantees this value
5059 * will never match a generated boot time. */
5061 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
5063 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
5064 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
5065 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
5067 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5071 nfs4_init_nonuniform_client_string(struct nfs_client *clp)
5076 if (clp->cl_owner_id != NULL)
5080 len = 14 + strlen(clp->cl_ipaddr) + 1 +
5081 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5083 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5087 if (len > NFS4_OPAQUE_LIMIT + 1)
5091 * Since this string is allocated at mount time, and held until the
5092 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5093 * about a memory-reclaim deadlock.
5095 str = kmalloc(len, GFP_KERNEL);
5100 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
5102 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5103 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
5106 clp->cl_owner_id = str;
5111 nfs4_init_uniquifier_client_string(struct nfs_client *clp)
5116 len = 10 + 10 + 1 + 10 + 1 +
5117 strlen(nfs4_client_id_uniquifier) + 1 +
5118 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5120 if (len > NFS4_OPAQUE_LIMIT + 1)
5124 * Since this string is allocated at mount time, and held until the
5125 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5126 * about a memory-reclaim deadlock.
5128 str = kmalloc(len, GFP_KERNEL);
5132 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
5133 clp->rpc_ops->version, clp->cl_minorversion,
5134 nfs4_client_id_uniquifier,
5135 clp->cl_rpcclient->cl_nodename);
5136 clp->cl_owner_id = str;
5141 nfs4_init_uniform_client_string(struct nfs_client *clp)
5146 if (clp->cl_owner_id != NULL)
5149 if (nfs4_client_id_uniquifier[0] != '\0')
5150 return nfs4_init_uniquifier_client_string(clp);
5152 len = 10 + 10 + 1 + 10 + 1 +
5153 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5155 if (len > NFS4_OPAQUE_LIMIT + 1)
5159 * Since this string is allocated at mount time, and held until the
5160 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5161 * about a memory-reclaim deadlock.
5163 str = kmalloc(len, GFP_KERNEL);
5167 scnprintf(str, len, "Linux NFSv%u.%u %s",
5168 clp->rpc_ops->version, clp->cl_minorversion,
5169 clp->cl_rpcclient->cl_nodename);
5170 clp->cl_owner_id = str;
5175 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5176 * services. Advertise one based on the address family of the
5180 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5182 if (strchr(clp->cl_ipaddr, ':') != NULL)
5183 return scnprintf(buf, len, "tcp6");
5185 return scnprintf(buf, len, "tcp");
5188 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5190 struct nfs4_setclientid *sc = calldata;
5192 if (task->tk_status == 0)
5193 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5196 static const struct rpc_call_ops nfs4_setclientid_ops = {
5197 .rpc_call_done = nfs4_setclientid_done,
5201 * nfs4_proc_setclientid - Negotiate client ID
5202 * @clp: state data structure
5203 * @program: RPC program for NFSv4 callback service
5204 * @port: IP port number for NFS4 callback service
5205 * @cred: RPC credential to use for this call
5206 * @res: where to place the result
5208 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5210 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5211 unsigned short port, struct rpc_cred *cred,
5212 struct nfs4_setclientid_res *res)
5214 nfs4_verifier sc_verifier;
5215 struct nfs4_setclientid setclientid = {
5216 .sc_verifier = &sc_verifier,
5220 struct rpc_message msg = {
5221 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5222 .rpc_argp = &setclientid,
5226 struct rpc_task *task;
5227 struct rpc_task_setup task_setup_data = {
5228 .rpc_client = clp->cl_rpcclient,
5229 .rpc_message = &msg,
5230 .callback_ops = &nfs4_setclientid_ops,
5231 .callback_data = &setclientid,
5232 .flags = RPC_TASK_TIMEOUT,
5236 /* nfs_client_id4 */
5237 nfs4_init_boot_verifier(clp, &sc_verifier);
5239 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5240 status = nfs4_init_uniform_client_string(clp);
5242 status = nfs4_init_nonuniform_client_string(clp);
5248 setclientid.sc_netid_len =
5249 nfs4_init_callback_netid(clp,
5250 setclientid.sc_netid,
5251 sizeof(setclientid.sc_netid));
5252 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
5253 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
5254 clp->cl_ipaddr, port >> 8, port & 255);
5256 dprintk("NFS call setclientid auth=%s, '%s'\n",
5257 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5259 task = rpc_run_task(&task_setup_data);
5261 status = PTR_ERR(task);
5264 status = task->tk_status;
5265 if (setclientid.sc_cred) {
5266 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5267 put_rpccred(setclientid.sc_cred);
5271 trace_nfs4_setclientid(clp, status);
5272 dprintk("NFS reply setclientid: %d\n", status);
5277 * nfs4_proc_setclientid_confirm - Confirm client ID
5278 * @clp: state data structure
5279 * @res: result of a previous SETCLIENTID
5280 * @cred: RPC credential to use for this call
5282 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5284 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
5285 struct nfs4_setclientid_res *arg,
5286 struct rpc_cred *cred)
5288 struct rpc_message msg = {
5289 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
5295 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5296 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5298 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5299 trace_nfs4_setclientid_confirm(clp, status);
5300 dprintk("NFS reply setclientid_confirm: %d\n", status);
5304 struct nfs4_delegreturndata {
5305 struct nfs4_delegreturnargs args;
5306 struct nfs4_delegreturnres res;
5308 nfs4_stateid stateid;
5309 unsigned long timestamp;
5310 struct nfs_fattr fattr;
5312 struct inode *inode;
5317 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5319 struct nfs4_delegreturndata *data = calldata;
5321 if (!nfs4_sequence_done(task, &data->res.seq_res))
5324 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
5325 switch (task->tk_status) {
5327 renew_lease(data->res.server, data->timestamp);
5328 case -NFS4ERR_ADMIN_REVOKED:
5329 case -NFS4ERR_DELEG_REVOKED:
5330 case -NFS4ERR_BAD_STATEID:
5331 case -NFS4ERR_OLD_STATEID:
5332 case -NFS4ERR_STALE_STATEID:
5333 case -NFS4ERR_EXPIRED:
5334 task->tk_status = 0;
5336 pnfs_roc_set_barrier(data->inode, data->roc_barrier);
5339 if (nfs4_async_handle_error(task, data->res.server,
5340 NULL, NULL) == -EAGAIN) {
5341 rpc_restart_call_prepare(task);
5345 data->rpc_status = task->tk_status;
5348 static void nfs4_delegreturn_release(void *calldata)
5350 struct nfs4_delegreturndata *data = calldata;
5351 struct inode *inode = data->inode;
5355 pnfs_roc_release(inode);
5356 nfs_iput_and_deactive(inode);
5361 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5363 struct nfs4_delegreturndata *d_data;
5365 d_data = (struct nfs4_delegreturndata *)data;
5367 if (nfs4_wait_on_layoutreturn(d_data->inode, task))
5371 pnfs_roc_get_barrier(d_data->inode, &d_data->roc_barrier);
5373 nfs4_setup_sequence(d_data->res.server,
5374 &d_data->args.seq_args,
5375 &d_data->res.seq_res,
5379 static const struct rpc_call_ops nfs4_delegreturn_ops = {
5380 .rpc_call_prepare = nfs4_delegreturn_prepare,
5381 .rpc_call_done = nfs4_delegreturn_done,
5382 .rpc_release = nfs4_delegreturn_release,
5385 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
5387 struct nfs4_delegreturndata *data;
5388 struct nfs_server *server = NFS_SERVER(inode);
5389 struct rpc_task *task;
5390 struct rpc_message msg = {
5391 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5394 struct rpc_task_setup task_setup_data = {
5395 .rpc_client = server->client,
5396 .rpc_message = &msg,
5397 .callback_ops = &nfs4_delegreturn_ops,
5398 .flags = RPC_TASK_ASYNC,
5402 data = kzalloc(sizeof(*data), GFP_NOFS);
5405 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
5407 nfs4_state_protect(server->nfs_client,
5408 NFS_SP4_MACH_CRED_CLEANUP,
5409 &task_setup_data.rpc_client, &msg);
5411 data->args.fhandle = &data->fh;
5412 data->args.stateid = &data->stateid;
5413 data->args.bitmask = server->cache_consistency_bitmask;
5414 nfs_copy_fh(&data->fh, NFS_FH(inode));
5415 nfs4_stateid_copy(&data->stateid, stateid);
5416 data->res.fattr = &data->fattr;
5417 data->res.server = server;
5418 nfs_fattr_init(data->res.fattr);
5419 data->timestamp = jiffies;
5420 data->rpc_status = 0;
5421 data->inode = nfs_igrab_and_active(inode);
5423 data->roc = nfs4_roc(inode);
5425 task_setup_data.callback_data = data;
5426 msg.rpc_argp = &data->args;
5427 msg.rpc_resp = &data->res;
5428 task = rpc_run_task(&task_setup_data);
5430 return PTR_ERR(task);
5433 status = nfs4_wait_for_completion_rpc_task(task);
5436 status = data->rpc_status;
5438 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5440 nfs_refresh_inode(inode, &data->fattr);
5446 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
5448 struct nfs_server *server = NFS_SERVER(inode);
5449 struct nfs4_exception exception = { };
5452 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
5453 trace_nfs4_delegreturn(inode, stateid, err);
5455 case -NFS4ERR_STALE_STATEID:
5456 case -NFS4ERR_EXPIRED:
5460 err = nfs4_handle_exception(server, err, &exception);
5461 } while (exception.retry);
5465 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
5466 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
5469 * sleep, with exponential backoff, and retry the LOCK operation.
5471 static unsigned long
5472 nfs4_set_lock_task_retry(unsigned long timeout)
5474 freezable_schedule_timeout_killable_unsafe(timeout);
5476 if (timeout > NFS4_LOCK_MAXTIMEOUT)
5477 return NFS4_LOCK_MAXTIMEOUT;
5481 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5483 struct inode *inode = state->inode;
5484 struct nfs_server *server = NFS_SERVER(inode);
5485 struct nfs_client *clp = server->nfs_client;
5486 struct nfs_lockt_args arg = {
5487 .fh = NFS_FH(inode),
5490 struct nfs_lockt_res res = {
5493 struct rpc_message msg = {
5494 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5497 .rpc_cred = state->owner->so_cred,
5499 struct nfs4_lock_state *lsp;
5502 arg.lock_owner.clientid = clp->cl_clientid;
5503 status = nfs4_set_lock_state(state, request);
5506 lsp = request->fl_u.nfs4_fl.owner;
5507 arg.lock_owner.id = lsp->ls_seqid.owner_id;
5508 arg.lock_owner.s_dev = server->s_dev;
5509 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5512 request->fl_type = F_UNLCK;
5514 case -NFS4ERR_DENIED:
5517 request->fl_ops->fl_release_private(request);
5518 request->fl_ops = NULL;
5523 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5525 struct nfs4_exception exception = { };
5529 err = _nfs4_proc_getlk(state, cmd, request);
5530 trace_nfs4_get_lock(request, state, cmd, err);
5531 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
5533 } while (exception.retry);
5537 static int do_vfs_lock(struct inode *inode, struct file_lock *fl)
5539 return locks_lock_inode_wait(inode, fl);
5542 struct nfs4_unlockdata {
5543 struct nfs_locku_args arg;
5544 struct nfs_locku_res res;
5545 struct nfs4_lock_state *lsp;
5546 struct nfs_open_context *ctx;
5547 struct file_lock fl;
5548 struct nfs_server *server;
5549 unsigned long timestamp;
5552 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5553 struct nfs_open_context *ctx,
5554 struct nfs4_lock_state *lsp,
5555 struct nfs_seqid *seqid)
5557 struct nfs4_unlockdata *p;
5558 struct inode *inode = lsp->ls_state->inode;
5560 p = kzalloc(sizeof(*p), GFP_NOFS);
5563 p->arg.fh = NFS_FH(inode);
5565 p->arg.seqid = seqid;
5566 p->res.seqid = seqid;
5568 atomic_inc(&lsp->ls_count);
5569 /* Ensure we don't close file until we're done freeing locks! */
5570 p->ctx = get_nfs_open_context(ctx);
5571 memcpy(&p->fl, fl, sizeof(p->fl));
5572 p->server = NFS_SERVER(inode);
5576 static void nfs4_locku_release_calldata(void *data)
5578 struct nfs4_unlockdata *calldata = data;
5579 nfs_free_seqid(calldata->arg.seqid);
5580 nfs4_put_lock_state(calldata->lsp);
5581 put_nfs_open_context(calldata->ctx);
5585 static void nfs4_locku_done(struct rpc_task *task, void *data)
5587 struct nfs4_unlockdata *calldata = data;
5589 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5591 switch (task->tk_status) {
5593 renew_lease(calldata->server, calldata->timestamp);
5594 do_vfs_lock(calldata->lsp->ls_state->inode, &calldata->fl);
5595 if (nfs4_update_lock_stateid(calldata->lsp,
5596 &calldata->res.stateid))
5598 case -NFS4ERR_BAD_STATEID:
5599 case -NFS4ERR_OLD_STATEID:
5600 case -NFS4ERR_STALE_STATEID:
5601 case -NFS4ERR_EXPIRED:
5602 if (!nfs4_stateid_match(&calldata->arg.stateid,
5603 &calldata->lsp->ls_stateid))
5604 rpc_restart_call_prepare(task);
5607 if (nfs4_async_handle_error(task, calldata->server,
5608 NULL, NULL) == -EAGAIN)
5609 rpc_restart_call_prepare(task);
5611 nfs_release_seqid(calldata->arg.seqid);
5614 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
5616 struct nfs4_unlockdata *calldata = data;
5618 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
5620 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
5621 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
5622 /* Note: exit _without_ running nfs4_locku_done */
5625 calldata->timestamp = jiffies;
5626 if (nfs4_setup_sequence(calldata->server,
5627 &calldata->arg.seq_args,
5628 &calldata->res.seq_res,
5630 nfs_release_seqid(calldata->arg.seqid);
5633 task->tk_action = NULL;
5635 nfs4_sequence_done(task, &calldata->res.seq_res);
5638 static const struct rpc_call_ops nfs4_locku_ops = {
5639 .rpc_call_prepare = nfs4_locku_prepare,
5640 .rpc_call_done = nfs4_locku_done,
5641 .rpc_release = nfs4_locku_release_calldata,
5644 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5645 struct nfs_open_context *ctx,
5646 struct nfs4_lock_state *lsp,
5647 struct nfs_seqid *seqid)
5649 struct nfs4_unlockdata *data;
5650 struct rpc_message msg = {
5651 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5652 .rpc_cred = ctx->cred,
5654 struct rpc_task_setup task_setup_data = {
5655 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
5656 .rpc_message = &msg,
5657 .callback_ops = &nfs4_locku_ops,
5658 .workqueue = nfsiod_workqueue,
5659 .flags = RPC_TASK_ASYNC,
5662 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5663 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5665 /* Ensure this is an unlock - when canceling a lock, the
5666 * canceled lock is passed in, and it won't be an unlock.
5668 fl->fl_type = F_UNLCK;
5670 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5672 nfs_free_seqid(seqid);
5673 return ERR_PTR(-ENOMEM);
5676 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
5677 msg.rpc_argp = &data->arg;
5678 msg.rpc_resp = &data->res;
5679 task_setup_data.callback_data = data;
5680 return rpc_run_task(&task_setup_data);
5683 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5685 struct inode *inode = state->inode;
5686 struct nfs4_state_owner *sp = state->owner;
5687 struct nfs_inode *nfsi = NFS_I(inode);
5688 struct nfs_seqid *seqid;
5689 struct nfs4_lock_state *lsp;
5690 struct rpc_task *task;
5691 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
5693 unsigned char fl_flags = request->fl_flags;
5695 status = nfs4_set_lock_state(state, request);
5696 /* Unlock _before_ we do the RPC call */
5697 request->fl_flags |= FL_EXISTS;
5698 /* Exclude nfs_delegation_claim_locks() */
5699 mutex_lock(&sp->so_delegreturn_mutex);
5700 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
5701 down_read(&nfsi->rwsem);
5702 if (do_vfs_lock(inode, request) == -ENOENT) {
5703 up_read(&nfsi->rwsem);
5704 mutex_unlock(&sp->so_delegreturn_mutex);
5707 up_read(&nfsi->rwsem);
5708 mutex_unlock(&sp->so_delegreturn_mutex);
5711 /* Is this a delegated lock? */
5712 lsp = request->fl_u.nfs4_fl.owner;
5713 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5715 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
5716 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
5720 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
5721 status = PTR_ERR(task);
5724 status = nfs4_wait_for_completion_rpc_task(task);
5727 request->fl_flags = fl_flags;
5728 trace_nfs4_unlock(request, state, F_SETLK, status);
5732 struct nfs4_lockdata {
5733 struct nfs_lock_args arg;
5734 struct nfs_lock_res res;
5735 struct nfs4_lock_state *lsp;
5736 struct nfs_open_context *ctx;
5737 struct file_lock fl;
5738 unsigned long timestamp;
5741 struct nfs_server *server;
5744 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
5745 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5748 struct nfs4_lockdata *p;
5749 struct inode *inode = lsp->ls_state->inode;
5750 struct nfs_server *server = NFS_SERVER(inode);
5751 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
5753 p = kzalloc(sizeof(*p), gfp_mask);
5757 p->arg.fh = NFS_FH(inode);
5759 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
5760 if (IS_ERR(p->arg.open_seqid))
5762 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
5763 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
5764 if (IS_ERR(p->arg.lock_seqid))
5765 goto out_free_seqid;
5766 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
5767 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
5768 p->arg.lock_owner.s_dev = server->s_dev;
5769 p->res.lock_seqid = p->arg.lock_seqid;
5772 atomic_inc(&lsp->ls_count);
5773 p->ctx = get_nfs_open_context(ctx);
5774 get_file(fl->fl_file);
5775 memcpy(&p->fl, fl, sizeof(p->fl));
5778 nfs_free_seqid(p->arg.open_seqid);
5784 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5786 struct nfs4_lockdata *data = calldata;
5787 struct nfs4_state *state = data->lsp->ls_state;
5789 dprintk("%s: begin!\n", __func__);
5790 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
5792 /* Do we need to do an open_to_lock_owner? */
5793 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
5794 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
5795 goto out_release_lock_seqid;
5797 nfs4_stateid_copy(&data->arg.open_stateid,
5798 &state->open_stateid);
5799 data->arg.new_lock_owner = 1;
5800 data->res.open_seqid = data->arg.open_seqid;
5802 data->arg.new_lock_owner = 0;
5803 nfs4_stateid_copy(&data->arg.lock_stateid,
5804 &data->lsp->ls_stateid);
5806 if (!nfs4_valid_open_stateid(state)) {
5807 data->rpc_status = -EBADF;
5808 task->tk_action = NULL;
5809 goto out_release_open_seqid;
5811 data->timestamp = jiffies;
5812 if (nfs4_setup_sequence(data->server,
5813 &data->arg.seq_args,
5817 out_release_open_seqid:
5818 nfs_release_seqid(data->arg.open_seqid);
5819 out_release_lock_seqid:
5820 nfs_release_seqid(data->arg.lock_seqid);
5822 nfs4_sequence_done(task, &data->res.seq_res);
5823 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
5826 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5828 struct nfs4_lockdata *data = calldata;
5829 struct nfs4_lock_state *lsp = data->lsp;
5831 dprintk("%s: begin!\n", __func__);
5833 if (!nfs4_sequence_done(task, &data->res.seq_res))
5836 data->rpc_status = task->tk_status;
5837 switch (task->tk_status) {
5839 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
5841 if (data->arg.new_lock) {
5842 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
5843 if (do_vfs_lock(lsp->ls_state->inode, &data->fl) < 0) {
5844 rpc_restart_call_prepare(task);
5848 if (data->arg.new_lock_owner != 0) {
5849 nfs_confirm_seqid(&lsp->ls_seqid, 0);
5850 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
5851 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
5852 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
5853 rpc_restart_call_prepare(task);
5855 case -NFS4ERR_BAD_STATEID:
5856 case -NFS4ERR_OLD_STATEID:
5857 case -NFS4ERR_STALE_STATEID:
5858 case -NFS4ERR_EXPIRED:
5859 if (data->arg.new_lock_owner != 0) {
5860 if (!nfs4_stateid_match(&data->arg.open_stateid,
5861 &lsp->ls_state->open_stateid))
5862 rpc_restart_call_prepare(task);
5863 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
5865 rpc_restart_call_prepare(task);
5867 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
5870 static void nfs4_lock_release(void *calldata)
5872 struct nfs4_lockdata *data = calldata;
5874 dprintk("%s: begin!\n", __func__);
5875 nfs_free_seqid(data->arg.open_seqid);
5876 if (data->cancelled != 0) {
5877 struct rpc_task *task;
5878 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5879 data->arg.lock_seqid);
5881 rpc_put_task_async(task);
5882 dprintk("%s: cancelling lock!\n", __func__);
5884 nfs_free_seqid(data->arg.lock_seqid);
5885 nfs4_put_lock_state(data->lsp);
5886 put_nfs_open_context(data->ctx);
5887 fput(data->fl.fl_file);
5889 dprintk("%s: done!\n", __func__);
5892 static const struct rpc_call_ops nfs4_lock_ops = {
5893 .rpc_call_prepare = nfs4_lock_prepare,
5894 .rpc_call_done = nfs4_lock_done,
5895 .rpc_release = nfs4_lock_release,
5898 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5901 case -NFS4ERR_ADMIN_REVOKED:
5902 case -NFS4ERR_BAD_STATEID:
5903 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
5904 if (new_lock_owner != 0 ||
5905 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
5906 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
5908 case -NFS4ERR_STALE_STATEID:
5909 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
5910 case -NFS4ERR_EXPIRED:
5911 nfs4_schedule_lease_recovery(server->nfs_client);
5915 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
5917 struct nfs4_lockdata *data;
5918 struct rpc_task *task;
5919 struct rpc_message msg = {
5920 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5921 .rpc_cred = state->owner->so_cred,
5923 struct rpc_task_setup task_setup_data = {
5924 .rpc_client = NFS_CLIENT(state->inode),
5925 .rpc_message = &msg,
5926 .callback_ops = &nfs4_lock_ops,
5927 .workqueue = nfsiod_workqueue,
5928 .flags = RPC_TASK_ASYNC,
5932 dprintk("%s: begin!\n", __func__);
5933 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
5934 fl->fl_u.nfs4_fl.owner,
5935 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
5939 data->arg.block = 1;
5940 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
5941 msg.rpc_argp = &data->arg;
5942 msg.rpc_resp = &data->res;
5943 task_setup_data.callback_data = data;
5944 if (recovery_type > NFS_LOCK_NEW) {
5945 if (recovery_type == NFS_LOCK_RECLAIM)
5946 data->arg.reclaim = NFS_LOCK_RECLAIM;
5947 nfs4_set_sequence_privileged(&data->arg.seq_args);
5949 data->arg.new_lock = 1;
5950 task = rpc_run_task(&task_setup_data);
5952 return PTR_ERR(task);
5953 ret = nfs4_wait_for_completion_rpc_task(task);
5955 ret = data->rpc_status;
5957 nfs4_handle_setlk_error(data->server, data->lsp,
5958 data->arg.new_lock_owner, ret);
5960 data->cancelled = 1;
5962 dprintk("%s: done, ret = %d!\n", __func__, ret);
5963 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
5967 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
5969 struct nfs_server *server = NFS_SERVER(state->inode);
5970 struct nfs4_exception exception = {
5971 .inode = state->inode,
5976 /* Cache the lock if possible... */
5977 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5979 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
5980 if (err != -NFS4ERR_DELAY)
5982 nfs4_handle_exception(server, err, &exception);
5983 } while (exception.retry);
5987 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
5989 struct nfs_server *server = NFS_SERVER(state->inode);
5990 struct nfs4_exception exception = {
5991 .inode = state->inode,
5995 err = nfs4_set_lock_state(state, request);
5998 if (!recover_lost_locks) {
5999 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6003 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6005 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
6009 case -NFS4ERR_GRACE:
6010 case -NFS4ERR_DELAY:
6011 nfs4_handle_exception(server, err, &exception);
6014 } while (exception.retry);
6019 #if defined(CONFIG_NFS_V4_1)
6021 * nfs41_check_expired_locks - possibly free a lock stateid
6023 * @state: NFSv4 state for an inode
6025 * Returns NFS_OK if recovery for this stateid is now finished.
6026 * Otherwise a negative NFS4ERR value is returned.
6028 static int nfs41_check_expired_locks(struct nfs4_state *state)
6030 int status, ret = -NFS4ERR_BAD_STATEID;
6031 struct nfs4_lock_state *lsp;
6032 struct nfs_server *server = NFS_SERVER(state->inode);
6034 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
6035 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
6036 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
6038 status = nfs41_test_stateid(server,
6041 trace_nfs4_test_lock_stateid(state, lsp, status);
6042 if (status != NFS_OK) {
6043 /* Free the stateid unless the server
6044 * informs us the stateid is unrecognized. */
6045 if (status != -NFS4ERR_BAD_STATEID)
6046 nfs41_free_stateid(server,
6049 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6058 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6060 int status = NFS_OK;
6062 if (test_bit(LK_STATE_IN_USE, &state->flags))
6063 status = nfs41_check_expired_locks(state);
6064 if (status != NFS_OK)
6065 status = nfs4_lock_expired(state, request);
6070 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6072 struct nfs_inode *nfsi = NFS_I(state->inode);
6073 struct nfs4_state_owner *sp = state->owner;
6074 unsigned char fl_flags = request->fl_flags;
6075 int status = -ENOLCK;
6077 if ((fl_flags & FL_POSIX) &&
6078 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6080 /* Is this a delegated open? */
6081 status = nfs4_set_lock_state(state, request);
6084 request->fl_flags |= FL_ACCESS;
6085 status = do_vfs_lock(state->inode, request);
6088 mutex_lock(&sp->so_delegreturn_mutex);
6089 down_read(&nfsi->rwsem);
6090 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
6091 /* Yes: cache locks! */
6092 /* ...but avoid races with delegation recall... */
6093 request->fl_flags = fl_flags & ~FL_SLEEP;
6094 status = do_vfs_lock(state->inode, request);
6095 up_read(&nfsi->rwsem);
6096 mutex_unlock(&sp->so_delegreturn_mutex);
6099 up_read(&nfsi->rwsem);
6100 mutex_unlock(&sp->so_delegreturn_mutex);
6101 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
6103 request->fl_flags = fl_flags;
6107 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6109 struct nfs4_exception exception = {
6111 .inode = state->inode,
6116 err = _nfs4_proc_setlk(state, cmd, request);
6117 if (err == -NFS4ERR_DENIED)
6119 err = nfs4_handle_exception(NFS_SERVER(state->inode),
6121 } while (exception.retry);
6126 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6128 struct nfs_open_context *ctx;
6129 struct nfs4_state *state;
6130 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6133 /* verify open state */
6134 ctx = nfs_file_open_context(filp);
6137 if (request->fl_start < 0 || request->fl_end < 0)
6140 if (IS_GETLK(cmd)) {
6142 return nfs4_proc_getlk(state, F_GETLK, request);
6146 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6149 if (request->fl_type == F_UNLCK) {
6151 return nfs4_proc_unlck(state, cmd, request);
6158 * Don't rely on the VFS having checked the file open mode,
6159 * since it won't do this for flock() locks.
6161 switch (request->fl_type) {
6163 if (!(filp->f_mode & FMODE_READ))
6167 if (!(filp->f_mode & FMODE_WRITE))
6172 status = nfs4_proc_setlk(state, cmd, request);
6173 if ((status != -EAGAIN) || IS_SETLK(cmd))
6175 timeout = nfs4_set_lock_task_retry(timeout);
6176 status = -ERESTARTSYS;
6179 } while(status < 0);
6183 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
6185 struct nfs_server *server = NFS_SERVER(state->inode);
6188 err = nfs4_set_lock_state(state, fl);
6191 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
6192 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
6195 struct nfs_release_lockowner_data {
6196 struct nfs4_lock_state *lsp;
6197 struct nfs_server *server;
6198 struct nfs_release_lockowner_args args;
6199 struct nfs_release_lockowner_res res;
6200 unsigned long timestamp;
6203 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6205 struct nfs_release_lockowner_data *data = calldata;
6206 struct nfs_server *server = data->server;
6207 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
6208 &data->args.seq_args, &data->res.seq_res, task);
6209 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6210 data->timestamp = jiffies;
6213 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6215 struct nfs_release_lockowner_data *data = calldata;
6216 struct nfs_server *server = data->server;
6218 nfs40_sequence_done(task, &data->res.seq_res);
6220 switch (task->tk_status) {
6222 renew_lease(server, data->timestamp);
6224 case -NFS4ERR_STALE_CLIENTID:
6225 case -NFS4ERR_EXPIRED:
6226 nfs4_schedule_lease_recovery(server->nfs_client);
6228 case -NFS4ERR_LEASE_MOVED:
6229 case -NFS4ERR_DELAY:
6230 if (nfs4_async_handle_error(task, server,
6231 NULL, NULL) == -EAGAIN)
6232 rpc_restart_call_prepare(task);
6236 static void nfs4_release_lockowner_release(void *calldata)
6238 struct nfs_release_lockowner_data *data = calldata;
6239 nfs4_free_lock_state(data->server, data->lsp);
6243 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
6244 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6245 .rpc_call_done = nfs4_release_lockowner_done,
6246 .rpc_release = nfs4_release_lockowner_release,
6250 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
6252 struct nfs_release_lockowner_data *data;
6253 struct rpc_message msg = {
6254 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6257 if (server->nfs_client->cl_mvops->minor_version != 0)
6260 data = kmalloc(sizeof(*data), GFP_NOFS);
6264 data->server = server;
6265 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6266 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6267 data->args.lock_owner.s_dev = server->s_dev;
6269 msg.rpc_argp = &data->args;
6270 msg.rpc_resp = &data->res;
6271 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
6272 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
6275 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6277 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
6278 struct dentry *dentry, const char *key,
6279 const void *buf, size_t buflen,
6282 return nfs4_proc_set_acl(d_inode(dentry), buf, buflen);
6285 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
6286 struct dentry *dentry, const char *key,
6287 void *buf, size_t buflen)
6289 return nfs4_proc_get_acl(d_inode(dentry), buf, buflen);
6292 static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
6294 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
6297 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
6299 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
6300 struct dentry *dentry, const char *key,
6301 const void *buf, size_t buflen,
6304 if (security_ismaclabel(key))
6305 return nfs4_set_security_label(dentry, buf, buflen);
6310 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
6311 struct dentry *dentry, const char *key,
6312 void *buf, size_t buflen)
6314 if (security_ismaclabel(key))
6315 return nfs4_get_security_label(d_inode(dentry), buf, buflen);
6320 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6324 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6325 len = security_inode_listsecurity(inode, list, list_len);
6326 if (list_len && len > list_len)
6332 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6333 .prefix = XATTR_SECURITY_PREFIX,
6334 .get = nfs4_xattr_get_nfs4_label,
6335 .set = nfs4_xattr_set_nfs4_label,
6341 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6349 * nfs_fhget will use either the mounted_on_fileid or the fileid
6351 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6353 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6354 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6355 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
6356 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
6359 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
6360 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
6361 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6365 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6366 const struct qstr *name,
6367 struct nfs4_fs_locations *fs_locations,
6370 struct nfs_server *server = NFS_SERVER(dir);
6372 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6374 struct nfs4_fs_locations_arg args = {
6375 .dir_fh = NFS_FH(dir),
6380 struct nfs4_fs_locations_res res = {
6381 .fs_locations = fs_locations,
6383 struct rpc_message msg = {
6384 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6390 dprintk("%s: start\n", __func__);
6392 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6393 * is not supported */
6394 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6395 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6397 bitmask[0] |= FATTR4_WORD0_FILEID;
6399 nfs_fattr_init(&fs_locations->fattr);
6400 fs_locations->server = server;
6401 fs_locations->nlocations = 0;
6402 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
6403 dprintk("%s: returned status = %d\n", __func__, status);
6407 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6408 const struct qstr *name,
6409 struct nfs4_fs_locations *fs_locations,
6412 struct nfs4_exception exception = { };
6415 err = _nfs4_proc_fs_locations(client, dir, name,
6416 fs_locations, page);
6417 trace_nfs4_get_fs_locations(dir, name, err);
6418 err = nfs4_handle_exception(NFS_SERVER(dir), err,
6420 } while (exception.retry);
6425 * This operation also signals the server that this client is
6426 * performing migration recovery. The server can stop returning
6427 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6428 * appended to this compound to identify the client ID which is
6429 * performing recovery.
6431 static int _nfs40_proc_get_locations(struct inode *inode,
6432 struct nfs4_fs_locations *locations,
6433 struct page *page, struct rpc_cred *cred)
6435 struct nfs_server *server = NFS_SERVER(inode);
6436 struct rpc_clnt *clnt = server->client;
6438 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6440 struct nfs4_fs_locations_arg args = {
6441 .clientid = server->nfs_client->cl_clientid,
6442 .fh = NFS_FH(inode),
6445 .migration = 1, /* skip LOOKUP */
6446 .renew = 1, /* append RENEW */
6448 struct nfs4_fs_locations_res res = {
6449 .fs_locations = locations,
6453 struct rpc_message msg = {
6454 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6459 unsigned long now = jiffies;
6462 nfs_fattr_init(&locations->fattr);
6463 locations->server = server;
6464 locations->nlocations = 0;
6466 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6467 nfs4_set_sequence_privileged(&args.seq_args);
6468 status = nfs4_call_sync_sequence(clnt, server, &msg,
6469 &args.seq_args, &res.seq_res);
6473 renew_lease(server, now);
6477 #ifdef CONFIG_NFS_V4_1
6480 * This operation also signals the server that this client is
6481 * performing migration recovery. The server can stop asserting
6482 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6483 * performing this operation is identified in the SEQUENCE
6484 * operation in this compound.
6486 * When the client supports GETATTR(fs_locations_info), it can
6487 * be plumbed in here.
6489 static int _nfs41_proc_get_locations(struct inode *inode,
6490 struct nfs4_fs_locations *locations,
6491 struct page *page, struct rpc_cred *cred)
6493 struct nfs_server *server = NFS_SERVER(inode);
6494 struct rpc_clnt *clnt = server->client;
6496 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6498 struct nfs4_fs_locations_arg args = {
6499 .fh = NFS_FH(inode),
6502 .migration = 1, /* skip LOOKUP */
6504 struct nfs4_fs_locations_res res = {
6505 .fs_locations = locations,
6508 struct rpc_message msg = {
6509 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6516 nfs_fattr_init(&locations->fattr);
6517 locations->server = server;
6518 locations->nlocations = 0;
6520 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6521 nfs4_set_sequence_privileged(&args.seq_args);
6522 status = nfs4_call_sync_sequence(clnt, server, &msg,
6523 &args.seq_args, &res.seq_res);
6524 if (status == NFS4_OK &&
6525 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6526 status = -NFS4ERR_LEASE_MOVED;
6530 #endif /* CONFIG_NFS_V4_1 */
6533 * nfs4_proc_get_locations - discover locations for a migrated FSID
6534 * @inode: inode on FSID that is migrating
6535 * @locations: result of query
6537 * @cred: credential to use for this operation
6539 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6540 * operation failed, or a negative errno if a local error occurred.
6542 * On success, "locations" is filled in, but if the server has
6543 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6546 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6547 * from this client that require migration recovery.
6549 int nfs4_proc_get_locations(struct inode *inode,
6550 struct nfs4_fs_locations *locations,
6551 struct page *page, struct rpc_cred *cred)
6553 struct nfs_server *server = NFS_SERVER(inode);
6554 struct nfs_client *clp = server->nfs_client;
6555 const struct nfs4_mig_recovery_ops *ops =
6556 clp->cl_mvops->mig_recovery_ops;
6557 struct nfs4_exception exception = { };
6560 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6561 (unsigned long long)server->fsid.major,
6562 (unsigned long long)server->fsid.minor,
6564 nfs_display_fhandle(NFS_FH(inode), __func__);
6567 status = ops->get_locations(inode, locations, page, cred);
6568 if (status != -NFS4ERR_DELAY)
6570 nfs4_handle_exception(server, status, &exception);
6571 } while (exception.retry);
6576 * This operation also signals the server that this client is
6577 * performing "lease moved" recovery. The server can stop
6578 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6579 * is appended to this compound to identify the client ID which is
6580 * performing recovery.
6582 static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6584 struct nfs_server *server = NFS_SERVER(inode);
6585 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6586 struct rpc_clnt *clnt = server->client;
6587 struct nfs4_fsid_present_arg args = {
6588 .fh = NFS_FH(inode),
6589 .clientid = clp->cl_clientid,
6590 .renew = 1, /* append RENEW */
6592 struct nfs4_fsid_present_res res = {
6595 struct rpc_message msg = {
6596 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6601 unsigned long now = jiffies;
6604 res.fh = nfs_alloc_fhandle();
6608 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6609 nfs4_set_sequence_privileged(&args.seq_args);
6610 status = nfs4_call_sync_sequence(clnt, server, &msg,
6611 &args.seq_args, &res.seq_res);
6612 nfs_free_fhandle(res.fh);
6616 do_renew_lease(clp, now);
6620 #ifdef CONFIG_NFS_V4_1
6623 * This operation also signals the server that this client is
6624 * performing "lease moved" recovery. The server can stop asserting
6625 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6626 * this operation is identified in the SEQUENCE operation in this
6629 static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6631 struct nfs_server *server = NFS_SERVER(inode);
6632 struct rpc_clnt *clnt = server->client;
6633 struct nfs4_fsid_present_arg args = {
6634 .fh = NFS_FH(inode),
6636 struct nfs4_fsid_present_res res = {
6638 struct rpc_message msg = {
6639 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6646 res.fh = nfs_alloc_fhandle();
6650 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6651 nfs4_set_sequence_privileged(&args.seq_args);
6652 status = nfs4_call_sync_sequence(clnt, server, &msg,
6653 &args.seq_args, &res.seq_res);
6654 nfs_free_fhandle(res.fh);
6655 if (status == NFS4_OK &&
6656 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6657 status = -NFS4ERR_LEASE_MOVED;
6661 #endif /* CONFIG_NFS_V4_1 */
6664 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6665 * @inode: inode on FSID to check
6666 * @cred: credential to use for this operation
6668 * Server indicates whether the FSID is present, moved, or not
6669 * recognized. This operation is necessary to clear a LEASE_MOVED
6670 * condition for this client ID.
6672 * Returns NFS4_OK if the FSID is present on this server,
6673 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6674 * NFS4ERR code if some error occurred on the server, or a
6675 * negative errno if a local failure occurred.
6677 int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6679 struct nfs_server *server = NFS_SERVER(inode);
6680 struct nfs_client *clp = server->nfs_client;
6681 const struct nfs4_mig_recovery_ops *ops =
6682 clp->cl_mvops->mig_recovery_ops;
6683 struct nfs4_exception exception = { };
6686 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6687 (unsigned long long)server->fsid.major,
6688 (unsigned long long)server->fsid.minor,
6690 nfs_display_fhandle(NFS_FH(inode), __func__);
6693 status = ops->fsid_present(inode, cred);
6694 if (status != -NFS4ERR_DELAY)
6696 nfs4_handle_exception(server, status, &exception);
6697 } while (exception.retry);
6702 * If 'use_integrity' is true and the state managment nfs_client
6703 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
6704 * and the machine credential as per RFC3530bis and RFC5661 Security
6705 * Considerations sections. Otherwise, just use the user cred with the
6706 * filesystem's rpc_client.
6708 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
6711 struct nfs4_secinfo_arg args = {
6712 .dir_fh = NFS_FH(dir),
6715 struct nfs4_secinfo_res res = {
6718 struct rpc_message msg = {
6719 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
6723 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
6724 struct rpc_cred *cred = NULL;
6726 if (use_integrity) {
6727 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
6728 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
6729 msg.rpc_cred = cred;
6732 dprintk("NFS call secinfo %s\n", name->name);
6734 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
6735 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
6737 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
6739 dprintk("NFS reply secinfo: %d\n", status);
6747 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
6748 struct nfs4_secinfo_flavors *flavors)
6750 struct nfs4_exception exception = { };
6753 err = -NFS4ERR_WRONGSEC;
6755 /* try to use integrity protection with machine cred */
6756 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
6757 err = _nfs4_proc_secinfo(dir, name, flavors, true);
6760 * if unable to use integrity protection, or SECINFO with
6761 * integrity protection returns NFS4ERR_WRONGSEC (which is
6762 * disallowed by spec, but exists in deployed servers) use
6763 * the current filesystem's rpc_client and the user cred.
6765 if (err == -NFS4ERR_WRONGSEC)
6766 err = _nfs4_proc_secinfo(dir, name, flavors, false);
6768 trace_nfs4_secinfo(dir, name, err);
6769 err = nfs4_handle_exception(NFS_SERVER(dir), err,
6771 } while (exception.retry);
6775 #ifdef CONFIG_NFS_V4_1
6777 * Check the exchange flags returned by the server for invalid flags, having
6778 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
6781 static int nfs4_check_cl_exchange_flags(u32 flags)
6783 if (flags & ~EXCHGID4_FLAG_MASK_R)
6785 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
6786 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
6788 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
6792 return -NFS4ERR_INVAL;
6796 nfs41_same_server_scope(struct nfs41_server_scope *a,
6797 struct nfs41_server_scope *b)
6799 if (a->server_scope_sz == b->server_scope_sz &&
6800 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
6807 nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
6811 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
6812 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
6816 * nfs4_proc_bind_one_conn_to_session()
6818 * The 4.1 client currently uses the same TCP connection for the
6819 * fore and backchannel.
6822 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
6823 struct rpc_xprt *xprt,
6824 struct nfs_client *clp,
6825 struct rpc_cred *cred)
6828 struct nfs41_bind_conn_to_session_args args = {
6830 .dir = NFS4_CDFC4_FORE_OR_BOTH,
6832 struct nfs41_bind_conn_to_session_res res;
6833 struct rpc_message msg = {
6835 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
6840 struct rpc_task_setup task_setup_data = {
6843 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
6844 .rpc_message = &msg,
6845 .flags = RPC_TASK_TIMEOUT,
6847 struct rpc_task *task;
6849 dprintk("--> %s\n", __func__);
6851 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
6852 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
6853 args.dir = NFS4_CDFC4_FORE;
6855 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
6856 if (xprt != rcu_access_pointer(clnt->cl_xprt))
6857 args.dir = NFS4_CDFC4_FORE;
6859 task = rpc_run_task(&task_setup_data);
6860 if (!IS_ERR(task)) {
6861 status = task->tk_status;
6864 status = PTR_ERR(task);
6865 trace_nfs4_bind_conn_to_session(clp, status);
6867 if (memcmp(res.sessionid.data,
6868 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
6869 dprintk("NFS: %s: Session ID mismatch\n", __func__);
6873 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
6874 dprintk("NFS: %s: Unexpected direction from server\n",
6879 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
6880 dprintk("NFS: %s: Server returned RDMA mode = true\n",
6887 dprintk("<-- %s status= %d\n", __func__, status);
6891 struct rpc_bind_conn_calldata {
6892 struct nfs_client *clp;
6893 struct rpc_cred *cred;
6897 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
6898 struct rpc_xprt *xprt,
6901 struct rpc_bind_conn_calldata *p = calldata;
6903 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
6906 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
6908 struct rpc_bind_conn_calldata data = {
6912 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
6913 nfs4_proc_bind_conn_to_session_callback, &data);
6917 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
6918 * and operations we'd like to see to enable certain features in the allow map
6920 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
6921 .how = SP4_MACH_CRED,
6922 .enforce.u.words = {
6923 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6924 1 << (OP_EXCHANGE_ID - 32) |
6925 1 << (OP_CREATE_SESSION - 32) |
6926 1 << (OP_DESTROY_SESSION - 32) |
6927 1 << (OP_DESTROY_CLIENTID - 32)
6930 [0] = 1 << (OP_CLOSE) |
6931 1 << (OP_OPEN_DOWNGRADE) |
6933 1 << (OP_DELEGRETURN) |
6935 [1] = 1 << (OP_SECINFO - 32) |
6936 1 << (OP_SECINFO_NO_NAME - 32) |
6937 1 << (OP_LAYOUTRETURN - 32) |
6938 1 << (OP_TEST_STATEID - 32) |
6939 1 << (OP_FREE_STATEID - 32) |
6940 1 << (OP_WRITE - 32)
6945 * Select the state protection mode for client `clp' given the server results
6946 * from exchange_id in `sp'.
6948 * Returns 0 on success, negative errno otherwise.
6950 static int nfs4_sp4_select_mode(struct nfs_client *clp,
6951 struct nfs41_state_protection *sp)
6953 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
6954 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6955 1 << (OP_EXCHANGE_ID - 32) |
6956 1 << (OP_CREATE_SESSION - 32) |
6957 1 << (OP_DESTROY_SESSION - 32) |
6958 1 << (OP_DESTROY_CLIENTID - 32)
6962 if (sp->how == SP4_MACH_CRED) {
6963 /* Print state protect result */
6964 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
6965 for (i = 0; i <= LAST_NFS4_OP; i++) {
6966 if (test_bit(i, sp->enforce.u.longs))
6967 dfprintk(MOUNT, " enforce op %d\n", i);
6968 if (test_bit(i, sp->allow.u.longs))
6969 dfprintk(MOUNT, " allow op %d\n", i);
6972 /* make sure nothing is on enforce list that isn't supported */
6973 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
6974 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
6975 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6981 * Minimal mode - state operations are allowed to use machine
6982 * credential. Note this already happens by default, so the
6983 * client doesn't have to do anything more than the negotiation.
6985 * NOTE: we don't care if EXCHANGE_ID is in the list -
6986 * we're already using the machine cred for exchange_id
6987 * and will never use a different cred.
6989 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
6990 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
6991 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
6992 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
6993 dfprintk(MOUNT, "sp4_mach_cred:\n");
6994 dfprintk(MOUNT, " minimal mode enabled\n");
6995 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
6997 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7001 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
7002 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7003 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
7004 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7005 dfprintk(MOUNT, " cleanup mode enabled\n");
7006 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
7009 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7010 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7011 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
7012 &clp->cl_sp4_flags);
7015 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7016 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7017 dfprintk(MOUNT, " secinfo mode enabled\n");
7018 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
7021 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7022 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7023 dfprintk(MOUNT, " stateid mode enabled\n");
7024 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
7027 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7028 dfprintk(MOUNT, " write mode enabled\n");
7029 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
7032 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7033 dfprintk(MOUNT, " commit mode enabled\n");
7034 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
7042 * _nfs4_proc_exchange_id()
7044 * Wrapper for EXCHANGE_ID operation.
7046 static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
7049 nfs4_verifier verifier;
7050 struct nfs41_exchange_id_args args = {
7051 .verifier = &verifier,
7053 #ifdef CONFIG_NFS_V4_1_MIGRATION
7054 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7055 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7056 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7058 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7059 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7062 struct nfs41_exchange_id_res res = {
7066 struct rpc_message msg = {
7067 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
7073 nfs4_init_boot_verifier(clp, &verifier);
7075 status = nfs4_init_uniform_client_string(clp);
7079 dprintk("NFS call exchange_id auth=%s, '%s'\n",
7080 clp->cl_rpcclient->cl_auth->au_ops->au_name,
7083 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7085 if (unlikely(res.server_owner == NULL)) {
7090 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
7092 if (unlikely(res.server_scope == NULL)) {
7094 goto out_server_owner;
7097 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7098 if (unlikely(res.impl_id == NULL)) {
7100 goto out_server_scope;
7105 args.state_protect.how = SP4_NONE;
7109 args.state_protect = nfs4_sp4_mach_cred_request;
7119 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7120 trace_nfs4_exchange_id(clp, status);
7122 status = nfs4_check_cl_exchange_flags(res.flags);
7125 status = nfs4_sp4_select_mode(clp, &res.state_protect);
7128 clp->cl_clientid = res.clientid;
7129 clp->cl_exchange_flags = res.flags;
7130 /* Client ID is not confirmed */
7131 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
7132 clear_bit(NFS4_SESSION_ESTABLISHED,
7133 &clp->cl_session->session_state);
7134 clp->cl_seqid = res.seqid;
7137 kfree(clp->cl_serverowner);
7138 clp->cl_serverowner = res.server_owner;
7139 res.server_owner = NULL;
7141 /* use the most recent implementation id */
7142 kfree(clp->cl_implid);
7143 clp->cl_implid = res.impl_id;
7146 if (clp->cl_serverscope != NULL &&
7147 !nfs41_same_server_scope(clp->cl_serverscope,
7148 res.server_scope)) {
7149 dprintk("%s: server_scope mismatch detected\n",
7151 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
7152 kfree(clp->cl_serverscope);
7153 clp->cl_serverscope = NULL;
7156 if (clp->cl_serverscope == NULL) {
7157 clp->cl_serverscope = res.server_scope;
7158 res.server_scope = NULL;
7165 kfree(res.server_scope);
7167 kfree(res.server_owner);
7169 if (clp->cl_implid != NULL)
7170 dprintk("NFS reply exchange_id: Server Implementation ID: "
7171 "domain: %s, name: %s, date: %llu,%u\n",
7172 clp->cl_implid->domain, clp->cl_implid->name,
7173 clp->cl_implid->date.seconds,
7174 clp->cl_implid->date.nseconds);
7175 dprintk("NFS reply exchange_id: %d\n", status);
7180 * nfs4_proc_exchange_id()
7182 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7184 * Since the clientid has expired, all compounds using sessions
7185 * associated with the stale clientid will be returning
7186 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7187 * be in some phase of session reset.
7189 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7191 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7193 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7196 /* try SP4_MACH_CRED if krb5i/p */
7197 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7198 authflavor == RPC_AUTH_GSS_KRB5P) {
7199 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
7205 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
7208 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7209 struct rpc_cred *cred)
7211 struct rpc_message msg = {
7212 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7218 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7219 trace_nfs4_destroy_clientid(clp, status);
7221 dprintk("NFS: Got error %d from the server %s on "
7222 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7226 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7227 struct rpc_cred *cred)
7232 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7233 ret = _nfs4_proc_destroy_clientid(clp, cred);
7235 case -NFS4ERR_DELAY:
7236 case -NFS4ERR_CLIENTID_BUSY:
7246 int nfs4_destroy_clientid(struct nfs_client *clp)
7248 struct rpc_cred *cred;
7251 if (clp->cl_mvops->minor_version < 1)
7253 if (clp->cl_exchange_flags == 0)
7255 if (clp->cl_preserve_clid)
7257 cred = nfs4_get_clid_cred(clp);
7258 ret = nfs4_proc_destroy_clientid(clp, cred);
7263 case -NFS4ERR_STALE_CLIENTID:
7264 clp->cl_exchange_flags = 0;
7270 struct nfs4_get_lease_time_data {
7271 struct nfs4_get_lease_time_args *args;
7272 struct nfs4_get_lease_time_res *res;
7273 struct nfs_client *clp;
7276 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7279 struct nfs4_get_lease_time_data *data =
7280 (struct nfs4_get_lease_time_data *)calldata;
7282 dprintk("--> %s\n", __func__);
7283 /* just setup sequence, do not trigger session recovery
7284 since we're invoked within one */
7285 nfs41_setup_sequence(data->clp->cl_session,
7286 &data->args->la_seq_args,
7287 &data->res->lr_seq_res,
7289 dprintk("<-- %s\n", __func__);
7293 * Called from nfs4_state_manager thread for session setup, so don't recover
7294 * from sequence operation or clientid errors.
7296 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7298 struct nfs4_get_lease_time_data *data =
7299 (struct nfs4_get_lease_time_data *)calldata;
7301 dprintk("--> %s\n", __func__);
7302 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7304 switch (task->tk_status) {
7305 case -NFS4ERR_DELAY:
7306 case -NFS4ERR_GRACE:
7307 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7308 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7309 task->tk_status = 0;
7311 case -NFS4ERR_RETRY_UNCACHED_REP:
7312 rpc_restart_call_prepare(task);
7315 dprintk("<-- %s\n", __func__);
7318 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
7319 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7320 .rpc_call_done = nfs4_get_lease_time_done,
7323 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7325 struct rpc_task *task;
7326 struct nfs4_get_lease_time_args args;
7327 struct nfs4_get_lease_time_res res = {
7328 .lr_fsinfo = fsinfo,
7330 struct nfs4_get_lease_time_data data = {
7335 struct rpc_message msg = {
7336 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7340 struct rpc_task_setup task_setup = {
7341 .rpc_client = clp->cl_rpcclient,
7342 .rpc_message = &msg,
7343 .callback_ops = &nfs4_get_lease_time_ops,
7344 .callback_data = &data,
7345 .flags = RPC_TASK_TIMEOUT,
7349 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
7350 nfs4_set_sequence_privileged(&args.la_seq_args);
7351 dprintk("--> %s\n", __func__);
7352 task = rpc_run_task(&task_setup);
7355 status = PTR_ERR(task);
7357 status = task->tk_status;
7360 dprintk("<-- %s return %d\n", __func__, status);
7366 * Initialize the values to be used by the client in CREATE_SESSION
7367 * If nfs4_init_session set the fore channel request and response sizes,
7370 * Set the back channel max_resp_sz_cached to zero to force the client to
7371 * always set csa_cachethis to FALSE because the current implementation
7372 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7374 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7375 struct rpc_clnt *clnt)
7377 unsigned int max_rqst_sz, max_resp_sz;
7378 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
7380 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7381 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7383 /* Fore channel attributes */
7384 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7385 args->fc_attrs.max_resp_sz = max_resp_sz;
7386 args->fc_attrs.max_ops = NFS4_MAX_OPS;
7387 args->fc_attrs.max_reqs = max_session_slots;
7389 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
7390 "max_ops=%u max_reqs=%u\n",
7392 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
7393 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
7395 /* Back channel attributes */
7396 args->bc_attrs.max_rqst_sz = max_bc_payload;
7397 args->bc_attrs.max_resp_sz = max_bc_payload;
7398 args->bc_attrs.max_resp_sz_cached = 0;
7399 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
7400 args->bc_attrs.max_reqs = NFS41_BC_MAX_CALLBACKS;
7402 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7403 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7405 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7406 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7407 args->bc_attrs.max_reqs);
7410 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7411 struct nfs41_create_session_res *res)
7413 struct nfs4_channel_attrs *sent = &args->fc_attrs;
7414 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
7416 if (rcvd->max_resp_sz > sent->max_resp_sz)
7419 * Our requested max_ops is the minimum we need; we're not
7420 * prepared to break up compounds into smaller pieces than that.
7421 * So, no point even trying to continue if the server won't
7424 if (rcvd->max_ops < sent->max_ops)
7426 if (rcvd->max_reqs == 0)
7428 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7429 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
7433 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7434 struct nfs41_create_session_res *res)
7436 struct nfs4_channel_attrs *sent = &args->bc_attrs;
7437 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
7439 if (!(res->flags & SESSION4_BACK_CHAN))
7441 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7443 if (rcvd->max_resp_sz < sent->max_resp_sz)
7445 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7447 /* These would render the backchannel useless: */
7448 if (rcvd->max_ops != sent->max_ops)
7450 if (rcvd->max_reqs != sent->max_reqs)
7456 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
7457 struct nfs41_create_session_res *res)
7461 ret = nfs4_verify_fore_channel_attrs(args, res);
7464 return nfs4_verify_back_channel_attrs(args, res);
7467 static void nfs4_update_session(struct nfs4_session *session,
7468 struct nfs41_create_session_res *res)
7470 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
7471 /* Mark client id and session as being confirmed */
7472 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7473 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
7474 session->flags = res->flags;
7475 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
7476 if (res->flags & SESSION4_BACK_CHAN)
7477 memcpy(&session->bc_attrs, &res->bc_attrs,
7478 sizeof(session->bc_attrs));
7481 static int _nfs4_proc_create_session(struct nfs_client *clp,
7482 struct rpc_cred *cred)
7484 struct nfs4_session *session = clp->cl_session;
7485 struct nfs41_create_session_args args = {
7487 .clientid = clp->cl_clientid,
7488 .seqid = clp->cl_seqid,
7489 .cb_program = NFS4_CALLBACK,
7491 struct nfs41_create_session_res res;
7493 struct rpc_message msg = {
7494 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7501 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
7502 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
7504 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7505 trace_nfs4_create_session(clp, status);
7508 /* Verify the session's negotiated channel_attrs values */
7509 status = nfs4_verify_channel_attrs(&args, &res);
7510 /* Increment the clientid slot sequence id */
7511 if (clp->cl_seqid == res.seqid)
7515 nfs4_update_session(session, &res);
7522 * Issues a CREATE_SESSION operation to the server.
7523 * It is the responsibility of the caller to verify the session is
7524 * expired before calling this routine.
7526 int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
7530 struct nfs4_session *session = clp->cl_session;
7532 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7534 status = _nfs4_proc_create_session(clp, cred);
7538 /* Init or reset the session slot tables */
7539 status = nfs4_setup_session_slot_tables(session);
7540 dprintk("slot table setup returned %d\n", status);
7544 ptr = (unsigned *)&session->sess_id.data[0];
7545 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7546 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
7548 dprintk("<-- %s\n", __func__);
7553 * Issue the over-the-wire RPC DESTROY_SESSION.
7554 * The caller must serialize access to this routine.
7556 int nfs4_proc_destroy_session(struct nfs4_session *session,
7557 struct rpc_cred *cred)
7559 struct rpc_message msg = {
7560 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
7561 .rpc_argp = session,
7566 dprintk("--> nfs4_proc_destroy_session\n");
7568 /* session is still being setup */
7569 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
7572 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7573 trace_nfs4_destroy_session(session->clp, status);
7576 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
7577 "Session has been destroyed regardless...\n", status);
7579 dprintk("<-- nfs4_proc_destroy_session\n");
7584 * Renew the cl_session lease.
7586 struct nfs4_sequence_data {
7587 struct nfs_client *clp;
7588 struct nfs4_sequence_args args;
7589 struct nfs4_sequence_res res;
7592 static void nfs41_sequence_release(void *data)
7594 struct nfs4_sequence_data *calldata = data;
7595 struct nfs_client *clp = calldata->clp;
7597 if (atomic_read(&clp->cl_count) > 1)
7598 nfs4_schedule_state_renewal(clp);
7599 nfs_put_client(clp);
7603 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7605 switch(task->tk_status) {
7606 case -NFS4ERR_DELAY:
7607 rpc_delay(task, NFS4_POLL_RETRY_MAX);
7610 nfs4_schedule_lease_recovery(clp);
7615 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
7617 struct nfs4_sequence_data *calldata = data;
7618 struct nfs_client *clp = calldata->clp;
7620 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
7623 trace_nfs4_sequence(clp, task->tk_status);
7624 if (task->tk_status < 0) {
7625 dprintk("%s ERROR %d\n", __func__, task->tk_status);
7626 if (atomic_read(&clp->cl_count) == 1)
7629 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
7630 rpc_restart_call_prepare(task);
7634 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
7636 dprintk("<-- %s\n", __func__);
7639 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
7641 struct nfs4_sequence_data *calldata = data;
7642 struct nfs_client *clp = calldata->clp;
7643 struct nfs4_sequence_args *args;
7644 struct nfs4_sequence_res *res;
7646 args = task->tk_msg.rpc_argp;
7647 res = task->tk_msg.rpc_resp;
7649 nfs41_setup_sequence(clp->cl_session, args, res, task);
7652 static const struct rpc_call_ops nfs41_sequence_ops = {
7653 .rpc_call_done = nfs41_sequence_call_done,
7654 .rpc_call_prepare = nfs41_sequence_prepare,
7655 .rpc_release = nfs41_sequence_release,
7658 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
7659 struct rpc_cred *cred,
7662 struct nfs4_sequence_data *calldata;
7663 struct rpc_message msg = {
7664 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
7667 struct rpc_task_setup task_setup_data = {
7668 .rpc_client = clp->cl_rpcclient,
7669 .rpc_message = &msg,
7670 .callback_ops = &nfs41_sequence_ops,
7671 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7674 if (!atomic_inc_not_zero(&clp->cl_count))
7675 return ERR_PTR(-EIO);
7676 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7677 if (calldata == NULL) {
7678 nfs_put_client(clp);
7679 return ERR_PTR(-ENOMEM);
7681 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
7683 nfs4_set_sequence_privileged(&calldata->args);
7684 msg.rpc_argp = &calldata->args;
7685 msg.rpc_resp = &calldata->res;
7686 calldata->clp = clp;
7687 task_setup_data.callback_data = calldata;
7689 return rpc_run_task(&task_setup_data);
7692 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
7694 struct rpc_task *task;
7697 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
7699 task = _nfs41_proc_sequence(clp, cred, false);
7701 ret = PTR_ERR(task);
7703 rpc_put_task_async(task);
7704 dprintk("<-- %s status=%d\n", __func__, ret);
7708 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
7710 struct rpc_task *task;
7713 task = _nfs41_proc_sequence(clp, cred, true);
7715 ret = PTR_ERR(task);
7718 ret = rpc_wait_for_completion_task(task);
7720 ret = task->tk_status;
7723 dprintk("<-- %s status=%d\n", __func__, ret);
7727 struct nfs4_reclaim_complete_data {
7728 struct nfs_client *clp;
7729 struct nfs41_reclaim_complete_args arg;
7730 struct nfs41_reclaim_complete_res res;
7733 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
7735 struct nfs4_reclaim_complete_data *calldata = data;
7737 nfs41_setup_sequence(calldata->clp->cl_session,
7738 &calldata->arg.seq_args,
7739 &calldata->res.seq_res,
7743 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7745 switch(task->tk_status) {
7747 case -NFS4ERR_COMPLETE_ALREADY:
7748 case -NFS4ERR_WRONG_CRED: /* What to do here? */
7750 case -NFS4ERR_DELAY:
7751 rpc_delay(task, NFS4_POLL_RETRY_MAX);
7753 case -NFS4ERR_RETRY_UNCACHED_REP:
7756 nfs4_schedule_lease_recovery(clp);
7761 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
7763 struct nfs4_reclaim_complete_data *calldata = data;
7764 struct nfs_client *clp = calldata->clp;
7765 struct nfs4_sequence_res *res = &calldata->res.seq_res;
7767 dprintk("--> %s\n", __func__);
7768 if (!nfs41_sequence_done(task, res))
7771 trace_nfs4_reclaim_complete(clp, task->tk_status);
7772 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
7773 rpc_restart_call_prepare(task);
7776 dprintk("<-- %s\n", __func__);
7779 static void nfs4_free_reclaim_complete_data(void *data)
7781 struct nfs4_reclaim_complete_data *calldata = data;
7786 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
7787 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
7788 .rpc_call_done = nfs4_reclaim_complete_done,
7789 .rpc_release = nfs4_free_reclaim_complete_data,
7793 * Issue a global reclaim complete.
7795 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
7796 struct rpc_cred *cred)
7798 struct nfs4_reclaim_complete_data *calldata;
7799 struct rpc_task *task;
7800 struct rpc_message msg = {
7801 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
7804 struct rpc_task_setup task_setup_data = {
7805 .rpc_client = clp->cl_rpcclient,
7806 .rpc_message = &msg,
7807 .callback_ops = &nfs4_reclaim_complete_call_ops,
7808 .flags = RPC_TASK_ASYNC,
7810 int status = -ENOMEM;
7812 dprintk("--> %s\n", __func__);
7813 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7814 if (calldata == NULL)
7816 calldata->clp = clp;
7817 calldata->arg.one_fs = 0;
7819 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
7820 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
7821 msg.rpc_argp = &calldata->arg;
7822 msg.rpc_resp = &calldata->res;
7823 task_setup_data.callback_data = calldata;
7824 task = rpc_run_task(&task_setup_data);
7826 status = PTR_ERR(task);
7829 status = nfs4_wait_for_completion_rpc_task(task);
7831 status = task->tk_status;
7835 dprintk("<-- %s status=%d\n", __func__, status);
7840 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
7842 struct nfs4_layoutget *lgp = calldata;
7843 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
7844 struct nfs4_session *session = nfs4_get_session(server);
7847 dprintk("--> %s\n", __func__);
7848 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
7849 * right now covering the LAYOUTGET we are about to send.
7850 * However, that is not so catastrophic, and there seems
7851 * to be no way to prevent it completely.
7853 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
7854 &lgp->res.seq_res, task))
7856 ret = pnfs_choose_layoutget_stateid(&lgp->args.stateid,
7857 NFS_I(lgp->args.inode)->layout,
7859 lgp->args.ctx->state);
7861 rpc_exit(task, ret);
7864 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
7866 struct nfs4_layoutget *lgp = calldata;
7867 struct inode *inode = lgp->args.inode;
7868 struct nfs_server *server = NFS_SERVER(inode);
7869 struct pnfs_layout_hdr *lo;
7870 struct nfs4_state *state = NULL;
7871 unsigned long timeo, now, giveup;
7873 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
7875 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
7878 switch (task->tk_status) {
7883 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
7884 * on the file. set tk_status to -ENODATA to tell upper layer to
7887 case -NFS4ERR_LAYOUTUNAVAILABLE:
7888 task->tk_status = -ENODATA;
7891 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
7892 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
7894 case -NFS4ERR_BADLAYOUT:
7897 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
7898 * (or clients) writing to the same RAID stripe except when
7899 * the minlength argument is 0 (see RFC5661 section 18.43.3).
7901 case -NFS4ERR_LAYOUTTRYLATER:
7902 if (lgp->args.minlength == 0)
7905 * NFS4ERR_RECALLCONFLICT is when conflict with self (must recall
7906 * existing layout before getting a new one).
7908 case -NFS4ERR_RECALLCONFLICT:
7909 timeo = rpc_get_timeout(task->tk_client);
7910 giveup = lgp->args.timestamp + timeo;
7912 if (time_after(giveup, now)) {
7913 unsigned long delay;
7916 * - Not less then NFS4_POLL_RETRY_MIN.
7917 * - One last time a jiffie before we give up
7918 * - exponential backoff (time_now minus start_attempt)
7920 delay = max_t(unsigned long, NFS4_POLL_RETRY_MIN,
7921 min((giveup - now - 1),
7922 now - lgp->args.timestamp));
7924 dprintk("%s: NFS4ERR_RECALLCONFLICT waiting %lu\n",
7926 rpc_delay(task, delay);
7927 /* Do not call nfs4_async_handle_error() */
7931 case -NFS4ERR_EXPIRED:
7932 case -NFS4ERR_BAD_STATEID:
7933 spin_lock(&inode->i_lock);
7934 if (nfs4_stateid_match(&lgp->args.stateid,
7935 &lgp->args.ctx->state->stateid)) {
7936 spin_unlock(&inode->i_lock);
7937 /* If the open stateid was bad, then recover it. */
7938 state = lgp->args.ctx->state;
7941 lo = NFS_I(inode)->layout;
7942 if (lo && nfs4_stateid_match(&lgp->args.stateid,
7943 &lo->plh_stateid)) {
7947 * Mark the bad layout state as invalid, then retry
7948 * with the current stateid.
7950 set_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags);
7951 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
7952 spin_unlock(&inode->i_lock);
7953 pnfs_free_lseg_list(&head);
7955 spin_unlock(&inode->i_lock);
7958 if (nfs4_async_handle_error(task, server, state, &lgp->timeout) == -EAGAIN)
7961 dprintk("<-- %s\n", __func__);
7964 task->tk_status = 0;
7965 rpc_restart_call_prepare(task);
7968 task->tk_status = -EOVERFLOW;
7972 static size_t max_response_pages(struct nfs_server *server)
7974 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
7975 return nfs_page_array_len(0, max_resp_sz);
7978 static void nfs4_free_pages(struct page **pages, size_t size)
7985 for (i = 0; i < size; i++) {
7988 __free_page(pages[i]);
7993 static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
7995 struct page **pages;
7998 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8000 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8004 for (i = 0; i < size; i++) {
8005 pages[i] = alloc_page(gfp_flags);
8007 dprintk("%s: failed to allocate page\n", __func__);
8008 nfs4_free_pages(pages, size);
8016 static void nfs4_layoutget_release(void *calldata)
8018 struct nfs4_layoutget *lgp = calldata;
8019 struct inode *inode = lgp->args.inode;
8020 struct nfs_server *server = NFS_SERVER(inode);
8021 size_t max_pages = max_response_pages(server);
8023 dprintk("--> %s\n", __func__);
8024 nfs4_free_pages(lgp->args.layout.pages, max_pages);
8025 pnfs_put_layout_hdr(NFS_I(inode)->layout);
8026 put_nfs_open_context(lgp->args.ctx);
8028 dprintk("<-- %s\n", __func__);
8031 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8032 .rpc_call_prepare = nfs4_layoutget_prepare,
8033 .rpc_call_done = nfs4_layoutget_done,
8034 .rpc_release = nfs4_layoutget_release,
8037 struct pnfs_layout_segment *
8038 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
8040 struct inode *inode = lgp->args.inode;
8041 struct nfs_server *server = NFS_SERVER(inode);
8042 size_t max_pages = max_response_pages(server);
8043 struct rpc_task *task;
8044 struct rpc_message msg = {
8045 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8046 .rpc_argp = &lgp->args,
8047 .rpc_resp = &lgp->res,
8048 .rpc_cred = lgp->cred,
8050 struct rpc_task_setup task_setup_data = {
8051 .rpc_client = server->client,
8052 .rpc_message = &msg,
8053 .callback_ops = &nfs4_layoutget_call_ops,
8054 .callback_data = lgp,
8055 .flags = RPC_TASK_ASYNC,
8057 struct pnfs_layout_segment *lseg = NULL;
8060 dprintk("--> %s\n", __func__);
8062 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8063 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8065 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8066 if (!lgp->args.layout.pages) {
8067 nfs4_layoutget_release(lgp);
8068 return ERR_PTR(-ENOMEM);
8070 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8071 lgp->args.timestamp = jiffies;
8073 lgp->res.layoutp = &lgp->args.layout;
8074 lgp->res.seq_res.sr_slot = NULL;
8075 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
8077 task = rpc_run_task(&task_setup_data);
8079 return ERR_CAST(task);
8080 status = nfs4_wait_for_completion_rpc_task(task);
8082 status = task->tk_status;
8083 trace_nfs4_layoutget(lgp->args.ctx,
8088 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8089 if (status == 0 && lgp->res.layoutp->len)
8090 lseg = pnfs_layout_process(lgp);
8092 dprintk("<-- %s status=%d\n", __func__, status);
8094 return ERR_PTR(status);
8099 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8101 struct nfs4_layoutreturn *lrp = calldata;
8103 dprintk("--> %s\n", __func__);
8104 nfs41_setup_sequence(lrp->clp->cl_session,
8105 &lrp->args.seq_args,
8110 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8112 struct nfs4_layoutreturn *lrp = calldata;
8113 struct nfs_server *server;
8115 dprintk("--> %s\n", __func__);
8117 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
8120 server = NFS_SERVER(lrp->args.inode);
8121 switch (task->tk_status) {
8123 task->tk_status = 0;
8126 case -NFS4ERR_DELAY:
8127 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
8129 rpc_restart_call_prepare(task);
8132 dprintk("<-- %s\n", __func__);
8135 static void nfs4_layoutreturn_release(void *calldata)
8137 struct nfs4_layoutreturn *lrp = calldata;
8138 struct pnfs_layout_hdr *lo = lrp->args.layout;
8141 dprintk("--> %s\n", __func__);
8142 spin_lock(&lo->plh_inode->i_lock);
8143 pnfs_mark_matching_lsegs_invalid(lo, &freeme, &lrp->args.range);
8144 pnfs_mark_layout_returned_if_empty(lo);
8145 if (lrp->res.lrs_present)
8146 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
8147 pnfs_clear_layoutreturn_waitbit(lo);
8148 spin_unlock(&lo->plh_inode->i_lock);
8149 pnfs_free_lseg_list(&freeme);
8150 pnfs_put_layout_hdr(lrp->args.layout);
8151 nfs_iput_and_deactive(lrp->inode);
8153 dprintk("<-- %s\n", __func__);
8156 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8157 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8158 .rpc_call_done = nfs4_layoutreturn_done,
8159 .rpc_release = nfs4_layoutreturn_release,
8162 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
8164 struct rpc_task *task;
8165 struct rpc_message msg = {
8166 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8167 .rpc_argp = &lrp->args,
8168 .rpc_resp = &lrp->res,
8169 .rpc_cred = lrp->cred,
8171 struct rpc_task_setup task_setup_data = {
8172 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
8173 .rpc_message = &msg,
8174 .callback_ops = &nfs4_layoutreturn_call_ops,
8175 .callback_data = lrp,
8179 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8180 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8181 &task_setup_data.rpc_client, &msg);
8183 dprintk("--> %s\n", __func__);
8185 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8187 nfs4_layoutreturn_release(lrp);
8190 task_setup_data.flags |= RPC_TASK_ASYNC;
8192 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
8193 task = rpc_run_task(&task_setup_data);
8195 return PTR_ERR(task);
8197 status = task->tk_status;
8198 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
8199 dprintk("<-- %s status=%d\n", __func__, status);
8205 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
8206 struct pnfs_device *pdev,
8207 struct rpc_cred *cred)
8209 struct nfs4_getdeviceinfo_args args = {
8211 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8212 NOTIFY_DEVICEID4_DELETE,
8214 struct nfs4_getdeviceinfo_res res = {
8217 struct rpc_message msg = {
8218 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8225 dprintk("--> %s\n", __func__);
8226 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
8227 if (res.notification & ~args.notify_types)
8228 dprintk("%s: unsupported notification\n", __func__);
8229 if (res.notification != args.notify_types)
8232 dprintk("<-- %s status=%d\n", __func__, status);
8237 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8238 struct pnfs_device *pdev,
8239 struct rpc_cred *cred)
8241 struct nfs4_exception exception = { };
8245 err = nfs4_handle_exception(server,
8246 _nfs4_proc_getdeviceinfo(server, pdev, cred),
8248 } while (exception.retry);
8251 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8253 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8255 struct nfs4_layoutcommit_data *data = calldata;
8256 struct nfs_server *server = NFS_SERVER(data->args.inode);
8257 struct nfs4_session *session = nfs4_get_session(server);
8259 nfs41_setup_sequence(session,
8260 &data->args.seq_args,
8266 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8268 struct nfs4_layoutcommit_data *data = calldata;
8269 struct nfs_server *server = NFS_SERVER(data->args.inode);
8271 if (!nfs41_sequence_done(task, &data->res.seq_res))
8274 switch (task->tk_status) { /* Just ignore these failures */
8275 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8276 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8277 case -NFS4ERR_BADLAYOUT: /* no layout */
8278 case -NFS4ERR_GRACE: /* loca_recalim always false */
8279 task->tk_status = 0;
8283 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
8284 rpc_restart_call_prepare(task);
8290 static void nfs4_layoutcommit_release(void *calldata)
8292 struct nfs4_layoutcommit_data *data = calldata;
8294 pnfs_cleanup_layoutcommit(data);
8295 nfs_post_op_update_inode_force_wcc(data->args.inode,
8297 put_rpccred(data->cred);
8298 nfs_iput_and_deactive(data->inode);
8302 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8303 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8304 .rpc_call_done = nfs4_layoutcommit_done,
8305 .rpc_release = nfs4_layoutcommit_release,
8309 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
8311 struct rpc_message msg = {
8312 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8313 .rpc_argp = &data->args,
8314 .rpc_resp = &data->res,
8315 .rpc_cred = data->cred,
8317 struct rpc_task_setup task_setup_data = {
8318 .task = &data->task,
8319 .rpc_client = NFS_CLIENT(data->args.inode),
8320 .rpc_message = &msg,
8321 .callback_ops = &nfs4_layoutcommit_ops,
8322 .callback_data = data,
8324 struct rpc_task *task;
8327 dprintk("NFS: initiating layoutcommit call. sync %d "
8328 "lbw: %llu inode %lu\n", sync,
8329 data->args.lastbytewritten,
8330 data->args.inode->i_ino);
8333 data->inode = nfs_igrab_and_active(data->args.inode);
8334 if (data->inode == NULL) {
8335 nfs4_layoutcommit_release(data);
8338 task_setup_data.flags = RPC_TASK_ASYNC;
8340 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
8341 task = rpc_run_task(&task_setup_data);
8343 return PTR_ERR(task);
8345 status = task->tk_status;
8346 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
8347 dprintk("%s: status %d\n", __func__, status);
8353 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8354 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8357 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8358 struct nfs_fsinfo *info,
8359 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
8361 struct nfs41_secinfo_no_name_args args = {
8362 .style = SECINFO_STYLE_CURRENT_FH,
8364 struct nfs4_secinfo_res res = {
8367 struct rpc_message msg = {
8368 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8372 struct rpc_clnt *clnt = server->client;
8373 struct rpc_cred *cred = NULL;
8376 if (use_integrity) {
8377 clnt = server->nfs_client->cl_rpcclient;
8378 cred = nfs4_get_clid_cred(server->nfs_client);
8379 msg.rpc_cred = cred;
8382 dprintk("--> %s\n", __func__);
8383 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8385 dprintk("<-- %s status=%d\n", __func__, status);
8394 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8395 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8397 struct nfs4_exception exception = { };
8400 /* first try using integrity protection */
8401 err = -NFS4ERR_WRONGSEC;
8403 /* try to use integrity protection with machine cred */
8404 if (_nfs4_is_integrity_protected(server->nfs_client))
8405 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8409 * if unable to use integrity protection, or SECINFO with
8410 * integrity protection returns NFS4ERR_WRONGSEC (which is
8411 * disallowed by spec, but exists in deployed servers) use
8412 * the current filesystem's rpc_client and the user cred.
8414 if (err == -NFS4ERR_WRONGSEC)
8415 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8420 case -NFS4ERR_WRONGSEC:
8424 err = nfs4_handle_exception(server, err, &exception);
8426 } while (exception.retry);
8432 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8433 struct nfs_fsinfo *info)
8437 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
8438 struct nfs4_secinfo_flavors *flavors;
8439 struct nfs4_secinfo4 *secinfo;
8442 page = alloc_page(GFP_KERNEL);
8448 flavors = page_address(page);
8449 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8452 * Fall back on "guess and check" method if
8453 * the server doesn't support SECINFO_NO_NAME
8455 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
8456 err = nfs4_find_root_sec(server, fhandle, info);
8462 for (i = 0; i < flavors->num_flavors; i++) {
8463 secinfo = &flavors->flavors[i];
8465 switch (secinfo->flavor) {
8469 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8470 &secinfo->flavor_info);
8473 flavor = RPC_AUTH_MAXFLAVOR;
8477 if (!nfs_auth_info_match(&server->auth_info, flavor))
8478 flavor = RPC_AUTH_MAXFLAVOR;
8480 if (flavor != RPC_AUTH_MAXFLAVOR) {
8481 err = nfs4_lookup_root_sec(server, fhandle,
8488 if (flavor == RPC_AUTH_MAXFLAVOR)
8499 static int _nfs41_test_stateid(struct nfs_server *server,
8500 nfs4_stateid *stateid,
8501 struct rpc_cred *cred)
8504 struct nfs41_test_stateid_args args = {
8507 struct nfs41_test_stateid_res res;
8508 struct rpc_message msg = {
8509 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8514 struct rpc_clnt *rpc_client = server->client;
8516 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8519 dprintk("NFS call test_stateid %p\n", stateid);
8520 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
8521 nfs4_set_sequence_privileged(&args.seq_args);
8522 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
8523 &args.seq_args, &res.seq_res);
8524 if (status != NFS_OK) {
8525 dprintk("NFS reply test_stateid: failed, %d\n", status);
8528 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
8533 * nfs41_test_stateid - perform a TEST_STATEID operation
8535 * @server: server / transport on which to perform the operation
8536 * @stateid: state ID to test
8539 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8540 * Otherwise a negative NFS4ERR value is returned if the operation
8541 * failed or the state ID is not currently valid.
8543 static int nfs41_test_stateid(struct nfs_server *server,
8544 nfs4_stateid *stateid,
8545 struct rpc_cred *cred)
8547 struct nfs4_exception exception = { };
8550 err = _nfs41_test_stateid(server, stateid, cred);
8551 if (err != -NFS4ERR_DELAY)
8553 nfs4_handle_exception(server, err, &exception);
8554 } while (exception.retry);
8558 struct nfs_free_stateid_data {
8559 struct nfs_server *server;
8560 struct nfs41_free_stateid_args args;
8561 struct nfs41_free_stateid_res res;
8564 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
8566 struct nfs_free_stateid_data *data = calldata;
8567 nfs41_setup_sequence(nfs4_get_session(data->server),
8568 &data->args.seq_args,
8573 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
8575 struct nfs_free_stateid_data *data = calldata;
8577 nfs41_sequence_done(task, &data->res.seq_res);
8579 switch (task->tk_status) {
8580 case -NFS4ERR_DELAY:
8581 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
8582 rpc_restart_call_prepare(task);
8586 static void nfs41_free_stateid_release(void *calldata)
8591 static const struct rpc_call_ops nfs41_free_stateid_ops = {
8592 .rpc_call_prepare = nfs41_free_stateid_prepare,
8593 .rpc_call_done = nfs41_free_stateid_done,
8594 .rpc_release = nfs41_free_stateid_release,
8597 static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
8598 nfs4_stateid *stateid,
8599 struct rpc_cred *cred,
8602 struct rpc_message msg = {
8603 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
8606 struct rpc_task_setup task_setup = {
8607 .rpc_client = server->client,
8608 .rpc_message = &msg,
8609 .callback_ops = &nfs41_free_stateid_ops,
8610 .flags = RPC_TASK_ASYNC,
8612 struct nfs_free_stateid_data *data;
8614 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8615 &task_setup.rpc_client, &msg);
8617 dprintk("NFS call free_stateid %p\n", stateid);
8618 data = kmalloc(sizeof(*data), GFP_NOFS);
8620 return ERR_PTR(-ENOMEM);
8621 data->server = server;
8622 nfs4_stateid_copy(&data->args.stateid, stateid);
8624 task_setup.callback_data = data;
8626 msg.rpc_argp = &data->args;
8627 msg.rpc_resp = &data->res;
8628 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
8630 nfs4_set_sequence_privileged(&data->args.seq_args);
8632 return rpc_run_task(&task_setup);
8636 * nfs41_free_stateid - perform a FREE_STATEID operation
8638 * @server: server / transport on which to perform the operation
8639 * @stateid: state ID to release
8642 * Returns NFS_OK if the server freed "stateid". Otherwise a
8643 * negative NFS4ERR value is returned.
8645 static int nfs41_free_stateid(struct nfs_server *server,
8646 nfs4_stateid *stateid,
8647 struct rpc_cred *cred)
8649 struct rpc_task *task;
8652 task = _nfs41_free_stateid(server, stateid, cred, true);
8654 return PTR_ERR(task);
8655 ret = rpc_wait_for_completion_task(task);
8657 ret = task->tk_status;
8663 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
8665 struct rpc_task *task;
8666 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
8668 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
8669 nfs4_free_lock_state(server, lsp);
8675 static bool nfs41_match_stateid(const nfs4_stateid *s1,
8676 const nfs4_stateid *s2)
8678 if (s1->type != s2->type)
8681 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
8684 if (s1->seqid == s2->seqid)
8686 if (s1->seqid == 0 || s2->seqid == 0)
8692 #endif /* CONFIG_NFS_V4_1 */
8694 static bool nfs4_match_stateid(const nfs4_stateid *s1,
8695 const nfs4_stateid *s2)
8697 return nfs4_stateid_match(s1, s2);
8701 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
8702 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
8703 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
8704 .recover_open = nfs4_open_reclaim,
8705 .recover_lock = nfs4_lock_reclaim,
8706 .establish_clid = nfs4_init_clientid,
8707 .detect_trunking = nfs40_discover_server_trunking,
8710 #if defined(CONFIG_NFS_V4_1)
8711 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
8712 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
8713 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
8714 .recover_open = nfs4_open_reclaim,
8715 .recover_lock = nfs4_lock_reclaim,
8716 .establish_clid = nfs41_init_clientid,
8717 .reclaim_complete = nfs41_proc_reclaim_complete,
8718 .detect_trunking = nfs41_discover_server_trunking,
8720 #endif /* CONFIG_NFS_V4_1 */
8722 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
8723 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
8724 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
8725 .recover_open = nfs40_open_expired,
8726 .recover_lock = nfs4_lock_expired,
8727 .establish_clid = nfs4_init_clientid,
8730 #if defined(CONFIG_NFS_V4_1)
8731 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
8732 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
8733 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
8734 .recover_open = nfs41_open_expired,
8735 .recover_lock = nfs41_lock_expired,
8736 .establish_clid = nfs41_init_clientid,
8738 #endif /* CONFIG_NFS_V4_1 */
8740 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
8741 .sched_state_renewal = nfs4_proc_async_renew,
8742 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
8743 .renew_lease = nfs4_proc_renew,
8746 #if defined(CONFIG_NFS_V4_1)
8747 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
8748 .sched_state_renewal = nfs41_proc_async_sequence,
8749 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
8750 .renew_lease = nfs4_proc_sequence,
8754 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
8755 .get_locations = _nfs40_proc_get_locations,
8756 .fsid_present = _nfs40_proc_fsid_present,
8759 #if defined(CONFIG_NFS_V4_1)
8760 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
8761 .get_locations = _nfs41_proc_get_locations,
8762 .fsid_present = _nfs41_proc_fsid_present,
8764 #endif /* CONFIG_NFS_V4_1 */
8766 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
8768 .init_caps = NFS_CAP_READDIRPLUS
8769 | NFS_CAP_ATOMIC_OPEN
8770 | NFS_CAP_POSIX_LOCK,
8771 .init_client = nfs40_init_client,
8772 .shutdown_client = nfs40_shutdown_client,
8773 .match_stateid = nfs4_match_stateid,
8774 .find_root_sec = nfs4_find_root_sec,
8775 .free_lock_state = nfs4_release_lockowner,
8776 .alloc_seqid = nfs_alloc_seqid,
8777 .call_sync_ops = &nfs40_call_sync_ops,
8778 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
8779 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
8780 .state_renewal_ops = &nfs40_state_renewal_ops,
8781 .mig_recovery_ops = &nfs40_mig_recovery_ops,
8784 #if defined(CONFIG_NFS_V4_1)
8785 static struct nfs_seqid *
8786 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
8791 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
8793 .init_caps = NFS_CAP_READDIRPLUS
8794 | NFS_CAP_ATOMIC_OPEN
8795 | NFS_CAP_POSIX_LOCK
8796 | NFS_CAP_STATEID_NFSV41
8797 | NFS_CAP_ATOMIC_OPEN_V1,
8798 .init_client = nfs41_init_client,
8799 .shutdown_client = nfs41_shutdown_client,
8800 .match_stateid = nfs41_match_stateid,
8801 .find_root_sec = nfs41_find_root_sec,
8802 .free_lock_state = nfs41_free_lock_state,
8803 .alloc_seqid = nfs_alloc_no_seqid,
8804 .call_sync_ops = &nfs41_call_sync_ops,
8805 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8806 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8807 .state_renewal_ops = &nfs41_state_renewal_ops,
8808 .mig_recovery_ops = &nfs41_mig_recovery_ops,
8812 #if defined(CONFIG_NFS_V4_2)
8813 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
8815 .init_caps = NFS_CAP_READDIRPLUS
8816 | NFS_CAP_ATOMIC_OPEN
8817 | NFS_CAP_POSIX_LOCK
8818 | NFS_CAP_STATEID_NFSV41
8819 | NFS_CAP_ATOMIC_OPEN_V1
8822 | NFS_CAP_DEALLOCATE
8824 | NFS_CAP_LAYOUTSTATS
8826 .init_client = nfs41_init_client,
8827 .shutdown_client = nfs41_shutdown_client,
8828 .match_stateid = nfs41_match_stateid,
8829 .find_root_sec = nfs41_find_root_sec,
8830 .free_lock_state = nfs41_free_lock_state,
8831 .call_sync_ops = &nfs41_call_sync_ops,
8832 .alloc_seqid = nfs_alloc_no_seqid,
8833 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8834 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8835 .state_renewal_ops = &nfs41_state_renewal_ops,
8836 .mig_recovery_ops = &nfs41_mig_recovery_ops,
8840 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
8841 [0] = &nfs_v4_0_minor_ops,
8842 #if defined(CONFIG_NFS_V4_1)
8843 [1] = &nfs_v4_1_minor_ops,
8845 #if defined(CONFIG_NFS_V4_2)
8846 [2] = &nfs_v4_2_minor_ops,
8850 ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
8852 ssize_t error, error2;
8854 error = generic_listxattr(dentry, list, size);
8862 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
8865 return error + error2;
8868 static const struct inode_operations nfs4_dir_inode_operations = {
8869 .create = nfs_create,
8870 .lookup = nfs_lookup,
8871 .atomic_open = nfs_atomic_open,
8873 .unlink = nfs_unlink,
8874 .symlink = nfs_symlink,
8878 .rename = nfs_rename,
8879 .permission = nfs_permission,
8880 .getattr = nfs_getattr,
8881 .setattr = nfs_setattr,
8882 .getxattr = generic_getxattr,
8883 .setxattr = generic_setxattr,
8884 .listxattr = nfs4_listxattr,
8885 .removexattr = generic_removexattr,
8888 static const struct inode_operations nfs4_file_inode_operations = {
8889 .permission = nfs_permission,
8890 .getattr = nfs_getattr,
8891 .setattr = nfs_setattr,
8892 .getxattr = generic_getxattr,
8893 .setxattr = generic_setxattr,
8894 .listxattr = nfs4_listxattr,
8895 .removexattr = generic_removexattr,
8898 const struct nfs_rpc_ops nfs_v4_clientops = {
8899 .version = 4, /* protocol version */
8900 .dentry_ops = &nfs4_dentry_operations,
8901 .dir_inode_ops = &nfs4_dir_inode_operations,
8902 .file_inode_ops = &nfs4_file_inode_operations,
8903 .file_ops = &nfs4_file_operations,
8904 .getroot = nfs4_proc_get_root,
8905 .submount = nfs4_submount,
8906 .try_mount = nfs4_try_mount,
8907 .getattr = nfs4_proc_getattr,
8908 .setattr = nfs4_proc_setattr,
8909 .lookup = nfs4_proc_lookup,
8910 .access = nfs4_proc_access,
8911 .readlink = nfs4_proc_readlink,
8912 .create = nfs4_proc_create,
8913 .remove = nfs4_proc_remove,
8914 .unlink_setup = nfs4_proc_unlink_setup,
8915 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
8916 .unlink_done = nfs4_proc_unlink_done,
8917 .rename_setup = nfs4_proc_rename_setup,
8918 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
8919 .rename_done = nfs4_proc_rename_done,
8920 .link = nfs4_proc_link,
8921 .symlink = nfs4_proc_symlink,
8922 .mkdir = nfs4_proc_mkdir,
8923 .rmdir = nfs4_proc_remove,
8924 .readdir = nfs4_proc_readdir,
8925 .mknod = nfs4_proc_mknod,
8926 .statfs = nfs4_proc_statfs,
8927 .fsinfo = nfs4_proc_fsinfo,
8928 .pathconf = nfs4_proc_pathconf,
8929 .set_capabilities = nfs4_server_capabilities,
8930 .decode_dirent = nfs4_decode_dirent,
8931 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
8932 .read_setup = nfs4_proc_read_setup,
8933 .read_done = nfs4_read_done,
8934 .write_setup = nfs4_proc_write_setup,
8935 .write_done = nfs4_write_done,
8936 .commit_setup = nfs4_proc_commit_setup,
8937 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
8938 .commit_done = nfs4_commit_done,
8939 .lock = nfs4_proc_lock,
8940 .clear_acl_cache = nfs4_zap_acl_attr,
8941 .close_context = nfs4_close_context,
8942 .open_context = nfs4_atomic_open,
8943 .have_delegation = nfs4_have_delegation,
8944 .return_delegation = nfs4_inode_return_delegation,
8945 .alloc_client = nfs4_alloc_client,
8946 .init_client = nfs4_init_client,
8947 .free_client = nfs4_free_client,
8948 .create_server = nfs4_create_server,
8949 .clone_server = nfs_clone_server,
8952 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
8953 .name = XATTR_NAME_NFSV4_ACL,
8954 .list = nfs4_xattr_list_nfs4_acl,
8955 .get = nfs4_xattr_get_nfs4_acl,
8956 .set = nfs4_xattr_set_nfs4_acl,
8959 const struct xattr_handler *nfs4_xattr_handlers[] = {
8960 &nfs4_xattr_nfs4_acl_handler,
8961 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
8962 &nfs4_xattr_nfs4_label_handler,