4 * Client-side procedure declarations for NFSv4.
6 * Copyright (c) 2002 The Regents of the University of Michigan.
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <linux/delay.h>
40 #include <linux/errno.h>
41 #include <linux/string.h>
42 #include <linux/ratelimit.h>
43 #include <linux/printk.h>
44 #include <linux/slab.h>
45 #include <linux/sunrpc/clnt.h>
46 #include <linux/nfs.h>
47 #include <linux/nfs4.h>
48 #include <linux/nfs_fs.h>
49 #include <linux/nfs_page.h>
50 #include <linux/nfs_mount.h>
51 #include <linux/namei.h>
52 #include <linux/mount.h>
53 #include <linux/module.h>
54 #include <linux/xattr.h>
55 #include <linux/utsname.h>
56 #include <linux/freezer.h>
59 #include "delegation.h"
65 #include "nfs4idmap.h"
66 #include "nfs4session.h"
69 #include "nfs4trace.h"
71 #define NFSDBG_FACILITY NFSDBG_PROC
73 #define NFS4_POLL_RETRY_MIN (HZ/10)
74 #define NFS4_POLL_RETRY_MAX (15*HZ)
76 /* file attributes which can be mapped to nfs attributes */
77 #define NFS4_VALID_ATTRS (ATTR_MODE \
88 static int _nfs4_proc_open(struct nfs4_opendata *data);
89 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
90 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
91 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
92 static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
93 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label);
94 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
95 struct nfs_fattr *fattr, struct iattr *sattr,
96 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
97 struct nfs4_label *olabel);
98 #ifdef CONFIG_NFS_V4_1
99 static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
101 static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
102 struct rpc_cred *, bool);
105 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
106 static inline struct nfs4_label *
107 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
108 struct iattr *sattr, struct nfs4_label *label)
115 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
118 err = security_dentry_init_security(dentry, sattr->ia_mode,
119 &dentry->d_name, (void **)&label->label, &label->len);
126 nfs4_label_release_security(struct nfs4_label *label)
129 security_release_secctx(label->label, label->len);
131 static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
134 return server->attr_bitmask;
136 return server->attr_bitmask_nl;
139 static inline struct nfs4_label *
140 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
141 struct iattr *sattr, struct nfs4_label *l)
144 nfs4_label_release_security(struct nfs4_label *label)
147 nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
148 { return server->attr_bitmask; }
151 /* Prevent leaks of NFSv4 errors into userland */
152 static int nfs4_map_errors(int err)
157 case -NFS4ERR_RESOURCE:
158 case -NFS4ERR_LAYOUTTRYLATER:
159 case -NFS4ERR_RECALLCONFLICT:
161 case -NFS4ERR_WRONGSEC:
162 case -NFS4ERR_WRONG_CRED:
164 case -NFS4ERR_BADOWNER:
165 case -NFS4ERR_BADNAME:
167 case -NFS4ERR_SHARE_DENIED:
169 case -NFS4ERR_MINOR_VERS_MISMATCH:
170 return -EPROTONOSUPPORT;
171 case -NFS4ERR_FILE_OPEN:
174 dprintk("%s could not handle NFSv4 error %d\n",
182 * This is our standard bitmap for GETATTR requests.
184 const u32 nfs4_fattr_bitmap[3] = {
186 | FATTR4_WORD0_CHANGE
189 | FATTR4_WORD0_FILEID,
191 | FATTR4_WORD1_NUMLINKS
193 | FATTR4_WORD1_OWNER_GROUP
194 | FATTR4_WORD1_RAWDEV
195 | FATTR4_WORD1_SPACE_USED
196 | FATTR4_WORD1_TIME_ACCESS
197 | FATTR4_WORD1_TIME_METADATA
198 | FATTR4_WORD1_TIME_MODIFY
199 | FATTR4_WORD1_MOUNTED_ON_FILEID,
200 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
201 FATTR4_WORD2_SECURITY_LABEL
205 static const u32 nfs4_pnfs_open_bitmap[3] = {
207 | FATTR4_WORD0_CHANGE
210 | FATTR4_WORD0_FILEID,
212 | FATTR4_WORD1_NUMLINKS
214 | FATTR4_WORD1_OWNER_GROUP
215 | FATTR4_WORD1_RAWDEV
216 | FATTR4_WORD1_SPACE_USED
217 | FATTR4_WORD1_TIME_ACCESS
218 | FATTR4_WORD1_TIME_METADATA
219 | FATTR4_WORD1_TIME_MODIFY,
220 FATTR4_WORD2_MDSTHRESHOLD
221 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
222 | FATTR4_WORD2_SECURITY_LABEL
226 static const u32 nfs4_open_noattr_bitmap[3] = {
228 | FATTR4_WORD0_FILEID,
231 const u32 nfs4_statfs_bitmap[3] = {
232 FATTR4_WORD0_FILES_AVAIL
233 | FATTR4_WORD0_FILES_FREE
234 | FATTR4_WORD0_FILES_TOTAL,
235 FATTR4_WORD1_SPACE_AVAIL
236 | FATTR4_WORD1_SPACE_FREE
237 | FATTR4_WORD1_SPACE_TOTAL
240 const u32 nfs4_pathconf_bitmap[3] = {
242 | FATTR4_WORD0_MAXNAME,
246 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
247 | FATTR4_WORD0_MAXREAD
248 | FATTR4_WORD0_MAXWRITE
249 | FATTR4_WORD0_LEASE_TIME,
250 FATTR4_WORD1_TIME_DELTA
251 | FATTR4_WORD1_FS_LAYOUT_TYPES,
252 FATTR4_WORD2_LAYOUT_BLKSIZE
253 | FATTR4_WORD2_CLONE_BLKSIZE
256 const u32 nfs4_fs_locations_bitmap[3] = {
258 | FATTR4_WORD0_CHANGE
261 | FATTR4_WORD0_FILEID
262 | FATTR4_WORD0_FS_LOCATIONS,
264 | FATTR4_WORD1_NUMLINKS
266 | FATTR4_WORD1_OWNER_GROUP
267 | FATTR4_WORD1_RAWDEV
268 | FATTR4_WORD1_SPACE_USED
269 | FATTR4_WORD1_TIME_ACCESS
270 | FATTR4_WORD1_TIME_METADATA
271 | FATTR4_WORD1_TIME_MODIFY
272 | FATTR4_WORD1_MOUNTED_ON_FILEID,
275 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
276 struct nfs4_readdir_arg *readdir)
281 readdir->cookie = cookie;
282 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
287 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
292 * NFSv4 servers do not return entries for '.' and '..'
293 * Therefore, we fake these entries here. We let '.'
294 * have cookie 0 and '..' have cookie 1. Note that
295 * when talking to the server, we always send cookie 0
298 start = p = kmap_atomic(*readdir->pages);
301 *p++ = xdr_one; /* next */
302 *p++ = xdr_zero; /* cookie, first word */
303 *p++ = xdr_one; /* cookie, second word */
304 *p++ = xdr_one; /* entry len */
305 memcpy(p, ".\0\0\0", 4); /* entry */
307 *p++ = xdr_one; /* bitmap length */
308 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
309 *p++ = htonl(8); /* attribute buffer length */
310 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
313 *p++ = xdr_one; /* next */
314 *p++ = xdr_zero; /* cookie, first word */
315 *p++ = xdr_two; /* cookie, second word */
316 *p++ = xdr_two; /* entry len */
317 memcpy(p, "..\0\0", 4); /* entry */
319 *p++ = xdr_one; /* bitmap length */
320 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
321 *p++ = htonl(8); /* attribute buffer length */
322 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
324 readdir->pgbase = (char *)p - (char *)start;
325 readdir->count -= readdir->pgbase;
326 kunmap_atomic(start);
329 static void nfs4_test_and_free_stateid(struct nfs_server *server,
330 nfs4_stateid *stateid,
331 struct rpc_cred *cred)
333 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
335 ops->test_and_free_expired(server, stateid, cred);
338 static void __nfs4_free_revoked_stateid(struct nfs_server *server,
339 nfs4_stateid *stateid,
340 struct rpc_cred *cred)
342 stateid->type = NFS4_REVOKED_STATEID_TYPE;
343 nfs4_test_and_free_stateid(server, stateid, cred);
346 static void nfs4_free_revoked_stateid(struct nfs_server *server,
347 const nfs4_stateid *stateid,
348 struct rpc_cred *cred)
352 nfs4_stateid_copy(&tmp, stateid);
353 __nfs4_free_revoked_stateid(server, &tmp, cred);
356 static long nfs4_update_delay(long *timeout)
360 return NFS4_POLL_RETRY_MAX;
362 *timeout = NFS4_POLL_RETRY_MIN;
363 if (*timeout > NFS4_POLL_RETRY_MAX)
364 *timeout = NFS4_POLL_RETRY_MAX;
370 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
376 freezable_schedule_timeout_killable_unsafe(
377 nfs4_update_delay(timeout));
378 if (fatal_signal_pending(current))
383 /* This is the error handling routine for processes that are allowed
386 static int nfs4_do_handle_exception(struct nfs_server *server,
387 int errorcode, struct nfs4_exception *exception)
389 struct nfs_client *clp = server->nfs_client;
390 struct nfs4_state *state = exception->state;
391 const nfs4_stateid *stateid = exception->stateid;
392 struct inode *inode = exception->inode;
395 exception->delay = 0;
396 exception->recovering = 0;
397 exception->retry = 0;
399 if (stateid == NULL && state != NULL)
400 stateid = &state->stateid;
405 case -NFS4ERR_DELEG_REVOKED:
406 case -NFS4ERR_ADMIN_REVOKED:
407 case -NFS4ERR_EXPIRED:
408 case -NFS4ERR_BAD_STATEID:
409 if (inode != NULL && stateid != NULL) {
410 nfs_inode_find_state_and_recover(inode,
412 goto wait_on_recovery;
414 case -NFS4ERR_OPENMODE:
418 err = nfs_async_inode_return_delegation(inode,
421 goto wait_on_recovery;
422 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
423 exception->retry = 1;
429 ret = nfs4_schedule_stateid_recovery(server, state);
432 goto wait_on_recovery;
433 case -NFS4ERR_STALE_STATEID:
434 case -NFS4ERR_STALE_CLIENTID:
435 nfs4_schedule_lease_recovery(clp);
436 goto wait_on_recovery;
438 ret = nfs4_schedule_migration_recovery(server);
441 goto wait_on_recovery;
442 case -NFS4ERR_LEASE_MOVED:
443 nfs4_schedule_lease_moved_recovery(clp);
444 goto wait_on_recovery;
445 #if defined(CONFIG_NFS_V4_1)
446 case -NFS4ERR_BADSESSION:
447 case -NFS4ERR_BADSLOT:
448 case -NFS4ERR_BAD_HIGH_SLOT:
449 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
450 case -NFS4ERR_DEADSESSION:
451 case -NFS4ERR_SEQ_FALSE_RETRY:
452 case -NFS4ERR_SEQ_MISORDERED:
453 dprintk("%s ERROR: %d Reset session\n", __func__,
455 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
456 goto wait_on_recovery;
457 #endif /* defined(CONFIG_NFS_V4_1) */
458 case -NFS4ERR_FILE_OPEN:
459 if (exception->timeout > HZ) {
460 /* We have retried a decent amount, time to
467 nfs_inc_server_stats(server, NFSIOS_DELAY);
469 case -NFS4ERR_LAYOUTTRYLATER:
470 case -NFS4ERR_RECALLCONFLICT:
471 exception->delay = 1;
474 case -NFS4ERR_RETRY_UNCACHED_REP:
475 case -NFS4ERR_OLD_STATEID:
476 exception->retry = 1;
478 case -NFS4ERR_BADOWNER:
479 /* The following works around a Linux server bug! */
480 case -NFS4ERR_BADNAME:
481 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
482 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
483 exception->retry = 1;
484 printk(KERN_WARNING "NFS: v4 server %s "
485 "does not accept raw "
487 "Reenabling the idmapper.\n",
488 server->nfs_client->cl_hostname);
491 /* We failed to handle the error */
492 return nfs4_map_errors(ret);
494 exception->recovering = 1;
498 /* This is the error handling routine for processes that are allowed
501 int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
503 struct nfs_client *clp = server->nfs_client;
506 ret = nfs4_do_handle_exception(server, errorcode, exception);
507 if (exception->delay) {
508 ret = nfs4_delay(server->client, &exception->timeout);
511 if (exception->recovering) {
512 ret = nfs4_wait_clnt_recover(clp);
513 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
520 exception->retry = 1;
525 nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
526 int errorcode, struct nfs4_exception *exception)
528 struct nfs_client *clp = server->nfs_client;
531 ret = nfs4_do_handle_exception(server, errorcode, exception);
532 if (exception->delay) {
533 rpc_delay(task, nfs4_update_delay(&exception->timeout));
536 if (exception->recovering) {
537 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
538 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
539 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
542 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
547 exception->retry = 1;
552 nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
553 struct nfs4_state *state, long *timeout)
555 struct nfs4_exception exception = {
559 if (task->tk_status >= 0)
562 exception.timeout = *timeout;
563 task->tk_status = nfs4_async_handle_exception(task, server,
566 if (exception.delay && timeout)
567 *timeout = exception.timeout;
574 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
575 * or 'false' otherwise.
577 static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
579 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
581 if (flavor == RPC_AUTH_GSS_KRB5I ||
582 flavor == RPC_AUTH_GSS_KRB5P)
588 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
590 spin_lock(&clp->cl_lock);
591 if (time_before(clp->cl_last_renewal,timestamp))
592 clp->cl_last_renewal = timestamp;
593 spin_unlock(&clp->cl_lock);
596 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
598 struct nfs_client *clp = server->nfs_client;
600 if (!nfs4_has_session(clp))
601 do_renew_lease(clp, timestamp);
604 struct nfs4_call_sync_data {
605 const struct nfs_server *seq_server;
606 struct nfs4_sequence_args *seq_args;
607 struct nfs4_sequence_res *seq_res;
610 void nfs4_init_sequence(struct nfs4_sequence_args *args,
611 struct nfs4_sequence_res *res, int cache_reply)
613 args->sa_slot = NULL;
614 args->sa_cache_this = cache_reply;
615 args->sa_privileged = 0;
620 static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
622 args->sa_privileged = 1;
625 int nfs40_setup_sequence(struct nfs4_slot_table *tbl,
626 struct nfs4_sequence_args *args,
627 struct nfs4_sequence_res *res,
628 struct rpc_task *task)
630 struct nfs4_slot *slot;
632 /* slot already allocated? */
633 if (res->sr_slot != NULL)
636 spin_lock(&tbl->slot_tbl_lock);
637 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
640 slot = nfs4_alloc_slot(tbl);
642 if (slot == ERR_PTR(-ENOMEM))
643 task->tk_timeout = HZ >> 2;
646 spin_unlock(&tbl->slot_tbl_lock);
648 slot->privileged = args->sa_privileged ? 1 : 0;
649 args->sa_slot = slot;
653 rpc_call_start(task);
657 if (args->sa_privileged)
658 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
659 NULL, RPC_PRIORITY_PRIVILEGED);
661 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
662 spin_unlock(&tbl->slot_tbl_lock);
665 EXPORT_SYMBOL_GPL(nfs40_setup_sequence);
667 static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
669 struct nfs4_slot *slot = res->sr_slot;
670 struct nfs4_slot_table *tbl;
673 spin_lock(&tbl->slot_tbl_lock);
674 if (!nfs41_wake_and_assign_slot(tbl, slot))
675 nfs4_free_slot(tbl, slot);
676 spin_unlock(&tbl->slot_tbl_lock);
681 static int nfs40_sequence_done(struct rpc_task *task,
682 struct nfs4_sequence_res *res)
684 if (res->sr_slot != NULL)
685 nfs40_sequence_free_slot(res);
689 #if defined(CONFIG_NFS_V4_1)
691 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
693 struct nfs4_session *session;
694 struct nfs4_slot_table *tbl;
695 struct nfs4_slot *slot = res->sr_slot;
696 bool send_new_highest_used_slotid = false;
699 session = tbl->session;
701 /* Bump the slot sequence number */
706 spin_lock(&tbl->slot_tbl_lock);
707 /* Be nice to the server: try to ensure that the last transmitted
708 * value for highest_user_slotid <= target_highest_slotid
710 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
711 send_new_highest_used_slotid = true;
713 if (nfs41_wake_and_assign_slot(tbl, slot)) {
714 send_new_highest_used_slotid = false;
717 nfs4_free_slot(tbl, slot);
719 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
720 send_new_highest_used_slotid = false;
722 spin_unlock(&tbl->slot_tbl_lock);
724 if (send_new_highest_used_slotid)
725 nfs41_notify_server(session->clp);
726 if (waitqueue_active(&tbl->slot_waitq))
727 wake_up_all(&tbl->slot_waitq);
730 static int nfs41_sequence_process(struct rpc_task *task,
731 struct nfs4_sequence_res *res)
733 struct nfs4_session *session;
734 struct nfs4_slot *slot = res->sr_slot;
735 struct nfs_client *clp;
736 bool interrupted = false;
741 /* don't increment the sequence number if the task wasn't sent */
742 if (!RPC_WAS_SENT(task))
745 session = slot->table->session;
747 if (slot->interrupted) {
748 slot->interrupted = 0;
752 trace_nfs4_sequence_done(session, res);
753 /* Check the SEQUENCE operation status */
754 switch (res->sr_status) {
756 /* If previous op on slot was interrupted and we reused
757 * the seq# and got a reply from the cache, then retry
759 if (task->tk_status == -EREMOTEIO && interrupted) {
763 /* Update the slot's sequence and clientid lease timer */
766 do_renew_lease(clp, res->sr_timestamp);
767 /* Check sequence flags */
768 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
770 nfs41_update_target_slotid(slot->table, slot, res);
774 * sr_status remains 1 if an RPC level error occurred.
775 * The server may or may not have processed the sequence
777 * Mark the slot as having hosted an interrupted RPC call.
779 slot->interrupted = 1;
782 /* The server detected a resend of the RPC call and
783 * returned NFS4ERR_DELAY as per Section 2.10.6.2
786 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
791 case -NFS4ERR_BADSLOT:
793 * The slot id we used was probably retired. Try again
794 * using a different slot id.
797 case -NFS4ERR_SEQ_MISORDERED:
799 * Was the last operation on this sequence interrupted?
800 * If so, retry after bumping the sequence number.
807 * Could this slot have been previously retired?
808 * If so, then the server may be expecting seq_nr = 1!
810 if (slot->seq_nr != 1) {
815 case -NFS4ERR_SEQ_FALSE_RETRY:
818 case -NFS4ERR_DEADSESSION:
819 case -NFS4ERR_BADSESSION:
820 nfs4_schedule_session_recovery(session, res->sr_status);
823 /* Just update the slot sequence no. */
827 /* The session may be reset by one of the error handlers. */
828 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
832 if (rpc_restart_call_prepare(task)) {
833 nfs41_sequence_free_slot(res);
839 if (!rpc_restart_call(task))
841 rpc_delay(task, NFS4_POLL_RETRY_MAX);
845 int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
847 if (!nfs41_sequence_process(task, res))
849 if (res->sr_slot != NULL)
850 nfs41_sequence_free_slot(res);
854 EXPORT_SYMBOL_GPL(nfs41_sequence_done);
856 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
858 if (res->sr_slot == NULL)
860 if (res->sr_slot->table->session != NULL)
861 return nfs41_sequence_process(task, res);
862 return nfs40_sequence_done(task, res);
865 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
867 if (res->sr_slot != NULL) {
868 if (res->sr_slot->table->session != NULL)
869 nfs41_sequence_free_slot(res);
871 nfs40_sequence_free_slot(res);
875 int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
877 if (res->sr_slot == NULL)
879 if (!res->sr_slot->table->session)
880 return nfs40_sequence_done(task, res);
881 return nfs41_sequence_done(task, res);
883 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
885 int nfs41_setup_sequence(struct nfs4_session *session,
886 struct nfs4_sequence_args *args,
887 struct nfs4_sequence_res *res,
888 struct rpc_task *task)
890 struct nfs4_slot *slot;
891 struct nfs4_slot_table *tbl;
893 dprintk("--> %s\n", __func__);
894 /* slot already allocated? */
895 if (res->sr_slot != NULL)
898 tbl = &session->fc_slot_table;
900 task->tk_timeout = 0;
902 spin_lock(&tbl->slot_tbl_lock);
903 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
904 !args->sa_privileged) {
905 /* The state manager will wait until the slot table is empty */
906 dprintk("%s session is draining\n", __func__);
910 slot = nfs4_alloc_slot(tbl);
912 /* If out of memory, try again in 1/4 second */
913 if (slot == ERR_PTR(-ENOMEM))
914 task->tk_timeout = HZ >> 2;
915 dprintk("<-- %s: no free slots\n", __func__);
918 spin_unlock(&tbl->slot_tbl_lock);
920 slot->privileged = args->sa_privileged ? 1 : 0;
921 args->sa_slot = slot;
923 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
924 slot->slot_nr, slot->seq_nr);
927 res->sr_timestamp = jiffies;
928 res->sr_status_flags = 0;
930 * sr_status is only set in decode_sequence, and so will remain
931 * set to 1 if an rpc level failure occurs.
934 trace_nfs4_setup_sequence(session, args);
936 rpc_call_start(task);
939 /* Privileged tasks are queued with top priority */
940 if (args->sa_privileged)
941 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
942 NULL, RPC_PRIORITY_PRIVILEGED);
944 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
945 spin_unlock(&tbl->slot_tbl_lock);
948 EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
950 static int nfs4_setup_sequence(const struct nfs_server *server,
951 struct nfs4_sequence_args *args,
952 struct nfs4_sequence_res *res,
953 struct rpc_task *task)
955 struct nfs4_session *session = nfs4_get_session(server);
959 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
962 dprintk("--> %s clp %p session %p sr_slot %u\n",
963 __func__, session->clp, session, res->sr_slot ?
964 res->sr_slot->slot_nr : NFS4_NO_SLOT);
966 ret = nfs41_setup_sequence(session, args, res, task);
968 dprintk("<-- %s status=%d\n", __func__, ret);
972 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
974 struct nfs4_call_sync_data *data = calldata;
975 struct nfs4_session *session = nfs4_get_session(data->seq_server);
977 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
979 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
982 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
984 struct nfs4_call_sync_data *data = calldata;
986 nfs41_sequence_done(task, data->seq_res);
989 static const struct rpc_call_ops nfs41_call_sync_ops = {
990 .rpc_call_prepare = nfs41_call_sync_prepare,
991 .rpc_call_done = nfs41_call_sync_done,
994 #else /* !CONFIG_NFS_V4_1 */
996 static int nfs4_setup_sequence(const struct nfs_server *server,
997 struct nfs4_sequence_args *args,
998 struct nfs4_sequence_res *res,
999 struct rpc_task *task)
1001 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
1005 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
1007 return nfs40_sequence_done(task, res);
1010 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
1012 if (res->sr_slot != NULL)
1013 nfs40_sequence_free_slot(res);
1016 int nfs4_sequence_done(struct rpc_task *task,
1017 struct nfs4_sequence_res *res)
1019 return nfs40_sequence_done(task, res);
1021 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
1023 #endif /* !CONFIG_NFS_V4_1 */
1025 static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1027 struct nfs4_call_sync_data *data = calldata;
1028 nfs4_setup_sequence(data->seq_server,
1029 data->seq_args, data->seq_res, task);
1032 static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1034 struct nfs4_call_sync_data *data = calldata;
1035 nfs4_sequence_done(task, data->seq_res);
1038 static const struct rpc_call_ops nfs40_call_sync_ops = {
1039 .rpc_call_prepare = nfs40_call_sync_prepare,
1040 .rpc_call_done = nfs40_call_sync_done,
1043 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
1044 struct nfs_server *server,
1045 struct rpc_message *msg,
1046 struct nfs4_sequence_args *args,
1047 struct nfs4_sequence_res *res)
1050 struct rpc_task *task;
1051 struct nfs_client *clp = server->nfs_client;
1052 struct nfs4_call_sync_data data = {
1053 .seq_server = server,
1057 struct rpc_task_setup task_setup = {
1060 .callback_ops = clp->cl_mvops->call_sync_ops,
1061 .callback_data = &data
1064 task = rpc_run_task(&task_setup);
1066 ret = PTR_ERR(task);
1068 ret = task->tk_status;
1074 int nfs4_call_sync(struct rpc_clnt *clnt,
1075 struct nfs_server *server,
1076 struct rpc_message *msg,
1077 struct nfs4_sequence_args *args,
1078 struct nfs4_sequence_res *res,
1081 nfs4_init_sequence(args, res, cache_reply);
1082 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
1085 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1086 unsigned long timestamp)
1088 struct nfs_inode *nfsi = NFS_I(dir);
1090 spin_lock(&dir->i_lock);
1091 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
1092 if (cinfo->atomic && cinfo->before == dir->i_version) {
1093 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1094 nfsi->attrtimeo_timestamp = jiffies;
1096 nfs_force_lookup_revalidate(dir);
1097 if (cinfo->before != dir->i_version)
1098 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1099 NFS_INO_INVALID_ACL;
1101 dir->i_version = cinfo->after;
1102 nfsi->read_cache_jiffies = timestamp;
1103 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
1104 nfs_fscache_invalidate(dir);
1105 spin_unlock(&dir->i_lock);
1108 struct nfs4_opendata {
1110 struct nfs_openargs o_arg;
1111 struct nfs_openres o_res;
1112 struct nfs_open_confirmargs c_arg;
1113 struct nfs_open_confirmres c_res;
1114 struct nfs4_string owner_name;
1115 struct nfs4_string group_name;
1116 struct nfs4_label *a_label;
1117 struct nfs_fattr f_attr;
1118 struct nfs4_label *f_label;
1120 struct dentry *dentry;
1121 struct nfs4_state_owner *owner;
1122 struct nfs4_state *state;
1124 unsigned long timestamp;
1125 unsigned int rpc_done : 1;
1126 unsigned int file_created : 1;
1127 unsigned int is_recover : 1;
1132 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1133 int err, struct nfs4_exception *exception)
1137 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1139 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1140 exception->retry = 1;
1145 nfs4_map_atomic_open_share(struct nfs_server *server,
1146 fmode_t fmode, int openflags)
1150 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1152 res = NFS4_SHARE_ACCESS_READ;
1155 res = NFS4_SHARE_ACCESS_WRITE;
1157 case FMODE_READ|FMODE_WRITE:
1158 res = NFS4_SHARE_ACCESS_BOTH;
1160 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1162 /* Want no delegation if we're using O_DIRECT */
1163 if (openflags & O_DIRECT)
1164 res |= NFS4_SHARE_WANT_NO_DELEG;
1169 static enum open_claim_type4
1170 nfs4_map_atomic_open_claim(struct nfs_server *server,
1171 enum open_claim_type4 claim)
1173 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1178 case NFS4_OPEN_CLAIM_FH:
1179 return NFS4_OPEN_CLAIM_NULL;
1180 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1181 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1182 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1183 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1187 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
1189 p->o_res.f_attr = &p->f_attr;
1190 p->o_res.f_label = p->f_label;
1191 p->o_res.seqid = p->o_arg.seqid;
1192 p->c_res.seqid = p->c_arg.seqid;
1193 p->o_res.server = p->o_arg.server;
1194 p->o_res.access_request = p->o_arg.access;
1195 nfs_fattr_init(&p->f_attr);
1196 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
1199 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
1200 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
1201 const struct iattr *attrs,
1202 struct nfs4_label *label,
1203 enum open_claim_type4 claim,
1206 struct dentry *parent = dget_parent(dentry);
1207 struct inode *dir = d_inode(parent);
1208 struct nfs_server *server = NFS_SERVER(dir);
1209 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
1210 struct nfs4_opendata *p;
1212 p = kzalloc(sizeof(*p), gfp_mask);
1216 p->f_label = nfs4_label_alloc(server, gfp_mask);
1217 if (IS_ERR(p->f_label))
1220 p->a_label = nfs4_label_alloc(server, gfp_mask);
1221 if (IS_ERR(p->a_label))
1224 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1225 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
1226 if (IS_ERR(p->o_arg.seqid))
1227 goto err_free_label;
1228 nfs_sb_active(dentry->d_sb);
1229 p->dentry = dget(dentry);
1232 atomic_inc(&sp->so_count);
1233 p->o_arg.open_flags = flags;
1234 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
1235 p->o_arg.umask = current_umask();
1236 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1237 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1239 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1240 * will return permission denied for all bits until close */
1241 if (!(flags & O_EXCL)) {
1242 /* ask server to check for all possible rights as results
1244 switch (p->o_arg.claim) {
1247 case NFS4_OPEN_CLAIM_NULL:
1248 case NFS4_OPEN_CLAIM_FH:
1249 p->o_arg.access = NFS4_ACCESS_READ |
1250 NFS4_ACCESS_MODIFY |
1251 NFS4_ACCESS_EXTEND |
1252 NFS4_ACCESS_EXECUTE;
1255 p->o_arg.clientid = server->nfs_client->cl_clientid;
1256 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1257 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1258 p->o_arg.name = &dentry->d_name;
1259 p->o_arg.server = server;
1260 p->o_arg.bitmask = nfs4_bitmask(server, label);
1261 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1262 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1263 switch (p->o_arg.claim) {
1264 case NFS4_OPEN_CLAIM_NULL:
1265 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1266 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1267 p->o_arg.fh = NFS_FH(dir);
1269 case NFS4_OPEN_CLAIM_PREVIOUS:
1270 case NFS4_OPEN_CLAIM_FH:
1271 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1272 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1273 p->o_arg.fh = NFS_FH(d_inode(dentry));
1275 if (attrs != NULL && attrs->ia_valid != 0) {
1278 p->o_arg.u.attrs = &p->attrs;
1279 memcpy(&p->attrs, attrs, sizeof(p->attrs));
1282 verf[1] = current->pid;
1283 memcpy(p->o_arg.u.verifier.data, verf,
1284 sizeof(p->o_arg.u.verifier.data));
1286 p->c_arg.fh = &p->o_res.fh;
1287 p->c_arg.stateid = &p->o_res.stateid;
1288 p->c_arg.seqid = p->o_arg.seqid;
1289 nfs4_init_opendata_res(p);
1290 kref_init(&p->kref);
1294 nfs4_label_free(p->a_label);
1296 nfs4_label_free(p->f_label);
1304 static void nfs4_opendata_free(struct kref *kref)
1306 struct nfs4_opendata *p = container_of(kref,
1307 struct nfs4_opendata, kref);
1308 struct super_block *sb = p->dentry->d_sb;
1310 nfs_free_seqid(p->o_arg.seqid);
1311 nfs4_sequence_free_slot(&p->o_res.seq_res);
1312 if (p->state != NULL)
1313 nfs4_put_open_state(p->state);
1314 nfs4_put_state_owner(p->owner);
1316 nfs4_label_free(p->a_label);
1317 nfs4_label_free(p->f_label);
1321 nfs_sb_deactive(sb);
1322 nfs_fattr_free_names(&p->f_attr);
1323 kfree(p->f_attr.mdsthreshold);
1327 static void nfs4_opendata_put(struct nfs4_opendata *p)
1330 kref_put(&p->kref, nfs4_opendata_free);
1333 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1337 ret = rpc_wait_for_completion_task(task);
1341 static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1344 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1345 case FMODE_READ|FMODE_WRITE:
1346 return state->n_rdwr != 0;
1348 return state->n_wronly != 0;
1350 return state->n_rdonly != 0;
1356 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
1360 if (open_mode & (O_EXCL|O_TRUNC))
1362 switch (mode & (FMODE_READ|FMODE_WRITE)) {
1364 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1365 && state->n_rdonly != 0;
1368 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1369 && state->n_wronly != 0;
1371 case FMODE_READ|FMODE_WRITE:
1372 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1373 && state->n_rdwr != 0;
1379 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1380 enum open_claim_type4 claim)
1382 if (delegation == NULL)
1384 if ((delegation->type & fmode) != fmode)
1386 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1389 case NFS4_OPEN_CLAIM_NULL:
1390 case NFS4_OPEN_CLAIM_FH:
1392 case NFS4_OPEN_CLAIM_PREVIOUS:
1393 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1398 nfs_mark_delegation_referenced(delegation);
1402 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1411 case FMODE_READ|FMODE_WRITE:
1414 nfs4_state_set_mode_locked(state, state->state | fmode);
1417 #ifdef CONFIG_NFS_V4_1
1418 static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1420 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1422 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1424 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1428 #endif /* CONFIG_NFS_V4_1 */
1430 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
1432 struct nfs_client *clp = state->owner->so_server->nfs_client;
1433 bool need_recover = false;
1435 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1436 need_recover = true;
1437 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1438 need_recover = true;
1439 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1440 need_recover = true;
1442 nfs4_state_mark_reclaim_nograce(clp, state);
1445 static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1446 const nfs4_stateid *stateid, nfs4_stateid *freeme)
1448 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1450 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1451 nfs4_stateid_copy(freeme, &state->open_stateid);
1452 nfs_test_and_clear_all_open_stateid(state);
1455 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1460 static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1462 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1464 if (state->n_wronly)
1465 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1466 if (state->n_rdonly)
1467 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1469 set_bit(NFS_O_RDWR_STATE, &state->flags);
1470 set_bit(NFS_OPEN_STATE, &state->flags);
1473 static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1474 nfs4_stateid *stateid, fmode_t fmode)
1476 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1477 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1479 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1482 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1485 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1486 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1487 clear_bit(NFS_OPEN_STATE, &state->flags);
1489 if (stateid == NULL)
1491 /* Handle OPEN+OPEN_DOWNGRADE races */
1492 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1493 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1494 nfs_resync_open_stateid_locked(state);
1497 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1498 nfs4_stateid_copy(&state->stateid, stateid);
1499 nfs4_stateid_copy(&state->open_stateid, stateid);
1502 static void nfs_clear_open_stateid(struct nfs4_state *state,
1503 nfs4_stateid *arg_stateid,
1504 nfs4_stateid *stateid, fmode_t fmode)
1506 write_seqlock(&state->seqlock);
1507 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1508 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1509 nfs_clear_open_stateid_locked(state, stateid, fmode);
1510 write_sequnlock(&state->seqlock);
1511 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1512 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1515 static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1516 const nfs4_stateid *stateid, fmode_t fmode,
1517 nfs4_stateid *freeme)
1521 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1524 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1526 case FMODE_READ|FMODE_WRITE:
1527 set_bit(NFS_O_RDWR_STATE, &state->flags);
1529 if (!nfs_need_update_open_stateid(state, stateid, freeme))
1531 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1532 nfs4_stateid_copy(&state->stateid, stateid);
1533 nfs4_stateid_copy(&state->open_stateid, stateid);
1536 static void __update_open_stateid(struct nfs4_state *state,
1537 const nfs4_stateid *open_stateid,
1538 const nfs4_stateid *deleg_stateid,
1540 nfs4_stateid *freeme)
1543 * Protect the call to nfs4_state_set_mode_locked and
1544 * serialise the stateid update
1546 spin_lock(&state->owner->so_lock);
1547 write_seqlock(&state->seqlock);
1548 if (deleg_stateid != NULL) {
1549 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1550 set_bit(NFS_DELEGATED_STATE, &state->flags);
1552 if (open_stateid != NULL)
1553 nfs_set_open_stateid_locked(state, open_stateid, fmode, freeme);
1554 write_sequnlock(&state->seqlock);
1555 update_open_stateflags(state, fmode);
1556 spin_unlock(&state->owner->so_lock);
1559 static int update_open_stateid(struct nfs4_state *state,
1560 const nfs4_stateid *open_stateid,
1561 const nfs4_stateid *delegation,
1564 struct nfs_server *server = NFS_SERVER(state->inode);
1565 struct nfs_client *clp = server->nfs_client;
1566 struct nfs_inode *nfsi = NFS_I(state->inode);
1567 struct nfs_delegation *deleg_cur;
1568 nfs4_stateid freeme = { };
1571 fmode &= (FMODE_READ|FMODE_WRITE);
1574 deleg_cur = rcu_dereference(nfsi->delegation);
1575 if (deleg_cur == NULL)
1578 spin_lock(&deleg_cur->lock);
1579 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1580 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1581 (deleg_cur->type & fmode) != fmode)
1582 goto no_delegation_unlock;
1584 if (delegation == NULL)
1585 delegation = &deleg_cur->stateid;
1586 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
1587 goto no_delegation_unlock;
1589 nfs_mark_delegation_referenced(deleg_cur);
1590 __update_open_stateid(state, open_stateid, &deleg_cur->stateid,
1593 no_delegation_unlock:
1594 spin_unlock(&deleg_cur->lock);
1598 if (!ret && open_stateid != NULL) {
1599 __update_open_stateid(state, open_stateid, NULL, fmode, &freeme);
1602 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1603 nfs4_schedule_state_manager(clp);
1604 if (freeme.type != 0)
1605 nfs4_test_and_free_stateid(server, &freeme,
1606 state->owner->so_cred);
1611 static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1612 const nfs4_stateid *stateid)
1614 struct nfs4_state *state = lsp->ls_state;
1617 spin_lock(&state->state_lock);
1618 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1620 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1622 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1625 spin_unlock(&state->state_lock);
1629 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1631 struct nfs_delegation *delegation;
1634 delegation = rcu_dereference(NFS_I(inode)->delegation);
1635 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1640 nfs4_inode_return_delegation(inode);
1643 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1645 struct nfs4_state *state = opendata->state;
1646 struct nfs_inode *nfsi = NFS_I(state->inode);
1647 struct nfs_delegation *delegation;
1648 int open_mode = opendata->o_arg.open_flags;
1649 fmode_t fmode = opendata->o_arg.fmode;
1650 enum open_claim_type4 claim = opendata->o_arg.claim;
1651 nfs4_stateid stateid;
1655 spin_lock(&state->owner->so_lock);
1656 if (can_open_cached(state, fmode, open_mode)) {
1657 update_open_stateflags(state, fmode);
1658 spin_unlock(&state->owner->so_lock);
1659 goto out_return_state;
1661 spin_unlock(&state->owner->so_lock);
1663 delegation = rcu_dereference(nfsi->delegation);
1664 if (!can_open_delegated(delegation, fmode, claim)) {
1668 /* Save the delegation */
1669 nfs4_stateid_copy(&stateid, &delegation->stateid);
1671 nfs_release_seqid(opendata->o_arg.seqid);
1672 if (!opendata->is_recover) {
1673 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1679 /* Try to update the stateid using the delegation */
1680 if (update_open_stateid(state, NULL, &stateid, fmode))
1681 goto out_return_state;
1684 return ERR_PTR(ret);
1686 atomic_inc(&state->count);
1691 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1693 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1694 struct nfs_delegation *delegation;
1695 int delegation_flags = 0;
1698 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1700 delegation_flags = delegation->flags;
1702 switch (data->o_arg.claim) {
1705 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1706 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1707 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1708 "returning a delegation for "
1709 "OPEN(CLAIM_DELEGATE_CUR)\n",
1713 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1714 nfs_inode_set_delegation(state->inode,
1715 data->owner->so_cred,
1718 nfs_inode_reclaim_delegation(state->inode,
1719 data->owner->so_cred,
1724 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1725 * and update the nfs4_state.
1727 static struct nfs4_state *
1728 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1730 struct inode *inode = data->state->inode;
1731 struct nfs4_state *state = data->state;
1734 if (!data->rpc_done) {
1735 if (data->rpc_status) {
1736 ret = data->rpc_status;
1739 /* cached opens have already been processed */
1743 ret = nfs_refresh_inode(inode, &data->f_attr);
1747 if (data->o_res.delegation_type != 0)
1748 nfs4_opendata_check_deleg(data, state);
1750 update_open_stateid(state, &data->o_res.stateid, NULL,
1752 atomic_inc(&state->count);
1756 return ERR_PTR(ret);
1760 static struct nfs4_state *
1761 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1763 struct inode *inode;
1764 struct nfs4_state *state = NULL;
1767 if (!data->rpc_done) {
1768 state = nfs4_try_open_cached(data);
1769 trace_nfs4_cached_open(data->state);
1774 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1776 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
1777 ret = PTR_ERR(inode);
1781 state = nfs4_get_open_state(inode, data->owner);
1784 if (data->o_res.delegation_type != 0)
1785 nfs4_opendata_check_deleg(data, state);
1786 update_open_stateid(state, &data->o_res.stateid, NULL,
1790 nfs_release_seqid(data->o_arg.seqid);
1795 return ERR_PTR(ret);
1798 static struct nfs4_state *
1799 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1801 struct nfs4_state *ret;
1803 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1804 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1806 ret = _nfs4_opendata_to_nfs4_state(data);
1807 nfs4_sequence_free_slot(&data->o_res.seq_res);
1811 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1813 struct nfs_inode *nfsi = NFS_I(state->inode);
1814 struct nfs_open_context *ctx;
1816 spin_lock(&state->inode->i_lock);
1817 list_for_each_entry(ctx, &nfsi->open_files, list) {
1818 if (ctx->state != state)
1820 get_nfs_open_context(ctx);
1821 spin_unlock(&state->inode->i_lock);
1824 spin_unlock(&state->inode->i_lock);
1825 return ERR_PTR(-ENOENT);
1828 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1829 struct nfs4_state *state, enum open_claim_type4 claim)
1831 struct nfs4_opendata *opendata;
1833 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
1834 NULL, NULL, claim, GFP_NOFS);
1835 if (opendata == NULL)
1836 return ERR_PTR(-ENOMEM);
1837 opendata->state = state;
1838 atomic_inc(&state->count);
1842 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1845 struct nfs4_state *newstate;
1848 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
1850 opendata->o_arg.open_flags = 0;
1851 opendata->o_arg.fmode = fmode;
1852 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1853 NFS_SB(opendata->dentry->d_sb),
1855 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1856 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1857 nfs4_init_opendata_res(opendata);
1858 ret = _nfs4_recover_proc_open(opendata);
1861 newstate = nfs4_opendata_to_nfs4_state(opendata);
1862 if (IS_ERR(newstate))
1863 return PTR_ERR(newstate);
1864 if (newstate != opendata->state)
1866 nfs4_close_state(newstate, fmode);
1870 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1874 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1875 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1876 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1877 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1878 /* memory barrier prior to reading state->n_* */
1879 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1880 clear_bit(NFS_OPEN_STATE, &state->flags);
1882 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1885 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1888 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1892 * We may have performed cached opens for all three recoveries.
1893 * Check if we need to update the current stateid.
1895 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1896 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
1897 write_seqlock(&state->seqlock);
1898 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1899 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1900 write_sequnlock(&state->seqlock);
1907 * reclaim state on the server after a reboot.
1909 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1911 struct nfs_delegation *delegation;
1912 struct nfs4_opendata *opendata;
1913 fmode_t delegation_type = 0;
1916 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1917 NFS4_OPEN_CLAIM_PREVIOUS);
1918 if (IS_ERR(opendata))
1919 return PTR_ERR(opendata);
1921 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1922 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1923 delegation_type = delegation->type;
1925 opendata->o_arg.u.delegation_type = delegation_type;
1926 status = nfs4_open_recover(opendata, state);
1927 nfs4_opendata_put(opendata);
1931 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1933 struct nfs_server *server = NFS_SERVER(state->inode);
1934 struct nfs4_exception exception = { };
1937 err = _nfs4_do_open_reclaim(ctx, state);
1938 trace_nfs4_open_reclaim(ctx, 0, err);
1939 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1941 if (err != -NFS4ERR_DELAY)
1943 nfs4_handle_exception(server, err, &exception);
1944 } while (exception.retry);
1948 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1950 struct nfs_open_context *ctx;
1953 ctx = nfs4_state_find_open_context(state);
1956 ret = nfs4_do_open_reclaim(ctx, state);
1957 put_nfs_open_context(ctx);
1961 static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, int err)
1965 printk(KERN_ERR "NFS: %s: unhandled error "
1966 "%d.\n", __func__, err);
1972 case -NFS4ERR_BADSESSION:
1973 case -NFS4ERR_BADSLOT:
1974 case -NFS4ERR_BAD_HIGH_SLOT:
1975 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1976 case -NFS4ERR_DEADSESSION:
1977 set_bit(NFS_DELEGATED_STATE, &state->flags);
1978 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1980 case -NFS4ERR_STALE_CLIENTID:
1981 case -NFS4ERR_STALE_STATEID:
1982 set_bit(NFS_DELEGATED_STATE, &state->flags);
1983 /* Don't recall a delegation if it was lost */
1984 nfs4_schedule_lease_recovery(server->nfs_client);
1986 case -NFS4ERR_MOVED:
1987 nfs4_schedule_migration_recovery(server);
1989 case -NFS4ERR_LEASE_MOVED:
1990 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1992 case -NFS4ERR_DELEG_REVOKED:
1993 case -NFS4ERR_ADMIN_REVOKED:
1994 case -NFS4ERR_EXPIRED:
1995 case -NFS4ERR_BAD_STATEID:
1996 case -NFS4ERR_OPENMODE:
1997 nfs_inode_find_state_and_recover(state->inode,
1999 nfs4_schedule_stateid_recovery(server, state);
2001 case -NFS4ERR_DELAY:
2002 case -NFS4ERR_GRACE:
2003 set_bit(NFS_DELEGATED_STATE, &state->flags);
2007 case -NFS4ERR_DENIED:
2008 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
2014 int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2015 struct nfs4_state *state, const nfs4_stateid *stateid,
2018 struct nfs_server *server = NFS_SERVER(state->inode);
2019 struct nfs4_opendata *opendata;
2022 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2023 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2024 if (IS_ERR(opendata))
2025 return PTR_ERR(opendata);
2026 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
2027 write_seqlock(&state->seqlock);
2028 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2029 write_sequnlock(&state->seqlock);
2030 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2031 switch (type & (FMODE_READ|FMODE_WRITE)) {
2032 case FMODE_READ|FMODE_WRITE:
2034 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2037 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2041 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2043 nfs4_opendata_put(opendata);
2044 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
2047 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2049 struct nfs4_opendata *data = calldata;
2051 nfs40_setup_sequence(data->o_arg.server->nfs_client->cl_slot_tbl,
2052 &data->c_arg.seq_args, &data->c_res.seq_res, task);
2055 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2057 struct nfs4_opendata *data = calldata;
2059 nfs40_sequence_done(task, &data->c_res.seq_res);
2061 data->rpc_status = task->tk_status;
2062 if (data->rpc_status == 0) {
2063 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
2064 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2065 renew_lease(data->o_res.server, data->timestamp);
2070 static void nfs4_open_confirm_release(void *calldata)
2072 struct nfs4_opendata *data = calldata;
2073 struct nfs4_state *state = NULL;
2075 /* If this request hasn't been cancelled, do nothing */
2076 if (data->cancelled == 0)
2078 /* In case of error, no cleanup! */
2079 if (!data->rpc_done)
2081 state = nfs4_opendata_to_nfs4_state(data);
2083 nfs4_close_state(state, data->o_arg.fmode);
2085 nfs4_opendata_put(data);
2088 static const struct rpc_call_ops nfs4_open_confirm_ops = {
2089 .rpc_call_prepare = nfs4_open_confirm_prepare,
2090 .rpc_call_done = nfs4_open_confirm_done,
2091 .rpc_release = nfs4_open_confirm_release,
2095 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2097 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2099 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
2100 struct rpc_task *task;
2101 struct rpc_message msg = {
2102 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2103 .rpc_argp = &data->c_arg,
2104 .rpc_resp = &data->c_res,
2105 .rpc_cred = data->owner->so_cred,
2107 struct rpc_task_setup task_setup_data = {
2108 .rpc_client = server->client,
2109 .rpc_message = &msg,
2110 .callback_ops = &nfs4_open_confirm_ops,
2111 .callback_data = data,
2112 .workqueue = nfsiod_workqueue,
2113 .flags = RPC_TASK_ASYNC,
2117 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
2118 kref_get(&data->kref);
2120 data->rpc_status = 0;
2121 data->timestamp = jiffies;
2122 if (data->is_recover)
2123 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
2124 task = rpc_run_task(&task_setup_data);
2126 return PTR_ERR(task);
2127 status = nfs4_wait_for_completion_rpc_task(task);
2129 data->cancelled = 1;
2132 status = data->rpc_status;
2137 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
2139 struct nfs4_opendata *data = calldata;
2140 struct nfs4_state_owner *sp = data->owner;
2141 struct nfs_client *clp = sp->so_server->nfs_client;
2142 enum open_claim_type4 claim = data->o_arg.claim;
2144 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
2147 * Check if we still need to send an OPEN call, or if we can use
2148 * a delegation instead.
2150 if (data->state != NULL) {
2151 struct nfs_delegation *delegation;
2153 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
2156 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
2157 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
2158 goto unlock_no_action;
2161 /* Update client id. */
2162 data->o_arg.clientid = clp->cl_clientid;
2166 case NFS4_OPEN_CLAIM_PREVIOUS:
2167 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2168 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
2169 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
2170 case NFS4_OPEN_CLAIM_FH:
2171 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
2172 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
2174 data->timestamp = jiffies;
2175 if (nfs4_setup_sequence(data->o_arg.server,
2176 &data->o_arg.seq_args,
2177 &data->o_res.seq_res,
2179 nfs_release_seqid(data->o_arg.seqid);
2181 /* Set the create mode (note dependency on the session type) */
2182 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2183 if (data->o_arg.open_flags & O_EXCL) {
2184 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2185 if (nfs4_has_persistent_session(clp))
2186 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2187 else if (clp->cl_mvops->minor_version > 0)
2188 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2192 trace_nfs4_cached_open(data->state);
2195 task->tk_action = NULL;
2197 nfs4_sequence_done(task, &data->o_res.seq_res);
2200 static void nfs4_open_done(struct rpc_task *task, void *calldata)
2202 struct nfs4_opendata *data = calldata;
2204 data->rpc_status = task->tk_status;
2206 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
2209 if (task->tk_status == 0) {
2210 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2211 switch (data->o_res.f_attr->mode & S_IFMT) {
2215 data->rpc_status = -ELOOP;
2218 data->rpc_status = -EISDIR;
2221 data->rpc_status = -ENOTDIR;
2224 renew_lease(data->o_res.server, data->timestamp);
2225 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2226 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2231 static void nfs4_open_release(void *calldata)
2233 struct nfs4_opendata *data = calldata;
2234 struct nfs4_state *state = NULL;
2236 /* If this request hasn't been cancelled, do nothing */
2237 if (data->cancelled == 0)
2239 /* In case of error, no cleanup! */
2240 if (data->rpc_status != 0 || !data->rpc_done)
2242 /* In case we need an open_confirm, no cleanup! */
2243 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2245 state = nfs4_opendata_to_nfs4_state(data);
2247 nfs4_close_state(state, data->o_arg.fmode);
2249 nfs4_opendata_put(data);
2252 static const struct rpc_call_ops nfs4_open_ops = {
2253 .rpc_call_prepare = nfs4_open_prepare,
2254 .rpc_call_done = nfs4_open_done,
2255 .rpc_release = nfs4_open_release,
2258 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
2260 struct inode *dir = d_inode(data->dir);
2261 struct nfs_server *server = NFS_SERVER(dir);
2262 struct nfs_openargs *o_arg = &data->o_arg;
2263 struct nfs_openres *o_res = &data->o_res;
2264 struct rpc_task *task;
2265 struct rpc_message msg = {
2266 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2269 .rpc_cred = data->owner->so_cred,
2271 struct rpc_task_setup task_setup_data = {
2272 .rpc_client = server->client,
2273 .rpc_message = &msg,
2274 .callback_ops = &nfs4_open_ops,
2275 .callback_data = data,
2276 .workqueue = nfsiod_workqueue,
2277 .flags = RPC_TASK_ASYNC,
2281 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
2282 kref_get(&data->kref);
2284 data->rpc_status = 0;
2285 data->cancelled = 0;
2286 data->is_recover = 0;
2288 nfs4_set_sequence_privileged(&o_arg->seq_args);
2289 data->is_recover = 1;
2291 task = rpc_run_task(&task_setup_data);
2293 return PTR_ERR(task);
2294 status = nfs4_wait_for_completion_rpc_task(task);
2296 data->cancelled = 1;
2299 status = data->rpc_status;
2305 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2307 struct inode *dir = d_inode(data->dir);
2308 struct nfs_openres *o_res = &data->o_res;
2311 status = nfs4_run_open_task(data, 1);
2312 if (status != 0 || !data->rpc_done)
2315 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2317 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2318 status = _nfs4_proc_open_confirm(data);
2327 * Additional permission checks in order to distinguish between an
2328 * open for read, and an open for execute. This works around the
2329 * fact that NFSv4 OPEN treats read and execute permissions as being
2331 * Note that in the non-execute case, we want to turn off permission
2332 * checking if we just created a new file (POSIX open() semantics).
2334 static int nfs4_opendata_access(struct rpc_cred *cred,
2335 struct nfs4_opendata *opendata,
2336 struct nfs4_state *state, fmode_t fmode,
2339 struct nfs_access_entry cache;
2342 /* access call failed or for some reason the server doesn't
2343 * support any access modes -- defer access call until later */
2344 if (opendata->o_res.access_supported == 0)
2349 * Use openflags to check for exec, because fmode won't
2350 * always have FMODE_EXEC set when file open for exec.
2352 if (openflags & __FMODE_EXEC) {
2353 /* ONLY check for exec rights */
2355 } else if ((fmode & FMODE_READ) && !opendata->file_created)
2359 cache.jiffies = jiffies;
2360 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2361 nfs_access_add_cache(state->inode, &cache);
2363 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
2366 /* even though OPEN succeeded, access is denied. Close the file */
2367 nfs4_close_state(state, fmode);
2372 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2374 static int _nfs4_proc_open(struct nfs4_opendata *data)
2376 struct inode *dir = d_inode(data->dir);
2377 struct nfs_server *server = NFS_SERVER(dir);
2378 struct nfs_openargs *o_arg = &data->o_arg;
2379 struct nfs_openres *o_res = &data->o_res;
2382 status = nfs4_run_open_task(data, 0);
2383 if (!data->rpc_done)
2386 if (status == -NFS4ERR_BADNAME &&
2387 !(o_arg->open_flags & O_CREAT))
2392 nfs_fattr_map_and_free_names(server, &data->f_attr);
2394 if (o_arg->open_flags & O_CREAT) {
2395 if (o_arg->open_flags & O_EXCL)
2396 data->file_created = 1;
2397 else if (o_res->cinfo.before != o_res->cinfo.after)
2398 data->file_created = 1;
2399 if (data->file_created || dir->i_version != o_res->cinfo.after)
2400 update_changeattr(dir, &o_res->cinfo,
2401 o_res->f_attr->time_start);
2403 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2404 server->caps &= ~NFS_CAP_POSIX_LOCK;
2405 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2406 status = _nfs4_proc_open_confirm(data);
2410 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
2411 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
2415 static int nfs4_recover_expired_lease(struct nfs_server *server)
2417 return nfs4_client_recover_expired_lease(server->nfs_client);
2422 * reclaim state on the server after a network partition.
2423 * Assumes caller holds the appropriate lock
2425 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2427 struct nfs4_opendata *opendata;
2430 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2431 NFS4_OPEN_CLAIM_FH);
2432 if (IS_ERR(opendata))
2433 return PTR_ERR(opendata);
2434 ret = nfs4_open_recover(opendata, state);
2436 d_drop(ctx->dentry);
2437 nfs4_opendata_put(opendata);
2441 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2443 struct nfs_server *server = NFS_SERVER(state->inode);
2444 struct nfs4_exception exception = { };
2448 err = _nfs4_open_expired(ctx, state);
2449 trace_nfs4_open_expired(ctx, 0, err);
2450 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2455 case -NFS4ERR_GRACE:
2456 case -NFS4ERR_DELAY:
2457 nfs4_handle_exception(server, err, &exception);
2460 } while (exception.retry);
2465 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2467 struct nfs_open_context *ctx;
2470 ctx = nfs4_state_find_open_context(state);
2473 ret = nfs4_do_open_expired(ctx, state);
2474 put_nfs_open_context(ctx);
2478 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2479 const nfs4_stateid *stateid)
2481 nfs_remove_bad_delegation(state->inode, stateid);
2482 write_seqlock(&state->seqlock);
2483 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2484 write_sequnlock(&state->seqlock);
2485 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2488 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2490 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2491 nfs_finish_clear_delegation_stateid(state, NULL);
2494 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2496 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2497 nfs40_clear_delegation_stateid(state);
2498 return nfs4_open_expired(sp, state);
2501 static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2502 nfs4_stateid *stateid,
2503 struct rpc_cred *cred)
2505 return -NFS4ERR_BAD_STATEID;
2508 #if defined(CONFIG_NFS_V4_1)
2509 static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2510 nfs4_stateid *stateid,
2511 struct rpc_cred *cred)
2515 switch (stateid->type) {
2518 case NFS4_INVALID_STATEID_TYPE:
2519 case NFS4_SPECIAL_STATEID_TYPE:
2520 return -NFS4ERR_BAD_STATEID;
2521 case NFS4_REVOKED_STATEID_TYPE:
2525 status = nfs41_test_stateid(server, stateid, cred);
2527 case -NFS4ERR_EXPIRED:
2528 case -NFS4ERR_ADMIN_REVOKED:
2529 case -NFS4ERR_DELEG_REVOKED:
2535 /* Ack the revoked state to the server */
2536 nfs41_free_stateid(server, stateid, cred, true);
2537 return -NFS4ERR_EXPIRED;
2540 static void nfs41_check_delegation_stateid(struct nfs4_state *state)
2542 struct nfs_server *server = NFS_SERVER(state->inode);
2543 nfs4_stateid stateid;
2544 struct nfs_delegation *delegation;
2545 struct rpc_cred *cred;
2548 /* Get the delegation credential for use by test/free_stateid */
2550 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2551 if (delegation == NULL) {
2556 nfs4_stateid_copy(&stateid, &delegation->stateid);
2557 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
2559 nfs_finish_clear_delegation_stateid(state, &stateid);
2563 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags)) {
2568 cred = get_rpccred(delegation->cred);
2570 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
2571 trace_nfs4_test_delegation_stateid(state, NULL, status);
2572 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
2573 nfs_finish_clear_delegation_stateid(state, &stateid);
2579 * nfs41_check_expired_locks - possibly free a lock stateid
2581 * @state: NFSv4 state for an inode
2583 * Returns NFS_OK if recovery for this stateid is now finished.
2584 * Otherwise a negative NFS4ERR value is returned.
2586 static int nfs41_check_expired_locks(struct nfs4_state *state)
2588 int status, ret = NFS_OK;
2589 struct nfs4_lock_state *lsp, *prev = NULL;
2590 struct nfs_server *server = NFS_SERVER(state->inode);
2592 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2595 spin_lock(&state->state_lock);
2596 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2597 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2598 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2600 atomic_inc(&lsp->ls_count);
2601 spin_unlock(&state->state_lock);
2603 nfs4_put_lock_state(prev);
2606 status = nfs41_test_and_free_expired_stateid(server,
2609 trace_nfs4_test_lock_stateid(state, lsp, status);
2610 if (status == -NFS4ERR_EXPIRED ||
2611 status == -NFS4ERR_BAD_STATEID) {
2612 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
2613 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
2614 if (!recover_lost_locks)
2615 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2616 } else if (status != NFS_OK) {
2618 nfs4_put_lock_state(prev);
2621 spin_lock(&state->state_lock);
2624 spin_unlock(&state->state_lock);
2625 nfs4_put_lock_state(prev);
2631 * nfs41_check_open_stateid - possibly free an open stateid
2633 * @state: NFSv4 state for an inode
2635 * Returns NFS_OK if recovery for this stateid is now finished.
2636 * Otherwise a negative NFS4ERR value is returned.
2638 static int nfs41_check_open_stateid(struct nfs4_state *state)
2640 struct nfs_server *server = NFS_SERVER(state->inode);
2641 nfs4_stateid *stateid = &state->open_stateid;
2642 struct rpc_cred *cred = state->owner->so_cred;
2645 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
2646 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2647 if (nfs4_have_delegation(state->inode, state->state))
2649 return -NFS4ERR_OPENMODE;
2651 return -NFS4ERR_BAD_STATEID;
2653 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
2654 trace_nfs4_test_open_stateid(state, NULL, status);
2655 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
2656 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2657 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2658 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2659 clear_bit(NFS_OPEN_STATE, &state->flags);
2660 stateid->type = NFS4_INVALID_STATEID_TYPE;
2662 if (status != NFS_OK)
2664 if (nfs_open_stateid_recover_openmode(state))
2665 return -NFS4ERR_OPENMODE;
2669 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2673 nfs41_check_delegation_stateid(state);
2674 status = nfs41_check_expired_locks(state);
2675 if (status != NFS_OK)
2677 status = nfs41_check_open_stateid(state);
2678 if (status != NFS_OK)
2679 status = nfs4_open_expired(sp, state);
2685 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2686 * fields corresponding to attributes that were used to store the verifier.
2687 * Make sure we clobber those fields in the later setattr call
2689 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2690 struct iattr *sattr, struct nfs4_label **label)
2692 const u32 *attrset = opendata->o_res.attrset;
2694 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2695 !(sattr->ia_valid & ATTR_ATIME_SET))
2696 sattr->ia_valid |= ATTR_ATIME;
2698 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2699 !(sattr->ia_valid & ATTR_MTIME_SET))
2700 sattr->ia_valid |= ATTR_MTIME;
2702 /* Except MODE, it seems harmless of setting twice. */
2703 if ((attrset[1] & FATTR4_WORD1_MODE))
2704 sattr->ia_valid &= ~ATTR_MODE;
2706 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2710 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2713 struct nfs_open_context *ctx)
2715 struct nfs4_state_owner *sp = opendata->owner;
2716 struct nfs_server *server = sp->so_server;
2717 struct dentry *dentry;
2718 struct nfs4_state *state;
2722 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2724 ret = _nfs4_proc_open(opendata);
2728 state = nfs4_opendata_to_nfs4_state(opendata);
2729 ret = PTR_ERR(state);
2732 if (server->caps & NFS_CAP_POSIX_LOCK)
2733 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2734 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2735 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
2737 dentry = opendata->dentry;
2738 if (d_really_is_negative(dentry)) {
2739 struct dentry *alias;
2741 alias = d_exact_alias(dentry, state->inode);
2743 alias = d_splice_alias(igrab(state->inode), dentry);
2744 /* d_splice_alias() can't fail here - it's a non-directory */
2747 ctx->dentry = dentry = alias;
2749 nfs_set_verifier(dentry,
2750 nfs_save_change_attribute(d_inode(opendata->dir)));
2753 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2758 if (d_inode(dentry) == state->inode) {
2759 nfs_inode_attach_open_context(ctx);
2760 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2761 nfs4_schedule_stateid_recovery(server, state);
2768 * Returns a referenced nfs4_state
2770 static int _nfs4_do_open(struct inode *dir,
2771 struct nfs_open_context *ctx,
2773 struct iattr *sattr,
2774 struct nfs4_label *label,
2777 struct nfs4_state_owner *sp;
2778 struct nfs4_state *state = NULL;
2779 struct nfs_server *server = NFS_SERVER(dir);
2780 struct nfs4_opendata *opendata;
2781 struct dentry *dentry = ctx->dentry;
2782 struct rpc_cred *cred = ctx->cred;
2783 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2784 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
2785 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
2786 struct nfs4_label *olabel = NULL;
2789 /* Protect against reboot recovery conflicts */
2791 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2793 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2796 status = nfs4_recover_expired_lease(server);
2798 goto err_put_state_owner;
2799 if (d_really_is_positive(dentry))
2800 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
2802 if (d_really_is_positive(dentry))
2803 claim = NFS4_OPEN_CLAIM_FH;
2804 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
2805 label, claim, GFP_KERNEL);
2806 if (opendata == NULL)
2807 goto err_put_state_owner;
2810 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2811 if (IS_ERR(olabel)) {
2812 status = PTR_ERR(olabel);
2813 goto err_opendata_put;
2817 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2818 if (!opendata->f_attr.mdsthreshold) {
2819 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2820 if (!opendata->f_attr.mdsthreshold)
2821 goto err_free_label;
2823 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
2825 if (d_really_is_positive(dentry))
2826 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
2828 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
2830 goto err_free_label;
2833 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
2834 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
2835 nfs4_exclusive_attrset(opendata, sattr, &label);
2837 * send create attributes which was not set by open
2838 * with an extra setattr.
2840 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2841 nfs_fattr_init(opendata->o_res.f_attr);
2842 status = nfs4_do_setattr(state->inode, cred,
2843 opendata->o_res.f_attr, sattr,
2844 ctx, label, olabel);
2846 nfs_setattr_update_inode(state->inode, sattr,
2847 opendata->o_res.f_attr);
2848 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2852 if (opened && opendata->file_created)
2853 *opened |= FILE_CREATED;
2855 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
2856 *ctx_th = opendata->f_attr.mdsthreshold;
2857 opendata->f_attr.mdsthreshold = NULL;
2860 nfs4_label_free(olabel);
2862 nfs4_opendata_put(opendata);
2863 nfs4_put_state_owner(sp);
2866 nfs4_label_free(olabel);
2868 nfs4_opendata_put(opendata);
2869 err_put_state_owner:
2870 nfs4_put_state_owner(sp);
2876 static struct nfs4_state *nfs4_do_open(struct inode *dir,
2877 struct nfs_open_context *ctx,
2879 struct iattr *sattr,
2880 struct nfs4_label *label,
2883 struct nfs_server *server = NFS_SERVER(dir);
2884 struct nfs4_exception exception = { };
2885 struct nfs4_state *res;
2889 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
2891 trace_nfs4_open_file(ctx, flags, status);
2894 /* NOTE: BAD_SEQID means the server and client disagree about the
2895 * book-keeping w.r.t. state-changing operations
2896 * (OPEN/CLOSE/LOCK/LOCKU...)
2897 * It is actually a sign of a bug on the client or on the server.
2899 * If we receive a BAD_SEQID error in the particular case of
2900 * doing an OPEN, we assume that nfs_increment_open_seqid() will
2901 * have unhashed the old state_owner for us, and that we can
2902 * therefore safely retry using a new one. We should still warn
2903 * the user though...
2905 if (status == -NFS4ERR_BAD_SEQID) {
2906 pr_warn_ratelimited("NFS: v4 server %s "
2907 " returned a bad sequence-id error!\n",
2908 NFS_SERVER(dir)->nfs_client->cl_hostname);
2909 exception.retry = 1;
2913 * BAD_STATEID on OPEN means that the server cancelled our
2914 * state before it received the OPEN_CONFIRM.
2915 * Recover by retrying the request as per the discussion
2916 * on Page 181 of RFC3530.
2918 if (status == -NFS4ERR_BAD_STATEID) {
2919 exception.retry = 1;
2922 if (status == -EAGAIN) {
2923 /* We must have found a delegation */
2924 exception.retry = 1;
2927 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2929 res = ERR_PTR(nfs4_handle_exception(server,
2930 status, &exception));
2931 } while (exception.retry);
2935 static int _nfs4_do_setattr(struct inode *inode,
2936 struct nfs_setattrargs *arg,
2937 struct nfs_setattrres *res,
2938 struct rpc_cred *cred,
2939 struct nfs_open_context *ctx)
2941 struct nfs_server *server = NFS_SERVER(inode);
2942 struct rpc_message msg = {
2943 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2948 struct rpc_cred *delegation_cred = NULL;
2949 unsigned long timestamp = jiffies;
2954 nfs_fattr_init(res->fattr);
2956 /* Servers should only apply open mode checks for file size changes */
2957 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
2958 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2960 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
2961 /* Use that stateid */
2962 } else if (truncate && ctx != NULL) {
2963 struct nfs_lock_context *l_ctx;
2964 if (!nfs4_valid_open_stateid(ctx->state))
2966 l_ctx = nfs_get_lock_context(ctx);
2968 return PTR_ERR(l_ctx);
2969 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
2970 &arg->stateid, &delegation_cred);
2971 nfs_put_lock_context(l_ctx);
2975 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
2976 if (delegation_cred)
2977 msg.rpc_cred = delegation_cred;
2979 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
2981 put_rpccred(delegation_cred);
2982 if (status == 0 && ctx != NULL)
2983 renew_lease(server, timestamp);
2984 trace_nfs4_setattr(inode, &arg->stateid, status);
2988 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2989 struct nfs_fattr *fattr, struct iattr *sattr,
2990 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
2991 struct nfs4_label *olabel)
2993 struct nfs_server *server = NFS_SERVER(inode);
2994 struct nfs4_state *state = ctx ? ctx->state : NULL;
2995 struct nfs_setattrargs arg = {
2996 .fh = NFS_FH(inode),
2999 .bitmask = server->attr_bitmask,
3002 struct nfs_setattrres res = {
3007 struct nfs4_exception exception = {
3010 .stateid = &arg.stateid,
3014 arg.bitmask = nfs4_bitmask(server, ilabel);
3016 arg.bitmask = nfs4_bitmask(server, olabel);
3019 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
3021 case -NFS4ERR_OPENMODE:
3022 if (!(sattr->ia_valid & ATTR_SIZE)) {
3023 pr_warn_once("NFSv4: server %s is incorrectly "
3024 "applying open mode checks to "
3025 "a SETATTR that is not "
3026 "changing file size.\n",
3027 server->nfs_client->cl_hostname);
3029 if (state && !(state->state & FMODE_WRITE)) {
3031 if (sattr->ia_valid & ATTR_OPEN)
3036 err = nfs4_handle_exception(server, err, &exception);
3037 } while (exception.retry);
3043 nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3045 if (inode == NULL || !nfs_have_layout(inode))
3048 return pnfs_wait_on_layoutreturn(inode, task);
3051 struct nfs4_closedata {
3052 struct inode *inode;
3053 struct nfs4_state *state;
3054 struct nfs_closeargs arg;
3055 struct nfs_closeres res;
3057 struct nfs4_layoutreturn_args arg;
3058 struct nfs4_layoutreturn_res res;
3059 struct nfs4_xdr_opaque_data ld_private;
3063 struct nfs_fattr fattr;
3064 unsigned long timestamp;
3067 static void nfs4_free_closedata(void *data)
3069 struct nfs4_closedata *calldata = data;
3070 struct nfs4_state_owner *sp = calldata->state->owner;
3071 struct super_block *sb = calldata->state->inode->i_sb;
3073 if (calldata->lr.roc)
3074 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3075 calldata->res.lr_ret);
3076 nfs4_put_open_state(calldata->state);
3077 nfs_free_seqid(calldata->arg.seqid);
3078 nfs4_put_state_owner(sp);
3079 nfs_sb_deactive(sb);
3083 static void nfs4_close_done(struct rpc_task *task, void *data)
3085 struct nfs4_closedata *calldata = data;
3086 struct nfs4_state *state = calldata->state;
3087 struct nfs_server *server = NFS_SERVER(calldata->inode);
3088 nfs4_stateid *res_stateid = NULL;
3090 dprintk("%s: begin!\n", __func__);
3091 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3093 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
3095 /* Handle Layoutreturn errors */
3096 if (calldata->arg.lr_args && task->tk_status != 0) {
3097 switch (calldata->res.lr_ret) {
3099 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3102 calldata->arg.lr_args = NULL;
3103 calldata->res.lr_res = NULL;
3105 case -NFS4ERR_ADMIN_REVOKED:
3106 case -NFS4ERR_DELEG_REVOKED:
3107 case -NFS4ERR_EXPIRED:
3108 case -NFS4ERR_BAD_STATEID:
3109 case -NFS4ERR_OLD_STATEID:
3110 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3111 case -NFS4ERR_WRONG_CRED:
3112 calldata->arg.lr_args = NULL;
3113 calldata->res.lr_res = NULL;
3114 calldata->res.lr_ret = 0;
3115 rpc_restart_call_prepare(task);
3120 /* hmm. we are done with the inode, and in the process of freeing
3121 * the state_owner. we keep this around to process errors
3123 switch (task->tk_status) {
3125 res_stateid = &calldata->res.stateid;
3126 renew_lease(server, calldata->timestamp);
3128 case -NFS4ERR_ACCESS:
3129 if (calldata->arg.bitmask != NULL) {
3130 calldata->arg.bitmask = NULL;
3131 calldata->res.fattr = NULL;
3132 task->tk_status = 0;
3133 rpc_restart_call_prepare(task);
3138 case -NFS4ERR_ADMIN_REVOKED:
3139 case -NFS4ERR_STALE_STATEID:
3140 case -NFS4ERR_EXPIRED:
3141 nfs4_free_revoked_stateid(server,
3142 &calldata->arg.stateid,
3143 task->tk_msg.rpc_cred);
3144 case -NFS4ERR_OLD_STATEID:
3145 case -NFS4ERR_BAD_STATEID:
3146 if (!nfs4_stateid_match(&calldata->arg.stateid,
3147 &state->open_stateid)) {
3148 rpc_restart_call_prepare(task);
3151 if (calldata->arg.fmode == 0)
3154 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
3155 rpc_restart_call_prepare(task);
3159 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3160 res_stateid, calldata->arg.fmode);
3162 nfs_release_seqid(calldata->arg.seqid);
3163 nfs_refresh_inode(calldata->inode, &calldata->fattr);
3164 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
3167 static void nfs4_close_prepare(struct rpc_task *task, void *data)
3169 struct nfs4_closedata *calldata = data;
3170 struct nfs4_state *state = calldata->state;
3171 struct inode *inode = calldata->inode;
3172 bool is_rdonly, is_wronly, is_rdwr;
3175 dprintk("%s: begin!\n", __func__);
3176 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3179 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
3180 spin_lock(&state->owner->so_lock);
3181 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3182 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3183 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
3184 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
3185 /* Calculate the change in open mode */
3186 calldata->arg.fmode = 0;
3187 if (state->n_rdwr == 0) {
3188 if (state->n_rdonly == 0)
3189 call_close |= is_rdonly;
3191 calldata->arg.fmode |= FMODE_READ;
3192 if (state->n_wronly == 0)
3193 call_close |= is_wronly;
3195 calldata->arg.fmode |= FMODE_WRITE;
3196 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3197 call_close |= is_rdwr;
3199 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3201 if (!nfs4_valid_open_stateid(state) ||
3202 test_bit(NFS_OPEN_STATE, &state->flags) == 0)
3204 spin_unlock(&state->owner->so_lock);
3207 /* Note: exit _without_ calling nfs4_close_done */
3211 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
3212 nfs_release_seqid(calldata->arg.seqid);
3216 if (calldata->arg.fmode == 0)
3217 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
3219 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
3220 /* Close-to-open cache consistency revalidation */
3221 if (!nfs4_have_delegation(inode, FMODE_READ))
3222 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3224 calldata->arg.bitmask = NULL;
3227 calldata->arg.share_access =
3228 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3229 calldata->arg.fmode, 0);
3231 if (calldata->res.fattr == NULL)
3232 calldata->arg.bitmask = NULL;
3233 else if (calldata->arg.bitmask == NULL)
3234 calldata->res.fattr = NULL;
3235 calldata->timestamp = jiffies;
3236 if (nfs4_setup_sequence(NFS_SERVER(inode),
3237 &calldata->arg.seq_args,
3238 &calldata->res.seq_res,
3240 nfs_release_seqid(calldata->arg.seqid);
3241 dprintk("%s: done!\n", __func__);
3244 task->tk_action = NULL;
3246 nfs4_sequence_done(task, &calldata->res.seq_res);
3249 static const struct rpc_call_ops nfs4_close_ops = {
3250 .rpc_call_prepare = nfs4_close_prepare,
3251 .rpc_call_done = nfs4_close_done,
3252 .rpc_release = nfs4_free_closedata,
3256 * It is possible for data to be read/written from a mem-mapped file
3257 * after the sys_close call (which hits the vfs layer as a flush).
3258 * This means that we can't safely call nfsv4 close on a file until
3259 * the inode is cleared. This in turn means that we are not good
3260 * NFSv4 citizens - we do not indicate to the server to update the file's
3261 * share state even when we are done with one of the three share
3262 * stateid's in the inode.
3264 * NOTE: Caller must be holding the sp->so_owner semaphore!
3266 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
3268 struct nfs_server *server = NFS_SERVER(state->inode);
3269 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
3270 struct nfs4_closedata *calldata;
3271 struct nfs4_state_owner *sp = state->owner;
3272 struct rpc_task *task;
3273 struct rpc_message msg = {
3274 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3275 .rpc_cred = state->owner->so_cred,
3277 struct rpc_task_setup task_setup_data = {
3278 .rpc_client = server->client,
3279 .rpc_message = &msg,
3280 .callback_ops = &nfs4_close_ops,
3281 .workqueue = nfsiod_workqueue,
3282 .flags = RPC_TASK_ASYNC,
3284 int status = -ENOMEM;
3286 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3287 &task_setup_data.rpc_client, &msg);
3289 calldata = kzalloc(sizeof(*calldata), gfp_mask);
3290 if (calldata == NULL)
3292 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
3293 calldata->inode = state->inode;
3294 calldata->state = state;
3295 calldata->arg.fh = NFS_FH(state->inode);
3296 /* Serialization for the sequence id */
3297 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3298 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
3299 if (IS_ERR(calldata->arg.seqid))
3300 goto out_free_calldata;
3301 nfs_fattr_init(&calldata->fattr);
3302 calldata->arg.fmode = 0;
3303 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
3304 calldata->res.fattr = &calldata->fattr;
3305 calldata->res.seqid = calldata->arg.seqid;
3306 calldata->res.server = server;
3307 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3308 calldata->lr.roc = pnfs_roc(state->inode,
3309 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3310 if (calldata->lr.roc) {
3311 calldata->arg.lr_args = &calldata->lr.arg;
3312 calldata->res.lr_res = &calldata->lr.res;
3314 nfs_sb_active(calldata->inode->i_sb);
3316 msg.rpc_argp = &calldata->arg;
3317 msg.rpc_resp = &calldata->res;
3318 task_setup_data.callback_data = calldata;
3319 task = rpc_run_task(&task_setup_data);
3321 return PTR_ERR(task);
3324 status = rpc_wait_for_completion_task(task);
3330 nfs4_put_open_state(state);
3331 nfs4_put_state_owner(sp);
3335 static struct inode *
3336 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3337 int open_flags, struct iattr *attr, int *opened)
3339 struct nfs4_state *state;
3340 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3342 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
3344 /* Protect against concurrent sillydeletes */
3345 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
3347 nfs4_label_release_security(label);
3350 return ERR_CAST(state);
3351 return state->inode;
3354 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
3356 if (ctx->state == NULL)
3359 nfs4_close_sync(ctx->state, ctx->mode);
3361 nfs4_close_state(ctx->state, ctx->mode);
3364 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3365 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3366 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
3368 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3370 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
3371 struct nfs4_server_caps_arg args = {
3375 struct nfs4_server_caps_res res = {};
3376 struct rpc_message msg = {
3377 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
3383 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3384 FATTR4_WORD0_FH_EXPIRE_TYPE |
3385 FATTR4_WORD0_LINK_SUPPORT |
3386 FATTR4_WORD0_SYMLINK_SUPPORT |
3387 FATTR4_WORD0_ACLSUPPORT;
3389 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3391 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3393 /* Sanity check the server answers */
3394 switch (minorversion) {
3396 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3397 res.attr_bitmask[2] = 0;
3400 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3403 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3405 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
3406 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3407 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3408 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3409 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
3410 NFS_CAP_CTIME|NFS_CAP_MTIME|
3411 NFS_CAP_SECURITY_LABEL);
3412 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3413 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3414 server->caps |= NFS_CAP_ACLS;
3415 if (res.has_links != 0)
3416 server->caps |= NFS_CAP_HARDLINKS;
3417 if (res.has_symlinks != 0)
3418 server->caps |= NFS_CAP_SYMLINKS;
3419 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3420 server->caps |= NFS_CAP_FILEID;
3421 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3422 server->caps |= NFS_CAP_MODE;
3423 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3424 server->caps |= NFS_CAP_NLINK;
3425 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3426 server->caps |= NFS_CAP_OWNER;
3427 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3428 server->caps |= NFS_CAP_OWNER_GROUP;
3429 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3430 server->caps |= NFS_CAP_ATIME;
3431 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3432 server->caps |= NFS_CAP_CTIME;
3433 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3434 server->caps |= NFS_CAP_MTIME;
3435 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3436 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3437 server->caps |= NFS_CAP_SECURITY_LABEL;
3439 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3440 sizeof(server->attr_bitmask));
3441 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3443 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3444 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3445 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
3446 server->cache_consistency_bitmask[2] = 0;
3447 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3448 sizeof(server->exclcreat_bitmask));
3449 server->acl_bitmask = res.acl_bitmask;
3450 server->fh_expire_type = res.fh_expire_type;
3456 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3458 struct nfs4_exception exception = { };
3461 err = nfs4_handle_exception(server,
3462 _nfs4_server_capabilities(server, fhandle),
3464 } while (exception.retry);
3468 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3469 struct nfs_fsinfo *info)
3472 struct nfs4_lookup_root_arg args = {
3475 struct nfs4_lookup_res res = {
3477 .fattr = info->fattr,
3480 struct rpc_message msg = {
3481 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3486 bitmask[0] = nfs4_fattr_bitmap[0];
3487 bitmask[1] = nfs4_fattr_bitmap[1];
3489 * Process the label in the upcoming getfattr
3491 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3493 nfs_fattr_init(info->fattr);
3494 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3497 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3498 struct nfs_fsinfo *info)
3500 struct nfs4_exception exception = { };
3503 err = _nfs4_lookup_root(server, fhandle, info);
3504 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
3507 case -NFS4ERR_WRONGSEC:
3510 err = nfs4_handle_exception(server, err, &exception);
3512 } while (exception.retry);
3517 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3518 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3520 struct rpc_auth_create_args auth_args = {
3521 .pseudoflavor = flavor,
3523 struct rpc_auth *auth;
3526 auth = rpcauth_create(&auth_args, server->client);
3531 ret = nfs4_lookup_root(server, fhandle, info);
3537 * Retry pseudoroot lookup with various security flavors. We do this when:
3539 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3540 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3542 * Returns zero on success, or a negative NFS4ERR value, or a
3543 * negative errno value.
3545 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3546 struct nfs_fsinfo *info)
3548 /* Per 3530bis 15.33.5 */
3549 static const rpc_authflavor_t flav_array[] = {
3553 RPC_AUTH_UNIX, /* courtesy */
3556 int status = -EPERM;
3559 if (server->auth_info.flavor_len > 0) {
3560 /* try each flavor specified by user */
3561 for (i = 0; i < server->auth_info.flavor_len; i++) {
3562 status = nfs4_lookup_root_sec(server, fhandle, info,
3563 server->auth_info.flavors[i]);
3564 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3569 /* no flavors specified by user, try default list */
3570 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3571 status = nfs4_lookup_root_sec(server, fhandle, info,
3573 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3580 * -EACCESS could mean that the user doesn't have correct permissions
3581 * to access the mount. It could also mean that we tried to mount
3582 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3583 * existing mount programs don't handle -EACCES very well so it should
3584 * be mapped to -EPERM instead.
3586 if (status == -EACCES)
3592 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3593 * @server: initialized nfs_server handle
3594 * @fhandle: we fill in the pseudo-fs root file handle
3595 * @info: we fill in an FSINFO struct
3596 * @auth_probe: probe the auth flavours
3598 * Returns zero on success, or a negative errno.
3600 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
3601 struct nfs_fsinfo *info,
3607 status = nfs4_lookup_root(server, fhandle, info);
3609 if (auth_probe || status == NFS4ERR_WRONGSEC)
3610 status = server->nfs_client->cl_mvops->find_root_sec(server,
3614 status = nfs4_server_capabilities(server, fhandle);
3616 status = nfs4_do_fsinfo(server, fhandle, info);
3618 return nfs4_map_errors(status);
3621 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3622 struct nfs_fsinfo *info)
3625 struct nfs_fattr *fattr = info->fattr;
3626 struct nfs4_label *label = NULL;
3628 error = nfs4_server_capabilities(server, mntfh);
3630 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3634 label = nfs4_label_alloc(server, GFP_KERNEL);
3636 return PTR_ERR(label);
3638 error = nfs4_proc_getattr(server, mntfh, fattr, label);
3640 dprintk("nfs4_get_root: getattr error = %d\n", -error);
3641 goto err_free_label;
3644 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3645 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3646 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3649 nfs4_label_free(label);
3655 * Get locations and (maybe) other attributes of a referral.
3656 * Note that we'll actually follow the referral later when
3657 * we detect fsid mismatch in inode revalidation
3659 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3660 const struct qstr *name, struct nfs_fattr *fattr,
3661 struct nfs_fh *fhandle)
3663 int status = -ENOMEM;
3664 struct page *page = NULL;
3665 struct nfs4_fs_locations *locations = NULL;
3667 page = alloc_page(GFP_KERNEL);
3670 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3671 if (locations == NULL)
3674 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
3679 * If the fsid didn't change, this is a migration event, not a
3680 * referral. Cause us to drop into the exception handler, which
3681 * will kick off migration recovery.
3683 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
3684 dprintk("%s: server did not return a different fsid for"
3685 " a referral at %s\n", __func__, name->name);
3686 status = -NFS4ERR_MOVED;
3689 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3690 nfs_fixup_referral_attributes(&locations->fattr);
3692 /* replace the lookup nfs_fattr with the locations nfs_fattr */
3693 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
3694 memset(fhandle, 0, sizeof(struct nfs_fh));
3702 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3703 struct nfs_fattr *fattr, struct nfs4_label *label)
3705 struct nfs4_getattr_arg args = {
3707 .bitmask = server->attr_bitmask,
3709 struct nfs4_getattr_res res = {
3714 struct rpc_message msg = {
3715 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3720 args.bitmask = nfs4_bitmask(server, label);
3722 nfs_fattr_init(fattr);
3723 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3726 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3727 struct nfs_fattr *fattr, struct nfs4_label *label)
3729 struct nfs4_exception exception = { };
3732 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3733 trace_nfs4_getattr(server, fhandle, fattr, err);
3734 err = nfs4_handle_exception(server, err,
3736 } while (exception.retry);
3741 * The file is not closed if it is opened due to the a request to change
3742 * the size of the file. The open call will not be needed once the
3743 * VFS layer lookup-intents are implemented.
3745 * Close is called when the inode is destroyed.
3746 * If we haven't opened the file for O_WRONLY, we
3747 * need to in the size_change case to obtain a stateid.
3750 * Because OPEN is always done by name in nfsv4, it is
3751 * possible that we opened a different file by the same
3752 * name. We can recognize this race condition, but we
3753 * can't do anything about it besides returning an error.
3755 * This will be fixed with VFS changes (lookup-intent).
3758 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3759 struct iattr *sattr)
3761 struct inode *inode = d_inode(dentry);
3762 struct rpc_cred *cred = NULL;
3763 struct nfs_open_context *ctx = NULL;
3764 struct nfs4_label *label = NULL;
3767 if (pnfs_ld_layoutret_on_setattr(inode) &&
3768 sattr->ia_valid & ATTR_SIZE &&
3769 sattr->ia_size < i_size_read(inode))
3770 pnfs_commit_and_return_layout(inode);
3772 nfs_fattr_init(fattr);
3774 /* Deal with open(O_TRUNC) */
3775 if (sattr->ia_valid & ATTR_OPEN)
3776 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
3778 /* Optimization: if the end result is no change, don't RPC */
3779 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
3782 /* Search for an existing open(O_WRITE) file */
3783 if (sattr->ia_valid & ATTR_FILE) {
3785 ctx = nfs_file_open_context(sattr->ia_file);
3790 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3792 return PTR_ERR(label);
3794 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
3796 nfs_setattr_update_inode(inode, sattr, fattr);
3797 nfs_setsecurity(inode, fattr, label);
3799 nfs4_label_free(label);
3803 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3804 const struct qstr *name, struct nfs_fh *fhandle,
3805 struct nfs_fattr *fattr, struct nfs4_label *label)
3807 struct nfs_server *server = NFS_SERVER(dir);
3809 struct nfs4_lookup_arg args = {
3810 .bitmask = server->attr_bitmask,
3811 .dir_fh = NFS_FH(dir),
3814 struct nfs4_lookup_res res = {
3820 struct rpc_message msg = {
3821 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3826 args.bitmask = nfs4_bitmask(server, label);
3828 nfs_fattr_init(fattr);
3830 dprintk("NFS call lookup %s\n", name->name);
3831 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
3832 dprintk("NFS reply lookup: %d\n", status);
3836 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
3838 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
3839 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
3840 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3844 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3845 const struct qstr *name, struct nfs_fh *fhandle,
3846 struct nfs_fattr *fattr, struct nfs4_label *label)
3848 struct nfs4_exception exception = { };
3849 struct rpc_clnt *client = *clnt;
3852 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
3853 trace_nfs4_lookup(dir, name, err);
3855 case -NFS4ERR_BADNAME:
3858 case -NFS4ERR_MOVED:
3859 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
3860 if (err == -NFS4ERR_MOVED)
3861 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3863 case -NFS4ERR_WRONGSEC:
3865 if (client != *clnt)
3867 client = nfs4_negotiate_security(client, dir, name);
3869 return PTR_ERR(client);
3871 exception.retry = 1;
3874 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3876 } while (exception.retry);
3881 else if (client != *clnt)
3882 rpc_shutdown_client(client);
3887 static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
3888 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3889 struct nfs4_label *label)
3892 struct rpc_clnt *client = NFS_CLIENT(dir);
3894 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
3895 if (client != NFS_CLIENT(dir)) {
3896 rpc_shutdown_client(client);
3897 nfs_fixup_secinfo_attributes(fattr);
3903 nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
3904 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3906 struct rpc_clnt *client = NFS_CLIENT(dir);
3909 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
3911 return ERR_PTR(status);
3912 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
3915 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3917 struct nfs_server *server = NFS_SERVER(inode);
3918 struct nfs4_accessargs args = {
3919 .fh = NFS_FH(inode),
3920 .bitmask = server->cache_consistency_bitmask,
3922 struct nfs4_accessres res = {
3925 struct rpc_message msg = {
3926 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3929 .rpc_cred = entry->cred,
3931 int mode = entry->mask;
3935 * Determine which access bits we want to ask for...
3937 if (mode & MAY_READ)
3938 args.access |= NFS4_ACCESS_READ;
3939 if (S_ISDIR(inode->i_mode)) {
3940 if (mode & MAY_WRITE)
3941 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3942 if (mode & MAY_EXEC)
3943 args.access |= NFS4_ACCESS_LOOKUP;
3945 if (mode & MAY_WRITE)
3946 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3947 if (mode & MAY_EXEC)
3948 args.access |= NFS4_ACCESS_EXECUTE;
3951 res.fattr = nfs_alloc_fattr();
3952 if (res.fattr == NULL)
3955 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3957 nfs_access_set_mask(entry, res.access);
3958 nfs_refresh_inode(inode, res.fattr);
3960 nfs_free_fattr(res.fattr);
3964 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3966 struct nfs4_exception exception = { };
3969 err = _nfs4_proc_access(inode, entry);
3970 trace_nfs4_access(inode, err);
3971 err = nfs4_handle_exception(NFS_SERVER(inode), err,
3973 } while (exception.retry);
3978 * TODO: For the time being, we don't try to get any attributes
3979 * along with any of the zero-copy operations READ, READDIR,
3982 * In the case of the first three, we want to put the GETATTR
3983 * after the read-type operation -- this is because it is hard
3984 * to predict the length of a GETATTR response in v4, and thus
3985 * align the READ data correctly. This means that the GETATTR
3986 * may end up partially falling into the page cache, and we should
3987 * shift it into the 'tail' of the xdr_buf before processing.
3988 * To do this efficiently, we need to know the total length
3989 * of data received, which doesn't seem to be available outside
3992 * In the case of WRITE, we also want to put the GETATTR after
3993 * the operation -- in this case because we want to make sure
3994 * we get the post-operation mtime and size.
3996 * Both of these changes to the XDR layer would in fact be quite
3997 * minor, but I decided to leave them for a subsequent patch.
3999 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4000 unsigned int pgbase, unsigned int pglen)
4002 struct nfs4_readlink args = {
4003 .fh = NFS_FH(inode),
4008 struct nfs4_readlink_res res;
4009 struct rpc_message msg = {
4010 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4015 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
4018 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4019 unsigned int pgbase, unsigned int pglen)
4021 struct nfs4_exception exception = { };
4024 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4025 trace_nfs4_readlink(inode, err);
4026 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4028 } while (exception.retry);
4033 * This is just for mknod. open(O_CREAT) will always do ->open_context().
4036 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
4039 struct nfs_server *server = NFS_SERVER(dir);
4040 struct nfs4_label l, *ilabel = NULL;
4041 struct nfs_open_context *ctx;
4042 struct nfs4_state *state;
4045 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
4047 return PTR_ERR(ctx);
4049 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4051 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4052 sattr->ia_mode &= ~current_umask();
4053 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
4054 if (IS_ERR(state)) {
4055 status = PTR_ERR(state);
4059 nfs4_label_release_security(ilabel);
4060 put_nfs_open_context(ctx);
4064 static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
4066 struct nfs_server *server = NFS_SERVER(dir);
4067 struct nfs_removeargs args = {
4071 struct nfs_removeres res = {
4074 struct rpc_message msg = {
4075 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4079 unsigned long timestamp = jiffies;
4082 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
4084 update_changeattr(dir, &res.cinfo, timestamp);
4088 static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
4090 struct nfs4_exception exception = { };
4093 err = _nfs4_proc_remove(dir, name);
4094 trace_nfs4_remove(dir, name, err);
4095 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4097 } while (exception.retry);
4101 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
4103 struct nfs_server *server = NFS_SERVER(dir);
4104 struct nfs_removeargs *args = msg->rpc_argp;
4105 struct nfs_removeres *res = msg->rpc_resp;
4107 res->server = server;
4108 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
4109 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
4111 nfs_fattr_init(res->dir_attr);
4114 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4116 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb),
4117 &data->args.seq_args,
4122 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
4124 struct nfs_unlinkdata *data = task->tk_calldata;
4125 struct nfs_removeres *res = &data->res;
4127 if (!nfs4_sequence_done(task, &res->seq_res))
4129 if (nfs4_async_handle_error(task, res->server, NULL,
4130 &data->timeout) == -EAGAIN)
4132 if (task->tk_status == 0)
4133 update_changeattr(dir, &res->cinfo, res->dir_attr->time_start);
4137 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4139 struct nfs_server *server = NFS_SERVER(dir);
4140 struct nfs_renameargs *arg = msg->rpc_argp;
4141 struct nfs_renameres *res = msg->rpc_resp;
4143 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
4144 res->server = server;
4145 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
4148 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4150 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
4151 &data->args.seq_args,
4156 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4157 struct inode *new_dir)
4159 struct nfs_renamedata *data = task->tk_calldata;
4160 struct nfs_renameres *res = &data->res;
4162 if (!nfs4_sequence_done(task, &res->seq_res))
4164 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
4167 if (task->tk_status == 0) {
4168 update_changeattr(old_dir, &res->old_cinfo, res->old_fattr->time_start);
4169 if (new_dir != old_dir)
4170 update_changeattr(new_dir, &res->new_cinfo, res->new_fattr->time_start);
4175 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4177 struct nfs_server *server = NFS_SERVER(inode);
4178 struct nfs4_link_arg arg = {
4179 .fh = NFS_FH(inode),
4180 .dir_fh = NFS_FH(dir),
4182 .bitmask = server->attr_bitmask,
4184 struct nfs4_link_res res = {
4188 struct rpc_message msg = {
4189 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4193 int status = -ENOMEM;
4195 res.fattr = nfs_alloc_fattr();
4196 if (res.fattr == NULL)
4199 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4200 if (IS_ERR(res.label)) {
4201 status = PTR_ERR(res.label);
4204 arg.bitmask = nfs4_bitmask(server, res.label);
4206 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4208 update_changeattr(dir, &res.cinfo, res.fattr->time_start);
4209 status = nfs_post_op_update_inode(inode, res.fattr);
4211 nfs_setsecurity(inode, res.fattr, res.label);
4215 nfs4_label_free(res.label);
4218 nfs_free_fattr(res.fattr);
4222 static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4224 struct nfs4_exception exception = { };
4227 err = nfs4_handle_exception(NFS_SERVER(inode),
4228 _nfs4_proc_link(inode, dir, name),
4230 } while (exception.retry);
4234 struct nfs4_createdata {
4235 struct rpc_message msg;
4236 struct nfs4_create_arg arg;
4237 struct nfs4_create_res res;
4239 struct nfs_fattr fattr;
4240 struct nfs4_label *label;
4243 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
4244 const struct qstr *name, struct iattr *sattr, u32 ftype)
4246 struct nfs4_createdata *data;
4248 data = kzalloc(sizeof(*data), GFP_KERNEL);
4250 struct nfs_server *server = NFS_SERVER(dir);
4252 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4253 if (IS_ERR(data->label))
4256 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4257 data->msg.rpc_argp = &data->arg;
4258 data->msg.rpc_resp = &data->res;
4259 data->arg.dir_fh = NFS_FH(dir);
4260 data->arg.server = server;
4261 data->arg.name = name;
4262 data->arg.attrs = sattr;
4263 data->arg.ftype = ftype;
4264 data->arg.bitmask = nfs4_bitmask(server, data->label);
4265 data->arg.umask = current_umask();
4266 data->res.server = server;
4267 data->res.fh = &data->fh;
4268 data->res.fattr = &data->fattr;
4269 data->res.label = data->label;
4270 nfs_fattr_init(data->res.fattr);
4278 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4280 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
4281 &data->arg.seq_args, &data->res.seq_res, 1);
4283 update_changeattr(dir, &data->res.dir_cinfo,
4284 data->res.fattr->time_start);
4285 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
4290 static void nfs4_free_createdata(struct nfs4_createdata *data)
4292 nfs4_label_free(data->label);
4296 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4297 struct page *page, unsigned int len, struct iattr *sattr,
4298 struct nfs4_label *label)
4300 struct nfs4_createdata *data;
4301 int status = -ENAMETOOLONG;
4303 if (len > NFS4_MAXPATHLEN)
4307 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4311 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4312 data->arg.u.symlink.pages = &page;
4313 data->arg.u.symlink.len = len;
4314 data->arg.label = label;
4316 status = nfs4_do_create(dir, dentry, data);
4318 nfs4_free_createdata(data);
4323 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4324 struct page *page, unsigned int len, struct iattr *sattr)
4326 struct nfs4_exception exception = { };
4327 struct nfs4_label l, *label = NULL;
4330 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4333 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4334 trace_nfs4_symlink(dir, &dentry->d_name, err);
4335 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4337 } while (exception.retry);
4339 nfs4_label_release_security(label);
4343 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4344 struct iattr *sattr, struct nfs4_label *label)
4346 struct nfs4_createdata *data;
4347 int status = -ENOMEM;
4349 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4353 data->arg.label = label;
4354 status = nfs4_do_create(dir, dentry, data);
4356 nfs4_free_createdata(data);
4361 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4362 struct iattr *sattr)
4364 struct nfs_server *server = NFS_SERVER(dir);
4365 struct nfs4_exception exception = { };
4366 struct nfs4_label l, *label = NULL;
4369 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4371 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4372 sattr->ia_mode &= ~current_umask();
4374 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4375 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4376 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4378 } while (exception.retry);
4379 nfs4_label_release_security(label);
4384 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
4385 u64 cookie, struct page **pages, unsigned int count, int plus)
4387 struct inode *dir = d_inode(dentry);
4388 struct nfs4_readdir_arg args = {
4393 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
4396 struct nfs4_readdir_res res;
4397 struct rpc_message msg = {
4398 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4405 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4407 (unsigned long long)cookie);
4408 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
4409 res.pgbase = args.pgbase;
4410 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4412 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
4413 status += args.pgbase;
4416 nfs_invalidate_atime(dir);
4418 dprintk("%s: returns %d\n", __func__, status);
4422 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
4423 u64 cookie, struct page **pages, unsigned int count, int plus)
4425 struct nfs4_exception exception = { };
4428 err = _nfs4_proc_readdir(dentry, cred, cookie,
4429 pages, count, plus);
4430 trace_nfs4_readdir(d_inode(dentry), err);
4431 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
4433 } while (exception.retry);
4437 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4438 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
4440 struct nfs4_createdata *data;
4441 int mode = sattr->ia_mode;
4442 int status = -ENOMEM;
4444 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4449 data->arg.ftype = NF4FIFO;
4450 else if (S_ISBLK(mode)) {
4451 data->arg.ftype = NF4BLK;
4452 data->arg.u.device.specdata1 = MAJOR(rdev);
4453 data->arg.u.device.specdata2 = MINOR(rdev);
4455 else if (S_ISCHR(mode)) {
4456 data->arg.ftype = NF4CHR;
4457 data->arg.u.device.specdata1 = MAJOR(rdev);
4458 data->arg.u.device.specdata2 = MINOR(rdev);
4459 } else if (!S_ISSOCK(mode)) {
4464 data->arg.label = label;
4465 status = nfs4_do_create(dir, dentry, data);
4467 nfs4_free_createdata(data);
4472 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4473 struct iattr *sattr, dev_t rdev)
4475 struct nfs_server *server = NFS_SERVER(dir);
4476 struct nfs4_exception exception = { };
4477 struct nfs4_label l, *label = NULL;
4480 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4482 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4483 sattr->ia_mode &= ~current_umask();
4485 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4486 trace_nfs4_mknod(dir, &dentry->d_name, err);
4487 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4489 } while (exception.retry);
4491 nfs4_label_release_security(label);
4496 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4497 struct nfs_fsstat *fsstat)
4499 struct nfs4_statfs_arg args = {
4501 .bitmask = server->attr_bitmask,
4503 struct nfs4_statfs_res res = {
4506 struct rpc_message msg = {
4507 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4512 nfs_fattr_init(fsstat->fattr);
4513 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4516 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4518 struct nfs4_exception exception = { };
4521 err = nfs4_handle_exception(server,
4522 _nfs4_proc_statfs(server, fhandle, fsstat),
4524 } while (exception.retry);
4528 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4529 struct nfs_fsinfo *fsinfo)
4531 struct nfs4_fsinfo_arg args = {
4533 .bitmask = server->attr_bitmask,
4535 struct nfs4_fsinfo_res res = {
4538 struct rpc_message msg = {
4539 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4544 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4547 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4549 struct nfs4_exception exception = { };
4550 unsigned long now = jiffies;
4554 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
4555 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
4557 nfs4_set_lease_period(server->nfs_client,
4558 fsinfo->lease_time * HZ,
4562 err = nfs4_handle_exception(server, err, &exception);
4563 } while (exception.retry);
4567 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4571 nfs_fattr_init(fsinfo->fattr);
4572 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
4574 /* block layout checks this! */
4575 server->pnfs_blksize = fsinfo->blksize;
4576 set_pnfs_layoutdriver(server, fhandle, fsinfo);
4582 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4583 struct nfs_pathconf *pathconf)
4585 struct nfs4_pathconf_arg args = {
4587 .bitmask = server->attr_bitmask,
4589 struct nfs4_pathconf_res res = {
4590 .pathconf = pathconf,
4592 struct rpc_message msg = {
4593 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4598 /* None of the pathconf attributes are mandatory to implement */
4599 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4600 memset(pathconf, 0, sizeof(*pathconf));
4604 nfs_fattr_init(pathconf->fattr);
4605 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4608 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4609 struct nfs_pathconf *pathconf)
4611 struct nfs4_exception exception = { };
4615 err = nfs4_handle_exception(server,
4616 _nfs4_proc_pathconf(server, fhandle, pathconf),
4618 } while (exception.retry);
4622 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
4623 const struct nfs_open_context *ctx,
4624 const struct nfs_lock_context *l_ctx,
4627 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
4629 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4631 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4632 const struct nfs_open_context *ctx,
4633 const struct nfs_lock_context *l_ctx,
4636 nfs4_stateid current_stateid;
4638 /* If the current stateid represents a lost lock, then exit */
4639 if (nfs4_set_rw_stateid(¤t_stateid, ctx, l_ctx, fmode) == -EIO)
4641 return nfs4_stateid_match(stateid, ¤t_stateid);
4644 static bool nfs4_error_stateid_expired(int err)
4647 case -NFS4ERR_DELEG_REVOKED:
4648 case -NFS4ERR_ADMIN_REVOKED:
4649 case -NFS4ERR_BAD_STATEID:
4650 case -NFS4ERR_STALE_STATEID:
4651 case -NFS4ERR_OLD_STATEID:
4652 case -NFS4ERR_OPENMODE:
4653 case -NFS4ERR_EXPIRED:
4659 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
4661 struct nfs_server *server = NFS_SERVER(hdr->inode);
4663 trace_nfs4_read(hdr, task->tk_status);
4664 if (task->tk_status < 0) {
4665 struct nfs4_exception exception = {
4666 .inode = hdr->inode,
4667 .state = hdr->args.context->state,
4668 .stateid = &hdr->args.stateid,
4670 task->tk_status = nfs4_async_handle_exception(task,
4671 server, task->tk_status, &exception);
4672 if (exception.retry) {
4673 rpc_restart_call_prepare(task);
4678 if (task->tk_status > 0)
4679 renew_lease(server, hdr->timestamp);
4683 static bool nfs4_read_stateid_changed(struct rpc_task *task,
4684 struct nfs_pgio_args *args)
4687 if (!nfs4_error_stateid_expired(task->tk_status) ||
4688 nfs4_stateid_is_current(&args->stateid,
4693 rpc_restart_call_prepare(task);
4697 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
4700 dprintk("--> %s\n", __func__);
4702 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
4704 if (nfs4_read_stateid_changed(task, &hdr->args))
4706 if (task->tk_status > 0)
4707 nfs_invalidate_atime(hdr->inode);
4708 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4709 nfs4_read_done_cb(task, hdr);
4712 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4713 struct rpc_message *msg)
4715 hdr->timestamp = jiffies;
4716 if (!hdr->pgio_done_cb)
4717 hdr->pgio_done_cb = nfs4_read_done_cb;
4718 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
4719 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
4722 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4723 struct nfs_pgio_header *hdr)
4725 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4726 &hdr->args.seq_args,
4730 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4731 hdr->args.lock_context,
4732 hdr->rw_ops->rw_mode) == -EIO)
4734 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
4739 static int nfs4_write_done_cb(struct rpc_task *task,
4740 struct nfs_pgio_header *hdr)
4742 struct inode *inode = hdr->inode;
4744 trace_nfs4_write(hdr, task->tk_status);
4745 if (task->tk_status < 0) {
4746 struct nfs4_exception exception = {
4747 .inode = hdr->inode,
4748 .state = hdr->args.context->state,
4749 .stateid = &hdr->args.stateid,
4751 task->tk_status = nfs4_async_handle_exception(task,
4752 NFS_SERVER(inode), task->tk_status,
4754 if (exception.retry) {
4755 rpc_restart_call_prepare(task);
4759 if (task->tk_status >= 0) {
4760 renew_lease(NFS_SERVER(inode), hdr->timestamp);
4761 nfs_writeback_update_inode(hdr);
4766 static bool nfs4_write_stateid_changed(struct rpc_task *task,
4767 struct nfs_pgio_args *args)
4770 if (!nfs4_error_stateid_expired(task->tk_status) ||
4771 nfs4_stateid_is_current(&args->stateid,
4776 rpc_restart_call_prepare(task);
4780 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
4782 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
4784 if (nfs4_write_stateid_changed(task, &hdr->args))
4786 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4787 nfs4_write_done_cb(task, hdr);
4791 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
4793 /* Don't request attributes for pNFS or O_DIRECT writes */
4794 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
4796 /* Otherwise, request attributes if and only if we don't hold
4799 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
4802 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4803 struct rpc_message *msg)
4805 struct nfs_server *server = NFS_SERVER(hdr->inode);
4807 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4808 hdr->args.bitmask = NULL;
4809 hdr->res.fattr = NULL;
4811 hdr->args.bitmask = server->cache_consistency_bitmask;
4813 if (!hdr->pgio_done_cb)
4814 hdr->pgio_done_cb = nfs4_write_done_cb;
4815 hdr->res.server = server;
4816 hdr->timestamp = jiffies;
4818 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
4819 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
4822 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4824 nfs4_setup_sequence(NFS_SERVER(data->inode),
4825 &data->args.seq_args,
4830 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
4832 struct inode *inode = data->inode;
4834 trace_nfs4_commit(data, task->tk_status);
4835 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4836 NULL, NULL) == -EAGAIN) {
4837 rpc_restart_call_prepare(task);
4843 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
4845 if (!nfs4_sequence_done(task, &data->res.seq_res))
4847 return data->commit_done_cb(task, data);
4850 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
4852 struct nfs_server *server = NFS_SERVER(data->inode);
4854 if (data->commit_done_cb == NULL)
4855 data->commit_done_cb = nfs4_commit_done_cb;
4856 data->res.server = server;
4857 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
4858 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4861 struct nfs4_renewdata {
4862 struct nfs_client *client;
4863 unsigned long timestamp;
4867 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4868 * standalone procedure for queueing an asynchronous RENEW.
4870 static void nfs4_renew_release(void *calldata)
4872 struct nfs4_renewdata *data = calldata;
4873 struct nfs_client *clp = data->client;
4875 if (atomic_read(&clp->cl_count) > 1)
4876 nfs4_schedule_state_renewal(clp);
4877 nfs_put_client(clp);
4881 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
4883 struct nfs4_renewdata *data = calldata;
4884 struct nfs_client *clp = data->client;
4885 unsigned long timestamp = data->timestamp;
4887 trace_nfs4_renew_async(clp, task->tk_status);
4888 switch (task->tk_status) {
4891 case -NFS4ERR_LEASE_MOVED:
4892 nfs4_schedule_lease_moved_recovery(clp);
4895 /* Unless we're shutting down, schedule state recovery! */
4896 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4898 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
4899 nfs4_schedule_lease_recovery(clp);
4902 nfs4_schedule_path_down_recovery(clp);
4904 do_renew_lease(clp, timestamp);
4907 static const struct rpc_call_ops nfs4_renew_ops = {
4908 .rpc_call_done = nfs4_renew_done,
4909 .rpc_release = nfs4_renew_release,
4912 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
4914 struct rpc_message msg = {
4915 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4919 struct nfs4_renewdata *data;
4921 if (renew_flags == 0)
4923 if (!atomic_inc_not_zero(&clp->cl_count))
4925 data = kmalloc(sizeof(*data), GFP_NOFS);
4929 data->timestamp = jiffies;
4930 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
4931 &nfs4_renew_ops, data);
4934 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
4936 struct rpc_message msg = {
4937 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4941 unsigned long now = jiffies;
4944 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4947 do_renew_lease(clp, now);
4951 static inline int nfs4_server_supports_acls(struct nfs_server *server)
4953 return server->caps & NFS_CAP_ACLS;
4956 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4957 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
4960 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
4962 static int buf_to_pages_noslab(const void *buf, size_t buflen,
4963 struct page **pages)
4965 struct page *newpage, **spages;
4971 len = min_t(size_t, PAGE_SIZE, buflen);
4972 newpage = alloc_page(GFP_KERNEL);
4974 if (newpage == NULL)
4976 memcpy(page_address(newpage), buf, len);
4981 } while (buflen != 0);
4987 __free_page(spages[rc-1]);
4991 struct nfs4_cached_acl {
4997 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
4999 struct nfs_inode *nfsi = NFS_I(inode);
5001 spin_lock(&inode->i_lock);
5002 kfree(nfsi->nfs4_acl);
5003 nfsi->nfs4_acl = acl;
5004 spin_unlock(&inode->i_lock);
5007 static void nfs4_zap_acl_attr(struct inode *inode)
5009 nfs4_set_cached_acl(inode, NULL);
5012 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5014 struct nfs_inode *nfsi = NFS_I(inode);
5015 struct nfs4_cached_acl *acl;
5018 spin_lock(&inode->i_lock);
5019 acl = nfsi->nfs4_acl;
5022 if (buf == NULL) /* user is just asking for length */
5024 if (acl->cached == 0)
5026 ret = -ERANGE; /* see getxattr(2) man page */
5027 if (acl->len > buflen)
5029 memcpy(buf, acl->data, acl->len);
5033 spin_unlock(&inode->i_lock);
5037 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
5039 struct nfs4_cached_acl *acl;
5040 size_t buflen = sizeof(*acl) + acl_len;
5042 if (buflen <= PAGE_SIZE) {
5043 acl = kmalloc(buflen, GFP_KERNEL);
5047 _copy_from_pages(acl->data, pages, pgbase, acl_len);
5049 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5056 nfs4_set_cached_acl(inode, acl);
5060 * The getxattr API returns the required buffer length when called with a
5061 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5062 * the required buf. On a NULL buf, we send a page of data to the server
5063 * guessing that the ACL request can be serviced by a page. If so, we cache
5064 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5065 * the cache. If not so, we throw away the page, and cache the required
5066 * length. The next getxattr call will then produce another round trip to
5067 * the server, this time with the input buf of the required size.
5069 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5071 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
5072 struct nfs_getaclargs args = {
5073 .fh = NFS_FH(inode),
5077 struct nfs_getaclres res = {
5080 struct rpc_message msg = {
5081 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5085 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
5086 int ret = -ENOMEM, i;
5088 /* As long as we're doing a round trip to the server anyway,
5089 * let's be prepared for a page of acl data. */
5092 if (npages > ARRAY_SIZE(pages))
5095 for (i = 0; i < npages; i++) {
5096 pages[i] = alloc_page(GFP_KERNEL);
5101 /* for decoding across pages */
5102 res.acl_scratch = alloc_page(GFP_KERNEL);
5103 if (!res.acl_scratch)
5106 args.acl_len = npages * PAGE_SIZE;
5108 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
5109 __func__, buf, buflen, npages, args.acl_len);
5110 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5111 &msg, &args.seq_args, &res.seq_res, 0);
5115 /* Handle the case where the passed-in buffer is too short */
5116 if (res.acl_flags & NFS4_ACL_TRUNC) {
5117 /* Did the user only issue a request for the acl length? */
5123 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
5125 if (res.acl_len > buflen) {
5129 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
5134 for (i = 0; i < npages; i++)
5136 __free_page(pages[i]);
5137 if (res.acl_scratch)
5138 __free_page(res.acl_scratch);
5142 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5144 struct nfs4_exception exception = { };
5147 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
5148 trace_nfs4_get_acl(inode, ret);
5151 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5152 } while (exception.retry);
5156 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5158 struct nfs_server *server = NFS_SERVER(inode);
5161 if (!nfs4_server_supports_acls(server))
5163 ret = nfs_revalidate_inode(server, inode);
5166 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5167 nfs_zap_acl_cache(inode);
5168 ret = nfs4_read_cached_acl(inode, buf, buflen);
5170 /* -ENOENT is returned if there is no ACL or if there is an ACL
5171 * but no cached acl data, just the acl length */
5173 return nfs4_get_acl_uncached(inode, buf, buflen);
5176 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5178 struct nfs_server *server = NFS_SERVER(inode);
5179 struct page *pages[NFS4ACL_MAXPAGES];
5180 struct nfs_setaclargs arg = {
5181 .fh = NFS_FH(inode),
5185 struct nfs_setaclres res;
5186 struct rpc_message msg = {
5187 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5191 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
5194 if (!nfs4_server_supports_acls(server))
5196 if (npages > ARRAY_SIZE(pages))
5198 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
5201 nfs4_inode_return_delegation(inode);
5202 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5205 * Free each page after tx, so the only ref left is
5206 * held by the network stack
5209 put_page(pages[i-1]);
5212 * Acl update can result in inode attribute update.
5213 * so mark the attribute cache invalid.
5215 spin_lock(&inode->i_lock);
5216 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5217 spin_unlock(&inode->i_lock);
5218 nfs_access_zap_cache(inode);
5219 nfs_zap_acl_cache(inode);
5223 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5225 struct nfs4_exception exception = { };
5228 err = __nfs4_proc_set_acl(inode, buf, buflen);
5229 trace_nfs4_set_acl(inode, err);
5230 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5232 } while (exception.retry);
5236 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
5237 static int _nfs4_get_security_label(struct inode *inode, void *buf,
5240 struct nfs_server *server = NFS_SERVER(inode);
5241 struct nfs_fattr fattr;
5242 struct nfs4_label label = {0, 0, buflen, buf};
5244 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5245 struct nfs4_getattr_arg arg = {
5246 .fh = NFS_FH(inode),
5249 struct nfs4_getattr_res res = {
5254 struct rpc_message msg = {
5255 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
5261 nfs_fattr_init(&fattr);
5263 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
5266 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5268 if (buflen < label.len)
5273 static int nfs4_get_security_label(struct inode *inode, void *buf,
5276 struct nfs4_exception exception = { };
5279 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5283 err = _nfs4_get_security_label(inode, buf, buflen);
5284 trace_nfs4_get_security_label(inode, err);
5285 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5287 } while (exception.retry);
5291 static int _nfs4_do_set_security_label(struct inode *inode,
5292 struct nfs4_label *ilabel,
5293 struct nfs_fattr *fattr,
5294 struct nfs4_label *olabel)
5297 struct iattr sattr = {0};
5298 struct nfs_server *server = NFS_SERVER(inode);
5299 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5300 struct nfs_setattrargs arg = {
5301 .fh = NFS_FH(inode),
5307 struct nfs_setattrres res = {
5312 struct rpc_message msg = {
5313 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5319 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
5321 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5323 dprintk("%s failed: %d\n", __func__, status);
5328 static int nfs4_do_set_security_label(struct inode *inode,
5329 struct nfs4_label *ilabel,
5330 struct nfs_fattr *fattr,
5331 struct nfs4_label *olabel)
5333 struct nfs4_exception exception = { };
5337 err = _nfs4_do_set_security_label(inode, ilabel,
5339 trace_nfs4_set_security_label(inode, err);
5340 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5342 } while (exception.retry);
5347 nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
5349 struct nfs4_label ilabel, *olabel = NULL;
5350 struct nfs_fattr fattr;
5351 struct rpc_cred *cred;
5354 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5357 nfs_fattr_init(&fattr);
5361 ilabel.label = (char *)buf;
5362 ilabel.len = buflen;
5364 cred = rpc_lookup_cred();
5366 return PTR_ERR(cred);
5368 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5369 if (IS_ERR(olabel)) {
5370 status = -PTR_ERR(olabel);
5374 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5376 nfs_setsecurity(inode, &fattr, olabel);
5378 nfs4_label_free(olabel);
5383 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5386 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5387 nfs4_verifier *bootverf)
5391 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5392 /* An impossible timestamp guarantees this value
5393 * will never match a generated boot time. */
5394 verf[0] = cpu_to_be32(U32_MAX);
5395 verf[1] = cpu_to_be32(U32_MAX);
5397 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
5398 u64 ns = ktime_to_ns(nn->boot_time);
5400 verf[0] = cpu_to_be32(ns >> 32);
5401 verf[1] = cpu_to_be32(ns);
5403 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5407 nfs4_init_nonuniform_client_string(struct nfs_client *clp)
5412 if (clp->cl_owner_id != NULL)
5416 len = 14 + strlen(clp->cl_ipaddr) + 1 +
5417 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5419 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5423 if (len > NFS4_OPAQUE_LIMIT + 1)
5427 * Since this string is allocated at mount time, and held until the
5428 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5429 * about a memory-reclaim deadlock.
5431 str = kmalloc(len, GFP_KERNEL);
5436 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
5438 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5439 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
5442 clp->cl_owner_id = str;
5447 nfs4_init_uniquifier_client_string(struct nfs_client *clp)
5452 len = 10 + 10 + 1 + 10 + 1 +
5453 strlen(nfs4_client_id_uniquifier) + 1 +
5454 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5456 if (len > NFS4_OPAQUE_LIMIT + 1)
5460 * Since this string is allocated at mount time, and held until the
5461 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5462 * about a memory-reclaim deadlock.
5464 str = kmalloc(len, GFP_KERNEL);
5468 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
5469 clp->rpc_ops->version, clp->cl_minorversion,
5470 nfs4_client_id_uniquifier,
5471 clp->cl_rpcclient->cl_nodename);
5472 clp->cl_owner_id = str;
5477 nfs4_init_uniform_client_string(struct nfs_client *clp)
5482 if (clp->cl_owner_id != NULL)
5485 if (nfs4_client_id_uniquifier[0] != '\0')
5486 return nfs4_init_uniquifier_client_string(clp);
5488 len = 10 + 10 + 1 + 10 + 1 +
5489 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5491 if (len > NFS4_OPAQUE_LIMIT + 1)
5495 * Since this string is allocated at mount time, and held until the
5496 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5497 * about a memory-reclaim deadlock.
5499 str = kmalloc(len, GFP_KERNEL);
5503 scnprintf(str, len, "Linux NFSv%u.%u %s",
5504 clp->rpc_ops->version, clp->cl_minorversion,
5505 clp->cl_rpcclient->cl_nodename);
5506 clp->cl_owner_id = str;
5511 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5512 * services. Advertise one based on the address family of the
5516 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5518 if (strchr(clp->cl_ipaddr, ':') != NULL)
5519 return scnprintf(buf, len, "tcp6");
5521 return scnprintf(buf, len, "tcp");
5524 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5526 struct nfs4_setclientid *sc = calldata;
5528 if (task->tk_status == 0)
5529 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5532 static const struct rpc_call_ops nfs4_setclientid_ops = {
5533 .rpc_call_done = nfs4_setclientid_done,
5537 * nfs4_proc_setclientid - Negotiate client ID
5538 * @clp: state data structure
5539 * @program: RPC program for NFSv4 callback service
5540 * @port: IP port number for NFS4 callback service
5541 * @cred: RPC credential to use for this call
5542 * @res: where to place the result
5544 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5546 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5547 unsigned short port, struct rpc_cred *cred,
5548 struct nfs4_setclientid_res *res)
5550 nfs4_verifier sc_verifier;
5551 struct nfs4_setclientid setclientid = {
5552 .sc_verifier = &sc_verifier,
5556 struct rpc_message msg = {
5557 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5558 .rpc_argp = &setclientid,
5562 struct rpc_task *task;
5563 struct rpc_task_setup task_setup_data = {
5564 .rpc_client = clp->cl_rpcclient,
5565 .rpc_message = &msg,
5566 .callback_ops = &nfs4_setclientid_ops,
5567 .callback_data = &setclientid,
5568 .flags = RPC_TASK_TIMEOUT,
5572 /* nfs_client_id4 */
5573 nfs4_init_boot_verifier(clp, &sc_verifier);
5575 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5576 status = nfs4_init_uniform_client_string(clp);
5578 status = nfs4_init_nonuniform_client_string(clp);
5584 setclientid.sc_netid_len =
5585 nfs4_init_callback_netid(clp,
5586 setclientid.sc_netid,
5587 sizeof(setclientid.sc_netid));
5588 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
5589 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
5590 clp->cl_ipaddr, port >> 8, port & 255);
5592 dprintk("NFS call setclientid auth=%s, '%s'\n",
5593 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5595 task = rpc_run_task(&task_setup_data);
5597 status = PTR_ERR(task);
5600 status = task->tk_status;
5601 if (setclientid.sc_cred) {
5602 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5603 put_rpccred(setclientid.sc_cred);
5607 trace_nfs4_setclientid(clp, status);
5608 dprintk("NFS reply setclientid: %d\n", status);
5613 * nfs4_proc_setclientid_confirm - Confirm client ID
5614 * @clp: state data structure
5615 * @res: result of a previous SETCLIENTID
5616 * @cred: RPC credential to use for this call
5618 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5620 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
5621 struct nfs4_setclientid_res *arg,
5622 struct rpc_cred *cred)
5624 struct rpc_message msg = {
5625 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
5631 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5632 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5634 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5635 trace_nfs4_setclientid_confirm(clp, status);
5636 dprintk("NFS reply setclientid_confirm: %d\n", status);
5640 struct nfs4_delegreturndata {
5641 struct nfs4_delegreturnargs args;
5642 struct nfs4_delegreturnres res;
5644 nfs4_stateid stateid;
5645 unsigned long timestamp;
5647 struct nfs4_layoutreturn_args arg;
5648 struct nfs4_layoutreturn_res res;
5649 struct nfs4_xdr_opaque_data ld_private;
5653 struct nfs_fattr fattr;
5655 struct inode *inode;
5658 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5660 struct nfs4_delegreturndata *data = calldata;
5662 if (!nfs4_sequence_done(task, &data->res.seq_res))
5665 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
5667 /* Handle Layoutreturn errors */
5668 if (data->args.lr_args && task->tk_status != 0) {
5669 switch(data->res.lr_ret) {
5671 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5674 data->args.lr_args = NULL;
5675 data->res.lr_res = NULL;
5677 case -NFS4ERR_ADMIN_REVOKED:
5678 case -NFS4ERR_DELEG_REVOKED:
5679 case -NFS4ERR_EXPIRED:
5680 case -NFS4ERR_BAD_STATEID:
5681 case -NFS4ERR_OLD_STATEID:
5682 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
5683 case -NFS4ERR_WRONG_CRED:
5684 data->args.lr_args = NULL;
5685 data->res.lr_res = NULL;
5686 data->res.lr_ret = 0;
5687 rpc_restart_call_prepare(task);
5692 switch (task->tk_status) {
5694 renew_lease(data->res.server, data->timestamp);
5696 case -NFS4ERR_ADMIN_REVOKED:
5697 case -NFS4ERR_DELEG_REVOKED:
5698 case -NFS4ERR_EXPIRED:
5699 nfs4_free_revoked_stateid(data->res.server,
5701 task->tk_msg.rpc_cred);
5702 case -NFS4ERR_BAD_STATEID:
5703 case -NFS4ERR_OLD_STATEID:
5704 case -NFS4ERR_STALE_STATEID:
5705 task->tk_status = 0;
5707 case -NFS4ERR_ACCESS:
5708 if (data->args.bitmask) {
5709 data->args.bitmask = NULL;
5710 data->res.fattr = NULL;
5711 task->tk_status = 0;
5712 rpc_restart_call_prepare(task);
5716 if (nfs4_async_handle_error(task, data->res.server,
5717 NULL, NULL) == -EAGAIN) {
5718 rpc_restart_call_prepare(task);
5722 data->rpc_status = task->tk_status;
5725 static void nfs4_delegreturn_release(void *calldata)
5727 struct nfs4_delegreturndata *data = calldata;
5728 struct inode *inode = data->inode;
5732 pnfs_roc_release(&data->lr.arg, &data->lr.res,
5734 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5735 nfs_iput_and_deactive(inode);
5740 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5742 struct nfs4_delegreturndata *d_data;
5744 d_data = (struct nfs4_delegreturndata *)data;
5746 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
5749 nfs4_setup_sequence(d_data->res.server,
5750 &d_data->args.seq_args,
5751 &d_data->res.seq_res,
5755 static const struct rpc_call_ops nfs4_delegreturn_ops = {
5756 .rpc_call_prepare = nfs4_delegreturn_prepare,
5757 .rpc_call_done = nfs4_delegreturn_done,
5758 .rpc_release = nfs4_delegreturn_release,
5761 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
5763 struct nfs4_delegreturndata *data;
5764 struct nfs_server *server = NFS_SERVER(inode);
5765 struct rpc_task *task;
5766 struct rpc_message msg = {
5767 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5770 struct rpc_task_setup task_setup_data = {
5771 .rpc_client = server->client,
5772 .rpc_message = &msg,
5773 .callback_ops = &nfs4_delegreturn_ops,
5774 .flags = RPC_TASK_ASYNC,
5778 data = kzalloc(sizeof(*data), GFP_NOFS);
5781 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
5783 nfs4_state_protect(server->nfs_client,
5784 NFS_SP4_MACH_CRED_CLEANUP,
5785 &task_setup_data.rpc_client, &msg);
5787 data->args.fhandle = &data->fh;
5788 data->args.stateid = &data->stateid;
5789 data->args.bitmask = server->cache_consistency_bitmask;
5790 nfs_copy_fh(&data->fh, NFS_FH(inode));
5791 nfs4_stateid_copy(&data->stateid, stateid);
5792 data->res.fattr = &data->fattr;
5793 data->res.server = server;
5794 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5795 data->lr.arg.ld_private = &data->lr.ld_private;
5796 nfs_fattr_init(data->res.fattr);
5797 data->timestamp = jiffies;
5798 data->rpc_status = 0;
5799 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
5800 data->inode = nfs_igrab_and_active(inode);
5803 data->args.lr_args = &data->lr.arg;
5804 data->res.lr_res = &data->lr.res;
5806 } else if (data->lr.roc) {
5807 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
5808 data->lr.roc = false;
5811 task_setup_data.callback_data = data;
5812 msg.rpc_argp = &data->args;
5813 msg.rpc_resp = &data->res;
5814 task = rpc_run_task(&task_setup_data);
5816 return PTR_ERR(task);
5819 status = nfs4_wait_for_completion_rpc_task(task);
5822 status = data->rpc_status;
5828 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
5830 struct nfs_server *server = NFS_SERVER(inode);
5831 struct nfs4_exception exception = { };
5834 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
5835 trace_nfs4_delegreturn(inode, stateid, err);
5837 case -NFS4ERR_STALE_STATEID:
5838 case -NFS4ERR_EXPIRED:
5842 err = nfs4_handle_exception(server, err, &exception);
5843 } while (exception.retry);
5847 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5849 struct inode *inode = state->inode;
5850 struct nfs_server *server = NFS_SERVER(inode);
5851 struct nfs_client *clp = server->nfs_client;
5852 struct nfs_lockt_args arg = {
5853 .fh = NFS_FH(inode),
5856 struct nfs_lockt_res res = {
5859 struct rpc_message msg = {
5860 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5863 .rpc_cred = state->owner->so_cred,
5865 struct nfs4_lock_state *lsp;
5868 arg.lock_owner.clientid = clp->cl_clientid;
5869 status = nfs4_set_lock_state(state, request);
5872 lsp = request->fl_u.nfs4_fl.owner;
5873 arg.lock_owner.id = lsp->ls_seqid.owner_id;
5874 arg.lock_owner.s_dev = server->s_dev;
5875 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5878 request->fl_type = F_UNLCK;
5880 case -NFS4ERR_DENIED:
5883 request->fl_ops->fl_release_private(request);
5884 request->fl_ops = NULL;
5889 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5891 struct nfs4_exception exception = { };
5895 err = _nfs4_proc_getlk(state, cmd, request);
5896 trace_nfs4_get_lock(request, state, cmd, err);
5897 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
5899 } while (exception.retry);
5903 struct nfs4_unlockdata {
5904 struct nfs_locku_args arg;
5905 struct nfs_locku_res res;
5906 struct nfs4_lock_state *lsp;
5907 struct nfs_open_context *ctx;
5908 struct file_lock fl;
5909 struct nfs_server *server;
5910 unsigned long timestamp;
5913 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5914 struct nfs_open_context *ctx,
5915 struct nfs4_lock_state *lsp,
5916 struct nfs_seqid *seqid)
5918 struct nfs4_unlockdata *p;
5919 struct inode *inode = lsp->ls_state->inode;
5921 p = kzalloc(sizeof(*p), GFP_NOFS);
5924 p->arg.fh = NFS_FH(inode);
5926 p->arg.seqid = seqid;
5927 p->res.seqid = seqid;
5929 atomic_inc(&lsp->ls_count);
5930 /* Ensure we don't close file until we're done freeing locks! */
5931 p->ctx = get_nfs_open_context(ctx);
5932 memcpy(&p->fl, fl, sizeof(p->fl));
5933 p->server = NFS_SERVER(inode);
5937 static void nfs4_locku_release_calldata(void *data)
5939 struct nfs4_unlockdata *calldata = data;
5940 nfs_free_seqid(calldata->arg.seqid);
5941 nfs4_put_lock_state(calldata->lsp);
5942 put_nfs_open_context(calldata->ctx);
5946 static void nfs4_locku_done(struct rpc_task *task, void *data)
5948 struct nfs4_unlockdata *calldata = data;
5950 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5952 switch (task->tk_status) {
5954 renew_lease(calldata->server, calldata->timestamp);
5955 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
5956 if (nfs4_update_lock_stateid(calldata->lsp,
5957 &calldata->res.stateid))
5959 case -NFS4ERR_ADMIN_REVOKED:
5960 case -NFS4ERR_EXPIRED:
5961 nfs4_free_revoked_stateid(calldata->server,
5962 &calldata->arg.stateid,
5963 task->tk_msg.rpc_cred);
5964 case -NFS4ERR_BAD_STATEID:
5965 case -NFS4ERR_OLD_STATEID:
5966 case -NFS4ERR_STALE_STATEID:
5967 if (!nfs4_stateid_match(&calldata->arg.stateid,
5968 &calldata->lsp->ls_stateid))
5969 rpc_restart_call_prepare(task);
5972 if (nfs4_async_handle_error(task, calldata->server,
5973 NULL, NULL) == -EAGAIN)
5974 rpc_restart_call_prepare(task);
5976 nfs_release_seqid(calldata->arg.seqid);
5979 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
5981 struct nfs4_unlockdata *calldata = data;
5983 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
5985 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
5986 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
5987 /* Note: exit _without_ running nfs4_locku_done */
5990 calldata->timestamp = jiffies;
5991 if (nfs4_setup_sequence(calldata->server,
5992 &calldata->arg.seq_args,
5993 &calldata->res.seq_res,
5995 nfs_release_seqid(calldata->arg.seqid);
5998 task->tk_action = NULL;
6000 nfs4_sequence_done(task, &calldata->res.seq_res);
6003 static const struct rpc_call_ops nfs4_locku_ops = {
6004 .rpc_call_prepare = nfs4_locku_prepare,
6005 .rpc_call_done = nfs4_locku_done,
6006 .rpc_release = nfs4_locku_release_calldata,
6009 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6010 struct nfs_open_context *ctx,
6011 struct nfs4_lock_state *lsp,
6012 struct nfs_seqid *seqid)
6014 struct nfs4_unlockdata *data;
6015 struct rpc_message msg = {
6016 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6017 .rpc_cred = ctx->cred,
6019 struct rpc_task_setup task_setup_data = {
6020 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
6021 .rpc_message = &msg,
6022 .callback_ops = &nfs4_locku_ops,
6023 .workqueue = nfsiod_workqueue,
6024 .flags = RPC_TASK_ASYNC,
6027 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6028 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6030 /* Ensure this is an unlock - when canceling a lock, the
6031 * canceled lock is passed in, and it won't be an unlock.
6033 fl->fl_type = F_UNLCK;
6035 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6037 nfs_free_seqid(seqid);
6038 return ERR_PTR(-ENOMEM);
6041 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
6042 msg.rpc_argp = &data->arg;
6043 msg.rpc_resp = &data->res;
6044 task_setup_data.callback_data = data;
6045 return rpc_run_task(&task_setup_data);
6048 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6050 struct inode *inode = state->inode;
6051 struct nfs4_state_owner *sp = state->owner;
6052 struct nfs_inode *nfsi = NFS_I(inode);
6053 struct nfs_seqid *seqid;
6054 struct nfs4_lock_state *lsp;
6055 struct rpc_task *task;
6056 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6058 unsigned char fl_flags = request->fl_flags;
6060 status = nfs4_set_lock_state(state, request);
6061 /* Unlock _before_ we do the RPC call */
6062 request->fl_flags |= FL_EXISTS;
6063 /* Exclude nfs_delegation_claim_locks() */
6064 mutex_lock(&sp->so_delegreturn_mutex);
6065 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
6066 down_read(&nfsi->rwsem);
6067 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
6068 up_read(&nfsi->rwsem);
6069 mutex_unlock(&sp->so_delegreturn_mutex);
6072 up_read(&nfsi->rwsem);
6073 mutex_unlock(&sp->so_delegreturn_mutex);
6076 /* Is this a delegated lock? */
6077 lsp = request->fl_u.nfs4_fl.owner;
6078 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6080 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6081 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
6085 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
6086 status = PTR_ERR(task);
6089 status = nfs4_wait_for_completion_rpc_task(task);
6092 request->fl_flags = fl_flags;
6093 trace_nfs4_unlock(request, state, F_SETLK, status);
6097 struct nfs4_lockdata {
6098 struct nfs_lock_args arg;
6099 struct nfs_lock_res res;
6100 struct nfs4_lock_state *lsp;
6101 struct nfs_open_context *ctx;
6102 struct file_lock fl;
6103 unsigned long timestamp;
6106 struct nfs_server *server;
6109 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
6110 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6113 struct nfs4_lockdata *p;
6114 struct inode *inode = lsp->ls_state->inode;
6115 struct nfs_server *server = NFS_SERVER(inode);
6116 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6118 p = kzalloc(sizeof(*p), gfp_mask);
6122 p->arg.fh = NFS_FH(inode);
6124 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
6125 if (IS_ERR(p->arg.open_seqid))
6127 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6128 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
6129 if (IS_ERR(p->arg.lock_seqid))
6130 goto out_free_seqid;
6131 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
6132 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
6133 p->arg.lock_owner.s_dev = server->s_dev;
6134 p->res.lock_seqid = p->arg.lock_seqid;
6137 atomic_inc(&lsp->ls_count);
6138 p->ctx = get_nfs_open_context(ctx);
6139 memcpy(&p->fl, fl, sizeof(p->fl));
6142 nfs_free_seqid(p->arg.open_seqid);
6148 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6150 struct nfs4_lockdata *data = calldata;
6151 struct nfs4_state *state = data->lsp->ls_state;
6153 dprintk("%s: begin!\n", __func__);
6154 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
6156 /* Do we need to do an open_to_lock_owner? */
6157 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
6158 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
6159 goto out_release_lock_seqid;
6161 nfs4_stateid_copy(&data->arg.open_stateid,
6162 &state->open_stateid);
6163 data->arg.new_lock_owner = 1;
6164 data->res.open_seqid = data->arg.open_seqid;
6166 data->arg.new_lock_owner = 0;
6167 nfs4_stateid_copy(&data->arg.lock_stateid,
6168 &data->lsp->ls_stateid);
6170 if (!nfs4_valid_open_stateid(state)) {
6171 data->rpc_status = -EBADF;
6172 task->tk_action = NULL;
6173 goto out_release_open_seqid;
6175 data->timestamp = jiffies;
6176 if (nfs4_setup_sequence(data->server,
6177 &data->arg.seq_args,
6181 out_release_open_seqid:
6182 nfs_release_seqid(data->arg.open_seqid);
6183 out_release_lock_seqid:
6184 nfs_release_seqid(data->arg.lock_seqid);
6186 nfs4_sequence_done(task, &data->res.seq_res);
6187 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
6190 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6192 struct nfs4_lockdata *data = calldata;
6193 struct nfs4_lock_state *lsp = data->lsp;
6195 dprintk("%s: begin!\n", __func__);
6197 if (!nfs4_sequence_done(task, &data->res.seq_res))
6200 data->rpc_status = task->tk_status;
6201 switch (task->tk_status) {
6203 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
6205 if (data->arg.new_lock) {
6206 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
6207 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
6208 rpc_restart_call_prepare(task);
6212 if (data->arg.new_lock_owner != 0) {
6213 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6214 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6215 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6216 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6217 rpc_restart_call_prepare(task);
6219 case -NFS4ERR_BAD_STATEID:
6220 case -NFS4ERR_OLD_STATEID:
6221 case -NFS4ERR_STALE_STATEID:
6222 case -NFS4ERR_EXPIRED:
6223 if (data->arg.new_lock_owner != 0) {
6224 if (!nfs4_stateid_match(&data->arg.open_stateid,
6225 &lsp->ls_state->open_stateid))
6226 rpc_restart_call_prepare(task);
6227 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6229 rpc_restart_call_prepare(task);
6231 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
6234 static void nfs4_lock_release(void *calldata)
6236 struct nfs4_lockdata *data = calldata;
6238 dprintk("%s: begin!\n", __func__);
6239 nfs_free_seqid(data->arg.open_seqid);
6240 if (data->cancelled != 0) {
6241 struct rpc_task *task;
6242 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6243 data->arg.lock_seqid);
6245 rpc_put_task_async(task);
6246 dprintk("%s: cancelling lock!\n", __func__);
6248 nfs_free_seqid(data->arg.lock_seqid);
6249 nfs4_put_lock_state(data->lsp);
6250 put_nfs_open_context(data->ctx);
6252 dprintk("%s: done!\n", __func__);
6255 static const struct rpc_call_ops nfs4_lock_ops = {
6256 .rpc_call_prepare = nfs4_lock_prepare,
6257 .rpc_call_done = nfs4_lock_done,
6258 .rpc_release = nfs4_lock_release,
6261 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6264 case -NFS4ERR_ADMIN_REVOKED:
6265 case -NFS4ERR_EXPIRED:
6266 case -NFS4ERR_BAD_STATEID:
6267 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6268 if (new_lock_owner != 0 ||
6269 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
6270 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
6272 case -NFS4ERR_STALE_STATEID:
6273 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6274 nfs4_schedule_lease_recovery(server->nfs_client);
6278 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
6280 struct nfs4_lockdata *data;
6281 struct rpc_task *task;
6282 struct rpc_message msg = {
6283 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6284 .rpc_cred = state->owner->so_cred,
6286 struct rpc_task_setup task_setup_data = {
6287 .rpc_client = NFS_CLIENT(state->inode),
6288 .rpc_message = &msg,
6289 .callback_ops = &nfs4_lock_ops,
6290 .workqueue = nfsiod_workqueue,
6291 .flags = RPC_TASK_ASYNC,
6295 dprintk("%s: begin!\n", __func__);
6296 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
6297 fl->fl_u.nfs4_fl.owner,
6298 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
6302 data->arg.block = 1;
6303 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
6304 msg.rpc_argp = &data->arg;
6305 msg.rpc_resp = &data->res;
6306 task_setup_data.callback_data = data;
6307 if (recovery_type > NFS_LOCK_NEW) {
6308 if (recovery_type == NFS_LOCK_RECLAIM)
6309 data->arg.reclaim = NFS_LOCK_RECLAIM;
6310 nfs4_set_sequence_privileged(&data->arg.seq_args);
6312 data->arg.new_lock = 1;
6313 task = rpc_run_task(&task_setup_data);
6315 return PTR_ERR(task);
6316 ret = nfs4_wait_for_completion_rpc_task(task);
6318 ret = data->rpc_status;
6320 nfs4_handle_setlk_error(data->server, data->lsp,
6321 data->arg.new_lock_owner, ret);
6323 data->cancelled = 1;
6325 dprintk("%s: done, ret = %d!\n", __func__, ret);
6326 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
6330 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6332 struct nfs_server *server = NFS_SERVER(state->inode);
6333 struct nfs4_exception exception = {
6334 .inode = state->inode,
6339 /* Cache the lock if possible... */
6340 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6342 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
6343 if (err != -NFS4ERR_DELAY)
6345 nfs4_handle_exception(server, err, &exception);
6346 } while (exception.retry);
6350 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6352 struct nfs_server *server = NFS_SERVER(state->inode);
6353 struct nfs4_exception exception = {
6354 .inode = state->inode,
6358 err = nfs4_set_lock_state(state, request);
6361 if (!recover_lost_locks) {
6362 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6366 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6368 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
6372 case -NFS4ERR_GRACE:
6373 case -NFS4ERR_DELAY:
6374 nfs4_handle_exception(server, err, &exception);
6377 } while (exception.retry);
6382 #if defined(CONFIG_NFS_V4_1)
6383 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6385 struct nfs4_lock_state *lsp;
6388 status = nfs4_set_lock_state(state, request);
6391 lsp = request->fl_u.nfs4_fl.owner;
6392 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6393 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6395 status = nfs4_lock_expired(state, request);
6400 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6402 struct nfs_inode *nfsi = NFS_I(state->inode);
6403 struct nfs4_state_owner *sp = state->owner;
6404 unsigned char fl_flags = request->fl_flags;
6407 request->fl_flags |= FL_ACCESS;
6408 status = locks_lock_inode_wait(state->inode, request);
6411 mutex_lock(&sp->so_delegreturn_mutex);
6412 down_read(&nfsi->rwsem);
6413 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
6414 /* Yes: cache locks! */
6415 /* ...but avoid races with delegation recall... */
6416 request->fl_flags = fl_flags & ~FL_SLEEP;
6417 status = locks_lock_inode_wait(state->inode, request);
6418 up_read(&nfsi->rwsem);
6419 mutex_unlock(&sp->so_delegreturn_mutex);
6422 up_read(&nfsi->rwsem);
6423 mutex_unlock(&sp->so_delegreturn_mutex);
6424 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
6426 request->fl_flags = fl_flags;
6430 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6432 struct nfs4_exception exception = {
6434 .inode = state->inode,
6439 err = _nfs4_proc_setlk(state, cmd, request);
6440 if (err == -NFS4ERR_DENIED)
6442 err = nfs4_handle_exception(NFS_SERVER(state->inode),
6444 } while (exception.retry);
6448 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6449 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6452 nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6453 struct file_lock *request)
6455 int status = -ERESTARTSYS;
6456 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6458 while(!signalled()) {
6459 status = nfs4_proc_setlk(state, cmd, request);
6460 if ((status != -EAGAIN) || IS_SETLK(cmd))
6462 freezable_schedule_timeout_interruptible(timeout);
6464 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6465 status = -ERESTARTSYS;
6470 #ifdef CONFIG_NFS_V4_1
6471 struct nfs4_lock_waiter {
6472 struct task_struct *task;
6473 struct inode *inode;
6474 struct nfs_lowner *owner;
6479 nfs4_wake_lock_waiter(wait_queue_t *wait, unsigned int mode, int flags, void *key)
6482 struct cb_notify_lock_args *cbnl = key;
6483 struct nfs4_lock_waiter *waiter = wait->private;
6484 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6485 *wowner = waiter->owner;
6487 /* Only wake if the callback was for the same owner */
6488 if (lowner->clientid != wowner->clientid ||
6489 lowner->id != wowner->id ||
6490 lowner->s_dev != wowner->s_dev)
6493 /* Make sure it's for the right inode */
6494 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6497 waiter->notified = true;
6499 /* override "private" so we can use default_wake_function */
6500 wait->private = waiter->task;
6501 ret = autoremove_wake_function(wait, mode, flags, key);
6502 wait->private = waiter;
6507 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6509 int status = -ERESTARTSYS;
6510 unsigned long flags;
6511 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6512 struct nfs_server *server = NFS_SERVER(state->inode);
6513 struct nfs_client *clp = server->nfs_client;
6514 wait_queue_head_t *q = &clp->cl_lock_waitq;
6515 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6516 .id = lsp->ls_seqid.owner_id,
6517 .s_dev = server->s_dev };
6518 struct nfs4_lock_waiter waiter = { .task = current,
6519 .inode = state->inode,
6521 .notified = false };
6524 /* Don't bother with waitqueue if we don't expect a callback */
6525 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6526 return nfs4_retry_setlk_simple(state, cmd, request);
6529 wait.private = &waiter;
6530 wait.func = nfs4_wake_lock_waiter;
6531 add_wait_queue(q, &wait);
6533 while(!signalled()) {
6534 status = nfs4_proc_setlk(state, cmd, request);
6535 if ((status != -EAGAIN) || IS_SETLK(cmd))
6538 status = -ERESTARTSYS;
6539 spin_lock_irqsave(&q->lock, flags);
6540 if (waiter.notified) {
6541 spin_unlock_irqrestore(&q->lock, flags);
6544 set_current_state(TASK_INTERRUPTIBLE);
6545 spin_unlock_irqrestore(&q->lock, flags);
6547 freezable_schedule_timeout_interruptible(NFS4_LOCK_MAXTIMEOUT);
6550 finish_wait(q, &wait);
6553 #else /* !CONFIG_NFS_V4_1 */
6555 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6557 return nfs4_retry_setlk_simple(state, cmd, request);
6562 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6564 struct nfs_open_context *ctx;
6565 struct nfs4_state *state;
6568 /* verify open state */
6569 ctx = nfs_file_open_context(filp);
6572 if (request->fl_start < 0 || request->fl_end < 0)
6575 if (IS_GETLK(cmd)) {
6577 return nfs4_proc_getlk(state, F_GETLK, request);
6581 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6584 if (request->fl_type == F_UNLCK) {
6586 return nfs4_proc_unlck(state, cmd, request);
6593 if ((request->fl_flags & FL_POSIX) &&
6594 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6598 * Don't rely on the VFS having checked the file open mode,
6599 * since it won't do this for flock() locks.
6601 switch (request->fl_type) {
6603 if (!(filp->f_mode & FMODE_READ))
6607 if (!(filp->f_mode & FMODE_WRITE))
6611 status = nfs4_set_lock_state(state, request);
6615 return nfs4_retry_setlk(state, cmd, request);
6618 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
6620 struct nfs_server *server = NFS_SERVER(state->inode);
6623 err = nfs4_set_lock_state(state, fl);
6626 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
6627 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
6630 struct nfs_release_lockowner_data {
6631 struct nfs4_lock_state *lsp;
6632 struct nfs_server *server;
6633 struct nfs_release_lockowner_args args;
6634 struct nfs_release_lockowner_res res;
6635 unsigned long timestamp;
6638 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6640 struct nfs_release_lockowner_data *data = calldata;
6641 struct nfs_server *server = data->server;
6642 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
6643 &data->args.seq_args, &data->res.seq_res, task);
6644 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6645 data->timestamp = jiffies;
6648 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6650 struct nfs_release_lockowner_data *data = calldata;
6651 struct nfs_server *server = data->server;
6653 nfs40_sequence_done(task, &data->res.seq_res);
6655 switch (task->tk_status) {
6657 renew_lease(server, data->timestamp);
6659 case -NFS4ERR_STALE_CLIENTID:
6660 case -NFS4ERR_EXPIRED:
6661 nfs4_schedule_lease_recovery(server->nfs_client);
6663 case -NFS4ERR_LEASE_MOVED:
6664 case -NFS4ERR_DELAY:
6665 if (nfs4_async_handle_error(task, server,
6666 NULL, NULL) == -EAGAIN)
6667 rpc_restart_call_prepare(task);
6671 static void nfs4_release_lockowner_release(void *calldata)
6673 struct nfs_release_lockowner_data *data = calldata;
6674 nfs4_free_lock_state(data->server, data->lsp);
6678 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
6679 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6680 .rpc_call_done = nfs4_release_lockowner_done,
6681 .rpc_release = nfs4_release_lockowner_release,
6685 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
6687 struct nfs_release_lockowner_data *data;
6688 struct rpc_message msg = {
6689 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6692 if (server->nfs_client->cl_mvops->minor_version != 0)
6695 data = kmalloc(sizeof(*data), GFP_NOFS);
6699 data->server = server;
6700 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6701 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6702 data->args.lock_owner.s_dev = server->s_dev;
6704 msg.rpc_argp = &data->args;
6705 msg.rpc_resp = &data->res;
6706 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
6707 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
6710 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6712 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
6713 struct dentry *unused, struct inode *inode,
6714 const char *key, const void *buf,
6715 size_t buflen, int flags)
6717 return nfs4_proc_set_acl(inode, buf, buflen);
6720 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
6721 struct dentry *unused, struct inode *inode,
6722 const char *key, void *buf, size_t buflen)
6724 return nfs4_proc_get_acl(inode, buf, buflen);
6727 static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
6729 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
6732 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
6734 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
6735 struct dentry *unused, struct inode *inode,
6736 const char *key, const void *buf,
6737 size_t buflen, int flags)
6739 if (security_ismaclabel(key))
6740 return nfs4_set_security_label(inode, buf, buflen);
6745 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
6746 struct dentry *unused, struct inode *inode,
6747 const char *key, void *buf, size_t buflen)
6749 if (security_ismaclabel(key))
6750 return nfs4_get_security_label(inode, buf, buflen);
6755 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6759 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6760 len = security_inode_listsecurity(inode, list, list_len);
6761 if (list_len && len > list_len)
6767 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6768 .prefix = XATTR_SECURITY_PREFIX,
6769 .get = nfs4_xattr_get_nfs4_label,
6770 .set = nfs4_xattr_set_nfs4_label,
6776 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6784 * nfs_fhget will use either the mounted_on_fileid or the fileid
6786 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6788 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6789 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6790 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
6791 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
6794 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
6795 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
6796 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6800 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6801 const struct qstr *name,
6802 struct nfs4_fs_locations *fs_locations,
6805 struct nfs_server *server = NFS_SERVER(dir);
6807 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6809 struct nfs4_fs_locations_arg args = {
6810 .dir_fh = NFS_FH(dir),
6815 struct nfs4_fs_locations_res res = {
6816 .fs_locations = fs_locations,
6818 struct rpc_message msg = {
6819 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6825 dprintk("%s: start\n", __func__);
6827 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6828 * is not supported */
6829 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6830 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6832 bitmask[0] |= FATTR4_WORD0_FILEID;
6834 nfs_fattr_init(&fs_locations->fattr);
6835 fs_locations->server = server;
6836 fs_locations->nlocations = 0;
6837 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
6838 dprintk("%s: returned status = %d\n", __func__, status);
6842 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6843 const struct qstr *name,
6844 struct nfs4_fs_locations *fs_locations,
6847 struct nfs4_exception exception = { };
6850 err = _nfs4_proc_fs_locations(client, dir, name,
6851 fs_locations, page);
6852 trace_nfs4_get_fs_locations(dir, name, err);
6853 err = nfs4_handle_exception(NFS_SERVER(dir), err,
6855 } while (exception.retry);
6860 * This operation also signals the server that this client is
6861 * performing migration recovery. The server can stop returning
6862 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6863 * appended to this compound to identify the client ID which is
6864 * performing recovery.
6866 static int _nfs40_proc_get_locations(struct inode *inode,
6867 struct nfs4_fs_locations *locations,
6868 struct page *page, struct rpc_cred *cred)
6870 struct nfs_server *server = NFS_SERVER(inode);
6871 struct rpc_clnt *clnt = server->client;
6873 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6875 struct nfs4_fs_locations_arg args = {
6876 .clientid = server->nfs_client->cl_clientid,
6877 .fh = NFS_FH(inode),
6880 .migration = 1, /* skip LOOKUP */
6881 .renew = 1, /* append RENEW */
6883 struct nfs4_fs_locations_res res = {
6884 .fs_locations = locations,
6888 struct rpc_message msg = {
6889 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6894 unsigned long now = jiffies;
6897 nfs_fattr_init(&locations->fattr);
6898 locations->server = server;
6899 locations->nlocations = 0;
6901 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6902 nfs4_set_sequence_privileged(&args.seq_args);
6903 status = nfs4_call_sync_sequence(clnt, server, &msg,
6904 &args.seq_args, &res.seq_res);
6908 renew_lease(server, now);
6912 #ifdef CONFIG_NFS_V4_1
6915 * This operation also signals the server that this client is
6916 * performing migration recovery. The server can stop asserting
6917 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6918 * performing this operation is identified in the SEQUENCE
6919 * operation in this compound.
6921 * When the client supports GETATTR(fs_locations_info), it can
6922 * be plumbed in here.
6924 static int _nfs41_proc_get_locations(struct inode *inode,
6925 struct nfs4_fs_locations *locations,
6926 struct page *page, struct rpc_cred *cred)
6928 struct nfs_server *server = NFS_SERVER(inode);
6929 struct rpc_clnt *clnt = server->client;
6931 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6933 struct nfs4_fs_locations_arg args = {
6934 .fh = NFS_FH(inode),
6937 .migration = 1, /* skip LOOKUP */
6939 struct nfs4_fs_locations_res res = {
6940 .fs_locations = locations,
6943 struct rpc_message msg = {
6944 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6951 nfs_fattr_init(&locations->fattr);
6952 locations->server = server;
6953 locations->nlocations = 0;
6955 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6956 nfs4_set_sequence_privileged(&args.seq_args);
6957 status = nfs4_call_sync_sequence(clnt, server, &msg,
6958 &args.seq_args, &res.seq_res);
6959 if (status == NFS4_OK &&
6960 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6961 status = -NFS4ERR_LEASE_MOVED;
6965 #endif /* CONFIG_NFS_V4_1 */
6968 * nfs4_proc_get_locations - discover locations for a migrated FSID
6969 * @inode: inode on FSID that is migrating
6970 * @locations: result of query
6972 * @cred: credential to use for this operation
6974 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6975 * operation failed, or a negative errno if a local error occurred.
6977 * On success, "locations" is filled in, but if the server has
6978 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6981 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6982 * from this client that require migration recovery.
6984 int nfs4_proc_get_locations(struct inode *inode,
6985 struct nfs4_fs_locations *locations,
6986 struct page *page, struct rpc_cred *cred)
6988 struct nfs_server *server = NFS_SERVER(inode);
6989 struct nfs_client *clp = server->nfs_client;
6990 const struct nfs4_mig_recovery_ops *ops =
6991 clp->cl_mvops->mig_recovery_ops;
6992 struct nfs4_exception exception = { };
6995 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6996 (unsigned long long)server->fsid.major,
6997 (unsigned long long)server->fsid.minor,
6999 nfs_display_fhandle(NFS_FH(inode), __func__);
7002 status = ops->get_locations(inode, locations, page, cred);
7003 if (status != -NFS4ERR_DELAY)
7005 nfs4_handle_exception(server, status, &exception);
7006 } while (exception.retry);
7011 * This operation also signals the server that this client is
7012 * performing "lease moved" recovery. The server can stop
7013 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7014 * is appended to this compound to identify the client ID which is
7015 * performing recovery.
7017 static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7019 struct nfs_server *server = NFS_SERVER(inode);
7020 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7021 struct rpc_clnt *clnt = server->client;
7022 struct nfs4_fsid_present_arg args = {
7023 .fh = NFS_FH(inode),
7024 .clientid = clp->cl_clientid,
7025 .renew = 1, /* append RENEW */
7027 struct nfs4_fsid_present_res res = {
7030 struct rpc_message msg = {
7031 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7036 unsigned long now = jiffies;
7039 res.fh = nfs_alloc_fhandle();
7043 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7044 nfs4_set_sequence_privileged(&args.seq_args);
7045 status = nfs4_call_sync_sequence(clnt, server, &msg,
7046 &args.seq_args, &res.seq_res);
7047 nfs_free_fhandle(res.fh);
7051 do_renew_lease(clp, now);
7055 #ifdef CONFIG_NFS_V4_1
7058 * This operation also signals the server that this client is
7059 * performing "lease moved" recovery. The server can stop asserting
7060 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7061 * this operation is identified in the SEQUENCE operation in this
7064 static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7066 struct nfs_server *server = NFS_SERVER(inode);
7067 struct rpc_clnt *clnt = server->client;
7068 struct nfs4_fsid_present_arg args = {
7069 .fh = NFS_FH(inode),
7071 struct nfs4_fsid_present_res res = {
7073 struct rpc_message msg = {
7074 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7081 res.fh = nfs_alloc_fhandle();
7085 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7086 nfs4_set_sequence_privileged(&args.seq_args);
7087 status = nfs4_call_sync_sequence(clnt, server, &msg,
7088 &args.seq_args, &res.seq_res);
7089 nfs_free_fhandle(res.fh);
7090 if (status == NFS4_OK &&
7091 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7092 status = -NFS4ERR_LEASE_MOVED;
7096 #endif /* CONFIG_NFS_V4_1 */
7099 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7100 * @inode: inode on FSID to check
7101 * @cred: credential to use for this operation
7103 * Server indicates whether the FSID is present, moved, or not
7104 * recognized. This operation is necessary to clear a LEASE_MOVED
7105 * condition for this client ID.
7107 * Returns NFS4_OK if the FSID is present on this server,
7108 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7109 * NFS4ERR code if some error occurred on the server, or a
7110 * negative errno if a local failure occurred.
7112 int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7114 struct nfs_server *server = NFS_SERVER(inode);
7115 struct nfs_client *clp = server->nfs_client;
7116 const struct nfs4_mig_recovery_ops *ops =
7117 clp->cl_mvops->mig_recovery_ops;
7118 struct nfs4_exception exception = { };
7121 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7122 (unsigned long long)server->fsid.major,
7123 (unsigned long long)server->fsid.minor,
7125 nfs_display_fhandle(NFS_FH(inode), __func__);
7128 status = ops->fsid_present(inode, cred);
7129 if (status != -NFS4ERR_DELAY)
7131 nfs4_handle_exception(server, status, &exception);
7132 } while (exception.retry);
7137 * If 'use_integrity' is true and the state managment nfs_client
7138 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7139 * and the machine credential as per RFC3530bis and RFC5661 Security
7140 * Considerations sections. Otherwise, just use the user cred with the
7141 * filesystem's rpc_client.
7143 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
7146 struct nfs4_secinfo_arg args = {
7147 .dir_fh = NFS_FH(dir),
7150 struct nfs4_secinfo_res res = {
7153 struct rpc_message msg = {
7154 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7158 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
7159 struct rpc_cred *cred = NULL;
7161 if (use_integrity) {
7162 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
7163 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7164 msg.rpc_cred = cred;
7167 dprintk("NFS call secinfo %s\n", name->name);
7169 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7170 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7172 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7174 dprintk("NFS reply secinfo: %d\n", status);
7182 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7183 struct nfs4_secinfo_flavors *flavors)
7185 struct nfs4_exception exception = { };
7188 err = -NFS4ERR_WRONGSEC;
7190 /* try to use integrity protection with machine cred */
7191 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7192 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7195 * if unable to use integrity protection, or SECINFO with
7196 * integrity protection returns NFS4ERR_WRONGSEC (which is
7197 * disallowed by spec, but exists in deployed servers) use
7198 * the current filesystem's rpc_client and the user cred.
7200 if (err == -NFS4ERR_WRONGSEC)
7201 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7203 trace_nfs4_secinfo(dir, name, err);
7204 err = nfs4_handle_exception(NFS_SERVER(dir), err,
7206 } while (exception.retry);
7210 #ifdef CONFIG_NFS_V4_1
7212 * Check the exchange flags returned by the server for invalid flags, having
7213 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7216 static int nfs4_check_cl_exchange_flags(u32 flags)
7218 if (flags & ~EXCHGID4_FLAG_MASK_R)
7220 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7221 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7223 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7227 return -NFS4ERR_INVAL;
7231 nfs41_same_server_scope(struct nfs41_server_scope *a,
7232 struct nfs41_server_scope *b)
7234 if (a->server_scope_sz == b->server_scope_sz &&
7235 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
7242 nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7246 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7247 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7251 * nfs4_proc_bind_one_conn_to_session()
7253 * The 4.1 client currently uses the same TCP connection for the
7254 * fore and backchannel.
7257 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7258 struct rpc_xprt *xprt,
7259 struct nfs_client *clp,
7260 struct rpc_cred *cred)
7263 struct nfs41_bind_conn_to_session_args args = {
7265 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7267 struct nfs41_bind_conn_to_session_res res;
7268 struct rpc_message msg = {
7270 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
7275 struct rpc_task_setup task_setup_data = {
7278 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
7279 .rpc_message = &msg,
7280 .flags = RPC_TASK_TIMEOUT,
7282 struct rpc_task *task;
7284 dprintk("--> %s\n", __func__);
7286 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7287 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7288 args.dir = NFS4_CDFC4_FORE;
7290 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7291 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7292 args.dir = NFS4_CDFC4_FORE;
7294 task = rpc_run_task(&task_setup_data);
7295 if (!IS_ERR(task)) {
7296 status = task->tk_status;
7299 status = PTR_ERR(task);
7300 trace_nfs4_bind_conn_to_session(clp, status);
7302 if (memcmp(res.sessionid.data,
7303 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7304 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7308 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
7309 dprintk("NFS: %s: Unexpected direction from server\n",
7314 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
7315 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7322 dprintk("<-- %s status= %d\n", __func__, status);
7326 struct rpc_bind_conn_calldata {
7327 struct nfs_client *clp;
7328 struct rpc_cred *cred;
7332 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7333 struct rpc_xprt *xprt,
7336 struct rpc_bind_conn_calldata *p = calldata;
7338 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7341 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7343 struct rpc_bind_conn_calldata data = {
7347 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7348 nfs4_proc_bind_conn_to_session_callback, &data);
7352 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7353 * and operations we'd like to see to enable certain features in the allow map
7355 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7356 .how = SP4_MACH_CRED,
7357 .enforce.u.words = {
7358 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7359 1 << (OP_EXCHANGE_ID - 32) |
7360 1 << (OP_CREATE_SESSION - 32) |
7361 1 << (OP_DESTROY_SESSION - 32) |
7362 1 << (OP_DESTROY_CLIENTID - 32)
7365 [0] = 1 << (OP_CLOSE) |
7366 1 << (OP_OPEN_DOWNGRADE) |
7368 1 << (OP_DELEGRETURN) |
7370 [1] = 1 << (OP_SECINFO - 32) |
7371 1 << (OP_SECINFO_NO_NAME - 32) |
7372 1 << (OP_LAYOUTRETURN - 32) |
7373 1 << (OP_TEST_STATEID - 32) |
7374 1 << (OP_FREE_STATEID - 32) |
7375 1 << (OP_WRITE - 32)
7380 * Select the state protection mode for client `clp' given the server results
7381 * from exchange_id in `sp'.
7383 * Returns 0 on success, negative errno otherwise.
7385 static int nfs4_sp4_select_mode(struct nfs_client *clp,
7386 struct nfs41_state_protection *sp)
7388 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7389 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7390 1 << (OP_EXCHANGE_ID - 32) |
7391 1 << (OP_CREATE_SESSION - 32) |
7392 1 << (OP_DESTROY_SESSION - 32) |
7393 1 << (OP_DESTROY_CLIENTID - 32)
7397 if (sp->how == SP4_MACH_CRED) {
7398 /* Print state protect result */
7399 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7400 for (i = 0; i <= LAST_NFS4_OP; i++) {
7401 if (test_bit(i, sp->enforce.u.longs))
7402 dfprintk(MOUNT, " enforce op %d\n", i);
7403 if (test_bit(i, sp->allow.u.longs))
7404 dfprintk(MOUNT, " allow op %d\n", i);
7407 /* make sure nothing is on enforce list that isn't supported */
7408 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7409 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7410 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7416 * Minimal mode - state operations are allowed to use machine
7417 * credential. Note this already happens by default, so the
7418 * client doesn't have to do anything more than the negotiation.
7420 * NOTE: we don't care if EXCHANGE_ID is in the list -
7421 * we're already using the machine cred for exchange_id
7422 * and will never use a different cred.
7424 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7425 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7426 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7427 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7428 dfprintk(MOUNT, "sp4_mach_cred:\n");
7429 dfprintk(MOUNT, " minimal mode enabled\n");
7430 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
7432 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7436 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
7437 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7438 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
7439 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7440 dfprintk(MOUNT, " cleanup mode enabled\n");
7441 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
7444 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7445 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7446 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
7447 &clp->cl_sp4_flags);
7450 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7451 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7452 dfprintk(MOUNT, " secinfo mode enabled\n");
7453 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
7456 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7457 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7458 dfprintk(MOUNT, " stateid mode enabled\n");
7459 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
7462 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7463 dfprintk(MOUNT, " write mode enabled\n");
7464 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
7467 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7468 dfprintk(MOUNT, " commit mode enabled\n");
7469 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
7476 struct nfs41_exchange_id_data {
7477 struct nfs41_exchange_id_res res;
7478 struct nfs41_exchange_id_args args;
7479 struct rpc_xprt *xprt;
7483 static void nfs4_exchange_id_done(struct rpc_task *task, void *data)
7485 struct nfs41_exchange_id_data *cdata =
7486 (struct nfs41_exchange_id_data *)data;
7487 struct nfs_client *clp = cdata->args.client;
7488 int status = task->tk_status;
7490 trace_nfs4_exchange_id(clp, status);
7493 status = nfs4_check_cl_exchange_flags(cdata->res.flags);
7495 if (cdata->xprt && status == 0) {
7496 status = nfs4_detect_session_trunking(clp, &cdata->res,
7502 status = nfs4_sp4_select_mode(clp, &cdata->res.state_protect);
7505 clp->cl_clientid = cdata->res.clientid;
7506 clp->cl_exchange_flags = cdata->res.flags;
7507 /* Client ID is not confirmed */
7508 if (!(cdata->res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
7509 clear_bit(NFS4_SESSION_ESTABLISHED,
7510 &clp->cl_session->session_state);
7511 clp->cl_seqid = cdata->res.seqid;
7514 kfree(clp->cl_serverowner);
7515 clp->cl_serverowner = cdata->res.server_owner;
7516 cdata->res.server_owner = NULL;
7518 /* use the most recent implementation id */
7519 kfree(clp->cl_implid);
7520 clp->cl_implid = cdata->res.impl_id;
7521 cdata->res.impl_id = NULL;
7523 if (clp->cl_serverscope != NULL &&
7524 !nfs41_same_server_scope(clp->cl_serverscope,
7525 cdata->res.server_scope)) {
7526 dprintk("%s: server_scope mismatch detected\n",
7528 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
7529 kfree(clp->cl_serverscope);
7530 clp->cl_serverscope = NULL;
7533 if (clp->cl_serverscope == NULL) {
7534 clp->cl_serverscope = cdata->res.server_scope;
7535 cdata->res.server_scope = NULL;
7537 /* Save the EXCHANGE_ID verifier session trunk tests */
7538 memcpy(clp->cl_confirm.data, cdata->args.verifier->data,
7539 sizeof(clp->cl_confirm.data));
7542 cdata->rpc_status = status;
7546 static void nfs4_exchange_id_release(void *data)
7548 struct nfs41_exchange_id_data *cdata =
7549 (struct nfs41_exchange_id_data *)data;
7551 nfs_put_client(cdata->args.client);
7553 xprt_put(cdata->xprt);
7554 rpc_clnt_xprt_switch_put(cdata->args.client->cl_rpcclient);
7556 kfree(cdata->res.impl_id);
7557 kfree(cdata->res.server_scope);
7558 kfree(cdata->res.server_owner);
7562 static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7563 .rpc_call_done = nfs4_exchange_id_done,
7564 .rpc_release = nfs4_exchange_id_release,
7568 * _nfs4_proc_exchange_id()
7570 * Wrapper for EXCHANGE_ID operation.
7572 static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
7573 u32 sp4_how, struct rpc_xprt *xprt)
7575 nfs4_verifier verifier;
7576 struct rpc_message msg = {
7577 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
7580 struct rpc_task_setup task_setup_data = {
7581 .rpc_client = clp->cl_rpcclient,
7582 .callback_ops = &nfs4_exchange_id_call_ops,
7583 .rpc_message = &msg,
7584 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7586 struct nfs41_exchange_id_data *calldata;
7587 struct rpc_task *task;
7590 if (!atomic_inc_not_zero(&clp->cl_count))
7594 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7599 nfs4_init_boot_verifier(clp, &verifier);
7601 status = nfs4_init_uniform_client_string(clp);
7605 dprintk("NFS call exchange_id auth=%s, '%s'\n",
7606 clp->cl_rpcclient->cl_auth->au_ops->au_name,
7609 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7612 if (unlikely(calldata->res.server_owner == NULL))
7615 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
7617 if (unlikely(calldata->res.server_scope == NULL))
7618 goto out_server_owner;
7620 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7621 if (unlikely(calldata->res.impl_id == NULL))
7622 goto out_server_scope;
7626 calldata->args.state_protect.how = SP4_NONE;
7630 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
7640 calldata->xprt = xprt;
7641 task_setup_data.rpc_xprt = xprt;
7642 task_setup_data.flags =
7643 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC;
7644 calldata->args.verifier = &clp->cl_confirm;
7646 calldata->args.verifier = &verifier;
7648 calldata->args.client = clp;
7649 #ifdef CONFIG_NFS_V4_1_MIGRATION
7650 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7651 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7652 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7654 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7655 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7657 msg.rpc_argp = &calldata->args;
7658 msg.rpc_resp = &calldata->res;
7659 task_setup_data.callback_data = calldata;
7661 task = rpc_run_task(&task_setup_data);
7663 status = PTR_ERR(task);
7668 status = rpc_wait_for_completion_task(task);
7670 status = calldata->rpc_status;
7671 } else /* session trunking test */
7672 status = calldata->rpc_status;
7676 if (clp->cl_implid != NULL)
7677 dprintk("NFS reply exchange_id: Server Implementation ID: "
7678 "domain: %s, name: %s, date: %llu,%u\n",
7679 clp->cl_implid->domain, clp->cl_implid->name,
7680 clp->cl_implid->date.seconds,
7681 clp->cl_implid->date.nseconds);
7682 dprintk("NFS reply exchange_id: %d\n", status);
7686 kfree(calldata->res.impl_id);
7688 kfree(calldata->res.server_scope);
7690 kfree(calldata->res.server_owner);
7697 * nfs4_proc_exchange_id()
7699 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7701 * Since the clientid has expired, all compounds using sessions
7702 * associated with the stale clientid will be returning
7703 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7704 * be in some phase of session reset.
7706 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7708 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7710 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7713 /* try SP4_MACH_CRED if krb5i/p */
7714 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7715 authflavor == RPC_AUTH_GSS_KRB5P) {
7716 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED, NULL);
7722 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE, NULL);
7726 * nfs4_test_session_trunk
7728 * This is an add_xprt_test() test function called from
7729 * rpc_clnt_setup_test_and_add_xprt.
7731 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7732 * and is dereferrenced in nfs4_exchange_id_release
7734 * Upon success, add the new transport to the rpc_clnt
7736 * @clnt: struct rpc_clnt to get new transport
7737 * @xprt: the rpc_xprt to test
7738 * @data: call data for _nfs4_proc_exchange_id.
7740 int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7743 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
7746 dprintk("--> %s try %s\n", __func__,
7747 xprt->address_strings[RPC_DISPLAY_ADDR]);
7749 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7751 /* Test connection for session trunking. Async exchange_id call */
7752 return _nfs4_proc_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7754 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7756 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7757 struct rpc_cred *cred)
7759 struct rpc_message msg = {
7760 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7766 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7767 trace_nfs4_destroy_clientid(clp, status);
7769 dprintk("NFS: Got error %d from the server %s on "
7770 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7774 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7775 struct rpc_cred *cred)
7780 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7781 ret = _nfs4_proc_destroy_clientid(clp, cred);
7783 case -NFS4ERR_DELAY:
7784 case -NFS4ERR_CLIENTID_BUSY:
7794 int nfs4_destroy_clientid(struct nfs_client *clp)
7796 struct rpc_cred *cred;
7799 if (clp->cl_mvops->minor_version < 1)
7801 if (clp->cl_exchange_flags == 0)
7803 if (clp->cl_preserve_clid)
7805 cred = nfs4_get_clid_cred(clp);
7806 ret = nfs4_proc_destroy_clientid(clp, cred);
7811 case -NFS4ERR_STALE_CLIENTID:
7812 clp->cl_exchange_flags = 0;
7818 struct nfs4_get_lease_time_data {
7819 struct nfs4_get_lease_time_args *args;
7820 struct nfs4_get_lease_time_res *res;
7821 struct nfs_client *clp;
7824 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7827 struct nfs4_get_lease_time_data *data =
7828 (struct nfs4_get_lease_time_data *)calldata;
7830 dprintk("--> %s\n", __func__);
7831 /* just setup sequence, do not trigger session recovery
7832 since we're invoked within one */
7833 nfs41_setup_sequence(data->clp->cl_session,
7834 &data->args->la_seq_args,
7835 &data->res->lr_seq_res,
7837 dprintk("<-- %s\n", __func__);
7841 * Called from nfs4_state_manager thread for session setup, so don't recover
7842 * from sequence operation or clientid errors.
7844 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7846 struct nfs4_get_lease_time_data *data =
7847 (struct nfs4_get_lease_time_data *)calldata;
7849 dprintk("--> %s\n", __func__);
7850 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7852 switch (task->tk_status) {
7853 case -NFS4ERR_DELAY:
7854 case -NFS4ERR_GRACE:
7855 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7856 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7857 task->tk_status = 0;
7859 case -NFS4ERR_RETRY_UNCACHED_REP:
7860 rpc_restart_call_prepare(task);
7863 dprintk("<-- %s\n", __func__);
7866 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
7867 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7868 .rpc_call_done = nfs4_get_lease_time_done,
7871 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7873 struct rpc_task *task;
7874 struct nfs4_get_lease_time_args args;
7875 struct nfs4_get_lease_time_res res = {
7876 .lr_fsinfo = fsinfo,
7878 struct nfs4_get_lease_time_data data = {
7883 struct rpc_message msg = {
7884 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7888 struct rpc_task_setup task_setup = {
7889 .rpc_client = clp->cl_rpcclient,
7890 .rpc_message = &msg,
7891 .callback_ops = &nfs4_get_lease_time_ops,
7892 .callback_data = &data,
7893 .flags = RPC_TASK_TIMEOUT,
7897 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
7898 nfs4_set_sequence_privileged(&args.la_seq_args);
7899 dprintk("--> %s\n", __func__);
7900 task = rpc_run_task(&task_setup);
7903 status = PTR_ERR(task);
7905 status = task->tk_status;
7908 dprintk("<-- %s return %d\n", __func__, status);
7914 * Initialize the values to be used by the client in CREATE_SESSION
7915 * If nfs4_init_session set the fore channel request and response sizes,
7918 * Set the back channel max_resp_sz_cached to zero to force the client to
7919 * always set csa_cachethis to FALSE because the current implementation
7920 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7922 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7923 struct rpc_clnt *clnt)
7925 unsigned int max_rqst_sz, max_resp_sz;
7926 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
7928 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7929 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7931 /* Fore channel attributes */
7932 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7933 args->fc_attrs.max_resp_sz = max_resp_sz;
7934 args->fc_attrs.max_ops = NFS4_MAX_OPS;
7935 args->fc_attrs.max_reqs = max_session_slots;
7937 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
7938 "max_ops=%u max_reqs=%u\n",
7940 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
7941 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
7943 /* Back channel attributes */
7944 args->bc_attrs.max_rqst_sz = max_bc_payload;
7945 args->bc_attrs.max_resp_sz = max_bc_payload;
7946 args->bc_attrs.max_resp_sz_cached = 0;
7947 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
7948 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
7950 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7951 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7953 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7954 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7955 args->bc_attrs.max_reqs);
7958 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7959 struct nfs41_create_session_res *res)
7961 struct nfs4_channel_attrs *sent = &args->fc_attrs;
7962 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
7964 if (rcvd->max_resp_sz > sent->max_resp_sz)
7967 * Our requested max_ops is the minimum we need; we're not
7968 * prepared to break up compounds into smaller pieces than that.
7969 * So, no point even trying to continue if the server won't
7972 if (rcvd->max_ops < sent->max_ops)
7974 if (rcvd->max_reqs == 0)
7976 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7977 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
7981 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7982 struct nfs41_create_session_res *res)
7984 struct nfs4_channel_attrs *sent = &args->bc_attrs;
7985 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
7987 if (!(res->flags & SESSION4_BACK_CHAN))
7989 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7991 if (rcvd->max_resp_sz < sent->max_resp_sz)
7993 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7995 if (rcvd->max_ops > sent->max_ops)
7997 if (rcvd->max_reqs > sent->max_reqs)
8003 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
8004 struct nfs41_create_session_res *res)
8008 ret = nfs4_verify_fore_channel_attrs(args, res);
8011 return nfs4_verify_back_channel_attrs(args, res);
8014 static void nfs4_update_session(struct nfs4_session *session,
8015 struct nfs41_create_session_res *res)
8017 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
8018 /* Mark client id and session as being confirmed */
8019 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8020 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
8021 session->flags = res->flags;
8022 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
8023 if (res->flags & SESSION4_BACK_CHAN)
8024 memcpy(&session->bc_attrs, &res->bc_attrs,
8025 sizeof(session->bc_attrs));
8028 static int _nfs4_proc_create_session(struct nfs_client *clp,
8029 struct rpc_cred *cred)
8031 struct nfs4_session *session = clp->cl_session;
8032 struct nfs41_create_session_args args = {
8034 .clientid = clp->cl_clientid,
8035 .seqid = clp->cl_seqid,
8036 .cb_program = NFS4_CALLBACK,
8038 struct nfs41_create_session_res res;
8040 struct rpc_message msg = {
8041 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8048 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
8049 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
8051 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
8052 trace_nfs4_create_session(clp, status);
8055 case -NFS4ERR_STALE_CLIENTID:
8056 case -NFS4ERR_DELAY:
8065 /* Verify the session's negotiated channel_attrs values */
8066 status = nfs4_verify_channel_attrs(&args, &res);
8067 /* Increment the clientid slot sequence id */
8070 nfs4_update_session(session, &res);
8077 * Issues a CREATE_SESSION operation to the server.
8078 * It is the responsibility of the caller to verify the session is
8079 * expired before calling this routine.
8081 int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
8085 struct nfs4_session *session = clp->cl_session;
8087 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8089 status = _nfs4_proc_create_session(clp, cred);
8093 /* Init or reset the session slot tables */
8094 status = nfs4_setup_session_slot_tables(session);
8095 dprintk("slot table setup returned %d\n", status);
8099 ptr = (unsigned *)&session->sess_id.data[0];
8100 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8101 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
8103 dprintk("<-- %s\n", __func__);
8108 * Issue the over-the-wire RPC DESTROY_SESSION.
8109 * The caller must serialize access to this routine.
8111 int nfs4_proc_destroy_session(struct nfs4_session *session,
8112 struct rpc_cred *cred)
8114 struct rpc_message msg = {
8115 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8116 .rpc_argp = session,
8121 dprintk("--> nfs4_proc_destroy_session\n");
8123 /* session is still being setup */
8124 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8127 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
8128 trace_nfs4_destroy_session(session->clp, status);
8131 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
8132 "Session has been destroyed regardless...\n", status);
8134 dprintk("<-- nfs4_proc_destroy_session\n");
8139 * Renew the cl_session lease.
8141 struct nfs4_sequence_data {
8142 struct nfs_client *clp;
8143 struct nfs4_sequence_args args;
8144 struct nfs4_sequence_res res;
8147 static void nfs41_sequence_release(void *data)
8149 struct nfs4_sequence_data *calldata = data;
8150 struct nfs_client *clp = calldata->clp;
8152 if (atomic_read(&clp->cl_count) > 1)
8153 nfs4_schedule_state_renewal(clp);
8154 nfs_put_client(clp);
8158 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8160 switch(task->tk_status) {
8161 case -NFS4ERR_DELAY:
8162 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8165 nfs4_schedule_lease_recovery(clp);
8170 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
8172 struct nfs4_sequence_data *calldata = data;
8173 struct nfs_client *clp = calldata->clp;
8175 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8178 trace_nfs4_sequence(clp, task->tk_status);
8179 if (task->tk_status < 0) {
8180 dprintk("%s ERROR %d\n", __func__, task->tk_status);
8181 if (atomic_read(&clp->cl_count) == 1)
8184 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8185 rpc_restart_call_prepare(task);
8189 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
8191 dprintk("<-- %s\n", __func__);
8194 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8196 struct nfs4_sequence_data *calldata = data;
8197 struct nfs_client *clp = calldata->clp;
8198 struct nfs4_sequence_args *args;
8199 struct nfs4_sequence_res *res;
8201 args = task->tk_msg.rpc_argp;
8202 res = task->tk_msg.rpc_resp;
8204 nfs41_setup_sequence(clp->cl_session, args, res, task);
8207 static const struct rpc_call_ops nfs41_sequence_ops = {
8208 .rpc_call_done = nfs41_sequence_call_done,
8209 .rpc_call_prepare = nfs41_sequence_prepare,
8210 .rpc_release = nfs41_sequence_release,
8213 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8214 struct rpc_cred *cred,
8217 struct nfs4_sequence_data *calldata;
8218 struct rpc_message msg = {
8219 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8222 struct rpc_task_setup task_setup_data = {
8223 .rpc_client = clp->cl_rpcclient,
8224 .rpc_message = &msg,
8225 .callback_ops = &nfs41_sequence_ops,
8226 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
8229 if (!atomic_inc_not_zero(&clp->cl_count))
8230 return ERR_PTR(-EIO);
8231 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8232 if (calldata == NULL) {
8233 nfs_put_client(clp);
8234 return ERR_PTR(-ENOMEM);
8236 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
8238 nfs4_set_sequence_privileged(&calldata->args);
8239 msg.rpc_argp = &calldata->args;
8240 msg.rpc_resp = &calldata->res;
8241 calldata->clp = clp;
8242 task_setup_data.callback_data = calldata;
8244 return rpc_run_task(&task_setup_data);
8247 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
8249 struct rpc_task *task;
8252 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
8254 task = _nfs41_proc_sequence(clp, cred, false);
8256 ret = PTR_ERR(task);
8258 rpc_put_task_async(task);
8259 dprintk("<-- %s status=%d\n", __func__, ret);
8263 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8265 struct rpc_task *task;
8268 task = _nfs41_proc_sequence(clp, cred, true);
8270 ret = PTR_ERR(task);
8273 ret = rpc_wait_for_completion_task(task);
8275 ret = task->tk_status;
8278 dprintk("<-- %s status=%d\n", __func__, ret);
8282 struct nfs4_reclaim_complete_data {
8283 struct nfs_client *clp;
8284 struct nfs41_reclaim_complete_args arg;
8285 struct nfs41_reclaim_complete_res res;
8288 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8290 struct nfs4_reclaim_complete_data *calldata = data;
8292 nfs41_setup_sequence(calldata->clp->cl_session,
8293 &calldata->arg.seq_args,
8294 &calldata->res.seq_res,
8298 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8300 switch(task->tk_status) {
8302 case -NFS4ERR_COMPLETE_ALREADY:
8303 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8305 case -NFS4ERR_DELAY:
8306 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8308 case -NFS4ERR_RETRY_UNCACHED_REP:
8311 nfs4_schedule_lease_recovery(clp);
8316 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8318 struct nfs4_reclaim_complete_data *calldata = data;
8319 struct nfs_client *clp = calldata->clp;
8320 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8322 dprintk("--> %s\n", __func__);
8323 if (!nfs41_sequence_done(task, res))
8326 trace_nfs4_reclaim_complete(clp, task->tk_status);
8327 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8328 rpc_restart_call_prepare(task);
8331 dprintk("<-- %s\n", __func__);
8334 static void nfs4_free_reclaim_complete_data(void *data)
8336 struct nfs4_reclaim_complete_data *calldata = data;
8341 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8342 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8343 .rpc_call_done = nfs4_reclaim_complete_done,
8344 .rpc_release = nfs4_free_reclaim_complete_data,
8348 * Issue a global reclaim complete.
8350 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8351 struct rpc_cred *cred)
8353 struct nfs4_reclaim_complete_data *calldata;
8354 struct rpc_task *task;
8355 struct rpc_message msg = {
8356 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
8359 struct rpc_task_setup task_setup_data = {
8360 .rpc_client = clp->cl_rpcclient,
8361 .rpc_message = &msg,
8362 .callback_ops = &nfs4_reclaim_complete_call_ops,
8363 .flags = RPC_TASK_ASYNC,
8365 int status = -ENOMEM;
8367 dprintk("--> %s\n", __func__);
8368 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8369 if (calldata == NULL)
8371 calldata->clp = clp;
8372 calldata->arg.one_fs = 0;
8374 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
8375 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
8376 msg.rpc_argp = &calldata->arg;
8377 msg.rpc_resp = &calldata->res;
8378 task_setup_data.callback_data = calldata;
8379 task = rpc_run_task(&task_setup_data);
8381 status = PTR_ERR(task);
8384 status = nfs4_wait_for_completion_rpc_task(task);
8386 status = task->tk_status;
8390 dprintk("<-- %s status=%d\n", __func__, status);
8395 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8397 struct nfs4_layoutget *lgp = calldata;
8398 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
8399 struct nfs4_session *session = nfs4_get_session(server);
8401 dprintk("--> %s\n", __func__);
8402 nfs41_setup_sequence(session, &lgp->args.seq_args,
8403 &lgp->res.seq_res, task);
8404 dprintk("<-- %s\n", __func__);
8407 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8409 struct nfs4_layoutget *lgp = calldata;
8411 dprintk("--> %s\n", __func__);
8412 nfs41_sequence_process(task, &lgp->res.seq_res);
8413 dprintk("<-- %s\n", __func__);
8417 nfs4_layoutget_handle_exception(struct rpc_task *task,
8418 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8420 struct inode *inode = lgp->args.inode;
8421 struct nfs_server *server = NFS_SERVER(inode);
8422 struct pnfs_layout_hdr *lo;
8423 int nfs4err = task->tk_status;
8424 int err, status = 0;
8427 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
8434 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8435 * on the file. set tk_status to -ENODATA to tell upper layer to
8438 case -NFS4ERR_LAYOUTUNAVAILABLE:
8442 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8443 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8445 case -NFS4ERR_BADLAYOUT:
8446 status = -EOVERFLOW;
8449 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
8450 * (or clients) writing to the same RAID stripe except when
8451 * the minlength argument is 0 (see RFC5661 section 18.43.3).
8453 * Treat it like we would RECALLCONFLICT -- we retry for a little
8454 * while, and then eventually give up.
8456 case -NFS4ERR_LAYOUTTRYLATER:
8457 if (lgp->args.minlength == 0) {
8458 status = -EOVERFLOW;
8463 case -NFS4ERR_RECALLCONFLICT:
8464 status = -ERECALLCONFLICT;
8466 case -NFS4ERR_DELEG_REVOKED:
8467 case -NFS4ERR_ADMIN_REVOKED:
8468 case -NFS4ERR_EXPIRED:
8469 case -NFS4ERR_BAD_STATEID:
8470 exception->timeout = 0;
8471 spin_lock(&inode->i_lock);
8472 lo = NFS_I(inode)->layout;
8473 /* If the open stateid was bad, then recover it. */
8474 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8475 nfs4_stateid_match_other(&lgp->args.stateid,
8476 &lgp->args.ctx->state->stateid)) {
8477 spin_unlock(&inode->i_lock);
8478 exception->state = lgp->args.ctx->state;
8479 exception->stateid = &lgp->args.stateid;
8484 * Mark the bad layout state as invalid, then retry
8486 pnfs_mark_layout_stateid_invalid(lo, &head);
8487 spin_unlock(&inode->i_lock);
8488 pnfs_free_lseg_list(&head);
8493 err = nfs4_handle_exception(server, nfs4err, exception);
8495 if (exception->retry)
8501 dprintk("<-- %s\n", __func__);
8505 static size_t max_response_pages(struct nfs_server *server)
8507 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8508 return nfs_page_array_len(0, max_resp_sz);
8511 static void nfs4_free_pages(struct page **pages, size_t size)
8518 for (i = 0; i < size; i++) {
8521 __free_page(pages[i]);
8526 static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8528 struct page **pages;
8531 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8533 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8537 for (i = 0; i < size; i++) {
8538 pages[i] = alloc_page(gfp_flags);
8540 dprintk("%s: failed to allocate page\n", __func__);
8541 nfs4_free_pages(pages, size);
8549 static void nfs4_layoutget_release(void *calldata)
8551 struct nfs4_layoutget *lgp = calldata;
8552 struct inode *inode = lgp->args.inode;
8553 struct nfs_server *server = NFS_SERVER(inode);
8554 size_t max_pages = max_response_pages(server);
8556 dprintk("--> %s\n", __func__);
8557 nfs4_free_pages(lgp->args.layout.pages, max_pages);
8558 pnfs_put_layout_hdr(NFS_I(inode)->layout);
8559 put_nfs_open_context(lgp->args.ctx);
8561 dprintk("<-- %s\n", __func__);
8564 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8565 .rpc_call_prepare = nfs4_layoutget_prepare,
8566 .rpc_call_done = nfs4_layoutget_done,
8567 .rpc_release = nfs4_layoutget_release,
8570 struct pnfs_layout_segment *
8571 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
8573 struct inode *inode = lgp->args.inode;
8574 struct nfs_server *server = NFS_SERVER(inode);
8575 size_t max_pages = max_response_pages(server);
8576 struct rpc_task *task;
8577 struct rpc_message msg = {
8578 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8579 .rpc_argp = &lgp->args,
8580 .rpc_resp = &lgp->res,
8581 .rpc_cred = lgp->cred,
8583 struct rpc_task_setup task_setup_data = {
8584 .rpc_client = server->client,
8585 .rpc_message = &msg,
8586 .callback_ops = &nfs4_layoutget_call_ops,
8587 .callback_data = lgp,
8588 .flags = RPC_TASK_ASYNC,
8590 struct pnfs_layout_segment *lseg = NULL;
8591 struct nfs4_exception exception = {
8593 .timeout = *timeout,
8597 dprintk("--> %s\n", __func__);
8599 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8600 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8602 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8603 if (!lgp->args.layout.pages) {
8604 nfs4_layoutget_release(lgp);
8605 return ERR_PTR(-ENOMEM);
8607 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8609 lgp->res.layoutp = &lgp->args.layout;
8610 lgp->res.seq_res.sr_slot = NULL;
8611 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
8613 task = rpc_run_task(&task_setup_data);
8615 return ERR_CAST(task);
8616 status = nfs4_wait_for_completion_rpc_task(task);
8618 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8619 *timeout = exception.timeout;
8622 trace_nfs4_layoutget(lgp->args.ctx,
8628 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8629 if (status == 0 && lgp->res.layoutp->len)
8630 lseg = pnfs_layout_process(lgp);
8631 nfs4_sequence_free_slot(&lgp->res.seq_res);
8633 dprintk("<-- %s status=%d\n", __func__, status);
8635 return ERR_PTR(status);
8640 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8642 struct nfs4_layoutreturn *lrp = calldata;
8644 dprintk("--> %s\n", __func__);
8645 nfs41_setup_sequence(lrp->clp->cl_session,
8646 &lrp->args.seq_args,
8651 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8653 struct nfs4_layoutreturn *lrp = calldata;
8654 struct nfs_server *server;
8656 dprintk("--> %s\n", __func__);
8658 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
8661 server = NFS_SERVER(lrp->args.inode);
8662 switch (task->tk_status) {
8664 task->tk_status = 0;
8667 case -NFS4ERR_DELAY:
8668 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
8670 nfs4_sequence_free_slot(&lrp->res.seq_res);
8671 rpc_restart_call_prepare(task);
8674 dprintk("<-- %s\n", __func__);
8677 static void nfs4_layoutreturn_release(void *calldata)
8679 struct nfs4_layoutreturn *lrp = calldata;
8680 struct pnfs_layout_hdr *lo = lrp->args.layout;
8682 dprintk("--> %s\n", __func__);
8683 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
8684 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
8685 nfs4_sequence_free_slot(&lrp->res.seq_res);
8686 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
8687 lrp->ld_private.ops->free(&lrp->ld_private);
8688 pnfs_put_layout_hdr(lrp->args.layout);
8689 nfs_iput_and_deactive(lrp->inode);
8691 dprintk("<-- %s\n", __func__);
8694 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8695 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8696 .rpc_call_done = nfs4_layoutreturn_done,
8697 .rpc_release = nfs4_layoutreturn_release,
8700 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
8702 struct rpc_task *task;
8703 struct rpc_message msg = {
8704 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8705 .rpc_argp = &lrp->args,
8706 .rpc_resp = &lrp->res,
8707 .rpc_cred = lrp->cred,
8709 struct rpc_task_setup task_setup_data = {
8710 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
8711 .rpc_message = &msg,
8712 .callback_ops = &nfs4_layoutreturn_call_ops,
8713 .callback_data = lrp,
8717 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8718 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8719 &task_setup_data.rpc_client, &msg);
8721 dprintk("--> %s\n", __func__);
8723 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8725 nfs4_layoutreturn_release(lrp);
8728 task_setup_data.flags |= RPC_TASK_ASYNC;
8730 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
8731 task = rpc_run_task(&task_setup_data);
8733 return PTR_ERR(task);
8735 status = task->tk_status;
8736 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
8737 dprintk("<-- %s status=%d\n", __func__, status);
8743 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
8744 struct pnfs_device *pdev,
8745 struct rpc_cred *cred)
8747 struct nfs4_getdeviceinfo_args args = {
8749 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8750 NOTIFY_DEVICEID4_DELETE,
8752 struct nfs4_getdeviceinfo_res res = {
8755 struct rpc_message msg = {
8756 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8763 dprintk("--> %s\n", __func__);
8764 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
8765 if (res.notification & ~args.notify_types)
8766 dprintk("%s: unsupported notification\n", __func__);
8767 if (res.notification != args.notify_types)
8770 dprintk("<-- %s status=%d\n", __func__, status);
8775 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8776 struct pnfs_device *pdev,
8777 struct rpc_cred *cred)
8779 struct nfs4_exception exception = { };
8783 err = nfs4_handle_exception(server,
8784 _nfs4_proc_getdeviceinfo(server, pdev, cred),
8786 } while (exception.retry);
8789 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8791 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8793 struct nfs4_layoutcommit_data *data = calldata;
8794 struct nfs_server *server = NFS_SERVER(data->args.inode);
8795 struct nfs4_session *session = nfs4_get_session(server);
8797 nfs41_setup_sequence(session,
8798 &data->args.seq_args,
8804 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8806 struct nfs4_layoutcommit_data *data = calldata;
8807 struct nfs_server *server = NFS_SERVER(data->args.inode);
8809 if (!nfs41_sequence_done(task, &data->res.seq_res))
8812 switch (task->tk_status) { /* Just ignore these failures */
8813 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8814 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8815 case -NFS4ERR_BADLAYOUT: /* no layout */
8816 case -NFS4ERR_GRACE: /* loca_recalim always false */
8817 task->tk_status = 0;
8821 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
8822 rpc_restart_call_prepare(task);
8828 static void nfs4_layoutcommit_release(void *calldata)
8830 struct nfs4_layoutcommit_data *data = calldata;
8832 pnfs_cleanup_layoutcommit(data);
8833 nfs_post_op_update_inode_force_wcc(data->args.inode,
8835 put_rpccred(data->cred);
8836 nfs_iput_and_deactive(data->inode);
8840 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8841 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8842 .rpc_call_done = nfs4_layoutcommit_done,
8843 .rpc_release = nfs4_layoutcommit_release,
8847 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
8849 struct rpc_message msg = {
8850 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8851 .rpc_argp = &data->args,
8852 .rpc_resp = &data->res,
8853 .rpc_cred = data->cred,
8855 struct rpc_task_setup task_setup_data = {
8856 .task = &data->task,
8857 .rpc_client = NFS_CLIENT(data->args.inode),
8858 .rpc_message = &msg,
8859 .callback_ops = &nfs4_layoutcommit_ops,
8860 .callback_data = data,
8862 struct rpc_task *task;
8865 dprintk("NFS: initiating layoutcommit call. sync %d "
8866 "lbw: %llu inode %lu\n", sync,
8867 data->args.lastbytewritten,
8868 data->args.inode->i_ino);
8871 data->inode = nfs_igrab_and_active(data->args.inode);
8872 if (data->inode == NULL) {
8873 nfs4_layoutcommit_release(data);
8876 task_setup_data.flags = RPC_TASK_ASYNC;
8878 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
8879 task = rpc_run_task(&task_setup_data);
8881 return PTR_ERR(task);
8883 status = task->tk_status;
8884 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
8885 dprintk("%s: status %d\n", __func__, status);
8891 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8892 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8895 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8896 struct nfs_fsinfo *info,
8897 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
8899 struct nfs41_secinfo_no_name_args args = {
8900 .style = SECINFO_STYLE_CURRENT_FH,
8902 struct nfs4_secinfo_res res = {
8905 struct rpc_message msg = {
8906 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8910 struct rpc_clnt *clnt = server->client;
8911 struct rpc_cred *cred = NULL;
8914 if (use_integrity) {
8915 clnt = server->nfs_client->cl_rpcclient;
8916 cred = nfs4_get_clid_cred(server->nfs_client);
8917 msg.rpc_cred = cred;
8920 dprintk("--> %s\n", __func__);
8921 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8923 dprintk("<-- %s status=%d\n", __func__, status);
8932 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8933 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8935 struct nfs4_exception exception = { };
8938 /* first try using integrity protection */
8939 err = -NFS4ERR_WRONGSEC;
8941 /* try to use integrity protection with machine cred */
8942 if (_nfs4_is_integrity_protected(server->nfs_client))
8943 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8947 * if unable to use integrity protection, or SECINFO with
8948 * integrity protection returns NFS4ERR_WRONGSEC (which is
8949 * disallowed by spec, but exists in deployed servers) use
8950 * the current filesystem's rpc_client and the user cred.
8952 if (err == -NFS4ERR_WRONGSEC)
8953 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8958 case -NFS4ERR_WRONGSEC:
8962 err = nfs4_handle_exception(server, err, &exception);
8964 } while (exception.retry);
8970 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8971 struct nfs_fsinfo *info)
8975 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
8976 struct nfs4_secinfo_flavors *flavors;
8977 struct nfs4_secinfo4 *secinfo;
8980 page = alloc_page(GFP_KERNEL);
8986 flavors = page_address(page);
8987 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8990 * Fall back on "guess and check" method if
8991 * the server doesn't support SECINFO_NO_NAME
8993 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
8994 err = nfs4_find_root_sec(server, fhandle, info);
9000 for (i = 0; i < flavors->num_flavors; i++) {
9001 secinfo = &flavors->flavors[i];
9003 switch (secinfo->flavor) {
9007 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9008 &secinfo->flavor_info);
9011 flavor = RPC_AUTH_MAXFLAVOR;
9015 if (!nfs_auth_info_match(&server->auth_info, flavor))
9016 flavor = RPC_AUTH_MAXFLAVOR;
9018 if (flavor != RPC_AUTH_MAXFLAVOR) {
9019 err = nfs4_lookup_root_sec(server, fhandle,
9026 if (flavor == RPC_AUTH_MAXFLAVOR)
9037 static int _nfs41_test_stateid(struct nfs_server *server,
9038 nfs4_stateid *stateid,
9039 struct rpc_cred *cred)
9042 struct nfs41_test_stateid_args args = {
9045 struct nfs41_test_stateid_res res;
9046 struct rpc_message msg = {
9047 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9052 struct rpc_clnt *rpc_client = server->client;
9054 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9057 dprintk("NFS call test_stateid %p\n", stateid);
9058 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
9059 nfs4_set_sequence_privileged(&args.seq_args);
9060 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
9061 &args.seq_args, &res.seq_res);
9062 if (status != NFS_OK) {
9063 dprintk("NFS reply test_stateid: failed, %d\n", status);
9066 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
9070 static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9071 int err, struct nfs4_exception *exception)
9073 exception->retry = 0;
9075 case -NFS4ERR_DELAY:
9076 case -NFS4ERR_RETRY_UNCACHED_REP:
9077 nfs4_handle_exception(server, err, exception);
9079 case -NFS4ERR_BADSESSION:
9080 case -NFS4ERR_BADSLOT:
9081 case -NFS4ERR_BAD_HIGH_SLOT:
9082 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9083 case -NFS4ERR_DEADSESSION:
9084 nfs4_do_handle_exception(server, err, exception);
9089 * nfs41_test_stateid - perform a TEST_STATEID operation
9091 * @server: server / transport on which to perform the operation
9092 * @stateid: state ID to test
9095 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9096 * Otherwise a negative NFS4ERR value is returned if the operation
9097 * failed or the state ID is not currently valid.
9099 static int nfs41_test_stateid(struct nfs_server *server,
9100 nfs4_stateid *stateid,
9101 struct rpc_cred *cred)
9103 struct nfs4_exception exception = { };
9106 err = _nfs41_test_stateid(server, stateid, cred);
9107 nfs4_handle_delay_or_session_error(server, err, &exception);
9108 } while (exception.retry);
9112 struct nfs_free_stateid_data {
9113 struct nfs_server *server;
9114 struct nfs41_free_stateid_args args;
9115 struct nfs41_free_stateid_res res;
9118 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9120 struct nfs_free_stateid_data *data = calldata;
9121 nfs41_setup_sequence(nfs4_get_session(data->server),
9122 &data->args.seq_args,
9127 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9129 struct nfs_free_stateid_data *data = calldata;
9131 nfs41_sequence_done(task, &data->res.seq_res);
9133 switch (task->tk_status) {
9134 case -NFS4ERR_DELAY:
9135 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
9136 rpc_restart_call_prepare(task);
9140 static void nfs41_free_stateid_release(void *calldata)
9145 static const struct rpc_call_ops nfs41_free_stateid_ops = {
9146 .rpc_call_prepare = nfs41_free_stateid_prepare,
9147 .rpc_call_done = nfs41_free_stateid_done,
9148 .rpc_release = nfs41_free_stateid_release,
9151 static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
9152 const nfs4_stateid *stateid,
9153 struct rpc_cred *cred,
9156 struct rpc_message msg = {
9157 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
9160 struct rpc_task_setup task_setup = {
9161 .rpc_client = server->client,
9162 .rpc_message = &msg,
9163 .callback_ops = &nfs41_free_stateid_ops,
9164 .flags = RPC_TASK_ASYNC,
9166 struct nfs_free_stateid_data *data;
9168 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9169 &task_setup.rpc_client, &msg);
9171 dprintk("NFS call free_stateid %p\n", stateid);
9172 data = kmalloc(sizeof(*data), GFP_NOFS);
9174 return ERR_PTR(-ENOMEM);
9175 data->server = server;
9176 nfs4_stateid_copy(&data->args.stateid, stateid);
9178 task_setup.callback_data = data;
9180 msg.rpc_argp = &data->args;
9181 msg.rpc_resp = &data->res;
9182 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
9184 nfs4_set_sequence_privileged(&data->args.seq_args);
9186 return rpc_run_task(&task_setup);
9190 * nfs41_free_stateid - perform a FREE_STATEID operation
9192 * @server: server / transport on which to perform the operation
9193 * @stateid: state ID to release
9195 * @is_recovery: set to true if this call needs to be privileged
9197 * Note: this function is always asynchronous.
9199 static int nfs41_free_stateid(struct nfs_server *server,
9200 const nfs4_stateid *stateid,
9201 struct rpc_cred *cred,
9204 struct rpc_task *task;
9206 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
9208 return PTR_ERR(task);
9214 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
9216 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
9218 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
9219 nfs4_free_lock_state(server, lsp);
9222 static bool nfs41_match_stateid(const nfs4_stateid *s1,
9223 const nfs4_stateid *s2)
9225 if (s1->type != s2->type)
9228 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
9231 if (s1->seqid == s2->seqid)
9233 if (s1->seqid == 0 || s2->seqid == 0)
9239 #endif /* CONFIG_NFS_V4_1 */
9241 static bool nfs4_match_stateid(const nfs4_stateid *s1,
9242 const nfs4_stateid *s2)
9244 return nfs4_stateid_match(s1, s2);
9248 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
9249 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9250 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9251 .recover_open = nfs4_open_reclaim,
9252 .recover_lock = nfs4_lock_reclaim,
9253 .establish_clid = nfs4_init_clientid,
9254 .detect_trunking = nfs40_discover_server_trunking,
9257 #if defined(CONFIG_NFS_V4_1)
9258 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
9259 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9260 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9261 .recover_open = nfs4_open_reclaim,
9262 .recover_lock = nfs4_lock_reclaim,
9263 .establish_clid = nfs41_init_clientid,
9264 .reclaim_complete = nfs41_proc_reclaim_complete,
9265 .detect_trunking = nfs41_discover_server_trunking,
9267 #endif /* CONFIG_NFS_V4_1 */
9269 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
9270 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9271 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
9272 .recover_open = nfs40_open_expired,
9273 .recover_lock = nfs4_lock_expired,
9274 .establish_clid = nfs4_init_clientid,
9277 #if defined(CONFIG_NFS_V4_1)
9278 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
9279 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9280 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
9281 .recover_open = nfs41_open_expired,
9282 .recover_lock = nfs41_lock_expired,
9283 .establish_clid = nfs41_init_clientid,
9285 #endif /* CONFIG_NFS_V4_1 */
9287 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
9288 .sched_state_renewal = nfs4_proc_async_renew,
9289 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
9290 .renew_lease = nfs4_proc_renew,
9293 #if defined(CONFIG_NFS_V4_1)
9294 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
9295 .sched_state_renewal = nfs41_proc_async_sequence,
9296 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
9297 .renew_lease = nfs4_proc_sequence,
9301 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
9302 .get_locations = _nfs40_proc_get_locations,
9303 .fsid_present = _nfs40_proc_fsid_present,
9306 #if defined(CONFIG_NFS_V4_1)
9307 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
9308 .get_locations = _nfs41_proc_get_locations,
9309 .fsid_present = _nfs41_proc_fsid_present,
9311 #endif /* CONFIG_NFS_V4_1 */
9313 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9315 .init_caps = NFS_CAP_READDIRPLUS
9316 | NFS_CAP_ATOMIC_OPEN
9317 | NFS_CAP_POSIX_LOCK,
9318 .init_client = nfs40_init_client,
9319 .shutdown_client = nfs40_shutdown_client,
9320 .match_stateid = nfs4_match_stateid,
9321 .find_root_sec = nfs4_find_root_sec,
9322 .free_lock_state = nfs4_release_lockowner,
9323 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
9324 .alloc_seqid = nfs_alloc_seqid,
9325 .call_sync_ops = &nfs40_call_sync_ops,
9326 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9327 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9328 .state_renewal_ops = &nfs40_state_renewal_ops,
9329 .mig_recovery_ops = &nfs40_mig_recovery_ops,
9332 #if defined(CONFIG_NFS_V4_1)
9333 static struct nfs_seqid *
9334 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9339 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9341 .init_caps = NFS_CAP_READDIRPLUS
9342 | NFS_CAP_ATOMIC_OPEN
9343 | NFS_CAP_POSIX_LOCK
9344 | NFS_CAP_STATEID_NFSV41
9345 | NFS_CAP_ATOMIC_OPEN_V1,
9346 .init_client = nfs41_init_client,
9347 .shutdown_client = nfs41_shutdown_client,
9348 .match_stateid = nfs41_match_stateid,
9349 .find_root_sec = nfs41_find_root_sec,
9350 .free_lock_state = nfs41_free_lock_state,
9351 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
9352 .alloc_seqid = nfs_alloc_no_seqid,
9353 .session_trunk = nfs4_test_session_trunk,
9354 .call_sync_ops = &nfs41_call_sync_ops,
9355 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9356 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9357 .state_renewal_ops = &nfs41_state_renewal_ops,
9358 .mig_recovery_ops = &nfs41_mig_recovery_ops,
9362 #if defined(CONFIG_NFS_V4_2)
9363 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9365 .init_caps = NFS_CAP_READDIRPLUS
9366 | NFS_CAP_ATOMIC_OPEN
9367 | NFS_CAP_POSIX_LOCK
9368 | NFS_CAP_STATEID_NFSV41
9369 | NFS_CAP_ATOMIC_OPEN_V1
9372 | NFS_CAP_DEALLOCATE
9374 | NFS_CAP_LAYOUTSTATS
9376 .init_client = nfs41_init_client,
9377 .shutdown_client = nfs41_shutdown_client,
9378 .match_stateid = nfs41_match_stateid,
9379 .find_root_sec = nfs41_find_root_sec,
9380 .free_lock_state = nfs41_free_lock_state,
9381 .call_sync_ops = &nfs41_call_sync_ops,
9382 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
9383 .alloc_seqid = nfs_alloc_no_seqid,
9384 .session_trunk = nfs4_test_session_trunk,
9385 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9386 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9387 .state_renewal_ops = &nfs41_state_renewal_ops,
9388 .mig_recovery_ops = &nfs41_mig_recovery_ops,
9392 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9393 [0] = &nfs_v4_0_minor_ops,
9394 #if defined(CONFIG_NFS_V4_1)
9395 [1] = &nfs_v4_1_minor_ops,
9397 #if defined(CONFIG_NFS_V4_2)
9398 [2] = &nfs_v4_2_minor_ops,
9402 static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
9404 ssize_t error, error2;
9406 error = generic_listxattr(dentry, list, size);
9414 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9417 return error + error2;
9420 static const struct inode_operations nfs4_dir_inode_operations = {
9421 .create = nfs_create,
9422 .lookup = nfs_lookup,
9423 .atomic_open = nfs_atomic_open,
9425 .unlink = nfs_unlink,
9426 .symlink = nfs_symlink,
9430 .rename = nfs_rename,
9431 .permission = nfs_permission,
9432 .getattr = nfs_getattr,
9433 .setattr = nfs_setattr,
9434 .listxattr = nfs4_listxattr,
9437 static const struct inode_operations nfs4_file_inode_operations = {
9438 .permission = nfs_permission,
9439 .getattr = nfs_getattr,
9440 .setattr = nfs_setattr,
9441 .listxattr = nfs4_listxattr,
9444 const struct nfs_rpc_ops nfs_v4_clientops = {
9445 .version = 4, /* protocol version */
9446 .dentry_ops = &nfs4_dentry_operations,
9447 .dir_inode_ops = &nfs4_dir_inode_operations,
9448 .file_inode_ops = &nfs4_file_inode_operations,
9449 .file_ops = &nfs4_file_operations,
9450 .getroot = nfs4_proc_get_root,
9451 .submount = nfs4_submount,
9452 .try_mount = nfs4_try_mount,
9453 .getattr = nfs4_proc_getattr,
9454 .setattr = nfs4_proc_setattr,
9455 .lookup = nfs4_proc_lookup,
9456 .access = nfs4_proc_access,
9457 .readlink = nfs4_proc_readlink,
9458 .create = nfs4_proc_create,
9459 .remove = nfs4_proc_remove,
9460 .unlink_setup = nfs4_proc_unlink_setup,
9461 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
9462 .unlink_done = nfs4_proc_unlink_done,
9463 .rename_setup = nfs4_proc_rename_setup,
9464 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
9465 .rename_done = nfs4_proc_rename_done,
9466 .link = nfs4_proc_link,
9467 .symlink = nfs4_proc_symlink,
9468 .mkdir = nfs4_proc_mkdir,
9469 .rmdir = nfs4_proc_remove,
9470 .readdir = nfs4_proc_readdir,
9471 .mknod = nfs4_proc_mknod,
9472 .statfs = nfs4_proc_statfs,
9473 .fsinfo = nfs4_proc_fsinfo,
9474 .pathconf = nfs4_proc_pathconf,
9475 .set_capabilities = nfs4_server_capabilities,
9476 .decode_dirent = nfs4_decode_dirent,
9477 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
9478 .read_setup = nfs4_proc_read_setup,
9479 .read_done = nfs4_read_done,
9480 .write_setup = nfs4_proc_write_setup,
9481 .write_done = nfs4_write_done,
9482 .commit_setup = nfs4_proc_commit_setup,
9483 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
9484 .commit_done = nfs4_commit_done,
9485 .lock = nfs4_proc_lock,
9486 .clear_acl_cache = nfs4_zap_acl_attr,
9487 .close_context = nfs4_close_context,
9488 .open_context = nfs4_atomic_open,
9489 .have_delegation = nfs4_have_delegation,
9490 .return_delegation = nfs4_inode_return_delegation,
9491 .alloc_client = nfs4_alloc_client,
9492 .init_client = nfs4_init_client,
9493 .free_client = nfs4_free_client,
9494 .create_server = nfs4_create_server,
9495 .clone_server = nfs_clone_server,
9498 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
9499 .name = XATTR_NAME_NFSV4_ACL,
9500 .list = nfs4_xattr_list_nfs4_acl,
9501 .get = nfs4_xattr_get_nfs4_acl,
9502 .set = nfs4_xattr_set_nfs4_acl,
9505 const struct xattr_handler *nfs4_xattr_handlers[] = {
9506 &nfs4_xattr_nfs4_acl_handler,
9507 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
9508 &nfs4_xattr_nfs4_label_handler,