4 * Client-side procedure declarations for NFSv4.
6 * Copyright (c) 2002 The Regents of the University of Michigan.
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <linux/delay.h>
40 #include <linux/errno.h>
41 #include <linux/file.h>
42 #include <linux/string.h>
43 #include <linux/ratelimit.h>
44 #include <linux/printk.h>
45 #include <linux/slab.h>
46 #include <linux/sunrpc/clnt.h>
47 #include <linux/nfs.h>
48 #include <linux/nfs4.h>
49 #include <linux/nfs_fs.h>
50 #include <linux/nfs_page.h>
51 #include <linux/nfs_mount.h>
52 #include <linux/namei.h>
53 #include <linux/mount.h>
54 #include <linux/module.h>
55 #include <linux/xattr.h>
56 #include <linux/utsname.h>
57 #include <linux/freezer.h>
60 #include "delegation.h"
66 #include "nfs4idmap.h"
67 #include "nfs4session.h"
70 #include "nfs4trace.h"
72 #define NFSDBG_FACILITY NFSDBG_PROC
74 #define NFS4_POLL_RETRY_MIN (HZ/10)
75 #define NFS4_POLL_RETRY_MAX (15*HZ)
77 /* file attributes which can be mapped to nfs attributes */
78 #define NFS4_VALID_ATTRS (ATTR_MODE \
89 static int _nfs4_proc_open(struct nfs4_opendata *data);
90 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
91 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
92 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
93 static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
94 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label);
95 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
96 struct nfs_fattr *fattr, struct iattr *sattr,
97 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
98 struct nfs4_label *olabel);
99 #ifdef CONFIG_NFS_V4_1
100 static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
102 static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
103 struct rpc_cred *, bool);
106 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
107 static inline struct nfs4_label *
108 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
109 struct iattr *sattr, struct nfs4_label *label)
116 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
119 err = security_dentry_init_security(dentry, sattr->ia_mode,
120 &dentry->d_name, (void **)&label->label, &label->len);
127 nfs4_label_release_security(struct nfs4_label *label)
130 security_release_secctx(label->label, label->len);
132 static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
135 return server->attr_bitmask;
137 return server->attr_bitmask_nl;
140 static inline struct nfs4_label *
141 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
142 struct iattr *sattr, struct nfs4_label *l)
145 nfs4_label_release_security(struct nfs4_label *label)
148 nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
149 { return server->attr_bitmask; }
152 /* Prevent leaks of NFSv4 errors into userland */
153 static int nfs4_map_errors(int err)
158 case -NFS4ERR_RESOURCE:
159 case -NFS4ERR_LAYOUTTRYLATER:
160 case -NFS4ERR_RECALLCONFLICT:
162 case -NFS4ERR_WRONGSEC:
163 case -NFS4ERR_WRONG_CRED:
165 case -NFS4ERR_BADOWNER:
166 case -NFS4ERR_BADNAME:
168 case -NFS4ERR_SHARE_DENIED:
170 case -NFS4ERR_MINOR_VERS_MISMATCH:
171 return -EPROTONOSUPPORT;
172 case -NFS4ERR_FILE_OPEN:
175 dprintk("%s could not handle NFSv4 error %d\n",
183 * This is our standard bitmap for GETATTR requests.
185 const u32 nfs4_fattr_bitmap[3] = {
187 | FATTR4_WORD0_CHANGE
190 | FATTR4_WORD0_FILEID,
192 | FATTR4_WORD1_NUMLINKS
194 | FATTR4_WORD1_OWNER_GROUP
195 | FATTR4_WORD1_RAWDEV
196 | FATTR4_WORD1_SPACE_USED
197 | FATTR4_WORD1_TIME_ACCESS
198 | FATTR4_WORD1_TIME_METADATA
199 | FATTR4_WORD1_TIME_MODIFY
200 | FATTR4_WORD1_MOUNTED_ON_FILEID,
201 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
202 FATTR4_WORD2_SECURITY_LABEL
206 static const u32 nfs4_pnfs_open_bitmap[3] = {
208 | FATTR4_WORD0_CHANGE
211 | FATTR4_WORD0_FILEID,
213 | FATTR4_WORD1_NUMLINKS
215 | FATTR4_WORD1_OWNER_GROUP
216 | FATTR4_WORD1_RAWDEV
217 | FATTR4_WORD1_SPACE_USED
218 | FATTR4_WORD1_TIME_ACCESS
219 | FATTR4_WORD1_TIME_METADATA
220 | FATTR4_WORD1_TIME_MODIFY,
221 FATTR4_WORD2_MDSTHRESHOLD
222 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
223 | FATTR4_WORD2_SECURITY_LABEL
227 static const u32 nfs4_open_noattr_bitmap[3] = {
229 | FATTR4_WORD0_FILEID,
232 const u32 nfs4_statfs_bitmap[3] = {
233 FATTR4_WORD0_FILES_AVAIL
234 | FATTR4_WORD0_FILES_FREE
235 | FATTR4_WORD0_FILES_TOTAL,
236 FATTR4_WORD1_SPACE_AVAIL
237 | FATTR4_WORD1_SPACE_FREE
238 | FATTR4_WORD1_SPACE_TOTAL
241 const u32 nfs4_pathconf_bitmap[3] = {
243 | FATTR4_WORD0_MAXNAME,
247 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
248 | FATTR4_WORD0_MAXREAD
249 | FATTR4_WORD0_MAXWRITE
250 | FATTR4_WORD0_LEASE_TIME,
251 FATTR4_WORD1_TIME_DELTA
252 | FATTR4_WORD1_FS_LAYOUT_TYPES,
253 FATTR4_WORD2_LAYOUT_BLKSIZE
254 | FATTR4_WORD2_CLONE_BLKSIZE
257 const u32 nfs4_fs_locations_bitmap[3] = {
259 | FATTR4_WORD0_CHANGE
262 | FATTR4_WORD0_FILEID
263 | FATTR4_WORD0_FS_LOCATIONS,
265 | FATTR4_WORD1_NUMLINKS
267 | FATTR4_WORD1_OWNER_GROUP
268 | FATTR4_WORD1_RAWDEV
269 | FATTR4_WORD1_SPACE_USED
270 | FATTR4_WORD1_TIME_ACCESS
271 | FATTR4_WORD1_TIME_METADATA
272 | FATTR4_WORD1_TIME_MODIFY
273 | FATTR4_WORD1_MOUNTED_ON_FILEID,
276 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
277 struct nfs4_readdir_arg *readdir)
282 readdir->cookie = cookie;
283 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
288 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
293 * NFSv4 servers do not return entries for '.' and '..'
294 * Therefore, we fake these entries here. We let '.'
295 * have cookie 0 and '..' have cookie 1. Note that
296 * when talking to the server, we always send cookie 0
299 start = p = kmap_atomic(*readdir->pages);
302 *p++ = xdr_one; /* next */
303 *p++ = xdr_zero; /* cookie, first word */
304 *p++ = xdr_one; /* cookie, second word */
305 *p++ = xdr_one; /* entry len */
306 memcpy(p, ".\0\0\0", 4); /* entry */
308 *p++ = xdr_one; /* bitmap length */
309 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
310 *p++ = htonl(8); /* attribute buffer length */
311 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
314 *p++ = xdr_one; /* next */
315 *p++ = xdr_zero; /* cookie, first word */
316 *p++ = xdr_two; /* cookie, second word */
317 *p++ = xdr_two; /* entry len */
318 memcpy(p, "..\0\0", 4); /* entry */
320 *p++ = xdr_one; /* bitmap length */
321 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
322 *p++ = htonl(8); /* attribute buffer length */
323 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
325 readdir->pgbase = (char *)p - (char *)start;
326 readdir->count -= readdir->pgbase;
327 kunmap_atomic(start);
330 static void nfs4_test_and_free_stateid(struct nfs_server *server,
331 nfs4_stateid *stateid,
332 struct rpc_cred *cred)
334 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
336 ops->test_and_free_expired(server, stateid, cred);
339 static void __nfs4_free_revoked_stateid(struct nfs_server *server,
340 nfs4_stateid *stateid,
341 struct rpc_cred *cred)
343 stateid->type = NFS4_REVOKED_STATEID_TYPE;
344 nfs4_test_and_free_stateid(server, stateid, cred);
347 static void nfs4_free_revoked_stateid(struct nfs_server *server,
348 const nfs4_stateid *stateid,
349 struct rpc_cred *cred)
353 nfs4_stateid_copy(&tmp, stateid);
354 __nfs4_free_revoked_stateid(server, &tmp, cred);
357 static long nfs4_update_delay(long *timeout)
361 return NFS4_POLL_RETRY_MAX;
363 *timeout = NFS4_POLL_RETRY_MIN;
364 if (*timeout > NFS4_POLL_RETRY_MAX)
365 *timeout = NFS4_POLL_RETRY_MAX;
371 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
377 freezable_schedule_timeout_killable_unsafe(
378 nfs4_update_delay(timeout));
379 if (fatal_signal_pending(current))
384 /* This is the error handling routine for processes that are allowed
387 static int nfs4_do_handle_exception(struct nfs_server *server,
388 int errorcode, struct nfs4_exception *exception)
390 struct nfs_client *clp = server->nfs_client;
391 struct nfs4_state *state = exception->state;
392 const nfs4_stateid *stateid = exception->stateid;
393 struct inode *inode = exception->inode;
396 exception->delay = 0;
397 exception->recovering = 0;
398 exception->retry = 0;
400 if (stateid == NULL && state != NULL)
401 stateid = &state->stateid;
406 case -NFS4ERR_DELEG_REVOKED:
407 case -NFS4ERR_ADMIN_REVOKED:
408 case -NFS4ERR_EXPIRED:
409 case -NFS4ERR_BAD_STATEID:
410 if (inode != NULL && stateid != NULL) {
411 nfs_inode_find_state_and_recover(inode,
413 goto wait_on_recovery;
415 case -NFS4ERR_OPENMODE:
419 err = nfs_async_inode_return_delegation(inode,
422 goto wait_on_recovery;
423 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
424 exception->retry = 1;
430 ret = nfs4_schedule_stateid_recovery(server, state);
433 goto wait_on_recovery;
434 case -NFS4ERR_STALE_STATEID:
435 case -NFS4ERR_STALE_CLIENTID:
436 nfs4_schedule_lease_recovery(clp);
437 goto wait_on_recovery;
439 ret = nfs4_schedule_migration_recovery(server);
442 goto wait_on_recovery;
443 case -NFS4ERR_LEASE_MOVED:
444 nfs4_schedule_lease_moved_recovery(clp);
445 goto wait_on_recovery;
446 #if defined(CONFIG_NFS_V4_1)
447 case -NFS4ERR_BADSESSION:
448 case -NFS4ERR_BADSLOT:
449 case -NFS4ERR_BAD_HIGH_SLOT:
450 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
451 case -NFS4ERR_DEADSESSION:
452 case -NFS4ERR_SEQ_FALSE_RETRY:
453 case -NFS4ERR_SEQ_MISORDERED:
454 dprintk("%s ERROR: %d Reset session\n", __func__,
456 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
457 goto wait_on_recovery;
458 #endif /* defined(CONFIG_NFS_V4_1) */
459 case -NFS4ERR_FILE_OPEN:
460 if (exception->timeout > HZ) {
461 /* We have retried a decent amount, time to
468 nfs_inc_server_stats(server, NFSIOS_DELAY);
470 case -NFS4ERR_LAYOUTTRYLATER:
471 case -NFS4ERR_RECALLCONFLICT:
472 exception->delay = 1;
475 case -NFS4ERR_RETRY_UNCACHED_REP:
476 case -NFS4ERR_OLD_STATEID:
477 exception->retry = 1;
479 case -NFS4ERR_BADOWNER:
480 /* The following works around a Linux server bug! */
481 case -NFS4ERR_BADNAME:
482 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
483 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
484 exception->retry = 1;
485 printk(KERN_WARNING "NFS: v4 server %s "
486 "does not accept raw "
488 "Reenabling the idmapper.\n",
489 server->nfs_client->cl_hostname);
492 /* We failed to handle the error */
493 return nfs4_map_errors(ret);
495 exception->recovering = 1;
499 /* This is the error handling routine for processes that are allowed
502 int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
504 struct nfs_client *clp = server->nfs_client;
507 ret = nfs4_do_handle_exception(server, errorcode, exception);
508 if (exception->delay) {
509 ret = nfs4_delay(server->client, &exception->timeout);
512 if (exception->recovering) {
513 ret = nfs4_wait_clnt_recover(clp);
514 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
521 exception->retry = 1;
526 nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
527 int errorcode, struct nfs4_exception *exception)
529 struct nfs_client *clp = server->nfs_client;
532 ret = nfs4_do_handle_exception(server, errorcode, exception);
533 if (exception->delay) {
534 rpc_delay(task, nfs4_update_delay(&exception->timeout));
537 if (exception->recovering) {
538 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
539 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
540 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
543 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
548 exception->retry = 1;
553 nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
554 struct nfs4_state *state, long *timeout)
556 struct nfs4_exception exception = {
560 if (task->tk_status >= 0)
563 exception.timeout = *timeout;
564 task->tk_status = nfs4_async_handle_exception(task, server,
567 if (exception.delay && timeout)
568 *timeout = exception.timeout;
575 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
576 * or 'false' otherwise.
578 static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
580 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
582 if (flavor == RPC_AUTH_GSS_KRB5I ||
583 flavor == RPC_AUTH_GSS_KRB5P)
589 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
591 spin_lock(&clp->cl_lock);
592 if (time_before(clp->cl_last_renewal,timestamp))
593 clp->cl_last_renewal = timestamp;
594 spin_unlock(&clp->cl_lock);
597 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
599 struct nfs_client *clp = server->nfs_client;
601 if (!nfs4_has_session(clp))
602 do_renew_lease(clp, timestamp);
605 struct nfs4_call_sync_data {
606 const struct nfs_server *seq_server;
607 struct nfs4_sequence_args *seq_args;
608 struct nfs4_sequence_res *seq_res;
611 void nfs4_init_sequence(struct nfs4_sequence_args *args,
612 struct nfs4_sequence_res *res, int cache_reply)
614 args->sa_slot = NULL;
615 args->sa_cache_this = cache_reply;
616 args->sa_privileged = 0;
621 static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
623 args->sa_privileged = 1;
626 int nfs40_setup_sequence(struct nfs4_slot_table *tbl,
627 struct nfs4_sequence_args *args,
628 struct nfs4_sequence_res *res,
629 struct rpc_task *task)
631 struct nfs4_slot *slot;
633 /* slot already allocated? */
634 if (res->sr_slot != NULL)
637 spin_lock(&tbl->slot_tbl_lock);
638 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
641 slot = nfs4_alloc_slot(tbl);
643 if (slot == ERR_PTR(-ENOMEM))
644 task->tk_timeout = HZ >> 2;
647 spin_unlock(&tbl->slot_tbl_lock);
649 slot->privileged = args->sa_privileged ? 1 : 0;
650 args->sa_slot = slot;
654 rpc_call_start(task);
658 if (args->sa_privileged)
659 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
660 NULL, RPC_PRIORITY_PRIVILEGED);
662 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
663 spin_unlock(&tbl->slot_tbl_lock);
666 EXPORT_SYMBOL_GPL(nfs40_setup_sequence);
668 static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
670 struct nfs4_slot *slot = res->sr_slot;
671 struct nfs4_slot_table *tbl;
674 spin_lock(&tbl->slot_tbl_lock);
675 if (!nfs41_wake_and_assign_slot(tbl, slot))
676 nfs4_free_slot(tbl, slot);
677 spin_unlock(&tbl->slot_tbl_lock);
682 static int nfs40_sequence_done(struct rpc_task *task,
683 struct nfs4_sequence_res *res)
685 if (res->sr_slot != NULL)
686 nfs40_sequence_free_slot(res);
690 #if defined(CONFIG_NFS_V4_1)
692 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
694 struct nfs4_session *session;
695 struct nfs4_slot_table *tbl;
696 struct nfs4_slot *slot = res->sr_slot;
697 bool send_new_highest_used_slotid = false;
700 session = tbl->session;
702 /* Bump the slot sequence number */
707 spin_lock(&tbl->slot_tbl_lock);
708 /* Be nice to the server: try to ensure that the last transmitted
709 * value for highest_user_slotid <= target_highest_slotid
711 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
712 send_new_highest_used_slotid = true;
714 if (nfs41_wake_and_assign_slot(tbl, slot)) {
715 send_new_highest_used_slotid = false;
718 nfs4_free_slot(tbl, slot);
720 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
721 send_new_highest_used_slotid = false;
723 spin_unlock(&tbl->slot_tbl_lock);
725 if (send_new_highest_used_slotid)
726 nfs41_notify_server(session->clp);
727 if (waitqueue_active(&tbl->slot_waitq))
728 wake_up_all(&tbl->slot_waitq);
731 static int nfs41_sequence_process(struct rpc_task *task,
732 struct nfs4_sequence_res *res)
734 struct nfs4_session *session;
735 struct nfs4_slot *slot = res->sr_slot;
736 struct nfs_client *clp;
737 bool interrupted = false;
742 /* don't increment the sequence number if the task wasn't sent */
743 if (!RPC_WAS_SENT(task))
746 session = slot->table->session;
748 if (slot->interrupted) {
749 slot->interrupted = 0;
753 trace_nfs4_sequence_done(session, res);
754 /* Check the SEQUENCE operation status */
755 switch (res->sr_status) {
757 /* If previous op on slot was interrupted and we reused
758 * the seq# and got a reply from the cache, then retry
760 if (task->tk_status == -EREMOTEIO && interrupted) {
764 /* Update the slot's sequence and clientid lease timer */
767 do_renew_lease(clp, res->sr_timestamp);
768 /* Check sequence flags */
769 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
771 nfs41_update_target_slotid(slot->table, slot, res);
775 * sr_status remains 1 if an RPC level error occurred.
776 * The server may or may not have processed the sequence
778 * Mark the slot as having hosted an interrupted RPC call.
780 slot->interrupted = 1;
783 /* The server detected a resend of the RPC call and
784 * returned NFS4ERR_DELAY as per Section 2.10.6.2
787 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
792 case -NFS4ERR_BADSLOT:
794 * The slot id we used was probably retired. Try again
795 * using a different slot id.
798 case -NFS4ERR_SEQ_MISORDERED:
800 * Was the last operation on this sequence interrupted?
801 * If so, retry after bumping the sequence number.
808 * Could this slot have been previously retired?
809 * If so, then the server may be expecting seq_nr = 1!
811 if (slot->seq_nr != 1) {
816 case -NFS4ERR_SEQ_FALSE_RETRY:
819 case -NFS4ERR_DEADSESSION:
820 case -NFS4ERR_BADSESSION:
821 nfs4_schedule_session_recovery(session, res->sr_status);
824 /* Just update the slot sequence no. */
828 /* The session may be reset by one of the error handlers. */
829 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
833 if (rpc_restart_call_prepare(task)) {
834 nfs41_sequence_free_slot(res);
840 if (!rpc_restart_call(task))
842 rpc_delay(task, NFS4_POLL_RETRY_MAX);
846 int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
848 if (!nfs41_sequence_process(task, res))
850 if (res->sr_slot != NULL)
851 nfs41_sequence_free_slot(res);
855 EXPORT_SYMBOL_GPL(nfs41_sequence_done);
857 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
859 if (res->sr_slot == NULL)
861 if (res->sr_slot->table->session != NULL)
862 return nfs41_sequence_process(task, res);
863 return nfs40_sequence_done(task, res);
866 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
868 if (res->sr_slot != NULL) {
869 if (res->sr_slot->table->session != NULL)
870 nfs41_sequence_free_slot(res);
872 nfs40_sequence_free_slot(res);
876 int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
878 if (res->sr_slot == NULL)
880 if (!res->sr_slot->table->session)
881 return nfs40_sequence_done(task, res);
882 return nfs41_sequence_done(task, res);
884 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
886 int nfs41_setup_sequence(struct nfs4_session *session,
887 struct nfs4_sequence_args *args,
888 struct nfs4_sequence_res *res,
889 struct rpc_task *task)
891 struct nfs4_slot *slot;
892 struct nfs4_slot_table *tbl;
894 dprintk("--> %s\n", __func__);
895 /* slot already allocated? */
896 if (res->sr_slot != NULL)
899 tbl = &session->fc_slot_table;
901 task->tk_timeout = 0;
903 spin_lock(&tbl->slot_tbl_lock);
904 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
905 !args->sa_privileged) {
906 /* The state manager will wait until the slot table is empty */
907 dprintk("%s session is draining\n", __func__);
911 slot = nfs4_alloc_slot(tbl);
913 /* If out of memory, try again in 1/4 second */
914 if (slot == ERR_PTR(-ENOMEM))
915 task->tk_timeout = HZ >> 2;
916 dprintk("<-- %s: no free slots\n", __func__);
919 spin_unlock(&tbl->slot_tbl_lock);
921 slot->privileged = args->sa_privileged ? 1 : 0;
922 args->sa_slot = slot;
924 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
925 slot->slot_nr, slot->seq_nr);
928 res->sr_timestamp = jiffies;
929 res->sr_status_flags = 0;
931 * sr_status is only set in decode_sequence, and so will remain
932 * set to 1 if an rpc level failure occurs.
935 trace_nfs4_setup_sequence(session, args);
937 rpc_call_start(task);
940 /* Privileged tasks are queued with top priority */
941 if (args->sa_privileged)
942 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
943 NULL, RPC_PRIORITY_PRIVILEGED);
945 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
946 spin_unlock(&tbl->slot_tbl_lock);
949 EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
951 static int nfs4_setup_sequence(const struct nfs_server *server,
952 struct nfs4_sequence_args *args,
953 struct nfs4_sequence_res *res,
954 struct rpc_task *task)
956 struct nfs4_session *session = nfs4_get_session(server);
960 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
963 dprintk("--> %s clp %p session %p sr_slot %u\n",
964 __func__, session->clp, session, res->sr_slot ?
965 res->sr_slot->slot_nr : NFS4_NO_SLOT);
967 ret = nfs41_setup_sequence(session, args, res, task);
969 dprintk("<-- %s status=%d\n", __func__, ret);
973 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
975 struct nfs4_call_sync_data *data = calldata;
976 struct nfs4_session *session = nfs4_get_session(data->seq_server);
978 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
980 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
983 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
985 struct nfs4_call_sync_data *data = calldata;
987 nfs41_sequence_done(task, data->seq_res);
990 static const struct rpc_call_ops nfs41_call_sync_ops = {
991 .rpc_call_prepare = nfs41_call_sync_prepare,
992 .rpc_call_done = nfs41_call_sync_done,
995 #else /* !CONFIG_NFS_V4_1 */
997 static int nfs4_setup_sequence(const struct nfs_server *server,
998 struct nfs4_sequence_args *args,
999 struct nfs4_sequence_res *res,
1000 struct rpc_task *task)
1002 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
1006 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
1008 return nfs40_sequence_done(task, res);
1011 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
1013 if (res->sr_slot != NULL)
1014 nfs40_sequence_free_slot(res);
1017 int nfs4_sequence_done(struct rpc_task *task,
1018 struct nfs4_sequence_res *res)
1020 return nfs40_sequence_done(task, res);
1022 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
1024 #endif /* !CONFIG_NFS_V4_1 */
1026 static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1028 struct nfs4_call_sync_data *data = calldata;
1029 nfs4_setup_sequence(data->seq_server,
1030 data->seq_args, data->seq_res, task);
1033 static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1035 struct nfs4_call_sync_data *data = calldata;
1036 nfs4_sequence_done(task, data->seq_res);
1039 static const struct rpc_call_ops nfs40_call_sync_ops = {
1040 .rpc_call_prepare = nfs40_call_sync_prepare,
1041 .rpc_call_done = nfs40_call_sync_done,
1044 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
1045 struct nfs_server *server,
1046 struct rpc_message *msg,
1047 struct nfs4_sequence_args *args,
1048 struct nfs4_sequence_res *res)
1051 struct rpc_task *task;
1052 struct nfs_client *clp = server->nfs_client;
1053 struct nfs4_call_sync_data data = {
1054 .seq_server = server,
1058 struct rpc_task_setup task_setup = {
1061 .callback_ops = clp->cl_mvops->call_sync_ops,
1062 .callback_data = &data
1065 task = rpc_run_task(&task_setup);
1067 ret = PTR_ERR(task);
1069 ret = task->tk_status;
1075 int nfs4_call_sync(struct rpc_clnt *clnt,
1076 struct nfs_server *server,
1077 struct rpc_message *msg,
1078 struct nfs4_sequence_args *args,
1079 struct nfs4_sequence_res *res,
1082 nfs4_init_sequence(args, res, cache_reply);
1083 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
1086 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
1088 struct nfs_inode *nfsi = NFS_I(dir);
1090 spin_lock(&dir->i_lock);
1091 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
1092 if (!cinfo->atomic || cinfo->before != dir->i_version)
1093 nfs_force_lookup_revalidate(dir);
1094 dir->i_version = cinfo->after;
1095 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
1096 nfs_fscache_invalidate(dir);
1097 spin_unlock(&dir->i_lock);
1100 struct nfs4_opendata {
1102 struct nfs_openargs o_arg;
1103 struct nfs_openres o_res;
1104 struct nfs_open_confirmargs c_arg;
1105 struct nfs_open_confirmres c_res;
1106 struct nfs4_string owner_name;
1107 struct nfs4_string group_name;
1108 struct nfs4_label *a_label;
1109 struct nfs_fattr f_attr;
1110 struct nfs4_label *f_label;
1112 struct dentry *dentry;
1113 struct nfs4_state_owner *owner;
1114 struct nfs4_state *state;
1116 unsigned long timestamp;
1117 unsigned int rpc_done : 1;
1118 unsigned int file_created : 1;
1119 unsigned int is_recover : 1;
1124 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1125 int err, struct nfs4_exception *exception)
1129 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1131 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1132 exception->retry = 1;
1137 nfs4_map_atomic_open_share(struct nfs_server *server,
1138 fmode_t fmode, int openflags)
1142 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1144 res = NFS4_SHARE_ACCESS_READ;
1147 res = NFS4_SHARE_ACCESS_WRITE;
1149 case FMODE_READ|FMODE_WRITE:
1150 res = NFS4_SHARE_ACCESS_BOTH;
1152 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1154 /* Want no delegation if we're using O_DIRECT */
1155 if (openflags & O_DIRECT)
1156 res |= NFS4_SHARE_WANT_NO_DELEG;
1161 static enum open_claim_type4
1162 nfs4_map_atomic_open_claim(struct nfs_server *server,
1163 enum open_claim_type4 claim)
1165 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1170 case NFS4_OPEN_CLAIM_FH:
1171 return NFS4_OPEN_CLAIM_NULL;
1172 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1173 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1174 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1175 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1179 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
1181 p->o_res.f_attr = &p->f_attr;
1182 p->o_res.f_label = p->f_label;
1183 p->o_res.seqid = p->o_arg.seqid;
1184 p->c_res.seqid = p->c_arg.seqid;
1185 p->o_res.server = p->o_arg.server;
1186 p->o_res.access_request = p->o_arg.access;
1187 nfs_fattr_init(&p->f_attr);
1188 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
1191 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
1192 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
1193 const struct iattr *attrs,
1194 struct nfs4_label *label,
1195 enum open_claim_type4 claim,
1198 struct dentry *parent = dget_parent(dentry);
1199 struct inode *dir = d_inode(parent);
1200 struct nfs_server *server = NFS_SERVER(dir);
1201 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
1202 struct nfs4_opendata *p;
1204 p = kzalloc(sizeof(*p), gfp_mask);
1208 p->f_label = nfs4_label_alloc(server, gfp_mask);
1209 if (IS_ERR(p->f_label))
1212 p->a_label = nfs4_label_alloc(server, gfp_mask);
1213 if (IS_ERR(p->a_label))
1216 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1217 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
1218 if (IS_ERR(p->o_arg.seqid))
1219 goto err_free_label;
1220 nfs_sb_active(dentry->d_sb);
1221 p->dentry = dget(dentry);
1224 atomic_inc(&sp->so_count);
1225 p->o_arg.open_flags = flags;
1226 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
1227 p->o_arg.umask = current_umask();
1228 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1229 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1231 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1232 * will return permission denied for all bits until close */
1233 if (!(flags & O_EXCL)) {
1234 /* ask server to check for all possible rights as results
1236 switch (p->o_arg.claim) {
1239 case NFS4_OPEN_CLAIM_NULL:
1240 case NFS4_OPEN_CLAIM_FH:
1241 p->o_arg.access = NFS4_ACCESS_READ |
1242 NFS4_ACCESS_MODIFY |
1243 NFS4_ACCESS_EXTEND |
1244 NFS4_ACCESS_EXECUTE;
1247 p->o_arg.clientid = server->nfs_client->cl_clientid;
1248 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1249 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1250 p->o_arg.name = &dentry->d_name;
1251 p->o_arg.server = server;
1252 p->o_arg.bitmask = nfs4_bitmask(server, label);
1253 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1254 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1255 switch (p->o_arg.claim) {
1256 case NFS4_OPEN_CLAIM_NULL:
1257 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1258 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1259 p->o_arg.fh = NFS_FH(dir);
1261 case NFS4_OPEN_CLAIM_PREVIOUS:
1262 case NFS4_OPEN_CLAIM_FH:
1263 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1264 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1265 p->o_arg.fh = NFS_FH(d_inode(dentry));
1267 if (attrs != NULL && attrs->ia_valid != 0) {
1270 p->o_arg.u.attrs = &p->attrs;
1271 memcpy(&p->attrs, attrs, sizeof(p->attrs));
1274 verf[1] = current->pid;
1275 memcpy(p->o_arg.u.verifier.data, verf,
1276 sizeof(p->o_arg.u.verifier.data));
1278 p->c_arg.fh = &p->o_res.fh;
1279 p->c_arg.stateid = &p->o_res.stateid;
1280 p->c_arg.seqid = p->o_arg.seqid;
1281 nfs4_init_opendata_res(p);
1282 kref_init(&p->kref);
1286 nfs4_label_free(p->a_label);
1288 nfs4_label_free(p->f_label);
1296 static void nfs4_opendata_free(struct kref *kref)
1298 struct nfs4_opendata *p = container_of(kref,
1299 struct nfs4_opendata, kref);
1300 struct super_block *sb = p->dentry->d_sb;
1302 nfs_free_seqid(p->o_arg.seqid);
1303 nfs4_sequence_free_slot(&p->o_res.seq_res);
1304 if (p->state != NULL)
1305 nfs4_put_open_state(p->state);
1306 nfs4_put_state_owner(p->owner);
1308 nfs4_label_free(p->a_label);
1309 nfs4_label_free(p->f_label);
1313 nfs_sb_deactive(sb);
1314 nfs_fattr_free_names(&p->f_attr);
1315 kfree(p->f_attr.mdsthreshold);
1319 static void nfs4_opendata_put(struct nfs4_opendata *p)
1322 kref_put(&p->kref, nfs4_opendata_free);
1325 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1329 ret = rpc_wait_for_completion_task(task);
1333 static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1336 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1337 case FMODE_READ|FMODE_WRITE:
1338 return state->n_rdwr != 0;
1340 return state->n_wronly != 0;
1342 return state->n_rdonly != 0;
1348 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
1352 if (open_mode & (O_EXCL|O_TRUNC))
1354 switch (mode & (FMODE_READ|FMODE_WRITE)) {
1356 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1357 && state->n_rdonly != 0;
1360 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1361 && state->n_wronly != 0;
1363 case FMODE_READ|FMODE_WRITE:
1364 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1365 && state->n_rdwr != 0;
1371 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1372 enum open_claim_type4 claim)
1374 if (delegation == NULL)
1376 if ((delegation->type & fmode) != fmode)
1378 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1381 case NFS4_OPEN_CLAIM_NULL:
1382 case NFS4_OPEN_CLAIM_FH:
1384 case NFS4_OPEN_CLAIM_PREVIOUS:
1385 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1390 nfs_mark_delegation_referenced(delegation);
1394 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1403 case FMODE_READ|FMODE_WRITE:
1406 nfs4_state_set_mode_locked(state, state->state | fmode);
1409 #ifdef CONFIG_NFS_V4_1
1410 static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1412 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1414 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1416 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1420 #endif /* CONFIG_NFS_V4_1 */
1422 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
1424 struct nfs_client *clp = state->owner->so_server->nfs_client;
1425 bool need_recover = false;
1427 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1428 need_recover = true;
1429 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1430 need_recover = true;
1431 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1432 need_recover = true;
1434 nfs4_state_mark_reclaim_nograce(clp, state);
1437 static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1438 const nfs4_stateid *stateid, nfs4_stateid *freeme)
1440 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1442 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1443 nfs4_stateid_copy(freeme, &state->open_stateid);
1444 nfs_test_and_clear_all_open_stateid(state);
1447 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1452 static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1454 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1456 if (state->n_wronly)
1457 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1458 if (state->n_rdonly)
1459 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1461 set_bit(NFS_O_RDWR_STATE, &state->flags);
1462 set_bit(NFS_OPEN_STATE, &state->flags);
1465 static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1466 nfs4_stateid *stateid, fmode_t fmode)
1468 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1469 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1471 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1474 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1477 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1478 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1479 clear_bit(NFS_OPEN_STATE, &state->flags);
1481 if (stateid == NULL)
1483 /* Handle OPEN+OPEN_DOWNGRADE races */
1484 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1485 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1486 nfs_resync_open_stateid_locked(state);
1489 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1490 nfs4_stateid_copy(&state->stateid, stateid);
1491 nfs4_stateid_copy(&state->open_stateid, stateid);
1494 static void nfs_clear_open_stateid(struct nfs4_state *state,
1495 nfs4_stateid *arg_stateid,
1496 nfs4_stateid *stateid, fmode_t fmode)
1498 write_seqlock(&state->seqlock);
1499 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1500 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1501 nfs_clear_open_stateid_locked(state, stateid, fmode);
1502 write_sequnlock(&state->seqlock);
1503 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1504 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1507 static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1508 const nfs4_stateid *stateid, fmode_t fmode,
1509 nfs4_stateid *freeme)
1513 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1516 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1518 case FMODE_READ|FMODE_WRITE:
1519 set_bit(NFS_O_RDWR_STATE, &state->flags);
1521 if (!nfs_need_update_open_stateid(state, stateid, freeme))
1523 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1524 nfs4_stateid_copy(&state->stateid, stateid);
1525 nfs4_stateid_copy(&state->open_stateid, stateid);
1528 static void __update_open_stateid(struct nfs4_state *state,
1529 const nfs4_stateid *open_stateid,
1530 const nfs4_stateid *deleg_stateid,
1532 nfs4_stateid *freeme)
1535 * Protect the call to nfs4_state_set_mode_locked and
1536 * serialise the stateid update
1538 spin_lock(&state->owner->so_lock);
1539 write_seqlock(&state->seqlock);
1540 if (deleg_stateid != NULL) {
1541 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1542 set_bit(NFS_DELEGATED_STATE, &state->flags);
1544 if (open_stateid != NULL)
1545 nfs_set_open_stateid_locked(state, open_stateid, fmode, freeme);
1546 write_sequnlock(&state->seqlock);
1547 update_open_stateflags(state, fmode);
1548 spin_unlock(&state->owner->so_lock);
1551 static int update_open_stateid(struct nfs4_state *state,
1552 const nfs4_stateid *open_stateid,
1553 const nfs4_stateid *delegation,
1556 struct nfs_server *server = NFS_SERVER(state->inode);
1557 struct nfs_client *clp = server->nfs_client;
1558 struct nfs_inode *nfsi = NFS_I(state->inode);
1559 struct nfs_delegation *deleg_cur;
1560 nfs4_stateid freeme = { };
1563 fmode &= (FMODE_READ|FMODE_WRITE);
1566 deleg_cur = rcu_dereference(nfsi->delegation);
1567 if (deleg_cur == NULL)
1570 spin_lock(&deleg_cur->lock);
1571 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1572 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1573 (deleg_cur->type & fmode) != fmode)
1574 goto no_delegation_unlock;
1576 if (delegation == NULL)
1577 delegation = &deleg_cur->stateid;
1578 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
1579 goto no_delegation_unlock;
1581 nfs_mark_delegation_referenced(deleg_cur);
1582 __update_open_stateid(state, open_stateid, &deleg_cur->stateid,
1585 no_delegation_unlock:
1586 spin_unlock(&deleg_cur->lock);
1590 if (!ret && open_stateid != NULL) {
1591 __update_open_stateid(state, open_stateid, NULL, fmode, &freeme);
1594 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1595 nfs4_schedule_state_manager(clp);
1596 if (freeme.type != 0)
1597 nfs4_test_and_free_stateid(server, &freeme,
1598 state->owner->so_cred);
1603 static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1604 const nfs4_stateid *stateid)
1606 struct nfs4_state *state = lsp->ls_state;
1609 spin_lock(&state->state_lock);
1610 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1612 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1614 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1617 spin_unlock(&state->state_lock);
1621 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1623 struct nfs_delegation *delegation;
1626 delegation = rcu_dereference(NFS_I(inode)->delegation);
1627 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1632 nfs4_inode_return_delegation(inode);
1635 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1637 struct nfs4_state *state = opendata->state;
1638 struct nfs_inode *nfsi = NFS_I(state->inode);
1639 struct nfs_delegation *delegation;
1640 int open_mode = opendata->o_arg.open_flags;
1641 fmode_t fmode = opendata->o_arg.fmode;
1642 enum open_claim_type4 claim = opendata->o_arg.claim;
1643 nfs4_stateid stateid;
1647 spin_lock(&state->owner->so_lock);
1648 if (can_open_cached(state, fmode, open_mode)) {
1649 update_open_stateflags(state, fmode);
1650 spin_unlock(&state->owner->so_lock);
1651 goto out_return_state;
1653 spin_unlock(&state->owner->so_lock);
1655 delegation = rcu_dereference(nfsi->delegation);
1656 if (!can_open_delegated(delegation, fmode, claim)) {
1660 /* Save the delegation */
1661 nfs4_stateid_copy(&stateid, &delegation->stateid);
1663 nfs_release_seqid(opendata->o_arg.seqid);
1664 if (!opendata->is_recover) {
1665 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1671 /* Try to update the stateid using the delegation */
1672 if (update_open_stateid(state, NULL, &stateid, fmode))
1673 goto out_return_state;
1676 return ERR_PTR(ret);
1678 atomic_inc(&state->count);
1683 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1685 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1686 struct nfs_delegation *delegation;
1687 int delegation_flags = 0;
1690 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1692 delegation_flags = delegation->flags;
1694 switch (data->o_arg.claim) {
1697 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1698 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1699 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1700 "returning a delegation for "
1701 "OPEN(CLAIM_DELEGATE_CUR)\n",
1705 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1706 nfs_inode_set_delegation(state->inode,
1707 data->owner->so_cred,
1710 nfs_inode_reclaim_delegation(state->inode,
1711 data->owner->so_cred,
1716 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1717 * and update the nfs4_state.
1719 static struct nfs4_state *
1720 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1722 struct inode *inode = data->state->inode;
1723 struct nfs4_state *state = data->state;
1726 if (!data->rpc_done) {
1727 if (data->rpc_status) {
1728 ret = data->rpc_status;
1731 /* cached opens have already been processed */
1735 ret = nfs_refresh_inode(inode, &data->f_attr);
1739 if (data->o_res.delegation_type != 0)
1740 nfs4_opendata_check_deleg(data, state);
1742 update_open_stateid(state, &data->o_res.stateid, NULL,
1744 atomic_inc(&state->count);
1748 return ERR_PTR(ret);
1752 static struct nfs4_state *
1753 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1755 struct inode *inode;
1756 struct nfs4_state *state = NULL;
1759 if (!data->rpc_done) {
1760 state = nfs4_try_open_cached(data);
1761 trace_nfs4_cached_open(data->state);
1766 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1768 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
1769 ret = PTR_ERR(inode);
1773 state = nfs4_get_open_state(inode, data->owner);
1776 if (data->o_res.delegation_type != 0)
1777 nfs4_opendata_check_deleg(data, state);
1778 update_open_stateid(state, &data->o_res.stateid, NULL,
1782 nfs_release_seqid(data->o_arg.seqid);
1787 return ERR_PTR(ret);
1790 static struct nfs4_state *
1791 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1793 struct nfs4_state *ret;
1795 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1796 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1798 ret = _nfs4_opendata_to_nfs4_state(data);
1799 nfs4_sequence_free_slot(&data->o_res.seq_res);
1803 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1805 struct nfs_inode *nfsi = NFS_I(state->inode);
1806 struct nfs_open_context *ctx;
1808 spin_lock(&state->inode->i_lock);
1809 list_for_each_entry(ctx, &nfsi->open_files, list) {
1810 if (ctx->state != state)
1812 get_nfs_open_context(ctx);
1813 spin_unlock(&state->inode->i_lock);
1816 spin_unlock(&state->inode->i_lock);
1817 return ERR_PTR(-ENOENT);
1820 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1821 struct nfs4_state *state, enum open_claim_type4 claim)
1823 struct nfs4_opendata *opendata;
1825 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
1826 NULL, NULL, claim, GFP_NOFS);
1827 if (opendata == NULL)
1828 return ERR_PTR(-ENOMEM);
1829 opendata->state = state;
1830 atomic_inc(&state->count);
1834 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1837 struct nfs4_state *newstate;
1840 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
1842 opendata->o_arg.open_flags = 0;
1843 opendata->o_arg.fmode = fmode;
1844 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1845 NFS_SB(opendata->dentry->d_sb),
1847 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1848 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1849 nfs4_init_opendata_res(opendata);
1850 ret = _nfs4_recover_proc_open(opendata);
1853 newstate = nfs4_opendata_to_nfs4_state(opendata);
1854 if (IS_ERR(newstate))
1855 return PTR_ERR(newstate);
1856 if (newstate != opendata->state)
1858 nfs4_close_state(newstate, fmode);
1862 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1866 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1867 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1868 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1869 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1870 /* memory barrier prior to reading state->n_* */
1871 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1872 clear_bit(NFS_OPEN_STATE, &state->flags);
1874 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1877 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1880 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1884 * We may have performed cached opens for all three recoveries.
1885 * Check if we need to update the current stateid.
1887 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1888 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
1889 write_seqlock(&state->seqlock);
1890 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1891 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1892 write_sequnlock(&state->seqlock);
1899 * reclaim state on the server after a reboot.
1901 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1903 struct nfs_delegation *delegation;
1904 struct nfs4_opendata *opendata;
1905 fmode_t delegation_type = 0;
1908 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1909 NFS4_OPEN_CLAIM_PREVIOUS);
1910 if (IS_ERR(opendata))
1911 return PTR_ERR(opendata);
1913 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1914 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1915 delegation_type = delegation->type;
1917 opendata->o_arg.u.delegation_type = delegation_type;
1918 status = nfs4_open_recover(opendata, state);
1919 nfs4_opendata_put(opendata);
1923 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1925 struct nfs_server *server = NFS_SERVER(state->inode);
1926 struct nfs4_exception exception = { };
1929 err = _nfs4_do_open_reclaim(ctx, state);
1930 trace_nfs4_open_reclaim(ctx, 0, err);
1931 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1933 if (err != -NFS4ERR_DELAY)
1935 nfs4_handle_exception(server, err, &exception);
1936 } while (exception.retry);
1940 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1942 struct nfs_open_context *ctx;
1945 ctx = nfs4_state_find_open_context(state);
1948 ret = nfs4_do_open_reclaim(ctx, state);
1949 put_nfs_open_context(ctx);
1953 static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, int err)
1957 printk(KERN_ERR "NFS: %s: unhandled error "
1958 "%d.\n", __func__, err);
1964 case -NFS4ERR_BADSESSION:
1965 case -NFS4ERR_BADSLOT:
1966 case -NFS4ERR_BAD_HIGH_SLOT:
1967 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1968 case -NFS4ERR_DEADSESSION:
1969 set_bit(NFS_DELEGATED_STATE, &state->flags);
1970 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1972 case -NFS4ERR_STALE_CLIENTID:
1973 case -NFS4ERR_STALE_STATEID:
1974 set_bit(NFS_DELEGATED_STATE, &state->flags);
1975 /* Don't recall a delegation if it was lost */
1976 nfs4_schedule_lease_recovery(server->nfs_client);
1978 case -NFS4ERR_MOVED:
1979 nfs4_schedule_migration_recovery(server);
1981 case -NFS4ERR_LEASE_MOVED:
1982 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1984 case -NFS4ERR_DELEG_REVOKED:
1985 case -NFS4ERR_ADMIN_REVOKED:
1986 case -NFS4ERR_EXPIRED:
1987 case -NFS4ERR_BAD_STATEID:
1988 case -NFS4ERR_OPENMODE:
1989 nfs_inode_find_state_and_recover(state->inode,
1991 nfs4_schedule_stateid_recovery(server, state);
1993 case -NFS4ERR_DELAY:
1994 case -NFS4ERR_GRACE:
1995 set_bit(NFS_DELEGATED_STATE, &state->flags);
1999 case -NFS4ERR_DENIED:
2000 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
2006 int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2007 struct nfs4_state *state, const nfs4_stateid *stateid,
2010 struct nfs_server *server = NFS_SERVER(state->inode);
2011 struct nfs4_opendata *opendata;
2014 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2015 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2016 if (IS_ERR(opendata))
2017 return PTR_ERR(opendata);
2018 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
2019 write_seqlock(&state->seqlock);
2020 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2021 write_sequnlock(&state->seqlock);
2022 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2023 switch (type & (FMODE_READ|FMODE_WRITE)) {
2024 case FMODE_READ|FMODE_WRITE:
2026 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2029 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2033 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2035 nfs4_opendata_put(opendata);
2036 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
2039 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2041 struct nfs4_opendata *data = calldata;
2043 nfs40_setup_sequence(data->o_arg.server->nfs_client->cl_slot_tbl,
2044 &data->c_arg.seq_args, &data->c_res.seq_res, task);
2047 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2049 struct nfs4_opendata *data = calldata;
2051 nfs40_sequence_done(task, &data->c_res.seq_res);
2053 data->rpc_status = task->tk_status;
2054 if (data->rpc_status == 0) {
2055 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
2056 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2057 renew_lease(data->o_res.server, data->timestamp);
2062 static void nfs4_open_confirm_release(void *calldata)
2064 struct nfs4_opendata *data = calldata;
2065 struct nfs4_state *state = NULL;
2067 /* If this request hasn't been cancelled, do nothing */
2068 if (data->cancelled == 0)
2070 /* In case of error, no cleanup! */
2071 if (!data->rpc_done)
2073 state = nfs4_opendata_to_nfs4_state(data);
2075 nfs4_close_state(state, data->o_arg.fmode);
2077 nfs4_opendata_put(data);
2080 static const struct rpc_call_ops nfs4_open_confirm_ops = {
2081 .rpc_call_prepare = nfs4_open_confirm_prepare,
2082 .rpc_call_done = nfs4_open_confirm_done,
2083 .rpc_release = nfs4_open_confirm_release,
2087 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2089 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2091 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
2092 struct rpc_task *task;
2093 struct rpc_message msg = {
2094 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2095 .rpc_argp = &data->c_arg,
2096 .rpc_resp = &data->c_res,
2097 .rpc_cred = data->owner->so_cred,
2099 struct rpc_task_setup task_setup_data = {
2100 .rpc_client = server->client,
2101 .rpc_message = &msg,
2102 .callback_ops = &nfs4_open_confirm_ops,
2103 .callback_data = data,
2104 .workqueue = nfsiod_workqueue,
2105 .flags = RPC_TASK_ASYNC,
2109 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
2110 kref_get(&data->kref);
2112 data->rpc_status = 0;
2113 data->timestamp = jiffies;
2114 if (data->is_recover)
2115 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
2116 task = rpc_run_task(&task_setup_data);
2118 return PTR_ERR(task);
2119 status = nfs4_wait_for_completion_rpc_task(task);
2121 data->cancelled = 1;
2124 status = data->rpc_status;
2129 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
2131 struct nfs4_opendata *data = calldata;
2132 struct nfs4_state_owner *sp = data->owner;
2133 struct nfs_client *clp = sp->so_server->nfs_client;
2134 enum open_claim_type4 claim = data->o_arg.claim;
2136 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
2139 * Check if we still need to send an OPEN call, or if we can use
2140 * a delegation instead.
2142 if (data->state != NULL) {
2143 struct nfs_delegation *delegation;
2145 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
2148 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
2149 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
2150 goto unlock_no_action;
2153 /* Update client id. */
2154 data->o_arg.clientid = clp->cl_clientid;
2158 case NFS4_OPEN_CLAIM_PREVIOUS:
2159 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2160 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
2161 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
2162 case NFS4_OPEN_CLAIM_FH:
2163 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
2164 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
2166 data->timestamp = jiffies;
2167 if (nfs4_setup_sequence(data->o_arg.server,
2168 &data->o_arg.seq_args,
2169 &data->o_res.seq_res,
2171 nfs_release_seqid(data->o_arg.seqid);
2173 /* Set the create mode (note dependency on the session type) */
2174 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2175 if (data->o_arg.open_flags & O_EXCL) {
2176 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2177 if (nfs4_has_persistent_session(clp))
2178 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2179 else if (clp->cl_mvops->minor_version > 0)
2180 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2184 trace_nfs4_cached_open(data->state);
2187 task->tk_action = NULL;
2189 nfs4_sequence_done(task, &data->o_res.seq_res);
2192 static void nfs4_open_done(struct rpc_task *task, void *calldata)
2194 struct nfs4_opendata *data = calldata;
2196 data->rpc_status = task->tk_status;
2198 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
2201 if (task->tk_status == 0) {
2202 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2203 switch (data->o_res.f_attr->mode & S_IFMT) {
2207 data->rpc_status = -ELOOP;
2210 data->rpc_status = -EISDIR;
2213 data->rpc_status = -ENOTDIR;
2216 renew_lease(data->o_res.server, data->timestamp);
2217 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2218 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2223 static void nfs4_open_release(void *calldata)
2225 struct nfs4_opendata *data = calldata;
2226 struct nfs4_state *state = NULL;
2228 /* If this request hasn't been cancelled, do nothing */
2229 if (data->cancelled == 0)
2231 /* In case of error, no cleanup! */
2232 if (data->rpc_status != 0 || !data->rpc_done)
2234 /* In case we need an open_confirm, no cleanup! */
2235 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2237 state = nfs4_opendata_to_nfs4_state(data);
2239 nfs4_close_state(state, data->o_arg.fmode);
2241 nfs4_opendata_put(data);
2244 static const struct rpc_call_ops nfs4_open_ops = {
2245 .rpc_call_prepare = nfs4_open_prepare,
2246 .rpc_call_done = nfs4_open_done,
2247 .rpc_release = nfs4_open_release,
2250 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
2252 struct inode *dir = d_inode(data->dir);
2253 struct nfs_server *server = NFS_SERVER(dir);
2254 struct nfs_openargs *o_arg = &data->o_arg;
2255 struct nfs_openres *o_res = &data->o_res;
2256 struct rpc_task *task;
2257 struct rpc_message msg = {
2258 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2261 .rpc_cred = data->owner->so_cred,
2263 struct rpc_task_setup task_setup_data = {
2264 .rpc_client = server->client,
2265 .rpc_message = &msg,
2266 .callback_ops = &nfs4_open_ops,
2267 .callback_data = data,
2268 .workqueue = nfsiod_workqueue,
2269 .flags = RPC_TASK_ASYNC,
2273 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
2274 kref_get(&data->kref);
2276 data->rpc_status = 0;
2277 data->cancelled = 0;
2278 data->is_recover = 0;
2280 nfs4_set_sequence_privileged(&o_arg->seq_args);
2281 data->is_recover = 1;
2283 task = rpc_run_task(&task_setup_data);
2285 return PTR_ERR(task);
2286 status = nfs4_wait_for_completion_rpc_task(task);
2288 data->cancelled = 1;
2291 status = data->rpc_status;
2297 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2299 struct inode *dir = d_inode(data->dir);
2300 struct nfs_openres *o_res = &data->o_res;
2303 status = nfs4_run_open_task(data, 1);
2304 if (status != 0 || !data->rpc_done)
2307 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2309 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2310 status = _nfs4_proc_open_confirm(data);
2319 * Additional permission checks in order to distinguish between an
2320 * open for read, and an open for execute. This works around the
2321 * fact that NFSv4 OPEN treats read and execute permissions as being
2323 * Note that in the non-execute case, we want to turn off permission
2324 * checking if we just created a new file (POSIX open() semantics).
2326 static int nfs4_opendata_access(struct rpc_cred *cred,
2327 struct nfs4_opendata *opendata,
2328 struct nfs4_state *state, fmode_t fmode,
2331 struct nfs_access_entry cache;
2334 /* access call failed or for some reason the server doesn't
2335 * support any access modes -- defer access call until later */
2336 if (opendata->o_res.access_supported == 0)
2341 * Use openflags to check for exec, because fmode won't
2342 * always have FMODE_EXEC set when file open for exec.
2344 if (openflags & __FMODE_EXEC) {
2345 /* ONLY check for exec rights */
2347 } else if ((fmode & FMODE_READ) && !opendata->file_created)
2351 cache.jiffies = jiffies;
2352 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2353 nfs_access_add_cache(state->inode, &cache);
2355 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
2358 /* even though OPEN succeeded, access is denied. Close the file */
2359 nfs4_close_state(state, fmode);
2364 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2366 static int _nfs4_proc_open(struct nfs4_opendata *data)
2368 struct inode *dir = d_inode(data->dir);
2369 struct nfs_server *server = NFS_SERVER(dir);
2370 struct nfs_openargs *o_arg = &data->o_arg;
2371 struct nfs_openres *o_res = &data->o_res;
2374 status = nfs4_run_open_task(data, 0);
2375 if (!data->rpc_done)
2378 if (status == -NFS4ERR_BADNAME &&
2379 !(o_arg->open_flags & O_CREAT))
2384 nfs_fattr_map_and_free_names(server, &data->f_attr);
2386 if (o_arg->open_flags & O_CREAT) {
2387 update_changeattr(dir, &o_res->cinfo);
2388 if (o_arg->open_flags & O_EXCL)
2389 data->file_created = 1;
2390 else if (o_res->cinfo.before != o_res->cinfo.after)
2391 data->file_created = 1;
2393 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2394 server->caps &= ~NFS_CAP_POSIX_LOCK;
2395 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2396 status = _nfs4_proc_open_confirm(data);
2400 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
2401 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
2405 static int nfs4_recover_expired_lease(struct nfs_server *server)
2407 return nfs4_client_recover_expired_lease(server->nfs_client);
2412 * reclaim state on the server after a network partition.
2413 * Assumes caller holds the appropriate lock
2415 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2417 struct nfs4_opendata *opendata;
2420 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2421 NFS4_OPEN_CLAIM_FH);
2422 if (IS_ERR(opendata))
2423 return PTR_ERR(opendata);
2424 ret = nfs4_open_recover(opendata, state);
2426 d_drop(ctx->dentry);
2427 nfs4_opendata_put(opendata);
2431 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2433 struct nfs_server *server = NFS_SERVER(state->inode);
2434 struct nfs4_exception exception = { };
2438 err = _nfs4_open_expired(ctx, state);
2439 trace_nfs4_open_expired(ctx, 0, err);
2440 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2445 case -NFS4ERR_GRACE:
2446 case -NFS4ERR_DELAY:
2447 nfs4_handle_exception(server, err, &exception);
2450 } while (exception.retry);
2455 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2457 struct nfs_open_context *ctx;
2460 ctx = nfs4_state_find_open_context(state);
2463 ret = nfs4_do_open_expired(ctx, state);
2464 put_nfs_open_context(ctx);
2468 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2469 const nfs4_stateid *stateid)
2471 nfs_remove_bad_delegation(state->inode, stateid);
2472 write_seqlock(&state->seqlock);
2473 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2474 write_sequnlock(&state->seqlock);
2475 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2478 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2480 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2481 nfs_finish_clear_delegation_stateid(state, NULL);
2484 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2486 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2487 nfs40_clear_delegation_stateid(state);
2488 return nfs4_open_expired(sp, state);
2491 static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2492 nfs4_stateid *stateid,
2493 struct rpc_cred *cred)
2495 return -NFS4ERR_BAD_STATEID;
2498 #if defined(CONFIG_NFS_V4_1)
2499 static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2500 nfs4_stateid *stateid,
2501 struct rpc_cred *cred)
2505 switch (stateid->type) {
2508 case NFS4_INVALID_STATEID_TYPE:
2509 case NFS4_SPECIAL_STATEID_TYPE:
2510 return -NFS4ERR_BAD_STATEID;
2511 case NFS4_REVOKED_STATEID_TYPE:
2515 status = nfs41_test_stateid(server, stateid, cred);
2517 case -NFS4ERR_EXPIRED:
2518 case -NFS4ERR_ADMIN_REVOKED:
2519 case -NFS4ERR_DELEG_REVOKED:
2525 /* Ack the revoked state to the server */
2526 nfs41_free_stateid(server, stateid, cred, true);
2527 return -NFS4ERR_EXPIRED;
2530 static void nfs41_check_delegation_stateid(struct nfs4_state *state)
2532 struct nfs_server *server = NFS_SERVER(state->inode);
2533 nfs4_stateid stateid;
2534 struct nfs_delegation *delegation;
2535 struct rpc_cred *cred;
2538 /* Get the delegation credential for use by test/free_stateid */
2540 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2541 if (delegation == NULL) {
2546 nfs4_stateid_copy(&stateid, &delegation->stateid);
2547 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
2549 nfs_finish_clear_delegation_stateid(state, &stateid);
2553 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags)) {
2558 cred = get_rpccred(delegation->cred);
2560 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
2561 trace_nfs4_test_delegation_stateid(state, NULL, status);
2562 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
2563 nfs_finish_clear_delegation_stateid(state, &stateid);
2569 * nfs41_check_expired_locks - possibly free a lock stateid
2571 * @state: NFSv4 state for an inode
2573 * Returns NFS_OK if recovery for this stateid is now finished.
2574 * Otherwise a negative NFS4ERR value is returned.
2576 static int nfs41_check_expired_locks(struct nfs4_state *state)
2578 int status, ret = NFS_OK;
2579 struct nfs4_lock_state *lsp, *prev = NULL;
2580 struct nfs_server *server = NFS_SERVER(state->inode);
2582 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2585 spin_lock(&state->state_lock);
2586 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2587 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2588 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2590 atomic_inc(&lsp->ls_count);
2591 spin_unlock(&state->state_lock);
2593 nfs4_put_lock_state(prev);
2596 status = nfs41_test_and_free_expired_stateid(server,
2599 trace_nfs4_test_lock_stateid(state, lsp, status);
2600 if (status == -NFS4ERR_EXPIRED ||
2601 status == -NFS4ERR_BAD_STATEID) {
2602 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
2603 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
2604 if (!recover_lost_locks)
2605 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2606 } else if (status != NFS_OK) {
2608 nfs4_put_lock_state(prev);
2611 spin_lock(&state->state_lock);
2614 spin_unlock(&state->state_lock);
2615 nfs4_put_lock_state(prev);
2621 * nfs41_check_open_stateid - possibly free an open stateid
2623 * @state: NFSv4 state for an inode
2625 * Returns NFS_OK if recovery for this stateid is now finished.
2626 * Otherwise a negative NFS4ERR value is returned.
2628 static int nfs41_check_open_stateid(struct nfs4_state *state)
2630 struct nfs_server *server = NFS_SERVER(state->inode);
2631 nfs4_stateid *stateid = &state->open_stateid;
2632 struct rpc_cred *cred = state->owner->so_cred;
2635 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
2636 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2637 if (nfs4_have_delegation(state->inode, state->state))
2639 return -NFS4ERR_OPENMODE;
2641 return -NFS4ERR_BAD_STATEID;
2643 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
2644 trace_nfs4_test_open_stateid(state, NULL, status);
2645 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
2646 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2647 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2648 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2649 clear_bit(NFS_OPEN_STATE, &state->flags);
2650 stateid->type = NFS4_INVALID_STATEID_TYPE;
2652 if (status != NFS_OK)
2654 if (nfs_open_stateid_recover_openmode(state))
2655 return -NFS4ERR_OPENMODE;
2659 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2663 nfs41_check_delegation_stateid(state);
2664 status = nfs41_check_expired_locks(state);
2665 if (status != NFS_OK)
2667 status = nfs41_check_open_stateid(state);
2668 if (status != NFS_OK)
2669 status = nfs4_open_expired(sp, state);
2675 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2676 * fields corresponding to attributes that were used to store the verifier.
2677 * Make sure we clobber those fields in the later setattr call
2679 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2680 struct iattr *sattr, struct nfs4_label **label)
2682 const u32 *attrset = opendata->o_res.attrset;
2684 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2685 !(sattr->ia_valid & ATTR_ATIME_SET))
2686 sattr->ia_valid |= ATTR_ATIME;
2688 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2689 !(sattr->ia_valid & ATTR_MTIME_SET))
2690 sattr->ia_valid |= ATTR_MTIME;
2692 /* Except MODE, it seems harmless of setting twice. */
2693 if ((attrset[1] & FATTR4_WORD1_MODE))
2694 sattr->ia_valid &= ~ATTR_MODE;
2696 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2700 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2703 struct nfs_open_context *ctx)
2705 struct nfs4_state_owner *sp = opendata->owner;
2706 struct nfs_server *server = sp->so_server;
2707 struct dentry *dentry;
2708 struct nfs4_state *state;
2712 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2714 ret = _nfs4_proc_open(opendata);
2718 state = nfs4_opendata_to_nfs4_state(opendata);
2719 ret = PTR_ERR(state);
2722 if (server->caps & NFS_CAP_POSIX_LOCK)
2723 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2724 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2725 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
2727 dentry = opendata->dentry;
2728 if (d_really_is_negative(dentry)) {
2729 struct dentry *alias;
2731 alias = d_exact_alias(dentry, state->inode);
2733 alias = d_splice_alias(igrab(state->inode), dentry);
2734 /* d_splice_alias() can't fail here - it's a non-directory */
2737 ctx->dentry = dentry = alias;
2739 nfs_set_verifier(dentry,
2740 nfs_save_change_attribute(d_inode(opendata->dir)));
2743 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2748 if (d_inode(dentry) == state->inode) {
2749 nfs_inode_attach_open_context(ctx);
2750 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2751 nfs4_schedule_stateid_recovery(server, state);
2758 * Returns a referenced nfs4_state
2760 static int _nfs4_do_open(struct inode *dir,
2761 struct nfs_open_context *ctx,
2763 struct iattr *sattr,
2764 struct nfs4_label *label,
2767 struct nfs4_state_owner *sp;
2768 struct nfs4_state *state = NULL;
2769 struct nfs_server *server = NFS_SERVER(dir);
2770 struct nfs4_opendata *opendata;
2771 struct dentry *dentry = ctx->dentry;
2772 struct rpc_cred *cred = ctx->cred;
2773 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2774 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
2775 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
2776 struct nfs4_label *olabel = NULL;
2779 /* Protect against reboot recovery conflicts */
2781 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2783 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2786 status = nfs4_recover_expired_lease(server);
2788 goto err_put_state_owner;
2789 if (d_really_is_positive(dentry))
2790 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
2792 if (d_really_is_positive(dentry))
2793 claim = NFS4_OPEN_CLAIM_FH;
2794 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
2795 label, claim, GFP_KERNEL);
2796 if (opendata == NULL)
2797 goto err_put_state_owner;
2800 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2801 if (IS_ERR(olabel)) {
2802 status = PTR_ERR(olabel);
2803 goto err_opendata_put;
2807 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2808 if (!opendata->f_attr.mdsthreshold) {
2809 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2810 if (!opendata->f_attr.mdsthreshold)
2811 goto err_free_label;
2813 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
2815 if (d_really_is_positive(dentry))
2816 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
2818 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
2820 goto err_free_label;
2823 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
2824 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
2825 nfs4_exclusive_attrset(opendata, sattr, &label);
2827 * send create attributes which was not set by open
2828 * with an extra setattr.
2830 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2831 nfs_fattr_init(opendata->o_res.f_attr);
2832 status = nfs4_do_setattr(state->inode, cred,
2833 opendata->o_res.f_attr, sattr,
2834 ctx, label, olabel);
2836 nfs_setattr_update_inode(state->inode, sattr,
2837 opendata->o_res.f_attr);
2838 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2842 if (opened && opendata->file_created)
2843 *opened |= FILE_CREATED;
2845 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
2846 *ctx_th = opendata->f_attr.mdsthreshold;
2847 opendata->f_attr.mdsthreshold = NULL;
2850 nfs4_label_free(olabel);
2852 nfs4_opendata_put(opendata);
2853 nfs4_put_state_owner(sp);
2856 nfs4_label_free(olabel);
2858 nfs4_opendata_put(opendata);
2859 err_put_state_owner:
2860 nfs4_put_state_owner(sp);
2866 static struct nfs4_state *nfs4_do_open(struct inode *dir,
2867 struct nfs_open_context *ctx,
2869 struct iattr *sattr,
2870 struct nfs4_label *label,
2873 struct nfs_server *server = NFS_SERVER(dir);
2874 struct nfs4_exception exception = { };
2875 struct nfs4_state *res;
2879 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
2881 trace_nfs4_open_file(ctx, flags, status);
2884 /* NOTE: BAD_SEQID means the server and client disagree about the
2885 * book-keeping w.r.t. state-changing operations
2886 * (OPEN/CLOSE/LOCK/LOCKU...)
2887 * It is actually a sign of a bug on the client or on the server.
2889 * If we receive a BAD_SEQID error in the particular case of
2890 * doing an OPEN, we assume that nfs_increment_open_seqid() will
2891 * have unhashed the old state_owner for us, and that we can
2892 * therefore safely retry using a new one. We should still warn
2893 * the user though...
2895 if (status == -NFS4ERR_BAD_SEQID) {
2896 pr_warn_ratelimited("NFS: v4 server %s "
2897 " returned a bad sequence-id error!\n",
2898 NFS_SERVER(dir)->nfs_client->cl_hostname);
2899 exception.retry = 1;
2903 * BAD_STATEID on OPEN means that the server cancelled our
2904 * state before it received the OPEN_CONFIRM.
2905 * Recover by retrying the request as per the discussion
2906 * on Page 181 of RFC3530.
2908 if (status == -NFS4ERR_BAD_STATEID) {
2909 exception.retry = 1;
2912 if (status == -EAGAIN) {
2913 /* We must have found a delegation */
2914 exception.retry = 1;
2917 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2919 res = ERR_PTR(nfs4_handle_exception(server,
2920 status, &exception));
2921 } while (exception.retry);
2925 static int _nfs4_do_setattr(struct inode *inode,
2926 struct nfs_setattrargs *arg,
2927 struct nfs_setattrres *res,
2928 struct rpc_cred *cred,
2929 struct nfs_open_context *ctx)
2931 struct nfs_server *server = NFS_SERVER(inode);
2932 struct rpc_message msg = {
2933 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2938 struct rpc_cred *delegation_cred = NULL;
2939 unsigned long timestamp = jiffies;
2944 nfs_fattr_init(res->fattr);
2946 /* Servers should only apply open mode checks for file size changes */
2947 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
2948 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2950 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
2951 /* Use that stateid */
2952 } else if (truncate && ctx != NULL) {
2953 struct nfs_lock_context *l_ctx;
2954 if (!nfs4_valid_open_stateid(ctx->state))
2956 l_ctx = nfs_get_lock_context(ctx);
2958 return PTR_ERR(l_ctx);
2959 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
2960 &arg->stateid, &delegation_cred);
2961 nfs_put_lock_context(l_ctx);
2965 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
2966 if (delegation_cred)
2967 msg.rpc_cred = delegation_cred;
2969 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
2971 put_rpccred(delegation_cred);
2972 if (status == 0 && ctx != NULL)
2973 renew_lease(server, timestamp);
2974 trace_nfs4_setattr(inode, &arg->stateid, status);
2978 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2979 struct nfs_fattr *fattr, struct iattr *sattr,
2980 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
2981 struct nfs4_label *olabel)
2983 struct nfs_server *server = NFS_SERVER(inode);
2984 struct nfs4_state *state = ctx ? ctx->state : NULL;
2985 struct nfs_setattrargs arg = {
2986 .fh = NFS_FH(inode),
2989 .bitmask = server->attr_bitmask,
2992 struct nfs_setattrres res = {
2997 struct nfs4_exception exception = {
3000 .stateid = &arg.stateid,
3004 arg.bitmask = nfs4_bitmask(server, ilabel);
3006 arg.bitmask = nfs4_bitmask(server, olabel);
3009 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
3011 case -NFS4ERR_OPENMODE:
3012 if (!(sattr->ia_valid & ATTR_SIZE)) {
3013 pr_warn_once("NFSv4: server %s is incorrectly "
3014 "applying open mode checks to "
3015 "a SETATTR that is not "
3016 "changing file size.\n",
3017 server->nfs_client->cl_hostname);
3019 if (state && !(state->state & FMODE_WRITE)) {
3021 if (sattr->ia_valid & ATTR_OPEN)
3026 err = nfs4_handle_exception(server, err, &exception);
3027 } while (exception.retry);
3033 nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3035 if (inode == NULL || !nfs_have_layout(inode))
3038 return pnfs_wait_on_layoutreturn(inode, task);
3041 struct nfs4_closedata {
3042 struct inode *inode;
3043 struct nfs4_state *state;
3044 struct nfs_closeargs arg;
3045 struct nfs_closeres res;
3047 struct nfs4_layoutreturn_args arg;
3048 struct nfs4_layoutreturn_res res;
3049 struct nfs4_xdr_opaque_data ld_private;
3053 struct nfs_fattr fattr;
3054 unsigned long timestamp;
3057 static void nfs4_free_closedata(void *data)
3059 struct nfs4_closedata *calldata = data;
3060 struct nfs4_state_owner *sp = calldata->state->owner;
3061 struct super_block *sb = calldata->state->inode->i_sb;
3063 if (calldata->lr.roc)
3064 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3065 calldata->res.lr_ret);
3066 nfs4_put_open_state(calldata->state);
3067 nfs_free_seqid(calldata->arg.seqid);
3068 nfs4_put_state_owner(sp);
3069 nfs_sb_deactive(sb);
3073 static void nfs4_close_done(struct rpc_task *task, void *data)
3075 struct nfs4_closedata *calldata = data;
3076 struct nfs4_state *state = calldata->state;
3077 struct nfs_server *server = NFS_SERVER(calldata->inode);
3078 nfs4_stateid *res_stateid = NULL;
3080 dprintk("%s: begin!\n", __func__);
3081 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3083 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
3085 /* Handle Layoutreturn errors */
3086 if (calldata->arg.lr_args && task->tk_status != 0) {
3087 switch (calldata->res.lr_ret) {
3089 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3092 calldata->arg.lr_args = NULL;
3093 calldata->res.lr_res = NULL;
3095 case -NFS4ERR_ADMIN_REVOKED:
3096 case -NFS4ERR_DELEG_REVOKED:
3097 case -NFS4ERR_EXPIRED:
3098 case -NFS4ERR_BAD_STATEID:
3099 case -NFS4ERR_OLD_STATEID:
3100 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3101 case -NFS4ERR_WRONG_CRED:
3102 calldata->arg.lr_args = NULL;
3103 calldata->res.lr_res = NULL;
3104 calldata->res.lr_ret = 0;
3105 rpc_restart_call_prepare(task);
3110 /* hmm. we are done with the inode, and in the process of freeing
3111 * the state_owner. we keep this around to process errors
3113 switch (task->tk_status) {
3115 res_stateid = &calldata->res.stateid;
3116 renew_lease(server, calldata->timestamp);
3118 case -NFS4ERR_ADMIN_REVOKED:
3119 case -NFS4ERR_STALE_STATEID:
3120 case -NFS4ERR_EXPIRED:
3121 nfs4_free_revoked_stateid(server,
3122 &calldata->arg.stateid,
3123 task->tk_msg.rpc_cred);
3124 case -NFS4ERR_OLD_STATEID:
3125 case -NFS4ERR_BAD_STATEID:
3126 if (!nfs4_stateid_match(&calldata->arg.stateid,
3127 &state->open_stateid)) {
3128 rpc_restart_call_prepare(task);
3131 if (calldata->arg.fmode == 0)
3134 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
3135 rpc_restart_call_prepare(task);
3139 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3140 res_stateid, calldata->arg.fmode);
3142 nfs_release_seqid(calldata->arg.seqid);
3143 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
3144 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
3147 static void nfs4_close_prepare(struct rpc_task *task, void *data)
3149 struct nfs4_closedata *calldata = data;
3150 struct nfs4_state *state = calldata->state;
3151 struct inode *inode = calldata->inode;
3152 bool is_rdonly, is_wronly, is_rdwr;
3155 dprintk("%s: begin!\n", __func__);
3156 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3159 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
3160 spin_lock(&state->owner->so_lock);
3161 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3162 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3163 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
3164 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
3165 /* Calculate the change in open mode */
3166 calldata->arg.fmode = 0;
3167 if (state->n_rdwr == 0) {
3168 if (state->n_rdonly == 0)
3169 call_close |= is_rdonly;
3171 calldata->arg.fmode |= FMODE_READ;
3172 if (state->n_wronly == 0)
3173 call_close |= is_wronly;
3175 calldata->arg.fmode |= FMODE_WRITE;
3176 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3177 call_close |= is_rdwr;
3179 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3181 if (!nfs4_valid_open_stateid(state) ||
3182 test_bit(NFS_OPEN_STATE, &state->flags) == 0)
3184 spin_unlock(&state->owner->so_lock);
3187 /* Note: exit _without_ calling nfs4_close_done */
3191 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
3192 nfs_release_seqid(calldata->arg.seqid);
3196 if (calldata->arg.fmode == 0) {
3197 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
3199 /* Close-to-open cache consistency revalidation */
3200 if (!nfs4_have_delegation(inode, FMODE_READ))
3201 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3203 calldata->arg.bitmask = NULL;
3206 calldata->arg.share_access =
3207 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3208 calldata->arg.fmode, 0);
3210 nfs_fattr_init(calldata->res.fattr);
3211 calldata->timestamp = jiffies;
3212 if (nfs4_setup_sequence(NFS_SERVER(inode),
3213 &calldata->arg.seq_args,
3214 &calldata->res.seq_res,
3216 nfs_release_seqid(calldata->arg.seqid);
3217 dprintk("%s: done!\n", __func__);
3220 task->tk_action = NULL;
3222 nfs4_sequence_done(task, &calldata->res.seq_res);
3225 static const struct rpc_call_ops nfs4_close_ops = {
3226 .rpc_call_prepare = nfs4_close_prepare,
3227 .rpc_call_done = nfs4_close_done,
3228 .rpc_release = nfs4_free_closedata,
3232 * It is possible for data to be read/written from a mem-mapped file
3233 * after the sys_close call (which hits the vfs layer as a flush).
3234 * This means that we can't safely call nfsv4 close on a file until
3235 * the inode is cleared. This in turn means that we are not good
3236 * NFSv4 citizens - we do not indicate to the server to update the file's
3237 * share state even when we are done with one of the three share
3238 * stateid's in the inode.
3240 * NOTE: Caller must be holding the sp->so_owner semaphore!
3242 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
3244 struct nfs_server *server = NFS_SERVER(state->inode);
3245 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
3246 struct nfs4_closedata *calldata;
3247 struct nfs4_state_owner *sp = state->owner;
3248 struct rpc_task *task;
3249 struct rpc_message msg = {
3250 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3251 .rpc_cred = state->owner->so_cred,
3253 struct rpc_task_setup task_setup_data = {
3254 .rpc_client = server->client,
3255 .rpc_message = &msg,
3256 .callback_ops = &nfs4_close_ops,
3257 .workqueue = nfsiod_workqueue,
3258 .flags = RPC_TASK_ASYNC,
3260 int status = -ENOMEM;
3262 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3263 &task_setup_data.rpc_client, &msg);
3265 calldata = kzalloc(sizeof(*calldata), gfp_mask);
3266 if (calldata == NULL)
3268 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
3269 calldata->inode = state->inode;
3270 calldata->state = state;
3271 calldata->arg.fh = NFS_FH(state->inode);
3272 /* Serialization for the sequence id */
3273 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3274 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
3275 if (IS_ERR(calldata->arg.seqid))
3276 goto out_free_calldata;
3277 calldata->arg.fmode = 0;
3278 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
3279 calldata->res.fattr = &calldata->fattr;
3280 calldata->res.seqid = calldata->arg.seqid;
3281 calldata->res.server = server;
3282 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3283 calldata->lr.roc = pnfs_roc(state->inode,
3284 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3285 if (calldata->lr.roc) {
3286 calldata->arg.lr_args = &calldata->lr.arg;
3287 calldata->res.lr_res = &calldata->lr.res;
3289 nfs_sb_active(calldata->inode->i_sb);
3291 msg.rpc_argp = &calldata->arg;
3292 msg.rpc_resp = &calldata->res;
3293 task_setup_data.callback_data = calldata;
3294 task = rpc_run_task(&task_setup_data);
3296 return PTR_ERR(task);
3299 status = rpc_wait_for_completion_task(task);
3305 nfs4_put_open_state(state);
3306 nfs4_put_state_owner(sp);
3310 static struct inode *
3311 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3312 int open_flags, struct iattr *attr, int *opened)
3314 struct nfs4_state *state;
3315 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3317 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
3319 /* Protect against concurrent sillydeletes */
3320 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
3322 nfs4_label_release_security(label);
3325 return ERR_CAST(state);
3326 return state->inode;
3329 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
3331 if (ctx->state == NULL)
3334 nfs4_close_sync(ctx->state, ctx->mode);
3336 nfs4_close_state(ctx->state, ctx->mode);
3339 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3340 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3341 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
3343 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3345 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
3346 struct nfs4_server_caps_arg args = {
3350 struct nfs4_server_caps_res res = {};
3351 struct rpc_message msg = {
3352 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
3358 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3359 FATTR4_WORD0_FH_EXPIRE_TYPE |
3360 FATTR4_WORD0_LINK_SUPPORT |
3361 FATTR4_WORD0_SYMLINK_SUPPORT |
3362 FATTR4_WORD0_ACLSUPPORT;
3364 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3366 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3368 /* Sanity check the server answers */
3369 switch (minorversion) {
3371 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3372 res.attr_bitmask[2] = 0;
3375 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3378 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3380 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
3381 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3382 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3383 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3384 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
3385 NFS_CAP_CTIME|NFS_CAP_MTIME|
3386 NFS_CAP_SECURITY_LABEL);
3387 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3388 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3389 server->caps |= NFS_CAP_ACLS;
3390 if (res.has_links != 0)
3391 server->caps |= NFS_CAP_HARDLINKS;
3392 if (res.has_symlinks != 0)
3393 server->caps |= NFS_CAP_SYMLINKS;
3394 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3395 server->caps |= NFS_CAP_FILEID;
3396 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3397 server->caps |= NFS_CAP_MODE;
3398 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3399 server->caps |= NFS_CAP_NLINK;
3400 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3401 server->caps |= NFS_CAP_OWNER;
3402 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3403 server->caps |= NFS_CAP_OWNER_GROUP;
3404 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3405 server->caps |= NFS_CAP_ATIME;
3406 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3407 server->caps |= NFS_CAP_CTIME;
3408 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3409 server->caps |= NFS_CAP_MTIME;
3410 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3411 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3412 server->caps |= NFS_CAP_SECURITY_LABEL;
3414 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3415 sizeof(server->attr_bitmask));
3416 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3418 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3419 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3420 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
3421 server->cache_consistency_bitmask[2] = 0;
3422 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3423 sizeof(server->exclcreat_bitmask));
3424 server->acl_bitmask = res.acl_bitmask;
3425 server->fh_expire_type = res.fh_expire_type;
3431 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3433 struct nfs4_exception exception = { };
3436 err = nfs4_handle_exception(server,
3437 _nfs4_server_capabilities(server, fhandle),
3439 } while (exception.retry);
3443 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3444 struct nfs_fsinfo *info)
3447 struct nfs4_lookup_root_arg args = {
3450 struct nfs4_lookup_res res = {
3452 .fattr = info->fattr,
3455 struct rpc_message msg = {
3456 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3461 bitmask[0] = nfs4_fattr_bitmap[0];
3462 bitmask[1] = nfs4_fattr_bitmap[1];
3464 * Process the label in the upcoming getfattr
3466 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3468 nfs_fattr_init(info->fattr);
3469 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3472 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3473 struct nfs_fsinfo *info)
3475 struct nfs4_exception exception = { };
3478 err = _nfs4_lookup_root(server, fhandle, info);
3479 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
3482 case -NFS4ERR_WRONGSEC:
3485 err = nfs4_handle_exception(server, err, &exception);
3487 } while (exception.retry);
3492 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3493 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3495 struct rpc_auth_create_args auth_args = {
3496 .pseudoflavor = flavor,
3498 struct rpc_auth *auth;
3501 auth = rpcauth_create(&auth_args, server->client);
3506 ret = nfs4_lookup_root(server, fhandle, info);
3512 * Retry pseudoroot lookup with various security flavors. We do this when:
3514 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3515 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3517 * Returns zero on success, or a negative NFS4ERR value, or a
3518 * negative errno value.
3520 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3521 struct nfs_fsinfo *info)
3523 /* Per 3530bis 15.33.5 */
3524 static const rpc_authflavor_t flav_array[] = {
3528 RPC_AUTH_UNIX, /* courtesy */
3531 int status = -EPERM;
3534 if (server->auth_info.flavor_len > 0) {
3535 /* try each flavor specified by user */
3536 for (i = 0; i < server->auth_info.flavor_len; i++) {
3537 status = nfs4_lookup_root_sec(server, fhandle, info,
3538 server->auth_info.flavors[i]);
3539 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3544 /* no flavors specified by user, try default list */
3545 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3546 status = nfs4_lookup_root_sec(server, fhandle, info,
3548 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3555 * -EACCESS could mean that the user doesn't have correct permissions
3556 * to access the mount. It could also mean that we tried to mount
3557 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3558 * existing mount programs don't handle -EACCES very well so it should
3559 * be mapped to -EPERM instead.
3561 if (status == -EACCES)
3567 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3568 * @server: initialized nfs_server handle
3569 * @fhandle: we fill in the pseudo-fs root file handle
3570 * @info: we fill in an FSINFO struct
3571 * @auth_probe: probe the auth flavours
3573 * Returns zero on success, or a negative errno.
3575 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
3576 struct nfs_fsinfo *info,
3582 status = nfs4_lookup_root(server, fhandle, info);
3584 if (auth_probe || status == NFS4ERR_WRONGSEC)
3585 status = server->nfs_client->cl_mvops->find_root_sec(server,
3589 status = nfs4_server_capabilities(server, fhandle);
3591 status = nfs4_do_fsinfo(server, fhandle, info);
3593 return nfs4_map_errors(status);
3596 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3597 struct nfs_fsinfo *info)
3600 struct nfs_fattr *fattr = info->fattr;
3601 struct nfs4_label *label = NULL;
3603 error = nfs4_server_capabilities(server, mntfh);
3605 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3609 label = nfs4_label_alloc(server, GFP_KERNEL);
3611 return PTR_ERR(label);
3613 error = nfs4_proc_getattr(server, mntfh, fattr, label);
3615 dprintk("nfs4_get_root: getattr error = %d\n", -error);
3616 goto err_free_label;
3619 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3620 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3621 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3624 nfs4_label_free(label);
3630 * Get locations and (maybe) other attributes of a referral.
3631 * Note that we'll actually follow the referral later when
3632 * we detect fsid mismatch in inode revalidation
3634 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3635 const struct qstr *name, struct nfs_fattr *fattr,
3636 struct nfs_fh *fhandle)
3638 int status = -ENOMEM;
3639 struct page *page = NULL;
3640 struct nfs4_fs_locations *locations = NULL;
3642 page = alloc_page(GFP_KERNEL);
3645 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3646 if (locations == NULL)
3649 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
3654 * If the fsid didn't change, this is a migration event, not a
3655 * referral. Cause us to drop into the exception handler, which
3656 * will kick off migration recovery.
3658 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
3659 dprintk("%s: server did not return a different fsid for"
3660 " a referral at %s\n", __func__, name->name);
3661 status = -NFS4ERR_MOVED;
3664 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3665 nfs_fixup_referral_attributes(&locations->fattr);
3667 /* replace the lookup nfs_fattr with the locations nfs_fattr */
3668 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
3669 memset(fhandle, 0, sizeof(struct nfs_fh));
3677 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3678 struct nfs_fattr *fattr, struct nfs4_label *label)
3680 struct nfs4_getattr_arg args = {
3682 .bitmask = server->attr_bitmask,
3684 struct nfs4_getattr_res res = {
3689 struct rpc_message msg = {
3690 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3695 args.bitmask = nfs4_bitmask(server, label);
3697 nfs_fattr_init(fattr);
3698 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3701 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3702 struct nfs_fattr *fattr, struct nfs4_label *label)
3704 struct nfs4_exception exception = { };
3707 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3708 trace_nfs4_getattr(server, fhandle, fattr, err);
3709 err = nfs4_handle_exception(server, err,
3711 } while (exception.retry);
3716 * The file is not closed if it is opened due to the a request to change
3717 * the size of the file. The open call will not be needed once the
3718 * VFS layer lookup-intents are implemented.
3720 * Close is called when the inode is destroyed.
3721 * If we haven't opened the file for O_WRONLY, we
3722 * need to in the size_change case to obtain a stateid.
3725 * Because OPEN is always done by name in nfsv4, it is
3726 * possible that we opened a different file by the same
3727 * name. We can recognize this race condition, but we
3728 * can't do anything about it besides returning an error.
3730 * This will be fixed with VFS changes (lookup-intent).
3733 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3734 struct iattr *sattr)
3736 struct inode *inode = d_inode(dentry);
3737 struct rpc_cred *cred = NULL;
3738 struct nfs_open_context *ctx = NULL;
3739 struct nfs4_label *label = NULL;
3742 if (pnfs_ld_layoutret_on_setattr(inode) &&
3743 sattr->ia_valid & ATTR_SIZE &&
3744 sattr->ia_size < i_size_read(inode))
3745 pnfs_commit_and_return_layout(inode);
3747 nfs_fattr_init(fattr);
3749 /* Deal with open(O_TRUNC) */
3750 if (sattr->ia_valid & ATTR_OPEN)
3751 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
3753 /* Optimization: if the end result is no change, don't RPC */
3754 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
3757 /* Search for an existing open(O_WRITE) file */
3758 if (sattr->ia_valid & ATTR_FILE) {
3760 ctx = nfs_file_open_context(sattr->ia_file);
3765 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3767 return PTR_ERR(label);
3769 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
3771 nfs_setattr_update_inode(inode, sattr, fattr);
3772 nfs_setsecurity(inode, fattr, label);
3774 nfs4_label_free(label);
3778 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3779 const struct qstr *name, struct nfs_fh *fhandle,
3780 struct nfs_fattr *fattr, struct nfs4_label *label)
3782 struct nfs_server *server = NFS_SERVER(dir);
3784 struct nfs4_lookup_arg args = {
3785 .bitmask = server->attr_bitmask,
3786 .dir_fh = NFS_FH(dir),
3789 struct nfs4_lookup_res res = {
3795 struct rpc_message msg = {
3796 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3801 args.bitmask = nfs4_bitmask(server, label);
3803 nfs_fattr_init(fattr);
3805 dprintk("NFS call lookup %s\n", name->name);
3806 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
3807 dprintk("NFS reply lookup: %d\n", status);
3811 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
3813 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
3814 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
3815 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3819 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3820 const struct qstr *name, struct nfs_fh *fhandle,
3821 struct nfs_fattr *fattr, struct nfs4_label *label)
3823 struct nfs4_exception exception = { };
3824 struct rpc_clnt *client = *clnt;
3827 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
3828 trace_nfs4_lookup(dir, name, err);
3830 case -NFS4ERR_BADNAME:
3833 case -NFS4ERR_MOVED:
3834 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
3835 if (err == -NFS4ERR_MOVED)
3836 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3838 case -NFS4ERR_WRONGSEC:
3840 if (client != *clnt)
3842 client = nfs4_negotiate_security(client, dir, name);
3844 return PTR_ERR(client);
3846 exception.retry = 1;
3849 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3851 } while (exception.retry);
3856 else if (client != *clnt)
3857 rpc_shutdown_client(client);
3862 static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
3863 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3864 struct nfs4_label *label)
3867 struct rpc_clnt *client = NFS_CLIENT(dir);
3869 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
3870 if (client != NFS_CLIENT(dir)) {
3871 rpc_shutdown_client(client);
3872 nfs_fixup_secinfo_attributes(fattr);
3878 nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
3879 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3881 struct rpc_clnt *client = NFS_CLIENT(dir);
3884 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
3886 return ERR_PTR(status);
3887 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
3890 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3892 struct nfs_server *server = NFS_SERVER(inode);
3893 struct nfs4_accessargs args = {
3894 .fh = NFS_FH(inode),
3895 .bitmask = server->cache_consistency_bitmask,
3897 struct nfs4_accessres res = {
3900 struct rpc_message msg = {
3901 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3904 .rpc_cred = entry->cred,
3906 int mode = entry->mask;
3910 * Determine which access bits we want to ask for...
3912 if (mode & MAY_READ)
3913 args.access |= NFS4_ACCESS_READ;
3914 if (S_ISDIR(inode->i_mode)) {
3915 if (mode & MAY_WRITE)
3916 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3917 if (mode & MAY_EXEC)
3918 args.access |= NFS4_ACCESS_LOOKUP;
3920 if (mode & MAY_WRITE)
3921 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3922 if (mode & MAY_EXEC)
3923 args.access |= NFS4_ACCESS_EXECUTE;
3926 res.fattr = nfs_alloc_fattr();
3927 if (res.fattr == NULL)
3930 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3932 nfs_access_set_mask(entry, res.access);
3933 nfs_refresh_inode(inode, res.fattr);
3935 nfs_free_fattr(res.fattr);
3939 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3941 struct nfs4_exception exception = { };
3944 err = _nfs4_proc_access(inode, entry);
3945 trace_nfs4_access(inode, err);
3946 err = nfs4_handle_exception(NFS_SERVER(inode), err,
3948 } while (exception.retry);
3953 * TODO: For the time being, we don't try to get any attributes
3954 * along with any of the zero-copy operations READ, READDIR,
3957 * In the case of the first three, we want to put the GETATTR
3958 * after the read-type operation -- this is because it is hard
3959 * to predict the length of a GETATTR response in v4, and thus
3960 * align the READ data correctly. This means that the GETATTR
3961 * may end up partially falling into the page cache, and we should
3962 * shift it into the 'tail' of the xdr_buf before processing.
3963 * To do this efficiently, we need to know the total length
3964 * of data received, which doesn't seem to be available outside
3967 * In the case of WRITE, we also want to put the GETATTR after
3968 * the operation -- in this case because we want to make sure
3969 * we get the post-operation mtime and size.
3971 * Both of these changes to the XDR layer would in fact be quite
3972 * minor, but I decided to leave them for a subsequent patch.
3974 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3975 unsigned int pgbase, unsigned int pglen)
3977 struct nfs4_readlink args = {
3978 .fh = NFS_FH(inode),
3983 struct nfs4_readlink_res res;
3984 struct rpc_message msg = {
3985 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3990 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
3993 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3994 unsigned int pgbase, unsigned int pglen)
3996 struct nfs4_exception exception = { };
3999 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4000 trace_nfs4_readlink(inode, err);
4001 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4003 } while (exception.retry);
4008 * This is just for mknod. open(O_CREAT) will always do ->open_context().
4011 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
4014 struct nfs_server *server = NFS_SERVER(dir);
4015 struct nfs4_label l, *ilabel = NULL;
4016 struct nfs_open_context *ctx;
4017 struct nfs4_state *state;
4020 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
4022 return PTR_ERR(ctx);
4024 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4026 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4027 sattr->ia_mode &= ~current_umask();
4028 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
4029 if (IS_ERR(state)) {
4030 status = PTR_ERR(state);
4034 nfs4_label_release_security(ilabel);
4035 put_nfs_open_context(ctx);
4039 static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
4041 struct nfs_server *server = NFS_SERVER(dir);
4042 struct nfs_removeargs args = {
4046 struct nfs_removeres res = {
4049 struct rpc_message msg = {
4050 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4056 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
4058 update_changeattr(dir, &res.cinfo);
4062 static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
4064 struct nfs4_exception exception = { };
4067 err = _nfs4_proc_remove(dir, name);
4068 trace_nfs4_remove(dir, name, err);
4069 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4071 } while (exception.retry);
4075 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
4077 struct nfs_server *server = NFS_SERVER(dir);
4078 struct nfs_removeargs *args = msg->rpc_argp;
4079 struct nfs_removeres *res = msg->rpc_resp;
4081 res->server = server;
4082 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
4083 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
4085 nfs_fattr_init(res->dir_attr);
4088 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4090 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb),
4091 &data->args.seq_args,
4096 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
4098 struct nfs_unlinkdata *data = task->tk_calldata;
4099 struct nfs_removeres *res = &data->res;
4101 if (!nfs4_sequence_done(task, &res->seq_res))
4103 if (nfs4_async_handle_error(task, res->server, NULL,
4104 &data->timeout) == -EAGAIN)
4106 update_changeattr(dir, &res->cinfo);
4110 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4112 struct nfs_server *server = NFS_SERVER(dir);
4113 struct nfs_renameargs *arg = msg->rpc_argp;
4114 struct nfs_renameres *res = msg->rpc_resp;
4116 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
4117 res->server = server;
4118 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
4121 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4123 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
4124 &data->args.seq_args,
4129 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4130 struct inode *new_dir)
4132 struct nfs_renamedata *data = task->tk_calldata;
4133 struct nfs_renameres *res = &data->res;
4135 if (!nfs4_sequence_done(task, &res->seq_res))
4137 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
4140 update_changeattr(old_dir, &res->old_cinfo);
4141 update_changeattr(new_dir, &res->new_cinfo);
4145 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4147 struct nfs_server *server = NFS_SERVER(inode);
4148 struct nfs4_link_arg arg = {
4149 .fh = NFS_FH(inode),
4150 .dir_fh = NFS_FH(dir),
4152 .bitmask = server->attr_bitmask,
4154 struct nfs4_link_res res = {
4158 struct rpc_message msg = {
4159 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4163 int status = -ENOMEM;
4165 res.fattr = nfs_alloc_fattr();
4166 if (res.fattr == NULL)
4169 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4170 if (IS_ERR(res.label)) {
4171 status = PTR_ERR(res.label);
4174 arg.bitmask = nfs4_bitmask(server, res.label);
4176 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4178 update_changeattr(dir, &res.cinfo);
4179 status = nfs_post_op_update_inode(inode, res.fattr);
4181 nfs_setsecurity(inode, res.fattr, res.label);
4185 nfs4_label_free(res.label);
4188 nfs_free_fattr(res.fattr);
4192 static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4194 struct nfs4_exception exception = { };
4197 err = nfs4_handle_exception(NFS_SERVER(inode),
4198 _nfs4_proc_link(inode, dir, name),
4200 } while (exception.retry);
4204 struct nfs4_createdata {
4205 struct rpc_message msg;
4206 struct nfs4_create_arg arg;
4207 struct nfs4_create_res res;
4209 struct nfs_fattr fattr;
4210 struct nfs4_label *label;
4213 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
4214 const struct qstr *name, struct iattr *sattr, u32 ftype)
4216 struct nfs4_createdata *data;
4218 data = kzalloc(sizeof(*data), GFP_KERNEL);
4220 struct nfs_server *server = NFS_SERVER(dir);
4222 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4223 if (IS_ERR(data->label))
4226 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4227 data->msg.rpc_argp = &data->arg;
4228 data->msg.rpc_resp = &data->res;
4229 data->arg.dir_fh = NFS_FH(dir);
4230 data->arg.server = server;
4231 data->arg.name = name;
4232 data->arg.attrs = sattr;
4233 data->arg.ftype = ftype;
4234 data->arg.bitmask = nfs4_bitmask(server, data->label);
4235 data->arg.umask = current_umask();
4236 data->res.server = server;
4237 data->res.fh = &data->fh;
4238 data->res.fattr = &data->fattr;
4239 data->res.label = data->label;
4240 nfs_fattr_init(data->res.fattr);
4248 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4250 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
4251 &data->arg.seq_args, &data->res.seq_res, 1);
4253 update_changeattr(dir, &data->res.dir_cinfo);
4254 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
4259 static void nfs4_free_createdata(struct nfs4_createdata *data)
4261 nfs4_label_free(data->label);
4265 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4266 struct page *page, unsigned int len, struct iattr *sattr,
4267 struct nfs4_label *label)
4269 struct nfs4_createdata *data;
4270 int status = -ENAMETOOLONG;
4272 if (len > NFS4_MAXPATHLEN)
4276 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4280 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4281 data->arg.u.symlink.pages = &page;
4282 data->arg.u.symlink.len = len;
4283 data->arg.label = label;
4285 status = nfs4_do_create(dir, dentry, data);
4287 nfs4_free_createdata(data);
4292 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4293 struct page *page, unsigned int len, struct iattr *sattr)
4295 struct nfs4_exception exception = { };
4296 struct nfs4_label l, *label = NULL;
4299 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4302 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4303 trace_nfs4_symlink(dir, &dentry->d_name, err);
4304 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4306 } while (exception.retry);
4308 nfs4_label_release_security(label);
4312 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4313 struct iattr *sattr, struct nfs4_label *label)
4315 struct nfs4_createdata *data;
4316 int status = -ENOMEM;
4318 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4322 data->arg.label = label;
4323 status = nfs4_do_create(dir, dentry, data);
4325 nfs4_free_createdata(data);
4330 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4331 struct iattr *sattr)
4333 struct nfs_server *server = NFS_SERVER(dir);
4334 struct nfs4_exception exception = { };
4335 struct nfs4_label l, *label = NULL;
4338 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4340 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4341 sattr->ia_mode &= ~current_umask();
4343 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4344 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4345 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4347 } while (exception.retry);
4348 nfs4_label_release_security(label);
4353 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
4354 u64 cookie, struct page **pages, unsigned int count, int plus)
4356 struct inode *dir = d_inode(dentry);
4357 struct nfs4_readdir_arg args = {
4362 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
4365 struct nfs4_readdir_res res;
4366 struct rpc_message msg = {
4367 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4374 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4376 (unsigned long long)cookie);
4377 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
4378 res.pgbase = args.pgbase;
4379 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4381 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
4382 status += args.pgbase;
4385 nfs_invalidate_atime(dir);
4387 dprintk("%s: returns %d\n", __func__, status);
4391 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
4392 u64 cookie, struct page **pages, unsigned int count, int plus)
4394 struct nfs4_exception exception = { };
4397 err = _nfs4_proc_readdir(dentry, cred, cookie,
4398 pages, count, plus);
4399 trace_nfs4_readdir(d_inode(dentry), err);
4400 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
4402 } while (exception.retry);
4406 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4407 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
4409 struct nfs4_createdata *data;
4410 int mode = sattr->ia_mode;
4411 int status = -ENOMEM;
4413 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4418 data->arg.ftype = NF4FIFO;
4419 else if (S_ISBLK(mode)) {
4420 data->arg.ftype = NF4BLK;
4421 data->arg.u.device.specdata1 = MAJOR(rdev);
4422 data->arg.u.device.specdata2 = MINOR(rdev);
4424 else if (S_ISCHR(mode)) {
4425 data->arg.ftype = NF4CHR;
4426 data->arg.u.device.specdata1 = MAJOR(rdev);
4427 data->arg.u.device.specdata2 = MINOR(rdev);
4428 } else if (!S_ISSOCK(mode)) {
4433 data->arg.label = label;
4434 status = nfs4_do_create(dir, dentry, data);
4436 nfs4_free_createdata(data);
4441 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4442 struct iattr *sattr, dev_t rdev)
4444 struct nfs_server *server = NFS_SERVER(dir);
4445 struct nfs4_exception exception = { };
4446 struct nfs4_label l, *label = NULL;
4449 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4451 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4452 sattr->ia_mode &= ~current_umask();
4454 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4455 trace_nfs4_mknod(dir, &dentry->d_name, err);
4456 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4458 } while (exception.retry);
4460 nfs4_label_release_security(label);
4465 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4466 struct nfs_fsstat *fsstat)
4468 struct nfs4_statfs_arg args = {
4470 .bitmask = server->attr_bitmask,
4472 struct nfs4_statfs_res res = {
4475 struct rpc_message msg = {
4476 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4481 nfs_fattr_init(fsstat->fattr);
4482 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4485 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4487 struct nfs4_exception exception = { };
4490 err = nfs4_handle_exception(server,
4491 _nfs4_proc_statfs(server, fhandle, fsstat),
4493 } while (exception.retry);
4497 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4498 struct nfs_fsinfo *fsinfo)
4500 struct nfs4_fsinfo_arg args = {
4502 .bitmask = server->attr_bitmask,
4504 struct nfs4_fsinfo_res res = {
4507 struct rpc_message msg = {
4508 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4513 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4516 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4518 struct nfs4_exception exception = { };
4519 unsigned long now = jiffies;
4523 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
4524 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
4526 nfs4_set_lease_period(server->nfs_client,
4527 fsinfo->lease_time * HZ,
4531 err = nfs4_handle_exception(server, err, &exception);
4532 } while (exception.retry);
4536 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4540 nfs_fattr_init(fsinfo->fattr);
4541 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
4543 /* block layout checks this! */
4544 server->pnfs_blksize = fsinfo->blksize;
4545 set_pnfs_layoutdriver(server, fhandle, fsinfo);
4551 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4552 struct nfs_pathconf *pathconf)
4554 struct nfs4_pathconf_arg args = {
4556 .bitmask = server->attr_bitmask,
4558 struct nfs4_pathconf_res res = {
4559 .pathconf = pathconf,
4561 struct rpc_message msg = {
4562 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4567 /* None of the pathconf attributes are mandatory to implement */
4568 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4569 memset(pathconf, 0, sizeof(*pathconf));
4573 nfs_fattr_init(pathconf->fattr);
4574 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4577 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4578 struct nfs_pathconf *pathconf)
4580 struct nfs4_exception exception = { };
4584 err = nfs4_handle_exception(server,
4585 _nfs4_proc_pathconf(server, fhandle, pathconf),
4587 } while (exception.retry);
4591 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
4592 const struct nfs_open_context *ctx,
4593 const struct nfs_lock_context *l_ctx,
4596 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
4598 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4600 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4601 const struct nfs_open_context *ctx,
4602 const struct nfs_lock_context *l_ctx,
4605 nfs4_stateid current_stateid;
4607 /* If the current stateid represents a lost lock, then exit */
4608 if (nfs4_set_rw_stateid(¤t_stateid, ctx, l_ctx, fmode) == -EIO)
4610 return nfs4_stateid_match(stateid, ¤t_stateid);
4613 static bool nfs4_error_stateid_expired(int err)
4616 case -NFS4ERR_DELEG_REVOKED:
4617 case -NFS4ERR_ADMIN_REVOKED:
4618 case -NFS4ERR_BAD_STATEID:
4619 case -NFS4ERR_STALE_STATEID:
4620 case -NFS4ERR_OLD_STATEID:
4621 case -NFS4ERR_OPENMODE:
4622 case -NFS4ERR_EXPIRED:
4628 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
4630 struct nfs_server *server = NFS_SERVER(hdr->inode);
4632 trace_nfs4_read(hdr, task->tk_status);
4633 if (task->tk_status < 0) {
4634 struct nfs4_exception exception = {
4635 .inode = hdr->inode,
4636 .state = hdr->args.context->state,
4637 .stateid = &hdr->args.stateid,
4639 task->tk_status = nfs4_async_handle_exception(task,
4640 server, task->tk_status, &exception);
4641 if (exception.retry) {
4642 rpc_restart_call_prepare(task);
4647 if (task->tk_status > 0)
4648 renew_lease(server, hdr->timestamp);
4652 static bool nfs4_read_stateid_changed(struct rpc_task *task,
4653 struct nfs_pgio_args *args)
4656 if (!nfs4_error_stateid_expired(task->tk_status) ||
4657 nfs4_stateid_is_current(&args->stateid,
4662 rpc_restart_call_prepare(task);
4666 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
4669 dprintk("--> %s\n", __func__);
4671 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
4673 if (nfs4_read_stateid_changed(task, &hdr->args))
4675 if (task->tk_status > 0)
4676 nfs_invalidate_atime(hdr->inode);
4677 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4678 nfs4_read_done_cb(task, hdr);
4681 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4682 struct rpc_message *msg)
4684 hdr->timestamp = jiffies;
4685 if (!hdr->pgio_done_cb)
4686 hdr->pgio_done_cb = nfs4_read_done_cb;
4687 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
4688 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
4691 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4692 struct nfs_pgio_header *hdr)
4694 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4695 &hdr->args.seq_args,
4699 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4700 hdr->args.lock_context,
4701 hdr->rw_ops->rw_mode) == -EIO)
4703 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
4708 static int nfs4_write_done_cb(struct rpc_task *task,
4709 struct nfs_pgio_header *hdr)
4711 struct inode *inode = hdr->inode;
4713 trace_nfs4_write(hdr, task->tk_status);
4714 if (task->tk_status < 0) {
4715 struct nfs4_exception exception = {
4716 .inode = hdr->inode,
4717 .state = hdr->args.context->state,
4718 .stateid = &hdr->args.stateid,
4720 task->tk_status = nfs4_async_handle_exception(task,
4721 NFS_SERVER(inode), task->tk_status,
4723 if (exception.retry) {
4724 rpc_restart_call_prepare(task);
4728 if (task->tk_status >= 0) {
4729 renew_lease(NFS_SERVER(inode), hdr->timestamp);
4730 nfs_writeback_update_inode(hdr);
4735 static bool nfs4_write_stateid_changed(struct rpc_task *task,
4736 struct nfs_pgio_args *args)
4739 if (!nfs4_error_stateid_expired(task->tk_status) ||
4740 nfs4_stateid_is_current(&args->stateid,
4745 rpc_restart_call_prepare(task);
4749 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
4751 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
4753 if (nfs4_write_stateid_changed(task, &hdr->args))
4755 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4756 nfs4_write_done_cb(task, hdr);
4760 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
4762 /* Don't request attributes for pNFS or O_DIRECT writes */
4763 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
4765 /* Otherwise, request attributes if and only if we don't hold
4768 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
4771 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4772 struct rpc_message *msg)
4774 struct nfs_server *server = NFS_SERVER(hdr->inode);
4776 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4777 hdr->args.bitmask = NULL;
4778 hdr->res.fattr = NULL;
4780 hdr->args.bitmask = server->cache_consistency_bitmask;
4782 if (!hdr->pgio_done_cb)
4783 hdr->pgio_done_cb = nfs4_write_done_cb;
4784 hdr->res.server = server;
4785 hdr->timestamp = jiffies;
4787 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
4788 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
4791 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4793 nfs4_setup_sequence(NFS_SERVER(data->inode),
4794 &data->args.seq_args,
4799 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
4801 struct inode *inode = data->inode;
4803 trace_nfs4_commit(data, task->tk_status);
4804 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4805 NULL, NULL) == -EAGAIN) {
4806 rpc_restart_call_prepare(task);
4812 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
4814 if (!nfs4_sequence_done(task, &data->res.seq_res))
4816 return data->commit_done_cb(task, data);
4819 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
4821 struct nfs_server *server = NFS_SERVER(data->inode);
4823 if (data->commit_done_cb == NULL)
4824 data->commit_done_cb = nfs4_commit_done_cb;
4825 data->res.server = server;
4826 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
4827 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4830 struct nfs4_renewdata {
4831 struct nfs_client *client;
4832 unsigned long timestamp;
4836 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4837 * standalone procedure for queueing an asynchronous RENEW.
4839 static void nfs4_renew_release(void *calldata)
4841 struct nfs4_renewdata *data = calldata;
4842 struct nfs_client *clp = data->client;
4844 if (atomic_read(&clp->cl_count) > 1)
4845 nfs4_schedule_state_renewal(clp);
4846 nfs_put_client(clp);
4850 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
4852 struct nfs4_renewdata *data = calldata;
4853 struct nfs_client *clp = data->client;
4854 unsigned long timestamp = data->timestamp;
4856 trace_nfs4_renew_async(clp, task->tk_status);
4857 switch (task->tk_status) {
4860 case -NFS4ERR_LEASE_MOVED:
4861 nfs4_schedule_lease_moved_recovery(clp);
4864 /* Unless we're shutting down, schedule state recovery! */
4865 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4867 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
4868 nfs4_schedule_lease_recovery(clp);
4871 nfs4_schedule_path_down_recovery(clp);
4873 do_renew_lease(clp, timestamp);
4876 static const struct rpc_call_ops nfs4_renew_ops = {
4877 .rpc_call_done = nfs4_renew_done,
4878 .rpc_release = nfs4_renew_release,
4881 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
4883 struct rpc_message msg = {
4884 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4888 struct nfs4_renewdata *data;
4890 if (renew_flags == 0)
4892 if (!atomic_inc_not_zero(&clp->cl_count))
4894 data = kmalloc(sizeof(*data), GFP_NOFS);
4898 data->timestamp = jiffies;
4899 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
4900 &nfs4_renew_ops, data);
4903 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
4905 struct rpc_message msg = {
4906 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4910 unsigned long now = jiffies;
4913 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4916 do_renew_lease(clp, now);
4920 static inline int nfs4_server_supports_acls(struct nfs_server *server)
4922 return server->caps & NFS_CAP_ACLS;
4925 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4926 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
4929 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
4931 static int buf_to_pages_noslab(const void *buf, size_t buflen,
4932 struct page **pages)
4934 struct page *newpage, **spages;
4940 len = min_t(size_t, PAGE_SIZE, buflen);
4941 newpage = alloc_page(GFP_KERNEL);
4943 if (newpage == NULL)
4945 memcpy(page_address(newpage), buf, len);
4950 } while (buflen != 0);
4956 __free_page(spages[rc-1]);
4960 struct nfs4_cached_acl {
4966 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
4968 struct nfs_inode *nfsi = NFS_I(inode);
4970 spin_lock(&inode->i_lock);
4971 kfree(nfsi->nfs4_acl);
4972 nfsi->nfs4_acl = acl;
4973 spin_unlock(&inode->i_lock);
4976 static void nfs4_zap_acl_attr(struct inode *inode)
4978 nfs4_set_cached_acl(inode, NULL);
4981 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4983 struct nfs_inode *nfsi = NFS_I(inode);
4984 struct nfs4_cached_acl *acl;
4987 spin_lock(&inode->i_lock);
4988 acl = nfsi->nfs4_acl;
4991 if (buf == NULL) /* user is just asking for length */
4993 if (acl->cached == 0)
4995 ret = -ERANGE; /* see getxattr(2) man page */
4996 if (acl->len > buflen)
4998 memcpy(buf, acl->data, acl->len);
5002 spin_unlock(&inode->i_lock);
5006 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
5008 struct nfs4_cached_acl *acl;
5009 size_t buflen = sizeof(*acl) + acl_len;
5011 if (buflen <= PAGE_SIZE) {
5012 acl = kmalloc(buflen, GFP_KERNEL);
5016 _copy_from_pages(acl->data, pages, pgbase, acl_len);
5018 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5025 nfs4_set_cached_acl(inode, acl);
5029 * The getxattr API returns the required buffer length when called with a
5030 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5031 * the required buf. On a NULL buf, we send a page of data to the server
5032 * guessing that the ACL request can be serviced by a page. If so, we cache
5033 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5034 * the cache. If not so, we throw away the page, and cache the required
5035 * length. The next getxattr call will then produce another round trip to
5036 * the server, this time with the input buf of the required size.
5038 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5040 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
5041 struct nfs_getaclargs args = {
5042 .fh = NFS_FH(inode),
5046 struct nfs_getaclres res = {
5049 struct rpc_message msg = {
5050 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5054 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
5055 int ret = -ENOMEM, i;
5057 /* As long as we're doing a round trip to the server anyway,
5058 * let's be prepared for a page of acl data. */
5061 if (npages > ARRAY_SIZE(pages))
5064 for (i = 0; i < npages; i++) {
5065 pages[i] = alloc_page(GFP_KERNEL);
5070 /* for decoding across pages */
5071 res.acl_scratch = alloc_page(GFP_KERNEL);
5072 if (!res.acl_scratch)
5075 args.acl_len = npages * PAGE_SIZE;
5077 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
5078 __func__, buf, buflen, npages, args.acl_len);
5079 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5080 &msg, &args.seq_args, &res.seq_res, 0);
5084 /* Handle the case where the passed-in buffer is too short */
5085 if (res.acl_flags & NFS4_ACL_TRUNC) {
5086 /* Did the user only issue a request for the acl length? */
5092 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
5094 if (res.acl_len > buflen) {
5098 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
5103 for (i = 0; i < npages; i++)
5105 __free_page(pages[i]);
5106 if (res.acl_scratch)
5107 __free_page(res.acl_scratch);
5111 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5113 struct nfs4_exception exception = { };
5116 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
5117 trace_nfs4_get_acl(inode, ret);
5120 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5121 } while (exception.retry);
5125 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5127 struct nfs_server *server = NFS_SERVER(inode);
5130 if (!nfs4_server_supports_acls(server))
5132 ret = nfs_revalidate_inode(server, inode);
5135 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5136 nfs_zap_acl_cache(inode);
5137 ret = nfs4_read_cached_acl(inode, buf, buflen);
5139 /* -ENOENT is returned if there is no ACL or if there is an ACL
5140 * but no cached acl data, just the acl length */
5142 return nfs4_get_acl_uncached(inode, buf, buflen);
5145 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5147 struct nfs_server *server = NFS_SERVER(inode);
5148 struct page *pages[NFS4ACL_MAXPAGES];
5149 struct nfs_setaclargs arg = {
5150 .fh = NFS_FH(inode),
5154 struct nfs_setaclres res;
5155 struct rpc_message msg = {
5156 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5160 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
5163 if (!nfs4_server_supports_acls(server))
5165 if (npages > ARRAY_SIZE(pages))
5167 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
5170 nfs4_inode_return_delegation(inode);
5171 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5174 * Free each page after tx, so the only ref left is
5175 * held by the network stack
5178 put_page(pages[i-1]);
5181 * Acl update can result in inode attribute update.
5182 * so mark the attribute cache invalid.
5184 spin_lock(&inode->i_lock);
5185 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5186 spin_unlock(&inode->i_lock);
5187 nfs_access_zap_cache(inode);
5188 nfs_zap_acl_cache(inode);
5192 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5194 struct nfs4_exception exception = { };
5197 err = __nfs4_proc_set_acl(inode, buf, buflen);
5198 trace_nfs4_set_acl(inode, err);
5199 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5201 } while (exception.retry);
5205 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
5206 static int _nfs4_get_security_label(struct inode *inode, void *buf,
5209 struct nfs_server *server = NFS_SERVER(inode);
5210 struct nfs_fattr fattr;
5211 struct nfs4_label label = {0, 0, buflen, buf};
5213 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5214 struct nfs4_getattr_arg arg = {
5215 .fh = NFS_FH(inode),
5218 struct nfs4_getattr_res res = {
5223 struct rpc_message msg = {
5224 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
5230 nfs_fattr_init(&fattr);
5232 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
5235 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5237 if (buflen < label.len)
5242 static int nfs4_get_security_label(struct inode *inode, void *buf,
5245 struct nfs4_exception exception = { };
5248 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5252 err = _nfs4_get_security_label(inode, buf, buflen);
5253 trace_nfs4_get_security_label(inode, err);
5254 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5256 } while (exception.retry);
5260 static int _nfs4_do_set_security_label(struct inode *inode,
5261 struct nfs4_label *ilabel,
5262 struct nfs_fattr *fattr,
5263 struct nfs4_label *olabel)
5266 struct iattr sattr = {0};
5267 struct nfs_server *server = NFS_SERVER(inode);
5268 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5269 struct nfs_setattrargs arg = {
5270 .fh = NFS_FH(inode),
5276 struct nfs_setattrres res = {
5281 struct rpc_message msg = {
5282 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5288 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
5290 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5292 dprintk("%s failed: %d\n", __func__, status);
5297 static int nfs4_do_set_security_label(struct inode *inode,
5298 struct nfs4_label *ilabel,
5299 struct nfs_fattr *fattr,
5300 struct nfs4_label *olabel)
5302 struct nfs4_exception exception = { };
5306 err = _nfs4_do_set_security_label(inode, ilabel,
5308 trace_nfs4_set_security_label(inode, err);
5309 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5311 } while (exception.retry);
5316 nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
5318 struct nfs4_label ilabel, *olabel = NULL;
5319 struct nfs_fattr fattr;
5320 struct rpc_cred *cred;
5323 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5326 nfs_fattr_init(&fattr);
5330 ilabel.label = (char *)buf;
5331 ilabel.len = buflen;
5333 cred = rpc_lookup_cred();
5335 return PTR_ERR(cred);
5337 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5338 if (IS_ERR(olabel)) {
5339 status = -PTR_ERR(olabel);
5343 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5345 nfs_setsecurity(inode, &fattr, olabel);
5347 nfs4_label_free(olabel);
5352 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5355 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5356 nfs4_verifier *bootverf)
5360 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5361 /* An impossible timestamp guarantees this value
5362 * will never match a generated boot time. */
5363 verf[0] = cpu_to_be32(U32_MAX);
5364 verf[1] = cpu_to_be32(U32_MAX);
5366 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
5367 u64 ns = ktime_to_ns(nn->boot_time);
5369 verf[0] = cpu_to_be32(ns >> 32);
5370 verf[1] = cpu_to_be32(ns);
5372 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5376 nfs4_init_nonuniform_client_string(struct nfs_client *clp)
5381 if (clp->cl_owner_id != NULL)
5385 len = 14 + strlen(clp->cl_ipaddr) + 1 +
5386 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5388 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5392 if (len > NFS4_OPAQUE_LIMIT + 1)
5396 * Since this string is allocated at mount time, and held until the
5397 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5398 * about a memory-reclaim deadlock.
5400 str = kmalloc(len, GFP_KERNEL);
5405 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
5407 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5408 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
5411 clp->cl_owner_id = str;
5416 nfs4_init_uniquifier_client_string(struct nfs_client *clp)
5421 len = 10 + 10 + 1 + 10 + 1 +
5422 strlen(nfs4_client_id_uniquifier) + 1 +
5423 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5425 if (len > NFS4_OPAQUE_LIMIT + 1)
5429 * Since this string is allocated at mount time, and held until the
5430 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5431 * about a memory-reclaim deadlock.
5433 str = kmalloc(len, GFP_KERNEL);
5437 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
5438 clp->rpc_ops->version, clp->cl_minorversion,
5439 nfs4_client_id_uniquifier,
5440 clp->cl_rpcclient->cl_nodename);
5441 clp->cl_owner_id = str;
5446 nfs4_init_uniform_client_string(struct nfs_client *clp)
5451 if (clp->cl_owner_id != NULL)
5454 if (nfs4_client_id_uniquifier[0] != '\0')
5455 return nfs4_init_uniquifier_client_string(clp);
5457 len = 10 + 10 + 1 + 10 + 1 +
5458 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5460 if (len > NFS4_OPAQUE_LIMIT + 1)
5464 * Since this string is allocated at mount time, and held until the
5465 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5466 * about a memory-reclaim deadlock.
5468 str = kmalloc(len, GFP_KERNEL);
5472 scnprintf(str, len, "Linux NFSv%u.%u %s",
5473 clp->rpc_ops->version, clp->cl_minorversion,
5474 clp->cl_rpcclient->cl_nodename);
5475 clp->cl_owner_id = str;
5480 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5481 * services. Advertise one based on the address family of the
5485 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5487 if (strchr(clp->cl_ipaddr, ':') != NULL)
5488 return scnprintf(buf, len, "tcp6");
5490 return scnprintf(buf, len, "tcp");
5493 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5495 struct nfs4_setclientid *sc = calldata;
5497 if (task->tk_status == 0)
5498 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5501 static const struct rpc_call_ops nfs4_setclientid_ops = {
5502 .rpc_call_done = nfs4_setclientid_done,
5506 * nfs4_proc_setclientid - Negotiate client ID
5507 * @clp: state data structure
5508 * @program: RPC program for NFSv4 callback service
5509 * @port: IP port number for NFS4 callback service
5510 * @cred: RPC credential to use for this call
5511 * @res: where to place the result
5513 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5515 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5516 unsigned short port, struct rpc_cred *cred,
5517 struct nfs4_setclientid_res *res)
5519 nfs4_verifier sc_verifier;
5520 struct nfs4_setclientid setclientid = {
5521 .sc_verifier = &sc_verifier,
5525 struct rpc_message msg = {
5526 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5527 .rpc_argp = &setclientid,
5531 struct rpc_task *task;
5532 struct rpc_task_setup task_setup_data = {
5533 .rpc_client = clp->cl_rpcclient,
5534 .rpc_message = &msg,
5535 .callback_ops = &nfs4_setclientid_ops,
5536 .callback_data = &setclientid,
5537 .flags = RPC_TASK_TIMEOUT,
5541 /* nfs_client_id4 */
5542 nfs4_init_boot_verifier(clp, &sc_verifier);
5544 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5545 status = nfs4_init_uniform_client_string(clp);
5547 status = nfs4_init_nonuniform_client_string(clp);
5553 setclientid.sc_netid_len =
5554 nfs4_init_callback_netid(clp,
5555 setclientid.sc_netid,
5556 sizeof(setclientid.sc_netid));
5557 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
5558 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
5559 clp->cl_ipaddr, port >> 8, port & 255);
5561 dprintk("NFS call setclientid auth=%s, '%s'\n",
5562 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5564 task = rpc_run_task(&task_setup_data);
5566 status = PTR_ERR(task);
5569 status = task->tk_status;
5570 if (setclientid.sc_cred) {
5571 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5572 put_rpccred(setclientid.sc_cred);
5576 trace_nfs4_setclientid(clp, status);
5577 dprintk("NFS reply setclientid: %d\n", status);
5582 * nfs4_proc_setclientid_confirm - Confirm client ID
5583 * @clp: state data structure
5584 * @res: result of a previous SETCLIENTID
5585 * @cred: RPC credential to use for this call
5587 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5589 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
5590 struct nfs4_setclientid_res *arg,
5591 struct rpc_cred *cred)
5593 struct rpc_message msg = {
5594 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
5600 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5601 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5603 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5604 trace_nfs4_setclientid_confirm(clp, status);
5605 dprintk("NFS reply setclientid_confirm: %d\n", status);
5609 struct nfs4_delegreturndata {
5610 struct nfs4_delegreturnargs args;
5611 struct nfs4_delegreturnres res;
5613 nfs4_stateid stateid;
5614 unsigned long timestamp;
5616 struct nfs4_layoutreturn_args arg;
5617 struct nfs4_layoutreturn_res res;
5618 struct nfs4_xdr_opaque_data ld_private;
5622 struct nfs_fattr fattr;
5624 struct inode *inode;
5627 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5629 struct nfs4_delegreturndata *data = calldata;
5631 if (!nfs4_sequence_done(task, &data->res.seq_res))
5634 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
5636 /* Handle Layoutreturn errors */
5637 if (data->args.lr_args && task->tk_status != 0) {
5638 switch(data->res.lr_ret) {
5640 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5643 data->args.lr_args = NULL;
5644 data->res.lr_res = NULL;
5646 case -NFS4ERR_ADMIN_REVOKED:
5647 case -NFS4ERR_DELEG_REVOKED:
5648 case -NFS4ERR_EXPIRED:
5649 case -NFS4ERR_BAD_STATEID:
5650 case -NFS4ERR_OLD_STATEID:
5651 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
5652 case -NFS4ERR_WRONG_CRED:
5653 data->args.lr_args = NULL;
5654 data->res.lr_res = NULL;
5655 data->res.lr_ret = 0;
5656 rpc_restart_call_prepare(task);
5661 switch (task->tk_status) {
5663 renew_lease(data->res.server, data->timestamp);
5665 case -NFS4ERR_ADMIN_REVOKED:
5666 case -NFS4ERR_DELEG_REVOKED:
5667 case -NFS4ERR_EXPIRED:
5668 nfs4_free_revoked_stateid(data->res.server,
5670 task->tk_msg.rpc_cred);
5671 case -NFS4ERR_BAD_STATEID:
5672 case -NFS4ERR_OLD_STATEID:
5673 case -NFS4ERR_STALE_STATEID:
5674 task->tk_status = 0;
5677 if (nfs4_async_handle_error(task, data->res.server,
5678 NULL, NULL) == -EAGAIN) {
5679 rpc_restart_call_prepare(task);
5683 data->rpc_status = task->tk_status;
5686 static void nfs4_delegreturn_release(void *calldata)
5688 struct nfs4_delegreturndata *data = calldata;
5689 struct inode *inode = data->inode;
5693 pnfs_roc_release(&data->lr.arg, &data->lr.res,
5695 nfs_iput_and_deactive(inode);
5700 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5702 struct nfs4_delegreturndata *d_data;
5704 d_data = (struct nfs4_delegreturndata *)data;
5706 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
5709 nfs4_setup_sequence(d_data->res.server,
5710 &d_data->args.seq_args,
5711 &d_data->res.seq_res,
5715 static const struct rpc_call_ops nfs4_delegreturn_ops = {
5716 .rpc_call_prepare = nfs4_delegreturn_prepare,
5717 .rpc_call_done = nfs4_delegreturn_done,
5718 .rpc_release = nfs4_delegreturn_release,
5721 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
5723 struct nfs4_delegreturndata *data;
5724 struct nfs_server *server = NFS_SERVER(inode);
5725 struct rpc_task *task;
5726 struct rpc_message msg = {
5727 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5730 struct rpc_task_setup task_setup_data = {
5731 .rpc_client = server->client,
5732 .rpc_message = &msg,
5733 .callback_ops = &nfs4_delegreturn_ops,
5734 .flags = RPC_TASK_ASYNC,
5738 data = kzalloc(sizeof(*data), GFP_NOFS);
5741 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
5743 nfs4_state_protect(server->nfs_client,
5744 NFS_SP4_MACH_CRED_CLEANUP,
5745 &task_setup_data.rpc_client, &msg);
5747 data->args.fhandle = &data->fh;
5748 data->args.stateid = &data->stateid;
5749 data->args.bitmask = server->cache_consistency_bitmask;
5750 nfs_copy_fh(&data->fh, NFS_FH(inode));
5751 nfs4_stateid_copy(&data->stateid, stateid);
5752 data->res.fattr = &data->fattr;
5753 data->res.server = server;
5754 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5755 data->lr.arg.ld_private = &data->lr.ld_private;
5756 nfs_fattr_init(data->res.fattr);
5757 data->timestamp = jiffies;
5758 data->rpc_status = 0;
5759 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
5760 data->inode = nfs_igrab_and_active(inode);
5763 data->args.lr_args = &data->lr.arg;
5764 data->res.lr_res = &data->lr.res;
5766 } else if (data->lr.roc) {
5767 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
5768 data->lr.roc = false;
5771 task_setup_data.callback_data = data;
5772 msg.rpc_argp = &data->args;
5773 msg.rpc_resp = &data->res;
5774 task = rpc_run_task(&task_setup_data);
5776 return PTR_ERR(task);
5779 status = nfs4_wait_for_completion_rpc_task(task);
5782 status = data->rpc_status;
5784 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5786 nfs_refresh_inode(inode, &data->fattr);
5792 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
5794 struct nfs_server *server = NFS_SERVER(inode);
5795 struct nfs4_exception exception = { };
5798 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
5799 trace_nfs4_delegreturn(inode, stateid, err);
5801 case -NFS4ERR_STALE_STATEID:
5802 case -NFS4ERR_EXPIRED:
5806 err = nfs4_handle_exception(server, err, &exception);
5807 } while (exception.retry);
5811 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5813 struct inode *inode = state->inode;
5814 struct nfs_server *server = NFS_SERVER(inode);
5815 struct nfs_client *clp = server->nfs_client;
5816 struct nfs_lockt_args arg = {
5817 .fh = NFS_FH(inode),
5820 struct nfs_lockt_res res = {
5823 struct rpc_message msg = {
5824 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5827 .rpc_cred = state->owner->so_cred,
5829 struct nfs4_lock_state *lsp;
5832 arg.lock_owner.clientid = clp->cl_clientid;
5833 status = nfs4_set_lock_state(state, request);
5836 lsp = request->fl_u.nfs4_fl.owner;
5837 arg.lock_owner.id = lsp->ls_seqid.owner_id;
5838 arg.lock_owner.s_dev = server->s_dev;
5839 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5842 request->fl_type = F_UNLCK;
5844 case -NFS4ERR_DENIED:
5847 request->fl_ops->fl_release_private(request);
5848 request->fl_ops = NULL;
5853 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5855 struct nfs4_exception exception = { };
5859 err = _nfs4_proc_getlk(state, cmd, request);
5860 trace_nfs4_get_lock(request, state, cmd, err);
5861 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
5863 } while (exception.retry);
5867 struct nfs4_unlockdata {
5868 struct nfs_locku_args arg;
5869 struct nfs_locku_res res;
5870 struct nfs4_lock_state *lsp;
5871 struct nfs_open_context *ctx;
5872 struct file_lock fl;
5873 struct nfs_server *server;
5874 unsigned long timestamp;
5877 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5878 struct nfs_open_context *ctx,
5879 struct nfs4_lock_state *lsp,
5880 struct nfs_seqid *seqid)
5882 struct nfs4_unlockdata *p;
5883 struct inode *inode = lsp->ls_state->inode;
5885 p = kzalloc(sizeof(*p), GFP_NOFS);
5888 p->arg.fh = NFS_FH(inode);
5890 p->arg.seqid = seqid;
5891 p->res.seqid = seqid;
5893 atomic_inc(&lsp->ls_count);
5894 /* Ensure we don't close file until we're done freeing locks! */
5895 p->ctx = get_nfs_open_context(ctx);
5896 memcpy(&p->fl, fl, sizeof(p->fl));
5897 p->server = NFS_SERVER(inode);
5901 static void nfs4_locku_release_calldata(void *data)
5903 struct nfs4_unlockdata *calldata = data;
5904 nfs_free_seqid(calldata->arg.seqid);
5905 nfs4_put_lock_state(calldata->lsp);
5906 put_nfs_open_context(calldata->ctx);
5910 static void nfs4_locku_done(struct rpc_task *task, void *data)
5912 struct nfs4_unlockdata *calldata = data;
5914 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5916 switch (task->tk_status) {
5918 renew_lease(calldata->server, calldata->timestamp);
5919 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
5920 if (nfs4_update_lock_stateid(calldata->lsp,
5921 &calldata->res.stateid))
5923 case -NFS4ERR_ADMIN_REVOKED:
5924 case -NFS4ERR_EXPIRED:
5925 nfs4_free_revoked_stateid(calldata->server,
5926 &calldata->arg.stateid,
5927 task->tk_msg.rpc_cred);
5928 case -NFS4ERR_BAD_STATEID:
5929 case -NFS4ERR_OLD_STATEID:
5930 case -NFS4ERR_STALE_STATEID:
5931 if (!nfs4_stateid_match(&calldata->arg.stateid,
5932 &calldata->lsp->ls_stateid))
5933 rpc_restart_call_prepare(task);
5936 if (nfs4_async_handle_error(task, calldata->server,
5937 NULL, NULL) == -EAGAIN)
5938 rpc_restart_call_prepare(task);
5940 nfs_release_seqid(calldata->arg.seqid);
5943 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
5945 struct nfs4_unlockdata *calldata = data;
5947 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
5949 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
5950 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
5951 /* Note: exit _without_ running nfs4_locku_done */
5954 calldata->timestamp = jiffies;
5955 if (nfs4_setup_sequence(calldata->server,
5956 &calldata->arg.seq_args,
5957 &calldata->res.seq_res,
5959 nfs_release_seqid(calldata->arg.seqid);
5962 task->tk_action = NULL;
5964 nfs4_sequence_done(task, &calldata->res.seq_res);
5967 static const struct rpc_call_ops nfs4_locku_ops = {
5968 .rpc_call_prepare = nfs4_locku_prepare,
5969 .rpc_call_done = nfs4_locku_done,
5970 .rpc_release = nfs4_locku_release_calldata,
5973 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5974 struct nfs_open_context *ctx,
5975 struct nfs4_lock_state *lsp,
5976 struct nfs_seqid *seqid)
5978 struct nfs4_unlockdata *data;
5979 struct rpc_message msg = {
5980 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5981 .rpc_cred = ctx->cred,
5983 struct rpc_task_setup task_setup_data = {
5984 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
5985 .rpc_message = &msg,
5986 .callback_ops = &nfs4_locku_ops,
5987 .workqueue = nfsiod_workqueue,
5988 .flags = RPC_TASK_ASYNC,
5991 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5992 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5994 /* Ensure this is an unlock - when canceling a lock, the
5995 * canceled lock is passed in, and it won't be an unlock.
5997 fl->fl_type = F_UNLCK;
5999 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6001 nfs_free_seqid(seqid);
6002 return ERR_PTR(-ENOMEM);
6005 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
6006 msg.rpc_argp = &data->arg;
6007 msg.rpc_resp = &data->res;
6008 task_setup_data.callback_data = data;
6009 return rpc_run_task(&task_setup_data);
6012 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6014 struct inode *inode = state->inode;
6015 struct nfs4_state_owner *sp = state->owner;
6016 struct nfs_inode *nfsi = NFS_I(inode);
6017 struct nfs_seqid *seqid;
6018 struct nfs4_lock_state *lsp;
6019 struct rpc_task *task;
6020 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6022 unsigned char fl_flags = request->fl_flags;
6024 status = nfs4_set_lock_state(state, request);
6025 /* Unlock _before_ we do the RPC call */
6026 request->fl_flags |= FL_EXISTS;
6027 /* Exclude nfs_delegation_claim_locks() */
6028 mutex_lock(&sp->so_delegreturn_mutex);
6029 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
6030 down_read(&nfsi->rwsem);
6031 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
6032 up_read(&nfsi->rwsem);
6033 mutex_unlock(&sp->so_delegreturn_mutex);
6036 up_read(&nfsi->rwsem);
6037 mutex_unlock(&sp->so_delegreturn_mutex);
6040 /* Is this a delegated lock? */
6041 lsp = request->fl_u.nfs4_fl.owner;
6042 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6044 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6045 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
6049 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
6050 status = PTR_ERR(task);
6053 status = nfs4_wait_for_completion_rpc_task(task);
6056 request->fl_flags = fl_flags;
6057 trace_nfs4_unlock(request, state, F_SETLK, status);
6061 struct nfs4_lockdata {
6062 struct nfs_lock_args arg;
6063 struct nfs_lock_res res;
6064 struct nfs4_lock_state *lsp;
6065 struct nfs_open_context *ctx;
6066 struct file_lock fl;
6067 unsigned long timestamp;
6070 struct nfs_server *server;
6073 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
6074 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6077 struct nfs4_lockdata *p;
6078 struct inode *inode = lsp->ls_state->inode;
6079 struct nfs_server *server = NFS_SERVER(inode);
6080 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6082 p = kzalloc(sizeof(*p), gfp_mask);
6086 p->arg.fh = NFS_FH(inode);
6088 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
6089 if (IS_ERR(p->arg.open_seqid))
6091 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6092 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
6093 if (IS_ERR(p->arg.lock_seqid))
6094 goto out_free_seqid;
6095 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
6096 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
6097 p->arg.lock_owner.s_dev = server->s_dev;
6098 p->res.lock_seqid = p->arg.lock_seqid;
6101 atomic_inc(&lsp->ls_count);
6102 p->ctx = get_nfs_open_context(ctx);
6103 get_file(fl->fl_file);
6104 memcpy(&p->fl, fl, sizeof(p->fl));
6107 nfs_free_seqid(p->arg.open_seqid);
6113 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6115 struct nfs4_lockdata *data = calldata;
6116 struct nfs4_state *state = data->lsp->ls_state;
6118 dprintk("%s: begin!\n", __func__);
6119 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
6121 /* Do we need to do an open_to_lock_owner? */
6122 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
6123 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
6124 goto out_release_lock_seqid;
6126 nfs4_stateid_copy(&data->arg.open_stateid,
6127 &state->open_stateid);
6128 data->arg.new_lock_owner = 1;
6129 data->res.open_seqid = data->arg.open_seqid;
6131 data->arg.new_lock_owner = 0;
6132 nfs4_stateid_copy(&data->arg.lock_stateid,
6133 &data->lsp->ls_stateid);
6135 if (!nfs4_valid_open_stateid(state)) {
6136 data->rpc_status = -EBADF;
6137 task->tk_action = NULL;
6138 goto out_release_open_seqid;
6140 data->timestamp = jiffies;
6141 if (nfs4_setup_sequence(data->server,
6142 &data->arg.seq_args,
6146 out_release_open_seqid:
6147 nfs_release_seqid(data->arg.open_seqid);
6148 out_release_lock_seqid:
6149 nfs_release_seqid(data->arg.lock_seqid);
6151 nfs4_sequence_done(task, &data->res.seq_res);
6152 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
6155 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6157 struct nfs4_lockdata *data = calldata;
6158 struct nfs4_lock_state *lsp = data->lsp;
6160 dprintk("%s: begin!\n", __func__);
6162 if (!nfs4_sequence_done(task, &data->res.seq_res))
6165 data->rpc_status = task->tk_status;
6166 switch (task->tk_status) {
6168 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
6170 if (data->arg.new_lock) {
6171 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
6172 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
6173 rpc_restart_call_prepare(task);
6177 if (data->arg.new_lock_owner != 0) {
6178 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6179 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6180 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6181 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6182 rpc_restart_call_prepare(task);
6184 case -NFS4ERR_BAD_STATEID:
6185 case -NFS4ERR_OLD_STATEID:
6186 case -NFS4ERR_STALE_STATEID:
6187 case -NFS4ERR_EXPIRED:
6188 if (data->arg.new_lock_owner != 0) {
6189 if (!nfs4_stateid_match(&data->arg.open_stateid,
6190 &lsp->ls_state->open_stateid))
6191 rpc_restart_call_prepare(task);
6192 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6194 rpc_restart_call_prepare(task);
6196 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
6199 static void nfs4_lock_release(void *calldata)
6201 struct nfs4_lockdata *data = calldata;
6203 dprintk("%s: begin!\n", __func__);
6204 nfs_free_seqid(data->arg.open_seqid);
6205 if (data->cancelled != 0) {
6206 struct rpc_task *task;
6207 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6208 data->arg.lock_seqid);
6210 rpc_put_task_async(task);
6211 dprintk("%s: cancelling lock!\n", __func__);
6213 nfs_free_seqid(data->arg.lock_seqid);
6214 nfs4_put_lock_state(data->lsp);
6215 put_nfs_open_context(data->ctx);
6216 fput(data->fl.fl_file);
6218 dprintk("%s: done!\n", __func__);
6221 static const struct rpc_call_ops nfs4_lock_ops = {
6222 .rpc_call_prepare = nfs4_lock_prepare,
6223 .rpc_call_done = nfs4_lock_done,
6224 .rpc_release = nfs4_lock_release,
6227 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6230 case -NFS4ERR_ADMIN_REVOKED:
6231 case -NFS4ERR_EXPIRED:
6232 case -NFS4ERR_BAD_STATEID:
6233 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6234 if (new_lock_owner != 0 ||
6235 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
6236 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
6238 case -NFS4ERR_STALE_STATEID:
6239 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6240 nfs4_schedule_lease_recovery(server->nfs_client);
6244 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
6246 struct nfs4_lockdata *data;
6247 struct rpc_task *task;
6248 struct rpc_message msg = {
6249 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6250 .rpc_cred = state->owner->so_cred,
6252 struct rpc_task_setup task_setup_data = {
6253 .rpc_client = NFS_CLIENT(state->inode),
6254 .rpc_message = &msg,
6255 .callback_ops = &nfs4_lock_ops,
6256 .workqueue = nfsiod_workqueue,
6257 .flags = RPC_TASK_ASYNC,
6261 dprintk("%s: begin!\n", __func__);
6262 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
6263 fl->fl_u.nfs4_fl.owner,
6264 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
6268 data->arg.block = 1;
6269 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
6270 msg.rpc_argp = &data->arg;
6271 msg.rpc_resp = &data->res;
6272 task_setup_data.callback_data = data;
6273 if (recovery_type > NFS_LOCK_NEW) {
6274 if (recovery_type == NFS_LOCK_RECLAIM)
6275 data->arg.reclaim = NFS_LOCK_RECLAIM;
6276 nfs4_set_sequence_privileged(&data->arg.seq_args);
6278 data->arg.new_lock = 1;
6279 task = rpc_run_task(&task_setup_data);
6281 return PTR_ERR(task);
6282 ret = nfs4_wait_for_completion_rpc_task(task);
6284 ret = data->rpc_status;
6286 nfs4_handle_setlk_error(data->server, data->lsp,
6287 data->arg.new_lock_owner, ret);
6289 data->cancelled = 1;
6291 dprintk("%s: done, ret = %d!\n", __func__, ret);
6292 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
6296 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6298 struct nfs_server *server = NFS_SERVER(state->inode);
6299 struct nfs4_exception exception = {
6300 .inode = state->inode,
6305 /* Cache the lock if possible... */
6306 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6308 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
6309 if (err != -NFS4ERR_DELAY)
6311 nfs4_handle_exception(server, err, &exception);
6312 } while (exception.retry);
6316 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6318 struct nfs_server *server = NFS_SERVER(state->inode);
6319 struct nfs4_exception exception = {
6320 .inode = state->inode,
6324 err = nfs4_set_lock_state(state, request);
6327 if (!recover_lost_locks) {
6328 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6332 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6334 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
6338 case -NFS4ERR_GRACE:
6339 case -NFS4ERR_DELAY:
6340 nfs4_handle_exception(server, err, &exception);
6343 } while (exception.retry);
6348 #if defined(CONFIG_NFS_V4_1)
6349 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6351 struct nfs4_lock_state *lsp;
6354 status = nfs4_set_lock_state(state, request);
6357 lsp = request->fl_u.nfs4_fl.owner;
6358 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6359 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6361 status = nfs4_lock_expired(state, request);
6366 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6368 struct nfs_inode *nfsi = NFS_I(state->inode);
6369 struct nfs4_state_owner *sp = state->owner;
6370 unsigned char fl_flags = request->fl_flags;
6373 request->fl_flags |= FL_ACCESS;
6374 status = locks_lock_inode_wait(state->inode, request);
6377 mutex_lock(&sp->so_delegreturn_mutex);
6378 down_read(&nfsi->rwsem);
6379 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
6380 /* Yes: cache locks! */
6381 /* ...but avoid races with delegation recall... */
6382 request->fl_flags = fl_flags & ~FL_SLEEP;
6383 status = locks_lock_inode_wait(state->inode, request);
6384 up_read(&nfsi->rwsem);
6385 mutex_unlock(&sp->so_delegreturn_mutex);
6388 up_read(&nfsi->rwsem);
6389 mutex_unlock(&sp->so_delegreturn_mutex);
6390 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
6392 request->fl_flags = fl_flags;
6396 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6398 struct nfs4_exception exception = {
6400 .inode = state->inode,
6405 err = _nfs4_proc_setlk(state, cmd, request);
6406 if (err == -NFS4ERR_DENIED)
6408 err = nfs4_handle_exception(NFS_SERVER(state->inode),
6410 } while (exception.retry);
6414 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6415 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6418 nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6419 struct file_lock *request)
6421 int status = -ERESTARTSYS;
6422 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6424 while(!signalled()) {
6425 status = nfs4_proc_setlk(state, cmd, request);
6426 if ((status != -EAGAIN) || IS_SETLK(cmd))
6428 freezable_schedule_timeout_interruptible(timeout);
6430 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6431 status = -ERESTARTSYS;
6436 #ifdef CONFIG_NFS_V4_1
6437 struct nfs4_lock_waiter {
6438 struct task_struct *task;
6439 struct inode *inode;
6440 struct nfs_lowner *owner;
6445 nfs4_wake_lock_waiter(wait_queue_t *wait, unsigned int mode, int flags, void *key)
6448 struct cb_notify_lock_args *cbnl = key;
6449 struct nfs4_lock_waiter *waiter = wait->private;
6450 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6451 *wowner = waiter->owner;
6453 /* Only wake if the callback was for the same owner */
6454 if (lowner->clientid != wowner->clientid ||
6455 lowner->id != wowner->id ||
6456 lowner->s_dev != wowner->s_dev)
6459 /* Make sure it's for the right inode */
6460 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6463 waiter->notified = true;
6465 /* override "private" so we can use default_wake_function */
6466 wait->private = waiter->task;
6467 ret = autoremove_wake_function(wait, mode, flags, key);
6468 wait->private = waiter;
6473 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6475 int status = -ERESTARTSYS;
6476 unsigned long flags;
6477 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6478 struct nfs_server *server = NFS_SERVER(state->inode);
6479 struct nfs_client *clp = server->nfs_client;
6480 wait_queue_head_t *q = &clp->cl_lock_waitq;
6481 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6482 .id = lsp->ls_seqid.owner_id,
6483 .s_dev = server->s_dev };
6484 struct nfs4_lock_waiter waiter = { .task = current,
6485 .inode = state->inode,
6487 .notified = false };
6490 /* Don't bother with waitqueue if we don't expect a callback */
6491 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6492 return nfs4_retry_setlk_simple(state, cmd, request);
6495 wait.private = &waiter;
6496 wait.func = nfs4_wake_lock_waiter;
6497 add_wait_queue(q, &wait);
6499 while(!signalled()) {
6500 status = nfs4_proc_setlk(state, cmd, request);
6501 if ((status != -EAGAIN) || IS_SETLK(cmd))
6504 status = -ERESTARTSYS;
6505 spin_lock_irqsave(&q->lock, flags);
6506 if (waiter.notified) {
6507 spin_unlock_irqrestore(&q->lock, flags);
6510 set_current_state(TASK_INTERRUPTIBLE);
6511 spin_unlock_irqrestore(&q->lock, flags);
6513 freezable_schedule_timeout_interruptible(NFS4_LOCK_MAXTIMEOUT);
6516 finish_wait(q, &wait);
6519 #else /* !CONFIG_NFS_V4_1 */
6521 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6523 return nfs4_retry_setlk_simple(state, cmd, request);
6528 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6530 struct nfs_open_context *ctx;
6531 struct nfs4_state *state;
6534 /* verify open state */
6535 ctx = nfs_file_open_context(filp);
6538 if (request->fl_start < 0 || request->fl_end < 0)
6541 if (IS_GETLK(cmd)) {
6543 return nfs4_proc_getlk(state, F_GETLK, request);
6547 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6550 if (request->fl_type == F_UNLCK) {
6552 return nfs4_proc_unlck(state, cmd, request);
6559 if ((request->fl_flags & FL_POSIX) &&
6560 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6564 * Don't rely on the VFS having checked the file open mode,
6565 * since it won't do this for flock() locks.
6567 switch (request->fl_type) {
6569 if (!(filp->f_mode & FMODE_READ))
6573 if (!(filp->f_mode & FMODE_WRITE))
6577 status = nfs4_set_lock_state(state, request);
6581 return nfs4_retry_setlk(state, cmd, request);
6584 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
6586 struct nfs_server *server = NFS_SERVER(state->inode);
6589 err = nfs4_set_lock_state(state, fl);
6592 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
6593 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
6596 struct nfs_release_lockowner_data {
6597 struct nfs4_lock_state *lsp;
6598 struct nfs_server *server;
6599 struct nfs_release_lockowner_args args;
6600 struct nfs_release_lockowner_res res;
6601 unsigned long timestamp;
6604 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6606 struct nfs_release_lockowner_data *data = calldata;
6607 struct nfs_server *server = data->server;
6608 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
6609 &data->args.seq_args, &data->res.seq_res, task);
6610 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6611 data->timestamp = jiffies;
6614 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6616 struct nfs_release_lockowner_data *data = calldata;
6617 struct nfs_server *server = data->server;
6619 nfs40_sequence_done(task, &data->res.seq_res);
6621 switch (task->tk_status) {
6623 renew_lease(server, data->timestamp);
6625 case -NFS4ERR_STALE_CLIENTID:
6626 case -NFS4ERR_EXPIRED:
6627 nfs4_schedule_lease_recovery(server->nfs_client);
6629 case -NFS4ERR_LEASE_MOVED:
6630 case -NFS4ERR_DELAY:
6631 if (nfs4_async_handle_error(task, server,
6632 NULL, NULL) == -EAGAIN)
6633 rpc_restart_call_prepare(task);
6637 static void nfs4_release_lockowner_release(void *calldata)
6639 struct nfs_release_lockowner_data *data = calldata;
6640 nfs4_free_lock_state(data->server, data->lsp);
6644 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
6645 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6646 .rpc_call_done = nfs4_release_lockowner_done,
6647 .rpc_release = nfs4_release_lockowner_release,
6651 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
6653 struct nfs_release_lockowner_data *data;
6654 struct rpc_message msg = {
6655 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6658 if (server->nfs_client->cl_mvops->minor_version != 0)
6661 data = kmalloc(sizeof(*data), GFP_NOFS);
6665 data->server = server;
6666 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6667 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6668 data->args.lock_owner.s_dev = server->s_dev;
6670 msg.rpc_argp = &data->args;
6671 msg.rpc_resp = &data->res;
6672 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
6673 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
6676 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6678 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
6679 struct dentry *unused, struct inode *inode,
6680 const char *key, const void *buf,
6681 size_t buflen, int flags)
6683 return nfs4_proc_set_acl(inode, buf, buflen);
6686 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
6687 struct dentry *unused, struct inode *inode,
6688 const char *key, void *buf, size_t buflen)
6690 return nfs4_proc_get_acl(inode, buf, buflen);
6693 static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
6695 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
6698 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
6700 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
6701 struct dentry *unused, struct inode *inode,
6702 const char *key, const void *buf,
6703 size_t buflen, int flags)
6705 if (security_ismaclabel(key))
6706 return nfs4_set_security_label(inode, buf, buflen);
6711 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
6712 struct dentry *unused, struct inode *inode,
6713 const char *key, void *buf, size_t buflen)
6715 if (security_ismaclabel(key))
6716 return nfs4_get_security_label(inode, buf, buflen);
6721 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6725 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6726 len = security_inode_listsecurity(inode, list, list_len);
6727 if (list_len && len > list_len)
6733 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6734 .prefix = XATTR_SECURITY_PREFIX,
6735 .get = nfs4_xattr_get_nfs4_label,
6736 .set = nfs4_xattr_set_nfs4_label,
6742 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6750 * nfs_fhget will use either the mounted_on_fileid or the fileid
6752 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6754 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6755 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6756 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
6757 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
6760 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
6761 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
6762 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6766 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6767 const struct qstr *name,
6768 struct nfs4_fs_locations *fs_locations,
6771 struct nfs_server *server = NFS_SERVER(dir);
6773 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6775 struct nfs4_fs_locations_arg args = {
6776 .dir_fh = NFS_FH(dir),
6781 struct nfs4_fs_locations_res res = {
6782 .fs_locations = fs_locations,
6784 struct rpc_message msg = {
6785 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6791 dprintk("%s: start\n", __func__);
6793 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6794 * is not supported */
6795 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6796 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6798 bitmask[0] |= FATTR4_WORD0_FILEID;
6800 nfs_fattr_init(&fs_locations->fattr);
6801 fs_locations->server = server;
6802 fs_locations->nlocations = 0;
6803 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
6804 dprintk("%s: returned status = %d\n", __func__, status);
6808 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6809 const struct qstr *name,
6810 struct nfs4_fs_locations *fs_locations,
6813 struct nfs4_exception exception = { };
6816 err = _nfs4_proc_fs_locations(client, dir, name,
6817 fs_locations, page);
6818 trace_nfs4_get_fs_locations(dir, name, err);
6819 err = nfs4_handle_exception(NFS_SERVER(dir), err,
6821 } while (exception.retry);
6826 * This operation also signals the server that this client is
6827 * performing migration recovery. The server can stop returning
6828 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6829 * appended to this compound to identify the client ID which is
6830 * performing recovery.
6832 static int _nfs40_proc_get_locations(struct inode *inode,
6833 struct nfs4_fs_locations *locations,
6834 struct page *page, struct rpc_cred *cred)
6836 struct nfs_server *server = NFS_SERVER(inode);
6837 struct rpc_clnt *clnt = server->client;
6839 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6841 struct nfs4_fs_locations_arg args = {
6842 .clientid = server->nfs_client->cl_clientid,
6843 .fh = NFS_FH(inode),
6846 .migration = 1, /* skip LOOKUP */
6847 .renew = 1, /* append RENEW */
6849 struct nfs4_fs_locations_res res = {
6850 .fs_locations = locations,
6854 struct rpc_message msg = {
6855 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6860 unsigned long now = jiffies;
6863 nfs_fattr_init(&locations->fattr);
6864 locations->server = server;
6865 locations->nlocations = 0;
6867 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6868 nfs4_set_sequence_privileged(&args.seq_args);
6869 status = nfs4_call_sync_sequence(clnt, server, &msg,
6870 &args.seq_args, &res.seq_res);
6874 renew_lease(server, now);
6878 #ifdef CONFIG_NFS_V4_1
6881 * This operation also signals the server that this client is
6882 * performing migration recovery. The server can stop asserting
6883 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6884 * performing this operation is identified in the SEQUENCE
6885 * operation in this compound.
6887 * When the client supports GETATTR(fs_locations_info), it can
6888 * be plumbed in here.
6890 static int _nfs41_proc_get_locations(struct inode *inode,
6891 struct nfs4_fs_locations *locations,
6892 struct page *page, struct rpc_cred *cred)
6894 struct nfs_server *server = NFS_SERVER(inode);
6895 struct rpc_clnt *clnt = server->client;
6897 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6899 struct nfs4_fs_locations_arg args = {
6900 .fh = NFS_FH(inode),
6903 .migration = 1, /* skip LOOKUP */
6905 struct nfs4_fs_locations_res res = {
6906 .fs_locations = locations,
6909 struct rpc_message msg = {
6910 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6917 nfs_fattr_init(&locations->fattr);
6918 locations->server = server;
6919 locations->nlocations = 0;
6921 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6922 nfs4_set_sequence_privileged(&args.seq_args);
6923 status = nfs4_call_sync_sequence(clnt, server, &msg,
6924 &args.seq_args, &res.seq_res);
6925 if (status == NFS4_OK &&
6926 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6927 status = -NFS4ERR_LEASE_MOVED;
6931 #endif /* CONFIG_NFS_V4_1 */
6934 * nfs4_proc_get_locations - discover locations for a migrated FSID
6935 * @inode: inode on FSID that is migrating
6936 * @locations: result of query
6938 * @cred: credential to use for this operation
6940 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6941 * operation failed, or a negative errno if a local error occurred.
6943 * On success, "locations" is filled in, but if the server has
6944 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6947 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6948 * from this client that require migration recovery.
6950 int nfs4_proc_get_locations(struct inode *inode,
6951 struct nfs4_fs_locations *locations,
6952 struct page *page, struct rpc_cred *cred)
6954 struct nfs_server *server = NFS_SERVER(inode);
6955 struct nfs_client *clp = server->nfs_client;
6956 const struct nfs4_mig_recovery_ops *ops =
6957 clp->cl_mvops->mig_recovery_ops;
6958 struct nfs4_exception exception = { };
6961 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6962 (unsigned long long)server->fsid.major,
6963 (unsigned long long)server->fsid.minor,
6965 nfs_display_fhandle(NFS_FH(inode), __func__);
6968 status = ops->get_locations(inode, locations, page, cred);
6969 if (status != -NFS4ERR_DELAY)
6971 nfs4_handle_exception(server, status, &exception);
6972 } while (exception.retry);
6977 * This operation also signals the server that this client is
6978 * performing "lease moved" recovery. The server can stop
6979 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6980 * is appended to this compound to identify the client ID which is
6981 * performing recovery.
6983 static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6985 struct nfs_server *server = NFS_SERVER(inode);
6986 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6987 struct rpc_clnt *clnt = server->client;
6988 struct nfs4_fsid_present_arg args = {
6989 .fh = NFS_FH(inode),
6990 .clientid = clp->cl_clientid,
6991 .renew = 1, /* append RENEW */
6993 struct nfs4_fsid_present_res res = {
6996 struct rpc_message msg = {
6997 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7002 unsigned long now = jiffies;
7005 res.fh = nfs_alloc_fhandle();
7009 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7010 nfs4_set_sequence_privileged(&args.seq_args);
7011 status = nfs4_call_sync_sequence(clnt, server, &msg,
7012 &args.seq_args, &res.seq_res);
7013 nfs_free_fhandle(res.fh);
7017 do_renew_lease(clp, now);
7021 #ifdef CONFIG_NFS_V4_1
7024 * This operation also signals the server that this client is
7025 * performing "lease moved" recovery. The server can stop asserting
7026 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7027 * this operation is identified in the SEQUENCE operation in this
7030 static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7032 struct nfs_server *server = NFS_SERVER(inode);
7033 struct rpc_clnt *clnt = server->client;
7034 struct nfs4_fsid_present_arg args = {
7035 .fh = NFS_FH(inode),
7037 struct nfs4_fsid_present_res res = {
7039 struct rpc_message msg = {
7040 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7047 res.fh = nfs_alloc_fhandle();
7051 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7052 nfs4_set_sequence_privileged(&args.seq_args);
7053 status = nfs4_call_sync_sequence(clnt, server, &msg,
7054 &args.seq_args, &res.seq_res);
7055 nfs_free_fhandle(res.fh);
7056 if (status == NFS4_OK &&
7057 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7058 status = -NFS4ERR_LEASE_MOVED;
7062 #endif /* CONFIG_NFS_V4_1 */
7065 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7066 * @inode: inode on FSID to check
7067 * @cred: credential to use for this operation
7069 * Server indicates whether the FSID is present, moved, or not
7070 * recognized. This operation is necessary to clear a LEASE_MOVED
7071 * condition for this client ID.
7073 * Returns NFS4_OK if the FSID is present on this server,
7074 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7075 * NFS4ERR code if some error occurred on the server, or a
7076 * negative errno if a local failure occurred.
7078 int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7080 struct nfs_server *server = NFS_SERVER(inode);
7081 struct nfs_client *clp = server->nfs_client;
7082 const struct nfs4_mig_recovery_ops *ops =
7083 clp->cl_mvops->mig_recovery_ops;
7084 struct nfs4_exception exception = { };
7087 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7088 (unsigned long long)server->fsid.major,
7089 (unsigned long long)server->fsid.minor,
7091 nfs_display_fhandle(NFS_FH(inode), __func__);
7094 status = ops->fsid_present(inode, cred);
7095 if (status != -NFS4ERR_DELAY)
7097 nfs4_handle_exception(server, status, &exception);
7098 } while (exception.retry);
7103 * If 'use_integrity' is true and the state managment nfs_client
7104 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7105 * and the machine credential as per RFC3530bis and RFC5661 Security
7106 * Considerations sections. Otherwise, just use the user cred with the
7107 * filesystem's rpc_client.
7109 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
7112 struct nfs4_secinfo_arg args = {
7113 .dir_fh = NFS_FH(dir),
7116 struct nfs4_secinfo_res res = {
7119 struct rpc_message msg = {
7120 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7124 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
7125 struct rpc_cred *cred = NULL;
7127 if (use_integrity) {
7128 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
7129 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7130 msg.rpc_cred = cred;
7133 dprintk("NFS call secinfo %s\n", name->name);
7135 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7136 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7138 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7140 dprintk("NFS reply secinfo: %d\n", status);
7148 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7149 struct nfs4_secinfo_flavors *flavors)
7151 struct nfs4_exception exception = { };
7154 err = -NFS4ERR_WRONGSEC;
7156 /* try to use integrity protection with machine cred */
7157 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7158 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7161 * if unable to use integrity protection, or SECINFO with
7162 * integrity protection returns NFS4ERR_WRONGSEC (which is
7163 * disallowed by spec, but exists in deployed servers) use
7164 * the current filesystem's rpc_client and the user cred.
7166 if (err == -NFS4ERR_WRONGSEC)
7167 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7169 trace_nfs4_secinfo(dir, name, err);
7170 err = nfs4_handle_exception(NFS_SERVER(dir), err,
7172 } while (exception.retry);
7176 #ifdef CONFIG_NFS_V4_1
7178 * Check the exchange flags returned by the server for invalid flags, having
7179 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7182 static int nfs4_check_cl_exchange_flags(u32 flags)
7184 if (flags & ~EXCHGID4_FLAG_MASK_R)
7186 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7187 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7189 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7193 return -NFS4ERR_INVAL;
7197 nfs41_same_server_scope(struct nfs41_server_scope *a,
7198 struct nfs41_server_scope *b)
7200 if (a->server_scope_sz == b->server_scope_sz &&
7201 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
7208 nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7212 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7213 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7217 * nfs4_proc_bind_one_conn_to_session()
7219 * The 4.1 client currently uses the same TCP connection for the
7220 * fore and backchannel.
7223 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7224 struct rpc_xprt *xprt,
7225 struct nfs_client *clp,
7226 struct rpc_cred *cred)
7229 struct nfs41_bind_conn_to_session_args args = {
7231 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7233 struct nfs41_bind_conn_to_session_res res;
7234 struct rpc_message msg = {
7236 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
7241 struct rpc_task_setup task_setup_data = {
7244 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
7245 .rpc_message = &msg,
7246 .flags = RPC_TASK_TIMEOUT,
7248 struct rpc_task *task;
7250 dprintk("--> %s\n", __func__);
7252 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7253 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7254 args.dir = NFS4_CDFC4_FORE;
7256 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7257 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7258 args.dir = NFS4_CDFC4_FORE;
7260 task = rpc_run_task(&task_setup_data);
7261 if (!IS_ERR(task)) {
7262 status = task->tk_status;
7265 status = PTR_ERR(task);
7266 trace_nfs4_bind_conn_to_session(clp, status);
7268 if (memcmp(res.sessionid.data,
7269 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7270 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7274 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
7275 dprintk("NFS: %s: Unexpected direction from server\n",
7280 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
7281 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7288 dprintk("<-- %s status= %d\n", __func__, status);
7292 struct rpc_bind_conn_calldata {
7293 struct nfs_client *clp;
7294 struct rpc_cred *cred;
7298 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7299 struct rpc_xprt *xprt,
7302 struct rpc_bind_conn_calldata *p = calldata;
7304 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7307 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7309 struct rpc_bind_conn_calldata data = {
7313 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7314 nfs4_proc_bind_conn_to_session_callback, &data);
7318 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7319 * and operations we'd like to see to enable certain features in the allow map
7321 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7322 .how = SP4_MACH_CRED,
7323 .enforce.u.words = {
7324 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7325 1 << (OP_EXCHANGE_ID - 32) |
7326 1 << (OP_CREATE_SESSION - 32) |
7327 1 << (OP_DESTROY_SESSION - 32) |
7328 1 << (OP_DESTROY_CLIENTID - 32)
7331 [0] = 1 << (OP_CLOSE) |
7332 1 << (OP_OPEN_DOWNGRADE) |
7334 1 << (OP_DELEGRETURN) |
7336 [1] = 1 << (OP_SECINFO - 32) |
7337 1 << (OP_SECINFO_NO_NAME - 32) |
7338 1 << (OP_LAYOUTRETURN - 32) |
7339 1 << (OP_TEST_STATEID - 32) |
7340 1 << (OP_FREE_STATEID - 32) |
7341 1 << (OP_WRITE - 32)
7346 * Select the state protection mode for client `clp' given the server results
7347 * from exchange_id in `sp'.
7349 * Returns 0 on success, negative errno otherwise.
7351 static int nfs4_sp4_select_mode(struct nfs_client *clp,
7352 struct nfs41_state_protection *sp)
7354 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7355 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7356 1 << (OP_EXCHANGE_ID - 32) |
7357 1 << (OP_CREATE_SESSION - 32) |
7358 1 << (OP_DESTROY_SESSION - 32) |
7359 1 << (OP_DESTROY_CLIENTID - 32)
7363 if (sp->how == SP4_MACH_CRED) {
7364 /* Print state protect result */
7365 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7366 for (i = 0; i <= LAST_NFS4_OP; i++) {
7367 if (test_bit(i, sp->enforce.u.longs))
7368 dfprintk(MOUNT, " enforce op %d\n", i);
7369 if (test_bit(i, sp->allow.u.longs))
7370 dfprintk(MOUNT, " allow op %d\n", i);
7373 /* make sure nothing is on enforce list that isn't supported */
7374 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7375 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7376 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7382 * Minimal mode - state operations are allowed to use machine
7383 * credential. Note this already happens by default, so the
7384 * client doesn't have to do anything more than the negotiation.
7386 * NOTE: we don't care if EXCHANGE_ID is in the list -
7387 * we're already using the machine cred for exchange_id
7388 * and will never use a different cred.
7390 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7391 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7392 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7393 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7394 dfprintk(MOUNT, "sp4_mach_cred:\n");
7395 dfprintk(MOUNT, " minimal mode enabled\n");
7396 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
7398 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7402 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
7403 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7404 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
7405 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7406 dfprintk(MOUNT, " cleanup mode enabled\n");
7407 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
7410 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7411 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7412 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
7413 &clp->cl_sp4_flags);
7416 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7417 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7418 dfprintk(MOUNT, " secinfo mode enabled\n");
7419 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
7422 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7423 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7424 dfprintk(MOUNT, " stateid mode enabled\n");
7425 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
7428 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7429 dfprintk(MOUNT, " write mode enabled\n");
7430 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
7433 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7434 dfprintk(MOUNT, " commit mode enabled\n");
7435 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
7442 struct nfs41_exchange_id_data {
7443 struct nfs41_exchange_id_res res;
7444 struct nfs41_exchange_id_args args;
7445 struct rpc_xprt *xprt;
7449 static void nfs4_exchange_id_done(struct rpc_task *task, void *data)
7451 struct nfs41_exchange_id_data *cdata =
7452 (struct nfs41_exchange_id_data *)data;
7453 struct nfs_client *clp = cdata->args.client;
7454 int status = task->tk_status;
7456 trace_nfs4_exchange_id(clp, status);
7459 status = nfs4_check_cl_exchange_flags(cdata->res.flags);
7461 if (cdata->xprt && status == 0) {
7462 status = nfs4_detect_session_trunking(clp, &cdata->res,
7468 status = nfs4_sp4_select_mode(clp, &cdata->res.state_protect);
7471 clp->cl_clientid = cdata->res.clientid;
7472 clp->cl_exchange_flags = cdata->res.flags;
7473 /* Client ID is not confirmed */
7474 if (!(cdata->res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
7475 clear_bit(NFS4_SESSION_ESTABLISHED,
7476 &clp->cl_session->session_state);
7477 clp->cl_seqid = cdata->res.seqid;
7480 kfree(clp->cl_serverowner);
7481 clp->cl_serverowner = cdata->res.server_owner;
7482 cdata->res.server_owner = NULL;
7484 /* use the most recent implementation id */
7485 kfree(clp->cl_implid);
7486 clp->cl_implid = cdata->res.impl_id;
7487 cdata->res.impl_id = NULL;
7489 if (clp->cl_serverscope != NULL &&
7490 !nfs41_same_server_scope(clp->cl_serverscope,
7491 cdata->res.server_scope)) {
7492 dprintk("%s: server_scope mismatch detected\n",
7494 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
7495 kfree(clp->cl_serverscope);
7496 clp->cl_serverscope = NULL;
7499 if (clp->cl_serverscope == NULL) {
7500 clp->cl_serverscope = cdata->res.server_scope;
7501 cdata->res.server_scope = NULL;
7503 /* Save the EXCHANGE_ID verifier session trunk tests */
7504 memcpy(clp->cl_confirm.data, cdata->args.verifier->data,
7505 sizeof(clp->cl_confirm.data));
7508 cdata->rpc_status = status;
7512 static void nfs4_exchange_id_release(void *data)
7514 struct nfs41_exchange_id_data *cdata =
7515 (struct nfs41_exchange_id_data *)data;
7517 nfs_put_client(cdata->args.client);
7519 xprt_put(cdata->xprt);
7520 rpc_clnt_xprt_switch_put(cdata->args.client->cl_rpcclient);
7522 kfree(cdata->res.impl_id);
7523 kfree(cdata->res.server_scope);
7524 kfree(cdata->res.server_owner);
7528 static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7529 .rpc_call_done = nfs4_exchange_id_done,
7530 .rpc_release = nfs4_exchange_id_release,
7534 * _nfs4_proc_exchange_id()
7536 * Wrapper for EXCHANGE_ID operation.
7538 static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
7539 u32 sp4_how, struct rpc_xprt *xprt)
7541 nfs4_verifier verifier;
7542 struct rpc_message msg = {
7543 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
7546 struct rpc_task_setup task_setup_data = {
7547 .rpc_client = clp->cl_rpcclient,
7548 .callback_ops = &nfs4_exchange_id_call_ops,
7549 .rpc_message = &msg,
7550 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7552 struct nfs41_exchange_id_data *calldata;
7553 struct rpc_task *task;
7556 if (!atomic_inc_not_zero(&clp->cl_count))
7560 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7565 nfs4_init_boot_verifier(clp, &verifier);
7567 status = nfs4_init_uniform_client_string(clp);
7571 dprintk("NFS call exchange_id auth=%s, '%s'\n",
7572 clp->cl_rpcclient->cl_auth->au_ops->au_name,
7575 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7578 if (unlikely(calldata->res.server_owner == NULL))
7581 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
7583 if (unlikely(calldata->res.server_scope == NULL))
7584 goto out_server_owner;
7586 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7587 if (unlikely(calldata->res.impl_id == NULL))
7588 goto out_server_scope;
7592 calldata->args.state_protect.how = SP4_NONE;
7596 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
7606 calldata->xprt = xprt;
7607 task_setup_data.rpc_xprt = xprt;
7608 task_setup_data.flags =
7609 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC;
7610 calldata->args.verifier = &clp->cl_confirm;
7612 calldata->args.verifier = &verifier;
7614 calldata->args.client = clp;
7615 #ifdef CONFIG_NFS_V4_1_MIGRATION
7616 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7617 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7618 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7620 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7621 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7623 msg.rpc_argp = &calldata->args;
7624 msg.rpc_resp = &calldata->res;
7625 task_setup_data.callback_data = calldata;
7627 task = rpc_run_task(&task_setup_data);
7629 status = PTR_ERR(task);
7634 status = rpc_wait_for_completion_task(task);
7636 status = calldata->rpc_status;
7637 } else /* session trunking test */
7638 status = calldata->rpc_status;
7642 if (clp->cl_implid != NULL)
7643 dprintk("NFS reply exchange_id: Server Implementation ID: "
7644 "domain: %s, name: %s, date: %llu,%u\n",
7645 clp->cl_implid->domain, clp->cl_implid->name,
7646 clp->cl_implid->date.seconds,
7647 clp->cl_implid->date.nseconds);
7648 dprintk("NFS reply exchange_id: %d\n", status);
7652 kfree(calldata->res.impl_id);
7654 kfree(calldata->res.server_scope);
7656 kfree(calldata->res.server_owner);
7663 * nfs4_proc_exchange_id()
7665 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7667 * Since the clientid has expired, all compounds using sessions
7668 * associated with the stale clientid will be returning
7669 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7670 * be in some phase of session reset.
7672 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7674 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7676 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7679 /* try SP4_MACH_CRED if krb5i/p */
7680 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7681 authflavor == RPC_AUTH_GSS_KRB5P) {
7682 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED, NULL);
7688 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE, NULL);
7692 * nfs4_test_session_trunk
7694 * This is an add_xprt_test() test function called from
7695 * rpc_clnt_setup_test_and_add_xprt.
7697 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7698 * and is dereferrenced in nfs4_exchange_id_release
7700 * Upon success, add the new transport to the rpc_clnt
7702 * @clnt: struct rpc_clnt to get new transport
7703 * @xprt: the rpc_xprt to test
7704 * @data: call data for _nfs4_proc_exchange_id.
7706 int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7709 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
7712 dprintk("--> %s try %s\n", __func__,
7713 xprt->address_strings[RPC_DISPLAY_ADDR]);
7715 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7717 /* Test connection for session trunking. Async exchange_id call */
7718 return _nfs4_proc_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7720 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7722 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7723 struct rpc_cred *cred)
7725 struct rpc_message msg = {
7726 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7732 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7733 trace_nfs4_destroy_clientid(clp, status);
7735 dprintk("NFS: Got error %d from the server %s on "
7736 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7740 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7741 struct rpc_cred *cred)
7746 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7747 ret = _nfs4_proc_destroy_clientid(clp, cred);
7749 case -NFS4ERR_DELAY:
7750 case -NFS4ERR_CLIENTID_BUSY:
7760 int nfs4_destroy_clientid(struct nfs_client *clp)
7762 struct rpc_cred *cred;
7765 if (clp->cl_mvops->minor_version < 1)
7767 if (clp->cl_exchange_flags == 0)
7769 if (clp->cl_preserve_clid)
7771 cred = nfs4_get_clid_cred(clp);
7772 ret = nfs4_proc_destroy_clientid(clp, cred);
7777 case -NFS4ERR_STALE_CLIENTID:
7778 clp->cl_exchange_flags = 0;
7784 struct nfs4_get_lease_time_data {
7785 struct nfs4_get_lease_time_args *args;
7786 struct nfs4_get_lease_time_res *res;
7787 struct nfs_client *clp;
7790 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7793 struct nfs4_get_lease_time_data *data =
7794 (struct nfs4_get_lease_time_data *)calldata;
7796 dprintk("--> %s\n", __func__);
7797 /* just setup sequence, do not trigger session recovery
7798 since we're invoked within one */
7799 nfs41_setup_sequence(data->clp->cl_session,
7800 &data->args->la_seq_args,
7801 &data->res->lr_seq_res,
7803 dprintk("<-- %s\n", __func__);
7807 * Called from nfs4_state_manager thread for session setup, so don't recover
7808 * from sequence operation or clientid errors.
7810 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7812 struct nfs4_get_lease_time_data *data =
7813 (struct nfs4_get_lease_time_data *)calldata;
7815 dprintk("--> %s\n", __func__);
7816 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7818 switch (task->tk_status) {
7819 case -NFS4ERR_DELAY:
7820 case -NFS4ERR_GRACE:
7821 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7822 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7823 task->tk_status = 0;
7825 case -NFS4ERR_RETRY_UNCACHED_REP:
7826 rpc_restart_call_prepare(task);
7829 dprintk("<-- %s\n", __func__);
7832 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
7833 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7834 .rpc_call_done = nfs4_get_lease_time_done,
7837 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7839 struct rpc_task *task;
7840 struct nfs4_get_lease_time_args args;
7841 struct nfs4_get_lease_time_res res = {
7842 .lr_fsinfo = fsinfo,
7844 struct nfs4_get_lease_time_data data = {
7849 struct rpc_message msg = {
7850 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7854 struct rpc_task_setup task_setup = {
7855 .rpc_client = clp->cl_rpcclient,
7856 .rpc_message = &msg,
7857 .callback_ops = &nfs4_get_lease_time_ops,
7858 .callback_data = &data,
7859 .flags = RPC_TASK_TIMEOUT,
7863 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
7864 nfs4_set_sequence_privileged(&args.la_seq_args);
7865 dprintk("--> %s\n", __func__);
7866 task = rpc_run_task(&task_setup);
7869 status = PTR_ERR(task);
7871 status = task->tk_status;
7874 dprintk("<-- %s return %d\n", __func__, status);
7880 * Initialize the values to be used by the client in CREATE_SESSION
7881 * If nfs4_init_session set the fore channel request and response sizes,
7884 * Set the back channel max_resp_sz_cached to zero to force the client to
7885 * always set csa_cachethis to FALSE because the current implementation
7886 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7888 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7889 struct rpc_clnt *clnt)
7891 unsigned int max_rqst_sz, max_resp_sz;
7892 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
7894 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7895 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7897 /* Fore channel attributes */
7898 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7899 args->fc_attrs.max_resp_sz = max_resp_sz;
7900 args->fc_attrs.max_ops = NFS4_MAX_OPS;
7901 args->fc_attrs.max_reqs = max_session_slots;
7903 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
7904 "max_ops=%u max_reqs=%u\n",
7906 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
7907 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
7909 /* Back channel attributes */
7910 args->bc_attrs.max_rqst_sz = max_bc_payload;
7911 args->bc_attrs.max_resp_sz = max_bc_payload;
7912 args->bc_attrs.max_resp_sz_cached = 0;
7913 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
7914 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
7916 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7917 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7919 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7920 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7921 args->bc_attrs.max_reqs);
7924 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7925 struct nfs41_create_session_res *res)
7927 struct nfs4_channel_attrs *sent = &args->fc_attrs;
7928 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
7930 if (rcvd->max_resp_sz > sent->max_resp_sz)
7933 * Our requested max_ops is the minimum we need; we're not
7934 * prepared to break up compounds into smaller pieces than that.
7935 * So, no point even trying to continue if the server won't
7938 if (rcvd->max_ops < sent->max_ops)
7940 if (rcvd->max_reqs == 0)
7942 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7943 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
7947 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7948 struct nfs41_create_session_res *res)
7950 struct nfs4_channel_attrs *sent = &args->bc_attrs;
7951 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
7953 if (!(res->flags & SESSION4_BACK_CHAN))
7955 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7957 if (rcvd->max_resp_sz < sent->max_resp_sz)
7959 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7961 if (rcvd->max_ops > sent->max_ops)
7963 if (rcvd->max_reqs > sent->max_reqs)
7969 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
7970 struct nfs41_create_session_res *res)
7974 ret = nfs4_verify_fore_channel_attrs(args, res);
7977 return nfs4_verify_back_channel_attrs(args, res);
7980 static void nfs4_update_session(struct nfs4_session *session,
7981 struct nfs41_create_session_res *res)
7983 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
7984 /* Mark client id and session as being confirmed */
7985 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7986 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
7987 session->flags = res->flags;
7988 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
7989 if (res->flags & SESSION4_BACK_CHAN)
7990 memcpy(&session->bc_attrs, &res->bc_attrs,
7991 sizeof(session->bc_attrs));
7994 static int _nfs4_proc_create_session(struct nfs_client *clp,
7995 struct rpc_cred *cred)
7997 struct nfs4_session *session = clp->cl_session;
7998 struct nfs41_create_session_args args = {
8000 .clientid = clp->cl_clientid,
8001 .seqid = clp->cl_seqid,
8002 .cb_program = NFS4_CALLBACK,
8004 struct nfs41_create_session_res res;
8006 struct rpc_message msg = {
8007 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8014 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
8015 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
8017 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
8018 trace_nfs4_create_session(clp, status);
8021 case -NFS4ERR_STALE_CLIENTID:
8022 case -NFS4ERR_DELAY:
8031 /* Verify the session's negotiated channel_attrs values */
8032 status = nfs4_verify_channel_attrs(&args, &res);
8033 /* Increment the clientid slot sequence id */
8036 nfs4_update_session(session, &res);
8043 * Issues a CREATE_SESSION operation to the server.
8044 * It is the responsibility of the caller to verify the session is
8045 * expired before calling this routine.
8047 int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
8051 struct nfs4_session *session = clp->cl_session;
8053 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8055 status = _nfs4_proc_create_session(clp, cred);
8059 /* Init or reset the session slot tables */
8060 status = nfs4_setup_session_slot_tables(session);
8061 dprintk("slot table setup returned %d\n", status);
8065 ptr = (unsigned *)&session->sess_id.data[0];
8066 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8067 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
8069 dprintk("<-- %s\n", __func__);
8074 * Issue the over-the-wire RPC DESTROY_SESSION.
8075 * The caller must serialize access to this routine.
8077 int nfs4_proc_destroy_session(struct nfs4_session *session,
8078 struct rpc_cred *cred)
8080 struct rpc_message msg = {
8081 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8082 .rpc_argp = session,
8087 dprintk("--> nfs4_proc_destroy_session\n");
8089 /* session is still being setup */
8090 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8093 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
8094 trace_nfs4_destroy_session(session->clp, status);
8097 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
8098 "Session has been destroyed regardless...\n", status);
8100 dprintk("<-- nfs4_proc_destroy_session\n");
8105 * Renew the cl_session lease.
8107 struct nfs4_sequence_data {
8108 struct nfs_client *clp;
8109 struct nfs4_sequence_args args;
8110 struct nfs4_sequence_res res;
8113 static void nfs41_sequence_release(void *data)
8115 struct nfs4_sequence_data *calldata = data;
8116 struct nfs_client *clp = calldata->clp;
8118 if (atomic_read(&clp->cl_count) > 1)
8119 nfs4_schedule_state_renewal(clp);
8120 nfs_put_client(clp);
8124 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8126 switch(task->tk_status) {
8127 case -NFS4ERR_DELAY:
8128 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8131 nfs4_schedule_lease_recovery(clp);
8136 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
8138 struct nfs4_sequence_data *calldata = data;
8139 struct nfs_client *clp = calldata->clp;
8141 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8144 trace_nfs4_sequence(clp, task->tk_status);
8145 if (task->tk_status < 0) {
8146 dprintk("%s ERROR %d\n", __func__, task->tk_status);
8147 if (atomic_read(&clp->cl_count) == 1)
8150 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8151 rpc_restart_call_prepare(task);
8155 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
8157 dprintk("<-- %s\n", __func__);
8160 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8162 struct nfs4_sequence_data *calldata = data;
8163 struct nfs_client *clp = calldata->clp;
8164 struct nfs4_sequence_args *args;
8165 struct nfs4_sequence_res *res;
8167 args = task->tk_msg.rpc_argp;
8168 res = task->tk_msg.rpc_resp;
8170 nfs41_setup_sequence(clp->cl_session, args, res, task);
8173 static const struct rpc_call_ops nfs41_sequence_ops = {
8174 .rpc_call_done = nfs41_sequence_call_done,
8175 .rpc_call_prepare = nfs41_sequence_prepare,
8176 .rpc_release = nfs41_sequence_release,
8179 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8180 struct rpc_cred *cred,
8183 struct nfs4_sequence_data *calldata;
8184 struct rpc_message msg = {
8185 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8188 struct rpc_task_setup task_setup_data = {
8189 .rpc_client = clp->cl_rpcclient,
8190 .rpc_message = &msg,
8191 .callback_ops = &nfs41_sequence_ops,
8192 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
8195 if (!atomic_inc_not_zero(&clp->cl_count))
8196 return ERR_PTR(-EIO);
8197 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8198 if (calldata == NULL) {
8199 nfs_put_client(clp);
8200 return ERR_PTR(-ENOMEM);
8202 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
8204 nfs4_set_sequence_privileged(&calldata->args);
8205 msg.rpc_argp = &calldata->args;
8206 msg.rpc_resp = &calldata->res;
8207 calldata->clp = clp;
8208 task_setup_data.callback_data = calldata;
8210 return rpc_run_task(&task_setup_data);
8213 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
8215 struct rpc_task *task;
8218 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
8220 task = _nfs41_proc_sequence(clp, cred, false);
8222 ret = PTR_ERR(task);
8224 rpc_put_task_async(task);
8225 dprintk("<-- %s status=%d\n", __func__, ret);
8229 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8231 struct rpc_task *task;
8234 task = _nfs41_proc_sequence(clp, cred, true);
8236 ret = PTR_ERR(task);
8239 ret = rpc_wait_for_completion_task(task);
8241 ret = task->tk_status;
8244 dprintk("<-- %s status=%d\n", __func__, ret);
8248 struct nfs4_reclaim_complete_data {
8249 struct nfs_client *clp;
8250 struct nfs41_reclaim_complete_args arg;
8251 struct nfs41_reclaim_complete_res res;
8254 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8256 struct nfs4_reclaim_complete_data *calldata = data;
8258 nfs41_setup_sequence(calldata->clp->cl_session,
8259 &calldata->arg.seq_args,
8260 &calldata->res.seq_res,
8264 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8266 switch(task->tk_status) {
8268 case -NFS4ERR_COMPLETE_ALREADY:
8269 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8271 case -NFS4ERR_DELAY:
8272 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8274 case -NFS4ERR_RETRY_UNCACHED_REP:
8277 nfs4_schedule_lease_recovery(clp);
8282 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8284 struct nfs4_reclaim_complete_data *calldata = data;
8285 struct nfs_client *clp = calldata->clp;
8286 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8288 dprintk("--> %s\n", __func__);
8289 if (!nfs41_sequence_done(task, res))
8292 trace_nfs4_reclaim_complete(clp, task->tk_status);
8293 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8294 rpc_restart_call_prepare(task);
8297 dprintk("<-- %s\n", __func__);
8300 static void nfs4_free_reclaim_complete_data(void *data)
8302 struct nfs4_reclaim_complete_data *calldata = data;
8307 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8308 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8309 .rpc_call_done = nfs4_reclaim_complete_done,
8310 .rpc_release = nfs4_free_reclaim_complete_data,
8314 * Issue a global reclaim complete.
8316 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8317 struct rpc_cred *cred)
8319 struct nfs4_reclaim_complete_data *calldata;
8320 struct rpc_task *task;
8321 struct rpc_message msg = {
8322 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
8325 struct rpc_task_setup task_setup_data = {
8326 .rpc_client = clp->cl_rpcclient,
8327 .rpc_message = &msg,
8328 .callback_ops = &nfs4_reclaim_complete_call_ops,
8329 .flags = RPC_TASK_ASYNC,
8331 int status = -ENOMEM;
8333 dprintk("--> %s\n", __func__);
8334 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8335 if (calldata == NULL)
8337 calldata->clp = clp;
8338 calldata->arg.one_fs = 0;
8340 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
8341 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
8342 msg.rpc_argp = &calldata->arg;
8343 msg.rpc_resp = &calldata->res;
8344 task_setup_data.callback_data = calldata;
8345 task = rpc_run_task(&task_setup_data);
8347 status = PTR_ERR(task);
8350 status = nfs4_wait_for_completion_rpc_task(task);
8352 status = task->tk_status;
8356 dprintk("<-- %s status=%d\n", __func__, status);
8361 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8363 struct nfs4_layoutget *lgp = calldata;
8364 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
8365 struct nfs4_session *session = nfs4_get_session(server);
8367 dprintk("--> %s\n", __func__);
8368 nfs41_setup_sequence(session, &lgp->args.seq_args,
8369 &lgp->res.seq_res, task);
8370 dprintk("<-- %s\n", __func__);
8373 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8375 struct nfs4_layoutget *lgp = calldata;
8377 dprintk("--> %s\n", __func__);
8378 nfs41_sequence_process(task, &lgp->res.seq_res);
8379 dprintk("<-- %s\n", __func__);
8383 nfs4_layoutget_handle_exception(struct rpc_task *task,
8384 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8386 struct inode *inode = lgp->args.inode;
8387 struct nfs_server *server = NFS_SERVER(inode);
8388 struct pnfs_layout_hdr *lo;
8389 int nfs4err = task->tk_status;
8390 int err, status = 0;
8393 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
8400 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8401 * on the file. set tk_status to -ENODATA to tell upper layer to
8404 case -NFS4ERR_LAYOUTUNAVAILABLE:
8408 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8409 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8411 case -NFS4ERR_BADLAYOUT:
8412 status = -EOVERFLOW;
8415 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
8416 * (or clients) writing to the same RAID stripe except when
8417 * the minlength argument is 0 (see RFC5661 section 18.43.3).
8419 * Treat it like we would RECALLCONFLICT -- we retry for a little
8420 * while, and then eventually give up.
8422 case -NFS4ERR_LAYOUTTRYLATER:
8423 if (lgp->args.minlength == 0) {
8424 status = -EOVERFLOW;
8429 case -NFS4ERR_RECALLCONFLICT:
8430 status = -ERECALLCONFLICT;
8432 case -NFS4ERR_DELEG_REVOKED:
8433 case -NFS4ERR_ADMIN_REVOKED:
8434 case -NFS4ERR_EXPIRED:
8435 case -NFS4ERR_BAD_STATEID:
8436 exception->timeout = 0;
8437 spin_lock(&inode->i_lock);
8438 lo = NFS_I(inode)->layout;
8439 /* If the open stateid was bad, then recover it. */
8440 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8441 nfs4_stateid_match_other(&lgp->args.stateid,
8442 &lgp->args.ctx->state->stateid)) {
8443 spin_unlock(&inode->i_lock);
8444 exception->state = lgp->args.ctx->state;
8445 exception->stateid = &lgp->args.stateid;
8450 * Mark the bad layout state as invalid, then retry
8452 pnfs_mark_layout_stateid_invalid(lo, &head);
8453 spin_unlock(&inode->i_lock);
8454 pnfs_free_lseg_list(&head);
8459 err = nfs4_handle_exception(server, nfs4err, exception);
8461 if (exception->retry)
8467 dprintk("<-- %s\n", __func__);
8471 static size_t max_response_pages(struct nfs_server *server)
8473 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8474 return nfs_page_array_len(0, max_resp_sz);
8477 static void nfs4_free_pages(struct page **pages, size_t size)
8484 for (i = 0; i < size; i++) {
8487 __free_page(pages[i]);
8492 static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8494 struct page **pages;
8497 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8499 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8503 for (i = 0; i < size; i++) {
8504 pages[i] = alloc_page(gfp_flags);
8506 dprintk("%s: failed to allocate page\n", __func__);
8507 nfs4_free_pages(pages, size);
8515 static void nfs4_layoutget_release(void *calldata)
8517 struct nfs4_layoutget *lgp = calldata;
8518 struct inode *inode = lgp->args.inode;
8519 struct nfs_server *server = NFS_SERVER(inode);
8520 size_t max_pages = max_response_pages(server);
8522 dprintk("--> %s\n", __func__);
8523 nfs4_free_pages(lgp->args.layout.pages, max_pages);
8524 pnfs_put_layout_hdr(NFS_I(inode)->layout);
8525 put_nfs_open_context(lgp->args.ctx);
8527 dprintk("<-- %s\n", __func__);
8530 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8531 .rpc_call_prepare = nfs4_layoutget_prepare,
8532 .rpc_call_done = nfs4_layoutget_done,
8533 .rpc_release = nfs4_layoutget_release,
8536 struct pnfs_layout_segment *
8537 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
8539 struct inode *inode = lgp->args.inode;
8540 struct nfs_server *server = NFS_SERVER(inode);
8541 size_t max_pages = max_response_pages(server);
8542 struct rpc_task *task;
8543 struct rpc_message msg = {
8544 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8545 .rpc_argp = &lgp->args,
8546 .rpc_resp = &lgp->res,
8547 .rpc_cred = lgp->cred,
8549 struct rpc_task_setup task_setup_data = {
8550 .rpc_client = server->client,
8551 .rpc_message = &msg,
8552 .callback_ops = &nfs4_layoutget_call_ops,
8553 .callback_data = lgp,
8554 .flags = RPC_TASK_ASYNC,
8556 struct pnfs_layout_segment *lseg = NULL;
8557 struct nfs4_exception exception = {
8559 .timeout = *timeout,
8563 dprintk("--> %s\n", __func__);
8565 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8566 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8568 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8569 if (!lgp->args.layout.pages) {
8570 nfs4_layoutget_release(lgp);
8571 return ERR_PTR(-ENOMEM);
8573 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8575 lgp->res.layoutp = &lgp->args.layout;
8576 lgp->res.seq_res.sr_slot = NULL;
8577 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
8579 task = rpc_run_task(&task_setup_data);
8581 return ERR_CAST(task);
8582 status = nfs4_wait_for_completion_rpc_task(task);
8584 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8585 *timeout = exception.timeout;
8588 trace_nfs4_layoutget(lgp->args.ctx,
8594 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8595 if (status == 0 && lgp->res.layoutp->len)
8596 lseg = pnfs_layout_process(lgp);
8597 nfs4_sequence_free_slot(&lgp->res.seq_res);
8599 dprintk("<-- %s status=%d\n", __func__, status);
8601 return ERR_PTR(status);
8606 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8608 struct nfs4_layoutreturn *lrp = calldata;
8610 dprintk("--> %s\n", __func__);
8611 nfs41_setup_sequence(lrp->clp->cl_session,
8612 &lrp->args.seq_args,
8617 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8619 struct nfs4_layoutreturn *lrp = calldata;
8620 struct nfs_server *server;
8622 dprintk("--> %s\n", __func__);
8624 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
8627 server = NFS_SERVER(lrp->args.inode);
8628 switch (task->tk_status) {
8630 task->tk_status = 0;
8633 case -NFS4ERR_DELAY:
8634 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
8636 nfs4_sequence_free_slot(&lrp->res.seq_res);
8637 rpc_restart_call_prepare(task);
8640 dprintk("<-- %s\n", __func__);
8643 static void nfs4_layoutreturn_release(void *calldata)
8645 struct nfs4_layoutreturn *lrp = calldata;
8646 struct pnfs_layout_hdr *lo = lrp->args.layout;
8648 dprintk("--> %s\n", __func__);
8649 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
8650 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
8651 nfs4_sequence_free_slot(&lrp->res.seq_res);
8652 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
8653 lrp->ld_private.ops->free(&lrp->ld_private);
8654 pnfs_put_layout_hdr(lrp->args.layout);
8655 nfs_iput_and_deactive(lrp->inode);
8657 dprintk("<-- %s\n", __func__);
8660 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8661 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8662 .rpc_call_done = nfs4_layoutreturn_done,
8663 .rpc_release = nfs4_layoutreturn_release,
8666 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
8668 struct rpc_task *task;
8669 struct rpc_message msg = {
8670 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8671 .rpc_argp = &lrp->args,
8672 .rpc_resp = &lrp->res,
8673 .rpc_cred = lrp->cred,
8675 struct rpc_task_setup task_setup_data = {
8676 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
8677 .rpc_message = &msg,
8678 .callback_ops = &nfs4_layoutreturn_call_ops,
8679 .callback_data = lrp,
8683 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8684 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8685 &task_setup_data.rpc_client, &msg);
8687 dprintk("--> %s\n", __func__);
8689 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8691 nfs4_layoutreturn_release(lrp);
8694 task_setup_data.flags |= RPC_TASK_ASYNC;
8696 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
8697 task = rpc_run_task(&task_setup_data);
8699 return PTR_ERR(task);
8701 status = task->tk_status;
8702 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
8703 dprintk("<-- %s status=%d\n", __func__, status);
8709 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
8710 struct pnfs_device *pdev,
8711 struct rpc_cred *cred)
8713 struct nfs4_getdeviceinfo_args args = {
8715 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8716 NOTIFY_DEVICEID4_DELETE,
8718 struct nfs4_getdeviceinfo_res res = {
8721 struct rpc_message msg = {
8722 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8729 dprintk("--> %s\n", __func__);
8730 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
8731 if (res.notification & ~args.notify_types)
8732 dprintk("%s: unsupported notification\n", __func__);
8733 if (res.notification != args.notify_types)
8736 dprintk("<-- %s status=%d\n", __func__, status);
8741 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8742 struct pnfs_device *pdev,
8743 struct rpc_cred *cred)
8745 struct nfs4_exception exception = { };
8749 err = nfs4_handle_exception(server,
8750 _nfs4_proc_getdeviceinfo(server, pdev, cred),
8752 } while (exception.retry);
8755 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8757 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8759 struct nfs4_layoutcommit_data *data = calldata;
8760 struct nfs_server *server = NFS_SERVER(data->args.inode);
8761 struct nfs4_session *session = nfs4_get_session(server);
8763 nfs41_setup_sequence(session,
8764 &data->args.seq_args,
8770 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8772 struct nfs4_layoutcommit_data *data = calldata;
8773 struct nfs_server *server = NFS_SERVER(data->args.inode);
8775 if (!nfs41_sequence_done(task, &data->res.seq_res))
8778 switch (task->tk_status) { /* Just ignore these failures */
8779 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8780 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8781 case -NFS4ERR_BADLAYOUT: /* no layout */
8782 case -NFS4ERR_GRACE: /* loca_recalim always false */
8783 task->tk_status = 0;
8787 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
8788 rpc_restart_call_prepare(task);
8794 static void nfs4_layoutcommit_release(void *calldata)
8796 struct nfs4_layoutcommit_data *data = calldata;
8798 pnfs_cleanup_layoutcommit(data);
8799 nfs_post_op_update_inode_force_wcc(data->args.inode,
8801 put_rpccred(data->cred);
8802 nfs_iput_and_deactive(data->inode);
8806 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8807 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8808 .rpc_call_done = nfs4_layoutcommit_done,
8809 .rpc_release = nfs4_layoutcommit_release,
8813 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
8815 struct rpc_message msg = {
8816 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8817 .rpc_argp = &data->args,
8818 .rpc_resp = &data->res,
8819 .rpc_cred = data->cred,
8821 struct rpc_task_setup task_setup_data = {
8822 .task = &data->task,
8823 .rpc_client = NFS_CLIENT(data->args.inode),
8824 .rpc_message = &msg,
8825 .callback_ops = &nfs4_layoutcommit_ops,
8826 .callback_data = data,
8828 struct rpc_task *task;
8831 dprintk("NFS: initiating layoutcommit call. sync %d "
8832 "lbw: %llu inode %lu\n", sync,
8833 data->args.lastbytewritten,
8834 data->args.inode->i_ino);
8837 data->inode = nfs_igrab_and_active(data->args.inode);
8838 if (data->inode == NULL) {
8839 nfs4_layoutcommit_release(data);
8842 task_setup_data.flags = RPC_TASK_ASYNC;
8844 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
8845 task = rpc_run_task(&task_setup_data);
8847 return PTR_ERR(task);
8849 status = task->tk_status;
8850 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
8851 dprintk("%s: status %d\n", __func__, status);
8857 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8858 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8861 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8862 struct nfs_fsinfo *info,
8863 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
8865 struct nfs41_secinfo_no_name_args args = {
8866 .style = SECINFO_STYLE_CURRENT_FH,
8868 struct nfs4_secinfo_res res = {
8871 struct rpc_message msg = {
8872 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8876 struct rpc_clnt *clnt = server->client;
8877 struct rpc_cred *cred = NULL;
8880 if (use_integrity) {
8881 clnt = server->nfs_client->cl_rpcclient;
8882 cred = nfs4_get_clid_cred(server->nfs_client);
8883 msg.rpc_cred = cred;
8886 dprintk("--> %s\n", __func__);
8887 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8889 dprintk("<-- %s status=%d\n", __func__, status);
8898 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8899 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8901 struct nfs4_exception exception = { };
8904 /* first try using integrity protection */
8905 err = -NFS4ERR_WRONGSEC;
8907 /* try to use integrity protection with machine cred */
8908 if (_nfs4_is_integrity_protected(server->nfs_client))
8909 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8913 * if unable to use integrity protection, or SECINFO with
8914 * integrity protection returns NFS4ERR_WRONGSEC (which is
8915 * disallowed by spec, but exists in deployed servers) use
8916 * the current filesystem's rpc_client and the user cred.
8918 if (err == -NFS4ERR_WRONGSEC)
8919 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8924 case -NFS4ERR_WRONGSEC:
8928 err = nfs4_handle_exception(server, err, &exception);
8930 } while (exception.retry);
8936 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8937 struct nfs_fsinfo *info)
8941 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
8942 struct nfs4_secinfo_flavors *flavors;
8943 struct nfs4_secinfo4 *secinfo;
8946 page = alloc_page(GFP_KERNEL);
8952 flavors = page_address(page);
8953 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8956 * Fall back on "guess and check" method if
8957 * the server doesn't support SECINFO_NO_NAME
8959 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
8960 err = nfs4_find_root_sec(server, fhandle, info);
8966 for (i = 0; i < flavors->num_flavors; i++) {
8967 secinfo = &flavors->flavors[i];
8969 switch (secinfo->flavor) {
8973 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8974 &secinfo->flavor_info);
8977 flavor = RPC_AUTH_MAXFLAVOR;
8981 if (!nfs_auth_info_match(&server->auth_info, flavor))
8982 flavor = RPC_AUTH_MAXFLAVOR;
8984 if (flavor != RPC_AUTH_MAXFLAVOR) {
8985 err = nfs4_lookup_root_sec(server, fhandle,
8992 if (flavor == RPC_AUTH_MAXFLAVOR)
9003 static int _nfs41_test_stateid(struct nfs_server *server,
9004 nfs4_stateid *stateid,
9005 struct rpc_cred *cred)
9008 struct nfs41_test_stateid_args args = {
9011 struct nfs41_test_stateid_res res;
9012 struct rpc_message msg = {
9013 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9018 struct rpc_clnt *rpc_client = server->client;
9020 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9023 dprintk("NFS call test_stateid %p\n", stateid);
9024 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
9025 nfs4_set_sequence_privileged(&args.seq_args);
9026 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
9027 &args.seq_args, &res.seq_res);
9028 if (status != NFS_OK) {
9029 dprintk("NFS reply test_stateid: failed, %d\n", status);
9032 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
9036 static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9037 int err, struct nfs4_exception *exception)
9039 exception->retry = 0;
9041 case -NFS4ERR_DELAY:
9042 case -NFS4ERR_RETRY_UNCACHED_REP:
9043 nfs4_handle_exception(server, err, exception);
9045 case -NFS4ERR_BADSESSION:
9046 case -NFS4ERR_BADSLOT:
9047 case -NFS4ERR_BAD_HIGH_SLOT:
9048 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9049 case -NFS4ERR_DEADSESSION:
9050 nfs4_do_handle_exception(server, err, exception);
9055 * nfs41_test_stateid - perform a TEST_STATEID operation
9057 * @server: server / transport on which to perform the operation
9058 * @stateid: state ID to test
9061 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9062 * Otherwise a negative NFS4ERR value is returned if the operation
9063 * failed or the state ID is not currently valid.
9065 static int nfs41_test_stateid(struct nfs_server *server,
9066 nfs4_stateid *stateid,
9067 struct rpc_cred *cred)
9069 struct nfs4_exception exception = { };
9072 err = _nfs41_test_stateid(server, stateid, cred);
9073 nfs4_handle_delay_or_session_error(server, err, &exception);
9074 } while (exception.retry);
9078 struct nfs_free_stateid_data {
9079 struct nfs_server *server;
9080 struct nfs41_free_stateid_args args;
9081 struct nfs41_free_stateid_res res;
9084 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9086 struct nfs_free_stateid_data *data = calldata;
9087 nfs41_setup_sequence(nfs4_get_session(data->server),
9088 &data->args.seq_args,
9093 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9095 struct nfs_free_stateid_data *data = calldata;
9097 nfs41_sequence_done(task, &data->res.seq_res);
9099 switch (task->tk_status) {
9100 case -NFS4ERR_DELAY:
9101 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
9102 rpc_restart_call_prepare(task);
9106 static void nfs41_free_stateid_release(void *calldata)
9111 static const struct rpc_call_ops nfs41_free_stateid_ops = {
9112 .rpc_call_prepare = nfs41_free_stateid_prepare,
9113 .rpc_call_done = nfs41_free_stateid_done,
9114 .rpc_release = nfs41_free_stateid_release,
9117 static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
9118 const nfs4_stateid *stateid,
9119 struct rpc_cred *cred,
9122 struct rpc_message msg = {
9123 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
9126 struct rpc_task_setup task_setup = {
9127 .rpc_client = server->client,
9128 .rpc_message = &msg,
9129 .callback_ops = &nfs41_free_stateid_ops,
9130 .flags = RPC_TASK_ASYNC,
9132 struct nfs_free_stateid_data *data;
9134 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9135 &task_setup.rpc_client, &msg);
9137 dprintk("NFS call free_stateid %p\n", stateid);
9138 data = kmalloc(sizeof(*data), GFP_NOFS);
9140 return ERR_PTR(-ENOMEM);
9141 data->server = server;
9142 nfs4_stateid_copy(&data->args.stateid, stateid);
9144 task_setup.callback_data = data;
9146 msg.rpc_argp = &data->args;
9147 msg.rpc_resp = &data->res;
9148 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
9150 nfs4_set_sequence_privileged(&data->args.seq_args);
9152 return rpc_run_task(&task_setup);
9156 * nfs41_free_stateid - perform a FREE_STATEID operation
9158 * @server: server / transport on which to perform the operation
9159 * @stateid: state ID to release
9161 * @is_recovery: set to true if this call needs to be privileged
9163 * Note: this function is always asynchronous.
9165 static int nfs41_free_stateid(struct nfs_server *server,
9166 const nfs4_stateid *stateid,
9167 struct rpc_cred *cred,
9170 struct rpc_task *task;
9172 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
9174 return PTR_ERR(task);
9180 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
9182 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
9184 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
9185 nfs4_free_lock_state(server, lsp);
9188 static bool nfs41_match_stateid(const nfs4_stateid *s1,
9189 const nfs4_stateid *s2)
9191 if (s1->type != s2->type)
9194 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
9197 if (s1->seqid == s2->seqid)
9199 if (s1->seqid == 0 || s2->seqid == 0)
9205 #endif /* CONFIG_NFS_V4_1 */
9207 static bool nfs4_match_stateid(const nfs4_stateid *s1,
9208 const nfs4_stateid *s2)
9210 return nfs4_stateid_match(s1, s2);
9214 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
9215 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9216 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9217 .recover_open = nfs4_open_reclaim,
9218 .recover_lock = nfs4_lock_reclaim,
9219 .establish_clid = nfs4_init_clientid,
9220 .detect_trunking = nfs40_discover_server_trunking,
9223 #if defined(CONFIG_NFS_V4_1)
9224 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
9225 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9226 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9227 .recover_open = nfs4_open_reclaim,
9228 .recover_lock = nfs4_lock_reclaim,
9229 .establish_clid = nfs41_init_clientid,
9230 .reclaim_complete = nfs41_proc_reclaim_complete,
9231 .detect_trunking = nfs41_discover_server_trunking,
9233 #endif /* CONFIG_NFS_V4_1 */
9235 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
9236 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9237 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
9238 .recover_open = nfs40_open_expired,
9239 .recover_lock = nfs4_lock_expired,
9240 .establish_clid = nfs4_init_clientid,
9243 #if defined(CONFIG_NFS_V4_1)
9244 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
9245 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9246 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
9247 .recover_open = nfs41_open_expired,
9248 .recover_lock = nfs41_lock_expired,
9249 .establish_clid = nfs41_init_clientid,
9251 #endif /* CONFIG_NFS_V4_1 */
9253 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
9254 .sched_state_renewal = nfs4_proc_async_renew,
9255 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
9256 .renew_lease = nfs4_proc_renew,
9259 #if defined(CONFIG_NFS_V4_1)
9260 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
9261 .sched_state_renewal = nfs41_proc_async_sequence,
9262 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
9263 .renew_lease = nfs4_proc_sequence,
9267 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
9268 .get_locations = _nfs40_proc_get_locations,
9269 .fsid_present = _nfs40_proc_fsid_present,
9272 #if defined(CONFIG_NFS_V4_1)
9273 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
9274 .get_locations = _nfs41_proc_get_locations,
9275 .fsid_present = _nfs41_proc_fsid_present,
9277 #endif /* CONFIG_NFS_V4_1 */
9279 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9281 .init_caps = NFS_CAP_READDIRPLUS
9282 | NFS_CAP_ATOMIC_OPEN
9283 | NFS_CAP_POSIX_LOCK,
9284 .init_client = nfs40_init_client,
9285 .shutdown_client = nfs40_shutdown_client,
9286 .match_stateid = nfs4_match_stateid,
9287 .find_root_sec = nfs4_find_root_sec,
9288 .free_lock_state = nfs4_release_lockowner,
9289 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
9290 .alloc_seqid = nfs_alloc_seqid,
9291 .call_sync_ops = &nfs40_call_sync_ops,
9292 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9293 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9294 .state_renewal_ops = &nfs40_state_renewal_ops,
9295 .mig_recovery_ops = &nfs40_mig_recovery_ops,
9298 #if defined(CONFIG_NFS_V4_1)
9299 static struct nfs_seqid *
9300 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9305 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9307 .init_caps = NFS_CAP_READDIRPLUS
9308 | NFS_CAP_ATOMIC_OPEN
9309 | NFS_CAP_POSIX_LOCK
9310 | NFS_CAP_STATEID_NFSV41
9311 | NFS_CAP_ATOMIC_OPEN_V1,
9312 .init_client = nfs41_init_client,
9313 .shutdown_client = nfs41_shutdown_client,
9314 .match_stateid = nfs41_match_stateid,
9315 .find_root_sec = nfs41_find_root_sec,
9316 .free_lock_state = nfs41_free_lock_state,
9317 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
9318 .alloc_seqid = nfs_alloc_no_seqid,
9319 .session_trunk = nfs4_test_session_trunk,
9320 .call_sync_ops = &nfs41_call_sync_ops,
9321 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9322 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9323 .state_renewal_ops = &nfs41_state_renewal_ops,
9324 .mig_recovery_ops = &nfs41_mig_recovery_ops,
9328 #if defined(CONFIG_NFS_V4_2)
9329 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9331 .init_caps = NFS_CAP_READDIRPLUS
9332 | NFS_CAP_ATOMIC_OPEN
9333 | NFS_CAP_POSIX_LOCK
9334 | NFS_CAP_STATEID_NFSV41
9335 | NFS_CAP_ATOMIC_OPEN_V1
9338 | NFS_CAP_DEALLOCATE
9340 | NFS_CAP_LAYOUTSTATS
9342 .init_client = nfs41_init_client,
9343 .shutdown_client = nfs41_shutdown_client,
9344 .match_stateid = nfs41_match_stateid,
9345 .find_root_sec = nfs41_find_root_sec,
9346 .free_lock_state = nfs41_free_lock_state,
9347 .call_sync_ops = &nfs41_call_sync_ops,
9348 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
9349 .alloc_seqid = nfs_alloc_no_seqid,
9350 .session_trunk = nfs4_test_session_trunk,
9351 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9352 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9353 .state_renewal_ops = &nfs41_state_renewal_ops,
9354 .mig_recovery_ops = &nfs41_mig_recovery_ops,
9358 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9359 [0] = &nfs_v4_0_minor_ops,
9360 #if defined(CONFIG_NFS_V4_1)
9361 [1] = &nfs_v4_1_minor_ops,
9363 #if defined(CONFIG_NFS_V4_2)
9364 [2] = &nfs_v4_2_minor_ops,
9368 static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
9370 ssize_t error, error2;
9372 error = generic_listxattr(dentry, list, size);
9380 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9383 return error + error2;
9386 static const struct inode_operations nfs4_dir_inode_operations = {
9387 .create = nfs_create,
9388 .lookup = nfs_lookup,
9389 .atomic_open = nfs_atomic_open,
9391 .unlink = nfs_unlink,
9392 .symlink = nfs_symlink,
9396 .rename = nfs_rename,
9397 .permission = nfs_permission,
9398 .getattr = nfs_getattr,
9399 .setattr = nfs_setattr,
9400 .listxattr = nfs4_listxattr,
9403 static const struct inode_operations nfs4_file_inode_operations = {
9404 .permission = nfs_permission,
9405 .getattr = nfs_getattr,
9406 .setattr = nfs_setattr,
9407 .listxattr = nfs4_listxattr,
9410 const struct nfs_rpc_ops nfs_v4_clientops = {
9411 .version = 4, /* protocol version */
9412 .dentry_ops = &nfs4_dentry_operations,
9413 .dir_inode_ops = &nfs4_dir_inode_operations,
9414 .file_inode_ops = &nfs4_file_inode_operations,
9415 .file_ops = &nfs4_file_operations,
9416 .getroot = nfs4_proc_get_root,
9417 .submount = nfs4_submount,
9418 .try_mount = nfs4_try_mount,
9419 .getattr = nfs4_proc_getattr,
9420 .setattr = nfs4_proc_setattr,
9421 .lookup = nfs4_proc_lookup,
9422 .access = nfs4_proc_access,
9423 .readlink = nfs4_proc_readlink,
9424 .create = nfs4_proc_create,
9425 .remove = nfs4_proc_remove,
9426 .unlink_setup = nfs4_proc_unlink_setup,
9427 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
9428 .unlink_done = nfs4_proc_unlink_done,
9429 .rename_setup = nfs4_proc_rename_setup,
9430 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
9431 .rename_done = nfs4_proc_rename_done,
9432 .link = nfs4_proc_link,
9433 .symlink = nfs4_proc_symlink,
9434 .mkdir = nfs4_proc_mkdir,
9435 .rmdir = nfs4_proc_remove,
9436 .readdir = nfs4_proc_readdir,
9437 .mknod = nfs4_proc_mknod,
9438 .statfs = nfs4_proc_statfs,
9439 .fsinfo = nfs4_proc_fsinfo,
9440 .pathconf = nfs4_proc_pathconf,
9441 .set_capabilities = nfs4_server_capabilities,
9442 .decode_dirent = nfs4_decode_dirent,
9443 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
9444 .read_setup = nfs4_proc_read_setup,
9445 .read_done = nfs4_read_done,
9446 .write_setup = nfs4_proc_write_setup,
9447 .write_done = nfs4_write_done,
9448 .commit_setup = nfs4_proc_commit_setup,
9449 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
9450 .commit_done = nfs4_commit_done,
9451 .lock = nfs4_proc_lock,
9452 .clear_acl_cache = nfs4_zap_acl_attr,
9453 .close_context = nfs4_close_context,
9454 .open_context = nfs4_atomic_open,
9455 .have_delegation = nfs4_have_delegation,
9456 .return_delegation = nfs4_inode_return_delegation,
9457 .alloc_client = nfs4_alloc_client,
9458 .init_client = nfs4_init_client,
9459 .free_client = nfs4_free_client,
9460 .create_server = nfs4_create_server,
9461 .clone_server = nfs_clone_server,
9464 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
9465 .name = XATTR_NAME_NFSV4_ACL,
9466 .list = nfs4_xattr_list_nfs4_acl,
9467 .get = nfs4_xattr_get_nfs4_acl,
9468 .set = nfs4_xattr_set_nfs4_acl,
9471 const struct xattr_handler *nfs4_xattr_handlers[] = {
9472 &nfs4_xattr_nfs4_acl_handler,
9473 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
9474 &nfs4_xattr_nfs4_label_handler,