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)
1087 struct nfs_inode *nfsi = NFS_I(dir);
1089 spin_lock(&dir->i_lock);
1090 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
1091 if (cinfo->atomic && cinfo->before == dir->i_version) {
1092 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1093 nfsi->attrtimeo_timestamp = jiffies;
1095 nfs_force_lookup_revalidate(dir);
1096 if (cinfo->before != dir->i_version)
1097 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1098 NFS_INO_INVALID_ACL;
1100 dir->i_version = cinfo->after;
1101 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
1102 nfs_fscache_invalidate(dir);
1103 spin_unlock(&dir->i_lock);
1106 struct nfs4_opendata {
1108 struct nfs_openargs o_arg;
1109 struct nfs_openres o_res;
1110 struct nfs_open_confirmargs c_arg;
1111 struct nfs_open_confirmres c_res;
1112 struct nfs4_string owner_name;
1113 struct nfs4_string group_name;
1114 struct nfs4_label *a_label;
1115 struct nfs_fattr f_attr;
1116 struct nfs4_label *f_label;
1118 struct dentry *dentry;
1119 struct nfs4_state_owner *owner;
1120 struct nfs4_state *state;
1122 unsigned long timestamp;
1123 unsigned int rpc_done : 1;
1124 unsigned int file_created : 1;
1125 unsigned int is_recover : 1;
1130 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1131 int err, struct nfs4_exception *exception)
1135 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1137 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1138 exception->retry = 1;
1143 nfs4_map_atomic_open_share(struct nfs_server *server,
1144 fmode_t fmode, int openflags)
1148 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1150 res = NFS4_SHARE_ACCESS_READ;
1153 res = NFS4_SHARE_ACCESS_WRITE;
1155 case FMODE_READ|FMODE_WRITE:
1156 res = NFS4_SHARE_ACCESS_BOTH;
1158 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1160 /* Want no delegation if we're using O_DIRECT */
1161 if (openflags & O_DIRECT)
1162 res |= NFS4_SHARE_WANT_NO_DELEG;
1167 static enum open_claim_type4
1168 nfs4_map_atomic_open_claim(struct nfs_server *server,
1169 enum open_claim_type4 claim)
1171 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1176 case NFS4_OPEN_CLAIM_FH:
1177 return NFS4_OPEN_CLAIM_NULL;
1178 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1179 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1180 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1181 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1185 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
1187 p->o_res.f_attr = &p->f_attr;
1188 p->o_res.f_label = p->f_label;
1189 p->o_res.seqid = p->o_arg.seqid;
1190 p->c_res.seqid = p->c_arg.seqid;
1191 p->o_res.server = p->o_arg.server;
1192 p->o_res.access_request = p->o_arg.access;
1193 nfs_fattr_init(&p->f_attr);
1194 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
1197 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
1198 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
1199 const struct iattr *attrs,
1200 struct nfs4_label *label,
1201 enum open_claim_type4 claim,
1204 struct dentry *parent = dget_parent(dentry);
1205 struct inode *dir = d_inode(parent);
1206 struct nfs_server *server = NFS_SERVER(dir);
1207 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
1208 struct nfs4_opendata *p;
1210 p = kzalloc(sizeof(*p), gfp_mask);
1214 p->f_label = nfs4_label_alloc(server, gfp_mask);
1215 if (IS_ERR(p->f_label))
1218 p->a_label = nfs4_label_alloc(server, gfp_mask);
1219 if (IS_ERR(p->a_label))
1222 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1223 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
1224 if (IS_ERR(p->o_arg.seqid))
1225 goto err_free_label;
1226 nfs_sb_active(dentry->d_sb);
1227 p->dentry = dget(dentry);
1230 atomic_inc(&sp->so_count);
1231 p->o_arg.open_flags = flags;
1232 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
1233 p->o_arg.umask = current_umask();
1234 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1235 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1237 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1238 * will return permission denied for all bits until close */
1239 if (!(flags & O_EXCL)) {
1240 /* ask server to check for all possible rights as results
1242 switch (p->o_arg.claim) {
1245 case NFS4_OPEN_CLAIM_NULL:
1246 case NFS4_OPEN_CLAIM_FH:
1247 p->o_arg.access = NFS4_ACCESS_READ |
1248 NFS4_ACCESS_MODIFY |
1249 NFS4_ACCESS_EXTEND |
1250 NFS4_ACCESS_EXECUTE;
1253 p->o_arg.clientid = server->nfs_client->cl_clientid;
1254 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1255 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1256 p->o_arg.name = &dentry->d_name;
1257 p->o_arg.server = server;
1258 p->o_arg.bitmask = nfs4_bitmask(server, label);
1259 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1260 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1261 switch (p->o_arg.claim) {
1262 case NFS4_OPEN_CLAIM_NULL:
1263 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1264 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1265 p->o_arg.fh = NFS_FH(dir);
1267 case NFS4_OPEN_CLAIM_PREVIOUS:
1268 case NFS4_OPEN_CLAIM_FH:
1269 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1270 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1271 p->o_arg.fh = NFS_FH(d_inode(dentry));
1273 if (attrs != NULL && attrs->ia_valid != 0) {
1276 p->o_arg.u.attrs = &p->attrs;
1277 memcpy(&p->attrs, attrs, sizeof(p->attrs));
1280 verf[1] = current->pid;
1281 memcpy(p->o_arg.u.verifier.data, verf,
1282 sizeof(p->o_arg.u.verifier.data));
1284 p->c_arg.fh = &p->o_res.fh;
1285 p->c_arg.stateid = &p->o_res.stateid;
1286 p->c_arg.seqid = p->o_arg.seqid;
1287 nfs4_init_opendata_res(p);
1288 kref_init(&p->kref);
1292 nfs4_label_free(p->a_label);
1294 nfs4_label_free(p->f_label);
1302 static void nfs4_opendata_free(struct kref *kref)
1304 struct nfs4_opendata *p = container_of(kref,
1305 struct nfs4_opendata, kref);
1306 struct super_block *sb = p->dentry->d_sb;
1308 nfs_free_seqid(p->o_arg.seqid);
1309 nfs4_sequence_free_slot(&p->o_res.seq_res);
1310 if (p->state != NULL)
1311 nfs4_put_open_state(p->state);
1312 nfs4_put_state_owner(p->owner);
1314 nfs4_label_free(p->a_label);
1315 nfs4_label_free(p->f_label);
1319 nfs_sb_deactive(sb);
1320 nfs_fattr_free_names(&p->f_attr);
1321 kfree(p->f_attr.mdsthreshold);
1325 static void nfs4_opendata_put(struct nfs4_opendata *p)
1328 kref_put(&p->kref, nfs4_opendata_free);
1331 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1335 ret = rpc_wait_for_completion_task(task);
1339 static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1342 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1343 case FMODE_READ|FMODE_WRITE:
1344 return state->n_rdwr != 0;
1346 return state->n_wronly != 0;
1348 return state->n_rdonly != 0;
1354 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
1358 if (open_mode & (O_EXCL|O_TRUNC))
1360 switch (mode & (FMODE_READ|FMODE_WRITE)) {
1362 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1363 && state->n_rdonly != 0;
1366 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1367 && state->n_wronly != 0;
1369 case FMODE_READ|FMODE_WRITE:
1370 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1371 && state->n_rdwr != 0;
1377 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1378 enum open_claim_type4 claim)
1380 if (delegation == NULL)
1382 if ((delegation->type & fmode) != fmode)
1384 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1387 case NFS4_OPEN_CLAIM_NULL:
1388 case NFS4_OPEN_CLAIM_FH:
1390 case NFS4_OPEN_CLAIM_PREVIOUS:
1391 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1396 nfs_mark_delegation_referenced(delegation);
1400 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1409 case FMODE_READ|FMODE_WRITE:
1412 nfs4_state_set_mode_locked(state, state->state | fmode);
1415 #ifdef CONFIG_NFS_V4_1
1416 static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1418 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1420 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1422 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1426 #endif /* CONFIG_NFS_V4_1 */
1428 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
1430 struct nfs_client *clp = state->owner->so_server->nfs_client;
1431 bool need_recover = false;
1433 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1434 need_recover = true;
1435 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1436 need_recover = true;
1437 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1438 need_recover = true;
1440 nfs4_state_mark_reclaim_nograce(clp, state);
1443 static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1444 const nfs4_stateid *stateid, nfs4_stateid *freeme)
1446 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1448 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1449 nfs4_stateid_copy(freeme, &state->open_stateid);
1450 nfs_test_and_clear_all_open_stateid(state);
1453 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1458 static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1460 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1462 if (state->n_wronly)
1463 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1464 if (state->n_rdonly)
1465 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1467 set_bit(NFS_O_RDWR_STATE, &state->flags);
1468 set_bit(NFS_OPEN_STATE, &state->flags);
1471 static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1472 nfs4_stateid *stateid, fmode_t fmode)
1474 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1475 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1477 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1480 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1483 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1484 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1485 clear_bit(NFS_OPEN_STATE, &state->flags);
1487 if (stateid == NULL)
1489 /* Handle OPEN+OPEN_DOWNGRADE races */
1490 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1491 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1492 nfs_resync_open_stateid_locked(state);
1495 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1496 nfs4_stateid_copy(&state->stateid, stateid);
1497 nfs4_stateid_copy(&state->open_stateid, stateid);
1500 static void nfs_clear_open_stateid(struct nfs4_state *state,
1501 nfs4_stateid *arg_stateid,
1502 nfs4_stateid *stateid, fmode_t fmode)
1504 write_seqlock(&state->seqlock);
1505 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1506 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1507 nfs_clear_open_stateid_locked(state, stateid, fmode);
1508 write_sequnlock(&state->seqlock);
1509 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1510 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1513 static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1514 const nfs4_stateid *stateid, fmode_t fmode,
1515 nfs4_stateid *freeme)
1519 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1522 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1524 case FMODE_READ|FMODE_WRITE:
1525 set_bit(NFS_O_RDWR_STATE, &state->flags);
1527 if (!nfs_need_update_open_stateid(state, stateid, freeme))
1529 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1530 nfs4_stateid_copy(&state->stateid, stateid);
1531 nfs4_stateid_copy(&state->open_stateid, stateid);
1534 static void __update_open_stateid(struct nfs4_state *state,
1535 const nfs4_stateid *open_stateid,
1536 const nfs4_stateid *deleg_stateid,
1538 nfs4_stateid *freeme)
1541 * Protect the call to nfs4_state_set_mode_locked and
1542 * serialise the stateid update
1544 spin_lock(&state->owner->so_lock);
1545 write_seqlock(&state->seqlock);
1546 if (deleg_stateid != NULL) {
1547 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1548 set_bit(NFS_DELEGATED_STATE, &state->flags);
1550 if (open_stateid != NULL)
1551 nfs_set_open_stateid_locked(state, open_stateid, fmode, freeme);
1552 write_sequnlock(&state->seqlock);
1553 update_open_stateflags(state, fmode);
1554 spin_unlock(&state->owner->so_lock);
1557 static int update_open_stateid(struct nfs4_state *state,
1558 const nfs4_stateid *open_stateid,
1559 const nfs4_stateid *delegation,
1562 struct nfs_server *server = NFS_SERVER(state->inode);
1563 struct nfs_client *clp = server->nfs_client;
1564 struct nfs_inode *nfsi = NFS_I(state->inode);
1565 struct nfs_delegation *deleg_cur;
1566 nfs4_stateid freeme = { };
1569 fmode &= (FMODE_READ|FMODE_WRITE);
1572 deleg_cur = rcu_dereference(nfsi->delegation);
1573 if (deleg_cur == NULL)
1576 spin_lock(&deleg_cur->lock);
1577 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1578 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1579 (deleg_cur->type & fmode) != fmode)
1580 goto no_delegation_unlock;
1582 if (delegation == NULL)
1583 delegation = &deleg_cur->stateid;
1584 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
1585 goto no_delegation_unlock;
1587 nfs_mark_delegation_referenced(deleg_cur);
1588 __update_open_stateid(state, open_stateid, &deleg_cur->stateid,
1591 no_delegation_unlock:
1592 spin_unlock(&deleg_cur->lock);
1596 if (!ret && open_stateid != NULL) {
1597 __update_open_stateid(state, open_stateid, NULL, fmode, &freeme);
1600 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1601 nfs4_schedule_state_manager(clp);
1602 if (freeme.type != 0)
1603 nfs4_test_and_free_stateid(server, &freeme,
1604 state->owner->so_cred);
1609 static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1610 const nfs4_stateid *stateid)
1612 struct nfs4_state *state = lsp->ls_state;
1615 spin_lock(&state->state_lock);
1616 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1618 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1620 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1623 spin_unlock(&state->state_lock);
1627 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1629 struct nfs_delegation *delegation;
1632 delegation = rcu_dereference(NFS_I(inode)->delegation);
1633 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1638 nfs4_inode_return_delegation(inode);
1641 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1643 struct nfs4_state *state = opendata->state;
1644 struct nfs_inode *nfsi = NFS_I(state->inode);
1645 struct nfs_delegation *delegation;
1646 int open_mode = opendata->o_arg.open_flags;
1647 fmode_t fmode = opendata->o_arg.fmode;
1648 enum open_claim_type4 claim = opendata->o_arg.claim;
1649 nfs4_stateid stateid;
1653 spin_lock(&state->owner->so_lock);
1654 if (can_open_cached(state, fmode, open_mode)) {
1655 update_open_stateflags(state, fmode);
1656 spin_unlock(&state->owner->so_lock);
1657 goto out_return_state;
1659 spin_unlock(&state->owner->so_lock);
1661 delegation = rcu_dereference(nfsi->delegation);
1662 if (!can_open_delegated(delegation, fmode, claim)) {
1666 /* Save the delegation */
1667 nfs4_stateid_copy(&stateid, &delegation->stateid);
1669 nfs_release_seqid(opendata->o_arg.seqid);
1670 if (!opendata->is_recover) {
1671 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1677 /* Try to update the stateid using the delegation */
1678 if (update_open_stateid(state, NULL, &stateid, fmode))
1679 goto out_return_state;
1682 return ERR_PTR(ret);
1684 atomic_inc(&state->count);
1689 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1691 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1692 struct nfs_delegation *delegation;
1693 int delegation_flags = 0;
1696 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1698 delegation_flags = delegation->flags;
1700 switch (data->o_arg.claim) {
1703 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1704 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1705 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1706 "returning a delegation for "
1707 "OPEN(CLAIM_DELEGATE_CUR)\n",
1711 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1712 nfs_inode_set_delegation(state->inode,
1713 data->owner->so_cred,
1716 nfs_inode_reclaim_delegation(state->inode,
1717 data->owner->so_cred,
1722 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1723 * and update the nfs4_state.
1725 static struct nfs4_state *
1726 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1728 struct inode *inode = data->state->inode;
1729 struct nfs4_state *state = data->state;
1732 if (!data->rpc_done) {
1733 if (data->rpc_status) {
1734 ret = data->rpc_status;
1737 /* cached opens have already been processed */
1741 ret = nfs_refresh_inode(inode, &data->f_attr);
1745 if (data->o_res.delegation_type != 0)
1746 nfs4_opendata_check_deleg(data, state);
1748 update_open_stateid(state, &data->o_res.stateid, NULL,
1750 atomic_inc(&state->count);
1754 return ERR_PTR(ret);
1758 static struct nfs4_state *
1759 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1761 struct inode *inode;
1762 struct nfs4_state *state = NULL;
1765 if (!data->rpc_done) {
1766 state = nfs4_try_open_cached(data);
1767 trace_nfs4_cached_open(data->state);
1772 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1774 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
1775 ret = PTR_ERR(inode);
1779 state = nfs4_get_open_state(inode, data->owner);
1782 if (data->o_res.delegation_type != 0)
1783 nfs4_opendata_check_deleg(data, state);
1784 update_open_stateid(state, &data->o_res.stateid, NULL,
1788 nfs_release_seqid(data->o_arg.seqid);
1793 return ERR_PTR(ret);
1796 static struct nfs4_state *
1797 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1799 struct nfs4_state *ret;
1801 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1802 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1804 ret = _nfs4_opendata_to_nfs4_state(data);
1805 nfs4_sequence_free_slot(&data->o_res.seq_res);
1809 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1811 struct nfs_inode *nfsi = NFS_I(state->inode);
1812 struct nfs_open_context *ctx;
1814 spin_lock(&state->inode->i_lock);
1815 list_for_each_entry(ctx, &nfsi->open_files, list) {
1816 if (ctx->state != state)
1818 get_nfs_open_context(ctx);
1819 spin_unlock(&state->inode->i_lock);
1822 spin_unlock(&state->inode->i_lock);
1823 return ERR_PTR(-ENOENT);
1826 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1827 struct nfs4_state *state, enum open_claim_type4 claim)
1829 struct nfs4_opendata *opendata;
1831 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
1832 NULL, NULL, claim, GFP_NOFS);
1833 if (opendata == NULL)
1834 return ERR_PTR(-ENOMEM);
1835 opendata->state = state;
1836 atomic_inc(&state->count);
1840 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1843 struct nfs4_state *newstate;
1846 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
1848 opendata->o_arg.open_flags = 0;
1849 opendata->o_arg.fmode = fmode;
1850 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1851 NFS_SB(opendata->dentry->d_sb),
1853 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1854 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1855 nfs4_init_opendata_res(opendata);
1856 ret = _nfs4_recover_proc_open(opendata);
1859 newstate = nfs4_opendata_to_nfs4_state(opendata);
1860 if (IS_ERR(newstate))
1861 return PTR_ERR(newstate);
1862 if (newstate != opendata->state)
1864 nfs4_close_state(newstate, fmode);
1868 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1872 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1873 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1874 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1875 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1876 /* memory barrier prior to reading state->n_* */
1877 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1878 clear_bit(NFS_OPEN_STATE, &state->flags);
1880 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1883 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1886 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1890 * We may have performed cached opens for all three recoveries.
1891 * Check if we need to update the current stateid.
1893 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1894 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
1895 write_seqlock(&state->seqlock);
1896 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1897 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1898 write_sequnlock(&state->seqlock);
1905 * reclaim state on the server after a reboot.
1907 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1909 struct nfs_delegation *delegation;
1910 struct nfs4_opendata *opendata;
1911 fmode_t delegation_type = 0;
1914 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1915 NFS4_OPEN_CLAIM_PREVIOUS);
1916 if (IS_ERR(opendata))
1917 return PTR_ERR(opendata);
1919 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1920 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1921 delegation_type = delegation->type;
1923 opendata->o_arg.u.delegation_type = delegation_type;
1924 status = nfs4_open_recover(opendata, state);
1925 nfs4_opendata_put(opendata);
1929 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1931 struct nfs_server *server = NFS_SERVER(state->inode);
1932 struct nfs4_exception exception = { };
1935 err = _nfs4_do_open_reclaim(ctx, state);
1936 trace_nfs4_open_reclaim(ctx, 0, err);
1937 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1939 if (err != -NFS4ERR_DELAY)
1941 nfs4_handle_exception(server, err, &exception);
1942 } while (exception.retry);
1946 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1948 struct nfs_open_context *ctx;
1951 ctx = nfs4_state_find_open_context(state);
1954 ret = nfs4_do_open_reclaim(ctx, state);
1955 put_nfs_open_context(ctx);
1959 static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, int err)
1963 printk(KERN_ERR "NFS: %s: unhandled error "
1964 "%d.\n", __func__, err);
1970 case -NFS4ERR_BADSESSION:
1971 case -NFS4ERR_BADSLOT:
1972 case -NFS4ERR_BAD_HIGH_SLOT:
1973 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1974 case -NFS4ERR_DEADSESSION:
1975 set_bit(NFS_DELEGATED_STATE, &state->flags);
1976 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1978 case -NFS4ERR_STALE_CLIENTID:
1979 case -NFS4ERR_STALE_STATEID:
1980 set_bit(NFS_DELEGATED_STATE, &state->flags);
1981 /* Don't recall a delegation if it was lost */
1982 nfs4_schedule_lease_recovery(server->nfs_client);
1984 case -NFS4ERR_MOVED:
1985 nfs4_schedule_migration_recovery(server);
1987 case -NFS4ERR_LEASE_MOVED:
1988 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1990 case -NFS4ERR_DELEG_REVOKED:
1991 case -NFS4ERR_ADMIN_REVOKED:
1992 case -NFS4ERR_EXPIRED:
1993 case -NFS4ERR_BAD_STATEID:
1994 case -NFS4ERR_OPENMODE:
1995 nfs_inode_find_state_and_recover(state->inode,
1997 nfs4_schedule_stateid_recovery(server, state);
1999 case -NFS4ERR_DELAY:
2000 case -NFS4ERR_GRACE:
2001 set_bit(NFS_DELEGATED_STATE, &state->flags);
2005 case -NFS4ERR_DENIED:
2006 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
2012 int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2013 struct nfs4_state *state, const nfs4_stateid *stateid,
2016 struct nfs_server *server = NFS_SERVER(state->inode);
2017 struct nfs4_opendata *opendata;
2020 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2021 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2022 if (IS_ERR(opendata))
2023 return PTR_ERR(opendata);
2024 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
2025 write_seqlock(&state->seqlock);
2026 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2027 write_sequnlock(&state->seqlock);
2028 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2029 switch (type & (FMODE_READ|FMODE_WRITE)) {
2030 case FMODE_READ|FMODE_WRITE:
2032 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2035 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2039 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2041 nfs4_opendata_put(opendata);
2042 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
2045 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2047 struct nfs4_opendata *data = calldata;
2049 nfs40_setup_sequence(data->o_arg.server->nfs_client->cl_slot_tbl,
2050 &data->c_arg.seq_args, &data->c_res.seq_res, task);
2053 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2055 struct nfs4_opendata *data = calldata;
2057 nfs40_sequence_done(task, &data->c_res.seq_res);
2059 data->rpc_status = task->tk_status;
2060 if (data->rpc_status == 0) {
2061 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
2062 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2063 renew_lease(data->o_res.server, data->timestamp);
2068 static void nfs4_open_confirm_release(void *calldata)
2070 struct nfs4_opendata *data = calldata;
2071 struct nfs4_state *state = NULL;
2073 /* If this request hasn't been cancelled, do nothing */
2074 if (data->cancelled == 0)
2076 /* In case of error, no cleanup! */
2077 if (!data->rpc_done)
2079 state = nfs4_opendata_to_nfs4_state(data);
2081 nfs4_close_state(state, data->o_arg.fmode);
2083 nfs4_opendata_put(data);
2086 static const struct rpc_call_ops nfs4_open_confirm_ops = {
2087 .rpc_call_prepare = nfs4_open_confirm_prepare,
2088 .rpc_call_done = nfs4_open_confirm_done,
2089 .rpc_release = nfs4_open_confirm_release,
2093 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2095 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2097 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
2098 struct rpc_task *task;
2099 struct rpc_message msg = {
2100 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2101 .rpc_argp = &data->c_arg,
2102 .rpc_resp = &data->c_res,
2103 .rpc_cred = data->owner->so_cred,
2105 struct rpc_task_setup task_setup_data = {
2106 .rpc_client = server->client,
2107 .rpc_message = &msg,
2108 .callback_ops = &nfs4_open_confirm_ops,
2109 .callback_data = data,
2110 .workqueue = nfsiod_workqueue,
2111 .flags = RPC_TASK_ASYNC,
2115 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
2116 kref_get(&data->kref);
2118 data->rpc_status = 0;
2119 data->timestamp = jiffies;
2120 if (data->is_recover)
2121 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
2122 task = rpc_run_task(&task_setup_data);
2124 return PTR_ERR(task);
2125 status = nfs4_wait_for_completion_rpc_task(task);
2127 data->cancelled = 1;
2130 status = data->rpc_status;
2135 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
2137 struct nfs4_opendata *data = calldata;
2138 struct nfs4_state_owner *sp = data->owner;
2139 struct nfs_client *clp = sp->so_server->nfs_client;
2140 enum open_claim_type4 claim = data->o_arg.claim;
2142 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
2145 * Check if we still need to send an OPEN call, or if we can use
2146 * a delegation instead.
2148 if (data->state != NULL) {
2149 struct nfs_delegation *delegation;
2151 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
2154 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
2155 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
2156 goto unlock_no_action;
2159 /* Update client id. */
2160 data->o_arg.clientid = clp->cl_clientid;
2164 case NFS4_OPEN_CLAIM_PREVIOUS:
2165 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2166 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
2167 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
2168 case NFS4_OPEN_CLAIM_FH:
2169 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
2170 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
2172 data->timestamp = jiffies;
2173 if (nfs4_setup_sequence(data->o_arg.server,
2174 &data->o_arg.seq_args,
2175 &data->o_res.seq_res,
2177 nfs_release_seqid(data->o_arg.seqid);
2179 /* Set the create mode (note dependency on the session type) */
2180 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2181 if (data->o_arg.open_flags & O_EXCL) {
2182 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2183 if (nfs4_has_persistent_session(clp))
2184 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2185 else if (clp->cl_mvops->minor_version > 0)
2186 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2190 trace_nfs4_cached_open(data->state);
2193 task->tk_action = NULL;
2195 nfs4_sequence_done(task, &data->o_res.seq_res);
2198 static void nfs4_open_done(struct rpc_task *task, void *calldata)
2200 struct nfs4_opendata *data = calldata;
2202 data->rpc_status = task->tk_status;
2204 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
2207 if (task->tk_status == 0) {
2208 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2209 switch (data->o_res.f_attr->mode & S_IFMT) {
2213 data->rpc_status = -ELOOP;
2216 data->rpc_status = -EISDIR;
2219 data->rpc_status = -ENOTDIR;
2222 renew_lease(data->o_res.server, data->timestamp);
2223 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2224 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2229 static void nfs4_open_release(void *calldata)
2231 struct nfs4_opendata *data = calldata;
2232 struct nfs4_state *state = NULL;
2234 /* If this request hasn't been cancelled, do nothing */
2235 if (data->cancelled == 0)
2237 /* In case of error, no cleanup! */
2238 if (data->rpc_status != 0 || !data->rpc_done)
2240 /* In case we need an open_confirm, no cleanup! */
2241 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2243 state = nfs4_opendata_to_nfs4_state(data);
2245 nfs4_close_state(state, data->o_arg.fmode);
2247 nfs4_opendata_put(data);
2250 static const struct rpc_call_ops nfs4_open_ops = {
2251 .rpc_call_prepare = nfs4_open_prepare,
2252 .rpc_call_done = nfs4_open_done,
2253 .rpc_release = nfs4_open_release,
2256 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
2258 struct inode *dir = d_inode(data->dir);
2259 struct nfs_server *server = NFS_SERVER(dir);
2260 struct nfs_openargs *o_arg = &data->o_arg;
2261 struct nfs_openres *o_res = &data->o_res;
2262 struct rpc_task *task;
2263 struct rpc_message msg = {
2264 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2267 .rpc_cred = data->owner->so_cred,
2269 struct rpc_task_setup task_setup_data = {
2270 .rpc_client = server->client,
2271 .rpc_message = &msg,
2272 .callback_ops = &nfs4_open_ops,
2273 .callback_data = data,
2274 .workqueue = nfsiod_workqueue,
2275 .flags = RPC_TASK_ASYNC,
2279 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
2280 kref_get(&data->kref);
2282 data->rpc_status = 0;
2283 data->cancelled = 0;
2284 data->is_recover = 0;
2286 nfs4_set_sequence_privileged(&o_arg->seq_args);
2287 data->is_recover = 1;
2289 task = rpc_run_task(&task_setup_data);
2291 return PTR_ERR(task);
2292 status = nfs4_wait_for_completion_rpc_task(task);
2294 data->cancelled = 1;
2297 status = data->rpc_status;
2303 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2305 struct inode *dir = d_inode(data->dir);
2306 struct nfs_openres *o_res = &data->o_res;
2309 status = nfs4_run_open_task(data, 1);
2310 if (status != 0 || !data->rpc_done)
2313 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2315 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2316 status = _nfs4_proc_open_confirm(data);
2325 * Additional permission checks in order to distinguish between an
2326 * open for read, and an open for execute. This works around the
2327 * fact that NFSv4 OPEN treats read and execute permissions as being
2329 * Note that in the non-execute case, we want to turn off permission
2330 * checking if we just created a new file (POSIX open() semantics).
2332 static int nfs4_opendata_access(struct rpc_cred *cred,
2333 struct nfs4_opendata *opendata,
2334 struct nfs4_state *state, fmode_t fmode,
2337 struct nfs_access_entry cache;
2340 /* access call failed or for some reason the server doesn't
2341 * support any access modes -- defer access call until later */
2342 if (opendata->o_res.access_supported == 0)
2347 * Use openflags to check for exec, because fmode won't
2348 * always have FMODE_EXEC set when file open for exec.
2350 if (openflags & __FMODE_EXEC) {
2351 /* ONLY check for exec rights */
2353 } else if ((fmode & FMODE_READ) && !opendata->file_created)
2357 cache.jiffies = jiffies;
2358 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2359 nfs_access_add_cache(state->inode, &cache);
2361 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
2364 /* even though OPEN succeeded, access is denied. Close the file */
2365 nfs4_close_state(state, fmode);
2370 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2372 static int _nfs4_proc_open(struct nfs4_opendata *data)
2374 struct inode *dir = d_inode(data->dir);
2375 struct nfs_server *server = NFS_SERVER(dir);
2376 struct nfs_openargs *o_arg = &data->o_arg;
2377 struct nfs_openres *o_res = &data->o_res;
2380 status = nfs4_run_open_task(data, 0);
2381 if (!data->rpc_done)
2384 if (status == -NFS4ERR_BADNAME &&
2385 !(o_arg->open_flags & O_CREAT))
2390 nfs_fattr_map_and_free_names(server, &data->f_attr);
2392 if (o_arg->open_flags & O_CREAT) {
2393 if (o_arg->open_flags & O_EXCL)
2394 data->file_created = 1;
2395 else if (o_res->cinfo.before != o_res->cinfo.after)
2396 data->file_created = 1;
2397 if (data->file_created || dir->i_version != o_res->cinfo.after)
2398 update_changeattr(dir, &o_res->cinfo);
2400 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2401 server->caps &= ~NFS_CAP_POSIX_LOCK;
2402 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2403 status = _nfs4_proc_open_confirm(data);
2407 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
2408 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
2412 static int nfs4_recover_expired_lease(struct nfs_server *server)
2414 return nfs4_client_recover_expired_lease(server->nfs_client);
2419 * reclaim state on the server after a network partition.
2420 * Assumes caller holds the appropriate lock
2422 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2424 struct nfs4_opendata *opendata;
2427 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2428 NFS4_OPEN_CLAIM_FH);
2429 if (IS_ERR(opendata))
2430 return PTR_ERR(opendata);
2431 ret = nfs4_open_recover(opendata, state);
2433 d_drop(ctx->dentry);
2434 nfs4_opendata_put(opendata);
2438 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2440 struct nfs_server *server = NFS_SERVER(state->inode);
2441 struct nfs4_exception exception = { };
2445 err = _nfs4_open_expired(ctx, state);
2446 trace_nfs4_open_expired(ctx, 0, err);
2447 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2452 case -NFS4ERR_GRACE:
2453 case -NFS4ERR_DELAY:
2454 nfs4_handle_exception(server, err, &exception);
2457 } while (exception.retry);
2462 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2464 struct nfs_open_context *ctx;
2467 ctx = nfs4_state_find_open_context(state);
2470 ret = nfs4_do_open_expired(ctx, state);
2471 put_nfs_open_context(ctx);
2475 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2476 const nfs4_stateid *stateid)
2478 nfs_remove_bad_delegation(state->inode, stateid);
2479 write_seqlock(&state->seqlock);
2480 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2481 write_sequnlock(&state->seqlock);
2482 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2485 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2487 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2488 nfs_finish_clear_delegation_stateid(state, NULL);
2491 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2493 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2494 nfs40_clear_delegation_stateid(state);
2495 return nfs4_open_expired(sp, state);
2498 static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2499 nfs4_stateid *stateid,
2500 struct rpc_cred *cred)
2502 return -NFS4ERR_BAD_STATEID;
2505 #if defined(CONFIG_NFS_V4_1)
2506 static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2507 nfs4_stateid *stateid,
2508 struct rpc_cred *cred)
2512 switch (stateid->type) {
2515 case NFS4_INVALID_STATEID_TYPE:
2516 case NFS4_SPECIAL_STATEID_TYPE:
2517 return -NFS4ERR_BAD_STATEID;
2518 case NFS4_REVOKED_STATEID_TYPE:
2522 status = nfs41_test_stateid(server, stateid, cred);
2524 case -NFS4ERR_EXPIRED:
2525 case -NFS4ERR_ADMIN_REVOKED:
2526 case -NFS4ERR_DELEG_REVOKED:
2532 /* Ack the revoked state to the server */
2533 nfs41_free_stateid(server, stateid, cred, true);
2534 return -NFS4ERR_EXPIRED;
2537 static void nfs41_check_delegation_stateid(struct nfs4_state *state)
2539 struct nfs_server *server = NFS_SERVER(state->inode);
2540 nfs4_stateid stateid;
2541 struct nfs_delegation *delegation;
2542 struct rpc_cred *cred;
2545 /* Get the delegation credential for use by test/free_stateid */
2547 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2548 if (delegation == NULL) {
2553 nfs4_stateid_copy(&stateid, &delegation->stateid);
2554 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
2556 nfs_finish_clear_delegation_stateid(state, &stateid);
2560 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags)) {
2565 cred = get_rpccred(delegation->cred);
2567 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
2568 trace_nfs4_test_delegation_stateid(state, NULL, status);
2569 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
2570 nfs_finish_clear_delegation_stateid(state, &stateid);
2576 * nfs41_check_expired_locks - possibly free a lock stateid
2578 * @state: NFSv4 state for an inode
2580 * Returns NFS_OK if recovery for this stateid is now finished.
2581 * Otherwise a negative NFS4ERR value is returned.
2583 static int nfs41_check_expired_locks(struct nfs4_state *state)
2585 int status, ret = NFS_OK;
2586 struct nfs4_lock_state *lsp, *prev = NULL;
2587 struct nfs_server *server = NFS_SERVER(state->inode);
2589 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2592 spin_lock(&state->state_lock);
2593 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2594 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2595 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2597 atomic_inc(&lsp->ls_count);
2598 spin_unlock(&state->state_lock);
2600 nfs4_put_lock_state(prev);
2603 status = nfs41_test_and_free_expired_stateid(server,
2606 trace_nfs4_test_lock_stateid(state, lsp, status);
2607 if (status == -NFS4ERR_EXPIRED ||
2608 status == -NFS4ERR_BAD_STATEID) {
2609 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
2610 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
2611 if (!recover_lost_locks)
2612 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2613 } else if (status != NFS_OK) {
2615 nfs4_put_lock_state(prev);
2618 spin_lock(&state->state_lock);
2621 spin_unlock(&state->state_lock);
2622 nfs4_put_lock_state(prev);
2628 * nfs41_check_open_stateid - possibly free an open stateid
2630 * @state: NFSv4 state for an inode
2632 * Returns NFS_OK if recovery for this stateid is now finished.
2633 * Otherwise a negative NFS4ERR value is returned.
2635 static int nfs41_check_open_stateid(struct nfs4_state *state)
2637 struct nfs_server *server = NFS_SERVER(state->inode);
2638 nfs4_stateid *stateid = &state->open_stateid;
2639 struct rpc_cred *cred = state->owner->so_cred;
2642 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
2643 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2644 if (nfs4_have_delegation(state->inode, state->state))
2646 return -NFS4ERR_OPENMODE;
2648 return -NFS4ERR_BAD_STATEID;
2650 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
2651 trace_nfs4_test_open_stateid(state, NULL, status);
2652 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
2653 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2654 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2655 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2656 clear_bit(NFS_OPEN_STATE, &state->flags);
2657 stateid->type = NFS4_INVALID_STATEID_TYPE;
2659 if (status != NFS_OK)
2661 if (nfs_open_stateid_recover_openmode(state))
2662 return -NFS4ERR_OPENMODE;
2666 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2670 nfs41_check_delegation_stateid(state);
2671 status = nfs41_check_expired_locks(state);
2672 if (status != NFS_OK)
2674 status = nfs41_check_open_stateid(state);
2675 if (status != NFS_OK)
2676 status = nfs4_open_expired(sp, state);
2682 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2683 * fields corresponding to attributes that were used to store the verifier.
2684 * Make sure we clobber those fields in the later setattr call
2686 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2687 struct iattr *sattr, struct nfs4_label **label)
2689 const u32 *attrset = opendata->o_res.attrset;
2691 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2692 !(sattr->ia_valid & ATTR_ATIME_SET))
2693 sattr->ia_valid |= ATTR_ATIME;
2695 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2696 !(sattr->ia_valid & ATTR_MTIME_SET))
2697 sattr->ia_valid |= ATTR_MTIME;
2699 /* Except MODE, it seems harmless of setting twice. */
2700 if ((attrset[1] & FATTR4_WORD1_MODE))
2701 sattr->ia_valid &= ~ATTR_MODE;
2703 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2707 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2710 struct nfs_open_context *ctx)
2712 struct nfs4_state_owner *sp = opendata->owner;
2713 struct nfs_server *server = sp->so_server;
2714 struct dentry *dentry;
2715 struct nfs4_state *state;
2719 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2721 ret = _nfs4_proc_open(opendata);
2725 state = nfs4_opendata_to_nfs4_state(opendata);
2726 ret = PTR_ERR(state);
2729 if (server->caps & NFS_CAP_POSIX_LOCK)
2730 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2731 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2732 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
2734 dentry = opendata->dentry;
2735 if (d_really_is_negative(dentry)) {
2736 struct dentry *alias;
2738 alias = d_exact_alias(dentry, state->inode);
2740 alias = d_splice_alias(igrab(state->inode), dentry);
2741 /* d_splice_alias() can't fail here - it's a non-directory */
2744 ctx->dentry = dentry = alias;
2746 nfs_set_verifier(dentry,
2747 nfs_save_change_attribute(d_inode(opendata->dir)));
2750 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2755 if (d_inode(dentry) == state->inode) {
2756 nfs_inode_attach_open_context(ctx);
2757 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2758 nfs4_schedule_stateid_recovery(server, state);
2765 * Returns a referenced nfs4_state
2767 static int _nfs4_do_open(struct inode *dir,
2768 struct nfs_open_context *ctx,
2770 struct iattr *sattr,
2771 struct nfs4_label *label,
2774 struct nfs4_state_owner *sp;
2775 struct nfs4_state *state = NULL;
2776 struct nfs_server *server = NFS_SERVER(dir);
2777 struct nfs4_opendata *opendata;
2778 struct dentry *dentry = ctx->dentry;
2779 struct rpc_cred *cred = ctx->cred;
2780 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2781 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
2782 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
2783 struct nfs4_label *olabel = NULL;
2786 /* Protect against reboot recovery conflicts */
2788 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2790 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2793 status = nfs4_recover_expired_lease(server);
2795 goto err_put_state_owner;
2796 if (d_really_is_positive(dentry))
2797 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
2799 if (d_really_is_positive(dentry))
2800 claim = NFS4_OPEN_CLAIM_FH;
2801 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
2802 label, claim, GFP_KERNEL);
2803 if (opendata == NULL)
2804 goto err_put_state_owner;
2807 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2808 if (IS_ERR(olabel)) {
2809 status = PTR_ERR(olabel);
2810 goto err_opendata_put;
2814 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2815 if (!opendata->f_attr.mdsthreshold) {
2816 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2817 if (!opendata->f_attr.mdsthreshold)
2818 goto err_free_label;
2820 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
2822 if (d_really_is_positive(dentry))
2823 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
2825 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
2827 goto err_free_label;
2830 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
2831 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
2832 nfs4_exclusive_attrset(opendata, sattr, &label);
2834 * send create attributes which was not set by open
2835 * with an extra setattr.
2837 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2838 nfs_fattr_init(opendata->o_res.f_attr);
2839 status = nfs4_do_setattr(state->inode, cred,
2840 opendata->o_res.f_attr, sattr,
2841 ctx, label, olabel);
2843 nfs_setattr_update_inode(state->inode, sattr,
2844 opendata->o_res.f_attr);
2845 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2849 if (opened && opendata->file_created)
2850 *opened |= FILE_CREATED;
2852 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
2853 *ctx_th = opendata->f_attr.mdsthreshold;
2854 opendata->f_attr.mdsthreshold = NULL;
2857 nfs4_label_free(olabel);
2859 nfs4_opendata_put(opendata);
2860 nfs4_put_state_owner(sp);
2863 nfs4_label_free(olabel);
2865 nfs4_opendata_put(opendata);
2866 err_put_state_owner:
2867 nfs4_put_state_owner(sp);
2873 static struct nfs4_state *nfs4_do_open(struct inode *dir,
2874 struct nfs_open_context *ctx,
2876 struct iattr *sattr,
2877 struct nfs4_label *label,
2880 struct nfs_server *server = NFS_SERVER(dir);
2881 struct nfs4_exception exception = { };
2882 struct nfs4_state *res;
2886 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
2888 trace_nfs4_open_file(ctx, flags, status);
2891 /* NOTE: BAD_SEQID means the server and client disagree about the
2892 * book-keeping w.r.t. state-changing operations
2893 * (OPEN/CLOSE/LOCK/LOCKU...)
2894 * It is actually a sign of a bug on the client or on the server.
2896 * If we receive a BAD_SEQID error in the particular case of
2897 * doing an OPEN, we assume that nfs_increment_open_seqid() will
2898 * have unhashed the old state_owner for us, and that we can
2899 * therefore safely retry using a new one. We should still warn
2900 * the user though...
2902 if (status == -NFS4ERR_BAD_SEQID) {
2903 pr_warn_ratelimited("NFS: v4 server %s "
2904 " returned a bad sequence-id error!\n",
2905 NFS_SERVER(dir)->nfs_client->cl_hostname);
2906 exception.retry = 1;
2910 * BAD_STATEID on OPEN means that the server cancelled our
2911 * state before it received the OPEN_CONFIRM.
2912 * Recover by retrying the request as per the discussion
2913 * on Page 181 of RFC3530.
2915 if (status == -NFS4ERR_BAD_STATEID) {
2916 exception.retry = 1;
2919 if (status == -EAGAIN) {
2920 /* We must have found a delegation */
2921 exception.retry = 1;
2924 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2926 res = ERR_PTR(nfs4_handle_exception(server,
2927 status, &exception));
2928 } while (exception.retry);
2932 static int _nfs4_do_setattr(struct inode *inode,
2933 struct nfs_setattrargs *arg,
2934 struct nfs_setattrres *res,
2935 struct rpc_cred *cred,
2936 struct nfs_open_context *ctx)
2938 struct nfs_server *server = NFS_SERVER(inode);
2939 struct rpc_message msg = {
2940 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2945 struct rpc_cred *delegation_cred = NULL;
2946 unsigned long timestamp = jiffies;
2951 nfs_fattr_init(res->fattr);
2953 /* Servers should only apply open mode checks for file size changes */
2954 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
2955 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2957 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
2958 /* Use that stateid */
2959 } else if (truncate && ctx != NULL) {
2960 struct nfs_lock_context *l_ctx;
2961 if (!nfs4_valid_open_stateid(ctx->state))
2963 l_ctx = nfs_get_lock_context(ctx);
2965 return PTR_ERR(l_ctx);
2966 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
2967 &arg->stateid, &delegation_cred);
2968 nfs_put_lock_context(l_ctx);
2972 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
2973 if (delegation_cred)
2974 msg.rpc_cred = delegation_cred;
2976 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
2978 put_rpccred(delegation_cred);
2979 if (status == 0 && ctx != NULL)
2980 renew_lease(server, timestamp);
2981 trace_nfs4_setattr(inode, &arg->stateid, status);
2985 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2986 struct nfs_fattr *fattr, struct iattr *sattr,
2987 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
2988 struct nfs4_label *olabel)
2990 struct nfs_server *server = NFS_SERVER(inode);
2991 struct nfs4_state *state = ctx ? ctx->state : NULL;
2992 struct nfs_setattrargs arg = {
2993 .fh = NFS_FH(inode),
2996 .bitmask = server->attr_bitmask,
2999 struct nfs_setattrres res = {
3004 struct nfs4_exception exception = {
3007 .stateid = &arg.stateid,
3011 arg.bitmask = nfs4_bitmask(server, ilabel);
3013 arg.bitmask = nfs4_bitmask(server, olabel);
3016 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
3018 case -NFS4ERR_OPENMODE:
3019 if (!(sattr->ia_valid & ATTR_SIZE)) {
3020 pr_warn_once("NFSv4: server %s is incorrectly "
3021 "applying open mode checks to "
3022 "a SETATTR that is not "
3023 "changing file size.\n",
3024 server->nfs_client->cl_hostname);
3026 if (state && !(state->state & FMODE_WRITE)) {
3028 if (sattr->ia_valid & ATTR_OPEN)
3033 err = nfs4_handle_exception(server, err, &exception);
3034 } while (exception.retry);
3040 nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3042 if (inode == NULL || !nfs_have_layout(inode))
3045 return pnfs_wait_on_layoutreturn(inode, task);
3048 struct nfs4_closedata {
3049 struct inode *inode;
3050 struct nfs4_state *state;
3051 struct nfs_closeargs arg;
3052 struct nfs_closeres res;
3054 struct nfs4_layoutreturn_args arg;
3055 struct nfs4_layoutreturn_res res;
3056 struct nfs4_xdr_opaque_data ld_private;
3060 struct nfs_fattr fattr;
3061 unsigned long timestamp;
3064 static void nfs4_free_closedata(void *data)
3066 struct nfs4_closedata *calldata = data;
3067 struct nfs4_state_owner *sp = calldata->state->owner;
3068 struct super_block *sb = calldata->state->inode->i_sb;
3070 if (calldata->lr.roc)
3071 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3072 calldata->res.lr_ret);
3073 nfs4_put_open_state(calldata->state);
3074 nfs_free_seqid(calldata->arg.seqid);
3075 nfs4_put_state_owner(sp);
3076 nfs_sb_deactive(sb);
3080 static void nfs4_close_done(struct rpc_task *task, void *data)
3082 struct nfs4_closedata *calldata = data;
3083 struct nfs4_state *state = calldata->state;
3084 struct nfs_server *server = NFS_SERVER(calldata->inode);
3085 nfs4_stateid *res_stateid = NULL;
3087 dprintk("%s: begin!\n", __func__);
3088 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3090 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
3092 /* Handle Layoutreturn errors */
3093 if (calldata->arg.lr_args && task->tk_status != 0) {
3094 switch (calldata->res.lr_ret) {
3096 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3099 calldata->arg.lr_args = NULL;
3100 calldata->res.lr_res = NULL;
3102 case -NFS4ERR_ADMIN_REVOKED:
3103 case -NFS4ERR_DELEG_REVOKED:
3104 case -NFS4ERR_EXPIRED:
3105 case -NFS4ERR_BAD_STATEID:
3106 case -NFS4ERR_OLD_STATEID:
3107 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3108 case -NFS4ERR_WRONG_CRED:
3109 calldata->arg.lr_args = NULL;
3110 calldata->res.lr_res = NULL;
3111 calldata->res.lr_ret = 0;
3112 rpc_restart_call_prepare(task);
3117 /* hmm. we are done with the inode, and in the process of freeing
3118 * the state_owner. we keep this around to process errors
3120 switch (task->tk_status) {
3122 res_stateid = &calldata->res.stateid;
3123 renew_lease(server, calldata->timestamp);
3125 case -NFS4ERR_ACCESS:
3126 if (calldata->arg.bitmask != NULL) {
3127 calldata->arg.bitmask = NULL;
3128 calldata->res.fattr = NULL;
3129 task->tk_status = 0;
3130 rpc_restart_call_prepare(task);
3135 case -NFS4ERR_ADMIN_REVOKED:
3136 case -NFS4ERR_STALE_STATEID:
3137 case -NFS4ERR_EXPIRED:
3138 nfs4_free_revoked_stateid(server,
3139 &calldata->arg.stateid,
3140 task->tk_msg.rpc_cred);
3141 case -NFS4ERR_OLD_STATEID:
3142 case -NFS4ERR_BAD_STATEID:
3143 if (!nfs4_stateid_match(&calldata->arg.stateid,
3144 &state->open_stateid)) {
3145 rpc_restart_call_prepare(task);
3148 if (calldata->arg.fmode == 0)
3151 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
3152 rpc_restart_call_prepare(task);
3156 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3157 res_stateid, calldata->arg.fmode);
3159 nfs_release_seqid(calldata->arg.seqid);
3160 nfs_refresh_inode(calldata->inode, &calldata->fattr);
3161 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
3164 static void nfs4_close_prepare(struct rpc_task *task, void *data)
3166 struct nfs4_closedata *calldata = data;
3167 struct nfs4_state *state = calldata->state;
3168 struct inode *inode = calldata->inode;
3169 bool is_rdonly, is_wronly, is_rdwr;
3172 dprintk("%s: begin!\n", __func__);
3173 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3176 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
3177 spin_lock(&state->owner->so_lock);
3178 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3179 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3180 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
3181 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
3182 /* Calculate the change in open mode */
3183 calldata->arg.fmode = 0;
3184 if (state->n_rdwr == 0) {
3185 if (state->n_rdonly == 0)
3186 call_close |= is_rdonly;
3188 calldata->arg.fmode |= FMODE_READ;
3189 if (state->n_wronly == 0)
3190 call_close |= is_wronly;
3192 calldata->arg.fmode |= FMODE_WRITE;
3193 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3194 call_close |= is_rdwr;
3196 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3198 if (!nfs4_valid_open_stateid(state) ||
3199 test_bit(NFS_OPEN_STATE, &state->flags) == 0)
3201 spin_unlock(&state->owner->so_lock);
3204 /* Note: exit _without_ calling nfs4_close_done */
3208 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
3209 nfs_release_seqid(calldata->arg.seqid);
3213 if (calldata->arg.fmode == 0)
3214 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
3216 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
3217 /* Close-to-open cache consistency revalidation */
3218 if (!nfs4_have_delegation(inode, FMODE_READ))
3219 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3221 calldata->arg.bitmask = NULL;
3224 calldata->arg.share_access =
3225 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3226 calldata->arg.fmode, 0);
3228 if (calldata->res.fattr == NULL)
3229 calldata->arg.bitmask = NULL;
3230 else if (calldata->arg.bitmask == NULL)
3231 calldata->res.fattr = NULL;
3232 calldata->timestamp = jiffies;
3233 if (nfs4_setup_sequence(NFS_SERVER(inode),
3234 &calldata->arg.seq_args,
3235 &calldata->res.seq_res,
3237 nfs_release_seqid(calldata->arg.seqid);
3238 dprintk("%s: done!\n", __func__);
3241 task->tk_action = NULL;
3243 nfs4_sequence_done(task, &calldata->res.seq_res);
3246 static const struct rpc_call_ops nfs4_close_ops = {
3247 .rpc_call_prepare = nfs4_close_prepare,
3248 .rpc_call_done = nfs4_close_done,
3249 .rpc_release = nfs4_free_closedata,
3253 * It is possible for data to be read/written from a mem-mapped file
3254 * after the sys_close call (which hits the vfs layer as a flush).
3255 * This means that we can't safely call nfsv4 close on a file until
3256 * the inode is cleared. This in turn means that we are not good
3257 * NFSv4 citizens - we do not indicate to the server to update the file's
3258 * share state even when we are done with one of the three share
3259 * stateid's in the inode.
3261 * NOTE: Caller must be holding the sp->so_owner semaphore!
3263 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
3265 struct nfs_server *server = NFS_SERVER(state->inode);
3266 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
3267 struct nfs4_closedata *calldata;
3268 struct nfs4_state_owner *sp = state->owner;
3269 struct rpc_task *task;
3270 struct rpc_message msg = {
3271 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3272 .rpc_cred = state->owner->so_cred,
3274 struct rpc_task_setup task_setup_data = {
3275 .rpc_client = server->client,
3276 .rpc_message = &msg,
3277 .callback_ops = &nfs4_close_ops,
3278 .workqueue = nfsiod_workqueue,
3279 .flags = RPC_TASK_ASYNC,
3281 int status = -ENOMEM;
3283 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3284 &task_setup_data.rpc_client, &msg);
3286 calldata = kzalloc(sizeof(*calldata), gfp_mask);
3287 if (calldata == NULL)
3289 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
3290 calldata->inode = state->inode;
3291 calldata->state = state;
3292 calldata->arg.fh = NFS_FH(state->inode);
3293 /* Serialization for the sequence id */
3294 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3295 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
3296 if (IS_ERR(calldata->arg.seqid))
3297 goto out_free_calldata;
3298 nfs_fattr_init(&calldata->fattr);
3299 calldata->arg.fmode = 0;
3300 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
3301 calldata->res.fattr = &calldata->fattr;
3302 calldata->res.seqid = calldata->arg.seqid;
3303 calldata->res.server = server;
3304 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3305 calldata->lr.roc = pnfs_roc(state->inode,
3306 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3307 if (calldata->lr.roc) {
3308 calldata->arg.lr_args = &calldata->lr.arg;
3309 calldata->res.lr_res = &calldata->lr.res;
3311 nfs_sb_active(calldata->inode->i_sb);
3313 msg.rpc_argp = &calldata->arg;
3314 msg.rpc_resp = &calldata->res;
3315 task_setup_data.callback_data = calldata;
3316 task = rpc_run_task(&task_setup_data);
3318 return PTR_ERR(task);
3321 status = rpc_wait_for_completion_task(task);
3327 nfs4_put_open_state(state);
3328 nfs4_put_state_owner(sp);
3332 static struct inode *
3333 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3334 int open_flags, struct iattr *attr, int *opened)
3336 struct nfs4_state *state;
3337 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3339 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
3341 /* Protect against concurrent sillydeletes */
3342 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
3344 nfs4_label_release_security(label);
3347 return ERR_CAST(state);
3348 return state->inode;
3351 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
3353 if (ctx->state == NULL)
3356 nfs4_close_sync(ctx->state, ctx->mode);
3358 nfs4_close_state(ctx->state, ctx->mode);
3361 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3362 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3363 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
3365 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3367 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
3368 struct nfs4_server_caps_arg args = {
3372 struct nfs4_server_caps_res res = {};
3373 struct rpc_message msg = {
3374 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
3380 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3381 FATTR4_WORD0_FH_EXPIRE_TYPE |
3382 FATTR4_WORD0_LINK_SUPPORT |
3383 FATTR4_WORD0_SYMLINK_SUPPORT |
3384 FATTR4_WORD0_ACLSUPPORT;
3386 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3388 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3390 /* Sanity check the server answers */
3391 switch (minorversion) {
3393 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3394 res.attr_bitmask[2] = 0;
3397 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3400 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3402 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
3403 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3404 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3405 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3406 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
3407 NFS_CAP_CTIME|NFS_CAP_MTIME|
3408 NFS_CAP_SECURITY_LABEL);
3409 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3410 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3411 server->caps |= NFS_CAP_ACLS;
3412 if (res.has_links != 0)
3413 server->caps |= NFS_CAP_HARDLINKS;
3414 if (res.has_symlinks != 0)
3415 server->caps |= NFS_CAP_SYMLINKS;
3416 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3417 server->caps |= NFS_CAP_FILEID;
3418 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3419 server->caps |= NFS_CAP_MODE;
3420 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3421 server->caps |= NFS_CAP_NLINK;
3422 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3423 server->caps |= NFS_CAP_OWNER;
3424 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3425 server->caps |= NFS_CAP_OWNER_GROUP;
3426 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3427 server->caps |= NFS_CAP_ATIME;
3428 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3429 server->caps |= NFS_CAP_CTIME;
3430 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3431 server->caps |= NFS_CAP_MTIME;
3432 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3433 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3434 server->caps |= NFS_CAP_SECURITY_LABEL;
3436 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3437 sizeof(server->attr_bitmask));
3438 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3440 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3441 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3442 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
3443 server->cache_consistency_bitmask[2] = 0;
3444 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3445 sizeof(server->exclcreat_bitmask));
3446 server->acl_bitmask = res.acl_bitmask;
3447 server->fh_expire_type = res.fh_expire_type;
3453 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3455 struct nfs4_exception exception = { };
3458 err = nfs4_handle_exception(server,
3459 _nfs4_server_capabilities(server, fhandle),
3461 } while (exception.retry);
3465 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3466 struct nfs_fsinfo *info)
3469 struct nfs4_lookup_root_arg args = {
3472 struct nfs4_lookup_res res = {
3474 .fattr = info->fattr,
3477 struct rpc_message msg = {
3478 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3483 bitmask[0] = nfs4_fattr_bitmap[0];
3484 bitmask[1] = nfs4_fattr_bitmap[1];
3486 * Process the label in the upcoming getfattr
3488 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3490 nfs_fattr_init(info->fattr);
3491 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3494 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3495 struct nfs_fsinfo *info)
3497 struct nfs4_exception exception = { };
3500 err = _nfs4_lookup_root(server, fhandle, info);
3501 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
3504 case -NFS4ERR_WRONGSEC:
3507 err = nfs4_handle_exception(server, err, &exception);
3509 } while (exception.retry);
3514 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3515 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3517 struct rpc_auth_create_args auth_args = {
3518 .pseudoflavor = flavor,
3520 struct rpc_auth *auth;
3523 auth = rpcauth_create(&auth_args, server->client);
3528 ret = nfs4_lookup_root(server, fhandle, info);
3534 * Retry pseudoroot lookup with various security flavors. We do this when:
3536 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3537 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3539 * Returns zero on success, or a negative NFS4ERR value, or a
3540 * negative errno value.
3542 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3543 struct nfs_fsinfo *info)
3545 /* Per 3530bis 15.33.5 */
3546 static const rpc_authflavor_t flav_array[] = {
3550 RPC_AUTH_UNIX, /* courtesy */
3553 int status = -EPERM;
3556 if (server->auth_info.flavor_len > 0) {
3557 /* try each flavor specified by user */
3558 for (i = 0; i < server->auth_info.flavor_len; i++) {
3559 status = nfs4_lookup_root_sec(server, fhandle, info,
3560 server->auth_info.flavors[i]);
3561 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3566 /* no flavors specified by user, try default list */
3567 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3568 status = nfs4_lookup_root_sec(server, fhandle, info,
3570 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3577 * -EACCESS could mean that the user doesn't have correct permissions
3578 * to access the mount. It could also mean that we tried to mount
3579 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3580 * existing mount programs don't handle -EACCES very well so it should
3581 * be mapped to -EPERM instead.
3583 if (status == -EACCES)
3589 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3590 * @server: initialized nfs_server handle
3591 * @fhandle: we fill in the pseudo-fs root file handle
3592 * @info: we fill in an FSINFO struct
3593 * @auth_probe: probe the auth flavours
3595 * Returns zero on success, or a negative errno.
3597 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
3598 struct nfs_fsinfo *info,
3604 status = nfs4_lookup_root(server, fhandle, info);
3606 if (auth_probe || status == NFS4ERR_WRONGSEC)
3607 status = server->nfs_client->cl_mvops->find_root_sec(server,
3611 status = nfs4_server_capabilities(server, fhandle);
3613 status = nfs4_do_fsinfo(server, fhandle, info);
3615 return nfs4_map_errors(status);
3618 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3619 struct nfs_fsinfo *info)
3622 struct nfs_fattr *fattr = info->fattr;
3623 struct nfs4_label *label = NULL;
3625 error = nfs4_server_capabilities(server, mntfh);
3627 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3631 label = nfs4_label_alloc(server, GFP_KERNEL);
3633 return PTR_ERR(label);
3635 error = nfs4_proc_getattr(server, mntfh, fattr, label);
3637 dprintk("nfs4_get_root: getattr error = %d\n", -error);
3638 goto err_free_label;
3641 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3642 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3643 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3646 nfs4_label_free(label);
3652 * Get locations and (maybe) other attributes of a referral.
3653 * Note that we'll actually follow the referral later when
3654 * we detect fsid mismatch in inode revalidation
3656 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3657 const struct qstr *name, struct nfs_fattr *fattr,
3658 struct nfs_fh *fhandle)
3660 int status = -ENOMEM;
3661 struct page *page = NULL;
3662 struct nfs4_fs_locations *locations = NULL;
3664 page = alloc_page(GFP_KERNEL);
3667 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3668 if (locations == NULL)
3671 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
3676 * If the fsid didn't change, this is a migration event, not a
3677 * referral. Cause us to drop into the exception handler, which
3678 * will kick off migration recovery.
3680 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
3681 dprintk("%s: server did not return a different fsid for"
3682 " a referral at %s\n", __func__, name->name);
3683 status = -NFS4ERR_MOVED;
3686 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3687 nfs_fixup_referral_attributes(&locations->fattr);
3689 /* replace the lookup nfs_fattr with the locations nfs_fattr */
3690 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
3691 memset(fhandle, 0, sizeof(struct nfs_fh));
3699 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3700 struct nfs_fattr *fattr, struct nfs4_label *label)
3702 struct nfs4_getattr_arg args = {
3704 .bitmask = server->attr_bitmask,
3706 struct nfs4_getattr_res res = {
3711 struct rpc_message msg = {
3712 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3717 args.bitmask = nfs4_bitmask(server, label);
3719 nfs_fattr_init(fattr);
3720 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3723 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3724 struct nfs_fattr *fattr, struct nfs4_label *label)
3726 struct nfs4_exception exception = { };
3729 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3730 trace_nfs4_getattr(server, fhandle, fattr, err);
3731 err = nfs4_handle_exception(server, err,
3733 } while (exception.retry);
3738 * The file is not closed if it is opened due to the a request to change
3739 * the size of the file. The open call will not be needed once the
3740 * VFS layer lookup-intents are implemented.
3742 * Close is called when the inode is destroyed.
3743 * If we haven't opened the file for O_WRONLY, we
3744 * need to in the size_change case to obtain a stateid.
3747 * Because OPEN is always done by name in nfsv4, it is
3748 * possible that we opened a different file by the same
3749 * name. We can recognize this race condition, but we
3750 * can't do anything about it besides returning an error.
3752 * This will be fixed with VFS changes (lookup-intent).
3755 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3756 struct iattr *sattr)
3758 struct inode *inode = d_inode(dentry);
3759 struct rpc_cred *cred = NULL;
3760 struct nfs_open_context *ctx = NULL;
3761 struct nfs4_label *label = NULL;
3764 if (pnfs_ld_layoutret_on_setattr(inode) &&
3765 sattr->ia_valid & ATTR_SIZE &&
3766 sattr->ia_size < i_size_read(inode))
3767 pnfs_commit_and_return_layout(inode);
3769 nfs_fattr_init(fattr);
3771 /* Deal with open(O_TRUNC) */
3772 if (sattr->ia_valid & ATTR_OPEN)
3773 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
3775 /* Optimization: if the end result is no change, don't RPC */
3776 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
3779 /* Search for an existing open(O_WRITE) file */
3780 if (sattr->ia_valid & ATTR_FILE) {
3782 ctx = nfs_file_open_context(sattr->ia_file);
3787 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3789 return PTR_ERR(label);
3791 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
3793 nfs_setattr_update_inode(inode, sattr, fattr);
3794 nfs_setsecurity(inode, fattr, label);
3796 nfs4_label_free(label);
3800 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3801 const struct qstr *name, struct nfs_fh *fhandle,
3802 struct nfs_fattr *fattr, struct nfs4_label *label)
3804 struct nfs_server *server = NFS_SERVER(dir);
3806 struct nfs4_lookup_arg args = {
3807 .bitmask = server->attr_bitmask,
3808 .dir_fh = NFS_FH(dir),
3811 struct nfs4_lookup_res res = {
3817 struct rpc_message msg = {
3818 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3823 args.bitmask = nfs4_bitmask(server, label);
3825 nfs_fattr_init(fattr);
3827 dprintk("NFS call lookup %s\n", name->name);
3828 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
3829 dprintk("NFS reply lookup: %d\n", status);
3833 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
3835 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
3836 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
3837 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3841 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3842 const struct qstr *name, struct nfs_fh *fhandle,
3843 struct nfs_fattr *fattr, struct nfs4_label *label)
3845 struct nfs4_exception exception = { };
3846 struct rpc_clnt *client = *clnt;
3849 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
3850 trace_nfs4_lookup(dir, name, err);
3852 case -NFS4ERR_BADNAME:
3855 case -NFS4ERR_MOVED:
3856 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
3857 if (err == -NFS4ERR_MOVED)
3858 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3860 case -NFS4ERR_WRONGSEC:
3862 if (client != *clnt)
3864 client = nfs4_negotiate_security(client, dir, name);
3866 return PTR_ERR(client);
3868 exception.retry = 1;
3871 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3873 } while (exception.retry);
3878 else if (client != *clnt)
3879 rpc_shutdown_client(client);
3884 static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
3885 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3886 struct nfs4_label *label)
3889 struct rpc_clnt *client = NFS_CLIENT(dir);
3891 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
3892 if (client != NFS_CLIENT(dir)) {
3893 rpc_shutdown_client(client);
3894 nfs_fixup_secinfo_attributes(fattr);
3900 nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
3901 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3903 struct rpc_clnt *client = NFS_CLIENT(dir);
3906 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
3908 return ERR_PTR(status);
3909 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
3912 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3914 struct nfs_server *server = NFS_SERVER(inode);
3915 struct nfs4_accessargs args = {
3916 .fh = NFS_FH(inode),
3917 .bitmask = server->cache_consistency_bitmask,
3919 struct nfs4_accessres res = {
3922 struct rpc_message msg = {
3923 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3926 .rpc_cred = entry->cred,
3928 int mode = entry->mask;
3932 * Determine which access bits we want to ask for...
3934 if (mode & MAY_READ)
3935 args.access |= NFS4_ACCESS_READ;
3936 if (S_ISDIR(inode->i_mode)) {
3937 if (mode & MAY_WRITE)
3938 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3939 if (mode & MAY_EXEC)
3940 args.access |= NFS4_ACCESS_LOOKUP;
3942 if (mode & MAY_WRITE)
3943 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3944 if (mode & MAY_EXEC)
3945 args.access |= NFS4_ACCESS_EXECUTE;
3948 res.fattr = nfs_alloc_fattr();
3949 if (res.fattr == NULL)
3952 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3954 nfs_access_set_mask(entry, res.access);
3955 nfs_refresh_inode(inode, res.fattr);
3957 nfs_free_fattr(res.fattr);
3961 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3963 struct nfs4_exception exception = { };
3966 err = _nfs4_proc_access(inode, entry);
3967 trace_nfs4_access(inode, err);
3968 err = nfs4_handle_exception(NFS_SERVER(inode), err,
3970 } while (exception.retry);
3975 * TODO: For the time being, we don't try to get any attributes
3976 * along with any of the zero-copy operations READ, READDIR,
3979 * In the case of the first three, we want to put the GETATTR
3980 * after the read-type operation -- this is because it is hard
3981 * to predict the length of a GETATTR response in v4, and thus
3982 * align the READ data correctly. This means that the GETATTR
3983 * may end up partially falling into the page cache, and we should
3984 * shift it into the 'tail' of the xdr_buf before processing.
3985 * To do this efficiently, we need to know the total length
3986 * of data received, which doesn't seem to be available outside
3989 * In the case of WRITE, we also want to put the GETATTR after
3990 * the operation -- in this case because we want to make sure
3991 * we get the post-operation mtime and size.
3993 * Both of these changes to the XDR layer would in fact be quite
3994 * minor, but I decided to leave them for a subsequent patch.
3996 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3997 unsigned int pgbase, unsigned int pglen)
3999 struct nfs4_readlink args = {
4000 .fh = NFS_FH(inode),
4005 struct nfs4_readlink_res res;
4006 struct rpc_message msg = {
4007 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4012 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
4015 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4016 unsigned int pgbase, unsigned int pglen)
4018 struct nfs4_exception exception = { };
4021 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4022 trace_nfs4_readlink(inode, err);
4023 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4025 } while (exception.retry);
4030 * This is just for mknod. open(O_CREAT) will always do ->open_context().
4033 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
4036 struct nfs_server *server = NFS_SERVER(dir);
4037 struct nfs4_label l, *ilabel = NULL;
4038 struct nfs_open_context *ctx;
4039 struct nfs4_state *state;
4042 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
4044 return PTR_ERR(ctx);
4046 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4048 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4049 sattr->ia_mode &= ~current_umask();
4050 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
4051 if (IS_ERR(state)) {
4052 status = PTR_ERR(state);
4056 nfs4_label_release_security(ilabel);
4057 put_nfs_open_context(ctx);
4061 static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
4063 struct nfs_server *server = NFS_SERVER(dir);
4064 struct nfs_removeargs args = {
4068 struct nfs_removeres res = {
4071 struct rpc_message msg = {
4072 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4078 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
4080 update_changeattr(dir, &res.cinfo);
4084 static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
4086 struct nfs4_exception exception = { };
4089 err = _nfs4_proc_remove(dir, name);
4090 trace_nfs4_remove(dir, name, err);
4091 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4093 } while (exception.retry);
4097 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
4099 struct nfs_server *server = NFS_SERVER(dir);
4100 struct nfs_removeargs *args = msg->rpc_argp;
4101 struct nfs_removeres *res = msg->rpc_resp;
4103 res->server = server;
4104 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
4105 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
4107 nfs_fattr_init(res->dir_attr);
4110 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4112 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb),
4113 &data->args.seq_args,
4118 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
4120 struct nfs_unlinkdata *data = task->tk_calldata;
4121 struct nfs_removeres *res = &data->res;
4123 if (!nfs4_sequence_done(task, &res->seq_res))
4125 if (nfs4_async_handle_error(task, res->server, NULL,
4126 &data->timeout) == -EAGAIN)
4128 if (task->tk_status == 0)
4129 update_changeattr(dir, &res->cinfo);
4133 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4135 struct nfs_server *server = NFS_SERVER(dir);
4136 struct nfs_renameargs *arg = msg->rpc_argp;
4137 struct nfs_renameres *res = msg->rpc_resp;
4139 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
4140 res->server = server;
4141 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
4144 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4146 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
4147 &data->args.seq_args,
4152 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4153 struct inode *new_dir)
4155 struct nfs_renamedata *data = task->tk_calldata;
4156 struct nfs_renameres *res = &data->res;
4158 if (!nfs4_sequence_done(task, &res->seq_res))
4160 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
4163 if (task->tk_status == 0) {
4164 update_changeattr(old_dir, &res->old_cinfo);
4165 if (new_dir != old_dir)
4166 update_changeattr(new_dir, &res->new_cinfo);
4171 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4173 struct nfs_server *server = NFS_SERVER(inode);
4174 struct nfs4_link_arg arg = {
4175 .fh = NFS_FH(inode),
4176 .dir_fh = NFS_FH(dir),
4178 .bitmask = server->attr_bitmask,
4180 struct nfs4_link_res res = {
4184 struct rpc_message msg = {
4185 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4189 int status = -ENOMEM;
4191 res.fattr = nfs_alloc_fattr();
4192 if (res.fattr == NULL)
4195 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4196 if (IS_ERR(res.label)) {
4197 status = PTR_ERR(res.label);
4200 arg.bitmask = nfs4_bitmask(server, res.label);
4202 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4204 update_changeattr(dir, &res.cinfo);
4205 status = nfs_post_op_update_inode(inode, res.fattr);
4207 nfs_setsecurity(inode, res.fattr, res.label);
4211 nfs4_label_free(res.label);
4214 nfs_free_fattr(res.fattr);
4218 static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4220 struct nfs4_exception exception = { };
4223 err = nfs4_handle_exception(NFS_SERVER(inode),
4224 _nfs4_proc_link(inode, dir, name),
4226 } while (exception.retry);
4230 struct nfs4_createdata {
4231 struct rpc_message msg;
4232 struct nfs4_create_arg arg;
4233 struct nfs4_create_res res;
4235 struct nfs_fattr fattr;
4236 struct nfs4_label *label;
4239 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
4240 const struct qstr *name, struct iattr *sattr, u32 ftype)
4242 struct nfs4_createdata *data;
4244 data = kzalloc(sizeof(*data), GFP_KERNEL);
4246 struct nfs_server *server = NFS_SERVER(dir);
4248 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4249 if (IS_ERR(data->label))
4252 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4253 data->msg.rpc_argp = &data->arg;
4254 data->msg.rpc_resp = &data->res;
4255 data->arg.dir_fh = NFS_FH(dir);
4256 data->arg.server = server;
4257 data->arg.name = name;
4258 data->arg.attrs = sattr;
4259 data->arg.ftype = ftype;
4260 data->arg.bitmask = nfs4_bitmask(server, data->label);
4261 data->arg.umask = current_umask();
4262 data->res.server = server;
4263 data->res.fh = &data->fh;
4264 data->res.fattr = &data->fattr;
4265 data->res.label = data->label;
4266 nfs_fattr_init(data->res.fattr);
4274 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4276 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
4277 &data->arg.seq_args, &data->res.seq_res, 1);
4279 update_changeattr(dir, &data->res.dir_cinfo);
4280 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
4285 static void nfs4_free_createdata(struct nfs4_createdata *data)
4287 nfs4_label_free(data->label);
4291 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4292 struct page *page, unsigned int len, struct iattr *sattr,
4293 struct nfs4_label *label)
4295 struct nfs4_createdata *data;
4296 int status = -ENAMETOOLONG;
4298 if (len > NFS4_MAXPATHLEN)
4302 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4306 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4307 data->arg.u.symlink.pages = &page;
4308 data->arg.u.symlink.len = len;
4309 data->arg.label = label;
4311 status = nfs4_do_create(dir, dentry, data);
4313 nfs4_free_createdata(data);
4318 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4319 struct page *page, unsigned int len, struct iattr *sattr)
4321 struct nfs4_exception exception = { };
4322 struct nfs4_label l, *label = NULL;
4325 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4328 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4329 trace_nfs4_symlink(dir, &dentry->d_name, err);
4330 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4332 } while (exception.retry);
4334 nfs4_label_release_security(label);
4338 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4339 struct iattr *sattr, struct nfs4_label *label)
4341 struct nfs4_createdata *data;
4342 int status = -ENOMEM;
4344 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4348 data->arg.label = label;
4349 status = nfs4_do_create(dir, dentry, data);
4351 nfs4_free_createdata(data);
4356 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4357 struct iattr *sattr)
4359 struct nfs_server *server = NFS_SERVER(dir);
4360 struct nfs4_exception exception = { };
4361 struct nfs4_label l, *label = NULL;
4364 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4366 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4367 sattr->ia_mode &= ~current_umask();
4369 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4370 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4371 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4373 } while (exception.retry);
4374 nfs4_label_release_security(label);
4379 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
4380 u64 cookie, struct page **pages, unsigned int count, int plus)
4382 struct inode *dir = d_inode(dentry);
4383 struct nfs4_readdir_arg args = {
4388 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
4391 struct nfs4_readdir_res res;
4392 struct rpc_message msg = {
4393 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4400 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4402 (unsigned long long)cookie);
4403 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
4404 res.pgbase = args.pgbase;
4405 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4407 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
4408 status += args.pgbase;
4411 nfs_invalidate_atime(dir);
4413 dprintk("%s: returns %d\n", __func__, status);
4417 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
4418 u64 cookie, struct page **pages, unsigned int count, int plus)
4420 struct nfs4_exception exception = { };
4423 err = _nfs4_proc_readdir(dentry, cred, cookie,
4424 pages, count, plus);
4425 trace_nfs4_readdir(d_inode(dentry), err);
4426 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
4428 } while (exception.retry);
4432 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4433 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
4435 struct nfs4_createdata *data;
4436 int mode = sattr->ia_mode;
4437 int status = -ENOMEM;
4439 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4444 data->arg.ftype = NF4FIFO;
4445 else if (S_ISBLK(mode)) {
4446 data->arg.ftype = NF4BLK;
4447 data->arg.u.device.specdata1 = MAJOR(rdev);
4448 data->arg.u.device.specdata2 = MINOR(rdev);
4450 else if (S_ISCHR(mode)) {
4451 data->arg.ftype = NF4CHR;
4452 data->arg.u.device.specdata1 = MAJOR(rdev);
4453 data->arg.u.device.specdata2 = MINOR(rdev);
4454 } else if (!S_ISSOCK(mode)) {
4459 data->arg.label = label;
4460 status = nfs4_do_create(dir, dentry, data);
4462 nfs4_free_createdata(data);
4467 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4468 struct iattr *sattr, dev_t rdev)
4470 struct nfs_server *server = NFS_SERVER(dir);
4471 struct nfs4_exception exception = { };
4472 struct nfs4_label l, *label = NULL;
4475 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4477 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4478 sattr->ia_mode &= ~current_umask();
4480 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4481 trace_nfs4_mknod(dir, &dentry->d_name, err);
4482 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4484 } while (exception.retry);
4486 nfs4_label_release_security(label);
4491 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4492 struct nfs_fsstat *fsstat)
4494 struct nfs4_statfs_arg args = {
4496 .bitmask = server->attr_bitmask,
4498 struct nfs4_statfs_res res = {
4501 struct rpc_message msg = {
4502 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4507 nfs_fattr_init(fsstat->fattr);
4508 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4511 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4513 struct nfs4_exception exception = { };
4516 err = nfs4_handle_exception(server,
4517 _nfs4_proc_statfs(server, fhandle, fsstat),
4519 } while (exception.retry);
4523 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4524 struct nfs_fsinfo *fsinfo)
4526 struct nfs4_fsinfo_arg args = {
4528 .bitmask = server->attr_bitmask,
4530 struct nfs4_fsinfo_res res = {
4533 struct rpc_message msg = {
4534 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4539 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4542 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4544 struct nfs4_exception exception = { };
4545 unsigned long now = jiffies;
4549 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
4550 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
4552 nfs4_set_lease_period(server->nfs_client,
4553 fsinfo->lease_time * HZ,
4557 err = nfs4_handle_exception(server, err, &exception);
4558 } while (exception.retry);
4562 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4566 nfs_fattr_init(fsinfo->fattr);
4567 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
4569 /* block layout checks this! */
4570 server->pnfs_blksize = fsinfo->blksize;
4571 set_pnfs_layoutdriver(server, fhandle, fsinfo);
4577 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4578 struct nfs_pathconf *pathconf)
4580 struct nfs4_pathconf_arg args = {
4582 .bitmask = server->attr_bitmask,
4584 struct nfs4_pathconf_res res = {
4585 .pathconf = pathconf,
4587 struct rpc_message msg = {
4588 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4593 /* None of the pathconf attributes are mandatory to implement */
4594 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4595 memset(pathconf, 0, sizeof(*pathconf));
4599 nfs_fattr_init(pathconf->fattr);
4600 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4603 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4604 struct nfs_pathconf *pathconf)
4606 struct nfs4_exception exception = { };
4610 err = nfs4_handle_exception(server,
4611 _nfs4_proc_pathconf(server, fhandle, pathconf),
4613 } while (exception.retry);
4617 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
4618 const struct nfs_open_context *ctx,
4619 const struct nfs_lock_context *l_ctx,
4622 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
4624 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4626 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4627 const struct nfs_open_context *ctx,
4628 const struct nfs_lock_context *l_ctx,
4631 nfs4_stateid current_stateid;
4633 /* If the current stateid represents a lost lock, then exit */
4634 if (nfs4_set_rw_stateid(¤t_stateid, ctx, l_ctx, fmode) == -EIO)
4636 return nfs4_stateid_match(stateid, ¤t_stateid);
4639 static bool nfs4_error_stateid_expired(int err)
4642 case -NFS4ERR_DELEG_REVOKED:
4643 case -NFS4ERR_ADMIN_REVOKED:
4644 case -NFS4ERR_BAD_STATEID:
4645 case -NFS4ERR_STALE_STATEID:
4646 case -NFS4ERR_OLD_STATEID:
4647 case -NFS4ERR_OPENMODE:
4648 case -NFS4ERR_EXPIRED:
4654 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
4656 struct nfs_server *server = NFS_SERVER(hdr->inode);
4658 trace_nfs4_read(hdr, task->tk_status);
4659 if (task->tk_status < 0) {
4660 struct nfs4_exception exception = {
4661 .inode = hdr->inode,
4662 .state = hdr->args.context->state,
4663 .stateid = &hdr->args.stateid,
4665 task->tk_status = nfs4_async_handle_exception(task,
4666 server, task->tk_status, &exception);
4667 if (exception.retry) {
4668 rpc_restart_call_prepare(task);
4673 if (task->tk_status > 0)
4674 renew_lease(server, hdr->timestamp);
4678 static bool nfs4_read_stateid_changed(struct rpc_task *task,
4679 struct nfs_pgio_args *args)
4682 if (!nfs4_error_stateid_expired(task->tk_status) ||
4683 nfs4_stateid_is_current(&args->stateid,
4688 rpc_restart_call_prepare(task);
4692 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
4695 dprintk("--> %s\n", __func__);
4697 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
4699 if (nfs4_read_stateid_changed(task, &hdr->args))
4701 if (task->tk_status > 0)
4702 nfs_invalidate_atime(hdr->inode);
4703 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4704 nfs4_read_done_cb(task, hdr);
4707 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4708 struct rpc_message *msg)
4710 hdr->timestamp = jiffies;
4711 if (!hdr->pgio_done_cb)
4712 hdr->pgio_done_cb = nfs4_read_done_cb;
4713 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
4714 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
4717 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4718 struct nfs_pgio_header *hdr)
4720 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4721 &hdr->args.seq_args,
4725 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4726 hdr->args.lock_context,
4727 hdr->rw_ops->rw_mode) == -EIO)
4729 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
4734 static int nfs4_write_done_cb(struct rpc_task *task,
4735 struct nfs_pgio_header *hdr)
4737 struct inode *inode = hdr->inode;
4739 trace_nfs4_write(hdr, task->tk_status);
4740 if (task->tk_status < 0) {
4741 struct nfs4_exception exception = {
4742 .inode = hdr->inode,
4743 .state = hdr->args.context->state,
4744 .stateid = &hdr->args.stateid,
4746 task->tk_status = nfs4_async_handle_exception(task,
4747 NFS_SERVER(inode), task->tk_status,
4749 if (exception.retry) {
4750 rpc_restart_call_prepare(task);
4754 if (task->tk_status >= 0) {
4755 renew_lease(NFS_SERVER(inode), hdr->timestamp);
4756 nfs_writeback_update_inode(hdr);
4761 static bool nfs4_write_stateid_changed(struct rpc_task *task,
4762 struct nfs_pgio_args *args)
4765 if (!nfs4_error_stateid_expired(task->tk_status) ||
4766 nfs4_stateid_is_current(&args->stateid,
4771 rpc_restart_call_prepare(task);
4775 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
4777 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
4779 if (nfs4_write_stateid_changed(task, &hdr->args))
4781 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4782 nfs4_write_done_cb(task, hdr);
4786 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
4788 /* Don't request attributes for pNFS or O_DIRECT writes */
4789 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
4791 /* Otherwise, request attributes if and only if we don't hold
4794 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
4797 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4798 struct rpc_message *msg)
4800 struct nfs_server *server = NFS_SERVER(hdr->inode);
4802 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4803 hdr->args.bitmask = NULL;
4804 hdr->res.fattr = NULL;
4806 hdr->args.bitmask = server->cache_consistency_bitmask;
4808 if (!hdr->pgio_done_cb)
4809 hdr->pgio_done_cb = nfs4_write_done_cb;
4810 hdr->res.server = server;
4811 hdr->timestamp = jiffies;
4813 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
4814 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
4817 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4819 nfs4_setup_sequence(NFS_SERVER(data->inode),
4820 &data->args.seq_args,
4825 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
4827 struct inode *inode = data->inode;
4829 trace_nfs4_commit(data, task->tk_status);
4830 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4831 NULL, NULL) == -EAGAIN) {
4832 rpc_restart_call_prepare(task);
4838 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
4840 if (!nfs4_sequence_done(task, &data->res.seq_res))
4842 return data->commit_done_cb(task, data);
4845 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
4847 struct nfs_server *server = NFS_SERVER(data->inode);
4849 if (data->commit_done_cb == NULL)
4850 data->commit_done_cb = nfs4_commit_done_cb;
4851 data->res.server = server;
4852 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
4853 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4856 struct nfs4_renewdata {
4857 struct nfs_client *client;
4858 unsigned long timestamp;
4862 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4863 * standalone procedure for queueing an asynchronous RENEW.
4865 static void nfs4_renew_release(void *calldata)
4867 struct nfs4_renewdata *data = calldata;
4868 struct nfs_client *clp = data->client;
4870 if (atomic_read(&clp->cl_count) > 1)
4871 nfs4_schedule_state_renewal(clp);
4872 nfs_put_client(clp);
4876 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
4878 struct nfs4_renewdata *data = calldata;
4879 struct nfs_client *clp = data->client;
4880 unsigned long timestamp = data->timestamp;
4882 trace_nfs4_renew_async(clp, task->tk_status);
4883 switch (task->tk_status) {
4886 case -NFS4ERR_LEASE_MOVED:
4887 nfs4_schedule_lease_moved_recovery(clp);
4890 /* Unless we're shutting down, schedule state recovery! */
4891 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4893 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
4894 nfs4_schedule_lease_recovery(clp);
4897 nfs4_schedule_path_down_recovery(clp);
4899 do_renew_lease(clp, timestamp);
4902 static const struct rpc_call_ops nfs4_renew_ops = {
4903 .rpc_call_done = nfs4_renew_done,
4904 .rpc_release = nfs4_renew_release,
4907 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
4909 struct rpc_message msg = {
4910 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4914 struct nfs4_renewdata *data;
4916 if (renew_flags == 0)
4918 if (!atomic_inc_not_zero(&clp->cl_count))
4920 data = kmalloc(sizeof(*data), GFP_NOFS);
4924 data->timestamp = jiffies;
4925 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
4926 &nfs4_renew_ops, data);
4929 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
4931 struct rpc_message msg = {
4932 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4936 unsigned long now = jiffies;
4939 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4942 do_renew_lease(clp, now);
4946 static inline int nfs4_server_supports_acls(struct nfs_server *server)
4948 return server->caps & NFS_CAP_ACLS;
4951 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4952 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
4955 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
4957 static int buf_to_pages_noslab(const void *buf, size_t buflen,
4958 struct page **pages)
4960 struct page *newpage, **spages;
4966 len = min_t(size_t, PAGE_SIZE, buflen);
4967 newpage = alloc_page(GFP_KERNEL);
4969 if (newpage == NULL)
4971 memcpy(page_address(newpage), buf, len);
4976 } while (buflen != 0);
4982 __free_page(spages[rc-1]);
4986 struct nfs4_cached_acl {
4992 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
4994 struct nfs_inode *nfsi = NFS_I(inode);
4996 spin_lock(&inode->i_lock);
4997 kfree(nfsi->nfs4_acl);
4998 nfsi->nfs4_acl = acl;
4999 spin_unlock(&inode->i_lock);
5002 static void nfs4_zap_acl_attr(struct inode *inode)
5004 nfs4_set_cached_acl(inode, NULL);
5007 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5009 struct nfs_inode *nfsi = NFS_I(inode);
5010 struct nfs4_cached_acl *acl;
5013 spin_lock(&inode->i_lock);
5014 acl = nfsi->nfs4_acl;
5017 if (buf == NULL) /* user is just asking for length */
5019 if (acl->cached == 0)
5021 ret = -ERANGE; /* see getxattr(2) man page */
5022 if (acl->len > buflen)
5024 memcpy(buf, acl->data, acl->len);
5028 spin_unlock(&inode->i_lock);
5032 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
5034 struct nfs4_cached_acl *acl;
5035 size_t buflen = sizeof(*acl) + acl_len;
5037 if (buflen <= PAGE_SIZE) {
5038 acl = kmalloc(buflen, GFP_KERNEL);
5042 _copy_from_pages(acl->data, pages, pgbase, acl_len);
5044 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5051 nfs4_set_cached_acl(inode, acl);
5055 * The getxattr API returns the required buffer length when called with a
5056 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5057 * the required buf. On a NULL buf, we send a page of data to the server
5058 * guessing that the ACL request can be serviced by a page. If so, we cache
5059 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5060 * the cache. If not so, we throw away the page, and cache the required
5061 * length. The next getxattr call will then produce another round trip to
5062 * the server, this time with the input buf of the required size.
5064 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5066 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
5067 struct nfs_getaclargs args = {
5068 .fh = NFS_FH(inode),
5072 struct nfs_getaclres res = {
5075 struct rpc_message msg = {
5076 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5080 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
5081 int ret = -ENOMEM, i;
5083 /* As long as we're doing a round trip to the server anyway,
5084 * let's be prepared for a page of acl data. */
5087 if (npages > ARRAY_SIZE(pages))
5090 for (i = 0; i < npages; i++) {
5091 pages[i] = alloc_page(GFP_KERNEL);
5096 /* for decoding across pages */
5097 res.acl_scratch = alloc_page(GFP_KERNEL);
5098 if (!res.acl_scratch)
5101 args.acl_len = npages * PAGE_SIZE;
5103 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
5104 __func__, buf, buflen, npages, args.acl_len);
5105 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5106 &msg, &args.seq_args, &res.seq_res, 0);
5110 /* Handle the case where the passed-in buffer is too short */
5111 if (res.acl_flags & NFS4_ACL_TRUNC) {
5112 /* Did the user only issue a request for the acl length? */
5118 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
5120 if (res.acl_len > buflen) {
5124 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
5129 for (i = 0; i < npages; i++)
5131 __free_page(pages[i]);
5132 if (res.acl_scratch)
5133 __free_page(res.acl_scratch);
5137 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5139 struct nfs4_exception exception = { };
5142 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
5143 trace_nfs4_get_acl(inode, ret);
5146 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5147 } while (exception.retry);
5151 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5153 struct nfs_server *server = NFS_SERVER(inode);
5156 if (!nfs4_server_supports_acls(server))
5158 ret = nfs_revalidate_inode(server, inode);
5161 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5162 nfs_zap_acl_cache(inode);
5163 ret = nfs4_read_cached_acl(inode, buf, buflen);
5165 /* -ENOENT is returned if there is no ACL or if there is an ACL
5166 * but no cached acl data, just the acl length */
5168 return nfs4_get_acl_uncached(inode, buf, buflen);
5171 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5173 struct nfs_server *server = NFS_SERVER(inode);
5174 struct page *pages[NFS4ACL_MAXPAGES];
5175 struct nfs_setaclargs arg = {
5176 .fh = NFS_FH(inode),
5180 struct nfs_setaclres res;
5181 struct rpc_message msg = {
5182 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5186 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
5189 if (!nfs4_server_supports_acls(server))
5191 if (npages > ARRAY_SIZE(pages))
5193 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
5196 nfs4_inode_return_delegation(inode);
5197 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5200 * Free each page after tx, so the only ref left is
5201 * held by the network stack
5204 put_page(pages[i-1]);
5207 * Acl update can result in inode attribute update.
5208 * so mark the attribute cache invalid.
5210 spin_lock(&inode->i_lock);
5211 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5212 spin_unlock(&inode->i_lock);
5213 nfs_access_zap_cache(inode);
5214 nfs_zap_acl_cache(inode);
5218 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5220 struct nfs4_exception exception = { };
5223 err = __nfs4_proc_set_acl(inode, buf, buflen);
5224 trace_nfs4_set_acl(inode, err);
5225 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5227 } while (exception.retry);
5231 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
5232 static int _nfs4_get_security_label(struct inode *inode, void *buf,
5235 struct nfs_server *server = NFS_SERVER(inode);
5236 struct nfs_fattr fattr;
5237 struct nfs4_label label = {0, 0, buflen, buf};
5239 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5240 struct nfs4_getattr_arg arg = {
5241 .fh = NFS_FH(inode),
5244 struct nfs4_getattr_res res = {
5249 struct rpc_message msg = {
5250 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
5256 nfs_fattr_init(&fattr);
5258 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
5261 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5263 if (buflen < label.len)
5268 static int nfs4_get_security_label(struct inode *inode, void *buf,
5271 struct nfs4_exception exception = { };
5274 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5278 err = _nfs4_get_security_label(inode, buf, buflen);
5279 trace_nfs4_get_security_label(inode, err);
5280 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5282 } while (exception.retry);
5286 static int _nfs4_do_set_security_label(struct inode *inode,
5287 struct nfs4_label *ilabel,
5288 struct nfs_fattr *fattr,
5289 struct nfs4_label *olabel)
5292 struct iattr sattr = {0};
5293 struct nfs_server *server = NFS_SERVER(inode);
5294 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5295 struct nfs_setattrargs arg = {
5296 .fh = NFS_FH(inode),
5302 struct nfs_setattrres res = {
5307 struct rpc_message msg = {
5308 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5314 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
5316 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5318 dprintk("%s failed: %d\n", __func__, status);
5323 static int nfs4_do_set_security_label(struct inode *inode,
5324 struct nfs4_label *ilabel,
5325 struct nfs_fattr *fattr,
5326 struct nfs4_label *olabel)
5328 struct nfs4_exception exception = { };
5332 err = _nfs4_do_set_security_label(inode, ilabel,
5334 trace_nfs4_set_security_label(inode, err);
5335 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5337 } while (exception.retry);
5342 nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
5344 struct nfs4_label ilabel, *olabel = NULL;
5345 struct nfs_fattr fattr;
5346 struct rpc_cred *cred;
5349 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5352 nfs_fattr_init(&fattr);
5356 ilabel.label = (char *)buf;
5357 ilabel.len = buflen;
5359 cred = rpc_lookup_cred();
5361 return PTR_ERR(cred);
5363 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5364 if (IS_ERR(olabel)) {
5365 status = -PTR_ERR(olabel);
5369 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5371 nfs_setsecurity(inode, &fattr, olabel);
5373 nfs4_label_free(olabel);
5378 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5381 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5382 nfs4_verifier *bootverf)
5386 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5387 /* An impossible timestamp guarantees this value
5388 * will never match a generated boot time. */
5389 verf[0] = cpu_to_be32(U32_MAX);
5390 verf[1] = cpu_to_be32(U32_MAX);
5392 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
5393 u64 ns = ktime_to_ns(nn->boot_time);
5395 verf[0] = cpu_to_be32(ns >> 32);
5396 verf[1] = cpu_to_be32(ns);
5398 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5402 nfs4_init_nonuniform_client_string(struct nfs_client *clp)
5407 if (clp->cl_owner_id != NULL)
5411 len = 14 + strlen(clp->cl_ipaddr) + 1 +
5412 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5414 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5418 if (len > NFS4_OPAQUE_LIMIT + 1)
5422 * Since this string is allocated at mount time, and held until the
5423 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5424 * about a memory-reclaim deadlock.
5426 str = kmalloc(len, GFP_KERNEL);
5431 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
5433 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5434 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
5437 clp->cl_owner_id = str;
5442 nfs4_init_uniquifier_client_string(struct nfs_client *clp)
5447 len = 10 + 10 + 1 + 10 + 1 +
5448 strlen(nfs4_client_id_uniquifier) + 1 +
5449 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5451 if (len > NFS4_OPAQUE_LIMIT + 1)
5455 * Since this string is allocated at mount time, and held until the
5456 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5457 * about a memory-reclaim deadlock.
5459 str = kmalloc(len, GFP_KERNEL);
5463 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
5464 clp->rpc_ops->version, clp->cl_minorversion,
5465 nfs4_client_id_uniquifier,
5466 clp->cl_rpcclient->cl_nodename);
5467 clp->cl_owner_id = str;
5472 nfs4_init_uniform_client_string(struct nfs_client *clp)
5477 if (clp->cl_owner_id != NULL)
5480 if (nfs4_client_id_uniquifier[0] != '\0')
5481 return nfs4_init_uniquifier_client_string(clp);
5483 len = 10 + 10 + 1 + 10 + 1 +
5484 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5486 if (len > NFS4_OPAQUE_LIMIT + 1)
5490 * Since this string is allocated at mount time, and held until the
5491 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5492 * about a memory-reclaim deadlock.
5494 str = kmalloc(len, GFP_KERNEL);
5498 scnprintf(str, len, "Linux NFSv%u.%u %s",
5499 clp->rpc_ops->version, clp->cl_minorversion,
5500 clp->cl_rpcclient->cl_nodename);
5501 clp->cl_owner_id = str;
5506 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5507 * services. Advertise one based on the address family of the
5511 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5513 if (strchr(clp->cl_ipaddr, ':') != NULL)
5514 return scnprintf(buf, len, "tcp6");
5516 return scnprintf(buf, len, "tcp");
5519 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5521 struct nfs4_setclientid *sc = calldata;
5523 if (task->tk_status == 0)
5524 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5527 static const struct rpc_call_ops nfs4_setclientid_ops = {
5528 .rpc_call_done = nfs4_setclientid_done,
5532 * nfs4_proc_setclientid - Negotiate client ID
5533 * @clp: state data structure
5534 * @program: RPC program for NFSv4 callback service
5535 * @port: IP port number for NFS4 callback service
5536 * @cred: RPC credential to use for this call
5537 * @res: where to place the result
5539 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5541 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5542 unsigned short port, struct rpc_cred *cred,
5543 struct nfs4_setclientid_res *res)
5545 nfs4_verifier sc_verifier;
5546 struct nfs4_setclientid setclientid = {
5547 .sc_verifier = &sc_verifier,
5551 struct rpc_message msg = {
5552 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5553 .rpc_argp = &setclientid,
5557 struct rpc_task *task;
5558 struct rpc_task_setup task_setup_data = {
5559 .rpc_client = clp->cl_rpcclient,
5560 .rpc_message = &msg,
5561 .callback_ops = &nfs4_setclientid_ops,
5562 .callback_data = &setclientid,
5563 .flags = RPC_TASK_TIMEOUT,
5567 /* nfs_client_id4 */
5568 nfs4_init_boot_verifier(clp, &sc_verifier);
5570 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5571 status = nfs4_init_uniform_client_string(clp);
5573 status = nfs4_init_nonuniform_client_string(clp);
5579 setclientid.sc_netid_len =
5580 nfs4_init_callback_netid(clp,
5581 setclientid.sc_netid,
5582 sizeof(setclientid.sc_netid));
5583 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
5584 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
5585 clp->cl_ipaddr, port >> 8, port & 255);
5587 dprintk("NFS call setclientid auth=%s, '%s'\n",
5588 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5590 task = rpc_run_task(&task_setup_data);
5592 status = PTR_ERR(task);
5595 status = task->tk_status;
5596 if (setclientid.sc_cred) {
5597 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5598 put_rpccred(setclientid.sc_cred);
5602 trace_nfs4_setclientid(clp, status);
5603 dprintk("NFS reply setclientid: %d\n", status);
5608 * nfs4_proc_setclientid_confirm - Confirm client ID
5609 * @clp: state data structure
5610 * @res: result of a previous SETCLIENTID
5611 * @cred: RPC credential to use for this call
5613 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5615 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
5616 struct nfs4_setclientid_res *arg,
5617 struct rpc_cred *cred)
5619 struct rpc_message msg = {
5620 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
5626 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5627 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5629 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5630 trace_nfs4_setclientid_confirm(clp, status);
5631 dprintk("NFS reply setclientid_confirm: %d\n", status);
5635 struct nfs4_delegreturndata {
5636 struct nfs4_delegreturnargs args;
5637 struct nfs4_delegreturnres res;
5639 nfs4_stateid stateid;
5640 unsigned long timestamp;
5642 struct nfs4_layoutreturn_args arg;
5643 struct nfs4_layoutreturn_res res;
5644 struct nfs4_xdr_opaque_data ld_private;
5648 struct nfs_fattr fattr;
5650 struct inode *inode;
5653 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5655 struct nfs4_delegreturndata *data = calldata;
5657 if (!nfs4_sequence_done(task, &data->res.seq_res))
5660 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
5662 /* Handle Layoutreturn errors */
5663 if (data->args.lr_args && task->tk_status != 0) {
5664 switch(data->res.lr_ret) {
5666 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5669 data->args.lr_args = NULL;
5670 data->res.lr_res = NULL;
5672 case -NFS4ERR_ADMIN_REVOKED:
5673 case -NFS4ERR_DELEG_REVOKED:
5674 case -NFS4ERR_EXPIRED:
5675 case -NFS4ERR_BAD_STATEID:
5676 case -NFS4ERR_OLD_STATEID:
5677 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
5678 case -NFS4ERR_WRONG_CRED:
5679 data->args.lr_args = NULL;
5680 data->res.lr_res = NULL;
5681 data->res.lr_ret = 0;
5682 rpc_restart_call_prepare(task);
5687 switch (task->tk_status) {
5689 renew_lease(data->res.server, data->timestamp);
5691 case -NFS4ERR_ADMIN_REVOKED:
5692 case -NFS4ERR_DELEG_REVOKED:
5693 case -NFS4ERR_EXPIRED:
5694 nfs4_free_revoked_stateid(data->res.server,
5696 task->tk_msg.rpc_cred);
5697 case -NFS4ERR_BAD_STATEID:
5698 case -NFS4ERR_OLD_STATEID:
5699 case -NFS4ERR_STALE_STATEID:
5700 task->tk_status = 0;
5702 case -NFS4ERR_ACCESS:
5703 if (data->args.bitmask) {
5704 data->args.bitmask = NULL;
5705 data->res.fattr = NULL;
5706 task->tk_status = 0;
5707 rpc_restart_call_prepare(task);
5711 if (nfs4_async_handle_error(task, data->res.server,
5712 NULL, NULL) == -EAGAIN) {
5713 rpc_restart_call_prepare(task);
5717 data->rpc_status = task->tk_status;
5720 static void nfs4_delegreturn_release(void *calldata)
5722 struct nfs4_delegreturndata *data = calldata;
5723 struct inode *inode = data->inode;
5727 pnfs_roc_release(&data->lr.arg, &data->lr.res,
5729 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5730 nfs_iput_and_deactive(inode);
5735 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5737 struct nfs4_delegreturndata *d_data;
5739 d_data = (struct nfs4_delegreturndata *)data;
5741 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
5744 nfs4_setup_sequence(d_data->res.server,
5745 &d_data->args.seq_args,
5746 &d_data->res.seq_res,
5750 static const struct rpc_call_ops nfs4_delegreturn_ops = {
5751 .rpc_call_prepare = nfs4_delegreturn_prepare,
5752 .rpc_call_done = nfs4_delegreturn_done,
5753 .rpc_release = nfs4_delegreturn_release,
5756 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
5758 struct nfs4_delegreturndata *data;
5759 struct nfs_server *server = NFS_SERVER(inode);
5760 struct rpc_task *task;
5761 struct rpc_message msg = {
5762 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5765 struct rpc_task_setup task_setup_data = {
5766 .rpc_client = server->client,
5767 .rpc_message = &msg,
5768 .callback_ops = &nfs4_delegreturn_ops,
5769 .flags = RPC_TASK_ASYNC,
5773 data = kzalloc(sizeof(*data), GFP_NOFS);
5776 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
5778 nfs4_state_protect(server->nfs_client,
5779 NFS_SP4_MACH_CRED_CLEANUP,
5780 &task_setup_data.rpc_client, &msg);
5782 data->args.fhandle = &data->fh;
5783 data->args.stateid = &data->stateid;
5784 data->args.bitmask = server->cache_consistency_bitmask;
5785 nfs_copy_fh(&data->fh, NFS_FH(inode));
5786 nfs4_stateid_copy(&data->stateid, stateid);
5787 data->res.fattr = &data->fattr;
5788 data->res.server = server;
5789 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5790 data->lr.arg.ld_private = &data->lr.ld_private;
5791 nfs_fattr_init(data->res.fattr);
5792 data->timestamp = jiffies;
5793 data->rpc_status = 0;
5794 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
5795 data->inode = nfs_igrab_and_active(inode);
5798 data->args.lr_args = &data->lr.arg;
5799 data->res.lr_res = &data->lr.res;
5801 } else if (data->lr.roc) {
5802 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
5803 data->lr.roc = false;
5806 task_setup_data.callback_data = data;
5807 msg.rpc_argp = &data->args;
5808 msg.rpc_resp = &data->res;
5809 task = rpc_run_task(&task_setup_data);
5811 return PTR_ERR(task);
5814 status = nfs4_wait_for_completion_rpc_task(task);
5817 status = data->rpc_status;
5823 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
5825 struct nfs_server *server = NFS_SERVER(inode);
5826 struct nfs4_exception exception = { };
5829 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
5830 trace_nfs4_delegreturn(inode, stateid, err);
5832 case -NFS4ERR_STALE_STATEID:
5833 case -NFS4ERR_EXPIRED:
5837 err = nfs4_handle_exception(server, err, &exception);
5838 } while (exception.retry);
5842 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5844 struct inode *inode = state->inode;
5845 struct nfs_server *server = NFS_SERVER(inode);
5846 struct nfs_client *clp = server->nfs_client;
5847 struct nfs_lockt_args arg = {
5848 .fh = NFS_FH(inode),
5851 struct nfs_lockt_res res = {
5854 struct rpc_message msg = {
5855 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5858 .rpc_cred = state->owner->so_cred,
5860 struct nfs4_lock_state *lsp;
5863 arg.lock_owner.clientid = clp->cl_clientid;
5864 status = nfs4_set_lock_state(state, request);
5867 lsp = request->fl_u.nfs4_fl.owner;
5868 arg.lock_owner.id = lsp->ls_seqid.owner_id;
5869 arg.lock_owner.s_dev = server->s_dev;
5870 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5873 request->fl_type = F_UNLCK;
5875 case -NFS4ERR_DENIED:
5878 request->fl_ops->fl_release_private(request);
5879 request->fl_ops = NULL;
5884 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5886 struct nfs4_exception exception = { };
5890 err = _nfs4_proc_getlk(state, cmd, request);
5891 trace_nfs4_get_lock(request, state, cmd, err);
5892 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
5894 } while (exception.retry);
5898 struct nfs4_unlockdata {
5899 struct nfs_locku_args arg;
5900 struct nfs_locku_res res;
5901 struct nfs4_lock_state *lsp;
5902 struct nfs_open_context *ctx;
5903 struct file_lock fl;
5904 struct nfs_server *server;
5905 unsigned long timestamp;
5908 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5909 struct nfs_open_context *ctx,
5910 struct nfs4_lock_state *lsp,
5911 struct nfs_seqid *seqid)
5913 struct nfs4_unlockdata *p;
5914 struct inode *inode = lsp->ls_state->inode;
5916 p = kzalloc(sizeof(*p), GFP_NOFS);
5919 p->arg.fh = NFS_FH(inode);
5921 p->arg.seqid = seqid;
5922 p->res.seqid = seqid;
5924 atomic_inc(&lsp->ls_count);
5925 /* Ensure we don't close file until we're done freeing locks! */
5926 p->ctx = get_nfs_open_context(ctx);
5927 memcpy(&p->fl, fl, sizeof(p->fl));
5928 p->server = NFS_SERVER(inode);
5932 static void nfs4_locku_release_calldata(void *data)
5934 struct nfs4_unlockdata *calldata = data;
5935 nfs_free_seqid(calldata->arg.seqid);
5936 nfs4_put_lock_state(calldata->lsp);
5937 put_nfs_open_context(calldata->ctx);
5941 static void nfs4_locku_done(struct rpc_task *task, void *data)
5943 struct nfs4_unlockdata *calldata = data;
5945 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5947 switch (task->tk_status) {
5949 renew_lease(calldata->server, calldata->timestamp);
5950 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
5951 if (nfs4_update_lock_stateid(calldata->lsp,
5952 &calldata->res.stateid))
5954 case -NFS4ERR_ADMIN_REVOKED:
5955 case -NFS4ERR_EXPIRED:
5956 nfs4_free_revoked_stateid(calldata->server,
5957 &calldata->arg.stateid,
5958 task->tk_msg.rpc_cred);
5959 case -NFS4ERR_BAD_STATEID:
5960 case -NFS4ERR_OLD_STATEID:
5961 case -NFS4ERR_STALE_STATEID:
5962 if (!nfs4_stateid_match(&calldata->arg.stateid,
5963 &calldata->lsp->ls_stateid))
5964 rpc_restart_call_prepare(task);
5967 if (nfs4_async_handle_error(task, calldata->server,
5968 NULL, NULL) == -EAGAIN)
5969 rpc_restart_call_prepare(task);
5971 nfs_release_seqid(calldata->arg.seqid);
5974 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
5976 struct nfs4_unlockdata *calldata = data;
5978 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
5980 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
5981 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
5982 /* Note: exit _without_ running nfs4_locku_done */
5985 calldata->timestamp = jiffies;
5986 if (nfs4_setup_sequence(calldata->server,
5987 &calldata->arg.seq_args,
5988 &calldata->res.seq_res,
5990 nfs_release_seqid(calldata->arg.seqid);
5993 task->tk_action = NULL;
5995 nfs4_sequence_done(task, &calldata->res.seq_res);
5998 static const struct rpc_call_ops nfs4_locku_ops = {
5999 .rpc_call_prepare = nfs4_locku_prepare,
6000 .rpc_call_done = nfs4_locku_done,
6001 .rpc_release = nfs4_locku_release_calldata,
6004 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6005 struct nfs_open_context *ctx,
6006 struct nfs4_lock_state *lsp,
6007 struct nfs_seqid *seqid)
6009 struct nfs4_unlockdata *data;
6010 struct rpc_message msg = {
6011 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6012 .rpc_cred = ctx->cred,
6014 struct rpc_task_setup task_setup_data = {
6015 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
6016 .rpc_message = &msg,
6017 .callback_ops = &nfs4_locku_ops,
6018 .workqueue = nfsiod_workqueue,
6019 .flags = RPC_TASK_ASYNC,
6022 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6023 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6025 /* Ensure this is an unlock - when canceling a lock, the
6026 * canceled lock is passed in, and it won't be an unlock.
6028 fl->fl_type = F_UNLCK;
6030 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6032 nfs_free_seqid(seqid);
6033 return ERR_PTR(-ENOMEM);
6036 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
6037 msg.rpc_argp = &data->arg;
6038 msg.rpc_resp = &data->res;
6039 task_setup_data.callback_data = data;
6040 return rpc_run_task(&task_setup_data);
6043 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6045 struct inode *inode = state->inode;
6046 struct nfs4_state_owner *sp = state->owner;
6047 struct nfs_inode *nfsi = NFS_I(inode);
6048 struct nfs_seqid *seqid;
6049 struct nfs4_lock_state *lsp;
6050 struct rpc_task *task;
6051 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6053 unsigned char fl_flags = request->fl_flags;
6055 status = nfs4_set_lock_state(state, request);
6056 /* Unlock _before_ we do the RPC call */
6057 request->fl_flags |= FL_EXISTS;
6058 /* Exclude nfs_delegation_claim_locks() */
6059 mutex_lock(&sp->so_delegreturn_mutex);
6060 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
6061 down_read(&nfsi->rwsem);
6062 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
6063 up_read(&nfsi->rwsem);
6064 mutex_unlock(&sp->so_delegreturn_mutex);
6067 up_read(&nfsi->rwsem);
6068 mutex_unlock(&sp->so_delegreturn_mutex);
6071 /* Is this a delegated lock? */
6072 lsp = request->fl_u.nfs4_fl.owner;
6073 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6075 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6076 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
6080 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
6081 status = PTR_ERR(task);
6084 status = nfs4_wait_for_completion_rpc_task(task);
6087 request->fl_flags = fl_flags;
6088 trace_nfs4_unlock(request, state, F_SETLK, status);
6092 struct nfs4_lockdata {
6093 struct nfs_lock_args arg;
6094 struct nfs_lock_res res;
6095 struct nfs4_lock_state *lsp;
6096 struct nfs_open_context *ctx;
6097 struct file_lock fl;
6098 unsigned long timestamp;
6101 struct nfs_server *server;
6104 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
6105 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6108 struct nfs4_lockdata *p;
6109 struct inode *inode = lsp->ls_state->inode;
6110 struct nfs_server *server = NFS_SERVER(inode);
6111 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6113 p = kzalloc(sizeof(*p), gfp_mask);
6117 p->arg.fh = NFS_FH(inode);
6119 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
6120 if (IS_ERR(p->arg.open_seqid))
6122 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6123 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
6124 if (IS_ERR(p->arg.lock_seqid))
6125 goto out_free_seqid;
6126 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
6127 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
6128 p->arg.lock_owner.s_dev = server->s_dev;
6129 p->res.lock_seqid = p->arg.lock_seqid;
6132 atomic_inc(&lsp->ls_count);
6133 p->ctx = get_nfs_open_context(ctx);
6134 memcpy(&p->fl, fl, sizeof(p->fl));
6137 nfs_free_seqid(p->arg.open_seqid);
6143 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6145 struct nfs4_lockdata *data = calldata;
6146 struct nfs4_state *state = data->lsp->ls_state;
6148 dprintk("%s: begin!\n", __func__);
6149 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
6151 /* Do we need to do an open_to_lock_owner? */
6152 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
6153 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
6154 goto out_release_lock_seqid;
6156 nfs4_stateid_copy(&data->arg.open_stateid,
6157 &state->open_stateid);
6158 data->arg.new_lock_owner = 1;
6159 data->res.open_seqid = data->arg.open_seqid;
6161 data->arg.new_lock_owner = 0;
6162 nfs4_stateid_copy(&data->arg.lock_stateid,
6163 &data->lsp->ls_stateid);
6165 if (!nfs4_valid_open_stateid(state)) {
6166 data->rpc_status = -EBADF;
6167 task->tk_action = NULL;
6168 goto out_release_open_seqid;
6170 data->timestamp = jiffies;
6171 if (nfs4_setup_sequence(data->server,
6172 &data->arg.seq_args,
6176 out_release_open_seqid:
6177 nfs_release_seqid(data->arg.open_seqid);
6178 out_release_lock_seqid:
6179 nfs_release_seqid(data->arg.lock_seqid);
6181 nfs4_sequence_done(task, &data->res.seq_res);
6182 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
6185 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6187 struct nfs4_lockdata *data = calldata;
6188 struct nfs4_lock_state *lsp = data->lsp;
6190 dprintk("%s: begin!\n", __func__);
6192 if (!nfs4_sequence_done(task, &data->res.seq_res))
6195 data->rpc_status = task->tk_status;
6196 switch (task->tk_status) {
6198 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
6200 if (data->arg.new_lock) {
6201 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
6202 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
6203 rpc_restart_call_prepare(task);
6207 if (data->arg.new_lock_owner != 0) {
6208 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6209 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6210 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6211 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6212 rpc_restart_call_prepare(task);
6214 case -NFS4ERR_BAD_STATEID:
6215 case -NFS4ERR_OLD_STATEID:
6216 case -NFS4ERR_STALE_STATEID:
6217 case -NFS4ERR_EXPIRED:
6218 if (data->arg.new_lock_owner != 0) {
6219 if (!nfs4_stateid_match(&data->arg.open_stateid,
6220 &lsp->ls_state->open_stateid))
6221 rpc_restart_call_prepare(task);
6222 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6224 rpc_restart_call_prepare(task);
6226 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
6229 static void nfs4_lock_release(void *calldata)
6231 struct nfs4_lockdata *data = calldata;
6233 dprintk("%s: begin!\n", __func__);
6234 nfs_free_seqid(data->arg.open_seqid);
6235 if (data->cancelled != 0) {
6236 struct rpc_task *task;
6237 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6238 data->arg.lock_seqid);
6240 rpc_put_task_async(task);
6241 dprintk("%s: cancelling lock!\n", __func__);
6243 nfs_free_seqid(data->arg.lock_seqid);
6244 nfs4_put_lock_state(data->lsp);
6245 put_nfs_open_context(data->ctx);
6247 dprintk("%s: done!\n", __func__);
6250 static const struct rpc_call_ops nfs4_lock_ops = {
6251 .rpc_call_prepare = nfs4_lock_prepare,
6252 .rpc_call_done = nfs4_lock_done,
6253 .rpc_release = nfs4_lock_release,
6256 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6259 case -NFS4ERR_ADMIN_REVOKED:
6260 case -NFS4ERR_EXPIRED:
6261 case -NFS4ERR_BAD_STATEID:
6262 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6263 if (new_lock_owner != 0 ||
6264 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
6265 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
6267 case -NFS4ERR_STALE_STATEID:
6268 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6269 nfs4_schedule_lease_recovery(server->nfs_client);
6273 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
6275 struct nfs4_lockdata *data;
6276 struct rpc_task *task;
6277 struct rpc_message msg = {
6278 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6279 .rpc_cred = state->owner->so_cred,
6281 struct rpc_task_setup task_setup_data = {
6282 .rpc_client = NFS_CLIENT(state->inode),
6283 .rpc_message = &msg,
6284 .callback_ops = &nfs4_lock_ops,
6285 .workqueue = nfsiod_workqueue,
6286 .flags = RPC_TASK_ASYNC,
6290 dprintk("%s: begin!\n", __func__);
6291 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
6292 fl->fl_u.nfs4_fl.owner,
6293 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
6297 data->arg.block = 1;
6298 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
6299 msg.rpc_argp = &data->arg;
6300 msg.rpc_resp = &data->res;
6301 task_setup_data.callback_data = data;
6302 if (recovery_type > NFS_LOCK_NEW) {
6303 if (recovery_type == NFS_LOCK_RECLAIM)
6304 data->arg.reclaim = NFS_LOCK_RECLAIM;
6305 nfs4_set_sequence_privileged(&data->arg.seq_args);
6307 data->arg.new_lock = 1;
6308 task = rpc_run_task(&task_setup_data);
6310 return PTR_ERR(task);
6311 ret = nfs4_wait_for_completion_rpc_task(task);
6313 ret = data->rpc_status;
6315 nfs4_handle_setlk_error(data->server, data->lsp,
6316 data->arg.new_lock_owner, ret);
6318 data->cancelled = 1;
6320 dprintk("%s: done, ret = %d!\n", __func__, ret);
6321 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
6325 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6327 struct nfs_server *server = NFS_SERVER(state->inode);
6328 struct nfs4_exception exception = {
6329 .inode = state->inode,
6334 /* Cache the lock if possible... */
6335 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6337 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
6338 if (err != -NFS4ERR_DELAY)
6340 nfs4_handle_exception(server, err, &exception);
6341 } while (exception.retry);
6345 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6347 struct nfs_server *server = NFS_SERVER(state->inode);
6348 struct nfs4_exception exception = {
6349 .inode = state->inode,
6353 err = nfs4_set_lock_state(state, request);
6356 if (!recover_lost_locks) {
6357 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6361 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6363 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
6367 case -NFS4ERR_GRACE:
6368 case -NFS4ERR_DELAY:
6369 nfs4_handle_exception(server, err, &exception);
6372 } while (exception.retry);
6377 #if defined(CONFIG_NFS_V4_1)
6378 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6380 struct nfs4_lock_state *lsp;
6383 status = nfs4_set_lock_state(state, request);
6386 lsp = request->fl_u.nfs4_fl.owner;
6387 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6388 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6390 status = nfs4_lock_expired(state, request);
6395 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6397 struct nfs_inode *nfsi = NFS_I(state->inode);
6398 struct nfs4_state_owner *sp = state->owner;
6399 unsigned char fl_flags = request->fl_flags;
6402 request->fl_flags |= FL_ACCESS;
6403 status = locks_lock_inode_wait(state->inode, request);
6406 mutex_lock(&sp->so_delegreturn_mutex);
6407 down_read(&nfsi->rwsem);
6408 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
6409 /* Yes: cache locks! */
6410 /* ...but avoid races with delegation recall... */
6411 request->fl_flags = fl_flags & ~FL_SLEEP;
6412 status = locks_lock_inode_wait(state->inode, request);
6413 up_read(&nfsi->rwsem);
6414 mutex_unlock(&sp->so_delegreturn_mutex);
6417 up_read(&nfsi->rwsem);
6418 mutex_unlock(&sp->so_delegreturn_mutex);
6419 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
6421 request->fl_flags = fl_flags;
6425 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6427 struct nfs4_exception exception = {
6429 .inode = state->inode,
6434 err = _nfs4_proc_setlk(state, cmd, request);
6435 if (err == -NFS4ERR_DENIED)
6437 err = nfs4_handle_exception(NFS_SERVER(state->inode),
6439 } while (exception.retry);
6443 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6444 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6447 nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6448 struct file_lock *request)
6450 int status = -ERESTARTSYS;
6451 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6453 while(!signalled()) {
6454 status = nfs4_proc_setlk(state, cmd, request);
6455 if ((status != -EAGAIN) || IS_SETLK(cmd))
6457 freezable_schedule_timeout_interruptible(timeout);
6459 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6460 status = -ERESTARTSYS;
6465 #ifdef CONFIG_NFS_V4_1
6466 struct nfs4_lock_waiter {
6467 struct task_struct *task;
6468 struct inode *inode;
6469 struct nfs_lowner *owner;
6474 nfs4_wake_lock_waiter(wait_queue_t *wait, unsigned int mode, int flags, void *key)
6477 struct cb_notify_lock_args *cbnl = key;
6478 struct nfs4_lock_waiter *waiter = wait->private;
6479 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6480 *wowner = waiter->owner;
6482 /* Only wake if the callback was for the same owner */
6483 if (lowner->clientid != wowner->clientid ||
6484 lowner->id != wowner->id ||
6485 lowner->s_dev != wowner->s_dev)
6488 /* Make sure it's for the right inode */
6489 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6492 waiter->notified = true;
6494 /* override "private" so we can use default_wake_function */
6495 wait->private = waiter->task;
6496 ret = autoremove_wake_function(wait, mode, flags, key);
6497 wait->private = waiter;
6502 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6504 int status = -ERESTARTSYS;
6505 unsigned long flags;
6506 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6507 struct nfs_server *server = NFS_SERVER(state->inode);
6508 struct nfs_client *clp = server->nfs_client;
6509 wait_queue_head_t *q = &clp->cl_lock_waitq;
6510 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6511 .id = lsp->ls_seqid.owner_id,
6512 .s_dev = server->s_dev };
6513 struct nfs4_lock_waiter waiter = { .task = current,
6514 .inode = state->inode,
6516 .notified = false };
6519 /* Don't bother with waitqueue if we don't expect a callback */
6520 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6521 return nfs4_retry_setlk_simple(state, cmd, request);
6524 wait.private = &waiter;
6525 wait.func = nfs4_wake_lock_waiter;
6526 add_wait_queue(q, &wait);
6528 while(!signalled()) {
6529 status = nfs4_proc_setlk(state, cmd, request);
6530 if ((status != -EAGAIN) || IS_SETLK(cmd))
6533 status = -ERESTARTSYS;
6534 spin_lock_irqsave(&q->lock, flags);
6535 if (waiter.notified) {
6536 spin_unlock_irqrestore(&q->lock, flags);
6539 set_current_state(TASK_INTERRUPTIBLE);
6540 spin_unlock_irqrestore(&q->lock, flags);
6542 freezable_schedule_timeout_interruptible(NFS4_LOCK_MAXTIMEOUT);
6545 finish_wait(q, &wait);
6548 #else /* !CONFIG_NFS_V4_1 */
6550 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6552 return nfs4_retry_setlk_simple(state, cmd, request);
6557 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6559 struct nfs_open_context *ctx;
6560 struct nfs4_state *state;
6563 /* verify open state */
6564 ctx = nfs_file_open_context(filp);
6567 if (request->fl_start < 0 || request->fl_end < 0)
6570 if (IS_GETLK(cmd)) {
6572 return nfs4_proc_getlk(state, F_GETLK, request);
6576 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6579 if (request->fl_type == F_UNLCK) {
6581 return nfs4_proc_unlck(state, cmd, request);
6588 if ((request->fl_flags & FL_POSIX) &&
6589 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6593 * Don't rely on the VFS having checked the file open mode,
6594 * since it won't do this for flock() locks.
6596 switch (request->fl_type) {
6598 if (!(filp->f_mode & FMODE_READ))
6602 if (!(filp->f_mode & FMODE_WRITE))
6606 status = nfs4_set_lock_state(state, request);
6610 return nfs4_retry_setlk(state, cmd, request);
6613 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
6615 struct nfs_server *server = NFS_SERVER(state->inode);
6618 err = nfs4_set_lock_state(state, fl);
6621 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
6622 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
6625 struct nfs_release_lockowner_data {
6626 struct nfs4_lock_state *lsp;
6627 struct nfs_server *server;
6628 struct nfs_release_lockowner_args args;
6629 struct nfs_release_lockowner_res res;
6630 unsigned long timestamp;
6633 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6635 struct nfs_release_lockowner_data *data = calldata;
6636 struct nfs_server *server = data->server;
6637 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
6638 &data->args.seq_args, &data->res.seq_res, task);
6639 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6640 data->timestamp = jiffies;
6643 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6645 struct nfs_release_lockowner_data *data = calldata;
6646 struct nfs_server *server = data->server;
6648 nfs40_sequence_done(task, &data->res.seq_res);
6650 switch (task->tk_status) {
6652 renew_lease(server, data->timestamp);
6654 case -NFS4ERR_STALE_CLIENTID:
6655 case -NFS4ERR_EXPIRED:
6656 nfs4_schedule_lease_recovery(server->nfs_client);
6658 case -NFS4ERR_LEASE_MOVED:
6659 case -NFS4ERR_DELAY:
6660 if (nfs4_async_handle_error(task, server,
6661 NULL, NULL) == -EAGAIN)
6662 rpc_restart_call_prepare(task);
6666 static void nfs4_release_lockowner_release(void *calldata)
6668 struct nfs_release_lockowner_data *data = calldata;
6669 nfs4_free_lock_state(data->server, data->lsp);
6673 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
6674 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6675 .rpc_call_done = nfs4_release_lockowner_done,
6676 .rpc_release = nfs4_release_lockowner_release,
6680 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
6682 struct nfs_release_lockowner_data *data;
6683 struct rpc_message msg = {
6684 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6687 if (server->nfs_client->cl_mvops->minor_version != 0)
6690 data = kmalloc(sizeof(*data), GFP_NOFS);
6694 data->server = server;
6695 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6696 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6697 data->args.lock_owner.s_dev = server->s_dev;
6699 msg.rpc_argp = &data->args;
6700 msg.rpc_resp = &data->res;
6701 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
6702 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
6705 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6707 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
6708 struct dentry *unused, struct inode *inode,
6709 const char *key, const void *buf,
6710 size_t buflen, int flags)
6712 return nfs4_proc_set_acl(inode, buf, buflen);
6715 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
6716 struct dentry *unused, struct inode *inode,
6717 const char *key, void *buf, size_t buflen)
6719 return nfs4_proc_get_acl(inode, buf, buflen);
6722 static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
6724 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
6727 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
6729 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
6730 struct dentry *unused, struct inode *inode,
6731 const char *key, const void *buf,
6732 size_t buflen, int flags)
6734 if (security_ismaclabel(key))
6735 return nfs4_set_security_label(inode, buf, buflen);
6740 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
6741 struct dentry *unused, struct inode *inode,
6742 const char *key, void *buf, size_t buflen)
6744 if (security_ismaclabel(key))
6745 return nfs4_get_security_label(inode, buf, buflen);
6750 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6754 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6755 len = security_inode_listsecurity(inode, list, list_len);
6756 if (list_len && len > list_len)
6762 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6763 .prefix = XATTR_SECURITY_PREFIX,
6764 .get = nfs4_xattr_get_nfs4_label,
6765 .set = nfs4_xattr_set_nfs4_label,
6771 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6779 * nfs_fhget will use either the mounted_on_fileid or the fileid
6781 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6783 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6784 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6785 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
6786 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
6789 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
6790 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
6791 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6795 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6796 const struct qstr *name,
6797 struct nfs4_fs_locations *fs_locations,
6800 struct nfs_server *server = NFS_SERVER(dir);
6802 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6804 struct nfs4_fs_locations_arg args = {
6805 .dir_fh = NFS_FH(dir),
6810 struct nfs4_fs_locations_res res = {
6811 .fs_locations = fs_locations,
6813 struct rpc_message msg = {
6814 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6820 dprintk("%s: start\n", __func__);
6822 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6823 * is not supported */
6824 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6825 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6827 bitmask[0] |= FATTR4_WORD0_FILEID;
6829 nfs_fattr_init(&fs_locations->fattr);
6830 fs_locations->server = server;
6831 fs_locations->nlocations = 0;
6832 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
6833 dprintk("%s: returned status = %d\n", __func__, status);
6837 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6838 const struct qstr *name,
6839 struct nfs4_fs_locations *fs_locations,
6842 struct nfs4_exception exception = { };
6845 err = _nfs4_proc_fs_locations(client, dir, name,
6846 fs_locations, page);
6847 trace_nfs4_get_fs_locations(dir, name, err);
6848 err = nfs4_handle_exception(NFS_SERVER(dir), err,
6850 } while (exception.retry);
6855 * This operation also signals the server that this client is
6856 * performing migration recovery. The server can stop returning
6857 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6858 * appended to this compound to identify the client ID which is
6859 * performing recovery.
6861 static int _nfs40_proc_get_locations(struct inode *inode,
6862 struct nfs4_fs_locations *locations,
6863 struct page *page, struct rpc_cred *cred)
6865 struct nfs_server *server = NFS_SERVER(inode);
6866 struct rpc_clnt *clnt = server->client;
6868 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6870 struct nfs4_fs_locations_arg args = {
6871 .clientid = server->nfs_client->cl_clientid,
6872 .fh = NFS_FH(inode),
6875 .migration = 1, /* skip LOOKUP */
6876 .renew = 1, /* append RENEW */
6878 struct nfs4_fs_locations_res res = {
6879 .fs_locations = locations,
6883 struct rpc_message msg = {
6884 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6889 unsigned long now = jiffies;
6892 nfs_fattr_init(&locations->fattr);
6893 locations->server = server;
6894 locations->nlocations = 0;
6896 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6897 nfs4_set_sequence_privileged(&args.seq_args);
6898 status = nfs4_call_sync_sequence(clnt, server, &msg,
6899 &args.seq_args, &res.seq_res);
6903 renew_lease(server, now);
6907 #ifdef CONFIG_NFS_V4_1
6910 * This operation also signals the server that this client is
6911 * performing migration recovery. The server can stop asserting
6912 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6913 * performing this operation is identified in the SEQUENCE
6914 * operation in this compound.
6916 * When the client supports GETATTR(fs_locations_info), it can
6917 * be plumbed in here.
6919 static int _nfs41_proc_get_locations(struct inode *inode,
6920 struct nfs4_fs_locations *locations,
6921 struct page *page, struct rpc_cred *cred)
6923 struct nfs_server *server = NFS_SERVER(inode);
6924 struct rpc_clnt *clnt = server->client;
6926 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6928 struct nfs4_fs_locations_arg args = {
6929 .fh = NFS_FH(inode),
6932 .migration = 1, /* skip LOOKUP */
6934 struct nfs4_fs_locations_res res = {
6935 .fs_locations = locations,
6938 struct rpc_message msg = {
6939 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6946 nfs_fattr_init(&locations->fattr);
6947 locations->server = server;
6948 locations->nlocations = 0;
6950 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6951 nfs4_set_sequence_privileged(&args.seq_args);
6952 status = nfs4_call_sync_sequence(clnt, server, &msg,
6953 &args.seq_args, &res.seq_res);
6954 if (status == NFS4_OK &&
6955 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6956 status = -NFS4ERR_LEASE_MOVED;
6960 #endif /* CONFIG_NFS_V4_1 */
6963 * nfs4_proc_get_locations - discover locations for a migrated FSID
6964 * @inode: inode on FSID that is migrating
6965 * @locations: result of query
6967 * @cred: credential to use for this operation
6969 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6970 * operation failed, or a negative errno if a local error occurred.
6972 * On success, "locations" is filled in, but if the server has
6973 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6976 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6977 * from this client that require migration recovery.
6979 int nfs4_proc_get_locations(struct inode *inode,
6980 struct nfs4_fs_locations *locations,
6981 struct page *page, struct rpc_cred *cred)
6983 struct nfs_server *server = NFS_SERVER(inode);
6984 struct nfs_client *clp = server->nfs_client;
6985 const struct nfs4_mig_recovery_ops *ops =
6986 clp->cl_mvops->mig_recovery_ops;
6987 struct nfs4_exception exception = { };
6990 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6991 (unsigned long long)server->fsid.major,
6992 (unsigned long long)server->fsid.minor,
6994 nfs_display_fhandle(NFS_FH(inode), __func__);
6997 status = ops->get_locations(inode, locations, page, cred);
6998 if (status != -NFS4ERR_DELAY)
7000 nfs4_handle_exception(server, status, &exception);
7001 } while (exception.retry);
7006 * This operation also signals the server that this client is
7007 * performing "lease moved" recovery. The server can stop
7008 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7009 * is appended to this compound to identify the client ID which is
7010 * performing recovery.
7012 static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7014 struct nfs_server *server = NFS_SERVER(inode);
7015 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7016 struct rpc_clnt *clnt = server->client;
7017 struct nfs4_fsid_present_arg args = {
7018 .fh = NFS_FH(inode),
7019 .clientid = clp->cl_clientid,
7020 .renew = 1, /* append RENEW */
7022 struct nfs4_fsid_present_res res = {
7025 struct rpc_message msg = {
7026 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7031 unsigned long now = jiffies;
7034 res.fh = nfs_alloc_fhandle();
7038 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7039 nfs4_set_sequence_privileged(&args.seq_args);
7040 status = nfs4_call_sync_sequence(clnt, server, &msg,
7041 &args.seq_args, &res.seq_res);
7042 nfs_free_fhandle(res.fh);
7046 do_renew_lease(clp, now);
7050 #ifdef CONFIG_NFS_V4_1
7053 * This operation also signals the server that this client is
7054 * performing "lease moved" recovery. The server can stop asserting
7055 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7056 * this operation is identified in the SEQUENCE operation in this
7059 static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7061 struct nfs_server *server = NFS_SERVER(inode);
7062 struct rpc_clnt *clnt = server->client;
7063 struct nfs4_fsid_present_arg args = {
7064 .fh = NFS_FH(inode),
7066 struct nfs4_fsid_present_res res = {
7068 struct rpc_message msg = {
7069 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7076 res.fh = nfs_alloc_fhandle();
7080 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7081 nfs4_set_sequence_privileged(&args.seq_args);
7082 status = nfs4_call_sync_sequence(clnt, server, &msg,
7083 &args.seq_args, &res.seq_res);
7084 nfs_free_fhandle(res.fh);
7085 if (status == NFS4_OK &&
7086 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7087 status = -NFS4ERR_LEASE_MOVED;
7091 #endif /* CONFIG_NFS_V4_1 */
7094 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7095 * @inode: inode on FSID to check
7096 * @cred: credential to use for this operation
7098 * Server indicates whether the FSID is present, moved, or not
7099 * recognized. This operation is necessary to clear a LEASE_MOVED
7100 * condition for this client ID.
7102 * Returns NFS4_OK if the FSID is present on this server,
7103 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7104 * NFS4ERR code if some error occurred on the server, or a
7105 * negative errno if a local failure occurred.
7107 int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7109 struct nfs_server *server = NFS_SERVER(inode);
7110 struct nfs_client *clp = server->nfs_client;
7111 const struct nfs4_mig_recovery_ops *ops =
7112 clp->cl_mvops->mig_recovery_ops;
7113 struct nfs4_exception exception = { };
7116 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7117 (unsigned long long)server->fsid.major,
7118 (unsigned long long)server->fsid.minor,
7120 nfs_display_fhandle(NFS_FH(inode), __func__);
7123 status = ops->fsid_present(inode, cred);
7124 if (status != -NFS4ERR_DELAY)
7126 nfs4_handle_exception(server, status, &exception);
7127 } while (exception.retry);
7132 * If 'use_integrity' is true and the state managment nfs_client
7133 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7134 * and the machine credential as per RFC3530bis and RFC5661 Security
7135 * Considerations sections. Otherwise, just use the user cred with the
7136 * filesystem's rpc_client.
7138 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
7141 struct nfs4_secinfo_arg args = {
7142 .dir_fh = NFS_FH(dir),
7145 struct nfs4_secinfo_res res = {
7148 struct rpc_message msg = {
7149 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7153 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
7154 struct rpc_cred *cred = NULL;
7156 if (use_integrity) {
7157 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
7158 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7159 msg.rpc_cred = cred;
7162 dprintk("NFS call secinfo %s\n", name->name);
7164 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7165 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7167 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7169 dprintk("NFS reply secinfo: %d\n", status);
7177 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7178 struct nfs4_secinfo_flavors *flavors)
7180 struct nfs4_exception exception = { };
7183 err = -NFS4ERR_WRONGSEC;
7185 /* try to use integrity protection with machine cred */
7186 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7187 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7190 * if unable to use integrity protection, or SECINFO with
7191 * integrity protection returns NFS4ERR_WRONGSEC (which is
7192 * disallowed by spec, but exists in deployed servers) use
7193 * the current filesystem's rpc_client and the user cred.
7195 if (err == -NFS4ERR_WRONGSEC)
7196 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7198 trace_nfs4_secinfo(dir, name, err);
7199 err = nfs4_handle_exception(NFS_SERVER(dir), err,
7201 } while (exception.retry);
7205 #ifdef CONFIG_NFS_V4_1
7207 * Check the exchange flags returned by the server for invalid flags, having
7208 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7211 static int nfs4_check_cl_exchange_flags(u32 flags)
7213 if (flags & ~EXCHGID4_FLAG_MASK_R)
7215 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7216 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7218 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7222 return -NFS4ERR_INVAL;
7226 nfs41_same_server_scope(struct nfs41_server_scope *a,
7227 struct nfs41_server_scope *b)
7229 if (a->server_scope_sz == b->server_scope_sz &&
7230 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
7237 nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7241 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7242 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7246 * nfs4_proc_bind_one_conn_to_session()
7248 * The 4.1 client currently uses the same TCP connection for the
7249 * fore and backchannel.
7252 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7253 struct rpc_xprt *xprt,
7254 struct nfs_client *clp,
7255 struct rpc_cred *cred)
7258 struct nfs41_bind_conn_to_session_args args = {
7260 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7262 struct nfs41_bind_conn_to_session_res res;
7263 struct rpc_message msg = {
7265 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
7270 struct rpc_task_setup task_setup_data = {
7273 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
7274 .rpc_message = &msg,
7275 .flags = RPC_TASK_TIMEOUT,
7277 struct rpc_task *task;
7279 dprintk("--> %s\n", __func__);
7281 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7282 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7283 args.dir = NFS4_CDFC4_FORE;
7285 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7286 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7287 args.dir = NFS4_CDFC4_FORE;
7289 task = rpc_run_task(&task_setup_data);
7290 if (!IS_ERR(task)) {
7291 status = task->tk_status;
7294 status = PTR_ERR(task);
7295 trace_nfs4_bind_conn_to_session(clp, status);
7297 if (memcmp(res.sessionid.data,
7298 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7299 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7303 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
7304 dprintk("NFS: %s: Unexpected direction from server\n",
7309 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
7310 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7317 dprintk("<-- %s status= %d\n", __func__, status);
7321 struct rpc_bind_conn_calldata {
7322 struct nfs_client *clp;
7323 struct rpc_cred *cred;
7327 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7328 struct rpc_xprt *xprt,
7331 struct rpc_bind_conn_calldata *p = calldata;
7333 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7336 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7338 struct rpc_bind_conn_calldata data = {
7342 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7343 nfs4_proc_bind_conn_to_session_callback, &data);
7347 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7348 * and operations we'd like to see to enable certain features in the allow map
7350 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7351 .how = SP4_MACH_CRED,
7352 .enforce.u.words = {
7353 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7354 1 << (OP_EXCHANGE_ID - 32) |
7355 1 << (OP_CREATE_SESSION - 32) |
7356 1 << (OP_DESTROY_SESSION - 32) |
7357 1 << (OP_DESTROY_CLIENTID - 32)
7360 [0] = 1 << (OP_CLOSE) |
7361 1 << (OP_OPEN_DOWNGRADE) |
7363 1 << (OP_DELEGRETURN) |
7365 [1] = 1 << (OP_SECINFO - 32) |
7366 1 << (OP_SECINFO_NO_NAME - 32) |
7367 1 << (OP_LAYOUTRETURN - 32) |
7368 1 << (OP_TEST_STATEID - 32) |
7369 1 << (OP_FREE_STATEID - 32) |
7370 1 << (OP_WRITE - 32)
7375 * Select the state protection mode for client `clp' given the server results
7376 * from exchange_id in `sp'.
7378 * Returns 0 on success, negative errno otherwise.
7380 static int nfs4_sp4_select_mode(struct nfs_client *clp,
7381 struct nfs41_state_protection *sp)
7383 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7384 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7385 1 << (OP_EXCHANGE_ID - 32) |
7386 1 << (OP_CREATE_SESSION - 32) |
7387 1 << (OP_DESTROY_SESSION - 32) |
7388 1 << (OP_DESTROY_CLIENTID - 32)
7392 if (sp->how == SP4_MACH_CRED) {
7393 /* Print state protect result */
7394 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7395 for (i = 0; i <= LAST_NFS4_OP; i++) {
7396 if (test_bit(i, sp->enforce.u.longs))
7397 dfprintk(MOUNT, " enforce op %d\n", i);
7398 if (test_bit(i, sp->allow.u.longs))
7399 dfprintk(MOUNT, " allow op %d\n", i);
7402 /* make sure nothing is on enforce list that isn't supported */
7403 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7404 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7405 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7411 * Minimal mode - state operations are allowed to use machine
7412 * credential. Note this already happens by default, so the
7413 * client doesn't have to do anything more than the negotiation.
7415 * NOTE: we don't care if EXCHANGE_ID is in the list -
7416 * we're already using the machine cred for exchange_id
7417 * and will never use a different cred.
7419 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7420 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7421 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7422 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7423 dfprintk(MOUNT, "sp4_mach_cred:\n");
7424 dfprintk(MOUNT, " minimal mode enabled\n");
7425 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
7427 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7431 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
7432 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7433 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
7434 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7435 dfprintk(MOUNT, " cleanup mode enabled\n");
7436 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
7439 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7440 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7441 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
7442 &clp->cl_sp4_flags);
7445 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7446 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7447 dfprintk(MOUNT, " secinfo mode enabled\n");
7448 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
7451 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7452 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7453 dfprintk(MOUNT, " stateid mode enabled\n");
7454 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
7457 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7458 dfprintk(MOUNT, " write mode enabled\n");
7459 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
7462 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7463 dfprintk(MOUNT, " commit mode enabled\n");
7464 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
7471 struct nfs41_exchange_id_data {
7472 struct nfs41_exchange_id_res res;
7473 struct nfs41_exchange_id_args args;
7474 struct rpc_xprt *xprt;
7478 static void nfs4_exchange_id_done(struct rpc_task *task, void *data)
7480 struct nfs41_exchange_id_data *cdata =
7481 (struct nfs41_exchange_id_data *)data;
7482 struct nfs_client *clp = cdata->args.client;
7483 int status = task->tk_status;
7485 trace_nfs4_exchange_id(clp, status);
7488 status = nfs4_check_cl_exchange_flags(cdata->res.flags);
7490 if (cdata->xprt && status == 0) {
7491 status = nfs4_detect_session_trunking(clp, &cdata->res,
7497 status = nfs4_sp4_select_mode(clp, &cdata->res.state_protect);
7500 clp->cl_clientid = cdata->res.clientid;
7501 clp->cl_exchange_flags = cdata->res.flags;
7502 /* Client ID is not confirmed */
7503 if (!(cdata->res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
7504 clear_bit(NFS4_SESSION_ESTABLISHED,
7505 &clp->cl_session->session_state);
7506 clp->cl_seqid = cdata->res.seqid;
7509 kfree(clp->cl_serverowner);
7510 clp->cl_serverowner = cdata->res.server_owner;
7511 cdata->res.server_owner = NULL;
7513 /* use the most recent implementation id */
7514 kfree(clp->cl_implid);
7515 clp->cl_implid = cdata->res.impl_id;
7516 cdata->res.impl_id = NULL;
7518 if (clp->cl_serverscope != NULL &&
7519 !nfs41_same_server_scope(clp->cl_serverscope,
7520 cdata->res.server_scope)) {
7521 dprintk("%s: server_scope mismatch detected\n",
7523 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
7524 kfree(clp->cl_serverscope);
7525 clp->cl_serverscope = NULL;
7528 if (clp->cl_serverscope == NULL) {
7529 clp->cl_serverscope = cdata->res.server_scope;
7530 cdata->res.server_scope = NULL;
7532 /* Save the EXCHANGE_ID verifier session trunk tests */
7533 memcpy(clp->cl_confirm.data, cdata->args.verifier->data,
7534 sizeof(clp->cl_confirm.data));
7537 cdata->rpc_status = status;
7541 static void nfs4_exchange_id_release(void *data)
7543 struct nfs41_exchange_id_data *cdata =
7544 (struct nfs41_exchange_id_data *)data;
7546 nfs_put_client(cdata->args.client);
7548 xprt_put(cdata->xprt);
7549 rpc_clnt_xprt_switch_put(cdata->args.client->cl_rpcclient);
7551 kfree(cdata->res.impl_id);
7552 kfree(cdata->res.server_scope);
7553 kfree(cdata->res.server_owner);
7557 static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7558 .rpc_call_done = nfs4_exchange_id_done,
7559 .rpc_release = nfs4_exchange_id_release,
7563 * _nfs4_proc_exchange_id()
7565 * Wrapper for EXCHANGE_ID operation.
7567 static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
7568 u32 sp4_how, struct rpc_xprt *xprt)
7570 nfs4_verifier verifier;
7571 struct rpc_message msg = {
7572 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
7575 struct rpc_task_setup task_setup_data = {
7576 .rpc_client = clp->cl_rpcclient,
7577 .callback_ops = &nfs4_exchange_id_call_ops,
7578 .rpc_message = &msg,
7579 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7581 struct nfs41_exchange_id_data *calldata;
7582 struct rpc_task *task;
7585 if (!atomic_inc_not_zero(&clp->cl_count))
7589 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7594 nfs4_init_boot_verifier(clp, &verifier);
7596 status = nfs4_init_uniform_client_string(clp);
7600 dprintk("NFS call exchange_id auth=%s, '%s'\n",
7601 clp->cl_rpcclient->cl_auth->au_ops->au_name,
7604 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7607 if (unlikely(calldata->res.server_owner == NULL))
7610 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
7612 if (unlikely(calldata->res.server_scope == NULL))
7613 goto out_server_owner;
7615 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7616 if (unlikely(calldata->res.impl_id == NULL))
7617 goto out_server_scope;
7621 calldata->args.state_protect.how = SP4_NONE;
7625 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
7635 calldata->xprt = xprt;
7636 task_setup_data.rpc_xprt = xprt;
7637 task_setup_data.flags =
7638 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC;
7639 calldata->args.verifier = &clp->cl_confirm;
7641 calldata->args.verifier = &verifier;
7643 calldata->args.client = clp;
7644 #ifdef CONFIG_NFS_V4_1_MIGRATION
7645 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7646 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7647 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7649 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7650 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7652 msg.rpc_argp = &calldata->args;
7653 msg.rpc_resp = &calldata->res;
7654 task_setup_data.callback_data = calldata;
7656 task = rpc_run_task(&task_setup_data);
7658 status = PTR_ERR(task);
7663 status = rpc_wait_for_completion_task(task);
7665 status = calldata->rpc_status;
7666 } else /* session trunking test */
7667 status = calldata->rpc_status;
7671 if (clp->cl_implid != NULL)
7672 dprintk("NFS reply exchange_id: Server Implementation ID: "
7673 "domain: %s, name: %s, date: %llu,%u\n",
7674 clp->cl_implid->domain, clp->cl_implid->name,
7675 clp->cl_implid->date.seconds,
7676 clp->cl_implid->date.nseconds);
7677 dprintk("NFS reply exchange_id: %d\n", status);
7681 kfree(calldata->res.impl_id);
7683 kfree(calldata->res.server_scope);
7685 kfree(calldata->res.server_owner);
7692 * nfs4_proc_exchange_id()
7694 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7696 * Since the clientid has expired, all compounds using sessions
7697 * associated with the stale clientid will be returning
7698 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7699 * be in some phase of session reset.
7701 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7703 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7705 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7708 /* try SP4_MACH_CRED if krb5i/p */
7709 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7710 authflavor == RPC_AUTH_GSS_KRB5P) {
7711 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED, NULL);
7717 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE, NULL);
7721 * nfs4_test_session_trunk
7723 * This is an add_xprt_test() test function called from
7724 * rpc_clnt_setup_test_and_add_xprt.
7726 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7727 * and is dereferrenced in nfs4_exchange_id_release
7729 * Upon success, add the new transport to the rpc_clnt
7731 * @clnt: struct rpc_clnt to get new transport
7732 * @xprt: the rpc_xprt to test
7733 * @data: call data for _nfs4_proc_exchange_id.
7735 int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7738 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
7741 dprintk("--> %s try %s\n", __func__,
7742 xprt->address_strings[RPC_DISPLAY_ADDR]);
7744 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7746 /* Test connection for session trunking. Async exchange_id call */
7747 return _nfs4_proc_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7749 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7751 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7752 struct rpc_cred *cred)
7754 struct rpc_message msg = {
7755 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7761 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7762 trace_nfs4_destroy_clientid(clp, status);
7764 dprintk("NFS: Got error %d from the server %s on "
7765 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7769 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7770 struct rpc_cred *cred)
7775 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7776 ret = _nfs4_proc_destroy_clientid(clp, cred);
7778 case -NFS4ERR_DELAY:
7779 case -NFS4ERR_CLIENTID_BUSY:
7789 int nfs4_destroy_clientid(struct nfs_client *clp)
7791 struct rpc_cred *cred;
7794 if (clp->cl_mvops->minor_version < 1)
7796 if (clp->cl_exchange_flags == 0)
7798 if (clp->cl_preserve_clid)
7800 cred = nfs4_get_clid_cred(clp);
7801 ret = nfs4_proc_destroy_clientid(clp, cred);
7806 case -NFS4ERR_STALE_CLIENTID:
7807 clp->cl_exchange_flags = 0;
7813 struct nfs4_get_lease_time_data {
7814 struct nfs4_get_lease_time_args *args;
7815 struct nfs4_get_lease_time_res *res;
7816 struct nfs_client *clp;
7819 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7822 struct nfs4_get_lease_time_data *data =
7823 (struct nfs4_get_lease_time_data *)calldata;
7825 dprintk("--> %s\n", __func__);
7826 /* just setup sequence, do not trigger session recovery
7827 since we're invoked within one */
7828 nfs41_setup_sequence(data->clp->cl_session,
7829 &data->args->la_seq_args,
7830 &data->res->lr_seq_res,
7832 dprintk("<-- %s\n", __func__);
7836 * Called from nfs4_state_manager thread for session setup, so don't recover
7837 * from sequence operation or clientid errors.
7839 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7841 struct nfs4_get_lease_time_data *data =
7842 (struct nfs4_get_lease_time_data *)calldata;
7844 dprintk("--> %s\n", __func__);
7845 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7847 switch (task->tk_status) {
7848 case -NFS4ERR_DELAY:
7849 case -NFS4ERR_GRACE:
7850 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7851 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7852 task->tk_status = 0;
7854 case -NFS4ERR_RETRY_UNCACHED_REP:
7855 rpc_restart_call_prepare(task);
7858 dprintk("<-- %s\n", __func__);
7861 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
7862 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7863 .rpc_call_done = nfs4_get_lease_time_done,
7866 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7868 struct rpc_task *task;
7869 struct nfs4_get_lease_time_args args;
7870 struct nfs4_get_lease_time_res res = {
7871 .lr_fsinfo = fsinfo,
7873 struct nfs4_get_lease_time_data data = {
7878 struct rpc_message msg = {
7879 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7883 struct rpc_task_setup task_setup = {
7884 .rpc_client = clp->cl_rpcclient,
7885 .rpc_message = &msg,
7886 .callback_ops = &nfs4_get_lease_time_ops,
7887 .callback_data = &data,
7888 .flags = RPC_TASK_TIMEOUT,
7892 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
7893 nfs4_set_sequence_privileged(&args.la_seq_args);
7894 dprintk("--> %s\n", __func__);
7895 task = rpc_run_task(&task_setup);
7898 status = PTR_ERR(task);
7900 status = task->tk_status;
7903 dprintk("<-- %s return %d\n", __func__, status);
7909 * Initialize the values to be used by the client in CREATE_SESSION
7910 * If nfs4_init_session set the fore channel request and response sizes,
7913 * Set the back channel max_resp_sz_cached to zero to force the client to
7914 * always set csa_cachethis to FALSE because the current implementation
7915 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7917 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7918 struct rpc_clnt *clnt)
7920 unsigned int max_rqst_sz, max_resp_sz;
7921 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
7923 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7924 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7926 /* Fore channel attributes */
7927 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7928 args->fc_attrs.max_resp_sz = max_resp_sz;
7929 args->fc_attrs.max_ops = NFS4_MAX_OPS;
7930 args->fc_attrs.max_reqs = max_session_slots;
7932 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
7933 "max_ops=%u max_reqs=%u\n",
7935 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
7936 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
7938 /* Back channel attributes */
7939 args->bc_attrs.max_rqst_sz = max_bc_payload;
7940 args->bc_attrs.max_resp_sz = max_bc_payload;
7941 args->bc_attrs.max_resp_sz_cached = 0;
7942 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
7943 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
7945 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7946 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7948 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7949 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7950 args->bc_attrs.max_reqs);
7953 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7954 struct nfs41_create_session_res *res)
7956 struct nfs4_channel_attrs *sent = &args->fc_attrs;
7957 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
7959 if (rcvd->max_resp_sz > sent->max_resp_sz)
7962 * Our requested max_ops is the minimum we need; we're not
7963 * prepared to break up compounds into smaller pieces than that.
7964 * So, no point even trying to continue if the server won't
7967 if (rcvd->max_ops < sent->max_ops)
7969 if (rcvd->max_reqs == 0)
7971 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7972 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
7976 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7977 struct nfs41_create_session_res *res)
7979 struct nfs4_channel_attrs *sent = &args->bc_attrs;
7980 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
7982 if (!(res->flags & SESSION4_BACK_CHAN))
7984 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7986 if (rcvd->max_resp_sz < sent->max_resp_sz)
7988 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7990 if (rcvd->max_ops > sent->max_ops)
7992 if (rcvd->max_reqs > sent->max_reqs)
7998 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
7999 struct nfs41_create_session_res *res)
8003 ret = nfs4_verify_fore_channel_attrs(args, res);
8006 return nfs4_verify_back_channel_attrs(args, res);
8009 static void nfs4_update_session(struct nfs4_session *session,
8010 struct nfs41_create_session_res *res)
8012 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
8013 /* Mark client id and session as being confirmed */
8014 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8015 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
8016 session->flags = res->flags;
8017 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
8018 if (res->flags & SESSION4_BACK_CHAN)
8019 memcpy(&session->bc_attrs, &res->bc_attrs,
8020 sizeof(session->bc_attrs));
8023 static int _nfs4_proc_create_session(struct nfs_client *clp,
8024 struct rpc_cred *cred)
8026 struct nfs4_session *session = clp->cl_session;
8027 struct nfs41_create_session_args args = {
8029 .clientid = clp->cl_clientid,
8030 .seqid = clp->cl_seqid,
8031 .cb_program = NFS4_CALLBACK,
8033 struct nfs41_create_session_res res;
8035 struct rpc_message msg = {
8036 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8043 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
8044 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
8046 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
8047 trace_nfs4_create_session(clp, status);
8050 case -NFS4ERR_STALE_CLIENTID:
8051 case -NFS4ERR_DELAY:
8060 /* Verify the session's negotiated channel_attrs values */
8061 status = nfs4_verify_channel_attrs(&args, &res);
8062 /* Increment the clientid slot sequence id */
8065 nfs4_update_session(session, &res);
8072 * Issues a CREATE_SESSION operation to the server.
8073 * It is the responsibility of the caller to verify the session is
8074 * expired before calling this routine.
8076 int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
8080 struct nfs4_session *session = clp->cl_session;
8082 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8084 status = _nfs4_proc_create_session(clp, cred);
8088 /* Init or reset the session slot tables */
8089 status = nfs4_setup_session_slot_tables(session);
8090 dprintk("slot table setup returned %d\n", status);
8094 ptr = (unsigned *)&session->sess_id.data[0];
8095 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8096 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
8098 dprintk("<-- %s\n", __func__);
8103 * Issue the over-the-wire RPC DESTROY_SESSION.
8104 * The caller must serialize access to this routine.
8106 int nfs4_proc_destroy_session(struct nfs4_session *session,
8107 struct rpc_cred *cred)
8109 struct rpc_message msg = {
8110 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8111 .rpc_argp = session,
8116 dprintk("--> nfs4_proc_destroy_session\n");
8118 /* session is still being setup */
8119 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8122 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
8123 trace_nfs4_destroy_session(session->clp, status);
8126 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
8127 "Session has been destroyed regardless...\n", status);
8129 dprintk("<-- nfs4_proc_destroy_session\n");
8134 * Renew the cl_session lease.
8136 struct nfs4_sequence_data {
8137 struct nfs_client *clp;
8138 struct nfs4_sequence_args args;
8139 struct nfs4_sequence_res res;
8142 static void nfs41_sequence_release(void *data)
8144 struct nfs4_sequence_data *calldata = data;
8145 struct nfs_client *clp = calldata->clp;
8147 if (atomic_read(&clp->cl_count) > 1)
8148 nfs4_schedule_state_renewal(clp);
8149 nfs_put_client(clp);
8153 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8155 switch(task->tk_status) {
8156 case -NFS4ERR_DELAY:
8157 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8160 nfs4_schedule_lease_recovery(clp);
8165 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
8167 struct nfs4_sequence_data *calldata = data;
8168 struct nfs_client *clp = calldata->clp;
8170 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8173 trace_nfs4_sequence(clp, task->tk_status);
8174 if (task->tk_status < 0) {
8175 dprintk("%s ERROR %d\n", __func__, task->tk_status);
8176 if (atomic_read(&clp->cl_count) == 1)
8179 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8180 rpc_restart_call_prepare(task);
8184 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
8186 dprintk("<-- %s\n", __func__);
8189 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8191 struct nfs4_sequence_data *calldata = data;
8192 struct nfs_client *clp = calldata->clp;
8193 struct nfs4_sequence_args *args;
8194 struct nfs4_sequence_res *res;
8196 args = task->tk_msg.rpc_argp;
8197 res = task->tk_msg.rpc_resp;
8199 nfs41_setup_sequence(clp->cl_session, args, res, task);
8202 static const struct rpc_call_ops nfs41_sequence_ops = {
8203 .rpc_call_done = nfs41_sequence_call_done,
8204 .rpc_call_prepare = nfs41_sequence_prepare,
8205 .rpc_release = nfs41_sequence_release,
8208 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8209 struct rpc_cred *cred,
8212 struct nfs4_sequence_data *calldata;
8213 struct rpc_message msg = {
8214 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8217 struct rpc_task_setup task_setup_data = {
8218 .rpc_client = clp->cl_rpcclient,
8219 .rpc_message = &msg,
8220 .callback_ops = &nfs41_sequence_ops,
8221 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
8224 if (!atomic_inc_not_zero(&clp->cl_count))
8225 return ERR_PTR(-EIO);
8226 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8227 if (calldata == NULL) {
8228 nfs_put_client(clp);
8229 return ERR_PTR(-ENOMEM);
8231 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
8233 nfs4_set_sequence_privileged(&calldata->args);
8234 msg.rpc_argp = &calldata->args;
8235 msg.rpc_resp = &calldata->res;
8236 calldata->clp = clp;
8237 task_setup_data.callback_data = calldata;
8239 return rpc_run_task(&task_setup_data);
8242 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
8244 struct rpc_task *task;
8247 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
8249 task = _nfs41_proc_sequence(clp, cred, false);
8251 ret = PTR_ERR(task);
8253 rpc_put_task_async(task);
8254 dprintk("<-- %s status=%d\n", __func__, ret);
8258 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8260 struct rpc_task *task;
8263 task = _nfs41_proc_sequence(clp, cred, true);
8265 ret = PTR_ERR(task);
8268 ret = rpc_wait_for_completion_task(task);
8270 ret = task->tk_status;
8273 dprintk("<-- %s status=%d\n", __func__, ret);
8277 struct nfs4_reclaim_complete_data {
8278 struct nfs_client *clp;
8279 struct nfs41_reclaim_complete_args arg;
8280 struct nfs41_reclaim_complete_res res;
8283 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8285 struct nfs4_reclaim_complete_data *calldata = data;
8287 nfs41_setup_sequence(calldata->clp->cl_session,
8288 &calldata->arg.seq_args,
8289 &calldata->res.seq_res,
8293 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8295 switch(task->tk_status) {
8297 case -NFS4ERR_COMPLETE_ALREADY:
8298 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8300 case -NFS4ERR_DELAY:
8301 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8303 case -NFS4ERR_RETRY_UNCACHED_REP:
8306 nfs4_schedule_lease_recovery(clp);
8311 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8313 struct nfs4_reclaim_complete_data *calldata = data;
8314 struct nfs_client *clp = calldata->clp;
8315 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8317 dprintk("--> %s\n", __func__);
8318 if (!nfs41_sequence_done(task, res))
8321 trace_nfs4_reclaim_complete(clp, task->tk_status);
8322 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8323 rpc_restart_call_prepare(task);
8326 dprintk("<-- %s\n", __func__);
8329 static void nfs4_free_reclaim_complete_data(void *data)
8331 struct nfs4_reclaim_complete_data *calldata = data;
8336 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8337 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8338 .rpc_call_done = nfs4_reclaim_complete_done,
8339 .rpc_release = nfs4_free_reclaim_complete_data,
8343 * Issue a global reclaim complete.
8345 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8346 struct rpc_cred *cred)
8348 struct nfs4_reclaim_complete_data *calldata;
8349 struct rpc_task *task;
8350 struct rpc_message msg = {
8351 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
8354 struct rpc_task_setup task_setup_data = {
8355 .rpc_client = clp->cl_rpcclient,
8356 .rpc_message = &msg,
8357 .callback_ops = &nfs4_reclaim_complete_call_ops,
8358 .flags = RPC_TASK_ASYNC,
8360 int status = -ENOMEM;
8362 dprintk("--> %s\n", __func__);
8363 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8364 if (calldata == NULL)
8366 calldata->clp = clp;
8367 calldata->arg.one_fs = 0;
8369 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
8370 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
8371 msg.rpc_argp = &calldata->arg;
8372 msg.rpc_resp = &calldata->res;
8373 task_setup_data.callback_data = calldata;
8374 task = rpc_run_task(&task_setup_data);
8376 status = PTR_ERR(task);
8379 status = nfs4_wait_for_completion_rpc_task(task);
8381 status = task->tk_status;
8385 dprintk("<-- %s status=%d\n", __func__, status);
8390 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8392 struct nfs4_layoutget *lgp = calldata;
8393 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
8394 struct nfs4_session *session = nfs4_get_session(server);
8396 dprintk("--> %s\n", __func__);
8397 nfs41_setup_sequence(session, &lgp->args.seq_args,
8398 &lgp->res.seq_res, task);
8399 dprintk("<-- %s\n", __func__);
8402 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8404 struct nfs4_layoutget *lgp = calldata;
8406 dprintk("--> %s\n", __func__);
8407 nfs41_sequence_process(task, &lgp->res.seq_res);
8408 dprintk("<-- %s\n", __func__);
8412 nfs4_layoutget_handle_exception(struct rpc_task *task,
8413 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8415 struct inode *inode = lgp->args.inode;
8416 struct nfs_server *server = NFS_SERVER(inode);
8417 struct pnfs_layout_hdr *lo;
8418 int nfs4err = task->tk_status;
8419 int err, status = 0;
8422 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
8429 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8430 * on the file. set tk_status to -ENODATA to tell upper layer to
8433 case -NFS4ERR_LAYOUTUNAVAILABLE:
8437 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8438 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8440 case -NFS4ERR_BADLAYOUT:
8441 status = -EOVERFLOW;
8444 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
8445 * (or clients) writing to the same RAID stripe except when
8446 * the minlength argument is 0 (see RFC5661 section 18.43.3).
8448 * Treat it like we would RECALLCONFLICT -- we retry for a little
8449 * while, and then eventually give up.
8451 case -NFS4ERR_LAYOUTTRYLATER:
8452 if (lgp->args.minlength == 0) {
8453 status = -EOVERFLOW;
8458 case -NFS4ERR_RECALLCONFLICT:
8459 status = -ERECALLCONFLICT;
8461 case -NFS4ERR_DELEG_REVOKED:
8462 case -NFS4ERR_ADMIN_REVOKED:
8463 case -NFS4ERR_EXPIRED:
8464 case -NFS4ERR_BAD_STATEID:
8465 exception->timeout = 0;
8466 spin_lock(&inode->i_lock);
8467 lo = NFS_I(inode)->layout;
8468 /* If the open stateid was bad, then recover it. */
8469 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8470 nfs4_stateid_match_other(&lgp->args.stateid,
8471 &lgp->args.ctx->state->stateid)) {
8472 spin_unlock(&inode->i_lock);
8473 exception->state = lgp->args.ctx->state;
8474 exception->stateid = &lgp->args.stateid;
8479 * Mark the bad layout state as invalid, then retry
8481 pnfs_mark_layout_stateid_invalid(lo, &head);
8482 spin_unlock(&inode->i_lock);
8483 pnfs_free_lseg_list(&head);
8488 err = nfs4_handle_exception(server, nfs4err, exception);
8490 if (exception->retry)
8496 dprintk("<-- %s\n", __func__);
8500 static size_t max_response_pages(struct nfs_server *server)
8502 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8503 return nfs_page_array_len(0, max_resp_sz);
8506 static void nfs4_free_pages(struct page **pages, size_t size)
8513 for (i = 0; i < size; i++) {
8516 __free_page(pages[i]);
8521 static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8523 struct page **pages;
8526 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8528 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8532 for (i = 0; i < size; i++) {
8533 pages[i] = alloc_page(gfp_flags);
8535 dprintk("%s: failed to allocate page\n", __func__);
8536 nfs4_free_pages(pages, size);
8544 static void nfs4_layoutget_release(void *calldata)
8546 struct nfs4_layoutget *lgp = calldata;
8547 struct inode *inode = lgp->args.inode;
8548 struct nfs_server *server = NFS_SERVER(inode);
8549 size_t max_pages = max_response_pages(server);
8551 dprintk("--> %s\n", __func__);
8552 nfs4_free_pages(lgp->args.layout.pages, max_pages);
8553 pnfs_put_layout_hdr(NFS_I(inode)->layout);
8554 put_nfs_open_context(lgp->args.ctx);
8556 dprintk("<-- %s\n", __func__);
8559 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8560 .rpc_call_prepare = nfs4_layoutget_prepare,
8561 .rpc_call_done = nfs4_layoutget_done,
8562 .rpc_release = nfs4_layoutget_release,
8565 struct pnfs_layout_segment *
8566 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
8568 struct inode *inode = lgp->args.inode;
8569 struct nfs_server *server = NFS_SERVER(inode);
8570 size_t max_pages = max_response_pages(server);
8571 struct rpc_task *task;
8572 struct rpc_message msg = {
8573 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8574 .rpc_argp = &lgp->args,
8575 .rpc_resp = &lgp->res,
8576 .rpc_cred = lgp->cred,
8578 struct rpc_task_setup task_setup_data = {
8579 .rpc_client = server->client,
8580 .rpc_message = &msg,
8581 .callback_ops = &nfs4_layoutget_call_ops,
8582 .callback_data = lgp,
8583 .flags = RPC_TASK_ASYNC,
8585 struct pnfs_layout_segment *lseg = NULL;
8586 struct nfs4_exception exception = {
8588 .timeout = *timeout,
8592 dprintk("--> %s\n", __func__);
8594 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8595 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8597 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8598 if (!lgp->args.layout.pages) {
8599 nfs4_layoutget_release(lgp);
8600 return ERR_PTR(-ENOMEM);
8602 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8604 lgp->res.layoutp = &lgp->args.layout;
8605 lgp->res.seq_res.sr_slot = NULL;
8606 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
8608 task = rpc_run_task(&task_setup_data);
8610 return ERR_CAST(task);
8611 status = nfs4_wait_for_completion_rpc_task(task);
8613 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8614 *timeout = exception.timeout;
8617 trace_nfs4_layoutget(lgp->args.ctx,
8623 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8624 if (status == 0 && lgp->res.layoutp->len)
8625 lseg = pnfs_layout_process(lgp);
8626 nfs4_sequence_free_slot(&lgp->res.seq_res);
8628 dprintk("<-- %s status=%d\n", __func__, status);
8630 return ERR_PTR(status);
8635 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8637 struct nfs4_layoutreturn *lrp = calldata;
8639 dprintk("--> %s\n", __func__);
8640 nfs41_setup_sequence(lrp->clp->cl_session,
8641 &lrp->args.seq_args,
8646 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8648 struct nfs4_layoutreturn *lrp = calldata;
8649 struct nfs_server *server;
8651 dprintk("--> %s\n", __func__);
8653 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
8656 server = NFS_SERVER(lrp->args.inode);
8657 switch (task->tk_status) {
8659 task->tk_status = 0;
8662 case -NFS4ERR_DELAY:
8663 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
8665 nfs4_sequence_free_slot(&lrp->res.seq_res);
8666 rpc_restart_call_prepare(task);
8669 dprintk("<-- %s\n", __func__);
8672 static void nfs4_layoutreturn_release(void *calldata)
8674 struct nfs4_layoutreturn *lrp = calldata;
8675 struct pnfs_layout_hdr *lo = lrp->args.layout;
8677 dprintk("--> %s\n", __func__);
8678 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
8679 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
8680 nfs4_sequence_free_slot(&lrp->res.seq_res);
8681 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
8682 lrp->ld_private.ops->free(&lrp->ld_private);
8683 pnfs_put_layout_hdr(lrp->args.layout);
8684 nfs_iput_and_deactive(lrp->inode);
8686 dprintk("<-- %s\n", __func__);
8689 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8690 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8691 .rpc_call_done = nfs4_layoutreturn_done,
8692 .rpc_release = nfs4_layoutreturn_release,
8695 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
8697 struct rpc_task *task;
8698 struct rpc_message msg = {
8699 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8700 .rpc_argp = &lrp->args,
8701 .rpc_resp = &lrp->res,
8702 .rpc_cred = lrp->cred,
8704 struct rpc_task_setup task_setup_data = {
8705 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
8706 .rpc_message = &msg,
8707 .callback_ops = &nfs4_layoutreturn_call_ops,
8708 .callback_data = lrp,
8712 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8713 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8714 &task_setup_data.rpc_client, &msg);
8716 dprintk("--> %s\n", __func__);
8718 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8720 nfs4_layoutreturn_release(lrp);
8723 task_setup_data.flags |= RPC_TASK_ASYNC;
8725 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
8726 task = rpc_run_task(&task_setup_data);
8728 return PTR_ERR(task);
8730 status = task->tk_status;
8731 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
8732 dprintk("<-- %s status=%d\n", __func__, status);
8738 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
8739 struct pnfs_device *pdev,
8740 struct rpc_cred *cred)
8742 struct nfs4_getdeviceinfo_args args = {
8744 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8745 NOTIFY_DEVICEID4_DELETE,
8747 struct nfs4_getdeviceinfo_res res = {
8750 struct rpc_message msg = {
8751 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8758 dprintk("--> %s\n", __func__);
8759 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
8760 if (res.notification & ~args.notify_types)
8761 dprintk("%s: unsupported notification\n", __func__);
8762 if (res.notification != args.notify_types)
8765 dprintk("<-- %s status=%d\n", __func__, status);
8770 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8771 struct pnfs_device *pdev,
8772 struct rpc_cred *cred)
8774 struct nfs4_exception exception = { };
8778 err = nfs4_handle_exception(server,
8779 _nfs4_proc_getdeviceinfo(server, pdev, cred),
8781 } while (exception.retry);
8784 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8786 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8788 struct nfs4_layoutcommit_data *data = calldata;
8789 struct nfs_server *server = NFS_SERVER(data->args.inode);
8790 struct nfs4_session *session = nfs4_get_session(server);
8792 nfs41_setup_sequence(session,
8793 &data->args.seq_args,
8799 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8801 struct nfs4_layoutcommit_data *data = calldata;
8802 struct nfs_server *server = NFS_SERVER(data->args.inode);
8804 if (!nfs41_sequence_done(task, &data->res.seq_res))
8807 switch (task->tk_status) { /* Just ignore these failures */
8808 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8809 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8810 case -NFS4ERR_BADLAYOUT: /* no layout */
8811 case -NFS4ERR_GRACE: /* loca_recalim always false */
8812 task->tk_status = 0;
8816 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
8817 rpc_restart_call_prepare(task);
8823 static void nfs4_layoutcommit_release(void *calldata)
8825 struct nfs4_layoutcommit_data *data = calldata;
8827 pnfs_cleanup_layoutcommit(data);
8828 nfs_post_op_update_inode_force_wcc(data->args.inode,
8830 put_rpccred(data->cred);
8831 nfs_iput_and_deactive(data->inode);
8835 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8836 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8837 .rpc_call_done = nfs4_layoutcommit_done,
8838 .rpc_release = nfs4_layoutcommit_release,
8842 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
8844 struct rpc_message msg = {
8845 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8846 .rpc_argp = &data->args,
8847 .rpc_resp = &data->res,
8848 .rpc_cred = data->cred,
8850 struct rpc_task_setup task_setup_data = {
8851 .task = &data->task,
8852 .rpc_client = NFS_CLIENT(data->args.inode),
8853 .rpc_message = &msg,
8854 .callback_ops = &nfs4_layoutcommit_ops,
8855 .callback_data = data,
8857 struct rpc_task *task;
8860 dprintk("NFS: initiating layoutcommit call. sync %d "
8861 "lbw: %llu inode %lu\n", sync,
8862 data->args.lastbytewritten,
8863 data->args.inode->i_ino);
8866 data->inode = nfs_igrab_and_active(data->args.inode);
8867 if (data->inode == NULL) {
8868 nfs4_layoutcommit_release(data);
8871 task_setup_data.flags = RPC_TASK_ASYNC;
8873 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
8874 task = rpc_run_task(&task_setup_data);
8876 return PTR_ERR(task);
8878 status = task->tk_status;
8879 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
8880 dprintk("%s: status %d\n", __func__, status);
8886 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8887 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8890 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8891 struct nfs_fsinfo *info,
8892 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
8894 struct nfs41_secinfo_no_name_args args = {
8895 .style = SECINFO_STYLE_CURRENT_FH,
8897 struct nfs4_secinfo_res res = {
8900 struct rpc_message msg = {
8901 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8905 struct rpc_clnt *clnt = server->client;
8906 struct rpc_cred *cred = NULL;
8909 if (use_integrity) {
8910 clnt = server->nfs_client->cl_rpcclient;
8911 cred = nfs4_get_clid_cred(server->nfs_client);
8912 msg.rpc_cred = cred;
8915 dprintk("--> %s\n", __func__);
8916 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8918 dprintk("<-- %s status=%d\n", __func__, status);
8927 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8928 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8930 struct nfs4_exception exception = { };
8933 /* first try using integrity protection */
8934 err = -NFS4ERR_WRONGSEC;
8936 /* try to use integrity protection with machine cred */
8937 if (_nfs4_is_integrity_protected(server->nfs_client))
8938 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8942 * if unable to use integrity protection, or SECINFO with
8943 * integrity protection returns NFS4ERR_WRONGSEC (which is
8944 * disallowed by spec, but exists in deployed servers) use
8945 * the current filesystem's rpc_client and the user cred.
8947 if (err == -NFS4ERR_WRONGSEC)
8948 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8953 case -NFS4ERR_WRONGSEC:
8957 err = nfs4_handle_exception(server, err, &exception);
8959 } while (exception.retry);
8965 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8966 struct nfs_fsinfo *info)
8970 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
8971 struct nfs4_secinfo_flavors *flavors;
8972 struct nfs4_secinfo4 *secinfo;
8975 page = alloc_page(GFP_KERNEL);
8981 flavors = page_address(page);
8982 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8985 * Fall back on "guess and check" method if
8986 * the server doesn't support SECINFO_NO_NAME
8988 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
8989 err = nfs4_find_root_sec(server, fhandle, info);
8995 for (i = 0; i < flavors->num_flavors; i++) {
8996 secinfo = &flavors->flavors[i];
8998 switch (secinfo->flavor) {
9002 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9003 &secinfo->flavor_info);
9006 flavor = RPC_AUTH_MAXFLAVOR;
9010 if (!nfs_auth_info_match(&server->auth_info, flavor))
9011 flavor = RPC_AUTH_MAXFLAVOR;
9013 if (flavor != RPC_AUTH_MAXFLAVOR) {
9014 err = nfs4_lookup_root_sec(server, fhandle,
9021 if (flavor == RPC_AUTH_MAXFLAVOR)
9032 static int _nfs41_test_stateid(struct nfs_server *server,
9033 nfs4_stateid *stateid,
9034 struct rpc_cred *cred)
9037 struct nfs41_test_stateid_args args = {
9040 struct nfs41_test_stateid_res res;
9041 struct rpc_message msg = {
9042 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9047 struct rpc_clnt *rpc_client = server->client;
9049 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9052 dprintk("NFS call test_stateid %p\n", stateid);
9053 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
9054 nfs4_set_sequence_privileged(&args.seq_args);
9055 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
9056 &args.seq_args, &res.seq_res);
9057 if (status != NFS_OK) {
9058 dprintk("NFS reply test_stateid: failed, %d\n", status);
9061 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
9065 static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9066 int err, struct nfs4_exception *exception)
9068 exception->retry = 0;
9070 case -NFS4ERR_DELAY:
9071 case -NFS4ERR_RETRY_UNCACHED_REP:
9072 nfs4_handle_exception(server, err, exception);
9074 case -NFS4ERR_BADSESSION:
9075 case -NFS4ERR_BADSLOT:
9076 case -NFS4ERR_BAD_HIGH_SLOT:
9077 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9078 case -NFS4ERR_DEADSESSION:
9079 nfs4_do_handle_exception(server, err, exception);
9084 * nfs41_test_stateid - perform a TEST_STATEID operation
9086 * @server: server / transport on which to perform the operation
9087 * @stateid: state ID to test
9090 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9091 * Otherwise a negative NFS4ERR value is returned if the operation
9092 * failed or the state ID is not currently valid.
9094 static int nfs41_test_stateid(struct nfs_server *server,
9095 nfs4_stateid *stateid,
9096 struct rpc_cred *cred)
9098 struct nfs4_exception exception = { };
9101 err = _nfs41_test_stateid(server, stateid, cred);
9102 nfs4_handle_delay_or_session_error(server, err, &exception);
9103 } while (exception.retry);
9107 struct nfs_free_stateid_data {
9108 struct nfs_server *server;
9109 struct nfs41_free_stateid_args args;
9110 struct nfs41_free_stateid_res res;
9113 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9115 struct nfs_free_stateid_data *data = calldata;
9116 nfs41_setup_sequence(nfs4_get_session(data->server),
9117 &data->args.seq_args,
9122 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9124 struct nfs_free_stateid_data *data = calldata;
9126 nfs41_sequence_done(task, &data->res.seq_res);
9128 switch (task->tk_status) {
9129 case -NFS4ERR_DELAY:
9130 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
9131 rpc_restart_call_prepare(task);
9135 static void nfs41_free_stateid_release(void *calldata)
9140 static const struct rpc_call_ops nfs41_free_stateid_ops = {
9141 .rpc_call_prepare = nfs41_free_stateid_prepare,
9142 .rpc_call_done = nfs41_free_stateid_done,
9143 .rpc_release = nfs41_free_stateid_release,
9146 static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
9147 const nfs4_stateid *stateid,
9148 struct rpc_cred *cred,
9151 struct rpc_message msg = {
9152 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
9155 struct rpc_task_setup task_setup = {
9156 .rpc_client = server->client,
9157 .rpc_message = &msg,
9158 .callback_ops = &nfs41_free_stateid_ops,
9159 .flags = RPC_TASK_ASYNC,
9161 struct nfs_free_stateid_data *data;
9163 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9164 &task_setup.rpc_client, &msg);
9166 dprintk("NFS call free_stateid %p\n", stateid);
9167 data = kmalloc(sizeof(*data), GFP_NOFS);
9169 return ERR_PTR(-ENOMEM);
9170 data->server = server;
9171 nfs4_stateid_copy(&data->args.stateid, stateid);
9173 task_setup.callback_data = data;
9175 msg.rpc_argp = &data->args;
9176 msg.rpc_resp = &data->res;
9177 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
9179 nfs4_set_sequence_privileged(&data->args.seq_args);
9181 return rpc_run_task(&task_setup);
9185 * nfs41_free_stateid - perform a FREE_STATEID operation
9187 * @server: server / transport on which to perform the operation
9188 * @stateid: state ID to release
9190 * @is_recovery: set to true if this call needs to be privileged
9192 * Note: this function is always asynchronous.
9194 static int nfs41_free_stateid(struct nfs_server *server,
9195 const nfs4_stateid *stateid,
9196 struct rpc_cred *cred,
9199 struct rpc_task *task;
9201 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
9203 return PTR_ERR(task);
9209 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
9211 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
9213 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
9214 nfs4_free_lock_state(server, lsp);
9217 static bool nfs41_match_stateid(const nfs4_stateid *s1,
9218 const nfs4_stateid *s2)
9220 if (s1->type != s2->type)
9223 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
9226 if (s1->seqid == s2->seqid)
9228 if (s1->seqid == 0 || s2->seqid == 0)
9234 #endif /* CONFIG_NFS_V4_1 */
9236 static bool nfs4_match_stateid(const nfs4_stateid *s1,
9237 const nfs4_stateid *s2)
9239 return nfs4_stateid_match(s1, s2);
9243 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
9244 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9245 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9246 .recover_open = nfs4_open_reclaim,
9247 .recover_lock = nfs4_lock_reclaim,
9248 .establish_clid = nfs4_init_clientid,
9249 .detect_trunking = nfs40_discover_server_trunking,
9252 #if defined(CONFIG_NFS_V4_1)
9253 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
9254 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9255 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9256 .recover_open = nfs4_open_reclaim,
9257 .recover_lock = nfs4_lock_reclaim,
9258 .establish_clid = nfs41_init_clientid,
9259 .reclaim_complete = nfs41_proc_reclaim_complete,
9260 .detect_trunking = nfs41_discover_server_trunking,
9262 #endif /* CONFIG_NFS_V4_1 */
9264 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
9265 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9266 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
9267 .recover_open = nfs40_open_expired,
9268 .recover_lock = nfs4_lock_expired,
9269 .establish_clid = nfs4_init_clientid,
9272 #if defined(CONFIG_NFS_V4_1)
9273 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
9274 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9275 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
9276 .recover_open = nfs41_open_expired,
9277 .recover_lock = nfs41_lock_expired,
9278 .establish_clid = nfs41_init_clientid,
9280 #endif /* CONFIG_NFS_V4_1 */
9282 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
9283 .sched_state_renewal = nfs4_proc_async_renew,
9284 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
9285 .renew_lease = nfs4_proc_renew,
9288 #if defined(CONFIG_NFS_V4_1)
9289 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
9290 .sched_state_renewal = nfs41_proc_async_sequence,
9291 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
9292 .renew_lease = nfs4_proc_sequence,
9296 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
9297 .get_locations = _nfs40_proc_get_locations,
9298 .fsid_present = _nfs40_proc_fsid_present,
9301 #if defined(CONFIG_NFS_V4_1)
9302 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
9303 .get_locations = _nfs41_proc_get_locations,
9304 .fsid_present = _nfs41_proc_fsid_present,
9306 #endif /* CONFIG_NFS_V4_1 */
9308 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9310 .init_caps = NFS_CAP_READDIRPLUS
9311 | NFS_CAP_ATOMIC_OPEN
9312 | NFS_CAP_POSIX_LOCK,
9313 .init_client = nfs40_init_client,
9314 .shutdown_client = nfs40_shutdown_client,
9315 .match_stateid = nfs4_match_stateid,
9316 .find_root_sec = nfs4_find_root_sec,
9317 .free_lock_state = nfs4_release_lockowner,
9318 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
9319 .alloc_seqid = nfs_alloc_seqid,
9320 .call_sync_ops = &nfs40_call_sync_ops,
9321 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9322 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9323 .state_renewal_ops = &nfs40_state_renewal_ops,
9324 .mig_recovery_ops = &nfs40_mig_recovery_ops,
9327 #if defined(CONFIG_NFS_V4_1)
9328 static struct nfs_seqid *
9329 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9334 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9336 .init_caps = NFS_CAP_READDIRPLUS
9337 | NFS_CAP_ATOMIC_OPEN
9338 | NFS_CAP_POSIX_LOCK
9339 | NFS_CAP_STATEID_NFSV41
9340 | NFS_CAP_ATOMIC_OPEN_V1,
9341 .init_client = nfs41_init_client,
9342 .shutdown_client = nfs41_shutdown_client,
9343 .match_stateid = nfs41_match_stateid,
9344 .find_root_sec = nfs41_find_root_sec,
9345 .free_lock_state = nfs41_free_lock_state,
9346 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
9347 .alloc_seqid = nfs_alloc_no_seqid,
9348 .session_trunk = nfs4_test_session_trunk,
9349 .call_sync_ops = &nfs41_call_sync_ops,
9350 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9351 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9352 .state_renewal_ops = &nfs41_state_renewal_ops,
9353 .mig_recovery_ops = &nfs41_mig_recovery_ops,
9357 #if defined(CONFIG_NFS_V4_2)
9358 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9360 .init_caps = NFS_CAP_READDIRPLUS
9361 | NFS_CAP_ATOMIC_OPEN
9362 | NFS_CAP_POSIX_LOCK
9363 | NFS_CAP_STATEID_NFSV41
9364 | NFS_CAP_ATOMIC_OPEN_V1
9367 | NFS_CAP_DEALLOCATE
9369 | NFS_CAP_LAYOUTSTATS
9371 .init_client = nfs41_init_client,
9372 .shutdown_client = nfs41_shutdown_client,
9373 .match_stateid = nfs41_match_stateid,
9374 .find_root_sec = nfs41_find_root_sec,
9375 .free_lock_state = nfs41_free_lock_state,
9376 .call_sync_ops = &nfs41_call_sync_ops,
9377 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
9378 .alloc_seqid = nfs_alloc_no_seqid,
9379 .session_trunk = nfs4_test_session_trunk,
9380 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9381 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9382 .state_renewal_ops = &nfs41_state_renewal_ops,
9383 .mig_recovery_ops = &nfs41_mig_recovery_ops,
9387 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9388 [0] = &nfs_v4_0_minor_ops,
9389 #if defined(CONFIG_NFS_V4_1)
9390 [1] = &nfs_v4_1_minor_ops,
9392 #if defined(CONFIG_NFS_V4_2)
9393 [2] = &nfs_v4_2_minor_ops,
9397 static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
9399 ssize_t error, error2;
9401 error = generic_listxattr(dentry, list, size);
9409 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9412 return error + error2;
9415 static const struct inode_operations nfs4_dir_inode_operations = {
9416 .create = nfs_create,
9417 .lookup = nfs_lookup,
9418 .atomic_open = nfs_atomic_open,
9420 .unlink = nfs_unlink,
9421 .symlink = nfs_symlink,
9425 .rename = nfs_rename,
9426 .permission = nfs_permission,
9427 .getattr = nfs_getattr,
9428 .setattr = nfs_setattr,
9429 .listxattr = nfs4_listxattr,
9432 static const struct inode_operations nfs4_file_inode_operations = {
9433 .permission = nfs_permission,
9434 .getattr = nfs_getattr,
9435 .setattr = nfs_setattr,
9436 .listxattr = nfs4_listxattr,
9439 const struct nfs_rpc_ops nfs_v4_clientops = {
9440 .version = 4, /* protocol version */
9441 .dentry_ops = &nfs4_dentry_operations,
9442 .dir_inode_ops = &nfs4_dir_inode_operations,
9443 .file_inode_ops = &nfs4_file_inode_operations,
9444 .file_ops = &nfs4_file_operations,
9445 .getroot = nfs4_proc_get_root,
9446 .submount = nfs4_submount,
9447 .try_mount = nfs4_try_mount,
9448 .getattr = nfs4_proc_getattr,
9449 .setattr = nfs4_proc_setattr,
9450 .lookup = nfs4_proc_lookup,
9451 .access = nfs4_proc_access,
9452 .readlink = nfs4_proc_readlink,
9453 .create = nfs4_proc_create,
9454 .remove = nfs4_proc_remove,
9455 .unlink_setup = nfs4_proc_unlink_setup,
9456 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
9457 .unlink_done = nfs4_proc_unlink_done,
9458 .rename_setup = nfs4_proc_rename_setup,
9459 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
9460 .rename_done = nfs4_proc_rename_done,
9461 .link = nfs4_proc_link,
9462 .symlink = nfs4_proc_symlink,
9463 .mkdir = nfs4_proc_mkdir,
9464 .rmdir = nfs4_proc_remove,
9465 .readdir = nfs4_proc_readdir,
9466 .mknod = nfs4_proc_mknod,
9467 .statfs = nfs4_proc_statfs,
9468 .fsinfo = nfs4_proc_fsinfo,
9469 .pathconf = nfs4_proc_pathconf,
9470 .set_capabilities = nfs4_server_capabilities,
9471 .decode_dirent = nfs4_decode_dirent,
9472 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
9473 .read_setup = nfs4_proc_read_setup,
9474 .read_done = nfs4_read_done,
9475 .write_setup = nfs4_proc_write_setup,
9476 .write_done = nfs4_write_done,
9477 .commit_setup = nfs4_proc_commit_setup,
9478 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
9479 .commit_done = nfs4_commit_done,
9480 .lock = nfs4_proc_lock,
9481 .clear_acl_cache = nfs4_zap_acl_attr,
9482 .close_context = nfs4_close_context,
9483 .open_context = nfs4_atomic_open,
9484 .have_delegation = nfs4_have_delegation,
9485 .return_delegation = nfs4_inode_return_delegation,
9486 .alloc_client = nfs4_alloc_client,
9487 .init_client = nfs4_init_client,
9488 .free_client = nfs4_free_client,
9489 .create_server = nfs4_create_server,
9490 .clone_server = nfs_clone_server,
9493 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
9494 .name = XATTR_NAME_NFSV4_ACL,
9495 .list = nfs4_xattr_list_nfs4_acl,
9496 .get = nfs4_xattr_get_nfs4_acl,
9497 .set = nfs4_xattr_set_nfs4_acl,
9500 const struct xattr_handler *nfs4_xattr_handlers[] = {
9501 &nfs4_xattr_nfs4_acl_handler,
9502 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
9503 &nfs4_xattr_nfs4_label_handler,