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 *, const 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 int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
349 struct nfs_client *clp = server->nfs_client;
350 struct nfs4_state *state = exception->state;
351 struct inode *inode = exception->inode;
354 exception->retry = 0;
358 case -NFS4ERR_OPENMODE:
359 case -NFS4ERR_DELEG_REVOKED:
360 case -NFS4ERR_ADMIN_REVOKED:
361 case -NFS4ERR_BAD_STATEID:
362 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
363 nfs4_inode_return_delegation(inode);
364 exception->retry = 1;
369 ret = nfs4_schedule_stateid_recovery(server, state);
372 goto wait_on_recovery;
373 case -NFS4ERR_EXPIRED:
375 ret = nfs4_schedule_stateid_recovery(server, state);
379 case -NFS4ERR_STALE_STATEID:
380 case -NFS4ERR_STALE_CLIENTID:
381 nfs4_schedule_lease_recovery(clp);
382 goto wait_on_recovery;
384 ret = nfs4_schedule_migration_recovery(server);
387 goto wait_on_recovery;
388 case -NFS4ERR_LEASE_MOVED:
389 nfs4_schedule_lease_moved_recovery(clp);
390 goto wait_on_recovery;
391 #if defined(CONFIG_NFS_V4_1)
392 case -NFS4ERR_BADSESSION:
393 case -NFS4ERR_BADSLOT:
394 case -NFS4ERR_BAD_HIGH_SLOT:
395 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
396 case -NFS4ERR_DEADSESSION:
397 case -NFS4ERR_SEQ_FALSE_RETRY:
398 case -NFS4ERR_SEQ_MISORDERED:
399 dprintk("%s ERROR: %d Reset session\n", __func__,
401 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
402 goto wait_on_recovery;
403 #endif /* defined(CONFIG_NFS_V4_1) */
404 case -NFS4ERR_FILE_OPEN:
405 if (exception->timeout > HZ) {
406 /* We have retried a decent amount, time to
414 ret = nfs4_delay(server->client, &exception->timeout);
417 case -NFS4ERR_RETRY_UNCACHED_REP:
418 case -NFS4ERR_OLD_STATEID:
419 exception->retry = 1;
421 case -NFS4ERR_BADOWNER:
422 /* The following works around a Linux server bug! */
423 case -NFS4ERR_BADNAME:
424 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
425 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
426 exception->retry = 1;
427 printk(KERN_WARNING "NFS: v4 server %s "
428 "does not accept raw "
430 "Reenabling the idmapper.\n",
431 server->nfs_client->cl_hostname);
434 /* We failed to handle the error */
435 return nfs4_map_errors(ret);
437 ret = nfs4_wait_clnt_recover(clp);
438 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
441 exception->retry = 1;
446 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
447 * or 'false' otherwise.
449 static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
451 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
453 if (flavor == RPC_AUTH_GSS_KRB5I ||
454 flavor == RPC_AUTH_GSS_KRB5P)
460 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
462 spin_lock(&clp->cl_lock);
463 if (time_before(clp->cl_last_renewal,timestamp))
464 clp->cl_last_renewal = timestamp;
465 spin_unlock(&clp->cl_lock);
468 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
470 struct nfs_client *clp = server->nfs_client;
472 if (!nfs4_has_session(clp))
473 do_renew_lease(clp, timestamp);
476 struct nfs4_call_sync_data {
477 const struct nfs_server *seq_server;
478 struct nfs4_sequence_args *seq_args;
479 struct nfs4_sequence_res *seq_res;
482 void nfs4_init_sequence(struct nfs4_sequence_args *args,
483 struct nfs4_sequence_res *res, int cache_reply)
485 args->sa_slot = NULL;
486 args->sa_cache_this = cache_reply;
487 args->sa_privileged = 0;
492 static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
494 args->sa_privileged = 1;
497 int nfs40_setup_sequence(struct nfs4_slot_table *tbl,
498 struct nfs4_sequence_args *args,
499 struct nfs4_sequence_res *res,
500 struct rpc_task *task)
502 struct nfs4_slot *slot;
504 /* slot already allocated? */
505 if (res->sr_slot != NULL)
508 spin_lock(&tbl->slot_tbl_lock);
509 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
512 slot = nfs4_alloc_slot(tbl);
514 if (slot == ERR_PTR(-ENOMEM))
515 task->tk_timeout = HZ >> 2;
518 spin_unlock(&tbl->slot_tbl_lock);
520 args->sa_slot = slot;
524 rpc_call_start(task);
528 if (args->sa_privileged)
529 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
530 NULL, RPC_PRIORITY_PRIVILEGED);
532 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
533 spin_unlock(&tbl->slot_tbl_lock);
536 EXPORT_SYMBOL_GPL(nfs40_setup_sequence);
538 static int nfs40_sequence_done(struct rpc_task *task,
539 struct nfs4_sequence_res *res)
541 struct nfs4_slot *slot = res->sr_slot;
542 struct nfs4_slot_table *tbl;
548 spin_lock(&tbl->slot_tbl_lock);
549 if (!nfs41_wake_and_assign_slot(tbl, slot))
550 nfs4_free_slot(tbl, slot);
551 spin_unlock(&tbl->slot_tbl_lock);
558 #if defined(CONFIG_NFS_V4_1)
560 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
562 struct nfs4_session *session;
563 struct nfs4_slot_table *tbl;
564 struct nfs4_slot *slot = res->sr_slot;
565 bool send_new_highest_used_slotid = false;
568 session = tbl->session;
570 spin_lock(&tbl->slot_tbl_lock);
571 /* Be nice to the server: try to ensure that the last transmitted
572 * value for highest_user_slotid <= target_highest_slotid
574 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
575 send_new_highest_used_slotid = true;
577 if (nfs41_wake_and_assign_slot(tbl, slot)) {
578 send_new_highest_used_slotid = false;
581 nfs4_free_slot(tbl, slot);
583 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
584 send_new_highest_used_slotid = false;
586 spin_unlock(&tbl->slot_tbl_lock);
588 if (send_new_highest_used_slotid)
589 nfs41_server_notify_highest_slotid_update(session->clp);
592 int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
594 struct nfs4_session *session;
595 struct nfs4_slot *slot = res->sr_slot;
596 struct nfs_client *clp;
597 bool interrupted = false;
602 /* don't increment the sequence number if the task wasn't sent */
603 if (!RPC_WAS_SENT(task))
606 session = slot->table->session;
608 if (slot->interrupted) {
609 slot->interrupted = 0;
613 trace_nfs4_sequence_done(session, res);
614 /* Check the SEQUENCE operation status */
615 switch (res->sr_status) {
617 /* Update the slot's sequence and clientid lease timer */
620 do_renew_lease(clp, res->sr_timestamp);
621 /* Check sequence flags */
622 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
623 nfs41_update_target_slotid(slot->table, slot, res);
627 * sr_status remains 1 if an RPC level error occurred.
628 * The server may or may not have processed the sequence
630 * Mark the slot as having hosted an interrupted RPC call.
632 slot->interrupted = 1;
635 /* The server detected a resend of the RPC call and
636 * returned NFS4ERR_DELAY as per Section 2.10.6.2
639 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
644 case -NFS4ERR_BADSLOT:
646 * The slot id we used was probably retired. Try again
647 * using a different slot id.
650 case -NFS4ERR_SEQ_MISORDERED:
652 * Was the last operation on this sequence interrupted?
653 * If so, retry after bumping the sequence number.
660 * Could this slot have been previously retired?
661 * If so, then the server may be expecting seq_nr = 1!
663 if (slot->seq_nr != 1) {
668 case -NFS4ERR_SEQ_FALSE_RETRY:
672 /* Just update the slot sequence no. */
676 /* The session may be reset by one of the error handlers. */
677 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
678 nfs41_sequence_free_slot(res);
682 if (rpc_restart_call_prepare(task)) {
688 if (!rpc_restart_call(task))
690 rpc_delay(task, NFS4_POLL_RETRY_MAX);
693 EXPORT_SYMBOL_GPL(nfs41_sequence_done);
695 int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
697 if (res->sr_slot == NULL)
699 if (!res->sr_slot->table->session)
700 return nfs40_sequence_done(task, res);
701 return nfs41_sequence_done(task, res);
703 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
705 int nfs41_setup_sequence(struct nfs4_session *session,
706 struct nfs4_sequence_args *args,
707 struct nfs4_sequence_res *res,
708 struct rpc_task *task)
710 struct nfs4_slot *slot;
711 struct nfs4_slot_table *tbl;
713 dprintk("--> %s\n", __func__);
714 /* slot already allocated? */
715 if (res->sr_slot != NULL)
718 tbl = &session->fc_slot_table;
720 task->tk_timeout = 0;
722 spin_lock(&tbl->slot_tbl_lock);
723 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
724 !args->sa_privileged) {
725 /* The state manager will wait until the slot table is empty */
726 dprintk("%s session is draining\n", __func__);
730 slot = nfs4_alloc_slot(tbl);
732 /* If out of memory, try again in 1/4 second */
733 if (slot == ERR_PTR(-ENOMEM))
734 task->tk_timeout = HZ >> 2;
735 dprintk("<-- %s: no free slots\n", __func__);
738 spin_unlock(&tbl->slot_tbl_lock);
740 args->sa_slot = slot;
742 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
743 slot->slot_nr, slot->seq_nr);
746 res->sr_timestamp = jiffies;
747 res->sr_status_flags = 0;
749 * sr_status is only set in decode_sequence, and so will remain
750 * set to 1 if an rpc level failure occurs.
753 trace_nfs4_setup_sequence(session, args);
755 rpc_call_start(task);
758 /* Privileged tasks are queued with top priority */
759 if (args->sa_privileged)
760 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
761 NULL, RPC_PRIORITY_PRIVILEGED);
763 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
764 spin_unlock(&tbl->slot_tbl_lock);
767 EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
769 static int nfs4_setup_sequence(const struct nfs_server *server,
770 struct nfs4_sequence_args *args,
771 struct nfs4_sequence_res *res,
772 struct rpc_task *task)
774 struct nfs4_session *session = nfs4_get_session(server);
778 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
781 dprintk("--> %s clp %p session %p sr_slot %u\n",
782 __func__, session->clp, session, res->sr_slot ?
783 res->sr_slot->slot_nr : NFS4_NO_SLOT);
785 ret = nfs41_setup_sequence(session, args, res, task);
787 dprintk("<-- %s status=%d\n", __func__, ret);
791 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
793 struct nfs4_call_sync_data *data = calldata;
794 struct nfs4_session *session = nfs4_get_session(data->seq_server);
796 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
798 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
801 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
803 struct nfs4_call_sync_data *data = calldata;
805 nfs41_sequence_done(task, data->seq_res);
808 static const struct rpc_call_ops nfs41_call_sync_ops = {
809 .rpc_call_prepare = nfs41_call_sync_prepare,
810 .rpc_call_done = nfs41_call_sync_done,
813 #else /* !CONFIG_NFS_V4_1 */
815 static int nfs4_setup_sequence(const struct nfs_server *server,
816 struct nfs4_sequence_args *args,
817 struct nfs4_sequence_res *res,
818 struct rpc_task *task)
820 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
824 int nfs4_sequence_done(struct rpc_task *task,
825 struct nfs4_sequence_res *res)
827 return nfs40_sequence_done(task, res);
829 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
831 #endif /* !CONFIG_NFS_V4_1 */
833 static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
835 struct nfs4_call_sync_data *data = calldata;
836 nfs4_setup_sequence(data->seq_server,
837 data->seq_args, data->seq_res, task);
840 static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
842 struct nfs4_call_sync_data *data = calldata;
843 nfs4_sequence_done(task, data->seq_res);
846 static const struct rpc_call_ops nfs40_call_sync_ops = {
847 .rpc_call_prepare = nfs40_call_sync_prepare,
848 .rpc_call_done = nfs40_call_sync_done,
851 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
852 struct nfs_server *server,
853 struct rpc_message *msg,
854 struct nfs4_sequence_args *args,
855 struct nfs4_sequence_res *res)
858 struct rpc_task *task;
859 struct nfs_client *clp = server->nfs_client;
860 struct nfs4_call_sync_data data = {
861 .seq_server = server,
865 struct rpc_task_setup task_setup = {
868 .callback_ops = clp->cl_mvops->call_sync_ops,
869 .callback_data = &data
872 task = rpc_run_task(&task_setup);
876 ret = task->tk_status;
882 int nfs4_call_sync(struct rpc_clnt *clnt,
883 struct nfs_server *server,
884 struct rpc_message *msg,
885 struct nfs4_sequence_args *args,
886 struct nfs4_sequence_res *res,
889 nfs4_init_sequence(args, res, cache_reply);
890 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
893 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
895 struct nfs_inode *nfsi = NFS_I(dir);
897 spin_lock(&dir->i_lock);
898 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
899 if (!cinfo->atomic || cinfo->before != dir->i_version)
900 nfs_force_lookup_revalidate(dir);
901 dir->i_version = cinfo->after;
902 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
903 nfs_fscache_invalidate(dir);
904 spin_unlock(&dir->i_lock);
907 struct nfs4_opendata {
909 struct nfs_openargs o_arg;
910 struct nfs_openres o_res;
911 struct nfs_open_confirmargs c_arg;
912 struct nfs_open_confirmres c_res;
913 struct nfs4_string owner_name;
914 struct nfs4_string group_name;
915 struct nfs_fattr f_attr;
916 struct nfs4_label *f_label;
918 struct dentry *dentry;
919 struct nfs4_state_owner *owner;
920 struct nfs4_state *state;
922 unsigned long timestamp;
923 unsigned int rpc_done : 1;
924 unsigned int file_created : 1;
925 unsigned int is_recover : 1;
930 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
931 int err, struct nfs4_exception *exception)
935 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
937 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
938 exception->retry = 1;
943 nfs4_map_atomic_open_share(struct nfs_server *server,
944 fmode_t fmode, int openflags)
948 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
950 res = NFS4_SHARE_ACCESS_READ;
953 res = NFS4_SHARE_ACCESS_WRITE;
955 case FMODE_READ|FMODE_WRITE:
956 res = NFS4_SHARE_ACCESS_BOTH;
958 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
960 /* Want no delegation if we're using O_DIRECT */
961 if (openflags & O_DIRECT)
962 res |= NFS4_SHARE_WANT_NO_DELEG;
967 static enum open_claim_type4
968 nfs4_map_atomic_open_claim(struct nfs_server *server,
969 enum open_claim_type4 claim)
971 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
976 case NFS4_OPEN_CLAIM_FH:
977 return NFS4_OPEN_CLAIM_NULL;
978 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
979 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
980 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
981 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
985 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
987 p->o_res.f_attr = &p->f_attr;
988 p->o_res.f_label = p->f_label;
989 p->o_res.seqid = p->o_arg.seqid;
990 p->c_res.seqid = p->c_arg.seqid;
991 p->o_res.server = p->o_arg.server;
992 p->o_res.access_request = p->o_arg.access;
993 nfs_fattr_init(&p->f_attr);
994 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
997 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
998 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
999 const struct iattr *attrs,
1000 struct nfs4_label *label,
1001 enum open_claim_type4 claim,
1004 struct dentry *parent = dget_parent(dentry);
1005 struct inode *dir = d_inode(parent);
1006 struct nfs_server *server = NFS_SERVER(dir);
1007 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
1008 struct nfs4_opendata *p;
1010 p = kzalloc(sizeof(*p), gfp_mask);
1014 p->f_label = nfs4_label_alloc(server, gfp_mask);
1015 if (IS_ERR(p->f_label))
1018 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1019 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
1020 if (IS_ERR(p->o_arg.seqid))
1021 goto err_free_label;
1022 nfs_sb_active(dentry->d_sb);
1023 p->dentry = dget(dentry);
1026 atomic_inc(&sp->so_count);
1027 p->o_arg.open_flags = flags;
1028 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
1029 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1031 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1032 * will return permission denied for all bits until close */
1033 if (!(flags & O_EXCL)) {
1034 /* ask server to check for all possible rights as results
1036 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1037 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
1039 p->o_arg.clientid = server->nfs_client->cl_clientid;
1040 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1041 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1042 p->o_arg.name = &dentry->d_name;
1043 p->o_arg.server = server;
1044 p->o_arg.bitmask = nfs4_bitmask(server, label);
1045 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1046 p->o_arg.label = label;
1047 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1048 switch (p->o_arg.claim) {
1049 case NFS4_OPEN_CLAIM_NULL:
1050 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1051 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1052 p->o_arg.fh = NFS_FH(dir);
1054 case NFS4_OPEN_CLAIM_PREVIOUS:
1055 case NFS4_OPEN_CLAIM_FH:
1056 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1057 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1058 p->o_arg.fh = NFS_FH(d_inode(dentry));
1060 if (attrs != NULL && attrs->ia_valid != 0) {
1063 p->o_arg.u.attrs = &p->attrs;
1064 memcpy(&p->attrs, attrs, sizeof(p->attrs));
1067 verf[1] = current->pid;
1068 memcpy(p->o_arg.u.verifier.data, verf,
1069 sizeof(p->o_arg.u.verifier.data));
1071 p->c_arg.fh = &p->o_res.fh;
1072 p->c_arg.stateid = &p->o_res.stateid;
1073 p->c_arg.seqid = p->o_arg.seqid;
1074 nfs4_init_opendata_res(p);
1075 kref_init(&p->kref);
1079 nfs4_label_free(p->f_label);
1087 static void nfs4_opendata_free(struct kref *kref)
1089 struct nfs4_opendata *p = container_of(kref,
1090 struct nfs4_opendata, kref);
1091 struct super_block *sb = p->dentry->d_sb;
1093 nfs_free_seqid(p->o_arg.seqid);
1094 if (p->state != NULL)
1095 nfs4_put_open_state(p->state);
1096 nfs4_put_state_owner(p->owner);
1098 nfs4_label_free(p->f_label);
1102 nfs_sb_deactive(sb);
1103 nfs_fattr_free_names(&p->f_attr);
1104 kfree(p->f_attr.mdsthreshold);
1108 static void nfs4_opendata_put(struct nfs4_opendata *p)
1111 kref_put(&p->kref, nfs4_opendata_free);
1114 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1118 ret = rpc_wait_for_completion_task(task);
1122 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
1126 if (open_mode & (O_EXCL|O_TRUNC))
1128 switch (mode & (FMODE_READ|FMODE_WRITE)) {
1130 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1131 && state->n_rdonly != 0;
1134 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1135 && state->n_wronly != 0;
1137 case FMODE_READ|FMODE_WRITE:
1138 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1139 && state->n_rdwr != 0;
1145 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
1147 if (delegation == NULL)
1149 if ((delegation->type & fmode) != fmode)
1151 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1153 nfs_mark_delegation_referenced(delegation);
1157 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1166 case FMODE_READ|FMODE_WRITE:
1169 nfs4_state_set_mode_locked(state, state->state | fmode);
1172 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
1174 struct nfs_client *clp = state->owner->so_server->nfs_client;
1175 bool need_recover = false;
1177 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1178 need_recover = true;
1179 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1180 need_recover = true;
1181 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1182 need_recover = true;
1184 nfs4_state_mark_reclaim_nograce(clp, state);
1187 static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1188 nfs4_stateid *stateid)
1190 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1192 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1193 nfs_test_and_clear_all_open_stateid(state);
1196 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1201 static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1203 if (state->n_wronly)
1204 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1205 if (state->n_rdonly)
1206 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1208 set_bit(NFS_O_RDWR_STATE, &state->flags);
1211 static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1212 nfs4_stateid *stateid, fmode_t fmode)
1214 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1215 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1217 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1220 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1223 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1224 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1225 clear_bit(NFS_OPEN_STATE, &state->flags);
1227 if (stateid == NULL)
1229 /* Handle races with OPEN */
1230 if (!nfs4_stateid_match_other(stateid, &state->open_stateid) ||
1231 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1232 nfs_resync_open_stateid_locked(state);
1235 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1236 nfs4_stateid_copy(&state->stateid, stateid);
1237 nfs4_stateid_copy(&state->open_stateid, stateid);
1240 static void nfs_clear_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1242 write_seqlock(&state->seqlock);
1243 nfs_clear_open_stateid_locked(state, stateid, fmode);
1244 write_sequnlock(&state->seqlock);
1245 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1246 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1249 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1253 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1256 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1258 case FMODE_READ|FMODE_WRITE:
1259 set_bit(NFS_O_RDWR_STATE, &state->flags);
1261 if (!nfs_need_update_open_stateid(state, stateid))
1263 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1264 nfs4_stateid_copy(&state->stateid, stateid);
1265 nfs4_stateid_copy(&state->open_stateid, stateid);
1268 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
1271 * Protect the call to nfs4_state_set_mode_locked and
1272 * serialise the stateid update
1274 write_seqlock(&state->seqlock);
1275 if (deleg_stateid != NULL) {
1276 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1277 set_bit(NFS_DELEGATED_STATE, &state->flags);
1279 if (open_stateid != NULL)
1280 nfs_set_open_stateid_locked(state, open_stateid, fmode);
1281 write_sequnlock(&state->seqlock);
1282 spin_lock(&state->owner->so_lock);
1283 update_open_stateflags(state, fmode);
1284 spin_unlock(&state->owner->so_lock);
1287 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
1289 struct nfs_inode *nfsi = NFS_I(state->inode);
1290 struct nfs_delegation *deleg_cur;
1293 fmode &= (FMODE_READ|FMODE_WRITE);
1296 deleg_cur = rcu_dereference(nfsi->delegation);
1297 if (deleg_cur == NULL)
1300 spin_lock(&deleg_cur->lock);
1301 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1302 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1303 (deleg_cur->type & fmode) != fmode)
1304 goto no_delegation_unlock;
1306 if (delegation == NULL)
1307 delegation = &deleg_cur->stateid;
1308 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
1309 goto no_delegation_unlock;
1311 nfs_mark_delegation_referenced(deleg_cur);
1312 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
1314 no_delegation_unlock:
1315 spin_unlock(&deleg_cur->lock);
1319 if (!ret && open_stateid != NULL) {
1320 __update_open_stateid(state, open_stateid, NULL, fmode);
1323 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1324 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1329 static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1330 const nfs4_stateid *stateid)
1332 struct nfs4_state *state = lsp->ls_state;
1335 spin_lock(&state->state_lock);
1336 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1338 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1340 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1343 spin_unlock(&state->state_lock);
1347 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1349 struct nfs_delegation *delegation;
1352 delegation = rcu_dereference(NFS_I(inode)->delegation);
1353 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1358 nfs4_inode_return_delegation(inode);
1361 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1363 struct nfs4_state *state = opendata->state;
1364 struct nfs_inode *nfsi = NFS_I(state->inode);
1365 struct nfs_delegation *delegation;
1366 int open_mode = opendata->o_arg.open_flags;
1367 fmode_t fmode = opendata->o_arg.fmode;
1368 nfs4_stateid stateid;
1372 spin_lock(&state->owner->so_lock);
1373 if (can_open_cached(state, fmode, open_mode)) {
1374 update_open_stateflags(state, fmode);
1375 spin_unlock(&state->owner->so_lock);
1376 goto out_return_state;
1378 spin_unlock(&state->owner->so_lock);
1380 delegation = rcu_dereference(nfsi->delegation);
1381 if (!can_open_delegated(delegation, fmode)) {
1385 /* Save the delegation */
1386 nfs4_stateid_copy(&stateid, &delegation->stateid);
1388 nfs_release_seqid(opendata->o_arg.seqid);
1389 if (!opendata->is_recover) {
1390 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1396 /* Try to update the stateid using the delegation */
1397 if (update_open_stateid(state, NULL, &stateid, fmode))
1398 goto out_return_state;
1401 return ERR_PTR(ret);
1403 atomic_inc(&state->count);
1408 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1410 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1411 struct nfs_delegation *delegation;
1412 int delegation_flags = 0;
1415 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1417 delegation_flags = delegation->flags;
1419 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1420 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1421 "returning a delegation for "
1422 "OPEN(CLAIM_DELEGATE_CUR)\n",
1424 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1425 nfs_inode_set_delegation(state->inode,
1426 data->owner->so_cred,
1429 nfs_inode_reclaim_delegation(state->inode,
1430 data->owner->so_cred,
1435 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1436 * and update the nfs4_state.
1438 static struct nfs4_state *
1439 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1441 struct inode *inode = data->state->inode;
1442 struct nfs4_state *state = data->state;
1445 if (!data->rpc_done) {
1446 if (data->rpc_status) {
1447 ret = data->rpc_status;
1450 /* cached opens have already been processed */
1454 ret = nfs_refresh_inode(inode, &data->f_attr);
1458 if (data->o_res.delegation_type != 0)
1459 nfs4_opendata_check_deleg(data, state);
1461 update_open_stateid(state, &data->o_res.stateid, NULL,
1463 atomic_inc(&state->count);
1467 return ERR_PTR(ret);
1471 static struct nfs4_state *
1472 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1474 struct inode *inode;
1475 struct nfs4_state *state = NULL;
1478 if (!data->rpc_done) {
1479 state = nfs4_try_open_cached(data);
1484 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1486 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
1487 ret = PTR_ERR(inode);
1491 state = nfs4_get_open_state(inode, data->owner);
1494 if (data->o_res.delegation_type != 0)
1495 nfs4_opendata_check_deleg(data, state);
1496 update_open_stateid(state, &data->o_res.stateid, NULL,
1500 nfs_release_seqid(data->o_arg.seqid);
1505 return ERR_PTR(ret);
1508 static struct nfs4_state *
1509 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1511 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1512 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1513 return _nfs4_opendata_to_nfs4_state(data);
1516 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1518 struct nfs_inode *nfsi = NFS_I(state->inode);
1519 struct nfs_open_context *ctx;
1521 spin_lock(&state->inode->i_lock);
1522 list_for_each_entry(ctx, &nfsi->open_files, list) {
1523 if (ctx->state != state)
1525 get_nfs_open_context(ctx);
1526 spin_unlock(&state->inode->i_lock);
1529 spin_unlock(&state->inode->i_lock);
1530 return ERR_PTR(-ENOENT);
1533 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1534 struct nfs4_state *state, enum open_claim_type4 claim)
1536 struct nfs4_opendata *opendata;
1538 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
1539 NULL, NULL, claim, GFP_NOFS);
1540 if (opendata == NULL)
1541 return ERR_PTR(-ENOMEM);
1542 opendata->state = state;
1543 atomic_inc(&state->count);
1547 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1549 struct nfs4_state *newstate;
1552 if ((opendata->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
1553 opendata->o_arg.claim == NFS4_OPEN_CLAIM_DELEG_CUR_FH) &&
1554 (opendata->o_arg.u.delegation_type & fmode) != fmode)
1555 /* This mode can't have been delegated, so we must have
1556 * a valid open_stateid to cover it - not need to reclaim.
1559 opendata->o_arg.open_flags = 0;
1560 opendata->o_arg.fmode = fmode;
1561 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1562 NFS_SB(opendata->dentry->d_sb),
1564 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1565 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1566 nfs4_init_opendata_res(opendata);
1567 ret = _nfs4_recover_proc_open(opendata);
1570 newstate = nfs4_opendata_to_nfs4_state(opendata);
1571 if (IS_ERR(newstate))
1572 return PTR_ERR(newstate);
1573 nfs4_close_state(newstate, fmode);
1578 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1580 struct nfs4_state *newstate;
1583 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1584 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1585 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1586 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1587 /* memory barrier prior to reading state->n_* */
1588 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1589 clear_bit(NFS_OPEN_STATE, &state->flags);
1591 if (state->n_rdwr != 0) {
1592 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1595 if (newstate != state)
1598 if (state->n_wronly != 0) {
1599 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1602 if (newstate != state)
1605 if (state->n_rdonly != 0) {
1606 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1609 if (newstate != state)
1613 * We may have performed cached opens for all three recoveries.
1614 * Check if we need to update the current stateid.
1616 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1617 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
1618 write_seqlock(&state->seqlock);
1619 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1620 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1621 write_sequnlock(&state->seqlock);
1628 * reclaim state on the server after a reboot.
1630 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1632 struct nfs_delegation *delegation;
1633 struct nfs4_opendata *opendata;
1634 fmode_t delegation_type = 0;
1637 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1638 NFS4_OPEN_CLAIM_PREVIOUS);
1639 if (IS_ERR(opendata))
1640 return PTR_ERR(opendata);
1642 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1643 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1644 delegation_type = delegation->type;
1646 opendata->o_arg.u.delegation_type = delegation_type;
1647 status = nfs4_open_recover(opendata, state);
1648 nfs4_opendata_put(opendata);
1652 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1654 struct nfs_server *server = NFS_SERVER(state->inode);
1655 struct nfs4_exception exception = { };
1658 err = _nfs4_do_open_reclaim(ctx, state);
1659 trace_nfs4_open_reclaim(ctx, 0, err);
1660 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1662 if (err != -NFS4ERR_DELAY)
1664 nfs4_handle_exception(server, err, &exception);
1665 } while (exception.retry);
1669 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1671 struct nfs_open_context *ctx;
1674 ctx = nfs4_state_find_open_context(state);
1677 ret = nfs4_do_open_reclaim(ctx, state);
1678 put_nfs_open_context(ctx);
1682 static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, int err)
1686 printk(KERN_ERR "NFS: %s: unhandled error "
1687 "%d.\n", __func__, err);
1693 case -NFS4ERR_BADSESSION:
1694 case -NFS4ERR_BADSLOT:
1695 case -NFS4ERR_BAD_HIGH_SLOT:
1696 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1697 case -NFS4ERR_DEADSESSION:
1698 set_bit(NFS_DELEGATED_STATE, &state->flags);
1699 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1701 case -NFS4ERR_STALE_CLIENTID:
1702 case -NFS4ERR_STALE_STATEID:
1703 set_bit(NFS_DELEGATED_STATE, &state->flags);
1704 case -NFS4ERR_EXPIRED:
1705 /* Don't recall a delegation if it was lost */
1706 nfs4_schedule_lease_recovery(server->nfs_client);
1708 case -NFS4ERR_MOVED:
1709 nfs4_schedule_migration_recovery(server);
1711 case -NFS4ERR_LEASE_MOVED:
1712 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1714 case -NFS4ERR_DELEG_REVOKED:
1715 case -NFS4ERR_ADMIN_REVOKED:
1716 case -NFS4ERR_BAD_STATEID:
1717 case -NFS4ERR_OPENMODE:
1718 nfs_inode_find_state_and_recover(state->inode,
1720 nfs4_schedule_stateid_recovery(server, state);
1722 case -NFS4ERR_DELAY:
1723 case -NFS4ERR_GRACE:
1724 set_bit(NFS_DELEGATED_STATE, &state->flags);
1728 case -NFS4ERR_DENIED:
1729 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1735 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1737 struct nfs_server *server = NFS_SERVER(state->inode);
1738 struct nfs4_opendata *opendata;
1741 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1742 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1743 if (IS_ERR(opendata))
1744 return PTR_ERR(opendata);
1745 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1746 err = nfs4_open_recover(opendata, state);
1747 nfs4_opendata_put(opendata);
1748 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1751 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1753 struct nfs4_opendata *data = calldata;
1755 nfs40_setup_sequence(data->o_arg.server->nfs_client->cl_slot_tbl,
1756 &data->c_arg.seq_args, &data->c_res.seq_res, task);
1759 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1761 struct nfs4_opendata *data = calldata;
1763 nfs40_sequence_done(task, &data->c_res.seq_res);
1765 data->rpc_status = task->tk_status;
1766 if (data->rpc_status == 0) {
1767 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
1768 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1769 renew_lease(data->o_res.server, data->timestamp);
1774 static void nfs4_open_confirm_release(void *calldata)
1776 struct nfs4_opendata *data = calldata;
1777 struct nfs4_state *state = NULL;
1779 /* If this request hasn't been cancelled, do nothing */
1780 if (data->cancelled == 0)
1782 /* In case of error, no cleanup! */
1783 if (!data->rpc_done)
1785 state = nfs4_opendata_to_nfs4_state(data);
1787 nfs4_close_state(state, data->o_arg.fmode);
1789 nfs4_opendata_put(data);
1792 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1793 .rpc_call_prepare = nfs4_open_confirm_prepare,
1794 .rpc_call_done = nfs4_open_confirm_done,
1795 .rpc_release = nfs4_open_confirm_release,
1799 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1801 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1803 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
1804 struct rpc_task *task;
1805 struct rpc_message msg = {
1806 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1807 .rpc_argp = &data->c_arg,
1808 .rpc_resp = &data->c_res,
1809 .rpc_cred = data->owner->so_cred,
1811 struct rpc_task_setup task_setup_data = {
1812 .rpc_client = server->client,
1813 .rpc_message = &msg,
1814 .callback_ops = &nfs4_open_confirm_ops,
1815 .callback_data = data,
1816 .workqueue = nfsiod_workqueue,
1817 .flags = RPC_TASK_ASYNC,
1821 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
1822 kref_get(&data->kref);
1824 data->rpc_status = 0;
1825 data->timestamp = jiffies;
1826 task = rpc_run_task(&task_setup_data);
1828 return PTR_ERR(task);
1829 status = nfs4_wait_for_completion_rpc_task(task);
1831 data->cancelled = 1;
1834 status = data->rpc_status;
1839 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1841 struct nfs4_opendata *data = calldata;
1842 struct nfs4_state_owner *sp = data->owner;
1843 struct nfs_client *clp = sp->so_server->nfs_client;
1845 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1848 * Check if we still need to send an OPEN call, or if we can use
1849 * a delegation instead.
1851 if (data->state != NULL) {
1852 struct nfs_delegation *delegation;
1854 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1857 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1858 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1859 data->o_arg.claim != NFS4_OPEN_CLAIM_DELEG_CUR_FH &&
1860 can_open_delegated(delegation, data->o_arg.fmode))
1861 goto unlock_no_action;
1864 /* Update client id. */
1865 data->o_arg.clientid = clp->cl_clientid;
1866 switch (data->o_arg.claim) {
1867 case NFS4_OPEN_CLAIM_PREVIOUS:
1868 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1869 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1870 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
1871 case NFS4_OPEN_CLAIM_FH:
1872 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1873 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1875 data->timestamp = jiffies;
1876 if (nfs4_setup_sequence(data->o_arg.server,
1877 &data->o_arg.seq_args,
1878 &data->o_res.seq_res,
1880 nfs_release_seqid(data->o_arg.seqid);
1882 /* Set the create mode (note dependency on the session type) */
1883 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1884 if (data->o_arg.open_flags & O_EXCL) {
1885 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1886 if (nfs4_has_persistent_session(clp))
1887 data->o_arg.createmode = NFS4_CREATE_GUARDED;
1888 else if (clp->cl_mvops->minor_version > 0)
1889 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1895 task->tk_action = NULL;
1897 nfs4_sequence_done(task, &data->o_res.seq_res);
1900 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1902 struct nfs4_opendata *data = calldata;
1904 data->rpc_status = task->tk_status;
1906 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1909 if (task->tk_status == 0) {
1910 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1911 switch (data->o_res.f_attr->mode & S_IFMT) {
1915 data->rpc_status = -ELOOP;
1918 data->rpc_status = -EISDIR;
1921 data->rpc_status = -ENOTDIR;
1924 renew_lease(data->o_res.server, data->timestamp);
1925 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1926 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1931 static void nfs4_open_release(void *calldata)
1933 struct nfs4_opendata *data = calldata;
1934 struct nfs4_state *state = NULL;
1936 /* If this request hasn't been cancelled, do nothing */
1937 if (data->cancelled == 0)
1939 /* In case of error, no cleanup! */
1940 if (data->rpc_status != 0 || !data->rpc_done)
1942 /* In case we need an open_confirm, no cleanup! */
1943 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1945 state = nfs4_opendata_to_nfs4_state(data);
1947 nfs4_close_state(state, data->o_arg.fmode);
1949 nfs4_opendata_put(data);
1952 static const struct rpc_call_ops nfs4_open_ops = {
1953 .rpc_call_prepare = nfs4_open_prepare,
1954 .rpc_call_done = nfs4_open_done,
1955 .rpc_release = nfs4_open_release,
1958 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
1960 struct inode *dir = d_inode(data->dir);
1961 struct nfs_server *server = NFS_SERVER(dir);
1962 struct nfs_openargs *o_arg = &data->o_arg;
1963 struct nfs_openres *o_res = &data->o_res;
1964 struct rpc_task *task;
1965 struct rpc_message msg = {
1966 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1969 .rpc_cred = data->owner->so_cred,
1971 struct rpc_task_setup task_setup_data = {
1972 .rpc_client = server->client,
1973 .rpc_message = &msg,
1974 .callback_ops = &nfs4_open_ops,
1975 .callback_data = data,
1976 .workqueue = nfsiod_workqueue,
1977 .flags = RPC_TASK_ASYNC,
1981 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
1982 kref_get(&data->kref);
1984 data->rpc_status = 0;
1985 data->cancelled = 0;
1986 data->is_recover = 0;
1988 nfs4_set_sequence_privileged(&o_arg->seq_args);
1989 data->is_recover = 1;
1991 task = rpc_run_task(&task_setup_data);
1993 return PTR_ERR(task);
1994 status = nfs4_wait_for_completion_rpc_task(task);
1996 data->cancelled = 1;
1999 status = data->rpc_status;
2005 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2007 struct inode *dir = d_inode(data->dir);
2008 struct nfs_openres *o_res = &data->o_res;
2011 status = nfs4_run_open_task(data, 1);
2012 if (status != 0 || !data->rpc_done)
2015 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2017 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2018 status = _nfs4_proc_open_confirm(data);
2027 * Additional permission checks in order to distinguish between an
2028 * open for read, and an open for execute. This works around the
2029 * fact that NFSv4 OPEN treats read and execute permissions as being
2031 * Note that in the non-execute case, we want to turn off permission
2032 * checking if we just created a new file (POSIX open() semantics).
2034 static int nfs4_opendata_access(struct rpc_cred *cred,
2035 struct nfs4_opendata *opendata,
2036 struct nfs4_state *state, fmode_t fmode,
2039 struct nfs_access_entry cache;
2042 /* access call failed or for some reason the server doesn't
2043 * support any access modes -- defer access call until later */
2044 if (opendata->o_res.access_supported == 0)
2049 * Use openflags to check for exec, because fmode won't
2050 * always have FMODE_EXEC set when file open for exec.
2052 if (openflags & __FMODE_EXEC) {
2053 /* ONLY check for exec rights */
2055 } else if ((fmode & FMODE_READ) && !opendata->file_created)
2059 cache.jiffies = jiffies;
2060 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2061 nfs_access_add_cache(state->inode, &cache);
2063 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
2066 /* even though OPEN succeeded, access is denied. Close the file */
2067 nfs4_close_state(state, fmode);
2072 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2074 static int _nfs4_proc_open(struct nfs4_opendata *data)
2076 struct inode *dir = d_inode(data->dir);
2077 struct nfs_server *server = NFS_SERVER(dir);
2078 struct nfs_openargs *o_arg = &data->o_arg;
2079 struct nfs_openres *o_res = &data->o_res;
2082 status = nfs4_run_open_task(data, 0);
2083 if (!data->rpc_done)
2086 if (status == -NFS4ERR_BADNAME &&
2087 !(o_arg->open_flags & O_CREAT))
2092 nfs_fattr_map_and_free_names(server, &data->f_attr);
2094 if (o_arg->open_flags & O_CREAT) {
2095 update_changeattr(dir, &o_res->cinfo);
2096 if (o_arg->open_flags & O_EXCL)
2097 data->file_created = 1;
2098 else if (o_res->cinfo.before != o_res->cinfo.after)
2099 data->file_created = 1;
2101 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2102 server->caps &= ~NFS_CAP_POSIX_LOCK;
2103 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2104 status = _nfs4_proc_open_confirm(data);
2108 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
2109 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
2113 static int nfs4_recover_expired_lease(struct nfs_server *server)
2115 return nfs4_client_recover_expired_lease(server->nfs_client);
2120 * reclaim state on the server after a network partition.
2121 * Assumes caller holds the appropriate lock
2123 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2125 struct nfs4_opendata *opendata;
2128 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2129 NFS4_OPEN_CLAIM_FH);
2130 if (IS_ERR(opendata))
2131 return PTR_ERR(opendata);
2132 ret = nfs4_open_recover(opendata, state);
2134 d_drop(ctx->dentry);
2135 nfs4_opendata_put(opendata);
2139 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2141 struct nfs_server *server = NFS_SERVER(state->inode);
2142 struct nfs4_exception exception = { };
2146 err = _nfs4_open_expired(ctx, state);
2147 trace_nfs4_open_expired(ctx, 0, err);
2148 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2153 case -NFS4ERR_GRACE:
2154 case -NFS4ERR_DELAY:
2155 nfs4_handle_exception(server, err, &exception);
2158 } while (exception.retry);
2163 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2165 struct nfs_open_context *ctx;
2168 ctx = nfs4_state_find_open_context(state);
2171 ret = nfs4_do_open_expired(ctx, state);
2172 put_nfs_open_context(ctx);
2176 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state)
2178 nfs_remove_bad_delegation(state->inode);
2179 write_seqlock(&state->seqlock);
2180 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2181 write_sequnlock(&state->seqlock);
2182 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2185 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2187 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2188 nfs_finish_clear_delegation_stateid(state);
2191 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2193 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2194 nfs40_clear_delegation_stateid(state);
2195 return nfs4_open_expired(sp, state);
2198 #if defined(CONFIG_NFS_V4_1)
2199 static void nfs41_check_delegation_stateid(struct nfs4_state *state)
2201 struct nfs_server *server = NFS_SERVER(state->inode);
2202 nfs4_stateid stateid;
2203 struct nfs_delegation *delegation;
2204 struct rpc_cred *cred;
2207 /* Get the delegation credential for use by test/free_stateid */
2209 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2210 if (delegation == NULL) {
2215 nfs4_stateid_copy(&stateid, &delegation->stateid);
2216 cred = get_rpccred(delegation->cred);
2218 status = nfs41_test_stateid(server, &stateid, cred);
2219 trace_nfs4_test_delegation_stateid(state, NULL, status);
2221 if (status != NFS_OK) {
2222 /* Free the stateid unless the server explicitly
2223 * informs us the stateid is unrecognized. */
2224 if (status != -NFS4ERR_BAD_STATEID)
2225 nfs41_free_stateid(server, &stateid, cred);
2226 nfs_finish_clear_delegation_stateid(state);
2233 * nfs41_check_open_stateid - possibly free an open stateid
2235 * @state: NFSv4 state for an inode
2237 * Returns NFS_OK if recovery for this stateid is now finished.
2238 * Otherwise a negative NFS4ERR value is returned.
2240 static int nfs41_check_open_stateid(struct nfs4_state *state)
2242 struct nfs_server *server = NFS_SERVER(state->inode);
2243 nfs4_stateid *stateid = &state->open_stateid;
2244 struct rpc_cred *cred = state->owner->so_cred;
2247 /* If a state reset has been done, test_stateid is unneeded */
2248 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2249 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2250 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2251 return -NFS4ERR_BAD_STATEID;
2253 status = nfs41_test_stateid(server, stateid, cred);
2254 trace_nfs4_test_open_stateid(state, NULL, status);
2255 if (status != NFS_OK) {
2256 /* Free the stateid unless the server explicitly
2257 * informs us the stateid is unrecognized. */
2258 if (status != -NFS4ERR_BAD_STATEID)
2259 nfs41_free_stateid(server, stateid, cred);
2261 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2262 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2263 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2264 clear_bit(NFS_OPEN_STATE, &state->flags);
2269 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2273 nfs41_check_delegation_stateid(state);
2274 status = nfs41_check_open_stateid(state);
2275 if (status != NFS_OK)
2276 status = nfs4_open_expired(sp, state);
2282 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2283 * fields corresponding to attributes that were used to store the verifier.
2284 * Make sure we clobber those fields in the later setattr call
2286 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
2288 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2289 !(sattr->ia_valid & ATTR_ATIME_SET))
2290 sattr->ia_valid |= ATTR_ATIME;
2292 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2293 !(sattr->ia_valid & ATTR_MTIME_SET))
2294 sattr->ia_valid |= ATTR_MTIME;
2297 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2300 struct nfs_open_context *ctx)
2302 struct nfs4_state_owner *sp = opendata->owner;
2303 struct nfs_server *server = sp->so_server;
2304 struct dentry *dentry;
2305 struct nfs4_state *state;
2309 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2311 ret = _nfs4_proc_open(opendata);
2315 state = nfs4_opendata_to_nfs4_state(opendata);
2316 ret = PTR_ERR(state);
2319 if (server->caps & NFS_CAP_POSIX_LOCK)
2320 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2322 dentry = opendata->dentry;
2323 if (d_really_is_negative(dentry)) {
2324 /* FIXME: Is this d_drop() ever needed? */
2326 dentry = d_add_unique(dentry, igrab(state->inode));
2327 if (dentry == NULL) {
2328 dentry = opendata->dentry;
2329 } else if (dentry != ctx->dentry) {
2331 ctx->dentry = dget(dentry);
2333 nfs_set_verifier(dentry,
2334 nfs_save_change_attribute(d_inode(opendata->dir)));
2337 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2342 if (d_inode(dentry) == state->inode) {
2343 nfs_inode_attach_open_context(ctx);
2344 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2345 nfs4_schedule_stateid_recovery(server, state);
2352 * Returns a referenced nfs4_state
2354 static int _nfs4_do_open(struct inode *dir,
2355 struct nfs_open_context *ctx,
2357 struct iattr *sattr,
2358 struct nfs4_label *label,
2361 struct nfs4_state_owner *sp;
2362 struct nfs4_state *state = NULL;
2363 struct nfs_server *server = NFS_SERVER(dir);
2364 struct nfs4_opendata *opendata;
2365 struct dentry *dentry = ctx->dentry;
2366 struct rpc_cred *cred = ctx->cred;
2367 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2368 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
2369 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
2370 struct nfs4_label *olabel = NULL;
2373 /* Protect against reboot recovery conflicts */
2375 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2377 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2380 status = nfs4_recover_expired_lease(server);
2382 goto err_put_state_owner;
2383 if (d_really_is_positive(dentry))
2384 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
2386 if (d_really_is_positive(dentry))
2387 claim = NFS4_OPEN_CLAIM_FH;
2388 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
2389 label, claim, GFP_KERNEL);
2390 if (opendata == NULL)
2391 goto err_put_state_owner;
2394 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2395 if (IS_ERR(olabel)) {
2396 status = PTR_ERR(olabel);
2397 goto err_opendata_put;
2401 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2402 if (!opendata->f_attr.mdsthreshold) {
2403 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2404 if (!opendata->f_attr.mdsthreshold)
2405 goto err_free_label;
2407 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
2409 if (d_really_is_positive(dentry))
2410 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
2412 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
2414 goto err_free_label;
2417 if ((opendata->o_arg.open_flags & O_EXCL) &&
2418 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
2419 nfs4_exclusive_attrset(opendata, sattr);
2421 nfs_fattr_init(opendata->o_res.f_attr);
2422 status = nfs4_do_setattr(state->inode, cred,
2423 opendata->o_res.f_attr, sattr,
2424 state, label, olabel);
2426 nfs_setattr_update_inode(state->inode, sattr,
2427 opendata->o_res.f_attr);
2428 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2431 if (opendata->file_created)
2432 *opened |= FILE_CREATED;
2434 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
2435 *ctx_th = opendata->f_attr.mdsthreshold;
2436 opendata->f_attr.mdsthreshold = NULL;
2439 nfs4_label_free(olabel);
2441 nfs4_opendata_put(opendata);
2442 nfs4_put_state_owner(sp);
2445 nfs4_label_free(olabel);
2447 nfs4_opendata_put(opendata);
2448 err_put_state_owner:
2449 nfs4_put_state_owner(sp);
2455 static struct nfs4_state *nfs4_do_open(struct inode *dir,
2456 struct nfs_open_context *ctx,
2458 struct iattr *sattr,
2459 struct nfs4_label *label,
2462 struct nfs_server *server = NFS_SERVER(dir);
2463 struct nfs4_exception exception = { };
2464 struct nfs4_state *res;
2468 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
2470 trace_nfs4_open_file(ctx, flags, status);
2473 /* NOTE: BAD_SEQID means the server and client disagree about the
2474 * book-keeping w.r.t. state-changing operations
2475 * (OPEN/CLOSE/LOCK/LOCKU...)
2476 * It is actually a sign of a bug on the client or on the server.
2478 * If we receive a BAD_SEQID error in the particular case of
2479 * doing an OPEN, we assume that nfs_increment_open_seqid() will
2480 * have unhashed the old state_owner for us, and that we can
2481 * therefore safely retry using a new one. We should still warn
2482 * the user though...
2484 if (status == -NFS4ERR_BAD_SEQID) {
2485 pr_warn_ratelimited("NFS: v4 server %s "
2486 " returned a bad sequence-id error!\n",
2487 NFS_SERVER(dir)->nfs_client->cl_hostname);
2488 exception.retry = 1;
2492 * BAD_STATEID on OPEN means that the server cancelled our
2493 * state before it received the OPEN_CONFIRM.
2494 * Recover by retrying the request as per the discussion
2495 * on Page 181 of RFC3530.
2497 if (status == -NFS4ERR_BAD_STATEID) {
2498 exception.retry = 1;
2501 if (status == -EAGAIN) {
2502 /* We must have found a delegation */
2503 exception.retry = 1;
2506 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2508 res = ERR_PTR(nfs4_handle_exception(server,
2509 status, &exception));
2510 } while (exception.retry);
2514 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2515 struct nfs_fattr *fattr, struct iattr *sattr,
2516 struct nfs4_state *state, struct nfs4_label *ilabel,
2517 struct nfs4_label *olabel)
2519 struct nfs_server *server = NFS_SERVER(inode);
2520 struct nfs_setattrargs arg = {
2521 .fh = NFS_FH(inode),
2524 .bitmask = server->attr_bitmask,
2527 struct nfs_setattrres res = {
2532 struct rpc_message msg = {
2533 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2538 unsigned long timestamp = jiffies;
2543 arg.bitmask = nfs4_bitmask(server, ilabel);
2545 arg.bitmask = nfs4_bitmask(server, olabel);
2547 nfs_fattr_init(fattr);
2549 /* Servers should only apply open mode checks for file size changes */
2550 truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false;
2551 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2553 if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) {
2554 /* Use that stateid */
2555 } else if (truncate && state != NULL) {
2556 struct nfs_lockowner lockowner = {
2557 .l_owner = current->files,
2558 .l_pid = current->tgid,
2560 if (!nfs4_valid_open_stateid(state))
2562 if (nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
2563 &lockowner) == -EIO)
2566 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
2568 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2569 if (status == 0 && state != NULL)
2570 renew_lease(server, timestamp);
2574 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2575 struct nfs_fattr *fattr, struct iattr *sattr,
2576 struct nfs4_state *state, struct nfs4_label *ilabel,
2577 struct nfs4_label *olabel)
2579 struct nfs_server *server = NFS_SERVER(inode);
2580 struct nfs4_exception exception = {
2586 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state, ilabel, olabel);
2587 trace_nfs4_setattr(inode, err);
2589 case -NFS4ERR_OPENMODE:
2590 if (!(sattr->ia_valid & ATTR_SIZE)) {
2591 pr_warn_once("NFSv4: server %s is incorrectly "
2592 "applying open mode checks to "
2593 "a SETATTR that is not "
2594 "changing file size.\n",
2595 server->nfs_client->cl_hostname);
2597 if (state && !(state->state & FMODE_WRITE)) {
2599 if (sattr->ia_valid & ATTR_OPEN)
2604 err = nfs4_handle_exception(server, err, &exception);
2605 } while (exception.retry);
2610 struct nfs4_closedata {
2611 struct inode *inode;
2612 struct nfs4_state *state;
2613 struct nfs_closeargs arg;
2614 struct nfs_closeres res;
2615 struct nfs_fattr fattr;
2616 unsigned long timestamp;
2621 static void nfs4_free_closedata(void *data)
2623 struct nfs4_closedata *calldata = data;
2624 struct nfs4_state_owner *sp = calldata->state->owner;
2625 struct super_block *sb = calldata->state->inode->i_sb;
2628 pnfs_roc_release(calldata->state->inode);
2629 nfs4_put_open_state(calldata->state);
2630 nfs_free_seqid(calldata->arg.seqid);
2631 nfs4_put_state_owner(sp);
2632 nfs_sb_deactive(sb);
2636 static void nfs4_close_done(struct rpc_task *task, void *data)
2638 struct nfs4_closedata *calldata = data;
2639 struct nfs4_state *state = calldata->state;
2640 struct nfs_server *server = NFS_SERVER(calldata->inode);
2641 nfs4_stateid *res_stateid = NULL;
2643 dprintk("%s: begin!\n", __func__);
2644 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2646 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
2647 /* hmm. we are done with the inode, and in the process of freeing
2648 * the state_owner. we keep this around to process errors
2650 switch (task->tk_status) {
2652 res_stateid = &calldata->res.stateid;
2653 if (calldata->arg.fmode == 0 && calldata->roc)
2654 pnfs_roc_set_barrier(state->inode,
2655 calldata->roc_barrier);
2656 renew_lease(server, calldata->timestamp);
2658 case -NFS4ERR_ADMIN_REVOKED:
2659 case -NFS4ERR_STALE_STATEID:
2660 case -NFS4ERR_OLD_STATEID:
2661 case -NFS4ERR_BAD_STATEID:
2662 case -NFS4ERR_EXPIRED:
2663 if (!nfs4_stateid_match(&calldata->arg.stateid,
2664 &state->open_stateid)) {
2665 rpc_restart_call_prepare(task);
2668 if (calldata->arg.fmode == 0)
2671 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
2672 rpc_restart_call_prepare(task);
2676 nfs_clear_open_stateid(state, res_stateid, calldata->arg.fmode);
2678 nfs_release_seqid(calldata->arg.seqid);
2679 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
2680 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
2683 static void nfs4_close_prepare(struct rpc_task *task, void *data)
2685 struct nfs4_closedata *calldata = data;
2686 struct nfs4_state *state = calldata->state;
2687 struct inode *inode = calldata->inode;
2688 bool is_rdonly, is_wronly, is_rdwr;
2691 dprintk("%s: begin!\n", __func__);
2692 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
2695 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2696 spin_lock(&state->owner->so_lock);
2697 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
2698 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
2699 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
2700 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
2701 /* Calculate the change in open mode */
2702 calldata->arg.fmode = 0;
2703 if (state->n_rdwr == 0) {
2704 if (state->n_rdonly == 0)
2705 call_close |= is_rdonly;
2707 calldata->arg.fmode |= FMODE_READ;
2708 if (state->n_wronly == 0)
2709 call_close |= is_wronly;
2711 calldata->arg.fmode |= FMODE_WRITE;
2713 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
2715 if (calldata->arg.fmode == 0)
2716 call_close |= is_rdwr;
2718 if (!nfs4_valid_open_stateid(state))
2720 spin_unlock(&state->owner->so_lock);
2723 /* Note: exit _without_ calling nfs4_close_done */
2727 if (calldata->arg.fmode == 0) {
2728 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
2729 if (calldata->roc &&
2730 pnfs_roc_drain(inode, &calldata->roc_barrier, task)) {
2731 nfs_release_seqid(calldata->arg.seqid);
2735 calldata->arg.share_access =
2736 nfs4_map_atomic_open_share(NFS_SERVER(inode),
2737 calldata->arg.fmode, 0);
2739 nfs_fattr_init(calldata->res.fattr);
2740 calldata->timestamp = jiffies;
2741 if (nfs4_setup_sequence(NFS_SERVER(inode),
2742 &calldata->arg.seq_args,
2743 &calldata->res.seq_res,
2745 nfs_release_seqid(calldata->arg.seqid);
2746 dprintk("%s: done!\n", __func__);
2749 task->tk_action = NULL;
2751 nfs4_sequence_done(task, &calldata->res.seq_res);
2754 static const struct rpc_call_ops nfs4_close_ops = {
2755 .rpc_call_prepare = nfs4_close_prepare,
2756 .rpc_call_done = nfs4_close_done,
2757 .rpc_release = nfs4_free_closedata,
2760 static bool nfs4_roc(struct inode *inode)
2762 if (!nfs_have_layout(inode))
2764 return pnfs_roc(inode);
2768 * It is possible for data to be read/written from a mem-mapped file
2769 * after the sys_close call (which hits the vfs layer as a flush).
2770 * This means that we can't safely call nfsv4 close on a file until
2771 * the inode is cleared. This in turn means that we are not good
2772 * NFSv4 citizens - we do not indicate to the server to update the file's
2773 * share state even when we are done with one of the three share
2774 * stateid's in the inode.
2776 * NOTE: Caller must be holding the sp->so_owner semaphore!
2778 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
2780 struct nfs_server *server = NFS_SERVER(state->inode);
2781 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
2782 struct nfs4_closedata *calldata;
2783 struct nfs4_state_owner *sp = state->owner;
2784 struct rpc_task *task;
2785 struct rpc_message msg = {
2786 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2787 .rpc_cred = state->owner->so_cred,
2789 struct rpc_task_setup task_setup_data = {
2790 .rpc_client = server->client,
2791 .rpc_message = &msg,
2792 .callback_ops = &nfs4_close_ops,
2793 .workqueue = nfsiod_workqueue,
2794 .flags = RPC_TASK_ASYNC,
2796 int status = -ENOMEM;
2798 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
2799 &task_setup_data.rpc_client, &msg);
2801 calldata = kzalloc(sizeof(*calldata), gfp_mask);
2802 if (calldata == NULL)
2804 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
2805 calldata->inode = state->inode;
2806 calldata->state = state;
2807 calldata->arg.fh = NFS_FH(state->inode);
2808 /* Serialization for the sequence id */
2809 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
2810 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
2811 if (IS_ERR(calldata->arg.seqid))
2812 goto out_free_calldata;
2813 calldata->arg.fmode = 0;
2814 calldata->arg.bitmask = server->cache_consistency_bitmask;
2815 calldata->res.fattr = &calldata->fattr;
2816 calldata->res.seqid = calldata->arg.seqid;
2817 calldata->res.server = server;
2818 calldata->roc = nfs4_roc(state->inode);
2819 nfs_sb_active(calldata->inode->i_sb);
2821 msg.rpc_argp = &calldata->arg;
2822 msg.rpc_resp = &calldata->res;
2823 task_setup_data.callback_data = calldata;
2824 task = rpc_run_task(&task_setup_data);
2826 return PTR_ERR(task);
2829 status = rpc_wait_for_completion_task(task);
2835 nfs4_put_open_state(state);
2836 nfs4_put_state_owner(sp);
2840 static struct inode *
2841 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
2842 int open_flags, struct iattr *attr, int *opened)
2844 struct nfs4_state *state;
2845 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
2847 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
2849 /* Protect against concurrent sillydeletes */
2850 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
2852 nfs4_label_release_security(label);
2855 return ERR_CAST(state);
2856 return state->inode;
2859 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2861 if (ctx->state == NULL)
2864 nfs4_close_sync(ctx->state, ctx->mode);
2866 nfs4_close_state(ctx->state, ctx->mode);
2869 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
2870 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
2871 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
2873 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2875 struct nfs4_server_caps_arg args = {
2878 struct nfs4_server_caps_res res = {};
2879 struct rpc_message msg = {
2880 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2886 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2888 /* Sanity check the server answers */
2889 switch (server->nfs_client->cl_minorversion) {
2891 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
2892 res.attr_bitmask[2] = 0;
2895 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
2898 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
2900 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2901 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2902 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2903 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2904 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2905 NFS_CAP_CTIME|NFS_CAP_MTIME|
2906 NFS_CAP_SECURITY_LABEL);
2907 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
2908 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
2909 server->caps |= NFS_CAP_ACLS;
2910 if (res.has_links != 0)
2911 server->caps |= NFS_CAP_HARDLINKS;
2912 if (res.has_symlinks != 0)
2913 server->caps |= NFS_CAP_SYMLINKS;
2914 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2915 server->caps |= NFS_CAP_FILEID;
2916 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2917 server->caps |= NFS_CAP_MODE;
2918 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2919 server->caps |= NFS_CAP_NLINK;
2920 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2921 server->caps |= NFS_CAP_OWNER;
2922 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2923 server->caps |= NFS_CAP_OWNER_GROUP;
2924 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2925 server->caps |= NFS_CAP_ATIME;
2926 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2927 server->caps |= NFS_CAP_CTIME;
2928 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2929 server->caps |= NFS_CAP_MTIME;
2930 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
2931 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
2932 server->caps |= NFS_CAP_SECURITY_LABEL;
2934 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
2935 sizeof(server->attr_bitmask));
2936 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2938 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2939 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2940 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2941 server->cache_consistency_bitmask[2] = 0;
2942 server->acl_bitmask = res.acl_bitmask;
2943 server->fh_expire_type = res.fh_expire_type;
2949 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2951 struct nfs4_exception exception = { };
2954 err = nfs4_handle_exception(server,
2955 _nfs4_server_capabilities(server, fhandle),
2957 } while (exception.retry);
2961 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2962 struct nfs_fsinfo *info)
2965 struct nfs4_lookup_root_arg args = {
2968 struct nfs4_lookup_res res = {
2970 .fattr = info->fattr,
2973 struct rpc_message msg = {
2974 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2979 bitmask[0] = nfs4_fattr_bitmap[0];
2980 bitmask[1] = nfs4_fattr_bitmap[1];
2982 * Process the label in the upcoming getfattr
2984 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
2986 nfs_fattr_init(info->fattr);
2987 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2990 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2991 struct nfs_fsinfo *info)
2993 struct nfs4_exception exception = { };
2996 err = _nfs4_lookup_root(server, fhandle, info);
2997 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
3000 case -NFS4ERR_WRONGSEC:
3003 err = nfs4_handle_exception(server, err, &exception);
3005 } while (exception.retry);
3010 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3011 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3013 struct rpc_auth_create_args auth_args = {
3014 .pseudoflavor = flavor,
3016 struct rpc_auth *auth;
3019 auth = rpcauth_create(&auth_args, server->client);
3024 ret = nfs4_lookup_root(server, fhandle, info);
3030 * Retry pseudoroot lookup with various security flavors. We do this when:
3032 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3033 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3035 * Returns zero on success, or a negative NFS4ERR value, or a
3036 * negative errno value.
3038 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3039 struct nfs_fsinfo *info)
3041 /* Per 3530bis 15.33.5 */
3042 static const rpc_authflavor_t flav_array[] = {
3046 RPC_AUTH_UNIX, /* courtesy */
3049 int status = -EPERM;
3052 if (server->auth_info.flavor_len > 0) {
3053 /* try each flavor specified by user */
3054 for (i = 0; i < server->auth_info.flavor_len; i++) {
3055 status = nfs4_lookup_root_sec(server, fhandle, info,
3056 server->auth_info.flavors[i]);
3057 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3062 /* no flavors specified by user, try default list */
3063 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3064 status = nfs4_lookup_root_sec(server, fhandle, info,
3066 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3073 * -EACCESS could mean that the user doesn't have correct permissions
3074 * to access the mount. It could also mean that we tried to mount
3075 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3076 * existing mount programs don't handle -EACCES very well so it should
3077 * be mapped to -EPERM instead.
3079 if (status == -EACCES)
3084 static int nfs4_do_find_root_sec(struct nfs_server *server,
3085 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
3087 int mv = server->nfs_client->cl_minorversion;
3088 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
3092 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3093 * @server: initialized nfs_server handle
3094 * @fhandle: we fill in the pseudo-fs root file handle
3095 * @info: we fill in an FSINFO struct
3096 * @auth_probe: probe the auth flavours
3098 * Returns zero on success, or a negative errno.
3100 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
3101 struct nfs_fsinfo *info,
3107 status = nfs4_lookup_root(server, fhandle, info);
3109 if (auth_probe || status == NFS4ERR_WRONGSEC)
3110 status = nfs4_do_find_root_sec(server, fhandle, info);
3113 status = nfs4_server_capabilities(server, fhandle);
3115 status = nfs4_do_fsinfo(server, fhandle, info);
3117 return nfs4_map_errors(status);
3120 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3121 struct nfs_fsinfo *info)
3124 struct nfs_fattr *fattr = info->fattr;
3125 struct nfs4_label *label = NULL;
3127 error = nfs4_server_capabilities(server, mntfh);
3129 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3133 label = nfs4_label_alloc(server, GFP_KERNEL);
3135 return PTR_ERR(label);
3137 error = nfs4_proc_getattr(server, mntfh, fattr, label);
3139 dprintk("nfs4_get_root: getattr error = %d\n", -error);
3140 goto err_free_label;
3143 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3144 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3145 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3148 nfs4_label_free(label);
3154 * Get locations and (maybe) other attributes of a referral.
3155 * Note that we'll actually follow the referral later when
3156 * we detect fsid mismatch in inode revalidation
3158 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3159 const struct qstr *name, struct nfs_fattr *fattr,
3160 struct nfs_fh *fhandle)
3162 int status = -ENOMEM;
3163 struct page *page = NULL;
3164 struct nfs4_fs_locations *locations = NULL;
3166 page = alloc_page(GFP_KERNEL);
3169 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3170 if (locations == NULL)
3173 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
3178 * If the fsid didn't change, this is a migration event, not a
3179 * referral. Cause us to drop into the exception handler, which
3180 * will kick off migration recovery.
3182 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
3183 dprintk("%s: server did not return a different fsid for"
3184 " a referral at %s\n", __func__, name->name);
3185 status = -NFS4ERR_MOVED;
3188 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3189 nfs_fixup_referral_attributes(&locations->fattr);
3191 /* replace the lookup nfs_fattr with the locations nfs_fattr */
3192 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
3193 memset(fhandle, 0, sizeof(struct nfs_fh));
3201 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3202 struct nfs_fattr *fattr, struct nfs4_label *label)
3204 struct nfs4_getattr_arg args = {
3206 .bitmask = server->attr_bitmask,
3208 struct nfs4_getattr_res res = {
3213 struct rpc_message msg = {
3214 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3219 args.bitmask = nfs4_bitmask(server, label);
3221 nfs_fattr_init(fattr);
3222 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3225 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3226 struct nfs_fattr *fattr, struct nfs4_label *label)
3228 struct nfs4_exception exception = { };
3231 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3232 trace_nfs4_getattr(server, fhandle, fattr, err);
3233 err = nfs4_handle_exception(server, err,
3235 } while (exception.retry);
3240 * The file is not closed if it is opened due to the a request to change
3241 * the size of the file. The open call will not be needed once the
3242 * VFS layer lookup-intents are implemented.
3244 * Close is called when the inode is destroyed.
3245 * If we haven't opened the file for O_WRONLY, we
3246 * need to in the size_change case to obtain a stateid.
3249 * Because OPEN is always done by name in nfsv4, it is
3250 * possible that we opened a different file by the same
3251 * name. We can recognize this race condition, but we
3252 * can't do anything about it besides returning an error.
3254 * This will be fixed with VFS changes (lookup-intent).
3257 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3258 struct iattr *sattr)
3260 struct inode *inode = d_inode(dentry);
3261 struct rpc_cred *cred = NULL;
3262 struct nfs4_state *state = NULL;
3263 struct nfs4_label *label = NULL;
3266 if (pnfs_ld_layoutret_on_setattr(inode) &&
3267 sattr->ia_valid & ATTR_SIZE &&
3268 sattr->ia_size < i_size_read(inode))
3269 pnfs_commit_and_return_layout(inode);
3271 nfs_fattr_init(fattr);
3273 /* Deal with open(O_TRUNC) */
3274 if (sattr->ia_valid & ATTR_OPEN)
3275 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
3277 /* Optimization: if the end result is no change, don't RPC */
3278 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
3281 /* Search for an existing open(O_WRITE) file */
3282 if (sattr->ia_valid & ATTR_FILE) {
3283 struct nfs_open_context *ctx;
3285 ctx = nfs_file_open_context(sattr->ia_file);
3292 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3294 return PTR_ERR(label);
3296 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
3298 nfs_setattr_update_inode(inode, sattr, fattr);
3299 nfs_setsecurity(inode, fattr, label);
3301 nfs4_label_free(label);
3305 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3306 const struct qstr *name, struct nfs_fh *fhandle,
3307 struct nfs_fattr *fattr, struct nfs4_label *label)
3309 struct nfs_server *server = NFS_SERVER(dir);
3311 struct nfs4_lookup_arg args = {
3312 .bitmask = server->attr_bitmask,
3313 .dir_fh = NFS_FH(dir),
3316 struct nfs4_lookup_res res = {
3322 struct rpc_message msg = {
3323 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3328 args.bitmask = nfs4_bitmask(server, label);
3330 nfs_fattr_init(fattr);
3332 dprintk("NFS call lookup %s\n", name->name);
3333 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
3334 dprintk("NFS reply lookup: %d\n", status);
3338 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
3340 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
3341 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
3342 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3346 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3347 struct qstr *name, struct nfs_fh *fhandle,
3348 struct nfs_fattr *fattr, struct nfs4_label *label)
3350 struct nfs4_exception exception = { };
3351 struct rpc_clnt *client = *clnt;
3354 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
3355 trace_nfs4_lookup(dir, name, err);
3357 case -NFS4ERR_BADNAME:
3360 case -NFS4ERR_MOVED:
3361 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
3362 if (err == -NFS4ERR_MOVED)
3363 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3365 case -NFS4ERR_WRONGSEC:
3367 if (client != *clnt)
3369 client = nfs4_negotiate_security(client, dir, name);
3371 return PTR_ERR(client);
3373 exception.retry = 1;
3376 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3378 } while (exception.retry);
3383 else if (client != *clnt)
3384 rpc_shutdown_client(client);
3389 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
3390 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3391 struct nfs4_label *label)
3394 struct rpc_clnt *client = NFS_CLIENT(dir);
3396 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
3397 if (client != NFS_CLIENT(dir)) {
3398 rpc_shutdown_client(client);
3399 nfs_fixup_secinfo_attributes(fattr);
3405 nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
3406 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3408 struct rpc_clnt *client = NFS_CLIENT(dir);
3411 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
3413 return ERR_PTR(status);
3414 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
3417 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3419 struct nfs_server *server = NFS_SERVER(inode);
3420 struct nfs4_accessargs args = {
3421 .fh = NFS_FH(inode),
3422 .bitmask = server->cache_consistency_bitmask,
3424 struct nfs4_accessres res = {
3427 struct rpc_message msg = {
3428 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3431 .rpc_cred = entry->cred,
3433 int mode = entry->mask;
3437 * Determine which access bits we want to ask for...
3439 if (mode & MAY_READ)
3440 args.access |= NFS4_ACCESS_READ;
3441 if (S_ISDIR(inode->i_mode)) {
3442 if (mode & MAY_WRITE)
3443 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3444 if (mode & MAY_EXEC)
3445 args.access |= NFS4_ACCESS_LOOKUP;
3447 if (mode & MAY_WRITE)
3448 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3449 if (mode & MAY_EXEC)
3450 args.access |= NFS4_ACCESS_EXECUTE;
3453 res.fattr = nfs_alloc_fattr();
3454 if (res.fattr == NULL)
3457 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3459 nfs_access_set_mask(entry, res.access);
3460 nfs_refresh_inode(inode, res.fattr);
3462 nfs_free_fattr(res.fattr);
3466 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3468 struct nfs4_exception exception = { };
3471 err = _nfs4_proc_access(inode, entry);
3472 trace_nfs4_access(inode, err);
3473 err = nfs4_handle_exception(NFS_SERVER(inode), err,
3475 } while (exception.retry);
3480 * TODO: For the time being, we don't try to get any attributes
3481 * along with any of the zero-copy operations READ, READDIR,
3484 * In the case of the first three, we want to put the GETATTR
3485 * after the read-type operation -- this is because it is hard
3486 * to predict the length of a GETATTR response in v4, and thus
3487 * align the READ data correctly. This means that the GETATTR
3488 * may end up partially falling into the page cache, and we should
3489 * shift it into the 'tail' of the xdr_buf before processing.
3490 * To do this efficiently, we need to know the total length
3491 * of data received, which doesn't seem to be available outside
3494 * In the case of WRITE, we also want to put the GETATTR after
3495 * the operation -- in this case because we want to make sure
3496 * we get the post-operation mtime and size.
3498 * Both of these changes to the XDR layer would in fact be quite
3499 * minor, but I decided to leave them for a subsequent patch.
3501 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3502 unsigned int pgbase, unsigned int pglen)
3504 struct nfs4_readlink args = {
3505 .fh = NFS_FH(inode),
3510 struct nfs4_readlink_res res;
3511 struct rpc_message msg = {
3512 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3517 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
3520 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3521 unsigned int pgbase, unsigned int pglen)
3523 struct nfs4_exception exception = { };
3526 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3527 trace_nfs4_readlink(inode, err);
3528 err = nfs4_handle_exception(NFS_SERVER(inode), err,
3530 } while (exception.retry);
3535 * This is just for mknod. open(O_CREAT) will always do ->open_context().
3538 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
3541 struct nfs4_label l, *ilabel = NULL;
3542 struct nfs_open_context *ctx;
3543 struct nfs4_state *state;
3547 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3549 return PTR_ERR(ctx);
3551 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3553 sattr->ia_mode &= ~current_umask();
3554 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, &opened);
3555 if (IS_ERR(state)) {
3556 status = PTR_ERR(state);
3560 nfs4_label_release_security(ilabel);
3561 put_nfs_open_context(ctx);
3565 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3567 struct nfs_server *server = NFS_SERVER(dir);
3568 struct nfs_removeargs args = {
3572 struct nfs_removeres res = {
3575 struct rpc_message msg = {
3576 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3582 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
3584 update_changeattr(dir, &res.cinfo);
3588 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3590 struct nfs4_exception exception = { };
3593 err = _nfs4_proc_remove(dir, name);
3594 trace_nfs4_remove(dir, name, err);
3595 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3597 } while (exception.retry);
3601 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
3603 struct nfs_server *server = NFS_SERVER(dir);
3604 struct nfs_removeargs *args = msg->rpc_argp;
3605 struct nfs_removeres *res = msg->rpc_resp;
3607 res->server = server;
3608 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
3609 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
3611 nfs_fattr_init(res->dir_attr);
3614 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3616 nfs4_setup_sequence(NFS_SERVER(data->dir),
3617 &data->args.seq_args,
3622 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
3624 struct nfs_unlinkdata *data = task->tk_calldata;
3625 struct nfs_removeres *res = &data->res;
3627 if (!nfs4_sequence_done(task, &res->seq_res))
3629 if (nfs4_async_handle_error(task, res->server, NULL,
3630 &data->timeout) == -EAGAIN)
3632 update_changeattr(dir, &res->cinfo);
3636 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3638 struct nfs_server *server = NFS_SERVER(dir);
3639 struct nfs_renameargs *arg = msg->rpc_argp;
3640 struct nfs_renameres *res = msg->rpc_resp;
3642 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
3643 res->server = server;
3644 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
3647 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3649 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3650 &data->args.seq_args,
3655 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3656 struct inode *new_dir)
3658 struct nfs_renamedata *data = task->tk_calldata;
3659 struct nfs_renameres *res = &data->res;
3661 if (!nfs4_sequence_done(task, &res->seq_res))
3663 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
3666 update_changeattr(old_dir, &res->old_cinfo);
3667 update_changeattr(new_dir, &res->new_cinfo);
3671 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3673 struct nfs_server *server = NFS_SERVER(inode);
3674 struct nfs4_link_arg arg = {
3675 .fh = NFS_FH(inode),
3676 .dir_fh = NFS_FH(dir),
3678 .bitmask = server->attr_bitmask,
3680 struct nfs4_link_res res = {
3684 struct rpc_message msg = {
3685 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3689 int status = -ENOMEM;
3691 res.fattr = nfs_alloc_fattr();
3692 if (res.fattr == NULL)
3695 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3696 if (IS_ERR(res.label)) {
3697 status = PTR_ERR(res.label);
3700 arg.bitmask = nfs4_bitmask(server, res.label);
3702 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3704 update_changeattr(dir, &res.cinfo);
3705 status = nfs_post_op_update_inode(inode, res.fattr);
3707 nfs_setsecurity(inode, res.fattr, res.label);
3711 nfs4_label_free(res.label);
3714 nfs_free_fattr(res.fattr);
3718 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3720 struct nfs4_exception exception = { };
3723 err = nfs4_handle_exception(NFS_SERVER(inode),
3724 _nfs4_proc_link(inode, dir, name),
3726 } while (exception.retry);
3730 struct nfs4_createdata {
3731 struct rpc_message msg;
3732 struct nfs4_create_arg arg;
3733 struct nfs4_create_res res;
3735 struct nfs_fattr fattr;
3736 struct nfs4_label *label;
3739 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3740 struct qstr *name, struct iattr *sattr, u32 ftype)
3742 struct nfs4_createdata *data;
3744 data = kzalloc(sizeof(*data), GFP_KERNEL);
3746 struct nfs_server *server = NFS_SERVER(dir);
3748 data->label = nfs4_label_alloc(server, GFP_KERNEL);
3749 if (IS_ERR(data->label))
3752 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3753 data->msg.rpc_argp = &data->arg;
3754 data->msg.rpc_resp = &data->res;
3755 data->arg.dir_fh = NFS_FH(dir);
3756 data->arg.server = server;
3757 data->arg.name = name;
3758 data->arg.attrs = sattr;
3759 data->arg.ftype = ftype;
3760 data->arg.bitmask = nfs4_bitmask(server, data->label);
3761 data->res.server = server;
3762 data->res.fh = &data->fh;
3763 data->res.fattr = &data->fattr;
3764 data->res.label = data->label;
3765 nfs_fattr_init(data->res.fattr);
3773 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3775 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
3776 &data->arg.seq_args, &data->res.seq_res, 1);
3778 update_changeattr(dir, &data->res.dir_cinfo);
3779 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
3784 static void nfs4_free_createdata(struct nfs4_createdata *data)
3786 nfs4_label_free(data->label);
3790 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3791 struct page *page, unsigned int len, struct iattr *sattr,
3792 struct nfs4_label *label)
3794 struct nfs4_createdata *data;
3795 int status = -ENAMETOOLONG;
3797 if (len > NFS4_MAXPATHLEN)
3801 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3805 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3806 data->arg.u.symlink.pages = &page;
3807 data->arg.u.symlink.len = len;
3808 data->arg.label = label;
3810 status = nfs4_do_create(dir, dentry, data);
3812 nfs4_free_createdata(data);
3817 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3818 struct page *page, unsigned int len, struct iattr *sattr)
3820 struct nfs4_exception exception = { };
3821 struct nfs4_label l, *label = NULL;
3824 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3827 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
3828 trace_nfs4_symlink(dir, &dentry->d_name, err);
3829 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3831 } while (exception.retry);
3833 nfs4_label_release_security(label);
3837 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3838 struct iattr *sattr, struct nfs4_label *label)
3840 struct nfs4_createdata *data;
3841 int status = -ENOMEM;
3843 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3847 data->arg.label = label;
3848 status = nfs4_do_create(dir, dentry, data);
3850 nfs4_free_createdata(data);
3855 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3856 struct iattr *sattr)
3858 struct nfs4_exception exception = { };
3859 struct nfs4_label l, *label = NULL;
3862 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3864 sattr->ia_mode &= ~current_umask();
3866 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
3867 trace_nfs4_mkdir(dir, &dentry->d_name, err);
3868 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3870 } while (exception.retry);
3871 nfs4_label_release_security(label);
3876 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3877 u64 cookie, struct page **pages, unsigned int count, int plus)
3879 struct inode *dir = d_inode(dentry);
3880 struct nfs4_readdir_arg args = {
3885 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
3888 struct nfs4_readdir_res res;
3889 struct rpc_message msg = {
3890 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3897 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
3899 (unsigned long long)cookie);
3900 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
3901 res.pgbase = args.pgbase;
3902 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
3904 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
3905 status += args.pgbase;
3908 nfs_invalidate_atime(dir);
3910 dprintk("%s: returns %d\n", __func__, status);
3914 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3915 u64 cookie, struct page **pages, unsigned int count, int plus)
3917 struct nfs4_exception exception = { };
3920 err = _nfs4_proc_readdir(dentry, cred, cookie,
3921 pages, count, plus);
3922 trace_nfs4_readdir(d_inode(dentry), err);
3923 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
3925 } while (exception.retry);
3929 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3930 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
3932 struct nfs4_createdata *data;
3933 int mode = sattr->ia_mode;
3934 int status = -ENOMEM;
3936 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3941 data->arg.ftype = NF4FIFO;
3942 else if (S_ISBLK(mode)) {
3943 data->arg.ftype = NF4BLK;
3944 data->arg.u.device.specdata1 = MAJOR(rdev);
3945 data->arg.u.device.specdata2 = MINOR(rdev);
3947 else if (S_ISCHR(mode)) {
3948 data->arg.ftype = NF4CHR;
3949 data->arg.u.device.specdata1 = MAJOR(rdev);
3950 data->arg.u.device.specdata2 = MINOR(rdev);
3951 } else if (!S_ISSOCK(mode)) {
3956 data->arg.label = label;
3957 status = nfs4_do_create(dir, dentry, data);
3959 nfs4_free_createdata(data);
3964 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3965 struct iattr *sattr, dev_t rdev)
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_mknod(dir, dentry, sattr, label, rdev);
3976 trace_nfs4_mknod(dir, &dentry->d_name, err);
3977 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3979 } while (exception.retry);
3981 nfs4_label_release_security(label);
3986 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3987 struct nfs_fsstat *fsstat)
3989 struct nfs4_statfs_arg args = {
3991 .bitmask = server->attr_bitmask,
3993 struct nfs4_statfs_res res = {
3996 struct rpc_message msg = {
3997 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4002 nfs_fattr_init(fsstat->fattr);
4003 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4006 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4008 struct nfs4_exception exception = { };
4011 err = nfs4_handle_exception(server,
4012 _nfs4_proc_statfs(server, fhandle, fsstat),
4014 } while (exception.retry);
4018 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4019 struct nfs_fsinfo *fsinfo)
4021 struct nfs4_fsinfo_arg args = {
4023 .bitmask = server->attr_bitmask,
4025 struct nfs4_fsinfo_res res = {
4028 struct rpc_message msg = {
4029 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4034 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4037 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4039 struct nfs4_exception exception = { };
4040 unsigned long now = jiffies;
4044 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
4045 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
4047 struct nfs_client *clp = server->nfs_client;
4049 spin_lock(&clp->cl_lock);
4050 clp->cl_lease_time = fsinfo->lease_time * HZ;
4051 clp->cl_last_renewal = now;
4052 spin_unlock(&clp->cl_lock);
4055 err = nfs4_handle_exception(server, err, &exception);
4056 } while (exception.retry);
4060 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4064 nfs_fattr_init(fsinfo->fattr);
4065 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
4067 /* block layout checks this! */
4068 server->pnfs_blksize = fsinfo->blksize;
4069 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
4075 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4076 struct nfs_pathconf *pathconf)
4078 struct nfs4_pathconf_arg args = {
4080 .bitmask = server->attr_bitmask,
4082 struct nfs4_pathconf_res res = {
4083 .pathconf = pathconf,
4085 struct rpc_message msg = {
4086 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4091 /* None of the pathconf attributes are mandatory to implement */
4092 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4093 memset(pathconf, 0, sizeof(*pathconf));
4097 nfs_fattr_init(pathconf->fattr);
4098 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4101 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4102 struct nfs_pathconf *pathconf)
4104 struct nfs4_exception exception = { };
4108 err = nfs4_handle_exception(server,
4109 _nfs4_proc_pathconf(server, fhandle, pathconf),
4111 } while (exception.retry);
4115 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
4116 const struct nfs_open_context *ctx,
4117 const struct nfs_lock_context *l_ctx,
4120 const struct nfs_lockowner *lockowner = NULL;
4123 lockowner = &l_ctx->lockowner;
4124 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
4126 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4128 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4129 const struct nfs_open_context *ctx,
4130 const struct nfs_lock_context *l_ctx,
4133 nfs4_stateid current_stateid;
4135 /* If the current stateid represents a lost lock, then exit */
4136 if (nfs4_set_rw_stateid(¤t_stateid, ctx, l_ctx, fmode) == -EIO)
4138 return nfs4_stateid_match(stateid, ¤t_stateid);
4141 static bool nfs4_error_stateid_expired(int err)
4144 case -NFS4ERR_DELEG_REVOKED:
4145 case -NFS4ERR_ADMIN_REVOKED:
4146 case -NFS4ERR_BAD_STATEID:
4147 case -NFS4ERR_STALE_STATEID:
4148 case -NFS4ERR_OLD_STATEID:
4149 case -NFS4ERR_OPENMODE:
4150 case -NFS4ERR_EXPIRED:
4156 void __nfs4_read_done_cb(struct nfs_pgio_header *hdr)
4158 nfs_invalidate_atime(hdr->inode);
4161 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
4163 struct nfs_server *server = NFS_SERVER(hdr->inode);
4165 trace_nfs4_read(hdr, task->tk_status);
4166 if (nfs4_async_handle_error(task, server,
4167 hdr->args.context->state,
4169 rpc_restart_call_prepare(task);
4173 __nfs4_read_done_cb(hdr);
4174 if (task->tk_status > 0)
4175 renew_lease(server, hdr->timestamp);
4179 static bool nfs4_read_stateid_changed(struct rpc_task *task,
4180 struct nfs_pgio_args *args)
4183 if (!nfs4_error_stateid_expired(task->tk_status) ||
4184 nfs4_stateid_is_current(&args->stateid,
4189 rpc_restart_call_prepare(task);
4193 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
4196 dprintk("--> %s\n", __func__);
4198 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
4200 if (nfs4_read_stateid_changed(task, &hdr->args))
4202 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4203 nfs4_read_done_cb(task, hdr);
4206 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4207 struct rpc_message *msg)
4209 hdr->timestamp = jiffies;
4210 hdr->pgio_done_cb = nfs4_read_done_cb;
4211 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
4212 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
4215 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4216 struct nfs_pgio_header *hdr)
4218 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4219 &hdr->args.seq_args,
4223 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4224 hdr->args.lock_context,
4225 hdr->rw_ops->rw_mode) == -EIO)
4227 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
4232 static int nfs4_write_done_cb(struct rpc_task *task,
4233 struct nfs_pgio_header *hdr)
4235 struct inode *inode = hdr->inode;
4237 trace_nfs4_write(hdr, task->tk_status);
4238 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4239 hdr->args.context->state,
4241 rpc_restart_call_prepare(task);
4244 if (task->tk_status >= 0) {
4245 renew_lease(NFS_SERVER(inode), hdr->timestamp);
4246 nfs_writeback_update_inode(hdr);
4251 static bool nfs4_write_stateid_changed(struct rpc_task *task,
4252 struct nfs_pgio_args *args)
4255 if (!nfs4_error_stateid_expired(task->tk_status) ||
4256 nfs4_stateid_is_current(&args->stateid,
4261 rpc_restart_call_prepare(task);
4265 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
4267 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
4269 if (nfs4_write_stateid_changed(task, &hdr->args))
4271 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4272 nfs4_write_done_cb(task, hdr);
4276 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
4278 /* Don't request attributes for pNFS or O_DIRECT writes */
4279 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
4281 /* Otherwise, request attributes if and only if we don't hold
4284 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
4287 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4288 struct rpc_message *msg)
4290 struct nfs_server *server = NFS_SERVER(hdr->inode);
4292 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4293 hdr->args.bitmask = NULL;
4294 hdr->res.fattr = NULL;
4296 hdr->args.bitmask = server->cache_consistency_bitmask;
4298 if (!hdr->pgio_done_cb)
4299 hdr->pgio_done_cb = nfs4_write_done_cb;
4300 hdr->res.server = server;
4301 hdr->timestamp = jiffies;
4303 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
4304 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
4307 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4309 nfs4_setup_sequence(NFS_SERVER(data->inode),
4310 &data->args.seq_args,
4315 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
4317 struct inode *inode = data->inode;
4319 trace_nfs4_commit(data, task->tk_status);
4320 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4321 NULL, NULL) == -EAGAIN) {
4322 rpc_restart_call_prepare(task);
4328 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
4330 if (!nfs4_sequence_done(task, &data->res.seq_res))
4332 return data->commit_done_cb(task, data);
4335 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
4337 struct nfs_server *server = NFS_SERVER(data->inode);
4339 if (data->commit_done_cb == NULL)
4340 data->commit_done_cb = nfs4_commit_done_cb;
4341 data->res.server = server;
4342 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
4343 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4346 struct nfs4_renewdata {
4347 struct nfs_client *client;
4348 unsigned long timestamp;
4352 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4353 * standalone procedure for queueing an asynchronous RENEW.
4355 static void nfs4_renew_release(void *calldata)
4357 struct nfs4_renewdata *data = calldata;
4358 struct nfs_client *clp = data->client;
4360 if (atomic_read(&clp->cl_count) > 1)
4361 nfs4_schedule_state_renewal(clp);
4362 nfs_put_client(clp);
4366 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
4368 struct nfs4_renewdata *data = calldata;
4369 struct nfs_client *clp = data->client;
4370 unsigned long timestamp = data->timestamp;
4372 trace_nfs4_renew_async(clp, task->tk_status);
4373 switch (task->tk_status) {
4376 case -NFS4ERR_LEASE_MOVED:
4377 nfs4_schedule_lease_moved_recovery(clp);
4380 /* Unless we're shutting down, schedule state recovery! */
4381 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4383 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
4384 nfs4_schedule_lease_recovery(clp);
4387 nfs4_schedule_path_down_recovery(clp);
4389 do_renew_lease(clp, timestamp);
4392 static const struct rpc_call_ops nfs4_renew_ops = {
4393 .rpc_call_done = nfs4_renew_done,
4394 .rpc_release = nfs4_renew_release,
4397 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
4399 struct rpc_message msg = {
4400 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4404 struct nfs4_renewdata *data;
4406 if (renew_flags == 0)
4408 if (!atomic_inc_not_zero(&clp->cl_count))
4410 data = kmalloc(sizeof(*data), GFP_NOFS);
4414 data->timestamp = jiffies;
4415 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
4416 &nfs4_renew_ops, data);
4419 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
4421 struct rpc_message msg = {
4422 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4426 unsigned long now = jiffies;
4429 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4432 do_renew_lease(clp, now);
4436 static inline int nfs4_server_supports_acls(struct nfs_server *server)
4438 return server->caps & NFS_CAP_ACLS;
4441 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4442 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
4445 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
4447 static int buf_to_pages_noslab(const void *buf, size_t buflen,
4448 struct page **pages, unsigned int *pgbase)
4450 struct page *newpage, **spages;
4456 len = min_t(size_t, PAGE_SIZE, buflen);
4457 newpage = alloc_page(GFP_KERNEL);
4459 if (newpage == NULL)
4461 memcpy(page_address(newpage), buf, len);
4466 } while (buflen != 0);
4472 __free_page(spages[rc-1]);
4476 struct nfs4_cached_acl {
4482 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
4484 struct nfs_inode *nfsi = NFS_I(inode);
4486 spin_lock(&inode->i_lock);
4487 kfree(nfsi->nfs4_acl);
4488 nfsi->nfs4_acl = acl;
4489 spin_unlock(&inode->i_lock);
4492 static void nfs4_zap_acl_attr(struct inode *inode)
4494 nfs4_set_cached_acl(inode, NULL);
4497 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4499 struct nfs_inode *nfsi = NFS_I(inode);
4500 struct nfs4_cached_acl *acl;
4503 spin_lock(&inode->i_lock);
4504 acl = nfsi->nfs4_acl;
4507 if (buf == NULL) /* user is just asking for length */
4509 if (acl->cached == 0)
4511 ret = -ERANGE; /* see getxattr(2) man page */
4512 if (acl->len > buflen)
4514 memcpy(buf, acl->data, acl->len);
4518 spin_unlock(&inode->i_lock);
4522 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
4524 struct nfs4_cached_acl *acl;
4525 size_t buflen = sizeof(*acl) + acl_len;
4527 if (buflen <= PAGE_SIZE) {
4528 acl = kmalloc(buflen, GFP_KERNEL);
4532 _copy_from_pages(acl->data, pages, pgbase, acl_len);
4534 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4541 nfs4_set_cached_acl(inode, acl);
4545 * The getxattr API returns the required buffer length when called with a
4546 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4547 * the required buf. On a NULL buf, we send a page of data to the server
4548 * guessing that the ACL request can be serviced by a page. If so, we cache
4549 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4550 * the cache. If not so, we throw away the page, and cache the required
4551 * length. The next getxattr call will then produce another round trip to
4552 * the server, this time with the input buf of the required size.
4554 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4556 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
4557 struct nfs_getaclargs args = {
4558 .fh = NFS_FH(inode),
4562 struct nfs_getaclres res = {
4565 struct rpc_message msg = {
4566 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4570 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4571 int ret = -ENOMEM, i;
4573 /* As long as we're doing a round trip to the server anyway,
4574 * let's be prepared for a page of acl data. */
4577 if (npages > ARRAY_SIZE(pages))
4580 for (i = 0; i < npages; i++) {
4581 pages[i] = alloc_page(GFP_KERNEL);
4586 /* for decoding across pages */
4587 res.acl_scratch = alloc_page(GFP_KERNEL);
4588 if (!res.acl_scratch)
4591 args.acl_len = npages * PAGE_SIZE;
4592 args.acl_pgbase = 0;
4594 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
4595 __func__, buf, buflen, npages, args.acl_len);
4596 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4597 &msg, &args.seq_args, &res.seq_res, 0);
4601 /* Handle the case where the passed-in buffer is too short */
4602 if (res.acl_flags & NFS4_ACL_TRUNC) {
4603 /* Did the user only issue a request for the acl length? */
4609 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
4611 if (res.acl_len > buflen) {
4615 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
4620 for (i = 0; i < npages; i++)
4622 __free_page(pages[i]);
4623 if (res.acl_scratch)
4624 __free_page(res.acl_scratch);
4628 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4630 struct nfs4_exception exception = { };
4633 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
4634 trace_nfs4_get_acl(inode, ret);
4637 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4638 } while (exception.retry);
4642 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4644 struct nfs_server *server = NFS_SERVER(inode);
4647 if (!nfs4_server_supports_acls(server))
4649 ret = nfs_revalidate_inode(server, inode);
4652 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4653 nfs_zap_acl_cache(inode);
4654 ret = nfs4_read_cached_acl(inode, buf, buflen);
4656 /* -ENOENT is returned if there is no ACL or if there is an ACL
4657 * but no cached acl data, just the acl length */
4659 return nfs4_get_acl_uncached(inode, buf, buflen);
4662 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4664 struct nfs_server *server = NFS_SERVER(inode);
4665 struct page *pages[NFS4ACL_MAXPAGES];
4666 struct nfs_setaclargs arg = {
4667 .fh = NFS_FH(inode),
4671 struct nfs_setaclres res;
4672 struct rpc_message msg = {
4673 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4677 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4680 if (!nfs4_server_supports_acls(server))
4682 if (npages > ARRAY_SIZE(pages))
4684 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4687 nfs4_inode_return_delegation(inode);
4688 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4691 * Free each page after tx, so the only ref left is
4692 * held by the network stack
4695 put_page(pages[i-1]);
4698 * Acl update can result in inode attribute update.
4699 * so mark the attribute cache invalid.
4701 spin_lock(&inode->i_lock);
4702 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4703 spin_unlock(&inode->i_lock);
4704 nfs_access_zap_cache(inode);
4705 nfs_zap_acl_cache(inode);
4709 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4711 struct nfs4_exception exception = { };
4714 err = __nfs4_proc_set_acl(inode, buf, buflen);
4715 trace_nfs4_set_acl(inode, err);
4716 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4718 } while (exception.retry);
4722 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
4723 static int _nfs4_get_security_label(struct inode *inode, void *buf,
4726 struct nfs_server *server = NFS_SERVER(inode);
4727 struct nfs_fattr fattr;
4728 struct nfs4_label label = {0, 0, buflen, buf};
4730 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4731 struct nfs4_getattr_arg arg = {
4732 .fh = NFS_FH(inode),
4735 struct nfs4_getattr_res res = {
4740 struct rpc_message msg = {
4741 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4747 nfs_fattr_init(&fattr);
4749 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
4752 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
4754 if (buflen < label.len)
4759 static int nfs4_get_security_label(struct inode *inode, void *buf,
4762 struct nfs4_exception exception = { };
4765 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4769 err = _nfs4_get_security_label(inode, buf, buflen);
4770 trace_nfs4_get_security_label(inode, err);
4771 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4773 } while (exception.retry);
4777 static int _nfs4_do_set_security_label(struct inode *inode,
4778 struct nfs4_label *ilabel,
4779 struct nfs_fattr *fattr,
4780 struct nfs4_label *olabel)
4783 struct iattr sattr = {0};
4784 struct nfs_server *server = NFS_SERVER(inode);
4785 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4786 struct nfs_setattrargs arg = {
4787 .fh = NFS_FH(inode),
4793 struct nfs_setattrres res = {
4798 struct rpc_message msg = {
4799 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
4805 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
4807 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4809 dprintk("%s failed: %d\n", __func__, status);
4814 static int nfs4_do_set_security_label(struct inode *inode,
4815 struct nfs4_label *ilabel,
4816 struct nfs_fattr *fattr,
4817 struct nfs4_label *olabel)
4819 struct nfs4_exception exception = { };
4823 err = _nfs4_do_set_security_label(inode, ilabel,
4825 trace_nfs4_set_security_label(inode, err);
4826 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4828 } while (exception.retry);
4833 nfs4_set_security_label(struct dentry *dentry, const void *buf, size_t buflen)
4835 struct nfs4_label ilabel, *olabel = NULL;
4836 struct nfs_fattr fattr;
4837 struct rpc_cred *cred;
4838 struct inode *inode = d_inode(dentry);
4841 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4844 nfs_fattr_init(&fattr);
4848 ilabel.label = (char *)buf;
4849 ilabel.len = buflen;
4851 cred = rpc_lookup_cred();
4853 return PTR_ERR(cred);
4855 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4856 if (IS_ERR(olabel)) {
4857 status = -PTR_ERR(olabel);
4861 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
4863 nfs_setsecurity(inode, &fattr, olabel);
4865 nfs4_label_free(olabel);
4870 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
4874 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server,
4875 struct nfs4_state *state, long *timeout)
4877 struct nfs_client *clp = server->nfs_client;
4879 if (task->tk_status >= 0)
4881 switch(task->tk_status) {
4882 case -NFS4ERR_DELEG_REVOKED:
4883 case -NFS4ERR_ADMIN_REVOKED:
4884 case -NFS4ERR_BAD_STATEID:
4885 case -NFS4ERR_OPENMODE:
4888 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4889 goto recovery_failed;
4890 goto wait_on_recovery;
4891 case -NFS4ERR_EXPIRED:
4892 if (state != NULL) {
4893 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4894 goto recovery_failed;
4896 case -NFS4ERR_STALE_STATEID:
4897 case -NFS4ERR_STALE_CLIENTID:
4898 nfs4_schedule_lease_recovery(clp);
4899 goto wait_on_recovery;
4900 case -NFS4ERR_MOVED:
4901 if (nfs4_schedule_migration_recovery(server) < 0)
4902 goto recovery_failed;
4903 goto wait_on_recovery;
4904 case -NFS4ERR_LEASE_MOVED:
4905 nfs4_schedule_lease_moved_recovery(clp);
4906 goto wait_on_recovery;
4907 #if defined(CONFIG_NFS_V4_1)
4908 case -NFS4ERR_BADSESSION:
4909 case -NFS4ERR_BADSLOT:
4910 case -NFS4ERR_BAD_HIGH_SLOT:
4911 case -NFS4ERR_DEADSESSION:
4912 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4913 case -NFS4ERR_SEQ_FALSE_RETRY:
4914 case -NFS4ERR_SEQ_MISORDERED:
4915 dprintk("%s ERROR %d, Reset session\n", __func__,
4917 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
4918 goto wait_on_recovery;
4919 #endif /* CONFIG_NFS_V4_1 */
4920 case -NFS4ERR_DELAY:
4921 nfs_inc_server_stats(server, NFSIOS_DELAY);
4922 rpc_delay(task, nfs4_update_delay(timeout));
4924 case -NFS4ERR_GRACE:
4925 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4926 case -NFS4ERR_RETRY_UNCACHED_REP:
4927 case -NFS4ERR_OLD_STATEID:
4930 task->tk_status = nfs4_map_errors(task->tk_status);
4933 task->tk_status = -EIO;
4936 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
4937 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4938 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4939 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
4940 goto recovery_failed;
4942 task->tk_status = 0;
4946 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4947 nfs4_verifier *bootverf)
4951 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4952 /* An impossible timestamp guarantees this value
4953 * will never match a generated boot time. */
4955 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
4957 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4958 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
4959 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
4961 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4965 nfs4_init_nonuniform_client_string(struct nfs_client *clp)
4970 bool retried = false;
4972 if (clp->cl_owner_id != NULL)
4976 len = 10 + strlen(clp->cl_ipaddr) + 1 +
4977 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
4979 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
4983 if (len > NFS4_OPAQUE_LIMIT + 1)
4987 * Since this string is allocated at mount time, and held until the
4988 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
4989 * about a memory-reclaim deadlock.
4991 str = kmalloc(len, GFP_KERNEL);
4996 result = scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
4998 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
4999 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
5002 /* Did something change? */
5003 if (result >= len) {
5010 clp->cl_owner_id = str;
5015 nfs4_init_uniquifier_client_string(struct nfs_client *clp)
5021 len = 10 + 10 + 1 + 10 + 1 +
5022 strlen(nfs4_client_id_uniquifier) + 1 +
5023 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5025 if (len > NFS4_OPAQUE_LIMIT + 1)
5029 * Since this string is allocated at mount time, and held until the
5030 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5031 * about a memory-reclaim deadlock.
5033 str = kmalloc(len, GFP_KERNEL);
5037 result = scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
5038 clp->rpc_ops->version, clp->cl_minorversion,
5039 nfs4_client_id_uniquifier,
5040 clp->cl_rpcclient->cl_nodename);
5041 if (result >= len) {
5045 clp->cl_owner_id = str;
5050 nfs4_init_uniform_client_string(struct nfs_client *clp)
5056 if (clp->cl_owner_id != NULL)
5059 if (nfs4_client_id_uniquifier[0] != '\0')
5060 return nfs4_init_uniquifier_client_string(clp);
5062 len = 10 + 10 + 1 + 10 + 1 +
5063 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5065 if (len > NFS4_OPAQUE_LIMIT + 1)
5069 * Since this string is allocated at mount time, and held until the
5070 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5071 * about a memory-reclaim deadlock.
5073 str = kmalloc(len, GFP_KERNEL);
5077 result = scnprintf(str, len, "Linux NFSv%u.%u %s",
5078 clp->rpc_ops->version, clp->cl_minorversion,
5079 clp->cl_rpcclient->cl_nodename);
5080 if (result >= len) {
5084 clp->cl_owner_id = str;
5089 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5090 * services. Advertise one based on the address family of the
5094 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5096 if (strchr(clp->cl_ipaddr, ':') != NULL)
5097 return scnprintf(buf, len, "tcp6");
5099 return scnprintf(buf, len, "tcp");
5102 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5104 struct nfs4_setclientid *sc = calldata;
5106 if (task->tk_status == 0)
5107 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5110 static const struct rpc_call_ops nfs4_setclientid_ops = {
5111 .rpc_call_done = nfs4_setclientid_done,
5115 * nfs4_proc_setclientid - Negotiate client ID
5116 * @clp: state data structure
5117 * @program: RPC program for NFSv4 callback service
5118 * @port: IP port number for NFS4 callback service
5119 * @cred: RPC credential to use for this call
5120 * @res: where to place the result
5122 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5124 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5125 unsigned short port, struct rpc_cred *cred,
5126 struct nfs4_setclientid_res *res)
5128 nfs4_verifier sc_verifier;
5129 struct nfs4_setclientid setclientid = {
5130 .sc_verifier = &sc_verifier,
5134 struct rpc_message msg = {
5135 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5136 .rpc_argp = &setclientid,
5140 struct rpc_task *task;
5141 struct rpc_task_setup task_setup_data = {
5142 .rpc_client = clp->cl_rpcclient,
5143 .rpc_message = &msg,
5144 .callback_ops = &nfs4_setclientid_ops,
5145 .callback_data = &setclientid,
5146 .flags = RPC_TASK_TIMEOUT,
5150 /* nfs_client_id4 */
5151 nfs4_init_boot_verifier(clp, &sc_verifier);
5153 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5154 status = nfs4_init_uniform_client_string(clp);
5156 status = nfs4_init_nonuniform_client_string(clp);
5162 setclientid.sc_netid_len =
5163 nfs4_init_callback_netid(clp,
5164 setclientid.sc_netid,
5165 sizeof(setclientid.sc_netid));
5166 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
5167 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
5168 clp->cl_ipaddr, port >> 8, port & 255);
5170 dprintk("NFS call setclientid auth=%s, '%s'\n",
5171 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5173 task = rpc_run_task(&task_setup_data);
5175 status = PTR_ERR(task);
5178 status = task->tk_status;
5179 if (setclientid.sc_cred) {
5180 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5181 put_rpccred(setclientid.sc_cred);
5185 trace_nfs4_setclientid(clp, status);
5186 dprintk("NFS reply setclientid: %d\n", status);
5191 * nfs4_proc_setclientid_confirm - Confirm client ID
5192 * @clp: state data structure
5193 * @res: result of a previous SETCLIENTID
5194 * @cred: RPC credential to use for this call
5196 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5198 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
5199 struct nfs4_setclientid_res *arg,
5200 struct rpc_cred *cred)
5202 struct rpc_message msg = {
5203 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
5209 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5210 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5212 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5213 trace_nfs4_setclientid_confirm(clp, status);
5214 dprintk("NFS reply setclientid_confirm: %d\n", status);
5218 struct nfs4_delegreturndata {
5219 struct nfs4_delegreturnargs args;
5220 struct nfs4_delegreturnres res;
5222 nfs4_stateid stateid;
5223 unsigned long timestamp;
5224 struct nfs_fattr fattr;
5226 struct inode *inode;
5231 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5233 struct nfs4_delegreturndata *data = calldata;
5235 if (!nfs4_sequence_done(task, &data->res.seq_res))
5238 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
5239 switch (task->tk_status) {
5241 renew_lease(data->res.server, data->timestamp);
5242 case -NFS4ERR_ADMIN_REVOKED:
5243 case -NFS4ERR_DELEG_REVOKED:
5244 case -NFS4ERR_BAD_STATEID:
5245 case -NFS4ERR_OLD_STATEID:
5246 case -NFS4ERR_STALE_STATEID:
5247 case -NFS4ERR_EXPIRED:
5248 task->tk_status = 0;
5250 pnfs_roc_set_barrier(data->inode, data->roc_barrier);
5253 if (nfs4_async_handle_error(task, data->res.server,
5254 NULL, NULL) == -EAGAIN) {
5255 rpc_restart_call_prepare(task);
5259 data->rpc_status = task->tk_status;
5262 static void nfs4_delegreturn_release(void *calldata)
5264 struct nfs4_delegreturndata *data = calldata;
5265 struct inode *inode = data->inode;
5269 pnfs_roc_release(inode);
5270 nfs_iput_and_deactive(inode);
5275 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5277 struct nfs4_delegreturndata *d_data;
5279 d_data = (struct nfs4_delegreturndata *)data;
5282 pnfs_roc_drain(d_data->inode, &d_data->roc_barrier, task))
5285 nfs4_setup_sequence(d_data->res.server,
5286 &d_data->args.seq_args,
5287 &d_data->res.seq_res,
5291 static const struct rpc_call_ops nfs4_delegreturn_ops = {
5292 .rpc_call_prepare = nfs4_delegreturn_prepare,
5293 .rpc_call_done = nfs4_delegreturn_done,
5294 .rpc_release = nfs4_delegreturn_release,
5297 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
5299 struct nfs4_delegreturndata *data;
5300 struct nfs_server *server = NFS_SERVER(inode);
5301 struct rpc_task *task;
5302 struct rpc_message msg = {
5303 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5306 struct rpc_task_setup task_setup_data = {
5307 .rpc_client = server->client,
5308 .rpc_message = &msg,
5309 .callback_ops = &nfs4_delegreturn_ops,
5310 .flags = RPC_TASK_ASYNC,
5314 data = kzalloc(sizeof(*data), GFP_NOFS);
5317 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
5318 data->args.fhandle = &data->fh;
5319 data->args.stateid = &data->stateid;
5320 data->args.bitmask = server->cache_consistency_bitmask;
5321 nfs_copy_fh(&data->fh, NFS_FH(inode));
5322 nfs4_stateid_copy(&data->stateid, stateid);
5323 data->res.fattr = &data->fattr;
5324 data->res.server = server;
5325 nfs_fattr_init(data->res.fattr);
5326 data->timestamp = jiffies;
5327 data->rpc_status = 0;
5328 data->inode = nfs_igrab_and_active(inode);
5330 data->roc = nfs4_roc(inode);
5332 task_setup_data.callback_data = data;
5333 msg.rpc_argp = &data->args;
5334 msg.rpc_resp = &data->res;
5335 task = rpc_run_task(&task_setup_data);
5337 return PTR_ERR(task);
5340 status = nfs4_wait_for_completion_rpc_task(task);
5343 status = data->rpc_status;
5345 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5347 nfs_refresh_inode(inode, &data->fattr);
5353 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
5355 struct nfs_server *server = NFS_SERVER(inode);
5356 struct nfs4_exception exception = { };
5359 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
5360 trace_nfs4_delegreturn(inode, err);
5362 case -NFS4ERR_STALE_STATEID:
5363 case -NFS4ERR_EXPIRED:
5367 err = nfs4_handle_exception(server, err, &exception);
5368 } while (exception.retry);
5372 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
5373 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
5376 * sleep, with exponential backoff, and retry the LOCK operation.
5378 static unsigned long
5379 nfs4_set_lock_task_retry(unsigned long timeout)
5381 freezable_schedule_timeout_killable_unsafe(timeout);
5383 if (timeout > NFS4_LOCK_MAXTIMEOUT)
5384 return NFS4_LOCK_MAXTIMEOUT;
5388 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5390 struct inode *inode = state->inode;
5391 struct nfs_server *server = NFS_SERVER(inode);
5392 struct nfs_client *clp = server->nfs_client;
5393 struct nfs_lockt_args arg = {
5394 .fh = NFS_FH(inode),
5397 struct nfs_lockt_res res = {
5400 struct rpc_message msg = {
5401 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5404 .rpc_cred = state->owner->so_cred,
5406 struct nfs4_lock_state *lsp;
5409 arg.lock_owner.clientid = clp->cl_clientid;
5410 status = nfs4_set_lock_state(state, request);
5413 lsp = request->fl_u.nfs4_fl.owner;
5414 arg.lock_owner.id = lsp->ls_seqid.owner_id;
5415 arg.lock_owner.s_dev = server->s_dev;
5416 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5419 request->fl_type = F_UNLCK;
5421 case -NFS4ERR_DENIED:
5424 request->fl_ops->fl_release_private(request);
5425 request->fl_ops = NULL;
5430 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5432 struct nfs4_exception exception = { };
5436 err = _nfs4_proc_getlk(state, cmd, request);
5437 trace_nfs4_get_lock(request, state, cmd, err);
5438 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
5440 } while (exception.retry);
5444 static int do_vfs_lock(struct file *file, struct file_lock *fl)
5447 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
5449 res = posix_lock_file_wait(file, fl);
5452 res = flock_lock_file_wait(file, fl);
5460 struct nfs4_unlockdata {
5461 struct nfs_locku_args arg;
5462 struct nfs_locku_res res;
5463 struct nfs4_lock_state *lsp;
5464 struct nfs_open_context *ctx;
5465 struct file_lock fl;
5466 const struct nfs_server *server;
5467 unsigned long timestamp;
5470 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5471 struct nfs_open_context *ctx,
5472 struct nfs4_lock_state *lsp,
5473 struct nfs_seqid *seqid)
5475 struct nfs4_unlockdata *p;
5476 struct inode *inode = lsp->ls_state->inode;
5478 p = kzalloc(sizeof(*p), GFP_NOFS);
5481 p->arg.fh = NFS_FH(inode);
5483 p->arg.seqid = seqid;
5484 p->res.seqid = seqid;
5486 atomic_inc(&lsp->ls_count);
5487 /* Ensure we don't close file until we're done freeing locks! */
5488 p->ctx = get_nfs_open_context(ctx);
5489 get_file(fl->fl_file);
5490 memcpy(&p->fl, fl, sizeof(p->fl));
5491 p->server = NFS_SERVER(inode);
5495 static void nfs4_locku_release_calldata(void *data)
5497 struct nfs4_unlockdata *calldata = data;
5498 nfs_free_seqid(calldata->arg.seqid);
5499 nfs4_put_lock_state(calldata->lsp);
5500 put_nfs_open_context(calldata->ctx);
5501 fput(calldata->fl.fl_file);
5505 static void nfs4_locku_done(struct rpc_task *task, void *data)
5507 struct nfs4_unlockdata *calldata = data;
5509 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5511 switch (task->tk_status) {
5513 renew_lease(calldata->server, calldata->timestamp);
5514 do_vfs_lock(calldata->fl.fl_file, &calldata->fl);
5515 if (nfs4_update_lock_stateid(calldata->lsp,
5516 &calldata->res.stateid))
5518 case -NFS4ERR_BAD_STATEID:
5519 case -NFS4ERR_OLD_STATEID:
5520 case -NFS4ERR_STALE_STATEID:
5521 case -NFS4ERR_EXPIRED:
5522 if (!nfs4_stateid_match(&calldata->arg.stateid,
5523 &calldata->lsp->ls_stateid))
5524 rpc_restart_call_prepare(task);
5527 if (nfs4_async_handle_error(task, calldata->server,
5528 NULL, NULL) == -EAGAIN)
5529 rpc_restart_call_prepare(task);
5531 nfs_release_seqid(calldata->arg.seqid);
5534 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
5536 struct nfs4_unlockdata *calldata = data;
5538 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
5540 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
5541 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
5542 /* Note: exit _without_ running nfs4_locku_done */
5545 calldata->timestamp = jiffies;
5546 if (nfs4_setup_sequence(calldata->server,
5547 &calldata->arg.seq_args,
5548 &calldata->res.seq_res,
5550 nfs_release_seqid(calldata->arg.seqid);
5553 task->tk_action = NULL;
5555 nfs4_sequence_done(task, &calldata->res.seq_res);
5558 static const struct rpc_call_ops nfs4_locku_ops = {
5559 .rpc_call_prepare = nfs4_locku_prepare,
5560 .rpc_call_done = nfs4_locku_done,
5561 .rpc_release = nfs4_locku_release_calldata,
5564 static struct rpc_task *nfs4_do_unlck(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 *data;
5570 struct rpc_message msg = {
5571 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5572 .rpc_cred = ctx->cred,
5574 struct rpc_task_setup task_setup_data = {
5575 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
5576 .rpc_message = &msg,
5577 .callback_ops = &nfs4_locku_ops,
5578 .workqueue = nfsiod_workqueue,
5579 .flags = RPC_TASK_ASYNC,
5582 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5583 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5585 /* Ensure this is an unlock - when canceling a lock, the
5586 * canceled lock is passed in, and it won't be an unlock.
5588 fl->fl_type = F_UNLCK;
5590 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5592 nfs_free_seqid(seqid);
5593 return ERR_PTR(-ENOMEM);
5596 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
5597 msg.rpc_argp = &data->arg;
5598 msg.rpc_resp = &data->res;
5599 task_setup_data.callback_data = data;
5600 return rpc_run_task(&task_setup_data);
5603 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5605 struct inode *inode = state->inode;
5606 struct nfs4_state_owner *sp = state->owner;
5607 struct nfs_inode *nfsi = NFS_I(inode);
5608 struct nfs_seqid *seqid;
5609 struct nfs4_lock_state *lsp;
5610 struct rpc_task *task;
5611 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
5613 unsigned char fl_flags = request->fl_flags;
5615 status = nfs4_set_lock_state(state, request);
5616 /* Unlock _before_ we do the RPC call */
5617 request->fl_flags |= FL_EXISTS;
5618 /* Exclude nfs_delegation_claim_locks() */
5619 mutex_lock(&sp->so_delegreturn_mutex);
5620 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
5621 down_read(&nfsi->rwsem);
5622 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
5623 up_read(&nfsi->rwsem);
5624 mutex_unlock(&sp->so_delegreturn_mutex);
5627 up_read(&nfsi->rwsem);
5628 mutex_unlock(&sp->so_delegreturn_mutex);
5631 /* Is this a delegated lock? */
5632 lsp = request->fl_u.nfs4_fl.owner;
5633 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5635 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
5636 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
5640 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
5641 status = PTR_ERR(task);
5644 status = nfs4_wait_for_completion_rpc_task(task);
5647 request->fl_flags = fl_flags;
5648 trace_nfs4_unlock(request, state, F_SETLK, status);
5652 struct nfs4_lockdata {
5653 struct nfs_lock_args arg;
5654 struct nfs_lock_res res;
5655 struct nfs4_lock_state *lsp;
5656 struct nfs_open_context *ctx;
5657 struct file_lock fl;
5658 unsigned long timestamp;
5661 struct nfs_server *server;
5664 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
5665 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5668 struct nfs4_lockdata *p;
5669 struct inode *inode = lsp->ls_state->inode;
5670 struct nfs_server *server = NFS_SERVER(inode);
5671 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
5673 p = kzalloc(sizeof(*p), gfp_mask);
5677 p->arg.fh = NFS_FH(inode);
5679 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
5680 if (IS_ERR(p->arg.open_seqid))
5682 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
5683 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
5684 if (IS_ERR(p->arg.lock_seqid))
5685 goto out_free_seqid;
5686 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
5687 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
5688 p->arg.lock_owner.s_dev = server->s_dev;
5689 p->res.lock_seqid = p->arg.lock_seqid;
5692 atomic_inc(&lsp->ls_count);
5693 p->ctx = get_nfs_open_context(ctx);
5694 get_file(fl->fl_file);
5695 memcpy(&p->fl, fl, sizeof(p->fl));
5698 nfs_free_seqid(p->arg.open_seqid);
5704 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5706 struct nfs4_lockdata *data = calldata;
5707 struct nfs4_state *state = data->lsp->ls_state;
5709 dprintk("%s: begin!\n", __func__);
5710 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
5712 /* Do we need to do an open_to_lock_owner? */
5713 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
5714 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
5715 goto out_release_lock_seqid;
5717 nfs4_stateid_copy(&data->arg.open_stateid,
5718 &state->open_stateid);
5719 data->arg.new_lock_owner = 1;
5720 data->res.open_seqid = data->arg.open_seqid;
5722 data->arg.new_lock_owner = 0;
5723 nfs4_stateid_copy(&data->arg.lock_stateid,
5724 &data->lsp->ls_stateid);
5726 if (!nfs4_valid_open_stateid(state)) {
5727 data->rpc_status = -EBADF;
5728 task->tk_action = NULL;
5729 goto out_release_open_seqid;
5731 data->timestamp = jiffies;
5732 if (nfs4_setup_sequence(data->server,
5733 &data->arg.seq_args,
5737 out_release_open_seqid:
5738 nfs_release_seqid(data->arg.open_seqid);
5739 out_release_lock_seqid:
5740 nfs_release_seqid(data->arg.lock_seqid);
5742 nfs4_sequence_done(task, &data->res.seq_res);
5743 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
5746 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5748 struct nfs4_lockdata *data = calldata;
5749 struct nfs4_lock_state *lsp = data->lsp;
5751 dprintk("%s: begin!\n", __func__);
5753 if (!nfs4_sequence_done(task, &data->res.seq_res))
5756 data->rpc_status = task->tk_status;
5757 switch (task->tk_status) {
5759 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
5761 if (data->arg.new_lock) {
5762 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
5763 if (do_vfs_lock(data->fl.fl_file, &data->fl) < 0) {
5764 rpc_restart_call_prepare(task);
5768 if (data->arg.new_lock_owner != 0) {
5769 nfs_confirm_seqid(&lsp->ls_seqid, 0);
5770 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
5771 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
5772 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
5773 rpc_restart_call_prepare(task);
5775 case -NFS4ERR_BAD_STATEID:
5776 case -NFS4ERR_OLD_STATEID:
5777 case -NFS4ERR_STALE_STATEID:
5778 case -NFS4ERR_EXPIRED:
5779 if (data->arg.new_lock_owner != 0) {
5780 if (!nfs4_stateid_match(&data->arg.open_stateid,
5781 &lsp->ls_state->open_stateid))
5782 rpc_restart_call_prepare(task);
5783 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
5785 rpc_restart_call_prepare(task);
5787 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
5790 static void nfs4_lock_release(void *calldata)
5792 struct nfs4_lockdata *data = calldata;
5794 dprintk("%s: begin!\n", __func__);
5795 nfs_free_seqid(data->arg.open_seqid);
5796 if (data->cancelled != 0) {
5797 struct rpc_task *task;
5798 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5799 data->arg.lock_seqid);
5801 rpc_put_task_async(task);
5802 dprintk("%s: cancelling lock!\n", __func__);
5804 nfs_free_seqid(data->arg.lock_seqid);
5805 nfs4_put_lock_state(data->lsp);
5806 put_nfs_open_context(data->ctx);
5807 fput(data->fl.fl_file);
5809 dprintk("%s: done!\n", __func__);
5812 static const struct rpc_call_ops nfs4_lock_ops = {
5813 .rpc_call_prepare = nfs4_lock_prepare,
5814 .rpc_call_done = nfs4_lock_done,
5815 .rpc_release = nfs4_lock_release,
5818 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5821 case -NFS4ERR_ADMIN_REVOKED:
5822 case -NFS4ERR_BAD_STATEID:
5823 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
5824 if (new_lock_owner != 0 ||
5825 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
5826 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
5828 case -NFS4ERR_STALE_STATEID:
5829 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
5830 case -NFS4ERR_EXPIRED:
5831 nfs4_schedule_lease_recovery(server->nfs_client);
5835 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
5837 struct nfs4_lockdata *data;
5838 struct rpc_task *task;
5839 struct rpc_message msg = {
5840 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5841 .rpc_cred = state->owner->so_cred,
5843 struct rpc_task_setup task_setup_data = {
5844 .rpc_client = NFS_CLIENT(state->inode),
5845 .rpc_message = &msg,
5846 .callback_ops = &nfs4_lock_ops,
5847 .workqueue = nfsiod_workqueue,
5848 .flags = RPC_TASK_ASYNC,
5852 dprintk("%s: begin!\n", __func__);
5853 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
5854 fl->fl_u.nfs4_fl.owner,
5855 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
5859 data->arg.block = 1;
5860 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
5861 msg.rpc_argp = &data->arg;
5862 msg.rpc_resp = &data->res;
5863 task_setup_data.callback_data = data;
5864 if (recovery_type > NFS_LOCK_NEW) {
5865 if (recovery_type == NFS_LOCK_RECLAIM)
5866 data->arg.reclaim = NFS_LOCK_RECLAIM;
5867 nfs4_set_sequence_privileged(&data->arg.seq_args);
5869 data->arg.new_lock = 1;
5870 task = rpc_run_task(&task_setup_data);
5872 return PTR_ERR(task);
5873 ret = nfs4_wait_for_completion_rpc_task(task);
5875 ret = data->rpc_status;
5877 nfs4_handle_setlk_error(data->server, data->lsp,
5878 data->arg.new_lock_owner, ret);
5880 data->cancelled = 1;
5882 dprintk("%s: done, ret = %d!\n", __func__, ret);
5886 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
5888 struct nfs_server *server = NFS_SERVER(state->inode);
5889 struct nfs4_exception exception = {
5890 .inode = state->inode,
5895 /* Cache the lock if possible... */
5896 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5898 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
5899 trace_nfs4_lock_reclaim(request, state, F_SETLK, err);
5900 if (err != -NFS4ERR_DELAY)
5902 nfs4_handle_exception(server, err, &exception);
5903 } while (exception.retry);
5907 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
5909 struct nfs_server *server = NFS_SERVER(state->inode);
5910 struct nfs4_exception exception = {
5911 .inode = state->inode,
5915 err = nfs4_set_lock_state(state, request);
5918 if (!recover_lost_locks) {
5919 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
5923 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5925 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
5926 trace_nfs4_lock_expired(request, state, F_SETLK, err);
5930 case -NFS4ERR_GRACE:
5931 case -NFS4ERR_DELAY:
5932 nfs4_handle_exception(server, err, &exception);
5935 } while (exception.retry);
5940 #if defined(CONFIG_NFS_V4_1)
5942 * nfs41_check_expired_locks - possibly free a lock stateid
5944 * @state: NFSv4 state for an inode
5946 * Returns NFS_OK if recovery for this stateid is now finished.
5947 * Otherwise a negative NFS4ERR value is returned.
5949 static int nfs41_check_expired_locks(struct nfs4_state *state)
5951 int status, ret = -NFS4ERR_BAD_STATEID;
5952 struct nfs4_lock_state *lsp;
5953 struct nfs_server *server = NFS_SERVER(state->inode);
5955 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
5956 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
5957 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
5959 status = nfs41_test_stateid(server,
5962 trace_nfs4_test_lock_stateid(state, lsp, status);
5963 if (status != NFS_OK) {
5964 /* Free the stateid unless the server
5965 * informs us the stateid is unrecognized. */
5966 if (status != -NFS4ERR_BAD_STATEID)
5967 nfs41_free_stateid(server,
5970 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
5979 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5981 int status = NFS_OK;
5983 if (test_bit(LK_STATE_IN_USE, &state->flags))
5984 status = nfs41_check_expired_locks(state);
5985 if (status != NFS_OK)
5986 status = nfs4_lock_expired(state, request);
5991 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5993 struct nfs_inode *nfsi = NFS_I(state->inode);
5994 unsigned char fl_flags = request->fl_flags;
5995 int status = -ENOLCK;
5997 if ((fl_flags & FL_POSIX) &&
5998 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6000 /* Is this a delegated open? */
6001 status = nfs4_set_lock_state(state, request);
6004 request->fl_flags |= FL_ACCESS;
6005 status = do_vfs_lock(request->fl_file, request);
6008 down_read(&nfsi->rwsem);
6009 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
6010 /* Yes: cache locks! */
6011 /* ...but avoid races with delegation recall... */
6012 request->fl_flags = fl_flags & ~FL_SLEEP;
6013 status = do_vfs_lock(request->fl_file, request);
6014 up_read(&nfsi->rwsem);
6017 up_read(&nfsi->rwsem);
6018 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
6020 request->fl_flags = fl_flags;
6024 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6026 struct nfs4_exception exception = {
6028 .inode = state->inode,
6033 err = _nfs4_proc_setlk(state, cmd, request);
6034 trace_nfs4_set_lock(request, state, cmd, err);
6035 if (err == -NFS4ERR_DENIED)
6037 err = nfs4_handle_exception(NFS_SERVER(state->inode),
6039 } while (exception.retry);
6044 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6046 struct nfs_open_context *ctx;
6047 struct nfs4_state *state;
6048 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6051 /* verify open state */
6052 ctx = nfs_file_open_context(filp);
6055 if (request->fl_start < 0 || request->fl_end < 0)
6058 if (IS_GETLK(cmd)) {
6060 return nfs4_proc_getlk(state, F_GETLK, request);
6064 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6067 if (request->fl_type == F_UNLCK) {
6069 return nfs4_proc_unlck(state, cmd, request);
6076 * Don't rely on the VFS having checked the file open mode,
6077 * since it won't do this for flock() locks.
6079 switch (request->fl_type) {
6081 if (!(filp->f_mode & FMODE_READ))
6085 if (!(filp->f_mode & FMODE_WRITE))
6090 status = nfs4_proc_setlk(state, cmd, request);
6091 if ((status != -EAGAIN) || IS_SETLK(cmd))
6093 timeout = nfs4_set_lock_task_retry(timeout);
6094 status = -ERESTARTSYS;
6097 } while(status < 0);
6101 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
6103 struct nfs_server *server = NFS_SERVER(state->inode);
6106 err = nfs4_set_lock_state(state, fl);
6109 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
6110 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
6113 struct nfs_release_lockowner_data {
6114 struct nfs4_lock_state *lsp;
6115 struct nfs_server *server;
6116 struct nfs_release_lockowner_args args;
6117 struct nfs_release_lockowner_res res;
6118 unsigned long timestamp;
6121 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6123 struct nfs_release_lockowner_data *data = calldata;
6124 struct nfs_server *server = data->server;
6125 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
6126 &data->args.seq_args, &data->res.seq_res, task);
6127 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6128 data->timestamp = jiffies;
6131 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6133 struct nfs_release_lockowner_data *data = calldata;
6134 struct nfs_server *server = data->server;
6136 nfs40_sequence_done(task, &data->res.seq_res);
6138 switch (task->tk_status) {
6140 renew_lease(server, data->timestamp);
6142 case -NFS4ERR_STALE_CLIENTID:
6143 case -NFS4ERR_EXPIRED:
6144 nfs4_schedule_lease_recovery(server->nfs_client);
6146 case -NFS4ERR_LEASE_MOVED:
6147 case -NFS4ERR_DELAY:
6148 if (nfs4_async_handle_error(task, server,
6149 NULL, NULL) == -EAGAIN)
6150 rpc_restart_call_prepare(task);
6154 static void nfs4_release_lockowner_release(void *calldata)
6156 struct nfs_release_lockowner_data *data = calldata;
6157 nfs4_free_lock_state(data->server, data->lsp);
6161 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
6162 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6163 .rpc_call_done = nfs4_release_lockowner_done,
6164 .rpc_release = nfs4_release_lockowner_release,
6168 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
6170 struct nfs_release_lockowner_data *data;
6171 struct rpc_message msg = {
6172 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6175 if (server->nfs_client->cl_mvops->minor_version != 0)
6178 data = kmalloc(sizeof(*data), GFP_NOFS);
6182 data->server = server;
6183 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6184 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6185 data->args.lock_owner.s_dev = server->s_dev;
6187 msg.rpc_argp = &data->args;
6188 msg.rpc_resp = &data->res;
6189 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
6190 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
6193 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6195 static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
6196 const void *buf, size_t buflen,
6197 int flags, int type)
6199 if (strcmp(key, "") != 0)
6202 return nfs4_proc_set_acl(d_inode(dentry), buf, buflen);
6205 static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
6206 void *buf, size_t buflen, int type)
6208 if (strcmp(key, "") != 0)
6211 return nfs4_proc_get_acl(d_inode(dentry), buf, buflen);
6214 static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
6215 size_t list_len, const char *name,
6216 size_t name_len, int type)
6218 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
6220 if (!nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry))))
6223 if (list && len <= list_len)
6224 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
6228 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
6229 static inline int nfs4_server_supports_labels(struct nfs_server *server)
6231 return server->caps & NFS_CAP_SECURITY_LABEL;
6234 static int nfs4_xattr_set_nfs4_label(struct dentry *dentry, const char *key,
6235 const void *buf, size_t buflen,
6236 int flags, int type)
6238 if (security_ismaclabel(key))
6239 return nfs4_set_security_label(dentry, buf, buflen);
6244 static int nfs4_xattr_get_nfs4_label(struct dentry *dentry, const char *key,
6245 void *buf, size_t buflen, int type)
6247 if (security_ismaclabel(key))
6248 return nfs4_get_security_label(d_inode(dentry), buf, buflen);
6252 static size_t nfs4_xattr_list_nfs4_label(struct dentry *dentry, char *list,
6253 size_t list_len, const char *name,
6254 size_t name_len, int type)
6258 if (nfs_server_capable(d_inode(dentry), NFS_CAP_SECURITY_LABEL)) {
6259 len = security_inode_listsecurity(d_inode(dentry), NULL, 0);
6260 if (list && len <= list_len)
6261 security_inode_listsecurity(d_inode(dentry), list, len);
6266 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6267 .prefix = XATTR_SECURITY_PREFIX,
6268 .list = nfs4_xattr_list_nfs4_label,
6269 .get = nfs4_xattr_get_nfs4_label,
6270 .set = nfs4_xattr_set_nfs4_label,
6276 * nfs_fhget will use either the mounted_on_fileid or the fileid
6278 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6280 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6281 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6282 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
6283 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
6286 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
6287 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
6288 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6292 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6293 const struct qstr *name,
6294 struct nfs4_fs_locations *fs_locations,
6297 struct nfs_server *server = NFS_SERVER(dir);
6299 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6301 struct nfs4_fs_locations_arg args = {
6302 .dir_fh = NFS_FH(dir),
6307 struct nfs4_fs_locations_res res = {
6308 .fs_locations = fs_locations,
6310 struct rpc_message msg = {
6311 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6317 dprintk("%s: start\n", __func__);
6319 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6320 * is not supported */
6321 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6322 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6324 bitmask[0] |= FATTR4_WORD0_FILEID;
6326 nfs_fattr_init(&fs_locations->fattr);
6327 fs_locations->server = server;
6328 fs_locations->nlocations = 0;
6329 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
6330 dprintk("%s: returned status = %d\n", __func__, status);
6334 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6335 const struct qstr *name,
6336 struct nfs4_fs_locations *fs_locations,
6339 struct nfs4_exception exception = { };
6342 err = _nfs4_proc_fs_locations(client, dir, name,
6343 fs_locations, page);
6344 trace_nfs4_get_fs_locations(dir, name, err);
6345 err = nfs4_handle_exception(NFS_SERVER(dir), err,
6347 } while (exception.retry);
6352 * This operation also signals the server that this client is
6353 * performing migration recovery. The server can stop returning
6354 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6355 * appended to this compound to identify the client ID which is
6356 * performing recovery.
6358 static int _nfs40_proc_get_locations(struct inode *inode,
6359 struct nfs4_fs_locations *locations,
6360 struct page *page, struct rpc_cred *cred)
6362 struct nfs_server *server = NFS_SERVER(inode);
6363 struct rpc_clnt *clnt = server->client;
6365 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6367 struct nfs4_fs_locations_arg args = {
6368 .clientid = server->nfs_client->cl_clientid,
6369 .fh = NFS_FH(inode),
6372 .migration = 1, /* skip LOOKUP */
6373 .renew = 1, /* append RENEW */
6375 struct nfs4_fs_locations_res res = {
6376 .fs_locations = locations,
6380 struct rpc_message msg = {
6381 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6386 unsigned long now = jiffies;
6389 nfs_fattr_init(&locations->fattr);
6390 locations->server = server;
6391 locations->nlocations = 0;
6393 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6394 nfs4_set_sequence_privileged(&args.seq_args);
6395 status = nfs4_call_sync_sequence(clnt, server, &msg,
6396 &args.seq_args, &res.seq_res);
6400 renew_lease(server, now);
6404 #ifdef CONFIG_NFS_V4_1
6407 * This operation also signals the server that this client is
6408 * performing migration recovery. The server can stop asserting
6409 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6410 * performing this operation is identified in the SEQUENCE
6411 * operation in this compound.
6413 * When the client supports GETATTR(fs_locations_info), it can
6414 * be plumbed in here.
6416 static int _nfs41_proc_get_locations(struct inode *inode,
6417 struct nfs4_fs_locations *locations,
6418 struct page *page, struct rpc_cred *cred)
6420 struct nfs_server *server = NFS_SERVER(inode);
6421 struct rpc_clnt *clnt = server->client;
6423 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6425 struct nfs4_fs_locations_arg args = {
6426 .fh = NFS_FH(inode),
6429 .migration = 1, /* skip LOOKUP */
6431 struct nfs4_fs_locations_res res = {
6432 .fs_locations = locations,
6435 struct rpc_message msg = {
6436 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6443 nfs_fattr_init(&locations->fattr);
6444 locations->server = server;
6445 locations->nlocations = 0;
6447 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6448 nfs4_set_sequence_privileged(&args.seq_args);
6449 status = nfs4_call_sync_sequence(clnt, server, &msg,
6450 &args.seq_args, &res.seq_res);
6451 if (status == NFS4_OK &&
6452 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6453 status = -NFS4ERR_LEASE_MOVED;
6457 #endif /* CONFIG_NFS_V4_1 */
6460 * nfs4_proc_get_locations - discover locations for a migrated FSID
6461 * @inode: inode on FSID that is migrating
6462 * @locations: result of query
6464 * @cred: credential to use for this operation
6466 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6467 * operation failed, or a negative errno if a local error occurred.
6469 * On success, "locations" is filled in, but if the server has
6470 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6473 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6474 * from this client that require migration recovery.
6476 int nfs4_proc_get_locations(struct inode *inode,
6477 struct nfs4_fs_locations *locations,
6478 struct page *page, struct rpc_cred *cred)
6480 struct nfs_server *server = NFS_SERVER(inode);
6481 struct nfs_client *clp = server->nfs_client;
6482 const struct nfs4_mig_recovery_ops *ops =
6483 clp->cl_mvops->mig_recovery_ops;
6484 struct nfs4_exception exception = { };
6487 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6488 (unsigned long long)server->fsid.major,
6489 (unsigned long long)server->fsid.minor,
6491 nfs_display_fhandle(NFS_FH(inode), __func__);
6494 status = ops->get_locations(inode, locations, page, cred);
6495 if (status != -NFS4ERR_DELAY)
6497 nfs4_handle_exception(server, status, &exception);
6498 } while (exception.retry);
6503 * This operation also signals the server that this client is
6504 * performing "lease moved" recovery. The server can stop
6505 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6506 * is appended to this compound to identify the client ID which is
6507 * performing recovery.
6509 static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6511 struct nfs_server *server = NFS_SERVER(inode);
6512 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6513 struct rpc_clnt *clnt = server->client;
6514 struct nfs4_fsid_present_arg args = {
6515 .fh = NFS_FH(inode),
6516 .clientid = clp->cl_clientid,
6517 .renew = 1, /* append RENEW */
6519 struct nfs4_fsid_present_res res = {
6522 struct rpc_message msg = {
6523 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6528 unsigned long now = jiffies;
6531 res.fh = nfs_alloc_fhandle();
6535 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6536 nfs4_set_sequence_privileged(&args.seq_args);
6537 status = nfs4_call_sync_sequence(clnt, server, &msg,
6538 &args.seq_args, &res.seq_res);
6539 nfs_free_fhandle(res.fh);
6543 do_renew_lease(clp, now);
6547 #ifdef CONFIG_NFS_V4_1
6550 * This operation also signals the server that this client is
6551 * performing "lease moved" recovery. The server can stop asserting
6552 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6553 * this operation is identified in the SEQUENCE operation in this
6556 static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6558 struct nfs_server *server = NFS_SERVER(inode);
6559 struct rpc_clnt *clnt = server->client;
6560 struct nfs4_fsid_present_arg args = {
6561 .fh = NFS_FH(inode),
6563 struct nfs4_fsid_present_res res = {
6565 struct rpc_message msg = {
6566 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6573 res.fh = nfs_alloc_fhandle();
6577 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6578 nfs4_set_sequence_privileged(&args.seq_args);
6579 status = nfs4_call_sync_sequence(clnt, server, &msg,
6580 &args.seq_args, &res.seq_res);
6581 nfs_free_fhandle(res.fh);
6582 if (status == NFS4_OK &&
6583 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6584 status = -NFS4ERR_LEASE_MOVED;
6588 #endif /* CONFIG_NFS_V4_1 */
6591 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6592 * @inode: inode on FSID to check
6593 * @cred: credential to use for this operation
6595 * Server indicates whether the FSID is present, moved, or not
6596 * recognized. This operation is necessary to clear a LEASE_MOVED
6597 * condition for this client ID.
6599 * Returns NFS4_OK if the FSID is present on this server,
6600 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6601 * NFS4ERR code if some error occurred on the server, or a
6602 * negative errno if a local failure occurred.
6604 int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6606 struct nfs_server *server = NFS_SERVER(inode);
6607 struct nfs_client *clp = server->nfs_client;
6608 const struct nfs4_mig_recovery_ops *ops =
6609 clp->cl_mvops->mig_recovery_ops;
6610 struct nfs4_exception exception = { };
6613 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6614 (unsigned long long)server->fsid.major,
6615 (unsigned long long)server->fsid.minor,
6617 nfs_display_fhandle(NFS_FH(inode), __func__);
6620 status = ops->fsid_present(inode, cred);
6621 if (status != -NFS4ERR_DELAY)
6623 nfs4_handle_exception(server, status, &exception);
6624 } while (exception.retry);
6629 * If 'use_integrity' is true and the state managment nfs_client
6630 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
6631 * and the machine credential as per RFC3530bis and RFC5661 Security
6632 * Considerations sections. Otherwise, just use the user cred with the
6633 * filesystem's rpc_client.
6635 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
6638 struct nfs4_secinfo_arg args = {
6639 .dir_fh = NFS_FH(dir),
6642 struct nfs4_secinfo_res res = {
6645 struct rpc_message msg = {
6646 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
6650 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
6651 struct rpc_cred *cred = NULL;
6653 if (use_integrity) {
6654 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
6655 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
6656 msg.rpc_cred = cred;
6659 dprintk("NFS call secinfo %s\n", name->name);
6661 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
6662 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
6664 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
6666 dprintk("NFS reply secinfo: %d\n", status);
6674 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
6675 struct nfs4_secinfo_flavors *flavors)
6677 struct nfs4_exception exception = { };
6680 err = -NFS4ERR_WRONGSEC;
6682 /* try to use integrity protection with machine cred */
6683 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
6684 err = _nfs4_proc_secinfo(dir, name, flavors, true);
6687 * if unable to use integrity protection, or SECINFO with
6688 * integrity protection returns NFS4ERR_WRONGSEC (which is
6689 * disallowed by spec, but exists in deployed servers) use
6690 * the current filesystem's rpc_client and the user cred.
6692 if (err == -NFS4ERR_WRONGSEC)
6693 err = _nfs4_proc_secinfo(dir, name, flavors, false);
6695 trace_nfs4_secinfo(dir, name, err);
6696 err = nfs4_handle_exception(NFS_SERVER(dir), err,
6698 } while (exception.retry);
6702 #ifdef CONFIG_NFS_V4_1
6704 * Check the exchange flags returned by the server for invalid flags, having
6705 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
6708 static int nfs4_check_cl_exchange_flags(u32 flags)
6710 if (flags & ~EXCHGID4_FLAG_MASK_R)
6712 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
6713 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
6715 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
6719 return -NFS4ERR_INVAL;
6723 nfs41_same_server_scope(struct nfs41_server_scope *a,
6724 struct nfs41_server_scope *b)
6726 if (a->server_scope_sz == b->server_scope_sz &&
6727 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
6734 * nfs4_proc_bind_conn_to_session()
6736 * The 4.1 client currently uses the same TCP connection for the
6737 * fore and backchannel.
6739 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
6742 struct nfs41_bind_conn_to_session_args args = {
6744 .dir = NFS4_CDFC4_FORE_OR_BOTH,
6746 struct nfs41_bind_conn_to_session_res res;
6747 struct rpc_message msg = {
6749 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
6755 dprintk("--> %s\n", __func__);
6757 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
6758 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
6759 args.dir = NFS4_CDFC4_FORE;
6761 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
6762 trace_nfs4_bind_conn_to_session(clp, status);
6764 if (memcmp(res.sessionid.data,
6765 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
6766 dprintk("NFS: %s: Session ID mismatch\n", __func__);
6770 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
6771 dprintk("NFS: %s: Unexpected direction from server\n",
6776 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
6777 dprintk("NFS: %s: Server returned RDMA mode = true\n",
6784 dprintk("<-- %s status= %d\n", __func__, status);
6789 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
6790 * and operations we'd like to see to enable certain features in the allow map
6792 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
6793 .how = SP4_MACH_CRED,
6794 .enforce.u.words = {
6795 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6796 1 << (OP_EXCHANGE_ID - 32) |
6797 1 << (OP_CREATE_SESSION - 32) |
6798 1 << (OP_DESTROY_SESSION - 32) |
6799 1 << (OP_DESTROY_CLIENTID - 32)
6802 [0] = 1 << (OP_CLOSE) |
6805 [1] = 1 << (OP_SECINFO - 32) |
6806 1 << (OP_SECINFO_NO_NAME - 32) |
6807 1 << (OP_TEST_STATEID - 32) |
6808 1 << (OP_FREE_STATEID - 32) |
6809 1 << (OP_WRITE - 32)
6814 * Select the state protection mode for client `clp' given the server results
6815 * from exchange_id in `sp'.
6817 * Returns 0 on success, negative errno otherwise.
6819 static int nfs4_sp4_select_mode(struct nfs_client *clp,
6820 struct nfs41_state_protection *sp)
6822 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
6823 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6824 1 << (OP_EXCHANGE_ID - 32) |
6825 1 << (OP_CREATE_SESSION - 32) |
6826 1 << (OP_DESTROY_SESSION - 32) |
6827 1 << (OP_DESTROY_CLIENTID - 32)
6831 if (sp->how == SP4_MACH_CRED) {
6832 /* Print state protect result */
6833 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
6834 for (i = 0; i <= LAST_NFS4_OP; i++) {
6835 if (test_bit(i, sp->enforce.u.longs))
6836 dfprintk(MOUNT, " enforce op %d\n", i);
6837 if (test_bit(i, sp->allow.u.longs))
6838 dfprintk(MOUNT, " allow op %d\n", i);
6841 /* make sure nothing is on enforce list that isn't supported */
6842 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
6843 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
6844 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6850 * Minimal mode - state operations are allowed to use machine
6851 * credential. Note this already happens by default, so the
6852 * client doesn't have to do anything more than the negotiation.
6854 * NOTE: we don't care if EXCHANGE_ID is in the list -
6855 * we're already using the machine cred for exchange_id
6856 * and will never use a different cred.
6858 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
6859 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
6860 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
6861 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
6862 dfprintk(MOUNT, "sp4_mach_cred:\n");
6863 dfprintk(MOUNT, " minimal mode enabled\n");
6864 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
6866 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6870 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
6871 test_bit(OP_LOCKU, sp->allow.u.longs)) {
6872 dfprintk(MOUNT, " cleanup mode enabled\n");
6873 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
6876 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
6877 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
6878 dfprintk(MOUNT, " secinfo mode enabled\n");
6879 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
6882 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
6883 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
6884 dfprintk(MOUNT, " stateid mode enabled\n");
6885 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
6888 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
6889 dfprintk(MOUNT, " write mode enabled\n");
6890 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
6893 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
6894 dfprintk(MOUNT, " commit mode enabled\n");
6895 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
6903 * _nfs4_proc_exchange_id()
6905 * Wrapper for EXCHANGE_ID operation.
6907 static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
6910 nfs4_verifier verifier;
6911 struct nfs41_exchange_id_args args = {
6912 .verifier = &verifier,
6914 #ifdef CONFIG_NFS_V4_1_MIGRATION
6915 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
6916 EXCHGID4_FLAG_BIND_PRINC_STATEID |
6917 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
6919 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
6920 EXCHGID4_FLAG_BIND_PRINC_STATEID,
6923 struct nfs41_exchange_id_res res = {
6927 struct rpc_message msg = {
6928 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
6934 nfs4_init_boot_verifier(clp, &verifier);
6936 status = nfs4_init_uniform_client_string(clp);
6940 dprintk("NFS call exchange_id auth=%s, '%s'\n",
6941 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6944 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
6946 if (unlikely(res.server_owner == NULL)) {
6951 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
6953 if (unlikely(res.server_scope == NULL)) {
6955 goto out_server_owner;
6958 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
6959 if (unlikely(res.impl_id == NULL)) {
6961 goto out_server_scope;
6966 args.state_protect.how = SP4_NONE;
6970 args.state_protect = nfs4_sp4_mach_cred_request;
6980 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
6981 trace_nfs4_exchange_id(clp, status);
6983 status = nfs4_check_cl_exchange_flags(res.flags);
6986 status = nfs4_sp4_select_mode(clp, &res.state_protect);
6989 clp->cl_clientid = res.clientid;
6990 clp->cl_exchange_flags = res.flags;
6991 /* Client ID is not confirmed */
6992 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
6993 clear_bit(NFS4_SESSION_ESTABLISHED,
6994 &clp->cl_session->session_state);
6995 clp->cl_seqid = res.seqid;
6998 kfree(clp->cl_serverowner);
6999 clp->cl_serverowner = res.server_owner;
7000 res.server_owner = NULL;
7002 /* use the most recent implementation id */
7003 kfree(clp->cl_implid);
7004 clp->cl_implid = res.impl_id;
7007 if (clp->cl_serverscope != NULL &&
7008 !nfs41_same_server_scope(clp->cl_serverscope,
7009 res.server_scope)) {
7010 dprintk("%s: server_scope mismatch detected\n",
7012 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
7013 kfree(clp->cl_serverscope);
7014 clp->cl_serverscope = NULL;
7017 if (clp->cl_serverscope == NULL) {
7018 clp->cl_serverscope = res.server_scope;
7019 res.server_scope = NULL;
7026 kfree(res.server_scope);
7028 kfree(res.server_owner);
7030 if (clp->cl_implid != NULL)
7031 dprintk("NFS reply exchange_id: Server Implementation ID: "
7032 "domain: %s, name: %s, date: %llu,%u\n",
7033 clp->cl_implid->domain, clp->cl_implid->name,
7034 clp->cl_implid->date.seconds,
7035 clp->cl_implid->date.nseconds);
7036 dprintk("NFS reply exchange_id: %d\n", status);
7041 * nfs4_proc_exchange_id()
7043 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7045 * Since the clientid has expired, all compounds using sessions
7046 * associated with the stale clientid will be returning
7047 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7048 * be in some phase of session reset.
7050 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7052 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7054 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7057 /* try SP4_MACH_CRED if krb5i/p */
7058 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7059 authflavor == RPC_AUTH_GSS_KRB5P) {
7060 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
7066 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
7069 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7070 struct rpc_cred *cred)
7072 struct rpc_message msg = {
7073 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7079 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7080 trace_nfs4_destroy_clientid(clp, status);
7082 dprintk("NFS: Got error %d from the server %s on "
7083 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7087 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7088 struct rpc_cred *cred)
7093 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7094 ret = _nfs4_proc_destroy_clientid(clp, cred);
7096 case -NFS4ERR_DELAY:
7097 case -NFS4ERR_CLIENTID_BUSY:
7107 int nfs4_destroy_clientid(struct nfs_client *clp)
7109 struct rpc_cred *cred;
7112 if (clp->cl_mvops->minor_version < 1)
7114 if (clp->cl_exchange_flags == 0)
7116 if (clp->cl_preserve_clid)
7118 cred = nfs4_get_clid_cred(clp);
7119 ret = nfs4_proc_destroy_clientid(clp, cred);
7124 case -NFS4ERR_STALE_CLIENTID:
7125 clp->cl_exchange_flags = 0;
7131 struct nfs4_get_lease_time_data {
7132 struct nfs4_get_lease_time_args *args;
7133 struct nfs4_get_lease_time_res *res;
7134 struct nfs_client *clp;
7137 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7140 struct nfs4_get_lease_time_data *data =
7141 (struct nfs4_get_lease_time_data *)calldata;
7143 dprintk("--> %s\n", __func__);
7144 /* just setup sequence, do not trigger session recovery
7145 since we're invoked within one */
7146 nfs41_setup_sequence(data->clp->cl_session,
7147 &data->args->la_seq_args,
7148 &data->res->lr_seq_res,
7150 dprintk("<-- %s\n", __func__);
7154 * Called from nfs4_state_manager thread for session setup, so don't recover
7155 * from sequence operation or clientid errors.
7157 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7159 struct nfs4_get_lease_time_data *data =
7160 (struct nfs4_get_lease_time_data *)calldata;
7162 dprintk("--> %s\n", __func__);
7163 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7165 switch (task->tk_status) {
7166 case -NFS4ERR_DELAY:
7167 case -NFS4ERR_GRACE:
7168 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7169 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7170 task->tk_status = 0;
7172 case -NFS4ERR_RETRY_UNCACHED_REP:
7173 rpc_restart_call_prepare(task);
7176 dprintk("<-- %s\n", __func__);
7179 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
7180 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7181 .rpc_call_done = nfs4_get_lease_time_done,
7184 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7186 struct rpc_task *task;
7187 struct nfs4_get_lease_time_args args;
7188 struct nfs4_get_lease_time_res res = {
7189 .lr_fsinfo = fsinfo,
7191 struct nfs4_get_lease_time_data data = {
7196 struct rpc_message msg = {
7197 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7201 struct rpc_task_setup task_setup = {
7202 .rpc_client = clp->cl_rpcclient,
7203 .rpc_message = &msg,
7204 .callback_ops = &nfs4_get_lease_time_ops,
7205 .callback_data = &data,
7206 .flags = RPC_TASK_TIMEOUT,
7210 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
7211 nfs4_set_sequence_privileged(&args.la_seq_args);
7212 dprintk("--> %s\n", __func__);
7213 task = rpc_run_task(&task_setup);
7216 status = PTR_ERR(task);
7218 status = task->tk_status;
7221 dprintk("<-- %s return %d\n", __func__, status);
7227 * Initialize the values to be used by the client in CREATE_SESSION
7228 * If nfs4_init_session set the fore channel request and response sizes,
7231 * Set the back channel max_resp_sz_cached to zero to force the client to
7232 * always set csa_cachethis to FALSE because the current implementation
7233 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7235 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
7237 unsigned int max_rqst_sz, max_resp_sz;
7239 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7240 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7242 /* Fore channel attributes */
7243 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7244 args->fc_attrs.max_resp_sz = max_resp_sz;
7245 args->fc_attrs.max_ops = NFS4_MAX_OPS;
7246 args->fc_attrs.max_reqs = max_session_slots;
7248 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
7249 "max_ops=%u max_reqs=%u\n",
7251 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
7252 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
7254 /* Back channel attributes */
7255 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
7256 args->bc_attrs.max_resp_sz = PAGE_SIZE;
7257 args->bc_attrs.max_resp_sz_cached = 0;
7258 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
7259 args->bc_attrs.max_reqs = 1;
7261 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7262 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7264 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7265 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7266 args->bc_attrs.max_reqs);
7269 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7270 struct nfs41_create_session_res *res)
7272 struct nfs4_channel_attrs *sent = &args->fc_attrs;
7273 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
7275 if (rcvd->max_resp_sz > sent->max_resp_sz)
7278 * Our requested max_ops is the minimum we need; we're not
7279 * prepared to break up compounds into smaller pieces than that.
7280 * So, no point even trying to continue if the server won't
7283 if (rcvd->max_ops < sent->max_ops)
7285 if (rcvd->max_reqs == 0)
7287 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7288 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
7292 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7293 struct nfs41_create_session_res *res)
7295 struct nfs4_channel_attrs *sent = &args->bc_attrs;
7296 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
7298 if (!(res->flags & SESSION4_BACK_CHAN))
7300 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7302 if (rcvd->max_resp_sz < sent->max_resp_sz)
7304 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7306 /* These would render the backchannel useless: */
7307 if (rcvd->max_ops != sent->max_ops)
7309 if (rcvd->max_reqs != sent->max_reqs)
7315 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
7316 struct nfs41_create_session_res *res)
7320 ret = nfs4_verify_fore_channel_attrs(args, res);
7323 return nfs4_verify_back_channel_attrs(args, res);
7326 static void nfs4_update_session(struct nfs4_session *session,
7327 struct nfs41_create_session_res *res)
7329 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
7330 /* Mark client id and session as being confirmed */
7331 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7332 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
7333 session->flags = res->flags;
7334 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
7335 if (res->flags & SESSION4_BACK_CHAN)
7336 memcpy(&session->bc_attrs, &res->bc_attrs,
7337 sizeof(session->bc_attrs));
7340 static int _nfs4_proc_create_session(struct nfs_client *clp,
7341 struct rpc_cred *cred)
7343 struct nfs4_session *session = clp->cl_session;
7344 struct nfs41_create_session_args args = {
7346 .clientid = clp->cl_clientid,
7347 .seqid = clp->cl_seqid,
7348 .cb_program = NFS4_CALLBACK,
7350 struct nfs41_create_session_res res;
7352 struct rpc_message msg = {
7353 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7360 nfs4_init_channel_attrs(&args);
7361 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
7363 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7364 trace_nfs4_create_session(clp, status);
7367 /* Verify the session's negotiated channel_attrs values */
7368 status = nfs4_verify_channel_attrs(&args, &res);
7369 /* Increment the clientid slot sequence id */
7370 if (clp->cl_seqid == res.seqid)
7374 nfs4_update_session(session, &res);
7381 * Issues a CREATE_SESSION operation to the server.
7382 * It is the responsibility of the caller to verify the session is
7383 * expired before calling this routine.
7385 int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
7389 struct nfs4_session *session = clp->cl_session;
7391 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7393 status = _nfs4_proc_create_session(clp, cred);
7397 /* Init or reset the session slot tables */
7398 status = nfs4_setup_session_slot_tables(session);
7399 dprintk("slot table setup returned %d\n", status);
7403 ptr = (unsigned *)&session->sess_id.data[0];
7404 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7405 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
7407 dprintk("<-- %s\n", __func__);
7412 * Issue the over-the-wire RPC DESTROY_SESSION.
7413 * The caller must serialize access to this routine.
7415 int nfs4_proc_destroy_session(struct nfs4_session *session,
7416 struct rpc_cred *cred)
7418 struct rpc_message msg = {
7419 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
7420 .rpc_argp = session,
7425 dprintk("--> nfs4_proc_destroy_session\n");
7427 /* session is still being setup */
7428 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
7431 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7432 trace_nfs4_destroy_session(session->clp, status);
7435 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
7436 "Session has been destroyed regardless...\n", status);
7438 dprintk("<-- nfs4_proc_destroy_session\n");
7443 * Renew the cl_session lease.
7445 struct nfs4_sequence_data {
7446 struct nfs_client *clp;
7447 struct nfs4_sequence_args args;
7448 struct nfs4_sequence_res res;
7451 static void nfs41_sequence_release(void *data)
7453 struct nfs4_sequence_data *calldata = data;
7454 struct nfs_client *clp = calldata->clp;
7456 if (atomic_read(&clp->cl_count) > 1)
7457 nfs4_schedule_state_renewal(clp);
7458 nfs_put_client(clp);
7462 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7464 switch(task->tk_status) {
7465 case -NFS4ERR_DELAY:
7466 rpc_delay(task, NFS4_POLL_RETRY_MAX);
7469 nfs4_schedule_lease_recovery(clp);
7474 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
7476 struct nfs4_sequence_data *calldata = data;
7477 struct nfs_client *clp = calldata->clp;
7479 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
7482 trace_nfs4_sequence(clp, task->tk_status);
7483 if (task->tk_status < 0) {
7484 dprintk("%s ERROR %d\n", __func__, task->tk_status);
7485 if (atomic_read(&clp->cl_count) == 1)
7488 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
7489 rpc_restart_call_prepare(task);
7493 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
7495 dprintk("<-- %s\n", __func__);
7498 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
7500 struct nfs4_sequence_data *calldata = data;
7501 struct nfs_client *clp = calldata->clp;
7502 struct nfs4_sequence_args *args;
7503 struct nfs4_sequence_res *res;
7505 args = task->tk_msg.rpc_argp;
7506 res = task->tk_msg.rpc_resp;
7508 nfs41_setup_sequence(clp->cl_session, args, res, task);
7511 static const struct rpc_call_ops nfs41_sequence_ops = {
7512 .rpc_call_done = nfs41_sequence_call_done,
7513 .rpc_call_prepare = nfs41_sequence_prepare,
7514 .rpc_release = nfs41_sequence_release,
7517 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
7518 struct rpc_cred *cred,
7521 struct nfs4_sequence_data *calldata;
7522 struct rpc_message msg = {
7523 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
7526 struct rpc_task_setup task_setup_data = {
7527 .rpc_client = clp->cl_rpcclient,
7528 .rpc_message = &msg,
7529 .callback_ops = &nfs41_sequence_ops,
7530 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7533 if (!atomic_inc_not_zero(&clp->cl_count))
7534 return ERR_PTR(-EIO);
7535 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7536 if (calldata == NULL) {
7537 nfs_put_client(clp);
7538 return ERR_PTR(-ENOMEM);
7540 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
7542 nfs4_set_sequence_privileged(&calldata->args);
7543 msg.rpc_argp = &calldata->args;
7544 msg.rpc_resp = &calldata->res;
7545 calldata->clp = clp;
7546 task_setup_data.callback_data = calldata;
7548 return rpc_run_task(&task_setup_data);
7551 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
7553 struct rpc_task *task;
7556 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
7558 task = _nfs41_proc_sequence(clp, cred, false);
7560 ret = PTR_ERR(task);
7562 rpc_put_task_async(task);
7563 dprintk("<-- %s status=%d\n", __func__, ret);
7567 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
7569 struct rpc_task *task;
7572 task = _nfs41_proc_sequence(clp, cred, true);
7574 ret = PTR_ERR(task);
7577 ret = rpc_wait_for_completion_task(task);
7579 ret = task->tk_status;
7582 dprintk("<-- %s status=%d\n", __func__, ret);
7586 struct nfs4_reclaim_complete_data {
7587 struct nfs_client *clp;
7588 struct nfs41_reclaim_complete_args arg;
7589 struct nfs41_reclaim_complete_res res;
7592 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
7594 struct nfs4_reclaim_complete_data *calldata = data;
7596 nfs41_setup_sequence(calldata->clp->cl_session,
7597 &calldata->arg.seq_args,
7598 &calldata->res.seq_res,
7602 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7604 switch(task->tk_status) {
7606 case -NFS4ERR_COMPLETE_ALREADY:
7607 case -NFS4ERR_WRONG_CRED: /* What to do here? */
7609 case -NFS4ERR_DELAY:
7610 rpc_delay(task, NFS4_POLL_RETRY_MAX);
7612 case -NFS4ERR_RETRY_UNCACHED_REP:
7615 nfs4_schedule_lease_recovery(clp);
7620 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
7622 struct nfs4_reclaim_complete_data *calldata = data;
7623 struct nfs_client *clp = calldata->clp;
7624 struct nfs4_sequence_res *res = &calldata->res.seq_res;
7626 dprintk("--> %s\n", __func__);
7627 if (!nfs41_sequence_done(task, res))
7630 trace_nfs4_reclaim_complete(clp, task->tk_status);
7631 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
7632 rpc_restart_call_prepare(task);
7635 dprintk("<-- %s\n", __func__);
7638 static void nfs4_free_reclaim_complete_data(void *data)
7640 struct nfs4_reclaim_complete_data *calldata = data;
7645 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
7646 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
7647 .rpc_call_done = nfs4_reclaim_complete_done,
7648 .rpc_release = nfs4_free_reclaim_complete_data,
7652 * Issue a global reclaim complete.
7654 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
7655 struct rpc_cred *cred)
7657 struct nfs4_reclaim_complete_data *calldata;
7658 struct rpc_task *task;
7659 struct rpc_message msg = {
7660 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
7663 struct rpc_task_setup task_setup_data = {
7664 .rpc_client = clp->cl_rpcclient,
7665 .rpc_message = &msg,
7666 .callback_ops = &nfs4_reclaim_complete_call_ops,
7667 .flags = RPC_TASK_ASYNC,
7669 int status = -ENOMEM;
7671 dprintk("--> %s\n", __func__);
7672 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7673 if (calldata == NULL)
7675 calldata->clp = clp;
7676 calldata->arg.one_fs = 0;
7678 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
7679 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
7680 msg.rpc_argp = &calldata->arg;
7681 msg.rpc_resp = &calldata->res;
7682 task_setup_data.callback_data = calldata;
7683 task = rpc_run_task(&task_setup_data);
7685 status = PTR_ERR(task);
7688 status = nfs4_wait_for_completion_rpc_task(task);
7690 status = task->tk_status;
7694 dprintk("<-- %s status=%d\n", __func__, status);
7699 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
7701 struct nfs4_layoutget *lgp = calldata;
7702 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
7703 struct nfs4_session *session = nfs4_get_session(server);
7705 dprintk("--> %s\n", __func__);
7706 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
7707 * right now covering the LAYOUTGET we are about to send.
7708 * However, that is not so catastrophic, and there seems
7709 * to be no way to prevent it completely.
7711 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
7712 &lgp->res.seq_res, task))
7714 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
7715 NFS_I(lgp->args.inode)->layout,
7717 lgp->args.ctx->state)) {
7718 rpc_exit(task, NFS4_OK);
7722 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
7724 struct nfs4_layoutget *lgp = calldata;
7725 struct inode *inode = lgp->args.inode;
7726 struct nfs_server *server = NFS_SERVER(inode);
7727 struct pnfs_layout_hdr *lo;
7728 struct nfs4_state *state = NULL;
7729 unsigned long timeo, now, giveup;
7731 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
7733 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
7736 switch (task->tk_status) {
7740 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
7741 * (or clients) writing to the same RAID stripe
7743 case -NFS4ERR_LAYOUTTRYLATER:
7745 * NFS4ERR_RECALLCONFLICT is when conflict with self (must recall
7746 * existing layout before getting a new one).
7748 case -NFS4ERR_RECALLCONFLICT:
7749 timeo = rpc_get_timeout(task->tk_client);
7750 giveup = lgp->args.timestamp + timeo;
7752 if (time_after(giveup, now)) {
7753 unsigned long delay;
7756 * - Not less then NFS4_POLL_RETRY_MIN.
7757 * - One last time a jiffie before we give up
7758 * - exponential backoff (time_now minus start_attempt)
7760 delay = max_t(unsigned long, NFS4_POLL_RETRY_MIN,
7761 min((giveup - now - 1),
7762 now - lgp->args.timestamp));
7764 dprintk("%s: NFS4ERR_RECALLCONFLICT waiting %lu\n",
7766 rpc_delay(task, delay);
7767 task->tk_status = 0;
7768 rpc_restart_call_prepare(task);
7769 goto out; /* Do not call nfs4_async_handle_error() */
7772 case -NFS4ERR_EXPIRED:
7773 case -NFS4ERR_BAD_STATEID:
7774 spin_lock(&inode->i_lock);
7775 lo = NFS_I(inode)->layout;
7776 if (!lo || list_empty(&lo->plh_segs)) {
7777 spin_unlock(&inode->i_lock);
7778 /* If the open stateid was bad, then recover it. */
7779 state = lgp->args.ctx->state;
7784 * Mark the bad layout state as invalid, then retry
7785 * with the current stateid.
7787 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
7788 spin_unlock(&inode->i_lock);
7789 pnfs_free_lseg_list(&head);
7791 task->tk_status = 0;
7792 rpc_restart_call_prepare(task);
7795 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN)
7796 rpc_restart_call_prepare(task);
7798 dprintk("<-- %s\n", __func__);
7801 static size_t max_response_pages(struct nfs_server *server)
7803 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
7804 return nfs_page_array_len(0, max_resp_sz);
7807 static void nfs4_free_pages(struct page **pages, size_t size)
7814 for (i = 0; i < size; i++) {
7817 __free_page(pages[i]);
7822 static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
7824 struct page **pages;
7827 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
7829 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
7833 for (i = 0; i < size; i++) {
7834 pages[i] = alloc_page(gfp_flags);
7836 dprintk("%s: failed to allocate page\n", __func__);
7837 nfs4_free_pages(pages, size);
7845 static void nfs4_layoutget_release(void *calldata)
7847 struct nfs4_layoutget *lgp = calldata;
7848 struct inode *inode = lgp->args.inode;
7849 struct nfs_server *server = NFS_SERVER(inode);
7850 size_t max_pages = max_response_pages(server);
7852 dprintk("--> %s\n", __func__);
7853 nfs4_free_pages(lgp->args.layout.pages, max_pages);
7854 pnfs_put_layout_hdr(NFS_I(inode)->layout);
7855 put_nfs_open_context(lgp->args.ctx);
7857 dprintk("<-- %s\n", __func__);
7860 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
7861 .rpc_call_prepare = nfs4_layoutget_prepare,
7862 .rpc_call_done = nfs4_layoutget_done,
7863 .rpc_release = nfs4_layoutget_release,
7866 struct pnfs_layout_segment *
7867 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
7869 struct inode *inode = lgp->args.inode;
7870 struct nfs_server *server = NFS_SERVER(inode);
7871 size_t max_pages = max_response_pages(server);
7872 struct rpc_task *task;
7873 struct rpc_message msg = {
7874 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
7875 .rpc_argp = &lgp->args,
7876 .rpc_resp = &lgp->res,
7877 .rpc_cred = lgp->cred,
7879 struct rpc_task_setup task_setup_data = {
7880 .rpc_client = server->client,
7881 .rpc_message = &msg,
7882 .callback_ops = &nfs4_layoutget_call_ops,
7883 .callback_data = lgp,
7884 .flags = RPC_TASK_ASYNC,
7886 struct pnfs_layout_segment *lseg = NULL;
7889 dprintk("--> %s\n", __func__);
7891 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
7892 pnfs_get_layout_hdr(NFS_I(inode)->layout);
7894 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
7895 if (!lgp->args.layout.pages) {
7896 nfs4_layoutget_release(lgp);
7897 return ERR_PTR(-ENOMEM);
7899 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
7900 lgp->args.timestamp = jiffies;
7902 lgp->res.layoutp = &lgp->args.layout;
7903 lgp->res.seq_res.sr_slot = NULL;
7904 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
7906 task = rpc_run_task(&task_setup_data);
7908 return ERR_CAST(task);
7909 status = nfs4_wait_for_completion_rpc_task(task);
7911 status = task->tk_status;
7912 trace_nfs4_layoutget(lgp->args.ctx,
7916 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
7917 if (status == 0 && lgp->res.layoutp->len)
7918 lseg = pnfs_layout_process(lgp);
7920 dprintk("<-- %s status=%d\n", __func__, status);
7922 return ERR_PTR(status);
7927 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
7929 struct nfs4_layoutreturn *lrp = calldata;
7931 dprintk("--> %s\n", __func__);
7932 nfs41_setup_sequence(lrp->clp->cl_session,
7933 &lrp->args.seq_args,
7938 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
7940 struct nfs4_layoutreturn *lrp = calldata;
7941 struct nfs_server *server;
7943 dprintk("--> %s\n", __func__);
7945 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
7948 server = NFS_SERVER(lrp->args.inode);
7949 switch (task->tk_status) {
7951 task->tk_status = 0;
7954 case -NFS4ERR_DELAY:
7955 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
7957 rpc_restart_call_prepare(task);
7960 dprintk("<-- %s\n", __func__);
7963 static void nfs4_layoutreturn_release(void *calldata)
7965 struct nfs4_layoutreturn *lrp = calldata;
7966 struct pnfs_layout_hdr *lo = lrp->args.layout;
7969 dprintk("--> %s\n", __func__);
7970 spin_lock(&lo->plh_inode->i_lock);
7971 if (lrp->res.lrs_present)
7972 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
7973 pnfs_mark_matching_lsegs_invalid(lo, &freeme, &lrp->args.range);
7974 pnfs_clear_layoutreturn_waitbit(lo);
7975 clear_bit(NFS_LAYOUT_RETURN_BEFORE_CLOSE, &lo->plh_flags);
7976 rpc_wake_up(&NFS_SERVER(lo->plh_inode)->roc_rpcwaitq);
7977 lo->plh_block_lgets--;
7978 spin_unlock(&lo->plh_inode->i_lock);
7979 pnfs_free_lseg_list(&freeme);
7980 pnfs_put_layout_hdr(lrp->args.layout);
7981 nfs_iput_and_deactive(lrp->inode);
7983 dprintk("<-- %s\n", __func__);
7986 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
7987 .rpc_call_prepare = nfs4_layoutreturn_prepare,
7988 .rpc_call_done = nfs4_layoutreturn_done,
7989 .rpc_release = nfs4_layoutreturn_release,
7992 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
7994 struct rpc_task *task;
7995 struct rpc_message msg = {
7996 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
7997 .rpc_argp = &lrp->args,
7998 .rpc_resp = &lrp->res,
7999 .rpc_cred = lrp->cred,
8001 struct rpc_task_setup task_setup_data = {
8002 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
8003 .rpc_message = &msg,
8004 .callback_ops = &nfs4_layoutreturn_call_ops,
8005 .callback_data = lrp,
8009 dprintk("--> %s\n", __func__);
8011 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8013 nfs4_layoutreturn_release(lrp);
8016 task_setup_data.flags |= RPC_TASK_ASYNC;
8018 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
8019 task = rpc_run_task(&task_setup_data);
8021 return PTR_ERR(task);
8023 status = task->tk_status;
8024 trace_nfs4_layoutreturn(lrp->args.inode, status);
8025 dprintk("<-- %s status=%d\n", __func__, status);
8031 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
8032 struct pnfs_device *pdev,
8033 struct rpc_cred *cred)
8035 struct nfs4_getdeviceinfo_args args = {
8037 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8038 NOTIFY_DEVICEID4_DELETE,
8040 struct nfs4_getdeviceinfo_res res = {
8043 struct rpc_message msg = {
8044 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8051 dprintk("--> %s\n", __func__);
8052 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
8053 if (res.notification & ~args.notify_types)
8054 dprintk("%s: unsupported notification\n", __func__);
8055 if (res.notification != args.notify_types)
8058 dprintk("<-- %s status=%d\n", __func__, status);
8063 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8064 struct pnfs_device *pdev,
8065 struct rpc_cred *cred)
8067 struct nfs4_exception exception = { };
8071 err = nfs4_handle_exception(server,
8072 _nfs4_proc_getdeviceinfo(server, pdev, cred),
8074 } while (exception.retry);
8077 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8079 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8081 struct nfs4_layoutcommit_data *data = calldata;
8082 struct nfs_server *server = NFS_SERVER(data->args.inode);
8083 struct nfs4_session *session = nfs4_get_session(server);
8085 nfs41_setup_sequence(session,
8086 &data->args.seq_args,
8092 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8094 struct nfs4_layoutcommit_data *data = calldata;
8095 struct nfs_server *server = NFS_SERVER(data->args.inode);
8097 if (!nfs41_sequence_done(task, &data->res.seq_res))
8100 switch (task->tk_status) { /* Just ignore these failures */
8101 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8102 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8103 case -NFS4ERR_BADLAYOUT: /* no layout */
8104 case -NFS4ERR_GRACE: /* loca_recalim always false */
8105 task->tk_status = 0;
8109 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
8110 rpc_restart_call_prepare(task);
8116 static void nfs4_layoutcommit_release(void *calldata)
8118 struct nfs4_layoutcommit_data *data = calldata;
8120 pnfs_cleanup_layoutcommit(data);
8121 nfs_post_op_update_inode_force_wcc(data->args.inode,
8123 put_rpccred(data->cred);
8124 nfs_iput_and_deactive(data->inode);
8128 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8129 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8130 .rpc_call_done = nfs4_layoutcommit_done,
8131 .rpc_release = nfs4_layoutcommit_release,
8135 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
8137 struct rpc_message msg = {
8138 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8139 .rpc_argp = &data->args,
8140 .rpc_resp = &data->res,
8141 .rpc_cred = data->cred,
8143 struct rpc_task_setup task_setup_data = {
8144 .task = &data->task,
8145 .rpc_client = NFS_CLIENT(data->args.inode),
8146 .rpc_message = &msg,
8147 .callback_ops = &nfs4_layoutcommit_ops,
8148 .callback_data = data,
8150 struct rpc_task *task;
8153 dprintk("NFS: initiating layoutcommit call. sync %d "
8154 "lbw: %llu inode %lu\n", sync,
8155 data->args.lastbytewritten,
8156 data->args.inode->i_ino);
8159 data->inode = nfs_igrab_and_active(data->args.inode);
8160 if (data->inode == NULL) {
8161 nfs4_layoutcommit_release(data);
8164 task_setup_data.flags = RPC_TASK_ASYNC;
8166 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
8167 task = rpc_run_task(&task_setup_data);
8169 return PTR_ERR(task);
8171 status = task->tk_status;
8172 trace_nfs4_layoutcommit(data->args.inode, status);
8173 dprintk("%s: status %d\n", __func__, status);
8179 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8180 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8183 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8184 struct nfs_fsinfo *info,
8185 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
8187 struct nfs41_secinfo_no_name_args args = {
8188 .style = SECINFO_STYLE_CURRENT_FH,
8190 struct nfs4_secinfo_res res = {
8193 struct rpc_message msg = {
8194 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8198 struct rpc_clnt *clnt = server->client;
8199 struct rpc_cred *cred = NULL;
8202 if (use_integrity) {
8203 clnt = server->nfs_client->cl_rpcclient;
8204 cred = nfs4_get_clid_cred(server->nfs_client);
8205 msg.rpc_cred = cred;
8208 dprintk("--> %s\n", __func__);
8209 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8211 dprintk("<-- %s status=%d\n", __func__, status);
8220 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8221 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8223 struct nfs4_exception exception = { };
8226 /* first try using integrity protection */
8227 err = -NFS4ERR_WRONGSEC;
8229 /* try to use integrity protection with machine cred */
8230 if (_nfs4_is_integrity_protected(server->nfs_client))
8231 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8235 * if unable to use integrity protection, or SECINFO with
8236 * integrity protection returns NFS4ERR_WRONGSEC (which is
8237 * disallowed by spec, but exists in deployed servers) use
8238 * the current filesystem's rpc_client and the user cred.
8240 if (err == -NFS4ERR_WRONGSEC)
8241 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8246 case -NFS4ERR_WRONGSEC:
8250 err = nfs4_handle_exception(server, err, &exception);
8252 } while (exception.retry);
8258 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8259 struct nfs_fsinfo *info)
8263 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
8264 struct nfs4_secinfo_flavors *flavors;
8265 struct nfs4_secinfo4 *secinfo;
8268 page = alloc_page(GFP_KERNEL);
8274 flavors = page_address(page);
8275 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8278 * Fall back on "guess and check" method if
8279 * the server doesn't support SECINFO_NO_NAME
8281 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
8282 err = nfs4_find_root_sec(server, fhandle, info);
8288 for (i = 0; i < flavors->num_flavors; i++) {
8289 secinfo = &flavors->flavors[i];
8291 switch (secinfo->flavor) {
8295 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8296 &secinfo->flavor_info);
8299 flavor = RPC_AUTH_MAXFLAVOR;
8303 if (!nfs_auth_info_match(&server->auth_info, flavor))
8304 flavor = RPC_AUTH_MAXFLAVOR;
8306 if (flavor != RPC_AUTH_MAXFLAVOR) {
8307 err = nfs4_lookup_root_sec(server, fhandle,
8314 if (flavor == RPC_AUTH_MAXFLAVOR)
8325 static int _nfs41_test_stateid(struct nfs_server *server,
8326 nfs4_stateid *stateid,
8327 struct rpc_cred *cred)
8330 struct nfs41_test_stateid_args args = {
8333 struct nfs41_test_stateid_res res;
8334 struct rpc_message msg = {
8335 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8340 struct rpc_clnt *rpc_client = server->client;
8342 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8345 dprintk("NFS call test_stateid %p\n", stateid);
8346 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
8347 nfs4_set_sequence_privileged(&args.seq_args);
8348 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
8349 &args.seq_args, &res.seq_res);
8350 if (status != NFS_OK) {
8351 dprintk("NFS reply test_stateid: failed, %d\n", status);
8354 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
8359 * nfs41_test_stateid - perform a TEST_STATEID operation
8361 * @server: server / transport on which to perform the operation
8362 * @stateid: state ID to test
8365 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8366 * Otherwise a negative NFS4ERR value is returned if the operation
8367 * failed or the state ID is not currently valid.
8369 static int nfs41_test_stateid(struct nfs_server *server,
8370 nfs4_stateid *stateid,
8371 struct rpc_cred *cred)
8373 struct nfs4_exception exception = { };
8376 err = _nfs41_test_stateid(server, stateid, cred);
8377 if (err != -NFS4ERR_DELAY)
8379 nfs4_handle_exception(server, err, &exception);
8380 } while (exception.retry);
8384 struct nfs_free_stateid_data {
8385 struct nfs_server *server;
8386 struct nfs41_free_stateid_args args;
8387 struct nfs41_free_stateid_res res;
8390 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
8392 struct nfs_free_stateid_data *data = calldata;
8393 nfs41_setup_sequence(nfs4_get_session(data->server),
8394 &data->args.seq_args,
8399 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
8401 struct nfs_free_stateid_data *data = calldata;
8403 nfs41_sequence_done(task, &data->res.seq_res);
8405 switch (task->tk_status) {
8406 case -NFS4ERR_DELAY:
8407 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
8408 rpc_restart_call_prepare(task);
8412 static void nfs41_free_stateid_release(void *calldata)
8417 static const struct rpc_call_ops nfs41_free_stateid_ops = {
8418 .rpc_call_prepare = nfs41_free_stateid_prepare,
8419 .rpc_call_done = nfs41_free_stateid_done,
8420 .rpc_release = nfs41_free_stateid_release,
8423 static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
8424 nfs4_stateid *stateid,
8425 struct rpc_cred *cred,
8428 struct rpc_message msg = {
8429 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
8432 struct rpc_task_setup task_setup = {
8433 .rpc_client = server->client,
8434 .rpc_message = &msg,
8435 .callback_ops = &nfs41_free_stateid_ops,
8436 .flags = RPC_TASK_ASYNC,
8438 struct nfs_free_stateid_data *data;
8440 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8441 &task_setup.rpc_client, &msg);
8443 dprintk("NFS call free_stateid %p\n", stateid);
8444 data = kmalloc(sizeof(*data), GFP_NOFS);
8446 return ERR_PTR(-ENOMEM);
8447 data->server = server;
8448 nfs4_stateid_copy(&data->args.stateid, stateid);
8450 task_setup.callback_data = data;
8452 msg.rpc_argp = &data->args;
8453 msg.rpc_resp = &data->res;
8454 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
8456 nfs4_set_sequence_privileged(&data->args.seq_args);
8458 return rpc_run_task(&task_setup);
8462 * nfs41_free_stateid - perform a FREE_STATEID operation
8464 * @server: server / transport on which to perform the operation
8465 * @stateid: state ID to release
8468 * Returns NFS_OK if the server freed "stateid". Otherwise a
8469 * negative NFS4ERR value is returned.
8471 static int nfs41_free_stateid(struct nfs_server *server,
8472 nfs4_stateid *stateid,
8473 struct rpc_cred *cred)
8475 struct rpc_task *task;
8478 task = _nfs41_free_stateid(server, stateid, cred, true);
8480 return PTR_ERR(task);
8481 ret = rpc_wait_for_completion_task(task);
8483 ret = task->tk_status;
8489 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
8491 struct rpc_task *task;
8492 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
8494 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
8495 nfs4_free_lock_state(server, lsp);
8501 static bool nfs41_match_stateid(const nfs4_stateid *s1,
8502 const nfs4_stateid *s2)
8504 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
8507 if (s1->seqid == s2->seqid)
8509 if (s1->seqid == 0 || s2->seqid == 0)
8515 #endif /* CONFIG_NFS_V4_1 */
8517 static bool nfs4_match_stateid(const nfs4_stateid *s1,
8518 const nfs4_stateid *s2)
8520 return nfs4_stateid_match(s1, s2);
8524 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
8525 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
8526 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
8527 .recover_open = nfs4_open_reclaim,
8528 .recover_lock = nfs4_lock_reclaim,
8529 .establish_clid = nfs4_init_clientid,
8530 .detect_trunking = nfs40_discover_server_trunking,
8533 #if defined(CONFIG_NFS_V4_1)
8534 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
8535 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
8536 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
8537 .recover_open = nfs4_open_reclaim,
8538 .recover_lock = nfs4_lock_reclaim,
8539 .establish_clid = nfs41_init_clientid,
8540 .reclaim_complete = nfs41_proc_reclaim_complete,
8541 .detect_trunking = nfs41_discover_server_trunking,
8543 #endif /* CONFIG_NFS_V4_1 */
8545 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
8546 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
8547 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
8548 .recover_open = nfs40_open_expired,
8549 .recover_lock = nfs4_lock_expired,
8550 .establish_clid = nfs4_init_clientid,
8553 #if defined(CONFIG_NFS_V4_1)
8554 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
8555 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
8556 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
8557 .recover_open = nfs41_open_expired,
8558 .recover_lock = nfs41_lock_expired,
8559 .establish_clid = nfs41_init_clientid,
8561 #endif /* CONFIG_NFS_V4_1 */
8563 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
8564 .sched_state_renewal = nfs4_proc_async_renew,
8565 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
8566 .renew_lease = nfs4_proc_renew,
8569 #if defined(CONFIG_NFS_V4_1)
8570 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
8571 .sched_state_renewal = nfs41_proc_async_sequence,
8572 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
8573 .renew_lease = nfs4_proc_sequence,
8577 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
8578 .get_locations = _nfs40_proc_get_locations,
8579 .fsid_present = _nfs40_proc_fsid_present,
8582 #if defined(CONFIG_NFS_V4_1)
8583 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
8584 .get_locations = _nfs41_proc_get_locations,
8585 .fsid_present = _nfs41_proc_fsid_present,
8587 #endif /* CONFIG_NFS_V4_1 */
8589 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
8591 .init_caps = NFS_CAP_READDIRPLUS
8592 | NFS_CAP_ATOMIC_OPEN
8593 | NFS_CAP_CHANGE_ATTR
8594 | NFS_CAP_POSIX_LOCK,
8595 .init_client = nfs40_init_client,
8596 .shutdown_client = nfs40_shutdown_client,
8597 .match_stateid = nfs4_match_stateid,
8598 .find_root_sec = nfs4_find_root_sec,
8599 .free_lock_state = nfs4_release_lockowner,
8600 .alloc_seqid = nfs_alloc_seqid,
8601 .call_sync_ops = &nfs40_call_sync_ops,
8602 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
8603 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
8604 .state_renewal_ops = &nfs40_state_renewal_ops,
8605 .mig_recovery_ops = &nfs40_mig_recovery_ops,
8608 #if defined(CONFIG_NFS_V4_1)
8609 static struct nfs_seqid *
8610 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
8615 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
8617 .init_caps = NFS_CAP_READDIRPLUS
8618 | NFS_CAP_ATOMIC_OPEN
8619 | NFS_CAP_CHANGE_ATTR
8620 | NFS_CAP_POSIX_LOCK
8621 | NFS_CAP_STATEID_NFSV41
8622 | NFS_CAP_ATOMIC_OPEN_V1,
8623 .init_client = nfs41_init_client,
8624 .shutdown_client = nfs41_shutdown_client,
8625 .match_stateid = nfs41_match_stateid,
8626 .find_root_sec = nfs41_find_root_sec,
8627 .free_lock_state = nfs41_free_lock_state,
8628 .alloc_seqid = nfs_alloc_no_seqid,
8629 .call_sync_ops = &nfs41_call_sync_ops,
8630 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8631 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8632 .state_renewal_ops = &nfs41_state_renewal_ops,
8633 .mig_recovery_ops = &nfs41_mig_recovery_ops,
8637 #if defined(CONFIG_NFS_V4_2)
8638 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
8640 .init_caps = NFS_CAP_READDIRPLUS
8641 | NFS_CAP_ATOMIC_OPEN
8642 | NFS_CAP_CHANGE_ATTR
8643 | NFS_CAP_POSIX_LOCK
8644 | NFS_CAP_STATEID_NFSV41
8645 | NFS_CAP_ATOMIC_OPEN_V1
8647 | NFS_CAP_DEALLOCATE
8649 | NFS_CAP_LAYOUTSTATS,
8650 .init_client = nfs41_init_client,
8651 .shutdown_client = nfs41_shutdown_client,
8652 .match_stateid = nfs41_match_stateid,
8653 .find_root_sec = nfs41_find_root_sec,
8654 .free_lock_state = nfs41_free_lock_state,
8655 .call_sync_ops = &nfs41_call_sync_ops,
8656 .alloc_seqid = nfs_alloc_no_seqid,
8657 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8658 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8659 .state_renewal_ops = &nfs41_state_renewal_ops,
8663 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
8664 [0] = &nfs_v4_0_minor_ops,
8665 #if defined(CONFIG_NFS_V4_1)
8666 [1] = &nfs_v4_1_minor_ops,
8668 #if defined(CONFIG_NFS_V4_2)
8669 [2] = &nfs_v4_2_minor_ops,
8673 static const struct inode_operations nfs4_dir_inode_operations = {
8674 .create = nfs_create,
8675 .lookup = nfs_lookup,
8676 .atomic_open = nfs_atomic_open,
8678 .unlink = nfs_unlink,
8679 .symlink = nfs_symlink,
8683 .rename = nfs_rename,
8684 .permission = nfs_permission,
8685 .getattr = nfs_getattr,
8686 .setattr = nfs_setattr,
8687 .getxattr = generic_getxattr,
8688 .setxattr = generic_setxattr,
8689 .listxattr = generic_listxattr,
8690 .removexattr = generic_removexattr,
8693 static const struct inode_operations nfs4_file_inode_operations = {
8694 .permission = nfs_permission,
8695 .getattr = nfs_getattr,
8696 .setattr = nfs_setattr,
8697 .getxattr = generic_getxattr,
8698 .setxattr = generic_setxattr,
8699 .listxattr = generic_listxattr,
8700 .removexattr = generic_removexattr,
8703 const struct nfs_rpc_ops nfs_v4_clientops = {
8704 .version = 4, /* protocol version */
8705 .dentry_ops = &nfs4_dentry_operations,
8706 .dir_inode_ops = &nfs4_dir_inode_operations,
8707 .file_inode_ops = &nfs4_file_inode_operations,
8708 .file_ops = &nfs4_file_operations,
8709 .getroot = nfs4_proc_get_root,
8710 .submount = nfs4_submount,
8711 .try_mount = nfs4_try_mount,
8712 .getattr = nfs4_proc_getattr,
8713 .setattr = nfs4_proc_setattr,
8714 .lookup = nfs4_proc_lookup,
8715 .access = nfs4_proc_access,
8716 .readlink = nfs4_proc_readlink,
8717 .create = nfs4_proc_create,
8718 .remove = nfs4_proc_remove,
8719 .unlink_setup = nfs4_proc_unlink_setup,
8720 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
8721 .unlink_done = nfs4_proc_unlink_done,
8722 .rename_setup = nfs4_proc_rename_setup,
8723 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
8724 .rename_done = nfs4_proc_rename_done,
8725 .link = nfs4_proc_link,
8726 .symlink = nfs4_proc_symlink,
8727 .mkdir = nfs4_proc_mkdir,
8728 .rmdir = nfs4_proc_remove,
8729 .readdir = nfs4_proc_readdir,
8730 .mknod = nfs4_proc_mknod,
8731 .statfs = nfs4_proc_statfs,
8732 .fsinfo = nfs4_proc_fsinfo,
8733 .pathconf = nfs4_proc_pathconf,
8734 .set_capabilities = nfs4_server_capabilities,
8735 .decode_dirent = nfs4_decode_dirent,
8736 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
8737 .read_setup = nfs4_proc_read_setup,
8738 .read_done = nfs4_read_done,
8739 .write_setup = nfs4_proc_write_setup,
8740 .write_done = nfs4_write_done,
8741 .commit_setup = nfs4_proc_commit_setup,
8742 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
8743 .commit_done = nfs4_commit_done,
8744 .lock = nfs4_proc_lock,
8745 .clear_acl_cache = nfs4_zap_acl_attr,
8746 .close_context = nfs4_close_context,
8747 .open_context = nfs4_atomic_open,
8748 .have_delegation = nfs4_have_delegation,
8749 .return_delegation = nfs4_inode_return_delegation,
8750 .alloc_client = nfs4_alloc_client,
8751 .init_client = nfs4_init_client,
8752 .free_client = nfs4_free_client,
8753 .create_server = nfs4_create_server,
8754 .clone_server = nfs_clone_server,
8757 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
8758 .prefix = XATTR_NAME_NFSV4_ACL,
8759 .list = nfs4_xattr_list_nfs4_acl,
8760 .get = nfs4_xattr_get_nfs4_acl,
8761 .set = nfs4_xattr_set_nfs4_acl,
8764 const struct xattr_handler *nfs4_xattr_handlers[] = {
8765 &nfs4_xattr_nfs4_acl_handler,
8766 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
8767 &nfs4_xattr_nfs4_label_handler,