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 if (nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
2959 &arg->stateid, &delegation_cred) == -EIO)
2962 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
2963 if (delegation_cred)
2964 msg.rpc_cred = delegation_cred;
2966 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
2968 put_rpccred(delegation_cred);
2969 if (status == 0 && ctx != NULL)
2970 renew_lease(server, timestamp);
2971 trace_nfs4_setattr(inode, &arg->stateid, status);
2975 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2976 struct nfs_fattr *fattr, struct iattr *sattr,
2977 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
2978 struct nfs4_label *olabel)
2980 struct nfs_server *server = NFS_SERVER(inode);
2981 struct nfs4_state *state = ctx ? ctx->state : NULL;
2982 struct nfs_setattrargs arg = {
2983 .fh = NFS_FH(inode),
2986 .bitmask = server->attr_bitmask,
2989 struct nfs_setattrres res = {
2994 struct nfs4_exception exception = {
2997 .stateid = &arg.stateid,
3001 arg.bitmask = nfs4_bitmask(server, ilabel);
3003 arg.bitmask = nfs4_bitmask(server, olabel);
3006 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
3008 case -NFS4ERR_OPENMODE:
3009 if (!(sattr->ia_valid & ATTR_SIZE)) {
3010 pr_warn_once("NFSv4: server %s is incorrectly "
3011 "applying open mode checks to "
3012 "a SETATTR that is not "
3013 "changing file size.\n",
3014 server->nfs_client->cl_hostname);
3016 if (state && !(state->state & FMODE_WRITE)) {
3018 if (sattr->ia_valid & ATTR_OPEN)
3023 err = nfs4_handle_exception(server, err, &exception);
3024 } while (exception.retry);
3030 nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3032 if (inode == NULL || !nfs_have_layout(inode))
3035 return pnfs_wait_on_layoutreturn(inode, task);
3038 struct nfs4_closedata {
3039 struct inode *inode;
3040 struct nfs4_state *state;
3041 struct nfs_closeargs arg;
3042 struct nfs_closeres res;
3044 struct nfs4_layoutreturn_args arg;
3045 struct nfs4_layoutreturn_res res;
3046 struct nfs4_xdr_opaque_data ld_private;
3050 struct nfs_fattr fattr;
3051 unsigned long timestamp;
3054 static void nfs4_free_closedata(void *data)
3056 struct nfs4_closedata *calldata = data;
3057 struct nfs4_state_owner *sp = calldata->state->owner;
3058 struct super_block *sb = calldata->state->inode->i_sb;
3060 if (calldata->lr.roc)
3061 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3062 calldata->res.lr_ret);
3063 nfs4_put_open_state(calldata->state);
3064 nfs_free_seqid(calldata->arg.seqid);
3065 nfs4_put_state_owner(sp);
3066 nfs_sb_deactive(sb);
3070 static void nfs4_close_done(struct rpc_task *task, void *data)
3072 struct nfs4_closedata *calldata = data;
3073 struct nfs4_state *state = calldata->state;
3074 struct nfs_server *server = NFS_SERVER(calldata->inode);
3075 nfs4_stateid *res_stateid = NULL;
3077 dprintk("%s: begin!\n", __func__);
3078 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3080 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
3082 /* Handle Layoutreturn errors */
3083 if (calldata->arg.lr_args && task->tk_status != 0) {
3084 switch (calldata->res.lr_ret) {
3086 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3089 calldata->arg.lr_args = NULL;
3090 calldata->res.lr_res = NULL;
3092 case -NFS4ERR_ADMIN_REVOKED:
3093 case -NFS4ERR_DELEG_REVOKED:
3094 case -NFS4ERR_EXPIRED:
3095 case -NFS4ERR_BAD_STATEID:
3096 case -NFS4ERR_OLD_STATEID:
3097 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3098 case -NFS4ERR_WRONG_CRED:
3099 calldata->arg.lr_args = NULL;
3100 calldata->res.lr_res = NULL;
3101 calldata->res.lr_ret = 0;
3102 rpc_restart_call_prepare(task);
3107 /* hmm. we are done with the inode, and in the process of freeing
3108 * the state_owner. we keep this around to process errors
3110 switch (task->tk_status) {
3112 res_stateid = &calldata->res.stateid;
3113 renew_lease(server, calldata->timestamp);
3115 case -NFS4ERR_ADMIN_REVOKED:
3116 case -NFS4ERR_STALE_STATEID:
3117 case -NFS4ERR_EXPIRED:
3118 nfs4_free_revoked_stateid(server,
3119 &calldata->arg.stateid,
3120 task->tk_msg.rpc_cred);
3121 case -NFS4ERR_OLD_STATEID:
3122 case -NFS4ERR_BAD_STATEID:
3123 if (!nfs4_stateid_match(&calldata->arg.stateid,
3124 &state->open_stateid)) {
3125 rpc_restart_call_prepare(task);
3128 if (calldata->arg.fmode == 0)
3131 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
3132 rpc_restart_call_prepare(task);
3136 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3137 res_stateid, calldata->arg.fmode);
3139 nfs_release_seqid(calldata->arg.seqid);
3140 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
3141 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
3144 static void nfs4_close_prepare(struct rpc_task *task, void *data)
3146 struct nfs4_closedata *calldata = data;
3147 struct nfs4_state *state = calldata->state;
3148 struct inode *inode = calldata->inode;
3149 bool is_rdonly, is_wronly, is_rdwr;
3152 dprintk("%s: begin!\n", __func__);
3153 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3156 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
3157 spin_lock(&state->owner->so_lock);
3158 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3159 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3160 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
3161 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
3162 /* Calculate the change in open mode */
3163 calldata->arg.fmode = 0;
3164 if (state->n_rdwr == 0) {
3165 if (state->n_rdonly == 0)
3166 call_close |= is_rdonly;
3168 calldata->arg.fmode |= FMODE_READ;
3169 if (state->n_wronly == 0)
3170 call_close |= is_wronly;
3172 calldata->arg.fmode |= FMODE_WRITE;
3173 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3174 call_close |= is_rdwr;
3176 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3178 if (!nfs4_valid_open_stateid(state) ||
3179 test_bit(NFS_OPEN_STATE, &state->flags) == 0)
3181 spin_unlock(&state->owner->so_lock);
3184 /* Note: exit _without_ calling nfs4_close_done */
3188 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
3189 nfs_release_seqid(calldata->arg.seqid);
3193 if (calldata->arg.fmode == 0) {
3194 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
3196 /* Close-to-open cache consistency revalidation */
3197 if (!nfs4_have_delegation(inode, FMODE_READ))
3198 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3200 calldata->arg.bitmask = NULL;
3203 calldata->arg.share_access =
3204 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3205 calldata->arg.fmode, 0);
3207 nfs_fattr_init(calldata->res.fattr);
3208 calldata->timestamp = jiffies;
3209 if (nfs4_setup_sequence(NFS_SERVER(inode),
3210 &calldata->arg.seq_args,
3211 &calldata->res.seq_res,
3213 nfs_release_seqid(calldata->arg.seqid);
3214 dprintk("%s: done!\n", __func__);
3217 task->tk_action = NULL;
3219 nfs4_sequence_done(task, &calldata->res.seq_res);
3222 static const struct rpc_call_ops nfs4_close_ops = {
3223 .rpc_call_prepare = nfs4_close_prepare,
3224 .rpc_call_done = nfs4_close_done,
3225 .rpc_release = nfs4_free_closedata,
3229 * It is possible for data to be read/written from a mem-mapped file
3230 * after the sys_close call (which hits the vfs layer as a flush).
3231 * This means that we can't safely call nfsv4 close on a file until
3232 * the inode is cleared. This in turn means that we are not good
3233 * NFSv4 citizens - we do not indicate to the server to update the file's
3234 * share state even when we are done with one of the three share
3235 * stateid's in the inode.
3237 * NOTE: Caller must be holding the sp->so_owner semaphore!
3239 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
3241 struct nfs_server *server = NFS_SERVER(state->inode);
3242 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
3243 struct nfs4_closedata *calldata;
3244 struct nfs4_state_owner *sp = state->owner;
3245 struct rpc_task *task;
3246 struct rpc_message msg = {
3247 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3248 .rpc_cred = state->owner->so_cred,
3250 struct rpc_task_setup task_setup_data = {
3251 .rpc_client = server->client,
3252 .rpc_message = &msg,
3253 .callback_ops = &nfs4_close_ops,
3254 .workqueue = nfsiod_workqueue,
3255 .flags = RPC_TASK_ASYNC,
3257 int status = -ENOMEM;
3259 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3260 &task_setup_data.rpc_client, &msg);
3262 calldata = kzalloc(sizeof(*calldata), gfp_mask);
3263 if (calldata == NULL)
3265 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
3266 calldata->inode = state->inode;
3267 calldata->state = state;
3268 calldata->arg.fh = NFS_FH(state->inode);
3269 /* Serialization for the sequence id */
3270 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3271 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
3272 if (IS_ERR(calldata->arg.seqid))
3273 goto out_free_calldata;
3274 calldata->arg.fmode = 0;
3275 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
3276 calldata->res.fattr = &calldata->fattr;
3277 calldata->res.seqid = calldata->arg.seqid;
3278 calldata->res.server = server;
3279 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3280 calldata->lr.roc = pnfs_roc(state->inode,
3281 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3282 if (calldata->lr.roc) {
3283 calldata->arg.lr_args = &calldata->lr.arg;
3284 calldata->res.lr_res = &calldata->lr.res;
3286 nfs_sb_active(calldata->inode->i_sb);
3288 msg.rpc_argp = &calldata->arg;
3289 msg.rpc_resp = &calldata->res;
3290 task_setup_data.callback_data = calldata;
3291 task = rpc_run_task(&task_setup_data);
3293 return PTR_ERR(task);
3296 status = rpc_wait_for_completion_task(task);
3302 nfs4_put_open_state(state);
3303 nfs4_put_state_owner(sp);
3307 static struct inode *
3308 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3309 int open_flags, struct iattr *attr, int *opened)
3311 struct nfs4_state *state;
3312 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3314 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
3316 /* Protect against concurrent sillydeletes */
3317 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
3319 nfs4_label_release_security(label);
3322 return ERR_CAST(state);
3323 return state->inode;
3326 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
3328 if (ctx->state == NULL)
3331 nfs4_close_sync(ctx->state, ctx->mode);
3333 nfs4_close_state(ctx->state, ctx->mode);
3336 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3337 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3338 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
3340 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3342 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
3343 struct nfs4_server_caps_arg args = {
3347 struct nfs4_server_caps_res res = {};
3348 struct rpc_message msg = {
3349 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
3355 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3356 FATTR4_WORD0_FH_EXPIRE_TYPE |
3357 FATTR4_WORD0_LINK_SUPPORT |
3358 FATTR4_WORD0_SYMLINK_SUPPORT |
3359 FATTR4_WORD0_ACLSUPPORT;
3361 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3363 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3365 /* Sanity check the server answers */
3366 switch (minorversion) {
3368 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3369 res.attr_bitmask[2] = 0;
3372 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3375 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3377 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
3378 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3379 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3380 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3381 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
3382 NFS_CAP_CTIME|NFS_CAP_MTIME|
3383 NFS_CAP_SECURITY_LABEL);
3384 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3385 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3386 server->caps |= NFS_CAP_ACLS;
3387 if (res.has_links != 0)
3388 server->caps |= NFS_CAP_HARDLINKS;
3389 if (res.has_symlinks != 0)
3390 server->caps |= NFS_CAP_SYMLINKS;
3391 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3392 server->caps |= NFS_CAP_FILEID;
3393 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3394 server->caps |= NFS_CAP_MODE;
3395 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3396 server->caps |= NFS_CAP_NLINK;
3397 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3398 server->caps |= NFS_CAP_OWNER;
3399 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3400 server->caps |= NFS_CAP_OWNER_GROUP;
3401 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3402 server->caps |= NFS_CAP_ATIME;
3403 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3404 server->caps |= NFS_CAP_CTIME;
3405 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3406 server->caps |= NFS_CAP_MTIME;
3407 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3408 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3409 server->caps |= NFS_CAP_SECURITY_LABEL;
3411 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3412 sizeof(server->attr_bitmask));
3413 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3415 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3416 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3417 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
3418 server->cache_consistency_bitmask[2] = 0;
3419 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3420 sizeof(server->exclcreat_bitmask));
3421 server->acl_bitmask = res.acl_bitmask;
3422 server->fh_expire_type = res.fh_expire_type;
3428 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3430 struct nfs4_exception exception = { };
3433 err = nfs4_handle_exception(server,
3434 _nfs4_server_capabilities(server, fhandle),
3436 } while (exception.retry);
3440 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3441 struct nfs_fsinfo *info)
3444 struct nfs4_lookup_root_arg args = {
3447 struct nfs4_lookup_res res = {
3449 .fattr = info->fattr,
3452 struct rpc_message msg = {
3453 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3458 bitmask[0] = nfs4_fattr_bitmap[0];
3459 bitmask[1] = nfs4_fattr_bitmap[1];
3461 * Process the label in the upcoming getfattr
3463 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3465 nfs_fattr_init(info->fattr);
3466 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3469 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3470 struct nfs_fsinfo *info)
3472 struct nfs4_exception exception = { };
3475 err = _nfs4_lookup_root(server, fhandle, info);
3476 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
3479 case -NFS4ERR_WRONGSEC:
3482 err = nfs4_handle_exception(server, err, &exception);
3484 } while (exception.retry);
3489 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3490 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3492 struct rpc_auth_create_args auth_args = {
3493 .pseudoflavor = flavor,
3495 struct rpc_auth *auth;
3498 auth = rpcauth_create(&auth_args, server->client);
3503 ret = nfs4_lookup_root(server, fhandle, info);
3509 * Retry pseudoroot lookup with various security flavors. We do this when:
3511 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3512 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3514 * Returns zero on success, or a negative NFS4ERR value, or a
3515 * negative errno value.
3517 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3518 struct nfs_fsinfo *info)
3520 /* Per 3530bis 15.33.5 */
3521 static const rpc_authflavor_t flav_array[] = {
3525 RPC_AUTH_UNIX, /* courtesy */
3528 int status = -EPERM;
3531 if (server->auth_info.flavor_len > 0) {
3532 /* try each flavor specified by user */
3533 for (i = 0; i < server->auth_info.flavor_len; i++) {
3534 status = nfs4_lookup_root_sec(server, fhandle, info,
3535 server->auth_info.flavors[i]);
3536 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3541 /* no flavors specified by user, try default list */
3542 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3543 status = nfs4_lookup_root_sec(server, fhandle, info,
3545 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3552 * -EACCESS could mean that the user doesn't have correct permissions
3553 * to access the mount. It could also mean that we tried to mount
3554 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3555 * existing mount programs don't handle -EACCES very well so it should
3556 * be mapped to -EPERM instead.
3558 if (status == -EACCES)
3564 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3565 * @server: initialized nfs_server handle
3566 * @fhandle: we fill in the pseudo-fs root file handle
3567 * @info: we fill in an FSINFO struct
3568 * @auth_probe: probe the auth flavours
3570 * Returns zero on success, or a negative errno.
3572 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
3573 struct nfs_fsinfo *info,
3579 status = nfs4_lookup_root(server, fhandle, info);
3581 if (auth_probe || status == NFS4ERR_WRONGSEC)
3582 status = server->nfs_client->cl_mvops->find_root_sec(server,
3586 status = nfs4_server_capabilities(server, fhandle);
3588 status = nfs4_do_fsinfo(server, fhandle, info);
3590 return nfs4_map_errors(status);
3593 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3594 struct nfs_fsinfo *info)
3597 struct nfs_fattr *fattr = info->fattr;
3598 struct nfs4_label *label = NULL;
3600 error = nfs4_server_capabilities(server, mntfh);
3602 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3606 label = nfs4_label_alloc(server, GFP_KERNEL);
3608 return PTR_ERR(label);
3610 error = nfs4_proc_getattr(server, mntfh, fattr, label);
3612 dprintk("nfs4_get_root: getattr error = %d\n", -error);
3613 goto err_free_label;
3616 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3617 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3618 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3621 nfs4_label_free(label);
3627 * Get locations and (maybe) other attributes of a referral.
3628 * Note that we'll actually follow the referral later when
3629 * we detect fsid mismatch in inode revalidation
3631 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3632 const struct qstr *name, struct nfs_fattr *fattr,
3633 struct nfs_fh *fhandle)
3635 int status = -ENOMEM;
3636 struct page *page = NULL;
3637 struct nfs4_fs_locations *locations = NULL;
3639 page = alloc_page(GFP_KERNEL);
3642 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3643 if (locations == NULL)
3646 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
3651 * If the fsid didn't change, this is a migration event, not a
3652 * referral. Cause us to drop into the exception handler, which
3653 * will kick off migration recovery.
3655 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
3656 dprintk("%s: server did not return a different fsid for"
3657 " a referral at %s\n", __func__, name->name);
3658 status = -NFS4ERR_MOVED;
3661 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3662 nfs_fixup_referral_attributes(&locations->fattr);
3664 /* replace the lookup nfs_fattr with the locations nfs_fattr */
3665 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
3666 memset(fhandle, 0, sizeof(struct nfs_fh));
3674 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3675 struct nfs_fattr *fattr, struct nfs4_label *label)
3677 struct nfs4_getattr_arg args = {
3679 .bitmask = server->attr_bitmask,
3681 struct nfs4_getattr_res res = {
3686 struct rpc_message msg = {
3687 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3692 args.bitmask = nfs4_bitmask(server, label);
3694 nfs_fattr_init(fattr);
3695 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3698 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3699 struct nfs_fattr *fattr, struct nfs4_label *label)
3701 struct nfs4_exception exception = { };
3704 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3705 trace_nfs4_getattr(server, fhandle, fattr, err);
3706 err = nfs4_handle_exception(server, err,
3708 } while (exception.retry);
3713 * The file is not closed if it is opened due to the a request to change
3714 * the size of the file. The open call will not be needed once the
3715 * VFS layer lookup-intents are implemented.
3717 * Close is called when the inode is destroyed.
3718 * If we haven't opened the file for O_WRONLY, we
3719 * need to in the size_change case to obtain a stateid.
3722 * Because OPEN is always done by name in nfsv4, it is
3723 * possible that we opened a different file by the same
3724 * name. We can recognize this race condition, but we
3725 * can't do anything about it besides returning an error.
3727 * This will be fixed with VFS changes (lookup-intent).
3730 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3731 struct iattr *sattr)
3733 struct inode *inode = d_inode(dentry);
3734 struct rpc_cred *cred = NULL;
3735 struct nfs_open_context *ctx = NULL;
3736 struct nfs4_label *label = NULL;
3739 if (pnfs_ld_layoutret_on_setattr(inode) &&
3740 sattr->ia_valid & ATTR_SIZE &&
3741 sattr->ia_size < i_size_read(inode))
3742 pnfs_commit_and_return_layout(inode);
3744 nfs_fattr_init(fattr);
3746 /* Deal with open(O_TRUNC) */
3747 if (sattr->ia_valid & ATTR_OPEN)
3748 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
3750 /* Optimization: if the end result is no change, don't RPC */
3751 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
3754 /* Search for an existing open(O_WRITE) file */
3755 if (sattr->ia_valid & ATTR_FILE) {
3757 ctx = nfs_file_open_context(sattr->ia_file);
3762 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3764 return PTR_ERR(label);
3766 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
3768 nfs_setattr_update_inode(inode, sattr, fattr);
3769 nfs_setsecurity(inode, fattr, label);
3771 nfs4_label_free(label);
3775 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3776 const struct qstr *name, struct nfs_fh *fhandle,
3777 struct nfs_fattr *fattr, struct nfs4_label *label)
3779 struct nfs_server *server = NFS_SERVER(dir);
3781 struct nfs4_lookup_arg args = {
3782 .bitmask = server->attr_bitmask,
3783 .dir_fh = NFS_FH(dir),
3786 struct nfs4_lookup_res res = {
3792 struct rpc_message msg = {
3793 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3798 args.bitmask = nfs4_bitmask(server, label);
3800 nfs_fattr_init(fattr);
3802 dprintk("NFS call lookup %s\n", name->name);
3803 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
3804 dprintk("NFS reply lookup: %d\n", status);
3808 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
3810 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
3811 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
3812 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3816 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3817 const struct qstr *name, struct nfs_fh *fhandle,
3818 struct nfs_fattr *fattr, struct nfs4_label *label)
3820 struct nfs4_exception exception = { };
3821 struct rpc_clnt *client = *clnt;
3824 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
3825 trace_nfs4_lookup(dir, name, err);
3827 case -NFS4ERR_BADNAME:
3830 case -NFS4ERR_MOVED:
3831 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
3832 if (err == -NFS4ERR_MOVED)
3833 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3835 case -NFS4ERR_WRONGSEC:
3837 if (client != *clnt)
3839 client = nfs4_negotiate_security(client, dir, name);
3841 return PTR_ERR(client);
3843 exception.retry = 1;
3846 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3848 } while (exception.retry);
3853 else if (client != *clnt)
3854 rpc_shutdown_client(client);
3859 static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
3860 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3861 struct nfs4_label *label)
3864 struct rpc_clnt *client = NFS_CLIENT(dir);
3866 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
3867 if (client != NFS_CLIENT(dir)) {
3868 rpc_shutdown_client(client);
3869 nfs_fixup_secinfo_attributes(fattr);
3875 nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
3876 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3878 struct rpc_clnt *client = NFS_CLIENT(dir);
3881 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
3883 return ERR_PTR(status);
3884 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
3887 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3889 struct nfs_server *server = NFS_SERVER(inode);
3890 struct nfs4_accessargs args = {
3891 .fh = NFS_FH(inode),
3892 .bitmask = server->cache_consistency_bitmask,
3894 struct nfs4_accessres res = {
3897 struct rpc_message msg = {
3898 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3901 .rpc_cred = entry->cred,
3903 int mode = entry->mask;
3907 * Determine which access bits we want to ask for...
3909 if (mode & MAY_READ)
3910 args.access |= NFS4_ACCESS_READ;
3911 if (S_ISDIR(inode->i_mode)) {
3912 if (mode & MAY_WRITE)
3913 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3914 if (mode & MAY_EXEC)
3915 args.access |= NFS4_ACCESS_LOOKUP;
3917 if (mode & MAY_WRITE)
3918 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3919 if (mode & MAY_EXEC)
3920 args.access |= NFS4_ACCESS_EXECUTE;
3923 res.fattr = nfs_alloc_fattr();
3924 if (res.fattr == NULL)
3927 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3929 nfs_access_set_mask(entry, res.access);
3930 nfs_refresh_inode(inode, res.fattr);
3932 nfs_free_fattr(res.fattr);
3936 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3938 struct nfs4_exception exception = { };
3941 err = _nfs4_proc_access(inode, entry);
3942 trace_nfs4_access(inode, err);
3943 err = nfs4_handle_exception(NFS_SERVER(inode), err,
3945 } while (exception.retry);
3950 * TODO: For the time being, we don't try to get any attributes
3951 * along with any of the zero-copy operations READ, READDIR,
3954 * In the case of the first three, we want to put the GETATTR
3955 * after the read-type operation -- this is because it is hard
3956 * to predict the length of a GETATTR response in v4, and thus
3957 * align the READ data correctly. This means that the GETATTR
3958 * may end up partially falling into the page cache, and we should
3959 * shift it into the 'tail' of the xdr_buf before processing.
3960 * To do this efficiently, we need to know the total length
3961 * of data received, which doesn't seem to be available outside
3964 * In the case of WRITE, we also want to put the GETATTR after
3965 * the operation -- in this case because we want to make sure
3966 * we get the post-operation mtime and size.
3968 * Both of these changes to the XDR layer would in fact be quite
3969 * minor, but I decided to leave them for a subsequent patch.
3971 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3972 unsigned int pgbase, unsigned int pglen)
3974 struct nfs4_readlink args = {
3975 .fh = NFS_FH(inode),
3980 struct nfs4_readlink_res res;
3981 struct rpc_message msg = {
3982 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3987 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
3990 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3991 unsigned int pgbase, unsigned int pglen)
3993 struct nfs4_exception exception = { };
3996 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3997 trace_nfs4_readlink(inode, err);
3998 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4000 } while (exception.retry);
4005 * This is just for mknod. open(O_CREAT) will always do ->open_context().
4008 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
4011 struct nfs4_label l, *ilabel = NULL;
4012 struct nfs_open_context *ctx;
4013 struct nfs4_state *state;
4016 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
4018 return PTR_ERR(ctx);
4020 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4022 sattr->ia_mode &= ~current_umask();
4023 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
4024 if (IS_ERR(state)) {
4025 status = PTR_ERR(state);
4029 nfs4_label_release_security(ilabel);
4030 put_nfs_open_context(ctx);
4034 static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
4036 struct nfs_server *server = NFS_SERVER(dir);
4037 struct nfs_removeargs args = {
4041 struct nfs_removeres res = {
4044 struct rpc_message msg = {
4045 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4051 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
4053 update_changeattr(dir, &res.cinfo);
4057 static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
4059 struct nfs4_exception exception = { };
4062 err = _nfs4_proc_remove(dir, name);
4063 trace_nfs4_remove(dir, name, err);
4064 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4066 } while (exception.retry);
4070 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
4072 struct nfs_server *server = NFS_SERVER(dir);
4073 struct nfs_removeargs *args = msg->rpc_argp;
4074 struct nfs_removeres *res = msg->rpc_resp;
4076 res->server = server;
4077 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
4078 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
4080 nfs_fattr_init(res->dir_attr);
4083 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4085 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb),
4086 &data->args.seq_args,
4091 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
4093 struct nfs_unlinkdata *data = task->tk_calldata;
4094 struct nfs_removeres *res = &data->res;
4096 if (!nfs4_sequence_done(task, &res->seq_res))
4098 if (nfs4_async_handle_error(task, res->server, NULL,
4099 &data->timeout) == -EAGAIN)
4101 update_changeattr(dir, &res->cinfo);
4105 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4107 struct nfs_server *server = NFS_SERVER(dir);
4108 struct nfs_renameargs *arg = msg->rpc_argp;
4109 struct nfs_renameres *res = msg->rpc_resp;
4111 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
4112 res->server = server;
4113 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
4116 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4118 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
4119 &data->args.seq_args,
4124 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4125 struct inode *new_dir)
4127 struct nfs_renamedata *data = task->tk_calldata;
4128 struct nfs_renameres *res = &data->res;
4130 if (!nfs4_sequence_done(task, &res->seq_res))
4132 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
4135 update_changeattr(old_dir, &res->old_cinfo);
4136 update_changeattr(new_dir, &res->new_cinfo);
4140 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4142 struct nfs_server *server = NFS_SERVER(inode);
4143 struct nfs4_link_arg arg = {
4144 .fh = NFS_FH(inode),
4145 .dir_fh = NFS_FH(dir),
4147 .bitmask = server->attr_bitmask,
4149 struct nfs4_link_res res = {
4153 struct rpc_message msg = {
4154 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4158 int status = -ENOMEM;
4160 res.fattr = nfs_alloc_fattr();
4161 if (res.fattr == NULL)
4164 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4165 if (IS_ERR(res.label)) {
4166 status = PTR_ERR(res.label);
4169 arg.bitmask = nfs4_bitmask(server, res.label);
4171 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4173 update_changeattr(dir, &res.cinfo);
4174 status = nfs_post_op_update_inode(inode, res.fattr);
4176 nfs_setsecurity(inode, res.fattr, res.label);
4180 nfs4_label_free(res.label);
4183 nfs_free_fattr(res.fattr);
4187 static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4189 struct nfs4_exception exception = { };
4192 err = nfs4_handle_exception(NFS_SERVER(inode),
4193 _nfs4_proc_link(inode, dir, name),
4195 } while (exception.retry);
4199 struct nfs4_createdata {
4200 struct rpc_message msg;
4201 struct nfs4_create_arg arg;
4202 struct nfs4_create_res res;
4204 struct nfs_fattr fattr;
4205 struct nfs4_label *label;
4208 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
4209 const struct qstr *name, struct iattr *sattr, u32 ftype)
4211 struct nfs4_createdata *data;
4213 data = kzalloc(sizeof(*data), GFP_KERNEL);
4215 struct nfs_server *server = NFS_SERVER(dir);
4217 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4218 if (IS_ERR(data->label))
4221 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4222 data->msg.rpc_argp = &data->arg;
4223 data->msg.rpc_resp = &data->res;
4224 data->arg.dir_fh = NFS_FH(dir);
4225 data->arg.server = server;
4226 data->arg.name = name;
4227 data->arg.attrs = sattr;
4228 data->arg.ftype = ftype;
4229 data->arg.bitmask = nfs4_bitmask(server, data->label);
4230 data->res.server = server;
4231 data->res.fh = &data->fh;
4232 data->res.fattr = &data->fattr;
4233 data->res.label = data->label;
4234 nfs_fattr_init(data->res.fattr);
4242 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4244 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
4245 &data->arg.seq_args, &data->res.seq_res, 1);
4247 update_changeattr(dir, &data->res.dir_cinfo);
4248 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
4253 static void nfs4_free_createdata(struct nfs4_createdata *data)
4255 nfs4_label_free(data->label);
4259 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4260 struct page *page, unsigned int len, struct iattr *sattr,
4261 struct nfs4_label *label)
4263 struct nfs4_createdata *data;
4264 int status = -ENAMETOOLONG;
4266 if (len > NFS4_MAXPATHLEN)
4270 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4274 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4275 data->arg.u.symlink.pages = &page;
4276 data->arg.u.symlink.len = len;
4277 data->arg.label = label;
4279 status = nfs4_do_create(dir, dentry, data);
4281 nfs4_free_createdata(data);
4286 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4287 struct page *page, unsigned int len, struct iattr *sattr)
4289 struct nfs4_exception exception = { };
4290 struct nfs4_label l, *label = NULL;
4293 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4296 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4297 trace_nfs4_symlink(dir, &dentry->d_name, err);
4298 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4300 } while (exception.retry);
4302 nfs4_label_release_security(label);
4306 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4307 struct iattr *sattr, struct nfs4_label *label)
4309 struct nfs4_createdata *data;
4310 int status = -ENOMEM;
4312 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4316 data->arg.label = label;
4317 status = nfs4_do_create(dir, dentry, data);
4319 nfs4_free_createdata(data);
4324 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4325 struct iattr *sattr)
4327 struct nfs4_exception exception = { };
4328 struct nfs4_label l, *label = NULL;
4331 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4333 sattr->ia_mode &= ~current_umask();
4335 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4336 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4337 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4339 } while (exception.retry);
4340 nfs4_label_release_security(label);
4345 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
4346 u64 cookie, struct page **pages, unsigned int count, int plus)
4348 struct inode *dir = d_inode(dentry);
4349 struct nfs4_readdir_arg args = {
4354 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
4357 struct nfs4_readdir_res res;
4358 struct rpc_message msg = {
4359 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4366 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4368 (unsigned long long)cookie);
4369 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
4370 res.pgbase = args.pgbase;
4371 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4373 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
4374 status += args.pgbase;
4377 nfs_invalidate_atime(dir);
4379 dprintk("%s: returns %d\n", __func__, status);
4383 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
4384 u64 cookie, struct page **pages, unsigned int count, int plus)
4386 struct nfs4_exception exception = { };
4389 err = _nfs4_proc_readdir(dentry, cred, cookie,
4390 pages, count, plus);
4391 trace_nfs4_readdir(d_inode(dentry), err);
4392 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
4394 } while (exception.retry);
4398 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4399 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
4401 struct nfs4_createdata *data;
4402 int mode = sattr->ia_mode;
4403 int status = -ENOMEM;
4405 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4410 data->arg.ftype = NF4FIFO;
4411 else if (S_ISBLK(mode)) {
4412 data->arg.ftype = NF4BLK;
4413 data->arg.u.device.specdata1 = MAJOR(rdev);
4414 data->arg.u.device.specdata2 = MINOR(rdev);
4416 else if (S_ISCHR(mode)) {
4417 data->arg.ftype = NF4CHR;
4418 data->arg.u.device.specdata1 = MAJOR(rdev);
4419 data->arg.u.device.specdata2 = MINOR(rdev);
4420 } else if (!S_ISSOCK(mode)) {
4425 data->arg.label = label;
4426 status = nfs4_do_create(dir, dentry, data);
4428 nfs4_free_createdata(data);
4433 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4434 struct iattr *sattr, dev_t rdev)
4436 struct nfs4_exception exception = { };
4437 struct nfs4_label l, *label = NULL;
4440 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4442 sattr->ia_mode &= ~current_umask();
4444 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4445 trace_nfs4_mknod(dir, &dentry->d_name, err);
4446 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4448 } while (exception.retry);
4450 nfs4_label_release_security(label);
4455 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4456 struct nfs_fsstat *fsstat)
4458 struct nfs4_statfs_arg args = {
4460 .bitmask = server->attr_bitmask,
4462 struct nfs4_statfs_res res = {
4465 struct rpc_message msg = {
4466 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4471 nfs_fattr_init(fsstat->fattr);
4472 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4475 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4477 struct nfs4_exception exception = { };
4480 err = nfs4_handle_exception(server,
4481 _nfs4_proc_statfs(server, fhandle, fsstat),
4483 } while (exception.retry);
4487 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4488 struct nfs_fsinfo *fsinfo)
4490 struct nfs4_fsinfo_arg args = {
4492 .bitmask = server->attr_bitmask,
4494 struct nfs4_fsinfo_res res = {
4497 struct rpc_message msg = {
4498 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4503 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4506 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4508 struct nfs4_exception exception = { };
4509 unsigned long now = jiffies;
4513 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
4514 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
4516 nfs4_set_lease_period(server->nfs_client,
4517 fsinfo->lease_time * HZ,
4521 err = nfs4_handle_exception(server, err, &exception);
4522 } while (exception.retry);
4526 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4530 nfs_fattr_init(fsinfo->fattr);
4531 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
4533 /* block layout checks this! */
4534 server->pnfs_blksize = fsinfo->blksize;
4535 set_pnfs_layoutdriver(server, fhandle, fsinfo);
4541 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4542 struct nfs_pathconf *pathconf)
4544 struct nfs4_pathconf_arg args = {
4546 .bitmask = server->attr_bitmask,
4548 struct nfs4_pathconf_res res = {
4549 .pathconf = pathconf,
4551 struct rpc_message msg = {
4552 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4557 /* None of the pathconf attributes are mandatory to implement */
4558 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4559 memset(pathconf, 0, sizeof(*pathconf));
4563 nfs_fattr_init(pathconf->fattr);
4564 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4567 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4568 struct nfs_pathconf *pathconf)
4570 struct nfs4_exception exception = { };
4574 err = nfs4_handle_exception(server,
4575 _nfs4_proc_pathconf(server, fhandle, pathconf),
4577 } while (exception.retry);
4581 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
4582 const struct nfs_open_context *ctx,
4583 const struct nfs_lock_context *l_ctx,
4586 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
4588 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4590 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4591 const struct nfs_open_context *ctx,
4592 const struct nfs_lock_context *l_ctx,
4595 nfs4_stateid current_stateid;
4597 /* If the current stateid represents a lost lock, then exit */
4598 if (nfs4_set_rw_stateid(¤t_stateid, ctx, l_ctx, fmode) == -EIO)
4600 return nfs4_stateid_match(stateid, ¤t_stateid);
4603 static bool nfs4_error_stateid_expired(int err)
4606 case -NFS4ERR_DELEG_REVOKED:
4607 case -NFS4ERR_ADMIN_REVOKED:
4608 case -NFS4ERR_BAD_STATEID:
4609 case -NFS4ERR_STALE_STATEID:
4610 case -NFS4ERR_OLD_STATEID:
4611 case -NFS4ERR_OPENMODE:
4612 case -NFS4ERR_EXPIRED:
4618 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
4620 struct nfs_server *server = NFS_SERVER(hdr->inode);
4622 trace_nfs4_read(hdr, task->tk_status);
4623 if (task->tk_status < 0) {
4624 struct nfs4_exception exception = {
4625 .inode = hdr->inode,
4626 .state = hdr->args.context->state,
4627 .stateid = &hdr->args.stateid,
4629 task->tk_status = nfs4_async_handle_exception(task,
4630 server, task->tk_status, &exception);
4631 if (exception.retry) {
4632 rpc_restart_call_prepare(task);
4637 if (task->tk_status > 0)
4638 renew_lease(server, hdr->timestamp);
4642 static bool nfs4_read_stateid_changed(struct rpc_task *task,
4643 struct nfs_pgio_args *args)
4646 if (!nfs4_error_stateid_expired(task->tk_status) ||
4647 nfs4_stateid_is_current(&args->stateid,
4652 rpc_restart_call_prepare(task);
4656 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
4659 dprintk("--> %s\n", __func__);
4661 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
4663 if (nfs4_read_stateid_changed(task, &hdr->args))
4665 if (task->tk_status > 0)
4666 nfs_invalidate_atime(hdr->inode);
4667 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4668 nfs4_read_done_cb(task, hdr);
4671 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4672 struct rpc_message *msg)
4674 hdr->timestamp = jiffies;
4675 if (!hdr->pgio_done_cb)
4676 hdr->pgio_done_cb = nfs4_read_done_cb;
4677 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
4678 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
4681 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4682 struct nfs_pgio_header *hdr)
4684 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4685 &hdr->args.seq_args,
4689 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4690 hdr->args.lock_context,
4691 hdr->rw_ops->rw_mode) == -EIO)
4693 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
4698 static int nfs4_write_done_cb(struct rpc_task *task,
4699 struct nfs_pgio_header *hdr)
4701 struct inode *inode = hdr->inode;
4703 trace_nfs4_write(hdr, task->tk_status);
4704 if (task->tk_status < 0) {
4705 struct nfs4_exception exception = {
4706 .inode = hdr->inode,
4707 .state = hdr->args.context->state,
4708 .stateid = &hdr->args.stateid,
4710 task->tk_status = nfs4_async_handle_exception(task,
4711 NFS_SERVER(inode), task->tk_status,
4713 if (exception.retry) {
4714 rpc_restart_call_prepare(task);
4718 if (task->tk_status >= 0) {
4719 renew_lease(NFS_SERVER(inode), hdr->timestamp);
4720 nfs_writeback_update_inode(hdr);
4725 static bool nfs4_write_stateid_changed(struct rpc_task *task,
4726 struct nfs_pgio_args *args)
4729 if (!nfs4_error_stateid_expired(task->tk_status) ||
4730 nfs4_stateid_is_current(&args->stateid,
4735 rpc_restart_call_prepare(task);
4739 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
4741 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
4743 if (nfs4_write_stateid_changed(task, &hdr->args))
4745 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4746 nfs4_write_done_cb(task, hdr);
4750 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
4752 /* Don't request attributes for pNFS or O_DIRECT writes */
4753 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
4755 /* Otherwise, request attributes if and only if we don't hold
4758 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
4761 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4762 struct rpc_message *msg)
4764 struct nfs_server *server = NFS_SERVER(hdr->inode);
4766 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4767 hdr->args.bitmask = NULL;
4768 hdr->res.fattr = NULL;
4770 hdr->args.bitmask = server->cache_consistency_bitmask;
4772 if (!hdr->pgio_done_cb)
4773 hdr->pgio_done_cb = nfs4_write_done_cb;
4774 hdr->res.server = server;
4775 hdr->timestamp = jiffies;
4777 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
4778 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
4781 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4783 nfs4_setup_sequence(NFS_SERVER(data->inode),
4784 &data->args.seq_args,
4789 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
4791 struct inode *inode = data->inode;
4793 trace_nfs4_commit(data, task->tk_status);
4794 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4795 NULL, NULL) == -EAGAIN) {
4796 rpc_restart_call_prepare(task);
4802 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
4804 if (!nfs4_sequence_done(task, &data->res.seq_res))
4806 return data->commit_done_cb(task, data);
4809 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
4811 struct nfs_server *server = NFS_SERVER(data->inode);
4813 if (data->commit_done_cb == NULL)
4814 data->commit_done_cb = nfs4_commit_done_cb;
4815 data->res.server = server;
4816 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
4817 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4820 struct nfs4_renewdata {
4821 struct nfs_client *client;
4822 unsigned long timestamp;
4826 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4827 * standalone procedure for queueing an asynchronous RENEW.
4829 static void nfs4_renew_release(void *calldata)
4831 struct nfs4_renewdata *data = calldata;
4832 struct nfs_client *clp = data->client;
4834 if (atomic_read(&clp->cl_count) > 1)
4835 nfs4_schedule_state_renewal(clp);
4836 nfs_put_client(clp);
4840 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
4842 struct nfs4_renewdata *data = calldata;
4843 struct nfs_client *clp = data->client;
4844 unsigned long timestamp = data->timestamp;
4846 trace_nfs4_renew_async(clp, task->tk_status);
4847 switch (task->tk_status) {
4850 case -NFS4ERR_LEASE_MOVED:
4851 nfs4_schedule_lease_moved_recovery(clp);
4854 /* Unless we're shutting down, schedule state recovery! */
4855 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4857 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
4858 nfs4_schedule_lease_recovery(clp);
4861 nfs4_schedule_path_down_recovery(clp);
4863 do_renew_lease(clp, timestamp);
4866 static const struct rpc_call_ops nfs4_renew_ops = {
4867 .rpc_call_done = nfs4_renew_done,
4868 .rpc_release = nfs4_renew_release,
4871 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
4873 struct rpc_message msg = {
4874 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4878 struct nfs4_renewdata *data;
4880 if (renew_flags == 0)
4882 if (!atomic_inc_not_zero(&clp->cl_count))
4884 data = kmalloc(sizeof(*data), GFP_NOFS);
4888 data->timestamp = jiffies;
4889 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
4890 &nfs4_renew_ops, data);
4893 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
4895 struct rpc_message msg = {
4896 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4900 unsigned long now = jiffies;
4903 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4906 do_renew_lease(clp, now);
4910 static inline int nfs4_server_supports_acls(struct nfs_server *server)
4912 return server->caps & NFS_CAP_ACLS;
4915 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4916 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
4919 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
4921 static int buf_to_pages_noslab(const void *buf, size_t buflen,
4922 struct page **pages)
4924 struct page *newpage, **spages;
4930 len = min_t(size_t, PAGE_SIZE, buflen);
4931 newpage = alloc_page(GFP_KERNEL);
4933 if (newpage == NULL)
4935 memcpy(page_address(newpage), buf, len);
4940 } while (buflen != 0);
4946 __free_page(spages[rc-1]);
4950 struct nfs4_cached_acl {
4956 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
4958 struct nfs_inode *nfsi = NFS_I(inode);
4960 spin_lock(&inode->i_lock);
4961 kfree(nfsi->nfs4_acl);
4962 nfsi->nfs4_acl = acl;
4963 spin_unlock(&inode->i_lock);
4966 static void nfs4_zap_acl_attr(struct inode *inode)
4968 nfs4_set_cached_acl(inode, NULL);
4971 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4973 struct nfs_inode *nfsi = NFS_I(inode);
4974 struct nfs4_cached_acl *acl;
4977 spin_lock(&inode->i_lock);
4978 acl = nfsi->nfs4_acl;
4981 if (buf == NULL) /* user is just asking for length */
4983 if (acl->cached == 0)
4985 ret = -ERANGE; /* see getxattr(2) man page */
4986 if (acl->len > buflen)
4988 memcpy(buf, acl->data, acl->len);
4992 spin_unlock(&inode->i_lock);
4996 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
4998 struct nfs4_cached_acl *acl;
4999 size_t buflen = sizeof(*acl) + acl_len;
5001 if (buflen <= PAGE_SIZE) {
5002 acl = kmalloc(buflen, GFP_KERNEL);
5006 _copy_from_pages(acl->data, pages, pgbase, acl_len);
5008 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5015 nfs4_set_cached_acl(inode, acl);
5019 * The getxattr API returns the required buffer length when called with a
5020 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5021 * the required buf. On a NULL buf, we send a page of data to the server
5022 * guessing that the ACL request can be serviced by a page. If so, we cache
5023 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5024 * the cache. If not so, we throw away the page, and cache the required
5025 * length. The next getxattr call will then produce another round trip to
5026 * the server, this time with the input buf of the required size.
5028 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5030 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
5031 struct nfs_getaclargs args = {
5032 .fh = NFS_FH(inode),
5036 struct nfs_getaclres res = {
5039 struct rpc_message msg = {
5040 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5044 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
5045 int ret = -ENOMEM, i;
5047 /* As long as we're doing a round trip to the server anyway,
5048 * let's be prepared for a page of acl data. */
5051 if (npages > ARRAY_SIZE(pages))
5054 for (i = 0; i < npages; i++) {
5055 pages[i] = alloc_page(GFP_KERNEL);
5060 /* for decoding across pages */
5061 res.acl_scratch = alloc_page(GFP_KERNEL);
5062 if (!res.acl_scratch)
5065 args.acl_len = npages * PAGE_SIZE;
5067 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
5068 __func__, buf, buflen, npages, args.acl_len);
5069 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5070 &msg, &args.seq_args, &res.seq_res, 0);
5074 /* Handle the case where the passed-in buffer is too short */
5075 if (res.acl_flags & NFS4_ACL_TRUNC) {
5076 /* Did the user only issue a request for the acl length? */
5082 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
5084 if (res.acl_len > buflen) {
5088 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
5093 for (i = 0; i < npages; i++)
5095 __free_page(pages[i]);
5096 if (res.acl_scratch)
5097 __free_page(res.acl_scratch);
5101 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5103 struct nfs4_exception exception = { };
5106 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
5107 trace_nfs4_get_acl(inode, ret);
5110 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5111 } while (exception.retry);
5115 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5117 struct nfs_server *server = NFS_SERVER(inode);
5120 if (!nfs4_server_supports_acls(server))
5122 ret = nfs_revalidate_inode(server, inode);
5125 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5126 nfs_zap_acl_cache(inode);
5127 ret = nfs4_read_cached_acl(inode, buf, buflen);
5129 /* -ENOENT is returned if there is no ACL or if there is an ACL
5130 * but no cached acl data, just the acl length */
5132 return nfs4_get_acl_uncached(inode, buf, buflen);
5135 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5137 struct nfs_server *server = NFS_SERVER(inode);
5138 struct page *pages[NFS4ACL_MAXPAGES];
5139 struct nfs_setaclargs arg = {
5140 .fh = NFS_FH(inode),
5144 struct nfs_setaclres res;
5145 struct rpc_message msg = {
5146 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5150 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
5153 if (!nfs4_server_supports_acls(server))
5155 if (npages > ARRAY_SIZE(pages))
5157 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
5160 nfs4_inode_return_delegation(inode);
5161 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5164 * Free each page after tx, so the only ref left is
5165 * held by the network stack
5168 put_page(pages[i-1]);
5171 * Acl update can result in inode attribute update.
5172 * so mark the attribute cache invalid.
5174 spin_lock(&inode->i_lock);
5175 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5176 spin_unlock(&inode->i_lock);
5177 nfs_access_zap_cache(inode);
5178 nfs_zap_acl_cache(inode);
5182 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5184 struct nfs4_exception exception = { };
5187 err = __nfs4_proc_set_acl(inode, buf, buflen);
5188 trace_nfs4_set_acl(inode, err);
5189 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5191 } while (exception.retry);
5195 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
5196 static int _nfs4_get_security_label(struct inode *inode, void *buf,
5199 struct nfs_server *server = NFS_SERVER(inode);
5200 struct nfs_fattr fattr;
5201 struct nfs4_label label = {0, 0, buflen, buf};
5203 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5204 struct nfs4_getattr_arg arg = {
5205 .fh = NFS_FH(inode),
5208 struct nfs4_getattr_res res = {
5213 struct rpc_message msg = {
5214 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
5220 nfs_fattr_init(&fattr);
5222 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
5225 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5227 if (buflen < label.len)
5232 static int nfs4_get_security_label(struct inode *inode, void *buf,
5235 struct nfs4_exception exception = { };
5238 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5242 err = _nfs4_get_security_label(inode, buf, buflen);
5243 trace_nfs4_get_security_label(inode, err);
5244 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5246 } while (exception.retry);
5250 static int _nfs4_do_set_security_label(struct inode *inode,
5251 struct nfs4_label *ilabel,
5252 struct nfs_fattr *fattr,
5253 struct nfs4_label *olabel)
5256 struct iattr sattr = {0};
5257 struct nfs_server *server = NFS_SERVER(inode);
5258 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5259 struct nfs_setattrargs arg = {
5260 .fh = NFS_FH(inode),
5266 struct nfs_setattrres res = {
5271 struct rpc_message msg = {
5272 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5278 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
5280 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5282 dprintk("%s failed: %d\n", __func__, status);
5287 static int nfs4_do_set_security_label(struct inode *inode,
5288 struct nfs4_label *ilabel,
5289 struct nfs_fattr *fattr,
5290 struct nfs4_label *olabel)
5292 struct nfs4_exception exception = { };
5296 err = _nfs4_do_set_security_label(inode, ilabel,
5298 trace_nfs4_set_security_label(inode, err);
5299 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5301 } while (exception.retry);
5306 nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
5308 struct nfs4_label ilabel, *olabel = NULL;
5309 struct nfs_fattr fattr;
5310 struct rpc_cred *cred;
5313 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5316 nfs_fattr_init(&fattr);
5320 ilabel.label = (char *)buf;
5321 ilabel.len = buflen;
5323 cred = rpc_lookup_cred();
5325 return PTR_ERR(cred);
5327 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5328 if (IS_ERR(olabel)) {
5329 status = -PTR_ERR(olabel);
5333 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5335 nfs_setsecurity(inode, &fattr, olabel);
5337 nfs4_label_free(olabel);
5342 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5345 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5346 nfs4_verifier *bootverf)
5350 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5351 /* An impossible timestamp guarantees this value
5352 * will never match a generated boot time. */
5353 verf[0] = cpu_to_be32(U32_MAX);
5354 verf[1] = cpu_to_be32(U32_MAX);
5356 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
5357 u64 ns = ktime_to_ns(nn->boot_time);
5359 verf[0] = cpu_to_be32(ns >> 32);
5360 verf[1] = cpu_to_be32(ns);
5362 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5366 nfs4_init_nonuniform_client_string(struct nfs_client *clp)
5371 if (clp->cl_owner_id != NULL)
5375 len = 14 + strlen(clp->cl_ipaddr) + 1 +
5376 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5378 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5382 if (len > NFS4_OPAQUE_LIMIT + 1)
5386 * Since this string is allocated at mount time, and held until the
5387 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5388 * about a memory-reclaim deadlock.
5390 str = kmalloc(len, GFP_KERNEL);
5395 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
5397 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5398 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
5401 clp->cl_owner_id = str;
5406 nfs4_init_uniquifier_client_string(struct nfs_client *clp)
5411 len = 10 + 10 + 1 + 10 + 1 +
5412 strlen(nfs4_client_id_uniquifier) + 1 +
5413 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5415 if (len > NFS4_OPAQUE_LIMIT + 1)
5419 * Since this string is allocated at mount time, and held until the
5420 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5421 * about a memory-reclaim deadlock.
5423 str = kmalloc(len, GFP_KERNEL);
5427 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
5428 clp->rpc_ops->version, clp->cl_minorversion,
5429 nfs4_client_id_uniquifier,
5430 clp->cl_rpcclient->cl_nodename);
5431 clp->cl_owner_id = str;
5436 nfs4_init_uniform_client_string(struct nfs_client *clp)
5441 if (clp->cl_owner_id != NULL)
5444 if (nfs4_client_id_uniquifier[0] != '\0')
5445 return nfs4_init_uniquifier_client_string(clp);
5447 len = 10 + 10 + 1 + 10 + 1 +
5448 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5450 if (len > NFS4_OPAQUE_LIMIT + 1)
5454 * Since this string is allocated at mount time, and held until the
5455 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5456 * about a memory-reclaim deadlock.
5458 str = kmalloc(len, GFP_KERNEL);
5462 scnprintf(str, len, "Linux NFSv%u.%u %s",
5463 clp->rpc_ops->version, clp->cl_minorversion,
5464 clp->cl_rpcclient->cl_nodename);
5465 clp->cl_owner_id = str;
5470 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5471 * services. Advertise one based on the address family of the
5475 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5477 if (strchr(clp->cl_ipaddr, ':') != NULL)
5478 return scnprintf(buf, len, "tcp6");
5480 return scnprintf(buf, len, "tcp");
5483 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5485 struct nfs4_setclientid *sc = calldata;
5487 if (task->tk_status == 0)
5488 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5491 static const struct rpc_call_ops nfs4_setclientid_ops = {
5492 .rpc_call_done = nfs4_setclientid_done,
5496 * nfs4_proc_setclientid - Negotiate client ID
5497 * @clp: state data structure
5498 * @program: RPC program for NFSv4 callback service
5499 * @port: IP port number for NFS4 callback service
5500 * @cred: RPC credential to use for this call
5501 * @res: where to place the result
5503 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5505 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5506 unsigned short port, struct rpc_cred *cred,
5507 struct nfs4_setclientid_res *res)
5509 nfs4_verifier sc_verifier;
5510 struct nfs4_setclientid setclientid = {
5511 .sc_verifier = &sc_verifier,
5515 struct rpc_message msg = {
5516 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5517 .rpc_argp = &setclientid,
5521 struct rpc_task *task;
5522 struct rpc_task_setup task_setup_data = {
5523 .rpc_client = clp->cl_rpcclient,
5524 .rpc_message = &msg,
5525 .callback_ops = &nfs4_setclientid_ops,
5526 .callback_data = &setclientid,
5527 .flags = RPC_TASK_TIMEOUT,
5531 /* nfs_client_id4 */
5532 nfs4_init_boot_verifier(clp, &sc_verifier);
5534 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5535 status = nfs4_init_uniform_client_string(clp);
5537 status = nfs4_init_nonuniform_client_string(clp);
5543 setclientid.sc_netid_len =
5544 nfs4_init_callback_netid(clp,
5545 setclientid.sc_netid,
5546 sizeof(setclientid.sc_netid));
5547 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
5548 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
5549 clp->cl_ipaddr, port >> 8, port & 255);
5551 dprintk("NFS call setclientid auth=%s, '%s'\n",
5552 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5554 task = rpc_run_task(&task_setup_data);
5556 status = PTR_ERR(task);
5559 status = task->tk_status;
5560 if (setclientid.sc_cred) {
5561 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5562 put_rpccred(setclientid.sc_cred);
5566 trace_nfs4_setclientid(clp, status);
5567 dprintk("NFS reply setclientid: %d\n", status);
5572 * nfs4_proc_setclientid_confirm - Confirm client ID
5573 * @clp: state data structure
5574 * @res: result of a previous SETCLIENTID
5575 * @cred: RPC credential to use for this call
5577 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5579 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
5580 struct nfs4_setclientid_res *arg,
5581 struct rpc_cred *cred)
5583 struct rpc_message msg = {
5584 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
5590 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5591 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5593 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5594 trace_nfs4_setclientid_confirm(clp, status);
5595 dprintk("NFS reply setclientid_confirm: %d\n", status);
5599 struct nfs4_delegreturndata {
5600 struct nfs4_delegreturnargs args;
5601 struct nfs4_delegreturnres res;
5603 nfs4_stateid stateid;
5604 unsigned long timestamp;
5606 struct nfs4_layoutreturn_args arg;
5607 struct nfs4_layoutreturn_res res;
5608 struct nfs4_xdr_opaque_data ld_private;
5612 struct nfs_fattr fattr;
5614 struct inode *inode;
5617 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5619 struct nfs4_delegreturndata *data = calldata;
5621 if (!nfs4_sequence_done(task, &data->res.seq_res))
5624 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
5626 /* Handle Layoutreturn errors */
5627 if (data->args.lr_args && task->tk_status != 0) {
5628 switch(data->res.lr_ret) {
5630 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5633 data->args.lr_args = NULL;
5634 data->res.lr_res = NULL;
5636 case -NFS4ERR_ADMIN_REVOKED:
5637 case -NFS4ERR_DELEG_REVOKED:
5638 case -NFS4ERR_EXPIRED:
5639 case -NFS4ERR_BAD_STATEID:
5640 case -NFS4ERR_OLD_STATEID:
5641 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
5642 case -NFS4ERR_WRONG_CRED:
5643 data->args.lr_args = NULL;
5644 data->res.lr_res = NULL;
5645 data->res.lr_ret = 0;
5646 rpc_restart_call_prepare(task);
5651 switch (task->tk_status) {
5653 renew_lease(data->res.server, data->timestamp);
5655 case -NFS4ERR_ADMIN_REVOKED:
5656 case -NFS4ERR_DELEG_REVOKED:
5657 case -NFS4ERR_EXPIRED:
5658 nfs4_free_revoked_stateid(data->res.server,
5660 task->tk_msg.rpc_cred);
5661 case -NFS4ERR_BAD_STATEID:
5662 case -NFS4ERR_OLD_STATEID:
5663 case -NFS4ERR_STALE_STATEID:
5664 task->tk_status = 0;
5667 if (nfs4_async_handle_error(task, data->res.server,
5668 NULL, NULL) == -EAGAIN) {
5669 rpc_restart_call_prepare(task);
5673 data->rpc_status = task->tk_status;
5676 static void nfs4_delegreturn_release(void *calldata)
5678 struct nfs4_delegreturndata *data = calldata;
5679 struct inode *inode = data->inode;
5683 pnfs_roc_release(&data->lr.arg, &data->lr.res,
5685 nfs_iput_and_deactive(inode);
5690 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5692 struct nfs4_delegreturndata *d_data;
5694 d_data = (struct nfs4_delegreturndata *)data;
5696 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
5699 nfs4_setup_sequence(d_data->res.server,
5700 &d_data->args.seq_args,
5701 &d_data->res.seq_res,
5705 static const struct rpc_call_ops nfs4_delegreturn_ops = {
5706 .rpc_call_prepare = nfs4_delegreturn_prepare,
5707 .rpc_call_done = nfs4_delegreturn_done,
5708 .rpc_release = nfs4_delegreturn_release,
5711 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
5713 struct nfs4_delegreturndata *data;
5714 struct nfs_server *server = NFS_SERVER(inode);
5715 struct rpc_task *task;
5716 struct rpc_message msg = {
5717 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5720 struct rpc_task_setup task_setup_data = {
5721 .rpc_client = server->client,
5722 .rpc_message = &msg,
5723 .callback_ops = &nfs4_delegreturn_ops,
5724 .flags = RPC_TASK_ASYNC,
5728 data = kzalloc(sizeof(*data), GFP_NOFS);
5731 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
5733 nfs4_state_protect(server->nfs_client,
5734 NFS_SP4_MACH_CRED_CLEANUP,
5735 &task_setup_data.rpc_client, &msg);
5737 data->args.fhandle = &data->fh;
5738 data->args.stateid = &data->stateid;
5739 data->args.bitmask = server->cache_consistency_bitmask;
5740 nfs_copy_fh(&data->fh, NFS_FH(inode));
5741 nfs4_stateid_copy(&data->stateid, stateid);
5742 data->res.fattr = &data->fattr;
5743 data->res.server = server;
5744 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5745 data->lr.arg.ld_private = &data->lr.ld_private;
5746 nfs_fattr_init(data->res.fattr);
5747 data->timestamp = jiffies;
5748 data->rpc_status = 0;
5749 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
5750 data->inode = nfs_igrab_and_active(inode);
5753 data->args.lr_args = &data->lr.arg;
5754 data->res.lr_res = &data->lr.res;
5756 } else if (data->lr.roc) {
5757 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
5758 data->lr.roc = false;
5761 task_setup_data.callback_data = data;
5762 msg.rpc_argp = &data->args;
5763 msg.rpc_resp = &data->res;
5764 task = rpc_run_task(&task_setup_data);
5766 return PTR_ERR(task);
5769 status = nfs4_wait_for_completion_rpc_task(task);
5772 status = data->rpc_status;
5774 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5776 nfs_refresh_inode(inode, &data->fattr);
5782 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
5784 struct nfs_server *server = NFS_SERVER(inode);
5785 struct nfs4_exception exception = { };
5788 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
5789 trace_nfs4_delegreturn(inode, stateid, err);
5791 case -NFS4ERR_STALE_STATEID:
5792 case -NFS4ERR_EXPIRED:
5796 err = nfs4_handle_exception(server, err, &exception);
5797 } while (exception.retry);
5801 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5803 struct inode *inode = state->inode;
5804 struct nfs_server *server = NFS_SERVER(inode);
5805 struct nfs_client *clp = server->nfs_client;
5806 struct nfs_lockt_args arg = {
5807 .fh = NFS_FH(inode),
5810 struct nfs_lockt_res res = {
5813 struct rpc_message msg = {
5814 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5817 .rpc_cred = state->owner->so_cred,
5819 struct nfs4_lock_state *lsp;
5822 arg.lock_owner.clientid = clp->cl_clientid;
5823 status = nfs4_set_lock_state(state, request);
5826 lsp = request->fl_u.nfs4_fl.owner;
5827 arg.lock_owner.id = lsp->ls_seqid.owner_id;
5828 arg.lock_owner.s_dev = server->s_dev;
5829 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5832 request->fl_type = F_UNLCK;
5834 case -NFS4ERR_DENIED:
5837 request->fl_ops->fl_release_private(request);
5838 request->fl_ops = NULL;
5843 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5845 struct nfs4_exception exception = { };
5849 err = _nfs4_proc_getlk(state, cmd, request);
5850 trace_nfs4_get_lock(request, state, cmd, err);
5851 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
5853 } while (exception.retry);
5857 struct nfs4_unlockdata {
5858 struct nfs_locku_args arg;
5859 struct nfs_locku_res res;
5860 struct nfs4_lock_state *lsp;
5861 struct nfs_open_context *ctx;
5862 struct file_lock fl;
5863 struct nfs_server *server;
5864 unsigned long timestamp;
5867 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5868 struct nfs_open_context *ctx,
5869 struct nfs4_lock_state *lsp,
5870 struct nfs_seqid *seqid)
5872 struct nfs4_unlockdata *p;
5873 struct inode *inode = lsp->ls_state->inode;
5875 p = kzalloc(sizeof(*p), GFP_NOFS);
5878 p->arg.fh = NFS_FH(inode);
5880 p->arg.seqid = seqid;
5881 p->res.seqid = seqid;
5883 atomic_inc(&lsp->ls_count);
5884 /* Ensure we don't close file until we're done freeing locks! */
5885 p->ctx = get_nfs_open_context(ctx);
5886 memcpy(&p->fl, fl, sizeof(p->fl));
5887 p->server = NFS_SERVER(inode);
5891 static void nfs4_locku_release_calldata(void *data)
5893 struct nfs4_unlockdata *calldata = data;
5894 nfs_free_seqid(calldata->arg.seqid);
5895 nfs4_put_lock_state(calldata->lsp);
5896 put_nfs_open_context(calldata->ctx);
5900 static void nfs4_locku_done(struct rpc_task *task, void *data)
5902 struct nfs4_unlockdata *calldata = data;
5904 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5906 switch (task->tk_status) {
5908 renew_lease(calldata->server, calldata->timestamp);
5909 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
5910 if (nfs4_update_lock_stateid(calldata->lsp,
5911 &calldata->res.stateid))
5913 case -NFS4ERR_ADMIN_REVOKED:
5914 case -NFS4ERR_EXPIRED:
5915 nfs4_free_revoked_stateid(calldata->server,
5916 &calldata->arg.stateid,
5917 task->tk_msg.rpc_cred);
5918 case -NFS4ERR_BAD_STATEID:
5919 case -NFS4ERR_OLD_STATEID:
5920 case -NFS4ERR_STALE_STATEID:
5921 if (!nfs4_stateid_match(&calldata->arg.stateid,
5922 &calldata->lsp->ls_stateid))
5923 rpc_restart_call_prepare(task);
5926 if (nfs4_async_handle_error(task, calldata->server,
5927 NULL, NULL) == -EAGAIN)
5928 rpc_restart_call_prepare(task);
5930 nfs_release_seqid(calldata->arg.seqid);
5933 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
5935 struct nfs4_unlockdata *calldata = data;
5937 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
5939 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
5940 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
5941 /* Note: exit _without_ running nfs4_locku_done */
5944 calldata->timestamp = jiffies;
5945 if (nfs4_setup_sequence(calldata->server,
5946 &calldata->arg.seq_args,
5947 &calldata->res.seq_res,
5949 nfs_release_seqid(calldata->arg.seqid);
5952 task->tk_action = NULL;
5954 nfs4_sequence_done(task, &calldata->res.seq_res);
5957 static const struct rpc_call_ops nfs4_locku_ops = {
5958 .rpc_call_prepare = nfs4_locku_prepare,
5959 .rpc_call_done = nfs4_locku_done,
5960 .rpc_release = nfs4_locku_release_calldata,
5963 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5964 struct nfs_open_context *ctx,
5965 struct nfs4_lock_state *lsp,
5966 struct nfs_seqid *seqid)
5968 struct nfs4_unlockdata *data;
5969 struct rpc_message msg = {
5970 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5971 .rpc_cred = ctx->cred,
5973 struct rpc_task_setup task_setup_data = {
5974 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
5975 .rpc_message = &msg,
5976 .callback_ops = &nfs4_locku_ops,
5977 .workqueue = nfsiod_workqueue,
5978 .flags = RPC_TASK_ASYNC,
5981 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5982 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5984 /* Ensure this is an unlock - when canceling a lock, the
5985 * canceled lock is passed in, and it won't be an unlock.
5987 fl->fl_type = F_UNLCK;
5989 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5991 nfs_free_seqid(seqid);
5992 return ERR_PTR(-ENOMEM);
5995 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
5996 msg.rpc_argp = &data->arg;
5997 msg.rpc_resp = &data->res;
5998 task_setup_data.callback_data = data;
5999 return rpc_run_task(&task_setup_data);
6002 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6004 struct inode *inode = state->inode;
6005 struct nfs4_state_owner *sp = state->owner;
6006 struct nfs_inode *nfsi = NFS_I(inode);
6007 struct nfs_seqid *seqid;
6008 struct nfs4_lock_state *lsp;
6009 struct rpc_task *task;
6010 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6012 unsigned char fl_flags = request->fl_flags;
6014 status = nfs4_set_lock_state(state, request);
6015 /* Unlock _before_ we do the RPC call */
6016 request->fl_flags |= FL_EXISTS;
6017 /* Exclude nfs_delegation_claim_locks() */
6018 mutex_lock(&sp->so_delegreturn_mutex);
6019 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
6020 down_read(&nfsi->rwsem);
6021 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
6022 up_read(&nfsi->rwsem);
6023 mutex_unlock(&sp->so_delegreturn_mutex);
6026 up_read(&nfsi->rwsem);
6027 mutex_unlock(&sp->so_delegreturn_mutex);
6030 /* Is this a delegated lock? */
6031 lsp = request->fl_u.nfs4_fl.owner;
6032 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6034 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6035 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
6039 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
6040 status = PTR_ERR(task);
6043 status = nfs4_wait_for_completion_rpc_task(task);
6046 request->fl_flags = fl_flags;
6047 trace_nfs4_unlock(request, state, F_SETLK, status);
6051 struct nfs4_lockdata {
6052 struct nfs_lock_args arg;
6053 struct nfs_lock_res res;
6054 struct nfs4_lock_state *lsp;
6055 struct nfs_open_context *ctx;
6056 struct file_lock fl;
6057 unsigned long timestamp;
6060 struct nfs_server *server;
6063 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
6064 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6067 struct nfs4_lockdata *p;
6068 struct inode *inode = lsp->ls_state->inode;
6069 struct nfs_server *server = NFS_SERVER(inode);
6070 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6072 p = kzalloc(sizeof(*p), gfp_mask);
6076 p->arg.fh = NFS_FH(inode);
6078 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
6079 if (IS_ERR(p->arg.open_seqid))
6081 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6082 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
6083 if (IS_ERR(p->arg.lock_seqid))
6084 goto out_free_seqid;
6085 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
6086 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
6087 p->arg.lock_owner.s_dev = server->s_dev;
6088 p->res.lock_seqid = p->arg.lock_seqid;
6091 atomic_inc(&lsp->ls_count);
6092 p->ctx = get_nfs_open_context(ctx);
6093 get_file(fl->fl_file);
6094 memcpy(&p->fl, fl, sizeof(p->fl));
6097 nfs_free_seqid(p->arg.open_seqid);
6103 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6105 struct nfs4_lockdata *data = calldata;
6106 struct nfs4_state *state = data->lsp->ls_state;
6108 dprintk("%s: begin!\n", __func__);
6109 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
6111 /* Do we need to do an open_to_lock_owner? */
6112 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
6113 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
6114 goto out_release_lock_seqid;
6116 nfs4_stateid_copy(&data->arg.open_stateid,
6117 &state->open_stateid);
6118 data->arg.new_lock_owner = 1;
6119 data->res.open_seqid = data->arg.open_seqid;
6121 data->arg.new_lock_owner = 0;
6122 nfs4_stateid_copy(&data->arg.lock_stateid,
6123 &data->lsp->ls_stateid);
6125 if (!nfs4_valid_open_stateid(state)) {
6126 data->rpc_status = -EBADF;
6127 task->tk_action = NULL;
6128 goto out_release_open_seqid;
6130 data->timestamp = jiffies;
6131 if (nfs4_setup_sequence(data->server,
6132 &data->arg.seq_args,
6136 out_release_open_seqid:
6137 nfs_release_seqid(data->arg.open_seqid);
6138 out_release_lock_seqid:
6139 nfs_release_seqid(data->arg.lock_seqid);
6141 nfs4_sequence_done(task, &data->res.seq_res);
6142 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
6145 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6147 struct nfs4_lockdata *data = calldata;
6148 struct nfs4_lock_state *lsp = data->lsp;
6150 dprintk("%s: begin!\n", __func__);
6152 if (!nfs4_sequence_done(task, &data->res.seq_res))
6155 data->rpc_status = task->tk_status;
6156 switch (task->tk_status) {
6158 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
6160 if (data->arg.new_lock) {
6161 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
6162 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
6163 rpc_restart_call_prepare(task);
6167 if (data->arg.new_lock_owner != 0) {
6168 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6169 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6170 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6171 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6172 rpc_restart_call_prepare(task);
6174 case -NFS4ERR_BAD_STATEID:
6175 case -NFS4ERR_OLD_STATEID:
6176 case -NFS4ERR_STALE_STATEID:
6177 case -NFS4ERR_EXPIRED:
6178 if (data->arg.new_lock_owner != 0) {
6179 if (!nfs4_stateid_match(&data->arg.open_stateid,
6180 &lsp->ls_state->open_stateid))
6181 rpc_restart_call_prepare(task);
6182 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6184 rpc_restart_call_prepare(task);
6186 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
6189 static void nfs4_lock_release(void *calldata)
6191 struct nfs4_lockdata *data = calldata;
6193 dprintk("%s: begin!\n", __func__);
6194 nfs_free_seqid(data->arg.open_seqid);
6195 if (data->cancelled != 0) {
6196 struct rpc_task *task;
6197 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6198 data->arg.lock_seqid);
6200 rpc_put_task_async(task);
6201 dprintk("%s: cancelling lock!\n", __func__);
6203 nfs_free_seqid(data->arg.lock_seqid);
6204 nfs4_put_lock_state(data->lsp);
6205 put_nfs_open_context(data->ctx);
6206 fput(data->fl.fl_file);
6208 dprintk("%s: done!\n", __func__);
6211 static const struct rpc_call_ops nfs4_lock_ops = {
6212 .rpc_call_prepare = nfs4_lock_prepare,
6213 .rpc_call_done = nfs4_lock_done,
6214 .rpc_release = nfs4_lock_release,
6217 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6220 case -NFS4ERR_ADMIN_REVOKED:
6221 case -NFS4ERR_EXPIRED:
6222 case -NFS4ERR_BAD_STATEID:
6223 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6224 if (new_lock_owner != 0 ||
6225 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
6226 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
6228 case -NFS4ERR_STALE_STATEID:
6229 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6230 nfs4_schedule_lease_recovery(server->nfs_client);
6234 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
6236 struct nfs4_lockdata *data;
6237 struct rpc_task *task;
6238 struct rpc_message msg = {
6239 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6240 .rpc_cred = state->owner->so_cred,
6242 struct rpc_task_setup task_setup_data = {
6243 .rpc_client = NFS_CLIENT(state->inode),
6244 .rpc_message = &msg,
6245 .callback_ops = &nfs4_lock_ops,
6246 .workqueue = nfsiod_workqueue,
6247 .flags = RPC_TASK_ASYNC,
6251 dprintk("%s: begin!\n", __func__);
6252 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
6253 fl->fl_u.nfs4_fl.owner,
6254 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
6258 data->arg.block = 1;
6259 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
6260 msg.rpc_argp = &data->arg;
6261 msg.rpc_resp = &data->res;
6262 task_setup_data.callback_data = data;
6263 if (recovery_type > NFS_LOCK_NEW) {
6264 if (recovery_type == NFS_LOCK_RECLAIM)
6265 data->arg.reclaim = NFS_LOCK_RECLAIM;
6266 nfs4_set_sequence_privileged(&data->arg.seq_args);
6268 data->arg.new_lock = 1;
6269 task = rpc_run_task(&task_setup_data);
6271 return PTR_ERR(task);
6272 ret = nfs4_wait_for_completion_rpc_task(task);
6274 ret = data->rpc_status;
6276 nfs4_handle_setlk_error(data->server, data->lsp,
6277 data->arg.new_lock_owner, ret);
6279 data->cancelled = 1;
6281 dprintk("%s: done, ret = %d!\n", __func__, ret);
6282 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
6286 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6288 struct nfs_server *server = NFS_SERVER(state->inode);
6289 struct nfs4_exception exception = {
6290 .inode = state->inode,
6295 /* Cache the lock if possible... */
6296 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6298 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
6299 if (err != -NFS4ERR_DELAY)
6301 nfs4_handle_exception(server, err, &exception);
6302 } while (exception.retry);
6306 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6308 struct nfs_server *server = NFS_SERVER(state->inode);
6309 struct nfs4_exception exception = {
6310 .inode = state->inode,
6314 err = nfs4_set_lock_state(state, request);
6317 if (!recover_lost_locks) {
6318 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6322 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6324 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
6328 case -NFS4ERR_GRACE:
6329 case -NFS4ERR_DELAY:
6330 nfs4_handle_exception(server, err, &exception);
6333 } while (exception.retry);
6338 #if defined(CONFIG_NFS_V4_1)
6339 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6341 struct nfs4_lock_state *lsp;
6344 status = nfs4_set_lock_state(state, request);
6347 lsp = request->fl_u.nfs4_fl.owner;
6348 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6349 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6351 status = nfs4_lock_expired(state, request);
6356 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6358 struct nfs_inode *nfsi = NFS_I(state->inode);
6359 struct nfs4_state_owner *sp = state->owner;
6360 unsigned char fl_flags = request->fl_flags;
6363 request->fl_flags |= FL_ACCESS;
6364 status = locks_lock_inode_wait(state->inode, request);
6367 mutex_lock(&sp->so_delegreturn_mutex);
6368 down_read(&nfsi->rwsem);
6369 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
6370 /* Yes: cache locks! */
6371 /* ...but avoid races with delegation recall... */
6372 request->fl_flags = fl_flags & ~FL_SLEEP;
6373 status = locks_lock_inode_wait(state->inode, request);
6374 up_read(&nfsi->rwsem);
6375 mutex_unlock(&sp->so_delegreturn_mutex);
6378 up_read(&nfsi->rwsem);
6379 mutex_unlock(&sp->so_delegreturn_mutex);
6380 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
6382 request->fl_flags = fl_flags;
6386 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6388 struct nfs4_exception exception = {
6390 .inode = state->inode,
6395 err = _nfs4_proc_setlk(state, cmd, request);
6396 if (err == -NFS4ERR_DENIED)
6398 err = nfs4_handle_exception(NFS_SERVER(state->inode),
6400 } while (exception.retry);
6404 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6405 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6408 nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6409 struct file_lock *request)
6411 int status = -ERESTARTSYS;
6412 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6414 while(!signalled()) {
6415 status = nfs4_proc_setlk(state, cmd, request);
6416 if ((status != -EAGAIN) || IS_SETLK(cmd))
6418 freezable_schedule_timeout_interruptible(timeout);
6420 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6421 status = -ERESTARTSYS;
6426 #ifdef CONFIG_NFS_V4_1
6427 struct nfs4_lock_waiter {
6428 struct task_struct *task;
6429 struct inode *inode;
6430 struct nfs_lowner *owner;
6435 nfs4_wake_lock_waiter(wait_queue_t *wait, unsigned int mode, int flags, void *key)
6438 struct cb_notify_lock_args *cbnl = key;
6439 struct nfs4_lock_waiter *waiter = wait->private;
6440 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6441 *wowner = waiter->owner;
6443 /* Only wake if the callback was for the same owner */
6444 if (lowner->clientid != wowner->clientid ||
6445 lowner->id != wowner->id ||
6446 lowner->s_dev != wowner->s_dev)
6449 /* Make sure it's for the right inode */
6450 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6453 waiter->notified = true;
6455 /* override "private" so we can use default_wake_function */
6456 wait->private = waiter->task;
6457 ret = autoremove_wake_function(wait, mode, flags, key);
6458 wait->private = waiter;
6463 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6465 int status = -ERESTARTSYS;
6466 unsigned long flags;
6467 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6468 struct nfs_server *server = NFS_SERVER(state->inode);
6469 struct nfs_client *clp = server->nfs_client;
6470 wait_queue_head_t *q = &clp->cl_lock_waitq;
6471 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6472 .id = lsp->ls_seqid.owner_id,
6473 .s_dev = server->s_dev };
6474 struct nfs4_lock_waiter waiter = { .task = current,
6475 .inode = state->inode,
6477 .notified = false };
6480 /* Don't bother with waitqueue if we don't expect a callback */
6481 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6482 return nfs4_retry_setlk_simple(state, cmd, request);
6485 wait.private = &waiter;
6486 wait.func = nfs4_wake_lock_waiter;
6487 add_wait_queue(q, &wait);
6489 while(!signalled()) {
6490 status = nfs4_proc_setlk(state, cmd, request);
6491 if ((status != -EAGAIN) || IS_SETLK(cmd))
6494 status = -ERESTARTSYS;
6495 spin_lock_irqsave(&q->lock, flags);
6496 if (waiter.notified) {
6497 spin_unlock_irqrestore(&q->lock, flags);
6500 set_current_state(TASK_INTERRUPTIBLE);
6501 spin_unlock_irqrestore(&q->lock, flags);
6503 freezable_schedule_timeout_interruptible(NFS4_LOCK_MAXTIMEOUT);
6506 finish_wait(q, &wait);
6509 #else /* !CONFIG_NFS_V4_1 */
6511 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6513 return nfs4_retry_setlk_simple(state, cmd, request);
6518 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6520 struct nfs_open_context *ctx;
6521 struct nfs4_state *state;
6524 /* verify open state */
6525 ctx = nfs_file_open_context(filp);
6528 if (request->fl_start < 0 || request->fl_end < 0)
6531 if (IS_GETLK(cmd)) {
6533 return nfs4_proc_getlk(state, F_GETLK, request);
6537 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6540 if (request->fl_type == F_UNLCK) {
6542 return nfs4_proc_unlck(state, cmd, request);
6549 if ((request->fl_flags & FL_POSIX) &&
6550 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6554 * Don't rely on the VFS having checked the file open mode,
6555 * since it won't do this for flock() locks.
6557 switch (request->fl_type) {
6559 if (!(filp->f_mode & FMODE_READ))
6563 if (!(filp->f_mode & FMODE_WRITE))
6567 status = nfs4_set_lock_state(state, request);
6571 return nfs4_retry_setlk(state, cmd, request);
6574 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
6576 struct nfs_server *server = NFS_SERVER(state->inode);
6579 err = nfs4_set_lock_state(state, fl);
6582 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
6583 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
6586 struct nfs_release_lockowner_data {
6587 struct nfs4_lock_state *lsp;
6588 struct nfs_server *server;
6589 struct nfs_release_lockowner_args args;
6590 struct nfs_release_lockowner_res res;
6591 unsigned long timestamp;
6594 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6596 struct nfs_release_lockowner_data *data = calldata;
6597 struct nfs_server *server = data->server;
6598 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
6599 &data->args.seq_args, &data->res.seq_res, task);
6600 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6601 data->timestamp = jiffies;
6604 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6606 struct nfs_release_lockowner_data *data = calldata;
6607 struct nfs_server *server = data->server;
6609 nfs40_sequence_done(task, &data->res.seq_res);
6611 switch (task->tk_status) {
6613 renew_lease(server, data->timestamp);
6615 case -NFS4ERR_STALE_CLIENTID:
6616 case -NFS4ERR_EXPIRED:
6617 nfs4_schedule_lease_recovery(server->nfs_client);
6619 case -NFS4ERR_LEASE_MOVED:
6620 case -NFS4ERR_DELAY:
6621 if (nfs4_async_handle_error(task, server,
6622 NULL, NULL) == -EAGAIN)
6623 rpc_restart_call_prepare(task);
6627 static void nfs4_release_lockowner_release(void *calldata)
6629 struct nfs_release_lockowner_data *data = calldata;
6630 nfs4_free_lock_state(data->server, data->lsp);
6634 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
6635 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6636 .rpc_call_done = nfs4_release_lockowner_done,
6637 .rpc_release = nfs4_release_lockowner_release,
6641 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
6643 struct nfs_release_lockowner_data *data;
6644 struct rpc_message msg = {
6645 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6648 if (server->nfs_client->cl_mvops->minor_version != 0)
6651 data = kmalloc(sizeof(*data), GFP_NOFS);
6655 data->server = server;
6656 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6657 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6658 data->args.lock_owner.s_dev = server->s_dev;
6660 msg.rpc_argp = &data->args;
6661 msg.rpc_resp = &data->res;
6662 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
6663 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
6666 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6668 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
6669 struct dentry *unused, struct inode *inode,
6670 const char *key, const void *buf,
6671 size_t buflen, int flags)
6673 return nfs4_proc_set_acl(inode, buf, buflen);
6676 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
6677 struct dentry *unused, struct inode *inode,
6678 const char *key, void *buf, size_t buflen)
6680 return nfs4_proc_get_acl(inode, buf, buflen);
6683 static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
6685 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
6688 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
6690 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
6691 struct dentry *unused, struct inode *inode,
6692 const char *key, const void *buf,
6693 size_t buflen, int flags)
6695 if (security_ismaclabel(key))
6696 return nfs4_set_security_label(inode, buf, buflen);
6701 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
6702 struct dentry *unused, struct inode *inode,
6703 const char *key, void *buf, size_t buflen)
6705 if (security_ismaclabel(key))
6706 return nfs4_get_security_label(inode, buf, buflen);
6711 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6715 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6716 len = security_inode_listsecurity(inode, list, list_len);
6717 if (list_len && len > list_len)
6723 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6724 .prefix = XATTR_SECURITY_PREFIX,
6725 .get = nfs4_xattr_get_nfs4_label,
6726 .set = nfs4_xattr_set_nfs4_label,
6732 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6740 * nfs_fhget will use either the mounted_on_fileid or the fileid
6742 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6744 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6745 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6746 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
6747 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
6750 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
6751 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
6752 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6756 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6757 const struct qstr *name,
6758 struct nfs4_fs_locations *fs_locations,
6761 struct nfs_server *server = NFS_SERVER(dir);
6763 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6765 struct nfs4_fs_locations_arg args = {
6766 .dir_fh = NFS_FH(dir),
6771 struct nfs4_fs_locations_res res = {
6772 .fs_locations = fs_locations,
6774 struct rpc_message msg = {
6775 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6781 dprintk("%s: start\n", __func__);
6783 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6784 * is not supported */
6785 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6786 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6788 bitmask[0] |= FATTR4_WORD0_FILEID;
6790 nfs_fattr_init(&fs_locations->fattr);
6791 fs_locations->server = server;
6792 fs_locations->nlocations = 0;
6793 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
6794 dprintk("%s: returned status = %d\n", __func__, status);
6798 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6799 const struct qstr *name,
6800 struct nfs4_fs_locations *fs_locations,
6803 struct nfs4_exception exception = { };
6806 err = _nfs4_proc_fs_locations(client, dir, name,
6807 fs_locations, page);
6808 trace_nfs4_get_fs_locations(dir, name, err);
6809 err = nfs4_handle_exception(NFS_SERVER(dir), err,
6811 } while (exception.retry);
6816 * This operation also signals the server that this client is
6817 * performing migration recovery. The server can stop returning
6818 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6819 * appended to this compound to identify the client ID which is
6820 * performing recovery.
6822 static int _nfs40_proc_get_locations(struct inode *inode,
6823 struct nfs4_fs_locations *locations,
6824 struct page *page, struct rpc_cred *cred)
6826 struct nfs_server *server = NFS_SERVER(inode);
6827 struct rpc_clnt *clnt = server->client;
6829 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6831 struct nfs4_fs_locations_arg args = {
6832 .clientid = server->nfs_client->cl_clientid,
6833 .fh = NFS_FH(inode),
6836 .migration = 1, /* skip LOOKUP */
6837 .renew = 1, /* append RENEW */
6839 struct nfs4_fs_locations_res res = {
6840 .fs_locations = locations,
6844 struct rpc_message msg = {
6845 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6850 unsigned long now = jiffies;
6853 nfs_fattr_init(&locations->fattr);
6854 locations->server = server;
6855 locations->nlocations = 0;
6857 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6858 nfs4_set_sequence_privileged(&args.seq_args);
6859 status = nfs4_call_sync_sequence(clnt, server, &msg,
6860 &args.seq_args, &res.seq_res);
6864 renew_lease(server, now);
6868 #ifdef CONFIG_NFS_V4_1
6871 * This operation also signals the server that this client is
6872 * performing migration recovery. The server can stop asserting
6873 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6874 * performing this operation is identified in the SEQUENCE
6875 * operation in this compound.
6877 * When the client supports GETATTR(fs_locations_info), it can
6878 * be plumbed in here.
6880 static int _nfs41_proc_get_locations(struct inode *inode,
6881 struct nfs4_fs_locations *locations,
6882 struct page *page, struct rpc_cred *cred)
6884 struct nfs_server *server = NFS_SERVER(inode);
6885 struct rpc_clnt *clnt = server->client;
6887 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6889 struct nfs4_fs_locations_arg args = {
6890 .fh = NFS_FH(inode),
6893 .migration = 1, /* skip LOOKUP */
6895 struct nfs4_fs_locations_res res = {
6896 .fs_locations = locations,
6899 struct rpc_message msg = {
6900 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6907 nfs_fattr_init(&locations->fattr);
6908 locations->server = server;
6909 locations->nlocations = 0;
6911 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6912 nfs4_set_sequence_privileged(&args.seq_args);
6913 status = nfs4_call_sync_sequence(clnt, server, &msg,
6914 &args.seq_args, &res.seq_res);
6915 if (status == NFS4_OK &&
6916 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6917 status = -NFS4ERR_LEASE_MOVED;
6921 #endif /* CONFIG_NFS_V4_1 */
6924 * nfs4_proc_get_locations - discover locations for a migrated FSID
6925 * @inode: inode on FSID that is migrating
6926 * @locations: result of query
6928 * @cred: credential to use for this operation
6930 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6931 * operation failed, or a negative errno if a local error occurred.
6933 * On success, "locations" is filled in, but if the server has
6934 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6937 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6938 * from this client that require migration recovery.
6940 int nfs4_proc_get_locations(struct inode *inode,
6941 struct nfs4_fs_locations *locations,
6942 struct page *page, struct rpc_cred *cred)
6944 struct nfs_server *server = NFS_SERVER(inode);
6945 struct nfs_client *clp = server->nfs_client;
6946 const struct nfs4_mig_recovery_ops *ops =
6947 clp->cl_mvops->mig_recovery_ops;
6948 struct nfs4_exception exception = { };
6951 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6952 (unsigned long long)server->fsid.major,
6953 (unsigned long long)server->fsid.minor,
6955 nfs_display_fhandle(NFS_FH(inode), __func__);
6958 status = ops->get_locations(inode, locations, page, cred);
6959 if (status != -NFS4ERR_DELAY)
6961 nfs4_handle_exception(server, status, &exception);
6962 } while (exception.retry);
6967 * This operation also signals the server that this client is
6968 * performing "lease moved" recovery. The server can stop
6969 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6970 * is appended to this compound to identify the client ID which is
6971 * performing recovery.
6973 static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6975 struct nfs_server *server = NFS_SERVER(inode);
6976 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6977 struct rpc_clnt *clnt = server->client;
6978 struct nfs4_fsid_present_arg args = {
6979 .fh = NFS_FH(inode),
6980 .clientid = clp->cl_clientid,
6981 .renew = 1, /* append RENEW */
6983 struct nfs4_fsid_present_res res = {
6986 struct rpc_message msg = {
6987 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6992 unsigned long now = jiffies;
6995 res.fh = nfs_alloc_fhandle();
6999 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7000 nfs4_set_sequence_privileged(&args.seq_args);
7001 status = nfs4_call_sync_sequence(clnt, server, &msg,
7002 &args.seq_args, &res.seq_res);
7003 nfs_free_fhandle(res.fh);
7007 do_renew_lease(clp, now);
7011 #ifdef CONFIG_NFS_V4_1
7014 * This operation also signals the server that this client is
7015 * performing "lease moved" recovery. The server can stop asserting
7016 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7017 * this operation is identified in the SEQUENCE operation in this
7020 static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7022 struct nfs_server *server = NFS_SERVER(inode);
7023 struct rpc_clnt *clnt = server->client;
7024 struct nfs4_fsid_present_arg args = {
7025 .fh = NFS_FH(inode),
7027 struct nfs4_fsid_present_res res = {
7029 struct rpc_message msg = {
7030 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7037 res.fh = nfs_alloc_fhandle();
7041 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7042 nfs4_set_sequence_privileged(&args.seq_args);
7043 status = nfs4_call_sync_sequence(clnt, server, &msg,
7044 &args.seq_args, &res.seq_res);
7045 nfs_free_fhandle(res.fh);
7046 if (status == NFS4_OK &&
7047 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7048 status = -NFS4ERR_LEASE_MOVED;
7052 #endif /* CONFIG_NFS_V4_1 */
7055 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7056 * @inode: inode on FSID to check
7057 * @cred: credential to use for this operation
7059 * Server indicates whether the FSID is present, moved, or not
7060 * recognized. This operation is necessary to clear a LEASE_MOVED
7061 * condition for this client ID.
7063 * Returns NFS4_OK if the FSID is present on this server,
7064 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7065 * NFS4ERR code if some error occurred on the server, or a
7066 * negative errno if a local failure occurred.
7068 int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7070 struct nfs_server *server = NFS_SERVER(inode);
7071 struct nfs_client *clp = server->nfs_client;
7072 const struct nfs4_mig_recovery_ops *ops =
7073 clp->cl_mvops->mig_recovery_ops;
7074 struct nfs4_exception exception = { };
7077 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7078 (unsigned long long)server->fsid.major,
7079 (unsigned long long)server->fsid.minor,
7081 nfs_display_fhandle(NFS_FH(inode), __func__);
7084 status = ops->fsid_present(inode, cred);
7085 if (status != -NFS4ERR_DELAY)
7087 nfs4_handle_exception(server, status, &exception);
7088 } while (exception.retry);
7093 * If 'use_integrity' is true and the state managment nfs_client
7094 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7095 * and the machine credential as per RFC3530bis and RFC5661 Security
7096 * Considerations sections. Otherwise, just use the user cred with the
7097 * filesystem's rpc_client.
7099 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
7102 struct nfs4_secinfo_arg args = {
7103 .dir_fh = NFS_FH(dir),
7106 struct nfs4_secinfo_res res = {
7109 struct rpc_message msg = {
7110 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7114 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
7115 struct rpc_cred *cred = NULL;
7117 if (use_integrity) {
7118 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
7119 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7120 msg.rpc_cred = cred;
7123 dprintk("NFS call secinfo %s\n", name->name);
7125 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7126 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7128 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7130 dprintk("NFS reply secinfo: %d\n", status);
7138 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7139 struct nfs4_secinfo_flavors *flavors)
7141 struct nfs4_exception exception = { };
7144 err = -NFS4ERR_WRONGSEC;
7146 /* try to use integrity protection with machine cred */
7147 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7148 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7151 * if unable to use integrity protection, or SECINFO with
7152 * integrity protection returns NFS4ERR_WRONGSEC (which is
7153 * disallowed by spec, but exists in deployed servers) use
7154 * the current filesystem's rpc_client and the user cred.
7156 if (err == -NFS4ERR_WRONGSEC)
7157 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7159 trace_nfs4_secinfo(dir, name, err);
7160 err = nfs4_handle_exception(NFS_SERVER(dir), err,
7162 } while (exception.retry);
7166 #ifdef CONFIG_NFS_V4_1
7168 * Check the exchange flags returned by the server for invalid flags, having
7169 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7172 static int nfs4_check_cl_exchange_flags(u32 flags)
7174 if (flags & ~EXCHGID4_FLAG_MASK_R)
7176 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7177 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7179 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7183 return -NFS4ERR_INVAL;
7187 nfs41_same_server_scope(struct nfs41_server_scope *a,
7188 struct nfs41_server_scope *b)
7190 if (a->server_scope_sz == b->server_scope_sz &&
7191 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
7198 nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7202 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7203 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7207 * nfs4_proc_bind_one_conn_to_session()
7209 * The 4.1 client currently uses the same TCP connection for the
7210 * fore and backchannel.
7213 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7214 struct rpc_xprt *xprt,
7215 struct nfs_client *clp,
7216 struct rpc_cred *cred)
7219 struct nfs41_bind_conn_to_session_args args = {
7221 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7223 struct nfs41_bind_conn_to_session_res res;
7224 struct rpc_message msg = {
7226 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
7231 struct rpc_task_setup task_setup_data = {
7234 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
7235 .rpc_message = &msg,
7236 .flags = RPC_TASK_TIMEOUT,
7238 struct rpc_task *task;
7240 dprintk("--> %s\n", __func__);
7242 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7243 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7244 args.dir = NFS4_CDFC4_FORE;
7246 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7247 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7248 args.dir = NFS4_CDFC4_FORE;
7250 task = rpc_run_task(&task_setup_data);
7251 if (!IS_ERR(task)) {
7252 status = task->tk_status;
7255 status = PTR_ERR(task);
7256 trace_nfs4_bind_conn_to_session(clp, status);
7258 if (memcmp(res.sessionid.data,
7259 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7260 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7264 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
7265 dprintk("NFS: %s: Unexpected direction from server\n",
7270 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
7271 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7278 dprintk("<-- %s status= %d\n", __func__, status);
7282 struct rpc_bind_conn_calldata {
7283 struct nfs_client *clp;
7284 struct rpc_cred *cred;
7288 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7289 struct rpc_xprt *xprt,
7292 struct rpc_bind_conn_calldata *p = calldata;
7294 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7297 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7299 struct rpc_bind_conn_calldata data = {
7303 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7304 nfs4_proc_bind_conn_to_session_callback, &data);
7308 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7309 * and operations we'd like to see to enable certain features in the allow map
7311 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7312 .how = SP4_MACH_CRED,
7313 .enforce.u.words = {
7314 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7315 1 << (OP_EXCHANGE_ID - 32) |
7316 1 << (OP_CREATE_SESSION - 32) |
7317 1 << (OP_DESTROY_SESSION - 32) |
7318 1 << (OP_DESTROY_CLIENTID - 32)
7321 [0] = 1 << (OP_CLOSE) |
7322 1 << (OP_OPEN_DOWNGRADE) |
7324 1 << (OP_DELEGRETURN) |
7326 [1] = 1 << (OP_SECINFO - 32) |
7327 1 << (OP_SECINFO_NO_NAME - 32) |
7328 1 << (OP_LAYOUTRETURN - 32) |
7329 1 << (OP_TEST_STATEID - 32) |
7330 1 << (OP_FREE_STATEID - 32) |
7331 1 << (OP_WRITE - 32)
7336 * Select the state protection mode for client `clp' given the server results
7337 * from exchange_id in `sp'.
7339 * Returns 0 on success, negative errno otherwise.
7341 static int nfs4_sp4_select_mode(struct nfs_client *clp,
7342 struct nfs41_state_protection *sp)
7344 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7345 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7346 1 << (OP_EXCHANGE_ID - 32) |
7347 1 << (OP_CREATE_SESSION - 32) |
7348 1 << (OP_DESTROY_SESSION - 32) |
7349 1 << (OP_DESTROY_CLIENTID - 32)
7353 if (sp->how == SP4_MACH_CRED) {
7354 /* Print state protect result */
7355 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7356 for (i = 0; i <= LAST_NFS4_OP; i++) {
7357 if (test_bit(i, sp->enforce.u.longs))
7358 dfprintk(MOUNT, " enforce op %d\n", i);
7359 if (test_bit(i, sp->allow.u.longs))
7360 dfprintk(MOUNT, " allow op %d\n", i);
7363 /* make sure nothing is on enforce list that isn't supported */
7364 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7365 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7366 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7372 * Minimal mode - state operations are allowed to use machine
7373 * credential. Note this already happens by default, so the
7374 * client doesn't have to do anything more than the negotiation.
7376 * NOTE: we don't care if EXCHANGE_ID is in the list -
7377 * we're already using the machine cred for exchange_id
7378 * and will never use a different cred.
7380 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7381 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7382 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7383 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7384 dfprintk(MOUNT, "sp4_mach_cred:\n");
7385 dfprintk(MOUNT, " minimal mode enabled\n");
7386 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
7388 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7392 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
7393 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7394 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
7395 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7396 dfprintk(MOUNT, " cleanup mode enabled\n");
7397 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
7400 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7401 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7402 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
7403 &clp->cl_sp4_flags);
7406 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7407 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7408 dfprintk(MOUNT, " secinfo mode enabled\n");
7409 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
7412 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7413 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7414 dfprintk(MOUNT, " stateid mode enabled\n");
7415 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
7418 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7419 dfprintk(MOUNT, " write mode enabled\n");
7420 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
7423 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7424 dfprintk(MOUNT, " commit mode enabled\n");
7425 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
7432 struct nfs41_exchange_id_data {
7433 struct nfs41_exchange_id_res res;
7434 struct nfs41_exchange_id_args args;
7435 struct rpc_xprt *xprt;
7439 static void nfs4_exchange_id_done(struct rpc_task *task, void *data)
7441 struct nfs41_exchange_id_data *cdata =
7442 (struct nfs41_exchange_id_data *)data;
7443 struct nfs_client *clp = cdata->args.client;
7444 int status = task->tk_status;
7446 trace_nfs4_exchange_id(clp, status);
7449 status = nfs4_check_cl_exchange_flags(cdata->res.flags);
7451 if (cdata->xprt && status == 0) {
7452 status = nfs4_detect_session_trunking(clp, &cdata->res,
7458 status = nfs4_sp4_select_mode(clp, &cdata->res.state_protect);
7461 clp->cl_clientid = cdata->res.clientid;
7462 clp->cl_exchange_flags = cdata->res.flags;
7463 /* Client ID is not confirmed */
7464 if (!(cdata->res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
7465 clear_bit(NFS4_SESSION_ESTABLISHED,
7466 &clp->cl_session->session_state);
7467 clp->cl_seqid = cdata->res.seqid;
7470 kfree(clp->cl_serverowner);
7471 clp->cl_serverowner = cdata->res.server_owner;
7472 cdata->res.server_owner = NULL;
7474 /* use the most recent implementation id */
7475 kfree(clp->cl_implid);
7476 clp->cl_implid = cdata->res.impl_id;
7477 cdata->res.impl_id = NULL;
7479 if (clp->cl_serverscope != NULL &&
7480 !nfs41_same_server_scope(clp->cl_serverscope,
7481 cdata->res.server_scope)) {
7482 dprintk("%s: server_scope mismatch detected\n",
7484 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
7485 kfree(clp->cl_serverscope);
7486 clp->cl_serverscope = NULL;
7489 if (clp->cl_serverscope == NULL) {
7490 clp->cl_serverscope = cdata->res.server_scope;
7491 cdata->res.server_scope = NULL;
7493 /* Save the EXCHANGE_ID verifier session trunk tests */
7494 memcpy(clp->cl_confirm.data, cdata->args.verifier->data,
7495 sizeof(clp->cl_confirm.data));
7498 cdata->rpc_status = status;
7502 static void nfs4_exchange_id_release(void *data)
7504 struct nfs41_exchange_id_data *cdata =
7505 (struct nfs41_exchange_id_data *)data;
7507 nfs_put_client(cdata->args.client);
7509 xprt_put(cdata->xprt);
7510 rpc_clnt_xprt_switch_put(cdata->args.client->cl_rpcclient);
7512 kfree(cdata->res.impl_id);
7513 kfree(cdata->res.server_scope);
7514 kfree(cdata->res.server_owner);
7518 static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7519 .rpc_call_done = nfs4_exchange_id_done,
7520 .rpc_release = nfs4_exchange_id_release,
7524 * _nfs4_proc_exchange_id()
7526 * Wrapper for EXCHANGE_ID operation.
7528 static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
7529 u32 sp4_how, struct rpc_xprt *xprt)
7531 nfs4_verifier verifier;
7532 struct rpc_message msg = {
7533 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
7536 struct rpc_task_setup task_setup_data = {
7537 .rpc_client = clp->cl_rpcclient,
7538 .callback_ops = &nfs4_exchange_id_call_ops,
7539 .rpc_message = &msg,
7540 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7542 struct nfs41_exchange_id_data *calldata;
7543 struct rpc_task *task;
7546 if (!atomic_inc_not_zero(&clp->cl_count))
7550 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7555 nfs4_init_boot_verifier(clp, &verifier);
7557 status = nfs4_init_uniform_client_string(clp);
7561 dprintk("NFS call exchange_id auth=%s, '%s'\n",
7562 clp->cl_rpcclient->cl_auth->au_ops->au_name,
7565 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7568 if (unlikely(calldata->res.server_owner == NULL))
7571 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
7573 if (unlikely(calldata->res.server_scope == NULL))
7574 goto out_server_owner;
7576 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7577 if (unlikely(calldata->res.impl_id == NULL))
7578 goto out_server_scope;
7582 calldata->args.state_protect.how = SP4_NONE;
7586 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
7596 calldata->xprt = xprt;
7597 task_setup_data.rpc_xprt = xprt;
7598 task_setup_data.flags =
7599 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC;
7600 calldata->args.verifier = &clp->cl_confirm;
7602 calldata->args.verifier = &verifier;
7604 calldata->args.client = clp;
7605 #ifdef CONFIG_NFS_V4_1_MIGRATION
7606 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7607 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7608 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7610 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7611 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7613 msg.rpc_argp = &calldata->args;
7614 msg.rpc_resp = &calldata->res;
7615 task_setup_data.callback_data = calldata;
7617 task = rpc_run_task(&task_setup_data);
7619 status = PTR_ERR(task);
7624 status = rpc_wait_for_completion_task(task);
7626 status = calldata->rpc_status;
7627 } else /* session trunking test */
7628 status = calldata->rpc_status;
7632 if (clp->cl_implid != NULL)
7633 dprintk("NFS reply exchange_id: Server Implementation ID: "
7634 "domain: %s, name: %s, date: %llu,%u\n",
7635 clp->cl_implid->domain, clp->cl_implid->name,
7636 clp->cl_implid->date.seconds,
7637 clp->cl_implid->date.nseconds);
7638 dprintk("NFS reply exchange_id: %d\n", status);
7642 kfree(calldata->res.impl_id);
7644 kfree(calldata->res.server_scope);
7646 kfree(calldata->res.server_owner);
7653 * nfs4_proc_exchange_id()
7655 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7657 * Since the clientid has expired, all compounds using sessions
7658 * associated with the stale clientid will be returning
7659 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7660 * be in some phase of session reset.
7662 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7664 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7666 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7669 /* try SP4_MACH_CRED if krb5i/p */
7670 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7671 authflavor == RPC_AUTH_GSS_KRB5P) {
7672 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED, NULL);
7678 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE, NULL);
7682 * nfs4_test_session_trunk
7684 * This is an add_xprt_test() test function called from
7685 * rpc_clnt_setup_test_and_add_xprt.
7687 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7688 * and is dereferrenced in nfs4_exchange_id_release
7690 * Upon success, add the new transport to the rpc_clnt
7692 * @clnt: struct rpc_clnt to get new transport
7693 * @xprt: the rpc_xprt to test
7694 * @data: call data for _nfs4_proc_exchange_id.
7696 int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7699 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
7702 dprintk("--> %s try %s\n", __func__,
7703 xprt->address_strings[RPC_DISPLAY_ADDR]);
7705 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7707 /* Test connection for session trunking. Async exchange_id call */
7708 return _nfs4_proc_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7710 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7712 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7713 struct rpc_cred *cred)
7715 struct rpc_message msg = {
7716 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7722 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7723 trace_nfs4_destroy_clientid(clp, status);
7725 dprintk("NFS: Got error %d from the server %s on "
7726 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7730 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7731 struct rpc_cred *cred)
7736 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7737 ret = _nfs4_proc_destroy_clientid(clp, cred);
7739 case -NFS4ERR_DELAY:
7740 case -NFS4ERR_CLIENTID_BUSY:
7750 int nfs4_destroy_clientid(struct nfs_client *clp)
7752 struct rpc_cred *cred;
7755 if (clp->cl_mvops->minor_version < 1)
7757 if (clp->cl_exchange_flags == 0)
7759 if (clp->cl_preserve_clid)
7761 cred = nfs4_get_clid_cred(clp);
7762 ret = nfs4_proc_destroy_clientid(clp, cred);
7767 case -NFS4ERR_STALE_CLIENTID:
7768 clp->cl_exchange_flags = 0;
7774 struct nfs4_get_lease_time_data {
7775 struct nfs4_get_lease_time_args *args;
7776 struct nfs4_get_lease_time_res *res;
7777 struct nfs_client *clp;
7780 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7783 struct nfs4_get_lease_time_data *data =
7784 (struct nfs4_get_lease_time_data *)calldata;
7786 dprintk("--> %s\n", __func__);
7787 /* just setup sequence, do not trigger session recovery
7788 since we're invoked within one */
7789 nfs41_setup_sequence(data->clp->cl_session,
7790 &data->args->la_seq_args,
7791 &data->res->lr_seq_res,
7793 dprintk("<-- %s\n", __func__);
7797 * Called from nfs4_state_manager thread for session setup, so don't recover
7798 * from sequence operation or clientid errors.
7800 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7802 struct nfs4_get_lease_time_data *data =
7803 (struct nfs4_get_lease_time_data *)calldata;
7805 dprintk("--> %s\n", __func__);
7806 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7808 switch (task->tk_status) {
7809 case -NFS4ERR_DELAY:
7810 case -NFS4ERR_GRACE:
7811 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7812 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7813 task->tk_status = 0;
7815 case -NFS4ERR_RETRY_UNCACHED_REP:
7816 rpc_restart_call_prepare(task);
7819 dprintk("<-- %s\n", __func__);
7822 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
7823 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7824 .rpc_call_done = nfs4_get_lease_time_done,
7827 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7829 struct rpc_task *task;
7830 struct nfs4_get_lease_time_args args;
7831 struct nfs4_get_lease_time_res res = {
7832 .lr_fsinfo = fsinfo,
7834 struct nfs4_get_lease_time_data data = {
7839 struct rpc_message msg = {
7840 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7844 struct rpc_task_setup task_setup = {
7845 .rpc_client = clp->cl_rpcclient,
7846 .rpc_message = &msg,
7847 .callback_ops = &nfs4_get_lease_time_ops,
7848 .callback_data = &data,
7849 .flags = RPC_TASK_TIMEOUT,
7853 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
7854 nfs4_set_sequence_privileged(&args.la_seq_args);
7855 dprintk("--> %s\n", __func__);
7856 task = rpc_run_task(&task_setup);
7859 status = PTR_ERR(task);
7861 status = task->tk_status;
7864 dprintk("<-- %s return %d\n", __func__, status);
7870 * Initialize the values to be used by the client in CREATE_SESSION
7871 * If nfs4_init_session set the fore channel request and response sizes,
7874 * Set the back channel max_resp_sz_cached to zero to force the client to
7875 * always set csa_cachethis to FALSE because the current implementation
7876 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7878 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7879 struct rpc_clnt *clnt)
7881 unsigned int max_rqst_sz, max_resp_sz;
7882 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
7884 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7885 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7887 /* Fore channel attributes */
7888 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7889 args->fc_attrs.max_resp_sz = max_resp_sz;
7890 args->fc_attrs.max_ops = NFS4_MAX_OPS;
7891 args->fc_attrs.max_reqs = max_session_slots;
7893 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
7894 "max_ops=%u max_reqs=%u\n",
7896 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
7897 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
7899 /* Back channel attributes */
7900 args->bc_attrs.max_rqst_sz = max_bc_payload;
7901 args->bc_attrs.max_resp_sz = max_bc_payload;
7902 args->bc_attrs.max_resp_sz_cached = 0;
7903 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
7904 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
7906 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7907 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7909 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7910 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7911 args->bc_attrs.max_reqs);
7914 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7915 struct nfs41_create_session_res *res)
7917 struct nfs4_channel_attrs *sent = &args->fc_attrs;
7918 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
7920 if (rcvd->max_resp_sz > sent->max_resp_sz)
7923 * Our requested max_ops is the minimum we need; we're not
7924 * prepared to break up compounds into smaller pieces than that.
7925 * So, no point even trying to continue if the server won't
7928 if (rcvd->max_ops < sent->max_ops)
7930 if (rcvd->max_reqs == 0)
7932 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7933 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
7937 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7938 struct nfs41_create_session_res *res)
7940 struct nfs4_channel_attrs *sent = &args->bc_attrs;
7941 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
7943 if (!(res->flags & SESSION4_BACK_CHAN))
7945 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7947 if (rcvd->max_resp_sz < sent->max_resp_sz)
7949 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7951 if (rcvd->max_ops > sent->max_ops)
7953 if (rcvd->max_reqs > sent->max_reqs)
7959 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
7960 struct nfs41_create_session_res *res)
7964 ret = nfs4_verify_fore_channel_attrs(args, res);
7967 return nfs4_verify_back_channel_attrs(args, res);
7970 static void nfs4_update_session(struct nfs4_session *session,
7971 struct nfs41_create_session_res *res)
7973 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
7974 /* Mark client id and session as being confirmed */
7975 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7976 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
7977 session->flags = res->flags;
7978 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
7979 if (res->flags & SESSION4_BACK_CHAN)
7980 memcpy(&session->bc_attrs, &res->bc_attrs,
7981 sizeof(session->bc_attrs));
7984 static int _nfs4_proc_create_session(struct nfs_client *clp,
7985 struct rpc_cred *cred)
7987 struct nfs4_session *session = clp->cl_session;
7988 struct nfs41_create_session_args args = {
7990 .clientid = clp->cl_clientid,
7991 .seqid = clp->cl_seqid,
7992 .cb_program = NFS4_CALLBACK,
7994 struct nfs41_create_session_res res;
7996 struct rpc_message msg = {
7997 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8004 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
8005 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
8007 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
8008 trace_nfs4_create_session(clp, status);
8011 case -NFS4ERR_STALE_CLIENTID:
8012 case -NFS4ERR_DELAY:
8021 /* Verify the session's negotiated channel_attrs values */
8022 status = nfs4_verify_channel_attrs(&args, &res);
8023 /* Increment the clientid slot sequence id */
8026 nfs4_update_session(session, &res);
8033 * Issues a CREATE_SESSION operation to the server.
8034 * It is the responsibility of the caller to verify the session is
8035 * expired before calling this routine.
8037 int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
8041 struct nfs4_session *session = clp->cl_session;
8043 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8045 status = _nfs4_proc_create_session(clp, cred);
8049 /* Init or reset the session slot tables */
8050 status = nfs4_setup_session_slot_tables(session);
8051 dprintk("slot table setup returned %d\n", status);
8055 ptr = (unsigned *)&session->sess_id.data[0];
8056 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8057 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
8059 dprintk("<-- %s\n", __func__);
8064 * Issue the over-the-wire RPC DESTROY_SESSION.
8065 * The caller must serialize access to this routine.
8067 int nfs4_proc_destroy_session(struct nfs4_session *session,
8068 struct rpc_cred *cred)
8070 struct rpc_message msg = {
8071 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8072 .rpc_argp = session,
8077 dprintk("--> nfs4_proc_destroy_session\n");
8079 /* session is still being setup */
8080 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8083 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
8084 trace_nfs4_destroy_session(session->clp, status);
8087 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
8088 "Session has been destroyed regardless...\n", status);
8090 dprintk("<-- nfs4_proc_destroy_session\n");
8095 * Renew the cl_session lease.
8097 struct nfs4_sequence_data {
8098 struct nfs_client *clp;
8099 struct nfs4_sequence_args args;
8100 struct nfs4_sequence_res res;
8103 static void nfs41_sequence_release(void *data)
8105 struct nfs4_sequence_data *calldata = data;
8106 struct nfs_client *clp = calldata->clp;
8108 if (atomic_read(&clp->cl_count) > 1)
8109 nfs4_schedule_state_renewal(clp);
8110 nfs_put_client(clp);
8114 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8116 switch(task->tk_status) {
8117 case -NFS4ERR_DELAY:
8118 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8121 nfs4_schedule_lease_recovery(clp);
8126 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
8128 struct nfs4_sequence_data *calldata = data;
8129 struct nfs_client *clp = calldata->clp;
8131 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8134 trace_nfs4_sequence(clp, task->tk_status);
8135 if (task->tk_status < 0) {
8136 dprintk("%s ERROR %d\n", __func__, task->tk_status);
8137 if (atomic_read(&clp->cl_count) == 1)
8140 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8141 rpc_restart_call_prepare(task);
8145 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
8147 dprintk("<-- %s\n", __func__);
8150 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8152 struct nfs4_sequence_data *calldata = data;
8153 struct nfs_client *clp = calldata->clp;
8154 struct nfs4_sequence_args *args;
8155 struct nfs4_sequence_res *res;
8157 args = task->tk_msg.rpc_argp;
8158 res = task->tk_msg.rpc_resp;
8160 nfs41_setup_sequence(clp->cl_session, args, res, task);
8163 static const struct rpc_call_ops nfs41_sequence_ops = {
8164 .rpc_call_done = nfs41_sequence_call_done,
8165 .rpc_call_prepare = nfs41_sequence_prepare,
8166 .rpc_release = nfs41_sequence_release,
8169 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8170 struct rpc_cred *cred,
8173 struct nfs4_sequence_data *calldata;
8174 struct rpc_message msg = {
8175 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8178 struct rpc_task_setup task_setup_data = {
8179 .rpc_client = clp->cl_rpcclient,
8180 .rpc_message = &msg,
8181 .callback_ops = &nfs41_sequence_ops,
8182 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
8185 if (!atomic_inc_not_zero(&clp->cl_count))
8186 return ERR_PTR(-EIO);
8187 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8188 if (calldata == NULL) {
8189 nfs_put_client(clp);
8190 return ERR_PTR(-ENOMEM);
8192 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
8194 nfs4_set_sequence_privileged(&calldata->args);
8195 msg.rpc_argp = &calldata->args;
8196 msg.rpc_resp = &calldata->res;
8197 calldata->clp = clp;
8198 task_setup_data.callback_data = calldata;
8200 return rpc_run_task(&task_setup_data);
8203 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
8205 struct rpc_task *task;
8208 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
8210 task = _nfs41_proc_sequence(clp, cred, false);
8212 ret = PTR_ERR(task);
8214 rpc_put_task_async(task);
8215 dprintk("<-- %s status=%d\n", __func__, ret);
8219 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8221 struct rpc_task *task;
8224 task = _nfs41_proc_sequence(clp, cred, true);
8226 ret = PTR_ERR(task);
8229 ret = rpc_wait_for_completion_task(task);
8231 ret = task->tk_status;
8234 dprintk("<-- %s status=%d\n", __func__, ret);
8238 struct nfs4_reclaim_complete_data {
8239 struct nfs_client *clp;
8240 struct nfs41_reclaim_complete_args arg;
8241 struct nfs41_reclaim_complete_res res;
8244 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8246 struct nfs4_reclaim_complete_data *calldata = data;
8248 nfs41_setup_sequence(calldata->clp->cl_session,
8249 &calldata->arg.seq_args,
8250 &calldata->res.seq_res,
8254 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8256 switch(task->tk_status) {
8258 case -NFS4ERR_COMPLETE_ALREADY:
8259 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8261 case -NFS4ERR_DELAY:
8262 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8264 case -NFS4ERR_RETRY_UNCACHED_REP:
8267 nfs4_schedule_lease_recovery(clp);
8272 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8274 struct nfs4_reclaim_complete_data *calldata = data;
8275 struct nfs_client *clp = calldata->clp;
8276 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8278 dprintk("--> %s\n", __func__);
8279 if (!nfs41_sequence_done(task, res))
8282 trace_nfs4_reclaim_complete(clp, task->tk_status);
8283 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8284 rpc_restart_call_prepare(task);
8287 dprintk("<-- %s\n", __func__);
8290 static void nfs4_free_reclaim_complete_data(void *data)
8292 struct nfs4_reclaim_complete_data *calldata = data;
8297 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8298 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8299 .rpc_call_done = nfs4_reclaim_complete_done,
8300 .rpc_release = nfs4_free_reclaim_complete_data,
8304 * Issue a global reclaim complete.
8306 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8307 struct rpc_cred *cred)
8309 struct nfs4_reclaim_complete_data *calldata;
8310 struct rpc_task *task;
8311 struct rpc_message msg = {
8312 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
8315 struct rpc_task_setup task_setup_data = {
8316 .rpc_client = clp->cl_rpcclient,
8317 .rpc_message = &msg,
8318 .callback_ops = &nfs4_reclaim_complete_call_ops,
8319 .flags = RPC_TASK_ASYNC,
8321 int status = -ENOMEM;
8323 dprintk("--> %s\n", __func__);
8324 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8325 if (calldata == NULL)
8327 calldata->clp = clp;
8328 calldata->arg.one_fs = 0;
8330 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
8331 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
8332 msg.rpc_argp = &calldata->arg;
8333 msg.rpc_resp = &calldata->res;
8334 task_setup_data.callback_data = calldata;
8335 task = rpc_run_task(&task_setup_data);
8337 status = PTR_ERR(task);
8340 status = nfs4_wait_for_completion_rpc_task(task);
8342 status = task->tk_status;
8346 dprintk("<-- %s status=%d\n", __func__, status);
8351 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8353 struct nfs4_layoutget *lgp = calldata;
8354 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
8355 struct nfs4_session *session = nfs4_get_session(server);
8357 dprintk("--> %s\n", __func__);
8358 nfs41_setup_sequence(session, &lgp->args.seq_args,
8359 &lgp->res.seq_res, task);
8360 dprintk("<-- %s\n", __func__);
8363 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8365 struct nfs4_layoutget *lgp = calldata;
8367 dprintk("--> %s\n", __func__);
8368 nfs41_sequence_process(task, &lgp->res.seq_res);
8369 dprintk("<-- %s\n", __func__);
8373 nfs4_layoutget_handle_exception(struct rpc_task *task,
8374 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8376 struct inode *inode = lgp->args.inode;
8377 struct nfs_server *server = NFS_SERVER(inode);
8378 struct pnfs_layout_hdr *lo;
8379 int nfs4err = task->tk_status;
8380 int err, status = 0;
8383 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
8390 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8391 * on the file. set tk_status to -ENODATA to tell upper layer to
8394 case -NFS4ERR_LAYOUTUNAVAILABLE:
8398 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8399 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8401 case -NFS4ERR_BADLAYOUT:
8402 status = -EOVERFLOW;
8405 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
8406 * (or clients) writing to the same RAID stripe except when
8407 * the minlength argument is 0 (see RFC5661 section 18.43.3).
8409 * Treat it like we would RECALLCONFLICT -- we retry for a little
8410 * while, and then eventually give up.
8412 case -NFS4ERR_LAYOUTTRYLATER:
8413 if (lgp->args.minlength == 0) {
8414 status = -EOVERFLOW;
8419 case -NFS4ERR_RECALLCONFLICT:
8420 status = -ERECALLCONFLICT;
8422 case -NFS4ERR_DELEG_REVOKED:
8423 case -NFS4ERR_ADMIN_REVOKED:
8424 case -NFS4ERR_EXPIRED:
8425 case -NFS4ERR_BAD_STATEID:
8426 exception->timeout = 0;
8427 spin_lock(&inode->i_lock);
8428 lo = NFS_I(inode)->layout;
8429 /* If the open stateid was bad, then recover it. */
8430 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8431 nfs4_stateid_match_other(&lgp->args.stateid,
8432 &lgp->args.ctx->state->stateid)) {
8433 spin_unlock(&inode->i_lock);
8434 exception->state = lgp->args.ctx->state;
8435 exception->stateid = &lgp->args.stateid;
8440 * Mark the bad layout state as invalid, then retry
8442 pnfs_mark_layout_stateid_invalid(lo, &head);
8443 spin_unlock(&inode->i_lock);
8444 pnfs_free_lseg_list(&head);
8449 err = nfs4_handle_exception(server, nfs4err, exception);
8451 if (exception->retry)
8457 dprintk("<-- %s\n", __func__);
8461 static size_t max_response_pages(struct nfs_server *server)
8463 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8464 return nfs_page_array_len(0, max_resp_sz);
8467 static void nfs4_free_pages(struct page **pages, size_t size)
8474 for (i = 0; i < size; i++) {
8477 __free_page(pages[i]);
8482 static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8484 struct page **pages;
8487 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8489 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8493 for (i = 0; i < size; i++) {
8494 pages[i] = alloc_page(gfp_flags);
8496 dprintk("%s: failed to allocate page\n", __func__);
8497 nfs4_free_pages(pages, size);
8505 static void nfs4_layoutget_release(void *calldata)
8507 struct nfs4_layoutget *lgp = calldata;
8508 struct inode *inode = lgp->args.inode;
8509 struct nfs_server *server = NFS_SERVER(inode);
8510 size_t max_pages = max_response_pages(server);
8512 dprintk("--> %s\n", __func__);
8513 nfs4_free_pages(lgp->args.layout.pages, max_pages);
8514 pnfs_put_layout_hdr(NFS_I(inode)->layout);
8515 put_nfs_open_context(lgp->args.ctx);
8517 dprintk("<-- %s\n", __func__);
8520 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8521 .rpc_call_prepare = nfs4_layoutget_prepare,
8522 .rpc_call_done = nfs4_layoutget_done,
8523 .rpc_release = nfs4_layoutget_release,
8526 struct pnfs_layout_segment *
8527 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
8529 struct inode *inode = lgp->args.inode;
8530 struct nfs_server *server = NFS_SERVER(inode);
8531 size_t max_pages = max_response_pages(server);
8532 struct rpc_task *task;
8533 struct rpc_message msg = {
8534 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8535 .rpc_argp = &lgp->args,
8536 .rpc_resp = &lgp->res,
8537 .rpc_cred = lgp->cred,
8539 struct rpc_task_setup task_setup_data = {
8540 .rpc_client = server->client,
8541 .rpc_message = &msg,
8542 .callback_ops = &nfs4_layoutget_call_ops,
8543 .callback_data = lgp,
8544 .flags = RPC_TASK_ASYNC,
8546 struct pnfs_layout_segment *lseg = NULL;
8547 struct nfs4_exception exception = {
8549 .timeout = *timeout,
8553 dprintk("--> %s\n", __func__);
8555 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8556 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8558 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8559 if (!lgp->args.layout.pages) {
8560 nfs4_layoutget_release(lgp);
8561 return ERR_PTR(-ENOMEM);
8563 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8565 lgp->res.layoutp = &lgp->args.layout;
8566 lgp->res.seq_res.sr_slot = NULL;
8567 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
8569 task = rpc_run_task(&task_setup_data);
8571 return ERR_CAST(task);
8572 status = nfs4_wait_for_completion_rpc_task(task);
8574 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8575 *timeout = exception.timeout;
8578 trace_nfs4_layoutget(lgp->args.ctx,
8584 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8585 if (status == 0 && lgp->res.layoutp->len)
8586 lseg = pnfs_layout_process(lgp);
8587 nfs4_sequence_free_slot(&lgp->res.seq_res);
8589 dprintk("<-- %s status=%d\n", __func__, status);
8591 return ERR_PTR(status);
8596 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8598 struct nfs4_layoutreturn *lrp = calldata;
8600 dprintk("--> %s\n", __func__);
8601 nfs41_setup_sequence(lrp->clp->cl_session,
8602 &lrp->args.seq_args,
8607 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8609 struct nfs4_layoutreturn *lrp = calldata;
8610 struct nfs_server *server;
8612 dprintk("--> %s\n", __func__);
8614 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
8617 server = NFS_SERVER(lrp->args.inode);
8618 switch (task->tk_status) {
8620 task->tk_status = 0;
8623 case -NFS4ERR_DELAY:
8624 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
8626 nfs4_sequence_free_slot(&lrp->res.seq_res);
8627 rpc_restart_call_prepare(task);
8630 dprintk("<-- %s\n", __func__);
8633 static void nfs4_layoutreturn_release(void *calldata)
8635 struct nfs4_layoutreturn *lrp = calldata;
8636 struct pnfs_layout_hdr *lo = lrp->args.layout;
8638 dprintk("--> %s\n", __func__);
8639 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
8640 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
8641 nfs4_sequence_free_slot(&lrp->res.seq_res);
8642 pnfs_put_layout_hdr(lrp->args.layout);
8643 nfs_iput_and_deactive(lrp->inode);
8644 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
8645 lrp->ld_private.ops->free(&lrp->ld_private);
8647 dprintk("<-- %s\n", __func__);
8650 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8651 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8652 .rpc_call_done = nfs4_layoutreturn_done,
8653 .rpc_release = nfs4_layoutreturn_release,
8656 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
8658 struct rpc_task *task;
8659 struct rpc_message msg = {
8660 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8661 .rpc_argp = &lrp->args,
8662 .rpc_resp = &lrp->res,
8663 .rpc_cred = lrp->cred,
8665 struct rpc_task_setup task_setup_data = {
8666 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
8667 .rpc_message = &msg,
8668 .callback_ops = &nfs4_layoutreturn_call_ops,
8669 .callback_data = lrp,
8673 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8674 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8675 &task_setup_data.rpc_client, &msg);
8677 dprintk("--> %s\n", __func__);
8679 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8681 nfs4_layoutreturn_release(lrp);
8684 task_setup_data.flags |= RPC_TASK_ASYNC;
8686 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
8687 task = rpc_run_task(&task_setup_data);
8689 return PTR_ERR(task);
8691 status = task->tk_status;
8692 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
8693 dprintk("<-- %s status=%d\n", __func__, status);
8699 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
8700 struct pnfs_device *pdev,
8701 struct rpc_cred *cred)
8703 struct nfs4_getdeviceinfo_args args = {
8705 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8706 NOTIFY_DEVICEID4_DELETE,
8708 struct nfs4_getdeviceinfo_res res = {
8711 struct rpc_message msg = {
8712 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8719 dprintk("--> %s\n", __func__);
8720 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
8721 if (res.notification & ~args.notify_types)
8722 dprintk("%s: unsupported notification\n", __func__);
8723 if (res.notification != args.notify_types)
8726 dprintk("<-- %s status=%d\n", __func__, status);
8731 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8732 struct pnfs_device *pdev,
8733 struct rpc_cred *cred)
8735 struct nfs4_exception exception = { };
8739 err = nfs4_handle_exception(server,
8740 _nfs4_proc_getdeviceinfo(server, pdev, cred),
8742 } while (exception.retry);
8745 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8747 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8749 struct nfs4_layoutcommit_data *data = calldata;
8750 struct nfs_server *server = NFS_SERVER(data->args.inode);
8751 struct nfs4_session *session = nfs4_get_session(server);
8753 nfs41_setup_sequence(session,
8754 &data->args.seq_args,
8760 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8762 struct nfs4_layoutcommit_data *data = calldata;
8763 struct nfs_server *server = NFS_SERVER(data->args.inode);
8765 if (!nfs41_sequence_done(task, &data->res.seq_res))
8768 switch (task->tk_status) { /* Just ignore these failures */
8769 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8770 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8771 case -NFS4ERR_BADLAYOUT: /* no layout */
8772 case -NFS4ERR_GRACE: /* loca_recalim always false */
8773 task->tk_status = 0;
8777 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
8778 rpc_restart_call_prepare(task);
8784 static void nfs4_layoutcommit_release(void *calldata)
8786 struct nfs4_layoutcommit_data *data = calldata;
8788 pnfs_cleanup_layoutcommit(data);
8789 nfs_post_op_update_inode_force_wcc(data->args.inode,
8791 put_rpccred(data->cred);
8792 nfs_iput_and_deactive(data->inode);
8796 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8797 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8798 .rpc_call_done = nfs4_layoutcommit_done,
8799 .rpc_release = nfs4_layoutcommit_release,
8803 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
8805 struct rpc_message msg = {
8806 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8807 .rpc_argp = &data->args,
8808 .rpc_resp = &data->res,
8809 .rpc_cred = data->cred,
8811 struct rpc_task_setup task_setup_data = {
8812 .task = &data->task,
8813 .rpc_client = NFS_CLIENT(data->args.inode),
8814 .rpc_message = &msg,
8815 .callback_ops = &nfs4_layoutcommit_ops,
8816 .callback_data = data,
8818 struct rpc_task *task;
8821 dprintk("NFS: initiating layoutcommit call. sync %d "
8822 "lbw: %llu inode %lu\n", sync,
8823 data->args.lastbytewritten,
8824 data->args.inode->i_ino);
8827 data->inode = nfs_igrab_and_active(data->args.inode);
8828 if (data->inode == NULL) {
8829 nfs4_layoutcommit_release(data);
8832 task_setup_data.flags = RPC_TASK_ASYNC;
8834 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
8835 task = rpc_run_task(&task_setup_data);
8837 return PTR_ERR(task);
8839 status = task->tk_status;
8840 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
8841 dprintk("%s: status %d\n", __func__, status);
8847 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8848 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8851 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8852 struct nfs_fsinfo *info,
8853 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
8855 struct nfs41_secinfo_no_name_args args = {
8856 .style = SECINFO_STYLE_CURRENT_FH,
8858 struct nfs4_secinfo_res res = {
8861 struct rpc_message msg = {
8862 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8866 struct rpc_clnt *clnt = server->client;
8867 struct rpc_cred *cred = NULL;
8870 if (use_integrity) {
8871 clnt = server->nfs_client->cl_rpcclient;
8872 cred = nfs4_get_clid_cred(server->nfs_client);
8873 msg.rpc_cred = cred;
8876 dprintk("--> %s\n", __func__);
8877 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8879 dprintk("<-- %s status=%d\n", __func__, status);
8888 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8889 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8891 struct nfs4_exception exception = { };
8894 /* first try using integrity protection */
8895 err = -NFS4ERR_WRONGSEC;
8897 /* try to use integrity protection with machine cred */
8898 if (_nfs4_is_integrity_protected(server->nfs_client))
8899 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8903 * if unable to use integrity protection, or SECINFO with
8904 * integrity protection returns NFS4ERR_WRONGSEC (which is
8905 * disallowed by spec, but exists in deployed servers) use
8906 * the current filesystem's rpc_client and the user cred.
8908 if (err == -NFS4ERR_WRONGSEC)
8909 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8914 case -NFS4ERR_WRONGSEC:
8918 err = nfs4_handle_exception(server, err, &exception);
8920 } while (exception.retry);
8926 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8927 struct nfs_fsinfo *info)
8931 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
8932 struct nfs4_secinfo_flavors *flavors;
8933 struct nfs4_secinfo4 *secinfo;
8936 page = alloc_page(GFP_KERNEL);
8942 flavors = page_address(page);
8943 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8946 * Fall back on "guess and check" method if
8947 * the server doesn't support SECINFO_NO_NAME
8949 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
8950 err = nfs4_find_root_sec(server, fhandle, info);
8956 for (i = 0; i < flavors->num_flavors; i++) {
8957 secinfo = &flavors->flavors[i];
8959 switch (secinfo->flavor) {
8963 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8964 &secinfo->flavor_info);
8967 flavor = RPC_AUTH_MAXFLAVOR;
8971 if (!nfs_auth_info_match(&server->auth_info, flavor))
8972 flavor = RPC_AUTH_MAXFLAVOR;
8974 if (flavor != RPC_AUTH_MAXFLAVOR) {
8975 err = nfs4_lookup_root_sec(server, fhandle,
8982 if (flavor == RPC_AUTH_MAXFLAVOR)
8993 static int _nfs41_test_stateid(struct nfs_server *server,
8994 nfs4_stateid *stateid,
8995 struct rpc_cred *cred)
8998 struct nfs41_test_stateid_args args = {
9001 struct nfs41_test_stateid_res res;
9002 struct rpc_message msg = {
9003 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9008 struct rpc_clnt *rpc_client = server->client;
9010 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9013 dprintk("NFS call test_stateid %p\n", stateid);
9014 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
9015 nfs4_set_sequence_privileged(&args.seq_args);
9016 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
9017 &args.seq_args, &res.seq_res);
9018 if (status != NFS_OK) {
9019 dprintk("NFS reply test_stateid: failed, %d\n", status);
9022 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
9026 static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9027 int err, struct nfs4_exception *exception)
9029 exception->retry = 0;
9031 case -NFS4ERR_DELAY:
9032 case -NFS4ERR_RETRY_UNCACHED_REP:
9033 nfs4_handle_exception(server, err, exception);
9035 case -NFS4ERR_BADSESSION:
9036 case -NFS4ERR_BADSLOT:
9037 case -NFS4ERR_BAD_HIGH_SLOT:
9038 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9039 case -NFS4ERR_DEADSESSION:
9040 nfs4_do_handle_exception(server, err, exception);
9045 * nfs41_test_stateid - perform a TEST_STATEID operation
9047 * @server: server / transport on which to perform the operation
9048 * @stateid: state ID to test
9051 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9052 * Otherwise a negative NFS4ERR value is returned if the operation
9053 * failed or the state ID is not currently valid.
9055 static int nfs41_test_stateid(struct nfs_server *server,
9056 nfs4_stateid *stateid,
9057 struct rpc_cred *cred)
9059 struct nfs4_exception exception = { };
9062 err = _nfs41_test_stateid(server, stateid, cred);
9063 nfs4_handle_delay_or_session_error(server, err, &exception);
9064 } while (exception.retry);
9068 struct nfs_free_stateid_data {
9069 struct nfs_server *server;
9070 struct nfs41_free_stateid_args args;
9071 struct nfs41_free_stateid_res res;
9074 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9076 struct nfs_free_stateid_data *data = calldata;
9077 nfs41_setup_sequence(nfs4_get_session(data->server),
9078 &data->args.seq_args,
9083 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9085 struct nfs_free_stateid_data *data = calldata;
9087 nfs41_sequence_done(task, &data->res.seq_res);
9089 switch (task->tk_status) {
9090 case -NFS4ERR_DELAY:
9091 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
9092 rpc_restart_call_prepare(task);
9096 static void nfs41_free_stateid_release(void *calldata)
9101 static const struct rpc_call_ops nfs41_free_stateid_ops = {
9102 .rpc_call_prepare = nfs41_free_stateid_prepare,
9103 .rpc_call_done = nfs41_free_stateid_done,
9104 .rpc_release = nfs41_free_stateid_release,
9107 static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
9108 const nfs4_stateid *stateid,
9109 struct rpc_cred *cred,
9112 struct rpc_message msg = {
9113 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
9116 struct rpc_task_setup task_setup = {
9117 .rpc_client = server->client,
9118 .rpc_message = &msg,
9119 .callback_ops = &nfs41_free_stateid_ops,
9120 .flags = RPC_TASK_ASYNC,
9122 struct nfs_free_stateid_data *data;
9124 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9125 &task_setup.rpc_client, &msg);
9127 dprintk("NFS call free_stateid %p\n", stateid);
9128 data = kmalloc(sizeof(*data), GFP_NOFS);
9130 return ERR_PTR(-ENOMEM);
9131 data->server = server;
9132 nfs4_stateid_copy(&data->args.stateid, stateid);
9134 task_setup.callback_data = data;
9136 msg.rpc_argp = &data->args;
9137 msg.rpc_resp = &data->res;
9138 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
9140 nfs4_set_sequence_privileged(&data->args.seq_args);
9142 return rpc_run_task(&task_setup);
9146 * nfs41_free_stateid - perform a FREE_STATEID operation
9148 * @server: server / transport on which to perform the operation
9149 * @stateid: state ID to release
9151 * @is_recovery: set to true if this call needs to be privileged
9153 * Note: this function is always asynchronous.
9155 static int nfs41_free_stateid(struct nfs_server *server,
9156 const nfs4_stateid *stateid,
9157 struct rpc_cred *cred,
9160 struct rpc_task *task;
9162 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
9164 return PTR_ERR(task);
9170 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
9172 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
9174 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
9175 nfs4_free_lock_state(server, lsp);
9178 static bool nfs41_match_stateid(const nfs4_stateid *s1,
9179 const nfs4_stateid *s2)
9181 if (s1->type != s2->type)
9184 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
9187 if (s1->seqid == s2->seqid)
9189 if (s1->seqid == 0 || s2->seqid == 0)
9195 #endif /* CONFIG_NFS_V4_1 */
9197 static bool nfs4_match_stateid(const nfs4_stateid *s1,
9198 const nfs4_stateid *s2)
9200 return nfs4_stateid_match(s1, s2);
9204 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
9205 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9206 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9207 .recover_open = nfs4_open_reclaim,
9208 .recover_lock = nfs4_lock_reclaim,
9209 .establish_clid = nfs4_init_clientid,
9210 .detect_trunking = nfs40_discover_server_trunking,
9213 #if defined(CONFIG_NFS_V4_1)
9214 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
9215 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9216 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9217 .recover_open = nfs4_open_reclaim,
9218 .recover_lock = nfs4_lock_reclaim,
9219 .establish_clid = nfs41_init_clientid,
9220 .reclaim_complete = nfs41_proc_reclaim_complete,
9221 .detect_trunking = nfs41_discover_server_trunking,
9223 #endif /* CONFIG_NFS_V4_1 */
9225 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
9226 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9227 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
9228 .recover_open = nfs40_open_expired,
9229 .recover_lock = nfs4_lock_expired,
9230 .establish_clid = nfs4_init_clientid,
9233 #if defined(CONFIG_NFS_V4_1)
9234 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
9235 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9236 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
9237 .recover_open = nfs41_open_expired,
9238 .recover_lock = nfs41_lock_expired,
9239 .establish_clid = nfs41_init_clientid,
9241 #endif /* CONFIG_NFS_V4_1 */
9243 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
9244 .sched_state_renewal = nfs4_proc_async_renew,
9245 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
9246 .renew_lease = nfs4_proc_renew,
9249 #if defined(CONFIG_NFS_V4_1)
9250 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
9251 .sched_state_renewal = nfs41_proc_async_sequence,
9252 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
9253 .renew_lease = nfs4_proc_sequence,
9257 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
9258 .get_locations = _nfs40_proc_get_locations,
9259 .fsid_present = _nfs40_proc_fsid_present,
9262 #if defined(CONFIG_NFS_V4_1)
9263 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
9264 .get_locations = _nfs41_proc_get_locations,
9265 .fsid_present = _nfs41_proc_fsid_present,
9267 #endif /* CONFIG_NFS_V4_1 */
9269 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9271 .init_caps = NFS_CAP_READDIRPLUS
9272 | NFS_CAP_ATOMIC_OPEN
9273 | NFS_CAP_POSIX_LOCK,
9274 .init_client = nfs40_init_client,
9275 .shutdown_client = nfs40_shutdown_client,
9276 .match_stateid = nfs4_match_stateid,
9277 .find_root_sec = nfs4_find_root_sec,
9278 .free_lock_state = nfs4_release_lockowner,
9279 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
9280 .alloc_seqid = nfs_alloc_seqid,
9281 .call_sync_ops = &nfs40_call_sync_ops,
9282 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9283 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9284 .state_renewal_ops = &nfs40_state_renewal_ops,
9285 .mig_recovery_ops = &nfs40_mig_recovery_ops,
9288 #if defined(CONFIG_NFS_V4_1)
9289 static struct nfs_seqid *
9290 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9295 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9297 .init_caps = NFS_CAP_READDIRPLUS
9298 | NFS_CAP_ATOMIC_OPEN
9299 | NFS_CAP_POSIX_LOCK
9300 | NFS_CAP_STATEID_NFSV41
9301 | NFS_CAP_ATOMIC_OPEN_V1,
9302 .init_client = nfs41_init_client,
9303 .shutdown_client = nfs41_shutdown_client,
9304 .match_stateid = nfs41_match_stateid,
9305 .find_root_sec = nfs41_find_root_sec,
9306 .free_lock_state = nfs41_free_lock_state,
9307 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
9308 .alloc_seqid = nfs_alloc_no_seqid,
9309 .session_trunk = nfs4_test_session_trunk,
9310 .call_sync_ops = &nfs41_call_sync_ops,
9311 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9312 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9313 .state_renewal_ops = &nfs41_state_renewal_ops,
9314 .mig_recovery_ops = &nfs41_mig_recovery_ops,
9318 #if defined(CONFIG_NFS_V4_2)
9319 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9321 .init_caps = NFS_CAP_READDIRPLUS
9322 | NFS_CAP_ATOMIC_OPEN
9323 | NFS_CAP_POSIX_LOCK
9324 | NFS_CAP_STATEID_NFSV41
9325 | NFS_CAP_ATOMIC_OPEN_V1
9328 | NFS_CAP_DEALLOCATE
9330 | NFS_CAP_LAYOUTSTATS
9332 .init_client = nfs41_init_client,
9333 .shutdown_client = nfs41_shutdown_client,
9334 .match_stateid = nfs41_match_stateid,
9335 .find_root_sec = nfs41_find_root_sec,
9336 .free_lock_state = nfs41_free_lock_state,
9337 .call_sync_ops = &nfs41_call_sync_ops,
9338 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
9339 .alloc_seqid = nfs_alloc_no_seqid,
9340 .session_trunk = nfs4_test_session_trunk,
9341 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9342 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9343 .state_renewal_ops = &nfs41_state_renewal_ops,
9344 .mig_recovery_ops = &nfs41_mig_recovery_ops,
9348 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9349 [0] = &nfs_v4_0_minor_ops,
9350 #if defined(CONFIG_NFS_V4_1)
9351 [1] = &nfs_v4_1_minor_ops,
9353 #if defined(CONFIG_NFS_V4_2)
9354 [2] = &nfs_v4_2_minor_ops,
9358 static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
9360 ssize_t error, error2;
9362 error = generic_listxattr(dentry, list, size);
9370 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9373 return error + error2;
9376 static const struct inode_operations nfs4_dir_inode_operations = {
9377 .create = nfs_create,
9378 .lookup = nfs_lookup,
9379 .atomic_open = nfs_atomic_open,
9381 .unlink = nfs_unlink,
9382 .symlink = nfs_symlink,
9386 .rename = nfs_rename,
9387 .permission = nfs_permission,
9388 .getattr = nfs_getattr,
9389 .setattr = nfs_setattr,
9390 .listxattr = nfs4_listxattr,
9393 static const struct inode_operations nfs4_file_inode_operations = {
9394 .permission = nfs_permission,
9395 .getattr = nfs_getattr,
9396 .setattr = nfs_setattr,
9397 .listxattr = nfs4_listxattr,
9400 const struct nfs_rpc_ops nfs_v4_clientops = {
9401 .version = 4, /* protocol version */
9402 .dentry_ops = &nfs4_dentry_operations,
9403 .dir_inode_ops = &nfs4_dir_inode_operations,
9404 .file_inode_ops = &nfs4_file_inode_operations,
9405 .file_ops = &nfs4_file_operations,
9406 .getroot = nfs4_proc_get_root,
9407 .submount = nfs4_submount,
9408 .try_mount = nfs4_try_mount,
9409 .getattr = nfs4_proc_getattr,
9410 .setattr = nfs4_proc_setattr,
9411 .lookup = nfs4_proc_lookup,
9412 .access = nfs4_proc_access,
9413 .readlink = nfs4_proc_readlink,
9414 .create = nfs4_proc_create,
9415 .remove = nfs4_proc_remove,
9416 .unlink_setup = nfs4_proc_unlink_setup,
9417 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
9418 .unlink_done = nfs4_proc_unlink_done,
9419 .rename_setup = nfs4_proc_rename_setup,
9420 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
9421 .rename_done = nfs4_proc_rename_done,
9422 .link = nfs4_proc_link,
9423 .symlink = nfs4_proc_symlink,
9424 .mkdir = nfs4_proc_mkdir,
9425 .rmdir = nfs4_proc_remove,
9426 .readdir = nfs4_proc_readdir,
9427 .mknod = nfs4_proc_mknod,
9428 .statfs = nfs4_proc_statfs,
9429 .fsinfo = nfs4_proc_fsinfo,
9430 .pathconf = nfs4_proc_pathconf,
9431 .set_capabilities = nfs4_server_capabilities,
9432 .decode_dirent = nfs4_decode_dirent,
9433 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
9434 .read_setup = nfs4_proc_read_setup,
9435 .read_done = nfs4_read_done,
9436 .write_setup = nfs4_proc_write_setup,
9437 .write_done = nfs4_write_done,
9438 .commit_setup = nfs4_proc_commit_setup,
9439 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
9440 .commit_done = nfs4_commit_done,
9441 .lock = nfs4_proc_lock,
9442 .clear_acl_cache = nfs4_zap_acl_attr,
9443 .close_context = nfs4_close_context,
9444 .open_context = nfs4_atomic_open,
9445 .have_delegation = nfs4_have_delegation,
9446 .return_delegation = nfs4_inode_return_delegation,
9447 .alloc_client = nfs4_alloc_client,
9448 .init_client = nfs4_init_client,
9449 .free_client = nfs4_free_client,
9450 .create_server = nfs4_create_server,
9451 .clone_server = nfs_clone_server,
9454 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
9455 .name = XATTR_NAME_NFSV4_ACL,
9456 .list = nfs4_xattr_list_nfs4_acl,
9457 .get = nfs4_xattr_get_nfs4_acl,
9458 .set = nfs4_xattr_set_nfs4_acl,
9461 const struct xattr_handler *nfs4_xattr_handlers[] = {
9462 &nfs4_xattr_nfs4_acl_handler,
9463 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
9464 &nfs4_xattr_nfs4_label_handler,