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 && nfs_async_inode_return_delegation(inode,
367 goto wait_on_recovery;
370 ret = nfs4_schedule_stateid_recovery(server, state);
373 goto wait_on_recovery;
374 case -NFS4ERR_EXPIRED:
376 ret = nfs4_schedule_stateid_recovery(server, state);
380 case -NFS4ERR_STALE_STATEID:
381 case -NFS4ERR_STALE_CLIENTID:
382 nfs4_schedule_lease_recovery(clp);
383 goto wait_on_recovery;
385 ret = nfs4_schedule_migration_recovery(server);
388 goto wait_on_recovery;
389 case -NFS4ERR_LEASE_MOVED:
390 nfs4_schedule_lease_moved_recovery(clp);
391 goto wait_on_recovery;
392 #if defined(CONFIG_NFS_V4_1)
393 case -NFS4ERR_BADSESSION:
394 case -NFS4ERR_BADSLOT:
395 case -NFS4ERR_BAD_HIGH_SLOT:
396 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
397 case -NFS4ERR_DEADSESSION:
398 case -NFS4ERR_SEQ_FALSE_RETRY:
399 case -NFS4ERR_SEQ_MISORDERED:
400 dprintk("%s ERROR: %d Reset session\n", __func__,
402 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
403 goto wait_on_recovery;
404 #endif /* defined(CONFIG_NFS_V4_1) */
405 case -NFS4ERR_FILE_OPEN:
406 if (exception->timeout > HZ) {
407 /* We have retried a decent amount, time to
414 nfs_inc_server_stats(server, NFSIOS_DELAY);
416 exception->delay = 1;
419 case -NFS4ERR_RETRY_UNCACHED_REP:
420 case -NFS4ERR_OLD_STATEID:
421 exception->retry = 1;
423 case -NFS4ERR_BADOWNER:
424 /* The following works around a Linux server bug! */
425 case -NFS4ERR_BADNAME:
426 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
427 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
428 exception->retry = 1;
429 printk(KERN_WARNING "NFS: v4 server %s "
430 "does not accept raw "
432 "Reenabling the idmapper.\n",
433 server->nfs_client->cl_hostname);
436 /* We failed to handle the error */
437 return nfs4_map_errors(ret);
439 exception->recovering = 1;
443 /* This is the error handling routine for processes that are allowed
446 int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
448 struct nfs_client *clp = server->nfs_client;
451 ret = nfs4_do_handle_exception(server, errorcode, exception);
452 if (exception->delay) {
453 ret = nfs4_delay(server->client, &exception->timeout);
456 if (exception->recovering) {
457 ret = nfs4_wait_clnt_recover(clp);
458 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
465 exception->retry = 1;
470 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
471 * or 'false' otherwise.
473 static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
475 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
477 if (flavor == RPC_AUTH_GSS_KRB5I ||
478 flavor == RPC_AUTH_GSS_KRB5P)
484 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
486 spin_lock(&clp->cl_lock);
487 if (time_before(clp->cl_last_renewal,timestamp))
488 clp->cl_last_renewal = timestamp;
489 spin_unlock(&clp->cl_lock);
492 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
494 struct nfs_client *clp = server->nfs_client;
496 if (!nfs4_has_session(clp))
497 do_renew_lease(clp, timestamp);
500 struct nfs4_call_sync_data {
501 const struct nfs_server *seq_server;
502 struct nfs4_sequence_args *seq_args;
503 struct nfs4_sequence_res *seq_res;
506 void nfs4_init_sequence(struct nfs4_sequence_args *args,
507 struct nfs4_sequence_res *res, int cache_reply)
509 args->sa_slot = NULL;
510 args->sa_cache_this = cache_reply;
511 args->sa_privileged = 0;
516 static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
518 args->sa_privileged = 1;
521 int nfs40_setup_sequence(struct nfs4_slot_table *tbl,
522 struct nfs4_sequence_args *args,
523 struct nfs4_sequence_res *res,
524 struct rpc_task *task)
526 struct nfs4_slot *slot;
528 /* slot already allocated? */
529 if (res->sr_slot != NULL)
532 spin_lock(&tbl->slot_tbl_lock);
533 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
536 slot = nfs4_alloc_slot(tbl);
538 if (slot == ERR_PTR(-ENOMEM))
539 task->tk_timeout = HZ >> 2;
542 spin_unlock(&tbl->slot_tbl_lock);
544 args->sa_slot = slot;
548 rpc_call_start(task);
552 if (args->sa_privileged)
553 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
554 NULL, RPC_PRIORITY_PRIVILEGED);
556 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
557 spin_unlock(&tbl->slot_tbl_lock);
560 EXPORT_SYMBOL_GPL(nfs40_setup_sequence);
562 static int nfs40_sequence_done(struct rpc_task *task,
563 struct nfs4_sequence_res *res)
565 struct nfs4_slot *slot = res->sr_slot;
566 struct nfs4_slot_table *tbl;
572 spin_lock(&tbl->slot_tbl_lock);
573 if (!nfs41_wake_and_assign_slot(tbl, slot))
574 nfs4_free_slot(tbl, slot);
575 spin_unlock(&tbl->slot_tbl_lock);
582 #if defined(CONFIG_NFS_V4_1)
584 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
586 struct nfs4_session *session;
587 struct nfs4_slot_table *tbl;
588 struct nfs4_slot *slot = res->sr_slot;
589 bool send_new_highest_used_slotid = false;
592 session = tbl->session;
594 spin_lock(&tbl->slot_tbl_lock);
595 /* Be nice to the server: try to ensure that the last transmitted
596 * value for highest_user_slotid <= target_highest_slotid
598 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
599 send_new_highest_used_slotid = true;
601 if (nfs41_wake_and_assign_slot(tbl, slot)) {
602 send_new_highest_used_slotid = false;
605 nfs4_free_slot(tbl, slot);
607 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
608 send_new_highest_used_slotid = false;
610 spin_unlock(&tbl->slot_tbl_lock);
612 if (send_new_highest_used_slotid)
613 nfs41_notify_server(session->clp);
616 int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
618 struct nfs4_session *session;
619 struct nfs4_slot *slot = res->sr_slot;
620 struct nfs_client *clp;
621 bool interrupted = false;
626 /* don't increment the sequence number if the task wasn't sent */
627 if (!RPC_WAS_SENT(task))
630 session = slot->table->session;
632 if (slot->interrupted) {
633 slot->interrupted = 0;
637 trace_nfs4_sequence_done(session, res);
638 /* Check the SEQUENCE operation status */
639 switch (res->sr_status) {
641 /* Update the slot's sequence and clientid lease timer */
644 do_renew_lease(clp, res->sr_timestamp);
645 /* Check sequence flags */
646 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
647 nfs41_update_target_slotid(slot->table, slot, res);
651 * sr_status remains 1 if an RPC level error occurred.
652 * The server may or may not have processed the sequence
654 * Mark the slot as having hosted an interrupted RPC call.
656 slot->interrupted = 1;
659 /* The server detected a resend of the RPC call and
660 * returned NFS4ERR_DELAY as per Section 2.10.6.2
663 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
668 case -NFS4ERR_BADSLOT:
670 * The slot id we used was probably retired. Try again
671 * using a different slot id.
674 case -NFS4ERR_SEQ_MISORDERED:
676 * Was the last operation on this sequence interrupted?
677 * If so, retry after bumping the sequence number.
684 * Could this slot have been previously retired?
685 * If so, then the server may be expecting seq_nr = 1!
687 if (slot->seq_nr != 1) {
692 case -NFS4ERR_SEQ_FALSE_RETRY:
696 /* Just update the slot sequence no. */
700 /* The session may be reset by one of the error handlers. */
701 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
702 nfs41_sequence_free_slot(res);
706 if (rpc_restart_call_prepare(task)) {
712 if (!rpc_restart_call(task))
714 rpc_delay(task, NFS4_POLL_RETRY_MAX);
717 EXPORT_SYMBOL_GPL(nfs41_sequence_done);
719 int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
721 if (res->sr_slot == NULL)
723 if (!res->sr_slot->table->session)
724 return nfs40_sequence_done(task, res);
725 return nfs41_sequence_done(task, res);
727 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
729 int nfs41_setup_sequence(struct nfs4_session *session,
730 struct nfs4_sequence_args *args,
731 struct nfs4_sequence_res *res,
732 struct rpc_task *task)
734 struct nfs4_slot *slot;
735 struct nfs4_slot_table *tbl;
737 dprintk("--> %s\n", __func__);
738 /* slot already allocated? */
739 if (res->sr_slot != NULL)
742 tbl = &session->fc_slot_table;
744 task->tk_timeout = 0;
746 spin_lock(&tbl->slot_tbl_lock);
747 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
748 !args->sa_privileged) {
749 /* The state manager will wait until the slot table is empty */
750 dprintk("%s session is draining\n", __func__);
754 slot = nfs4_alloc_slot(tbl);
756 /* If out of memory, try again in 1/4 second */
757 if (slot == ERR_PTR(-ENOMEM))
758 task->tk_timeout = HZ >> 2;
759 dprintk("<-- %s: no free slots\n", __func__);
762 spin_unlock(&tbl->slot_tbl_lock);
764 args->sa_slot = slot;
766 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
767 slot->slot_nr, slot->seq_nr);
770 res->sr_timestamp = jiffies;
771 res->sr_status_flags = 0;
773 * sr_status is only set in decode_sequence, and so will remain
774 * set to 1 if an rpc level failure occurs.
777 trace_nfs4_setup_sequence(session, args);
779 rpc_call_start(task);
782 /* Privileged tasks are queued with top priority */
783 if (args->sa_privileged)
784 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
785 NULL, RPC_PRIORITY_PRIVILEGED);
787 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
788 spin_unlock(&tbl->slot_tbl_lock);
791 EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
793 static int nfs4_setup_sequence(const struct nfs_server *server,
794 struct nfs4_sequence_args *args,
795 struct nfs4_sequence_res *res,
796 struct rpc_task *task)
798 struct nfs4_session *session = nfs4_get_session(server);
802 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
805 dprintk("--> %s clp %p session %p sr_slot %u\n",
806 __func__, session->clp, session, res->sr_slot ?
807 res->sr_slot->slot_nr : NFS4_NO_SLOT);
809 ret = nfs41_setup_sequence(session, args, res, task);
811 dprintk("<-- %s status=%d\n", __func__, ret);
815 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
817 struct nfs4_call_sync_data *data = calldata;
818 struct nfs4_session *session = nfs4_get_session(data->seq_server);
820 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
822 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
825 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
827 struct nfs4_call_sync_data *data = calldata;
829 nfs41_sequence_done(task, data->seq_res);
832 static const struct rpc_call_ops nfs41_call_sync_ops = {
833 .rpc_call_prepare = nfs41_call_sync_prepare,
834 .rpc_call_done = nfs41_call_sync_done,
837 #else /* !CONFIG_NFS_V4_1 */
839 static int nfs4_setup_sequence(const struct nfs_server *server,
840 struct nfs4_sequence_args *args,
841 struct nfs4_sequence_res *res,
842 struct rpc_task *task)
844 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
848 int nfs4_sequence_done(struct rpc_task *task,
849 struct nfs4_sequence_res *res)
851 return nfs40_sequence_done(task, res);
853 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
855 #endif /* !CONFIG_NFS_V4_1 */
857 static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
859 struct nfs4_call_sync_data *data = calldata;
860 nfs4_setup_sequence(data->seq_server,
861 data->seq_args, data->seq_res, task);
864 static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
866 struct nfs4_call_sync_data *data = calldata;
867 nfs4_sequence_done(task, data->seq_res);
870 static const struct rpc_call_ops nfs40_call_sync_ops = {
871 .rpc_call_prepare = nfs40_call_sync_prepare,
872 .rpc_call_done = nfs40_call_sync_done,
875 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
876 struct nfs_server *server,
877 struct rpc_message *msg,
878 struct nfs4_sequence_args *args,
879 struct nfs4_sequence_res *res)
882 struct rpc_task *task;
883 struct nfs_client *clp = server->nfs_client;
884 struct nfs4_call_sync_data data = {
885 .seq_server = server,
889 struct rpc_task_setup task_setup = {
892 .callback_ops = clp->cl_mvops->call_sync_ops,
893 .callback_data = &data
896 task = rpc_run_task(&task_setup);
900 ret = task->tk_status;
906 int nfs4_call_sync(struct rpc_clnt *clnt,
907 struct nfs_server *server,
908 struct rpc_message *msg,
909 struct nfs4_sequence_args *args,
910 struct nfs4_sequence_res *res,
913 nfs4_init_sequence(args, res, cache_reply);
914 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
917 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
919 struct nfs_inode *nfsi = NFS_I(dir);
921 spin_lock(&dir->i_lock);
922 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
923 if (!cinfo->atomic || cinfo->before != dir->i_version)
924 nfs_force_lookup_revalidate(dir);
925 dir->i_version = cinfo->after;
926 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
927 nfs_fscache_invalidate(dir);
928 spin_unlock(&dir->i_lock);
931 struct nfs4_opendata {
933 struct nfs_openargs o_arg;
934 struct nfs_openres o_res;
935 struct nfs_open_confirmargs c_arg;
936 struct nfs_open_confirmres c_res;
937 struct nfs4_string owner_name;
938 struct nfs4_string group_name;
939 struct nfs4_label *a_label;
940 struct nfs_fattr f_attr;
941 struct nfs4_label *f_label;
943 struct dentry *dentry;
944 struct nfs4_state_owner *owner;
945 struct nfs4_state *state;
947 unsigned long timestamp;
948 unsigned int rpc_done : 1;
949 unsigned int file_created : 1;
950 unsigned int is_recover : 1;
955 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
956 int err, struct nfs4_exception *exception)
960 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
962 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
963 exception->retry = 1;
968 nfs4_map_atomic_open_share(struct nfs_server *server,
969 fmode_t fmode, int openflags)
973 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
975 res = NFS4_SHARE_ACCESS_READ;
978 res = NFS4_SHARE_ACCESS_WRITE;
980 case FMODE_READ|FMODE_WRITE:
981 res = NFS4_SHARE_ACCESS_BOTH;
983 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
985 /* Want no delegation if we're using O_DIRECT */
986 if (openflags & O_DIRECT)
987 res |= NFS4_SHARE_WANT_NO_DELEG;
992 static enum open_claim_type4
993 nfs4_map_atomic_open_claim(struct nfs_server *server,
994 enum open_claim_type4 claim)
996 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1001 case NFS4_OPEN_CLAIM_FH:
1002 return NFS4_OPEN_CLAIM_NULL;
1003 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1004 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1005 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1006 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1010 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
1012 p->o_res.f_attr = &p->f_attr;
1013 p->o_res.f_label = p->f_label;
1014 p->o_res.seqid = p->o_arg.seqid;
1015 p->c_res.seqid = p->c_arg.seqid;
1016 p->o_res.server = p->o_arg.server;
1017 p->o_res.access_request = p->o_arg.access;
1018 nfs_fattr_init(&p->f_attr);
1019 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
1022 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
1023 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
1024 const struct iattr *attrs,
1025 struct nfs4_label *label,
1026 enum open_claim_type4 claim,
1029 struct dentry *parent = dget_parent(dentry);
1030 struct inode *dir = d_inode(parent);
1031 struct nfs_server *server = NFS_SERVER(dir);
1032 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
1033 struct nfs4_opendata *p;
1035 p = kzalloc(sizeof(*p), gfp_mask);
1039 p->f_label = nfs4_label_alloc(server, gfp_mask);
1040 if (IS_ERR(p->f_label))
1043 p->a_label = nfs4_label_alloc(server, gfp_mask);
1044 if (IS_ERR(p->a_label))
1047 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1048 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
1049 if (IS_ERR(p->o_arg.seqid))
1050 goto err_free_label;
1051 nfs_sb_active(dentry->d_sb);
1052 p->dentry = dget(dentry);
1055 atomic_inc(&sp->so_count);
1056 p->o_arg.open_flags = flags;
1057 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
1058 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1060 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1061 * will return permission denied for all bits until close */
1062 if (!(flags & O_EXCL)) {
1063 /* ask server to check for all possible rights as results
1065 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1066 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
1068 p->o_arg.clientid = server->nfs_client->cl_clientid;
1069 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1070 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1071 p->o_arg.name = &dentry->d_name;
1072 p->o_arg.server = server;
1073 p->o_arg.bitmask = nfs4_bitmask(server, label);
1074 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1075 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1076 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1077 switch (p->o_arg.claim) {
1078 case NFS4_OPEN_CLAIM_NULL:
1079 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1080 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1081 p->o_arg.fh = NFS_FH(dir);
1083 case NFS4_OPEN_CLAIM_PREVIOUS:
1084 case NFS4_OPEN_CLAIM_FH:
1085 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1086 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1087 p->o_arg.fh = NFS_FH(d_inode(dentry));
1089 if (attrs != NULL && attrs->ia_valid != 0) {
1092 p->o_arg.u.attrs = &p->attrs;
1093 memcpy(&p->attrs, attrs, sizeof(p->attrs));
1096 verf[1] = current->pid;
1097 memcpy(p->o_arg.u.verifier.data, verf,
1098 sizeof(p->o_arg.u.verifier.data));
1100 p->c_arg.fh = &p->o_res.fh;
1101 p->c_arg.stateid = &p->o_res.stateid;
1102 p->c_arg.seqid = p->o_arg.seqid;
1103 nfs4_init_opendata_res(p);
1104 kref_init(&p->kref);
1108 nfs4_label_free(p->a_label);
1110 nfs4_label_free(p->f_label);
1118 static void nfs4_opendata_free(struct kref *kref)
1120 struct nfs4_opendata *p = container_of(kref,
1121 struct nfs4_opendata, kref);
1122 struct super_block *sb = p->dentry->d_sb;
1124 nfs_free_seqid(p->o_arg.seqid);
1125 if (p->state != NULL)
1126 nfs4_put_open_state(p->state);
1127 nfs4_put_state_owner(p->owner);
1129 nfs4_label_free(p->a_label);
1130 nfs4_label_free(p->f_label);
1134 nfs_sb_deactive(sb);
1135 nfs_fattr_free_names(&p->f_attr);
1136 kfree(p->f_attr.mdsthreshold);
1140 static void nfs4_opendata_put(struct nfs4_opendata *p)
1143 kref_put(&p->kref, nfs4_opendata_free);
1146 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1150 ret = rpc_wait_for_completion_task(task);
1154 static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1157 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1158 case FMODE_READ|FMODE_WRITE:
1159 return state->n_rdwr != 0;
1161 return state->n_wronly != 0;
1163 return state->n_rdonly != 0;
1169 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
1173 if (open_mode & (O_EXCL|O_TRUNC))
1175 switch (mode & (FMODE_READ|FMODE_WRITE)) {
1177 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1178 && state->n_rdonly != 0;
1181 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1182 && state->n_wronly != 0;
1184 case FMODE_READ|FMODE_WRITE:
1185 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1186 && state->n_rdwr != 0;
1192 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1193 enum open_claim_type4 claim)
1195 if (delegation == NULL)
1197 if ((delegation->type & fmode) != fmode)
1199 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1202 case NFS4_OPEN_CLAIM_NULL:
1203 case NFS4_OPEN_CLAIM_FH:
1205 case NFS4_OPEN_CLAIM_PREVIOUS:
1206 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1211 nfs_mark_delegation_referenced(delegation);
1215 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1224 case FMODE_READ|FMODE_WRITE:
1227 nfs4_state_set_mode_locked(state, state->state | fmode);
1230 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
1232 struct nfs_client *clp = state->owner->so_server->nfs_client;
1233 bool need_recover = false;
1235 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1236 need_recover = true;
1237 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1238 need_recover = true;
1239 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1240 need_recover = true;
1242 nfs4_state_mark_reclaim_nograce(clp, state);
1245 static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1246 nfs4_stateid *stateid)
1248 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1250 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1251 nfs_test_and_clear_all_open_stateid(state);
1254 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1259 static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1261 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1263 if (state->n_wronly)
1264 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1265 if (state->n_rdonly)
1266 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1268 set_bit(NFS_O_RDWR_STATE, &state->flags);
1269 set_bit(NFS_OPEN_STATE, &state->flags);
1272 static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1273 nfs4_stateid *arg_stateid,
1274 nfs4_stateid *stateid, fmode_t fmode)
1276 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1277 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1279 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1282 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1285 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1286 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1287 clear_bit(NFS_OPEN_STATE, &state->flags);
1289 if (stateid == NULL)
1291 /* Handle races with OPEN */
1292 if (!nfs4_stateid_match_other(arg_stateid, &state->open_stateid) ||
1293 (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1294 !nfs4_stateid_is_newer(stateid, &state->open_stateid))) {
1295 nfs_resync_open_stateid_locked(state);
1298 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1299 nfs4_stateid_copy(&state->stateid, stateid);
1300 nfs4_stateid_copy(&state->open_stateid, stateid);
1303 static void nfs_clear_open_stateid(struct nfs4_state *state,
1304 nfs4_stateid *arg_stateid,
1305 nfs4_stateid *stateid, fmode_t fmode)
1307 write_seqlock(&state->seqlock);
1308 nfs_clear_open_stateid_locked(state, arg_stateid, stateid, fmode);
1309 write_sequnlock(&state->seqlock);
1310 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1311 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1314 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1318 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1321 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1323 case FMODE_READ|FMODE_WRITE:
1324 set_bit(NFS_O_RDWR_STATE, &state->flags);
1326 if (!nfs_need_update_open_stateid(state, stateid))
1328 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1329 nfs4_stateid_copy(&state->stateid, stateid);
1330 nfs4_stateid_copy(&state->open_stateid, stateid);
1333 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
1336 * Protect the call to nfs4_state_set_mode_locked and
1337 * serialise the stateid update
1339 write_seqlock(&state->seqlock);
1340 if (deleg_stateid != NULL) {
1341 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1342 set_bit(NFS_DELEGATED_STATE, &state->flags);
1344 if (open_stateid != NULL)
1345 nfs_set_open_stateid_locked(state, open_stateid, fmode);
1346 write_sequnlock(&state->seqlock);
1347 spin_lock(&state->owner->so_lock);
1348 update_open_stateflags(state, fmode);
1349 spin_unlock(&state->owner->so_lock);
1352 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
1354 struct nfs_inode *nfsi = NFS_I(state->inode);
1355 struct nfs_delegation *deleg_cur;
1358 fmode &= (FMODE_READ|FMODE_WRITE);
1361 deleg_cur = rcu_dereference(nfsi->delegation);
1362 if (deleg_cur == NULL)
1365 spin_lock(&deleg_cur->lock);
1366 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1367 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1368 (deleg_cur->type & fmode) != fmode)
1369 goto no_delegation_unlock;
1371 if (delegation == NULL)
1372 delegation = &deleg_cur->stateid;
1373 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
1374 goto no_delegation_unlock;
1376 nfs_mark_delegation_referenced(deleg_cur);
1377 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
1379 no_delegation_unlock:
1380 spin_unlock(&deleg_cur->lock);
1384 if (!ret && open_stateid != NULL) {
1385 __update_open_stateid(state, open_stateid, NULL, fmode);
1388 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1389 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1394 static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1395 const nfs4_stateid *stateid)
1397 struct nfs4_state *state = lsp->ls_state;
1400 spin_lock(&state->state_lock);
1401 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1403 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1405 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1408 spin_unlock(&state->state_lock);
1412 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1414 struct nfs_delegation *delegation;
1417 delegation = rcu_dereference(NFS_I(inode)->delegation);
1418 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1423 nfs4_inode_return_delegation(inode);
1426 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1428 struct nfs4_state *state = opendata->state;
1429 struct nfs_inode *nfsi = NFS_I(state->inode);
1430 struct nfs_delegation *delegation;
1431 int open_mode = opendata->o_arg.open_flags;
1432 fmode_t fmode = opendata->o_arg.fmode;
1433 enum open_claim_type4 claim = opendata->o_arg.claim;
1434 nfs4_stateid stateid;
1438 spin_lock(&state->owner->so_lock);
1439 if (can_open_cached(state, fmode, open_mode)) {
1440 update_open_stateflags(state, fmode);
1441 spin_unlock(&state->owner->so_lock);
1442 goto out_return_state;
1444 spin_unlock(&state->owner->so_lock);
1446 delegation = rcu_dereference(nfsi->delegation);
1447 if (!can_open_delegated(delegation, fmode, claim)) {
1451 /* Save the delegation */
1452 nfs4_stateid_copy(&stateid, &delegation->stateid);
1454 nfs_release_seqid(opendata->o_arg.seqid);
1455 if (!opendata->is_recover) {
1456 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1462 /* Try to update the stateid using the delegation */
1463 if (update_open_stateid(state, NULL, &stateid, fmode))
1464 goto out_return_state;
1467 return ERR_PTR(ret);
1469 atomic_inc(&state->count);
1474 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1476 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1477 struct nfs_delegation *delegation;
1478 int delegation_flags = 0;
1481 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1483 delegation_flags = delegation->flags;
1485 switch (data->o_arg.claim) {
1488 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1489 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1490 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1491 "returning a delegation for "
1492 "OPEN(CLAIM_DELEGATE_CUR)\n",
1496 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1497 nfs_inode_set_delegation(state->inode,
1498 data->owner->so_cred,
1501 nfs_inode_reclaim_delegation(state->inode,
1502 data->owner->so_cred,
1507 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1508 * and update the nfs4_state.
1510 static struct nfs4_state *
1511 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1513 struct inode *inode = data->state->inode;
1514 struct nfs4_state *state = data->state;
1517 if (!data->rpc_done) {
1518 if (data->rpc_status) {
1519 ret = data->rpc_status;
1522 /* cached opens have already been processed */
1526 ret = nfs_refresh_inode(inode, &data->f_attr);
1530 if (data->o_res.delegation_type != 0)
1531 nfs4_opendata_check_deleg(data, state);
1533 update_open_stateid(state, &data->o_res.stateid, NULL,
1535 atomic_inc(&state->count);
1539 return ERR_PTR(ret);
1543 static struct nfs4_state *
1544 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1546 struct inode *inode;
1547 struct nfs4_state *state = NULL;
1550 if (!data->rpc_done) {
1551 state = nfs4_try_open_cached(data);
1556 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1558 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
1559 ret = PTR_ERR(inode);
1563 state = nfs4_get_open_state(inode, data->owner);
1566 if (data->o_res.delegation_type != 0)
1567 nfs4_opendata_check_deleg(data, state);
1568 update_open_stateid(state, &data->o_res.stateid, NULL,
1572 nfs_release_seqid(data->o_arg.seqid);
1577 return ERR_PTR(ret);
1580 static struct nfs4_state *
1581 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1583 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1584 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1585 return _nfs4_opendata_to_nfs4_state(data);
1588 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1590 struct nfs_inode *nfsi = NFS_I(state->inode);
1591 struct nfs_open_context *ctx;
1593 spin_lock(&state->inode->i_lock);
1594 list_for_each_entry(ctx, &nfsi->open_files, list) {
1595 if (ctx->state != state)
1597 get_nfs_open_context(ctx);
1598 spin_unlock(&state->inode->i_lock);
1601 spin_unlock(&state->inode->i_lock);
1602 return ERR_PTR(-ENOENT);
1605 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1606 struct nfs4_state *state, enum open_claim_type4 claim)
1608 struct nfs4_opendata *opendata;
1610 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
1611 NULL, NULL, claim, GFP_NOFS);
1612 if (opendata == NULL)
1613 return ERR_PTR(-ENOMEM);
1614 opendata->state = state;
1615 atomic_inc(&state->count);
1619 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1622 struct nfs4_state *newstate;
1625 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
1627 opendata->o_arg.open_flags = 0;
1628 opendata->o_arg.fmode = fmode;
1629 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1630 NFS_SB(opendata->dentry->d_sb),
1632 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1633 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1634 nfs4_init_opendata_res(opendata);
1635 ret = _nfs4_recover_proc_open(opendata);
1638 newstate = nfs4_opendata_to_nfs4_state(opendata);
1639 if (IS_ERR(newstate))
1640 return PTR_ERR(newstate);
1641 if (newstate != opendata->state)
1643 nfs4_close_state(newstate, fmode);
1647 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1651 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1652 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1653 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1654 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1655 /* memory barrier prior to reading state->n_* */
1656 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1657 clear_bit(NFS_OPEN_STATE, &state->flags);
1659 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1662 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1665 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1669 * We may have performed cached opens for all three recoveries.
1670 * Check if we need to update the current stateid.
1672 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1673 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
1674 write_seqlock(&state->seqlock);
1675 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1676 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1677 write_sequnlock(&state->seqlock);
1684 * reclaim state on the server after a reboot.
1686 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1688 struct nfs_delegation *delegation;
1689 struct nfs4_opendata *opendata;
1690 fmode_t delegation_type = 0;
1693 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1694 NFS4_OPEN_CLAIM_PREVIOUS);
1695 if (IS_ERR(opendata))
1696 return PTR_ERR(opendata);
1698 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1699 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1700 delegation_type = delegation->type;
1702 opendata->o_arg.u.delegation_type = delegation_type;
1703 status = nfs4_open_recover(opendata, state);
1704 nfs4_opendata_put(opendata);
1708 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1710 struct nfs_server *server = NFS_SERVER(state->inode);
1711 struct nfs4_exception exception = { };
1714 err = _nfs4_do_open_reclaim(ctx, state);
1715 trace_nfs4_open_reclaim(ctx, 0, err);
1716 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1718 if (err != -NFS4ERR_DELAY)
1720 nfs4_handle_exception(server, err, &exception);
1721 } while (exception.retry);
1725 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1727 struct nfs_open_context *ctx;
1730 ctx = nfs4_state_find_open_context(state);
1733 ret = nfs4_do_open_reclaim(ctx, state);
1734 put_nfs_open_context(ctx);
1738 static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, int err)
1742 printk(KERN_ERR "NFS: %s: unhandled error "
1743 "%d.\n", __func__, err);
1749 case -NFS4ERR_BADSESSION:
1750 case -NFS4ERR_BADSLOT:
1751 case -NFS4ERR_BAD_HIGH_SLOT:
1752 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1753 case -NFS4ERR_DEADSESSION:
1754 set_bit(NFS_DELEGATED_STATE, &state->flags);
1755 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1757 case -NFS4ERR_STALE_CLIENTID:
1758 case -NFS4ERR_STALE_STATEID:
1759 set_bit(NFS_DELEGATED_STATE, &state->flags);
1760 case -NFS4ERR_EXPIRED:
1761 /* Don't recall a delegation if it was lost */
1762 nfs4_schedule_lease_recovery(server->nfs_client);
1764 case -NFS4ERR_MOVED:
1765 nfs4_schedule_migration_recovery(server);
1767 case -NFS4ERR_LEASE_MOVED:
1768 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1770 case -NFS4ERR_DELEG_REVOKED:
1771 case -NFS4ERR_ADMIN_REVOKED:
1772 case -NFS4ERR_BAD_STATEID:
1773 case -NFS4ERR_OPENMODE:
1774 nfs_inode_find_state_and_recover(state->inode,
1776 nfs4_schedule_stateid_recovery(server, state);
1778 case -NFS4ERR_DELAY:
1779 case -NFS4ERR_GRACE:
1780 set_bit(NFS_DELEGATED_STATE, &state->flags);
1784 case -NFS4ERR_DENIED:
1785 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1791 int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
1792 struct nfs4_state *state, const nfs4_stateid *stateid,
1795 struct nfs_server *server = NFS_SERVER(state->inode);
1796 struct nfs4_opendata *opendata;
1799 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1800 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1801 if (IS_ERR(opendata))
1802 return PTR_ERR(opendata);
1803 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1804 write_seqlock(&state->seqlock);
1805 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1806 write_sequnlock(&state->seqlock);
1807 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1808 switch (type & (FMODE_READ|FMODE_WRITE)) {
1809 case FMODE_READ|FMODE_WRITE:
1811 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1814 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1818 err = nfs4_open_recover_helper(opendata, FMODE_READ);
1820 nfs4_opendata_put(opendata);
1821 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1824 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1826 struct nfs4_opendata *data = calldata;
1828 nfs40_setup_sequence(data->o_arg.server->nfs_client->cl_slot_tbl,
1829 &data->c_arg.seq_args, &data->c_res.seq_res, task);
1832 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1834 struct nfs4_opendata *data = calldata;
1836 nfs40_sequence_done(task, &data->c_res.seq_res);
1838 data->rpc_status = task->tk_status;
1839 if (data->rpc_status == 0) {
1840 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
1841 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1842 renew_lease(data->o_res.server, data->timestamp);
1847 static void nfs4_open_confirm_release(void *calldata)
1849 struct nfs4_opendata *data = calldata;
1850 struct nfs4_state *state = NULL;
1852 /* If this request hasn't been cancelled, do nothing */
1853 if (data->cancelled == 0)
1855 /* In case of error, no cleanup! */
1856 if (!data->rpc_done)
1858 state = nfs4_opendata_to_nfs4_state(data);
1860 nfs4_close_state(state, data->o_arg.fmode);
1862 nfs4_opendata_put(data);
1865 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1866 .rpc_call_prepare = nfs4_open_confirm_prepare,
1867 .rpc_call_done = nfs4_open_confirm_done,
1868 .rpc_release = nfs4_open_confirm_release,
1872 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1874 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1876 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
1877 struct rpc_task *task;
1878 struct rpc_message msg = {
1879 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1880 .rpc_argp = &data->c_arg,
1881 .rpc_resp = &data->c_res,
1882 .rpc_cred = data->owner->so_cred,
1884 struct rpc_task_setup task_setup_data = {
1885 .rpc_client = server->client,
1886 .rpc_message = &msg,
1887 .callback_ops = &nfs4_open_confirm_ops,
1888 .callback_data = data,
1889 .workqueue = nfsiod_workqueue,
1890 .flags = RPC_TASK_ASYNC,
1894 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
1895 kref_get(&data->kref);
1897 data->rpc_status = 0;
1898 data->timestamp = jiffies;
1899 if (data->is_recover)
1900 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
1901 task = rpc_run_task(&task_setup_data);
1903 return PTR_ERR(task);
1904 status = nfs4_wait_for_completion_rpc_task(task);
1906 data->cancelled = 1;
1909 status = data->rpc_status;
1914 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1916 struct nfs4_opendata *data = calldata;
1917 struct nfs4_state_owner *sp = data->owner;
1918 struct nfs_client *clp = sp->so_server->nfs_client;
1919 enum open_claim_type4 claim = data->o_arg.claim;
1921 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1924 * Check if we still need to send an OPEN call, or if we can use
1925 * a delegation instead.
1927 if (data->state != NULL) {
1928 struct nfs_delegation *delegation;
1930 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1933 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1934 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
1935 goto unlock_no_action;
1938 /* Update client id. */
1939 data->o_arg.clientid = clp->cl_clientid;
1943 case NFS4_OPEN_CLAIM_PREVIOUS:
1944 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1945 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1946 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
1947 case NFS4_OPEN_CLAIM_FH:
1948 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1949 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1951 data->timestamp = jiffies;
1952 if (nfs4_setup_sequence(data->o_arg.server,
1953 &data->o_arg.seq_args,
1954 &data->o_res.seq_res,
1956 nfs_release_seqid(data->o_arg.seqid);
1958 /* Set the create mode (note dependency on the session type) */
1959 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1960 if (data->o_arg.open_flags & O_EXCL) {
1961 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1962 if (nfs4_has_persistent_session(clp))
1963 data->o_arg.createmode = NFS4_CREATE_GUARDED;
1964 else if (clp->cl_mvops->minor_version > 0)
1965 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1971 task->tk_action = NULL;
1973 nfs4_sequence_done(task, &data->o_res.seq_res);
1976 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1978 struct nfs4_opendata *data = calldata;
1980 data->rpc_status = task->tk_status;
1982 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1985 if (task->tk_status == 0) {
1986 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1987 switch (data->o_res.f_attr->mode & S_IFMT) {
1991 data->rpc_status = -ELOOP;
1994 data->rpc_status = -EISDIR;
1997 data->rpc_status = -ENOTDIR;
2000 renew_lease(data->o_res.server, data->timestamp);
2001 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2002 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2007 static void nfs4_open_release(void *calldata)
2009 struct nfs4_opendata *data = calldata;
2010 struct nfs4_state *state = NULL;
2012 /* If this request hasn't been cancelled, do nothing */
2013 if (data->cancelled == 0)
2015 /* In case of error, no cleanup! */
2016 if (data->rpc_status != 0 || !data->rpc_done)
2018 /* In case we need an open_confirm, no cleanup! */
2019 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2021 state = nfs4_opendata_to_nfs4_state(data);
2023 nfs4_close_state(state, data->o_arg.fmode);
2025 nfs4_opendata_put(data);
2028 static const struct rpc_call_ops nfs4_open_ops = {
2029 .rpc_call_prepare = nfs4_open_prepare,
2030 .rpc_call_done = nfs4_open_done,
2031 .rpc_release = nfs4_open_release,
2034 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
2036 struct inode *dir = d_inode(data->dir);
2037 struct nfs_server *server = NFS_SERVER(dir);
2038 struct nfs_openargs *o_arg = &data->o_arg;
2039 struct nfs_openres *o_res = &data->o_res;
2040 struct rpc_task *task;
2041 struct rpc_message msg = {
2042 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2045 .rpc_cred = data->owner->so_cred,
2047 struct rpc_task_setup task_setup_data = {
2048 .rpc_client = server->client,
2049 .rpc_message = &msg,
2050 .callback_ops = &nfs4_open_ops,
2051 .callback_data = data,
2052 .workqueue = nfsiod_workqueue,
2053 .flags = RPC_TASK_ASYNC,
2057 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
2058 kref_get(&data->kref);
2060 data->rpc_status = 0;
2061 data->cancelled = 0;
2062 data->is_recover = 0;
2064 nfs4_set_sequence_privileged(&o_arg->seq_args);
2065 data->is_recover = 1;
2067 task = rpc_run_task(&task_setup_data);
2069 return PTR_ERR(task);
2070 status = nfs4_wait_for_completion_rpc_task(task);
2072 data->cancelled = 1;
2075 status = data->rpc_status;
2081 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2083 struct inode *dir = d_inode(data->dir);
2084 struct nfs_openres *o_res = &data->o_res;
2087 status = nfs4_run_open_task(data, 1);
2088 if (status != 0 || !data->rpc_done)
2091 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2093 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2094 status = _nfs4_proc_open_confirm(data);
2103 * Additional permission checks in order to distinguish between an
2104 * open for read, and an open for execute. This works around the
2105 * fact that NFSv4 OPEN treats read and execute permissions as being
2107 * Note that in the non-execute case, we want to turn off permission
2108 * checking if we just created a new file (POSIX open() semantics).
2110 static int nfs4_opendata_access(struct rpc_cred *cred,
2111 struct nfs4_opendata *opendata,
2112 struct nfs4_state *state, fmode_t fmode,
2115 struct nfs_access_entry cache;
2118 /* access call failed or for some reason the server doesn't
2119 * support any access modes -- defer access call until later */
2120 if (opendata->o_res.access_supported == 0)
2125 * Use openflags to check for exec, because fmode won't
2126 * always have FMODE_EXEC set when file open for exec.
2128 if (openflags & __FMODE_EXEC) {
2129 /* ONLY check for exec rights */
2131 } else if ((fmode & FMODE_READ) && !opendata->file_created)
2135 cache.jiffies = jiffies;
2136 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2137 nfs_access_add_cache(state->inode, &cache);
2139 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
2142 /* even though OPEN succeeded, access is denied. Close the file */
2143 nfs4_close_state(state, fmode);
2148 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2150 static int _nfs4_proc_open(struct nfs4_opendata *data)
2152 struct inode *dir = d_inode(data->dir);
2153 struct nfs_server *server = NFS_SERVER(dir);
2154 struct nfs_openargs *o_arg = &data->o_arg;
2155 struct nfs_openres *o_res = &data->o_res;
2158 status = nfs4_run_open_task(data, 0);
2159 if (!data->rpc_done)
2162 if (status == -NFS4ERR_BADNAME &&
2163 !(o_arg->open_flags & O_CREAT))
2168 nfs_fattr_map_and_free_names(server, &data->f_attr);
2170 if (o_arg->open_flags & O_CREAT) {
2171 update_changeattr(dir, &o_res->cinfo);
2172 if (o_arg->open_flags & O_EXCL)
2173 data->file_created = 1;
2174 else if (o_res->cinfo.before != o_res->cinfo.after)
2175 data->file_created = 1;
2177 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2178 server->caps &= ~NFS_CAP_POSIX_LOCK;
2179 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2180 status = _nfs4_proc_open_confirm(data);
2184 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
2185 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
2189 static int nfs4_recover_expired_lease(struct nfs_server *server)
2191 return nfs4_client_recover_expired_lease(server->nfs_client);
2196 * reclaim state on the server after a network partition.
2197 * Assumes caller holds the appropriate lock
2199 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2201 struct nfs4_opendata *opendata;
2204 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2205 NFS4_OPEN_CLAIM_FH);
2206 if (IS_ERR(opendata))
2207 return PTR_ERR(opendata);
2208 ret = nfs4_open_recover(opendata, state);
2210 d_drop(ctx->dentry);
2211 nfs4_opendata_put(opendata);
2215 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2217 struct nfs_server *server = NFS_SERVER(state->inode);
2218 struct nfs4_exception exception = { };
2222 err = _nfs4_open_expired(ctx, state);
2223 trace_nfs4_open_expired(ctx, 0, err);
2224 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2229 case -NFS4ERR_GRACE:
2230 case -NFS4ERR_DELAY:
2231 nfs4_handle_exception(server, err, &exception);
2234 } while (exception.retry);
2239 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2241 struct nfs_open_context *ctx;
2244 ctx = nfs4_state_find_open_context(state);
2247 ret = nfs4_do_open_expired(ctx, state);
2248 put_nfs_open_context(ctx);
2252 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state)
2254 nfs_remove_bad_delegation(state->inode);
2255 write_seqlock(&state->seqlock);
2256 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2257 write_sequnlock(&state->seqlock);
2258 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2261 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2263 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2264 nfs_finish_clear_delegation_stateid(state);
2267 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2269 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2270 nfs40_clear_delegation_stateid(state);
2271 return nfs4_open_expired(sp, state);
2274 #if defined(CONFIG_NFS_V4_1)
2275 static void nfs41_check_delegation_stateid(struct nfs4_state *state)
2277 struct nfs_server *server = NFS_SERVER(state->inode);
2278 nfs4_stateid stateid;
2279 struct nfs_delegation *delegation;
2280 struct rpc_cred *cred;
2283 /* Get the delegation credential for use by test/free_stateid */
2285 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2286 if (delegation == NULL) {
2291 nfs4_stateid_copy(&stateid, &delegation->stateid);
2292 cred = get_rpccred(delegation->cred);
2294 status = nfs41_test_stateid(server, &stateid, cred);
2295 trace_nfs4_test_delegation_stateid(state, NULL, status);
2297 if (status != NFS_OK) {
2298 /* Free the stateid unless the server explicitly
2299 * informs us the stateid is unrecognized. */
2300 if (status != -NFS4ERR_BAD_STATEID)
2301 nfs41_free_stateid(server, &stateid, cred);
2302 nfs_finish_clear_delegation_stateid(state);
2309 * nfs41_check_open_stateid - possibly free an open stateid
2311 * @state: NFSv4 state for an inode
2313 * Returns NFS_OK if recovery for this stateid is now finished.
2314 * Otherwise a negative NFS4ERR value is returned.
2316 static int nfs41_check_open_stateid(struct nfs4_state *state)
2318 struct nfs_server *server = NFS_SERVER(state->inode);
2319 nfs4_stateid *stateid = &state->open_stateid;
2320 struct rpc_cred *cred = state->owner->so_cred;
2323 /* If a state reset has been done, test_stateid is unneeded */
2324 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2325 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2326 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2327 return -NFS4ERR_BAD_STATEID;
2329 status = nfs41_test_stateid(server, stateid, cred);
2330 trace_nfs4_test_open_stateid(state, NULL, status);
2331 if (status != NFS_OK) {
2332 /* Free the stateid unless the server explicitly
2333 * informs us the stateid is unrecognized. */
2334 if (status != -NFS4ERR_BAD_STATEID)
2335 nfs41_free_stateid(server, stateid, cred);
2337 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2338 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2339 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2340 clear_bit(NFS_OPEN_STATE, &state->flags);
2345 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2349 nfs41_check_delegation_stateid(state);
2350 status = nfs41_check_open_stateid(state);
2351 if (status != NFS_OK)
2352 status = nfs4_open_expired(sp, state);
2358 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2359 * fields corresponding to attributes that were used to store the verifier.
2360 * Make sure we clobber those fields in the later setattr call
2362 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2363 struct iattr *sattr, struct nfs4_label **label)
2365 const u32 *attrset = opendata->o_res.attrset;
2367 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2368 !(sattr->ia_valid & ATTR_ATIME_SET))
2369 sattr->ia_valid |= ATTR_ATIME;
2371 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2372 !(sattr->ia_valid & ATTR_MTIME_SET))
2373 sattr->ia_valid |= ATTR_MTIME;
2375 /* Except MODE, it seems harmless of setting twice. */
2376 if ((attrset[1] & FATTR4_WORD1_MODE))
2377 sattr->ia_valid &= ~ATTR_MODE;
2379 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2383 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2386 struct nfs_open_context *ctx)
2388 struct nfs4_state_owner *sp = opendata->owner;
2389 struct nfs_server *server = sp->so_server;
2390 struct dentry *dentry;
2391 struct nfs4_state *state;
2395 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2397 ret = _nfs4_proc_open(opendata);
2401 state = nfs4_opendata_to_nfs4_state(opendata);
2402 ret = PTR_ERR(state);
2405 if (server->caps & NFS_CAP_POSIX_LOCK)
2406 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2408 dentry = opendata->dentry;
2409 if (d_really_is_negative(dentry)) {
2410 /* FIXME: Is this d_drop() ever needed? */
2412 dentry = d_add_unique(dentry, igrab(state->inode));
2413 if (dentry == NULL) {
2414 dentry = opendata->dentry;
2415 } else if (dentry != ctx->dentry) {
2417 ctx->dentry = dget(dentry);
2419 nfs_set_verifier(dentry,
2420 nfs_save_change_attribute(d_inode(opendata->dir)));
2423 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2428 if (d_inode(dentry) == state->inode) {
2429 nfs_inode_attach_open_context(ctx);
2430 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2431 nfs4_schedule_stateid_recovery(server, state);
2438 * Returns a referenced nfs4_state
2440 static int _nfs4_do_open(struct inode *dir,
2441 struct nfs_open_context *ctx,
2443 struct iattr *sattr,
2444 struct nfs4_label *label,
2447 struct nfs4_state_owner *sp;
2448 struct nfs4_state *state = NULL;
2449 struct nfs_server *server = NFS_SERVER(dir);
2450 struct nfs4_opendata *opendata;
2451 struct dentry *dentry = ctx->dentry;
2452 struct rpc_cred *cred = ctx->cred;
2453 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2454 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
2455 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
2456 struct nfs4_label *olabel = NULL;
2459 /* Protect against reboot recovery conflicts */
2461 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2463 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2466 status = nfs4_recover_expired_lease(server);
2468 goto err_put_state_owner;
2469 if (d_really_is_positive(dentry))
2470 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
2472 if (d_really_is_positive(dentry))
2473 claim = NFS4_OPEN_CLAIM_FH;
2474 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
2475 label, claim, GFP_KERNEL);
2476 if (opendata == NULL)
2477 goto err_put_state_owner;
2480 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2481 if (IS_ERR(olabel)) {
2482 status = PTR_ERR(olabel);
2483 goto err_opendata_put;
2487 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2488 if (!opendata->f_attr.mdsthreshold) {
2489 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2490 if (!opendata->f_attr.mdsthreshold)
2491 goto err_free_label;
2493 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
2495 if (d_really_is_positive(dentry))
2496 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
2498 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
2500 goto err_free_label;
2503 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
2504 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
2505 nfs4_exclusive_attrset(opendata, sattr, &label);
2507 nfs_fattr_init(opendata->o_res.f_attr);
2508 status = nfs4_do_setattr(state->inode, cred,
2509 opendata->o_res.f_attr, sattr,
2510 state, label, olabel);
2512 nfs_setattr_update_inode(state->inode, sattr,
2513 opendata->o_res.f_attr);
2514 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2517 if (opened && opendata->file_created)
2518 *opened |= FILE_CREATED;
2520 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
2521 *ctx_th = opendata->f_attr.mdsthreshold;
2522 opendata->f_attr.mdsthreshold = NULL;
2525 nfs4_label_free(olabel);
2527 nfs4_opendata_put(opendata);
2528 nfs4_put_state_owner(sp);
2531 nfs4_label_free(olabel);
2533 nfs4_opendata_put(opendata);
2534 err_put_state_owner:
2535 nfs4_put_state_owner(sp);
2541 static struct nfs4_state *nfs4_do_open(struct inode *dir,
2542 struct nfs_open_context *ctx,
2544 struct iattr *sattr,
2545 struct nfs4_label *label,
2548 struct nfs_server *server = NFS_SERVER(dir);
2549 struct nfs4_exception exception = { };
2550 struct nfs4_state *res;
2554 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
2556 trace_nfs4_open_file(ctx, flags, status);
2559 /* NOTE: BAD_SEQID means the server and client disagree about the
2560 * book-keeping w.r.t. state-changing operations
2561 * (OPEN/CLOSE/LOCK/LOCKU...)
2562 * It is actually a sign of a bug on the client or on the server.
2564 * If we receive a BAD_SEQID error in the particular case of
2565 * doing an OPEN, we assume that nfs_increment_open_seqid() will
2566 * have unhashed the old state_owner for us, and that we can
2567 * therefore safely retry using a new one. We should still warn
2568 * the user though...
2570 if (status == -NFS4ERR_BAD_SEQID) {
2571 pr_warn_ratelimited("NFS: v4 server %s "
2572 " returned a bad sequence-id error!\n",
2573 NFS_SERVER(dir)->nfs_client->cl_hostname);
2574 exception.retry = 1;
2578 * BAD_STATEID on OPEN means that the server cancelled our
2579 * state before it received the OPEN_CONFIRM.
2580 * Recover by retrying the request as per the discussion
2581 * on Page 181 of RFC3530.
2583 if (status == -NFS4ERR_BAD_STATEID) {
2584 exception.retry = 1;
2587 if (status == -EAGAIN) {
2588 /* We must have found a delegation */
2589 exception.retry = 1;
2592 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2594 res = ERR_PTR(nfs4_handle_exception(server,
2595 status, &exception));
2596 } while (exception.retry);
2600 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2601 struct nfs_fattr *fattr, struct iattr *sattr,
2602 struct nfs4_state *state, struct nfs4_label *ilabel,
2603 struct nfs4_label *olabel)
2605 struct nfs_server *server = NFS_SERVER(inode);
2606 struct nfs_setattrargs arg = {
2607 .fh = NFS_FH(inode),
2610 .bitmask = server->attr_bitmask,
2613 struct nfs_setattrres res = {
2618 struct rpc_message msg = {
2619 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2624 unsigned long timestamp = jiffies;
2629 arg.bitmask = nfs4_bitmask(server, ilabel);
2631 arg.bitmask = nfs4_bitmask(server, olabel);
2633 nfs_fattr_init(fattr);
2635 /* Servers should only apply open mode checks for file size changes */
2636 truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false;
2637 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2639 if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) {
2640 /* Use that stateid */
2641 } else if (truncate && state != NULL) {
2642 struct nfs_lockowner lockowner = {
2643 .l_owner = current->files,
2644 .l_pid = current->tgid,
2646 if (!nfs4_valid_open_stateid(state))
2648 if (nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
2649 &lockowner) == -EIO)
2652 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
2654 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2655 if (status == 0 && state != NULL)
2656 renew_lease(server, timestamp);
2660 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2661 struct nfs_fattr *fattr, struct iattr *sattr,
2662 struct nfs4_state *state, struct nfs4_label *ilabel,
2663 struct nfs4_label *olabel)
2665 struct nfs_server *server = NFS_SERVER(inode);
2666 struct nfs4_exception exception = {
2672 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state, ilabel, olabel);
2673 trace_nfs4_setattr(inode, err);
2675 case -NFS4ERR_OPENMODE:
2676 if (!(sattr->ia_valid & ATTR_SIZE)) {
2677 pr_warn_once("NFSv4: server %s is incorrectly "
2678 "applying open mode checks to "
2679 "a SETATTR that is not "
2680 "changing file size.\n",
2681 server->nfs_client->cl_hostname);
2683 if (state && !(state->state & FMODE_WRITE)) {
2685 if (sattr->ia_valid & ATTR_OPEN)
2690 err = nfs4_handle_exception(server, err, &exception);
2691 } while (exception.retry);
2697 nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
2699 if (inode == NULL || !nfs_have_layout(inode))
2702 return pnfs_wait_on_layoutreturn(inode, task);
2705 struct nfs4_closedata {
2706 struct inode *inode;
2707 struct nfs4_state *state;
2708 struct nfs_closeargs arg;
2709 struct nfs_closeres res;
2710 struct nfs_fattr fattr;
2711 unsigned long timestamp;
2716 static void nfs4_free_closedata(void *data)
2718 struct nfs4_closedata *calldata = data;
2719 struct nfs4_state_owner *sp = calldata->state->owner;
2720 struct super_block *sb = calldata->state->inode->i_sb;
2723 pnfs_roc_release(calldata->state->inode);
2724 nfs4_put_open_state(calldata->state);
2725 nfs_free_seqid(calldata->arg.seqid);
2726 nfs4_put_state_owner(sp);
2727 nfs_sb_deactive(sb);
2731 static void nfs4_close_done(struct rpc_task *task, void *data)
2733 struct nfs4_closedata *calldata = data;
2734 struct nfs4_state *state = calldata->state;
2735 struct nfs_server *server = NFS_SERVER(calldata->inode);
2736 nfs4_stateid *res_stateid = NULL;
2738 dprintk("%s: begin!\n", __func__);
2739 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2741 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
2742 /* hmm. we are done with the inode, and in the process of freeing
2743 * the state_owner. we keep this around to process errors
2745 switch (task->tk_status) {
2747 res_stateid = &calldata->res.stateid;
2749 pnfs_roc_set_barrier(state->inode,
2750 calldata->roc_barrier);
2751 renew_lease(server, calldata->timestamp);
2753 case -NFS4ERR_ADMIN_REVOKED:
2754 case -NFS4ERR_STALE_STATEID:
2755 case -NFS4ERR_OLD_STATEID:
2756 case -NFS4ERR_BAD_STATEID:
2757 case -NFS4ERR_EXPIRED:
2758 if (!nfs4_stateid_match(&calldata->arg.stateid,
2759 &state->open_stateid)) {
2760 rpc_restart_call_prepare(task);
2763 if (calldata->arg.fmode == 0)
2766 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
2767 rpc_restart_call_prepare(task);
2771 nfs_clear_open_stateid(state, &calldata->arg.stateid,
2772 res_stateid, calldata->arg.fmode);
2774 nfs_release_seqid(calldata->arg.seqid);
2775 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
2776 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
2779 static void nfs4_close_prepare(struct rpc_task *task, void *data)
2781 struct nfs4_closedata *calldata = data;
2782 struct nfs4_state *state = calldata->state;
2783 struct inode *inode = calldata->inode;
2784 bool is_rdonly, is_wronly, is_rdwr;
2787 dprintk("%s: begin!\n", __func__);
2788 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
2791 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2792 spin_lock(&state->owner->so_lock);
2793 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
2794 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
2795 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
2796 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
2797 /* Calculate the change in open mode */
2798 calldata->arg.fmode = 0;
2799 if (state->n_rdwr == 0) {
2800 if (state->n_rdonly == 0)
2801 call_close |= is_rdonly;
2803 calldata->arg.fmode |= FMODE_READ;
2804 if (state->n_wronly == 0)
2805 call_close |= is_wronly;
2807 calldata->arg.fmode |= FMODE_WRITE;
2809 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
2811 if (calldata->arg.fmode == 0)
2812 call_close |= is_rdwr;
2814 if (!nfs4_valid_open_stateid(state))
2816 spin_unlock(&state->owner->so_lock);
2819 /* Note: exit _without_ calling nfs4_close_done */
2823 if (nfs4_wait_on_layoutreturn(inode, task)) {
2824 nfs_release_seqid(calldata->arg.seqid);
2828 if (calldata->arg.fmode == 0)
2829 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
2831 pnfs_roc_get_barrier(inode, &calldata->roc_barrier);
2833 calldata->arg.share_access =
2834 nfs4_map_atomic_open_share(NFS_SERVER(inode),
2835 calldata->arg.fmode, 0);
2837 nfs_fattr_init(calldata->res.fattr);
2838 calldata->timestamp = jiffies;
2839 if (nfs4_setup_sequence(NFS_SERVER(inode),
2840 &calldata->arg.seq_args,
2841 &calldata->res.seq_res,
2843 nfs_release_seqid(calldata->arg.seqid);
2844 dprintk("%s: done!\n", __func__);
2847 task->tk_action = NULL;
2849 nfs4_sequence_done(task, &calldata->res.seq_res);
2852 static const struct rpc_call_ops nfs4_close_ops = {
2853 .rpc_call_prepare = nfs4_close_prepare,
2854 .rpc_call_done = nfs4_close_done,
2855 .rpc_release = nfs4_free_closedata,
2858 static bool nfs4_roc(struct inode *inode)
2860 if (!nfs_have_layout(inode))
2862 return pnfs_roc(inode);
2866 * It is possible for data to be read/written from a mem-mapped file
2867 * after the sys_close call (which hits the vfs layer as a flush).
2868 * This means that we can't safely call nfsv4 close on a file until
2869 * the inode is cleared. This in turn means that we are not good
2870 * NFSv4 citizens - we do not indicate to the server to update the file's
2871 * share state even when we are done with one of the three share
2872 * stateid's in the inode.
2874 * NOTE: Caller must be holding the sp->so_owner semaphore!
2876 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
2878 struct nfs_server *server = NFS_SERVER(state->inode);
2879 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
2880 struct nfs4_closedata *calldata;
2881 struct nfs4_state_owner *sp = state->owner;
2882 struct rpc_task *task;
2883 struct rpc_message msg = {
2884 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2885 .rpc_cred = state->owner->so_cred,
2887 struct rpc_task_setup task_setup_data = {
2888 .rpc_client = server->client,
2889 .rpc_message = &msg,
2890 .callback_ops = &nfs4_close_ops,
2891 .workqueue = nfsiod_workqueue,
2892 .flags = RPC_TASK_ASYNC,
2894 int status = -ENOMEM;
2896 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
2897 &task_setup_data.rpc_client, &msg);
2899 calldata = kzalloc(sizeof(*calldata), gfp_mask);
2900 if (calldata == NULL)
2902 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
2903 calldata->inode = state->inode;
2904 calldata->state = state;
2905 calldata->arg.fh = NFS_FH(state->inode);
2906 /* Serialization for the sequence id */
2907 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
2908 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
2909 if (IS_ERR(calldata->arg.seqid))
2910 goto out_free_calldata;
2911 calldata->arg.fmode = 0;
2912 calldata->arg.bitmask = server->cache_consistency_bitmask;
2913 calldata->res.fattr = &calldata->fattr;
2914 calldata->res.seqid = calldata->arg.seqid;
2915 calldata->res.server = server;
2916 calldata->roc = nfs4_roc(state->inode);
2917 nfs_sb_active(calldata->inode->i_sb);
2919 msg.rpc_argp = &calldata->arg;
2920 msg.rpc_resp = &calldata->res;
2921 task_setup_data.callback_data = calldata;
2922 task = rpc_run_task(&task_setup_data);
2924 return PTR_ERR(task);
2927 status = rpc_wait_for_completion_task(task);
2933 nfs4_put_open_state(state);
2934 nfs4_put_state_owner(sp);
2938 static struct inode *
2939 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
2940 int open_flags, struct iattr *attr, int *opened)
2942 struct nfs4_state *state;
2943 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
2945 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
2947 /* Protect against concurrent sillydeletes */
2948 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
2950 nfs4_label_release_security(label);
2953 return ERR_CAST(state);
2954 return state->inode;
2957 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2959 if (ctx->state == NULL)
2962 nfs4_close_sync(ctx->state, ctx->mode);
2964 nfs4_close_state(ctx->state, ctx->mode);
2967 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
2968 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
2969 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
2971 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2973 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
2974 struct nfs4_server_caps_arg args = {
2978 struct nfs4_server_caps_res res = {};
2979 struct rpc_message msg = {
2980 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2986 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
2987 FATTR4_WORD0_FH_EXPIRE_TYPE |
2988 FATTR4_WORD0_LINK_SUPPORT |
2989 FATTR4_WORD0_SYMLINK_SUPPORT |
2990 FATTR4_WORD0_ACLSUPPORT;
2992 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
2994 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2996 /* Sanity check the server answers */
2997 switch (minorversion) {
2999 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3000 res.attr_bitmask[2] = 0;
3003 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3006 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3008 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
3009 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3010 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3011 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3012 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
3013 NFS_CAP_CTIME|NFS_CAP_MTIME|
3014 NFS_CAP_SECURITY_LABEL);
3015 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3016 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3017 server->caps |= NFS_CAP_ACLS;
3018 if (res.has_links != 0)
3019 server->caps |= NFS_CAP_HARDLINKS;
3020 if (res.has_symlinks != 0)
3021 server->caps |= NFS_CAP_SYMLINKS;
3022 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3023 server->caps |= NFS_CAP_FILEID;
3024 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3025 server->caps |= NFS_CAP_MODE;
3026 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3027 server->caps |= NFS_CAP_NLINK;
3028 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3029 server->caps |= NFS_CAP_OWNER;
3030 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3031 server->caps |= NFS_CAP_OWNER_GROUP;
3032 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3033 server->caps |= NFS_CAP_ATIME;
3034 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3035 server->caps |= NFS_CAP_CTIME;
3036 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3037 server->caps |= NFS_CAP_MTIME;
3038 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3039 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3040 server->caps |= NFS_CAP_SECURITY_LABEL;
3042 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3043 sizeof(server->attr_bitmask));
3044 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3046 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3047 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3048 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
3049 server->cache_consistency_bitmask[2] = 0;
3050 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3051 sizeof(server->exclcreat_bitmask));
3052 server->acl_bitmask = res.acl_bitmask;
3053 server->fh_expire_type = res.fh_expire_type;
3059 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3061 struct nfs4_exception exception = { };
3064 err = nfs4_handle_exception(server,
3065 _nfs4_server_capabilities(server, fhandle),
3067 } while (exception.retry);
3071 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3072 struct nfs_fsinfo *info)
3075 struct nfs4_lookup_root_arg args = {
3078 struct nfs4_lookup_res res = {
3080 .fattr = info->fattr,
3083 struct rpc_message msg = {
3084 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3089 bitmask[0] = nfs4_fattr_bitmap[0];
3090 bitmask[1] = nfs4_fattr_bitmap[1];
3092 * Process the label in the upcoming getfattr
3094 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3096 nfs_fattr_init(info->fattr);
3097 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3100 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3101 struct nfs_fsinfo *info)
3103 struct nfs4_exception exception = { };
3106 err = _nfs4_lookup_root(server, fhandle, info);
3107 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
3110 case -NFS4ERR_WRONGSEC:
3113 err = nfs4_handle_exception(server, err, &exception);
3115 } while (exception.retry);
3120 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3121 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3123 struct rpc_auth_create_args auth_args = {
3124 .pseudoflavor = flavor,
3126 struct rpc_auth *auth;
3129 auth = rpcauth_create(&auth_args, server->client);
3134 ret = nfs4_lookup_root(server, fhandle, info);
3140 * Retry pseudoroot lookup with various security flavors. We do this when:
3142 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3143 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3145 * Returns zero on success, or a negative NFS4ERR value, or a
3146 * negative errno value.
3148 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3149 struct nfs_fsinfo *info)
3151 /* Per 3530bis 15.33.5 */
3152 static const rpc_authflavor_t flav_array[] = {
3156 RPC_AUTH_UNIX, /* courtesy */
3159 int status = -EPERM;
3162 if (server->auth_info.flavor_len > 0) {
3163 /* try each flavor specified by user */
3164 for (i = 0; i < server->auth_info.flavor_len; i++) {
3165 status = nfs4_lookup_root_sec(server, fhandle, info,
3166 server->auth_info.flavors[i]);
3167 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3172 /* no flavors specified by user, try default list */
3173 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3174 status = nfs4_lookup_root_sec(server, fhandle, info,
3176 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3183 * -EACCESS could mean that the user doesn't have correct permissions
3184 * to access the mount. It could also mean that we tried to mount
3185 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3186 * existing mount programs don't handle -EACCES very well so it should
3187 * be mapped to -EPERM instead.
3189 if (status == -EACCES)
3194 static int nfs4_do_find_root_sec(struct nfs_server *server,
3195 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
3197 int mv = server->nfs_client->cl_minorversion;
3198 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
3202 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3203 * @server: initialized nfs_server handle
3204 * @fhandle: we fill in the pseudo-fs root file handle
3205 * @info: we fill in an FSINFO struct
3206 * @auth_probe: probe the auth flavours
3208 * Returns zero on success, or a negative errno.
3210 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
3211 struct nfs_fsinfo *info,
3217 status = nfs4_lookup_root(server, fhandle, info);
3219 if (auth_probe || status == NFS4ERR_WRONGSEC)
3220 status = nfs4_do_find_root_sec(server, fhandle, info);
3223 status = nfs4_server_capabilities(server, fhandle);
3225 status = nfs4_do_fsinfo(server, fhandle, info);
3227 return nfs4_map_errors(status);
3230 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3231 struct nfs_fsinfo *info)
3234 struct nfs_fattr *fattr = info->fattr;
3235 struct nfs4_label *label = NULL;
3237 error = nfs4_server_capabilities(server, mntfh);
3239 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3243 label = nfs4_label_alloc(server, GFP_KERNEL);
3245 return PTR_ERR(label);
3247 error = nfs4_proc_getattr(server, mntfh, fattr, label);
3249 dprintk("nfs4_get_root: getattr error = %d\n", -error);
3250 goto err_free_label;
3253 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3254 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3255 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3258 nfs4_label_free(label);
3264 * Get locations and (maybe) other attributes of a referral.
3265 * Note that we'll actually follow the referral later when
3266 * we detect fsid mismatch in inode revalidation
3268 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3269 const struct qstr *name, struct nfs_fattr *fattr,
3270 struct nfs_fh *fhandle)
3272 int status = -ENOMEM;
3273 struct page *page = NULL;
3274 struct nfs4_fs_locations *locations = NULL;
3276 page = alloc_page(GFP_KERNEL);
3279 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3280 if (locations == NULL)
3283 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
3288 * If the fsid didn't change, this is a migration event, not a
3289 * referral. Cause us to drop into the exception handler, which
3290 * will kick off migration recovery.
3292 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
3293 dprintk("%s: server did not return a different fsid for"
3294 " a referral at %s\n", __func__, name->name);
3295 status = -NFS4ERR_MOVED;
3298 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3299 nfs_fixup_referral_attributes(&locations->fattr);
3301 /* replace the lookup nfs_fattr with the locations nfs_fattr */
3302 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
3303 memset(fhandle, 0, sizeof(struct nfs_fh));
3311 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3312 struct nfs_fattr *fattr, struct nfs4_label *label)
3314 struct nfs4_getattr_arg args = {
3316 .bitmask = server->attr_bitmask,
3318 struct nfs4_getattr_res res = {
3323 struct rpc_message msg = {
3324 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3329 args.bitmask = nfs4_bitmask(server, label);
3331 nfs_fattr_init(fattr);
3332 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3335 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3336 struct nfs_fattr *fattr, struct nfs4_label *label)
3338 struct nfs4_exception exception = { };
3341 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3342 trace_nfs4_getattr(server, fhandle, fattr, err);
3343 err = nfs4_handle_exception(server, err,
3345 } while (exception.retry);
3350 * The file is not closed if it is opened due to the a request to change
3351 * the size of the file. The open call will not be needed once the
3352 * VFS layer lookup-intents are implemented.
3354 * Close is called when the inode is destroyed.
3355 * If we haven't opened the file for O_WRONLY, we
3356 * need to in the size_change case to obtain a stateid.
3359 * Because OPEN is always done by name in nfsv4, it is
3360 * possible that we opened a different file by the same
3361 * name. We can recognize this race condition, but we
3362 * can't do anything about it besides returning an error.
3364 * This will be fixed with VFS changes (lookup-intent).
3367 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3368 struct iattr *sattr)
3370 struct inode *inode = d_inode(dentry);
3371 struct rpc_cred *cred = NULL;
3372 struct nfs4_state *state = NULL;
3373 struct nfs4_label *label = NULL;
3376 if (pnfs_ld_layoutret_on_setattr(inode) &&
3377 sattr->ia_valid & ATTR_SIZE &&
3378 sattr->ia_size < i_size_read(inode))
3379 pnfs_commit_and_return_layout(inode);
3381 nfs_fattr_init(fattr);
3383 /* Deal with open(O_TRUNC) */
3384 if (sattr->ia_valid & ATTR_OPEN)
3385 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
3387 /* Optimization: if the end result is no change, don't RPC */
3388 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
3391 /* Search for an existing open(O_WRITE) file */
3392 if (sattr->ia_valid & ATTR_FILE) {
3393 struct nfs_open_context *ctx;
3395 ctx = nfs_file_open_context(sattr->ia_file);
3402 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3404 return PTR_ERR(label);
3406 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
3408 nfs_setattr_update_inode(inode, sattr, fattr);
3409 nfs_setsecurity(inode, fattr, label);
3411 nfs4_label_free(label);
3415 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3416 const struct qstr *name, struct nfs_fh *fhandle,
3417 struct nfs_fattr *fattr, struct nfs4_label *label)
3419 struct nfs_server *server = NFS_SERVER(dir);
3421 struct nfs4_lookup_arg args = {
3422 .bitmask = server->attr_bitmask,
3423 .dir_fh = NFS_FH(dir),
3426 struct nfs4_lookup_res res = {
3432 struct rpc_message msg = {
3433 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3438 args.bitmask = nfs4_bitmask(server, label);
3440 nfs_fattr_init(fattr);
3442 dprintk("NFS call lookup %s\n", name->name);
3443 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
3444 dprintk("NFS reply lookup: %d\n", status);
3448 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
3450 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
3451 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
3452 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3456 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3457 struct qstr *name, struct nfs_fh *fhandle,
3458 struct nfs_fattr *fattr, struct nfs4_label *label)
3460 struct nfs4_exception exception = { };
3461 struct rpc_clnt *client = *clnt;
3464 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
3465 trace_nfs4_lookup(dir, name, err);
3467 case -NFS4ERR_BADNAME:
3470 case -NFS4ERR_MOVED:
3471 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
3472 if (err == -NFS4ERR_MOVED)
3473 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3475 case -NFS4ERR_WRONGSEC:
3477 if (client != *clnt)
3479 client = nfs4_negotiate_security(client, dir, name);
3481 return PTR_ERR(client);
3483 exception.retry = 1;
3486 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3488 } while (exception.retry);
3493 else if (client != *clnt)
3494 rpc_shutdown_client(client);
3499 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
3500 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3501 struct nfs4_label *label)
3504 struct rpc_clnt *client = NFS_CLIENT(dir);
3506 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
3507 if (client != NFS_CLIENT(dir)) {
3508 rpc_shutdown_client(client);
3509 nfs_fixup_secinfo_attributes(fattr);
3515 nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
3516 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3518 struct rpc_clnt *client = NFS_CLIENT(dir);
3521 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
3523 return ERR_PTR(status);
3524 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
3527 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3529 struct nfs_server *server = NFS_SERVER(inode);
3530 struct nfs4_accessargs args = {
3531 .fh = NFS_FH(inode),
3532 .bitmask = server->cache_consistency_bitmask,
3534 struct nfs4_accessres res = {
3537 struct rpc_message msg = {
3538 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3541 .rpc_cred = entry->cred,
3543 int mode = entry->mask;
3547 * Determine which access bits we want to ask for...
3549 if (mode & MAY_READ)
3550 args.access |= NFS4_ACCESS_READ;
3551 if (S_ISDIR(inode->i_mode)) {
3552 if (mode & MAY_WRITE)
3553 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3554 if (mode & MAY_EXEC)
3555 args.access |= NFS4_ACCESS_LOOKUP;
3557 if (mode & MAY_WRITE)
3558 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3559 if (mode & MAY_EXEC)
3560 args.access |= NFS4_ACCESS_EXECUTE;
3563 res.fattr = nfs_alloc_fattr();
3564 if (res.fattr == NULL)
3567 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3569 nfs_access_set_mask(entry, res.access);
3570 nfs_refresh_inode(inode, res.fattr);
3572 nfs_free_fattr(res.fattr);
3576 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3578 struct nfs4_exception exception = { };
3581 err = _nfs4_proc_access(inode, entry);
3582 trace_nfs4_access(inode, err);
3583 err = nfs4_handle_exception(NFS_SERVER(inode), err,
3585 } while (exception.retry);
3590 * TODO: For the time being, we don't try to get any attributes
3591 * along with any of the zero-copy operations READ, READDIR,
3594 * In the case of the first three, we want to put the GETATTR
3595 * after the read-type operation -- this is because it is hard
3596 * to predict the length of a GETATTR response in v4, and thus
3597 * align the READ data correctly. This means that the GETATTR
3598 * may end up partially falling into the page cache, and we should
3599 * shift it into the 'tail' of the xdr_buf before processing.
3600 * To do this efficiently, we need to know the total length
3601 * of data received, which doesn't seem to be available outside
3604 * In the case of WRITE, we also want to put the GETATTR after
3605 * the operation -- in this case because we want to make sure
3606 * we get the post-operation mtime and size.
3608 * Both of these changes to the XDR layer would in fact be quite
3609 * minor, but I decided to leave them for a subsequent patch.
3611 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3612 unsigned int pgbase, unsigned int pglen)
3614 struct nfs4_readlink args = {
3615 .fh = NFS_FH(inode),
3620 struct nfs4_readlink_res res;
3621 struct rpc_message msg = {
3622 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3627 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
3630 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3631 unsigned int pgbase, unsigned int pglen)
3633 struct nfs4_exception exception = { };
3636 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3637 trace_nfs4_readlink(inode, err);
3638 err = nfs4_handle_exception(NFS_SERVER(inode), err,
3640 } while (exception.retry);
3645 * This is just for mknod. open(O_CREAT) will always do ->open_context().
3648 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
3651 struct nfs4_label l, *ilabel = NULL;
3652 struct nfs_open_context *ctx;
3653 struct nfs4_state *state;
3656 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3658 return PTR_ERR(ctx);
3660 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3662 sattr->ia_mode &= ~current_umask();
3663 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
3664 if (IS_ERR(state)) {
3665 status = PTR_ERR(state);
3669 nfs4_label_release_security(ilabel);
3670 put_nfs_open_context(ctx);
3674 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3676 struct nfs_server *server = NFS_SERVER(dir);
3677 struct nfs_removeargs args = {
3681 struct nfs_removeres res = {
3684 struct rpc_message msg = {
3685 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3691 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
3693 update_changeattr(dir, &res.cinfo);
3697 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3699 struct nfs4_exception exception = { };
3702 err = _nfs4_proc_remove(dir, name);
3703 trace_nfs4_remove(dir, name, err);
3704 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3706 } while (exception.retry);
3710 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
3712 struct nfs_server *server = NFS_SERVER(dir);
3713 struct nfs_removeargs *args = msg->rpc_argp;
3714 struct nfs_removeres *res = msg->rpc_resp;
3716 res->server = server;
3717 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
3718 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
3720 nfs_fattr_init(res->dir_attr);
3723 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3725 nfs4_setup_sequence(NFS_SERVER(data->dir),
3726 &data->args.seq_args,
3731 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
3733 struct nfs_unlinkdata *data = task->tk_calldata;
3734 struct nfs_removeres *res = &data->res;
3736 if (!nfs4_sequence_done(task, &res->seq_res))
3738 if (nfs4_async_handle_error(task, res->server, NULL,
3739 &data->timeout) == -EAGAIN)
3741 update_changeattr(dir, &res->cinfo);
3745 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3747 struct nfs_server *server = NFS_SERVER(dir);
3748 struct nfs_renameargs *arg = msg->rpc_argp;
3749 struct nfs_renameres *res = msg->rpc_resp;
3751 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
3752 res->server = server;
3753 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
3756 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3758 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3759 &data->args.seq_args,
3764 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3765 struct inode *new_dir)
3767 struct nfs_renamedata *data = task->tk_calldata;
3768 struct nfs_renameres *res = &data->res;
3770 if (!nfs4_sequence_done(task, &res->seq_res))
3772 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
3775 update_changeattr(old_dir, &res->old_cinfo);
3776 update_changeattr(new_dir, &res->new_cinfo);
3780 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3782 struct nfs_server *server = NFS_SERVER(inode);
3783 struct nfs4_link_arg arg = {
3784 .fh = NFS_FH(inode),
3785 .dir_fh = NFS_FH(dir),
3787 .bitmask = server->attr_bitmask,
3789 struct nfs4_link_res res = {
3793 struct rpc_message msg = {
3794 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3798 int status = -ENOMEM;
3800 res.fattr = nfs_alloc_fattr();
3801 if (res.fattr == NULL)
3804 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3805 if (IS_ERR(res.label)) {
3806 status = PTR_ERR(res.label);
3809 arg.bitmask = nfs4_bitmask(server, res.label);
3811 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3813 update_changeattr(dir, &res.cinfo);
3814 status = nfs_post_op_update_inode(inode, res.fattr);
3816 nfs_setsecurity(inode, res.fattr, res.label);
3820 nfs4_label_free(res.label);
3823 nfs_free_fattr(res.fattr);
3827 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3829 struct nfs4_exception exception = { };
3832 err = nfs4_handle_exception(NFS_SERVER(inode),
3833 _nfs4_proc_link(inode, dir, name),
3835 } while (exception.retry);
3839 struct nfs4_createdata {
3840 struct rpc_message msg;
3841 struct nfs4_create_arg arg;
3842 struct nfs4_create_res res;
3844 struct nfs_fattr fattr;
3845 struct nfs4_label *label;
3848 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3849 struct qstr *name, struct iattr *sattr, u32 ftype)
3851 struct nfs4_createdata *data;
3853 data = kzalloc(sizeof(*data), GFP_KERNEL);
3855 struct nfs_server *server = NFS_SERVER(dir);
3857 data->label = nfs4_label_alloc(server, GFP_KERNEL);
3858 if (IS_ERR(data->label))
3861 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3862 data->msg.rpc_argp = &data->arg;
3863 data->msg.rpc_resp = &data->res;
3864 data->arg.dir_fh = NFS_FH(dir);
3865 data->arg.server = server;
3866 data->arg.name = name;
3867 data->arg.attrs = sattr;
3868 data->arg.ftype = ftype;
3869 data->arg.bitmask = nfs4_bitmask(server, data->label);
3870 data->res.server = server;
3871 data->res.fh = &data->fh;
3872 data->res.fattr = &data->fattr;
3873 data->res.label = data->label;
3874 nfs_fattr_init(data->res.fattr);
3882 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3884 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
3885 &data->arg.seq_args, &data->res.seq_res, 1);
3887 update_changeattr(dir, &data->res.dir_cinfo);
3888 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
3893 static void nfs4_free_createdata(struct nfs4_createdata *data)
3895 nfs4_label_free(data->label);
3899 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3900 struct page *page, unsigned int len, struct iattr *sattr,
3901 struct nfs4_label *label)
3903 struct nfs4_createdata *data;
3904 int status = -ENAMETOOLONG;
3906 if (len > NFS4_MAXPATHLEN)
3910 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3914 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3915 data->arg.u.symlink.pages = &page;
3916 data->arg.u.symlink.len = len;
3917 data->arg.label = label;
3919 status = nfs4_do_create(dir, dentry, data);
3921 nfs4_free_createdata(data);
3926 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3927 struct page *page, unsigned int len, struct iattr *sattr)
3929 struct nfs4_exception exception = { };
3930 struct nfs4_label l, *label = NULL;
3933 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3936 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
3937 trace_nfs4_symlink(dir, &dentry->d_name, err);
3938 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3940 } while (exception.retry);
3942 nfs4_label_release_security(label);
3946 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3947 struct iattr *sattr, struct nfs4_label *label)
3949 struct nfs4_createdata *data;
3950 int status = -ENOMEM;
3952 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3956 data->arg.label = label;
3957 status = nfs4_do_create(dir, dentry, data);
3959 nfs4_free_createdata(data);
3964 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3965 struct iattr *sattr)
3967 struct nfs4_exception exception = { };
3968 struct nfs4_label l, *label = NULL;
3971 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3973 sattr->ia_mode &= ~current_umask();
3975 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
3976 trace_nfs4_mkdir(dir, &dentry->d_name, err);
3977 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3979 } while (exception.retry);
3980 nfs4_label_release_security(label);
3985 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3986 u64 cookie, struct page **pages, unsigned int count, int plus)
3988 struct inode *dir = d_inode(dentry);
3989 struct nfs4_readdir_arg args = {
3994 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
3997 struct nfs4_readdir_res res;
3998 struct rpc_message msg = {
3999 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4006 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4008 (unsigned long long)cookie);
4009 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
4010 res.pgbase = args.pgbase;
4011 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4013 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
4014 status += args.pgbase;
4017 nfs_invalidate_atime(dir);
4019 dprintk("%s: returns %d\n", __func__, status);
4023 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
4024 u64 cookie, struct page **pages, unsigned int count, int plus)
4026 struct nfs4_exception exception = { };
4029 err = _nfs4_proc_readdir(dentry, cred, cookie,
4030 pages, count, plus);
4031 trace_nfs4_readdir(d_inode(dentry), err);
4032 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
4034 } while (exception.retry);
4038 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4039 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
4041 struct nfs4_createdata *data;
4042 int mode = sattr->ia_mode;
4043 int status = -ENOMEM;
4045 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4050 data->arg.ftype = NF4FIFO;
4051 else if (S_ISBLK(mode)) {
4052 data->arg.ftype = NF4BLK;
4053 data->arg.u.device.specdata1 = MAJOR(rdev);
4054 data->arg.u.device.specdata2 = MINOR(rdev);
4056 else if (S_ISCHR(mode)) {
4057 data->arg.ftype = NF4CHR;
4058 data->arg.u.device.specdata1 = MAJOR(rdev);
4059 data->arg.u.device.specdata2 = MINOR(rdev);
4060 } else if (!S_ISSOCK(mode)) {
4065 data->arg.label = label;
4066 status = nfs4_do_create(dir, dentry, data);
4068 nfs4_free_createdata(data);
4073 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4074 struct iattr *sattr, dev_t rdev)
4076 struct nfs4_exception exception = { };
4077 struct nfs4_label l, *label = NULL;
4080 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4082 sattr->ia_mode &= ~current_umask();
4084 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4085 trace_nfs4_mknod(dir, &dentry->d_name, err);
4086 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4088 } while (exception.retry);
4090 nfs4_label_release_security(label);
4095 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4096 struct nfs_fsstat *fsstat)
4098 struct nfs4_statfs_arg args = {
4100 .bitmask = server->attr_bitmask,
4102 struct nfs4_statfs_res res = {
4105 struct rpc_message msg = {
4106 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4111 nfs_fattr_init(fsstat->fattr);
4112 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4115 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4117 struct nfs4_exception exception = { };
4120 err = nfs4_handle_exception(server,
4121 _nfs4_proc_statfs(server, fhandle, fsstat),
4123 } while (exception.retry);
4127 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4128 struct nfs_fsinfo *fsinfo)
4130 struct nfs4_fsinfo_arg args = {
4132 .bitmask = server->attr_bitmask,
4134 struct nfs4_fsinfo_res res = {
4137 struct rpc_message msg = {
4138 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4143 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4146 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4148 struct nfs4_exception exception = { };
4149 unsigned long now = jiffies;
4153 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
4154 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
4156 struct nfs_client *clp = server->nfs_client;
4158 spin_lock(&clp->cl_lock);
4159 clp->cl_lease_time = fsinfo->lease_time * HZ;
4160 clp->cl_last_renewal = now;
4161 spin_unlock(&clp->cl_lock);
4164 err = nfs4_handle_exception(server, err, &exception);
4165 } while (exception.retry);
4169 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4173 nfs_fattr_init(fsinfo->fattr);
4174 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
4176 /* block layout checks this! */
4177 server->pnfs_blksize = fsinfo->blksize;
4178 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
4184 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4185 struct nfs_pathconf *pathconf)
4187 struct nfs4_pathconf_arg args = {
4189 .bitmask = server->attr_bitmask,
4191 struct nfs4_pathconf_res res = {
4192 .pathconf = pathconf,
4194 struct rpc_message msg = {
4195 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4200 /* None of the pathconf attributes are mandatory to implement */
4201 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4202 memset(pathconf, 0, sizeof(*pathconf));
4206 nfs_fattr_init(pathconf->fattr);
4207 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4210 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4211 struct nfs_pathconf *pathconf)
4213 struct nfs4_exception exception = { };
4217 err = nfs4_handle_exception(server,
4218 _nfs4_proc_pathconf(server, fhandle, pathconf),
4220 } while (exception.retry);
4224 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
4225 const struct nfs_open_context *ctx,
4226 const struct nfs_lock_context *l_ctx,
4229 const struct nfs_lockowner *lockowner = NULL;
4232 lockowner = &l_ctx->lockowner;
4233 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
4235 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4237 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4238 const struct nfs_open_context *ctx,
4239 const struct nfs_lock_context *l_ctx,
4242 nfs4_stateid current_stateid;
4244 /* If the current stateid represents a lost lock, then exit */
4245 if (nfs4_set_rw_stateid(¤t_stateid, ctx, l_ctx, fmode) == -EIO)
4247 return nfs4_stateid_match(stateid, ¤t_stateid);
4250 static bool nfs4_error_stateid_expired(int err)
4253 case -NFS4ERR_DELEG_REVOKED:
4254 case -NFS4ERR_ADMIN_REVOKED:
4255 case -NFS4ERR_BAD_STATEID:
4256 case -NFS4ERR_STALE_STATEID:
4257 case -NFS4ERR_OLD_STATEID:
4258 case -NFS4ERR_OPENMODE:
4259 case -NFS4ERR_EXPIRED:
4265 void __nfs4_read_done_cb(struct nfs_pgio_header *hdr)
4267 nfs_invalidate_atime(hdr->inode);
4270 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
4272 struct nfs_server *server = NFS_SERVER(hdr->inode);
4274 trace_nfs4_read(hdr, task->tk_status);
4275 if (nfs4_async_handle_error(task, server,
4276 hdr->args.context->state,
4278 rpc_restart_call_prepare(task);
4282 __nfs4_read_done_cb(hdr);
4283 if (task->tk_status > 0)
4284 renew_lease(server, hdr->timestamp);
4288 static bool nfs4_read_stateid_changed(struct rpc_task *task,
4289 struct nfs_pgio_args *args)
4292 if (!nfs4_error_stateid_expired(task->tk_status) ||
4293 nfs4_stateid_is_current(&args->stateid,
4298 rpc_restart_call_prepare(task);
4302 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
4305 dprintk("--> %s\n", __func__);
4307 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
4309 if (nfs4_read_stateid_changed(task, &hdr->args))
4311 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4312 nfs4_read_done_cb(task, hdr);
4315 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4316 struct rpc_message *msg)
4318 hdr->timestamp = jiffies;
4319 hdr->pgio_done_cb = nfs4_read_done_cb;
4320 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
4321 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
4324 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4325 struct nfs_pgio_header *hdr)
4327 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4328 &hdr->args.seq_args,
4332 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4333 hdr->args.lock_context,
4334 hdr->rw_ops->rw_mode) == -EIO)
4336 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
4341 static int nfs4_write_done_cb(struct rpc_task *task,
4342 struct nfs_pgio_header *hdr)
4344 struct inode *inode = hdr->inode;
4346 trace_nfs4_write(hdr, task->tk_status);
4347 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4348 hdr->args.context->state,
4350 rpc_restart_call_prepare(task);
4353 if (task->tk_status >= 0) {
4354 renew_lease(NFS_SERVER(inode), hdr->timestamp);
4355 nfs_writeback_update_inode(hdr);
4360 static bool nfs4_write_stateid_changed(struct rpc_task *task,
4361 struct nfs_pgio_args *args)
4364 if (!nfs4_error_stateid_expired(task->tk_status) ||
4365 nfs4_stateid_is_current(&args->stateid,
4370 rpc_restart_call_prepare(task);
4374 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
4376 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
4378 if (nfs4_write_stateid_changed(task, &hdr->args))
4380 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4381 nfs4_write_done_cb(task, hdr);
4385 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
4387 /* Don't request attributes for pNFS or O_DIRECT writes */
4388 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
4390 /* Otherwise, request attributes if and only if we don't hold
4393 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
4396 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4397 struct rpc_message *msg)
4399 struct nfs_server *server = NFS_SERVER(hdr->inode);
4401 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4402 hdr->args.bitmask = NULL;
4403 hdr->res.fattr = NULL;
4405 hdr->args.bitmask = server->cache_consistency_bitmask;
4407 if (!hdr->pgio_done_cb)
4408 hdr->pgio_done_cb = nfs4_write_done_cb;
4409 hdr->res.server = server;
4410 hdr->timestamp = jiffies;
4412 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
4413 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
4416 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4418 nfs4_setup_sequence(NFS_SERVER(data->inode),
4419 &data->args.seq_args,
4424 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
4426 struct inode *inode = data->inode;
4428 trace_nfs4_commit(data, task->tk_status);
4429 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4430 NULL, NULL) == -EAGAIN) {
4431 rpc_restart_call_prepare(task);
4437 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
4439 if (!nfs4_sequence_done(task, &data->res.seq_res))
4441 return data->commit_done_cb(task, data);
4444 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
4446 struct nfs_server *server = NFS_SERVER(data->inode);
4448 if (data->commit_done_cb == NULL)
4449 data->commit_done_cb = nfs4_commit_done_cb;
4450 data->res.server = server;
4451 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
4452 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4455 struct nfs4_renewdata {
4456 struct nfs_client *client;
4457 unsigned long timestamp;
4461 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4462 * standalone procedure for queueing an asynchronous RENEW.
4464 static void nfs4_renew_release(void *calldata)
4466 struct nfs4_renewdata *data = calldata;
4467 struct nfs_client *clp = data->client;
4469 if (atomic_read(&clp->cl_count) > 1)
4470 nfs4_schedule_state_renewal(clp);
4471 nfs_put_client(clp);
4475 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
4477 struct nfs4_renewdata *data = calldata;
4478 struct nfs_client *clp = data->client;
4479 unsigned long timestamp = data->timestamp;
4481 trace_nfs4_renew_async(clp, task->tk_status);
4482 switch (task->tk_status) {
4485 case -NFS4ERR_LEASE_MOVED:
4486 nfs4_schedule_lease_moved_recovery(clp);
4489 /* Unless we're shutting down, schedule state recovery! */
4490 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4492 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
4493 nfs4_schedule_lease_recovery(clp);
4496 nfs4_schedule_path_down_recovery(clp);
4498 do_renew_lease(clp, timestamp);
4501 static const struct rpc_call_ops nfs4_renew_ops = {
4502 .rpc_call_done = nfs4_renew_done,
4503 .rpc_release = nfs4_renew_release,
4506 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
4508 struct rpc_message msg = {
4509 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4513 struct nfs4_renewdata *data;
4515 if (renew_flags == 0)
4517 if (!atomic_inc_not_zero(&clp->cl_count))
4519 data = kmalloc(sizeof(*data), GFP_NOFS);
4523 data->timestamp = jiffies;
4524 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
4525 &nfs4_renew_ops, data);
4528 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
4530 struct rpc_message msg = {
4531 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4535 unsigned long now = jiffies;
4538 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4541 do_renew_lease(clp, now);
4545 static inline int nfs4_server_supports_acls(struct nfs_server *server)
4547 return server->caps & NFS_CAP_ACLS;
4550 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4551 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
4554 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
4556 static int buf_to_pages_noslab(const void *buf, size_t buflen,
4557 struct page **pages, unsigned int *pgbase)
4559 struct page *newpage, **spages;
4565 len = min_t(size_t, PAGE_SIZE, buflen);
4566 newpage = alloc_page(GFP_KERNEL);
4568 if (newpage == NULL)
4570 memcpy(page_address(newpage), buf, len);
4575 } while (buflen != 0);
4581 __free_page(spages[rc-1]);
4585 struct nfs4_cached_acl {
4591 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
4593 struct nfs_inode *nfsi = NFS_I(inode);
4595 spin_lock(&inode->i_lock);
4596 kfree(nfsi->nfs4_acl);
4597 nfsi->nfs4_acl = acl;
4598 spin_unlock(&inode->i_lock);
4601 static void nfs4_zap_acl_attr(struct inode *inode)
4603 nfs4_set_cached_acl(inode, NULL);
4606 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4608 struct nfs_inode *nfsi = NFS_I(inode);
4609 struct nfs4_cached_acl *acl;
4612 spin_lock(&inode->i_lock);
4613 acl = nfsi->nfs4_acl;
4616 if (buf == NULL) /* user is just asking for length */
4618 if (acl->cached == 0)
4620 ret = -ERANGE; /* see getxattr(2) man page */
4621 if (acl->len > buflen)
4623 memcpy(buf, acl->data, acl->len);
4627 spin_unlock(&inode->i_lock);
4631 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
4633 struct nfs4_cached_acl *acl;
4634 size_t buflen = sizeof(*acl) + acl_len;
4636 if (buflen <= PAGE_SIZE) {
4637 acl = kmalloc(buflen, GFP_KERNEL);
4641 _copy_from_pages(acl->data, pages, pgbase, acl_len);
4643 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4650 nfs4_set_cached_acl(inode, acl);
4654 * The getxattr API returns the required buffer length when called with a
4655 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4656 * the required buf. On a NULL buf, we send a page of data to the server
4657 * guessing that the ACL request can be serviced by a page. If so, we cache
4658 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4659 * the cache. If not so, we throw away the page, and cache the required
4660 * length. The next getxattr call will then produce another round trip to
4661 * the server, this time with the input buf of the required size.
4663 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4665 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
4666 struct nfs_getaclargs args = {
4667 .fh = NFS_FH(inode),
4671 struct nfs_getaclres res = {
4674 struct rpc_message msg = {
4675 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4679 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4680 int ret = -ENOMEM, i;
4682 /* As long as we're doing a round trip to the server anyway,
4683 * let's be prepared for a page of acl data. */
4686 if (npages > ARRAY_SIZE(pages))
4689 for (i = 0; i < npages; i++) {
4690 pages[i] = alloc_page(GFP_KERNEL);
4695 /* for decoding across pages */
4696 res.acl_scratch = alloc_page(GFP_KERNEL);
4697 if (!res.acl_scratch)
4700 args.acl_len = npages * PAGE_SIZE;
4701 args.acl_pgbase = 0;
4703 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
4704 __func__, buf, buflen, npages, args.acl_len);
4705 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4706 &msg, &args.seq_args, &res.seq_res, 0);
4710 /* Handle the case where the passed-in buffer is too short */
4711 if (res.acl_flags & NFS4_ACL_TRUNC) {
4712 /* Did the user only issue a request for the acl length? */
4718 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
4720 if (res.acl_len > buflen) {
4724 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
4729 for (i = 0; i < npages; i++)
4731 __free_page(pages[i]);
4732 if (res.acl_scratch)
4733 __free_page(res.acl_scratch);
4737 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4739 struct nfs4_exception exception = { };
4742 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
4743 trace_nfs4_get_acl(inode, ret);
4746 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4747 } while (exception.retry);
4751 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4753 struct nfs_server *server = NFS_SERVER(inode);
4756 if (!nfs4_server_supports_acls(server))
4758 ret = nfs_revalidate_inode(server, inode);
4761 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4762 nfs_zap_acl_cache(inode);
4763 ret = nfs4_read_cached_acl(inode, buf, buflen);
4765 /* -ENOENT is returned if there is no ACL or if there is an ACL
4766 * but no cached acl data, just the acl length */
4768 return nfs4_get_acl_uncached(inode, buf, buflen);
4771 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4773 struct nfs_server *server = NFS_SERVER(inode);
4774 struct page *pages[NFS4ACL_MAXPAGES];
4775 struct nfs_setaclargs arg = {
4776 .fh = NFS_FH(inode),
4780 struct nfs_setaclres res;
4781 struct rpc_message msg = {
4782 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4786 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4789 if (!nfs4_server_supports_acls(server))
4791 if (npages > ARRAY_SIZE(pages))
4793 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4796 nfs4_inode_return_delegation(inode);
4797 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4800 * Free each page after tx, so the only ref left is
4801 * held by the network stack
4804 put_page(pages[i-1]);
4807 * Acl update can result in inode attribute update.
4808 * so mark the attribute cache invalid.
4810 spin_lock(&inode->i_lock);
4811 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4812 spin_unlock(&inode->i_lock);
4813 nfs_access_zap_cache(inode);
4814 nfs_zap_acl_cache(inode);
4818 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4820 struct nfs4_exception exception = { };
4823 err = __nfs4_proc_set_acl(inode, buf, buflen);
4824 trace_nfs4_set_acl(inode, err);
4825 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4827 } while (exception.retry);
4831 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
4832 static int _nfs4_get_security_label(struct inode *inode, void *buf,
4835 struct nfs_server *server = NFS_SERVER(inode);
4836 struct nfs_fattr fattr;
4837 struct nfs4_label label = {0, 0, buflen, buf};
4839 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4840 struct nfs4_getattr_arg arg = {
4841 .fh = NFS_FH(inode),
4844 struct nfs4_getattr_res res = {
4849 struct rpc_message msg = {
4850 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4856 nfs_fattr_init(&fattr);
4858 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
4861 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
4863 if (buflen < label.len)
4868 static int nfs4_get_security_label(struct inode *inode, void *buf,
4871 struct nfs4_exception exception = { };
4874 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4878 err = _nfs4_get_security_label(inode, buf, buflen);
4879 trace_nfs4_get_security_label(inode, err);
4880 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4882 } while (exception.retry);
4886 static int _nfs4_do_set_security_label(struct inode *inode,
4887 struct nfs4_label *ilabel,
4888 struct nfs_fattr *fattr,
4889 struct nfs4_label *olabel)
4892 struct iattr sattr = {0};
4893 struct nfs_server *server = NFS_SERVER(inode);
4894 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4895 struct nfs_setattrargs arg = {
4896 .fh = NFS_FH(inode),
4902 struct nfs_setattrres res = {
4907 struct rpc_message msg = {
4908 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
4914 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
4916 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4918 dprintk("%s failed: %d\n", __func__, status);
4923 static int nfs4_do_set_security_label(struct inode *inode,
4924 struct nfs4_label *ilabel,
4925 struct nfs_fattr *fattr,
4926 struct nfs4_label *olabel)
4928 struct nfs4_exception exception = { };
4932 err = _nfs4_do_set_security_label(inode, ilabel,
4934 trace_nfs4_set_security_label(inode, err);
4935 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4937 } while (exception.retry);
4942 nfs4_set_security_label(struct dentry *dentry, const void *buf, size_t buflen)
4944 struct nfs4_label ilabel, *olabel = NULL;
4945 struct nfs_fattr fattr;
4946 struct rpc_cred *cred;
4947 struct inode *inode = d_inode(dentry);
4950 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4953 nfs_fattr_init(&fattr);
4957 ilabel.label = (char *)buf;
4958 ilabel.len = buflen;
4960 cred = rpc_lookup_cred();
4962 return PTR_ERR(cred);
4964 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4965 if (IS_ERR(olabel)) {
4966 status = -PTR_ERR(olabel);
4970 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
4972 nfs_setsecurity(inode, &fattr, olabel);
4974 nfs4_label_free(olabel);
4979 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
4983 nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
4984 struct nfs4_state *state, long *timeout)
4986 struct nfs_client *clp = server->nfs_client;
4988 if (task->tk_status >= 0)
4990 switch(task->tk_status) {
4991 case -NFS4ERR_DELEG_REVOKED:
4992 case -NFS4ERR_ADMIN_REVOKED:
4993 case -NFS4ERR_BAD_STATEID:
4994 case -NFS4ERR_OPENMODE:
4997 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4998 goto recovery_failed;
4999 goto wait_on_recovery;
5000 case -NFS4ERR_EXPIRED:
5001 if (state != NULL) {
5002 if (nfs4_schedule_stateid_recovery(server, state) < 0)
5003 goto recovery_failed;
5005 case -NFS4ERR_STALE_STATEID:
5006 case -NFS4ERR_STALE_CLIENTID:
5007 nfs4_schedule_lease_recovery(clp);
5008 goto wait_on_recovery;
5009 case -NFS4ERR_MOVED:
5010 if (nfs4_schedule_migration_recovery(server) < 0)
5011 goto recovery_failed;
5012 goto wait_on_recovery;
5013 case -NFS4ERR_LEASE_MOVED:
5014 nfs4_schedule_lease_moved_recovery(clp);
5015 goto wait_on_recovery;
5016 #if defined(CONFIG_NFS_V4_1)
5017 case -NFS4ERR_BADSESSION:
5018 case -NFS4ERR_BADSLOT:
5019 case -NFS4ERR_BAD_HIGH_SLOT:
5020 case -NFS4ERR_DEADSESSION:
5021 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
5022 case -NFS4ERR_SEQ_FALSE_RETRY:
5023 case -NFS4ERR_SEQ_MISORDERED:
5024 dprintk("%s ERROR %d, Reset session\n", __func__,
5026 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
5027 goto wait_on_recovery;
5028 #endif /* CONFIG_NFS_V4_1 */
5029 case -NFS4ERR_DELAY:
5030 nfs_inc_server_stats(server, NFSIOS_DELAY);
5031 rpc_delay(task, nfs4_update_delay(timeout));
5033 case -NFS4ERR_GRACE:
5034 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5035 case -NFS4ERR_RETRY_UNCACHED_REP:
5036 case -NFS4ERR_OLD_STATEID:
5039 task->tk_status = nfs4_map_errors(task->tk_status);
5042 task->tk_status = -EIO;
5045 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
5046 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
5047 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
5048 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
5049 goto recovery_failed;
5051 task->tk_status = 0;
5055 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5056 nfs4_verifier *bootverf)
5060 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5061 /* An impossible timestamp guarantees this value
5062 * will never match a generated boot time. */
5064 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
5066 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
5067 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
5068 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
5070 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5074 nfs4_init_nonuniform_client_string(struct nfs_client *clp)
5080 if (clp->cl_owner_id != NULL)
5084 len = 14 + strlen(clp->cl_ipaddr) + 1 +
5085 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5087 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5091 if (len > NFS4_OPAQUE_LIMIT + 1)
5095 * Since this string is allocated at mount time, and held until the
5096 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5097 * about a memory-reclaim deadlock.
5099 str = kmalloc(len, GFP_KERNEL);
5104 result = scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
5106 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5107 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
5110 clp->cl_owner_id = str;
5115 nfs4_init_uniquifier_client_string(struct nfs_client *clp)
5121 len = 10 + 10 + 1 + 10 + 1 +
5122 strlen(nfs4_client_id_uniquifier) + 1 +
5123 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5125 if (len > NFS4_OPAQUE_LIMIT + 1)
5129 * Since this string is allocated at mount time, and held until the
5130 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5131 * about a memory-reclaim deadlock.
5133 str = kmalloc(len, GFP_KERNEL);
5137 result = scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
5138 clp->rpc_ops->version, clp->cl_minorversion,
5139 nfs4_client_id_uniquifier,
5140 clp->cl_rpcclient->cl_nodename);
5141 clp->cl_owner_id = str;
5146 nfs4_init_uniform_client_string(struct nfs_client *clp)
5152 if (clp->cl_owner_id != NULL)
5155 if (nfs4_client_id_uniquifier[0] != '\0')
5156 return nfs4_init_uniquifier_client_string(clp);
5158 len = 10 + 10 + 1 + 10 + 1 +
5159 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5161 if (len > NFS4_OPAQUE_LIMIT + 1)
5165 * Since this string is allocated at mount time, and held until the
5166 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5167 * about a memory-reclaim deadlock.
5169 str = kmalloc(len, GFP_KERNEL);
5173 result = scnprintf(str, len, "Linux NFSv%u.%u %s",
5174 clp->rpc_ops->version, clp->cl_minorversion,
5175 clp->cl_rpcclient->cl_nodename);
5176 clp->cl_owner_id = str;
5181 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5182 * services. Advertise one based on the address family of the
5186 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5188 if (strchr(clp->cl_ipaddr, ':') != NULL)
5189 return scnprintf(buf, len, "tcp6");
5191 return scnprintf(buf, len, "tcp");
5194 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5196 struct nfs4_setclientid *sc = calldata;
5198 if (task->tk_status == 0)
5199 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5202 static const struct rpc_call_ops nfs4_setclientid_ops = {
5203 .rpc_call_done = nfs4_setclientid_done,
5207 * nfs4_proc_setclientid - Negotiate client ID
5208 * @clp: state data structure
5209 * @program: RPC program for NFSv4 callback service
5210 * @port: IP port number for NFS4 callback service
5211 * @cred: RPC credential to use for this call
5212 * @res: where to place the result
5214 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5216 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5217 unsigned short port, struct rpc_cred *cred,
5218 struct nfs4_setclientid_res *res)
5220 nfs4_verifier sc_verifier;
5221 struct nfs4_setclientid setclientid = {
5222 .sc_verifier = &sc_verifier,
5226 struct rpc_message msg = {
5227 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5228 .rpc_argp = &setclientid,
5232 struct rpc_task *task;
5233 struct rpc_task_setup task_setup_data = {
5234 .rpc_client = clp->cl_rpcclient,
5235 .rpc_message = &msg,
5236 .callback_ops = &nfs4_setclientid_ops,
5237 .callback_data = &setclientid,
5238 .flags = RPC_TASK_TIMEOUT,
5242 /* nfs_client_id4 */
5243 nfs4_init_boot_verifier(clp, &sc_verifier);
5245 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5246 status = nfs4_init_uniform_client_string(clp);
5248 status = nfs4_init_nonuniform_client_string(clp);
5254 setclientid.sc_netid_len =
5255 nfs4_init_callback_netid(clp,
5256 setclientid.sc_netid,
5257 sizeof(setclientid.sc_netid));
5258 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
5259 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
5260 clp->cl_ipaddr, port >> 8, port & 255);
5262 dprintk("NFS call setclientid auth=%s, '%s'\n",
5263 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5265 task = rpc_run_task(&task_setup_data);
5267 status = PTR_ERR(task);
5270 status = task->tk_status;
5271 if (setclientid.sc_cred) {
5272 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5273 put_rpccred(setclientid.sc_cred);
5277 trace_nfs4_setclientid(clp, status);
5278 dprintk("NFS reply setclientid: %d\n", status);
5283 * nfs4_proc_setclientid_confirm - Confirm client ID
5284 * @clp: state data structure
5285 * @res: result of a previous SETCLIENTID
5286 * @cred: RPC credential to use for this call
5288 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5290 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
5291 struct nfs4_setclientid_res *arg,
5292 struct rpc_cred *cred)
5294 struct rpc_message msg = {
5295 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
5301 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5302 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5304 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5305 trace_nfs4_setclientid_confirm(clp, status);
5306 dprintk("NFS reply setclientid_confirm: %d\n", status);
5310 struct nfs4_delegreturndata {
5311 struct nfs4_delegreturnargs args;
5312 struct nfs4_delegreturnres res;
5314 nfs4_stateid stateid;
5315 unsigned long timestamp;
5316 struct nfs_fattr fattr;
5318 struct inode *inode;
5323 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5325 struct nfs4_delegreturndata *data = calldata;
5327 if (!nfs4_sequence_done(task, &data->res.seq_res))
5330 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
5331 switch (task->tk_status) {
5333 renew_lease(data->res.server, data->timestamp);
5334 case -NFS4ERR_ADMIN_REVOKED:
5335 case -NFS4ERR_DELEG_REVOKED:
5336 case -NFS4ERR_BAD_STATEID:
5337 case -NFS4ERR_OLD_STATEID:
5338 case -NFS4ERR_STALE_STATEID:
5339 case -NFS4ERR_EXPIRED:
5340 task->tk_status = 0;
5342 pnfs_roc_set_barrier(data->inode, data->roc_barrier);
5345 if (nfs4_async_handle_error(task, data->res.server,
5346 NULL, NULL) == -EAGAIN) {
5347 rpc_restart_call_prepare(task);
5351 data->rpc_status = task->tk_status;
5354 static void nfs4_delegreturn_release(void *calldata)
5356 struct nfs4_delegreturndata *data = calldata;
5357 struct inode *inode = data->inode;
5361 pnfs_roc_release(inode);
5362 nfs_iput_and_deactive(inode);
5367 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5369 struct nfs4_delegreturndata *d_data;
5371 d_data = (struct nfs4_delegreturndata *)data;
5373 if (nfs4_wait_on_layoutreturn(d_data->inode, task))
5377 pnfs_roc_get_barrier(d_data->inode, &d_data->roc_barrier);
5379 nfs4_setup_sequence(d_data->res.server,
5380 &d_data->args.seq_args,
5381 &d_data->res.seq_res,
5385 static const struct rpc_call_ops nfs4_delegreturn_ops = {
5386 .rpc_call_prepare = nfs4_delegreturn_prepare,
5387 .rpc_call_done = nfs4_delegreturn_done,
5388 .rpc_release = nfs4_delegreturn_release,
5391 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
5393 struct nfs4_delegreturndata *data;
5394 struct nfs_server *server = NFS_SERVER(inode);
5395 struct rpc_task *task;
5396 struct rpc_message msg = {
5397 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5400 struct rpc_task_setup task_setup_data = {
5401 .rpc_client = server->client,
5402 .rpc_message = &msg,
5403 .callback_ops = &nfs4_delegreturn_ops,
5404 .flags = RPC_TASK_ASYNC,
5408 data = kzalloc(sizeof(*data), GFP_NOFS);
5411 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
5412 data->args.fhandle = &data->fh;
5413 data->args.stateid = &data->stateid;
5414 data->args.bitmask = server->cache_consistency_bitmask;
5415 nfs_copy_fh(&data->fh, NFS_FH(inode));
5416 nfs4_stateid_copy(&data->stateid, stateid);
5417 data->res.fattr = &data->fattr;
5418 data->res.server = server;
5419 nfs_fattr_init(data->res.fattr);
5420 data->timestamp = jiffies;
5421 data->rpc_status = 0;
5422 data->inode = nfs_igrab_and_active(inode);
5424 data->roc = nfs4_roc(inode);
5426 task_setup_data.callback_data = data;
5427 msg.rpc_argp = &data->args;
5428 msg.rpc_resp = &data->res;
5429 task = rpc_run_task(&task_setup_data);
5431 return PTR_ERR(task);
5434 status = nfs4_wait_for_completion_rpc_task(task);
5437 status = data->rpc_status;
5439 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5441 nfs_refresh_inode(inode, &data->fattr);
5447 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
5449 struct nfs_server *server = NFS_SERVER(inode);
5450 struct nfs4_exception exception = { };
5453 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
5454 trace_nfs4_delegreturn(inode, err);
5456 case -NFS4ERR_STALE_STATEID:
5457 case -NFS4ERR_EXPIRED:
5461 err = nfs4_handle_exception(server, err, &exception);
5462 } while (exception.retry);
5466 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
5467 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
5470 * sleep, with exponential backoff, and retry the LOCK operation.
5472 static unsigned long
5473 nfs4_set_lock_task_retry(unsigned long timeout)
5475 freezable_schedule_timeout_killable_unsafe(timeout);
5477 if (timeout > NFS4_LOCK_MAXTIMEOUT)
5478 return NFS4_LOCK_MAXTIMEOUT;
5482 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5484 struct inode *inode = state->inode;
5485 struct nfs_server *server = NFS_SERVER(inode);
5486 struct nfs_client *clp = server->nfs_client;
5487 struct nfs_lockt_args arg = {
5488 .fh = NFS_FH(inode),
5491 struct nfs_lockt_res res = {
5494 struct rpc_message msg = {
5495 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5498 .rpc_cred = state->owner->so_cred,
5500 struct nfs4_lock_state *lsp;
5503 arg.lock_owner.clientid = clp->cl_clientid;
5504 status = nfs4_set_lock_state(state, request);
5507 lsp = request->fl_u.nfs4_fl.owner;
5508 arg.lock_owner.id = lsp->ls_seqid.owner_id;
5509 arg.lock_owner.s_dev = server->s_dev;
5510 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5513 request->fl_type = F_UNLCK;
5515 case -NFS4ERR_DENIED:
5518 request->fl_ops->fl_release_private(request);
5519 request->fl_ops = NULL;
5524 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5526 struct nfs4_exception exception = { };
5530 err = _nfs4_proc_getlk(state, cmd, request);
5531 trace_nfs4_get_lock(request, state, cmd, err);
5532 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
5534 } while (exception.retry);
5538 static int do_vfs_lock(struct inode *inode, struct file_lock *fl)
5541 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
5543 res = posix_lock_inode_wait(inode, fl);
5546 res = flock_lock_inode_wait(inode, fl);
5554 struct nfs4_unlockdata {
5555 struct nfs_locku_args arg;
5556 struct nfs_locku_res res;
5557 struct nfs4_lock_state *lsp;
5558 struct nfs_open_context *ctx;
5559 struct file_lock fl;
5560 struct nfs_server *server;
5561 unsigned long timestamp;
5564 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5565 struct nfs_open_context *ctx,
5566 struct nfs4_lock_state *lsp,
5567 struct nfs_seqid *seqid)
5569 struct nfs4_unlockdata *p;
5570 struct inode *inode = lsp->ls_state->inode;
5572 p = kzalloc(sizeof(*p), GFP_NOFS);
5575 p->arg.fh = NFS_FH(inode);
5577 p->arg.seqid = seqid;
5578 p->res.seqid = seqid;
5580 atomic_inc(&lsp->ls_count);
5581 /* Ensure we don't close file until we're done freeing locks! */
5582 p->ctx = get_nfs_open_context(ctx);
5583 memcpy(&p->fl, fl, sizeof(p->fl));
5584 p->server = NFS_SERVER(inode);
5588 static void nfs4_locku_release_calldata(void *data)
5590 struct nfs4_unlockdata *calldata = data;
5591 nfs_free_seqid(calldata->arg.seqid);
5592 nfs4_put_lock_state(calldata->lsp);
5593 put_nfs_open_context(calldata->ctx);
5597 static void nfs4_locku_done(struct rpc_task *task, void *data)
5599 struct nfs4_unlockdata *calldata = data;
5601 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5603 switch (task->tk_status) {
5605 renew_lease(calldata->server, calldata->timestamp);
5606 do_vfs_lock(calldata->lsp->ls_state->inode, &calldata->fl);
5607 if (nfs4_update_lock_stateid(calldata->lsp,
5608 &calldata->res.stateid))
5610 case -NFS4ERR_BAD_STATEID:
5611 case -NFS4ERR_OLD_STATEID:
5612 case -NFS4ERR_STALE_STATEID:
5613 case -NFS4ERR_EXPIRED:
5614 if (!nfs4_stateid_match(&calldata->arg.stateid,
5615 &calldata->lsp->ls_stateid))
5616 rpc_restart_call_prepare(task);
5619 if (nfs4_async_handle_error(task, calldata->server,
5620 NULL, NULL) == -EAGAIN)
5621 rpc_restart_call_prepare(task);
5623 nfs_release_seqid(calldata->arg.seqid);
5626 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
5628 struct nfs4_unlockdata *calldata = data;
5630 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
5632 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
5633 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
5634 /* Note: exit _without_ running nfs4_locku_done */
5637 calldata->timestamp = jiffies;
5638 if (nfs4_setup_sequence(calldata->server,
5639 &calldata->arg.seq_args,
5640 &calldata->res.seq_res,
5642 nfs_release_seqid(calldata->arg.seqid);
5645 task->tk_action = NULL;
5647 nfs4_sequence_done(task, &calldata->res.seq_res);
5650 static const struct rpc_call_ops nfs4_locku_ops = {
5651 .rpc_call_prepare = nfs4_locku_prepare,
5652 .rpc_call_done = nfs4_locku_done,
5653 .rpc_release = nfs4_locku_release_calldata,
5656 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5657 struct nfs_open_context *ctx,
5658 struct nfs4_lock_state *lsp,
5659 struct nfs_seqid *seqid)
5661 struct nfs4_unlockdata *data;
5662 struct rpc_message msg = {
5663 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5664 .rpc_cred = ctx->cred,
5666 struct rpc_task_setup task_setup_data = {
5667 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
5668 .rpc_message = &msg,
5669 .callback_ops = &nfs4_locku_ops,
5670 .workqueue = nfsiod_workqueue,
5671 .flags = RPC_TASK_ASYNC,
5674 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5675 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5677 /* Ensure this is an unlock - when canceling a lock, the
5678 * canceled lock is passed in, and it won't be an unlock.
5680 fl->fl_type = F_UNLCK;
5682 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5684 nfs_free_seqid(seqid);
5685 return ERR_PTR(-ENOMEM);
5688 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
5689 msg.rpc_argp = &data->arg;
5690 msg.rpc_resp = &data->res;
5691 task_setup_data.callback_data = data;
5692 return rpc_run_task(&task_setup_data);
5695 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5697 struct inode *inode = state->inode;
5698 struct nfs4_state_owner *sp = state->owner;
5699 struct nfs_inode *nfsi = NFS_I(inode);
5700 struct nfs_seqid *seqid;
5701 struct nfs4_lock_state *lsp;
5702 struct rpc_task *task;
5703 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
5705 unsigned char fl_flags = request->fl_flags;
5707 status = nfs4_set_lock_state(state, request);
5708 /* Unlock _before_ we do the RPC call */
5709 request->fl_flags |= FL_EXISTS;
5710 /* Exclude nfs_delegation_claim_locks() */
5711 mutex_lock(&sp->so_delegreturn_mutex);
5712 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
5713 down_read(&nfsi->rwsem);
5714 if (do_vfs_lock(inode, request) == -ENOENT) {
5715 up_read(&nfsi->rwsem);
5716 mutex_unlock(&sp->so_delegreturn_mutex);
5719 up_read(&nfsi->rwsem);
5720 mutex_unlock(&sp->so_delegreturn_mutex);
5723 /* Is this a delegated lock? */
5724 lsp = request->fl_u.nfs4_fl.owner;
5725 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5727 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
5728 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
5732 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
5733 status = PTR_ERR(task);
5736 status = nfs4_wait_for_completion_rpc_task(task);
5739 request->fl_flags = fl_flags;
5740 trace_nfs4_unlock(request, state, F_SETLK, status);
5744 struct nfs4_lockdata {
5745 struct nfs_lock_args arg;
5746 struct nfs_lock_res res;
5747 struct nfs4_lock_state *lsp;
5748 struct nfs_open_context *ctx;
5749 struct file_lock fl;
5750 unsigned long timestamp;
5753 struct nfs_server *server;
5756 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
5757 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5760 struct nfs4_lockdata *p;
5761 struct inode *inode = lsp->ls_state->inode;
5762 struct nfs_server *server = NFS_SERVER(inode);
5763 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
5765 p = kzalloc(sizeof(*p), gfp_mask);
5769 p->arg.fh = NFS_FH(inode);
5771 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
5772 if (IS_ERR(p->arg.open_seqid))
5774 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
5775 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
5776 if (IS_ERR(p->arg.lock_seqid))
5777 goto out_free_seqid;
5778 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
5779 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
5780 p->arg.lock_owner.s_dev = server->s_dev;
5781 p->res.lock_seqid = p->arg.lock_seqid;
5784 atomic_inc(&lsp->ls_count);
5785 p->ctx = get_nfs_open_context(ctx);
5786 get_file(fl->fl_file);
5787 memcpy(&p->fl, fl, sizeof(p->fl));
5790 nfs_free_seqid(p->arg.open_seqid);
5796 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5798 struct nfs4_lockdata *data = calldata;
5799 struct nfs4_state *state = data->lsp->ls_state;
5801 dprintk("%s: begin!\n", __func__);
5802 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
5804 /* Do we need to do an open_to_lock_owner? */
5805 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
5806 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
5807 goto out_release_lock_seqid;
5809 nfs4_stateid_copy(&data->arg.open_stateid,
5810 &state->open_stateid);
5811 data->arg.new_lock_owner = 1;
5812 data->res.open_seqid = data->arg.open_seqid;
5814 data->arg.new_lock_owner = 0;
5815 nfs4_stateid_copy(&data->arg.lock_stateid,
5816 &data->lsp->ls_stateid);
5818 if (!nfs4_valid_open_stateid(state)) {
5819 data->rpc_status = -EBADF;
5820 task->tk_action = NULL;
5821 goto out_release_open_seqid;
5823 data->timestamp = jiffies;
5824 if (nfs4_setup_sequence(data->server,
5825 &data->arg.seq_args,
5829 out_release_open_seqid:
5830 nfs_release_seqid(data->arg.open_seqid);
5831 out_release_lock_seqid:
5832 nfs_release_seqid(data->arg.lock_seqid);
5834 nfs4_sequence_done(task, &data->res.seq_res);
5835 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
5838 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5840 struct nfs4_lockdata *data = calldata;
5841 struct nfs4_lock_state *lsp = data->lsp;
5843 dprintk("%s: begin!\n", __func__);
5845 if (!nfs4_sequence_done(task, &data->res.seq_res))
5848 data->rpc_status = task->tk_status;
5849 switch (task->tk_status) {
5851 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
5853 if (data->arg.new_lock) {
5854 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
5855 if (do_vfs_lock(lsp->ls_state->inode, &data->fl) < 0) {
5856 rpc_restart_call_prepare(task);
5860 if (data->arg.new_lock_owner != 0) {
5861 nfs_confirm_seqid(&lsp->ls_seqid, 0);
5862 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
5863 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
5864 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
5865 rpc_restart_call_prepare(task);
5867 case -NFS4ERR_BAD_STATEID:
5868 case -NFS4ERR_OLD_STATEID:
5869 case -NFS4ERR_STALE_STATEID:
5870 case -NFS4ERR_EXPIRED:
5871 if (data->arg.new_lock_owner != 0) {
5872 if (!nfs4_stateid_match(&data->arg.open_stateid,
5873 &lsp->ls_state->open_stateid))
5874 rpc_restart_call_prepare(task);
5875 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
5877 rpc_restart_call_prepare(task);
5879 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
5882 static void nfs4_lock_release(void *calldata)
5884 struct nfs4_lockdata *data = calldata;
5886 dprintk("%s: begin!\n", __func__);
5887 nfs_free_seqid(data->arg.open_seqid);
5888 if (data->cancelled != 0) {
5889 struct rpc_task *task;
5890 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5891 data->arg.lock_seqid);
5893 rpc_put_task_async(task);
5894 dprintk("%s: cancelling lock!\n", __func__);
5896 nfs_free_seqid(data->arg.lock_seqid);
5897 nfs4_put_lock_state(data->lsp);
5898 put_nfs_open_context(data->ctx);
5899 fput(data->fl.fl_file);
5901 dprintk("%s: done!\n", __func__);
5904 static const struct rpc_call_ops nfs4_lock_ops = {
5905 .rpc_call_prepare = nfs4_lock_prepare,
5906 .rpc_call_done = nfs4_lock_done,
5907 .rpc_release = nfs4_lock_release,
5910 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5913 case -NFS4ERR_ADMIN_REVOKED:
5914 case -NFS4ERR_BAD_STATEID:
5915 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
5916 if (new_lock_owner != 0 ||
5917 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
5918 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
5920 case -NFS4ERR_STALE_STATEID:
5921 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
5922 case -NFS4ERR_EXPIRED:
5923 nfs4_schedule_lease_recovery(server->nfs_client);
5927 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
5929 struct nfs4_lockdata *data;
5930 struct rpc_task *task;
5931 struct rpc_message msg = {
5932 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5933 .rpc_cred = state->owner->so_cred,
5935 struct rpc_task_setup task_setup_data = {
5936 .rpc_client = NFS_CLIENT(state->inode),
5937 .rpc_message = &msg,
5938 .callback_ops = &nfs4_lock_ops,
5939 .workqueue = nfsiod_workqueue,
5940 .flags = RPC_TASK_ASYNC,
5944 dprintk("%s: begin!\n", __func__);
5945 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
5946 fl->fl_u.nfs4_fl.owner,
5947 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
5951 data->arg.block = 1;
5952 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
5953 msg.rpc_argp = &data->arg;
5954 msg.rpc_resp = &data->res;
5955 task_setup_data.callback_data = data;
5956 if (recovery_type > NFS_LOCK_NEW) {
5957 if (recovery_type == NFS_LOCK_RECLAIM)
5958 data->arg.reclaim = NFS_LOCK_RECLAIM;
5959 nfs4_set_sequence_privileged(&data->arg.seq_args);
5961 data->arg.new_lock = 1;
5962 task = rpc_run_task(&task_setup_data);
5964 return PTR_ERR(task);
5965 ret = nfs4_wait_for_completion_rpc_task(task);
5967 ret = data->rpc_status;
5969 nfs4_handle_setlk_error(data->server, data->lsp,
5970 data->arg.new_lock_owner, ret);
5972 data->cancelled = 1;
5974 dprintk("%s: done, ret = %d!\n", __func__, ret);
5978 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
5980 struct nfs_server *server = NFS_SERVER(state->inode);
5981 struct nfs4_exception exception = {
5982 .inode = state->inode,
5987 /* Cache the lock if possible... */
5988 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5990 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
5991 trace_nfs4_lock_reclaim(request, state, F_SETLK, err);
5992 if (err != -NFS4ERR_DELAY)
5994 nfs4_handle_exception(server, err, &exception);
5995 } while (exception.retry);
5999 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6001 struct nfs_server *server = NFS_SERVER(state->inode);
6002 struct nfs4_exception exception = {
6003 .inode = state->inode,
6007 err = nfs4_set_lock_state(state, request);
6010 if (!recover_lost_locks) {
6011 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6015 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6017 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
6018 trace_nfs4_lock_expired(request, state, F_SETLK, err);
6022 case -NFS4ERR_GRACE:
6023 case -NFS4ERR_DELAY:
6024 nfs4_handle_exception(server, err, &exception);
6027 } while (exception.retry);
6032 #if defined(CONFIG_NFS_V4_1)
6034 * nfs41_check_expired_locks - possibly free a lock stateid
6036 * @state: NFSv4 state for an inode
6038 * Returns NFS_OK if recovery for this stateid is now finished.
6039 * Otherwise a negative NFS4ERR value is returned.
6041 static int nfs41_check_expired_locks(struct nfs4_state *state)
6043 int status, ret = -NFS4ERR_BAD_STATEID;
6044 struct nfs4_lock_state *lsp;
6045 struct nfs_server *server = NFS_SERVER(state->inode);
6047 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
6048 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
6049 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
6051 status = nfs41_test_stateid(server,
6054 trace_nfs4_test_lock_stateid(state, lsp, status);
6055 if (status != NFS_OK) {
6056 /* Free the stateid unless the server
6057 * informs us the stateid is unrecognized. */
6058 if (status != -NFS4ERR_BAD_STATEID)
6059 nfs41_free_stateid(server,
6062 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6071 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6073 int status = NFS_OK;
6075 if (test_bit(LK_STATE_IN_USE, &state->flags))
6076 status = nfs41_check_expired_locks(state);
6077 if (status != NFS_OK)
6078 status = nfs4_lock_expired(state, request);
6083 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6085 struct nfs_inode *nfsi = NFS_I(state->inode);
6086 unsigned char fl_flags = request->fl_flags;
6087 int status = -ENOLCK;
6089 if ((fl_flags & FL_POSIX) &&
6090 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6092 /* Is this a delegated open? */
6093 status = nfs4_set_lock_state(state, request);
6096 request->fl_flags |= FL_ACCESS;
6097 status = do_vfs_lock(state->inode, request);
6100 down_read(&nfsi->rwsem);
6101 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
6102 /* Yes: cache locks! */
6103 /* ...but avoid races with delegation recall... */
6104 request->fl_flags = fl_flags & ~FL_SLEEP;
6105 status = do_vfs_lock(state->inode, request);
6106 up_read(&nfsi->rwsem);
6109 up_read(&nfsi->rwsem);
6110 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
6112 request->fl_flags = fl_flags;
6116 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6118 struct nfs4_exception exception = {
6120 .inode = state->inode,
6125 err = _nfs4_proc_setlk(state, cmd, request);
6126 trace_nfs4_set_lock(request, state, cmd, err);
6127 if (err == -NFS4ERR_DENIED)
6129 err = nfs4_handle_exception(NFS_SERVER(state->inode),
6131 } while (exception.retry);
6136 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6138 struct nfs_open_context *ctx;
6139 struct nfs4_state *state;
6140 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6143 /* verify open state */
6144 ctx = nfs_file_open_context(filp);
6147 if (request->fl_start < 0 || request->fl_end < 0)
6150 if (IS_GETLK(cmd)) {
6152 return nfs4_proc_getlk(state, F_GETLK, request);
6156 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6159 if (request->fl_type == F_UNLCK) {
6161 return nfs4_proc_unlck(state, cmd, request);
6168 * Don't rely on the VFS having checked the file open mode,
6169 * since it won't do this for flock() locks.
6171 switch (request->fl_type) {
6173 if (!(filp->f_mode & FMODE_READ))
6177 if (!(filp->f_mode & FMODE_WRITE))
6182 status = nfs4_proc_setlk(state, cmd, request);
6183 if ((status != -EAGAIN) || IS_SETLK(cmd))
6185 timeout = nfs4_set_lock_task_retry(timeout);
6186 status = -ERESTARTSYS;
6189 } while(status < 0);
6193 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
6195 struct nfs_server *server = NFS_SERVER(state->inode);
6198 err = nfs4_set_lock_state(state, fl);
6201 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
6202 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
6205 struct nfs_release_lockowner_data {
6206 struct nfs4_lock_state *lsp;
6207 struct nfs_server *server;
6208 struct nfs_release_lockowner_args args;
6209 struct nfs_release_lockowner_res res;
6210 unsigned long timestamp;
6213 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6215 struct nfs_release_lockowner_data *data = calldata;
6216 struct nfs_server *server = data->server;
6217 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
6218 &data->args.seq_args, &data->res.seq_res, task);
6219 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6220 data->timestamp = jiffies;
6223 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6225 struct nfs_release_lockowner_data *data = calldata;
6226 struct nfs_server *server = data->server;
6228 nfs40_sequence_done(task, &data->res.seq_res);
6230 switch (task->tk_status) {
6232 renew_lease(server, data->timestamp);
6234 case -NFS4ERR_STALE_CLIENTID:
6235 case -NFS4ERR_EXPIRED:
6236 nfs4_schedule_lease_recovery(server->nfs_client);
6238 case -NFS4ERR_LEASE_MOVED:
6239 case -NFS4ERR_DELAY:
6240 if (nfs4_async_handle_error(task, server,
6241 NULL, NULL) == -EAGAIN)
6242 rpc_restart_call_prepare(task);
6246 static void nfs4_release_lockowner_release(void *calldata)
6248 struct nfs_release_lockowner_data *data = calldata;
6249 nfs4_free_lock_state(data->server, data->lsp);
6253 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
6254 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6255 .rpc_call_done = nfs4_release_lockowner_done,
6256 .rpc_release = nfs4_release_lockowner_release,
6260 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
6262 struct nfs_release_lockowner_data *data;
6263 struct rpc_message msg = {
6264 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6267 if (server->nfs_client->cl_mvops->minor_version != 0)
6270 data = kmalloc(sizeof(*data), GFP_NOFS);
6274 data->server = server;
6275 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6276 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6277 data->args.lock_owner.s_dev = server->s_dev;
6279 msg.rpc_argp = &data->args;
6280 msg.rpc_resp = &data->res;
6281 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
6282 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
6285 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6287 static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
6288 const void *buf, size_t buflen,
6289 int flags, int type)
6291 if (strcmp(key, "") != 0)
6294 return nfs4_proc_set_acl(d_inode(dentry), buf, buflen);
6297 static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
6298 void *buf, size_t buflen, int type)
6300 if (strcmp(key, "") != 0)
6303 return nfs4_proc_get_acl(d_inode(dentry), buf, buflen);
6306 static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
6307 size_t list_len, const char *name,
6308 size_t name_len, int type)
6310 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
6312 if (!nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry))))
6315 if (list && len <= list_len)
6316 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
6320 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
6321 static inline int nfs4_server_supports_labels(struct nfs_server *server)
6323 return server->caps & NFS_CAP_SECURITY_LABEL;
6326 static int nfs4_xattr_set_nfs4_label(struct dentry *dentry, const char *key,
6327 const void *buf, size_t buflen,
6328 int flags, int type)
6330 if (security_ismaclabel(key))
6331 return nfs4_set_security_label(dentry, buf, buflen);
6336 static int nfs4_xattr_get_nfs4_label(struct dentry *dentry, const char *key,
6337 void *buf, size_t buflen, int type)
6339 if (security_ismaclabel(key))
6340 return nfs4_get_security_label(d_inode(dentry), buf, buflen);
6344 static size_t nfs4_xattr_list_nfs4_label(struct dentry *dentry, char *list,
6345 size_t list_len, const char *name,
6346 size_t name_len, int type)
6350 if (nfs_server_capable(d_inode(dentry), NFS_CAP_SECURITY_LABEL)) {
6351 len = security_inode_listsecurity(d_inode(dentry), NULL, 0);
6352 if (list && len <= list_len)
6353 security_inode_listsecurity(d_inode(dentry), list, len);
6358 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6359 .prefix = XATTR_SECURITY_PREFIX,
6360 .list = nfs4_xattr_list_nfs4_label,
6361 .get = nfs4_xattr_get_nfs4_label,
6362 .set = nfs4_xattr_set_nfs4_label,
6368 * nfs_fhget will use either the mounted_on_fileid or the fileid
6370 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6372 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6373 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6374 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
6375 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
6378 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
6379 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
6380 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6384 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6385 const struct qstr *name,
6386 struct nfs4_fs_locations *fs_locations,
6389 struct nfs_server *server = NFS_SERVER(dir);
6391 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6393 struct nfs4_fs_locations_arg args = {
6394 .dir_fh = NFS_FH(dir),
6399 struct nfs4_fs_locations_res res = {
6400 .fs_locations = fs_locations,
6402 struct rpc_message msg = {
6403 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6409 dprintk("%s: start\n", __func__);
6411 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6412 * is not supported */
6413 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6414 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6416 bitmask[0] |= FATTR4_WORD0_FILEID;
6418 nfs_fattr_init(&fs_locations->fattr);
6419 fs_locations->server = server;
6420 fs_locations->nlocations = 0;
6421 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
6422 dprintk("%s: returned status = %d\n", __func__, status);
6426 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6427 const struct qstr *name,
6428 struct nfs4_fs_locations *fs_locations,
6431 struct nfs4_exception exception = { };
6434 err = _nfs4_proc_fs_locations(client, dir, name,
6435 fs_locations, page);
6436 trace_nfs4_get_fs_locations(dir, name, err);
6437 err = nfs4_handle_exception(NFS_SERVER(dir), err,
6439 } while (exception.retry);
6444 * This operation also signals the server that this client is
6445 * performing migration recovery. The server can stop returning
6446 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6447 * appended to this compound to identify the client ID which is
6448 * performing recovery.
6450 static int _nfs40_proc_get_locations(struct inode *inode,
6451 struct nfs4_fs_locations *locations,
6452 struct page *page, struct rpc_cred *cred)
6454 struct nfs_server *server = NFS_SERVER(inode);
6455 struct rpc_clnt *clnt = server->client;
6457 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6459 struct nfs4_fs_locations_arg args = {
6460 .clientid = server->nfs_client->cl_clientid,
6461 .fh = NFS_FH(inode),
6464 .migration = 1, /* skip LOOKUP */
6465 .renew = 1, /* append RENEW */
6467 struct nfs4_fs_locations_res res = {
6468 .fs_locations = locations,
6472 struct rpc_message msg = {
6473 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6478 unsigned long now = jiffies;
6481 nfs_fattr_init(&locations->fattr);
6482 locations->server = server;
6483 locations->nlocations = 0;
6485 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6486 nfs4_set_sequence_privileged(&args.seq_args);
6487 status = nfs4_call_sync_sequence(clnt, server, &msg,
6488 &args.seq_args, &res.seq_res);
6492 renew_lease(server, now);
6496 #ifdef CONFIG_NFS_V4_1
6499 * This operation also signals the server that this client is
6500 * performing migration recovery. The server can stop asserting
6501 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6502 * performing this operation is identified in the SEQUENCE
6503 * operation in this compound.
6505 * When the client supports GETATTR(fs_locations_info), it can
6506 * be plumbed in here.
6508 static int _nfs41_proc_get_locations(struct inode *inode,
6509 struct nfs4_fs_locations *locations,
6510 struct page *page, struct rpc_cred *cred)
6512 struct nfs_server *server = NFS_SERVER(inode);
6513 struct rpc_clnt *clnt = server->client;
6515 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6517 struct nfs4_fs_locations_arg args = {
6518 .fh = NFS_FH(inode),
6521 .migration = 1, /* skip LOOKUP */
6523 struct nfs4_fs_locations_res res = {
6524 .fs_locations = locations,
6527 struct rpc_message msg = {
6528 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6535 nfs_fattr_init(&locations->fattr);
6536 locations->server = server;
6537 locations->nlocations = 0;
6539 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6540 nfs4_set_sequence_privileged(&args.seq_args);
6541 status = nfs4_call_sync_sequence(clnt, server, &msg,
6542 &args.seq_args, &res.seq_res);
6543 if (status == NFS4_OK &&
6544 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6545 status = -NFS4ERR_LEASE_MOVED;
6549 #endif /* CONFIG_NFS_V4_1 */
6552 * nfs4_proc_get_locations - discover locations for a migrated FSID
6553 * @inode: inode on FSID that is migrating
6554 * @locations: result of query
6556 * @cred: credential to use for this operation
6558 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6559 * operation failed, or a negative errno if a local error occurred.
6561 * On success, "locations" is filled in, but if the server has
6562 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6565 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6566 * from this client that require migration recovery.
6568 int nfs4_proc_get_locations(struct inode *inode,
6569 struct nfs4_fs_locations *locations,
6570 struct page *page, struct rpc_cred *cred)
6572 struct nfs_server *server = NFS_SERVER(inode);
6573 struct nfs_client *clp = server->nfs_client;
6574 const struct nfs4_mig_recovery_ops *ops =
6575 clp->cl_mvops->mig_recovery_ops;
6576 struct nfs4_exception exception = { };
6579 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6580 (unsigned long long)server->fsid.major,
6581 (unsigned long long)server->fsid.minor,
6583 nfs_display_fhandle(NFS_FH(inode), __func__);
6586 status = ops->get_locations(inode, locations, page, cred);
6587 if (status != -NFS4ERR_DELAY)
6589 nfs4_handle_exception(server, status, &exception);
6590 } while (exception.retry);
6595 * This operation also signals the server that this client is
6596 * performing "lease moved" recovery. The server can stop
6597 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6598 * is appended to this compound to identify the client ID which is
6599 * performing recovery.
6601 static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6603 struct nfs_server *server = NFS_SERVER(inode);
6604 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6605 struct rpc_clnt *clnt = server->client;
6606 struct nfs4_fsid_present_arg args = {
6607 .fh = NFS_FH(inode),
6608 .clientid = clp->cl_clientid,
6609 .renew = 1, /* append RENEW */
6611 struct nfs4_fsid_present_res res = {
6614 struct rpc_message msg = {
6615 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6620 unsigned long now = jiffies;
6623 res.fh = nfs_alloc_fhandle();
6627 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6628 nfs4_set_sequence_privileged(&args.seq_args);
6629 status = nfs4_call_sync_sequence(clnt, server, &msg,
6630 &args.seq_args, &res.seq_res);
6631 nfs_free_fhandle(res.fh);
6635 do_renew_lease(clp, now);
6639 #ifdef CONFIG_NFS_V4_1
6642 * This operation also signals the server that this client is
6643 * performing "lease moved" recovery. The server can stop asserting
6644 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6645 * this operation is identified in the SEQUENCE operation in this
6648 static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6650 struct nfs_server *server = NFS_SERVER(inode);
6651 struct rpc_clnt *clnt = server->client;
6652 struct nfs4_fsid_present_arg args = {
6653 .fh = NFS_FH(inode),
6655 struct nfs4_fsid_present_res res = {
6657 struct rpc_message msg = {
6658 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6665 res.fh = nfs_alloc_fhandle();
6669 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6670 nfs4_set_sequence_privileged(&args.seq_args);
6671 status = nfs4_call_sync_sequence(clnt, server, &msg,
6672 &args.seq_args, &res.seq_res);
6673 nfs_free_fhandle(res.fh);
6674 if (status == NFS4_OK &&
6675 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6676 status = -NFS4ERR_LEASE_MOVED;
6680 #endif /* CONFIG_NFS_V4_1 */
6683 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6684 * @inode: inode on FSID to check
6685 * @cred: credential to use for this operation
6687 * Server indicates whether the FSID is present, moved, or not
6688 * recognized. This operation is necessary to clear a LEASE_MOVED
6689 * condition for this client ID.
6691 * Returns NFS4_OK if the FSID is present on this server,
6692 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6693 * NFS4ERR code if some error occurred on the server, or a
6694 * negative errno if a local failure occurred.
6696 int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6698 struct nfs_server *server = NFS_SERVER(inode);
6699 struct nfs_client *clp = server->nfs_client;
6700 const struct nfs4_mig_recovery_ops *ops =
6701 clp->cl_mvops->mig_recovery_ops;
6702 struct nfs4_exception exception = { };
6705 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6706 (unsigned long long)server->fsid.major,
6707 (unsigned long long)server->fsid.minor,
6709 nfs_display_fhandle(NFS_FH(inode), __func__);
6712 status = ops->fsid_present(inode, cred);
6713 if (status != -NFS4ERR_DELAY)
6715 nfs4_handle_exception(server, status, &exception);
6716 } while (exception.retry);
6721 * If 'use_integrity' is true and the state managment nfs_client
6722 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
6723 * and the machine credential as per RFC3530bis and RFC5661 Security
6724 * Considerations sections. Otherwise, just use the user cred with the
6725 * filesystem's rpc_client.
6727 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
6730 struct nfs4_secinfo_arg args = {
6731 .dir_fh = NFS_FH(dir),
6734 struct nfs4_secinfo_res res = {
6737 struct rpc_message msg = {
6738 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
6742 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
6743 struct rpc_cred *cred = NULL;
6745 if (use_integrity) {
6746 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
6747 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
6748 msg.rpc_cred = cred;
6751 dprintk("NFS call secinfo %s\n", name->name);
6753 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
6754 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
6756 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
6758 dprintk("NFS reply secinfo: %d\n", status);
6766 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
6767 struct nfs4_secinfo_flavors *flavors)
6769 struct nfs4_exception exception = { };
6772 err = -NFS4ERR_WRONGSEC;
6774 /* try to use integrity protection with machine cred */
6775 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
6776 err = _nfs4_proc_secinfo(dir, name, flavors, true);
6779 * if unable to use integrity protection, or SECINFO with
6780 * integrity protection returns NFS4ERR_WRONGSEC (which is
6781 * disallowed by spec, but exists in deployed servers) use
6782 * the current filesystem's rpc_client and the user cred.
6784 if (err == -NFS4ERR_WRONGSEC)
6785 err = _nfs4_proc_secinfo(dir, name, flavors, false);
6787 trace_nfs4_secinfo(dir, name, err);
6788 err = nfs4_handle_exception(NFS_SERVER(dir), err,
6790 } while (exception.retry);
6794 #ifdef CONFIG_NFS_V4_1
6796 * Check the exchange flags returned by the server for invalid flags, having
6797 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
6800 static int nfs4_check_cl_exchange_flags(u32 flags)
6802 if (flags & ~EXCHGID4_FLAG_MASK_R)
6804 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
6805 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
6807 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
6811 return -NFS4ERR_INVAL;
6815 nfs41_same_server_scope(struct nfs41_server_scope *a,
6816 struct nfs41_server_scope *b)
6818 if (a->server_scope_sz == b->server_scope_sz &&
6819 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
6826 * nfs4_proc_bind_conn_to_session()
6828 * The 4.1 client currently uses the same TCP connection for the
6829 * fore and backchannel.
6831 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
6834 struct nfs41_bind_conn_to_session_args args = {
6836 .dir = NFS4_CDFC4_FORE_OR_BOTH,
6838 struct nfs41_bind_conn_to_session_res res;
6839 struct rpc_message msg = {
6841 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
6847 dprintk("--> %s\n", __func__);
6849 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
6850 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
6851 args.dir = NFS4_CDFC4_FORE;
6853 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
6854 trace_nfs4_bind_conn_to_session(clp, status);
6856 if (memcmp(res.sessionid.data,
6857 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
6858 dprintk("NFS: %s: Session ID mismatch\n", __func__);
6862 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
6863 dprintk("NFS: %s: Unexpected direction from server\n",
6868 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
6869 dprintk("NFS: %s: Server returned RDMA mode = true\n",
6876 dprintk("<-- %s status= %d\n", __func__, status);
6881 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
6882 * and operations we'd like to see to enable certain features in the allow map
6884 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
6885 .how = SP4_MACH_CRED,
6886 .enforce.u.words = {
6887 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6888 1 << (OP_EXCHANGE_ID - 32) |
6889 1 << (OP_CREATE_SESSION - 32) |
6890 1 << (OP_DESTROY_SESSION - 32) |
6891 1 << (OP_DESTROY_CLIENTID - 32)
6894 [0] = 1 << (OP_CLOSE) |
6897 [1] = 1 << (OP_SECINFO - 32) |
6898 1 << (OP_SECINFO_NO_NAME - 32) |
6899 1 << (OP_TEST_STATEID - 32) |
6900 1 << (OP_FREE_STATEID - 32) |
6901 1 << (OP_WRITE - 32)
6906 * Select the state protection mode for client `clp' given the server results
6907 * from exchange_id in `sp'.
6909 * Returns 0 on success, negative errno otherwise.
6911 static int nfs4_sp4_select_mode(struct nfs_client *clp,
6912 struct nfs41_state_protection *sp)
6914 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
6915 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6916 1 << (OP_EXCHANGE_ID - 32) |
6917 1 << (OP_CREATE_SESSION - 32) |
6918 1 << (OP_DESTROY_SESSION - 32) |
6919 1 << (OP_DESTROY_CLIENTID - 32)
6923 if (sp->how == SP4_MACH_CRED) {
6924 /* Print state protect result */
6925 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
6926 for (i = 0; i <= LAST_NFS4_OP; i++) {
6927 if (test_bit(i, sp->enforce.u.longs))
6928 dfprintk(MOUNT, " enforce op %d\n", i);
6929 if (test_bit(i, sp->allow.u.longs))
6930 dfprintk(MOUNT, " allow op %d\n", i);
6933 /* make sure nothing is on enforce list that isn't supported */
6934 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
6935 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
6936 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6942 * Minimal mode - state operations are allowed to use machine
6943 * credential. Note this already happens by default, so the
6944 * client doesn't have to do anything more than the negotiation.
6946 * NOTE: we don't care if EXCHANGE_ID is in the list -
6947 * we're already using the machine cred for exchange_id
6948 * and will never use a different cred.
6950 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
6951 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
6952 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
6953 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
6954 dfprintk(MOUNT, "sp4_mach_cred:\n");
6955 dfprintk(MOUNT, " minimal mode enabled\n");
6956 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
6958 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6962 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
6963 test_bit(OP_LOCKU, sp->allow.u.longs)) {
6964 dfprintk(MOUNT, " cleanup mode enabled\n");
6965 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
6968 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
6969 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
6970 dfprintk(MOUNT, " secinfo mode enabled\n");
6971 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
6974 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
6975 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
6976 dfprintk(MOUNT, " stateid mode enabled\n");
6977 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
6980 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
6981 dfprintk(MOUNT, " write mode enabled\n");
6982 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
6985 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
6986 dfprintk(MOUNT, " commit mode enabled\n");
6987 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
6995 * _nfs4_proc_exchange_id()
6997 * Wrapper for EXCHANGE_ID operation.
6999 static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
7002 nfs4_verifier verifier;
7003 struct nfs41_exchange_id_args args = {
7004 .verifier = &verifier,
7006 #ifdef CONFIG_NFS_V4_1_MIGRATION
7007 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7008 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7009 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7011 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7012 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7015 struct nfs41_exchange_id_res res = {
7019 struct rpc_message msg = {
7020 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
7026 nfs4_init_boot_verifier(clp, &verifier);
7028 status = nfs4_init_uniform_client_string(clp);
7032 dprintk("NFS call exchange_id auth=%s, '%s'\n",
7033 clp->cl_rpcclient->cl_auth->au_ops->au_name,
7036 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7038 if (unlikely(res.server_owner == NULL)) {
7043 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
7045 if (unlikely(res.server_scope == NULL)) {
7047 goto out_server_owner;
7050 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7051 if (unlikely(res.impl_id == NULL)) {
7053 goto out_server_scope;
7058 args.state_protect.how = SP4_NONE;
7062 args.state_protect = nfs4_sp4_mach_cred_request;
7072 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7073 trace_nfs4_exchange_id(clp, status);
7075 status = nfs4_check_cl_exchange_flags(res.flags);
7078 status = nfs4_sp4_select_mode(clp, &res.state_protect);
7081 clp->cl_clientid = res.clientid;
7082 clp->cl_exchange_flags = res.flags;
7083 /* Client ID is not confirmed */
7084 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
7085 clear_bit(NFS4_SESSION_ESTABLISHED,
7086 &clp->cl_session->session_state);
7087 clp->cl_seqid = res.seqid;
7090 kfree(clp->cl_serverowner);
7091 clp->cl_serverowner = res.server_owner;
7092 res.server_owner = NULL;
7094 /* use the most recent implementation id */
7095 kfree(clp->cl_implid);
7096 clp->cl_implid = res.impl_id;
7099 if (clp->cl_serverscope != NULL &&
7100 !nfs41_same_server_scope(clp->cl_serverscope,
7101 res.server_scope)) {
7102 dprintk("%s: server_scope mismatch detected\n",
7104 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
7105 kfree(clp->cl_serverscope);
7106 clp->cl_serverscope = NULL;
7109 if (clp->cl_serverscope == NULL) {
7110 clp->cl_serverscope = res.server_scope;
7111 res.server_scope = NULL;
7118 kfree(res.server_scope);
7120 kfree(res.server_owner);
7122 if (clp->cl_implid != NULL)
7123 dprintk("NFS reply exchange_id: Server Implementation ID: "
7124 "domain: %s, name: %s, date: %llu,%u\n",
7125 clp->cl_implid->domain, clp->cl_implid->name,
7126 clp->cl_implid->date.seconds,
7127 clp->cl_implid->date.nseconds);
7128 dprintk("NFS reply exchange_id: %d\n", status);
7133 * nfs4_proc_exchange_id()
7135 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7137 * Since the clientid has expired, all compounds using sessions
7138 * associated with the stale clientid will be returning
7139 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7140 * be in some phase of session reset.
7142 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7144 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7146 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7149 /* try SP4_MACH_CRED if krb5i/p */
7150 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7151 authflavor == RPC_AUTH_GSS_KRB5P) {
7152 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
7158 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
7161 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7162 struct rpc_cred *cred)
7164 struct rpc_message msg = {
7165 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7171 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7172 trace_nfs4_destroy_clientid(clp, status);
7174 dprintk("NFS: Got error %d from the server %s on "
7175 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7179 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7180 struct rpc_cred *cred)
7185 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7186 ret = _nfs4_proc_destroy_clientid(clp, cred);
7188 case -NFS4ERR_DELAY:
7189 case -NFS4ERR_CLIENTID_BUSY:
7199 int nfs4_destroy_clientid(struct nfs_client *clp)
7201 struct rpc_cred *cred;
7204 if (clp->cl_mvops->minor_version < 1)
7206 if (clp->cl_exchange_flags == 0)
7208 if (clp->cl_preserve_clid)
7210 cred = nfs4_get_clid_cred(clp);
7211 ret = nfs4_proc_destroy_clientid(clp, cred);
7216 case -NFS4ERR_STALE_CLIENTID:
7217 clp->cl_exchange_flags = 0;
7223 struct nfs4_get_lease_time_data {
7224 struct nfs4_get_lease_time_args *args;
7225 struct nfs4_get_lease_time_res *res;
7226 struct nfs_client *clp;
7229 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7232 struct nfs4_get_lease_time_data *data =
7233 (struct nfs4_get_lease_time_data *)calldata;
7235 dprintk("--> %s\n", __func__);
7236 /* just setup sequence, do not trigger session recovery
7237 since we're invoked within one */
7238 nfs41_setup_sequence(data->clp->cl_session,
7239 &data->args->la_seq_args,
7240 &data->res->lr_seq_res,
7242 dprintk("<-- %s\n", __func__);
7246 * Called from nfs4_state_manager thread for session setup, so don't recover
7247 * from sequence operation or clientid errors.
7249 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7251 struct nfs4_get_lease_time_data *data =
7252 (struct nfs4_get_lease_time_data *)calldata;
7254 dprintk("--> %s\n", __func__);
7255 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7257 switch (task->tk_status) {
7258 case -NFS4ERR_DELAY:
7259 case -NFS4ERR_GRACE:
7260 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7261 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7262 task->tk_status = 0;
7264 case -NFS4ERR_RETRY_UNCACHED_REP:
7265 rpc_restart_call_prepare(task);
7268 dprintk("<-- %s\n", __func__);
7271 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
7272 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7273 .rpc_call_done = nfs4_get_lease_time_done,
7276 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7278 struct rpc_task *task;
7279 struct nfs4_get_lease_time_args args;
7280 struct nfs4_get_lease_time_res res = {
7281 .lr_fsinfo = fsinfo,
7283 struct nfs4_get_lease_time_data data = {
7288 struct rpc_message msg = {
7289 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7293 struct rpc_task_setup task_setup = {
7294 .rpc_client = clp->cl_rpcclient,
7295 .rpc_message = &msg,
7296 .callback_ops = &nfs4_get_lease_time_ops,
7297 .callback_data = &data,
7298 .flags = RPC_TASK_TIMEOUT,
7302 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
7303 nfs4_set_sequence_privileged(&args.la_seq_args);
7304 dprintk("--> %s\n", __func__);
7305 task = rpc_run_task(&task_setup);
7308 status = PTR_ERR(task);
7310 status = task->tk_status;
7313 dprintk("<-- %s return %d\n", __func__, status);
7319 * Initialize the values to be used by the client in CREATE_SESSION
7320 * If nfs4_init_session set the fore channel request and response sizes,
7323 * Set the back channel max_resp_sz_cached to zero to force the client to
7324 * always set csa_cachethis to FALSE because the current implementation
7325 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7327 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
7329 unsigned int max_rqst_sz, max_resp_sz;
7331 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7332 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7334 /* Fore channel attributes */
7335 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7336 args->fc_attrs.max_resp_sz = max_resp_sz;
7337 args->fc_attrs.max_ops = NFS4_MAX_OPS;
7338 args->fc_attrs.max_reqs = max_session_slots;
7340 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
7341 "max_ops=%u max_reqs=%u\n",
7343 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
7344 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
7346 /* Back channel attributes */
7347 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
7348 args->bc_attrs.max_resp_sz = PAGE_SIZE;
7349 args->bc_attrs.max_resp_sz_cached = 0;
7350 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
7351 args->bc_attrs.max_reqs = 1;
7353 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7354 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7356 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7357 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7358 args->bc_attrs.max_reqs);
7361 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7362 struct nfs41_create_session_res *res)
7364 struct nfs4_channel_attrs *sent = &args->fc_attrs;
7365 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
7367 if (rcvd->max_resp_sz > sent->max_resp_sz)
7370 * Our requested max_ops is the minimum we need; we're not
7371 * prepared to break up compounds into smaller pieces than that.
7372 * So, no point even trying to continue if the server won't
7375 if (rcvd->max_ops < sent->max_ops)
7377 if (rcvd->max_reqs == 0)
7379 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7380 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
7384 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7385 struct nfs41_create_session_res *res)
7387 struct nfs4_channel_attrs *sent = &args->bc_attrs;
7388 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
7390 if (!(res->flags & SESSION4_BACK_CHAN))
7392 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7394 if (rcvd->max_resp_sz < sent->max_resp_sz)
7396 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7398 /* These would render the backchannel useless: */
7399 if (rcvd->max_ops != sent->max_ops)
7401 if (rcvd->max_reqs != sent->max_reqs)
7407 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
7408 struct nfs41_create_session_res *res)
7412 ret = nfs4_verify_fore_channel_attrs(args, res);
7415 return nfs4_verify_back_channel_attrs(args, res);
7418 static void nfs4_update_session(struct nfs4_session *session,
7419 struct nfs41_create_session_res *res)
7421 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
7422 /* Mark client id and session as being confirmed */
7423 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7424 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
7425 session->flags = res->flags;
7426 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
7427 if (res->flags & SESSION4_BACK_CHAN)
7428 memcpy(&session->bc_attrs, &res->bc_attrs,
7429 sizeof(session->bc_attrs));
7432 static int _nfs4_proc_create_session(struct nfs_client *clp,
7433 struct rpc_cred *cred)
7435 struct nfs4_session *session = clp->cl_session;
7436 struct nfs41_create_session_args args = {
7438 .clientid = clp->cl_clientid,
7439 .seqid = clp->cl_seqid,
7440 .cb_program = NFS4_CALLBACK,
7442 struct nfs41_create_session_res res;
7444 struct rpc_message msg = {
7445 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7452 nfs4_init_channel_attrs(&args);
7453 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
7455 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7456 trace_nfs4_create_session(clp, status);
7459 /* Verify the session's negotiated channel_attrs values */
7460 status = nfs4_verify_channel_attrs(&args, &res);
7461 /* Increment the clientid slot sequence id */
7462 if (clp->cl_seqid == res.seqid)
7466 nfs4_update_session(session, &res);
7473 * Issues a CREATE_SESSION operation to the server.
7474 * It is the responsibility of the caller to verify the session is
7475 * expired before calling this routine.
7477 int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
7481 struct nfs4_session *session = clp->cl_session;
7483 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7485 status = _nfs4_proc_create_session(clp, cred);
7489 /* Init or reset the session slot tables */
7490 status = nfs4_setup_session_slot_tables(session);
7491 dprintk("slot table setup returned %d\n", status);
7495 ptr = (unsigned *)&session->sess_id.data[0];
7496 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7497 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
7499 dprintk("<-- %s\n", __func__);
7504 * Issue the over-the-wire RPC DESTROY_SESSION.
7505 * The caller must serialize access to this routine.
7507 int nfs4_proc_destroy_session(struct nfs4_session *session,
7508 struct rpc_cred *cred)
7510 struct rpc_message msg = {
7511 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
7512 .rpc_argp = session,
7517 dprintk("--> nfs4_proc_destroy_session\n");
7519 /* session is still being setup */
7520 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
7523 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7524 trace_nfs4_destroy_session(session->clp, status);
7527 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
7528 "Session has been destroyed regardless...\n", status);
7530 dprintk("<-- nfs4_proc_destroy_session\n");
7535 * Renew the cl_session lease.
7537 struct nfs4_sequence_data {
7538 struct nfs_client *clp;
7539 struct nfs4_sequence_args args;
7540 struct nfs4_sequence_res res;
7543 static void nfs41_sequence_release(void *data)
7545 struct nfs4_sequence_data *calldata = data;
7546 struct nfs_client *clp = calldata->clp;
7548 if (atomic_read(&clp->cl_count) > 1)
7549 nfs4_schedule_state_renewal(clp);
7550 nfs_put_client(clp);
7554 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7556 switch(task->tk_status) {
7557 case -NFS4ERR_DELAY:
7558 rpc_delay(task, NFS4_POLL_RETRY_MAX);
7561 nfs4_schedule_lease_recovery(clp);
7566 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
7568 struct nfs4_sequence_data *calldata = data;
7569 struct nfs_client *clp = calldata->clp;
7571 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
7574 trace_nfs4_sequence(clp, task->tk_status);
7575 if (task->tk_status < 0) {
7576 dprintk("%s ERROR %d\n", __func__, task->tk_status);
7577 if (atomic_read(&clp->cl_count) == 1)
7580 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
7581 rpc_restart_call_prepare(task);
7585 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
7587 dprintk("<-- %s\n", __func__);
7590 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
7592 struct nfs4_sequence_data *calldata = data;
7593 struct nfs_client *clp = calldata->clp;
7594 struct nfs4_sequence_args *args;
7595 struct nfs4_sequence_res *res;
7597 args = task->tk_msg.rpc_argp;
7598 res = task->tk_msg.rpc_resp;
7600 nfs41_setup_sequence(clp->cl_session, args, res, task);
7603 static const struct rpc_call_ops nfs41_sequence_ops = {
7604 .rpc_call_done = nfs41_sequence_call_done,
7605 .rpc_call_prepare = nfs41_sequence_prepare,
7606 .rpc_release = nfs41_sequence_release,
7609 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
7610 struct rpc_cred *cred,
7613 struct nfs4_sequence_data *calldata;
7614 struct rpc_message msg = {
7615 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
7618 struct rpc_task_setup task_setup_data = {
7619 .rpc_client = clp->cl_rpcclient,
7620 .rpc_message = &msg,
7621 .callback_ops = &nfs41_sequence_ops,
7622 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7625 if (!atomic_inc_not_zero(&clp->cl_count))
7626 return ERR_PTR(-EIO);
7627 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7628 if (calldata == NULL) {
7629 nfs_put_client(clp);
7630 return ERR_PTR(-ENOMEM);
7632 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
7634 nfs4_set_sequence_privileged(&calldata->args);
7635 msg.rpc_argp = &calldata->args;
7636 msg.rpc_resp = &calldata->res;
7637 calldata->clp = clp;
7638 task_setup_data.callback_data = calldata;
7640 return rpc_run_task(&task_setup_data);
7643 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
7645 struct rpc_task *task;
7648 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
7650 task = _nfs41_proc_sequence(clp, cred, false);
7652 ret = PTR_ERR(task);
7654 rpc_put_task_async(task);
7655 dprintk("<-- %s status=%d\n", __func__, ret);
7659 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
7661 struct rpc_task *task;
7664 task = _nfs41_proc_sequence(clp, cred, true);
7666 ret = PTR_ERR(task);
7669 ret = rpc_wait_for_completion_task(task);
7671 ret = task->tk_status;
7674 dprintk("<-- %s status=%d\n", __func__, ret);
7678 struct nfs4_reclaim_complete_data {
7679 struct nfs_client *clp;
7680 struct nfs41_reclaim_complete_args arg;
7681 struct nfs41_reclaim_complete_res res;
7684 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
7686 struct nfs4_reclaim_complete_data *calldata = data;
7688 nfs41_setup_sequence(calldata->clp->cl_session,
7689 &calldata->arg.seq_args,
7690 &calldata->res.seq_res,
7694 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7696 switch(task->tk_status) {
7698 case -NFS4ERR_COMPLETE_ALREADY:
7699 case -NFS4ERR_WRONG_CRED: /* What to do here? */
7701 case -NFS4ERR_DELAY:
7702 rpc_delay(task, NFS4_POLL_RETRY_MAX);
7704 case -NFS4ERR_RETRY_UNCACHED_REP:
7707 nfs4_schedule_lease_recovery(clp);
7712 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
7714 struct nfs4_reclaim_complete_data *calldata = data;
7715 struct nfs_client *clp = calldata->clp;
7716 struct nfs4_sequence_res *res = &calldata->res.seq_res;
7718 dprintk("--> %s\n", __func__);
7719 if (!nfs41_sequence_done(task, res))
7722 trace_nfs4_reclaim_complete(clp, task->tk_status);
7723 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
7724 rpc_restart_call_prepare(task);
7727 dprintk("<-- %s\n", __func__);
7730 static void nfs4_free_reclaim_complete_data(void *data)
7732 struct nfs4_reclaim_complete_data *calldata = data;
7737 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
7738 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
7739 .rpc_call_done = nfs4_reclaim_complete_done,
7740 .rpc_release = nfs4_free_reclaim_complete_data,
7744 * Issue a global reclaim complete.
7746 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
7747 struct rpc_cred *cred)
7749 struct nfs4_reclaim_complete_data *calldata;
7750 struct rpc_task *task;
7751 struct rpc_message msg = {
7752 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
7755 struct rpc_task_setup task_setup_data = {
7756 .rpc_client = clp->cl_rpcclient,
7757 .rpc_message = &msg,
7758 .callback_ops = &nfs4_reclaim_complete_call_ops,
7759 .flags = RPC_TASK_ASYNC,
7761 int status = -ENOMEM;
7763 dprintk("--> %s\n", __func__);
7764 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7765 if (calldata == NULL)
7767 calldata->clp = clp;
7768 calldata->arg.one_fs = 0;
7770 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
7771 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
7772 msg.rpc_argp = &calldata->arg;
7773 msg.rpc_resp = &calldata->res;
7774 task_setup_data.callback_data = calldata;
7775 task = rpc_run_task(&task_setup_data);
7777 status = PTR_ERR(task);
7780 status = nfs4_wait_for_completion_rpc_task(task);
7782 status = task->tk_status;
7786 dprintk("<-- %s status=%d\n", __func__, status);
7791 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
7793 struct nfs4_layoutget *lgp = calldata;
7794 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
7795 struct nfs4_session *session = nfs4_get_session(server);
7797 dprintk("--> %s\n", __func__);
7798 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
7799 * right now covering the LAYOUTGET we are about to send.
7800 * However, that is not so catastrophic, and there seems
7801 * to be no way to prevent it completely.
7803 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
7804 &lgp->res.seq_res, task))
7806 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
7807 NFS_I(lgp->args.inode)->layout,
7809 lgp->args.ctx->state)) {
7810 rpc_exit(task, NFS4_OK);
7814 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
7816 struct nfs4_layoutget *lgp = calldata;
7817 struct inode *inode = lgp->args.inode;
7818 struct nfs_server *server = NFS_SERVER(inode);
7819 struct pnfs_layout_hdr *lo;
7820 struct nfs4_state *state = NULL;
7821 unsigned long timeo, now, giveup;
7823 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
7825 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
7828 switch (task->tk_status) {
7832 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
7833 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
7835 case -NFS4ERR_BADLAYOUT:
7838 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
7839 * (or clients) writing to the same RAID stripe except when
7840 * the minlength argument is 0 (see RFC5661 section 18.43.3).
7842 case -NFS4ERR_LAYOUTTRYLATER:
7843 if (lgp->args.minlength == 0)
7846 * NFS4ERR_RECALLCONFLICT is when conflict with self (must recall
7847 * existing layout before getting a new one).
7849 case -NFS4ERR_RECALLCONFLICT:
7850 timeo = rpc_get_timeout(task->tk_client);
7851 giveup = lgp->args.timestamp + timeo;
7853 if (time_after(giveup, now)) {
7854 unsigned long delay;
7857 * - Not less then NFS4_POLL_RETRY_MIN.
7858 * - One last time a jiffie before we give up
7859 * - exponential backoff (time_now minus start_attempt)
7861 delay = max_t(unsigned long, NFS4_POLL_RETRY_MIN,
7862 min((giveup - now - 1),
7863 now - lgp->args.timestamp));
7865 dprintk("%s: NFS4ERR_RECALLCONFLICT waiting %lu\n",
7867 rpc_delay(task, delay);
7868 /* Do not call nfs4_async_handle_error() */
7872 case -NFS4ERR_EXPIRED:
7873 case -NFS4ERR_BAD_STATEID:
7874 spin_lock(&inode->i_lock);
7875 if (nfs4_stateid_match(&lgp->args.stateid,
7876 &lgp->args.ctx->state->stateid)) {
7877 spin_unlock(&inode->i_lock);
7878 /* If the open stateid was bad, then recover it. */
7879 state = lgp->args.ctx->state;
7882 lo = NFS_I(inode)->layout;
7883 if (lo && nfs4_stateid_match(&lgp->args.stateid,
7884 &lo->plh_stateid)) {
7888 * Mark the bad layout state as invalid, then retry
7889 * with the current stateid.
7891 set_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags);
7892 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
7893 spin_unlock(&inode->i_lock);
7894 pnfs_free_lseg_list(&head);
7896 spin_unlock(&inode->i_lock);
7899 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN)
7902 dprintk("<-- %s\n", __func__);
7905 task->tk_status = 0;
7906 rpc_restart_call_prepare(task);
7909 task->tk_status = -EOVERFLOW;
7913 static size_t max_response_pages(struct nfs_server *server)
7915 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
7916 return nfs_page_array_len(0, max_resp_sz);
7919 static void nfs4_free_pages(struct page **pages, size_t size)
7926 for (i = 0; i < size; i++) {
7929 __free_page(pages[i]);
7934 static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
7936 struct page **pages;
7939 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
7941 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
7945 for (i = 0; i < size; i++) {
7946 pages[i] = alloc_page(gfp_flags);
7948 dprintk("%s: failed to allocate page\n", __func__);
7949 nfs4_free_pages(pages, size);
7957 static void nfs4_layoutget_release(void *calldata)
7959 struct nfs4_layoutget *lgp = calldata;
7960 struct inode *inode = lgp->args.inode;
7961 struct nfs_server *server = NFS_SERVER(inode);
7962 size_t max_pages = max_response_pages(server);
7964 dprintk("--> %s\n", __func__);
7965 nfs4_free_pages(lgp->args.layout.pages, max_pages);
7966 pnfs_put_layout_hdr(NFS_I(inode)->layout);
7967 put_nfs_open_context(lgp->args.ctx);
7969 dprintk("<-- %s\n", __func__);
7972 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
7973 .rpc_call_prepare = nfs4_layoutget_prepare,
7974 .rpc_call_done = nfs4_layoutget_done,
7975 .rpc_release = nfs4_layoutget_release,
7978 struct pnfs_layout_segment *
7979 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
7981 struct inode *inode = lgp->args.inode;
7982 struct nfs_server *server = NFS_SERVER(inode);
7983 size_t max_pages = max_response_pages(server);
7984 struct rpc_task *task;
7985 struct rpc_message msg = {
7986 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
7987 .rpc_argp = &lgp->args,
7988 .rpc_resp = &lgp->res,
7989 .rpc_cred = lgp->cred,
7991 struct rpc_task_setup task_setup_data = {
7992 .rpc_client = server->client,
7993 .rpc_message = &msg,
7994 .callback_ops = &nfs4_layoutget_call_ops,
7995 .callback_data = lgp,
7996 .flags = RPC_TASK_ASYNC,
7998 struct pnfs_layout_segment *lseg = NULL;
8001 dprintk("--> %s\n", __func__);
8003 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8004 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8006 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8007 if (!lgp->args.layout.pages) {
8008 nfs4_layoutget_release(lgp);
8009 return ERR_PTR(-ENOMEM);
8011 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8012 lgp->args.timestamp = jiffies;
8014 lgp->res.layoutp = &lgp->args.layout;
8015 lgp->res.seq_res.sr_slot = NULL;
8016 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
8018 task = rpc_run_task(&task_setup_data);
8020 return ERR_CAST(task);
8021 status = nfs4_wait_for_completion_rpc_task(task);
8023 status = task->tk_status;
8024 trace_nfs4_layoutget(lgp->args.ctx,
8028 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8029 if (status == 0 && lgp->res.layoutp->len)
8030 lseg = pnfs_layout_process(lgp);
8032 dprintk("<-- %s status=%d\n", __func__, status);
8034 return ERR_PTR(status);
8039 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8041 struct nfs4_layoutreturn *lrp = calldata;
8043 dprintk("--> %s\n", __func__);
8044 nfs41_setup_sequence(lrp->clp->cl_session,
8045 &lrp->args.seq_args,
8050 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8052 struct nfs4_layoutreturn *lrp = calldata;
8053 struct nfs_server *server;
8055 dprintk("--> %s\n", __func__);
8057 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
8060 server = NFS_SERVER(lrp->args.inode);
8061 switch (task->tk_status) {
8063 task->tk_status = 0;
8066 case -NFS4ERR_DELAY:
8067 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
8069 rpc_restart_call_prepare(task);
8072 dprintk("<-- %s\n", __func__);
8075 static void nfs4_layoutreturn_release(void *calldata)
8077 struct nfs4_layoutreturn *lrp = calldata;
8078 struct pnfs_layout_hdr *lo = lrp->args.layout;
8081 dprintk("--> %s\n", __func__);
8082 spin_lock(&lo->plh_inode->i_lock);
8083 if (lrp->res.lrs_present)
8084 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
8085 pnfs_mark_matching_lsegs_invalid(lo, &freeme, &lrp->args.range);
8086 pnfs_clear_layoutreturn_waitbit(lo);
8087 lo->plh_block_lgets--;
8088 spin_unlock(&lo->plh_inode->i_lock);
8089 pnfs_free_lseg_list(&freeme);
8090 pnfs_put_layout_hdr(lrp->args.layout);
8091 nfs_iput_and_deactive(lrp->inode);
8093 dprintk("<-- %s\n", __func__);
8096 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8097 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8098 .rpc_call_done = nfs4_layoutreturn_done,
8099 .rpc_release = nfs4_layoutreturn_release,
8102 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
8104 struct rpc_task *task;
8105 struct rpc_message msg = {
8106 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8107 .rpc_argp = &lrp->args,
8108 .rpc_resp = &lrp->res,
8109 .rpc_cred = lrp->cred,
8111 struct rpc_task_setup task_setup_data = {
8112 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
8113 .rpc_message = &msg,
8114 .callback_ops = &nfs4_layoutreturn_call_ops,
8115 .callback_data = lrp,
8119 dprintk("--> %s\n", __func__);
8121 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8123 nfs4_layoutreturn_release(lrp);
8126 task_setup_data.flags |= RPC_TASK_ASYNC;
8128 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
8129 task = rpc_run_task(&task_setup_data);
8131 return PTR_ERR(task);
8133 status = task->tk_status;
8134 trace_nfs4_layoutreturn(lrp->args.inode, status);
8135 dprintk("<-- %s status=%d\n", __func__, status);
8141 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
8142 struct pnfs_device *pdev,
8143 struct rpc_cred *cred)
8145 struct nfs4_getdeviceinfo_args args = {
8147 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8148 NOTIFY_DEVICEID4_DELETE,
8150 struct nfs4_getdeviceinfo_res res = {
8153 struct rpc_message msg = {
8154 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8161 dprintk("--> %s\n", __func__);
8162 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
8163 if (res.notification & ~args.notify_types)
8164 dprintk("%s: unsupported notification\n", __func__);
8165 if (res.notification != args.notify_types)
8168 dprintk("<-- %s status=%d\n", __func__, status);
8173 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8174 struct pnfs_device *pdev,
8175 struct rpc_cred *cred)
8177 struct nfs4_exception exception = { };
8181 err = nfs4_handle_exception(server,
8182 _nfs4_proc_getdeviceinfo(server, pdev, cred),
8184 } while (exception.retry);
8187 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8189 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8191 struct nfs4_layoutcommit_data *data = calldata;
8192 struct nfs_server *server = NFS_SERVER(data->args.inode);
8193 struct nfs4_session *session = nfs4_get_session(server);
8195 nfs41_setup_sequence(session,
8196 &data->args.seq_args,
8202 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8204 struct nfs4_layoutcommit_data *data = calldata;
8205 struct nfs_server *server = NFS_SERVER(data->args.inode);
8207 if (!nfs41_sequence_done(task, &data->res.seq_res))
8210 switch (task->tk_status) { /* Just ignore these failures */
8211 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8212 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8213 case -NFS4ERR_BADLAYOUT: /* no layout */
8214 case -NFS4ERR_GRACE: /* loca_recalim always false */
8215 task->tk_status = 0;
8219 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
8220 rpc_restart_call_prepare(task);
8226 static void nfs4_layoutcommit_release(void *calldata)
8228 struct nfs4_layoutcommit_data *data = calldata;
8230 pnfs_cleanup_layoutcommit(data);
8231 nfs_post_op_update_inode_force_wcc(data->args.inode,
8233 put_rpccred(data->cred);
8234 nfs_iput_and_deactive(data->inode);
8238 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8239 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8240 .rpc_call_done = nfs4_layoutcommit_done,
8241 .rpc_release = nfs4_layoutcommit_release,
8245 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
8247 struct rpc_message msg = {
8248 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8249 .rpc_argp = &data->args,
8250 .rpc_resp = &data->res,
8251 .rpc_cred = data->cred,
8253 struct rpc_task_setup task_setup_data = {
8254 .task = &data->task,
8255 .rpc_client = NFS_CLIENT(data->args.inode),
8256 .rpc_message = &msg,
8257 .callback_ops = &nfs4_layoutcommit_ops,
8258 .callback_data = data,
8260 struct rpc_task *task;
8263 dprintk("NFS: initiating layoutcommit call. sync %d "
8264 "lbw: %llu inode %lu\n", sync,
8265 data->args.lastbytewritten,
8266 data->args.inode->i_ino);
8269 data->inode = nfs_igrab_and_active(data->args.inode);
8270 if (data->inode == NULL) {
8271 nfs4_layoutcommit_release(data);
8274 task_setup_data.flags = RPC_TASK_ASYNC;
8276 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
8277 task = rpc_run_task(&task_setup_data);
8279 return PTR_ERR(task);
8281 status = task->tk_status;
8282 trace_nfs4_layoutcommit(data->args.inode, status);
8283 dprintk("%s: status %d\n", __func__, status);
8289 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8290 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8293 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8294 struct nfs_fsinfo *info,
8295 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
8297 struct nfs41_secinfo_no_name_args args = {
8298 .style = SECINFO_STYLE_CURRENT_FH,
8300 struct nfs4_secinfo_res res = {
8303 struct rpc_message msg = {
8304 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8308 struct rpc_clnt *clnt = server->client;
8309 struct rpc_cred *cred = NULL;
8312 if (use_integrity) {
8313 clnt = server->nfs_client->cl_rpcclient;
8314 cred = nfs4_get_clid_cred(server->nfs_client);
8315 msg.rpc_cred = cred;
8318 dprintk("--> %s\n", __func__);
8319 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8321 dprintk("<-- %s status=%d\n", __func__, status);
8330 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8331 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8333 struct nfs4_exception exception = { };
8336 /* first try using integrity protection */
8337 err = -NFS4ERR_WRONGSEC;
8339 /* try to use integrity protection with machine cred */
8340 if (_nfs4_is_integrity_protected(server->nfs_client))
8341 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8345 * if unable to use integrity protection, or SECINFO with
8346 * integrity protection returns NFS4ERR_WRONGSEC (which is
8347 * disallowed by spec, but exists in deployed servers) use
8348 * the current filesystem's rpc_client and the user cred.
8350 if (err == -NFS4ERR_WRONGSEC)
8351 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8356 case -NFS4ERR_WRONGSEC:
8360 err = nfs4_handle_exception(server, err, &exception);
8362 } while (exception.retry);
8368 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8369 struct nfs_fsinfo *info)
8373 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
8374 struct nfs4_secinfo_flavors *flavors;
8375 struct nfs4_secinfo4 *secinfo;
8378 page = alloc_page(GFP_KERNEL);
8384 flavors = page_address(page);
8385 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8388 * Fall back on "guess and check" method if
8389 * the server doesn't support SECINFO_NO_NAME
8391 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
8392 err = nfs4_find_root_sec(server, fhandle, info);
8398 for (i = 0; i < flavors->num_flavors; i++) {
8399 secinfo = &flavors->flavors[i];
8401 switch (secinfo->flavor) {
8405 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8406 &secinfo->flavor_info);
8409 flavor = RPC_AUTH_MAXFLAVOR;
8413 if (!nfs_auth_info_match(&server->auth_info, flavor))
8414 flavor = RPC_AUTH_MAXFLAVOR;
8416 if (flavor != RPC_AUTH_MAXFLAVOR) {
8417 err = nfs4_lookup_root_sec(server, fhandle,
8424 if (flavor == RPC_AUTH_MAXFLAVOR)
8435 static int _nfs41_test_stateid(struct nfs_server *server,
8436 nfs4_stateid *stateid,
8437 struct rpc_cred *cred)
8440 struct nfs41_test_stateid_args args = {
8443 struct nfs41_test_stateid_res res;
8444 struct rpc_message msg = {
8445 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8450 struct rpc_clnt *rpc_client = server->client;
8452 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8455 dprintk("NFS call test_stateid %p\n", stateid);
8456 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
8457 nfs4_set_sequence_privileged(&args.seq_args);
8458 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
8459 &args.seq_args, &res.seq_res);
8460 if (status != NFS_OK) {
8461 dprintk("NFS reply test_stateid: failed, %d\n", status);
8464 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
8469 * nfs41_test_stateid - perform a TEST_STATEID operation
8471 * @server: server / transport on which to perform the operation
8472 * @stateid: state ID to test
8475 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8476 * Otherwise a negative NFS4ERR value is returned if the operation
8477 * failed or the state ID is not currently valid.
8479 static int nfs41_test_stateid(struct nfs_server *server,
8480 nfs4_stateid *stateid,
8481 struct rpc_cred *cred)
8483 struct nfs4_exception exception = { };
8486 err = _nfs41_test_stateid(server, stateid, cred);
8487 if (err != -NFS4ERR_DELAY)
8489 nfs4_handle_exception(server, err, &exception);
8490 } while (exception.retry);
8494 struct nfs_free_stateid_data {
8495 struct nfs_server *server;
8496 struct nfs41_free_stateid_args args;
8497 struct nfs41_free_stateid_res res;
8500 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
8502 struct nfs_free_stateid_data *data = calldata;
8503 nfs41_setup_sequence(nfs4_get_session(data->server),
8504 &data->args.seq_args,
8509 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
8511 struct nfs_free_stateid_data *data = calldata;
8513 nfs41_sequence_done(task, &data->res.seq_res);
8515 switch (task->tk_status) {
8516 case -NFS4ERR_DELAY:
8517 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
8518 rpc_restart_call_prepare(task);
8522 static void nfs41_free_stateid_release(void *calldata)
8527 static const struct rpc_call_ops nfs41_free_stateid_ops = {
8528 .rpc_call_prepare = nfs41_free_stateid_prepare,
8529 .rpc_call_done = nfs41_free_stateid_done,
8530 .rpc_release = nfs41_free_stateid_release,
8533 static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
8534 nfs4_stateid *stateid,
8535 struct rpc_cred *cred,
8538 struct rpc_message msg = {
8539 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
8542 struct rpc_task_setup task_setup = {
8543 .rpc_client = server->client,
8544 .rpc_message = &msg,
8545 .callback_ops = &nfs41_free_stateid_ops,
8546 .flags = RPC_TASK_ASYNC,
8548 struct nfs_free_stateid_data *data;
8550 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8551 &task_setup.rpc_client, &msg);
8553 dprintk("NFS call free_stateid %p\n", stateid);
8554 data = kmalloc(sizeof(*data), GFP_NOFS);
8556 return ERR_PTR(-ENOMEM);
8557 data->server = server;
8558 nfs4_stateid_copy(&data->args.stateid, stateid);
8560 task_setup.callback_data = data;
8562 msg.rpc_argp = &data->args;
8563 msg.rpc_resp = &data->res;
8564 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
8566 nfs4_set_sequence_privileged(&data->args.seq_args);
8568 return rpc_run_task(&task_setup);
8572 * nfs41_free_stateid - perform a FREE_STATEID operation
8574 * @server: server / transport on which to perform the operation
8575 * @stateid: state ID to release
8578 * Returns NFS_OK if the server freed "stateid". Otherwise a
8579 * negative NFS4ERR value is returned.
8581 static int nfs41_free_stateid(struct nfs_server *server,
8582 nfs4_stateid *stateid,
8583 struct rpc_cred *cred)
8585 struct rpc_task *task;
8588 task = _nfs41_free_stateid(server, stateid, cred, true);
8590 return PTR_ERR(task);
8591 ret = rpc_wait_for_completion_task(task);
8593 ret = task->tk_status;
8599 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
8601 struct rpc_task *task;
8602 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
8604 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
8605 nfs4_free_lock_state(server, lsp);
8611 static bool nfs41_match_stateid(const nfs4_stateid *s1,
8612 const nfs4_stateid *s2)
8614 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
8617 if (s1->seqid == s2->seqid)
8619 if (s1->seqid == 0 || s2->seqid == 0)
8625 #endif /* CONFIG_NFS_V4_1 */
8627 static bool nfs4_match_stateid(const nfs4_stateid *s1,
8628 const nfs4_stateid *s2)
8630 return nfs4_stateid_match(s1, s2);
8634 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
8635 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
8636 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
8637 .recover_open = nfs4_open_reclaim,
8638 .recover_lock = nfs4_lock_reclaim,
8639 .establish_clid = nfs4_init_clientid,
8640 .detect_trunking = nfs40_discover_server_trunking,
8643 #if defined(CONFIG_NFS_V4_1)
8644 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
8645 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
8646 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
8647 .recover_open = nfs4_open_reclaim,
8648 .recover_lock = nfs4_lock_reclaim,
8649 .establish_clid = nfs41_init_clientid,
8650 .reclaim_complete = nfs41_proc_reclaim_complete,
8651 .detect_trunking = nfs41_discover_server_trunking,
8653 #endif /* CONFIG_NFS_V4_1 */
8655 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
8656 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
8657 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
8658 .recover_open = nfs40_open_expired,
8659 .recover_lock = nfs4_lock_expired,
8660 .establish_clid = nfs4_init_clientid,
8663 #if defined(CONFIG_NFS_V4_1)
8664 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
8665 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
8666 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
8667 .recover_open = nfs41_open_expired,
8668 .recover_lock = nfs41_lock_expired,
8669 .establish_clid = nfs41_init_clientid,
8671 #endif /* CONFIG_NFS_V4_1 */
8673 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
8674 .sched_state_renewal = nfs4_proc_async_renew,
8675 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
8676 .renew_lease = nfs4_proc_renew,
8679 #if defined(CONFIG_NFS_V4_1)
8680 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
8681 .sched_state_renewal = nfs41_proc_async_sequence,
8682 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
8683 .renew_lease = nfs4_proc_sequence,
8687 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
8688 .get_locations = _nfs40_proc_get_locations,
8689 .fsid_present = _nfs40_proc_fsid_present,
8692 #if defined(CONFIG_NFS_V4_1)
8693 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
8694 .get_locations = _nfs41_proc_get_locations,
8695 .fsid_present = _nfs41_proc_fsid_present,
8697 #endif /* CONFIG_NFS_V4_1 */
8699 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
8701 .init_caps = NFS_CAP_READDIRPLUS
8702 | NFS_CAP_ATOMIC_OPEN
8703 | NFS_CAP_POSIX_LOCK,
8704 .init_client = nfs40_init_client,
8705 .shutdown_client = nfs40_shutdown_client,
8706 .match_stateid = nfs4_match_stateid,
8707 .find_root_sec = nfs4_find_root_sec,
8708 .free_lock_state = nfs4_release_lockowner,
8709 .alloc_seqid = nfs_alloc_seqid,
8710 .call_sync_ops = &nfs40_call_sync_ops,
8711 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
8712 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
8713 .state_renewal_ops = &nfs40_state_renewal_ops,
8714 .mig_recovery_ops = &nfs40_mig_recovery_ops,
8717 #if defined(CONFIG_NFS_V4_1)
8718 static struct nfs_seqid *
8719 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
8724 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
8726 .init_caps = NFS_CAP_READDIRPLUS
8727 | NFS_CAP_ATOMIC_OPEN
8728 | NFS_CAP_POSIX_LOCK
8729 | NFS_CAP_STATEID_NFSV41
8730 | NFS_CAP_ATOMIC_OPEN_V1,
8731 .init_client = nfs41_init_client,
8732 .shutdown_client = nfs41_shutdown_client,
8733 .match_stateid = nfs41_match_stateid,
8734 .find_root_sec = nfs41_find_root_sec,
8735 .free_lock_state = nfs41_free_lock_state,
8736 .alloc_seqid = nfs_alloc_no_seqid,
8737 .call_sync_ops = &nfs41_call_sync_ops,
8738 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8739 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8740 .state_renewal_ops = &nfs41_state_renewal_ops,
8741 .mig_recovery_ops = &nfs41_mig_recovery_ops,
8745 #if defined(CONFIG_NFS_V4_2)
8746 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
8748 .init_caps = NFS_CAP_READDIRPLUS
8749 | NFS_CAP_ATOMIC_OPEN
8750 | NFS_CAP_POSIX_LOCK
8751 | NFS_CAP_STATEID_NFSV41
8752 | NFS_CAP_ATOMIC_OPEN_V1
8754 | NFS_CAP_DEALLOCATE
8756 | NFS_CAP_LAYOUTSTATS,
8757 .init_client = nfs41_init_client,
8758 .shutdown_client = nfs41_shutdown_client,
8759 .match_stateid = nfs41_match_stateid,
8760 .find_root_sec = nfs41_find_root_sec,
8761 .free_lock_state = nfs41_free_lock_state,
8762 .call_sync_ops = &nfs41_call_sync_ops,
8763 .alloc_seqid = nfs_alloc_no_seqid,
8764 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8765 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8766 .state_renewal_ops = &nfs41_state_renewal_ops,
8767 .mig_recovery_ops = &nfs41_mig_recovery_ops,
8771 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
8772 [0] = &nfs_v4_0_minor_ops,
8773 #if defined(CONFIG_NFS_V4_1)
8774 [1] = &nfs_v4_1_minor_ops,
8776 #if defined(CONFIG_NFS_V4_2)
8777 [2] = &nfs_v4_2_minor_ops,
8781 static const struct inode_operations nfs4_dir_inode_operations = {
8782 .create = nfs_create,
8783 .lookup = nfs_lookup,
8784 .atomic_open = nfs_atomic_open,
8786 .unlink = nfs_unlink,
8787 .symlink = nfs_symlink,
8791 .rename = nfs_rename,
8792 .permission = nfs_permission,
8793 .getattr = nfs_getattr,
8794 .setattr = nfs_setattr,
8795 .getxattr = generic_getxattr,
8796 .setxattr = generic_setxattr,
8797 .listxattr = generic_listxattr,
8798 .removexattr = generic_removexattr,
8801 static const struct inode_operations nfs4_file_inode_operations = {
8802 .permission = nfs_permission,
8803 .getattr = nfs_getattr,
8804 .setattr = nfs_setattr,
8805 .getxattr = generic_getxattr,
8806 .setxattr = generic_setxattr,
8807 .listxattr = generic_listxattr,
8808 .removexattr = generic_removexattr,
8811 const struct nfs_rpc_ops nfs_v4_clientops = {
8812 .version = 4, /* protocol version */
8813 .dentry_ops = &nfs4_dentry_operations,
8814 .dir_inode_ops = &nfs4_dir_inode_operations,
8815 .file_inode_ops = &nfs4_file_inode_operations,
8816 .file_ops = &nfs4_file_operations,
8817 .getroot = nfs4_proc_get_root,
8818 .submount = nfs4_submount,
8819 .try_mount = nfs4_try_mount,
8820 .getattr = nfs4_proc_getattr,
8821 .setattr = nfs4_proc_setattr,
8822 .lookup = nfs4_proc_lookup,
8823 .access = nfs4_proc_access,
8824 .readlink = nfs4_proc_readlink,
8825 .create = nfs4_proc_create,
8826 .remove = nfs4_proc_remove,
8827 .unlink_setup = nfs4_proc_unlink_setup,
8828 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
8829 .unlink_done = nfs4_proc_unlink_done,
8830 .rename_setup = nfs4_proc_rename_setup,
8831 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
8832 .rename_done = nfs4_proc_rename_done,
8833 .link = nfs4_proc_link,
8834 .symlink = nfs4_proc_symlink,
8835 .mkdir = nfs4_proc_mkdir,
8836 .rmdir = nfs4_proc_remove,
8837 .readdir = nfs4_proc_readdir,
8838 .mknod = nfs4_proc_mknod,
8839 .statfs = nfs4_proc_statfs,
8840 .fsinfo = nfs4_proc_fsinfo,
8841 .pathconf = nfs4_proc_pathconf,
8842 .set_capabilities = nfs4_server_capabilities,
8843 .decode_dirent = nfs4_decode_dirent,
8844 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
8845 .read_setup = nfs4_proc_read_setup,
8846 .read_done = nfs4_read_done,
8847 .write_setup = nfs4_proc_write_setup,
8848 .write_done = nfs4_write_done,
8849 .commit_setup = nfs4_proc_commit_setup,
8850 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
8851 .commit_done = nfs4_commit_done,
8852 .lock = nfs4_proc_lock,
8853 .clear_acl_cache = nfs4_zap_acl_attr,
8854 .close_context = nfs4_close_context,
8855 .open_context = nfs4_atomic_open,
8856 .have_delegation = nfs4_have_delegation,
8857 .return_delegation = nfs4_inode_return_delegation,
8858 .alloc_client = nfs4_alloc_client,
8859 .init_client = nfs4_init_client,
8860 .free_client = nfs4_free_client,
8861 .create_server = nfs4_create_server,
8862 .clone_server = nfs_clone_server,
8865 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
8866 .prefix = XATTR_NAME_NFSV4_ACL,
8867 .list = nfs4_xattr_list_nfs4_acl,
8868 .get = nfs4_xattr_get_nfs4_acl,
8869 .set = nfs4_xattr_set_nfs4_acl,
8872 const struct xattr_handler *nfs4_xattr_handlers[] = {
8873 &nfs4_xattr_nfs4_acl_handler,
8874 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
8875 &nfs4_xattr_nfs4_label_handler,