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.claim = nfs4_map_atomic_open_claim(server, claim);
1228 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1230 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1231 * will return permission denied for all bits until close */
1232 if (!(flags & O_EXCL)) {
1233 /* ask server to check for all possible rights as results
1235 switch (p->o_arg.claim) {
1238 case NFS4_OPEN_CLAIM_NULL:
1239 case NFS4_OPEN_CLAIM_FH:
1240 p->o_arg.access = NFS4_ACCESS_READ |
1241 NFS4_ACCESS_MODIFY |
1242 NFS4_ACCESS_EXTEND |
1243 NFS4_ACCESS_EXECUTE;
1246 p->o_arg.clientid = server->nfs_client->cl_clientid;
1247 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1248 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1249 p->o_arg.name = &dentry->d_name;
1250 p->o_arg.server = server;
1251 p->o_arg.bitmask = nfs4_bitmask(server, label);
1252 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1253 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1254 switch (p->o_arg.claim) {
1255 case NFS4_OPEN_CLAIM_NULL:
1256 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1257 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1258 p->o_arg.fh = NFS_FH(dir);
1260 case NFS4_OPEN_CLAIM_PREVIOUS:
1261 case NFS4_OPEN_CLAIM_FH:
1262 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1263 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1264 p->o_arg.fh = NFS_FH(d_inode(dentry));
1266 if (attrs != NULL && attrs->ia_valid != 0) {
1269 p->o_arg.u.attrs = &p->attrs;
1270 memcpy(&p->attrs, attrs, sizeof(p->attrs));
1273 verf[1] = current->pid;
1274 memcpy(p->o_arg.u.verifier.data, verf,
1275 sizeof(p->o_arg.u.verifier.data));
1277 p->c_arg.fh = &p->o_res.fh;
1278 p->c_arg.stateid = &p->o_res.stateid;
1279 p->c_arg.seqid = p->o_arg.seqid;
1280 nfs4_init_opendata_res(p);
1281 kref_init(&p->kref);
1285 nfs4_label_free(p->a_label);
1287 nfs4_label_free(p->f_label);
1295 static void nfs4_opendata_free(struct kref *kref)
1297 struct nfs4_opendata *p = container_of(kref,
1298 struct nfs4_opendata, kref);
1299 struct super_block *sb = p->dentry->d_sb;
1301 nfs_free_seqid(p->o_arg.seqid);
1302 nfs4_sequence_free_slot(&p->o_res.seq_res);
1303 if (p->state != NULL)
1304 nfs4_put_open_state(p->state);
1305 nfs4_put_state_owner(p->owner);
1307 nfs4_label_free(p->a_label);
1308 nfs4_label_free(p->f_label);
1312 nfs_sb_deactive(sb);
1313 nfs_fattr_free_names(&p->f_attr);
1314 kfree(p->f_attr.mdsthreshold);
1318 static void nfs4_opendata_put(struct nfs4_opendata *p)
1321 kref_put(&p->kref, nfs4_opendata_free);
1324 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1328 ret = rpc_wait_for_completion_task(task);
1332 static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1335 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1336 case FMODE_READ|FMODE_WRITE:
1337 return state->n_rdwr != 0;
1339 return state->n_wronly != 0;
1341 return state->n_rdonly != 0;
1347 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
1351 if (open_mode & (O_EXCL|O_TRUNC))
1353 switch (mode & (FMODE_READ|FMODE_WRITE)) {
1355 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1356 && state->n_rdonly != 0;
1359 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1360 && state->n_wronly != 0;
1362 case FMODE_READ|FMODE_WRITE:
1363 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1364 && state->n_rdwr != 0;
1370 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1371 enum open_claim_type4 claim)
1373 if (delegation == NULL)
1375 if ((delegation->type & fmode) != fmode)
1377 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1380 case NFS4_OPEN_CLAIM_NULL:
1381 case NFS4_OPEN_CLAIM_FH:
1383 case NFS4_OPEN_CLAIM_PREVIOUS:
1384 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1389 nfs_mark_delegation_referenced(delegation);
1393 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1402 case FMODE_READ|FMODE_WRITE:
1405 nfs4_state_set_mode_locked(state, state->state | fmode);
1408 #ifdef CONFIG_NFS_V4_1
1409 static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1411 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1413 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1415 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1419 #endif /* CONFIG_NFS_V4_1 */
1421 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
1423 struct nfs_client *clp = state->owner->so_server->nfs_client;
1424 bool need_recover = false;
1426 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1427 need_recover = true;
1428 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1429 need_recover = true;
1430 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1431 need_recover = true;
1433 nfs4_state_mark_reclaim_nograce(clp, state);
1436 static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1437 const nfs4_stateid *stateid, nfs4_stateid *freeme)
1439 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1441 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1442 nfs4_stateid_copy(freeme, &state->open_stateid);
1443 nfs_test_and_clear_all_open_stateid(state);
1446 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1451 static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1453 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1455 if (state->n_wronly)
1456 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1457 if (state->n_rdonly)
1458 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1460 set_bit(NFS_O_RDWR_STATE, &state->flags);
1461 set_bit(NFS_OPEN_STATE, &state->flags);
1464 static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1465 nfs4_stateid *stateid, fmode_t fmode)
1467 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1468 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1470 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1473 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1476 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1477 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1478 clear_bit(NFS_OPEN_STATE, &state->flags);
1480 if (stateid == NULL)
1482 /* Handle OPEN+OPEN_DOWNGRADE races */
1483 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1484 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1485 nfs_resync_open_stateid_locked(state);
1488 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1489 nfs4_stateid_copy(&state->stateid, stateid);
1490 nfs4_stateid_copy(&state->open_stateid, stateid);
1493 static void nfs_clear_open_stateid(struct nfs4_state *state,
1494 nfs4_stateid *arg_stateid,
1495 nfs4_stateid *stateid, fmode_t fmode)
1497 write_seqlock(&state->seqlock);
1498 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1499 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1500 nfs_clear_open_stateid_locked(state, stateid, fmode);
1501 write_sequnlock(&state->seqlock);
1502 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1503 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1506 static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1507 const nfs4_stateid *stateid, fmode_t fmode,
1508 nfs4_stateid *freeme)
1512 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1515 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1517 case FMODE_READ|FMODE_WRITE:
1518 set_bit(NFS_O_RDWR_STATE, &state->flags);
1520 if (!nfs_need_update_open_stateid(state, stateid, freeme))
1522 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1523 nfs4_stateid_copy(&state->stateid, stateid);
1524 nfs4_stateid_copy(&state->open_stateid, stateid);
1527 static void __update_open_stateid(struct nfs4_state *state,
1528 const nfs4_stateid *open_stateid,
1529 const nfs4_stateid *deleg_stateid,
1531 nfs4_stateid *freeme)
1534 * Protect the call to nfs4_state_set_mode_locked and
1535 * serialise the stateid update
1537 spin_lock(&state->owner->so_lock);
1538 write_seqlock(&state->seqlock);
1539 if (deleg_stateid != NULL) {
1540 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1541 set_bit(NFS_DELEGATED_STATE, &state->flags);
1543 if (open_stateid != NULL)
1544 nfs_set_open_stateid_locked(state, open_stateid, fmode, freeme);
1545 write_sequnlock(&state->seqlock);
1546 update_open_stateflags(state, fmode);
1547 spin_unlock(&state->owner->so_lock);
1550 static int update_open_stateid(struct nfs4_state *state,
1551 const nfs4_stateid *open_stateid,
1552 const nfs4_stateid *delegation,
1555 struct nfs_server *server = NFS_SERVER(state->inode);
1556 struct nfs_client *clp = server->nfs_client;
1557 struct nfs_inode *nfsi = NFS_I(state->inode);
1558 struct nfs_delegation *deleg_cur;
1559 nfs4_stateid freeme = { };
1562 fmode &= (FMODE_READ|FMODE_WRITE);
1565 deleg_cur = rcu_dereference(nfsi->delegation);
1566 if (deleg_cur == NULL)
1569 spin_lock(&deleg_cur->lock);
1570 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1571 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1572 (deleg_cur->type & fmode) != fmode)
1573 goto no_delegation_unlock;
1575 if (delegation == NULL)
1576 delegation = &deleg_cur->stateid;
1577 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
1578 goto no_delegation_unlock;
1580 nfs_mark_delegation_referenced(deleg_cur);
1581 __update_open_stateid(state, open_stateid, &deleg_cur->stateid,
1584 no_delegation_unlock:
1585 spin_unlock(&deleg_cur->lock);
1589 if (!ret && open_stateid != NULL) {
1590 __update_open_stateid(state, open_stateid, NULL, fmode, &freeme);
1593 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1594 nfs4_schedule_state_manager(clp);
1595 if (freeme.type != 0)
1596 nfs4_test_and_free_stateid(server, &freeme,
1597 state->owner->so_cred);
1602 static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1603 const nfs4_stateid *stateid)
1605 struct nfs4_state *state = lsp->ls_state;
1608 spin_lock(&state->state_lock);
1609 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1611 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1613 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1616 spin_unlock(&state->state_lock);
1620 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1622 struct nfs_delegation *delegation;
1625 delegation = rcu_dereference(NFS_I(inode)->delegation);
1626 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1631 nfs4_inode_return_delegation(inode);
1634 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1636 struct nfs4_state *state = opendata->state;
1637 struct nfs_inode *nfsi = NFS_I(state->inode);
1638 struct nfs_delegation *delegation;
1639 int open_mode = opendata->o_arg.open_flags;
1640 fmode_t fmode = opendata->o_arg.fmode;
1641 enum open_claim_type4 claim = opendata->o_arg.claim;
1642 nfs4_stateid stateid;
1646 spin_lock(&state->owner->so_lock);
1647 if (can_open_cached(state, fmode, open_mode)) {
1648 update_open_stateflags(state, fmode);
1649 spin_unlock(&state->owner->so_lock);
1650 goto out_return_state;
1652 spin_unlock(&state->owner->so_lock);
1654 delegation = rcu_dereference(nfsi->delegation);
1655 if (!can_open_delegated(delegation, fmode, claim)) {
1659 /* Save the delegation */
1660 nfs4_stateid_copy(&stateid, &delegation->stateid);
1662 nfs_release_seqid(opendata->o_arg.seqid);
1663 if (!opendata->is_recover) {
1664 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1670 /* Try to update the stateid using the delegation */
1671 if (update_open_stateid(state, NULL, &stateid, fmode))
1672 goto out_return_state;
1675 return ERR_PTR(ret);
1677 atomic_inc(&state->count);
1682 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1684 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1685 struct nfs_delegation *delegation;
1686 int delegation_flags = 0;
1689 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1691 delegation_flags = delegation->flags;
1693 switch (data->o_arg.claim) {
1696 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1697 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1698 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1699 "returning a delegation for "
1700 "OPEN(CLAIM_DELEGATE_CUR)\n",
1704 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1705 nfs_inode_set_delegation(state->inode,
1706 data->owner->so_cred,
1709 nfs_inode_reclaim_delegation(state->inode,
1710 data->owner->so_cred,
1715 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1716 * and update the nfs4_state.
1718 static struct nfs4_state *
1719 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1721 struct inode *inode = data->state->inode;
1722 struct nfs4_state *state = data->state;
1725 if (!data->rpc_done) {
1726 if (data->rpc_status) {
1727 ret = data->rpc_status;
1730 /* cached opens have already been processed */
1734 ret = nfs_refresh_inode(inode, &data->f_attr);
1738 if (data->o_res.delegation_type != 0)
1739 nfs4_opendata_check_deleg(data, state);
1741 update_open_stateid(state, &data->o_res.stateid, NULL,
1743 atomic_inc(&state->count);
1747 return ERR_PTR(ret);
1751 static struct nfs4_state *
1752 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1754 struct inode *inode;
1755 struct nfs4_state *state = NULL;
1758 if (!data->rpc_done) {
1759 state = nfs4_try_open_cached(data);
1760 trace_nfs4_cached_open(data->state);
1765 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1767 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
1768 ret = PTR_ERR(inode);
1772 state = nfs4_get_open_state(inode, data->owner);
1775 if (data->o_res.delegation_type != 0)
1776 nfs4_opendata_check_deleg(data, state);
1777 update_open_stateid(state, &data->o_res.stateid, NULL,
1781 nfs_release_seqid(data->o_arg.seqid);
1786 return ERR_PTR(ret);
1789 static struct nfs4_state *
1790 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1792 struct nfs4_state *ret;
1794 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1795 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1797 ret = _nfs4_opendata_to_nfs4_state(data);
1798 nfs4_sequence_free_slot(&data->o_res.seq_res);
1802 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1804 struct nfs_inode *nfsi = NFS_I(state->inode);
1805 struct nfs_open_context *ctx;
1807 spin_lock(&state->inode->i_lock);
1808 list_for_each_entry(ctx, &nfsi->open_files, list) {
1809 if (ctx->state != state)
1811 get_nfs_open_context(ctx);
1812 spin_unlock(&state->inode->i_lock);
1815 spin_unlock(&state->inode->i_lock);
1816 return ERR_PTR(-ENOENT);
1819 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1820 struct nfs4_state *state, enum open_claim_type4 claim)
1822 struct nfs4_opendata *opendata;
1824 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
1825 NULL, NULL, claim, GFP_NOFS);
1826 if (opendata == NULL)
1827 return ERR_PTR(-ENOMEM);
1828 opendata->state = state;
1829 atomic_inc(&state->count);
1833 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1836 struct nfs4_state *newstate;
1839 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
1841 opendata->o_arg.open_flags = 0;
1842 opendata->o_arg.fmode = fmode;
1843 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1844 NFS_SB(opendata->dentry->d_sb),
1846 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1847 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1848 nfs4_init_opendata_res(opendata);
1849 ret = _nfs4_recover_proc_open(opendata);
1852 newstate = nfs4_opendata_to_nfs4_state(opendata);
1853 if (IS_ERR(newstate))
1854 return PTR_ERR(newstate);
1855 if (newstate != opendata->state)
1857 nfs4_close_state(newstate, fmode);
1861 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1865 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1866 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1867 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1868 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1869 /* memory barrier prior to reading state->n_* */
1870 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1871 clear_bit(NFS_OPEN_STATE, &state->flags);
1873 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1876 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1879 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1883 * We may have performed cached opens for all three recoveries.
1884 * Check if we need to update the current stateid.
1886 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1887 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
1888 write_seqlock(&state->seqlock);
1889 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1890 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1891 write_sequnlock(&state->seqlock);
1898 * reclaim state on the server after a reboot.
1900 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1902 struct nfs_delegation *delegation;
1903 struct nfs4_opendata *opendata;
1904 fmode_t delegation_type = 0;
1907 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1908 NFS4_OPEN_CLAIM_PREVIOUS);
1909 if (IS_ERR(opendata))
1910 return PTR_ERR(opendata);
1912 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1913 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1914 delegation_type = delegation->type;
1916 opendata->o_arg.u.delegation_type = delegation_type;
1917 status = nfs4_open_recover(opendata, state);
1918 nfs4_opendata_put(opendata);
1922 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1924 struct nfs_server *server = NFS_SERVER(state->inode);
1925 struct nfs4_exception exception = { };
1928 err = _nfs4_do_open_reclaim(ctx, state);
1929 trace_nfs4_open_reclaim(ctx, 0, err);
1930 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1932 if (err != -NFS4ERR_DELAY)
1934 nfs4_handle_exception(server, err, &exception);
1935 } while (exception.retry);
1939 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1941 struct nfs_open_context *ctx;
1944 ctx = nfs4_state_find_open_context(state);
1947 ret = nfs4_do_open_reclaim(ctx, state);
1948 put_nfs_open_context(ctx);
1952 static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, int err)
1956 printk(KERN_ERR "NFS: %s: unhandled error "
1957 "%d.\n", __func__, err);
1963 case -NFS4ERR_BADSESSION:
1964 case -NFS4ERR_BADSLOT:
1965 case -NFS4ERR_BAD_HIGH_SLOT:
1966 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1967 case -NFS4ERR_DEADSESSION:
1968 set_bit(NFS_DELEGATED_STATE, &state->flags);
1969 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1971 case -NFS4ERR_STALE_CLIENTID:
1972 case -NFS4ERR_STALE_STATEID:
1973 set_bit(NFS_DELEGATED_STATE, &state->flags);
1974 /* Don't recall a delegation if it was lost */
1975 nfs4_schedule_lease_recovery(server->nfs_client);
1977 case -NFS4ERR_MOVED:
1978 nfs4_schedule_migration_recovery(server);
1980 case -NFS4ERR_LEASE_MOVED:
1981 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1983 case -NFS4ERR_DELEG_REVOKED:
1984 case -NFS4ERR_ADMIN_REVOKED:
1985 case -NFS4ERR_EXPIRED:
1986 case -NFS4ERR_BAD_STATEID:
1987 case -NFS4ERR_OPENMODE:
1988 nfs_inode_find_state_and_recover(state->inode,
1990 nfs4_schedule_stateid_recovery(server, state);
1992 case -NFS4ERR_DELAY:
1993 case -NFS4ERR_GRACE:
1994 set_bit(NFS_DELEGATED_STATE, &state->flags);
1998 case -NFS4ERR_DENIED:
1999 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
2005 int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2006 struct nfs4_state *state, const nfs4_stateid *stateid,
2009 struct nfs_server *server = NFS_SERVER(state->inode);
2010 struct nfs4_opendata *opendata;
2013 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2014 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2015 if (IS_ERR(opendata))
2016 return PTR_ERR(opendata);
2017 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
2018 write_seqlock(&state->seqlock);
2019 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2020 write_sequnlock(&state->seqlock);
2021 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2022 switch (type & (FMODE_READ|FMODE_WRITE)) {
2023 case FMODE_READ|FMODE_WRITE:
2025 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2028 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2032 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2034 nfs4_opendata_put(opendata);
2035 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
2038 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2040 struct nfs4_opendata *data = calldata;
2042 nfs40_setup_sequence(data->o_arg.server->nfs_client->cl_slot_tbl,
2043 &data->c_arg.seq_args, &data->c_res.seq_res, task);
2046 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2048 struct nfs4_opendata *data = calldata;
2050 nfs40_sequence_done(task, &data->c_res.seq_res);
2052 data->rpc_status = task->tk_status;
2053 if (data->rpc_status == 0) {
2054 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
2055 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2056 renew_lease(data->o_res.server, data->timestamp);
2061 static void nfs4_open_confirm_release(void *calldata)
2063 struct nfs4_opendata *data = calldata;
2064 struct nfs4_state *state = NULL;
2066 /* If this request hasn't been cancelled, do nothing */
2067 if (data->cancelled == 0)
2069 /* In case of error, no cleanup! */
2070 if (!data->rpc_done)
2072 state = nfs4_opendata_to_nfs4_state(data);
2074 nfs4_close_state(state, data->o_arg.fmode);
2076 nfs4_opendata_put(data);
2079 static const struct rpc_call_ops nfs4_open_confirm_ops = {
2080 .rpc_call_prepare = nfs4_open_confirm_prepare,
2081 .rpc_call_done = nfs4_open_confirm_done,
2082 .rpc_release = nfs4_open_confirm_release,
2086 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2088 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2090 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
2091 struct rpc_task *task;
2092 struct rpc_message msg = {
2093 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2094 .rpc_argp = &data->c_arg,
2095 .rpc_resp = &data->c_res,
2096 .rpc_cred = data->owner->so_cred,
2098 struct rpc_task_setup task_setup_data = {
2099 .rpc_client = server->client,
2100 .rpc_message = &msg,
2101 .callback_ops = &nfs4_open_confirm_ops,
2102 .callback_data = data,
2103 .workqueue = nfsiod_workqueue,
2104 .flags = RPC_TASK_ASYNC,
2108 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
2109 kref_get(&data->kref);
2111 data->rpc_status = 0;
2112 data->timestamp = jiffies;
2113 if (data->is_recover)
2114 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
2115 task = rpc_run_task(&task_setup_data);
2117 return PTR_ERR(task);
2118 status = nfs4_wait_for_completion_rpc_task(task);
2120 data->cancelled = 1;
2123 status = data->rpc_status;
2128 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
2130 struct nfs4_opendata *data = calldata;
2131 struct nfs4_state_owner *sp = data->owner;
2132 struct nfs_client *clp = sp->so_server->nfs_client;
2133 enum open_claim_type4 claim = data->o_arg.claim;
2135 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
2138 * Check if we still need to send an OPEN call, or if we can use
2139 * a delegation instead.
2141 if (data->state != NULL) {
2142 struct nfs_delegation *delegation;
2144 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
2147 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
2148 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
2149 goto unlock_no_action;
2152 /* Update client id. */
2153 data->o_arg.clientid = clp->cl_clientid;
2157 case NFS4_OPEN_CLAIM_PREVIOUS:
2158 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2159 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
2160 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
2161 case NFS4_OPEN_CLAIM_FH:
2162 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
2163 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
2165 data->timestamp = jiffies;
2166 if (nfs4_setup_sequence(data->o_arg.server,
2167 &data->o_arg.seq_args,
2168 &data->o_res.seq_res,
2170 nfs_release_seqid(data->o_arg.seqid);
2172 /* Set the create mode (note dependency on the session type) */
2173 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2174 if (data->o_arg.open_flags & O_EXCL) {
2175 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2176 if (nfs4_has_persistent_session(clp))
2177 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2178 else if (clp->cl_mvops->minor_version > 0)
2179 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2183 trace_nfs4_cached_open(data->state);
2186 task->tk_action = NULL;
2188 nfs4_sequence_done(task, &data->o_res.seq_res);
2191 static void nfs4_open_done(struct rpc_task *task, void *calldata)
2193 struct nfs4_opendata *data = calldata;
2195 data->rpc_status = task->tk_status;
2197 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
2200 if (task->tk_status == 0) {
2201 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2202 switch (data->o_res.f_attr->mode & S_IFMT) {
2206 data->rpc_status = -ELOOP;
2209 data->rpc_status = -EISDIR;
2212 data->rpc_status = -ENOTDIR;
2215 renew_lease(data->o_res.server, data->timestamp);
2216 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2217 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2222 static void nfs4_open_release(void *calldata)
2224 struct nfs4_opendata *data = calldata;
2225 struct nfs4_state *state = NULL;
2227 /* If this request hasn't been cancelled, do nothing */
2228 if (data->cancelled == 0)
2230 /* In case of error, no cleanup! */
2231 if (data->rpc_status != 0 || !data->rpc_done)
2233 /* In case we need an open_confirm, no cleanup! */
2234 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2236 state = nfs4_opendata_to_nfs4_state(data);
2238 nfs4_close_state(state, data->o_arg.fmode);
2240 nfs4_opendata_put(data);
2243 static const struct rpc_call_ops nfs4_open_ops = {
2244 .rpc_call_prepare = nfs4_open_prepare,
2245 .rpc_call_done = nfs4_open_done,
2246 .rpc_release = nfs4_open_release,
2249 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
2251 struct inode *dir = d_inode(data->dir);
2252 struct nfs_server *server = NFS_SERVER(dir);
2253 struct nfs_openargs *o_arg = &data->o_arg;
2254 struct nfs_openres *o_res = &data->o_res;
2255 struct rpc_task *task;
2256 struct rpc_message msg = {
2257 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2260 .rpc_cred = data->owner->so_cred,
2262 struct rpc_task_setup task_setup_data = {
2263 .rpc_client = server->client,
2264 .rpc_message = &msg,
2265 .callback_ops = &nfs4_open_ops,
2266 .callback_data = data,
2267 .workqueue = nfsiod_workqueue,
2268 .flags = RPC_TASK_ASYNC,
2272 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
2273 kref_get(&data->kref);
2275 data->rpc_status = 0;
2276 data->cancelled = 0;
2277 data->is_recover = 0;
2279 nfs4_set_sequence_privileged(&o_arg->seq_args);
2280 data->is_recover = 1;
2282 task = rpc_run_task(&task_setup_data);
2284 return PTR_ERR(task);
2285 status = nfs4_wait_for_completion_rpc_task(task);
2287 data->cancelled = 1;
2290 status = data->rpc_status;
2296 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2298 struct inode *dir = d_inode(data->dir);
2299 struct nfs_openres *o_res = &data->o_res;
2302 status = nfs4_run_open_task(data, 1);
2303 if (status != 0 || !data->rpc_done)
2306 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2308 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2309 status = _nfs4_proc_open_confirm(data);
2318 * Additional permission checks in order to distinguish between an
2319 * open for read, and an open for execute. This works around the
2320 * fact that NFSv4 OPEN treats read and execute permissions as being
2322 * Note that in the non-execute case, we want to turn off permission
2323 * checking if we just created a new file (POSIX open() semantics).
2325 static int nfs4_opendata_access(struct rpc_cred *cred,
2326 struct nfs4_opendata *opendata,
2327 struct nfs4_state *state, fmode_t fmode,
2330 struct nfs_access_entry cache;
2333 /* access call failed or for some reason the server doesn't
2334 * support any access modes -- defer access call until later */
2335 if (opendata->o_res.access_supported == 0)
2340 * Use openflags to check for exec, because fmode won't
2341 * always have FMODE_EXEC set when file open for exec.
2343 if (openflags & __FMODE_EXEC) {
2344 /* ONLY check for exec rights */
2346 } else if ((fmode & FMODE_READ) && !opendata->file_created)
2350 cache.jiffies = jiffies;
2351 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2352 nfs_access_add_cache(state->inode, &cache);
2354 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
2357 /* even though OPEN succeeded, access is denied. Close the file */
2358 nfs4_close_state(state, fmode);
2363 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2365 static int _nfs4_proc_open(struct nfs4_opendata *data)
2367 struct inode *dir = d_inode(data->dir);
2368 struct nfs_server *server = NFS_SERVER(dir);
2369 struct nfs_openargs *o_arg = &data->o_arg;
2370 struct nfs_openres *o_res = &data->o_res;
2373 status = nfs4_run_open_task(data, 0);
2374 if (!data->rpc_done)
2377 if (status == -NFS4ERR_BADNAME &&
2378 !(o_arg->open_flags & O_CREAT))
2383 nfs_fattr_map_and_free_names(server, &data->f_attr);
2385 if (o_arg->open_flags & O_CREAT) {
2386 update_changeattr(dir, &o_res->cinfo);
2387 if (o_arg->open_flags & O_EXCL)
2388 data->file_created = 1;
2389 else if (o_res->cinfo.before != o_res->cinfo.after)
2390 data->file_created = 1;
2392 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2393 server->caps &= ~NFS_CAP_POSIX_LOCK;
2394 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2395 status = _nfs4_proc_open_confirm(data);
2399 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
2400 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
2404 static int nfs4_recover_expired_lease(struct nfs_server *server)
2406 return nfs4_client_recover_expired_lease(server->nfs_client);
2411 * reclaim state on the server after a network partition.
2412 * Assumes caller holds the appropriate lock
2414 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2416 struct nfs4_opendata *opendata;
2419 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2420 NFS4_OPEN_CLAIM_FH);
2421 if (IS_ERR(opendata))
2422 return PTR_ERR(opendata);
2423 ret = nfs4_open_recover(opendata, state);
2425 d_drop(ctx->dentry);
2426 nfs4_opendata_put(opendata);
2430 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2432 struct nfs_server *server = NFS_SERVER(state->inode);
2433 struct nfs4_exception exception = { };
2437 err = _nfs4_open_expired(ctx, state);
2438 trace_nfs4_open_expired(ctx, 0, err);
2439 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2444 case -NFS4ERR_GRACE:
2445 case -NFS4ERR_DELAY:
2446 nfs4_handle_exception(server, err, &exception);
2449 } while (exception.retry);
2454 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2456 struct nfs_open_context *ctx;
2459 ctx = nfs4_state_find_open_context(state);
2462 ret = nfs4_do_open_expired(ctx, state);
2463 put_nfs_open_context(ctx);
2467 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2468 const nfs4_stateid *stateid)
2470 nfs_remove_bad_delegation(state->inode, stateid);
2471 write_seqlock(&state->seqlock);
2472 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2473 write_sequnlock(&state->seqlock);
2474 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2477 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2479 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2480 nfs_finish_clear_delegation_stateid(state, NULL);
2483 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2485 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2486 nfs40_clear_delegation_stateid(state);
2487 return nfs4_open_expired(sp, state);
2490 static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2491 nfs4_stateid *stateid,
2492 struct rpc_cred *cred)
2494 return -NFS4ERR_BAD_STATEID;
2497 #if defined(CONFIG_NFS_V4_1)
2498 static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2499 nfs4_stateid *stateid,
2500 struct rpc_cred *cred)
2504 switch (stateid->type) {
2507 case NFS4_INVALID_STATEID_TYPE:
2508 case NFS4_SPECIAL_STATEID_TYPE:
2509 return -NFS4ERR_BAD_STATEID;
2510 case NFS4_REVOKED_STATEID_TYPE:
2514 status = nfs41_test_stateid(server, stateid, cred);
2516 case -NFS4ERR_EXPIRED:
2517 case -NFS4ERR_ADMIN_REVOKED:
2518 case -NFS4ERR_DELEG_REVOKED:
2524 /* Ack the revoked state to the server */
2525 nfs41_free_stateid(server, stateid, cred, true);
2526 return -NFS4ERR_EXPIRED;
2529 static void nfs41_check_delegation_stateid(struct nfs4_state *state)
2531 struct nfs_server *server = NFS_SERVER(state->inode);
2532 nfs4_stateid stateid;
2533 struct nfs_delegation *delegation;
2534 struct rpc_cred *cred;
2537 /* Get the delegation credential for use by test/free_stateid */
2539 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2540 if (delegation == NULL) {
2545 nfs4_stateid_copy(&stateid, &delegation->stateid);
2546 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
2548 nfs_finish_clear_delegation_stateid(state, &stateid);
2552 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags)) {
2557 cred = get_rpccred(delegation->cred);
2559 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
2560 trace_nfs4_test_delegation_stateid(state, NULL, status);
2561 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
2562 nfs_finish_clear_delegation_stateid(state, &stateid);
2568 * nfs41_check_expired_locks - possibly free a lock stateid
2570 * @state: NFSv4 state for an inode
2572 * Returns NFS_OK if recovery for this stateid is now finished.
2573 * Otherwise a negative NFS4ERR value is returned.
2575 static int nfs41_check_expired_locks(struct nfs4_state *state)
2577 int status, ret = NFS_OK;
2578 struct nfs4_lock_state *lsp, *prev = NULL;
2579 struct nfs_server *server = NFS_SERVER(state->inode);
2581 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2584 spin_lock(&state->state_lock);
2585 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2586 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2587 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2589 atomic_inc(&lsp->ls_count);
2590 spin_unlock(&state->state_lock);
2592 nfs4_put_lock_state(prev);
2595 status = nfs41_test_and_free_expired_stateid(server,
2598 trace_nfs4_test_lock_stateid(state, lsp, status);
2599 if (status == -NFS4ERR_EXPIRED ||
2600 status == -NFS4ERR_BAD_STATEID) {
2601 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
2602 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
2603 if (!recover_lost_locks)
2604 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2605 } else if (status != NFS_OK) {
2607 nfs4_put_lock_state(prev);
2610 spin_lock(&state->state_lock);
2613 spin_unlock(&state->state_lock);
2614 nfs4_put_lock_state(prev);
2620 * nfs41_check_open_stateid - possibly free an open stateid
2622 * @state: NFSv4 state for an inode
2624 * Returns NFS_OK if recovery for this stateid is now finished.
2625 * Otherwise a negative NFS4ERR value is returned.
2627 static int nfs41_check_open_stateid(struct nfs4_state *state)
2629 struct nfs_server *server = NFS_SERVER(state->inode);
2630 nfs4_stateid *stateid = &state->open_stateid;
2631 struct rpc_cred *cred = state->owner->so_cred;
2634 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
2635 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2636 if (nfs4_have_delegation(state->inode, state->state))
2638 return -NFS4ERR_OPENMODE;
2640 return -NFS4ERR_BAD_STATEID;
2642 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
2643 trace_nfs4_test_open_stateid(state, NULL, status);
2644 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
2645 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2646 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2647 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2648 clear_bit(NFS_OPEN_STATE, &state->flags);
2649 stateid->type = NFS4_INVALID_STATEID_TYPE;
2651 if (status != NFS_OK)
2653 if (nfs_open_stateid_recover_openmode(state))
2654 return -NFS4ERR_OPENMODE;
2658 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2662 nfs41_check_delegation_stateid(state);
2663 status = nfs41_check_expired_locks(state);
2664 if (status != NFS_OK)
2666 status = nfs41_check_open_stateid(state);
2667 if (status != NFS_OK)
2668 status = nfs4_open_expired(sp, state);
2674 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2675 * fields corresponding to attributes that were used to store the verifier.
2676 * Make sure we clobber those fields in the later setattr call
2678 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2679 struct iattr *sattr, struct nfs4_label **label)
2681 const u32 *attrset = opendata->o_res.attrset;
2683 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2684 !(sattr->ia_valid & ATTR_ATIME_SET))
2685 sattr->ia_valid |= ATTR_ATIME;
2687 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2688 !(sattr->ia_valid & ATTR_MTIME_SET))
2689 sattr->ia_valid |= ATTR_MTIME;
2691 /* Except MODE, it seems harmless of setting twice. */
2692 if ((attrset[1] & FATTR4_WORD1_MODE))
2693 sattr->ia_valid &= ~ATTR_MODE;
2695 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2699 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2702 struct nfs_open_context *ctx)
2704 struct nfs4_state_owner *sp = opendata->owner;
2705 struct nfs_server *server = sp->so_server;
2706 struct dentry *dentry;
2707 struct nfs4_state *state;
2711 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2713 ret = _nfs4_proc_open(opendata);
2717 state = nfs4_opendata_to_nfs4_state(opendata);
2718 ret = PTR_ERR(state);
2721 if (server->caps & NFS_CAP_POSIX_LOCK)
2722 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2723 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2724 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
2726 dentry = opendata->dentry;
2727 if (d_really_is_negative(dentry)) {
2728 struct dentry *alias;
2730 alias = d_exact_alias(dentry, state->inode);
2732 alias = d_splice_alias(igrab(state->inode), dentry);
2733 /* d_splice_alias() can't fail here - it's a non-directory */
2736 ctx->dentry = dentry = alias;
2738 nfs_set_verifier(dentry,
2739 nfs_save_change_attribute(d_inode(opendata->dir)));
2742 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2747 if (d_inode(dentry) == state->inode) {
2748 nfs_inode_attach_open_context(ctx);
2749 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2750 nfs4_schedule_stateid_recovery(server, state);
2757 * Returns a referenced nfs4_state
2759 static int _nfs4_do_open(struct inode *dir,
2760 struct nfs_open_context *ctx,
2762 struct iattr *sattr,
2763 struct nfs4_label *label,
2766 struct nfs4_state_owner *sp;
2767 struct nfs4_state *state = NULL;
2768 struct nfs_server *server = NFS_SERVER(dir);
2769 struct nfs4_opendata *opendata;
2770 struct dentry *dentry = ctx->dentry;
2771 struct rpc_cred *cred = ctx->cred;
2772 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2773 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
2774 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
2775 struct nfs4_label *olabel = NULL;
2778 /* Protect against reboot recovery conflicts */
2780 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2782 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2785 status = nfs4_recover_expired_lease(server);
2787 goto err_put_state_owner;
2788 if (d_really_is_positive(dentry))
2789 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
2791 if (d_really_is_positive(dentry))
2792 claim = NFS4_OPEN_CLAIM_FH;
2793 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
2794 label, claim, GFP_KERNEL);
2795 if (opendata == NULL)
2796 goto err_put_state_owner;
2799 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2800 if (IS_ERR(olabel)) {
2801 status = PTR_ERR(olabel);
2802 goto err_opendata_put;
2806 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2807 if (!opendata->f_attr.mdsthreshold) {
2808 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2809 if (!opendata->f_attr.mdsthreshold)
2810 goto err_free_label;
2812 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
2814 if (d_really_is_positive(dentry))
2815 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
2817 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
2819 goto err_free_label;
2822 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
2823 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
2824 nfs4_exclusive_attrset(opendata, sattr, &label);
2826 * send create attributes which was not set by open
2827 * with an extra setattr.
2829 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2830 nfs_fattr_init(opendata->o_res.f_attr);
2831 status = nfs4_do_setattr(state->inode, cred,
2832 opendata->o_res.f_attr, sattr,
2833 ctx, label, olabel);
2835 nfs_setattr_update_inode(state->inode, sattr,
2836 opendata->o_res.f_attr);
2837 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2841 if (opened && opendata->file_created)
2842 *opened |= FILE_CREATED;
2844 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
2845 *ctx_th = opendata->f_attr.mdsthreshold;
2846 opendata->f_attr.mdsthreshold = NULL;
2849 nfs4_label_free(olabel);
2851 nfs4_opendata_put(opendata);
2852 nfs4_put_state_owner(sp);
2855 nfs4_label_free(olabel);
2857 nfs4_opendata_put(opendata);
2858 err_put_state_owner:
2859 nfs4_put_state_owner(sp);
2865 static struct nfs4_state *nfs4_do_open(struct inode *dir,
2866 struct nfs_open_context *ctx,
2868 struct iattr *sattr,
2869 struct nfs4_label *label,
2872 struct nfs_server *server = NFS_SERVER(dir);
2873 struct nfs4_exception exception = { };
2874 struct nfs4_state *res;
2878 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
2880 trace_nfs4_open_file(ctx, flags, status);
2883 /* NOTE: BAD_SEQID means the server and client disagree about the
2884 * book-keeping w.r.t. state-changing operations
2885 * (OPEN/CLOSE/LOCK/LOCKU...)
2886 * It is actually a sign of a bug on the client or on the server.
2888 * If we receive a BAD_SEQID error in the particular case of
2889 * doing an OPEN, we assume that nfs_increment_open_seqid() will
2890 * have unhashed the old state_owner for us, and that we can
2891 * therefore safely retry using a new one. We should still warn
2892 * the user though...
2894 if (status == -NFS4ERR_BAD_SEQID) {
2895 pr_warn_ratelimited("NFS: v4 server %s "
2896 " returned a bad sequence-id error!\n",
2897 NFS_SERVER(dir)->nfs_client->cl_hostname);
2898 exception.retry = 1;
2902 * BAD_STATEID on OPEN means that the server cancelled our
2903 * state before it received the OPEN_CONFIRM.
2904 * Recover by retrying the request as per the discussion
2905 * on Page 181 of RFC3530.
2907 if (status == -NFS4ERR_BAD_STATEID) {
2908 exception.retry = 1;
2911 if (status == -EAGAIN) {
2912 /* We must have found a delegation */
2913 exception.retry = 1;
2916 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2918 res = ERR_PTR(nfs4_handle_exception(server,
2919 status, &exception));
2920 } while (exception.retry);
2924 static int _nfs4_do_setattr(struct inode *inode,
2925 struct nfs_setattrargs *arg,
2926 struct nfs_setattrres *res,
2927 struct rpc_cred *cred,
2928 struct nfs_open_context *ctx)
2930 struct nfs_server *server = NFS_SERVER(inode);
2931 struct rpc_message msg = {
2932 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2937 struct rpc_cred *delegation_cred = NULL;
2938 unsigned long timestamp = jiffies;
2943 nfs_fattr_init(res->fattr);
2945 /* Servers should only apply open mode checks for file size changes */
2946 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
2947 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2949 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
2950 /* Use that stateid */
2951 } else if (truncate && ctx != NULL) {
2952 struct nfs_lock_context *l_ctx;
2953 if (!nfs4_valid_open_stateid(ctx->state))
2955 l_ctx = nfs_get_lock_context(ctx);
2957 return PTR_ERR(l_ctx);
2958 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
2959 &arg->stateid, &delegation_cred);
2960 nfs_put_lock_context(l_ctx);
2964 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
2965 if (delegation_cred)
2966 msg.rpc_cred = delegation_cred;
2968 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
2970 put_rpccred(delegation_cred);
2971 if (status == 0 && ctx != NULL)
2972 renew_lease(server, timestamp);
2973 trace_nfs4_setattr(inode, &arg->stateid, status);
2977 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2978 struct nfs_fattr *fattr, struct iattr *sattr,
2979 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
2980 struct nfs4_label *olabel)
2982 struct nfs_server *server = NFS_SERVER(inode);
2983 struct nfs4_state *state = ctx ? ctx->state : NULL;
2984 struct nfs_setattrargs arg = {
2985 .fh = NFS_FH(inode),
2988 .bitmask = server->attr_bitmask,
2991 struct nfs_setattrres res = {
2996 struct nfs4_exception exception = {
2999 .stateid = &arg.stateid,
3003 arg.bitmask = nfs4_bitmask(server, ilabel);
3005 arg.bitmask = nfs4_bitmask(server, olabel);
3008 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
3010 case -NFS4ERR_OPENMODE:
3011 if (!(sattr->ia_valid & ATTR_SIZE)) {
3012 pr_warn_once("NFSv4: server %s is incorrectly "
3013 "applying open mode checks to "
3014 "a SETATTR that is not "
3015 "changing file size.\n",
3016 server->nfs_client->cl_hostname);
3018 if (state && !(state->state & FMODE_WRITE)) {
3020 if (sattr->ia_valid & ATTR_OPEN)
3025 err = nfs4_handle_exception(server, err, &exception);
3026 } while (exception.retry);
3032 nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3034 if (inode == NULL || !nfs_have_layout(inode))
3037 return pnfs_wait_on_layoutreturn(inode, task);
3040 struct nfs4_closedata {
3041 struct inode *inode;
3042 struct nfs4_state *state;
3043 struct nfs_closeargs arg;
3044 struct nfs_closeres res;
3046 struct nfs4_layoutreturn_args arg;
3047 struct nfs4_layoutreturn_res res;
3048 struct nfs4_xdr_opaque_data ld_private;
3052 struct nfs_fattr fattr;
3053 unsigned long timestamp;
3056 static void nfs4_free_closedata(void *data)
3058 struct nfs4_closedata *calldata = data;
3059 struct nfs4_state_owner *sp = calldata->state->owner;
3060 struct super_block *sb = calldata->state->inode->i_sb;
3062 if (calldata->lr.roc)
3063 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3064 calldata->res.lr_ret);
3065 nfs4_put_open_state(calldata->state);
3066 nfs_free_seqid(calldata->arg.seqid);
3067 nfs4_put_state_owner(sp);
3068 nfs_sb_deactive(sb);
3072 static void nfs4_close_done(struct rpc_task *task, void *data)
3074 struct nfs4_closedata *calldata = data;
3075 struct nfs4_state *state = calldata->state;
3076 struct nfs_server *server = NFS_SERVER(calldata->inode);
3077 nfs4_stateid *res_stateid = NULL;
3079 dprintk("%s: begin!\n", __func__);
3080 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3082 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
3084 /* Handle Layoutreturn errors */
3085 if (calldata->arg.lr_args && task->tk_status != 0) {
3086 switch (calldata->res.lr_ret) {
3088 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3091 calldata->arg.lr_args = NULL;
3092 calldata->res.lr_res = NULL;
3094 case -NFS4ERR_ADMIN_REVOKED:
3095 case -NFS4ERR_DELEG_REVOKED:
3096 case -NFS4ERR_EXPIRED:
3097 case -NFS4ERR_BAD_STATEID:
3098 case -NFS4ERR_OLD_STATEID:
3099 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3100 case -NFS4ERR_WRONG_CRED:
3101 calldata->arg.lr_args = NULL;
3102 calldata->res.lr_res = NULL;
3103 calldata->res.lr_ret = 0;
3104 rpc_restart_call_prepare(task);
3109 /* hmm. we are done with the inode, and in the process of freeing
3110 * the state_owner. we keep this around to process errors
3112 switch (task->tk_status) {
3114 res_stateid = &calldata->res.stateid;
3115 renew_lease(server, calldata->timestamp);
3117 case -NFS4ERR_ADMIN_REVOKED:
3118 case -NFS4ERR_STALE_STATEID:
3119 case -NFS4ERR_EXPIRED:
3120 nfs4_free_revoked_stateid(server,
3121 &calldata->arg.stateid,
3122 task->tk_msg.rpc_cred);
3123 case -NFS4ERR_OLD_STATEID:
3124 case -NFS4ERR_BAD_STATEID:
3125 if (!nfs4_stateid_match(&calldata->arg.stateid,
3126 &state->open_stateid)) {
3127 rpc_restart_call_prepare(task);
3130 if (calldata->arg.fmode == 0)
3133 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
3134 rpc_restart_call_prepare(task);
3138 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3139 res_stateid, calldata->arg.fmode);
3141 nfs_release_seqid(calldata->arg.seqid);
3142 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
3143 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
3146 static void nfs4_close_prepare(struct rpc_task *task, void *data)
3148 struct nfs4_closedata *calldata = data;
3149 struct nfs4_state *state = calldata->state;
3150 struct inode *inode = calldata->inode;
3151 bool is_rdonly, is_wronly, is_rdwr;
3154 dprintk("%s: begin!\n", __func__);
3155 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3158 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
3159 spin_lock(&state->owner->so_lock);
3160 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3161 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3162 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
3163 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
3164 /* Calculate the change in open mode */
3165 calldata->arg.fmode = 0;
3166 if (state->n_rdwr == 0) {
3167 if (state->n_rdonly == 0)
3168 call_close |= is_rdonly;
3170 calldata->arg.fmode |= FMODE_READ;
3171 if (state->n_wronly == 0)
3172 call_close |= is_wronly;
3174 calldata->arg.fmode |= FMODE_WRITE;
3175 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3176 call_close |= is_rdwr;
3178 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3180 if (!nfs4_valid_open_stateid(state) ||
3181 test_bit(NFS_OPEN_STATE, &state->flags) == 0)
3183 spin_unlock(&state->owner->so_lock);
3186 /* Note: exit _without_ calling nfs4_close_done */
3190 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
3191 nfs_release_seqid(calldata->arg.seqid);
3195 if (calldata->arg.fmode == 0) {
3196 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
3198 /* Close-to-open cache consistency revalidation */
3199 if (!nfs4_have_delegation(inode, FMODE_READ))
3200 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3202 calldata->arg.bitmask = NULL;
3205 calldata->arg.share_access =
3206 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3207 calldata->arg.fmode, 0);
3209 nfs_fattr_init(calldata->res.fattr);
3210 calldata->timestamp = jiffies;
3211 if (nfs4_setup_sequence(NFS_SERVER(inode),
3212 &calldata->arg.seq_args,
3213 &calldata->res.seq_res,
3215 nfs_release_seqid(calldata->arg.seqid);
3216 dprintk("%s: done!\n", __func__);
3219 task->tk_action = NULL;
3221 nfs4_sequence_done(task, &calldata->res.seq_res);
3224 static const struct rpc_call_ops nfs4_close_ops = {
3225 .rpc_call_prepare = nfs4_close_prepare,
3226 .rpc_call_done = nfs4_close_done,
3227 .rpc_release = nfs4_free_closedata,
3231 * It is possible for data to be read/written from a mem-mapped file
3232 * after the sys_close call (which hits the vfs layer as a flush).
3233 * This means that we can't safely call nfsv4 close on a file until
3234 * the inode is cleared. This in turn means that we are not good
3235 * NFSv4 citizens - we do not indicate to the server to update the file's
3236 * share state even when we are done with one of the three share
3237 * stateid's in the inode.
3239 * NOTE: Caller must be holding the sp->so_owner semaphore!
3241 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
3243 struct nfs_server *server = NFS_SERVER(state->inode);
3244 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
3245 struct nfs4_closedata *calldata;
3246 struct nfs4_state_owner *sp = state->owner;
3247 struct rpc_task *task;
3248 struct rpc_message msg = {
3249 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3250 .rpc_cred = state->owner->so_cred,
3252 struct rpc_task_setup task_setup_data = {
3253 .rpc_client = server->client,
3254 .rpc_message = &msg,
3255 .callback_ops = &nfs4_close_ops,
3256 .workqueue = nfsiod_workqueue,
3257 .flags = RPC_TASK_ASYNC,
3259 int status = -ENOMEM;
3261 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3262 &task_setup_data.rpc_client, &msg);
3264 calldata = kzalloc(sizeof(*calldata), gfp_mask);
3265 if (calldata == NULL)
3267 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
3268 calldata->inode = state->inode;
3269 calldata->state = state;
3270 calldata->arg.fh = NFS_FH(state->inode);
3271 /* Serialization for the sequence id */
3272 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3273 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
3274 if (IS_ERR(calldata->arg.seqid))
3275 goto out_free_calldata;
3276 calldata->arg.fmode = 0;
3277 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
3278 calldata->res.fattr = &calldata->fattr;
3279 calldata->res.seqid = calldata->arg.seqid;
3280 calldata->res.server = server;
3281 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3282 calldata->lr.roc = pnfs_roc(state->inode,
3283 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3284 if (calldata->lr.roc) {
3285 calldata->arg.lr_args = &calldata->lr.arg;
3286 calldata->res.lr_res = &calldata->lr.res;
3288 nfs_sb_active(calldata->inode->i_sb);
3290 msg.rpc_argp = &calldata->arg;
3291 msg.rpc_resp = &calldata->res;
3292 task_setup_data.callback_data = calldata;
3293 task = rpc_run_task(&task_setup_data);
3295 return PTR_ERR(task);
3298 status = rpc_wait_for_completion_task(task);
3304 nfs4_put_open_state(state);
3305 nfs4_put_state_owner(sp);
3309 static struct inode *
3310 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3311 int open_flags, struct iattr *attr, int *opened)
3313 struct nfs4_state *state;
3314 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3316 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
3318 /* Protect against concurrent sillydeletes */
3319 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
3321 nfs4_label_release_security(label);
3324 return ERR_CAST(state);
3325 return state->inode;
3328 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
3330 if (ctx->state == NULL)
3333 nfs4_close_sync(ctx->state, ctx->mode);
3335 nfs4_close_state(ctx->state, ctx->mode);
3338 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3339 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3340 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
3342 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3344 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
3345 struct nfs4_server_caps_arg args = {
3349 struct nfs4_server_caps_res res = {};
3350 struct rpc_message msg = {
3351 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
3357 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3358 FATTR4_WORD0_FH_EXPIRE_TYPE |
3359 FATTR4_WORD0_LINK_SUPPORT |
3360 FATTR4_WORD0_SYMLINK_SUPPORT |
3361 FATTR4_WORD0_ACLSUPPORT;
3363 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3365 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3367 /* Sanity check the server answers */
3368 switch (minorversion) {
3370 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3371 res.attr_bitmask[2] = 0;
3374 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3377 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3379 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
3380 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3381 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3382 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3383 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
3384 NFS_CAP_CTIME|NFS_CAP_MTIME|
3385 NFS_CAP_SECURITY_LABEL);
3386 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3387 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3388 server->caps |= NFS_CAP_ACLS;
3389 if (res.has_links != 0)
3390 server->caps |= NFS_CAP_HARDLINKS;
3391 if (res.has_symlinks != 0)
3392 server->caps |= NFS_CAP_SYMLINKS;
3393 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3394 server->caps |= NFS_CAP_FILEID;
3395 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3396 server->caps |= NFS_CAP_MODE;
3397 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3398 server->caps |= NFS_CAP_NLINK;
3399 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3400 server->caps |= NFS_CAP_OWNER;
3401 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3402 server->caps |= NFS_CAP_OWNER_GROUP;
3403 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3404 server->caps |= NFS_CAP_ATIME;
3405 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3406 server->caps |= NFS_CAP_CTIME;
3407 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3408 server->caps |= NFS_CAP_MTIME;
3409 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3410 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3411 server->caps |= NFS_CAP_SECURITY_LABEL;
3413 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3414 sizeof(server->attr_bitmask));
3415 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3417 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3418 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3419 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
3420 server->cache_consistency_bitmask[2] = 0;
3421 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3422 sizeof(server->exclcreat_bitmask));
3423 server->acl_bitmask = res.acl_bitmask;
3424 server->fh_expire_type = res.fh_expire_type;
3430 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3432 struct nfs4_exception exception = { };
3435 err = nfs4_handle_exception(server,
3436 _nfs4_server_capabilities(server, fhandle),
3438 } while (exception.retry);
3442 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3443 struct nfs_fsinfo *info)
3446 struct nfs4_lookup_root_arg args = {
3449 struct nfs4_lookup_res res = {
3451 .fattr = info->fattr,
3454 struct rpc_message msg = {
3455 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3460 bitmask[0] = nfs4_fattr_bitmap[0];
3461 bitmask[1] = nfs4_fattr_bitmap[1];
3463 * Process the label in the upcoming getfattr
3465 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3467 nfs_fattr_init(info->fattr);
3468 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3471 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3472 struct nfs_fsinfo *info)
3474 struct nfs4_exception exception = { };
3477 err = _nfs4_lookup_root(server, fhandle, info);
3478 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
3481 case -NFS4ERR_WRONGSEC:
3484 err = nfs4_handle_exception(server, err, &exception);
3486 } while (exception.retry);
3491 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3492 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3494 struct rpc_auth_create_args auth_args = {
3495 .pseudoflavor = flavor,
3497 struct rpc_auth *auth;
3500 auth = rpcauth_create(&auth_args, server->client);
3505 ret = nfs4_lookup_root(server, fhandle, info);
3511 * Retry pseudoroot lookup with various security flavors. We do this when:
3513 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3514 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3516 * Returns zero on success, or a negative NFS4ERR value, or a
3517 * negative errno value.
3519 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3520 struct nfs_fsinfo *info)
3522 /* Per 3530bis 15.33.5 */
3523 static const rpc_authflavor_t flav_array[] = {
3527 RPC_AUTH_UNIX, /* courtesy */
3530 int status = -EPERM;
3533 if (server->auth_info.flavor_len > 0) {
3534 /* try each flavor specified by user */
3535 for (i = 0; i < server->auth_info.flavor_len; i++) {
3536 status = nfs4_lookup_root_sec(server, fhandle, info,
3537 server->auth_info.flavors[i]);
3538 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3543 /* no flavors specified by user, try default list */
3544 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3545 status = nfs4_lookup_root_sec(server, fhandle, info,
3547 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3554 * -EACCESS could mean that the user doesn't have correct permissions
3555 * to access the mount. It could also mean that we tried to mount
3556 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3557 * existing mount programs don't handle -EACCES very well so it should
3558 * be mapped to -EPERM instead.
3560 if (status == -EACCES)
3566 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3567 * @server: initialized nfs_server handle
3568 * @fhandle: we fill in the pseudo-fs root file handle
3569 * @info: we fill in an FSINFO struct
3570 * @auth_probe: probe the auth flavours
3572 * Returns zero on success, or a negative errno.
3574 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
3575 struct nfs_fsinfo *info,
3581 status = nfs4_lookup_root(server, fhandle, info);
3583 if (auth_probe || status == NFS4ERR_WRONGSEC)
3584 status = server->nfs_client->cl_mvops->find_root_sec(server,
3588 status = nfs4_server_capabilities(server, fhandle);
3590 status = nfs4_do_fsinfo(server, fhandle, info);
3592 return nfs4_map_errors(status);
3595 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3596 struct nfs_fsinfo *info)
3599 struct nfs_fattr *fattr = info->fattr;
3600 struct nfs4_label *label = NULL;
3602 error = nfs4_server_capabilities(server, mntfh);
3604 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3608 label = nfs4_label_alloc(server, GFP_KERNEL);
3610 return PTR_ERR(label);
3612 error = nfs4_proc_getattr(server, mntfh, fattr, label);
3614 dprintk("nfs4_get_root: getattr error = %d\n", -error);
3615 goto err_free_label;
3618 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3619 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3620 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3623 nfs4_label_free(label);
3629 * Get locations and (maybe) other attributes of a referral.
3630 * Note that we'll actually follow the referral later when
3631 * we detect fsid mismatch in inode revalidation
3633 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3634 const struct qstr *name, struct nfs_fattr *fattr,
3635 struct nfs_fh *fhandle)
3637 int status = -ENOMEM;
3638 struct page *page = NULL;
3639 struct nfs4_fs_locations *locations = NULL;
3641 page = alloc_page(GFP_KERNEL);
3644 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3645 if (locations == NULL)
3648 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
3653 * If the fsid didn't change, this is a migration event, not a
3654 * referral. Cause us to drop into the exception handler, which
3655 * will kick off migration recovery.
3657 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
3658 dprintk("%s: server did not return a different fsid for"
3659 " a referral at %s\n", __func__, name->name);
3660 status = -NFS4ERR_MOVED;
3663 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3664 nfs_fixup_referral_attributes(&locations->fattr);
3666 /* replace the lookup nfs_fattr with the locations nfs_fattr */
3667 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
3668 memset(fhandle, 0, sizeof(struct nfs_fh));
3676 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3677 struct nfs_fattr *fattr, struct nfs4_label *label)
3679 struct nfs4_getattr_arg args = {
3681 .bitmask = server->attr_bitmask,
3683 struct nfs4_getattr_res res = {
3688 struct rpc_message msg = {
3689 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3694 args.bitmask = nfs4_bitmask(server, label);
3696 nfs_fattr_init(fattr);
3697 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3700 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3701 struct nfs_fattr *fattr, struct nfs4_label *label)
3703 struct nfs4_exception exception = { };
3706 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3707 trace_nfs4_getattr(server, fhandle, fattr, err);
3708 err = nfs4_handle_exception(server, err,
3710 } while (exception.retry);
3715 * The file is not closed if it is opened due to the a request to change
3716 * the size of the file. The open call will not be needed once the
3717 * VFS layer lookup-intents are implemented.
3719 * Close is called when the inode is destroyed.
3720 * If we haven't opened the file for O_WRONLY, we
3721 * need to in the size_change case to obtain a stateid.
3724 * Because OPEN is always done by name in nfsv4, it is
3725 * possible that we opened a different file by the same
3726 * name. We can recognize this race condition, but we
3727 * can't do anything about it besides returning an error.
3729 * This will be fixed with VFS changes (lookup-intent).
3732 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3733 struct iattr *sattr)
3735 struct inode *inode = d_inode(dentry);
3736 struct rpc_cred *cred = NULL;
3737 struct nfs_open_context *ctx = NULL;
3738 struct nfs4_label *label = NULL;
3741 if (pnfs_ld_layoutret_on_setattr(inode) &&
3742 sattr->ia_valid & ATTR_SIZE &&
3743 sattr->ia_size < i_size_read(inode))
3744 pnfs_commit_and_return_layout(inode);
3746 nfs_fattr_init(fattr);
3748 /* Deal with open(O_TRUNC) */
3749 if (sattr->ia_valid & ATTR_OPEN)
3750 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
3752 /* Optimization: if the end result is no change, don't RPC */
3753 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
3756 /* Search for an existing open(O_WRITE) file */
3757 if (sattr->ia_valid & ATTR_FILE) {
3759 ctx = nfs_file_open_context(sattr->ia_file);
3764 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3766 return PTR_ERR(label);
3768 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
3770 nfs_setattr_update_inode(inode, sattr, fattr);
3771 nfs_setsecurity(inode, fattr, label);
3773 nfs4_label_free(label);
3777 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3778 const struct qstr *name, struct nfs_fh *fhandle,
3779 struct nfs_fattr *fattr, struct nfs4_label *label)
3781 struct nfs_server *server = NFS_SERVER(dir);
3783 struct nfs4_lookup_arg args = {
3784 .bitmask = server->attr_bitmask,
3785 .dir_fh = NFS_FH(dir),
3788 struct nfs4_lookup_res res = {
3794 struct rpc_message msg = {
3795 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3800 args.bitmask = nfs4_bitmask(server, label);
3802 nfs_fattr_init(fattr);
3804 dprintk("NFS call lookup %s\n", name->name);
3805 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
3806 dprintk("NFS reply lookup: %d\n", status);
3810 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
3812 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
3813 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
3814 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3818 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3819 const struct qstr *name, struct nfs_fh *fhandle,
3820 struct nfs_fattr *fattr, struct nfs4_label *label)
3822 struct nfs4_exception exception = { };
3823 struct rpc_clnt *client = *clnt;
3826 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
3827 trace_nfs4_lookup(dir, name, err);
3829 case -NFS4ERR_BADNAME:
3832 case -NFS4ERR_MOVED:
3833 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
3834 if (err == -NFS4ERR_MOVED)
3835 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3837 case -NFS4ERR_WRONGSEC:
3839 if (client != *clnt)
3841 client = nfs4_negotiate_security(client, dir, name);
3843 return PTR_ERR(client);
3845 exception.retry = 1;
3848 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3850 } while (exception.retry);
3855 else if (client != *clnt)
3856 rpc_shutdown_client(client);
3861 static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
3862 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3863 struct nfs4_label *label)
3866 struct rpc_clnt *client = NFS_CLIENT(dir);
3868 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
3869 if (client != NFS_CLIENT(dir)) {
3870 rpc_shutdown_client(client);
3871 nfs_fixup_secinfo_attributes(fattr);
3877 nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
3878 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3880 struct rpc_clnt *client = NFS_CLIENT(dir);
3883 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
3885 return ERR_PTR(status);
3886 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
3889 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3891 struct nfs_server *server = NFS_SERVER(inode);
3892 struct nfs4_accessargs args = {
3893 .fh = NFS_FH(inode),
3894 .bitmask = server->cache_consistency_bitmask,
3896 struct nfs4_accessres res = {
3899 struct rpc_message msg = {
3900 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3903 .rpc_cred = entry->cred,
3905 int mode = entry->mask;
3909 * Determine which access bits we want to ask for...
3911 if (mode & MAY_READ)
3912 args.access |= NFS4_ACCESS_READ;
3913 if (S_ISDIR(inode->i_mode)) {
3914 if (mode & MAY_WRITE)
3915 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3916 if (mode & MAY_EXEC)
3917 args.access |= NFS4_ACCESS_LOOKUP;
3919 if (mode & MAY_WRITE)
3920 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3921 if (mode & MAY_EXEC)
3922 args.access |= NFS4_ACCESS_EXECUTE;
3925 res.fattr = nfs_alloc_fattr();
3926 if (res.fattr == NULL)
3929 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3931 nfs_access_set_mask(entry, res.access);
3932 nfs_refresh_inode(inode, res.fattr);
3934 nfs_free_fattr(res.fattr);
3938 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3940 struct nfs4_exception exception = { };
3943 err = _nfs4_proc_access(inode, entry);
3944 trace_nfs4_access(inode, err);
3945 err = nfs4_handle_exception(NFS_SERVER(inode), err,
3947 } while (exception.retry);
3952 * TODO: For the time being, we don't try to get any attributes
3953 * along with any of the zero-copy operations READ, READDIR,
3956 * In the case of the first three, we want to put the GETATTR
3957 * after the read-type operation -- this is because it is hard
3958 * to predict the length of a GETATTR response in v4, and thus
3959 * align the READ data correctly. This means that the GETATTR
3960 * may end up partially falling into the page cache, and we should
3961 * shift it into the 'tail' of the xdr_buf before processing.
3962 * To do this efficiently, we need to know the total length
3963 * of data received, which doesn't seem to be available outside
3966 * In the case of WRITE, we also want to put the GETATTR after
3967 * the operation -- in this case because we want to make sure
3968 * we get the post-operation mtime and size.
3970 * Both of these changes to the XDR layer would in fact be quite
3971 * minor, but I decided to leave them for a subsequent patch.
3973 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3974 unsigned int pgbase, unsigned int pglen)
3976 struct nfs4_readlink args = {
3977 .fh = NFS_FH(inode),
3982 struct nfs4_readlink_res res;
3983 struct rpc_message msg = {
3984 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3989 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
3992 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3993 unsigned int pgbase, unsigned int pglen)
3995 struct nfs4_exception exception = { };
3998 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3999 trace_nfs4_readlink(inode, err);
4000 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4002 } while (exception.retry);
4007 * This is just for mknod. open(O_CREAT) will always do ->open_context().
4010 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
4013 struct nfs4_label l, *ilabel = NULL;
4014 struct nfs_open_context *ctx;
4015 struct nfs4_state *state;
4018 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
4020 return PTR_ERR(ctx);
4022 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4024 sattr->ia_mode &= ~current_umask();
4025 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
4026 if (IS_ERR(state)) {
4027 status = PTR_ERR(state);
4031 nfs4_label_release_security(ilabel);
4032 put_nfs_open_context(ctx);
4036 static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
4038 struct nfs_server *server = NFS_SERVER(dir);
4039 struct nfs_removeargs args = {
4043 struct nfs_removeres res = {
4046 struct rpc_message msg = {
4047 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4053 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
4055 update_changeattr(dir, &res.cinfo);
4059 static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
4061 struct nfs4_exception exception = { };
4064 err = _nfs4_proc_remove(dir, name);
4065 trace_nfs4_remove(dir, name, err);
4066 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4068 } while (exception.retry);
4072 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
4074 struct nfs_server *server = NFS_SERVER(dir);
4075 struct nfs_removeargs *args = msg->rpc_argp;
4076 struct nfs_removeres *res = msg->rpc_resp;
4078 res->server = server;
4079 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
4080 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
4082 nfs_fattr_init(res->dir_attr);
4085 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4087 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb),
4088 &data->args.seq_args,
4093 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
4095 struct nfs_unlinkdata *data = task->tk_calldata;
4096 struct nfs_removeres *res = &data->res;
4098 if (!nfs4_sequence_done(task, &res->seq_res))
4100 if (nfs4_async_handle_error(task, res->server, NULL,
4101 &data->timeout) == -EAGAIN)
4103 update_changeattr(dir, &res->cinfo);
4107 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4109 struct nfs_server *server = NFS_SERVER(dir);
4110 struct nfs_renameargs *arg = msg->rpc_argp;
4111 struct nfs_renameres *res = msg->rpc_resp;
4113 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
4114 res->server = server;
4115 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
4118 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4120 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
4121 &data->args.seq_args,
4126 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4127 struct inode *new_dir)
4129 struct nfs_renamedata *data = task->tk_calldata;
4130 struct nfs_renameres *res = &data->res;
4132 if (!nfs4_sequence_done(task, &res->seq_res))
4134 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
4137 update_changeattr(old_dir, &res->old_cinfo);
4138 update_changeattr(new_dir, &res->new_cinfo);
4142 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4144 struct nfs_server *server = NFS_SERVER(inode);
4145 struct nfs4_link_arg arg = {
4146 .fh = NFS_FH(inode),
4147 .dir_fh = NFS_FH(dir),
4149 .bitmask = server->attr_bitmask,
4151 struct nfs4_link_res res = {
4155 struct rpc_message msg = {
4156 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4160 int status = -ENOMEM;
4162 res.fattr = nfs_alloc_fattr();
4163 if (res.fattr == NULL)
4166 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4167 if (IS_ERR(res.label)) {
4168 status = PTR_ERR(res.label);
4171 arg.bitmask = nfs4_bitmask(server, res.label);
4173 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4175 update_changeattr(dir, &res.cinfo);
4176 status = nfs_post_op_update_inode(inode, res.fattr);
4178 nfs_setsecurity(inode, res.fattr, res.label);
4182 nfs4_label_free(res.label);
4185 nfs_free_fattr(res.fattr);
4189 static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4191 struct nfs4_exception exception = { };
4194 err = nfs4_handle_exception(NFS_SERVER(inode),
4195 _nfs4_proc_link(inode, dir, name),
4197 } while (exception.retry);
4201 struct nfs4_createdata {
4202 struct rpc_message msg;
4203 struct nfs4_create_arg arg;
4204 struct nfs4_create_res res;
4206 struct nfs_fattr fattr;
4207 struct nfs4_label *label;
4210 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
4211 const struct qstr *name, struct iattr *sattr, u32 ftype)
4213 struct nfs4_createdata *data;
4215 data = kzalloc(sizeof(*data), GFP_KERNEL);
4217 struct nfs_server *server = NFS_SERVER(dir);
4219 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4220 if (IS_ERR(data->label))
4223 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4224 data->msg.rpc_argp = &data->arg;
4225 data->msg.rpc_resp = &data->res;
4226 data->arg.dir_fh = NFS_FH(dir);
4227 data->arg.server = server;
4228 data->arg.name = name;
4229 data->arg.attrs = sattr;
4230 data->arg.ftype = ftype;
4231 data->arg.bitmask = nfs4_bitmask(server, data->label);
4232 data->res.server = server;
4233 data->res.fh = &data->fh;
4234 data->res.fattr = &data->fattr;
4235 data->res.label = data->label;
4236 nfs_fattr_init(data->res.fattr);
4244 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4246 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
4247 &data->arg.seq_args, &data->res.seq_res, 1);
4249 update_changeattr(dir, &data->res.dir_cinfo);
4250 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
4255 static void nfs4_free_createdata(struct nfs4_createdata *data)
4257 nfs4_label_free(data->label);
4261 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4262 struct page *page, unsigned int len, struct iattr *sattr,
4263 struct nfs4_label *label)
4265 struct nfs4_createdata *data;
4266 int status = -ENAMETOOLONG;
4268 if (len > NFS4_MAXPATHLEN)
4272 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4276 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4277 data->arg.u.symlink.pages = &page;
4278 data->arg.u.symlink.len = len;
4279 data->arg.label = label;
4281 status = nfs4_do_create(dir, dentry, data);
4283 nfs4_free_createdata(data);
4288 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4289 struct page *page, unsigned int len, struct iattr *sattr)
4291 struct nfs4_exception exception = { };
4292 struct nfs4_label l, *label = NULL;
4295 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4298 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4299 trace_nfs4_symlink(dir, &dentry->d_name, err);
4300 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4302 } while (exception.retry);
4304 nfs4_label_release_security(label);
4308 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4309 struct iattr *sattr, struct nfs4_label *label)
4311 struct nfs4_createdata *data;
4312 int status = -ENOMEM;
4314 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4318 data->arg.label = label;
4319 status = nfs4_do_create(dir, dentry, data);
4321 nfs4_free_createdata(data);
4326 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4327 struct iattr *sattr)
4329 struct nfs4_exception exception = { };
4330 struct nfs4_label l, *label = NULL;
4333 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4335 sattr->ia_mode &= ~current_umask();
4337 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4338 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4339 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4341 } while (exception.retry);
4342 nfs4_label_release_security(label);
4347 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
4348 u64 cookie, struct page **pages, unsigned int count, int plus)
4350 struct inode *dir = d_inode(dentry);
4351 struct nfs4_readdir_arg args = {
4356 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
4359 struct nfs4_readdir_res res;
4360 struct rpc_message msg = {
4361 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4368 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4370 (unsigned long long)cookie);
4371 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
4372 res.pgbase = args.pgbase;
4373 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4375 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
4376 status += args.pgbase;
4379 nfs_invalidate_atime(dir);
4381 dprintk("%s: returns %d\n", __func__, status);
4385 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
4386 u64 cookie, struct page **pages, unsigned int count, int plus)
4388 struct nfs4_exception exception = { };
4391 err = _nfs4_proc_readdir(dentry, cred, cookie,
4392 pages, count, plus);
4393 trace_nfs4_readdir(d_inode(dentry), err);
4394 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
4396 } while (exception.retry);
4400 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4401 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
4403 struct nfs4_createdata *data;
4404 int mode = sattr->ia_mode;
4405 int status = -ENOMEM;
4407 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4412 data->arg.ftype = NF4FIFO;
4413 else if (S_ISBLK(mode)) {
4414 data->arg.ftype = NF4BLK;
4415 data->arg.u.device.specdata1 = MAJOR(rdev);
4416 data->arg.u.device.specdata2 = MINOR(rdev);
4418 else if (S_ISCHR(mode)) {
4419 data->arg.ftype = NF4CHR;
4420 data->arg.u.device.specdata1 = MAJOR(rdev);
4421 data->arg.u.device.specdata2 = MINOR(rdev);
4422 } else if (!S_ISSOCK(mode)) {
4427 data->arg.label = label;
4428 status = nfs4_do_create(dir, dentry, data);
4430 nfs4_free_createdata(data);
4435 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4436 struct iattr *sattr, dev_t rdev)
4438 struct nfs4_exception exception = { };
4439 struct nfs4_label l, *label = NULL;
4442 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4444 sattr->ia_mode &= ~current_umask();
4446 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4447 trace_nfs4_mknod(dir, &dentry->d_name, err);
4448 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4450 } while (exception.retry);
4452 nfs4_label_release_security(label);
4457 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4458 struct nfs_fsstat *fsstat)
4460 struct nfs4_statfs_arg args = {
4462 .bitmask = server->attr_bitmask,
4464 struct nfs4_statfs_res res = {
4467 struct rpc_message msg = {
4468 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4473 nfs_fattr_init(fsstat->fattr);
4474 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4477 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4479 struct nfs4_exception exception = { };
4482 err = nfs4_handle_exception(server,
4483 _nfs4_proc_statfs(server, fhandle, fsstat),
4485 } while (exception.retry);
4489 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4490 struct nfs_fsinfo *fsinfo)
4492 struct nfs4_fsinfo_arg args = {
4494 .bitmask = server->attr_bitmask,
4496 struct nfs4_fsinfo_res res = {
4499 struct rpc_message msg = {
4500 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4505 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4508 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4510 struct nfs4_exception exception = { };
4511 unsigned long now = jiffies;
4515 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
4516 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
4518 nfs4_set_lease_period(server->nfs_client,
4519 fsinfo->lease_time * HZ,
4523 err = nfs4_handle_exception(server, err, &exception);
4524 } while (exception.retry);
4528 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4532 nfs_fattr_init(fsinfo->fattr);
4533 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
4535 /* block layout checks this! */
4536 server->pnfs_blksize = fsinfo->blksize;
4537 set_pnfs_layoutdriver(server, fhandle, fsinfo);
4543 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4544 struct nfs_pathconf *pathconf)
4546 struct nfs4_pathconf_arg args = {
4548 .bitmask = server->attr_bitmask,
4550 struct nfs4_pathconf_res res = {
4551 .pathconf = pathconf,
4553 struct rpc_message msg = {
4554 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4559 /* None of the pathconf attributes are mandatory to implement */
4560 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4561 memset(pathconf, 0, sizeof(*pathconf));
4565 nfs_fattr_init(pathconf->fattr);
4566 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4569 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4570 struct nfs_pathconf *pathconf)
4572 struct nfs4_exception exception = { };
4576 err = nfs4_handle_exception(server,
4577 _nfs4_proc_pathconf(server, fhandle, pathconf),
4579 } while (exception.retry);
4583 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
4584 const struct nfs_open_context *ctx,
4585 const struct nfs_lock_context *l_ctx,
4588 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
4590 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4592 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4593 const struct nfs_open_context *ctx,
4594 const struct nfs_lock_context *l_ctx,
4597 nfs4_stateid current_stateid;
4599 /* If the current stateid represents a lost lock, then exit */
4600 if (nfs4_set_rw_stateid(¤t_stateid, ctx, l_ctx, fmode) == -EIO)
4602 return nfs4_stateid_match(stateid, ¤t_stateid);
4605 static bool nfs4_error_stateid_expired(int err)
4608 case -NFS4ERR_DELEG_REVOKED:
4609 case -NFS4ERR_ADMIN_REVOKED:
4610 case -NFS4ERR_BAD_STATEID:
4611 case -NFS4ERR_STALE_STATEID:
4612 case -NFS4ERR_OLD_STATEID:
4613 case -NFS4ERR_OPENMODE:
4614 case -NFS4ERR_EXPIRED:
4620 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
4622 struct nfs_server *server = NFS_SERVER(hdr->inode);
4624 trace_nfs4_read(hdr, task->tk_status);
4625 if (task->tk_status < 0) {
4626 struct nfs4_exception exception = {
4627 .inode = hdr->inode,
4628 .state = hdr->args.context->state,
4629 .stateid = &hdr->args.stateid,
4631 task->tk_status = nfs4_async_handle_exception(task,
4632 server, task->tk_status, &exception);
4633 if (exception.retry) {
4634 rpc_restart_call_prepare(task);
4639 if (task->tk_status > 0)
4640 renew_lease(server, hdr->timestamp);
4644 static bool nfs4_read_stateid_changed(struct rpc_task *task,
4645 struct nfs_pgio_args *args)
4648 if (!nfs4_error_stateid_expired(task->tk_status) ||
4649 nfs4_stateid_is_current(&args->stateid,
4654 rpc_restart_call_prepare(task);
4658 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
4661 dprintk("--> %s\n", __func__);
4663 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
4665 if (nfs4_read_stateid_changed(task, &hdr->args))
4667 if (task->tk_status > 0)
4668 nfs_invalidate_atime(hdr->inode);
4669 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4670 nfs4_read_done_cb(task, hdr);
4673 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4674 struct rpc_message *msg)
4676 hdr->timestamp = jiffies;
4677 if (!hdr->pgio_done_cb)
4678 hdr->pgio_done_cb = nfs4_read_done_cb;
4679 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
4680 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
4683 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4684 struct nfs_pgio_header *hdr)
4686 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4687 &hdr->args.seq_args,
4691 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4692 hdr->args.lock_context,
4693 hdr->rw_ops->rw_mode) == -EIO)
4695 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
4700 static int nfs4_write_done_cb(struct rpc_task *task,
4701 struct nfs_pgio_header *hdr)
4703 struct inode *inode = hdr->inode;
4705 trace_nfs4_write(hdr, task->tk_status);
4706 if (task->tk_status < 0) {
4707 struct nfs4_exception exception = {
4708 .inode = hdr->inode,
4709 .state = hdr->args.context->state,
4710 .stateid = &hdr->args.stateid,
4712 task->tk_status = nfs4_async_handle_exception(task,
4713 NFS_SERVER(inode), task->tk_status,
4715 if (exception.retry) {
4716 rpc_restart_call_prepare(task);
4720 if (task->tk_status >= 0) {
4721 renew_lease(NFS_SERVER(inode), hdr->timestamp);
4722 nfs_writeback_update_inode(hdr);
4727 static bool nfs4_write_stateid_changed(struct rpc_task *task,
4728 struct nfs_pgio_args *args)
4731 if (!nfs4_error_stateid_expired(task->tk_status) ||
4732 nfs4_stateid_is_current(&args->stateid,
4737 rpc_restart_call_prepare(task);
4741 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
4743 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
4745 if (nfs4_write_stateid_changed(task, &hdr->args))
4747 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4748 nfs4_write_done_cb(task, hdr);
4752 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
4754 /* Don't request attributes for pNFS or O_DIRECT writes */
4755 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
4757 /* Otherwise, request attributes if and only if we don't hold
4760 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
4763 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4764 struct rpc_message *msg)
4766 struct nfs_server *server = NFS_SERVER(hdr->inode);
4768 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4769 hdr->args.bitmask = NULL;
4770 hdr->res.fattr = NULL;
4772 hdr->args.bitmask = server->cache_consistency_bitmask;
4774 if (!hdr->pgio_done_cb)
4775 hdr->pgio_done_cb = nfs4_write_done_cb;
4776 hdr->res.server = server;
4777 hdr->timestamp = jiffies;
4779 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
4780 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
4783 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4785 nfs4_setup_sequence(NFS_SERVER(data->inode),
4786 &data->args.seq_args,
4791 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
4793 struct inode *inode = data->inode;
4795 trace_nfs4_commit(data, task->tk_status);
4796 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4797 NULL, NULL) == -EAGAIN) {
4798 rpc_restart_call_prepare(task);
4804 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
4806 if (!nfs4_sequence_done(task, &data->res.seq_res))
4808 return data->commit_done_cb(task, data);
4811 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
4813 struct nfs_server *server = NFS_SERVER(data->inode);
4815 if (data->commit_done_cb == NULL)
4816 data->commit_done_cb = nfs4_commit_done_cb;
4817 data->res.server = server;
4818 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
4819 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4822 struct nfs4_renewdata {
4823 struct nfs_client *client;
4824 unsigned long timestamp;
4828 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4829 * standalone procedure for queueing an asynchronous RENEW.
4831 static void nfs4_renew_release(void *calldata)
4833 struct nfs4_renewdata *data = calldata;
4834 struct nfs_client *clp = data->client;
4836 if (atomic_read(&clp->cl_count) > 1)
4837 nfs4_schedule_state_renewal(clp);
4838 nfs_put_client(clp);
4842 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
4844 struct nfs4_renewdata *data = calldata;
4845 struct nfs_client *clp = data->client;
4846 unsigned long timestamp = data->timestamp;
4848 trace_nfs4_renew_async(clp, task->tk_status);
4849 switch (task->tk_status) {
4852 case -NFS4ERR_LEASE_MOVED:
4853 nfs4_schedule_lease_moved_recovery(clp);
4856 /* Unless we're shutting down, schedule state recovery! */
4857 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4859 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
4860 nfs4_schedule_lease_recovery(clp);
4863 nfs4_schedule_path_down_recovery(clp);
4865 do_renew_lease(clp, timestamp);
4868 static const struct rpc_call_ops nfs4_renew_ops = {
4869 .rpc_call_done = nfs4_renew_done,
4870 .rpc_release = nfs4_renew_release,
4873 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
4875 struct rpc_message msg = {
4876 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4880 struct nfs4_renewdata *data;
4882 if (renew_flags == 0)
4884 if (!atomic_inc_not_zero(&clp->cl_count))
4886 data = kmalloc(sizeof(*data), GFP_NOFS);
4890 data->timestamp = jiffies;
4891 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
4892 &nfs4_renew_ops, data);
4895 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
4897 struct rpc_message msg = {
4898 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4902 unsigned long now = jiffies;
4905 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4908 do_renew_lease(clp, now);
4912 static inline int nfs4_server_supports_acls(struct nfs_server *server)
4914 return server->caps & NFS_CAP_ACLS;
4917 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4918 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
4921 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
4923 static int buf_to_pages_noslab(const void *buf, size_t buflen,
4924 struct page **pages)
4926 struct page *newpage, **spages;
4932 len = min_t(size_t, PAGE_SIZE, buflen);
4933 newpage = alloc_page(GFP_KERNEL);
4935 if (newpage == NULL)
4937 memcpy(page_address(newpage), buf, len);
4942 } while (buflen != 0);
4948 __free_page(spages[rc-1]);
4952 struct nfs4_cached_acl {
4958 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
4960 struct nfs_inode *nfsi = NFS_I(inode);
4962 spin_lock(&inode->i_lock);
4963 kfree(nfsi->nfs4_acl);
4964 nfsi->nfs4_acl = acl;
4965 spin_unlock(&inode->i_lock);
4968 static void nfs4_zap_acl_attr(struct inode *inode)
4970 nfs4_set_cached_acl(inode, NULL);
4973 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4975 struct nfs_inode *nfsi = NFS_I(inode);
4976 struct nfs4_cached_acl *acl;
4979 spin_lock(&inode->i_lock);
4980 acl = nfsi->nfs4_acl;
4983 if (buf == NULL) /* user is just asking for length */
4985 if (acl->cached == 0)
4987 ret = -ERANGE; /* see getxattr(2) man page */
4988 if (acl->len > buflen)
4990 memcpy(buf, acl->data, acl->len);
4994 spin_unlock(&inode->i_lock);
4998 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
5000 struct nfs4_cached_acl *acl;
5001 size_t buflen = sizeof(*acl) + acl_len;
5003 if (buflen <= PAGE_SIZE) {
5004 acl = kmalloc(buflen, GFP_KERNEL);
5008 _copy_from_pages(acl->data, pages, pgbase, acl_len);
5010 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5017 nfs4_set_cached_acl(inode, acl);
5021 * The getxattr API returns the required buffer length when called with a
5022 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5023 * the required buf. On a NULL buf, we send a page of data to the server
5024 * guessing that the ACL request can be serviced by a page. If so, we cache
5025 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5026 * the cache. If not so, we throw away the page, and cache the required
5027 * length. The next getxattr call will then produce another round trip to
5028 * the server, this time with the input buf of the required size.
5030 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5032 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
5033 struct nfs_getaclargs args = {
5034 .fh = NFS_FH(inode),
5038 struct nfs_getaclres res = {
5041 struct rpc_message msg = {
5042 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5046 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
5047 int ret = -ENOMEM, i;
5049 /* As long as we're doing a round trip to the server anyway,
5050 * let's be prepared for a page of acl data. */
5053 if (npages > ARRAY_SIZE(pages))
5056 for (i = 0; i < npages; i++) {
5057 pages[i] = alloc_page(GFP_KERNEL);
5062 /* for decoding across pages */
5063 res.acl_scratch = alloc_page(GFP_KERNEL);
5064 if (!res.acl_scratch)
5067 args.acl_len = npages * PAGE_SIZE;
5069 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
5070 __func__, buf, buflen, npages, args.acl_len);
5071 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5072 &msg, &args.seq_args, &res.seq_res, 0);
5076 /* Handle the case where the passed-in buffer is too short */
5077 if (res.acl_flags & NFS4_ACL_TRUNC) {
5078 /* Did the user only issue a request for the acl length? */
5084 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
5086 if (res.acl_len > buflen) {
5090 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
5095 for (i = 0; i < npages; i++)
5097 __free_page(pages[i]);
5098 if (res.acl_scratch)
5099 __free_page(res.acl_scratch);
5103 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5105 struct nfs4_exception exception = { };
5108 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
5109 trace_nfs4_get_acl(inode, ret);
5112 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5113 } while (exception.retry);
5117 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5119 struct nfs_server *server = NFS_SERVER(inode);
5122 if (!nfs4_server_supports_acls(server))
5124 ret = nfs_revalidate_inode(server, inode);
5127 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5128 nfs_zap_acl_cache(inode);
5129 ret = nfs4_read_cached_acl(inode, buf, buflen);
5131 /* -ENOENT is returned if there is no ACL or if there is an ACL
5132 * but no cached acl data, just the acl length */
5134 return nfs4_get_acl_uncached(inode, buf, buflen);
5137 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5139 struct nfs_server *server = NFS_SERVER(inode);
5140 struct page *pages[NFS4ACL_MAXPAGES];
5141 struct nfs_setaclargs arg = {
5142 .fh = NFS_FH(inode),
5146 struct nfs_setaclres res;
5147 struct rpc_message msg = {
5148 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5152 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
5155 if (!nfs4_server_supports_acls(server))
5157 if (npages > ARRAY_SIZE(pages))
5159 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
5162 nfs4_inode_return_delegation(inode);
5163 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5166 * Free each page after tx, so the only ref left is
5167 * held by the network stack
5170 put_page(pages[i-1]);
5173 * Acl update can result in inode attribute update.
5174 * so mark the attribute cache invalid.
5176 spin_lock(&inode->i_lock);
5177 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5178 spin_unlock(&inode->i_lock);
5179 nfs_access_zap_cache(inode);
5180 nfs_zap_acl_cache(inode);
5184 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5186 struct nfs4_exception exception = { };
5189 err = __nfs4_proc_set_acl(inode, buf, buflen);
5190 trace_nfs4_set_acl(inode, err);
5191 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5193 } while (exception.retry);
5197 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
5198 static int _nfs4_get_security_label(struct inode *inode, void *buf,
5201 struct nfs_server *server = NFS_SERVER(inode);
5202 struct nfs_fattr fattr;
5203 struct nfs4_label label = {0, 0, buflen, buf};
5205 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5206 struct nfs4_getattr_arg arg = {
5207 .fh = NFS_FH(inode),
5210 struct nfs4_getattr_res res = {
5215 struct rpc_message msg = {
5216 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
5222 nfs_fattr_init(&fattr);
5224 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
5227 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5229 if (buflen < label.len)
5234 static int nfs4_get_security_label(struct inode *inode, void *buf,
5237 struct nfs4_exception exception = { };
5240 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5244 err = _nfs4_get_security_label(inode, buf, buflen);
5245 trace_nfs4_get_security_label(inode, err);
5246 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5248 } while (exception.retry);
5252 static int _nfs4_do_set_security_label(struct inode *inode,
5253 struct nfs4_label *ilabel,
5254 struct nfs_fattr *fattr,
5255 struct nfs4_label *olabel)
5258 struct iattr sattr = {0};
5259 struct nfs_server *server = NFS_SERVER(inode);
5260 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5261 struct nfs_setattrargs arg = {
5262 .fh = NFS_FH(inode),
5268 struct nfs_setattrres res = {
5273 struct rpc_message msg = {
5274 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5280 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
5282 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5284 dprintk("%s failed: %d\n", __func__, status);
5289 static int nfs4_do_set_security_label(struct inode *inode,
5290 struct nfs4_label *ilabel,
5291 struct nfs_fattr *fattr,
5292 struct nfs4_label *olabel)
5294 struct nfs4_exception exception = { };
5298 err = _nfs4_do_set_security_label(inode, ilabel,
5300 trace_nfs4_set_security_label(inode, err);
5301 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5303 } while (exception.retry);
5308 nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
5310 struct nfs4_label ilabel, *olabel = NULL;
5311 struct nfs_fattr fattr;
5312 struct rpc_cred *cred;
5315 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5318 nfs_fattr_init(&fattr);
5322 ilabel.label = (char *)buf;
5323 ilabel.len = buflen;
5325 cred = rpc_lookup_cred();
5327 return PTR_ERR(cred);
5329 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5330 if (IS_ERR(olabel)) {
5331 status = -PTR_ERR(olabel);
5335 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5337 nfs_setsecurity(inode, &fattr, olabel);
5339 nfs4_label_free(olabel);
5344 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5347 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5348 nfs4_verifier *bootverf)
5352 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5353 /* An impossible timestamp guarantees this value
5354 * will never match a generated boot time. */
5355 verf[0] = cpu_to_be32(U32_MAX);
5356 verf[1] = cpu_to_be32(U32_MAX);
5358 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
5359 u64 ns = ktime_to_ns(nn->boot_time);
5361 verf[0] = cpu_to_be32(ns >> 32);
5362 verf[1] = cpu_to_be32(ns);
5364 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5368 nfs4_init_nonuniform_client_string(struct nfs_client *clp)
5373 if (clp->cl_owner_id != NULL)
5377 len = 14 + strlen(clp->cl_ipaddr) + 1 +
5378 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5380 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5384 if (len > NFS4_OPAQUE_LIMIT + 1)
5388 * Since this string is allocated at mount time, and held until the
5389 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5390 * about a memory-reclaim deadlock.
5392 str = kmalloc(len, GFP_KERNEL);
5397 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
5399 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5400 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
5403 clp->cl_owner_id = str;
5408 nfs4_init_uniquifier_client_string(struct nfs_client *clp)
5413 len = 10 + 10 + 1 + 10 + 1 +
5414 strlen(nfs4_client_id_uniquifier) + 1 +
5415 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5417 if (len > NFS4_OPAQUE_LIMIT + 1)
5421 * Since this string is allocated at mount time, and held until the
5422 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5423 * about a memory-reclaim deadlock.
5425 str = kmalloc(len, GFP_KERNEL);
5429 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
5430 clp->rpc_ops->version, clp->cl_minorversion,
5431 nfs4_client_id_uniquifier,
5432 clp->cl_rpcclient->cl_nodename);
5433 clp->cl_owner_id = str;
5438 nfs4_init_uniform_client_string(struct nfs_client *clp)
5443 if (clp->cl_owner_id != NULL)
5446 if (nfs4_client_id_uniquifier[0] != '\0')
5447 return nfs4_init_uniquifier_client_string(clp);
5449 len = 10 + 10 + 1 + 10 + 1 +
5450 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5452 if (len > NFS4_OPAQUE_LIMIT + 1)
5456 * Since this string is allocated at mount time, and held until the
5457 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5458 * about a memory-reclaim deadlock.
5460 str = kmalloc(len, GFP_KERNEL);
5464 scnprintf(str, len, "Linux NFSv%u.%u %s",
5465 clp->rpc_ops->version, clp->cl_minorversion,
5466 clp->cl_rpcclient->cl_nodename);
5467 clp->cl_owner_id = str;
5472 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5473 * services. Advertise one based on the address family of the
5477 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5479 if (strchr(clp->cl_ipaddr, ':') != NULL)
5480 return scnprintf(buf, len, "tcp6");
5482 return scnprintf(buf, len, "tcp");
5485 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5487 struct nfs4_setclientid *sc = calldata;
5489 if (task->tk_status == 0)
5490 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5493 static const struct rpc_call_ops nfs4_setclientid_ops = {
5494 .rpc_call_done = nfs4_setclientid_done,
5498 * nfs4_proc_setclientid - Negotiate client ID
5499 * @clp: state data structure
5500 * @program: RPC program for NFSv4 callback service
5501 * @port: IP port number for NFS4 callback service
5502 * @cred: RPC credential to use for this call
5503 * @res: where to place the result
5505 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5507 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5508 unsigned short port, struct rpc_cred *cred,
5509 struct nfs4_setclientid_res *res)
5511 nfs4_verifier sc_verifier;
5512 struct nfs4_setclientid setclientid = {
5513 .sc_verifier = &sc_verifier,
5517 struct rpc_message msg = {
5518 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5519 .rpc_argp = &setclientid,
5523 struct rpc_task *task;
5524 struct rpc_task_setup task_setup_data = {
5525 .rpc_client = clp->cl_rpcclient,
5526 .rpc_message = &msg,
5527 .callback_ops = &nfs4_setclientid_ops,
5528 .callback_data = &setclientid,
5529 .flags = RPC_TASK_TIMEOUT,
5533 /* nfs_client_id4 */
5534 nfs4_init_boot_verifier(clp, &sc_verifier);
5536 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5537 status = nfs4_init_uniform_client_string(clp);
5539 status = nfs4_init_nonuniform_client_string(clp);
5545 setclientid.sc_netid_len =
5546 nfs4_init_callback_netid(clp,
5547 setclientid.sc_netid,
5548 sizeof(setclientid.sc_netid));
5549 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
5550 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
5551 clp->cl_ipaddr, port >> 8, port & 255);
5553 dprintk("NFS call setclientid auth=%s, '%s'\n",
5554 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5556 task = rpc_run_task(&task_setup_data);
5558 status = PTR_ERR(task);
5561 status = task->tk_status;
5562 if (setclientid.sc_cred) {
5563 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5564 put_rpccred(setclientid.sc_cred);
5568 trace_nfs4_setclientid(clp, status);
5569 dprintk("NFS reply setclientid: %d\n", status);
5574 * nfs4_proc_setclientid_confirm - Confirm client ID
5575 * @clp: state data structure
5576 * @res: result of a previous SETCLIENTID
5577 * @cred: RPC credential to use for this call
5579 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5581 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
5582 struct nfs4_setclientid_res *arg,
5583 struct rpc_cred *cred)
5585 struct rpc_message msg = {
5586 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
5592 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5593 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5595 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5596 trace_nfs4_setclientid_confirm(clp, status);
5597 dprintk("NFS reply setclientid_confirm: %d\n", status);
5601 struct nfs4_delegreturndata {
5602 struct nfs4_delegreturnargs args;
5603 struct nfs4_delegreturnres res;
5605 nfs4_stateid stateid;
5606 unsigned long timestamp;
5608 struct nfs4_layoutreturn_args arg;
5609 struct nfs4_layoutreturn_res res;
5610 struct nfs4_xdr_opaque_data ld_private;
5614 struct nfs_fattr fattr;
5616 struct inode *inode;
5619 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5621 struct nfs4_delegreturndata *data = calldata;
5623 if (!nfs4_sequence_done(task, &data->res.seq_res))
5626 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
5628 /* Handle Layoutreturn errors */
5629 if (data->args.lr_args && task->tk_status != 0) {
5630 switch(data->res.lr_ret) {
5632 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5635 data->args.lr_args = NULL;
5636 data->res.lr_res = NULL;
5638 case -NFS4ERR_ADMIN_REVOKED:
5639 case -NFS4ERR_DELEG_REVOKED:
5640 case -NFS4ERR_EXPIRED:
5641 case -NFS4ERR_BAD_STATEID:
5642 case -NFS4ERR_OLD_STATEID:
5643 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
5644 case -NFS4ERR_WRONG_CRED:
5645 data->args.lr_args = NULL;
5646 data->res.lr_res = NULL;
5647 data->res.lr_ret = 0;
5648 rpc_restart_call_prepare(task);
5653 switch (task->tk_status) {
5655 renew_lease(data->res.server, data->timestamp);
5657 case -NFS4ERR_ADMIN_REVOKED:
5658 case -NFS4ERR_DELEG_REVOKED:
5659 case -NFS4ERR_EXPIRED:
5660 nfs4_free_revoked_stateid(data->res.server,
5662 task->tk_msg.rpc_cred);
5663 case -NFS4ERR_BAD_STATEID:
5664 case -NFS4ERR_OLD_STATEID:
5665 case -NFS4ERR_STALE_STATEID:
5666 task->tk_status = 0;
5669 if (nfs4_async_handle_error(task, data->res.server,
5670 NULL, NULL) == -EAGAIN) {
5671 rpc_restart_call_prepare(task);
5675 data->rpc_status = task->tk_status;
5678 static void nfs4_delegreturn_release(void *calldata)
5680 struct nfs4_delegreturndata *data = calldata;
5681 struct inode *inode = data->inode;
5685 pnfs_roc_release(&data->lr.arg, &data->lr.res,
5687 nfs_iput_and_deactive(inode);
5692 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5694 struct nfs4_delegreturndata *d_data;
5696 d_data = (struct nfs4_delegreturndata *)data;
5698 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
5701 nfs4_setup_sequence(d_data->res.server,
5702 &d_data->args.seq_args,
5703 &d_data->res.seq_res,
5707 static const struct rpc_call_ops nfs4_delegreturn_ops = {
5708 .rpc_call_prepare = nfs4_delegreturn_prepare,
5709 .rpc_call_done = nfs4_delegreturn_done,
5710 .rpc_release = nfs4_delegreturn_release,
5713 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
5715 struct nfs4_delegreturndata *data;
5716 struct nfs_server *server = NFS_SERVER(inode);
5717 struct rpc_task *task;
5718 struct rpc_message msg = {
5719 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5722 struct rpc_task_setup task_setup_data = {
5723 .rpc_client = server->client,
5724 .rpc_message = &msg,
5725 .callback_ops = &nfs4_delegreturn_ops,
5726 .flags = RPC_TASK_ASYNC,
5730 data = kzalloc(sizeof(*data), GFP_NOFS);
5733 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
5735 nfs4_state_protect(server->nfs_client,
5736 NFS_SP4_MACH_CRED_CLEANUP,
5737 &task_setup_data.rpc_client, &msg);
5739 data->args.fhandle = &data->fh;
5740 data->args.stateid = &data->stateid;
5741 data->args.bitmask = server->cache_consistency_bitmask;
5742 nfs_copy_fh(&data->fh, NFS_FH(inode));
5743 nfs4_stateid_copy(&data->stateid, stateid);
5744 data->res.fattr = &data->fattr;
5745 data->res.server = server;
5746 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5747 data->lr.arg.ld_private = &data->lr.ld_private;
5748 nfs_fattr_init(data->res.fattr);
5749 data->timestamp = jiffies;
5750 data->rpc_status = 0;
5751 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
5752 data->inode = nfs_igrab_and_active(inode);
5755 data->args.lr_args = &data->lr.arg;
5756 data->res.lr_res = &data->lr.res;
5758 } else if (data->lr.roc) {
5759 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
5760 data->lr.roc = false;
5763 task_setup_data.callback_data = data;
5764 msg.rpc_argp = &data->args;
5765 msg.rpc_resp = &data->res;
5766 task = rpc_run_task(&task_setup_data);
5768 return PTR_ERR(task);
5771 status = nfs4_wait_for_completion_rpc_task(task);
5774 status = data->rpc_status;
5776 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5778 nfs_refresh_inode(inode, &data->fattr);
5784 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
5786 struct nfs_server *server = NFS_SERVER(inode);
5787 struct nfs4_exception exception = { };
5790 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
5791 trace_nfs4_delegreturn(inode, stateid, err);
5793 case -NFS4ERR_STALE_STATEID:
5794 case -NFS4ERR_EXPIRED:
5798 err = nfs4_handle_exception(server, err, &exception);
5799 } while (exception.retry);
5803 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5805 struct inode *inode = state->inode;
5806 struct nfs_server *server = NFS_SERVER(inode);
5807 struct nfs_client *clp = server->nfs_client;
5808 struct nfs_lockt_args arg = {
5809 .fh = NFS_FH(inode),
5812 struct nfs_lockt_res res = {
5815 struct rpc_message msg = {
5816 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5819 .rpc_cred = state->owner->so_cred,
5821 struct nfs4_lock_state *lsp;
5824 arg.lock_owner.clientid = clp->cl_clientid;
5825 status = nfs4_set_lock_state(state, request);
5828 lsp = request->fl_u.nfs4_fl.owner;
5829 arg.lock_owner.id = lsp->ls_seqid.owner_id;
5830 arg.lock_owner.s_dev = server->s_dev;
5831 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5834 request->fl_type = F_UNLCK;
5836 case -NFS4ERR_DENIED:
5839 request->fl_ops->fl_release_private(request);
5840 request->fl_ops = NULL;
5845 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5847 struct nfs4_exception exception = { };
5851 err = _nfs4_proc_getlk(state, cmd, request);
5852 trace_nfs4_get_lock(request, state, cmd, err);
5853 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
5855 } while (exception.retry);
5859 struct nfs4_unlockdata {
5860 struct nfs_locku_args arg;
5861 struct nfs_locku_res res;
5862 struct nfs4_lock_state *lsp;
5863 struct nfs_open_context *ctx;
5864 struct file_lock fl;
5865 struct nfs_server *server;
5866 unsigned long timestamp;
5869 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5870 struct nfs_open_context *ctx,
5871 struct nfs4_lock_state *lsp,
5872 struct nfs_seqid *seqid)
5874 struct nfs4_unlockdata *p;
5875 struct inode *inode = lsp->ls_state->inode;
5877 p = kzalloc(sizeof(*p), GFP_NOFS);
5880 p->arg.fh = NFS_FH(inode);
5882 p->arg.seqid = seqid;
5883 p->res.seqid = seqid;
5885 atomic_inc(&lsp->ls_count);
5886 /* Ensure we don't close file until we're done freeing locks! */
5887 p->ctx = get_nfs_open_context(ctx);
5888 memcpy(&p->fl, fl, sizeof(p->fl));
5889 p->server = NFS_SERVER(inode);
5893 static void nfs4_locku_release_calldata(void *data)
5895 struct nfs4_unlockdata *calldata = data;
5896 nfs_free_seqid(calldata->arg.seqid);
5897 nfs4_put_lock_state(calldata->lsp);
5898 put_nfs_open_context(calldata->ctx);
5902 static void nfs4_locku_done(struct rpc_task *task, void *data)
5904 struct nfs4_unlockdata *calldata = data;
5906 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5908 switch (task->tk_status) {
5910 renew_lease(calldata->server, calldata->timestamp);
5911 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
5912 if (nfs4_update_lock_stateid(calldata->lsp,
5913 &calldata->res.stateid))
5915 case -NFS4ERR_ADMIN_REVOKED:
5916 case -NFS4ERR_EXPIRED:
5917 nfs4_free_revoked_stateid(calldata->server,
5918 &calldata->arg.stateid,
5919 task->tk_msg.rpc_cred);
5920 case -NFS4ERR_BAD_STATEID:
5921 case -NFS4ERR_OLD_STATEID:
5922 case -NFS4ERR_STALE_STATEID:
5923 if (!nfs4_stateid_match(&calldata->arg.stateid,
5924 &calldata->lsp->ls_stateid))
5925 rpc_restart_call_prepare(task);
5928 if (nfs4_async_handle_error(task, calldata->server,
5929 NULL, NULL) == -EAGAIN)
5930 rpc_restart_call_prepare(task);
5932 nfs_release_seqid(calldata->arg.seqid);
5935 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
5937 struct nfs4_unlockdata *calldata = data;
5939 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
5941 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
5942 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
5943 /* Note: exit _without_ running nfs4_locku_done */
5946 calldata->timestamp = jiffies;
5947 if (nfs4_setup_sequence(calldata->server,
5948 &calldata->arg.seq_args,
5949 &calldata->res.seq_res,
5951 nfs_release_seqid(calldata->arg.seqid);
5954 task->tk_action = NULL;
5956 nfs4_sequence_done(task, &calldata->res.seq_res);
5959 static const struct rpc_call_ops nfs4_locku_ops = {
5960 .rpc_call_prepare = nfs4_locku_prepare,
5961 .rpc_call_done = nfs4_locku_done,
5962 .rpc_release = nfs4_locku_release_calldata,
5965 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5966 struct nfs_open_context *ctx,
5967 struct nfs4_lock_state *lsp,
5968 struct nfs_seqid *seqid)
5970 struct nfs4_unlockdata *data;
5971 struct rpc_message msg = {
5972 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5973 .rpc_cred = ctx->cred,
5975 struct rpc_task_setup task_setup_data = {
5976 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
5977 .rpc_message = &msg,
5978 .callback_ops = &nfs4_locku_ops,
5979 .workqueue = nfsiod_workqueue,
5980 .flags = RPC_TASK_ASYNC,
5983 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5984 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5986 /* Ensure this is an unlock - when canceling a lock, the
5987 * canceled lock is passed in, and it won't be an unlock.
5989 fl->fl_type = F_UNLCK;
5991 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5993 nfs_free_seqid(seqid);
5994 return ERR_PTR(-ENOMEM);
5997 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
5998 msg.rpc_argp = &data->arg;
5999 msg.rpc_resp = &data->res;
6000 task_setup_data.callback_data = data;
6001 return rpc_run_task(&task_setup_data);
6004 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6006 struct inode *inode = state->inode;
6007 struct nfs4_state_owner *sp = state->owner;
6008 struct nfs_inode *nfsi = NFS_I(inode);
6009 struct nfs_seqid *seqid;
6010 struct nfs4_lock_state *lsp;
6011 struct rpc_task *task;
6012 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6014 unsigned char fl_flags = request->fl_flags;
6016 status = nfs4_set_lock_state(state, request);
6017 /* Unlock _before_ we do the RPC call */
6018 request->fl_flags |= FL_EXISTS;
6019 /* Exclude nfs_delegation_claim_locks() */
6020 mutex_lock(&sp->so_delegreturn_mutex);
6021 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
6022 down_read(&nfsi->rwsem);
6023 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
6024 up_read(&nfsi->rwsem);
6025 mutex_unlock(&sp->so_delegreturn_mutex);
6028 up_read(&nfsi->rwsem);
6029 mutex_unlock(&sp->so_delegreturn_mutex);
6032 /* Is this a delegated lock? */
6033 lsp = request->fl_u.nfs4_fl.owner;
6034 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6036 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6037 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
6041 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
6042 status = PTR_ERR(task);
6045 status = nfs4_wait_for_completion_rpc_task(task);
6048 request->fl_flags = fl_flags;
6049 trace_nfs4_unlock(request, state, F_SETLK, status);
6053 struct nfs4_lockdata {
6054 struct nfs_lock_args arg;
6055 struct nfs_lock_res res;
6056 struct nfs4_lock_state *lsp;
6057 struct nfs_open_context *ctx;
6058 struct file_lock fl;
6059 unsigned long timestamp;
6062 struct nfs_server *server;
6065 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
6066 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6069 struct nfs4_lockdata *p;
6070 struct inode *inode = lsp->ls_state->inode;
6071 struct nfs_server *server = NFS_SERVER(inode);
6072 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6074 p = kzalloc(sizeof(*p), gfp_mask);
6078 p->arg.fh = NFS_FH(inode);
6080 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
6081 if (IS_ERR(p->arg.open_seqid))
6083 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6084 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
6085 if (IS_ERR(p->arg.lock_seqid))
6086 goto out_free_seqid;
6087 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
6088 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
6089 p->arg.lock_owner.s_dev = server->s_dev;
6090 p->res.lock_seqid = p->arg.lock_seqid;
6093 atomic_inc(&lsp->ls_count);
6094 p->ctx = get_nfs_open_context(ctx);
6095 get_file(fl->fl_file);
6096 memcpy(&p->fl, fl, sizeof(p->fl));
6099 nfs_free_seqid(p->arg.open_seqid);
6105 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6107 struct nfs4_lockdata *data = calldata;
6108 struct nfs4_state *state = data->lsp->ls_state;
6110 dprintk("%s: begin!\n", __func__);
6111 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
6113 /* Do we need to do an open_to_lock_owner? */
6114 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
6115 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
6116 goto out_release_lock_seqid;
6118 nfs4_stateid_copy(&data->arg.open_stateid,
6119 &state->open_stateid);
6120 data->arg.new_lock_owner = 1;
6121 data->res.open_seqid = data->arg.open_seqid;
6123 data->arg.new_lock_owner = 0;
6124 nfs4_stateid_copy(&data->arg.lock_stateid,
6125 &data->lsp->ls_stateid);
6127 if (!nfs4_valid_open_stateid(state)) {
6128 data->rpc_status = -EBADF;
6129 task->tk_action = NULL;
6130 goto out_release_open_seqid;
6132 data->timestamp = jiffies;
6133 if (nfs4_setup_sequence(data->server,
6134 &data->arg.seq_args,
6138 out_release_open_seqid:
6139 nfs_release_seqid(data->arg.open_seqid);
6140 out_release_lock_seqid:
6141 nfs_release_seqid(data->arg.lock_seqid);
6143 nfs4_sequence_done(task, &data->res.seq_res);
6144 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
6147 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6149 struct nfs4_lockdata *data = calldata;
6150 struct nfs4_lock_state *lsp = data->lsp;
6152 dprintk("%s: begin!\n", __func__);
6154 if (!nfs4_sequence_done(task, &data->res.seq_res))
6157 data->rpc_status = task->tk_status;
6158 switch (task->tk_status) {
6160 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
6162 if (data->arg.new_lock) {
6163 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
6164 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
6165 rpc_restart_call_prepare(task);
6169 if (data->arg.new_lock_owner != 0) {
6170 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6171 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6172 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6173 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6174 rpc_restart_call_prepare(task);
6176 case -NFS4ERR_BAD_STATEID:
6177 case -NFS4ERR_OLD_STATEID:
6178 case -NFS4ERR_STALE_STATEID:
6179 case -NFS4ERR_EXPIRED:
6180 if (data->arg.new_lock_owner != 0) {
6181 if (!nfs4_stateid_match(&data->arg.open_stateid,
6182 &lsp->ls_state->open_stateid))
6183 rpc_restart_call_prepare(task);
6184 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6186 rpc_restart_call_prepare(task);
6188 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
6191 static void nfs4_lock_release(void *calldata)
6193 struct nfs4_lockdata *data = calldata;
6195 dprintk("%s: begin!\n", __func__);
6196 nfs_free_seqid(data->arg.open_seqid);
6197 if (data->cancelled != 0) {
6198 struct rpc_task *task;
6199 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6200 data->arg.lock_seqid);
6202 rpc_put_task_async(task);
6203 dprintk("%s: cancelling lock!\n", __func__);
6205 nfs_free_seqid(data->arg.lock_seqid);
6206 nfs4_put_lock_state(data->lsp);
6207 put_nfs_open_context(data->ctx);
6208 fput(data->fl.fl_file);
6210 dprintk("%s: done!\n", __func__);
6213 static const struct rpc_call_ops nfs4_lock_ops = {
6214 .rpc_call_prepare = nfs4_lock_prepare,
6215 .rpc_call_done = nfs4_lock_done,
6216 .rpc_release = nfs4_lock_release,
6219 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6222 case -NFS4ERR_ADMIN_REVOKED:
6223 case -NFS4ERR_EXPIRED:
6224 case -NFS4ERR_BAD_STATEID:
6225 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6226 if (new_lock_owner != 0 ||
6227 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
6228 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
6230 case -NFS4ERR_STALE_STATEID:
6231 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6232 nfs4_schedule_lease_recovery(server->nfs_client);
6236 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
6238 struct nfs4_lockdata *data;
6239 struct rpc_task *task;
6240 struct rpc_message msg = {
6241 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6242 .rpc_cred = state->owner->so_cred,
6244 struct rpc_task_setup task_setup_data = {
6245 .rpc_client = NFS_CLIENT(state->inode),
6246 .rpc_message = &msg,
6247 .callback_ops = &nfs4_lock_ops,
6248 .workqueue = nfsiod_workqueue,
6249 .flags = RPC_TASK_ASYNC,
6253 dprintk("%s: begin!\n", __func__);
6254 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
6255 fl->fl_u.nfs4_fl.owner,
6256 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
6260 data->arg.block = 1;
6261 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
6262 msg.rpc_argp = &data->arg;
6263 msg.rpc_resp = &data->res;
6264 task_setup_data.callback_data = data;
6265 if (recovery_type > NFS_LOCK_NEW) {
6266 if (recovery_type == NFS_LOCK_RECLAIM)
6267 data->arg.reclaim = NFS_LOCK_RECLAIM;
6268 nfs4_set_sequence_privileged(&data->arg.seq_args);
6270 data->arg.new_lock = 1;
6271 task = rpc_run_task(&task_setup_data);
6273 return PTR_ERR(task);
6274 ret = nfs4_wait_for_completion_rpc_task(task);
6276 ret = data->rpc_status;
6278 nfs4_handle_setlk_error(data->server, data->lsp,
6279 data->arg.new_lock_owner, ret);
6281 data->cancelled = 1;
6283 dprintk("%s: done, ret = %d!\n", __func__, ret);
6284 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
6288 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6290 struct nfs_server *server = NFS_SERVER(state->inode);
6291 struct nfs4_exception exception = {
6292 .inode = state->inode,
6297 /* Cache the lock if possible... */
6298 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6300 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
6301 if (err != -NFS4ERR_DELAY)
6303 nfs4_handle_exception(server, err, &exception);
6304 } while (exception.retry);
6308 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6310 struct nfs_server *server = NFS_SERVER(state->inode);
6311 struct nfs4_exception exception = {
6312 .inode = state->inode,
6316 err = nfs4_set_lock_state(state, request);
6319 if (!recover_lost_locks) {
6320 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6324 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6326 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
6330 case -NFS4ERR_GRACE:
6331 case -NFS4ERR_DELAY:
6332 nfs4_handle_exception(server, err, &exception);
6335 } while (exception.retry);
6340 #if defined(CONFIG_NFS_V4_1)
6341 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6343 struct nfs4_lock_state *lsp;
6346 status = nfs4_set_lock_state(state, request);
6349 lsp = request->fl_u.nfs4_fl.owner;
6350 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6351 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6353 status = nfs4_lock_expired(state, request);
6358 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6360 struct nfs_inode *nfsi = NFS_I(state->inode);
6361 struct nfs4_state_owner *sp = state->owner;
6362 unsigned char fl_flags = request->fl_flags;
6365 request->fl_flags |= FL_ACCESS;
6366 status = locks_lock_inode_wait(state->inode, request);
6369 mutex_lock(&sp->so_delegreturn_mutex);
6370 down_read(&nfsi->rwsem);
6371 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
6372 /* Yes: cache locks! */
6373 /* ...but avoid races with delegation recall... */
6374 request->fl_flags = fl_flags & ~FL_SLEEP;
6375 status = locks_lock_inode_wait(state->inode, request);
6376 up_read(&nfsi->rwsem);
6377 mutex_unlock(&sp->so_delegreturn_mutex);
6380 up_read(&nfsi->rwsem);
6381 mutex_unlock(&sp->so_delegreturn_mutex);
6382 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
6384 request->fl_flags = fl_flags;
6388 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6390 struct nfs4_exception exception = {
6392 .inode = state->inode,
6397 err = _nfs4_proc_setlk(state, cmd, request);
6398 if (err == -NFS4ERR_DENIED)
6400 err = nfs4_handle_exception(NFS_SERVER(state->inode),
6402 } while (exception.retry);
6406 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6407 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6410 nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6411 struct file_lock *request)
6413 int status = -ERESTARTSYS;
6414 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6416 while(!signalled()) {
6417 status = nfs4_proc_setlk(state, cmd, request);
6418 if ((status != -EAGAIN) || IS_SETLK(cmd))
6420 freezable_schedule_timeout_interruptible(timeout);
6422 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6423 status = -ERESTARTSYS;
6428 #ifdef CONFIG_NFS_V4_1
6429 struct nfs4_lock_waiter {
6430 struct task_struct *task;
6431 struct inode *inode;
6432 struct nfs_lowner *owner;
6437 nfs4_wake_lock_waiter(wait_queue_t *wait, unsigned int mode, int flags, void *key)
6440 struct cb_notify_lock_args *cbnl = key;
6441 struct nfs4_lock_waiter *waiter = wait->private;
6442 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6443 *wowner = waiter->owner;
6445 /* Only wake if the callback was for the same owner */
6446 if (lowner->clientid != wowner->clientid ||
6447 lowner->id != wowner->id ||
6448 lowner->s_dev != wowner->s_dev)
6451 /* Make sure it's for the right inode */
6452 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6455 waiter->notified = true;
6457 /* override "private" so we can use default_wake_function */
6458 wait->private = waiter->task;
6459 ret = autoremove_wake_function(wait, mode, flags, key);
6460 wait->private = waiter;
6465 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6467 int status = -ERESTARTSYS;
6468 unsigned long flags;
6469 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6470 struct nfs_server *server = NFS_SERVER(state->inode);
6471 struct nfs_client *clp = server->nfs_client;
6472 wait_queue_head_t *q = &clp->cl_lock_waitq;
6473 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6474 .id = lsp->ls_seqid.owner_id,
6475 .s_dev = server->s_dev };
6476 struct nfs4_lock_waiter waiter = { .task = current,
6477 .inode = state->inode,
6479 .notified = false };
6482 /* Don't bother with waitqueue if we don't expect a callback */
6483 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6484 return nfs4_retry_setlk_simple(state, cmd, request);
6487 wait.private = &waiter;
6488 wait.func = nfs4_wake_lock_waiter;
6489 add_wait_queue(q, &wait);
6491 while(!signalled()) {
6492 status = nfs4_proc_setlk(state, cmd, request);
6493 if ((status != -EAGAIN) || IS_SETLK(cmd))
6496 status = -ERESTARTSYS;
6497 spin_lock_irqsave(&q->lock, flags);
6498 if (waiter.notified) {
6499 spin_unlock_irqrestore(&q->lock, flags);
6502 set_current_state(TASK_INTERRUPTIBLE);
6503 spin_unlock_irqrestore(&q->lock, flags);
6505 freezable_schedule_timeout_interruptible(NFS4_LOCK_MAXTIMEOUT);
6508 finish_wait(q, &wait);
6511 #else /* !CONFIG_NFS_V4_1 */
6513 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6515 return nfs4_retry_setlk_simple(state, cmd, request);
6520 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6522 struct nfs_open_context *ctx;
6523 struct nfs4_state *state;
6526 /* verify open state */
6527 ctx = nfs_file_open_context(filp);
6530 if (request->fl_start < 0 || request->fl_end < 0)
6533 if (IS_GETLK(cmd)) {
6535 return nfs4_proc_getlk(state, F_GETLK, request);
6539 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6542 if (request->fl_type == F_UNLCK) {
6544 return nfs4_proc_unlck(state, cmd, request);
6551 if ((request->fl_flags & FL_POSIX) &&
6552 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6556 * Don't rely on the VFS having checked the file open mode,
6557 * since it won't do this for flock() locks.
6559 switch (request->fl_type) {
6561 if (!(filp->f_mode & FMODE_READ))
6565 if (!(filp->f_mode & FMODE_WRITE))
6569 status = nfs4_set_lock_state(state, request);
6573 return nfs4_retry_setlk(state, cmd, request);
6576 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
6578 struct nfs_server *server = NFS_SERVER(state->inode);
6581 err = nfs4_set_lock_state(state, fl);
6584 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
6585 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
6588 struct nfs_release_lockowner_data {
6589 struct nfs4_lock_state *lsp;
6590 struct nfs_server *server;
6591 struct nfs_release_lockowner_args args;
6592 struct nfs_release_lockowner_res res;
6593 unsigned long timestamp;
6596 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6598 struct nfs_release_lockowner_data *data = calldata;
6599 struct nfs_server *server = data->server;
6600 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
6601 &data->args.seq_args, &data->res.seq_res, task);
6602 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6603 data->timestamp = jiffies;
6606 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6608 struct nfs_release_lockowner_data *data = calldata;
6609 struct nfs_server *server = data->server;
6611 nfs40_sequence_done(task, &data->res.seq_res);
6613 switch (task->tk_status) {
6615 renew_lease(server, data->timestamp);
6617 case -NFS4ERR_STALE_CLIENTID:
6618 case -NFS4ERR_EXPIRED:
6619 nfs4_schedule_lease_recovery(server->nfs_client);
6621 case -NFS4ERR_LEASE_MOVED:
6622 case -NFS4ERR_DELAY:
6623 if (nfs4_async_handle_error(task, server,
6624 NULL, NULL) == -EAGAIN)
6625 rpc_restart_call_prepare(task);
6629 static void nfs4_release_lockowner_release(void *calldata)
6631 struct nfs_release_lockowner_data *data = calldata;
6632 nfs4_free_lock_state(data->server, data->lsp);
6636 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
6637 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6638 .rpc_call_done = nfs4_release_lockowner_done,
6639 .rpc_release = nfs4_release_lockowner_release,
6643 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
6645 struct nfs_release_lockowner_data *data;
6646 struct rpc_message msg = {
6647 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6650 if (server->nfs_client->cl_mvops->minor_version != 0)
6653 data = kmalloc(sizeof(*data), GFP_NOFS);
6657 data->server = server;
6658 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6659 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6660 data->args.lock_owner.s_dev = server->s_dev;
6662 msg.rpc_argp = &data->args;
6663 msg.rpc_resp = &data->res;
6664 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
6665 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
6668 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6670 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
6671 struct dentry *unused, struct inode *inode,
6672 const char *key, const void *buf,
6673 size_t buflen, int flags)
6675 return nfs4_proc_set_acl(inode, buf, buflen);
6678 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
6679 struct dentry *unused, struct inode *inode,
6680 const char *key, void *buf, size_t buflen)
6682 return nfs4_proc_get_acl(inode, buf, buflen);
6685 static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
6687 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
6690 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
6692 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
6693 struct dentry *unused, struct inode *inode,
6694 const char *key, const void *buf,
6695 size_t buflen, int flags)
6697 if (security_ismaclabel(key))
6698 return nfs4_set_security_label(inode, buf, buflen);
6703 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
6704 struct dentry *unused, struct inode *inode,
6705 const char *key, void *buf, size_t buflen)
6707 if (security_ismaclabel(key))
6708 return nfs4_get_security_label(inode, buf, buflen);
6713 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6717 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6718 len = security_inode_listsecurity(inode, list, list_len);
6719 if (list_len && len > list_len)
6725 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6726 .prefix = XATTR_SECURITY_PREFIX,
6727 .get = nfs4_xattr_get_nfs4_label,
6728 .set = nfs4_xattr_set_nfs4_label,
6734 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6742 * nfs_fhget will use either the mounted_on_fileid or the fileid
6744 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6746 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6747 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6748 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
6749 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
6752 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
6753 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
6754 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6758 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6759 const struct qstr *name,
6760 struct nfs4_fs_locations *fs_locations,
6763 struct nfs_server *server = NFS_SERVER(dir);
6765 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6767 struct nfs4_fs_locations_arg args = {
6768 .dir_fh = NFS_FH(dir),
6773 struct nfs4_fs_locations_res res = {
6774 .fs_locations = fs_locations,
6776 struct rpc_message msg = {
6777 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6783 dprintk("%s: start\n", __func__);
6785 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6786 * is not supported */
6787 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6788 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6790 bitmask[0] |= FATTR4_WORD0_FILEID;
6792 nfs_fattr_init(&fs_locations->fattr);
6793 fs_locations->server = server;
6794 fs_locations->nlocations = 0;
6795 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
6796 dprintk("%s: returned status = %d\n", __func__, status);
6800 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6801 const struct qstr *name,
6802 struct nfs4_fs_locations *fs_locations,
6805 struct nfs4_exception exception = { };
6808 err = _nfs4_proc_fs_locations(client, dir, name,
6809 fs_locations, page);
6810 trace_nfs4_get_fs_locations(dir, name, err);
6811 err = nfs4_handle_exception(NFS_SERVER(dir), err,
6813 } while (exception.retry);
6818 * This operation also signals the server that this client is
6819 * performing migration recovery. The server can stop returning
6820 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6821 * appended to this compound to identify the client ID which is
6822 * performing recovery.
6824 static int _nfs40_proc_get_locations(struct inode *inode,
6825 struct nfs4_fs_locations *locations,
6826 struct page *page, struct rpc_cred *cred)
6828 struct nfs_server *server = NFS_SERVER(inode);
6829 struct rpc_clnt *clnt = server->client;
6831 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6833 struct nfs4_fs_locations_arg args = {
6834 .clientid = server->nfs_client->cl_clientid,
6835 .fh = NFS_FH(inode),
6838 .migration = 1, /* skip LOOKUP */
6839 .renew = 1, /* append RENEW */
6841 struct nfs4_fs_locations_res res = {
6842 .fs_locations = locations,
6846 struct rpc_message msg = {
6847 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6852 unsigned long now = jiffies;
6855 nfs_fattr_init(&locations->fattr);
6856 locations->server = server;
6857 locations->nlocations = 0;
6859 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6860 nfs4_set_sequence_privileged(&args.seq_args);
6861 status = nfs4_call_sync_sequence(clnt, server, &msg,
6862 &args.seq_args, &res.seq_res);
6866 renew_lease(server, now);
6870 #ifdef CONFIG_NFS_V4_1
6873 * This operation also signals the server that this client is
6874 * performing migration recovery. The server can stop asserting
6875 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6876 * performing this operation is identified in the SEQUENCE
6877 * operation in this compound.
6879 * When the client supports GETATTR(fs_locations_info), it can
6880 * be plumbed in here.
6882 static int _nfs41_proc_get_locations(struct inode *inode,
6883 struct nfs4_fs_locations *locations,
6884 struct page *page, struct rpc_cred *cred)
6886 struct nfs_server *server = NFS_SERVER(inode);
6887 struct rpc_clnt *clnt = server->client;
6889 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6891 struct nfs4_fs_locations_arg args = {
6892 .fh = NFS_FH(inode),
6895 .migration = 1, /* skip LOOKUP */
6897 struct nfs4_fs_locations_res res = {
6898 .fs_locations = locations,
6901 struct rpc_message msg = {
6902 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6909 nfs_fattr_init(&locations->fattr);
6910 locations->server = server;
6911 locations->nlocations = 0;
6913 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6914 nfs4_set_sequence_privileged(&args.seq_args);
6915 status = nfs4_call_sync_sequence(clnt, server, &msg,
6916 &args.seq_args, &res.seq_res);
6917 if (status == NFS4_OK &&
6918 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6919 status = -NFS4ERR_LEASE_MOVED;
6923 #endif /* CONFIG_NFS_V4_1 */
6926 * nfs4_proc_get_locations - discover locations for a migrated FSID
6927 * @inode: inode on FSID that is migrating
6928 * @locations: result of query
6930 * @cred: credential to use for this operation
6932 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6933 * operation failed, or a negative errno if a local error occurred.
6935 * On success, "locations" is filled in, but if the server has
6936 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6939 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6940 * from this client that require migration recovery.
6942 int nfs4_proc_get_locations(struct inode *inode,
6943 struct nfs4_fs_locations *locations,
6944 struct page *page, struct rpc_cred *cred)
6946 struct nfs_server *server = NFS_SERVER(inode);
6947 struct nfs_client *clp = server->nfs_client;
6948 const struct nfs4_mig_recovery_ops *ops =
6949 clp->cl_mvops->mig_recovery_ops;
6950 struct nfs4_exception exception = { };
6953 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6954 (unsigned long long)server->fsid.major,
6955 (unsigned long long)server->fsid.minor,
6957 nfs_display_fhandle(NFS_FH(inode), __func__);
6960 status = ops->get_locations(inode, locations, page, cred);
6961 if (status != -NFS4ERR_DELAY)
6963 nfs4_handle_exception(server, status, &exception);
6964 } while (exception.retry);
6969 * This operation also signals the server that this client is
6970 * performing "lease moved" recovery. The server can stop
6971 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6972 * is appended to this compound to identify the client ID which is
6973 * performing recovery.
6975 static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6977 struct nfs_server *server = NFS_SERVER(inode);
6978 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6979 struct rpc_clnt *clnt = server->client;
6980 struct nfs4_fsid_present_arg args = {
6981 .fh = NFS_FH(inode),
6982 .clientid = clp->cl_clientid,
6983 .renew = 1, /* append RENEW */
6985 struct nfs4_fsid_present_res res = {
6988 struct rpc_message msg = {
6989 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6994 unsigned long now = jiffies;
6997 res.fh = nfs_alloc_fhandle();
7001 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7002 nfs4_set_sequence_privileged(&args.seq_args);
7003 status = nfs4_call_sync_sequence(clnt, server, &msg,
7004 &args.seq_args, &res.seq_res);
7005 nfs_free_fhandle(res.fh);
7009 do_renew_lease(clp, now);
7013 #ifdef CONFIG_NFS_V4_1
7016 * This operation also signals the server that this client is
7017 * performing "lease moved" recovery. The server can stop asserting
7018 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7019 * this operation is identified in the SEQUENCE operation in this
7022 static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7024 struct nfs_server *server = NFS_SERVER(inode);
7025 struct rpc_clnt *clnt = server->client;
7026 struct nfs4_fsid_present_arg args = {
7027 .fh = NFS_FH(inode),
7029 struct nfs4_fsid_present_res res = {
7031 struct rpc_message msg = {
7032 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7039 res.fh = nfs_alloc_fhandle();
7043 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7044 nfs4_set_sequence_privileged(&args.seq_args);
7045 status = nfs4_call_sync_sequence(clnt, server, &msg,
7046 &args.seq_args, &res.seq_res);
7047 nfs_free_fhandle(res.fh);
7048 if (status == NFS4_OK &&
7049 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7050 status = -NFS4ERR_LEASE_MOVED;
7054 #endif /* CONFIG_NFS_V4_1 */
7057 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7058 * @inode: inode on FSID to check
7059 * @cred: credential to use for this operation
7061 * Server indicates whether the FSID is present, moved, or not
7062 * recognized. This operation is necessary to clear a LEASE_MOVED
7063 * condition for this client ID.
7065 * Returns NFS4_OK if the FSID is present on this server,
7066 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7067 * NFS4ERR code if some error occurred on the server, or a
7068 * negative errno if a local failure occurred.
7070 int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7072 struct nfs_server *server = NFS_SERVER(inode);
7073 struct nfs_client *clp = server->nfs_client;
7074 const struct nfs4_mig_recovery_ops *ops =
7075 clp->cl_mvops->mig_recovery_ops;
7076 struct nfs4_exception exception = { };
7079 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7080 (unsigned long long)server->fsid.major,
7081 (unsigned long long)server->fsid.minor,
7083 nfs_display_fhandle(NFS_FH(inode), __func__);
7086 status = ops->fsid_present(inode, cred);
7087 if (status != -NFS4ERR_DELAY)
7089 nfs4_handle_exception(server, status, &exception);
7090 } while (exception.retry);
7095 * If 'use_integrity' is true and the state managment nfs_client
7096 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7097 * and the machine credential as per RFC3530bis and RFC5661 Security
7098 * Considerations sections. Otherwise, just use the user cred with the
7099 * filesystem's rpc_client.
7101 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
7104 struct nfs4_secinfo_arg args = {
7105 .dir_fh = NFS_FH(dir),
7108 struct nfs4_secinfo_res res = {
7111 struct rpc_message msg = {
7112 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7116 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
7117 struct rpc_cred *cred = NULL;
7119 if (use_integrity) {
7120 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
7121 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7122 msg.rpc_cred = cred;
7125 dprintk("NFS call secinfo %s\n", name->name);
7127 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7128 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7130 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7132 dprintk("NFS reply secinfo: %d\n", status);
7140 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7141 struct nfs4_secinfo_flavors *flavors)
7143 struct nfs4_exception exception = { };
7146 err = -NFS4ERR_WRONGSEC;
7148 /* try to use integrity protection with machine cred */
7149 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7150 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7153 * if unable to use integrity protection, or SECINFO with
7154 * integrity protection returns NFS4ERR_WRONGSEC (which is
7155 * disallowed by spec, but exists in deployed servers) use
7156 * the current filesystem's rpc_client and the user cred.
7158 if (err == -NFS4ERR_WRONGSEC)
7159 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7161 trace_nfs4_secinfo(dir, name, err);
7162 err = nfs4_handle_exception(NFS_SERVER(dir), err,
7164 } while (exception.retry);
7168 #ifdef CONFIG_NFS_V4_1
7170 * Check the exchange flags returned by the server for invalid flags, having
7171 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7174 static int nfs4_check_cl_exchange_flags(u32 flags)
7176 if (flags & ~EXCHGID4_FLAG_MASK_R)
7178 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7179 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7181 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7185 return -NFS4ERR_INVAL;
7189 nfs41_same_server_scope(struct nfs41_server_scope *a,
7190 struct nfs41_server_scope *b)
7192 if (a->server_scope_sz == b->server_scope_sz &&
7193 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
7200 nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7204 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7205 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7209 * nfs4_proc_bind_one_conn_to_session()
7211 * The 4.1 client currently uses the same TCP connection for the
7212 * fore and backchannel.
7215 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7216 struct rpc_xprt *xprt,
7217 struct nfs_client *clp,
7218 struct rpc_cred *cred)
7221 struct nfs41_bind_conn_to_session_args args = {
7223 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7225 struct nfs41_bind_conn_to_session_res res;
7226 struct rpc_message msg = {
7228 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
7233 struct rpc_task_setup task_setup_data = {
7236 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
7237 .rpc_message = &msg,
7238 .flags = RPC_TASK_TIMEOUT,
7240 struct rpc_task *task;
7242 dprintk("--> %s\n", __func__);
7244 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7245 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7246 args.dir = NFS4_CDFC4_FORE;
7248 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7249 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7250 args.dir = NFS4_CDFC4_FORE;
7252 task = rpc_run_task(&task_setup_data);
7253 if (!IS_ERR(task)) {
7254 status = task->tk_status;
7257 status = PTR_ERR(task);
7258 trace_nfs4_bind_conn_to_session(clp, status);
7260 if (memcmp(res.sessionid.data,
7261 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7262 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7266 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
7267 dprintk("NFS: %s: Unexpected direction from server\n",
7272 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
7273 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7280 dprintk("<-- %s status= %d\n", __func__, status);
7284 struct rpc_bind_conn_calldata {
7285 struct nfs_client *clp;
7286 struct rpc_cred *cred;
7290 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7291 struct rpc_xprt *xprt,
7294 struct rpc_bind_conn_calldata *p = calldata;
7296 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7299 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7301 struct rpc_bind_conn_calldata data = {
7305 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7306 nfs4_proc_bind_conn_to_session_callback, &data);
7310 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7311 * and operations we'd like to see to enable certain features in the allow map
7313 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7314 .how = SP4_MACH_CRED,
7315 .enforce.u.words = {
7316 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7317 1 << (OP_EXCHANGE_ID - 32) |
7318 1 << (OP_CREATE_SESSION - 32) |
7319 1 << (OP_DESTROY_SESSION - 32) |
7320 1 << (OP_DESTROY_CLIENTID - 32)
7323 [0] = 1 << (OP_CLOSE) |
7324 1 << (OP_OPEN_DOWNGRADE) |
7326 1 << (OP_DELEGRETURN) |
7328 [1] = 1 << (OP_SECINFO - 32) |
7329 1 << (OP_SECINFO_NO_NAME - 32) |
7330 1 << (OP_LAYOUTRETURN - 32) |
7331 1 << (OP_TEST_STATEID - 32) |
7332 1 << (OP_FREE_STATEID - 32) |
7333 1 << (OP_WRITE - 32)
7338 * Select the state protection mode for client `clp' given the server results
7339 * from exchange_id in `sp'.
7341 * Returns 0 on success, negative errno otherwise.
7343 static int nfs4_sp4_select_mode(struct nfs_client *clp,
7344 struct nfs41_state_protection *sp)
7346 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7347 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7348 1 << (OP_EXCHANGE_ID - 32) |
7349 1 << (OP_CREATE_SESSION - 32) |
7350 1 << (OP_DESTROY_SESSION - 32) |
7351 1 << (OP_DESTROY_CLIENTID - 32)
7355 if (sp->how == SP4_MACH_CRED) {
7356 /* Print state protect result */
7357 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7358 for (i = 0; i <= LAST_NFS4_OP; i++) {
7359 if (test_bit(i, sp->enforce.u.longs))
7360 dfprintk(MOUNT, " enforce op %d\n", i);
7361 if (test_bit(i, sp->allow.u.longs))
7362 dfprintk(MOUNT, " allow op %d\n", i);
7365 /* make sure nothing is on enforce list that isn't supported */
7366 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7367 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7368 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7374 * Minimal mode - state operations are allowed to use machine
7375 * credential. Note this already happens by default, so the
7376 * client doesn't have to do anything more than the negotiation.
7378 * NOTE: we don't care if EXCHANGE_ID is in the list -
7379 * we're already using the machine cred for exchange_id
7380 * and will never use a different cred.
7382 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7383 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7384 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7385 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7386 dfprintk(MOUNT, "sp4_mach_cred:\n");
7387 dfprintk(MOUNT, " minimal mode enabled\n");
7388 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
7390 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7394 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
7395 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7396 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
7397 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7398 dfprintk(MOUNT, " cleanup mode enabled\n");
7399 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
7402 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7403 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7404 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
7405 &clp->cl_sp4_flags);
7408 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7409 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7410 dfprintk(MOUNT, " secinfo mode enabled\n");
7411 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
7414 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7415 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7416 dfprintk(MOUNT, " stateid mode enabled\n");
7417 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
7420 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7421 dfprintk(MOUNT, " write mode enabled\n");
7422 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
7425 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7426 dfprintk(MOUNT, " commit mode enabled\n");
7427 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
7434 struct nfs41_exchange_id_data {
7435 struct nfs41_exchange_id_res res;
7436 struct nfs41_exchange_id_args args;
7437 struct rpc_xprt *xprt;
7441 static void nfs4_exchange_id_done(struct rpc_task *task, void *data)
7443 struct nfs41_exchange_id_data *cdata =
7444 (struct nfs41_exchange_id_data *)data;
7445 struct nfs_client *clp = cdata->args.client;
7446 int status = task->tk_status;
7448 trace_nfs4_exchange_id(clp, status);
7451 status = nfs4_check_cl_exchange_flags(cdata->res.flags);
7453 if (cdata->xprt && status == 0) {
7454 status = nfs4_detect_session_trunking(clp, &cdata->res,
7460 status = nfs4_sp4_select_mode(clp, &cdata->res.state_protect);
7463 clp->cl_clientid = cdata->res.clientid;
7464 clp->cl_exchange_flags = cdata->res.flags;
7465 /* Client ID is not confirmed */
7466 if (!(cdata->res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
7467 clear_bit(NFS4_SESSION_ESTABLISHED,
7468 &clp->cl_session->session_state);
7469 clp->cl_seqid = cdata->res.seqid;
7472 kfree(clp->cl_serverowner);
7473 clp->cl_serverowner = cdata->res.server_owner;
7474 cdata->res.server_owner = NULL;
7476 /* use the most recent implementation id */
7477 kfree(clp->cl_implid);
7478 clp->cl_implid = cdata->res.impl_id;
7479 cdata->res.impl_id = NULL;
7481 if (clp->cl_serverscope != NULL &&
7482 !nfs41_same_server_scope(clp->cl_serverscope,
7483 cdata->res.server_scope)) {
7484 dprintk("%s: server_scope mismatch detected\n",
7486 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
7487 kfree(clp->cl_serverscope);
7488 clp->cl_serverscope = NULL;
7491 if (clp->cl_serverscope == NULL) {
7492 clp->cl_serverscope = cdata->res.server_scope;
7493 cdata->res.server_scope = NULL;
7495 /* Save the EXCHANGE_ID verifier session trunk tests */
7496 memcpy(clp->cl_confirm.data, cdata->args.verifier->data,
7497 sizeof(clp->cl_confirm.data));
7500 cdata->rpc_status = status;
7504 static void nfs4_exchange_id_release(void *data)
7506 struct nfs41_exchange_id_data *cdata =
7507 (struct nfs41_exchange_id_data *)data;
7509 nfs_put_client(cdata->args.client);
7511 xprt_put(cdata->xprt);
7512 rpc_clnt_xprt_switch_put(cdata->args.client->cl_rpcclient);
7514 kfree(cdata->res.impl_id);
7515 kfree(cdata->res.server_scope);
7516 kfree(cdata->res.server_owner);
7520 static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7521 .rpc_call_done = nfs4_exchange_id_done,
7522 .rpc_release = nfs4_exchange_id_release,
7526 * _nfs4_proc_exchange_id()
7528 * Wrapper for EXCHANGE_ID operation.
7530 static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
7531 u32 sp4_how, struct rpc_xprt *xprt)
7533 nfs4_verifier verifier;
7534 struct rpc_message msg = {
7535 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
7538 struct rpc_task_setup task_setup_data = {
7539 .rpc_client = clp->cl_rpcclient,
7540 .callback_ops = &nfs4_exchange_id_call_ops,
7541 .rpc_message = &msg,
7542 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7544 struct nfs41_exchange_id_data *calldata;
7545 struct rpc_task *task;
7548 if (!atomic_inc_not_zero(&clp->cl_count))
7552 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7557 nfs4_init_boot_verifier(clp, &verifier);
7559 status = nfs4_init_uniform_client_string(clp);
7563 dprintk("NFS call exchange_id auth=%s, '%s'\n",
7564 clp->cl_rpcclient->cl_auth->au_ops->au_name,
7567 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7570 if (unlikely(calldata->res.server_owner == NULL))
7573 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
7575 if (unlikely(calldata->res.server_scope == NULL))
7576 goto out_server_owner;
7578 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7579 if (unlikely(calldata->res.impl_id == NULL))
7580 goto out_server_scope;
7584 calldata->args.state_protect.how = SP4_NONE;
7588 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
7598 calldata->xprt = xprt;
7599 task_setup_data.rpc_xprt = xprt;
7600 task_setup_data.flags =
7601 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC;
7602 calldata->args.verifier = &clp->cl_confirm;
7604 calldata->args.verifier = &verifier;
7606 calldata->args.client = clp;
7607 #ifdef CONFIG_NFS_V4_1_MIGRATION
7608 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7609 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7610 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7612 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7613 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7615 msg.rpc_argp = &calldata->args;
7616 msg.rpc_resp = &calldata->res;
7617 task_setup_data.callback_data = calldata;
7619 task = rpc_run_task(&task_setup_data);
7621 status = PTR_ERR(task);
7626 status = rpc_wait_for_completion_task(task);
7628 status = calldata->rpc_status;
7629 } else /* session trunking test */
7630 status = calldata->rpc_status;
7634 if (clp->cl_implid != NULL)
7635 dprintk("NFS reply exchange_id: Server Implementation ID: "
7636 "domain: %s, name: %s, date: %llu,%u\n",
7637 clp->cl_implid->domain, clp->cl_implid->name,
7638 clp->cl_implid->date.seconds,
7639 clp->cl_implid->date.nseconds);
7640 dprintk("NFS reply exchange_id: %d\n", status);
7644 kfree(calldata->res.impl_id);
7646 kfree(calldata->res.server_scope);
7648 kfree(calldata->res.server_owner);
7655 * nfs4_proc_exchange_id()
7657 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7659 * Since the clientid has expired, all compounds using sessions
7660 * associated with the stale clientid will be returning
7661 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7662 * be in some phase of session reset.
7664 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7666 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7668 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7671 /* try SP4_MACH_CRED if krb5i/p */
7672 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7673 authflavor == RPC_AUTH_GSS_KRB5P) {
7674 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED, NULL);
7680 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE, NULL);
7684 * nfs4_test_session_trunk
7686 * This is an add_xprt_test() test function called from
7687 * rpc_clnt_setup_test_and_add_xprt.
7689 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7690 * and is dereferrenced in nfs4_exchange_id_release
7692 * Upon success, add the new transport to the rpc_clnt
7694 * @clnt: struct rpc_clnt to get new transport
7695 * @xprt: the rpc_xprt to test
7696 * @data: call data for _nfs4_proc_exchange_id.
7698 int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7701 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
7704 dprintk("--> %s try %s\n", __func__,
7705 xprt->address_strings[RPC_DISPLAY_ADDR]);
7707 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7709 /* Test connection for session trunking. Async exchange_id call */
7710 return _nfs4_proc_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7712 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7714 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7715 struct rpc_cred *cred)
7717 struct rpc_message msg = {
7718 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7724 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7725 trace_nfs4_destroy_clientid(clp, status);
7727 dprintk("NFS: Got error %d from the server %s on "
7728 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7732 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7733 struct rpc_cred *cred)
7738 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7739 ret = _nfs4_proc_destroy_clientid(clp, cred);
7741 case -NFS4ERR_DELAY:
7742 case -NFS4ERR_CLIENTID_BUSY:
7752 int nfs4_destroy_clientid(struct nfs_client *clp)
7754 struct rpc_cred *cred;
7757 if (clp->cl_mvops->minor_version < 1)
7759 if (clp->cl_exchange_flags == 0)
7761 if (clp->cl_preserve_clid)
7763 cred = nfs4_get_clid_cred(clp);
7764 ret = nfs4_proc_destroy_clientid(clp, cred);
7769 case -NFS4ERR_STALE_CLIENTID:
7770 clp->cl_exchange_flags = 0;
7776 struct nfs4_get_lease_time_data {
7777 struct nfs4_get_lease_time_args *args;
7778 struct nfs4_get_lease_time_res *res;
7779 struct nfs_client *clp;
7782 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7785 struct nfs4_get_lease_time_data *data =
7786 (struct nfs4_get_lease_time_data *)calldata;
7788 dprintk("--> %s\n", __func__);
7789 /* just setup sequence, do not trigger session recovery
7790 since we're invoked within one */
7791 nfs41_setup_sequence(data->clp->cl_session,
7792 &data->args->la_seq_args,
7793 &data->res->lr_seq_res,
7795 dprintk("<-- %s\n", __func__);
7799 * Called from nfs4_state_manager thread for session setup, so don't recover
7800 * from sequence operation or clientid errors.
7802 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7804 struct nfs4_get_lease_time_data *data =
7805 (struct nfs4_get_lease_time_data *)calldata;
7807 dprintk("--> %s\n", __func__);
7808 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7810 switch (task->tk_status) {
7811 case -NFS4ERR_DELAY:
7812 case -NFS4ERR_GRACE:
7813 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7814 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7815 task->tk_status = 0;
7817 case -NFS4ERR_RETRY_UNCACHED_REP:
7818 rpc_restart_call_prepare(task);
7821 dprintk("<-- %s\n", __func__);
7824 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
7825 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7826 .rpc_call_done = nfs4_get_lease_time_done,
7829 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7831 struct rpc_task *task;
7832 struct nfs4_get_lease_time_args args;
7833 struct nfs4_get_lease_time_res res = {
7834 .lr_fsinfo = fsinfo,
7836 struct nfs4_get_lease_time_data data = {
7841 struct rpc_message msg = {
7842 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7846 struct rpc_task_setup task_setup = {
7847 .rpc_client = clp->cl_rpcclient,
7848 .rpc_message = &msg,
7849 .callback_ops = &nfs4_get_lease_time_ops,
7850 .callback_data = &data,
7851 .flags = RPC_TASK_TIMEOUT,
7855 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
7856 nfs4_set_sequence_privileged(&args.la_seq_args);
7857 dprintk("--> %s\n", __func__);
7858 task = rpc_run_task(&task_setup);
7861 status = PTR_ERR(task);
7863 status = task->tk_status;
7866 dprintk("<-- %s return %d\n", __func__, status);
7872 * Initialize the values to be used by the client in CREATE_SESSION
7873 * If nfs4_init_session set the fore channel request and response sizes,
7876 * Set the back channel max_resp_sz_cached to zero to force the client to
7877 * always set csa_cachethis to FALSE because the current implementation
7878 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7880 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7881 struct rpc_clnt *clnt)
7883 unsigned int max_rqst_sz, max_resp_sz;
7884 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
7886 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7887 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7889 /* Fore channel attributes */
7890 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7891 args->fc_attrs.max_resp_sz = max_resp_sz;
7892 args->fc_attrs.max_ops = NFS4_MAX_OPS;
7893 args->fc_attrs.max_reqs = max_session_slots;
7895 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
7896 "max_ops=%u max_reqs=%u\n",
7898 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
7899 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
7901 /* Back channel attributes */
7902 args->bc_attrs.max_rqst_sz = max_bc_payload;
7903 args->bc_attrs.max_resp_sz = max_bc_payload;
7904 args->bc_attrs.max_resp_sz_cached = 0;
7905 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
7906 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
7908 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7909 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7911 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7912 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7913 args->bc_attrs.max_reqs);
7916 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7917 struct nfs41_create_session_res *res)
7919 struct nfs4_channel_attrs *sent = &args->fc_attrs;
7920 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
7922 if (rcvd->max_resp_sz > sent->max_resp_sz)
7925 * Our requested max_ops is the minimum we need; we're not
7926 * prepared to break up compounds into smaller pieces than that.
7927 * So, no point even trying to continue if the server won't
7930 if (rcvd->max_ops < sent->max_ops)
7932 if (rcvd->max_reqs == 0)
7934 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7935 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
7939 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7940 struct nfs41_create_session_res *res)
7942 struct nfs4_channel_attrs *sent = &args->bc_attrs;
7943 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
7945 if (!(res->flags & SESSION4_BACK_CHAN))
7947 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7949 if (rcvd->max_resp_sz < sent->max_resp_sz)
7951 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7953 if (rcvd->max_ops > sent->max_ops)
7955 if (rcvd->max_reqs > sent->max_reqs)
7961 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
7962 struct nfs41_create_session_res *res)
7966 ret = nfs4_verify_fore_channel_attrs(args, res);
7969 return nfs4_verify_back_channel_attrs(args, res);
7972 static void nfs4_update_session(struct nfs4_session *session,
7973 struct nfs41_create_session_res *res)
7975 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
7976 /* Mark client id and session as being confirmed */
7977 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7978 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
7979 session->flags = res->flags;
7980 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
7981 if (res->flags & SESSION4_BACK_CHAN)
7982 memcpy(&session->bc_attrs, &res->bc_attrs,
7983 sizeof(session->bc_attrs));
7986 static int _nfs4_proc_create_session(struct nfs_client *clp,
7987 struct rpc_cred *cred)
7989 struct nfs4_session *session = clp->cl_session;
7990 struct nfs41_create_session_args args = {
7992 .clientid = clp->cl_clientid,
7993 .seqid = clp->cl_seqid,
7994 .cb_program = NFS4_CALLBACK,
7996 struct nfs41_create_session_res res;
7998 struct rpc_message msg = {
7999 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8006 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
8007 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
8009 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
8010 trace_nfs4_create_session(clp, status);
8013 case -NFS4ERR_STALE_CLIENTID:
8014 case -NFS4ERR_DELAY:
8023 /* Verify the session's negotiated channel_attrs values */
8024 status = nfs4_verify_channel_attrs(&args, &res);
8025 /* Increment the clientid slot sequence id */
8028 nfs4_update_session(session, &res);
8035 * Issues a CREATE_SESSION operation to the server.
8036 * It is the responsibility of the caller to verify the session is
8037 * expired before calling this routine.
8039 int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
8043 struct nfs4_session *session = clp->cl_session;
8045 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8047 status = _nfs4_proc_create_session(clp, cred);
8051 /* Init or reset the session slot tables */
8052 status = nfs4_setup_session_slot_tables(session);
8053 dprintk("slot table setup returned %d\n", status);
8057 ptr = (unsigned *)&session->sess_id.data[0];
8058 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8059 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
8061 dprintk("<-- %s\n", __func__);
8066 * Issue the over-the-wire RPC DESTROY_SESSION.
8067 * The caller must serialize access to this routine.
8069 int nfs4_proc_destroy_session(struct nfs4_session *session,
8070 struct rpc_cred *cred)
8072 struct rpc_message msg = {
8073 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8074 .rpc_argp = session,
8079 dprintk("--> nfs4_proc_destroy_session\n");
8081 /* session is still being setup */
8082 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8085 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
8086 trace_nfs4_destroy_session(session->clp, status);
8089 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
8090 "Session has been destroyed regardless...\n", status);
8092 dprintk("<-- nfs4_proc_destroy_session\n");
8097 * Renew the cl_session lease.
8099 struct nfs4_sequence_data {
8100 struct nfs_client *clp;
8101 struct nfs4_sequence_args args;
8102 struct nfs4_sequence_res res;
8105 static void nfs41_sequence_release(void *data)
8107 struct nfs4_sequence_data *calldata = data;
8108 struct nfs_client *clp = calldata->clp;
8110 if (atomic_read(&clp->cl_count) > 1)
8111 nfs4_schedule_state_renewal(clp);
8112 nfs_put_client(clp);
8116 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8118 switch(task->tk_status) {
8119 case -NFS4ERR_DELAY:
8120 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8123 nfs4_schedule_lease_recovery(clp);
8128 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
8130 struct nfs4_sequence_data *calldata = data;
8131 struct nfs_client *clp = calldata->clp;
8133 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8136 trace_nfs4_sequence(clp, task->tk_status);
8137 if (task->tk_status < 0) {
8138 dprintk("%s ERROR %d\n", __func__, task->tk_status);
8139 if (atomic_read(&clp->cl_count) == 1)
8142 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8143 rpc_restart_call_prepare(task);
8147 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
8149 dprintk("<-- %s\n", __func__);
8152 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8154 struct nfs4_sequence_data *calldata = data;
8155 struct nfs_client *clp = calldata->clp;
8156 struct nfs4_sequence_args *args;
8157 struct nfs4_sequence_res *res;
8159 args = task->tk_msg.rpc_argp;
8160 res = task->tk_msg.rpc_resp;
8162 nfs41_setup_sequence(clp->cl_session, args, res, task);
8165 static const struct rpc_call_ops nfs41_sequence_ops = {
8166 .rpc_call_done = nfs41_sequence_call_done,
8167 .rpc_call_prepare = nfs41_sequence_prepare,
8168 .rpc_release = nfs41_sequence_release,
8171 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8172 struct rpc_cred *cred,
8175 struct nfs4_sequence_data *calldata;
8176 struct rpc_message msg = {
8177 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8180 struct rpc_task_setup task_setup_data = {
8181 .rpc_client = clp->cl_rpcclient,
8182 .rpc_message = &msg,
8183 .callback_ops = &nfs41_sequence_ops,
8184 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
8187 if (!atomic_inc_not_zero(&clp->cl_count))
8188 return ERR_PTR(-EIO);
8189 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8190 if (calldata == NULL) {
8191 nfs_put_client(clp);
8192 return ERR_PTR(-ENOMEM);
8194 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
8196 nfs4_set_sequence_privileged(&calldata->args);
8197 msg.rpc_argp = &calldata->args;
8198 msg.rpc_resp = &calldata->res;
8199 calldata->clp = clp;
8200 task_setup_data.callback_data = calldata;
8202 return rpc_run_task(&task_setup_data);
8205 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
8207 struct rpc_task *task;
8210 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
8212 task = _nfs41_proc_sequence(clp, cred, false);
8214 ret = PTR_ERR(task);
8216 rpc_put_task_async(task);
8217 dprintk("<-- %s status=%d\n", __func__, ret);
8221 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8223 struct rpc_task *task;
8226 task = _nfs41_proc_sequence(clp, cred, true);
8228 ret = PTR_ERR(task);
8231 ret = rpc_wait_for_completion_task(task);
8233 ret = task->tk_status;
8236 dprintk("<-- %s status=%d\n", __func__, ret);
8240 struct nfs4_reclaim_complete_data {
8241 struct nfs_client *clp;
8242 struct nfs41_reclaim_complete_args arg;
8243 struct nfs41_reclaim_complete_res res;
8246 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8248 struct nfs4_reclaim_complete_data *calldata = data;
8250 nfs41_setup_sequence(calldata->clp->cl_session,
8251 &calldata->arg.seq_args,
8252 &calldata->res.seq_res,
8256 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8258 switch(task->tk_status) {
8260 case -NFS4ERR_COMPLETE_ALREADY:
8261 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8263 case -NFS4ERR_DELAY:
8264 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8266 case -NFS4ERR_RETRY_UNCACHED_REP:
8269 nfs4_schedule_lease_recovery(clp);
8274 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8276 struct nfs4_reclaim_complete_data *calldata = data;
8277 struct nfs_client *clp = calldata->clp;
8278 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8280 dprintk("--> %s\n", __func__);
8281 if (!nfs41_sequence_done(task, res))
8284 trace_nfs4_reclaim_complete(clp, task->tk_status);
8285 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8286 rpc_restart_call_prepare(task);
8289 dprintk("<-- %s\n", __func__);
8292 static void nfs4_free_reclaim_complete_data(void *data)
8294 struct nfs4_reclaim_complete_data *calldata = data;
8299 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8300 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8301 .rpc_call_done = nfs4_reclaim_complete_done,
8302 .rpc_release = nfs4_free_reclaim_complete_data,
8306 * Issue a global reclaim complete.
8308 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8309 struct rpc_cred *cred)
8311 struct nfs4_reclaim_complete_data *calldata;
8312 struct rpc_task *task;
8313 struct rpc_message msg = {
8314 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
8317 struct rpc_task_setup task_setup_data = {
8318 .rpc_client = clp->cl_rpcclient,
8319 .rpc_message = &msg,
8320 .callback_ops = &nfs4_reclaim_complete_call_ops,
8321 .flags = RPC_TASK_ASYNC,
8323 int status = -ENOMEM;
8325 dprintk("--> %s\n", __func__);
8326 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8327 if (calldata == NULL)
8329 calldata->clp = clp;
8330 calldata->arg.one_fs = 0;
8332 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
8333 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
8334 msg.rpc_argp = &calldata->arg;
8335 msg.rpc_resp = &calldata->res;
8336 task_setup_data.callback_data = calldata;
8337 task = rpc_run_task(&task_setup_data);
8339 status = PTR_ERR(task);
8342 status = nfs4_wait_for_completion_rpc_task(task);
8344 status = task->tk_status;
8348 dprintk("<-- %s status=%d\n", __func__, status);
8353 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8355 struct nfs4_layoutget *lgp = calldata;
8356 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
8357 struct nfs4_session *session = nfs4_get_session(server);
8359 dprintk("--> %s\n", __func__);
8360 nfs41_setup_sequence(session, &lgp->args.seq_args,
8361 &lgp->res.seq_res, task);
8362 dprintk("<-- %s\n", __func__);
8365 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8367 struct nfs4_layoutget *lgp = calldata;
8369 dprintk("--> %s\n", __func__);
8370 nfs41_sequence_process(task, &lgp->res.seq_res);
8371 dprintk("<-- %s\n", __func__);
8375 nfs4_layoutget_handle_exception(struct rpc_task *task,
8376 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8378 struct inode *inode = lgp->args.inode;
8379 struct nfs_server *server = NFS_SERVER(inode);
8380 struct pnfs_layout_hdr *lo;
8381 int nfs4err = task->tk_status;
8382 int err, status = 0;
8385 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
8392 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8393 * on the file. set tk_status to -ENODATA to tell upper layer to
8396 case -NFS4ERR_LAYOUTUNAVAILABLE:
8400 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8401 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8403 case -NFS4ERR_BADLAYOUT:
8404 status = -EOVERFLOW;
8407 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
8408 * (or clients) writing to the same RAID stripe except when
8409 * the minlength argument is 0 (see RFC5661 section 18.43.3).
8411 * Treat it like we would RECALLCONFLICT -- we retry for a little
8412 * while, and then eventually give up.
8414 case -NFS4ERR_LAYOUTTRYLATER:
8415 if (lgp->args.minlength == 0) {
8416 status = -EOVERFLOW;
8421 case -NFS4ERR_RECALLCONFLICT:
8422 status = -ERECALLCONFLICT;
8424 case -NFS4ERR_DELEG_REVOKED:
8425 case -NFS4ERR_ADMIN_REVOKED:
8426 case -NFS4ERR_EXPIRED:
8427 case -NFS4ERR_BAD_STATEID:
8428 exception->timeout = 0;
8429 spin_lock(&inode->i_lock);
8430 lo = NFS_I(inode)->layout;
8431 /* If the open stateid was bad, then recover it. */
8432 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8433 nfs4_stateid_match_other(&lgp->args.stateid,
8434 &lgp->args.ctx->state->stateid)) {
8435 spin_unlock(&inode->i_lock);
8436 exception->state = lgp->args.ctx->state;
8437 exception->stateid = &lgp->args.stateid;
8442 * Mark the bad layout state as invalid, then retry
8444 pnfs_mark_layout_stateid_invalid(lo, &head);
8445 spin_unlock(&inode->i_lock);
8446 pnfs_free_lseg_list(&head);
8451 err = nfs4_handle_exception(server, nfs4err, exception);
8453 if (exception->retry)
8459 dprintk("<-- %s\n", __func__);
8463 static size_t max_response_pages(struct nfs_server *server)
8465 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8466 return nfs_page_array_len(0, max_resp_sz);
8469 static void nfs4_free_pages(struct page **pages, size_t size)
8476 for (i = 0; i < size; i++) {
8479 __free_page(pages[i]);
8484 static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8486 struct page **pages;
8489 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8491 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8495 for (i = 0; i < size; i++) {
8496 pages[i] = alloc_page(gfp_flags);
8498 dprintk("%s: failed to allocate page\n", __func__);
8499 nfs4_free_pages(pages, size);
8507 static void nfs4_layoutget_release(void *calldata)
8509 struct nfs4_layoutget *lgp = calldata;
8510 struct inode *inode = lgp->args.inode;
8511 struct nfs_server *server = NFS_SERVER(inode);
8512 size_t max_pages = max_response_pages(server);
8514 dprintk("--> %s\n", __func__);
8515 nfs4_free_pages(lgp->args.layout.pages, max_pages);
8516 pnfs_put_layout_hdr(NFS_I(inode)->layout);
8517 put_nfs_open_context(lgp->args.ctx);
8519 dprintk("<-- %s\n", __func__);
8522 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8523 .rpc_call_prepare = nfs4_layoutget_prepare,
8524 .rpc_call_done = nfs4_layoutget_done,
8525 .rpc_release = nfs4_layoutget_release,
8528 struct pnfs_layout_segment *
8529 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
8531 struct inode *inode = lgp->args.inode;
8532 struct nfs_server *server = NFS_SERVER(inode);
8533 size_t max_pages = max_response_pages(server);
8534 struct rpc_task *task;
8535 struct rpc_message msg = {
8536 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8537 .rpc_argp = &lgp->args,
8538 .rpc_resp = &lgp->res,
8539 .rpc_cred = lgp->cred,
8541 struct rpc_task_setup task_setup_data = {
8542 .rpc_client = server->client,
8543 .rpc_message = &msg,
8544 .callback_ops = &nfs4_layoutget_call_ops,
8545 .callback_data = lgp,
8546 .flags = RPC_TASK_ASYNC,
8548 struct pnfs_layout_segment *lseg = NULL;
8549 struct nfs4_exception exception = {
8551 .timeout = *timeout,
8555 dprintk("--> %s\n", __func__);
8557 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8558 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8560 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8561 if (!lgp->args.layout.pages) {
8562 nfs4_layoutget_release(lgp);
8563 return ERR_PTR(-ENOMEM);
8565 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8567 lgp->res.layoutp = &lgp->args.layout;
8568 lgp->res.seq_res.sr_slot = NULL;
8569 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
8571 task = rpc_run_task(&task_setup_data);
8573 return ERR_CAST(task);
8574 status = nfs4_wait_for_completion_rpc_task(task);
8576 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8577 *timeout = exception.timeout;
8580 trace_nfs4_layoutget(lgp->args.ctx,
8586 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8587 if (status == 0 && lgp->res.layoutp->len)
8588 lseg = pnfs_layout_process(lgp);
8589 nfs4_sequence_free_slot(&lgp->res.seq_res);
8591 dprintk("<-- %s status=%d\n", __func__, status);
8593 return ERR_PTR(status);
8598 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8600 struct nfs4_layoutreturn *lrp = calldata;
8602 dprintk("--> %s\n", __func__);
8603 nfs41_setup_sequence(lrp->clp->cl_session,
8604 &lrp->args.seq_args,
8609 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8611 struct nfs4_layoutreturn *lrp = calldata;
8612 struct nfs_server *server;
8614 dprintk("--> %s\n", __func__);
8616 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
8619 server = NFS_SERVER(lrp->args.inode);
8620 switch (task->tk_status) {
8622 task->tk_status = 0;
8625 case -NFS4ERR_DELAY:
8626 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
8628 nfs4_sequence_free_slot(&lrp->res.seq_res);
8629 rpc_restart_call_prepare(task);
8632 dprintk("<-- %s\n", __func__);
8635 static void nfs4_layoutreturn_release(void *calldata)
8637 struct nfs4_layoutreturn *lrp = calldata;
8638 struct pnfs_layout_hdr *lo = lrp->args.layout;
8640 dprintk("--> %s\n", __func__);
8641 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
8642 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
8643 nfs4_sequence_free_slot(&lrp->res.seq_res);
8644 pnfs_put_layout_hdr(lrp->args.layout);
8645 nfs_iput_and_deactive(lrp->inode);
8646 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
8647 lrp->ld_private.ops->free(&lrp->ld_private);
8649 dprintk("<-- %s\n", __func__);
8652 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8653 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8654 .rpc_call_done = nfs4_layoutreturn_done,
8655 .rpc_release = nfs4_layoutreturn_release,
8658 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
8660 struct rpc_task *task;
8661 struct rpc_message msg = {
8662 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8663 .rpc_argp = &lrp->args,
8664 .rpc_resp = &lrp->res,
8665 .rpc_cred = lrp->cred,
8667 struct rpc_task_setup task_setup_data = {
8668 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
8669 .rpc_message = &msg,
8670 .callback_ops = &nfs4_layoutreturn_call_ops,
8671 .callback_data = lrp,
8675 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8676 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8677 &task_setup_data.rpc_client, &msg);
8679 dprintk("--> %s\n", __func__);
8681 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8683 nfs4_layoutreturn_release(lrp);
8686 task_setup_data.flags |= RPC_TASK_ASYNC;
8688 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
8689 task = rpc_run_task(&task_setup_data);
8691 return PTR_ERR(task);
8693 status = task->tk_status;
8694 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
8695 dprintk("<-- %s status=%d\n", __func__, status);
8701 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
8702 struct pnfs_device *pdev,
8703 struct rpc_cred *cred)
8705 struct nfs4_getdeviceinfo_args args = {
8707 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8708 NOTIFY_DEVICEID4_DELETE,
8710 struct nfs4_getdeviceinfo_res res = {
8713 struct rpc_message msg = {
8714 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8721 dprintk("--> %s\n", __func__);
8722 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
8723 if (res.notification & ~args.notify_types)
8724 dprintk("%s: unsupported notification\n", __func__);
8725 if (res.notification != args.notify_types)
8728 dprintk("<-- %s status=%d\n", __func__, status);
8733 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8734 struct pnfs_device *pdev,
8735 struct rpc_cred *cred)
8737 struct nfs4_exception exception = { };
8741 err = nfs4_handle_exception(server,
8742 _nfs4_proc_getdeviceinfo(server, pdev, cred),
8744 } while (exception.retry);
8747 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8749 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8751 struct nfs4_layoutcommit_data *data = calldata;
8752 struct nfs_server *server = NFS_SERVER(data->args.inode);
8753 struct nfs4_session *session = nfs4_get_session(server);
8755 nfs41_setup_sequence(session,
8756 &data->args.seq_args,
8762 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8764 struct nfs4_layoutcommit_data *data = calldata;
8765 struct nfs_server *server = NFS_SERVER(data->args.inode);
8767 if (!nfs41_sequence_done(task, &data->res.seq_res))
8770 switch (task->tk_status) { /* Just ignore these failures */
8771 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8772 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8773 case -NFS4ERR_BADLAYOUT: /* no layout */
8774 case -NFS4ERR_GRACE: /* loca_recalim always false */
8775 task->tk_status = 0;
8779 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
8780 rpc_restart_call_prepare(task);
8786 static void nfs4_layoutcommit_release(void *calldata)
8788 struct nfs4_layoutcommit_data *data = calldata;
8790 pnfs_cleanup_layoutcommit(data);
8791 nfs_post_op_update_inode_force_wcc(data->args.inode,
8793 put_rpccred(data->cred);
8794 nfs_iput_and_deactive(data->inode);
8798 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8799 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8800 .rpc_call_done = nfs4_layoutcommit_done,
8801 .rpc_release = nfs4_layoutcommit_release,
8805 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
8807 struct rpc_message msg = {
8808 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8809 .rpc_argp = &data->args,
8810 .rpc_resp = &data->res,
8811 .rpc_cred = data->cred,
8813 struct rpc_task_setup task_setup_data = {
8814 .task = &data->task,
8815 .rpc_client = NFS_CLIENT(data->args.inode),
8816 .rpc_message = &msg,
8817 .callback_ops = &nfs4_layoutcommit_ops,
8818 .callback_data = data,
8820 struct rpc_task *task;
8823 dprintk("NFS: initiating layoutcommit call. sync %d "
8824 "lbw: %llu inode %lu\n", sync,
8825 data->args.lastbytewritten,
8826 data->args.inode->i_ino);
8829 data->inode = nfs_igrab_and_active(data->args.inode);
8830 if (data->inode == NULL) {
8831 nfs4_layoutcommit_release(data);
8834 task_setup_data.flags = RPC_TASK_ASYNC;
8836 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
8837 task = rpc_run_task(&task_setup_data);
8839 return PTR_ERR(task);
8841 status = task->tk_status;
8842 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
8843 dprintk("%s: status %d\n", __func__, status);
8849 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8850 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8853 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8854 struct nfs_fsinfo *info,
8855 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
8857 struct nfs41_secinfo_no_name_args args = {
8858 .style = SECINFO_STYLE_CURRENT_FH,
8860 struct nfs4_secinfo_res res = {
8863 struct rpc_message msg = {
8864 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8868 struct rpc_clnt *clnt = server->client;
8869 struct rpc_cred *cred = NULL;
8872 if (use_integrity) {
8873 clnt = server->nfs_client->cl_rpcclient;
8874 cred = nfs4_get_clid_cred(server->nfs_client);
8875 msg.rpc_cred = cred;
8878 dprintk("--> %s\n", __func__);
8879 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8881 dprintk("<-- %s status=%d\n", __func__, status);
8890 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8891 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8893 struct nfs4_exception exception = { };
8896 /* first try using integrity protection */
8897 err = -NFS4ERR_WRONGSEC;
8899 /* try to use integrity protection with machine cred */
8900 if (_nfs4_is_integrity_protected(server->nfs_client))
8901 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8905 * if unable to use integrity protection, or SECINFO with
8906 * integrity protection returns NFS4ERR_WRONGSEC (which is
8907 * disallowed by spec, but exists in deployed servers) use
8908 * the current filesystem's rpc_client and the user cred.
8910 if (err == -NFS4ERR_WRONGSEC)
8911 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8916 case -NFS4ERR_WRONGSEC:
8920 err = nfs4_handle_exception(server, err, &exception);
8922 } while (exception.retry);
8928 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8929 struct nfs_fsinfo *info)
8933 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
8934 struct nfs4_secinfo_flavors *flavors;
8935 struct nfs4_secinfo4 *secinfo;
8938 page = alloc_page(GFP_KERNEL);
8944 flavors = page_address(page);
8945 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8948 * Fall back on "guess and check" method if
8949 * the server doesn't support SECINFO_NO_NAME
8951 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
8952 err = nfs4_find_root_sec(server, fhandle, info);
8958 for (i = 0; i < flavors->num_flavors; i++) {
8959 secinfo = &flavors->flavors[i];
8961 switch (secinfo->flavor) {
8965 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8966 &secinfo->flavor_info);
8969 flavor = RPC_AUTH_MAXFLAVOR;
8973 if (!nfs_auth_info_match(&server->auth_info, flavor))
8974 flavor = RPC_AUTH_MAXFLAVOR;
8976 if (flavor != RPC_AUTH_MAXFLAVOR) {
8977 err = nfs4_lookup_root_sec(server, fhandle,
8984 if (flavor == RPC_AUTH_MAXFLAVOR)
8995 static int _nfs41_test_stateid(struct nfs_server *server,
8996 nfs4_stateid *stateid,
8997 struct rpc_cred *cred)
9000 struct nfs41_test_stateid_args args = {
9003 struct nfs41_test_stateid_res res;
9004 struct rpc_message msg = {
9005 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9010 struct rpc_clnt *rpc_client = server->client;
9012 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9015 dprintk("NFS call test_stateid %p\n", stateid);
9016 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
9017 nfs4_set_sequence_privileged(&args.seq_args);
9018 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
9019 &args.seq_args, &res.seq_res);
9020 if (status != NFS_OK) {
9021 dprintk("NFS reply test_stateid: failed, %d\n", status);
9024 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
9028 static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9029 int err, struct nfs4_exception *exception)
9031 exception->retry = 0;
9033 case -NFS4ERR_DELAY:
9034 case -NFS4ERR_RETRY_UNCACHED_REP:
9035 nfs4_handle_exception(server, err, exception);
9037 case -NFS4ERR_BADSESSION:
9038 case -NFS4ERR_BADSLOT:
9039 case -NFS4ERR_BAD_HIGH_SLOT:
9040 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9041 case -NFS4ERR_DEADSESSION:
9042 nfs4_do_handle_exception(server, err, exception);
9047 * nfs41_test_stateid - perform a TEST_STATEID operation
9049 * @server: server / transport on which to perform the operation
9050 * @stateid: state ID to test
9053 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9054 * Otherwise a negative NFS4ERR value is returned if the operation
9055 * failed or the state ID is not currently valid.
9057 static int nfs41_test_stateid(struct nfs_server *server,
9058 nfs4_stateid *stateid,
9059 struct rpc_cred *cred)
9061 struct nfs4_exception exception = { };
9064 err = _nfs41_test_stateid(server, stateid, cred);
9065 nfs4_handle_delay_or_session_error(server, err, &exception);
9066 } while (exception.retry);
9070 struct nfs_free_stateid_data {
9071 struct nfs_server *server;
9072 struct nfs41_free_stateid_args args;
9073 struct nfs41_free_stateid_res res;
9076 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9078 struct nfs_free_stateid_data *data = calldata;
9079 nfs41_setup_sequence(nfs4_get_session(data->server),
9080 &data->args.seq_args,
9085 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9087 struct nfs_free_stateid_data *data = calldata;
9089 nfs41_sequence_done(task, &data->res.seq_res);
9091 switch (task->tk_status) {
9092 case -NFS4ERR_DELAY:
9093 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
9094 rpc_restart_call_prepare(task);
9098 static void nfs41_free_stateid_release(void *calldata)
9103 static const struct rpc_call_ops nfs41_free_stateid_ops = {
9104 .rpc_call_prepare = nfs41_free_stateid_prepare,
9105 .rpc_call_done = nfs41_free_stateid_done,
9106 .rpc_release = nfs41_free_stateid_release,
9109 static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
9110 const nfs4_stateid *stateid,
9111 struct rpc_cred *cred,
9114 struct rpc_message msg = {
9115 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
9118 struct rpc_task_setup task_setup = {
9119 .rpc_client = server->client,
9120 .rpc_message = &msg,
9121 .callback_ops = &nfs41_free_stateid_ops,
9122 .flags = RPC_TASK_ASYNC,
9124 struct nfs_free_stateid_data *data;
9126 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9127 &task_setup.rpc_client, &msg);
9129 dprintk("NFS call free_stateid %p\n", stateid);
9130 data = kmalloc(sizeof(*data), GFP_NOFS);
9132 return ERR_PTR(-ENOMEM);
9133 data->server = server;
9134 nfs4_stateid_copy(&data->args.stateid, stateid);
9136 task_setup.callback_data = data;
9138 msg.rpc_argp = &data->args;
9139 msg.rpc_resp = &data->res;
9140 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
9142 nfs4_set_sequence_privileged(&data->args.seq_args);
9144 return rpc_run_task(&task_setup);
9148 * nfs41_free_stateid - perform a FREE_STATEID operation
9150 * @server: server / transport on which to perform the operation
9151 * @stateid: state ID to release
9153 * @is_recovery: set to true if this call needs to be privileged
9155 * Note: this function is always asynchronous.
9157 static int nfs41_free_stateid(struct nfs_server *server,
9158 const nfs4_stateid *stateid,
9159 struct rpc_cred *cred,
9162 struct rpc_task *task;
9164 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
9166 return PTR_ERR(task);
9172 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
9174 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
9176 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
9177 nfs4_free_lock_state(server, lsp);
9180 static bool nfs41_match_stateid(const nfs4_stateid *s1,
9181 const nfs4_stateid *s2)
9183 if (s1->type != s2->type)
9186 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
9189 if (s1->seqid == s2->seqid)
9191 if (s1->seqid == 0 || s2->seqid == 0)
9197 #endif /* CONFIG_NFS_V4_1 */
9199 static bool nfs4_match_stateid(const nfs4_stateid *s1,
9200 const nfs4_stateid *s2)
9202 return nfs4_stateid_match(s1, s2);
9206 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
9207 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9208 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9209 .recover_open = nfs4_open_reclaim,
9210 .recover_lock = nfs4_lock_reclaim,
9211 .establish_clid = nfs4_init_clientid,
9212 .detect_trunking = nfs40_discover_server_trunking,
9215 #if defined(CONFIG_NFS_V4_1)
9216 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
9217 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9218 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9219 .recover_open = nfs4_open_reclaim,
9220 .recover_lock = nfs4_lock_reclaim,
9221 .establish_clid = nfs41_init_clientid,
9222 .reclaim_complete = nfs41_proc_reclaim_complete,
9223 .detect_trunking = nfs41_discover_server_trunking,
9225 #endif /* CONFIG_NFS_V4_1 */
9227 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
9228 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9229 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
9230 .recover_open = nfs40_open_expired,
9231 .recover_lock = nfs4_lock_expired,
9232 .establish_clid = nfs4_init_clientid,
9235 #if defined(CONFIG_NFS_V4_1)
9236 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
9237 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9238 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
9239 .recover_open = nfs41_open_expired,
9240 .recover_lock = nfs41_lock_expired,
9241 .establish_clid = nfs41_init_clientid,
9243 #endif /* CONFIG_NFS_V4_1 */
9245 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
9246 .sched_state_renewal = nfs4_proc_async_renew,
9247 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
9248 .renew_lease = nfs4_proc_renew,
9251 #if defined(CONFIG_NFS_V4_1)
9252 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
9253 .sched_state_renewal = nfs41_proc_async_sequence,
9254 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
9255 .renew_lease = nfs4_proc_sequence,
9259 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
9260 .get_locations = _nfs40_proc_get_locations,
9261 .fsid_present = _nfs40_proc_fsid_present,
9264 #if defined(CONFIG_NFS_V4_1)
9265 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
9266 .get_locations = _nfs41_proc_get_locations,
9267 .fsid_present = _nfs41_proc_fsid_present,
9269 #endif /* CONFIG_NFS_V4_1 */
9271 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9273 .init_caps = NFS_CAP_READDIRPLUS
9274 | NFS_CAP_ATOMIC_OPEN
9275 | NFS_CAP_POSIX_LOCK,
9276 .init_client = nfs40_init_client,
9277 .shutdown_client = nfs40_shutdown_client,
9278 .match_stateid = nfs4_match_stateid,
9279 .find_root_sec = nfs4_find_root_sec,
9280 .free_lock_state = nfs4_release_lockowner,
9281 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
9282 .alloc_seqid = nfs_alloc_seqid,
9283 .call_sync_ops = &nfs40_call_sync_ops,
9284 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9285 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9286 .state_renewal_ops = &nfs40_state_renewal_ops,
9287 .mig_recovery_ops = &nfs40_mig_recovery_ops,
9290 #if defined(CONFIG_NFS_V4_1)
9291 static struct nfs_seqid *
9292 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9297 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9299 .init_caps = NFS_CAP_READDIRPLUS
9300 | NFS_CAP_ATOMIC_OPEN
9301 | NFS_CAP_POSIX_LOCK
9302 | NFS_CAP_STATEID_NFSV41
9303 | NFS_CAP_ATOMIC_OPEN_V1,
9304 .init_client = nfs41_init_client,
9305 .shutdown_client = nfs41_shutdown_client,
9306 .match_stateid = nfs41_match_stateid,
9307 .find_root_sec = nfs41_find_root_sec,
9308 .free_lock_state = nfs41_free_lock_state,
9309 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
9310 .alloc_seqid = nfs_alloc_no_seqid,
9311 .session_trunk = nfs4_test_session_trunk,
9312 .call_sync_ops = &nfs41_call_sync_ops,
9313 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9314 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9315 .state_renewal_ops = &nfs41_state_renewal_ops,
9316 .mig_recovery_ops = &nfs41_mig_recovery_ops,
9320 #if defined(CONFIG_NFS_V4_2)
9321 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9323 .init_caps = NFS_CAP_READDIRPLUS
9324 | NFS_CAP_ATOMIC_OPEN
9325 | NFS_CAP_POSIX_LOCK
9326 | NFS_CAP_STATEID_NFSV41
9327 | NFS_CAP_ATOMIC_OPEN_V1
9330 | NFS_CAP_DEALLOCATE
9332 | NFS_CAP_LAYOUTSTATS
9334 .init_client = nfs41_init_client,
9335 .shutdown_client = nfs41_shutdown_client,
9336 .match_stateid = nfs41_match_stateid,
9337 .find_root_sec = nfs41_find_root_sec,
9338 .free_lock_state = nfs41_free_lock_state,
9339 .call_sync_ops = &nfs41_call_sync_ops,
9340 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
9341 .alloc_seqid = nfs_alloc_no_seqid,
9342 .session_trunk = nfs4_test_session_trunk,
9343 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9344 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9345 .state_renewal_ops = &nfs41_state_renewal_ops,
9346 .mig_recovery_ops = &nfs41_mig_recovery_ops,
9350 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9351 [0] = &nfs_v4_0_minor_ops,
9352 #if defined(CONFIG_NFS_V4_1)
9353 [1] = &nfs_v4_1_minor_ops,
9355 #if defined(CONFIG_NFS_V4_2)
9356 [2] = &nfs_v4_2_minor_ops,
9360 static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
9362 ssize_t error, error2;
9364 error = generic_listxattr(dentry, list, size);
9372 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9375 return error + error2;
9378 static const struct inode_operations nfs4_dir_inode_operations = {
9379 .create = nfs_create,
9380 .lookup = nfs_lookup,
9381 .atomic_open = nfs_atomic_open,
9383 .unlink = nfs_unlink,
9384 .symlink = nfs_symlink,
9388 .rename = nfs_rename,
9389 .permission = nfs_permission,
9390 .getattr = nfs_getattr,
9391 .setattr = nfs_setattr,
9392 .listxattr = nfs4_listxattr,
9395 static const struct inode_operations nfs4_file_inode_operations = {
9396 .permission = nfs_permission,
9397 .getattr = nfs_getattr,
9398 .setattr = nfs_setattr,
9399 .listxattr = nfs4_listxattr,
9402 const struct nfs_rpc_ops nfs_v4_clientops = {
9403 .version = 4, /* protocol version */
9404 .dentry_ops = &nfs4_dentry_operations,
9405 .dir_inode_ops = &nfs4_dir_inode_operations,
9406 .file_inode_ops = &nfs4_file_inode_operations,
9407 .file_ops = &nfs4_file_operations,
9408 .getroot = nfs4_proc_get_root,
9409 .submount = nfs4_submount,
9410 .try_mount = nfs4_try_mount,
9411 .getattr = nfs4_proc_getattr,
9412 .setattr = nfs4_proc_setattr,
9413 .lookup = nfs4_proc_lookup,
9414 .access = nfs4_proc_access,
9415 .readlink = nfs4_proc_readlink,
9416 .create = nfs4_proc_create,
9417 .remove = nfs4_proc_remove,
9418 .unlink_setup = nfs4_proc_unlink_setup,
9419 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
9420 .unlink_done = nfs4_proc_unlink_done,
9421 .rename_setup = nfs4_proc_rename_setup,
9422 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
9423 .rename_done = nfs4_proc_rename_done,
9424 .link = nfs4_proc_link,
9425 .symlink = nfs4_proc_symlink,
9426 .mkdir = nfs4_proc_mkdir,
9427 .rmdir = nfs4_proc_remove,
9428 .readdir = nfs4_proc_readdir,
9429 .mknod = nfs4_proc_mknod,
9430 .statfs = nfs4_proc_statfs,
9431 .fsinfo = nfs4_proc_fsinfo,
9432 .pathconf = nfs4_proc_pathconf,
9433 .set_capabilities = nfs4_server_capabilities,
9434 .decode_dirent = nfs4_decode_dirent,
9435 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
9436 .read_setup = nfs4_proc_read_setup,
9437 .read_done = nfs4_read_done,
9438 .write_setup = nfs4_proc_write_setup,
9439 .write_done = nfs4_write_done,
9440 .commit_setup = nfs4_proc_commit_setup,
9441 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
9442 .commit_done = nfs4_commit_done,
9443 .lock = nfs4_proc_lock,
9444 .clear_acl_cache = nfs4_zap_acl_attr,
9445 .close_context = nfs4_close_context,
9446 .open_context = nfs4_atomic_open,
9447 .have_delegation = nfs4_have_delegation,
9448 .return_delegation = nfs4_inode_return_delegation,
9449 .alloc_client = nfs4_alloc_client,
9450 .init_client = nfs4_init_client,
9451 .free_client = nfs4_free_client,
9452 .create_server = nfs4_create_server,
9453 .clone_server = nfs_clone_server,
9456 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
9457 .name = XATTR_NAME_NFSV4_ACL,
9458 .list = nfs4_xattr_list_nfs4_acl,
9459 .get = nfs4_xattr_get_nfs4_acl,
9460 .set = nfs4_xattr_set_nfs4_acl,
9463 const struct xattr_handler *nfs4_xattr_handlers[] = {
9464 &nfs4_xattr_nfs4_acl_handler,
9465 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
9466 &nfs4_xattr_nfs4_label_handler,