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)
78 static int _nfs4_proc_open(struct nfs4_opendata *data);
79 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
80 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
81 static int nfs4_async_handle_error(struct rpc_task *, struct nfs_server *, struct nfs4_state *, long *);
82 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
83 static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
84 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label);
85 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
86 struct nfs_fattr *fattr, struct iattr *sattr,
87 struct nfs4_state *state, struct nfs4_label *ilabel,
88 struct nfs4_label *olabel);
89 #ifdef CONFIG_NFS_V4_1
90 static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
92 static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *,
96 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
97 static inline struct nfs4_label *
98 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
99 struct iattr *sattr, struct nfs4_label *label)
106 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
109 err = security_dentry_init_security(dentry, sattr->ia_mode,
110 &dentry->d_name, (void **)&label->label, &label->len);
117 nfs4_label_release_security(struct nfs4_label *label)
120 security_release_secctx(label->label, label->len);
122 static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
125 return server->attr_bitmask;
127 return server->attr_bitmask_nl;
130 static inline struct nfs4_label *
131 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
132 struct iattr *sattr, struct nfs4_label *l)
135 nfs4_label_release_security(struct nfs4_label *label)
138 nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
139 { return server->attr_bitmask; }
142 /* Prevent leaks of NFSv4 errors into userland */
143 static int nfs4_map_errors(int err)
148 case -NFS4ERR_RESOURCE:
149 case -NFS4ERR_LAYOUTTRYLATER:
150 case -NFS4ERR_RECALLCONFLICT:
152 case -NFS4ERR_WRONGSEC:
153 case -NFS4ERR_WRONG_CRED:
155 case -NFS4ERR_BADOWNER:
156 case -NFS4ERR_BADNAME:
158 case -NFS4ERR_SHARE_DENIED:
160 case -NFS4ERR_MINOR_VERS_MISMATCH:
161 return -EPROTONOSUPPORT;
162 case -NFS4ERR_FILE_OPEN:
165 dprintk("%s could not handle NFSv4 error %d\n",
173 * This is our standard bitmap for GETATTR requests.
175 const u32 nfs4_fattr_bitmap[3] = {
177 | FATTR4_WORD0_CHANGE
180 | FATTR4_WORD0_FILEID,
182 | FATTR4_WORD1_NUMLINKS
184 | FATTR4_WORD1_OWNER_GROUP
185 | FATTR4_WORD1_RAWDEV
186 | FATTR4_WORD1_SPACE_USED
187 | FATTR4_WORD1_TIME_ACCESS
188 | FATTR4_WORD1_TIME_METADATA
189 | FATTR4_WORD1_TIME_MODIFY
190 | FATTR4_WORD1_MOUNTED_ON_FILEID,
191 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
192 FATTR4_WORD2_SECURITY_LABEL
196 static const u32 nfs4_pnfs_open_bitmap[3] = {
198 | FATTR4_WORD0_CHANGE
201 | FATTR4_WORD0_FILEID,
203 | FATTR4_WORD1_NUMLINKS
205 | FATTR4_WORD1_OWNER_GROUP
206 | FATTR4_WORD1_RAWDEV
207 | FATTR4_WORD1_SPACE_USED
208 | FATTR4_WORD1_TIME_ACCESS
209 | FATTR4_WORD1_TIME_METADATA
210 | FATTR4_WORD1_TIME_MODIFY,
211 FATTR4_WORD2_MDSTHRESHOLD
214 static const u32 nfs4_open_noattr_bitmap[3] = {
216 | FATTR4_WORD0_CHANGE
217 | FATTR4_WORD0_FILEID,
220 const u32 nfs4_statfs_bitmap[3] = {
221 FATTR4_WORD0_FILES_AVAIL
222 | FATTR4_WORD0_FILES_FREE
223 | FATTR4_WORD0_FILES_TOTAL,
224 FATTR4_WORD1_SPACE_AVAIL
225 | FATTR4_WORD1_SPACE_FREE
226 | FATTR4_WORD1_SPACE_TOTAL
229 const u32 nfs4_pathconf_bitmap[3] = {
231 | FATTR4_WORD0_MAXNAME,
235 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
236 | FATTR4_WORD0_MAXREAD
237 | FATTR4_WORD0_MAXWRITE
238 | FATTR4_WORD0_LEASE_TIME,
239 FATTR4_WORD1_TIME_DELTA
240 | FATTR4_WORD1_FS_LAYOUT_TYPES,
241 FATTR4_WORD2_LAYOUT_BLKSIZE
244 const u32 nfs4_fs_locations_bitmap[3] = {
246 | FATTR4_WORD0_CHANGE
249 | FATTR4_WORD0_FILEID
250 | FATTR4_WORD0_FS_LOCATIONS,
252 | FATTR4_WORD1_NUMLINKS
254 | FATTR4_WORD1_OWNER_GROUP
255 | FATTR4_WORD1_RAWDEV
256 | FATTR4_WORD1_SPACE_USED
257 | FATTR4_WORD1_TIME_ACCESS
258 | FATTR4_WORD1_TIME_METADATA
259 | FATTR4_WORD1_TIME_MODIFY
260 | FATTR4_WORD1_MOUNTED_ON_FILEID,
263 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
264 struct nfs4_readdir_arg *readdir)
269 readdir->cookie = cookie;
270 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
275 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
280 * NFSv4 servers do not return entries for '.' and '..'
281 * Therefore, we fake these entries here. We let '.'
282 * have cookie 0 and '..' have cookie 1. Note that
283 * when talking to the server, we always send cookie 0
286 start = p = kmap_atomic(*readdir->pages);
289 *p++ = xdr_one; /* next */
290 *p++ = xdr_zero; /* cookie, first word */
291 *p++ = xdr_one; /* cookie, second word */
292 *p++ = xdr_one; /* entry len */
293 memcpy(p, ".\0\0\0", 4); /* entry */
295 *p++ = xdr_one; /* bitmap length */
296 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
297 *p++ = htonl(8); /* attribute buffer length */
298 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
301 *p++ = xdr_one; /* next */
302 *p++ = xdr_zero; /* cookie, first word */
303 *p++ = xdr_two; /* cookie, second word */
304 *p++ = xdr_two; /* entry len */
305 memcpy(p, "..\0\0", 4); /* entry */
307 *p++ = xdr_one; /* bitmap length */
308 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
309 *p++ = htonl(8); /* attribute buffer length */
310 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
312 readdir->pgbase = (char *)p - (char *)start;
313 readdir->count -= readdir->pgbase;
314 kunmap_atomic(start);
317 static long nfs4_update_delay(long *timeout)
321 return NFS4_POLL_RETRY_MAX;
323 *timeout = NFS4_POLL_RETRY_MIN;
324 if (*timeout > NFS4_POLL_RETRY_MAX)
325 *timeout = NFS4_POLL_RETRY_MAX;
331 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
337 freezable_schedule_timeout_killable_unsafe(
338 nfs4_update_delay(timeout));
339 if (fatal_signal_pending(current))
344 /* This is the error handling routine for processes that are allowed
347 static int nfs4_do_handle_exception(struct nfs_server *server,
348 int errorcode, struct nfs4_exception *exception)
350 struct nfs_client *clp = server->nfs_client;
351 struct nfs4_state *state = exception->state;
352 struct inode *inode = exception->inode;
355 exception->delay = 0;
356 exception->recovering = 0;
357 exception->retry = 0;
361 case -NFS4ERR_OPENMODE:
362 case -NFS4ERR_DELEG_REVOKED:
363 case -NFS4ERR_ADMIN_REVOKED:
364 case -NFS4ERR_BAD_STATEID:
365 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
366 nfs4_inode_return_delegation(inode);
367 exception->retry = 1;
372 ret = nfs4_schedule_stateid_recovery(server, state);
375 goto wait_on_recovery;
376 case -NFS4ERR_EXPIRED:
378 ret = nfs4_schedule_stateid_recovery(server, state);
382 case -NFS4ERR_STALE_STATEID:
383 case -NFS4ERR_STALE_CLIENTID:
384 nfs4_schedule_lease_recovery(clp);
385 goto wait_on_recovery;
387 ret = nfs4_schedule_migration_recovery(server);
390 goto wait_on_recovery;
391 case -NFS4ERR_LEASE_MOVED:
392 nfs4_schedule_lease_moved_recovery(clp);
393 goto wait_on_recovery;
394 #if defined(CONFIG_NFS_V4_1)
395 case -NFS4ERR_BADSESSION:
396 case -NFS4ERR_BADSLOT:
397 case -NFS4ERR_BAD_HIGH_SLOT:
398 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
399 case -NFS4ERR_DEADSESSION:
400 case -NFS4ERR_SEQ_FALSE_RETRY:
401 case -NFS4ERR_SEQ_MISORDERED:
402 dprintk("%s ERROR: %d Reset session\n", __func__,
404 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
405 goto wait_on_recovery;
406 #endif /* defined(CONFIG_NFS_V4_1) */
407 case -NFS4ERR_FILE_OPEN:
408 if (exception->timeout > HZ) {
409 /* We have retried a decent amount, time to
416 nfs_inc_server_stats(server, NFSIOS_DELAY);
418 exception->delay = 1;
421 case -NFS4ERR_RETRY_UNCACHED_REP:
422 case -NFS4ERR_OLD_STATEID:
423 exception->retry = 1;
425 case -NFS4ERR_BADOWNER:
426 /* The following works around a Linux server bug! */
427 case -NFS4ERR_BADNAME:
428 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
429 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
430 exception->retry = 1;
431 printk(KERN_WARNING "NFS: v4 server %s "
432 "does not accept raw "
434 "Reenabling the idmapper.\n",
435 server->nfs_client->cl_hostname);
438 /* We failed to handle the error */
439 return nfs4_map_errors(ret);
441 exception->recovering = 1;
445 /* This is the error handling routine for processes that are allowed
448 int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
450 struct nfs_client *clp = server->nfs_client;
453 ret = nfs4_do_handle_exception(server, errorcode, exception);
454 if (exception->delay) {
455 ret = nfs4_delay(server->client, &exception->timeout);
458 if (exception->recovering) {
459 ret = nfs4_wait_clnt_recover(clp);
460 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
467 exception->retry = 1;
472 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
473 * or 'false' otherwise.
475 static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
477 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
479 if (flavor == RPC_AUTH_GSS_KRB5I ||
480 flavor == RPC_AUTH_GSS_KRB5P)
486 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
488 spin_lock(&clp->cl_lock);
489 if (time_before(clp->cl_last_renewal,timestamp))
490 clp->cl_last_renewal = timestamp;
491 spin_unlock(&clp->cl_lock);
494 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
496 struct nfs_client *clp = server->nfs_client;
498 if (!nfs4_has_session(clp))
499 do_renew_lease(clp, timestamp);
502 struct nfs4_call_sync_data {
503 const struct nfs_server *seq_server;
504 struct nfs4_sequence_args *seq_args;
505 struct nfs4_sequence_res *seq_res;
508 void nfs4_init_sequence(struct nfs4_sequence_args *args,
509 struct nfs4_sequence_res *res, int cache_reply)
511 args->sa_slot = NULL;
512 args->sa_cache_this = cache_reply;
513 args->sa_privileged = 0;
518 static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
520 args->sa_privileged = 1;
523 int nfs40_setup_sequence(struct nfs4_slot_table *tbl,
524 struct nfs4_sequence_args *args,
525 struct nfs4_sequence_res *res,
526 struct rpc_task *task)
528 struct nfs4_slot *slot;
530 /* slot already allocated? */
531 if (res->sr_slot != NULL)
534 spin_lock(&tbl->slot_tbl_lock);
535 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
538 slot = nfs4_alloc_slot(tbl);
540 if (slot == ERR_PTR(-ENOMEM))
541 task->tk_timeout = HZ >> 2;
544 spin_unlock(&tbl->slot_tbl_lock);
546 args->sa_slot = slot;
550 rpc_call_start(task);
554 if (args->sa_privileged)
555 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
556 NULL, RPC_PRIORITY_PRIVILEGED);
558 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
559 spin_unlock(&tbl->slot_tbl_lock);
562 EXPORT_SYMBOL_GPL(nfs40_setup_sequence);
564 static int nfs40_sequence_done(struct rpc_task *task,
565 struct nfs4_sequence_res *res)
567 struct nfs4_slot *slot = res->sr_slot;
568 struct nfs4_slot_table *tbl;
574 spin_lock(&tbl->slot_tbl_lock);
575 if (!nfs41_wake_and_assign_slot(tbl, slot))
576 nfs4_free_slot(tbl, slot);
577 spin_unlock(&tbl->slot_tbl_lock);
584 #if defined(CONFIG_NFS_V4_1)
586 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
588 struct nfs4_session *session;
589 struct nfs4_slot_table *tbl;
590 struct nfs4_slot *slot = res->sr_slot;
591 bool send_new_highest_used_slotid = false;
594 session = tbl->session;
596 spin_lock(&tbl->slot_tbl_lock);
597 /* Be nice to the server: try to ensure that the last transmitted
598 * value for highest_user_slotid <= target_highest_slotid
600 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
601 send_new_highest_used_slotid = true;
603 if (nfs41_wake_and_assign_slot(tbl, slot)) {
604 send_new_highest_used_slotid = false;
607 nfs4_free_slot(tbl, slot);
609 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
610 send_new_highest_used_slotid = false;
612 spin_unlock(&tbl->slot_tbl_lock);
614 if (send_new_highest_used_slotid)
615 nfs41_notify_server(session->clp);
618 int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
620 struct nfs4_session *session;
621 struct nfs4_slot *slot = res->sr_slot;
622 struct nfs_client *clp;
623 bool interrupted = false;
628 /* don't increment the sequence number if the task wasn't sent */
629 if (!RPC_WAS_SENT(task))
632 session = slot->table->session;
634 if (slot->interrupted) {
635 slot->interrupted = 0;
639 trace_nfs4_sequence_done(session, res);
640 /* Check the SEQUENCE operation status */
641 switch (res->sr_status) {
643 /* Update the slot's sequence and clientid lease timer */
646 do_renew_lease(clp, res->sr_timestamp);
647 /* Check sequence flags */
648 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
649 nfs41_update_target_slotid(slot->table, slot, res);
653 * sr_status remains 1 if an RPC level error occurred.
654 * The server may or may not have processed the sequence
656 * Mark the slot as having hosted an interrupted RPC call.
658 slot->interrupted = 1;
661 /* The server detected a resend of the RPC call and
662 * returned NFS4ERR_DELAY as per Section 2.10.6.2
665 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
670 case -NFS4ERR_BADSLOT:
672 * The slot id we used was probably retired. Try again
673 * using a different slot id.
676 case -NFS4ERR_SEQ_MISORDERED:
678 * Was the last operation on this sequence interrupted?
679 * If so, retry after bumping the sequence number.
686 * Could this slot have been previously retired?
687 * If so, then the server may be expecting seq_nr = 1!
689 if (slot->seq_nr != 1) {
694 case -NFS4ERR_SEQ_FALSE_RETRY:
698 /* Just update the slot sequence no. */
702 /* The session may be reset by one of the error handlers. */
703 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
704 nfs41_sequence_free_slot(res);
708 if (rpc_restart_call_prepare(task)) {
714 if (!rpc_restart_call(task))
716 rpc_delay(task, NFS4_POLL_RETRY_MAX);
719 EXPORT_SYMBOL_GPL(nfs41_sequence_done);
721 int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
723 if (res->sr_slot == NULL)
725 if (!res->sr_slot->table->session)
726 return nfs40_sequence_done(task, res);
727 return nfs41_sequence_done(task, res);
729 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
731 int nfs41_setup_sequence(struct nfs4_session *session,
732 struct nfs4_sequence_args *args,
733 struct nfs4_sequence_res *res,
734 struct rpc_task *task)
736 struct nfs4_slot *slot;
737 struct nfs4_slot_table *tbl;
739 dprintk("--> %s\n", __func__);
740 /* slot already allocated? */
741 if (res->sr_slot != NULL)
744 tbl = &session->fc_slot_table;
746 task->tk_timeout = 0;
748 spin_lock(&tbl->slot_tbl_lock);
749 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
750 !args->sa_privileged) {
751 /* The state manager will wait until the slot table is empty */
752 dprintk("%s session is draining\n", __func__);
756 slot = nfs4_alloc_slot(tbl);
758 /* If out of memory, try again in 1/4 second */
759 if (slot == ERR_PTR(-ENOMEM))
760 task->tk_timeout = HZ >> 2;
761 dprintk("<-- %s: no free slots\n", __func__);
764 spin_unlock(&tbl->slot_tbl_lock);
766 args->sa_slot = slot;
768 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
769 slot->slot_nr, slot->seq_nr);
772 res->sr_timestamp = jiffies;
773 res->sr_status_flags = 0;
775 * sr_status is only set in decode_sequence, and so will remain
776 * set to 1 if an rpc level failure occurs.
779 trace_nfs4_setup_sequence(session, args);
781 rpc_call_start(task);
784 /* Privileged tasks are queued with top priority */
785 if (args->sa_privileged)
786 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
787 NULL, RPC_PRIORITY_PRIVILEGED);
789 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
790 spin_unlock(&tbl->slot_tbl_lock);
793 EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
795 static int nfs4_setup_sequence(const struct nfs_server *server,
796 struct nfs4_sequence_args *args,
797 struct nfs4_sequence_res *res,
798 struct rpc_task *task)
800 struct nfs4_session *session = nfs4_get_session(server);
804 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
807 dprintk("--> %s clp %p session %p sr_slot %u\n",
808 __func__, session->clp, session, res->sr_slot ?
809 res->sr_slot->slot_nr : NFS4_NO_SLOT);
811 ret = nfs41_setup_sequence(session, args, res, task);
813 dprintk("<-- %s status=%d\n", __func__, ret);
817 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
819 struct nfs4_call_sync_data *data = calldata;
820 struct nfs4_session *session = nfs4_get_session(data->seq_server);
822 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
824 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
827 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
829 struct nfs4_call_sync_data *data = calldata;
831 nfs41_sequence_done(task, data->seq_res);
834 static const struct rpc_call_ops nfs41_call_sync_ops = {
835 .rpc_call_prepare = nfs41_call_sync_prepare,
836 .rpc_call_done = nfs41_call_sync_done,
839 #else /* !CONFIG_NFS_V4_1 */
841 static int nfs4_setup_sequence(const struct nfs_server *server,
842 struct nfs4_sequence_args *args,
843 struct nfs4_sequence_res *res,
844 struct rpc_task *task)
846 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
850 int nfs4_sequence_done(struct rpc_task *task,
851 struct nfs4_sequence_res *res)
853 return nfs40_sequence_done(task, res);
855 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
857 #endif /* !CONFIG_NFS_V4_1 */
859 static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
861 struct nfs4_call_sync_data *data = calldata;
862 nfs4_setup_sequence(data->seq_server,
863 data->seq_args, data->seq_res, task);
866 static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
868 struct nfs4_call_sync_data *data = calldata;
869 nfs4_sequence_done(task, data->seq_res);
872 static const struct rpc_call_ops nfs40_call_sync_ops = {
873 .rpc_call_prepare = nfs40_call_sync_prepare,
874 .rpc_call_done = nfs40_call_sync_done,
877 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
878 struct nfs_server *server,
879 struct rpc_message *msg,
880 struct nfs4_sequence_args *args,
881 struct nfs4_sequence_res *res)
884 struct rpc_task *task;
885 struct nfs_client *clp = server->nfs_client;
886 struct nfs4_call_sync_data data = {
887 .seq_server = server,
891 struct rpc_task_setup task_setup = {
894 .callback_ops = clp->cl_mvops->call_sync_ops,
895 .callback_data = &data
898 task = rpc_run_task(&task_setup);
902 ret = task->tk_status;
908 int nfs4_call_sync(struct rpc_clnt *clnt,
909 struct nfs_server *server,
910 struct rpc_message *msg,
911 struct nfs4_sequence_args *args,
912 struct nfs4_sequence_res *res,
915 nfs4_init_sequence(args, res, cache_reply);
916 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
919 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
921 struct nfs_inode *nfsi = NFS_I(dir);
923 spin_lock(&dir->i_lock);
924 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
925 if (!cinfo->atomic || cinfo->before != dir->i_version)
926 nfs_force_lookup_revalidate(dir);
927 dir->i_version = cinfo->after;
928 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
929 nfs_fscache_invalidate(dir);
930 spin_unlock(&dir->i_lock);
933 struct nfs4_opendata {
935 struct nfs_openargs o_arg;
936 struct nfs_openres o_res;
937 struct nfs_open_confirmargs c_arg;
938 struct nfs_open_confirmres c_res;
939 struct nfs4_string owner_name;
940 struct nfs4_string group_name;
941 struct nfs4_label *a_label;
942 struct nfs_fattr f_attr;
943 struct nfs4_label *f_label;
945 struct dentry *dentry;
946 struct nfs4_state_owner *owner;
947 struct nfs4_state *state;
949 unsigned long timestamp;
950 unsigned int rpc_done : 1;
951 unsigned int file_created : 1;
952 unsigned int is_recover : 1;
957 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
958 int err, struct nfs4_exception *exception)
962 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
964 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
965 exception->retry = 1;
970 nfs4_map_atomic_open_share(struct nfs_server *server,
971 fmode_t fmode, int openflags)
975 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
977 res = NFS4_SHARE_ACCESS_READ;
980 res = NFS4_SHARE_ACCESS_WRITE;
982 case FMODE_READ|FMODE_WRITE:
983 res = NFS4_SHARE_ACCESS_BOTH;
985 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
987 /* Want no delegation if we're using O_DIRECT */
988 if (openflags & O_DIRECT)
989 res |= NFS4_SHARE_WANT_NO_DELEG;
994 static enum open_claim_type4
995 nfs4_map_atomic_open_claim(struct nfs_server *server,
996 enum open_claim_type4 claim)
998 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1003 case NFS4_OPEN_CLAIM_FH:
1004 return NFS4_OPEN_CLAIM_NULL;
1005 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1006 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1007 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1008 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1012 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
1014 p->o_res.f_attr = &p->f_attr;
1015 p->o_res.f_label = p->f_label;
1016 p->o_res.seqid = p->o_arg.seqid;
1017 p->c_res.seqid = p->c_arg.seqid;
1018 p->o_res.server = p->o_arg.server;
1019 p->o_res.access_request = p->o_arg.access;
1020 nfs_fattr_init(&p->f_attr);
1021 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
1024 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
1025 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
1026 const struct iattr *attrs,
1027 struct nfs4_label *label,
1028 enum open_claim_type4 claim,
1031 struct dentry *parent = dget_parent(dentry);
1032 struct inode *dir = d_inode(parent);
1033 struct nfs_server *server = NFS_SERVER(dir);
1034 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
1035 struct nfs4_opendata *p;
1037 p = kzalloc(sizeof(*p), gfp_mask);
1041 p->f_label = nfs4_label_alloc(server, gfp_mask);
1042 if (IS_ERR(p->f_label))
1045 p->a_label = nfs4_label_alloc(server, gfp_mask);
1046 if (IS_ERR(p->a_label))
1049 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1050 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
1051 if (IS_ERR(p->o_arg.seqid))
1052 goto err_free_label;
1053 nfs_sb_active(dentry->d_sb);
1054 p->dentry = dget(dentry);
1057 atomic_inc(&sp->so_count);
1058 p->o_arg.open_flags = flags;
1059 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
1060 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1062 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1063 * will return permission denied for all bits until close */
1064 if (!(flags & O_EXCL)) {
1065 /* ask server to check for all possible rights as results
1067 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1068 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
1070 p->o_arg.clientid = server->nfs_client->cl_clientid;
1071 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1072 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1073 p->o_arg.name = &dentry->d_name;
1074 p->o_arg.server = server;
1075 p->o_arg.bitmask = nfs4_bitmask(server, label);
1076 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1077 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1078 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1079 switch (p->o_arg.claim) {
1080 case NFS4_OPEN_CLAIM_NULL:
1081 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1082 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1083 p->o_arg.fh = NFS_FH(dir);
1085 case NFS4_OPEN_CLAIM_PREVIOUS:
1086 case NFS4_OPEN_CLAIM_FH:
1087 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1088 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1089 p->o_arg.fh = NFS_FH(d_inode(dentry));
1091 if (attrs != NULL && attrs->ia_valid != 0) {
1094 p->o_arg.u.attrs = &p->attrs;
1095 memcpy(&p->attrs, attrs, sizeof(p->attrs));
1098 verf[1] = current->pid;
1099 memcpy(p->o_arg.u.verifier.data, verf,
1100 sizeof(p->o_arg.u.verifier.data));
1102 p->c_arg.fh = &p->o_res.fh;
1103 p->c_arg.stateid = &p->o_res.stateid;
1104 p->c_arg.seqid = p->o_arg.seqid;
1105 nfs4_init_opendata_res(p);
1106 kref_init(&p->kref);
1110 nfs4_label_free(p->a_label);
1112 nfs4_label_free(p->f_label);
1120 static void nfs4_opendata_free(struct kref *kref)
1122 struct nfs4_opendata *p = container_of(kref,
1123 struct nfs4_opendata, kref);
1124 struct super_block *sb = p->dentry->d_sb;
1126 nfs_free_seqid(p->o_arg.seqid);
1127 if (p->state != NULL)
1128 nfs4_put_open_state(p->state);
1129 nfs4_put_state_owner(p->owner);
1131 nfs4_label_free(p->a_label);
1132 nfs4_label_free(p->f_label);
1136 nfs_sb_deactive(sb);
1137 nfs_fattr_free_names(&p->f_attr);
1138 kfree(p->f_attr.mdsthreshold);
1142 static void nfs4_opendata_put(struct nfs4_opendata *p)
1145 kref_put(&p->kref, nfs4_opendata_free);
1148 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1152 ret = rpc_wait_for_completion_task(task);
1156 static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1159 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1160 case FMODE_READ|FMODE_WRITE:
1161 return state->n_rdwr != 0;
1163 return state->n_wronly != 0;
1165 return state->n_rdonly != 0;
1171 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
1175 if (open_mode & (O_EXCL|O_TRUNC))
1177 switch (mode & (FMODE_READ|FMODE_WRITE)) {
1179 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1180 && state->n_rdonly != 0;
1183 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1184 && state->n_wronly != 0;
1186 case FMODE_READ|FMODE_WRITE:
1187 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1188 && state->n_rdwr != 0;
1194 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1195 enum open_claim_type4 claim)
1197 if (delegation == NULL)
1199 if ((delegation->type & fmode) != fmode)
1201 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1204 case NFS4_OPEN_CLAIM_NULL:
1205 case NFS4_OPEN_CLAIM_FH:
1207 case NFS4_OPEN_CLAIM_PREVIOUS:
1208 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1213 nfs_mark_delegation_referenced(delegation);
1217 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1226 case FMODE_READ|FMODE_WRITE:
1229 nfs4_state_set_mode_locked(state, state->state | fmode);
1232 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
1234 struct nfs_client *clp = state->owner->so_server->nfs_client;
1235 bool need_recover = false;
1237 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1238 need_recover = true;
1239 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1240 need_recover = true;
1241 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1242 need_recover = true;
1244 nfs4_state_mark_reclaim_nograce(clp, state);
1247 static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1248 nfs4_stateid *stateid)
1250 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1252 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1253 nfs_test_and_clear_all_open_stateid(state);
1256 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1261 static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1263 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1265 if (state->n_wronly)
1266 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1267 if (state->n_rdonly)
1268 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1270 set_bit(NFS_O_RDWR_STATE, &state->flags);
1271 set_bit(NFS_OPEN_STATE, &state->flags);
1274 static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1275 nfs4_stateid *arg_stateid,
1276 nfs4_stateid *stateid, fmode_t fmode)
1278 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1279 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1281 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1284 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1287 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1288 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1289 clear_bit(NFS_OPEN_STATE, &state->flags);
1291 if (stateid == NULL)
1293 /* Handle races with OPEN */
1294 if (!nfs4_stateid_match_other(arg_stateid, &state->open_stateid) ||
1295 (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1296 !nfs4_stateid_is_newer(stateid, &state->open_stateid))) {
1297 nfs_resync_open_stateid_locked(state);
1300 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1301 nfs4_stateid_copy(&state->stateid, stateid);
1302 nfs4_stateid_copy(&state->open_stateid, stateid);
1305 static void nfs_clear_open_stateid(struct nfs4_state *state,
1306 nfs4_stateid *arg_stateid,
1307 nfs4_stateid *stateid, fmode_t fmode)
1309 write_seqlock(&state->seqlock);
1310 nfs_clear_open_stateid_locked(state, arg_stateid, stateid, fmode);
1311 write_sequnlock(&state->seqlock);
1312 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1313 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1316 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1320 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1323 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1325 case FMODE_READ|FMODE_WRITE:
1326 set_bit(NFS_O_RDWR_STATE, &state->flags);
1328 if (!nfs_need_update_open_stateid(state, stateid))
1330 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1331 nfs4_stateid_copy(&state->stateid, stateid);
1332 nfs4_stateid_copy(&state->open_stateid, stateid);
1335 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
1338 * Protect the call to nfs4_state_set_mode_locked and
1339 * serialise the stateid update
1341 write_seqlock(&state->seqlock);
1342 if (deleg_stateid != NULL) {
1343 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1344 set_bit(NFS_DELEGATED_STATE, &state->flags);
1346 if (open_stateid != NULL)
1347 nfs_set_open_stateid_locked(state, open_stateid, fmode);
1348 write_sequnlock(&state->seqlock);
1349 spin_lock(&state->owner->so_lock);
1350 update_open_stateflags(state, fmode);
1351 spin_unlock(&state->owner->so_lock);
1354 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
1356 struct nfs_inode *nfsi = NFS_I(state->inode);
1357 struct nfs_delegation *deleg_cur;
1360 fmode &= (FMODE_READ|FMODE_WRITE);
1363 deleg_cur = rcu_dereference(nfsi->delegation);
1364 if (deleg_cur == NULL)
1367 spin_lock(&deleg_cur->lock);
1368 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1369 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1370 (deleg_cur->type & fmode) != fmode)
1371 goto no_delegation_unlock;
1373 if (delegation == NULL)
1374 delegation = &deleg_cur->stateid;
1375 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
1376 goto no_delegation_unlock;
1378 nfs_mark_delegation_referenced(deleg_cur);
1379 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
1381 no_delegation_unlock:
1382 spin_unlock(&deleg_cur->lock);
1386 if (!ret && open_stateid != NULL) {
1387 __update_open_stateid(state, open_stateid, NULL, fmode);
1390 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1391 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1396 static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1397 const nfs4_stateid *stateid)
1399 struct nfs4_state *state = lsp->ls_state;
1402 spin_lock(&state->state_lock);
1403 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1405 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1407 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1410 spin_unlock(&state->state_lock);
1414 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1416 struct nfs_delegation *delegation;
1419 delegation = rcu_dereference(NFS_I(inode)->delegation);
1420 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1425 nfs4_inode_return_delegation(inode);
1428 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1430 struct nfs4_state *state = opendata->state;
1431 struct nfs_inode *nfsi = NFS_I(state->inode);
1432 struct nfs_delegation *delegation;
1433 int open_mode = opendata->o_arg.open_flags;
1434 fmode_t fmode = opendata->o_arg.fmode;
1435 enum open_claim_type4 claim = opendata->o_arg.claim;
1436 nfs4_stateid stateid;
1440 spin_lock(&state->owner->so_lock);
1441 if (can_open_cached(state, fmode, open_mode)) {
1442 update_open_stateflags(state, fmode);
1443 spin_unlock(&state->owner->so_lock);
1444 goto out_return_state;
1446 spin_unlock(&state->owner->so_lock);
1448 delegation = rcu_dereference(nfsi->delegation);
1449 if (!can_open_delegated(delegation, fmode, claim)) {
1453 /* Save the delegation */
1454 nfs4_stateid_copy(&stateid, &delegation->stateid);
1456 nfs_release_seqid(opendata->o_arg.seqid);
1457 if (!opendata->is_recover) {
1458 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1464 /* Try to update the stateid using the delegation */
1465 if (update_open_stateid(state, NULL, &stateid, fmode))
1466 goto out_return_state;
1469 return ERR_PTR(ret);
1471 atomic_inc(&state->count);
1476 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1478 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1479 struct nfs_delegation *delegation;
1480 int delegation_flags = 0;
1483 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1485 delegation_flags = delegation->flags;
1487 switch (data->o_arg.claim) {
1490 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1491 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1492 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1493 "returning a delegation for "
1494 "OPEN(CLAIM_DELEGATE_CUR)\n",
1498 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1499 nfs_inode_set_delegation(state->inode,
1500 data->owner->so_cred,
1503 nfs_inode_reclaim_delegation(state->inode,
1504 data->owner->so_cred,
1509 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1510 * and update the nfs4_state.
1512 static struct nfs4_state *
1513 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1515 struct inode *inode = data->state->inode;
1516 struct nfs4_state *state = data->state;
1519 if (!data->rpc_done) {
1520 if (data->rpc_status) {
1521 ret = data->rpc_status;
1524 /* cached opens have already been processed */
1528 ret = nfs_refresh_inode(inode, &data->f_attr);
1532 if (data->o_res.delegation_type != 0)
1533 nfs4_opendata_check_deleg(data, state);
1535 update_open_stateid(state, &data->o_res.stateid, NULL,
1537 atomic_inc(&state->count);
1541 return ERR_PTR(ret);
1545 static struct nfs4_state *
1546 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1548 struct inode *inode;
1549 struct nfs4_state *state = NULL;
1552 if (!data->rpc_done) {
1553 state = nfs4_try_open_cached(data);
1558 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1560 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
1561 ret = PTR_ERR(inode);
1565 state = nfs4_get_open_state(inode, data->owner);
1568 if (data->o_res.delegation_type != 0)
1569 nfs4_opendata_check_deleg(data, state);
1570 update_open_stateid(state, &data->o_res.stateid, NULL,
1574 nfs_release_seqid(data->o_arg.seqid);
1579 return ERR_PTR(ret);
1582 static struct nfs4_state *
1583 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1585 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1586 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1587 return _nfs4_opendata_to_nfs4_state(data);
1590 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1592 struct nfs_inode *nfsi = NFS_I(state->inode);
1593 struct nfs_open_context *ctx;
1595 spin_lock(&state->inode->i_lock);
1596 list_for_each_entry(ctx, &nfsi->open_files, list) {
1597 if (ctx->state != state)
1599 get_nfs_open_context(ctx);
1600 spin_unlock(&state->inode->i_lock);
1603 spin_unlock(&state->inode->i_lock);
1604 return ERR_PTR(-ENOENT);
1607 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1608 struct nfs4_state *state, enum open_claim_type4 claim)
1610 struct nfs4_opendata *opendata;
1612 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
1613 NULL, NULL, claim, GFP_NOFS);
1614 if (opendata == NULL)
1615 return ERR_PTR(-ENOMEM);
1616 opendata->state = state;
1617 atomic_inc(&state->count);
1621 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1624 struct nfs4_state *newstate;
1627 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
1629 opendata->o_arg.open_flags = 0;
1630 opendata->o_arg.fmode = fmode;
1631 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1632 NFS_SB(opendata->dentry->d_sb),
1634 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1635 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1636 nfs4_init_opendata_res(opendata);
1637 ret = _nfs4_recover_proc_open(opendata);
1640 newstate = nfs4_opendata_to_nfs4_state(opendata);
1641 if (IS_ERR(newstate))
1642 return PTR_ERR(newstate);
1643 if (newstate != opendata->state)
1645 nfs4_close_state(newstate, fmode);
1649 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1653 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1654 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1655 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1656 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1657 /* memory barrier prior to reading state->n_* */
1658 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1659 clear_bit(NFS_OPEN_STATE, &state->flags);
1661 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1664 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1667 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1671 * We may have performed cached opens for all three recoveries.
1672 * Check if we need to update the current stateid.
1674 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1675 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
1676 write_seqlock(&state->seqlock);
1677 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1678 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1679 write_sequnlock(&state->seqlock);
1686 * reclaim state on the server after a reboot.
1688 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1690 struct nfs_delegation *delegation;
1691 struct nfs4_opendata *opendata;
1692 fmode_t delegation_type = 0;
1695 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1696 NFS4_OPEN_CLAIM_PREVIOUS);
1697 if (IS_ERR(opendata))
1698 return PTR_ERR(opendata);
1700 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1701 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1702 delegation_type = delegation->type;
1704 opendata->o_arg.u.delegation_type = delegation_type;
1705 status = nfs4_open_recover(opendata, state);
1706 nfs4_opendata_put(opendata);
1710 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1712 struct nfs_server *server = NFS_SERVER(state->inode);
1713 struct nfs4_exception exception = { };
1716 err = _nfs4_do_open_reclaim(ctx, state);
1717 trace_nfs4_open_reclaim(ctx, 0, err);
1718 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1720 if (err != -NFS4ERR_DELAY)
1722 nfs4_handle_exception(server, err, &exception);
1723 } while (exception.retry);
1727 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1729 struct nfs_open_context *ctx;
1732 ctx = nfs4_state_find_open_context(state);
1735 ret = nfs4_do_open_reclaim(ctx, state);
1736 put_nfs_open_context(ctx);
1740 static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, int err)
1744 printk(KERN_ERR "NFS: %s: unhandled error "
1745 "%d.\n", __func__, err);
1751 case -NFS4ERR_BADSESSION:
1752 case -NFS4ERR_BADSLOT:
1753 case -NFS4ERR_BAD_HIGH_SLOT:
1754 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1755 case -NFS4ERR_DEADSESSION:
1756 set_bit(NFS_DELEGATED_STATE, &state->flags);
1757 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1759 case -NFS4ERR_STALE_CLIENTID:
1760 case -NFS4ERR_STALE_STATEID:
1761 set_bit(NFS_DELEGATED_STATE, &state->flags);
1762 case -NFS4ERR_EXPIRED:
1763 /* Don't recall a delegation if it was lost */
1764 nfs4_schedule_lease_recovery(server->nfs_client);
1766 case -NFS4ERR_MOVED:
1767 nfs4_schedule_migration_recovery(server);
1769 case -NFS4ERR_LEASE_MOVED:
1770 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1772 case -NFS4ERR_DELEG_REVOKED:
1773 case -NFS4ERR_ADMIN_REVOKED:
1774 case -NFS4ERR_BAD_STATEID:
1775 case -NFS4ERR_OPENMODE:
1776 nfs_inode_find_state_and_recover(state->inode,
1778 nfs4_schedule_stateid_recovery(server, state);
1780 case -NFS4ERR_DELAY:
1781 case -NFS4ERR_GRACE:
1782 set_bit(NFS_DELEGATED_STATE, &state->flags);
1786 case -NFS4ERR_DENIED:
1787 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1793 int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
1794 struct nfs4_state *state, const nfs4_stateid *stateid,
1797 struct nfs_server *server = NFS_SERVER(state->inode);
1798 struct nfs4_opendata *opendata;
1801 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1802 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1803 if (IS_ERR(opendata))
1804 return PTR_ERR(opendata);
1805 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1806 write_seqlock(&state->seqlock);
1807 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1808 write_sequnlock(&state->seqlock);
1809 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1810 switch (type & (FMODE_READ|FMODE_WRITE)) {
1811 case FMODE_READ|FMODE_WRITE:
1813 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1816 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1820 err = nfs4_open_recover_helper(opendata, FMODE_READ);
1822 nfs4_opendata_put(opendata);
1823 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1826 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1828 struct nfs4_opendata *data = calldata;
1830 nfs40_setup_sequence(data->o_arg.server->nfs_client->cl_slot_tbl,
1831 &data->c_arg.seq_args, &data->c_res.seq_res, task);
1834 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1836 struct nfs4_opendata *data = calldata;
1838 nfs40_sequence_done(task, &data->c_res.seq_res);
1840 data->rpc_status = task->tk_status;
1841 if (data->rpc_status == 0) {
1842 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
1843 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1844 renew_lease(data->o_res.server, data->timestamp);
1849 static void nfs4_open_confirm_release(void *calldata)
1851 struct nfs4_opendata *data = calldata;
1852 struct nfs4_state *state = NULL;
1854 /* If this request hasn't been cancelled, do nothing */
1855 if (data->cancelled == 0)
1857 /* In case of error, no cleanup! */
1858 if (!data->rpc_done)
1860 state = nfs4_opendata_to_nfs4_state(data);
1862 nfs4_close_state(state, data->o_arg.fmode);
1864 nfs4_opendata_put(data);
1867 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1868 .rpc_call_prepare = nfs4_open_confirm_prepare,
1869 .rpc_call_done = nfs4_open_confirm_done,
1870 .rpc_release = nfs4_open_confirm_release,
1874 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1876 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1878 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
1879 struct rpc_task *task;
1880 struct rpc_message msg = {
1881 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1882 .rpc_argp = &data->c_arg,
1883 .rpc_resp = &data->c_res,
1884 .rpc_cred = data->owner->so_cred,
1886 struct rpc_task_setup task_setup_data = {
1887 .rpc_client = server->client,
1888 .rpc_message = &msg,
1889 .callback_ops = &nfs4_open_confirm_ops,
1890 .callback_data = data,
1891 .workqueue = nfsiod_workqueue,
1892 .flags = RPC_TASK_ASYNC,
1896 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
1897 kref_get(&data->kref);
1899 data->rpc_status = 0;
1900 data->timestamp = jiffies;
1901 if (data->is_recover)
1902 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
1903 task = rpc_run_task(&task_setup_data);
1905 return PTR_ERR(task);
1906 status = nfs4_wait_for_completion_rpc_task(task);
1908 data->cancelled = 1;
1911 status = data->rpc_status;
1916 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1918 struct nfs4_opendata *data = calldata;
1919 struct nfs4_state_owner *sp = data->owner;
1920 struct nfs_client *clp = sp->so_server->nfs_client;
1921 enum open_claim_type4 claim = data->o_arg.claim;
1923 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1926 * Check if we still need to send an OPEN call, or if we can use
1927 * a delegation instead.
1929 if (data->state != NULL) {
1930 struct nfs_delegation *delegation;
1932 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1935 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1936 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
1937 goto unlock_no_action;
1940 /* Update client id. */
1941 data->o_arg.clientid = clp->cl_clientid;
1945 case NFS4_OPEN_CLAIM_PREVIOUS:
1946 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1947 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1948 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
1949 case NFS4_OPEN_CLAIM_FH:
1950 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1951 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1953 data->timestamp = jiffies;
1954 if (nfs4_setup_sequence(data->o_arg.server,
1955 &data->o_arg.seq_args,
1956 &data->o_res.seq_res,
1958 nfs_release_seqid(data->o_arg.seqid);
1960 /* Set the create mode (note dependency on the session type) */
1961 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1962 if (data->o_arg.open_flags & O_EXCL) {
1963 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1964 if (nfs4_has_persistent_session(clp))
1965 data->o_arg.createmode = NFS4_CREATE_GUARDED;
1966 else if (clp->cl_mvops->minor_version > 0)
1967 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1973 task->tk_action = NULL;
1975 nfs4_sequence_done(task, &data->o_res.seq_res);
1978 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1980 struct nfs4_opendata *data = calldata;
1982 data->rpc_status = task->tk_status;
1984 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1987 if (task->tk_status == 0) {
1988 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1989 switch (data->o_res.f_attr->mode & S_IFMT) {
1993 data->rpc_status = -ELOOP;
1996 data->rpc_status = -EISDIR;
1999 data->rpc_status = -ENOTDIR;
2002 renew_lease(data->o_res.server, data->timestamp);
2003 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2004 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2009 static void nfs4_open_release(void *calldata)
2011 struct nfs4_opendata *data = calldata;
2012 struct nfs4_state *state = NULL;
2014 /* If this request hasn't been cancelled, do nothing */
2015 if (data->cancelled == 0)
2017 /* In case of error, no cleanup! */
2018 if (data->rpc_status != 0 || !data->rpc_done)
2020 /* In case we need an open_confirm, no cleanup! */
2021 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2023 state = nfs4_opendata_to_nfs4_state(data);
2025 nfs4_close_state(state, data->o_arg.fmode);
2027 nfs4_opendata_put(data);
2030 static const struct rpc_call_ops nfs4_open_ops = {
2031 .rpc_call_prepare = nfs4_open_prepare,
2032 .rpc_call_done = nfs4_open_done,
2033 .rpc_release = nfs4_open_release,
2036 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
2038 struct inode *dir = d_inode(data->dir);
2039 struct nfs_server *server = NFS_SERVER(dir);
2040 struct nfs_openargs *o_arg = &data->o_arg;
2041 struct nfs_openres *o_res = &data->o_res;
2042 struct rpc_task *task;
2043 struct rpc_message msg = {
2044 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2047 .rpc_cred = data->owner->so_cred,
2049 struct rpc_task_setup task_setup_data = {
2050 .rpc_client = server->client,
2051 .rpc_message = &msg,
2052 .callback_ops = &nfs4_open_ops,
2053 .callback_data = data,
2054 .workqueue = nfsiod_workqueue,
2055 .flags = RPC_TASK_ASYNC,
2059 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
2060 kref_get(&data->kref);
2062 data->rpc_status = 0;
2063 data->cancelled = 0;
2064 data->is_recover = 0;
2066 nfs4_set_sequence_privileged(&o_arg->seq_args);
2067 data->is_recover = 1;
2069 task = rpc_run_task(&task_setup_data);
2071 return PTR_ERR(task);
2072 status = nfs4_wait_for_completion_rpc_task(task);
2074 data->cancelled = 1;
2077 status = data->rpc_status;
2083 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2085 struct inode *dir = d_inode(data->dir);
2086 struct nfs_openres *o_res = &data->o_res;
2089 status = nfs4_run_open_task(data, 1);
2090 if (status != 0 || !data->rpc_done)
2093 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2095 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2096 status = _nfs4_proc_open_confirm(data);
2105 * Additional permission checks in order to distinguish between an
2106 * open for read, and an open for execute. This works around the
2107 * fact that NFSv4 OPEN treats read and execute permissions as being
2109 * Note that in the non-execute case, we want to turn off permission
2110 * checking if we just created a new file (POSIX open() semantics).
2112 static int nfs4_opendata_access(struct rpc_cred *cred,
2113 struct nfs4_opendata *opendata,
2114 struct nfs4_state *state, fmode_t fmode,
2117 struct nfs_access_entry cache;
2120 /* access call failed or for some reason the server doesn't
2121 * support any access modes -- defer access call until later */
2122 if (opendata->o_res.access_supported == 0)
2127 * Use openflags to check for exec, because fmode won't
2128 * always have FMODE_EXEC set when file open for exec.
2130 if (openflags & __FMODE_EXEC) {
2131 /* ONLY check for exec rights */
2133 } else if ((fmode & FMODE_READ) && !opendata->file_created)
2137 cache.jiffies = jiffies;
2138 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2139 nfs_access_add_cache(state->inode, &cache);
2141 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
2144 /* even though OPEN succeeded, access is denied. Close the file */
2145 nfs4_close_state(state, fmode);
2150 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2152 static int _nfs4_proc_open(struct nfs4_opendata *data)
2154 struct inode *dir = d_inode(data->dir);
2155 struct nfs_server *server = NFS_SERVER(dir);
2156 struct nfs_openargs *o_arg = &data->o_arg;
2157 struct nfs_openres *o_res = &data->o_res;
2160 status = nfs4_run_open_task(data, 0);
2161 if (!data->rpc_done)
2164 if (status == -NFS4ERR_BADNAME &&
2165 !(o_arg->open_flags & O_CREAT))
2170 nfs_fattr_map_and_free_names(server, &data->f_attr);
2172 if (o_arg->open_flags & O_CREAT) {
2173 update_changeattr(dir, &o_res->cinfo);
2174 if (o_arg->open_flags & O_EXCL)
2175 data->file_created = 1;
2176 else if (o_res->cinfo.before != o_res->cinfo.after)
2177 data->file_created = 1;
2179 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2180 server->caps &= ~NFS_CAP_POSIX_LOCK;
2181 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2182 status = _nfs4_proc_open_confirm(data);
2186 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
2187 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
2191 static int nfs4_recover_expired_lease(struct nfs_server *server)
2193 return nfs4_client_recover_expired_lease(server->nfs_client);
2198 * reclaim state on the server after a network partition.
2199 * Assumes caller holds the appropriate lock
2201 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2203 struct nfs4_opendata *opendata;
2206 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2207 NFS4_OPEN_CLAIM_FH);
2208 if (IS_ERR(opendata))
2209 return PTR_ERR(opendata);
2210 ret = nfs4_open_recover(opendata, state);
2212 d_drop(ctx->dentry);
2213 nfs4_opendata_put(opendata);
2217 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2219 struct nfs_server *server = NFS_SERVER(state->inode);
2220 struct nfs4_exception exception = { };
2224 err = _nfs4_open_expired(ctx, state);
2225 trace_nfs4_open_expired(ctx, 0, err);
2226 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2231 case -NFS4ERR_GRACE:
2232 case -NFS4ERR_DELAY:
2233 nfs4_handle_exception(server, err, &exception);
2236 } while (exception.retry);
2241 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2243 struct nfs_open_context *ctx;
2246 ctx = nfs4_state_find_open_context(state);
2249 ret = nfs4_do_open_expired(ctx, state);
2250 put_nfs_open_context(ctx);
2254 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state)
2256 nfs_remove_bad_delegation(state->inode);
2257 write_seqlock(&state->seqlock);
2258 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2259 write_sequnlock(&state->seqlock);
2260 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2263 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2265 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2266 nfs_finish_clear_delegation_stateid(state);
2269 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2271 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2272 nfs40_clear_delegation_stateid(state);
2273 return nfs4_open_expired(sp, state);
2276 #if defined(CONFIG_NFS_V4_1)
2277 static void nfs41_check_delegation_stateid(struct nfs4_state *state)
2279 struct nfs_server *server = NFS_SERVER(state->inode);
2280 nfs4_stateid stateid;
2281 struct nfs_delegation *delegation;
2282 struct rpc_cred *cred;
2285 /* Get the delegation credential for use by test/free_stateid */
2287 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2288 if (delegation == NULL) {
2293 nfs4_stateid_copy(&stateid, &delegation->stateid);
2294 cred = get_rpccred(delegation->cred);
2296 status = nfs41_test_stateid(server, &stateid, cred);
2297 trace_nfs4_test_delegation_stateid(state, NULL, status);
2299 if (status != NFS_OK) {
2300 /* Free the stateid unless the server explicitly
2301 * informs us the stateid is unrecognized. */
2302 if (status != -NFS4ERR_BAD_STATEID)
2303 nfs41_free_stateid(server, &stateid, cred);
2304 nfs_finish_clear_delegation_stateid(state);
2311 * nfs41_check_open_stateid - possibly free an open stateid
2313 * @state: NFSv4 state for an inode
2315 * Returns NFS_OK if recovery for this stateid is now finished.
2316 * Otherwise a negative NFS4ERR value is returned.
2318 static int nfs41_check_open_stateid(struct nfs4_state *state)
2320 struct nfs_server *server = NFS_SERVER(state->inode);
2321 nfs4_stateid *stateid = &state->open_stateid;
2322 struct rpc_cred *cred = state->owner->so_cred;
2325 /* If a state reset has been done, test_stateid is unneeded */
2326 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2327 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2328 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2329 return -NFS4ERR_BAD_STATEID;
2331 status = nfs41_test_stateid(server, stateid, cred);
2332 trace_nfs4_test_open_stateid(state, NULL, status);
2333 if (status != NFS_OK) {
2334 /* Free the stateid unless the server explicitly
2335 * informs us the stateid is unrecognized. */
2336 if (status != -NFS4ERR_BAD_STATEID)
2337 nfs41_free_stateid(server, stateid, cred);
2339 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2340 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2341 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2342 clear_bit(NFS_OPEN_STATE, &state->flags);
2347 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2351 nfs41_check_delegation_stateid(state);
2352 status = nfs41_check_open_stateid(state);
2353 if (status != NFS_OK)
2354 status = nfs4_open_expired(sp, state);
2360 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2361 * fields corresponding to attributes that were used to store the verifier.
2362 * Make sure we clobber those fields in the later setattr call
2364 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2365 struct iattr *sattr, struct nfs4_label **label)
2367 const u32 *attrset = opendata->o_res.attrset;
2369 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2370 !(sattr->ia_valid & ATTR_ATIME_SET))
2371 sattr->ia_valid |= ATTR_ATIME;
2373 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2374 !(sattr->ia_valid & ATTR_MTIME_SET))
2375 sattr->ia_valid |= ATTR_MTIME;
2377 /* Except MODE, it seems harmless of setting twice. */
2378 if ((attrset[1] & FATTR4_WORD1_MODE))
2379 sattr->ia_valid &= ~ATTR_MODE;
2381 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2385 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2388 struct nfs_open_context *ctx)
2390 struct nfs4_state_owner *sp = opendata->owner;
2391 struct nfs_server *server = sp->so_server;
2392 struct dentry *dentry;
2393 struct nfs4_state *state;
2397 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2399 ret = _nfs4_proc_open(opendata);
2403 state = nfs4_opendata_to_nfs4_state(opendata);
2404 ret = PTR_ERR(state);
2407 if (server->caps & NFS_CAP_POSIX_LOCK)
2408 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2410 dentry = opendata->dentry;
2411 if (d_really_is_negative(dentry)) {
2412 /* FIXME: Is this d_drop() ever needed? */
2414 dentry = d_add_unique(dentry, igrab(state->inode));
2415 if (dentry == NULL) {
2416 dentry = opendata->dentry;
2417 } else if (dentry != ctx->dentry) {
2419 ctx->dentry = dget(dentry);
2421 nfs_set_verifier(dentry,
2422 nfs_save_change_attribute(d_inode(opendata->dir)));
2425 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2430 if (d_inode(dentry) == state->inode) {
2431 nfs_inode_attach_open_context(ctx);
2432 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2433 nfs4_schedule_stateid_recovery(server, state);
2440 * Returns a referenced nfs4_state
2442 static int _nfs4_do_open(struct inode *dir,
2443 struct nfs_open_context *ctx,
2445 struct iattr *sattr,
2446 struct nfs4_label *label,
2449 struct nfs4_state_owner *sp;
2450 struct nfs4_state *state = NULL;
2451 struct nfs_server *server = NFS_SERVER(dir);
2452 struct nfs4_opendata *opendata;
2453 struct dentry *dentry = ctx->dentry;
2454 struct rpc_cred *cred = ctx->cred;
2455 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2456 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
2457 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
2458 struct nfs4_label *olabel = NULL;
2461 /* Protect against reboot recovery conflicts */
2463 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2465 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2468 status = nfs4_recover_expired_lease(server);
2470 goto err_put_state_owner;
2471 if (d_really_is_positive(dentry))
2472 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
2474 if (d_really_is_positive(dentry))
2475 claim = NFS4_OPEN_CLAIM_FH;
2476 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
2477 label, claim, GFP_KERNEL);
2478 if (opendata == NULL)
2479 goto err_put_state_owner;
2482 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2483 if (IS_ERR(olabel)) {
2484 status = PTR_ERR(olabel);
2485 goto err_opendata_put;
2489 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2490 if (!opendata->f_attr.mdsthreshold) {
2491 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2492 if (!opendata->f_attr.mdsthreshold)
2493 goto err_free_label;
2495 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
2497 if (d_really_is_positive(dentry))
2498 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
2500 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
2502 goto err_free_label;
2505 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
2506 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
2507 nfs4_exclusive_attrset(opendata, sattr, &label);
2509 nfs_fattr_init(opendata->o_res.f_attr);
2510 status = nfs4_do_setattr(state->inode, cred,
2511 opendata->o_res.f_attr, sattr,
2512 state, label, olabel);
2514 nfs_setattr_update_inode(state->inode, sattr,
2515 opendata->o_res.f_attr);
2516 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2519 if (opened && opendata->file_created)
2520 *opened |= FILE_CREATED;
2522 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
2523 *ctx_th = opendata->f_attr.mdsthreshold;
2524 opendata->f_attr.mdsthreshold = NULL;
2527 nfs4_label_free(olabel);
2529 nfs4_opendata_put(opendata);
2530 nfs4_put_state_owner(sp);
2533 nfs4_label_free(olabel);
2535 nfs4_opendata_put(opendata);
2536 err_put_state_owner:
2537 nfs4_put_state_owner(sp);
2543 static struct nfs4_state *nfs4_do_open(struct inode *dir,
2544 struct nfs_open_context *ctx,
2546 struct iattr *sattr,
2547 struct nfs4_label *label,
2550 struct nfs_server *server = NFS_SERVER(dir);
2551 struct nfs4_exception exception = { };
2552 struct nfs4_state *res;
2556 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
2558 trace_nfs4_open_file(ctx, flags, status);
2561 /* NOTE: BAD_SEQID means the server and client disagree about the
2562 * book-keeping w.r.t. state-changing operations
2563 * (OPEN/CLOSE/LOCK/LOCKU...)
2564 * It is actually a sign of a bug on the client or on the server.
2566 * If we receive a BAD_SEQID error in the particular case of
2567 * doing an OPEN, we assume that nfs_increment_open_seqid() will
2568 * have unhashed the old state_owner for us, and that we can
2569 * therefore safely retry using a new one. We should still warn
2570 * the user though...
2572 if (status == -NFS4ERR_BAD_SEQID) {
2573 pr_warn_ratelimited("NFS: v4 server %s "
2574 " returned a bad sequence-id error!\n",
2575 NFS_SERVER(dir)->nfs_client->cl_hostname);
2576 exception.retry = 1;
2580 * BAD_STATEID on OPEN means that the server cancelled our
2581 * state before it received the OPEN_CONFIRM.
2582 * Recover by retrying the request as per the discussion
2583 * on Page 181 of RFC3530.
2585 if (status == -NFS4ERR_BAD_STATEID) {
2586 exception.retry = 1;
2589 if (status == -EAGAIN) {
2590 /* We must have found a delegation */
2591 exception.retry = 1;
2594 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2596 res = ERR_PTR(nfs4_handle_exception(server,
2597 status, &exception));
2598 } while (exception.retry);
2602 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2603 struct nfs_fattr *fattr, struct iattr *sattr,
2604 struct nfs4_state *state, struct nfs4_label *ilabel,
2605 struct nfs4_label *olabel)
2607 struct nfs_server *server = NFS_SERVER(inode);
2608 struct nfs_setattrargs arg = {
2609 .fh = NFS_FH(inode),
2612 .bitmask = server->attr_bitmask,
2615 struct nfs_setattrres res = {
2620 struct rpc_message msg = {
2621 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2626 unsigned long timestamp = jiffies;
2631 arg.bitmask = nfs4_bitmask(server, ilabel);
2633 arg.bitmask = nfs4_bitmask(server, olabel);
2635 nfs_fattr_init(fattr);
2637 /* Servers should only apply open mode checks for file size changes */
2638 truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false;
2639 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2641 if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) {
2642 /* Use that stateid */
2643 } else if (truncate && state != NULL) {
2644 struct nfs_lockowner lockowner = {
2645 .l_owner = current->files,
2646 .l_pid = current->tgid,
2648 if (!nfs4_valid_open_stateid(state))
2650 if (nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
2651 &lockowner) == -EIO)
2654 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
2656 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2657 if (status == 0 && state != NULL)
2658 renew_lease(server, timestamp);
2662 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2663 struct nfs_fattr *fattr, struct iattr *sattr,
2664 struct nfs4_state *state, struct nfs4_label *ilabel,
2665 struct nfs4_label *olabel)
2667 struct nfs_server *server = NFS_SERVER(inode);
2668 struct nfs4_exception exception = {
2674 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state, ilabel, olabel);
2675 trace_nfs4_setattr(inode, err);
2677 case -NFS4ERR_OPENMODE:
2678 if (!(sattr->ia_valid & ATTR_SIZE)) {
2679 pr_warn_once("NFSv4: server %s is incorrectly "
2680 "applying open mode checks to "
2681 "a SETATTR that is not "
2682 "changing file size.\n",
2683 server->nfs_client->cl_hostname);
2685 if (state && !(state->state & FMODE_WRITE)) {
2687 if (sattr->ia_valid & ATTR_OPEN)
2692 err = nfs4_handle_exception(server, err, &exception);
2693 } while (exception.retry);
2699 nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
2701 if (inode == NULL || !nfs_have_layout(inode))
2704 return pnfs_wait_on_layoutreturn(inode, task);
2707 struct nfs4_closedata {
2708 struct inode *inode;
2709 struct nfs4_state *state;
2710 struct nfs_closeargs arg;
2711 struct nfs_closeres res;
2712 struct nfs_fattr fattr;
2713 unsigned long timestamp;
2718 static void nfs4_free_closedata(void *data)
2720 struct nfs4_closedata *calldata = data;
2721 struct nfs4_state_owner *sp = calldata->state->owner;
2722 struct super_block *sb = calldata->state->inode->i_sb;
2725 pnfs_roc_release(calldata->state->inode);
2726 nfs4_put_open_state(calldata->state);
2727 nfs_free_seqid(calldata->arg.seqid);
2728 nfs4_put_state_owner(sp);
2729 nfs_sb_deactive(sb);
2733 static void nfs4_close_done(struct rpc_task *task, void *data)
2735 struct nfs4_closedata *calldata = data;
2736 struct nfs4_state *state = calldata->state;
2737 struct nfs_server *server = NFS_SERVER(calldata->inode);
2738 nfs4_stateid *res_stateid = NULL;
2740 dprintk("%s: begin!\n", __func__);
2741 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2743 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
2744 /* hmm. we are done with the inode, and in the process of freeing
2745 * the state_owner. we keep this around to process errors
2747 switch (task->tk_status) {
2749 res_stateid = &calldata->res.stateid;
2751 pnfs_roc_set_barrier(state->inode,
2752 calldata->roc_barrier);
2753 renew_lease(server, calldata->timestamp);
2755 case -NFS4ERR_ADMIN_REVOKED:
2756 case -NFS4ERR_STALE_STATEID:
2757 case -NFS4ERR_OLD_STATEID:
2758 case -NFS4ERR_BAD_STATEID:
2759 case -NFS4ERR_EXPIRED:
2760 if (!nfs4_stateid_match(&calldata->arg.stateid,
2761 &state->open_stateid)) {
2762 rpc_restart_call_prepare(task);
2765 if (calldata->arg.fmode == 0)
2768 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
2769 rpc_restart_call_prepare(task);
2773 nfs_clear_open_stateid(state, &calldata->arg.stateid,
2774 res_stateid, calldata->arg.fmode);
2776 nfs_release_seqid(calldata->arg.seqid);
2777 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
2778 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
2781 static void nfs4_close_prepare(struct rpc_task *task, void *data)
2783 struct nfs4_closedata *calldata = data;
2784 struct nfs4_state *state = calldata->state;
2785 struct inode *inode = calldata->inode;
2786 bool is_rdonly, is_wronly, is_rdwr;
2789 dprintk("%s: begin!\n", __func__);
2790 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
2793 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2794 spin_lock(&state->owner->so_lock);
2795 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
2796 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
2797 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
2798 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
2799 /* Calculate the change in open mode */
2800 calldata->arg.fmode = 0;
2801 if (state->n_rdwr == 0) {
2802 if (state->n_rdonly == 0)
2803 call_close |= is_rdonly;
2805 calldata->arg.fmode |= FMODE_READ;
2806 if (state->n_wronly == 0)
2807 call_close |= is_wronly;
2809 calldata->arg.fmode |= FMODE_WRITE;
2811 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
2813 if (calldata->arg.fmode == 0)
2814 call_close |= is_rdwr;
2816 if (!nfs4_valid_open_stateid(state))
2818 spin_unlock(&state->owner->so_lock);
2821 /* Note: exit _without_ calling nfs4_close_done */
2825 if (nfs4_wait_on_layoutreturn(inode, task)) {
2826 nfs_release_seqid(calldata->arg.seqid);
2830 if (calldata->arg.fmode == 0)
2831 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
2833 pnfs_roc_get_barrier(inode, &calldata->roc_barrier);
2835 calldata->arg.share_access =
2836 nfs4_map_atomic_open_share(NFS_SERVER(inode),
2837 calldata->arg.fmode, 0);
2839 nfs_fattr_init(calldata->res.fattr);
2840 calldata->timestamp = jiffies;
2841 if (nfs4_setup_sequence(NFS_SERVER(inode),
2842 &calldata->arg.seq_args,
2843 &calldata->res.seq_res,
2845 nfs_release_seqid(calldata->arg.seqid);
2846 dprintk("%s: done!\n", __func__);
2849 task->tk_action = NULL;
2851 nfs4_sequence_done(task, &calldata->res.seq_res);
2854 static const struct rpc_call_ops nfs4_close_ops = {
2855 .rpc_call_prepare = nfs4_close_prepare,
2856 .rpc_call_done = nfs4_close_done,
2857 .rpc_release = nfs4_free_closedata,
2860 static bool nfs4_roc(struct inode *inode)
2862 if (!nfs_have_layout(inode))
2864 return pnfs_roc(inode);
2868 * It is possible for data to be read/written from a mem-mapped file
2869 * after the sys_close call (which hits the vfs layer as a flush).
2870 * This means that we can't safely call nfsv4 close on a file until
2871 * the inode is cleared. This in turn means that we are not good
2872 * NFSv4 citizens - we do not indicate to the server to update the file's
2873 * share state even when we are done with one of the three share
2874 * stateid's in the inode.
2876 * NOTE: Caller must be holding the sp->so_owner semaphore!
2878 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
2880 struct nfs_server *server = NFS_SERVER(state->inode);
2881 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
2882 struct nfs4_closedata *calldata;
2883 struct nfs4_state_owner *sp = state->owner;
2884 struct rpc_task *task;
2885 struct rpc_message msg = {
2886 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2887 .rpc_cred = state->owner->so_cred,
2889 struct rpc_task_setup task_setup_data = {
2890 .rpc_client = server->client,
2891 .rpc_message = &msg,
2892 .callback_ops = &nfs4_close_ops,
2893 .workqueue = nfsiod_workqueue,
2894 .flags = RPC_TASK_ASYNC,
2896 int status = -ENOMEM;
2898 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
2899 &task_setup_data.rpc_client, &msg);
2901 calldata = kzalloc(sizeof(*calldata), gfp_mask);
2902 if (calldata == NULL)
2904 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
2905 calldata->inode = state->inode;
2906 calldata->state = state;
2907 calldata->arg.fh = NFS_FH(state->inode);
2908 /* Serialization for the sequence id */
2909 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
2910 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
2911 if (IS_ERR(calldata->arg.seqid))
2912 goto out_free_calldata;
2913 calldata->arg.fmode = 0;
2914 calldata->arg.bitmask = server->cache_consistency_bitmask;
2915 calldata->res.fattr = &calldata->fattr;
2916 calldata->res.seqid = calldata->arg.seqid;
2917 calldata->res.server = server;
2918 calldata->roc = nfs4_roc(state->inode);
2919 nfs_sb_active(calldata->inode->i_sb);
2921 msg.rpc_argp = &calldata->arg;
2922 msg.rpc_resp = &calldata->res;
2923 task_setup_data.callback_data = calldata;
2924 task = rpc_run_task(&task_setup_data);
2926 return PTR_ERR(task);
2929 status = rpc_wait_for_completion_task(task);
2935 nfs4_put_open_state(state);
2936 nfs4_put_state_owner(sp);
2940 static struct inode *
2941 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
2942 int open_flags, struct iattr *attr, int *opened)
2944 struct nfs4_state *state;
2945 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
2947 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
2949 /* Protect against concurrent sillydeletes */
2950 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
2952 nfs4_label_release_security(label);
2955 return ERR_CAST(state);
2956 return state->inode;
2959 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2961 if (ctx->state == NULL)
2964 nfs4_close_sync(ctx->state, ctx->mode);
2966 nfs4_close_state(ctx->state, ctx->mode);
2969 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
2970 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
2971 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
2973 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2975 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
2976 struct nfs4_server_caps_arg args = {
2980 struct nfs4_server_caps_res res = {};
2981 struct rpc_message msg = {
2982 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2988 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
2989 FATTR4_WORD0_FH_EXPIRE_TYPE |
2990 FATTR4_WORD0_LINK_SUPPORT |
2991 FATTR4_WORD0_SYMLINK_SUPPORT |
2992 FATTR4_WORD0_ACLSUPPORT;
2994 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
2996 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2998 /* Sanity check the server answers */
2999 switch (minorversion) {
3001 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3002 res.attr_bitmask[2] = 0;
3005 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3008 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3010 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
3011 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3012 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3013 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3014 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
3015 NFS_CAP_CTIME|NFS_CAP_MTIME|
3016 NFS_CAP_SECURITY_LABEL);
3017 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3018 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3019 server->caps |= NFS_CAP_ACLS;
3020 if (res.has_links != 0)
3021 server->caps |= NFS_CAP_HARDLINKS;
3022 if (res.has_symlinks != 0)
3023 server->caps |= NFS_CAP_SYMLINKS;
3024 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3025 server->caps |= NFS_CAP_FILEID;
3026 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3027 server->caps |= NFS_CAP_MODE;
3028 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3029 server->caps |= NFS_CAP_NLINK;
3030 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3031 server->caps |= NFS_CAP_OWNER;
3032 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3033 server->caps |= NFS_CAP_OWNER_GROUP;
3034 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3035 server->caps |= NFS_CAP_ATIME;
3036 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3037 server->caps |= NFS_CAP_CTIME;
3038 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3039 server->caps |= NFS_CAP_MTIME;
3040 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3041 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3042 server->caps |= NFS_CAP_SECURITY_LABEL;
3044 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3045 sizeof(server->attr_bitmask));
3046 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3048 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3049 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3050 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
3051 server->cache_consistency_bitmask[2] = 0;
3052 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3053 sizeof(server->exclcreat_bitmask));
3054 server->acl_bitmask = res.acl_bitmask;
3055 server->fh_expire_type = res.fh_expire_type;
3061 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3063 struct nfs4_exception exception = { };
3066 err = nfs4_handle_exception(server,
3067 _nfs4_server_capabilities(server, fhandle),
3069 } while (exception.retry);
3073 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3074 struct nfs_fsinfo *info)
3077 struct nfs4_lookup_root_arg args = {
3080 struct nfs4_lookup_res res = {
3082 .fattr = info->fattr,
3085 struct rpc_message msg = {
3086 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3091 bitmask[0] = nfs4_fattr_bitmap[0];
3092 bitmask[1] = nfs4_fattr_bitmap[1];
3094 * Process the label in the upcoming getfattr
3096 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3098 nfs_fattr_init(info->fattr);
3099 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3102 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3103 struct nfs_fsinfo *info)
3105 struct nfs4_exception exception = { };
3108 err = _nfs4_lookup_root(server, fhandle, info);
3109 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
3112 case -NFS4ERR_WRONGSEC:
3115 err = nfs4_handle_exception(server, err, &exception);
3117 } while (exception.retry);
3122 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3123 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3125 struct rpc_auth_create_args auth_args = {
3126 .pseudoflavor = flavor,
3128 struct rpc_auth *auth;
3131 auth = rpcauth_create(&auth_args, server->client);
3136 ret = nfs4_lookup_root(server, fhandle, info);
3142 * Retry pseudoroot lookup with various security flavors. We do this when:
3144 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3145 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3147 * Returns zero on success, or a negative NFS4ERR value, or a
3148 * negative errno value.
3150 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3151 struct nfs_fsinfo *info)
3153 /* Per 3530bis 15.33.5 */
3154 static const rpc_authflavor_t flav_array[] = {
3158 RPC_AUTH_UNIX, /* courtesy */
3161 int status = -EPERM;
3164 if (server->auth_info.flavor_len > 0) {
3165 /* try each flavor specified by user */
3166 for (i = 0; i < server->auth_info.flavor_len; i++) {
3167 status = nfs4_lookup_root_sec(server, fhandle, info,
3168 server->auth_info.flavors[i]);
3169 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3174 /* no flavors specified by user, try default list */
3175 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3176 status = nfs4_lookup_root_sec(server, fhandle, info,
3178 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3185 * -EACCESS could mean that the user doesn't have correct permissions
3186 * to access the mount. It could also mean that we tried to mount
3187 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3188 * existing mount programs don't handle -EACCES very well so it should
3189 * be mapped to -EPERM instead.
3191 if (status == -EACCES)
3196 static int nfs4_do_find_root_sec(struct nfs_server *server,
3197 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
3199 int mv = server->nfs_client->cl_minorversion;
3200 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
3204 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3205 * @server: initialized nfs_server handle
3206 * @fhandle: we fill in the pseudo-fs root file handle
3207 * @info: we fill in an FSINFO struct
3208 * @auth_probe: probe the auth flavours
3210 * Returns zero on success, or a negative errno.
3212 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
3213 struct nfs_fsinfo *info,
3219 status = nfs4_lookup_root(server, fhandle, info);
3221 if (auth_probe || status == NFS4ERR_WRONGSEC)
3222 status = nfs4_do_find_root_sec(server, fhandle, info);
3225 status = nfs4_server_capabilities(server, fhandle);
3227 status = nfs4_do_fsinfo(server, fhandle, info);
3229 return nfs4_map_errors(status);
3232 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3233 struct nfs_fsinfo *info)
3236 struct nfs_fattr *fattr = info->fattr;
3237 struct nfs4_label *label = NULL;
3239 error = nfs4_server_capabilities(server, mntfh);
3241 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3245 label = nfs4_label_alloc(server, GFP_KERNEL);
3247 return PTR_ERR(label);
3249 error = nfs4_proc_getattr(server, mntfh, fattr, label);
3251 dprintk("nfs4_get_root: getattr error = %d\n", -error);
3252 goto err_free_label;
3255 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3256 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3257 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3260 nfs4_label_free(label);
3266 * Get locations and (maybe) other attributes of a referral.
3267 * Note that we'll actually follow the referral later when
3268 * we detect fsid mismatch in inode revalidation
3270 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3271 const struct qstr *name, struct nfs_fattr *fattr,
3272 struct nfs_fh *fhandle)
3274 int status = -ENOMEM;
3275 struct page *page = NULL;
3276 struct nfs4_fs_locations *locations = NULL;
3278 page = alloc_page(GFP_KERNEL);
3281 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3282 if (locations == NULL)
3285 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
3290 * If the fsid didn't change, this is a migration event, not a
3291 * referral. Cause us to drop into the exception handler, which
3292 * will kick off migration recovery.
3294 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
3295 dprintk("%s: server did not return a different fsid for"
3296 " a referral at %s\n", __func__, name->name);
3297 status = -NFS4ERR_MOVED;
3300 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3301 nfs_fixup_referral_attributes(&locations->fattr);
3303 /* replace the lookup nfs_fattr with the locations nfs_fattr */
3304 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
3305 memset(fhandle, 0, sizeof(struct nfs_fh));
3313 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3314 struct nfs_fattr *fattr, struct nfs4_label *label)
3316 struct nfs4_getattr_arg args = {
3318 .bitmask = server->attr_bitmask,
3320 struct nfs4_getattr_res res = {
3325 struct rpc_message msg = {
3326 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3331 args.bitmask = nfs4_bitmask(server, label);
3333 nfs_fattr_init(fattr);
3334 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3337 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3338 struct nfs_fattr *fattr, struct nfs4_label *label)
3340 struct nfs4_exception exception = { };
3343 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3344 trace_nfs4_getattr(server, fhandle, fattr, err);
3345 err = nfs4_handle_exception(server, err,
3347 } while (exception.retry);
3352 * The file is not closed if it is opened due to the a request to change
3353 * the size of the file. The open call will not be needed once the
3354 * VFS layer lookup-intents are implemented.
3356 * Close is called when the inode is destroyed.
3357 * If we haven't opened the file for O_WRONLY, we
3358 * need to in the size_change case to obtain a stateid.
3361 * Because OPEN is always done by name in nfsv4, it is
3362 * possible that we opened a different file by the same
3363 * name. We can recognize this race condition, but we
3364 * can't do anything about it besides returning an error.
3366 * This will be fixed with VFS changes (lookup-intent).
3369 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3370 struct iattr *sattr)
3372 struct inode *inode = d_inode(dentry);
3373 struct rpc_cred *cred = NULL;
3374 struct nfs4_state *state = NULL;
3375 struct nfs4_label *label = NULL;
3378 if (pnfs_ld_layoutret_on_setattr(inode) &&
3379 sattr->ia_valid & ATTR_SIZE &&
3380 sattr->ia_size < i_size_read(inode))
3381 pnfs_commit_and_return_layout(inode);
3383 nfs_fattr_init(fattr);
3385 /* Deal with open(O_TRUNC) */
3386 if (sattr->ia_valid & ATTR_OPEN)
3387 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
3389 /* Optimization: if the end result is no change, don't RPC */
3390 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
3393 /* Search for an existing open(O_WRITE) file */
3394 if (sattr->ia_valid & ATTR_FILE) {
3395 struct nfs_open_context *ctx;
3397 ctx = nfs_file_open_context(sattr->ia_file);
3404 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3406 return PTR_ERR(label);
3408 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
3410 nfs_setattr_update_inode(inode, sattr, fattr);
3411 nfs_setsecurity(inode, fattr, label);
3413 nfs4_label_free(label);
3417 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3418 const struct qstr *name, struct nfs_fh *fhandle,
3419 struct nfs_fattr *fattr, struct nfs4_label *label)
3421 struct nfs_server *server = NFS_SERVER(dir);
3423 struct nfs4_lookup_arg args = {
3424 .bitmask = server->attr_bitmask,
3425 .dir_fh = NFS_FH(dir),
3428 struct nfs4_lookup_res res = {
3434 struct rpc_message msg = {
3435 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3440 args.bitmask = nfs4_bitmask(server, label);
3442 nfs_fattr_init(fattr);
3444 dprintk("NFS call lookup %s\n", name->name);
3445 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
3446 dprintk("NFS reply lookup: %d\n", status);
3450 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
3452 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
3453 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
3454 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3458 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3459 struct qstr *name, struct nfs_fh *fhandle,
3460 struct nfs_fattr *fattr, struct nfs4_label *label)
3462 struct nfs4_exception exception = { };
3463 struct rpc_clnt *client = *clnt;
3466 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
3467 trace_nfs4_lookup(dir, name, err);
3469 case -NFS4ERR_BADNAME:
3472 case -NFS4ERR_MOVED:
3473 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
3474 if (err == -NFS4ERR_MOVED)
3475 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3477 case -NFS4ERR_WRONGSEC:
3479 if (client != *clnt)
3481 client = nfs4_negotiate_security(client, dir, name);
3483 return PTR_ERR(client);
3485 exception.retry = 1;
3488 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3490 } while (exception.retry);
3495 else if (client != *clnt)
3496 rpc_shutdown_client(client);
3501 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
3502 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3503 struct nfs4_label *label)
3506 struct rpc_clnt *client = NFS_CLIENT(dir);
3508 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
3509 if (client != NFS_CLIENT(dir)) {
3510 rpc_shutdown_client(client);
3511 nfs_fixup_secinfo_attributes(fattr);
3517 nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
3518 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3520 struct rpc_clnt *client = NFS_CLIENT(dir);
3523 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
3525 return ERR_PTR(status);
3526 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
3529 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3531 struct nfs_server *server = NFS_SERVER(inode);
3532 struct nfs4_accessargs args = {
3533 .fh = NFS_FH(inode),
3534 .bitmask = server->cache_consistency_bitmask,
3536 struct nfs4_accessres res = {
3539 struct rpc_message msg = {
3540 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3543 .rpc_cred = entry->cred,
3545 int mode = entry->mask;
3549 * Determine which access bits we want to ask for...
3551 if (mode & MAY_READ)
3552 args.access |= NFS4_ACCESS_READ;
3553 if (S_ISDIR(inode->i_mode)) {
3554 if (mode & MAY_WRITE)
3555 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3556 if (mode & MAY_EXEC)
3557 args.access |= NFS4_ACCESS_LOOKUP;
3559 if (mode & MAY_WRITE)
3560 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3561 if (mode & MAY_EXEC)
3562 args.access |= NFS4_ACCESS_EXECUTE;
3565 res.fattr = nfs_alloc_fattr();
3566 if (res.fattr == NULL)
3569 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3571 nfs_access_set_mask(entry, res.access);
3572 nfs_refresh_inode(inode, res.fattr);
3574 nfs_free_fattr(res.fattr);
3578 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3580 struct nfs4_exception exception = { };
3583 err = _nfs4_proc_access(inode, entry);
3584 trace_nfs4_access(inode, err);
3585 err = nfs4_handle_exception(NFS_SERVER(inode), err,
3587 } while (exception.retry);
3592 * TODO: For the time being, we don't try to get any attributes
3593 * along with any of the zero-copy operations READ, READDIR,
3596 * In the case of the first three, we want to put the GETATTR
3597 * after the read-type operation -- this is because it is hard
3598 * to predict the length of a GETATTR response in v4, and thus
3599 * align the READ data correctly. This means that the GETATTR
3600 * may end up partially falling into the page cache, and we should
3601 * shift it into the 'tail' of the xdr_buf before processing.
3602 * To do this efficiently, we need to know the total length
3603 * of data received, which doesn't seem to be available outside
3606 * In the case of WRITE, we also want to put the GETATTR after
3607 * the operation -- in this case because we want to make sure
3608 * we get the post-operation mtime and size.
3610 * Both of these changes to the XDR layer would in fact be quite
3611 * minor, but I decided to leave them for a subsequent patch.
3613 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3614 unsigned int pgbase, unsigned int pglen)
3616 struct nfs4_readlink args = {
3617 .fh = NFS_FH(inode),
3622 struct nfs4_readlink_res res;
3623 struct rpc_message msg = {
3624 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3629 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
3632 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3633 unsigned int pgbase, unsigned int pglen)
3635 struct nfs4_exception exception = { };
3638 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3639 trace_nfs4_readlink(inode, err);
3640 err = nfs4_handle_exception(NFS_SERVER(inode), err,
3642 } while (exception.retry);
3647 * This is just for mknod. open(O_CREAT) will always do ->open_context().
3650 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
3653 struct nfs4_label l, *ilabel = NULL;
3654 struct nfs_open_context *ctx;
3655 struct nfs4_state *state;
3658 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3660 return PTR_ERR(ctx);
3662 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3664 sattr->ia_mode &= ~current_umask();
3665 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
3666 if (IS_ERR(state)) {
3667 status = PTR_ERR(state);
3671 nfs4_label_release_security(ilabel);
3672 put_nfs_open_context(ctx);
3676 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3678 struct nfs_server *server = NFS_SERVER(dir);
3679 struct nfs_removeargs args = {
3683 struct nfs_removeres res = {
3686 struct rpc_message msg = {
3687 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3693 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
3695 update_changeattr(dir, &res.cinfo);
3699 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3701 struct nfs4_exception exception = { };
3704 err = _nfs4_proc_remove(dir, name);
3705 trace_nfs4_remove(dir, name, err);
3706 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3708 } while (exception.retry);
3712 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
3714 struct nfs_server *server = NFS_SERVER(dir);
3715 struct nfs_removeargs *args = msg->rpc_argp;
3716 struct nfs_removeres *res = msg->rpc_resp;
3718 res->server = server;
3719 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
3720 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
3722 nfs_fattr_init(res->dir_attr);
3725 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3727 nfs4_setup_sequence(NFS_SERVER(data->dir),
3728 &data->args.seq_args,
3733 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
3735 struct nfs_unlinkdata *data = task->tk_calldata;
3736 struct nfs_removeres *res = &data->res;
3738 if (!nfs4_sequence_done(task, &res->seq_res))
3740 if (nfs4_async_handle_error(task, res->server, NULL,
3741 &data->timeout) == -EAGAIN)
3743 update_changeattr(dir, &res->cinfo);
3747 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3749 struct nfs_server *server = NFS_SERVER(dir);
3750 struct nfs_renameargs *arg = msg->rpc_argp;
3751 struct nfs_renameres *res = msg->rpc_resp;
3753 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
3754 res->server = server;
3755 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
3758 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3760 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3761 &data->args.seq_args,
3766 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3767 struct inode *new_dir)
3769 struct nfs_renamedata *data = task->tk_calldata;
3770 struct nfs_renameres *res = &data->res;
3772 if (!nfs4_sequence_done(task, &res->seq_res))
3774 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
3777 update_changeattr(old_dir, &res->old_cinfo);
3778 update_changeattr(new_dir, &res->new_cinfo);
3782 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3784 struct nfs_server *server = NFS_SERVER(inode);
3785 struct nfs4_link_arg arg = {
3786 .fh = NFS_FH(inode),
3787 .dir_fh = NFS_FH(dir),
3789 .bitmask = server->attr_bitmask,
3791 struct nfs4_link_res res = {
3795 struct rpc_message msg = {
3796 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3800 int status = -ENOMEM;
3802 res.fattr = nfs_alloc_fattr();
3803 if (res.fattr == NULL)
3806 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3807 if (IS_ERR(res.label)) {
3808 status = PTR_ERR(res.label);
3811 arg.bitmask = nfs4_bitmask(server, res.label);
3813 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3815 update_changeattr(dir, &res.cinfo);
3816 status = nfs_post_op_update_inode(inode, res.fattr);
3818 nfs_setsecurity(inode, res.fattr, res.label);
3822 nfs4_label_free(res.label);
3825 nfs_free_fattr(res.fattr);
3829 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3831 struct nfs4_exception exception = { };
3834 err = nfs4_handle_exception(NFS_SERVER(inode),
3835 _nfs4_proc_link(inode, dir, name),
3837 } while (exception.retry);
3841 struct nfs4_createdata {
3842 struct rpc_message msg;
3843 struct nfs4_create_arg arg;
3844 struct nfs4_create_res res;
3846 struct nfs_fattr fattr;
3847 struct nfs4_label *label;
3850 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3851 struct qstr *name, struct iattr *sattr, u32 ftype)
3853 struct nfs4_createdata *data;
3855 data = kzalloc(sizeof(*data), GFP_KERNEL);
3857 struct nfs_server *server = NFS_SERVER(dir);
3859 data->label = nfs4_label_alloc(server, GFP_KERNEL);
3860 if (IS_ERR(data->label))
3863 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3864 data->msg.rpc_argp = &data->arg;
3865 data->msg.rpc_resp = &data->res;
3866 data->arg.dir_fh = NFS_FH(dir);
3867 data->arg.server = server;
3868 data->arg.name = name;
3869 data->arg.attrs = sattr;
3870 data->arg.ftype = ftype;
3871 data->arg.bitmask = nfs4_bitmask(server, data->label);
3872 data->res.server = server;
3873 data->res.fh = &data->fh;
3874 data->res.fattr = &data->fattr;
3875 data->res.label = data->label;
3876 nfs_fattr_init(data->res.fattr);
3884 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3886 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
3887 &data->arg.seq_args, &data->res.seq_res, 1);
3889 update_changeattr(dir, &data->res.dir_cinfo);
3890 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
3895 static void nfs4_free_createdata(struct nfs4_createdata *data)
3897 nfs4_label_free(data->label);
3901 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3902 struct page *page, unsigned int len, struct iattr *sattr,
3903 struct nfs4_label *label)
3905 struct nfs4_createdata *data;
3906 int status = -ENAMETOOLONG;
3908 if (len > NFS4_MAXPATHLEN)
3912 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3916 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3917 data->arg.u.symlink.pages = &page;
3918 data->arg.u.symlink.len = len;
3919 data->arg.label = label;
3921 status = nfs4_do_create(dir, dentry, data);
3923 nfs4_free_createdata(data);
3928 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3929 struct page *page, unsigned int len, struct iattr *sattr)
3931 struct nfs4_exception exception = { };
3932 struct nfs4_label l, *label = NULL;
3935 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3938 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
3939 trace_nfs4_symlink(dir, &dentry->d_name, err);
3940 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3942 } while (exception.retry);
3944 nfs4_label_release_security(label);
3948 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3949 struct iattr *sattr, struct nfs4_label *label)
3951 struct nfs4_createdata *data;
3952 int status = -ENOMEM;
3954 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3958 data->arg.label = label;
3959 status = nfs4_do_create(dir, dentry, data);
3961 nfs4_free_createdata(data);
3966 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3967 struct iattr *sattr)
3969 struct nfs4_exception exception = { };
3970 struct nfs4_label l, *label = NULL;
3973 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3975 sattr->ia_mode &= ~current_umask();
3977 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
3978 trace_nfs4_mkdir(dir, &dentry->d_name, err);
3979 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3981 } while (exception.retry);
3982 nfs4_label_release_security(label);
3987 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3988 u64 cookie, struct page **pages, unsigned int count, int plus)
3990 struct inode *dir = d_inode(dentry);
3991 struct nfs4_readdir_arg args = {
3996 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
3999 struct nfs4_readdir_res res;
4000 struct rpc_message msg = {
4001 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4008 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4010 (unsigned long long)cookie);
4011 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
4012 res.pgbase = args.pgbase;
4013 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4015 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
4016 status += args.pgbase;
4019 nfs_invalidate_atime(dir);
4021 dprintk("%s: returns %d\n", __func__, status);
4025 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
4026 u64 cookie, struct page **pages, unsigned int count, int plus)
4028 struct nfs4_exception exception = { };
4031 err = _nfs4_proc_readdir(dentry, cred, cookie,
4032 pages, count, plus);
4033 trace_nfs4_readdir(d_inode(dentry), err);
4034 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
4036 } while (exception.retry);
4040 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4041 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
4043 struct nfs4_createdata *data;
4044 int mode = sattr->ia_mode;
4045 int status = -ENOMEM;
4047 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4052 data->arg.ftype = NF4FIFO;
4053 else if (S_ISBLK(mode)) {
4054 data->arg.ftype = NF4BLK;
4055 data->arg.u.device.specdata1 = MAJOR(rdev);
4056 data->arg.u.device.specdata2 = MINOR(rdev);
4058 else if (S_ISCHR(mode)) {
4059 data->arg.ftype = NF4CHR;
4060 data->arg.u.device.specdata1 = MAJOR(rdev);
4061 data->arg.u.device.specdata2 = MINOR(rdev);
4062 } else if (!S_ISSOCK(mode)) {
4067 data->arg.label = label;
4068 status = nfs4_do_create(dir, dentry, data);
4070 nfs4_free_createdata(data);
4075 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4076 struct iattr *sattr, dev_t rdev)
4078 struct nfs4_exception exception = { };
4079 struct nfs4_label l, *label = NULL;
4082 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4084 sattr->ia_mode &= ~current_umask();
4086 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4087 trace_nfs4_mknod(dir, &dentry->d_name, err);
4088 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4090 } while (exception.retry);
4092 nfs4_label_release_security(label);
4097 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4098 struct nfs_fsstat *fsstat)
4100 struct nfs4_statfs_arg args = {
4102 .bitmask = server->attr_bitmask,
4104 struct nfs4_statfs_res res = {
4107 struct rpc_message msg = {
4108 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4113 nfs_fattr_init(fsstat->fattr);
4114 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4117 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4119 struct nfs4_exception exception = { };
4122 err = nfs4_handle_exception(server,
4123 _nfs4_proc_statfs(server, fhandle, fsstat),
4125 } while (exception.retry);
4129 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4130 struct nfs_fsinfo *fsinfo)
4132 struct nfs4_fsinfo_arg args = {
4134 .bitmask = server->attr_bitmask,
4136 struct nfs4_fsinfo_res res = {
4139 struct rpc_message msg = {
4140 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4145 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4148 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4150 struct nfs4_exception exception = { };
4151 unsigned long now = jiffies;
4155 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
4156 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
4158 struct nfs_client *clp = server->nfs_client;
4160 spin_lock(&clp->cl_lock);
4161 clp->cl_lease_time = fsinfo->lease_time * HZ;
4162 clp->cl_last_renewal = now;
4163 spin_unlock(&clp->cl_lock);
4166 err = nfs4_handle_exception(server, err, &exception);
4167 } while (exception.retry);
4171 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4175 nfs_fattr_init(fsinfo->fattr);
4176 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
4178 /* block layout checks this! */
4179 server->pnfs_blksize = fsinfo->blksize;
4180 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
4186 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4187 struct nfs_pathconf *pathconf)
4189 struct nfs4_pathconf_arg args = {
4191 .bitmask = server->attr_bitmask,
4193 struct nfs4_pathconf_res res = {
4194 .pathconf = pathconf,
4196 struct rpc_message msg = {
4197 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4202 /* None of the pathconf attributes are mandatory to implement */
4203 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4204 memset(pathconf, 0, sizeof(*pathconf));
4208 nfs_fattr_init(pathconf->fattr);
4209 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4212 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4213 struct nfs_pathconf *pathconf)
4215 struct nfs4_exception exception = { };
4219 err = nfs4_handle_exception(server,
4220 _nfs4_proc_pathconf(server, fhandle, pathconf),
4222 } while (exception.retry);
4226 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
4227 const struct nfs_open_context *ctx,
4228 const struct nfs_lock_context *l_ctx,
4231 const struct nfs_lockowner *lockowner = NULL;
4234 lockowner = &l_ctx->lockowner;
4235 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
4237 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4239 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4240 const struct nfs_open_context *ctx,
4241 const struct nfs_lock_context *l_ctx,
4244 nfs4_stateid current_stateid;
4246 /* If the current stateid represents a lost lock, then exit */
4247 if (nfs4_set_rw_stateid(¤t_stateid, ctx, l_ctx, fmode) == -EIO)
4249 return nfs4_stateid_match(stateid, ¤t_stateid);
4252 static bool nfs4_error_stateid_expired(int err)
4255 case -NFS4ERR_DELEG_REVOKED:
4256 case -NFS4ERR_ADMIN_REVOKED:
4257 case -NFS4ERR_BAD_STATEID:
4258 case -NFS4ERR_STALE_STATEID:
4259 case -NFS4ERR_OLD_STATEID:
4260 case -NFS4ERR_OPENMODE:
4261 case -NFS4ERR_EXPIRED:
4267 void __nfs4_read_done_cb(struct nfs_pgio_header *hdr)
4269 nfs_invalidate_atime(hdr->inode);
4272 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
4274 struct nfs_server *server = NFS_SERVER(hdr->inode);
4276 trace_nfs4_read(hdr, task->tk_status);
4277 if (nfs4_async_handle_error(task, server,
4278 hdr->args.context->state,
4280 rpc_restart_call_prepare(task);
4284 __nfs4_read_done_cb(hdr);
4285 if (task->tk_status > 0)
4286 renew_lease(server, hdr->timestamp);
4290 static bool nfs4_read_stateid_changed(struct rpc_task *task,
4291 struct nfs_pgio_args *args)
4294 if (!nfs4_error_stateid_expired(task->tk_status) ||
4295 nfs4_stateid_is_current(&args->stateid,
4300 rpc_restart_call_prepare(task);
4304 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
4307 dprintk("--> %s\n", __func__);
4309 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
4311 if (nfs4_read_stateid_changed(task, &hdr->args))
4313 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4314 nfs4_read_done_cb(task, hdr);
4317 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4318 struct rpc_message *msg)
4320 hdr->timestamp = jiffies;
4321 hdr->pgio_done_cb = nfs4_read_done_cb;
4322 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
4323 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
4326 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4327 struct nfs_pgio_header *hdr)
4329 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4330 &hdr->args.seq_args,
4334 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4335 hdr->args.lock_context,
4336 hdr->rw_ops->rw_mode) == -EIO)
4338 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
4343 static int nfs4_write_done_cb(struct rpc_task *task,
4344 struct nfs_pgio_header *hdr)
4346 struct inode *inode = hdr->inode;
4348 trace_nfs4_write(hdr, task->tk_status);
4349 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4350 hdr->args.context->state,
4352 rpc_restart_call_prepare(task);
4355 if (task->tk_status >= 0) {
4356 renew_lease(NFS_SERVER(inode), hdr->timestamp);
4357 nfs_writeback_update_inode(hdr);
4362 static bool nfs4_write_stateid_changed(struct rpc_task *task,
4363 struct nfs_pgio_args *args)
4366 if (!nfs4_error_stateid_expired(task->tk_status) ||
4367 nfs4_stateid_is_current(&args->stateid,
4372 rpc_restart_call_prepare(task);
4376 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
4378 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
4380 if (nfs4_write_stateid_changed(task, &hdr->args))
4382 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4383 nfs4_write_done_cb(task, hdr);
4387 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
4389 /* Don't request attributes for pNFS or O_DIRECT writes */
4390 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
4392 /* Otherwise, request attributes if and only if we don't hold
4395 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
4398 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4399 struct rpc_message *msg)
4401 struct nfs_server *server = NFS_SERVER(hdr->inode);
4403 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4404 hdr->args.bitmask = NULL;
4405 hdr->res.fattr = NULL;
4407 hdr->args.bitmask = server->cache_consistency_bitmask;
4409 if (!hdr->pgio_done_cb)
4410 hdr->pgio_done_cb = nfs4_write_done_cb;
4411 hdr->res.server = server;
4412 hdr->timestamp = jiffies;
4414 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
4415 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
4418 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4420 nfs4_setup_sequence(NFS_SERVER(data->inode),
4421 &data->args.seq_args,
4426 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
4428 struct inode *inode = data->inode;
4430 trace_nfs4_commit(data, task->tk_status);
4431 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4432 NULL, NULL) == -EAGAIN) {
4433 rpc_restart_call_prepare(task);
4439 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
4441 if (!nfs4_sequence_done(task, &data->res.seq_res))
4443 return data->commit_done_cb(task, data);
4446 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
4448 struct nfs_server *server = NFS_SERVER(data->inode);
4450 if (data->commit_done_cb == NULL)
4451 data->commit_done_cb = nfs4_commit_done_cb;
4452 data->res.server = server;
4453 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
4454 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4457 struct nfs4_renewdata {
4458 struct nfs_client *client;
4459 unsigned long timestamp;
4463 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4464 * standalone procedure for queueing an asynchronous RENEW.
4466 static void nfs4_renew_release(void *calldata)
4468 struct nfs4_renewdata *data = calldata;
4469 struct nfs_client *clp = data->client;
4471 if (atomic_read(&clp->cl_count) > 1)
4472 nfs4_schedule_state_renewal(clp);
4473 nfs_put_client(clp);
4477 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
4479 struct nfs4_renewdata *data = calldata;
4480 struct nfs_client *clp = data->client;
4481 unsigned long timestamp = data->timestamp;
4483 trace_nfs4_renew_async(clp, task->tk_status);
4484 switch (task->tk_status) {
4487 case -NFS4ERR_LEASE_MOVED:
4488 nfs4_schedule_lease_moved_recovery(clp);
4491 /* Unless we're shutting down, schedule state recovery! */
4492 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4494 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
4495 nfs4_schedule_lease_recovery(clp);
4498 nfs4_schedule_path_down_recovery(clp);
4500 do_renew_lease(clp, timestamp);
4503 static const struct rpc_call_ops nfs4_renew_ops = {
4504 .rpc_call_done = nfs4_renew_done,
4505 .rpc_release = nfs4_renew_release,
4508 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
4510 struct rpc_message msg = {
4511 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4515 struct nfs4_renewdata *data;
4517 if (renew_flags == 0)
4519 if (!atomic_inc_not_zero(&clp->cl_count))
4521 data = kmalloc(sizeof(*data), GFP_NOFS);
4525 data->timestamp = jiffies;
4526 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
4527 &nfs4_renew_ops, data);
4530 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
4532 struct rpc_message msg = {
4533 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4537 unsigned long now = jiffies;
4540 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4543 do_renew_lease(clp, now);
4547 static inline int nfs4_server_supports_acls(struct nfs_server *server)
4549 return server->caps & NFS_CAP_ACLS;
4552 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4553 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
4556 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
4558 static int buf_to_pages_noslab(const void *buf, size_t buflen,
4559 struct page **pages, unsigned int *pgbase)
4561 struct page *newpage, **spages;
4567 len = min_t(size_t, PAGE_SIZE, buflen);
4568 newpage = alloc_page(GFP_KERNEL);
4570 if (newpage == NULL)
4572 memcpy(page_address(newpage), buf, len);
4577 } while (buflen != 0);
4583 __free_page(spages[rc-1]);
4587 struct nfs4_cached_acl {
4593 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
4595 struct nfs_inode *nfsi = NFS_I(inode);
4597 spin_lock(&inode->i_lock);
4598 kfree(nfsi->nfs4_acl);
4599 nfsi->nfs4_acl = acl;
4600 spin_unlock(&inode->i_lock);
4603 static void nfs4_zap_acl_attr(struct inode *inode)
4605 nfs4_set_cached_acl(inode, NULL);
4608 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4610 struct nfs_inode *nfsi = NFS_I(inode);
4611 struct nfs4_cached_acl *acl;
4614 spin_lock(&inode->i_lock);
4615 acl = nfsi->nfs4_acl;
4618 if (buf == NULL) /* user is just asking for length */
4620 if (acl->cached == 0)
4622 ret = -ERANGE; /* see getxattr(2) man page */
4623 if (acl->len > buflen)
4625 memcpy(buf, acl->data, acl->len);
4629 spin_unlock(&inode->i_lock);
4633 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
4635 struct nfs4_cached_acl *acl;
4636 size_t buflen = sizeof(*acl) + acl_len;
4638 if (buflen <= PAGE_SIZE) {
4639 acl = kmalloc(buflen, GFP_KERNEL);
4643 _copy_from_pages(acl->data, pages, pgbase, acl_len);
4645 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4652 nfs4_set_cached_acl(inode, acl);
4656 * The getxattr API returns the required buffer length when called with a
4657 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4658 * the required buf. On a NULL buf, we send a page of data to the server
4659 * guessing that the ACL request can be serviced by a page. If so, we cache
4660 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4661 * the cache. If not so, we throw away the page, and cache the required
4662 * length. The next getxattr call will then produce another round trip to
4663 * the server, this time with the input buf of the required size.
4665 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4667 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
4668 struct nfs_getaclargs args = {
4669 .fh = NFS_FH(inode),
4673 struct nfs_getaclres res = {
4676 struct rpc_message msg = {
4677 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4681 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4682 int ret = -ENOMEM, i;
4684 /* As long as we're doing a round trip to the server anyway,
4685 * let's be prepared for a page of acl data. */
4688 if (npages > ARRAY_SIZE(pages))
4691 for (i = 0; i < npages; i++) {
4692 pages[i] = alloc_page(GFP_KERNEL);
4697 /* for decoding across pages */
4698 res.acl_scratch = alloc_page(GFP_KERNEL);
4699 if (!res.acl_scratch)
4702 args.acl_len = npages * PAGE_SIZE;
4703 args.acl_pgbase = 0;
4705 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
4706 __func__, buf, buflen, npages, args.acl_len);
4707 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4708 &msg, &args.seq_args, &res.seq_res, 0);
4712 /* Handle the case where the passed-in buffer is too short */
4713 if (res.acl_flags & NFS4_ACL_TRUNC) {
4714 /* Did the user only issue a request for the acl length? */
4720 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
4722 if (res.acl_len > buflen) {
4726 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
4731 for (i = 0; i < npages; i++)
4733 __free_page(pages[i]);
4734 if (res.acl_scratch)
4735 __free_page(res.acl_scratch);
4739 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4741 struct nfs4_exception exception = { };
4744 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
4745 trace_nfs4_get_acl(inode, ret);
4748 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4749 } while (exception.retry);
4753 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4755 struct nfs_server *server = NFS_SERVER(inode);
4758 if (!nfs4_server_supports_acls(server))
4760 ret = nfs_revalidate_inode(server, inode);
4763 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4764 nfs_zap_acl_cache(inode);
4765 ret = nfs4_read_cached_acl(inode, buf, buflen);
4767 /* -ENOENT is returned if there is no ACL or if there is an ACL
4768 * but no cached acl data, just the acl length */
4770 return nfs4_get_acl_uncached(inode, buf, buflen);
4773 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4775 struct nfs_server *server = NFS_SERVER(inode);
4776 struct page *pages[NFS4ACL_MAXPAGES];
4777 struct nfs_setaclargs arg = {
4778 .fh = NFS_FH(inode),
4782 struct nfs_setaclres res;
4783 struct rpc_message msg = {
4784 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4788 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4791 if (!nfs4_server_supports_acls(server))
4793 if (npages > ARRAY_SIZE(pages))
4795 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4798 nfs4_inode_return_delegation(inode);
4799 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4802 * Free each page after tx, so the only ref left is
4803 * held by the network stack
4806 put_page(pages[i-1]);
4809 * Acl update can result in inode attribute update.
4810 * so mark the attribute cache invalid.
4812 spin_lock(&inode->i_lock);
4813 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4814 spin_unlock(&inode->i_lock);
4815 nfs_access_zap_cache(inode);
4816 nfs_zap_acl_cache(inode);
4820 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4822 struct nfs4_exception exception = { };
4825 err = __nfs4_proc_set_acl(inode, buf, buflen);
4826 trace_nfs4_set_acl(inode, err);
4827 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4829 } while (exception.retry);
4833 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
4834 static int _nfs4_get_security_label(struct inode *inode, void *buf,
4837 struct nfs_server *server = NFS_SERVER(inode);
4838 struct nfs_fattr fattr;
4839 struct nfs4_label label = {0, 0, buflen, buf};
4841 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4842 struct nfs4_getattr_arg arg = {
4843 .fh = NFS_FH(inode),
4846 struct nfs4_getattr_res res = {
4851 struct rpc_message msg = {
4852 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4858 nfs_fattr_init(&fattr);
4860 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
4863 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
4865 if (buflen < label.len)
4870 static int nfs4_get_security_label(struct inode *inode, void *buf,
4873 struct nfs4_exception exception = { };
4876 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4880 err = _nfs4_get_security_label(inode, buf, buflen);
4881 trace_nfs4_get_security_label(inode, err);
4882 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4884 } while (exception.retry);
4888 static int _nfs4_do_set_security_label(struct inode *inode,
4889 struct nfs4_label *ilabel,
4890 struct nfs_fattr *fattr,
4891 struct nfs4_label *olabel)
4894 struct iattr sattr = {0};
4895 struct nfs_server *server = NFS_SERVER(inode);
4896 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4897 struct nfs_setattrargs arg = {
4898 .fh = NFS_FH(inode),
4904 struct nfs_setattrres res = {
4909 struct rpc_message msg = {
4910 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
4916 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
4918 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4920 dprintk("%s failed: %d\n", __func__, status);
4925 static int nfs4_do_set_security_label(struct inode *inode,
4926 struct nfs4_label *ilabel,
4927 struct nfs_fattr *fattr,
4928 struct nfs4_label *olabel)
4930 struct nfs4_exception exception = { };
4934 err = _nfs4_do_set_security_label(inode, ilabel,
4936 trace_nfs4_set_security_label(inode, err);
4937 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4939 } while (exception.retry);
4944 nfs4_set_security_label(struct dentry *dentry, const void *buf, size_t buflen)
4946 struct nfs4_label ilabel, *olabel = NULL;
4947 struct nfs_fattr fattr;
4948 struct rpc_cred *cred;
4949 struct inode *inode = d_inode(dentry);
4952 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4955 nfs_fattr_init(&fattr);
4959 ilabel.label = (char *)buf;
4960 ilabel.len = buflen;
4962 cred = rpc_lookup_cred();
4964 return PTR_ERR(cred);
4966 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4967 if (IS_ERR(olabel)) {
4968 status = -PTR_ERR(olabel);
4972 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
4974 nfs_setsecurity(inode, &fattr, olabel);
4976 nfs4_label_free(olabel);
4981 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
4985 nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
4986 struct nfs4_state *state, long *timeout)
4988 struct nfs_client *clp = server->nfs_client;
4990 if (task->tk_status >= 0)
4992 switch(task->tk_status) {
4993 case -NFS4ERR_DELEG_REVOKED:
4994 case -NFS4ERR_ADMIN_REVOKED:
4995 case -NFS4ERR_BAD_STATEID:
4996 case -NFS4ERR_OPENMODE:
4999 if (nfs4_schedule_stateid_recovery(server, state) < 0)
5000 goto recovery_failed;
5001 goto wait_on_recovery;
5002 case -NFS4ERR_EXPIRED:
5003 if (state != NULL) {
5004 if (nfs4_schedule_stateid_recovery(server, state) < 0)
5005 goto recovery_failed;
5007 case -NFS4ERR_STALE_STATEID:
5008 case -NFS4ERR_STALE_CLIENTID:
5009 nfs4_schedule_lease_recovery(clp);
5010 goto wait_on_recovery;
5011 case -NFS4ERR_MOVED:
5012 if (nfs4_schedule_migration_recovery(server) < 0)
5013 goto recovery_failed;
5014 goto wait_on_recovery;
5015 case -NFS4ERR_LEASE_MOVED:
5016 nfs4_schedule_lease_moved_recovery(clp);
5017 goto wait_on_recovery;
5018 #if defined(CONFIG_NFS_V4_1)
5019 case -NFS4ERR_BADSESSION:
5020 case -NFS4ERR_BADSLOT:
5021 case -NFS4ERR_BAD_HIGH_SLOT:
5022 case -NFS4ERR_DEADSESSION:
5023 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
5024 case -NFS4ERR_SEQ_FALSE_RETRY:
5025 case -NFS4ERR_SEQ_MISORDERED:
5026 dprintk("%s ERROR %d, Reset session\n", __func__,
5028 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
5029 goto wait_on_recovery;
5030 #endif /* CONFIG_NFS_V4_1 */
5031 case -NFS4ERR_DELAY:
5032 nfs_inc_server_stats(server, NFSIOS_DELAY);
5033 rpc_delay(task, nfs4_update_delay(timeout));
5035 case -NFS4ERR_GRACE:
5036 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5037 case -NFS4ERR_RETRY_UNCACHED_REP:
5038 case -NFS4ERR_OLD_STATEID:
5041 task->tk_status = nfs4_map_errors(task->tk_status);
5044 task->tk_status = -EIO;
5047 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
5048 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
5049 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
5050 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
5051 goto recovery_failed;
5053 task->tk_status = 0;
5057 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5058 nfs4_verifier *bootverf)
5062 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5063 /* An impossible timestamp guarantees this value
5064 * will never match a generated boot time. */
5066 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
5068 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
5069 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
5070 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
5072 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5076 nfs4_init_nonuniform_client_string(struct nfs_client *clp)
5082 if (clp->cl_owner_id != NULL)
5086 len = 14 + strlen(clp->cl_ipaddr) + 1 +
5087 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5089 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5093 if (len > NFS4_OPAQUE_LIMIT + 1)
5097 * Since this string is allocated at mount time, and held until the
5098 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5099 * about a memory-reclaim deadlock.
5101 str = kmalloc(len, GFP_KERNEL);
5106 result = scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
5108 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5109 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
5112 clp->cl_owner_id = str;
5117 nfs4_init_uniquifier_client_string(struct nfs_client *clp)
5123 len = 10 + 10 + 1 + 10 + 1 +
5124 strlen(nfs4_client_id_uniquifier) + 1 +
5125 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5127 if (len > NFS4_OPAQUE_LIMIT + 1)
5131 * Since this string is allocated at mount time, and held until the
5132 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5133 * about a memory-reclaim deadlock.
5135 str = kmalloc(len, GFP_KERNEL);
5139 result = scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
5140 clp->rpc_ops->version, clp->cl_minorversion,
5141 nfs4_client_id_uniquifier,
5142 clp->cl_rpcclient->cl_nodename);
5143 clp->cl_owner_id = str;
5148 nfs4_init_uniform_client_string(struct nfs_client *clp)
5154 if (clp->cl_owner_id != NULL)
5157 if (nfs4_client_id_uniquifier[0] != '\0')
5158 return nfs4_init_uniquifier_client_string(clp);
5160 len = 10 + 10 + 1 + 10 + 1 +
5161 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5163 if (len > NFS4_OPAQUE_LIMIT + 1)
5167 * Since this string is allocated at mount time, and held until the
5168 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5169 * about a memory-reclaim deadlock.
5171 str = kmalloc(len, GFP_KERNEL);
5175 result = scnprintf(str, len, "Linux NFSv%u.%u %s",
5176 clp->rpc_ops->version, clp->cl_minorversion,
5177 clp->cl_rpcclient->cl_nodename);
5178 clp->cl_owner_id = str;
5183 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5184 * services. Advertise one based on the address family of the
5188 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5190 if (strchr(clp->cl_ipaddr, ':') != NULL)
5191 return scnprintf(buf, len, "tcp6");
5193 return scnprintf(buf, len, "tcp");
5196 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5198 struct nfs4_setclientid *sc = calldata;
5200 if (task->tk_status == 0)
5201 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5204 static const struct rpc_call_ops nfs4_setclientid_ops = {
5205 .rpc_call_done = nfs4_setclientid_done,
5209 * nfs4_proc_setclientid - Negotiate client ID
5210 * @clp: state data structure
5211 * @program: RPC program for NFSv4 callback service
5212 * @port: IP port number for NFS4 callback service
5213 * @cred: RPC credential to use for this call
5214 * @res: where to place the result
5216 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5218 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5219 unsigned short port, struct rpc_cred *cred,
5220 struct nfs4_setclientid_res *res)
5222 nfs4_verifier sc_verifier;
5223 struct nfs4_setclientid setclientid = {
5224 .sc_verifier = &sc_verifier,
5228 struct rpc_message msg = {
5229 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5230 .rpc_argp = &setclientid,
5234 struct rpc_task *task;
5235 struct rpc_task_setup task_setup_data = {
5236 .rpc_client = clp->cl_rpcclient,
5237 .rpc_message = &msg,
5238 .callback_ops = &nfs4_setclientid_ops,
5239 .callback_data = &setclientid,
5240 .flags = RPC_TASK_TIMEOUT,
5244 /* nfs_client_id4 */
5245 nfs4_init_boot_verifier(clp, &sc_verifier);
5247 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5248 status = nfs4_init_uniform_client_string(clp);
5250 status = nfs4_init_nonuniform_client_string(clp);
5256 setclientid.sc_netid_len =
5257 nfs4_init_callback_netid(clp,
5258 setclientid.sc_netid,
5259 sizeof(setclientid.sc_netid));
5260 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
5261 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
5262 clp->cl_ipaddr, port >> 8, port & 255);
5264 dprintk("NFS call setclientid auth=%s, '%s'\n",
5265 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5267 task = rpc_run_task(&task_setup_data);
5269 status = PTR_ERR(task);
5272 status = task->tk_status;
5273 if (setclientid.sc_cred) {
5274 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5275 put_rpccred(setclientid.sc_cred);
5279 trace_nfs4_setclientid(clp, status);
5280 dprintk("NFS reply setclientid: %d\n", status);
5285 * nfs4_proc_setclientid_confirm - Confirm client ID
5286 * @clp: state data structure
5287 * @res: result of a previous SETCLIENTID
5288 * @cred: RPC credential to use for this call
5290 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5292 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
5293 struct nfs4_setclientid_res *arg,
5294 struct rpc_cred *cred)
5296 struct rpc_message msg = {
5297 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
5303 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5304 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5306 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5307 trace_nfs4_setclientid_confirm(clp, status);
5308 dprintk("NFS reply setclientid_confirm: %d\n", status);
5312 struct nfs4_delegreturndata {
5313 struct nfs4_delegreturnargs args;
5314 struct nfs4_delegreturnres res;
5316 nfs4_stateid stateid;
5317 unsigned long timestamp;
5318 struct nfs_fattr fattr;
5320 struct inode *inode;
5325 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5327 struct nfs4_delegreturndata *data = calldata;
5329 if (!nfs4_sequence_done(task, &data->res.seq_res))
5332 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
5333 switch (task->tk_status) {
5335 renew_lease(data->res.server, data->timestamp);
5336 case -NFS4ERR_ADMIN_REVOKED:
5337 case -NFS4ERR_DELEG_REVOKED:
5338 case -NFS4ERR_BAD_STATEID:
5339 case -NFS4ERR_OLD_STATEID:
5340 case -NFS4ERR_STALE_STATEID:
5341 case -NFS4ERR_EXPIRED:
5342 task->tk_status = 0;
5344 pnfs_roc_set_barrier(data->inode, data->roc_barrier);
5347 if (nfs4_async_handle_error(task, data->res.server,
5348 NULL, NULL) == -EAGAIN) {
5349 rpc_restart_call_prepare(task);
5353 data->rpc_status = task->tk_status;
5356 static void nfs4_delegreturn_release(void *calldata)
5358 struct nfs4_delegreturndata *data = calldata;
5359 struct inode *inode = data->inode;
5363 pnfs_roc_release(inode);
5364 nfs_iput_and_deactive(inode);
5369 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5371 struct nfs4_delegreturndata *d_data;
5373 d_data = (struct nfs4_delegreturndata *)data;
5375 if (nfs4_wait_on_layoutreturn(d_data->inode, task))
5379 pnfs_roc_get_barrier(d_data->inode, &d_data->roc_barrier);
5381 nfs4_setup_sequence(d_data->res.server,
5382 &d_data->args.seq_args,
5383 &d_data->res.seq_res,
5387 static const struct rpc_call_ops nfs4_delegreturn_ops = {
5388 .rpc_call_prepare = nfs4_delegreturn_prepare,
5389 .rpc_call_done = nfs4_delegreturn_done,
5390 .rpc_release = nfs4_delegreturn_release,
5393 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
5395 struct nfs4_delegreturndata *data;
5396 struct nfs_server *server = NFS_SERVER(inode);
5397 struct rpc_task *task;
5398 struct rpc_message msg = {
5399 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5402 struct rpc_task_setup task_setup_data = {
5403 .rpc_client = server->client,
5404 .rpc_message = &msg,
5405 .callback_ops = &nfs4_delegreturn_ops,
5406 .flags = RPC_TASK_ASYNC,
5410 data = kzalloc(sizeof(*data), GFP_NOFS);
5413 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
5414 data->args.fhandle = &data->fh;
5415 data->args.stateid = &data->stateid;
5416 data->args.bitmask = server->cache_consistency_bitmask;
5417 nfs_copy_fh(&data->fh, NFS_FH(inode));
5418 nfs4_stateid_copy(&data->stateid, stateid);
5419 data->res.fattr = &data->fattr;
5420 data->res.server = server;
5421 nfs_fattr_init(data->res.fattr);
5422 data->timestamp = jiffies;
5423 data->rpc_status = 0;
5424 data->inode = nfs_igrab_and_active(inode);
5426 data->roc = nfs4_roc(inode);
5428 task_setup_data.callback_data = data;
5429 msg.rpc_argp = &data->args;
5430 msg.rpc_resp = &data->res;
5431 task = rpc_run_task(&task_setup_data);
5433 return PTR_ERR(task);
5436 status = nfs4_wait_for_completion_rpc_task(task);
5439 status = data->rpc_status;
5441 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5443 nfs_refresh_inode(inode, &data->fattr);
5449 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
5451 struct nfs_server *server = NFS_SERVER(inode);
5452 struct nfs4_exception exception = { };
5455 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
5456 trace_nfs4_delegreturn(inode, err);
5458 case -NFS4ERR_STALE_STATEID:
5459 case -NFS4ERR_EXPIRED:
5463 err = nfs4_handle_exception(server, err, &exception);
5464 } while (exception.retry);
5468 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
5469 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
5472 * sleep, with exponential backoff, and retry the LOCK operation.
5474 static unsigned long
5475 nfs4_set_lock_task_retry(unsigned long timeout)
5477 freezable_schedule_timeout_killable_unsafe(timeout);
5479 if (timeout > NFS4_LOCK_MAXTIMEOUT)
5480 return NFS4_LOCK_MAXTIMEOUT;
5484 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5486 struct inode *inode = state->inode;
5487 struct nfs_server *server = NFS_SERVER(inode);
5488 struct nfs_client *clp = server->nfs_client;
5489 struct nfs_lockt_args arg = {
5490 .fh = NFS_FH(inode),
5493 struct nfs_lockt_res res = {
5496 struct rpc_message msg = {
5497 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5500 .rpc_cred = state->owner->so_cred,
5502 struct nfs4_lock_state *lsp;
5505 arg.lock_owner.clientid = clp->cl_clientid;
5506 status = nfs4_set_lock_state(state, request);
5509 lsp = request->fl_u.nfs4_fl.owner;
5510 arg.lock_owner.id = lsp->ls_seqid.owner_id;
5511 arg.lock_owner.s_dev = server->s_dev;
5512 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5515 request->fl_type = F_UNLCK;
5517 case -NFS4ERR_DENIED:
5520 request->fl_ops->fl_release_private(request);
5521 request->fl_ops = NULL;
5526 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5528 struct nfs4_exception exception = { };
5532 err = _nfs4_proc_getlk(state, cmd, request);
5533 trace_nfs4_get_lock(request, state, cmd, err);
5534 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
5536 } while (exception.retry);
5540 static int do_vfs_lock(struct inode *inode, struct file_lock *fl)
5543 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
5545 res = posix_lock_inode_wait(inode, fl);
5548 res = flock_lock_inode_wait(inode, fl);
5556 struct nfs4_unlockdata {
5557 struct nfs_locku_args arg;
5558 struct nfs_locku_res res;
5559 struct nfs4_lock_state *lsp;
5560 struct nfs_open_context *ctx;
5561 struct file_lock fl;
5562 struct nfs_server *server;
5563 unsigned long timestamp;
5566 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5567 struct nfs_open_context *ctx,
5568 struct nfs4_lock_state *lsp,
5569 struct nfs_seqid *seqid)
5571 struct nfs4_unlockdata *p;
5572 struct inode *inode = lsp->ls_state->inode;
5574 p = kzalloc(sizeof(*p), GFP_NOFS);
5577 p->arg.fh = NFS_FH(inode);
5579 p->arg.seqid = seqid;
5580 p->res.seqid = seqid;
5582 atomic_inc(&lsp->ls_count);
5583 /* Ensure we don't close file until we're done freeing locks! */
5584 p->ctx = get_nfs_open_context(ctx);
5585 memcpy(&p->fl, fl, sizeof(p->fl));
5586 p->server = NFS_SERVER(inode);
5590 static void nfs4_locku_release_calldata(void *data)
5592 struct nfs4_unlockdata *calldata = data;
5593 nfs_free_seqid(calldata->arg.seqid);
5594 nfs4_put_lock_state(calldata->lsp);
5595 put_nfs_open_context(calldata->ctx);
5599 static void nfs4_locku_done(struct rpc_task *task, void *data)
5601 struct nfs4_unlockdata *calldata = data;
5603 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5605 switch (task->tk_status) {
5607 renew_lease(calldata->server, calldata->timestamp);
5608 do_vfs_lock(calldata->lsp->ls_state->inode, &calldata->fl);
5609 if (nfs4_update_lock_stateid(calldata->lsp,
5610 &calldata->res.stateid))
5612 case -NFS4ERR_BAD_STATEID:
5613 case -NFS4ERR_OLD_STATEID:
5614 case -NFS4ERR_STALE_STATEID:
5615 case -NFS4ERR_EXPIRED:
5616 if (!nfs4_stateid_match(&calldata->arg.stateid,
5617 &calldata->lsp->ls_stateid))
5618 rpc_restart_call_prepare(task);
5621 if (nfs4_async_handle_error(task, calldata->server,
5622 NULL, NULL) == -EAGAIN)
5623 rpc_restart_call_prepare(task);
5625 nfs_release_seqid(calldata->arg.seqid);
5628 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
5630 struct nfs4_unlockdata *calldata = data;
5632 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
5634 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
5635 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
5636 /* Note: exit _without_ running nfs4_locku_done */
5639 calldata->timestamp = jiffies;
5640 if (nfs4_setup_sequence(calldata->server,
5641 &calldata->arg.seq_args,
5642 &calldata->res.seq_res,
5644 nfs_release_seqid(calldata->arg.seqid);
5647 task->tk_action = NULL;
5649 nfs4_sequence_done(task, &calldata->res.seq_res);
5652 static const struct rpc_call_ops nfs4_locku_ops = {
5653 .rpc_call_prepare = nfs4_locku_prepare,
5654 .rpc_call_done = nfs4_locku_done,
5655 .rpc_release = nfs4_locku_release_calldata,
5658 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5659 struct nfs_open_context *ctx,
5660 struct nfs4_lock_state *lsp,
5661 struct nfs_seqid *seqid)
5663 struct nfs4_unlockdata *data;
5664 struct rpc_message msg = {
5665 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5666 .rpc_cred = ctx->cred,
5668 struct rpc_task_setup task_setup_data = {
5669 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
5670 .rpc_message = &msg,
5671 .callback_ops = &nfs4_locku_ops,
5672 .workqueue = nfsiod_workqueue,
5673 .flags = RPC_TASK_ASYNC,
5676 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5677 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5679 /* Ensure this is an unlock - when canceling a lock, the
5680 * canceled lock is passed in, and it won't be an unlock.
5682 fl->fl_type = F_UNLCK;
5684 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5686 nfs_free_seqid(seqid);
5687 return ERR_PTR(-ENOMEM);
5690 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
5691 msg.rpc_argp = &data->arg;
5692 msg.rpc_resp = &data->res;
5693 task_setup_data.callback_data = data;
5694 return rpc_run_task(&task_setup_data);
5697 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5699 struct inode *inode = state->inode;
5700 struct nfs4_state_owner *sp = state->owner;
5701 struct nfs_inode *nfsi = NFS_I(inode);
5702 struct nfs_seqid *seqid;
5703 struct nfs4_lock_state *lsp;
5704 struct rpc_task *task;
5705 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
5707 unsigned char fl_flags = request->fl_flags;
5709 status = nfs4_set_lock_state(state, request);
5710 /* Unlock _before_ we do the RPC call */
5711 request->fl_flags |= FL_EXISTS;
5712 /* Exclude nfs_delegation_claim_locks() */
5713 mutex_lock(&sp->so_delegreturn_mutex);
5714 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
5715 down_read(&nfsi->rwsem);
5716 if (do_vfs_lock(inode, request) == -ENOENT) {
5717 up_read(&nfsi->rwsem);
5718 mutex_unlock(&sp->so_delegreturn_mutex);
5721 up_read(&nfsi->rwsem);
5722 mutex_unlock(&sp->so_delegreturn_mutex);
5725 /* Is this a delegated lock? */
5726 lsp = request->fl_u.nfs4_fl.owner;
5727 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5729 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
5730 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
5734 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
5735 status = PTR_ERR(task);
5738 status = nfs4_wait_for_completion_rpc_task(task);
5741 request->fl_flags = fl_flags;
5742 trace_nfs4_unlock(request, state, F_SETLK, status);
5746 struct nfs4_lockdata {
5747 struct nfs_lock_args arg;
5748 struct nfs_lock_res res;
5749 struct nfs4_lock_state *lsp;
5750 struct nfs_open_context *ctx;
5751 struct file_lock fl;
5752 unsigned long timestamp;
5755 struct nfs_server *server;
5758 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
5759 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5762 struct nfs4_lockdata *p;
5763 struct inode *inode = lsp->ls_state->inode;
5764 struct nfs_server *server = NFS_SERVER(inode);
5765 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
5767 p = kzalloc(sizeof(*p), gfp_mask);
5771 p->arg.fh = NFS_FH(inode);
5773 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
5774 if (IS_ERR(p->arg.open_seqid))
5776 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
5777 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
5778 if (IS_ERR(p->arg.lock_seqid))
5779 goto out_free_seqid;
5780 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
5781 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
5782 p->arg.lock_owner.s_dev = server->s_dev;
5783 p->res.lock_seqid = p->arg.lock_seqid;
5786 atomic_inc(&lsp->ls_count);
5787 p->ctx = get_nfs_open_context(ctx);
5788 get_file(fl->fl_file);
5789 memcpy(&p->fl, fl, sizeof(p->fl));
5792 nfs_free_seqid(p->arg.open_seqid);
5798 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5800 struct nfs4_lockdata *data = calldata;
5801 struct nfs4_state *state = data->lsp->ls_state;
5803 dprintk("%s: begin!\n", __func__);
5804 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
5806 /* Do we need to do an open_to_lock_owner? */
5807 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
5808 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
5809 goto out_release_lock_seqid;
5811 nfs4_stateid_copy(&data->arg.open_stateid,
5812 &state->open_stateid);
5813 data->arg.new_lock_owner = 1;
5814 data->res.open_seqid = data->arg.open_seqid;
5816 data->arg.new_lock_owner = 0;
5817 nfs4_stateid_copy(&data->arg.lock_stateid,
5818 &data->lsp->ls_stateid);
5820 if (!nfs4_valid_open_stateid(state)) {
5821 data->rpc_status = -EBADF;
5822 task->tk_action = NULL;
5823 goto out_release_open_seqid;
5825 data->timestamp = jiffies;
5826 if (nfs4_setup_sequence(data->server,
5827 &data->arg.seq_args,
5831 out_release_open_seqid:
5832 nfs_release_seqid(data->arg.open_seqid);
5833 out_release_lock_seqid:
5834 nfs_release_seqid(data->arg.lock_seqid);
5836 nfs4_sequence_done(task, &data->res.seq_res);
5837 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
5840 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5842 struct nfs4_lockdata *data = calldata;
5843 struct nfs4_lock_state *lsp = data->lsp;
5845 dprintk("%s: begin!\n", __func__);
5847 if (!nfs4_sequence_done(task, &data->res.seq_res))
5850 data->rpc_status = task->tk_status;
5851 switch (task->tk_status) {
5853 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
5855 if (data->arg.new_lock) {
5856 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
5857 if (do_vfs_lock(lsp->ls_state->inode, &data->fl) < 0) {
5858 rpc_restart_call_prepare(task);
5862 if (data->arg.new_lock_owner != 0) {
5863 nfs_confirm_seqid(&lsp->ls_seqid, 0);
5864 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
5865 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
5866 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
5867 rpc_restart_call_prepare(task);
5869 case -NFS4ERR_BAD_STATEID:
5870 case -NFS4ERR_OLD_STATEID:
5871 case -NFS4ERR_STALE_STATEID:
5872 case -NFS4ERR_EXPIRED:
5873 if (data->arg.new_lock_owner != 0) {
5874 if (!nfs4_stateid_match(&data->arg.open_stateid,
5875 &lsp->ls_state->open_stateid))
5876 rpc_restart_call_prepare(task);
5877 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
5879 rpc_restart_call_prepare(task);
5881 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
5884 static void nfs4_lock_release(void *calldata)
5886 struct nfs4_lockdata *data = calldata;
5888 dprintk("%s: begin!\n", __func__);
5889 nfs_free_seqid(data->arg.open_seqid);
5890 if (data->cancelled != 0) {
5891 struct rpc_task *task;
5892 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5893 data->arg.lock_seqid);
5895 rpc_put_task_async(task);
5896 dprintk("%s: cancelling lock!\n", __func__);
5898 nfs_free_seqid(data->arg.lock_seqid);
5899 nfs4_put_lock_state(data->lsp);
5900 put_nfs_open_context(data->ctx);
5901 fput(data->fl.fl_file);
5903 dprintk("%s: done!\n", __func__);
5906 static const struct rpc_call_ops nfs4_lock_ops = {
5907 .rpc_call_prepare = nfs4_lock_prepare,
5908 .rpc_call_done = nfs4_lock_done,
5909 .rpc_release = nfs4_lock_release,
5912 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5915 case -NFS4ERR_ADMIN_REVOKED:
5916 case -NFS4ERR_BAD_STATEID:
5917 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
5918 if (new_lock_owner != 0 ||
5919 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
5920 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
5922 case -NFS4ERR_STALE_STATEID:
5923 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
5924 case -NFS4ERR_EXPIRED:
5925 nfs4_schedule_lease_recovery(server->nfs_client);
5929 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
5931 struct nfs4_lockdata *data;
5932 struct rpc_task *task;
5933 struct rpc_message msg = {
5934 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5935 .rpc_cred = state->owner->so_cred,
5937 struct rpc_task_setup task_setup_data = {
5938 .rpc_client = NFS_CLIENT(state->inode),
5939 .rpc_message = &msg,
5940 .callback_ops = &nfs4_lock_ops,
5941 .workqueue = nfsiod_workqueue,
5942 .flags = RPC_TASK_ASYNC,
5946 dprintk("%s: begin!\n", __func__);
5947 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
5948 fl->fl_u.nfs4_fl.owner,
5949 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
5953 data->arg.block = 1;
5954 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
5955 msg.rpc_argp = &data->arg;
5956 msg.rpc_resp = &data->res;
5957 task_setup_data.callback_data = data;
5958 if (recovery_type > NFS_LOCK_NEW) {
5959 if (recovery_type == NFS_LOCK_RECLAIM)
5960 data->arg.reclaim = NFS_LOCK_RECLAIM;
5961 nfs4_set_sequence_privileged(&data->arg.seq_args);
5963 data->arg.new_lock = 1;
5964 task = rpc_run_task(&task_setup_data);
5966 return PTR_ERR(task);
5967 ret = nfs4_wait_for_completion_rpc_task(task);
5969 ret = data->rpc_status;
5971 nfs4_handle_setlk_error(data->server, data->lsp,
5972 data->arg.new_lock_owner, ret);
5974 data->cancelled = 1;
5976 dprintk("%s: done, ret = %d!\n", __func__, ret);
5980 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
5982 struct nfs_server *server = NFS_SERVER(state->inode);
5983 struct nfs4_exception exception = {
5984 .inode = state->inode,
5989 /* Cache the lock if possible... */
5990 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5992 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
5993 trace_nfs4_lock_reclaim(request, state, F_SETLK, err);
5994 if (err != -NFS4ERR_DELAY)
5996 nfs4_handle_exception(server, err, &exception);
5997 } while (exception.retry);
6001 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6003 struct nfs_server *server = NFS_SERVER(state->inode);
6004 struct nfs4_exception exception = {
6005 .inode = state->inode,
6009 err = nfs4_set_lock_state(state, request);
6012 if (!recover_lost_locks) {
6013 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6017 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6019 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
6020 trace_nfs4_lock_expired(request, state, F_SETLK, err);
6024 case -NFS4ERR_GRACE:
6025 case -NFS4ERR_DELAY:
6026 nfs4_handle_exception(server, err, &exception);
6029 } while (exception.retry);
6034 #if defined(CONFIG_NFS_V4_1)
6036 * nfs41_check_expired_locks - possibly free a lock stateid
6038 * @state: NFSv4 state for an inode
6040 * Returns NFS_OK if recovery for this stateid is now finished.
6041 * Otherwise a negative NFS4ERR value is returned.
6043 static int nfs41_check_expired_locks(struct nfs4_state *state)
6045 int status, ret = -NFS4ERR_BAD_STATEID;
6046 struct nfs4_lock_state *lsp;
6047 struct nfs_server *server = NFS_SERVER(state->inode);
6049 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
6050 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
6051 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
6053 status = nfs41_test_stateid(server,
6056 trace_nfs4_test_lock_stateid(state, lsp, status);
6057 if (status != NFS_OK) {
6058 /* Free the stateid unless the server
6059 * informs us the stateid is unrecognized. */
6060 if (status != -NFS4ERR_BAD_STATEID)
6061 nfs41_free_stateid(server,
6064 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6073 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6075 int status = NFS_OK;
6077 if (test_bit(LK_STATE_IN_USE, &state->flags))
6078 status = nfs41_check_expired_locks(state);
6079 if (status != NFS_OK)
6080 status = nfs4_lock_expired(state, request);
6085 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6087 struct nfs_inode *nfsi = NFS_I(state->inode);
6088 unsigned char fl_flags = request->fl_flags;
6089 int status = -ENOLCK;
6091 if ((fl_flags & FL_POSIX) &&
6092 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6094 /* Is this a delegated open? */
6095 status = nfs4_set_lock_state(state, request);
6098 request->fl_flags |= FL_ACCESS;
6099 status = do_vfs_lock(state->inode, request);
6102 down_read(&nfsi->rwsem);
6103 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
6104 /* Yes: cache locks! */
6105 /* ...but avoid races with delegation recall... */
6106 request->fl_flags = fl_flags & ~FL_SLEEP;
6107 status = do_vfs_lock(state->inode, request);
6108 up_read(&nfsi->rwsem);
6111 up_read(&nfsi->rwsem);
6112 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
6114 request->fl_flags = fl_flags;
6118 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6120 struct nfs4_exception exception = {
6122 .inode = state->inode,
6127 err = _nfs4_proc_setlk(state, cmd, request);
6128 trace_nfs4_set_lock(request, state, cmd, err);
6129 if (err == -NFS4ERR_DENIED)
6131 err = nfs4_handle_exception(NFS_SERVER(state->inode),
6133 } while (exception.retry);
6138 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6140 struct nfs_open_context *ctx;
6141 struct nfs4_state *state;
6142 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6145 /* verify open state */
6146 ctx = nfs_file_open_context(filp);
6149 if (request->fl_start < 0 || request->fl_end < 0)
6152 if (IS_GETLK(cmd)) {
6154 return nfs4_proc_getlk(state, F_GETLK, request);
6158 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6161 if (request->fl_type == F_UNLCK) {
6163 return nfs4_proc_unlck(state, cmd, request);
6170 * Don't rely on the VFS having checked the file open mode,
6171 * since it won't do this for flock() locks.
6173 switch (request->fl_type) {
6175 if (!(filp->f_mode & FMODE_READ))
6179 if (!(filp->f_mode & FMODE_WRITE))
6184 status = nfs4_proc_setlk(state, cmd, request);
6185 if ((status != -EAGAIN) || IS_SETLK(cmd))
6187 timeout = nfs4_set_lock_task_retry(timeout);
6188 status = -ERESTARTSYS;
6191 } while(status < 0);
6195 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
6197 struct nfs_server *server = NFS_SERVER(state->inode);
6200 err = nfs4_set_lock_state(state, fl);
6203 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
6204 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
6207 struct nfs_release_lockowner_data {
6208 struct nfs4_lock_state *lsp;
6209 struct nfs_server *server;
6210 struct nfs_release_lockowner_args args;
6211 struct nfs_release_lockowner_res res;
6212 unsigned long timestamp;
6215 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6217 struct nfs_release_lockowner_data *data = calldata;
6218 struct nfs_server *server = data->server;
6219 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
6220 &data->args.seq_args, &data->res.seq_res, task);
6221 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6222 data->timestamp = jiffies;
6225 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6227 struct nfs_release_lockowner_data *data = calldata;
6228 struct nfs_server *server = data->server;
6230 nfs40_sequence_done(task, &data->res.seq_res);
6232 switch (task->tk_status) {
6234 renew_lease(server, data->timestamp);
6236 case -NFS4ERR_STALE_CLIENTID:
6237 case -NFS4ERR_EXPIRED:
6238 nfs4_schedule_lease_recovery(server->nfs_client);
6240 case -NFS4ERR_LEASE_MOVED:
6241 case -NFS4ERR_DELAY:
6242 if (nfs4_async_handle_error(task, server,
6243 NULL, NULL) == -EAGAIN)
6244 rpc_restart_call_prepare(task);
6248 static void nfs4_release_lockowner_release(void *calldata)
6250 struct nfs_release_lockowner_data *data = calldata;
6251 nfs4_free_lock_state(data->server, data->lsp);
6255 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
6256 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6257 .rpc_call_done = nfs4_release_lockowner_done,
6258 .rpc_release = nfs4_release_lockowner_release,
6262 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
6264 struct nfs_release_lockowner_data *data;
6265 struct rpc_message msg = {
6266 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6269 if (server->nfs_client->cl_mvops->minor_version != 0)
6272 data = kmalloc(sizeof(*data), GFP_NOFS);
6276 data->server = server;
6277 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6278 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6279 data->args.lock_owner.s_dev = server->s_dev;
6281 msg.rpc_argp = &data->args;
6282 msg.rpc_resp = &data->res;
6283 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
6284 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
6287 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6289 static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
6290 const void *buf, size_t buflen,
6291 int flags, int type)
6293 if (strcmp(key, "") != 0)
6296 return nfs4_proc_set_acl(d_inode(dentry), buf, buflen);
6299 static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
6300 void *buf, size_t buflen, int type)
6302 if (strcmp(key, "") != 0)
6305 return nfs4_proc_get_acl(d_inode(dentry), buf, buflen);
6308 static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
6309 size_t list_len, const char *name,
6310 size_t name_len, int type)
6312 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
6314 if (!nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry))))
6317 if (list && len <= list_len)
6318 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
6322 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
6323 static inline int nfs4_server_supports_labels(struct nfs_server *server)
6325 return server->caps & NFS_CAP_SECURITY_LABEL;
6328 static int nfs4_xattr_set_nfs4_label(struct dentry *dentry, const char *key,
6329 const void *buf, size_t buflen,
6330 int flags, int type)
6332 if (security_ismaclabel(key))
6333 return nfs4_set_security_label(dentry, buf, buflen);
6338 static int nfs4_xattr_get_nfs4_label(struct dentry *dentry, const char *key,
6339 void *buf, size_t buflen, int type)
6341 if (security_ismaclabel(key))
6342 return nfs4_get_security_label(d_inode(dentry), buf, buflen);
6346 static size_t nfs4_xattr_list_nfs4_label(struct dentry *dentry, char *list,
6347 size_t list_len, const char *name,
6348 size_t name_len, int type)
6352 if (nfs_server_capable(d_inode(dentry), NFS_CAP_SECURITY_LABEL)) {
6353 len = security_inode_listsecurity(d_inode(dentry), NULL, 0);
6354 if (list && len <= list_len)
6355 security_inode_listsecurity(d_inode(dentry), list, len);
6360 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6361 .prefix = XATTR_SECURITY_PREFIX,
6362 .list = nfs4_xattr_list_nfs4_label,
6363 .get = nfs4_xattr_get_nfs4_label,
6364 .set = nfs4_xattr_set_nfs4_label,
6370 * nfs_fhget will use either the mounted_on_fileid or the fileid
6372 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6374 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6375 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6376 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
6377 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
6380 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
6381 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
6382 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6386 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6387 const struct qstr *name,
6388 struct nfs4_fs_locations *fs_locations,
6391 struct nfs_server *server = NFS_SERVER(dir);
6393 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6395 struct nfs4_fs_locations_arg args = {
6396 .dir_fh = NFS_FH(dir),
6401 struct nfs4_fs_locations_res res = {
6402 .fs_locations = fs_locations,
6404 struct rpc_message msg = {
6405 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6411 dprintk("%s: start\n", __func__);
6413 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6414 * is not supported */
6415 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6416 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6418 bitmask[0] |= FATTR4_WORD0_FILEID;
6420 nfs_fattr_init(&fs_locations->fattr);
6421 fs_locations->server = server;
6422 fs_locations->nlocations = 0;
6423 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
6424 dprintk("%s: returned status = %d\n", __func__, status);
6428 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6429 const struct qstr *name,
6430 struct nfs4_fs_locations *fs_locations,
6433 struct nfs4_exception exception = { };
6436 err = _nfs4_proc_fs_locations(client, dir, name,
6437 fs_locations, page);
6438 trace_nfs4_get_fs_locations(dir, name, err);
6439 err = nfs4_handle_exception(NFS_SERVER(dir), err,
6441 } while (exception.retry);
6446 * This operation also signals the server that this client is
6447 * performing migration recovery. The server can stop returning
6448 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6449 * appended to this compound to identify the client ID which is
6450 * performing recovery.
6452 static int _nfs40_proc_get_locations(struct inode *inode,
6453 struct nfs4_fs_locations *locations,
6454 struct page *page, struct rpc_cred *cred)
6456 struct nfs_server *server = NFS_SERVER(inode);
6457 struct rpc_clnt *clnt = server->client;
6459 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6461 struct nfs4_fs_locations_arg args = {
6462 .clientid = server->nfs_client->cl_clientid,
6463 .fh = NFS_FH(inode),
6466 .migration = 1, /* skip LOOKUP */
6467 .renew = 1, /* append RENEW */
6469 struct nfs4_fs_locations_res res = {
6470 .fs_locations = locations,
6474 struct rpc_message msg = {
6475 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6480 unsigned long now = jiffies;
6483 nfs_fattr_init(&locations->fattr);
6484 locations->server = server;
6485 locations->nlocations = 0;
6487 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6488 nfs4_set_sequence_privileged(&args.seq_args);
6489 status = nfs4_call_sync_sequence(clnt, server, &msg,
6490 &args.seq_args, &res.seq_res);
6494 renew_lease(server, now);
6498 #ifdef CONFIG_NFS_V4_1
6501 * This operation also signals the server that this client is
6502 * performing migration recovery. The server can stop asserting
6503 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6504 * performing this operation is identified in the SEQUENCE
6505 * operation in this compound.
6507 * When the client supports GETATTR(fs_locations_info), it can
6508 * be plumbed in here.
6510 static int _nfs41_proc_get_locations(struct inode *inode,
6511 struct nfs4_fs_locations *locations,
6512 struct page *page, struct rpc_cred *cred)
6514 struct nfs_server *server = NFS_SERVER(inode);
6515 struct rpc_clnt *clnt = server->client;
6517 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6519 struct nfs4_fs_locations_arg args = {
6520 .fh = NFS_FH(inode),
6523 .migration = 1, /* skip LOOKUP */
6525 struct nfs4_fs_locations_res res = {
6526 .fs_locations = locations,
6529 struct rpc_message msg = {
6530 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6537 nfs_fattr_init(&locations->fattr);
6538 locations->server = server;
6539 locations->nlocations = 0;
6541 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6542 nfs4_set_sequence_privileged(&args.seq_args);
6543 status = nfs4_call_sync_sequence(clnt, server, &msg,
6544 &args.seq_args, &res.seq_res);
6545 if (status == NFS4_OK &&
6546 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6547 status = -NFS4ERR_LEASE_MOVED;
6551 #endif /* CONFIG_NFS_V4_1 */
6554 * nfs4_proc_get_locations - discover locations for a migrated FSID
6555 * @inode: inode on FSID that is migrating
6556 * @locations: result of query
6558 * @cred: credential to use for this operation
6560 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6561 * operation failed, or a negative errno if a local error occurred.
6563 * On success, "locations" is filled in, but if the server has
6564 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6567 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6568 * from this client that require migration recovery.
6570 int nfs4_proc_get_locations(struct inode *inode,
6571 struct nfs4_fs_locations *locations,
6572 struct page *page, struct rpc_cred *cred)
6574 struct nfs_server *server = NFS_SERVER(inode);
6575 struct nfs_client *clp = server->nfs_client;
6576 const struct nfs4_mig_recovery_ops *ops =
6577 clp->cl_mvops->mig_recovery_ops;
6578 struct nfs4_exception exception = { };
6581 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6582 (unsigned long long)server->fsid.major,
6583 (unsigned long long)server->fsid.minor,
6585 nfs_display_fhandle(NFS_FH(inode), __func__);
6588 status = ops->get_locations(inode, locations, page, cred);
6589 if (status != -NFS4ERR_DELAY)
6591 nfs4_handle_exception(server, status, &exception);
6592 } while (exception.retry);
6597 * This operation also signals the server that this client is
6598 * performing "lease moved" recovery. The server can stop
6599 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6600 * is appended to this compound to identify the client ID which is
6601 * performing recovery.
6603 static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6605 struct nfs_server *server = NFS_SERVER(inode);
6606 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6607 struct rpc_clnt *clnt = server->client;
6608 struct nfs4_fsid_present_arg args = {
6609 .fh = NFS_FH(inode),
6610 .clientid = clp->cl_clientid,
6611 .renew = 1, /* append RENEW */
6613 struct nfs4_fsid_present_res res = {
6616 struct rpc_message msg = {
6617 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6622 unsigned long now = jiffies;
6625 res.fh = nfs_alloc_fhandle();
6629 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6630 nfs4_set_sequence_privileged(&args.seq_args);
6631 status = nfs4_call_sync_sequence(clnt, server, &msg,
6632 &args.seq_args, &res.seq_res);
6633 nfs_free_fhandle(res.fh);
6637 do_renew_lease(clp, now);
6641 #ifdef CONFIG_NFS_V4_1
6644 * This operation also signals the server that this client is
6645 * performing "lease moved" recovery. The server can stop asserting
6646 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6647 * this operation is identified in the SEQUENCE operation in this
6650 static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6652 struct nfs_server *server = NFS_SERVER(inode);
6653 struct rpc_clnt *clnt = server->client;
6654 struct nfs4_fsid_present_arg args = {
6655 .fh = NFS_FH(inode),
6657 struct nfs4_fsid_present_res res = {
6659 struct rpc_message msg = {
6660 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6667 res.fh = nfs_alloc_fhandle();
6671 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6672 nfs4_set_sequence_privileged(&args.seq_args);
6673 status = nfs4_call_sync_sequence(clnt, server, &msg,
6674 &args.seq_args, &res.seq_res);
6675 nfs_free_fhandle(res.fh);
6676 if (status == NFS4_OK &&
6677 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6678 status = -NFS4ERR_LEASE_MOVED;
6682 #endif /* CONFIG_NFS_V4_1 */
6685 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6686 * @inode: inode on FSID to check
6687 * @cred: credential to use for this operation
6689 * Server indicates whether the FSID is present, moved, or not
6690 * recognized. This operation is necessary to clear a LEASE_MOVED
6691 * condition for this client ID.
6693 * Returns NFS4_OK if the FSID is present on this server,
6694 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6695 * NFS4ERR code if some error occurred on the server, or a
6696 * negative errno if a local failure occurred.
6698 int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6700 struct nfs_server *server = NFS_SERVER(inode);
6701 struct nfs_client *clp = server->nfs_client;
6702 const struct nfs4_mig_recovery_ops *ops =
6703 clp->cl_mvops->mig_recovery_ops;
6704 struct nfs4_exception exception = { };
6707 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6708 (unsigned long long)server->fsid.major,
6709 (unsigned long long)server->fsid.minor,
6711 nfs_display_fhandle(NFS_FH(inode), __func__);
6714 status = ops->fsid_present(inode, cred);
6715 if (status != -NFS4ERR_DELAY)
6717 nfs4_handle_exception(server, status, &exception);
6718 } while (exception.retry);
6723 * If 'use_integrity' is true and the state managment nfs_client
6724 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
6725 * and the machine credential as per RFC3530bis and RFC5661 Security
6726 * Considerations sections. Otherwise, just use the user cred with the
6727 * filesystem's rpc_client.
6729 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
6732 struct nfs4_secinfo_arg args = {
6733 .dir_fh = NFS_FH(dir),
6736 struct nfs4_secinfo_res res = {
6739 struct rpc_message msg = {
6740 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
6744 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
6745 struct rpc_cred *cred = NULL;
6747 if (use_integrity) {
6748 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
6749 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
6750 msg.rpc_cred = cred;
6753 dprintk("NFS call secinfo %s\n", name->name);
6755 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
6756 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
6758 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
6760 dprintk("NFS reply secinfo: %d\n", status);
6768 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
6769 struct nfs4_secinfo_flavors *flavors)
6771 struct nfs4_exception exception = { };
6774 err = -NFS4ERR_WRONGSEC;
6776 /* try to use integrity protection with machine cred */
6777 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
6778 err = _nfs4_proc_secinfo(dir, name, flavors, true);
6781 * if unable to use integrity protection, or SECINFO with
6782 * integrity protection returns NFS4ERR_WRONGSEC (which is
6783 * disallowed by spec, but exists in deployed servers) use
6784 * the current filesystem's rpc_client and the user cred.
6786 if (err == -NFS4ERR_WRONGSEC)
6787 err = _nfs4_proc_secinfo(dir, name, flavors, false);
6789 trace_nfs4_secinfo(dir, name, err);
6790 err = nfs4_handle_exception(NFS_SERVER(dir), err,
6792 } while (exception.retry);
6796 #ifdef CONFIG_NFS_V4_1
6798 * Check the exchange flags returned by the server for invalid flags, having
6799 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
6802 static int nfs4_check_cl_exchange_flags(u32 flags)
6804 if (flags & ~EXCHGID4_FLAG_MASK_R)
6806 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
6807 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
6809 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
6813 return -NFS4ERR_INVAL;
6817 nfs41_same_server_scope(struct nfs41_server_scope *a,
6818 struct nfs41_server_scope *b)
6820 if (a->server_scope_sz == b->server_scope_sz &&
6821 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
6828 * nfs4_proc_bind_conn_to_session()
6830 * The 4.1 client currently uses the same TCP connection for the
6831 * fore and backchannel.
6833 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
6836 struct nfs41_bind_conn_to_session_args args = {
6838 .dir = NFS4_CDFC4_FORE_OR_BOTH,
6840 struct nfs41_bind_conn_to_session_res res;
6841 struct rpc_message msg = {
6843 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
6849 dprintk("--> %s\n", __func__);
6851 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
6852 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
6853 args.dir = NFS4_CDFC4_FORE;
6855 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
6856 trace_nfs4_bind_conn_to_session(clp, status);
6858 if (memcmp(res.sessionid.data,
6859 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
6860 dprintk("NFS: %s: Session ID mismatch\n", __func__);
6864 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
6865 dprintk("NFS: %s: Unexpected direction from server\n",
6870 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
6871 dprintk("NFS: %s: Server returned RDMA mode = true\n",
6878 dprintk("<-- %s status= %d\n", __func__, status);
6883 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
6884 * and operations we'd like to see to enable certain features in the allow map
6886 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
6887 .how = SP4_MACH_CRED,
6888 .enforce.u.words = {
6889 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6890 1 << (OP_EXCHANGE_ID - 32) |
6891 1 << (OP_CREATE_SESSION - 32) |
6892 1 << (OP_DESTROY_SESSION - 32) |
6893 1 << (OP_DESTROY_CLIENTID - 32)
6896 [0] = 1 << (OP_CLOSE) |
6899 [1] = 1 << (OP_SECINFO - 32) |
6900 1 << (OP_SECINFO_NO_NAME - 32) |
6901 1 << (OP_TEST_STATEID - 32) |
6902 1 << (OP_FREE_STATEID - 32) |
6903 1 << (OP_WRITE - 32)
6908 * Select the state protection mode for client `clp' given the server results
6909 * from exchange_id in `sp'.
6911 * Returns 0 on success, negative errno otherwise.
6913 static int nfs4_sp4_select_mode(struct nfs_client *clp,
6914 struct nfs41_state_protection *sp)
6916 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
6917 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6918 1 << (OP_EXCHANGE_ID - 32) |
6919 1 << (OP_CREATE_SESSION - 32) |
6920 1 << (OP_DESTROY_SESSION - 32) |
6921 1 << (OP_DESTROY_CLIENTID - 32)
6925 if (sp->how == SP4_MACH_CRED) {
6926 /* Print state protect result */
6927 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
6928 for (i = 0; i <= LAST_NFS4_OP; i++) {
6929 if (test_bit(i, sp->enforce.u.longs))
6930 dfprintk(MOUNT, " enforce op %d\n", i);
6931 if (test_bit(i, sp->allow.u.longs))
6932 dfprintk(MOUNT, " allow op %d\n", i);
6935 /* make sure nothing is on enforce list that isn't supported */
6936 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
6937 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
6938 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6944 * Minimal mode - state operations are allowed to use machine
6945 * credential. Note this already happens by default, so the
6946 * client doesn't have to do anything more than the negotiation.
6948 * NOTE: we don't care if EXCHANGE_ID is in the list -
6949 * we're already using the machine cred for exchange_id
6950 * and will never use a different cred.
6952 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
6953 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
6954 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
6955 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
6956 dfprintk(MOUNT, "sp4_mach_cred:\n");
6957 dfprintk(MOUNT, " minimal mode enabled\n");
6958 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
6960 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6964 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
6965 test_bit(OP_LOCKU, sp->allow.u.longs)) {
6966 dfprintk(MOUNT, " cleanup mode enabled\n");
6967 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
6970 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
6971 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
6972 dfprintk(MOUNT, " secinfo mode enabled\n");
6973 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
6976 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
6977 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
6978 dfprintk(MOUNT, " stateid mode enabled\n");
6979 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
6982 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
6983 dfprintk(MOUNT, " write mode enabled\n");
6984 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
6987 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
6988 dfprintk(MOUNT, " commit mode enabled\n");
6989 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
6997 * _nfs4_proc_exchange_id()
6999 * Wrapper for EXCHANGE_ID operation.
7001 static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
7004 nfs4_verifier verifier;
7005 struct nfs41_exchange_id_args args = {
7006 .verifier = &verifier,
7008 #ifdef CONFIG_NFS_V4_1_MIGRATION
7009 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7010 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7011 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7013 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7014 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7017 struct nfs41_exchange_id_res res = {
7021 struct rpc_message msg = {
7022 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
7028 nfs4_init_boot_verifier(clp, &verifier);
7030 status = nfs4_init_uniform_client_string(clp);
7034 dprintk("NFS call exchange_id auth=%s, '%s'\n",
7035 clp->cl_rpcclient->cl_auth->au_ops->au_name,
7038 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7040 if (unlikely(res.server_owner == NULL)) {
7045 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
7047 if (unlikely(res.server_scope == NULL)) {
7049 goto out_server_owner;
7052 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7053 if (unlikely(res.impl_id == NULL)) {
7055 goto out_server_scope;
7060 args.state_protect.how = SP4_NONE;
7064 args.state_protect = nfs4_sp4_mach_cred_request;
7074 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7075 trace_nfs4_exchange_id(clp, status);
7077 status = nfs4_check_cl_exchange_flags(res.flags);
7080 status = nfs4_sp4_select_mode(clp, &res.state_protect);
7083 clp->cl_clientid = res.clientid;
7084 clp->cl_exchange_flags = res.flags;
7085 /* Client ID is not confirmed */
7086 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
7087 clear_bit(NFS4_SESSION_ESTABLISHED,
7088 &clp->cl_session->session_state);
7089 clp->cl_seqid = res.seqid;
7092 kfree(clp->cl_serverowner);
7093 clp->cl_serverowner = res.server_owner;
7094 res.server_owner = NULL;
7096 /* use the most recent implementation id */
7097 kfree(clp->cl_implid);
7098 clp->cl_implid = res.impl_id;
7101 if (clp->cl_serverscope != NULL &&
7102 !nfs41_same_server_scope(clp->cl_serverscope,
7103 res.server_scope)) {
7104 dprintk("%s: server_scope mismatch detected\n",
7106 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
7107 kfree(clp->cl_serverscope);
7108 clp->cl_serverscope = NULL;
7111 if (clp->cl_serverscope == NULL) {
7112 clp->cl_serverscope = res.server_scope;
7113 res.server_scope = NULL;
7120 kfree(res.server_scope);
7122 kfree(res.server_owner);
7124 if (clp->cl_implid != NULL)
7125 dprintk("NFS reply exchange_id: Server Implementation ID: "
7126 "domain: %s, name: %s, date: %llu,%u\n",
7127 clp->cl_implid->domain, clp->cl_implid->name,
7128 clp->cl_implid->date.seconds,
7129 clp->cl_implid->date.nseconds);
7130 dprintk("NFS reply exchange_id: %d\n", status);
7135 * nfs4_proc_exchange_id()
7137 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7139 * Since the clientid has expired, all compounds using sessions
7140 * associated with the stale clientid will be returning
7141 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7142 * be in some phase of session reset.
7144 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7146 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7148 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7151 /* try SP4_MACH_CRED if krb5i/p */
7152 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7153 authflavor == RPC_AUTH_GSS_KRB5P) {
7154 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
7160 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
7163 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7164 struct rpc_cred *cred)
7166 struct rpc_message msg = {
7167 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7173 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7174 trace_nfs4_destroy_clientid(clp, status);
7176 dprintk("NFS: Got error %d from the server %s on "
7177 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7181 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7182 struct rpc_cred *cred)
7187 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7188 ret = _nfs4_proc_destroy_clientid(clp, cred);
7190 case -NFS4ERR_DELAY:
7191 case -NFS4ERR_CLIENTID_BUSY:
7201 int nfs4_destroy_clientid(struct nfs_client *clp)
7203 struct rpc_cred *cred;
7206 if (clp->cl_mvops->minor_version < 1)
7208 if (clp->cl_exchange_flags == 0)
7210 if (clp->cl_preserve_clid)
7212 cred = nfs4_get_clid_cred(clp);
7213 ret = nfs4_proc_destroy_clientid(clp, cred);
7218 case -NFS4ERR_STALE_CLIENTID:
7219 clp->cl_exchange_flags = 0;
7225 struct nfs4_get_lease_time_data {
7226 struct nfs4_get_lease_time_args *args;
7227 struct nfs4_get_lease_time_res *res;
7228 struct nfs_client *clp;
7231 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7234 struct nfs4_get_lease_time_data *data =
7235 (struct nfs4_get_lease_time_data *)calldata;
7237 dprintk("--> %s\n", __func__);
7238 /* just setup sequence, do not trigger session recovery
7239 since we're invoked within one */
7240 nfs41_setup_sequence(data->clp->cl_session,
7241 &data->args->la_seq_args,
7242 &data->res->lr_seq_res,
7244 dprintk("<-- %s\n", __func__);
7248 * Called from nfs4_state_manager thread for session setup, so don't recover
7249 * from sequence operation or clientid errors.
7251 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7253 struct nfs4_get_lease_time_data *data =
7254 (struct nfs4_get_lease_time_data *)calldata;
7256 dprintk("--> %s\n", __func__);
7257 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7259 switch (task->tk_status) {
7260 case -NFS4ERR_DELAY:
7261 case -NFS4ERR_GRACE:
7262 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7263 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7264 task->tk_status = 0;
7266 case -NFS4ERR_RETRY_UNCACHED_REP:
7267 rpc_restart_call_prepare(task);
7270 dprintk("<-- %s\n", __func__);
7273 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
7274 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7275 .rpc_call_done = nfs4_get_lease_time_done,
7278 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7280 struct rpc_task *task;
7281 struct nfs4_get_lease_time_args args;
7282 struct nfs4_get_lease_time_res res = {
7283 .lr_fsinfo = fsinfo,
7285 struct nfs4_get_lease_time_data data = {
7290 struct rpc_message msg = {
7291 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7295 struct rpc_task_setup task_setup = {
7296 .rpc_client = clp->cl_rpcclient,
7297 .rpc_message = &msg,
7298 .callback_ops = &nfs4_get_lease_time_ops,
7299 .callback_data = &data,
7300 .flags = RPC_TASK_TIMEOUT,
7304 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
7305 nfs4_set_sequence_privileged(&args.la_seq_args);
7306 dprintk("--> %s\n", __func__);
7307 task = rpc_run_task(&task_setup);
7310 status = PTR_ERR(task);
7312 status = task->tk_status;
7315 dprintk("<-- %s return %d\n", __func__, status);
7321 * Initialize the values to be used by the client in CREATE_SESSION
7322 * If nfs4_init_session set the fore channel request and response sizes,
7325 * Set the back channel max_resp_sz_cached to zero to force the client to
7326 * always set csa_cachethis to FALSE because the current implementation
7327 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7329 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
7331 unsigned int max_rqst_sz, max_resp_sz;
7333 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7334 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7336 /* Fore channel attributes */
7337 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7338 args->fc_attrs.max_resp_sz = max_resp_sz;
7339 args->fc_attrs.max_ops = NFS4_MAX_OPS;
7340 args->fc_attrs.max_reqs = max_session_slots;
7342 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
7343 "max_ops=%u max_reqs=%u\n",
7345 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
7346 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
7348 /* Back channel attributes */
7349 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
7350 args->bc_attrs.max_resp_sz = PAGE_SIZE;
7351 args->bc_attrs.max_resp_sz_cached = 0;
7352 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
7353 args->bc_attrs.max_reqs = 1;
7355 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7356 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7358 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7359 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7360 args->bc_attrs.max_reqs);
7363 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7364 struct nfs41_create_session_res *res)
7366 struct nfs4_channel_attrs *sent = &args->fc_attrs;
7367 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
7369 if (rcvd->max_resp_sz > sent->max_resp_sz)
7372 * Our requested max_ops is the minimum we need; we're not
7373 * prepared to break up compounds into smaller pieces than that.
7374 * So, no point even trying to continue if the server won't
7377 if (rcvd->max_ops < sent->max_ops)
7379 if (rcvd->max_reqs == 0)
7381 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7382 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
7386 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7387 struct nfs41_create_session_res *res)
7389 struct nfs4_channel_attrs *sent = &args->bc_attrs;
7390 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
7392 if (!(res->flags & SESSION4_BACK_CHAN))
7394 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7396 if (rcvd->max_resp_sz < sent->max_resp_sz)
7398 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7400 /* These would render the backchannel useless: */
7401 if (rcvd->max_ops != sent->max_ops)
7403 if (rcvd->max_reqs != sent->max_reqs)
7409 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
7410 struct nfs41_create_session_res *res)
7414 ret = nfs4_verify_fore_channel_attrs(args, res);
7417 return nfs4_verify_back_channel_attrs(args, res);
7420 static void nfs4_update_session(struct nfs4_session *session,
7421 struct nfs41_create_session_res *res)
7423 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
7424 /* Mark client id and session as being confirmed */
7425 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7426 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
7427 session->flags = res->flags;
7428 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
7429 if (res->flags & SESSION4_BACK_CHAN)
7430 memcpy(&session->bc_attrs, &res->bc_attrs,
7431 sizeof(session->bc_attrs));
7434 static int _nfs4_proc_create_session(struct nfs_client *clp,
7435 struct rpc_cred *cred)
7437 struct nfs4_session *session = clp->cl_session;
7438 struct nfs41_create_session_args args = {
7440 .clientid = clp->cl_clientid,
7441 .seqid = clp->cl_seqid,
7442 .cb_program = NFS4_CALLBACK,
7444 struct nfs41_create_session_res res;
7446 struct rpc_message msg = {
7447 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7454 nfs4_init_channel_attrs(&args);
7455 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
7457 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7458 trace_nfs4_create_session(clp, status);
7461 /* Verify the session's negotiated channel_attrs values */
7462 status = nfs4_verify_channel_attrs(&args, &res);
7463 /* Increment the clientid slot sequence id */
7464 if (clp->cl_seqid == res.seqid)
7468 nfs4_update_session(session, &res);
7475 * Issues a CREATE_SESSION operation to the server.
7476 * It is the responsibility of the caller to verify the session is
7477 * expired before calling this routine.
7479 int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
7483 struct nfs4_session *session = clp->cl_session;
7485 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7487 status = _nfs4_proc_create_session(clp, cred);
7491 /* Init or reset the session slot tables */
7492 status = nfs4_setup_session_slot_tables(session);
7493 dprintk("slot table setup returned %d\n", status);
7497 ptr = (unsigned *)&session->sess_id.data[0];
7498 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7499 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
7501 dprintk("<-- %s\n", __func__);
7506 * Issue the over-the-wire RPC DESTROY_SESSION.
7507 * The caller must serialize access to this routine.
7509 int nfs4_proc_destroy_session(struct nfs4_session *session,
7510 struct rpc_cred *cred)
7512 struct rpc_message msg = {
7513 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
7514 .rpc_argp = session,
7519 dprintk("--> nfs4_proc_destroy_session\n");
7521 /* session is still being setup */
7522 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
7525 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7526 trace_nfs4_destroy_session(session->clp, status);
7529 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
7530 "Session has been destroyed regardless...\n", status);
7532 dprintk("<-- nfs4_proc_destroy_session\n");
7537 * Renew the cl_session lease.
7539 struct nfs4_sequence_data {
7540 struct nfs_client *clp;
7541 struct nfs4_sequence_args args;
7542 struct nfs4_sequence_res res;
7545 static void nfs41_sequence_release(void *data)
7547 struct nfs4_sequence_data *calldata = data;
7548 struct nfs_client *clp = calldata->clp;
7550 if (atomic_read(&clp->cl_count) > 1)
7551 nfs4_schedule_state_renewal(clp);
7552 nfs_put_client(clp);
7556 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7558 switch(task->tk_status) {
7559 case -NFS4ERR_DELAY:
7560 rpc_delay(task, NFS4_POLL_RETRY_MAX);
7563 nfs4_schedule_lease_recovery(clp);
7568 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
7570 struct nfs4_sequence_data *calldata = data;
7571 struct nfs_client *clp = calldata->clp;
7573 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
7576 trace_nfs4_sequence(clp, task->tk_status);
7577 if (task->tk_status < 0) {
7578 dprintk("%s ERROR %d\n", __func__, task->tk_status);
7579 if (atomic_read(&clp->cl_count) == 1)
7582 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
7583 rpc_restart_call_prepare(task);
7587 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
7589 dprintk("<-- %s\n", __func__);
7592 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
7594 struct nfs4_sequence_data *calldata = data;
7595 struct nfs_client *clp = calldata->clp;
7596 struct nfs4_sequence_args *args;
7597 struct nfs4_sequence_res *res;
7599 args = task->tk_msg.rpc_argp;
7600 res = task->tk_msg.rpc_resp;
7602 nfs41_setup_sequence(clp->cl_session, args, res, task);
7605 static const struct rpc_call_ops nfs41_sequence_ops = {
7606 .rpc_call_done = nfs41_sequence_call_done,
7607 .rpc_call_prepare = nfs41_sequence_prepare,
7608 .rpc_release = nfs41_sequence_release,
7611 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
7612 struct rpc_cred *cred,
7615 struct nfs4_sequence_data *calldata;
7616 struct rpc_message msg = {
7617 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
7620 struct rpc_task_setup task_setup_data = {
7621 .rpc_client = clp->cl_rpcclient,
7622 .rpc_message = &msg,
7623 .callback_ops = &nfs41_sequence_ops,
7624 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7627 if (!atomic_inc_not_zero(&clp->cl_count))
7628 return ERR_PTR(-EIO);
7629 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7630 if (calldata == NULL) {
7631 nfs_put_client(clp);
7632 return ERR_PTR(-ENOMEM);
7634 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
7636 nfs4_set_sequence_privileged(&calldata->args);
7637 msg.rpc_argp = &calldata->args;
7638 msg.rpc_resp = &calldata->res;
7639 calldata->clp = clp;
7640 task_setup_data.callback_data = calldata;
7642 return rpc_run_task(&task_setup_data);
7645 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
7647 struct rpc_task *task;
7650 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
7652 task = _nfs41_proc_sequence(clp, cred, false);
7654 ret = PTR_ERR(task);
7656 rpc_put_task_async(task);
7657 dprintk("<-- %s status=%d\n", __func__, ret);
7661 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
7663 struct rpc_task *task;
7666 task = _nfs41_proc_sequence(clp, cred, true);
7668 ret = PTR_ERR(task);
7671 ret = rpc_wait_for_completion_task(task);
7673 ret = task->tk_status;
7676 dprintk("<-- %s status=%d\n", __func__, ret);
7680 struct nfs4_reclaim_complete_data {
7681 struct nfs_client *clp;
7682 struct nfs41_reclaim_complete_args arg;
7683 struct nfs41_reclaim_complete_res res;
7686 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
7688 struct nfs4_reclaim_complete_data *calldata = data;
7690 nfs41_setup_sequence(calldata->clp->cl_session,
7691 &calldata->arg.seq_args,
7692 &calldata->res.seq_res,
7696 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7698 switch(task->tk_status) {
7700 case -NFS4ERR_COMPLETE_ALREADY:
7701 case -NFS4ERR_WRONG_CRED: /* What to do here? */
7703 case -NFS4ERR_DELAY:
7704 rpc_delay(task, NFS4_POLL_RETRY_MAX);
7706 case -NFS4ERR_RETRY_UNCACHED_REP:
7709 nfs4_schedule_lease_recovery(clp);
7714 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
7716 struct nfs4_reclaim_complete_data *calldata = data;
7717 struct nfs_client *clp = calldata->clp;
7718 struct nfs4_sequence_res *res = &calldata->res.seq_res;
7720 dprintk("--> %s\n", __func__);
7721 if (!nfs41_sequence_done(task, res))
7724 trace_nfs4_reclaim_complete(clp, task->tk_status);
7725 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
7726 rpc_restart_call_prepare(task);
7729 dprintk("<-- %s\n", __func__);
7732 static void nfs4_free_reclaim_complete_data(void *data)
7734 struct nfs4_reclaim_complete_data *calldata = data;
7739 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
7740 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
7741 .rpc_call_done = nfs4_reclaim_complete_done,
7742 .rpc_release = nfs4_free_reclaim_complete_data,
7746 * Issue a global reclaim complete.
7748 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
7749 struct rpc_cred *cred)
7751 struct nfs4_reclaim_complete_data *calldata;
7752 struct rpc_task *task;
7753 struct rpc_message msg = {
7754 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
7757 struct rpc_task_setup task_setup_data = {
7758 .rpc_client = clp->cl_rpcclient,
7759 .rpc_message = &msg,
7760 .callback_ops = &nfs4_reclaim_complete_call_ops,
7761 .flags = RPC_TASK_ASYNC,
7763 int status = -ENOMEM;
7765 dprintk("--> %s\n", __func__);
7766 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7767 if (calldata == NULL)
7769 calldata->clp = clp;
7770 calldata->arg.one_fs = 0;
7772 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
7773 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
7774 msg.rpc_argp = &calldata->arg;
7775 msg.rpc_resp = &calldata->res;
7776 task_setup_data.callback_data = calldata;
7777 task = rpc_run_task(&task_setup_data);
7779 status = PTR_ERR(task);
7782 status = nfs4_wait_for_completion_rpc_task(task);
7784 status = task->tk_status;
7788 dprintk("<-- %s status=%d\n", __func__, status);
7793 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
7795 struct nfs4_layoutget *lgp = calldata;
7796 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
7797 struct nfs4_session *session = nfs4_get_session(server);
7799 dprintk("--> %s\n", __func__);
7800 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
7801 * right now covering the LAYOUTGET we are about to send.
7802 * However, that is not so catastrophic, and there seems
7803 * to be no way to prevent it completely.
7805 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
7806 &lgp->res.seq_res, task))
7808 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
7809 NFS_I(lgp->args.inode)->layout,
7811 lgp->args.ctx->state)) {
7812 rpc_exit(task, NFS4_OK);
7816 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
7818 struct nfs4_layoutget *lgp = calldata;
7819 struct inode *inode = lgp->args.inode;
7820 struct nfs_server *server = NFS_SERVER(inode);
7821 struct pnfs_layout_hdr *lo;
7822 struct nfs4_state *state = NULL;
7823 unsigned long timeo, now, giveup;
7825 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
7827 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
7830 switch (task->tk_status) {
7834 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
7835 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
7837 case -NFS4ERR_BADLAYOUT:
7840 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
7841 * (or clients) writing to the same RAID stripe except when
7842 * the minlength argument is 0 (see RFC5661 section 18.43.3).
7844 case -NFS4ERR_LAYOUTTRYLATER:
7845 if (lgp->args.minlength == 0)
7848 * NFS4ERR_RECALLCONFLICT is when conflict with self (must recall
7849 * existing layout before getting a new one).
7851 case -NFS4ERR_RECALLCONFLICT:
7852 timeo = rpc_get_timeout(task->tk_client);
7853 giveup = lgp->args.timestamp + timeo;
7855 if (time_after(giveup, now)) {
7856 unsigned long delay;
7859 * - Not less then NFS4_POLL_RETRY_MIN.
7860 * - One last time a jiffie before we give up
7861 * - exponential backoff (time_now minus start_attempt)
7863 delay = max_t(unsigned long, NFS4_POLL_RETRY_MIN,
7864 min((giveup - now - 1),
7865 now - lgp->args.timestamp));
7867 dprintk("%s: NFS4ERR_RECALLCONFLICT waiting %lu\n",
7869 rpc_delay(task, delay);
7870 /* Do not call nfs4_async_handle_error() */
7874 case -NFS4ERR_EXPIRED:
7875 case -NFS4ERR_BAD_STATEID:
7876 spin_lock(&inode->i_lock);
7877 if (nfs4_stateid_match(&lgp->args.stateid,
7878 &lgp->args.ctx->state->stateid)) {
7879 spin_unlock(&inode->i_lock);
7880 /* If the open stateid was bad, then recover it. */
7881 state = lgp->args.ctx->state;
7884 lo = NFS_I(inode)->layout;
7885 if (lo && nfs4_stateid_match(&lgp->args.stateid,
7886 &lo->plh_stateid)) {
7890 * Mark the bad layout state as invalid, then retry
7891 * with the current stateid.
7893 set_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags);
7894 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
7895 spin_unlock(&inode->i_lock);
7896 pnfs_free_lseg_list(&head);
7898 spin_unlock(&inode->i_lock);
7901 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN)
7904 dprintk("<-- %s\n", __func__);
7907 task->tk_status = 0;
7908 rpc_restart_call_prepare(task);
7911 task->tk_status = -EOVERFLOW;
7915 static size_t max_response_pages(struct nfs_server *server)
7917 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
7918 return nfs_page_array_len(0, max_resp_sz);
7921 static void nfs4_free_pages(struct page **pages, size_t size)
7928 for (i = 0; i < size; i++) {
7931 __free_page(pages[i]);
7936 static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
7938 struct page **pages;
7941 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
7943 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
7947 for (i = 0; i < size; i++) {
7948 pages[i] = alloc_page(gfp_flags);
7950 dprintk("%s: failed to allocate page\n", __func__);
7951 nfs4_free_pages(pages, size);
7959 static void nfs4_layoutget_release(void *calldata)
7961 struct nfs4_layoutget *lgp = calldata;
7962 struct inode *inode = lgp->args.inode;
7963 struct nfs_server *server = NFS_SERVER(inode);
7964 size_t max_pages = max_response_pages(server);
7966 dprintk("--> %s\n", __func__);
7967 nfs4_free_pages(lgp->args.layout.pages, max_pages);
7968 pnfs_put_layout_hdr(NFS_I(inode)->layout);
7969 put_nfs_open_context(lgp->args.ctx);
7971 dprintk("<-- %s\n", __func__);
7974 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
7975 .rpc_call_prepare = nfs4_layoutget_prepare,
7976 .rpc_call_done = nfs4_layoutget_done,
7977 .rpc_release = nfs4_layoutget_release,
7980 struct pnfs_layout_segment *
7981 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
7983 struct inode *inode = lgp->args.inode;
7984 struct nfs_server *server = NFS_SERVER(inode);
7985 size_t max_pages = max_response_pages(server);
7986 struct rpc_task *task;
7987 struct rpc_message msg = {
7988 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
7989 .rpc_argp = &lgp->args,
7990 .rpc_resp = &lgp->res,
7991 .rpc_cred = lgp->cred,
7993 struct rpc_task_setup task_setup_data = {
7994 .rpc_client = server->client,
7995 .rpc_message = &msg,
7996 .callback_ops = &nfs4_layoutget_call_ops,
7997 .callback_data = lgp,
7998 .flags = RPC_TASK_ASYNC,
8000 struct pnfs_layout_segment *lseg = NULL;
8003 dprintk("--> %s\n", __func__);
8005 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8006 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8008 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8009 if (!lgp->args.layout.pages) {
8010 nfs4_layoutget_release(lgp);
8011 return ERR_PTR(-ENOMEM);
8013 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8014 lgp->args.timestamp = jiffies;
8016 lgp->res.layoutp = &lgp->args.layout;
8017 lgp->res.seq_res.sr_slot = NULL;
8018 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
8020 task = rpc_run_task(&task_setup_data);
8022 return ERR_CAST(task);
8023 status = nfs4_wait_for_completion_rpc_task(task);
8025 status = task->tk_status;
8026 trace_nfs4_layoutget(lgp->args.ctx,
8030 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8031 if (status == 0 && lgp->res.layoutp->len)
8032 lseg = pnfs_layout_process(lgp);
8034 dprintk("<-- %s status=%d\n", __func__, status);
8036 return ERR_PTR(status);
8041 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8043 struct nfs4_layoutreturn *lrp = calldata;
8045 dprintk("--> %s\n", __func__);
8046 nfs41_setup_sequence(lrp->clp->cl_session,
8047 &lrp->args.seq_args,
8052 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8054 struct nfs4_layoutreturn *lrp = calldata;
8055 struct nfs_server *server;
8057 dprintk("--> %s\n", __func__);
8059 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
8062 server = NFS_SERVER(lrp->args.inode);
8063 switch (task->tk_status) {
8065 task->tk_status = 0;
8068 case -NFS4ERR_DELAY:
8069 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
8071 rpc_restart_call_prepare(task);
8074 dprintk("<-- %s\n", __func__);
8077 static void nfs4_layoutreturn_release(void *calldata)
8079 struct nfs4_layoutreturn *lrp = calldata;
8080 struct pnfs_layout_hdr *lo = lrp->args.layout;
8083 dprintk("--> %s\n", __func__);
8084 spin_lock(&lo->plh_inode->i_lock);
8085 if (lrp->res.lrs_present)
8086 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
8087 pnfs_mark_matching_lsegs_invalid(lo, &freeme, &lrp->args.range);
8088 pnfs_clear_layoutreturn_waitbit(lo);
8089 lo->plh_block_lgets--;
8090 spin_unlock(&lo->plh_inode->i_lock);
8091 pnfs_free_lseg_list(&freeme);
8092 pnfs_put_layout_hdr(lrp->args.layout);
8093 nfs_iput_and_deactive(lrp->inode);
8095 dprintk("<-- %s\n", __func__);
8098 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8099 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8100 .rpc_call_done = nfs4_layoutreturn_done,
8101 .rpc_release = nfs4_layoutreturn_release,
8104 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
8106 struct rpc_task *task;
8107 struct rpc_message msg = {
8108 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8109 .rpc_argp = &lrp->args,
8110 .rpc_resp = &lrp->res,
8111 .rpc_cred = lrp->cred,
8113 struct rpc_task_setup task_setup_data = {
8114 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
8115 .rpc_message = &msg,
8116 .callback_ops = &nfs4_layoutreturn_call_ops,
8117 .callback_data = lrp,
8121 dprintk("--> %s\n", __func__);
8123 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8125 nfs4_layoutreturn_release(lrp);
8128 task_setup_data.flags |= RPC_TASK_ASYNC;
8130 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
8131 task = rpc_run_task(&task_setup_data);
8133 return PTR_ERR(task);
8135 status = task->tk_status;
8136 trace_nfs4_layoutreturn(lrp->args.inode, status);
8137 dprintk("<-- %s status=%d\n", __func__, status);
8143 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
8144 struct pnfs_device *pdev,
8145 struct rpc_cred *cred)
8147 struct nfs4_getdeviceinfo_args args = {
8149 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8150 NOTIFY_DEVICEID4_DELETE,
8152 struct nfs4_getdeviceinfo_res res = {
8155 struct rpc_message msg = {
8156 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8163 dprintk("--> %s\n", __func__);
8164 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
8165 if (res.notification & ~args.notify_types)
8166 dprintk("%s: unsupported notification\n", __func__);
8167 if (res.notification != args.notify_types)
8170 dprintk("<-- %s status=%d\n", __func__, status);
8175 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8176 struct pnfs_device *pdev,
8177 struct rpc_cred *cred)
8179 struct nfs4_exception exception = { };
8183 err = nfs4_handle_exception(server,
8184 _nfs4_proc_getdeviceinfo(server, pdev, cred),
8186 } while (exception.retry);
8189 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8191 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8193 struct nfs4_layoutcommit_data *data = calldata;
8194 struct nfs_server *server = NFS_SERVER(data->args.inode);
8195 struct nfs4_session *session = nfs4_get_session(server);
8197 nfs41_setup_sequence(session,
8198 &data->args.seq_args,
8204 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8206 struct nfs4_layoutcommit_data *data = calldata;
8207 struct nfs_server *server = NFS_SERVER(data->args.inode);
8209 if (!nfs41_sequence_done(task, &data->res.seq_res))
8212 switch (task->tk_status) { /* Just ignore these failures */
8213 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8214 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8215 case -NFS4ERR_BADLAYOUT: /* no layout */
8216 case -NFS4ERR_GRACE: /* loca_recalim always false */
8217 task->tk_status = 0;
8221 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
8222 rpc_restart_call_prepare(task);
8228 static void nfs4_layoutcommit_release(void *calldata)
8230 struct nfs4_layoutcommit_data *data = calldata;
8232 pnfs_cleanup_layoutcommit(data);
8233 nfs_post_op_update_inode_force_wcc(data->args.inode,
8235 put_rpccred(data->cred);
8236 nfs_iput_and_deactive(data->inode);
8240 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8241 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8242 .rpc_call_done = nfs4_layoutcommit_done,
8243 .rpc_release = nfs4_layoutcommit_release,
8247 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
8249 struct rpc_message msg = {
8250 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8251 .rpc_argp = &data->args,
8252 .rpc_resp = &data->res,
8253 .rpc_cred = data->cred,
8255 struct rpc_task_setup task_setup_data = {
8256 .task = &data->task,
8257 .rpc_client = NFS_CLIENT(data->args.inode),
8258 .rpc_message = &msg,
8259 .callback_ops = &nfs4_layoutcommit_ops,
8260 .callback_data = data,
8262 struct rpc_task *task;
8265 dprintk("NFS: initiating layoutcommit call. sync %d "
8266 "lbw: %llu inode %lu\n", sync,
8267 data->args.lastbytewritten,
8268 data->args.inode->i_ino);
8271 data->inode = nfs_igrab_and_active(data->args.inode);
8272 if (data->inode == NULL) {
8273 nfs4_layoutcommit_release(data);
8276 task_setup_data.flags = RPC_TASK_ASYNC;
8278 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
8279 task = rpc_run_task(&task_setup_data);
8281 return PTR_ERR(task);
8283 status = task->tk_status;
8284 trace_nfs4_layoutcommit(data->args.inode, status);
8285 dprintk("%s: status %d\n", __func__, status);
8291 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8292 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8295 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8296 struct nfs_fsinfo *info,
8297 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
8299 struct nfs41_secinfo_no_name_args args = {
8300 .style = SECINFO_STYLE_CURRENT_FH,
8302 struct nfs4_secinfo_res res = {
8305 struct rpc_message msg = {
8306 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8310 struct rpc_clnt *clnt = server->client;
8311 struct rpc_cred *cred = NULL;
8314 if (use_integrity) {
8315 clnt = server->nfs_client->cl_rpcclient;
8316 cred = nfs4_get_clid_cred(server->nfs_client);
8317 msg.rpc_cred = cred;
8320 dprintk("--> %s\n", __func__);
8321 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8323 dprintk("<-- %s status=%d\n", __func__, status);
8332 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8333 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8335 struct nfs4_exception exception = { };
8338 /* first try using integrity protection */
8339 err = -NFS4ERR_WRONGSEC;
8341 /* try to use integrity protection with machine cred */
8342 if (_nfs4_is_integrity_protected(server->nfs_client))
8343 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8347 * if unable to use integrity protection, or SECINFO with
8348 * integrity protection returns NFS4ERR_WRONGSEC (which is
8349 * disallowed by spec, but exists in deployed servers) use
8350 * the current filesystem's rpc_client and the user cred.
8352 if (err == -NFS4ERR_WRONGSEC)
8353 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8358 case -NFS4ERR_WRONGSEC:
8362 err = nfs4_handle_exception(server, err, &exception);
8364 } while (exception.retry);
8370 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8371 struct nfs_fsinfo *info)
8375 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
8376 struct nfs4_secinfo_flavors *flavors;
8377 struct nfs4_secinfo4 *secinfo;
8380 page = alloc_page(GFP_KERNEL);
8386 flavors = page_address(page);
8387 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8390 * Fall back on "guess and check" method if
8391 * the server doesn't support SECINFO_NO_NAME
8393 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
8394 err = nfs4_find_root_sec(server, fhandle, info);
8400 for (i = 0; i < flavors->num_flavors; i++) {
8401 secinfo = &flavors->flavors[i];
8403 switch (secinfo->flavor) {
8407 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8408 &secinfo->flavor_info);
8411 flavor = RPC_AUTH_MAXFLAVOR;
8415 if (!nfs_auth_info_match(&server->auth_info, flavor))
8416 flavor = RPC_AUTH_MAXFLAVOR;
8418 if (flavor != RPC_AUTH_MAXFLAVOR) {
8419 err = nfs4_lookup_root_sec(server, fhandle,
8426 if (flavor == RPC_AUTH_MAXFLAVOR)
8437 static int _nfs41_test_stateid(struct nfs_server *server,
8438 nfs4_stateid *stateid,
8439 struct rpc_cred *cred)
8442 struct nfs41_test_stateid_args args = {
8445 struct nfs41_test_stateid_res res;
8446 struct rpc_message msg = {
8447 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8452 struct rpc_clnt *rpc_client = server->client;
8454 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8457 dprintk("NFS call test_stateid %p\n", stateid);
8458 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
8459 nfs4_set_sequence_privileged(&args.seq_args);
8460 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
8461 &args.seq_args, &res.seq_res);
8462 if (status != NFS_OK) {
8463 dprintk("NFS reply test_stateid: failed, %d\n", status);
8466 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
8471 * nfs41_test_stateid - perform a TEST_STATEID operation
8473 * @server: server / transport on which to perform the operation
8474 * @stateid: state ID to test
8477 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8478 * Otherwise a negative NFS4ERR value is returned if the operation
8479 * failed or the state ID is not currently valid.
8481 static int nfs41_test_stateid(struct nfs_server *server,
8482 nfs4_stateid *stateid,
8483 struct rpc_cred *cred)
8485 struct nfs4_exception exception = { };
8488 err = _nfs41_test_stateid(server, stateid, cred);
8489 if (err != -NFS4ERR_DELAY)
8491 nfs4_handle_exception(server, err, &exception);
8492 } while (exception.retry);
8496 struct nfs_free_stateid_data {
8497 struct nfs_server *server;
8498 struct nfs41_free_stateid_args args;
8499 struct nfs41_free_stateid_res res;
8502 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
8504 struct nfs_free_stateid_data *data = calldata;
8505 nfs41_setup_sequence(nfs4_get_session(data->server),
8506 &data->args.seq_args,
8511 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
8513 struct nfs_free_stateid_data *data = calldata;
8515 nfs41_sequence_done(task, &data->res.seq_res);
8517 switch (task->tk_status) {
8518 case -NFS4ERR_DELAY:
8519 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
8520 rpc_restart_call_prepare(task);
8524 static void nfs41_free_stateid_release(void *calldata)
8529 static const struct rpc_call_ops nfs41_free_stateid_ops = {
8530 .rpc_call_prepare = nfs41_free_stateid_prepare,
8531 .rpc_call_done = nfs41_free_stateid_done,
8532 .rpc_release = nfs41_free_stateid_release,
8535 static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
8536 nfs4_stateid *stateid,
8537 struct rpc_cred *cred,
8540 struct rpc_message msg = {
8541 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
8544 struct rpc_task_setup task_setup = {
8545 .rpc_client = server->client,
8546 .rpc_message = &msg,
8547 .callback_ops = &nfs41_free_stateid_ops,
8548 .flags = RPC_TASK_ASYNC,
8550 struct nfs_free_stateid_data *data;
8552 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8553 &task_setup.rpc_client, &msg);
8555 dprintk("NFS call free_stateid %p\n", stateid);
8556 data = kmalloc(sizeof(*data), GFP_NOFS);
8558 return ERR_PTR(-ENOMEM);
8559 data->server = server;
8560 nfs4_stateid_copy(&data->args.stateid, stateid);
8562 task_setup.callback_data = data;
8564 msg.rpc_argp = &data->args;
8565 msg.rpc_resp = &data->res;
8566 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
8568 nfs4_set_sequence_privileged(&data->args.seq_args);
8570 return rpc_run_task(&task_setup);
8574 * nfs41_free_stateid - perform a FREE_STATEID operation
8576 * @server: server / transport on which to perform the operation
8577 * @stateid: state ID to release
8580 * Returns NFS_OK if the server freed "stateid". Otherwise a
8581 * negative NFS4ERR value is returned.
8583 static int nfs41_free_stateid(struct nfs_server *server,
8584 nfs4_stateid *stateid,
8585 struct rpc_cred *cred)
8587 struct rpc_task *task;
8590 task = _nfs41_free_stateid(server, stateid, cred, true);
8592 return PTR_ERR(task);
8593 ret = rpc_wait_for_completion_task(task);
8595 ret = task->tk_status;
8601 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
8603 struct rpc_task *task;
8604 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
8606 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
8607 nfs4_free_lock_state(server, lsp);
8613 static bool nfs41_match_stateid(const nfs4_stateid *s1,
8614 const nfs4_stateid *s2)
8616 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
8619 if (s1->seqid == s2->seqid)
8621 if (s1->seqid == 0 || s2->seqid == 0)
8627 #endif /* CONFIG_NFS_V4_1 */
8629 static bool nfs4_match_stateid(const nfs4_stateid *s1,
8630 const nfs4_stateid *s2)
8632 return nfs4_stateid_match(s1, s2);
8636 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
8637 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
8638 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
8639 .recover_open = nfs4_open_reclaim,
8640 .recover_lock = nfs4_lock_reclaim,
8641 .establish_clid = nfs4_init_clientid,
8642 .detect_trunking = nfs40_discover_server_trunking,
8645 #if defined(CONFIG_NFS_V4_1)
8646 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
8647 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
8648 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
8649 .recover_open = nfs4_open_reclaim,
8650 .recover_lock = nfs4_lock_reclaim,
8651 .establish_clid = nfs41_init_clientid,
8652 .reclaim_complete = nfs41_proc_reclaim_complete,
8653 .detect_trunking = nfs41_discover_server_trunking,
8655 #endif /* CONFIG_NFS_V4_1 */
8657 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
8658 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
8659 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
8660 .recover_open = nfs40_open_expired,
8661 .recover_lock = nfs4_lock_expired,
8662 .establish_clid = nfs4_init_clientid,
8665 #if defined(CONFIG_NFS_V4_1)
8666 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
8667 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
8668 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
8669 .recover_open = nfs41_open_expired,
8670 .recover_lock = nfs41_lock_expired,
8671 .establish_clid = nfs41_init_clientid,
8673 #endif /* CONFIG_NFS_V4_1 */
8675 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
8676 .sched_state_renewal = nfs4_proc_async_renew,
8677 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
8678 .renew_lease = nfs4_proc_renew,
8681 #if defined(CONFIG_NFS_V4_1)
8682 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
8683 .sched_state_renewal = nfs41_proc_async_sequence,
8684 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
8685 .renew_lease = nfs4_proc_sequence,
8689 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
8690 .get_locations = _nfs40_proc_get_locations,
8691 .fsid_present = _nfs40_proc_fsid_present,
8694 #if defined(CONFIG_NFS_V4_1)
8695 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
8696 .get_locations = _nfs41_proc_get_locations,
8697 .fsid_present = _nfs41_proc_fsid_present,
8699 #endif /* CONFIG_NFS_V4_1 */
8701 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
8703 .init_caps = NFS_CAP_READDIRPLUS
8704 | NFS_CAP_ATOMIC_OPEN
8705 | NFS_CAP_POSIX_LOCK,
8706 .init_client = nfs40_init_client,
8707 .shutdown_client = nfs40_shutdown_client,
8708 .match_stateid = nfs4_match_stateid,
8709 .find_root_sec = nfs4_find_root_sec,
8710 .free_lock_state = nfs4_release_lockowner,
8711 .alloc_seqid = nfs_alloc_seqid,
8712 .call_sync_ops = &nfs40_call_sync_ops,
8713 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
8714 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
8715 .state_renewal_ops = &nfs40_state_renewal_ops,
8716 .mig_recovery_ops = &nfs40_mig_recovery_ops,
8719 #if defined(CONFIG_NFS_V4_1)
8720 static struct nfs_seqid *
8721 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
8726 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
8728 .init_caps = NFS_CAP_READDIRPLUS
8729 | NFS_CAP_ATOMIC_OPEN
8730 | NFS_CAP_POSIX_LOCK
8731 | NFS_CAP_STATEID_NFSV41
8732 | NFS_CAP_ATOMIC_OPEN_V1,
8733 .init_client = nfs41_init_client,
8734 .shutdown_client = nfs41_shutdown_client,
8735 .match_stateid = nfs41_match_stateid,
8736 .find_root_sec = nfs41_find_root_sec,
8737 .free_lock_state = nfs41_free_lock_state,
8738 .alloc_seqid = nfs_alloc_no_seqid,
8739 .call_sync_ops = &nfs41_call_sync_ops,
8740 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8741 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8742 .state_renewal_ops = &nfs41_state_renewal_ops,
8743 .mig_recovery_ops = &nfs41_mig_recovery_ops,
8747 #if defined(CONFIG_NFS_V4_2)
8748 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
8750 .init_caps = NFS_CAP_READDIRPLUS
8751 | NFS_CAP_ATOMIC_OPEN
8752 | NFS_CAP_POSIX_LOCK
8753 | NFS_CAP_STATEID_NFSV41
8754 | NFS_CAP_ATOMIC_OPEN_V1
8756 | NFS_CAP_DEALLOCATE
8758 | NFS_CAP_LAYOUTSTATS,
8759 .init_client = nfs41_init_client,
8760 .shutdown_client = nfs41_shutdown_client,
8761 .match_stateid = nfs41_match_stateid,
8762 .find_root_sec = nfs41_find_root_sec,
8763 .free_lock_state = nfs41_free_lock_state,
8764 .call_sync_ops = &nfs41_call_sync_ops,
8765 .alloc_seqid = nfs_alloc_no_seqid,
8766 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8767 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8768 .state_renewal_ops = &nfs41_state_renewal_ops,
8769 .mig_recovery_ops = &nfs41_mig_recovery_ops,
8773 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
8774 [0] = &nfs_v4_0_minor_ops,
8775 #if defined(CONFIG_NFS_V4_1)
8776 [1] = &nfs_v4_1_minor_ops,
8778 #if defined(CONFIG_NFS_V4_2)
8779 [2] = &nfs_v4_2_minor_ops,
8783 static const struct inode_operations nfs4_dir_inode_operations = {
8784 .create = nfs_create,
8785 .lookup = nfs_lookup,
8786 .atomic_open = nfs_atomic_open,
8788 .unlink = nfs_unlink,
8789 .symlink = nfs_symlink,
8793 .rename = nfs_rename,
8794 .permission = nfs_permission,
8795 .getattr = nfs_getattr,
8796 .setattr = nfs_setattr,
8797 .getxattr = generic_getxattr,
8798 .setxattr = generic_setxattr,
8799 .listxattr = generic_listxattr,
8800 .removexattr = generic_removexattr,
8803 static const struct inode_operations nfs4_file_inode_operations = {
8804 .permission = nfs_permission,
8805 .getattr = nfs_getattr,
8806 .setattr = nfs_setattr,
8807 .getxattr = generic_getxattr,
8808 .setxattr = generic_setxattr,
8809 .listxattr = generic_listxattr,
8810 .removexattr = generic_removexattr,
8813 const struct nfs_rpc_ops nfs_v4_clientops = {
8814 .version = 4, /* protocol version */
8815 .dentry_ops = &nfs4_dentry_operations,
8816 .dir_inode_ops = &nfs4_dir_inode_operations,
8817 .file_inode_ops = &nfs4_file_inode_operations,
8818 .file_ops = &nfs4_file_operations,
8819 .getroot = nfs4_proc_get_root,
8820 .submount = nfs4_submount,
8821 .try_mount = nfs4_try_mount,
8822 .getattr = nfs4_proc_getattr,
8823 .setattr = nfs4_proc_setattr,
8824 .lookup = nfs4_proc_lookup,
8825 .access = nfs4_proc_access,
8826 .readlink = nfs4_proc_readlink,
8827 .create = nfs4_proc_create,
8828 .remove = nfs4_proc_remove,
8829 .unlink_setup = nfs4_proc_unlink_setup,
8830 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
8831 .unlink_done = nfs4_proc_unlink_done,
8832 .rename_setup = nfs4_proc_rename_setup,
8833 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
8834 .rename_done = nfs4_proc_rename_done,
8835 .link = nfs4_proc_link,
8836 .symlink = nfs4_proc_symlink,
8837 .mkdir = nfs4_proc_mkdir,
8838 .rmdir = nfs4_proc_remove,
8839 .readdir = nfs4_proc_readdir,
8840 .mknod = nfs4_proc_mknod,
8841 .statfs = nfs4_proc_statfs,
8842 .fsinfo = nfs4_proc_fsinfo,
8843 .pathconf = nfs4_proc_pathconf,
8844 .set_capabilities = nfs4_server_capabilities,
8845 .decode_dirent = nfs4_decode_dirent,
8846 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
8847 .read_setup = nfs4_proc_read_setup,
8848 .read_done = nfs4_read_done,
8849 .write_setup = nfs4_proc_write_setup,
8850 .write_done = nfs4_write_done,
8851 .commit_setup = nfs4_proc_commit_setup,
8852 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
8853 .commit_done = nfs4_commit_done,
8854 .lock = nfs4_proc_lock,
8855 .clear_acl_cache = nfs4_zap_acl_attr,
8856 .close_context = nfs4_close_context,
8857 .open_context = nfs4_atomic_open,
8858 .have_delegation = nfs4_have_delegation,
8859 .return_delegation = nfs4_inode_return_delegation,
8860 .alloc_client = nfs4_alloc_client,
8861 .init_client = nfs4_init_client,
8862 .free_client = nfs4_free_client,
8863 .create_server = nfs4_create_server,
8864 .clone_server = nfs_clone_server,
8867 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
8868 .prefix = XATTR_NAME_NFSV4_ACL,
8869 .list = nfs4_xattr_list_nfs4_acl,
8870 .get = nfs4_xattr_get_nfs4_acl,
8871 .set = nfs4_xattr_set_nfs4_acl,
8874 const struct xattr_handler *nfs4_xattr_handlers[] = {
8875 &nfs4_xattr_nfs4_acl_handler,
8876 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
8877 &nfs4_xattr_nfs4_label_handler,