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 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
360 nfs4_inode_return_delegation(inode);
361 exception->retry = 1;
366 ret = nfs4_schedule_stateid_recovery(server, state);
369 goto wait_on_recovery;
370 case -NFS4ERR_DELEG_REVOKED:
371 case -NFS4ERR_ADMIN_REVOKED:
372 case -NFS4ERR_BAD_STATEID:
375 ret = nfs4_schedule_stateid_recovery(server, state);
378 goto wait_on_recovery;
379 case -NFS4ERR_EXPIRED:
381 ret = nfs4_schedule_stateid_recovery(server, state);
385 case -NFS4ERR_STALE_STATEID:
386 case -NFS4ERR_STALE_CLIENTID:
387 nfs4_schedule_lease_recovery(clp);
388 goto wait_on_recovery;
390 ret = nfs4_schedule_migration_recovery(server);
393 goto wait_on_recovery;
394 case -NFS4ERR_LEASE_MOVED:
395 nfs4_schedule_lease_moved_recovery(clp);
396 goto wait_on_recovery;
397 #if defined(CONFIG_NFS_V4_1)
398 case -NFS4ERR_BADSESSION:
399 case -NFS4ERR_BADSLOT:
400 case -NFS4ERR_BAD_HIGH_SLOT:
401 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
402 case -NFS4ERR_DEADSESSION:
403 case -NFS4ERR_SEQ_FALSE_RETRY:
404 case -NFS4ERR_SEQ_MISORDERED:
405 dprintk("%s ERROR: %d Reset session\n", __func__,
407 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
408 goto wait_on_recovery;
409 #endif /* defined(CONFIG_NFS_V4_1) */
410 case -NFS4ERR_FILE_OPEN:
411 if (exception->timeout > HZ) {
412 /* We have retried a decent amount, time to
420 ret = nfs4_delay(server->client, &exception->timeout);
423 case -NFS4ERR_RETRY_UNCACHED_REP:
424 case -NFS4ERR_OLD_STATEID:
425 exception->retry = 1;
427 case -NFS4ERR_BADOWNER:
428 /* The following works around a Linux server bug! */
429 case -NFS4ERR_BADNAME:
430 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
431 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
432 exception->retry = 1;
433 printk(KERN_WARNING "NFS: v4 server %s "
434 "does not accept raw "
436 "Reenabling the idmapper.\n",
437 server->nfs_client->cl_hostname);
440 /* We failed to handle the error */
441 return nfs4_map_errors(ret);
443 ret = nfs4_wait_clnt_recover(clp);
444 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
447 exception->retry = 1;
452 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
453 * or 'false' otherwise.
455 static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
457 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
459 if (flavor == RPC_AUTH_GSS_KRB5I ||
460 flavor == RPC_AUTH_GSS_KRB5P)
466 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
468 spin_lock(&clp->cl_lock);
469 if (time_before(clp->cl_last_renewal,timestamp))
470 clp->cl_last_renewal = timestamp;
471 spin_unlock(&clp->cl_lock);
474 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
476 do_renew_lease(server->nfs_client, timestamp);
479 struct nfs4_call_sync_data {
480 const struct nfs_server *seq_server;
481 struct nfs4_sequence_args *seq_args;
482 struct nfs4_sequence_res *seq_res;
485 static void nfs4_init_sequence(struct nfs4_sequence_args *args,
486 struct nfs4_sequence_res *res, int cache_reply)
488 args->sa_slot = NULL;
489 args->sa_cache_this = cache_reply;
490 args->sa_privileged = 0;
495 static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
497 args->sa_privileged = 1;
500 int nfs40_setup_sequence(struct nfs4_slot_table *tbl,
501 struct nfs4_sequence_args *args,
502 struct nfs4_sequence_res *res,
503 struct rpc_task *task)
505 struct nfs4_slot *slot;
507 /* slot already allocated? */
508 if (res->sr_slot != NULL)
511 spin_lock(&tbl->slot_tbl_lock);
512 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
515 slot = nfs4_alloc_slot(tbl);
517 if (slot == ERR_PTR(-ENOMEM))
518 task->tk_timeout = HZ >> 2;
521 spin_unlock(&tbl->slot_tbl_lock);
523 args->sa_slot = slot;
527 rpc_call_start(task);
531 if (args->sa_privileged)
532 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
533 NULL, RPC_PRIORITY_PRIVILEGED);
535 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
536 spin_unlock(&tbl->slot_tbl_lock);
539 EXPORT_SYMBOL_GPL(nfs40_setup_sequence);
541 static int nfs40_sequence_done(struct rpc_task *task,
542 struct nfs4_sequence_res *res)
544 struct nfs4_slot *slot = res->sr_slot;
545 struct nfs4_slot_table *tbl;
551 spin_lock(&tbl->slot_tbl_lock);
552 if (!nfs41_wake_and_assign_slot(tbl, slot))
553 nfs4_free_slot(tbl, slot);
554 spin_unlock(&tbl->slot_tbl_lock);
561 #if defined(CONFIG_NFS_V4_1)
563 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
565 struct nfs4_session *session;
566 struct nfs4_slot_table *tbl;
567 struct nfs4_slot *slot = res->sr_slot;
568 bool send_new_highest_used_slotid = false;
571 session = tbl->session;
573 spin_lock(&tbl->slot_tbl_lock);
574 /* Be nice to the server: try to ensure that the last transmitted
575 * value for highest_user_slotid <= target_highest_slotid
577 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
578 send_new_highest_used_slotid = true;
580 if (nfs41_wake_and_assign_slot(tbl, slot)) {
581 send_new_highest_used_slotid = false;
584 nfs4_free_slot(tbl, slot);
586 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
587 send_new_highest_used_slotid = false;
589 spin_unlock(&tbl->slot_tbl_lock);
591 if (send_new_highest_used_slotid)
592 nfs41_server_notify_highest_slotid_update(session->clp);
595 int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
597 struct nfs4_session *session;
598 struct nfs4_slot *slot = res->sr_slot;
599 struct nfs_client *clp;
600 bool interrupted = false;
605 /* don't increment the sequence number if the task wasn't sent */
606 if (!RPC_WAS_SENT(task))
609 session = slot->table->session;
611 if (slot->interrupted) {
612 slot->interrupted = 0;
616 trace_nfs4_sequence_done(session, res);
617 /* Check the SEQUENCE operation status */
618 switch (res->sr_status) {
620 /* Update the slot's sequence and clientid lease timer */
623 do_renew_lease(clp, res->sr_timestamp);
624 /* Check sequence flags */
625 if (res->sr_status_flags != 0)
626 nfs4_schedule_lease_recovery(clp);
627 nfs41_update_target_slotid(slot->table, slot, res);
631 * sr_status remains 1 if an RPC level error occurred.
632 * The server may or may not have processed the sequence
634 * Mark the slot as having hosted an interrupted RPC call.
636 slot->interrupted = 1;
639 /* The server detected a resend of the RPC call and
640 * returned NFS4ERR_DELAY as per Section 2.10.6.2
643 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
648 case -NFS4ERR_BADSLOT:
650 * The slot id we used was probably retired. Try again
651 * using a different slot id.
654 case -NFS4ERR_SEQ_MISORDERED:
656 * Was the last operation on this sequence interrupted?
657 * If so, retry after bumping the sequence number.
664 * Could this slot have been previously retired?
665 * If so, then the server may be expecting seq_nr = 1!
667 if (slot->seq_nr != 1) {
672 case -NFS4ERR_SEQ_FALSE_RETRY:
676 /* Just update the slot sequence no. */
680 /* The session may be reset by one of the error handlers. */
681 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
682 nfs41_sequence_free_slot(res);
686 if (rpc_restart_call_prepare(task)) {
692 if (!rpc_restart_call(task))
694 rpc_delay(task, NFS4_POLL_RETRY_MAX);
697 EXPORT_SYMBOL_GPL(nfs41_sequence_done);
699 int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
701 if (res->sr_slot == NULL)
703 if (!res->sr_slot->table->session)
704 return nfs40_sequence_done(task, res);
705 return nfs41_sequence_done(task, res);
707 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
709 int nfs41_setup_sequence(struct nfs4_session *session,
710 struct nfs4_sequence_args *args,
711 struct nfs4_sequence_res *res,
712 struct rpc_task *task)
714 struct nfs4_slot *slot;
715 struct nfs4_slot_table *tbl;
717 dprintk("--> %s\n", __func__);
718 /* slot already allocated? */
719 if (res->sr_slot != NULL)
722 tbl = &session->fc_slot_table;
724 task->tk_timeout = 0;
726 spin_lock(&tbl->slot_tbl_lock);
727 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
728 !args->sa_privileged) {
729 /* The state manager will wait until the slot table is empty */
730 dprintk("%s session is draining\n", __func__);
734 slot = nfs4_alloc_slot(tbl);
736 /* If out of memory, try again in 1/4 second */
737 if (slot == ERR_PTR(-ENOMEM))
738 task->tk_timeout = HZ >> 2;
739 dprintk("<-- %s: no free slots\n", __func__);
742 spin_unlock(&tbl->slot_tbl_lock);
744 args->sa_slot = slot;
746 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
747 slot->slot_nr, slot->seq_nr);
750 res->sr_timestamp = jiffies;
751 res->sr_status_flags = 0;
753 * sr_status is only set in decode_sequence, and so will remain
754 * set to 1 if an rpc level failure occurs.
757 trace_nfs4_setup_sequence(session, args);
759 rpc_call_start(task);
762 /* Privileged tasks are queued with top priority */
763 if (args->sa_privileged)
764 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
765 NULL, RPC_PRIORITY_PRIVILEGED);
767 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
768 spin_unlock(&tbl->slot_tbl_lock);
771 EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
773 static int nfs4_setup_sequence(const struct nfs_server *server,
774 struct nfs4_sequence_args *args,
775 struct nfs4_sequence_res *res,
776 struct rpc_task *task)
778 struct nfs4_session *session = nfs4_get_session(server);
782 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
785 dprintk("--> %s clp %p session %p sr_slot %u\n",
786 __func__, session->clp, session, res->sr_slot ?
787 res->sr_slot->slot_nr : NFS4_NO_SLOT);
789 ret = nfs41_setup_sequence(session, args, res, task);
791 dprintk("<-- %s status=%d\n", __func__, ret);
795 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
797 struct nfs4_call_sync_data *data = calldata;
798 struct nfs4_session *session = nfs4_get_session(data->seq_server);
800 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
802 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
805 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
807 struct nfs4_call_sync_data *data = calldata;
809 nfs41_sequence_done(task, data->seq_res);
812 static const struct rpc_call_ops nfs41_call_sync_ops = {
813 .rpc_call_prepare = nfs41_call_sync_prepare,
814 .rpc_call_done = nfs41_call_sync_done,
817 #else /* !CONFIG_NFS_V4_1 */
819 static int nfs4_setup_sequence(const struct nfs_server *server,
820 struct nfs4_sequence_args *args,
821 struct nfs4_sequence_res *res,
822 struct rpc_task *task)
824 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
828 int nfs4_sequence_done(struct rpc_task *task,
829 struct nfs4_sequence_res *res)
831 return nfs40_sequence_done(task, res);
833 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
835 #endif /* !CONFIG_NFS_V4_1 */
837 static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
839 struct nfs4_call_sync_data *data = calldata;
840 nfs4_setup_sequence(data->seq_server,
841 data->seq_args, data->seq_res, task);
844 static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
846 struct nfs4_call_sync_data *data = calldata;
847 nfs4_sequence_done(task, data->seq_res);
850 static const struct rpc_call_ops nfs40_call_sync_ops = {
851 .rpc_call_prepare = nfs40_call_sync_prepare,
852 .rpc_call_done = nfs40_call_sync_done,
855 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
856 struct nfs_server *server,
857 struct rpc_message *msg,
858 struct nfs4_sequence_args *args,
859 struct nfs4_sequence_res *res)
862 struct rpc_task *task;
863 struct nfs_client *clp = server->nfs_client;
864 struct nfs4_call_sync_data data = {
865 .seq_server = server,
869 struct rpc_task_setup task_setup = {
872 .callback_ops = clp->cl_mvops->call_sync_ops,
873 .callback_data = &data
876 task = rpc_run_task(&task_setup);
880 ret = task->tk_status;
886 int nfs4_call_sync(struct rpc_clnt *clnt,
887 struct nfs_server *server,
888 struct rpc_message *msg,
889 struct nfs4_sequence_args *args,
890 struct nfs4_sequence_res *res,
893 nfs4_init_sequence(args, res, cache_reply);
894 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
897 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
899 struct nfs_inode *nfsi = NFS_I(dir);
901 spin_lock(&dir->i_lock);
902 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
903 if (!cinfo->atomic || cinfo->before != dir->i_version)
904 nfs_force_lookup_revalidate(dir);
905 dir->i_version = cinfo->after;
906 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
907 nfs_fscache_invalidate(dir);
908 spin_unlock(&dir->i_lock);
911 struct nfs4_opendata {
913 struct nfs_openargs o_arg;
914 struct nfs_openres o_res;
915 struct nfs_open_confirmargs c_arg;
916 struct nfs_open_confirmres c_res;
917 struct nfs4_string owner_name;
918 struct nfs4_string group_name;
919 struct nfs_fattr f_attr;
920 struct nfs4_label *f_label;
922 struct dentry *dentry;
923 struct nfs4_state_owner *owner;
924 struct nfs4_state *state;
926 unsigned long timestamp;
927 unsigned int rpc_done : 1;
928 unsigned int file_created : 1;
929 unsigned int is_recover : 1;
934 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
935 int err, struct nfs4_exception *exception)
939 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
941 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
942 exception->retry = 1;
947 nfs4_map_atomic_open_share(struct nfs_server *server,
948 fmode_t fmode, int openflags)
952 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
954 res = NFS4_SHARE_ACCESS_READ;
957 res = NFS4_SHARE_ACCESS_WRITE;
959 case FMODE_READ|FMODE_WRITE:
960 res = NFS4_SHARE_ACCESS_BOTH;
962 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
964 /* Want no delegation if we're using O_DIRECT */
965 if (openflags & O_DIRECT)
966 res |= NFS4_SHARE_WANT_NO_DELEG;
971 static enum open_claim_type4
972 nfs4_map_atomic_open_claim(struct nfs_server *server,
973 enum open_claim_type4 claim)
975 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
980 case NFS4_OPEN_CLAIM_FH:
981 return NFS4_OPEN_CLAIM_NULL;
982 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
983 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
984 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
985 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
989 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
991 p->o_res.f_attr = &p->f_attr;
992 p->o_res.f_label = p->f_label;
993 p->o_res.seqid = p->o_arg.seqid;
994 p->c_res.seqid = p->c_arg.seqid;
995 p->o_res.server = p->o_arg.server;
996 p->o_res.access_request = p->o_arg.access;
997 nfs_fattr_init(&p->f_attr);
998 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
1001 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
1002 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
1003 const struct iattr *attrs,
1004 struct nfs4_label *label,
1005 enum open_claim_type4 claim,
1008 struct dentry *parent = dget_parent(dentry);
1009 struct inode *dir = d_inode(parent);
1010 struct nfs_server *server = NFS_SERVER(dir);
1011 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
1012 struct nfs4_opendata *p;
1014 p = kzalloc(sizeof(*p), gfp_mask);
1018 p->f_label = nfs4_label_alloc(server, gfp_mask);
1019 if (IS_ERR(p->f_label))
1022 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1023 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
1024 if (IS_ERR(p->o_arg.seqid))
1025 goto err_free_label;
1026 nfs_sb_active(dentry->d_sb);
1027 p->dentry = dget(dentry);
1030 atomic_inc(&sp->so_count);
1031 p->o_arg.open_flags = flags;
1032 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
1033 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1035 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1036 * will return permission denied for all bits until close */
1037 if (!(flags & O_EXCL)) {
1038 /* ask server to check for all possible rights as results
1040 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1041 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
1043 p->o_arg.clientid = server->nfs_client->cl_clientid;
1044 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1045 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1046 p->o_arg.name = &dentry->d_name;
1047 p->o_arg.server = server;
1048 p->o_arg.bitmask = nfs4_bitmask(server, label);
1049 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1050 p->o_arg.label = label;
1051 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1052 switch (p->o_arg.claim) {
1053 case NFS4_OPEN_CLAIM_NULL:
1054 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1055 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1056 p->o_arg.fh = NFS_FH(dir);
1058 case NFS4_OPEN_CLAIM_PREVIOUS:
1059 case NFS4_OPEN_CLAIM_FH:
1060 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1061 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1062 p->o_arg.fh = NFS_FH(d_inode(dentry));
1064 if (attrs != NULL && attrs->ia_valid != 0) {
1067 p->o_arg.u.attrs = &p->attrs;
1068 memcpy(&p->attrs, attrs, sizeof(p->attrs));
1071 verf[1] = current->pid;
1072 memcpy(p->o_arg.u.verifier.data, verf,
1073 sizeof(p->o_arg.u.verifier.data));
1075 p->c_arg.fh = &p->o_res.fh;
1076 p->c_arg.stateid = &p->o_res.stateid;
1077 p->c_arg.seqid = p->o_arg.seqid;
1078 nfs4_init_opendata_res(p);
1079 kref_init(&p->kref);
1083 nfs4_label_free(p->f_label);
1091 static void nfs4_opendata_free(struct kref *kref)
1093 struct nfs4_opendata *p = container_of(kref,
1094 struct nfs4_opendata, kref);
1095 struct super_block *sb = p->dentry->d_sb;
1097 nfs_free_seqid(p->o_arg.seqid);
1098 if (p->state != NULL)
1099 nfs4_put_open_state(p->state);
1100 nfs4_put_state_owner(p->owner);
1102 nfs4_label_free(p->f_label);
1106 nfs_sb_deactive(sb);
1107 nfs_fattr_free_names(&p->f_attr);
1108 kfree(p->f_attr.mdsthreshold);
1112 static void nfs4_opendata_put(struct nfs4_opendata *p)
1115 kref_put(&p->kref, nfs4_opendata_free);
1118 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1122 ret = rpc_wait_for_completion_task(task);
1126 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
1130 if (open_mode & (O_EXCL|O_TRUNC))
1132 switch (mode & (FMODE_READ|FMODE_WRITE)) {
1134 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1135 && state->n_rdonly != 0;
1138 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1139 && state->n_wronly != 0;
1141 case FMODE_READ|FMODE_WRITE:
1142 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1143 && state->n_rdwr != 0;
1149 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
1151 if (delegation == NULL)
1153 if ((delegation->type & fmode) != fmode)
1155 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1157 nfs_mark_delegation_referenced(delegation);
1161 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1170 case FMODE_READ|FMODE_WRITE:
1173 nfs4_state_set_mode_locked(state, state->state | fmode);
1176 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
1178 struct nfs_client *clp = state->owner->so_server->nfs_client;
1179 bool need_recover = false;
1181 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1182 need_recover = true;
1183 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1184 need_recover = true;
1185 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1186 need_recover = true;
1188 nfs4_state_mark_reclaim_nograce(clp, state);
1191 static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1192 nfs4_stateid *stateid)
1194 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1196 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1197 nfs_test_and_clear_all_open_stateid(state);
1200 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1205 static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1207 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1209 if (state->n_wronly)
1210 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1211 if (state->n_rdonly)
1212 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1214 set_bit(NFS_O_RDWR_STATE, &state->flags);
1215 set_bit(NFS_OPEN_STATE, &state->flags);
1218 static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1219 nfs4_stateid *stateid, fmode_t fmode)
1221 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1222 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1224 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1227 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1230 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1231 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1232 clear_bit(NFS_OPEN_STATE, &state->flags);
1234 if (stateid == NULL)
1236 /* Handle races with OPEN */
1237 if (!nfs4_stateid_match_other(stateid, &state->open_stateid) ||
1238 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1239 nfs_resync_open_stateid_locked(state);
1242 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1243 nfs4_stateid_copy(&state->stateid, stateid);
1244 nfs4_stateid_copy(&state->open_stateid, stateid);
1247 static void nfs_clear_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1249 write_seqlock(&state->seqlock);
1250 nfs_clear_open_stateid_locked(state, stateid, fmode);
1251 write_sequnlock(&state->seqlock);
1252 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1253 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1256 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1260 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1263 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1265 case FMODE_READ|FMODE_WRITE:
1266 set_bit(NFS_O_RDWR_STATE, &state->flags);
1268 if (!nfs_need_update_open_stateid(state, stateid))
1270 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1271 nfs4_stateid_copy(&state->stateid, stateid);
1272 nfs4_stateid_copy(&state->open_stateid, stateid);
1275 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
1278 * Protect the call to nfs4_state_set_mode_locked and
1279 * serialise the stateid update
1281 write_seqlock(&state->seqlock);
1282 if (deleg_stateid != NULL) {
1283 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1284 set_bit(NFS_DELEGATED_STATE, &state->flags);
1286 if (open_stateid != NULL)
1287 nfs_set_open_stateid_locked(state, open_stateid, fmode);
1288 write_sequnlock(&state->seqlock);
1289 spin_lock(&state->owner->so_lock);
1290 update_open_stateflags(state, fmode);
1291 spin_unlock(&state->owner->so_lock);
1294 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
1296 struct nfs_inode *nfsi = NFS_I(state->inode);
1297 struct nfs_delegation *deleg_cur;
1300 fmode &= (FMODE_READ|FMODE_WRITE);
1303 deleg_cur = rcu_dereference(nfsi->delegation);
1304 if (deleg_cur == NULL)
1307 spin_lock(&deleg_cur->lock);
1308 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1309 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1310 (deleg_cur->type & fmode) != fmode)
1311 goto no_delegation_unlock;
1313 if (delegation == NULL)
1314 delegation = &deleg_cur->stateid;
1315 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
1316 goto no_delegation_unlock;
1318 nfs_mark_delegation_referenced(deleg_cur);
1319 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
1321 no_delegation_unlock:
1322 spin_unlock(&deleg_cur->lock);
1326 if (!ret && open_stateid != NULL) {
1327 __update_open_stateid(state, open_stateid, NULL, fmode);
1330 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1331 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1336 static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1337 const nfs4_stateid *stateid)
1339 struct nfs4_state *state = lsp->ls_state;
1342 spin_lock(&state->state_lock);
1343 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1345 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1347 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1350 spin_unlock(&state->state_lock);
1354 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1356 struct nfs_delegation *delegation;
1359 delegation = rcu_dereference(NFS_I(inode)->delegation);
1360 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1365 nfs4_inode_return_delegation(inode);
1368 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1370 struct nfs4_state *state = opendata->state;
1371 struct nfs_inode *nfsi = NFS_I(state->inode);
1372 struct nfs_delegation *delegation;
1373 int open_mode = opendata->o_arg.open_flags;
1374 fmode_t fmode = opendata->o_arg.fmode;
1375 nfs4_stateid stateid;
1379 spin_lock(&state->owner->so_lock);
1380 if (can_open_cached(state, fmode, open_mode)) {
1381 update_open_stateflags(state, fmode);
1382 spin_unlock(&state->owner->so_lock);
1383 goto out_return_state;
1385 spin_unlock(&state->owner->so_lock);
1387 delegation = rcu_dereference(nfsi->delegation);
1388 if (!can_open_delegated(delegation, fmode)) {
1392 /* Save the delegation */
1393 nfs4_stateid_copy(&stateid, &delegation->stateid);
1395 nfs_release_seqid(opendata->o_arg.seqid);
1396 if (!opendata->is_recover) {
1397 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1403 /* Try to update the stateid using the delegation */
1404 if (update_open_stateid(state, NULL, &stateid, fmode))
1405 goto out_return_state;
1408 return ERR_PTR(ret);
1410 atomic_inc(&state->count);
1415 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1417 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1418 struct nfs_delegation *delegation;
1419 int delegation_flags = 0;
1422 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1424 delegation_flags = delegation->flags;
1426 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1427 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1428 "returning a delegation for "
1429 "OPEN(CLAIM_DELEGATE_CUR)\n",
1431 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1432 nfs_inode_set_delegation(state->inode,
1433 data->owner->so_cred,
1436 nfs_inode_reclaim_delegation(state->inode,
1437 data->owner->so_cred,
1442 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1443 * and update the nfs4_state.
1445 static struct nfs4_state *
1446 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1448 struct inode *inode = data->state->inode;
1449 struct nfs4_state *state = data->state;
1452 if (!data->rpc_done) {
1453 if (data->rpc_status) {
1454 ret = data->rpc_status;
1457 /* cached opens have already been processed */
1461 ret = nfs_refresh_inode(inode, &data->f_attr);
1465 if (data->o_res.delegation_type != 0)
1466 nfs4_opendata_check_deleg(data, state);
1468 update_open_stateid(state, &data->o_res.stateid, NULL,
1470 atomic_inc(&state->count);
1474 return ERR_PTR(ret);
1478 static struct nfs4_state *
1479 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1481 struct inode *inode;
1482 struct nfs4_state *state = NULL;
1485 if (!data->rpc_done) {
1486 state = nfs4_try_open_cached(data);
1491 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1493 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
1494 ret = PTR_ERR(inode);
1498 state = nfs4_get_open_state(inode, data->owner);
1501 if (data->o_res.delegation_type != 0)
1502 nfs4_opendata_check_deleg(data, state);
1503 update_open_stateid(state, &data->o_res.stateid, NULL,
1507 nfs_release_seqid(data->o_arg.seqid);
1512 return ERR_PTR(ret);
1515 static struct nfs4_state *
1516 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1518 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1519 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1520 return _nfs4_opendata_to_nfs4_state(data);
1523 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1525 struct nfs_inode *nfsi = NFS_I(state->inode);
1526 struct nfs_open_context *ctx;
1528 spin_lock(&state->inode->i_lock);
1529 list_for_each_entry(ctx, &nfsi->open_files, list) {
1530 if (ctx->state != state)
1532 get_nfs_open_context(ctx);
1533 spin_unlock(&state->inode->i_lock);
1536 spin_unlock(&state->inode->i_lock);
1537 return ERR_PTR(-ENOENT);
1540 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1541 struct nfs4_state *state, enum open_claim_type4 claim)
1543 struct nfs4_opendata *opendata;
1545 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
1546 NULL, NULL, claim, GFP_NOFS);
1547 if (opendata == NULL)
1548 return ERR_PTR(-ENOMEM);
1549 opendata->state = state;
1550 atomic_inc(&state->count);
1554 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1556 struct nfs4_state *newstate;
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);
1692 case -NFS4ERR_BADSESSION:
1693 case -NFS4ERR_BADSLOT:
1694 case -NFS4ERR_BAD_HIGH_SLOT:
1695 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1696 case -NFS4ERR_DEADSESSION:
1697 set_bit(NFS_DELEGATED_STATE, &state->flags);
1698 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1700 case -NFS4ERR_STALE_CLIENTID:
1701 case -NFS4ERR_STALE_STATEID:
1702 set_bit(NFS_DELEGATED_STATE, &state->flags);
1703 case -NFS4ERR_EXPIRED:
1704 /* Don't recall a delegation if it was lost */
1705 nfs4_schedule_lease_recovery(server->nfs_client);
1707 case -NFS4ERR_MOVED:
1708 nfs4_schedule_migration_recovery(server);
1710 case -NFS4ERR_LEASE_MOVED:
1711 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1713 case -NFS4ERR_DELEG_REVOKED:
1714 case -NFS4ERR_ADMIN_REVOKED:
1715 case -NFS4ERR_BAD_STATEID:
1716 case -NFS4ERR_OPENMODE:
1717 nfs_inode_find_state_and_recover(state->inode,
1719 nfs4_schedule_stateid_recovery(server, state);
1721 case -NFS4ERR_DELAY:
1722 case -NFS4ERR_GRACE:
1723 set_bit(NFS_DELEGATED_STATE, &state->flags);
1727 case -NFS4ERR_DENIED:
1728 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1734 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1736 struct nfs_server *server = NFS_SERVER(state->inode);
1737 struct nfs4_opendata *opendata;
1740 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1741 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1742 if (IS_ERR(opendata))
1743 return PTR_ERR(opendata);
1744 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1745 err = nfs4_open_recover(opendata, state);
1746 nfs4_opendata_put(opendata);
1747 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1750 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1752 struct nfs4_opendata *data = calldata;
1754 nfs40_setup_sequence(data->o_arg.server->nfs_client->cl_slot_tbl,
1755 &data->c_arg.seq_args, &data->c_res.seq_res, task);
1758 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1760 struct nfs4_opendata *data = calldata;
1762 nfs40_sequence_done(task, &data->c_res.seq_res);
1764 data->rpc_status = task->tk_status;
1765 if (data->rpc_status == 0) {
1766 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
1767 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1768 renew_lease(data->o_res.server, data->timestamp);
1773 static void nfs4_open_confirm_release(void *calldata)
1775 struct nfs4_opendata *data = calldata;
1776 struct nfs4_state *state = NULL;
1778 /* If this request hasn't been cancelled, do nothing */
1779 if (data->cancelled == 0)
1781 /* In case of error, no cleanup! */
1782 if (!data->rpc_done)
1784 state = nfs4_opendata_to_nfs4_state(data);
1786 nfs4_close_state(state, data->o_arg.fmode);
1788 nfs4_opendata_put(data);
1791 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1792 .rpc_call_prepare = nfs4_open_confirm_prepare,
1793 .rpc_call_done = nfs4_open_confirm_done,
1794 .rpc_release = nfs4_open_confirm_release,
1798 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1800 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1802 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
1803 struct rpc_task *task;
1804 struct rpc_message msg = {
1805 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1806 .rpc_argp = &data->c_arg,
1807 .rpc_resp = &data->c_res,
1808 .rpc_cred = data->owner->so_cred,
1810 struct rpc_task_setup task_setup_data = {
1811 .rpc_client = server->client,
1812 .rpc_message = &msg,
1813 .callback_ops = &nfs4_open_confirm_ops,
1814 .callback_data = data,
1815 .workqueue = nfsiod_workqueue,
1816 .flags = RPC_TASK_ASYNC,
1820 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
1821 kref_get(&data->kref);
1823 data->rpc_status = 0;
1824 data->timestamp = jiffies;
1825 task = rpc_run_task(&task_setup_data);
1827 return PTR_ERR(task);
1828 status = nfs4_wait_for_completion_rpc_task(task);
1830 data->cancelled = 1;
1833 status = data->rpc_status;
1838 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1840 struct nfs4_opendata *data = calldata;
1841 struct nfs4_state_owner *sp = data->owner;
1842 struct nfs_client *clp = sp->so_server->nfs_client;
1844 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1847 * Check if we still need to send an OPEN call, or if we can use
1848 * a delegation instead.
1850 if (data->state != NULL) {
1851 struct nfs_delegation *delegation;
1853 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1856 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1857 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1858 data->o_arg.claim != NFS4_OPEN_CLAIM_DELEG_CUR_FH &&
1859 can_open_delegated(delegation, data->o_arg.fmode))
1860 goto unlock_no_action;
1863 /* Update client id. */
1864 data->o_arg.clientid = clp->cl_clientid;
1865 switch (data->o_arg.claim) {
1866 case NFS4_OPEN_CLAIM_PREVIOUS:
1867 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1868 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1869 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
1870 case NFS4_OPEN_CLAIM_FH:
1871 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1872 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1874 data->timestamp = jiffies;
1875 if (nfs4_setup_sequence(data->o_arg.server,
1876 &data->o_arg.seq_args,
1877 &data->o_res.seq_res,
1879 nfs_release_seqid(data->o_arg.seqid);
1881 /* Set the create mode (note dependency on the session type) */
1882 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1883 if (data->o_arg.open_flags & O_EXCL) {
1884 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1885 if (nfs4_has_persistent_session(clp))
1886 data->o_arg.createmode = NFS4_CREATE_GUARDED;
1887 else if (clp->cl_mvops->minor_version > 0)
1888 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1894 task->tk_action = NULL;
1896 nfs4_sequence_done(task, &data->o_res.seq_res);
1899 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1901 struct nfs4_opendata *data = calldata;
1903 data->rpc_status = task->tk_status;
1905 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1908 if (task->tk_status == 0) {
1909 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1910 switch (data->o_res.f_attr->mode & S_IFMT) {
1914 data->rpc_status = -ELOOP;
1917 data->rpc_status = -EISDIR;
1920 data->rpc_status = -ENOTDIR;
1923 renew_lease(data->o_res.server, data->timestamp);
1924 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1925 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1930 static void nfs4_open_release(void *calldata)
1932 struct nfs4_opendata *data = calldata;
1933 struct nfs4_state *state = NULL;
1935 /* If this request hasn't been cancelled, do nothing */
1936 if (data->cancelled == 0)
1938 /* In case of error, no cleanup! */
1939 if (data->rpc_status != 0 || !data->rpc_done)
1941 /* In case we need an open_confirm, no cleanup! */
1942 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1944 state = nfs4_opendata_to_nfs4_state(data);
1946 nfs4_close_state(state, data->o_arg.fmode);
1948 nfs4_opendata_put(data);
1951 static const struct rpc_call_ops nfs4_open_ops = {
1952 .rpc_call_prepare = nfs4_open_prepare,
1953 .rpc_call_done = nfs4_open_done,
1954 .rpc_release = nfs4_open_release,
1957 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
1959 struct inode *dir = d_inode(data->dir);
1960 struct nfs_server *server = NFS_SERVER(dir);
1961 struct nfs_openargs *o_arg = &data->o_arg;
1962 struct nfs_openres *o_res = &data->o_res;
1963 struct rpc_task *task;
1964 struct rpc_message msg = {
1965 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1968 .rpc_cred = data->owner->so_cred,
1970 struct rpc_task_setup task_setup_data = {
1971 .rpc_client = server->client,
1972 .rpc_message = &msg,
1973 .callback_ops = &nfs4_open_ops,
1974 .callback_data = data,
1975 .workqueue = nfsiod_workqueue,
1976 .flags = RPC_TASK_ASYNC,
1980 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
1981 kref_get(&data->kref);
1983 data->rpc_status = 0;
1984 data->cancelled = 0;
1985 data->is_recover = 0;
1987 nfs4_set_sequence_privileged(&o_arg->seq_args);
1988 data->is_recover = 1;
1990 task = rpc_run_task(&task_setup_data);
1992 return PTR_ERR(task);
1993 status = nfs4_wait_for_completion_rpc_task(task);
1995 data->cancelled = 1;
1998 status = data->rpc_status;
2004 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2006 struct inode *dir = d_inode(data->dir);
2007 struct nfs_openres *o_res = &data->o_res;
2010 status = nfs4_run_open_task(data, 1);
2011 if (status != 0 || !data->rpc_done)
2014 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2016 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2017 status = _nfs4_proc_open_confirm(data);
2026 * Additional permission checks in order to distinguish between an
2027 * open for read, and an open for execute. This works around the
2028 * fact that NFSv4 OPEN treats read and execute permissions as being
2030 * Note that in the non-execute case, we want to turn off permission
2031 * checking if we just created a new file (POSIX open() semantics).
2033 static int nfs4_opendata_access(struct rpc_cred *cred,
2034 struct nfs4_opendata *opendata,
2035 struct nfs4_state *state, fmode_t fmode,
2038 struct nfs_access_entry cache;
2041 /* access call failed or for some reason the server doesn't
2042 * support any access modes -- defer access call until later */
2043 if (opendata->o_res.access_supported == 0)
2048 * Use openflags to check for exec, because fmode won't
2049 * always have FMODE_EXEC set when file open for exec.
2051 if (openflags & __FMODE_EXEC) {
2052 /* ONLY check for exec rights */
2054 } else if ((fmode & FMODE_READ) && !opendata->file_created)
2058 cache.jiffies = jiffies;
2059 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2060 nfs_access_add_cache(state->inode, &cache);
2062 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
2065 /* even though OPEN succeeded, access is denied. Close the file */
2066 nfs4_close_state(state, fmode);
2071 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2073 static int _nfs4_proc_open(struct nfs4_opendata *data)
2075 struct inode *dir = d_inode(data->dir);
2076 struct nfs_server *server = NFS_SERVER(dir);
2077 struct nfs_openargs *o_arg = &data->o_arg;
2078 struct nfs_openres *o_res = &data->o_res;
2081 status = nfs4_run_open_task(data, 0);
2082 if (!data->rpc_done)
2085 if (status == -NFS4ERR_BADNAME &&
2086 !(o_arg->open_flags & O_CREAT))
2091 nfs_fattr_map_and_free_names(server, &data->f_attr);
2093 if (o_arg->open_flags & O_CREAT) {
2094 update_changeattr(dir, &o_res->cinfo);
2095 if (o_arg->open_flags & O_EXCL)
2096 data->file_created = 1;
2097 else if (o_res->cinfo.before != o_res->cinfo.after)
2098 data->file_created = 1;
2100 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2101 server->caps &= ~NFS_CAP_POSIX_LOCK;
2102 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2103 status = _nfs4_proc_open_confirm(data);
2107 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
2108 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
2112 static int nfs4_recover_expired_lease(struct nfs_server *server)
2114 return nfs4_client_recover_expired_lease(server->nfs_client);
2119 * reclaim state on the server after a network partition.
2120 * Assumes caller holds the appropriate lock
2122 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2124 struct nfs4_opendata *opendata;
2127 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2128 NFS4_OPEN_CLAIM_FH);
2129 if (IS_ERR(opendata))
2130 return PTR_ERR(opendata);
2131 ret = nfs4_open_recover(opendata, state);
2133 d_drop(ctx->dentry);
2134 nfs4_opendata_put(opendata);
2138 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2140 struct nfs_server *server = NFS_SERVER(state->inode);
2141 struct nfs4_exception exception = { };
2145 err = _nfs4_open_expired(ctx, state);
2146 trace_nfs4_open_expired(ctx, 0, err);
2147 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2152 case -NFS4ERR_GRACE:
2153 case -NFS4ERR_DELAY:
2154 nfs4_handle_exception(server, err, &exception);
2157 } while (exception.retry);
2162 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2164 struct nfs_open_context *ctx;
2167 ctx = nfs4_state_find_open_context(state);
2170 ret = nfs4_do_open_expired(ctx, state);
2171 put_nfs_open_context(ctx);
2175 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state)
2177 nfs_remove_bad_delegation(state->inode);
2178 write_seqlock(&state->seqlock);
2179 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2180 write_sequnlock(&state->seqlock);
2181 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2184 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2186 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2187 nfs_finish_clear_delegation_stateid(state);
2190 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2192 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2193 nfs40_clear_delegation_stateid(state);
2194 return nfs4_open_expired(sp, state);
2197 #if defined(CONFIG_NFS_V4_1)
2198 static void nfs41_check_delegation_stateid(struct nfs4_state *state)
2200 struct nfs_server *server = NFS_SERVER(state->inode);
2201 nfs4_stateid stateid;
2202 struct nfs_delegation *delegation;
2203 struct rpc_cred *cred;
2206 /* Get the delegation credential for use by test/free_stateid */
2208 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2209 if (delegation == NULL) {
2214 nfs4_stateid_copy(&stateid, &delegation->stateid);
2215 cred = get_rpccred(delegation->cred);
2217 status = nfs41_test_stateid(server, &stateid, cred);
2218 trace_nfs4_test_delegation_stateid(state, NULL, status);
2220 if (status != NFS_OK) {
2221 /* Free the stateid unless the server explicitly
2222 * informs us the stateid is unrecognized. */
2223 if (status != -NFS4ERR_BAD_STATEID)
2224 nfs41_free_stateid(server, &stateid, cred);
2225 nfs_finish_clear_delegation_stateid(state);
2232 * nfs41_check_open_stateid - possibly free an open stateid
2234 * @state: NFSv4 state for an inode
2236 * Returns NFS_OK if recovery for this stateid is now finished.
2237 * Otherwise a negative NFS4ERR value is returned.
2239 static int nfs41_check_open_stateid(struct nfs4_state *state)
2241 struct nfs_server *server = NFS_SERVER(state->inode);
2242 nfs4_stateid *stateid = &state->open_stateid;
2243 struct rpc_cred *cred = state->owner->so_cred;
2246 /* If a state reset has been done, test_stateid is unneeded */
2247 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2248 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2249 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2250 return -NFS4ERR_BAD_STATEID;
2252 status = nfs41_test_stateid(server, stateid, cred);
2253 trace_nfs4_test_open_stateid(state, NULL, status);
2254 if (status != NFS_OK) {
2255 /* Free the stateid unless the server explicitly
2256 * informs us the stateid is unrecognized. */
2257 if (status != -NFS4ERR_BAD_STATEID)
2258 nfs41_free_stateid(server, stateid, cred);
2260 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2261 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2262 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2263 clear_bit(NFS_OPEN_STATE, &state->flags);
2268 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2272 nfs41_check_delegation_stateid(state);
2273 status = nfs41_check_open_stateid(state);
2274 if (status != NFS_OK)
2275 status = nfs4_open_expired(sp, state);
2281 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2282 * fields corresponding to attributes that were used to store the verifier.
2283 * Make sure we clobber those fields in the later setattr call
2285 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
2287 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2288 !(sattr->ia_valid & ATTR_ATIME_SET))
2289 sattr->ia_valid |= ATTR_ATIME;
2291 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2292 !(sattr->ia_valid & ATTR_MTIME_SET))
2293 sattr->ia_valid |= ATTR_MTIME;
2296 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2299 struct nfs_open_context *ctx)
2301 struct nfs4_state_owner *sp = opendata->owner;
2302 struct nfs_server *server = sp->so_server;
2303 struct dentry *dentry;
2304 struct nfs4_state *state;
2308 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2310 ret = _nfs4_proc_open(opendata);
2314 state = nfs4_opendata_to_nfs4_state(opendata);
2315 ret = PTR_ERR(state);
2318 if (server->caps & NFS_CAP_POSIX_LOCK)
2319 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2321 dentry = opendata->dentry;
2322 if (d_really_is_negative(dentry)) {
2323 /* FIXME: Is this d_drop() ever needed? */
2325 dentry = d_add_unique(dentry, igrab(state->inode));
2326 if (dentry == NULL) {
2327 dentry = opendata->dentry;
2328 } else if (dentry != ctx->dentry) {
2330 ctx->dentry = dget(dentry);
2332 nfs_set_verifier(dentry,
2333 nfs_save_change_attribute(d_inode(opendata->dir)));
2336 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2341 if (d_inode(dentry) == state->inode) {
2342 nfs_inode_attach_open_context(ctx);
2343 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2344 nfs4_schedule_stateid_recovery(server, state);
2351 * Returns a referenced nfs4_state
2353 static int _nfs4_do_open(struct inode *dir,
2354 struct nfs_open_context *ctx,
2356 struct iattr *sattr,
2357 struct nfs4_label *label,
2360 struct nfs4_state_owner *sp;
2361 struct nfs4_state *state = NULL;
2362 struct nfs_server *server = NFS_SERVER(dir);
2363 struct nfs4_opendata *opendata;
2364 struct dentry *dentry = ctx->dentry;
2365 struct rpc_cred *cred = ctx->cred;
2366 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2367 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
2368 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
2369 struct nfs4_label *olabel = NULL;
2372 /* Protect against reboot recovery conflicts */
2374 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2376 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2379 status = nfs4_recover_expired_lease(server);
2381 goto err_put_state_owner;
2382 if (d_really_is_positive(dentry))
2383 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
2385 if (d_really_is_positive(dentry))
2386 claim = NFS4_OPEN_CLAIM_FH;
2387 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
2388 label, claim, GFP_KERNEL);
2389 if (opendata == NULL)
2390 goto err_put_state_owner;
2393 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2394 if (IS_ERR(olabel)) {
2395 status = PTR_ERR(olabel);
2396 goto err_opendata_put;
2400 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2401 if (!opendata->f_attr.mdsthreshold) {
2402 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2403 if (!opendata->f_attr.mdsthreshold)
2404 goto err_free_label;
2406 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
2408 if (d_really_is_positive(dentry))
2409 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
2411 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
2413 goto err_free_label;
2416 if ((opendata->o_arg.open_flags & O_EXCL) &&
2417 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
2418 nfs4_exclusive_attrset(opendata, sattr);
2420 nfs_fattr_init(opendata->o_res.f_attr);
2421 status = nfs4_do_setattr(state->inode, cred,
2422 opendata->o_res.f_attr, sattr,
2423 state, label, olabel);
2425 nfs_setattr_update_inode(state->inode, sattr,
2426 opendata->o_res.f_attr);
2427 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2430 if (opendata->file_created)
2431 *opened |= FILE_CREATED;
2433 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
2434 *ctx_th = opendata->f_attr.mdsthreshold;
2435 opendata->f_attr.mdsthreshold = NULL;
2438 nfs4_label_free(olabel);
2440 nfs4_opendata_put(opendata);
2441 nfs4_put_state_owner(sp);
2444 nfs4_label_free(olabel);
2446 nfs4_opendata_put(opendata);
2447 err_put_state_owner:
2448 nfs4_put_state_owner(sp);
2454 static struct nfs4_state *nfs4_do_open(struct inode *dir,
2455 struct nfs_open_context *ctx,
2457 struct iattr *sattr,
2458 struct nfs4_label *label,
2461 struct nfs_server *server = NFS_SERVER(dir);
2462 struct nfs4_exception exception = { };
2463 struct nfs4_state *res;
2467 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
2469 trace_nfs4_open_file(ctx, flags, status);
2472 /* NOTE: BAD_SEQID means the server and client disagree about the
2473 * book-keeping w.r.t. state-changing operations
2474 * (OPEN/CLOSE/LOCK/LOCKU...)
2475 * It is actually a sign of a bug on the client or on the server.
2477 * If we receive a BAD_SEQID error in the particular case of
2478 * doing an OPEN, we assume that nfs_increment_open_seqid() will
2479 * have unhashed the old state_owner for us, and that we can
2480 * therefore safely retry using a new one. We should still warn
2481 * the user though...
2483 if (status == -NFS4ERR_BAD_SEQID) {
2484 pr_warn_ratelimited("NFS: v4 server %s "
2485 " returned a bad sequence-id error!\n",
2486 NFS_SERVER(dir)->nfs_client->cl_hostname);
2487 exception.retry = 1;
2491 * BAD_STATEID on OPEN means that the server cancelled our
2492 * state before it received the OPEN_CONFIRM.
2493 * Recover by retrying the request as per the discussion
2494 * on Page 181 of RFC3530.
2496 if (status == -NFS4ERR_BAD_STATEID) {
2497 exception.retry = 1;
2500 if (status == -EAGAIN) {
2501 /* We must have found a delegation */
2502 exception.retry = 1;
2505 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2507 res = ERR_PTR(nfs4_handle_exception(server,
2508 status, &exception));
2509 } while (exception.retry);
2513 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2514 struct nfs_fattr *fattr, struct iattr *sattr,
2515 struct nfs4_state *state, struct nfs4_label *ilabel,
2516 struct nfs4_label *olabel)
2518 struct nfs_server *server = NFS_SERVER(inode);
2519 struct nfs_setattrargs arg = {
2520 .fh = NFS_FH(inode),
2523 .bitmask = server->attr_bitmask,
2526 struct nfs_setattrres res = {
2531 struct rpc_message msg = {
2532 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2537 unsigned long timestamp = jiffies;
2542 arg.bitmask = nfs4_bitmask(server, ilabel);
2544 arg.bitmask = nfs4_bitmask(server, olabel);
2546 nfs_fattr_init(fattr);
2548 /* Servers should only apply open mode checks for file size changes */
2549 truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false;
2550 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2552 if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) {
2553 /* Use that stateid */
2554 } else if (truncate && state != NULL) {
2555 struct nfs_lockowner lockowner = {
2556 .l_owner = current->files,
2557 .l_pid = current->tgid,
2559 if (!nfs4_valid_open_stateid(state))
2561 if (nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
2562 &lockowner) == -EIO)
2565 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
2567 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2568 if (status == 0 && state != NULL)
2569 renew_lease(server, timestamp);
2573 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2574 struct nfs_fattr *fattr, struct iattr *sattr,
2575 struct nfs4_state *state, struct nfs4_label *ilabel,
2576 struct nfs4_label *olabel)
2578 struct nfs_server *server = NFS_SERVER(inode);
2579 struct nfs4_exception exception = {
2585 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state, ilabel, olabel);
2586 trace_nfs4_setattr(inode, err);
2588 case -NFS4ERR_OPENMODE:
2589 if (!(sattr->ia_valid & ATTR_SIZE)) {
2590 pr_warn_once("NFSv4: server %s is incorrectly "
2591 "applying open mode checks to "
2592 "a SETATTR that is not "
2593 "changing file size.\n",
2594 server->nfs_client->cl_hostname);
2596 if (state && !(state->state & FMODE_WRITE)) {
2598 if (sattr->ia_valid & ATTR_OPEN)
2603 err = nfs4_handle_exception(server, err, &exception);
2604 } while (exception.retry);
2609 struct nfs4_closedata {
2610 struct inode *inode;
2611 struct nfs4_state *state;
2612 struct nfs_closeargs arg;
2613 struct nfs_closeres res;
2614 struct nfs_fattr fattr;
2615 unsigned long timestamp;
2620 static void nfs4_free_closedata(void *data)
2622 struct nfs4_closedata *calldata = data;
2623 struct nfs4_state_owner *sp = calldata->state->owner;
2624 struct super_block *sb = calldata->state->inode->i_sb;
2627 pnfs_roc_release(calldata->state->inode);
2628 nfs4_put_open_state(calldata->state);
2629 nfs_free_seqid(calldata->arg.seqid);
2630 nfs4_put_state_owner(sp);
2631 nfs_sb_deactive(sb);
2635 static void nfs4_close_done(struct rpc_task *task, void *data)
2637 struct nfs4_closedata *calldata = data;
2638 struct nfs4_state *state = calldata->state;
2639 struct nfs_server *server = NFS_SERVER(calldata->inode);
2640 nfs4_stateid *res_stateid = NULL;
2642 dprintk("%s: begin!\n", __func__);
2643 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2645 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
2646 /* hmm. we are done with the inode, and in the process of freeing
2647 * the state_owner. we keep this around to process errors
2649 switch (task->tk_status) {
2651 res_stateid = &calldata->res.stateid;
2652 if (calldata->arg.fmode == 0 && calldata->roc)
2653 pnfs_roc_set_barrier(state->inode,
2654 calldata->roc_barrier);
2655 renew_lease(server, calldata->timestamp);
2657 case -NFS4ERR_ADMIN_REVOKED:
2658 case -NFS4ERR_STALE_STATEID:
2659 case -NFS4ERR_OLD_STATEID:
2660 case -NFS4ERR_BAD_STATEID:
2661 case -NFS4ERR_EXPIRED:
2662 if (!nfs4_stateid_match(&calldata->arg.stateid,
2663 &state->open_stateid)) {
2664 rpc_restart_call_prepare(task);
2667 if (calldata->arg.fmode == 0)
2670 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
2671 rpc_restart_call_prepare(task);
2675 nfs_clear_open_stateid(state, res_stateid, calldata->arg.fmode);
2677 nfs_release_seqid(calldata->arg.seqid);
2678 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
2679 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
2682 static void nfs4_close_prepare(struct rpc_task *task, void *data)
2684 struct nfs4_closedata *calldata = data;
2685 struct nfs4_state *state = calldata->state;
2686 struct inode *inode = calldata->inode;
2687 bool is_rdonly, is_wronly, is_rdwr;
2690 dprintk("%s: begin!\n", __func__);
2691 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
2694 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2695 spin_lock(&state->owner->so_lock);
2696 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
2697 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
2698 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
2699 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
2700 /* Calculate the change in open mode */
2701 calldata->arg.fmode = 0;
2702 if (state->n_rdwr == 0) {
2703 if (state->n_rdonly == 0)
2704 call_close |= is_rdonly;
2706 calldata->arg.fmode |= FMODE_READ;
2707 if (state->n_wronly == 0)
2708 call_close |= is_wronly;
2710 calldata->arg.fmode |= FMODE_WRITE;
2712 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
2714 if (calldata->arg.fmode == 0)
2715 call_close |= is_rdwr;
2717 if (!nfs4_valid_open_stateid(state))
2719 spin_unlock(&state->owner->so_lock);
2722 /* Note: exit _without_ calling nfs4_close_done */
2726 if (calldata->arg.fmode == 0) {
2727 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
2728 if (calldata->roc &&
2729 pnfs_roc_drain(inode, &calldata->roc_barrier, task)) {
2730 nfs_release_seqid(calldata->arg.seqid);
2734 calldata->arg.share_access =
2735 nfs4_map_atomic_open_share(NFS_SERVER(inode),
2736 calldata->arg.fmode, 0);
2738 nfs_fattr_init(calldata->res.fattr);
2739 calldata->timestamp = jiffies;
2740 if (nfs4_setup_sequence(NFS_SERVER(inode),
2741 &calldata->arg.seq_args,
2742 &calldata->res.seq_res,
2744 nfs_release_seqid(calldata->arg.seqid);
2745 dprintk("%s: done!\n", __func__);
2748 task->tk_action = NULL;
2750 nfs4_sequence_done(task, &calldata->res.seq_res);
2753 static const struct rpc_call_ops nfs4_close_ops = {
2754 .rpc_call_prepare = nfs4_close_prepare,
2755 .rpc_call_done = nfs4_close_done,
2756 .rpc_release = nfs4_free_closedata,
2759 static bool nfs4_roc(struct inode *inode)
2761 if (!nfs_have_layout(inode))
2763 return pnfs_roc(inode);
2767 * It is possible for data to be read/written from a mem-mapped file
2768 * after the sys_close call (which hits the vfs layer as a flush).
2769 * This means that we can't safely call nfsv4 close on a file until
2770 * the inode is cleared. This in turn means that we are not good
2771 * NFSv4 citizens - we do not indicate to the server to update the file's
2772 * share state even when we are done with one of the three share
2773 * stateid's in the inode.
2775 * NOTE: Caller must be holding the sp->so_owner semaphore!
2777 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
2779 struct nfs_server *server = NFS_SERVER(state->inode);
2780 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
2781 struct nfs4_closedata *calldata;
2782 struct nfs4_state_owner *sp = state->owner;
2783 struct rpc_task *task;
2784 struct rpc_message msg = {
2785 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2786 .rpc_cred = state->owner->so_cred,
2788 struct rpc_task_setup task_setup_data = {
2789 .rpc_client = server->client,
2790 .rpc_message = &msg,
2791 .callback_ops = &nfs4_close_ops,
2792 .workqueue = nfsiod_workqueue,
2793 .flags = RPC_TASK_ASYNC,
2795 int status = -ENOMEM;
2797 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
2798 &task_setup_data.rpc_client, &msg);
2800 calldata = kzalloc(sizeof(*calldata), gfp_mask);
2801 if (calldata == NULL)
2803 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
2804 calldata->inode = state->inode;
2805 calldata->state = state;
2806 calldata->arg.fh = NFS_FH(state->inode);
2807 /* Serialization for the sequence id */
2808 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
2809 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
2810 if (IS_ERR(calldata->arg.seqid))
2811 goto out_free_calldata;
2812 calldata->arg.fmode = 0;
2813 calldata->arg.bitmask = server->cache_consistency_bitmask;
2814 calldata->res.fattr = &calldata->fattr;
2815 calldata->res.seqid = calldata->arg.seqid;
2816 calldata->res.server = server;
2817 calldata->roc = nfs4_roc(state->inode);
2818 nfs_sb_active(calldata->inode->i_sb);
2820 msg.rpc_argp = &calldata->arg;
2821 msg.rpc_resp = &calldata->res;
2822 task_setup_data.callback_data = calldata;
2823 task = rpc_run_task(&task_setup_data);
2825 return PTR_ERR(task);
2828 status = rpc_wait_for_completion_task(task);
2834 nfs4_put_open_state(state);
2835 nfs4_put_state_owner(sp);
2839 static struct inode *
2840 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
2841 int open_flags, struct iattr *attr, int *opened)
2843 struct nfs4_state *state;
2844 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
2846 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
2848 /* Protect against concurrent sillydeletes */
2849 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
2851 nfs4_label_release_security(label);
2854 return ERR_CAST(state);
2855 return state->inode;
2858 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2860 if (ctx->state == NULL)
2863 nfs4_close_sync(ctx->state, ctx->mode);
2865 nfs4_close_state(ctx->state, ctx->mode);
2868 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
2869 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
2870 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
2872 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2874 struct nfs4_server_caps_arg args = {
2877 struct nfs4_server_caps_res res = {};
2878 struct rpc_message msg = {
2879 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2885 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2887 /* Sanity check the server answers */
2888 switch (server->nfs_client->cl_minorversion) {
2890 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
2891 res.attr_bitmask[2] = 0;
2894 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
2897 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
2899 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2900 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2901 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2902 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2903 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2904 NFS_CAP_CTIME|NFS_CAP_MTIME|
2905 NFS_CAP_SECURITY_LABEL);
2906 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
2907 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
2908 server->caps |= NFS_CAP_ACLS;
2909 if (res.has_links != 0)
2910 server->caps |= NFS_CAP_HARDLINKS;
2911 if (res.has_symlinks != 0)
2912 server->caps |= NFS_CAP_SYMLINKS;
2913 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2914 server->caps |= NFS_CAP_FILEID;
2915 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2916 server->caps |= NFS_CAP_MODE;
2917 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2918 server->caps |= NFS_CAP_NLINK;
2919 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2920 server->caps |= NFS_CAP_OWNER;
2921 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2922 server->caps |= NFS_CAP_OWNER_GROUP;
2923 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2924 server->caps |= NFS_CAP_ATIME;
2925 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2926 server->caps |= NFS_CAP_CTIME;
2927 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2928 server->caps |= NFS_CAP_MTIME;
2929 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
2930 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
2931 server->caps |= NFS_CAP_SECURITY_LABEL;
2933 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
2934 sizeof(server->attr_bitmask));
2935 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2937 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2938 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2939 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2940 server->cache_consistency_bitmask[2] = 0;
2941 server->acl_bitmask = res.acl_bitmask;
2942 server->fh_expire_type = res.fh_expire_type;
2948 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2950 struct nfs4_exception exception = { };
2953 err = nfs4_handle_exception(server,
2954 _nfs4_server_capabilities(server, fhandle),
2956 } while (exception.retry);
2960 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2961 struct nfs_fsinfo *info)
2964 struct nfs4_lookup_root_arg args = {
2967 struct nfs4_lookup_res res = {
2969 .fattr = info->fattr,
2972 struct rpc_message msg = {
2973 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2978 bitmask[0] = nfs4_fattr_bitmap[0];
2979 bitmask[1] = nfs4_fattr_bitmap[1];
2981 * Process the label in the upcoming getfattr
2983 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
2985 nfs_fattr_init(info->fattr);
2986 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2989 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2990 struct nfs_fsinfo *info)
2992 struct nfs4_exception exception = { };
2995 err = _nfs4_lookup_root(server, fhandle, info);
2996 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
2999 case -NFS4ERR_WRONGSEC:
3002 err = nfs4_handle_exception(server, err, &exception);
3004 } while (exception.retry);
3009 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3010 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3012 struct rpc_auth_create_args auth_args = {
3013 .pseudoflavor = flavor,
3015 struct rpc_auth *auth;
3018 auth = rpcauth_create(&auth_args, server->client);
3023 ret = nfs4_lookup_root(server, fhandle, info);
3029 * Retry pseudoroot lookup with various security flavors. We do this when:
3031 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3032 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3034 * Returns zero on success, or a negative NFS4ERR value, or a
3035 * negative errno value.
3037 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3038 struct nfs_fsinfo *info)
3040 /* Per 3530bis 15.33.5 */
3041 static const rpc_authflavor_t flav_array[] = {
3045 RPC_AUTH_UNIX, /* courtesy */
3048 int status = -EPERM;
3051 if (server->auth_info.flavor_len > 0) {
3052 /* try each flavor specified by user */
3053 for (i = 0; i < server->auth_info.flavor_len; i++) {
3054 status = nfs4_lookup_root_sec(server, fhandle, info,
3055 server->auth_info.flavors[i]);
3056 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3061 /* no flavors specified by user, try default list */
3062 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3063 status = nfs4_lookup_root_sec(server, fhandle, info,
3065 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3072 * -EACCESS could mean that the user doesn't have correct permissions
3073 * to access the mount. It could also mean that we tried to mount
3074 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3075 * existing mount programs don't handle -EACCES very well so it should
3076 * be mapped to -EPERM instead.
3078 if (status == -EACCES)
3083 static int nfs4_do_find_root_sec(struct nfs_server *server,
3084 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
3086 int mv = server->nfs_client->cl_minorversion;
3087 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
3091 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3092 * @server: initialized nfs_server handle
3093 * @fhandle: we fill in the pseudo-fs root file handle
3094 * @info: we fill in an FSINFO struct
3095 * @auth_probe: probe the auth flavours
3097 * Returns zero on success, or a negative errno.
3099 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
3100 struct nfs_fsinfo *info,
3106 status = nfs4_lookup_root(server, fhandle, info);
3108 if (auth_probe || status == NFS4ERR_WRONGSEC)
3109 status = nfs4_do_find_root_sec(server, fhandle, info);
3112 status = nfs4_server_capabilities(server, fhandle);
3114 status = nfs4_do_fsinfo(server, fhandle, info);
3116 return nfs4_map_errors(status);
3119 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3120 struct nfs_fsinfo *info)
3123 struct nfs_fattr *fattr = info->fattr;
3124 struct nfs4_label *label = NULL;
3126 error = nfs4_server_capabilities(server, mntfh);
3128 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3132 label = nfs4_label_alloc(server, GFP_KERNEL);
3134 return PTR_ERR(label);
3136 error = nfs4_proc_getattr(server, mntfh, fattr, label);
3138 dprintk("nfs4_get_root: getattr error = %d\n", -error);
3139 goto err_free_label;
3142 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3143 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3144 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3147 nfs4_label_free(label);
3153 * Get locations and (maybe) other attributes of a referral.
3154 * Note that we'll actually follow the referral later when
3155 * we detect fsid mismatch in inode revalidation
3157 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3158 const struct qstr *name, struct nfs_fattr *fattr,
3159 struct nfs_fh *fhandle)
3161 int status = -ENOMEM;
3162 struct page *page = NULL;
3163 struct nfs4_fs_locations *locations = NULL;
3165 page = alloc_page(GFP_KERNEL);
3168 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3169 if (locations == NULL)
3172 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
3177 * If the fsid didn't change, this is a migration event, not a
3178 * referral. Cause us to drop into the exception handler, which
3179 * will kick off migration recovery.
3181 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
3182 dprintk("%s: server did not return a different fsid for"
3183 " a referral at %s\n", __func__, name->name);
3184 status = -NFS4ERR_MOVED;
3187 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3188 nfs_fixup_referral_attributes(&locations->fattr);
3190 /* replace the lookup nfs_fattr with the locations nfs_fattr */
3191 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
3192 memset(fhandle, 0, sizeof(struct nfs_fh));
3200 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3201 struct nfs_fattr *fattr, struct nfs4_label *label)
3203 struct nfs4_getattr_arg args = {
3205 .bitmask = server->attr_bitmask,
3207 struct nfs4_getattr_res res = {
3212 struct rpc_message msg = {
3213 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3218 args.bitmask = nfs4_bitmask(server, label);
3220 nfs_fattr_init(fattr);
3221 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3224 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3225 struct nfs_fattr *fattr, struct nfs4_label *label)
3227 struct nfs4_exception exception = { };
3230 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3231 trace_nfs4_getattr(server, fhandle, fattr, err);
3232 err = nfs4_handle_exception(server, err,
3234 } while (exception.retry);
3239 * The file is not closed if it is opened due to the a request to change
3240 * the size of the file. The open call will not be needed once the
3241 * VFS layer lookup-intents are implemented.
3243 * Close is called when the inode is destroyed.
3244 * If we haven't opened the file for O_WRONLY, we
3245 * need to in the size_change case to obtain a stateid.
3248 * Because OPEN is always done by name in nfsv4, it is
3249 * possible that we opened a different file by the same
3250 * name. We can recognize this race condition, but we
3251 * can't do anything about it besides returning an error.
3253 * This will be fixed with VFS changes (lookup-intent).
3256 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3257 struct iattr *sattr)
3259 struct inode *inode = d_inode(dentry);
3260 struct rpc_cred *cred = NULL;
3261 struct nfs4_state *state = NULL;
3262 struct nfs4_label *label = NULL;
3265 if (pnfs_ld_layoutret_on_setattr(inode) &&
3266 sattr->ia_valid & ATTR_SIZE &&
3267 sattr->ia_size < i_size_read(inode))
3268 pnfs_commit_and_return_layout(inode);
3270 nfs_fattr_init(fattr);
3272 /* Deal with open(O_TRUNC) */
3273 if (sattr->ia_valid & ATTR_OPEN)
3274 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
3276 /* Optimization: if the end result is no change, don't RPC */
3277 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
3280 /* Search for an existing open(O_WRITE) file */
3281 if (sattr->ia_valid & ATTR_FILE) {
3282 struct nfs_open_context *ctx;
3284 ctx = nfs_file_open_context(sattr->ia_file);
3291 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3293 return PTR_ERR(label);
3295 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
3297 nfs_setattr_update_inode(inode, sattr, fattr);
3298 nfs_setsecurity(inode, fattr, label);
3300 nfs4_label_free(label);
3304 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3305 const struct qstr *name, struct nfs_fh *fhandle,
3306 struct nfs_fattr *fattr, struct nfs4_label *label)
3308 struct nfs_server *server = NFS_SERVER(dir);
3310 struct nfs4_lookup_arg args = {
3311 .bitmask = server->attr_bitmask,
3312 .dir_fh = NFS_FH(dir),
3315 struct nfs4_lookup_res res = {
3321 struct rpc_message msg = {
3322 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3327 args.bitmask = nfs4_bitmask(server, label);
3329 nfs_fattr_init(fattr);
3331 dprintk("NFS call lookup %s\n", name->name);
3332 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
3333 dprintk("NFS reply lookup: %d\n", status);
3337 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
3339 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
3340 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
3341 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3345 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3346 struct qstr *name, struct nfs_fh *fhandle,
3347 struct nfs_fattr *fattr, struct nfs4_label *label)
3349 struct nfs4_exception exception = { };
3350 struct rpc_clnt *client = *clnt;
3353 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
3354 trace_nfs4_lookup(dir, name, err);
3356 case -NFS4ERR_BADNAME:
3359 case -NFS4ERR_MOVED:
3360 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
3362 case -NFS4ERR_WRONGSEC:
3364 if (client != *clnt)
3366 client = nfs4_negotiate_security(client, dir, name);
3368 return PTR_ERR(client);
3370 exception.retry = 1;
3373 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3375 } while (exception.retry);
3380 else if (client != *clnt)
3381 rpc_shutdown_client(client);
3386 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
3387 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3388 struct nfs4_label *label)
3391 struct rpc_clnt *client = NFS_CLIENT(dir);
3393 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
3394 if (client != NFS_CLIENT(dir)) {
3395 rpc_shutdown_client(client);
3396 nfs_fixup_secinfo_attributes(fattr);
3402 nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
3403 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3405 struct rpc_clnt *client = NFS_CLIENT(dir);
3408 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
3410 return ERR_PTR(status);
3411 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
3414 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3416 struct nfs_server *server = NFS_SERVER(inode);
3417 struct nfs4_accessargs args = {
3418 .fh = NFS_FH(inode),
3419 .bitmask = server->cache_consistency_bitmask,
3421 struct nfs4_accessres res = {
3424 struct rpc_message msg = {
3425 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3428 .rpc_cred = entry->cred,
3430 int mode = entry->mask;
3434 * Determine which access bits we want to ask for...
3436 if (mode & MAY_READ)
3437 args.access |= NFS4_ACCESS_READ;
3438 if (S_ISDIR(inode->i_mode)) {
3439 if (mode & MAY_WRITE)
3440 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3441 if (mode & MAY_EXEC)
3442 args.access |= NFS4_ACCESS_LOOKUP;
3444 if (mode & MAY_WRITE)
3445 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3446 if (mode & MAY_EXEC)
3447 args.access |= NFS4_ACCESS_EXECUTE;
3450 res.fattr = nfs_alloc_fattr();
3451 if (res.fattr == NULL)
3454 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3456 nfs_access_set_mask(entry, res.access);
3457 nfs_refresh_inode(inode, res.fattr);
3459 nfs_free_fattr(res.fattr);
3463 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3465 struct nfs4_exception exception = { };
3468 err = _nfs4_proc_access(inode, entry);
3469 trace_nfs4_access(inode, err);
3470 err = nfs4_handle_exception(NFS_SERVER(inode), err,
3472 } while (exception.retry);
3477 * TODO: For the time being, we don't try to get any attributes
3478 * along with any of the zero-copy operations READ, READDIR,
3481 * In the case of the first three, we want to put the GETATTR
3482 * after the read-type operation -- this is because it is hard
3483 * to predict the length of a GETATTR response in v4, and thus
3484 * align the READ data correctly. This means that the GETATTR
3485 * may end up partially falling into the page cache, and we should
3486 * shift it into the 'tail' of the xdr_buf before processing.
3487 * To do this efficiently, we need to know the total length
3488 * of data received, which doesn't seem to be available outside
3491 * In the case of WRITE, we also want to put the GETATTR after
3492 * the operation -- in this case because we want to make sure
3493 * we get the post-operation mtime and size.
3495 * Both of these changes to the XDR layer would in fact be quite
3496 * minor, but I decided to leave them for a subsequent patch.
3498 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3499 unsigned int pgbase, unsigned int pglen)
3501 struct nfs4_readlink args = {
3502 .fh = NFS_FH(inode),
3507 struct nfs4_readlink_res res;
3508 struct rpc_message msg = {
3509 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3514 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
3517 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3518 unsigned int pgbase, unsigned int pglen)
3520 struct nfs4_exception exception = { };
3523 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3524 trace_nfs4_readlink(inode, err);
3525 err = nfs4_handle_exception(NFS_SERVER(inode), err,
3527 } while (exception.retry);
3532 * This is just for mknod. open(O_CREAT) will always do ->open_context().
3535 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
3538 struct nfs4_label l, *ilabel = NULL;
3539 struct nfs_open_context *ctx;
3540 struct nfs4_state *state;
3544 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3546 return PTR_ERR(ctx);
3548 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3550 sattr->ia_mode &= ~current_umask();
3551 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, &opened);
3552 if (IS_ERR(state)) {
3553 status = PTR_ERR(state);
3557 nfs4_label_release_security(ilabel);
3558 put_nfs_open_context(ctx);
3562 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3564 struct nfs_server *server = NFS_SERVER(dir);
3565 struct nfs_removeargs args = {
3569 struct nfs_removeres res = {
3572 struct rpc_message msg = {
3573 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3579 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
3581 update_changeattr(dir, &res.cinfo);
3585 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3587 struct nfs4_exception exception = { };
3590 err = _nfs4_proc_remove(dir, name);
3591 trace_nfs4_remove(dir, name, err);
3592 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3594 } while (exception.retry);
3598 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
3600 struct nfs_server *server = NFS_SERVER(dir);
3601 struct nfs_removeargs *args = msg->rpc_argp;
3602 struct nfs_removeres *res = msg->rpc_resp;
3604 res->server = server;
3605 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
3606 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
3608 nfs_fattr_init(res->dir_attr);
3611 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3613 nfs4_setup_sequence(NFS_SERVER(data->dir),
3614 &data->args.seq_args,
3619 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
3621 struct nfs_unlinkdata *data = task->tk_calldata;
3622 struct nfs_removeres *res = &data->res;
3624 if (!nfs4_sequence_done(task, &res->seq_res))
3626 if (nfs4_async_handle_error(task, res->server, NULL,
3627 &data->timeout) == -EAGAIN)
3629 update_changeattr(dir, &res->cinfo);
3633 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3635 struct nfs_server *server = NFS_SERVER(dir);
3636 struct nfs_renameargs *arg = msg->rpc_argp;
3637 struct nfs_renameres *res = msg->rpc_resp;
3639 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
3640 res->server = server;
3641 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
3644 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3646 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3647 &data->args.seq_args,
3652 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3653 struct inode *new_dir)
3655 struct nfs_renamedata *data = task->tk_calldata;
3656 struct nfs_renameres *res = &data->res;
3658 if (!nfs4_sequence_done(task, &res->seq_res))
3660 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
3663 update_changeattr(old_dir, &res->old_cinfo);
3664 update_changeattr(new_dir, &res->new_cinfo);
3668 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3670 struct nfs_server *server = NFS_SERVER(inode);
3671 struct nfs4_link_arg arg = {
3672 .fh = NFS_FH(inode),
3673 .dir_fh = NFS_FH(dir),
3675 .bitmask = server->attr_bitmask,
3677 struct nfs4_link_res res = {
3681 struct rpc_message msg = {
3682 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3686 int status = -ENOMEM;
3688 res.fattr = nfs_alloc_fattr();
3689 if (res.fattr == NULL)
3692 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3693 if (IS_ERR(res.label)) {
3694 status = PTR_ERR(res.label);
3697 arg.bitmask = nfs4_bitmask(server, res.label);
3699 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3701 update_changeattr(dir, &res.cinfo);
3702 status = nfs_post_op_update_inode(inode, res.fattr);
3704 nfs_setsecurity(inode, res.fattr, res.label);
3708 nfs4_label_free(res.label);
3711 nfs_free_fattr(res.fattr);
3715 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3717 struct nfs4_exception exception = { };
3720 err = nfs4_handle_exception(NFS_SERVER(inode),
3721 _nfs4_proc_link(inode, dir, name),
3723 } while (exception.retry);
3727 struct nfs4_createdata {
3728 struct rpc_message msg;
3729 struct nfs4_create_arg arg;
3730 struct nfs4_create_res res;
3732 struct nfs_fattr fattr;
3733 struct nfs4_label *label;
3736 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3737 struct qstr *name, struct iattr *sattr, u32 ftype)
3739 struct nfs4_createdata *data;
3741 data = kzalloc(sizeof(*data), GFP_KERNEL);
3743 struct nfs_server *server = NFS_SERVER(dir);
3745 data->label = nfs4_label_alloc(server, GFP_KERNEL);
3746 if (IS_ERR(data->label))
3749 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3750 data->msg.rpc_argp = &data->arg;
3751 data->msg.rpc_resp = &data->res;
3752 data->arg.dir_fh = NFS_FH(dir);
3753 data->arg.server = server;
3754 data->arg.name = name;
3755 data->arg.attrs = sattr;
3756 data->arg.ftype = ftype;
3757 data->arg.bitmask = nfs4_bitmask(server, data->label);
3758 data->res.server = server;
3759 data->res.fh = &data->fh;
3760 data->res.fattr = &data->fattr;
3761 data->res.label = data->label;
3762 nfs_fattr_init(data->res.fattr);
3770 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3772 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
3773 &data->arg.seq_args, &data->res.seq_res, 1);
3775 update_changeattr(dir, &data->res.dir_cinfo);
3776 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
3781 static void nfs4_free_createdata(struct nfs4_createdata *data)
3783 nfs4_label_free(data->label);
3787 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3788 struct page *page, unsigned int len, struct iattr *sattr,
3789 struct nfs4_label *label)
3791 struct nfs4_createdata *data;
3792 int status = -ENAMETOOLONG;
3794 if (len > NFS4_MAXPATHLEN)
3798 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3802 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3803 data->arg.u.symlink.pages = &page;
3804 data->arg.u.symlink.len = len;
3805 data->arg.label = label;
3807 status = nfs4_do_create(dir, dentry, data);
3809 nfs4_free_createdata(data);
3814 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3815 struct page *page, unsigned int len, struct iattr *sattr)
3817 struct nfs4_exception exception = { };
3818 struct nfs4_label l, *label = NULL;
3821 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3824 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
3825 trace_nfs4_symlink(dir, &dentry->d_name, err);
3826 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3828 } while (exception.retry);
3830 nfs4_label_release_security(label);
3834 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3835 struct iattr *sattr, struct nfs4_label *label)
3837 struct nfs4_createdata *data;
3838 int status = -ENOMEM;
3840 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3844 data->arg.label = label;
3845 status = nfs4_do_create(dir, dentry, data);
3847 nfs4_free_createdata(data);
3852 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3853 struct iattr *sattr)
3855 struct nfs4_exception exception = { };
3856 struct nfs4_label l, *label = NULL;
3859 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3861 sattr->ia_mode &= ~current_umask();
3863 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
3864 trace_nfs4_mkdir(dir, &dentry->d_name, err);
3865 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3867 } while (exception.retry);
3868 nfs4_label_release_security(label);
3873 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3874 u64 cookie, struct page **pages, unsigned int count, int plus)
3876 struct inode *dir = d_inode(dentry);
3877 struct nfs4_readdir_arg args = {
3882 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
3885 struct nfs4_readdir_res res;
3886 struct rpc_message msg = {
3887 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3894 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
3896 (unsigned long long)cookie);
3897 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
3898 res.pgbase = args.pgbase;
3899 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
3901 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
3902 status += args.pgbase;
3905 nfs_invalidate_atime(dir);
3907 dprintk("%s: returns %d\n", __func__, status);
3911 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3912 u64 cookie, struct page **pages, unsigned int count, int plus)
3914 struct nfs4_exception exception = { };
3917 err = _nfs4_proc_readdir(dentry, cred, cookie,
3918 pages, count, plus);
3919 trace_nfs4_readdir(d_inode(dentry), err);
3920 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
3922 } while (exception.retry);
3926 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3927 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
3929 struct nfs4_createdata *data;
3930 int mode = sattr->ia_mode;
3931 int status = -ENOMEM;
3933 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3938 data->arg.ftype = NF4FIFO;
3939 else if (S_ISBLK(mode)) {
3940 data->arg.ftype = NF4BLK;
3941 data->arg.u.device.specdata1 = MAJOR(rdev);
3942 data->arg.u.device.specdata2 = MINOR(rdev);
3944 else if (S_ISCHR(mode)) {
3945 data->arg.ftype = NF4CHR;
3946 data->arg.u.device.specdata1 = MAJOR(rdev);
3947 data->arg.u.device.specdata2 = MINOR(rdev);
3948 } else if (!S_ISSOCK(mode)) {
3953 data->arg.label = label;
3954 status = nfs4_do_create(dir, dentry, data);
3956 nfs4_free_createdata(data);
3961 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3962 struct iattr *sattr, dev_t rdev)
3964 struct nfs4_exception exception = { };
3965 struct nfs4_label l, *label = NULL;
3968 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3970 sattr->ia_mode &= ~current_umask();
3972 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
3973 trace_nfs4_mknod(dir, &dentry->d_name, err);
3974 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3976 } while (exception.retry);
3978 nfs4_label_release_security(label);
3983 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3984 struct nfs_fsstat *fsstat)
3986 struct nfs4_statfs_arg args = {
3988 .bitmask = server->attr_bitmask,
3990 struct nfs4_statfs_res res = {
3993 struct rpc_message msg = {
3994 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3999 nfs_fattr_init(fsstat->fattr);
4000 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4003 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4005 struct nfs4_exception exception = { };
4008 err = nfs4_handle_exception(server,
4009 _nfs4_proc_statfs(server, fhandle, fsstat),
4011 } while (exception.retry);
4015 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4016 struct nfs_fsinfo *fsinfo)
4018 struct nfs4_fsinfo_arg args = {
4020 .bitmask = server->attr_bitmask,
4022 struct nfs4_fsinfo_res res = {
4025 struct rpc_message msg = {
4026 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4031 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4034 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4036 struct nfs4_exception exception = { };
4037 unsigned long now = jiffies;
4041 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
4042 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
4044 struct nfs_client *clp = server->nfs_client;
4046 spin_lock(&clp->cl_lock);
4047 clp->cl_lease_time = fsinfo->lease_time * HZ;
4048 clp->cl_last_renewal = now;
4049 spin_unlock(&clp->cl_lock);
4052 err = nfs4_handle_exception(server, err, &exception);
4053 } while (exception.retry);
4057 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4061 nfs_fattr_init(fsinfo->fattr);
4062 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
4064 /* block layout checks this! */
4065 server->pnfs_blksize = fsinfo->blksize;
4066 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
4072 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4073 struct nfs_pathconf *pathconf)
4075 struct nfs4_pathconf_arg args = {
4077 .bitmask = server->attr_bitmask,
4079 struct nfs4_pathconf_res res = {
4080 .pathconf = pathconf,
4082 struct rpc_message msg = {
4083 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4088 /* None of the pathconf attributes are mandatory to implement */
4089 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4090 memset(pathconf, 0, sizeof(*pathconf));
4094 nfs_fattr_init(pathconf->fattr);
4095 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4098 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4099 struct nfs_pathconf *pathconf)
4101 struct nfs4_exception exception = { };
4105 err = nfs4_handle_exception(server,
4106 _nfs4_proc_pathconf(server, fhandle, pathconf),
4108 } while (exception.retry);
4112 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
4113 const struct nfs_open_context *ctx,
4114 const struct nfs_lock_context *l_ctx,
4117 const struct nfs_lockowner *lockowner = NULL;
4120 lockowner = &l_ctx->lockowner;
4121 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
4123 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4125 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4126 const struct nfs_open_context *ctx,
4127 const struct nfs_lock_context *l_ctx,
4130 nfs4_stateid current_stateid;
4132 /* If the current stateid represents a lost lock, then exit */
4133 if (nfs4_set_rw_stateid(¤t_stateid, ctx, l_ctx, fmode) == -EIO)
4135 return nfs4_stateid_match(stateid, ¤t_stateid);
4138 static bool nfs4_error_stateid_expired(int err)
4141 case -NFS4ERR_DELEG_REVOKED:
4142 case -NFS4ERR_ADMIN_REVOKED:
4143 case -NFS4ERR_BAD_STATEID:
4144 case -NFS4ERR_STALE_STATEID:
4145 case -NFS4ERR_OLD_STATEID:
4146 case -NFS4ERR_OPENMODE:
4147 case -NFS4ERR_EXPIRED:
4153 void __nfs4_read_done_cb(struct nfs_pgio_header *hdr)
4155 nfs_invalidate_atime(hdr->inode);
4158 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
4160 struct nfs_server *server = NFS_SERVER(hdr->inode);
4162 trace_nfs4_read(hdr, task->tk_status);
4163 if (nfs4_async_handle_error(task, server,
4164 hdr->args.context->state,
4166 rpc_restart_call_prepare(task);
4170 __nfs4_read_done_cb(hdr);
4171 if (task->tk_status > 0)
4172 renew_lease(server, hdr->timestamp);
4176 static bool nfs4_read_stateid_changed(struct rpc_task *task,
4177 struct nfs_pgio_args *args)
4180 if (!nfs4_error_stateid_expired(task->tk_status) ||
4181 nfs4_stateid_is_current(&args->stateid,
4186 rpc_restart_call_prepare(task);
4190 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
4193 dprintk("--> %s\n", __func__);
4195 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
4197 if (nfs4_read_stateid_changed(task, &hdr->args))
4199 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4200 nfs4_read_done_cb(task, hdr);
4203 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4204 struct rpc_message *msg)
4206 hdr->timestamp = jiffies;
4207 hdr->pgio_done_cb = nfs4_read_done_cb;
4208 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
4209 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
4212 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4213 struct nfs_pgio_header *hdr)
4215 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4216 &hdr->args.seq_args,
4220 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4221 hdr->args.lock_context,
4222 hdr->rw_ops->rw_mode) == -EIO)
4224 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
4229 static int nfs4_write_done_cb(struct rpc_task *task,
4230 struct nfs_pgio_header *hdr)
4232 struct inode *inode = hdr->inode;
4234 trace_nfs4_write(hdr, task->tk_status);
4235 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4236 hdr->args.context->state,
4238 rpc_restart_call_prepare(task);
4241 if (task->tk_status >= 0) {
4242 renew_lease(NFS_SERVER(inode), hdr->timestamp);
4243 nfs_writeback_update_inode(hdr);
4248 static bool nfs4_write_stateid_changed(struct rpc_task *task,
4249 struct nfs_pgio_args *args)
4252 if (!nfs4_error_stateid_expired(task->tk_status) ||
4253 nfs4_stateid_is_current(&args->stateid,
4258 rpc_restart_call_prepare(task);
4262 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
4264 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
4266 if (nfs4_write_stateid_changed(task, &hdr->args))
4268 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4269 nfs4_write_done_cb(task, hdr);
4273 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
4275 /* Don't request attributes for pNFS or O_DIRECT writes */
4276 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
4278 /* Otherwise, request attributes if and only if we don't hold
4281 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
4284 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4285 struct rpc_message *msg)
4287 struct nfs_server *server = NFS_SERVER(hdr->inode);
4289 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4290 hdr->args.bitmask = NULL;
4291 hdr->res.fattr = NULL;
4293 hdr->args.bitmask = server->cache_consistency_bitmask;
4295 if (!hdr->pgio_done_cb)
4296 hdr->pgio_done_cb = nfs4_write_done_cb;
4297 hdr->res.server = server;
4298 hdr->timestamp = jiffies;
4300 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
4301 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
4304 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4306 nfs4_setup_sequence(NFS_SERVER(data->inode),
4307 &data->args.seq_args,
4312 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
4314 struct inode *inode = data->inode;
4316 trace_nfs4_commit(data, task->tk_status);
4317 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4318 NULL, NULL) == -EAGAIN) {
4319 rpc_restart_call_prepare(task);
4325 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
4327 if (!nfs4_sequence_done(task, &data->res.seq_res))
4329 return data->commit_done_cb(task, data);
4332 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
4334 struct nfs_server *server = NFS_SERVER(data->inode);
4336 if (data->commit_done_cb == NULL)
4337 data->commit_done_cb = nfs4_commit_done_cb;
4338 data->res.server = server;
4339 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
4340 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4343 struct nfs4_renewdata {
4344 struct nfs_client *client;
4345 unsigned long timestamp;
4349 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4350 * standalone procedure for queueing an asynchronous RENEW.
4352 static void nfs4_renew_release(void *calldata)
4354 struct nfs4_renewdata *data = calldata;
4355 struct nfs_client *clp = data->client;
4357 if (atomic_read(&clp->cl_count) > 1)
4358 nfs4_schedule_state_renewal(clp);
4359 nfs_put_client(clp);
4363 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
4365 struct nfs4_renewdata *data = calldata;
4366 struct nfs_client *clp = data->client;
4367 unsigned long timestamp = data->timestamp;
4369 trace_nfs4_renew_async(clp, task->tk_status);
4370 switch (task->tk_status) {
4373 case -NFS4ERR_LEASE_MOVED:
4374 nfs4_schedule_lease_moved_recovery(clp);
4377 /* Unless we're shutting down, schedule state recovery! */
4378 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4380 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
4381 nfs4_schedule_lease_recovery(clp);
4384 nfs4_schedule_path_down_recovery(clp);
4386 do_renew_lease(clp, timestamp);
4389 static const struct rpc_call_ops nfs4_renew_ops = {
4390 .rpc_call_done = nfs4_renew_done,
4391 .rpc_release = nfs4_renew_release,
4394 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
4396 struct rpc_message msg = {
4397 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4401 struct nfs4_renewdata *data;
4403 if (renew_flags == 0)
4405 if (!atomic_inc_not_zero(&clp->cl_count))
4407 data = kmalloc(sizeof(*data), GFP_NOFS);
4411 data->timestamp = jiffies;
4412 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
4413 &nfs4_renew_ops, data);
4416 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
4418 struct rpc_message msg = {
4419 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4423 unsigned long now = jiffies;
4426 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4429 do_renew_lease(clp, now);
4433 static inline int nfs4_server_supports_acls(struct nfs_server *server)
4435 return server->caps & NFS_CAP_ACLS;
4438 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4439 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
4442 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
4444 static int buf_to_pages_noslab(const void *buf, size_t buflen,
4445 struct page **pages, unsigned int *pgbase)
4447 struct page *newpage, **spages;
4453 len = min_t(size_t, PAGE_SIZE, buflen);
4454 newpage = alloc_page(GFP_KERNEL);
4456 if (newpage == NULL)
4458 memcpy(page_address(newpage), buf, len);
4463 } while (buflen != 0);
4469 __free_page(spages[rc-1]);
4473 struct nfs4_cached_acl {
4479 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
4481 struct nfs_inode *nfsi = NFS_I(inode);
4483 spin_lock(&inode->i_lock);
4484 kfree(nfsi->nfs4_acl);
4485 nfsi->nfs4_acl = acl;
4486 spin_unlock(&inode->i_lock);
4489 static void nfs4_zap_acl_attr(struct inode *inode)
4491 nfs4_set_cached_acl(inode, NULL);
4494 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4496 struct nfs_inode *nfsi = NFS_I(inode);
4497 struct nfs4_cached_acl *acl;
4500 spin_lock(&inode->i_lock);
4501 acl = nfsi->nfs4_acl;
4504 if (buf == NULL) /* user is just asking for length */
4506 if (acl->cached == 0)
4508 ret = -ERANGE; /* see getxattr(2) man page */
4509 if (acl->len > buflen)
4511 memcpy(buf, acl->data, acl->len);
4515 spin_unlock(&inode->i_lock);
4519 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
4521 struct nfs4_cached_acl *acl;
4522 size_t buflen = sizeof(*acl) + acl_len;
4524 if (buflen <= PAGE_SIZE) {
4525 acl = kmalloc(buflen, GFP_KERNEL);
4529 _copy_from_pages(acl->data, pages, pgbase, acl_len);
4531 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4538 nfs4_set_cached_acl(inode, acl);
4542 * The getxattr API returns the required buffer length when called with a
4543 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4544 * the required buf. On a NULL buf, we send a page of data to the server
4545 * guessing that the ACL request can be serviced by a page. If so, we cache
4546 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4547 * the cache. If not so, we throw away the page, and cache the required
4548 * length. The next getxattr call will then produce another round trip to
4549 * the server, this time with the input buf of the required size.
4551 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4553 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
4554 struct nfs_getaclargs args = {
4555 .fh = NFS_FH(inode),
4559 struct nfs_getaclres res = {
4562 struct rpc_message msg = {
4563 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4567 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4568 int ret = -ENOMEM, i;
4570 /* As long as we're doing a round trip to the server anyway,
4571 * let's be prepared for a page of acl data. */
4574 if (npages > ARRAY_SIZE(pages))
4577 for (i = 0; i < npages; i++) {
4578 pages[i] = alloc_page(GFP_KERNEL);
4583 /* for decoding across pages */
4584 res.acl_scratch = alloc_page(GFP_KERNEL);
4585 if (!res.acl_scratch)
4588 args.acl_len = npages * PAGE_SIZE;
4589 args.acl_pgbase = 0;
4591 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
4592 __func__, buf, buflen, npages, args.acl_len);
4593 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4594 &msg, &args.seq_args, &res.seq_res, 0);
4598 /* Handle the case where the passed-in buffer is too short */
4599 if (res.acl_flags & NFS4_ACL_TRUNC) {
4600 /* Did the user only issue a request for the acl length? */
4606 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
4608 if (res.acl_len > buflen) {
4612 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
4617 for (i = 0; i < npages; i++)
4619 __free_page(pages[i]);
4620 if (res.acl_scratch)
4621 __free_page(res.acl_scratch);
4625 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4627 struct nfs4_exception exception = { };
4630 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
4631 trace_nfs4_get_acl(inode, ret);
4634 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4635 } while (exception.retry);
4639 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4641 struct nfs_server *server = NFS_SERVER(inode);
4644 if (!nfs4_server_supports_acls(server))
4646 ret = nfs_revalidate_inode(server, inode);
4649 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4650 nfs_zap_acl_cache(inode);
4651 ret = nfs4_read_cached_acl(inode, buf, buflen);
4653 /* -ENOENT is returned if there is no ACL or if there is an ACL
4654 * but no cached acl data, just the acl length */
4656 return nfs4_get_acl_uncached(inode, buf, buflen);
4659 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4661 struct nfs_server *server = NFS_SERVER(inode);
4662 struct page *pages[NFS4ACL_MAXPAGES];
4663 struct nfs_setaclargs arg = {
4664 .fh = NFS_FH(inode),
4668 struct nfs_setaclres res;
4669 struct rpc_message msg = {
4670 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4674 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4677 if (!nfs4_server_supports_acls(server))
4679 if (npages > ARRAY_SIZE(pages))
4681 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4684 nfs4_inode_return_delegation(inode);
4685 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4688 * Free each page after tx, so the only ref left is
4689 * held by the network stack
4692 put_page(pages[i-1]);
4695 * Acl update can result in inode attribute update.
4696 * so mark the attribute cache invalid.
4698 spin_lock(&inode->i_lock);
4699 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4700 spin_unlock(&inode->i_lock);
4701 nfs_access_zap_cache(inode);
4702 nfs_zap_acl_cache(inode);
4706 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4708 struct nfs4_exception exception = { };
4711 err = __nfs4_proc_set_acl(inode, buf, buflen);
4712 trace_nfs4_set_acl(inode, err);
4713 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4715 } while (exception.retry);
4719 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
4720 static int _nfs4_get_security_label(struct inode *inode, void *buf,
4723 struct nfs_server *server = NFS_SERVER(inode);
4724 struct nfs_fattr fattr;
4725 struct nfs4_label label = {0, 0, buflen, buf};
4727 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4728 struct nfs4_getattr_arg arg = {
4729 .fh = NFS_FH(inode),
4732 struct nfs4_getattr_res res = {
4737 struct rpc_message msg = {
4738 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4744 nfs_fattr_init(&fattr);
4746 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
4749 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
4751 if (buflen < label.len)
4756 static int nfs4_get_security_label(struct inode *inode, void *buf,
4759 struct nfs4_exception exception = { };
4762 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4766 err = _nfs4_get_security_label(inode, buf, buflen);
4767 trace_nfs4_get_security_label(inode, err);
4768 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4770 } while (exception.retry);
4774 static int _nfs4_do_set_security_label(struct inode *inode,
4775 struct nfs4_label *ilabel,
4776 struct nfs_fattr *fattr,
4777 struct nfs4_label *olabel)
4780 struct iattr sattr = {0};
4781 struct nfs_server *server = NFS_SERVER(inode);
4782 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4783 struct nfs_setattrargs arg = {
4784 .fh = NFS_FH(inode),
4790 struct nfs_setattrres res = {
4795 struct rpc_message msg = {
4796 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
4802 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
4804 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4806 dprintk("%s failed: %d\n", __func__, status);
4811 static int nfs4_do_set_security_label(struct inode *inode,
4812 struct nfs4_label *ilabel,
4813 struct nfs_fattr *fattr,
4814 struct nfs4_label *olabel)
4816 struct nfs4_exception exception = { };
4820 err = _nfs4_do_set_security_label(inode, ilabel,
4822 trace_nfs4_set_security_label(inode, err);
4823 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4825 } while (exception.retry);
4830 nfs4_set_security_label(struct dentry *dentry, const void *buf, size_t buflen)
4832 struct nfs4_label ilabel, *olabel = NULL;
4833 struct nfs_fattr fattr;
4834 struct rpc_cred *cred;
4835 struct inode *inode = d_inode(dentry);
4838 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4841 nfs_fattr_init(&fattr);
4845 ilabel.label = (char *)buf;
4846 ilabel.len = buflen;
4848 cred = rpc_lookup_cred();
4850 return PTR_ERR(cred);
4852 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4853 if (IS_ERR(olabel)) {
4854 status = -PTR_ERR(olabel);
4858 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
4860 nfs_setsecurity(inode, &fattr, olabel);
4862 nfs4_label_free(olabel);
4867 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
4871 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server,
4872 struct nfs4_state *state, long *timeout)
4874 struct nfs_client *clp = server->nfs_client;
4876 if (task->tk_status >= 0)
4878 switch(task->tk_status) {
4879 case -NFS4ERR_DELEG_REVOKED:
4880 case -NFS4ERR_ADMIN_REVOKED:
4881 case -NFS4ERR_BAD_STATEID:
4882 case -NFS4ERR_OPENMODE:
4885 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4886 goto recovery_failed;
4887 goto wait_on_recovery;
4888 case -NFS4ERR_EXPIRED:
4889 if (state != NULL) {
4890 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4891 goto recovery_failed;
4893 case -NFS4ERR_STALE_STATEID:
4894 case -NFS4ERR_STALE_CLIENTID:
4895 nfs4_schedule_lease_recovery(clp);
4896 goto wait_on_recovery;
4897 case -NFS4ERR_MOVED:
4898 if (nfs4_schedule_migration_recovery(server) < 0)
4899 goto recovery_failed;
4900 goto wait_on_recovery;
4901 case -NFS4ERR_LEASE_MOVED:
4902 nfs4_schedule_lease_moved_recovery(clp);
4903 goto wait_on_recovery;
4904 #if defined(CONFIG_NFS_V4_1)
4905 case -NFS4ERR_BADSESSION:
4906 case -NFS4ERR_BADSLOT:
4907 case -NFS4ERR_BAD_HIGH_SLOT:
4908 case -NFS4ERR_DEADSESSION:
4909 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4910 case -NFS4ERR_SEQ_FALSE_RETRY:
4911 case -NFS4ERR_SEQ_MISORDERED:
4912 dprintk("%s ERROR %d, Reset session\n", __func__,
4914 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
4915 goto wait_on_recovery;
4916 #endif /* CONFIG_NFS_V4_1 */
4917 case -NFS4ERR_DELAY:
4918 nfs_inc_server_stats(server, NFSIOS_DELAY);
4919 rpc_delay(task, nfs4_update_delay(timeout));
4921 case -NFS4ERR_GRACE:
4922 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4923 case -NFS4ERR_RETRY_UNCACHED_REP:
4924 case -NFS4ERR_OLD_STATEID:
4927 task->tk_status = nfs4_map_errors(task->tk_status);
4930 task->tk_status = -EIO;
4933 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
4934 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4935 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4936 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
4937 goto recovery_failed;
4939 task->tk_status = 0;
4943 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4944 nfs4_verifier *bootverf)
4948 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4949 /* An impossible timestamp guarantees this value
4950 * will never match a generated boot time. */
4952 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
4954 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4955 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
4956 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
4958 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4962 nfs4_init_nonuniform_client_string(struct nfs_client *clp,
4963 char *buf, size_t len)
4965 unsigned int result;
4967 if (clp->cl_owner_id != NULL)
4968 return strlcpy(buf, clp->cl_owner_id, len);
4971 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4973 rpc_peeraddr2str(clp->cl_rpcclient,
4975 rpc_peeraddr2str(clp->cl_rpcclient,
4976 RPC_DISPLAY_PROTO));
4978 clp->cl_owner_id = kstrdup(buf, GFP_KERNEL);
4983 nfs4_init_uniform_client_string(struct nfs_client *clp,
4984 char *buf, size_t len)
4986 const char *nodename = clp->cl_rpcclient->cl_nodename;
4987 unsigned int result;
4989 if (clp->cl_owner_id != NULL)
4990 return strlcpy(buf, clp->cl_owner_id, len);
4992 if (nfs4_client_id_uniquifier[0] != '\0')
4993 result = scnprintf(buf, len, "Linux NFSv%u.%u %s/%s",
4994 clp->rpc_ops->version,
4995 clp->cl_minorversion,
4996 nfs4_client_id_uniquifier,
4999 result = scnprintf(buf, len, "Linux NFSv%u.%u %s",
5000 clp->rpc_ops->version, clp->cl_minorversion,
5002 clp->cl_owner_id = kstrdup(buf, GFP_KERNEL);
5007 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5008 * services. Advertise one based on the address family of the
5012 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5014 if (strchr(clp->cl_ipaddr, ':') != NULL)
5015 return scnprintf(buf, len, "tcp6");
5017 return scnprintf(buf, len, "tcp");
5020 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5022 struct nfs4_setclientid *sc = calldata;
5024 if (task->tk_status == 0)
5025 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5028 static const struct rpc_call_ops nfs4_setclientid_ops = {
5029 .rpc_call_done = nfs4_setclientid_done,
5033 * nfs4_proc_setclientid - Negotiate client ID
5034 * @clp: state data structure
5035 * @program: RPC program for NFSv4 callback service
5036 * @port: IP port number for NFS4 callback service
5037 * @cred: RPC credential to use for this call
5038 * @res: where to place the result
5040 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5042 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5043 unsigned short port, struct rpc_cred *cred,
5044 struct nfs4_setclientid_res *res)
5046 nfs4_verifier sc_verifier;
5047 struct nfs4_setclientid setclientid = {
5048 .sc_verifier = &sc_verifier,
5050 .sc_cb_ident = clp->cl_cb_ident,
5052 struct rpc_message msg = {
5053 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5054 .rpc_argp = &setclientid,
5058 struct rpc_task *task;
5059 struct rpc_task_setup task_setup_data = {
5060 .rpc_client = clp->cl_rpcclient,
5061 .rpc_message = &msg,
5062 .callback_ops = &nfs4_setclientid_ops,
5063 .callback_data = &setclientid,
5064 .flags = RPC_TASK_TIMEOUT,
5068 /* nfs_client_id4 */
5069 nfs4_init_boot_verifier(clp, &sc_verifier);
5070 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5071 setclientid.sc_name_len =
5072 nfs4_init_uniform_client_string(clp,
5073 setclientid.sc_name,
5074 sizeof(setclientid.sc_name));
5076 setclientid.sc_name_len =
5077 nfs4_init_nonuniform_client_string(clp,
5078 setclientid.sc_name,
5079 sizeof(setclientid.sc_name));
5081 setclientid.sc_netid_len =
5082 nfs4_init_callback_netid(clp,
5083 setclientid.sc_netid,
5084 sizeof(setclientid.sc_netid));
5085 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
5086 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
5087 clp->cl_ipaddr, port >> 8, port & 255);
5089 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
5090 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5091 setclientid.sc_name_len, setclientid.sc_name);
5092 task = rpc_run_task(&task_setup_data);
5094 status = PTR_ERR(task);
5097 status = task->tk_status;
5098 if (setclientid.sc_cred) {
5099 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5100 put_rpccred(setclientid.sc_cred);
5104 trace_nfs4_setclientid(clp, status);
5105 dprintk("NFS reply setclientid: %d\n", status);
5110 * nfs4_proc_setclientid_confirm - Confirm client ID
5111 * @clp: state data structure
5112 * @res: result of a previous SETCLIENTID
5113 * @cred: RPC credential to use for this call
5115 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5117 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
5118 struct nfs4_setclientid_res *arg,
5119 struct rpc_cred *cred)
5121 struct rpc_message msg = {
5122 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
5128 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5129 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5131 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5132 trace_nfs4_setclientid_confirm(clp, status);
5133 dprintk("NFS reply setclientid_confirm: %d\n", status);
5137 struct nfs4_delegreturndata {
5138 struct nfs4_delegreturnargs args;
5139 struct nfs4_delegreturnres res;
5141 nfs4_stateid stateid;
5142 unsigned long timestamp;
5143 struct nfs_fattr fattr;
5145 struct inode *inode;
5150 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5152 struct nfs4_delegreturndata *data = calldata;
5154 if (!nfs4_sequence_done(task, &data->res.seq_res))
5157 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
5158 switch (task->tk_status) {
5160 renew_lease(data->res.server, data->timestamp);
5161 case -NFS4ERR_ADMIN_REVOKED:
5162 case -NFS4ERR_DELEG_REVOKED:
5163 case -NFS4ERR_BAD_STATEID:
5164 case -NFS4ERR_OLD_STATEID:
5165 case -NFS4ERR_STALE_STATEID:
5166 case -NFS4ERR_EXPIRED:
5167 task->tk_status = 0;
5169 pnfs_roc_set_barrier(data->inode, data->roc_barrier);
5172 if (nfs4_async_handle_error(task, data->res.server,
5173 NULL, NULL) == -EAGAIN) {
5174 rpc_restart_call_prepare(task);
5178 data->rpc_status = task->tk_status;
5181 static void nfs4_delegreturn_release(void *calldata)
5183 struct nfs4_delegreturndata *data = calldata;
5184 struct inode *inode = data->inode;
5188 pnfs_roc_release(inode);
5189 nfs_iput_and_deactive(inode);
5194 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5196 struct nfs4_delegreturndata *d_data;
5198 d_data = (struct nfs4_delegreturndata *)data;
5201 pnfs_roc_drain(d_data->inode, &d_data->roc_barrier, task))
5204 nfs4_setup_sequence(d_data->res.server,
5205 &d_data->args.seq_args,
5206 &d_data->res.seq_res,
5210 static const struct rpc_call_ops nfs4_delegreturn_ops = {
5211 .rpc_call_prepare = nfs4_delegreturn_prepare,
5212 .rpc_call_done = nfs4_delegreturn_done,
5213 .rpc_release = nfs4_delegreturn_release,
5216 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
5218 struct nfs4_delegreturndata *data;
5219 struct nfs_server *server = NFS_SERVER(inode);
5220 struct rpc_task *task;
5221 struct rpc_message msg = {
5222 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5225 struct rpc_task_setup task_setup_data = {
5226 .rpc_client = server->client,
5227 .rpc_message = &msg,
5228 .callback_ops = &nfs4_delegreturn_ops,
5229 .flags = RPC_TASK_ASYNC,
5233 data = kzalloc(sizeof(*data), GFP_NOFS);
5236 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
5237 data->args.fhandle = &data->fh;
5238 data->args.stateid = &data->stateid;
5239 data->args.bitmask = server->cache_consistency_bitmask;
5240 nfs_copy_fh(&data->fh, NFS_FH(inode));
5241 nfs4_stateid_copy(&data->stateid, stateid);
5242 data->res.fattr = &data->fattr;
5243 data->res.server = server;
5244 nfs_fattr_init(data->res.fattr);
5245 data->timestamp = jiffies;
5246 data->rpc_status = 0;
5247 data->inode = nfs_igrab_and_active(inode);
5249 data->roc = nfs4_roc(inode);
5251 task_setup_data.callback_data = data;
5252 msg.rpc_argp = &data->args;
5253 msg.rpc_resp = &data->res;
5254 task = rpc_run_task(&task_setup_data);
5256 return PTR_ERR(task);
5259 status = nfs4_wait_for_completion_rpc_task(task);
5262 status = data->rpc_status;
5264 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5266 nfs_refresh_inode(inode, &data->fattr);
5272 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
5274 struct nfs_server *server = NFS_SERVER(inode);
5275 struct nfs4_exception exception = { };
5278 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
5279 trace_nfs4_delegreturn(inode, err);
5281 case -NFS4ERR_STALE_STATEID:
5282 case -NFS4ERR_EXPIRED:
5286 err = nfs4_handle_exception(server, err, &exception);
5287 } while (exception.retry);
5291 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
5292 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
5295 * sleep, with exponential backoff, and retry the LOCK operation.
5297 static unsigned long
5298 nfs4_set_lock_task_retry(unsigned long timeout)
5300 freezable_schedule_timeout_killable_unsafe(timeout);
5302 if (timeout > NFS4_LOCK_MAXTIMEOUT)
5303 return NFS4_LOCK_MAXTIMEOUT;
5307 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5309 struct inode *inode = state->inode;
5310 struct nfs_server *server = NFS_SERVER(inode);
5311 struct nfs_client *clp = server->nfs_client;
5312 struct nfs_lockt_args arg = {
5313 .fh = NFS_FH(inode),
5316 struct nfs_lockt_res res = {
5319 struct rpc_message msg = {
5320 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5323 .rpc_cred = state->owner->so_cred,
5325 struct nfs4_lock_state *lsp;
5328 arg.lock_owner.clientid = clp->cl_clientid;
5329 status = nfs4_set_lock_state(state, request);
5332 lsp = request->fl_u.nfs4_fl.owner;
5333 arg.lock_owner.id = lsp->ls_seqid.owner_id;
5334 arg.lock_owner.s_dev = server->s_dev;
5335 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5338 request->fl_type = F_UNLCK;
5340 case -NFS4ERR_DENIED:
5343 request->fl_ops->fl_release_private(request);
5344 request->fl_ops = NULL;
5349 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5351 struct nfs4_exception exception = { };
5355 err = _nfs4_proc_getlk(state, cmd, request);
5356 trace_nfs4_get_lock(request, state, cmd, err);
5357 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
5359 } while (exception.retry);
5363 static int do_vfs_lock(struct file *file, struct file_lock *fl)
5366 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
5368 res = posix_lock_file_wait(file, fl);
5371 res = flock_lock_file_wait(file, fl);
5379 struct nfs4_unlockdata {
5380 struct nfs_locku_args arg;
5381 struct nfs_locku_res res;
5382 struct nfs4_lock_state *lsp;
5383 struct nfs_open_context *ctx;
5384 struct file_lock fl;
5385 const struct nfs_server *server;
5386 unsigned long timestamp;
5389 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5390 struct nfs_open_context *ctx,
5391 struct nfs4_lock_state *lsp,
5392 struct nfs_seqid *seqid)
5394 struct nfs4_unlockdata *p;
5395 struct inode *inode = lsp->ls_state->inode;
5397 p = kzalloc(sizeof(*p), GFP_NOFS);
5400 p->arg.fh = NFS_FH(inode);
5402 p->arg.seqid = seqid;
5403 p->res.seqid = seqid;
5405 atomic_inc(&lsp->ls_count);
5406 /* Ensure we don't close file until we're done freeing locks! */
5407 p->ctx = get_nfs_open_context(ctx);
5408 memcpy(&p->fl, fl, sizeof(p->fl));
5409 p->server = NFS_SERVER(inode);
5413 static void nfs4_locku_release_calldata(void *data)
5415 struct nfs4_unlockdata *calldata = data;
5416 nfs_free_seqid(calldata->arg.seqid);
5417 nfs4_put_lock_state(calldata->lsp);
5418 put_nfs_open_context(calldata->ctx);
5422 static void nfs4_locku_done(struct rpc_task *task, void *data)
5424 struct nfs4_unlockdata *calldata = data;
5426 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5428 switch (task->tk_status) {
5430 renew_lease(calldata->server, calldata->timestamp);
5431 do_vfs_lock(calldata->fl.fl_file, &calldata->fl);
5432 if (nfs4_update_lock_stateid(calldata->lsp,
5433 &calldata->res.stateid))
5435 case -NFS4ERR_BAD_STATEID:
5436 case -NFS4ERR_OLD_STATEID:
5437 case -NFS4ERR_STALE_STATEID:
5438 case -NFS4ERR_EXPIRED:
5439 if (!nfs4_stateid_match(&calldata->arg.stateid,
5440 &calldata->lsp->ls_stateid))
5441 rpc_restart_call_prepare(task);
5444 if (nfs4_async_handle_error(task, calldata->server,
5445 NULL, NULL) == -EAGAIN)
5446 rpc_restart_call_prepare(task);
5448 nfs_release_seqid(calldata->arg.seqid);
5451 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
5453 struct nfs4_unlockdata *calldata = data;
5455 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
5457 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
5458 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
5459 /* Note: exit _without_ running nfs4_locku_done */
5462 calldata->timestamp = jiffies;
5463 if (nfs4_setup_sequence(calldata->server,
5464 &calldata->arg.seq_args,
5465 &calldata->res.seq_res,
5467 nfs_release_seqid(calldata->arg.seqid);
5470 task->tk_action = NULL;
5472 nfs4_sequence_done(task, &calldata->res.seq_res);
5475 static const struct rpc_call_ops nfs4_locku_ops = {
5476 .rpc_call_prepare = nfs4_locku_prepare,
5477 .rpc_call_done = nfs4_locku_done,
5478 .rpc_release = nfs4_locku_release_calldata,
5481 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5482 struct nfs_open_context *ctx,
5483 struct nfs4_lock_state *lsp,
5484 struct nfs_seqid *seqid)
5486 struct nfs4_unlockdata *data;
5487 struct rpc_message msg = {
5488 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5489 .rpc_cred = ctx->cred,
5491 struct rpc_task_setup task_setup_data = {
5492 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
5493 .rpc_message = &msg,
5494 .callback_ops = &nfs4_locku_ops,
5495 .workqueue = nfsiod_workqueue,
5496 .flags = RPC_TASK_ASYNC,
5499 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5500 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5502 /* Ensure this is an unlock - when canceling a lock, the
5503 * canceled lock is passed in, and it won't be an unlock.
5505 fl->fl_type = F_UNLCK;
5507 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5509 nfs_free_seqid(seqid);
5510 return ERR_PTR(-ENOMEM);
5513 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
5514 msg.rpc_argp = &data->arg;
5515 msg.rpc_resp = &data->res;
5516 task_setup_data.callback_data = data;
5517 return rpc_run_task(&task_setup_data);
5520 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5522 struct inode *inode = state->inode;
5523 struct nfs4_state_owner *sp = state->owner;
5524 struct nfs_inode *nfsi = NFS_I(inode);
5525 struct nfs_seqid *seqid;
5526 struct nfs4_lock_state *lsp;
5527 struct rpc_task *task;
5528 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
5530 unsigned char fl_flags = request->fl_flags;
5532 status = nfs4_set_lock_state(state, request);
5533 /* Unlock _before_ we do the RPC call */
5534 request->fl_flags |= FL_EXISTS;
5535 /* Exclude nfs_delegation_claim_locks() */
5536 mutex_lock(&sp->so_delegreturn_mutex);
5537 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
5538 down_read(&nfsi->rwsem);
5539 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
5540 up_read(&nfsi->rwsem);
5541 mutex_unlock(&sp->so_delegreturn_mutex);
5544 up_read(&nfsi->rwsem);
5545 mutex_unlock(&sp->so_delegreturn_mutex);
5548 /* Is this a delegated lock? */
5549 lsp = request->fl_u.nfs4_fl.owner;
5550 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5552 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
5553 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
5557 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
5558 status = PTR_ERR(task);
5561 status = nfs4_wait_for_completion_rpc_task(task);
5564 request->fl_flags = fl_flags;
5565 trace_nfs4_unlock(request, state, F_SETLK, status);
5569 struct nfs4_lockdata {
5570 struct nfs_lock_args arg;
5571 struct nfs_lock_res res;
5572 struct nfs4_lock_state *lsp;
5573 struct nfs_open_context *ctx;
5574 struct file_lock fl;
5575 unsigned long timestamp;
5578 struct nfs_server *server;
5581 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
5582 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5585 struct nfs4_lockdata *p;
5586 struct inode *inode = lsp->ls_state->inode;
5587 struct nfs_server *server = NFS_SERVER(inode);
5588 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
5590 p = kzalloc(sizeof(*p), gfp_mask);
5594 p->arg.fh = NFS_FH(inode);
5596 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
5597 if (IS_ERR(p->arg.open_seqid))
5599 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
5600 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
5601 if (IS_ERR(p->arg.lock_seqid))
5602 goto out_free_seqid;
5603 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
5604 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
5605 p->arg.lock_owner.s_dev = server->s_dev;
5606 p->res.lock_seqid = p->arg.lock_seqid;
5609 atomic_inc(&lsp->ls_count);
5610 p->ctx = get_nfs_open_context(ctx);
5611 get_file(fl->fl_file);
5612 memcpy(&p->fl, fl, sizeof(p->fl));
5615 nfs_free_seqid(p->arg.open_seqid);
5621 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5623 struct nfs4_lockdata *data = calldata;
5624 struct nfs4_state *state = data->lsp->ls_state;
5626 dprintk("%s: begin!\n", __func__);
5627 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
5629 /* Do we need to do an open_to_lock_owner? */
5630 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
5631 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
5632 goto out_release_lock_seqid;
5634 nfs4_stateid_copy(&data->arg.open_stateid,
5635 &state->open_stateid);
5636 data->arg.new_lock_owner = 1;
5637 data->res.open_seqid = data->arg.open_seqid;
5639 data->arg.new_lock_owner = 0;
5640 nfs4_stateid_copy(&data->arg.lock_stateid,
5641 &data->lsp->ls_stateid);
5643 if (!nfs4_valid_open_stateid(state)) {
5644 data->rpc_status = -EBADF;
5645 task->tk_action = NULL;
5646 goto out_release_open_seqid;
5648 data->timestamp = jiffies;
5649 if (nfs4_setup_sequence(data->server,
5650 &data->arg.seq_args,
5654 out_release_open_seqid:
5655 nfs_release_seqid(data->arg.open_seqid);
5656 out_release_lock_seqid:
5657 nfs_release_seqid(data->arg.lock_seqid);
5659 nfs4_sequence_done(task, &data->res.seq_res);
5660 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
5663 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5665 struct nfs4_lockdata *data = calldata;
5666 struct nfs4_lock_state *lsp = data->lsp;
5668 dprintk("%s: begin!\n", __func__);
5670 if (!nfs4_sequence_done(task, &data->res.seq_res))
5673 data->rpc_status = task->tk_status;
5674 switch (task->tk_status) {
5676 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
5678 if (data->arg.new_lock) {
5679 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
5680 if (do_vfs_lock(data->fl.fl_file, &data->fl) < 0) {
5681 rpc_restart_call_prepare(task);
5685 if (data->arg.new_lock_owner != 0) {
5686 nfs_confirm_seqid(&lsp->ls_seqid, 0);
5687 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
5688 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
5689 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
5690 rpc_restart_call_prepare(task);
5692 case -NFS4ERR_BAD_STATEID:
5693 case -NFS4ERR_OLD_STATEID:
5694 case -NFS4ERR_STALE_STATEID:
5695 case -NFS4ERR_EXPIRED:
5696 if (data->arg.new_lock_owner != 0) {
5697 if (!nfs4_stateid_match(&data->arg.open_stateid,
5698 &lsp->ls_state->open_stateid))
5699 rpc_restart_call_prepare(task);
5700 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
5702 rpc_restart_call_prepare(task);
5704 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
5707 static void nfs4_lock_release(void *calldata)
5709 struct nfs4_lockdata *data = calldata;
5711 dprintk("%s: begin!\n", __func__);
5712 nfs_free_seqid(data->arg.open_seqid);
5713 if (data->cancelled != 0) {
5714 struct rpc_task *task;
5715 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5716 data->arg.lock_seqid);
5718 rpc_put_task_async(task);
5719 dprintk("%s: cancelling lock!\n", __func__);
5721 nfs_free_seqid(data->arg.lock_seqid);
5722 nfs4_put_lock_state(data->lsp);
5723 put_nfs_open_context(data->ctx);
5724 fput(data->fl.fl_file);
5726 dprintk("%s: done!\n", __func__);
5729 static const struct rpc_call_ops nfs4_lock_ops = {
5730 .rpc_call_prepare = nfs4_lock_prepare,
5731 .rpc_call_done = nfs4_lock_done,
5732 .rpc_release = nfs4_lock_release,
5735 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5738 case -NFS4ERR_ADMIN_REVOKED:
5739 case -NFS4ERR_BAD_STATEID:
5740 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
5741 if (new_lock_owner != 0 ||
5742 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
5743 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
5745 case -NFS4ERR_STALE_STATEID:
5746 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
5747 case -NFS4ERR_EXPIRED:
5748 nfs4_schedule_lease_recovery(server->nfs_client);
5752 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
5754 struct nfs4_lockdata *data;
5755 struct rpc_task *task;
5756 struct rpc_message msg = {
5757 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5758 .rpc_cred = state->owner->so_cred,
5760 struct rpc_task_setup task_setup_data = {
5761 .rpc_client = NFS_CLIENT(state->inode),
5762 .rpc_message = &msg,
5763 .callback_ops = &nfs4_lock_ops,
5764 .workqueue = nfsiod_workqueue,
5765 .flags = RPC_TASK_ASYNC,
5769 dprintk("%s: begin!\n", __func__);
5770 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
5771 fl->fl_u.nfs4_fl.owner,
5772 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
5776 data->arg.block = 1;
5777 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
5778 msg.rpc_argp = &data->arg;
5779 msg.rpc_resp = &data->res;
5780 task_setup_data.callback_data = data;
5781 if (recovery_type > NFS_LOCK_NEW) {
5782 if (recovery_type == NFS_LOCK_RECLAIM)
5783 data->arg.reclaim = NFS_LOCK_RECLAIM;
5784 nfs4_set_sequence_privileged(&data->arg.seq_args);
5786 data->arg.new_lock = 1;
5787 task = rpc_run_task(&task_setup_data);
5789 return PTR_ERR(task);
5790 ret = nfs4_wait_for_completion_rpc_task(task);
5792 ret = data->rpc_status;
5794 nfs4_handle_setlk_error(data->server, data->lsp,
5795 data->arg.new_lock_owner, ret);
5797 data->cancelled = 1;
5799 dprintk("%s: done, ret = %d!\n", __func__, ret);
5803 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
5805 struct nfs_server *server = NFS_SERVER(state->inode);
5806 struct nfs4_exception exception = {
5807 .inode = state->inode,
5812 /* Cache the lock if possible... */
5813 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5815 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
5816 trace_nfs4_lock_reclaim(request, state, F_SETLK, err);
5817 if (err != -NFS4ERR_DELAY)
5819 nfs4_handle_exception(server, err, &exception);
5820 } while (exception.retry);
5824 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
5826 struct nfs_server *server = NFS_SERVER(state->inode);
5827 struct nfs4_exception exception = {
5828 .inode = state->inode,
5832 err = nfs4_set_lock_state(state, request);
5835 if (!recover_lost_locks) {
5836 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
5840 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5842 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
5843 trace_nfs4_lock_expired(request, state, F_SETLK, err);
5847 case -NFS4ERR_GRACE:
5848 case -NFS4ERR_DELAY:
5849 nfs4_handle_exception(server, err, &exception);
5852 } while (exception.retry);
5857 #if defined(CONFIG_NFS_V4_1)
5859 * nfs41_check_expired_locks - possibly free a lock stateid
5861 * @state: NFSv4 state for an inode
5863 * Returns NFS_OK if recovery for this stateid is now finished.
5864 * Otherwise a negative NFS4ERR value is returned.
5866 static int nfs41_check_expired_locks(struct nfs4_state *state)
5868 int status, ret = -NFS4ERR_BAD_STATEID;
5869 struct nfs4_lock_state *lsp;
5870 struct nfs_server *server = NFS_SERVER(state->inode);
5872 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
5873 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
5874 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
5876 status = nfs41_test_stateid(server,
5879 trace_nfs4_test_lock_stateid(state, lsp, status);
5880 if (status != NFS_OK) {
5881 /* Free the stateid unless the server
5882 * informs us the stateid is unrecognized. */
5883 if (status != -NFS4ERR_BAD_STATEID)
5884 nfs41_free_stateid(server,
5887 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
5896 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5898 int status = NFS_OK;
5900 if (test_bit(LK_STATE_IN_USE, &state->flags))
5901 status = nfs41_check_expired_locks(state);
5902 if (status != NFS_OK)
5903 status = nfs4_lock_expired(state, request);
5908 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5910 struct nfs_inode *nfsi = NFS_I(state->inode);
5911 unsigned char fl_flags = request->fl_flags;
5912 int status = -ENOLCK;
5914 if ((fl_flags & FL_POSIX) &&
5915 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5917 /* Is this a delegated open? */
5918 status = nfs4_set_lock_state(state, request);
5921 request->fl_flags |= FL_ACCESS;
5922 status = do_vfs_lock(request->fl_file, request);
5925 down_read(&nfsi->rwsem);
5926 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
5927 /* Yes: cache locks! */
5928 /* ...but avoid races with delegation recall... */
5929 request->fl_flags = fl_flags & ~FL_SLEEP;
5930 status = do_vfs_lock(request->fl_file, request);
5931 up_read(&nfsi->rwsem);
5934 up_read(&nfsi->rwsem);
5935 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
5937 request->fl_flags = fl_flags;
5941 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5943 struct nfs4_exception exception = {
5945 .inode = state->inode,
5950 err = _nfs4_proc_setlk(state, cmd, request);
5951 trace_nfs4_set_lock(request, state, cmd, err);
5952 if (err == -NFS4ERR_DENIED)
5954 err = nfs4_handle_exception(NFS_SERVER(state->inode),
5956 } while (exception.retry);
5961 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5963 struct nfs_open_context *ctx;
5964 struct nfs4_state *state;
5965 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5968 /* verify open state */
5969 ctx = nfs_file_open_context(filp);
5972 if (request->fl_start < 0 || request->fl_end < 0)
5975 if (IS_GETLK(cmd)) {
5977 return nfs4_proc_getlk(state, F_GETLK, request);
5981 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5984 if (request->fl_type == F_UNLCK) {
5986 return nfs4_proc_unlck(state, cmd, request);
5993 * Don't rely on the VFS having checked the file open mode,
5994 * since it won't do this for flock() locks.
5996 switch (request->fl_type) {
5998 if (!(filp->f_mode & FMODE_READ))
6002 if (!(filp->f_mode & FMODE_WRITE))
6007 status = nfs4_proc_setlk(state, cmd, request);
6008 if ((status != -EAGAIN) || IS_SETLK(cmd))
6010 timeout = nfs4_set_lock_task_retry(timeout);
6011 status = -ERESTARTSYS;
6014 } while(status < 0);
6018 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
6020 struct nfs_server *server = NFS_SERVER(state->inode);
6023 err = nfs4_set_lock_state(state, fl);
6026 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
6027 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
6030 struct nfs_release_lockowner_data {
6031 struct nfs4_lock_state *lsp;
6032 struct nfs_server *server;
6033 struct nfs_release_lockowner_args args;
6034 struct nfs_release_lockowner_res res;
6035 unsigned long timestamp;
6038 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6040 struct nfs_release_lockowner_data *data = calldata;
6041 struct nfs_server *server = data->server;
6042 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
6043 &data->args.seq_args, &data->res.seq_res, task);
6044 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6045 data->timestamp = jiffies;
6048 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6050 struct nfs_release_lockowner_data *data = calldata;
6051 struct nfs_server *server = data->server;
6053 nfs40_sequence_done(task, &data->res.seq_res);
6055 switch (task->tk_status) {
6057 renew_lease(server, data->timestamp);
6059 case -NFS4ERR_STALE_CLIENTID:
6060 case -NFS4ERR_EXPIRED:
6061 nfs4_schedule_lease_recovery(server->nfs_client);
6063 case -NFS4ERR_LEASE_MOVED:
6064 case -NFS4ERR_DELAY:
6065 if (nfs4_async_handle_error(task, server,
6066 NULL, NULL) == -EAGAIN)
6067 rpc_restart_call_prepare(task);
6071 static void nfs4_release_lockowner_release(void *calldata)
6073 struct nfs_release_lockowner_data *data = calldata;
6074 nfs4_free_lock_state(data->server, data->lsp);
6078 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
6079 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6080 .rpc_call_done = nfs4_release_lockowner_done,
6081 .rpc_release = nfs4_release_lockowner_release,
6085 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
6087 struct nfs_release_lockowner_data *data;
6088 struct rpc_message msg = {
6089 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6092 if (server->nfs_client->cl_mvops->minor_version != 0)
6095 data = kmalloc(sizeof(*data), GFP_NOFS);
6099 data->server = server;
6100 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6101 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6102 data->args.lock_owner.s_dev = server->s_dev;
6104 msg.rpc_argp = &data->args;
6105 msg.rpc_resp = &data->res;
6106 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
6107 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
6110 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6112 static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
6113 const void *buf, size_t buflen,
6114 int flags, int type)
6116 if (strcmp(key, "") != 0)
6119 return nfs4_proc_set_acl(d_inode(dentry), buf, buflen);
6122 static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
6123 void *buf, size_t buflen, int type)
6125 if (strcmp(key, "") != 0)
6128 return nfs4_proc_get_acl(d_inode(dentry), buf, buflen);
6131 static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
6132 size_t list_len, const char *name,
6133 size_t name_len, int type)
6135 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
6137 if (!nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry))))
6140 if (list && len <= list_len)
6141 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
6145 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
6146 static inline int nfs4_server_supports_labels(struct nfs_server *server)
6148 return server->caps & NFS_CAP_SECURITY_LABEL;
6151 static int nfs4_xattr_set_nfs4_label(struct dentry *dentry, const char *key,
6152 const void *buf, size_t buflen,
6153 int flags, int type)
6155 if (security_ismaclabel(key))
6156 return nfs4_set_security_label(dentry, buf, buflen);
6161 static int nfs4_xattr_get_nfs4_label(struct dentry *dentry, const char *key,
6162 void *buf, size_t buflen, int type)
6164 if (security_ismaclabel(key))
6165 return nfs4_get_security_label(d_inode(dentry), buf, buflen);
6169 static size_t nfs4_xattr_list_nfs4_label(struct dentry *dentry, char *list,
6170 size_t list_len, const char *name,
6171 size_t name_len, int type)
6175 if (nfs_server_capable(d_inode(dentry), NFS_CAP_SECURITY_LABEL)) {
6176 len = security_inode_listsecurity(d_inode(dentry), NULL, 0);
6177 if (list && len <= list_len)
6178 security_inode_listsecurity(d_inode(dentry), list, len);
6183 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6184 .prefix = XATTR_SECURITY_PREFIX,
6185 .list = nfs4_xattr_list_nfs4_label,
6186 .get = nfs4_xattr_get_nfs4_label,
6187 .set = nfs4_xattr_set_nfs4_label,
6193 * nfs_fhget will use either the mounted_on_fileid or the fileid
6195 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6197 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6198 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6199 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
6200 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
6203 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
6204 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
6205 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6209 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6210 const struct qstr *name,
6211 struct nfs4_fs_locations *fs_locations,
6214 struct nfs_server *server = NFS_SERVER(dir);
6216 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6218 struct nfs4_fs_locations_arg args = {
6219 .dir_fh = NFS_FH(dir),
6224 struct nfs4_fs_locations_res res = {
6225 .fs_locations = fs_locations,
6227 struct rpc_message msg = {
6228 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6234 dprintk("%s: start\n", __func__);
6236 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6237 * is not supported */
6238 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6239 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6241 bitmask[0] |= FATTR4_WORD0_FILEID;
6243 nfs_fattr_init(&fs_locations->fattr);
6244 fs_locations->server = server;
6245 fs_locations->nlocations = 0;
6246 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
6247 dprintk("%s: returned status = %d\n", __func__, status);
6251 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6252 const struct qstr *name,
6253 struct nfs4_fs_locations *fs_locations,
6256 struct nfs4_exception exception = { };
6259 err = _nfs4_proc_fs_locations(client, dir, name,
6260 fs_locations, page);
6261 trace_nfs4_get_fs_locations(dir, name, err);
6262 err = nfs4_handle_exception(NFS_SERVER(dir), err,
6264 } while (exception.retry);
6269 * This operation also signals the server that this client is
6270 * performing migration recovery. The server can stop returning
6271 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6272 * appended to this compound to identify the client ID which is
6273 * performing recovery.
6275 static int _nfs40_proc_get_locations(struct inode *inode,
6276 struct nfs4_fs_locations *locations,
6277 struct page *page, struct rpc_cred *cred)
6279 struct nfs_server *server = NFS_SERVER(inode);
6280 struct rpc_clnt *clnt = server->client;
6282 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6284 struct nfs4_fs_locations_arg args = {
6285 .clientid = server->nfs_client->cl_clientid,
6286 .fh = NFS_FH(inode),
6289 .migration = 1, /* skip LOOKUP */
6290 .renew = 1, /* append RENEW */
6292 struct nfs4_fs_locations_res res = {
6293 .fs_locations = locations,
6297 struct rpc_message msg = {
6298 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6303 unsigned long now = jiffies;
6306 nfs_fattr_init(&locations->fattr);
6307 locations->server = server;
6308 locations->nlocations = 0;
6310 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6311 nfs4_set_sequence_privileged(&args.seq_args);
6312 status = nfs4_call_sync_sequence(clnt, server, &msg,
6313 &args.seq_args, &res.seq_res);
6317 renew_lease(server, now);
6321 #ifdef CONFIG_NFS_V4_1
6324 * This operation also signals the server that this client is
6325 * performing migration recovery. The server can stop asserting
6326 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6327 * performing this operation is identified in the SEQUENCE
6328 * operation in this compound.
6330 * When the client supports GETATTR(fs_locations_info), it can
6331 * be plumbed in here.
6333 static int _nfs41_proc_get_locations(struct inode *inode,
6334 struct nfs4_fs_locations *locations,
6335 struct page *page, struct rpc_cred *cred)
6337 struct nfs_server *server = NFS_SERVER(inode);
6338 struct rpc_clnt *clnt = server->client;
6340 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6342 struct nfs4_fs_locations_arg args = {
6343 .fh = NFS_FH(inode),
6346 .migration = 1, /* skip LOOKUP */
6348 struct nfs4_fs_locations_res res = {
6349 .fs_locations = locations,
6352 struct rpc_message msg = {
6353 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6360 nfs_fattr_init(&locations->fattr);
6361 locations->server = server;
6362 locations->nlocations = 0;
6364 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6365 nfs4_set_sequence_privileged(&args.seq_args);
6366 status = nfs4_call_sync_sequence(clnt, server, &msg,
6367 &args.seq_args, &res.seq_res);
6368 if (status == NFS4_OK &&
6369 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6370 status = -NFS4ERR_LEASE_MOVED;
6374 #endif /* CONFIG_NFS_V4_1 */
6377 * nfs4_proc_get_locations - discover locations for a migrated FSID
6378 * @inode: inode on FSID that is migrating
6379 * @locations: result of query
6381 * @cred: credential to use for this operation
6383 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6384 * operation failed, or a negative errno if a local error occurred.
6386 * On success, "locations" is filled in, but if the server has
6387 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6390 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6391 * from this client that require migration recovery.
6393 int nfs4_proc_get_locations(struct inode *inode,
6394 struct nfs4_fs_locations *locations,
6395 struct page *page, struct rpc_cred *cred)
6397 struct nfs_server *server = NFS_SERVER(inode);
6398 struct nfs_client *clp = server->nfs_client;
6399 const struct nfs4_mig_recovery_ops *ops =
6400 clp->cl_mvops->mig_recovery_ops;
6401 struct nfs4_exception exception = { };
6404 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6405 (unsigned long long)server->fsid.major,
6406 (unsigned long long)server->fsid.minor,
6408 nfs_display_fhandle(NFS_FH(inode), __func__);
6411 status = ops->get_locations(inode, locations, page, cred);
6412 if (status != -NFS4ERR_DELAY)
6414 nfs4_handle_exception(server, status, &exception);
6415 } while (exception.retry);
6420 * This operation also signals the server that this client is
6421 * performing "lease moved" recovery. The server can stop
6422 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6423 * is appended to this compound to identify the client ID which is
6424 * performing recovery.
6426 static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6428 struct nfs_server *server = NFS_SERVER(inode);
6429 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6430 struct rpc_clnt *clnt = server->client;
6431 struct nfs4_fsid_present_arg args = {
6432 .fh = NFS_FH(inode),
6433 .clientid = clp->cl_clientid,
6434 .renew = 1, /* append RENEW */
6436 struct nfs4_fsid_present_res res = {
6439 struct rpc_message msg = {
6440 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6445 unsigned long now = jiffies;
6448 res.fh = nfs_alloc_fhandle();
6452 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6453 nfs4_set_sequence_privileged(&args.seq_args);
6454 status = nfs4_call_sync_sequence(clnt, server, &msg,
6455 &args.seq_args, &res.seq_res);
6456 nfs_free_fhandle(res.fh);
6460 do_renew_lease(clp, now);
6464 #ifdef CONFIG_NFS_V4_1
6467 * This operation also signals the server that this client is
6468 * performing "lease moved" recovery. The server can stop asserting
6469 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6470 * this operation is identified in the SEQUENCE operation in this
6473 static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6475 struct nfs_server *server = NFS_SERVER(inode);
6476 struct rpc_clnt *clnt = server->client;
6477 struct nfs4_fsid_present_arg args = {
6478 .fh = NFS_FH(inode),
6480 struct nfs4_fsid_present_res res = {
6482 struct rpc_message msg = {
6483 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6490 res.fh = nfs_alloc_fhandle();
6494 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6495 nfs4_set_sequence_privileged(&args.seq_args);
6496 status = nfs4_call_sync_sequence(clnt, server, &msg,
6497 &args.seq_args, &res.seq_res);
6498 nfs_free_fhandle(res.fh);
6499 if (status == NFS4_OK &&
6500 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6501 status = -NFS4ERR_LEASE_MOVED;
6505 #endif /* CONFIG_NFS_V4_1 */
6508 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6509 * @inode: inode on FSID to check
6510 * @cred: credential to use for this operation
6512 * Server indicates whether the FSID is present, moved, or not
6513 * recognized. This operation is necessary to clear a LEASE_MOVED
6514 * condition for this client ID.
6516 * Returns NFS4_OK if the FSID is present on this server,
6517 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6518 * NFS4ERR code if some error occurred on the server, or a
6519 * negative errno if a local failure occurred.
6521 int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6523 struct nfs_server *server = NFS_SERVER(inode);
6524 struct nfs_client *clp = server->nfs_client;
6525 const struct nfs4_mig_recovery_ops *ops =
6526 clp->cl_mvops->mig_recovery_ops;
6527 struct nfs4_exception exception = { };
6530 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6531 (unsigned long long)server->fsid.major,
6532 (unsigned long long)server->fsid.minor,
6534 nfs_display_fhandle(NFS_FH(inode), __func__);
6537 status = ops->fsid_present(inode, cred);
6538 if (status != -NFS4ERR_DELAY)
6540 nfs4_handle_exception(server, status, &exception);
6541 } while (exception.retry);
6546 * If 'use_integrity' is true and the state managment nfs_client
6547 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
6548 * and the machine credential as per RFC3530bis and RFC5661 Security
6549 * Considerations sections. Otherwise, just use the user cred with the
6550 * filesystem's rpc_client.
6552 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
6555 struct nfs4_secinfo_arg args = {
6556 .dir_fh = NFS_FH(dir),
6559 struct nfs4_secinfo_res res = {
6562 struct rpc_message msg = {
6563 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
6567 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
6568 struct rpc_cred *cred = NULL;
6570 if (use_integrity) {
6571 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
6572 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
6573 msg.rpc_cred = cred;
6576 dprintk("NFS call secinfo %s\n", name->name);
6578 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
6579 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
6581 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
6583 dprintk("NFS reply secinfo: %d\n", status);
6591 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
6592 struct nfs4_secinfo_flavors *flavors)
6594 struct nfs4_exception exception = { };
6597 err = -NFS4ERR_WRONGSEC;
6599 /* try to use integrity protection with machine cred */
6600 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
6601 err = _nfs4_proc_secinfo(dir, name, flavors, true);
6604 * if unable to use integrity protection, or SECINFO with
6605 * integrity protection returns NFS4ERR_WRONGSEC (which is
6606 * disallowed by spec, but exists in deployed servers) use
6607 * the current filesystem's rpc_client and the user cred.
6609 if (err == -NFS4ERR_WRONGSEC)
6610 err = _nfs4_proc_secinfo(dir, name, flavors, false);
6612 trace_nfs4_secinfo(dir, name, err);
6613 err = nfs4_handle_exception(NFS_SERVER(dir), err,
6615 } while (exception.retry);
6619 #ifdef CONFIG_NFS_V4_1
6621 * Check the exchange flags returned by the server for invalid flags, having
6622 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
6625 static int nfs4_check_cl_exchange_flags(u32 flags)
6627 if (flags & ~EXCHGID4_FLAG_MASK_R)
6629 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
6630 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
6632 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
6636 return -NFS4ERR_INVAL;
6640 nfs41_same_server_scope(struct nfs41_server_scope *a,
6641 struct nfs41_server_scope *b)
6643 if (a->server_scope_sz == b->server_scope_sz &&
6644 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
6651 * nfs4_proc_bind_conn_to_session()
6653 * The 4.1 client currently uses the same TCP connection for the
6654 * fore and backchannel.
6656 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
6659 struct nfs41_bind_conn_to_session_args args = {
6661 .dir = NFS4_CDFC4_FORE_OR_BOTH,
6663 struct nfs41_bind_conn_to_session_res res;
6664 struct rpc_message msg = {
6666 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
6672 dprintk("--> %s\n", __func__);
6674 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
6675 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
6676 args.dir = NFS4_CDFC4_FORE;
6678 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
6679 trace_nfs4_bind_conn_to_session(clp, status);
6681 if (memcmp(res.sessionid.data,
6682 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
6683 dprintk("NFS: %s: Session ID mismatch\n", __func__);
6687 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
6688 dprintk("NFS: %s: Unexpected direction from server\n",
6693 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
6694 dprintk("NFS: %s: Server returned RDMA mode = true\n",
6701 dprintk("<-- %s status= %d\n", __func__, status);
6706 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
6707 * and operations we'd like to see to enable certain features in the allow map
6709 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
6710 .how = SP4_MACH_CRED,
6711 .enforce.u.words = {
6712 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6713 1 << (OP_EXCHANGE_ID - 32) |
6714 1 << (OP_CREATE_SESSION - 32) |
6715 1 << (OP_DESTROY_SESSION - 32) |
6716 1 << (OP_DESTROY_CLIENTID - 32)
6719 [0] = 1 << (OP_CLOSE) |
6722 [1] = 1 << (OP_SECINFO - 32) |
6723 1 << (OP_SECINFO_NO_NAME - 32) |
6724 1 << (OP_TEST_STATEID - 32) |
6725 1 << (OP_FREE_STATEID - 32) |
6726 1 << (OP_WRITE - 32)
6731 * Select the state protection mode for client `clp' given the server results
6732 * from exchange_id in `sp'.
6734 * Returns 0 on success, negative errno otherwise.
6736 static int nfs4_sp4_select_mode(struct nfs_client *clp,
6737 struct nfs41_state_protection *sp)
6739 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
6740 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6741 1 << (OP_EXCHANGE_ID - 32) |
6742 1 << (OP_CREATE_SESSION - 32) |
6743 1 << (OP_DESTROY_SESSION - 32) |
6744 1 << (OP_DESTROY_CLIENTID - 32)
6748 if (sp->how == SP4_MACH_CRED) {
6749 /* Print state protect result */
6750 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
6751 for (i = 0; i <= LAST_NFS4_OP; i++) {
6752 if (test_bit(i, sp->enforce.u.longs))
6753 dfprintk(MOUNT, " enforce op %d\n", i);
6754 if (test_bit(i, sp->allow.u.longs))
6755 dfprintk(MOUNT, " allow op %d\n", i);
6758 /* make sure nothing is on enforce list that isn't supported */
6759 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
6760 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
6761 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6767 * Minimal mode - state operations are allowed to use machine
6768 * credential. Note this already happens by default, so the
6769 * client doesn't have to do anything more than the negotiation.
6771 * NOTE: we don't care if EXCHANGE_ID is in the list -
6772 * we're already using the machine cred for exchange_id
6773 * and will never use a different cred.
6775 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
6776 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
6777 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
6778 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
6779 dfprintk(MOUNT, "sp4_mach_cred:\n");
6780 dfprintk(MOUNT, " minimal mode enabled\n");
6781 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
6783 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6787 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
6788 test_bit(OP_LOCKU, sp->allow.u.longs)) {
6789 dfprintk(MOUNT, " cleanup mode enabled\n");
6790 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
6793 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
6794 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
6795 dfprintk(MOUNT, " secinfo mode enabled\n");
6796 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
6799 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
6800 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
6801 dfprintk(MOUNT, " stateid mode enabled\n");
6802 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
6805 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
6806 dfprintk(MOUNT, " write mode enabled\n");
6807 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
6810 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
6811 dfprintk(MOUNT, " commit mode enabled\n");
6812 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
6820 * _nfs4_proc_exchange_id()
6822 * Wrapper for EXCHANGE_ID operation.
6824 static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
6827 nfs4_verifier verifier;
6828 struct nfs41_exchange_id_args args = {
6829 .verifier = &verifier,
6831 #ifdef CONFIG_NFS_V4_1_MIGRATION
6832 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
6833 EXCHGID4_FLAG_BIND_PRINC_STATEID |
6834 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
6836 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
6837 EXCHGID4_FLAG_BIND_PRINC_STATEID,
6840 struct nfs41_exchange_id_res res = {
6844 struct rpc_message msg = {
6845 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
6851 nfs4_init_boot_verifier(clp, &verifier);
6852 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
6854 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
6855 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6856 args.id_len, args.id);
6858 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
6860 if (unlikely(res.server_owner == NULL)) {
6865 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
6867 if (unlikely(res.server_scope == NULL)) {
6869 goto out_server_owner;
6872 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
6873 if (unlikely(res.impl_id == NULL)) {
6875 goto out_server_scope;
6880 args.state_protect.how = SP4_NONE;
6884 args.state_protect = nfs4_sp4_mach_cred_request;
6891 goto out_server_scope;
6894 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
6895 trace_nfs4_exchange_id(clp, status);
6897 status = nfs4_check_cl_exchange_flags(res.flags);
6900 status = nfs4_sp4_select_mode(clp, &res.state_protect);
6903 clp->cl_clientid = res.clientid;
6904 clp->cl_exchange_flags = res.flags;
6905 /* Client ID is not confirmed */
6906 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
6907 clear_bit(NFS4_SESSION_ESTABLISHED,
6908 &clp->cl_session->session_state);
6909 clp->cl_seqid = res.seqid;
6912 kfree(clp->cl_serverowner);
6913 clp->cl_serverowner = res.server_owner;
6914 res.server_owner = NULL;
6916 /* use the most recent implementation id */
6917 kfree(clp->cl_implid);
6918 clp->cl_implid = res.impl_id;
6920 if (clp->cl_serverscope != NULL &&
6921 !nfs41_same_server_scope(clp->cl_serverscope,
6922 res.server_scope)) {
6923 dprintk("%s: server_scope mismatch detected\n",
6925 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
6926 kfree(clp->cl_serverscope);
6927 clp->cl_serverscope = NULL;
6930 if (clp->cl_serverscope == NULL) {
6931 clp->cl_serverscope = res.server_scope;
6938 kfree(res.server_owner);
6940 kfree(res.server_scope);
6942 if (clp->cl_implid != NULL)
6943 dprintk("NFS reply exchange_id: Server Implementation ID: "
6944 "domain: %s, name: %s, date: %llu,%u\n",
6945 clp->cl_implid->domain, clp->cl_implid->name,
6946 clp->cl_implid->date.seconds,
6947 clp->cl_implid->date.nseconds);
6948 dprintk("NFS reply exchange_id: %d\n", status);
6953 * nfs4_proc_exchange_id()
6955 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6957 * Since the clientid has expired, all compounds using sessions
6958 * associated with the stale clientid will be returning
6959 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
6960 * be in some phase of session reset.
6962 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
6964 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
6966 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
6969 /* try SP4_MACH_CRED if krb5i/p */
6970 if (authflavor == RPC_AUTH_GSS_KRB5I ||
6971 authflavor == RPC_AUTH_GSS_KRB5P) {
6972 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
6978 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
6981 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
6982 struct rpc_cred *cred)
6984 struct rpc_message msg = {
6985 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
6991 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
6992 trace_nfs4_destroy_clientid(clp, status);
6994 dprintk("NFS: Got error %d from the server %s on "
6995 "DESTROY_CLIENTID.", status, clp->cl_hostname);
6999 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7000 struct rpc_cred *cred)
7005 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7006 ret = _nfs4_proc_destroy_clientid(clp, cred);
7008 case -NFS4ERR_DELAY:
7009 case -NFS4ERR_CLIENTID_BUSY:
7019 int nfs4_destroy_clientid(struct nfs_client *clp)
7021 struct rpc_cred *cred;
7024 if (clp->cl_mvops->minor_version < 1)
7026 if (clp->cl_exchange_flags == 0)
7028 if (clp->cl_preserve_clid)
7030 cred = nfs4_get_clid_cred(clp);
7031 ret = nfs4_proc_destroy_clientid(clp, cred);
7036 case -NFS4ERR_STALE_CLIENTID:
7037 clp->cl_exchange_flags = 0;
7043 struct nfs4_get_lease_time_data {
7044 struct nfs4_get_lease_time_args *args;
7045 struct nfs4_get_lease_time_res *res;
7046 struct nfs_client *clp;
7049 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7052 struct nfs4_get_lease_time_data *data =
7053 (struct nfs4_get_lease_time_data *)calldata;
7055 dprintk("--> %s\n", __func__);
7056 /* just setup sequence, do not trigger session recovery
7057 since we're invoked within one */
7058 nfs41_setup_sequence(data->clp->cl_session,
7059 &data->args->la_seq_args,
7060 &data->res->lr_seq_res,
7062 dprintk("<-- %s\n", __func__);
7066 * Called from nfs4_state_manager thread for session setup, so don't recover
7067 * from sequence operation or clientid errors.
7069 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7071 struct nfs4_get_lease_time_data *data =
7072 (struct nfs4_get_lease_time_data *)calldata;
7074 dprintk("--> %s\n", __func__);
7075 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7077 switch (task->tk_status) {
7078 case -NFS4ERR_DELAY:
7079 case -NFS4ERR_GRACE:
7080 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7081 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7082 task->tk_status = 0;
7084 case -NFS4ERR_RETRY_UNCACHED_REP:
7085 rpc_restart_call_prepare(task);
7088 dprintk("<-- %s\n", __func__);
7091 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
7092 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7093 .rpc_call_done = nfs4_get_lease_time_done,
7096 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7098 struct rpc_task *task;
7099 struct nfs4_get_lease_time_args args;
7100 struct nfs4_get_lease_time_res res = {
7101 .lr_fsinfo = fsinfo,
7103 struct nfs4_get_lease_time_data data = {
7108 struct rpc_message msg = {
7109 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7113 struct rpc_task_setup task_setup = {
7114 .rpc_client = clp->cl_rpcclient,
7115 .rpc_message = &msg,
7116 .callback_ops = &nfs4_get_lease_time_ops,
7117 .callback_data = &data,
7118 .flags = RPC_TASK_TIMEOUT,
7122 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
7123 nfs4_set_sequence_privileged(&args.la_seq_args);
7124 dprintk("--> %s\n", __func__);
7125 task = rpc_run_task(&task_setup);
7128 status = PTR_ERR(task);
7130 status = task->tk_status;
7133 dprintk("<-- %s return %d\n", __func__, status);
7139 * Initialize the values to be used by the client in CREATE_SESSION
7140 * If nfs4_init_session set the fore channel request and response sizes,
7143 * Set the back channel max_resp_sz_cached to zero to force the client to
7144 * always set csa_cachethis to FALSE because the current implementation
7145 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7147 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
7149 unsigned int max_rqst_sz, max_resp_sz;
7151 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7152 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7154 /* Fore channel attributes */
7155 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7156 args->fc_attrs.max_resp_sz = max_resp_sz;
7157 args->fc_attrs.max_ops = NFS4_MAX_OPS;
7158 args->fc_attrs.max_reqs = max_session_slots;
7160 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
7161 "max_ops=%u max_reqs=%u\n",
7163 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
7164 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
7166 /* Back channel attributes */
7167 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
7168 args->bc_attrs.max_resp_sz = PAGE_SIZE;
7169 args->bc_attrs.max_resp_sz_cached = 0;
7170 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
7171 args->bc_attrs.max_reqs = 1;
7173 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7174 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7176 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7177 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7178 args->bc_attrs.max_reqs);
7181 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7182 struct nfs41_create_session_res *res)
7184 struct nfs4_channel_attrs *sent = &args->fc_attrs;
7185 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
7187 if (rcvd->max_resp_sz > sent->max_resp_sz)
7190 * Our requested max_ops is the minimum we need; we're not
7191 * prepared to break up compounds into smaller pieces than that.
7192 * So, no point even trying to continue if the server won't
7195 if (rcvd->max_ops < sent->max_ops)
7197 if (rcvd->max_reqs == 0)
7199 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7200 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
7204 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7205 struct nfs41_create_session_res *res)
7207 struct nfs4_channel_attrs *sent = &args->bc_attrs;
7208 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
7210 if (!(res->flags & SESSION4_BACK_CHAN))
7212 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7214 if (rcvd->max_resp_sz < sent->max_resp_sz)
7216 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7218 /* These would render the backchannel useless: */
7219 if (rcvd->max_ops != sent->max_ops)
7221 if (rcvd->max_reqs != sent->max_reqs)
7227 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
7228 struct nfs41_create_session_res *res)
7232 ret = nfs4_verify_fore_channel_attrs(args, res);
7235 return nfs4_verify_back_channel_attrs(args, res);
7238 static void nfs4_update_session(struct nfs4_session *session,
7239 struct nfs41_create_session_res *res)
7241 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
7242 /* Mark client id and session as being confirmed */
7243 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7244 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
7245 session->flags = res->flags;
7246 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
7247 if (res->flags & SESSION4_BACK_CHAN)
7248 memcpy(&session->bc_attrs, &res->bc_attrs,
7249 sizeof(session->bc_attrs));
7252 static int _nfs4_proc_create_session(struct nfs_client *clp,
7253 struct rpc_cred *cred)
7255 struct nfs4_session *session = clp->cl_session;
7256 struct nfs41_create_session_args args = {
7258 .clientid = clp->cl_clientid,
7259 .seqid = clp->cl_seqid,
7260 .cb_program = NFS4_CALLBACK,
7262 struct nfs41_create_session_res res;
7264 struct rpc_message msg = {
7265 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7272 nfs4_init_channel_attrs(&args);
7273 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
7275 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7276 trace_nfs4_create_session(clp, status);
7279 /* Verify the session's negotiated channel_attrs values */
7280 status = nfs4_verify_channel_attrs(&args, &res);
7281 /* Increment the clientid slot sequence id */
7282 if (clp->cl_seqid == res.seqid)
7286 nfs4_update_session(session, &res);
7293 * Issues a CREATE_SESSION operation to the server.
7294 * It is the responsibility of the caller to verify the session is
7295 * expired before calling this routine.
7297 int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
7301 struct nfs4_session *session = clp->cl_session;
7303 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7305 status = _nfs4_proc_create_session(clp, cred);
7309 /* Init or reset the session slot tables */
7310 status = nfs4_setup_session_slot_tables(session);
7311 dprintk("slot table setup returned %d\n", status);
7315 ptr = (unsigned *)&session->sess_id.data[0];
7316 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7317 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
7319 dprintk("<-- %s\n", __func__);
7324 * Issue the over-the-wire RPC DESTROY_SESSION.
7325 * The caller must serialize access to this routine.
7327 int nfs4_proc_destroy_session(struct nfs4_session *session,
7328 struct rpc_cred *cred)
7330 struct rpc_message msg = {
7331 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
7332 .rpc_argp = session,
7337 dprintk("--> nfs4_proc_destroy_session\n");
7339 /* session is still being setup */
7340 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
7343 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7344 trace_nfs4_destroy_session(session->clp, status);
7347 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
7348 "Session has been destroyed regardless...\n", status);
7350 dprintk("<-- nfs4_proc_destroy_session\n");
7355 * Renew the cl_session lease.
7357 struct nfs4_sequence_data {
7358 struct nfs_client *clp;
7359 struct nfs4_sequence_args args;
7360 struct nfs4_sequence_res res;
7363 static void nfs41_sequence_release(void *data)
7365 struct nfs4_sequence_data *calldata = data;
7366 struct nfs_client *clp = calldata->clp;
7368 if (atomic_read(&clp->cl_count) > 1)
7369 nfs4_schedule_state_renewal(clp);
7370 nfs_put_client(clp);
7374 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7376 switch(task->tk_status) {
7377 case -NFS4ERR_DELAY:
7378 rpc_delay(task, NFS4_POLL_RETRY_MAX);
7381 nfs4_schedule_lease_recovery(clp);
7386 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
7388 struct nfs4_sequence_data *calldata = data;
7389 struct nfs_client *clp = calldata->clp;
7391 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
7394 trace_nfs4_sequence(clp, task->tk_status);
7395 if (task->tk_status < 0) {
7396 dprintk("%s ERROR %d\n", __func__, task->tk_status);
7397 if (atomic_read(&clp->cl_count) == 1)
7400 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
7401 rpc_restart_call_prepare(task);
7405 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
7407 dprintk("<-- %s\n", __func__);
7410 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
7412 struct nfs4_sequence_data *calldata = data;
7413 struct nfs_client *clp = calldata->clp;
7414 struct nfs4_sequence_args *args;
7415 struct nfs4_sequence_res *res;
7417 args = task->tk_msg.rpc_argp;
7418 res = task->tk_msg.rpc_resp;
7420 nfs41_setup_sequence(clp->cl_session, args, res, task);
7423 static const struct rpc_call_ops nfs41_sequence_ops = {
7424 .rpc_call_done = nfs41_sequence_call_done,
7425 .rpc_call_prepare = nfs41_sequence_prepare,
7426 .rpc_release = nfs41_sequence_release,
7429 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
7430 struct rpc_cred *cred,
7433 struct nfs4_sequence_data *calldata;
7434 struct rpc_message msg = {
7435 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
7438 struct rpc_task_setup task_setup_data = {
7439 .rpc_client = clp->cl_rpcclient,
7440 .rpc_message = &msg,
7441 .callback_ops = &nfs41_sequence_ops,
7442 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7445 if (!atomic_inc_not_zero(&clp->cl_count))
7446 return ERR_PTR(-EIO);
7447 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7448 if (calldata == NULL) {
7449 nfs_put_client(clp);
7450 return ERR_PTR(-ENOMEM);
7452 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
7454 nfs4_set_sequence_privileged(&calldata->args);
7455 msg.rpc_argp = &calldata->args;
7456 msg.rpc_resp = &calldata->res;
7457 calldata->clp = clp;
7458 task_setup_data.callback_data = calldata;
7460 return rpc_run_task(&task_setup_data);
7463 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
7465 struct rpc_task *task;
7468 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
7470 task = _nfs41_proc_sequence(clp, cred, false);
7472 ret = PTR_ERR(task);
7474 rpc_put_task_async(task);
7475 dprintk("<-- %s status=%d\n", __func__, ret);
7479 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
7481 struct rpc_task *task;
7484 task = _nfs41_proc_sequence(clp, cred, true);
7486 ret = PTR_ERR(task);
7489 ret = rpc_wait_for_completion_task(task);
7491 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
7493 if (task->tk_status == 0)
7494 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
7495 ret = task->tk_status;
7499 dprintk("<-- %s status=%d\n", __func__, ret);
7503 struct nfs4_reclaim_complete_data {
7504 struct nfs_client *clp;
7505 struct nfs41_reclaim_complete_args arg;
7506 struct nfs41_reclaim_complete_res res;
7509 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
7511 struct nfs4_reclaim_complete_data *calldata = data;
7513 nfs41_setup_sequence(calldata->clp->cl_session,
7514 &calldata->arg.seq_args,
7515 &calldata->res.seq_res,
7519 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7521 switch(task->tk_status) {
7523 case -NFS4ERR_COMPLETE_ALREADY:
7524 case -NFS4ERR_WRONG_CRED: /* What to do here? */
7526 case -NFS4ERR_DELAY:
7527 rpc_delay(task, NFS4_POLL_RETRY_MAX);
7529 case -NFS4ERR_RETRY_UNCACHED_REP:
7532 nfs4_schedule_lease_recovery(clp);
7537 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
7539 struct nfs4_reclaim_complete_data *calldata = data;
7540 struct nfs_client *clp = calldata->clp;
7541 struct nfs4_sequence_res *res = &calldata->res.seq_res;
7543 dprintk("--> %s\n", __func__);
7544 if (!nfs41_sequence_done(task, res))
7547 trace_nfs4_reclaim_complete(clp, task->tk_status);
7548 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
7549 rpc_restart_call_prepare(task);
7552 dprintk("<-- %s\n", __func__);
7555 static void nfs4_free_reclaim_complete_data(void *data)
7557 struct nfs4_reclaim_complete_data *calldata = data;
7562 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
7563 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
7564 .rpc_call_done = nfs4_reclaim_complete_done,
7565 .rpc_release = nfs4_free_reclaim_complete_data,
7569 * Issue a global reclaim complete.
7571 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
7572 struct rpc_cred *cred)
7574 struct nfs4_reclaim_complete_data *calldata;
7575 struct rpc_task *task;
7576 struct rpc_message msg = {
7577 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
7580 struct rpc_task_setup task_setup_data = {
7581 .rpc_client = clp->cl_rpcclient,
7582 .rpc_message = &msg,
7583 .callback_ops = &nfs4_reclaim_complete_call_ops,
7584 .flags = RPC_TASK_ASYNC,
7586 int status = -ENOMEM;
7588 dprintk("--> %s\n", __func__);
7589 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7590 if (calldata == NULL)
7592 calldata->clp = clp;
7593 calldata->arg.one_fs = 0;
7595 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
7596 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
7597 msg.rpc_argp = &calldata->arg;
7598 msg.rpc_resp = &calldata->res;
7599 task_setup_data.callback_data = calldata;
7600 task = rpc_run_task(&task_setup_data);
7602 status = PTR_ERR(task);
7605 status = nfs4_wait_for_completion_rpc_task(task);
7607 status = task->tk_status;
7611 dprintk("<-- %s status=%d\n", __func__, status);
7616 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
7618 struct nfs4_layoutget *lgp = calldata;
7619 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
7620 struct nfs4_session *session = nfs4_get_session(server);
7622 dprintk("--> %s\n", __func__);
7623 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
7624 * right now covering the LAYOUTGET we are about to send.
7625 * However, that is not so catastrophic, and there seems
7626 * to be no way to prevent it completely.
7628 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
7629 &lgp->res.seq_res, task))
7631 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
7632 NFS_I(lgp->args.inode)->layout,
7634 lgp->args.ctx->state)) {
7635 rpc_exit(task, NFS4_OK);
7639 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
7641 struct nfs4_layoutget *lgp = calldata;
7642 struct inode *inode = lgp->args.inode;
7643 struct nfs_server *server = NFS_SERVER(inode);
7644 struct pnfs_layout_hdr *lo;
7645 struct nfs4_state *state = NULL;
7646 unsigned long timeo, now, giveup;
7648 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
7650 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
7653 switch (task->tk_status) {
7657 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
7658 * (or clients) writing to the same RAID stripe
7660 case -NFS4ERR_LAYOUTTRYLATER:
7662 * NFS4ERR_RECALLCONFLICT is when conflict with self (must recall
7663 * existing layout before getting a new one).
7665 case -NFS4ERR_RECALLCONFLICT:
7666 timeo = rpc_get_timeout(task->tk_client);
7667 giveup = lgp->args.timestamp + timeo;
7669 if (time_after(giveup, now)) {
7670 unsigned long delay;
7673 * - Not less then NFS4_POLL_RETRY_MIN.
7674 * - One last time a jiffie before we give up
7675 * - exponential backoff (time_now minus start_attempt)
7677 delay = max_t(unsigned long, NFS4_POLL_RETRY_MIN,
7678 min((giveup - now - 1),
7679 now - lgp->args.timestamp));
7681 dprintk("%s: NFS4ERR_RECALLCONFLICT waiting %lu\n",
7683 rpc_delay(task, delay);
7684 task->tk_status = 0;
7685 rpc_restart_call_prepare(task);
7686 goto out; /* Do not call nfs4_async_handle_error() */
7689 case -NFS4ERR_EXPIRED:
7690 case -NFS4ERR_BAD_STATEID:
7691 spin_lock(&inode->i_lock);
7692 lo = NFS_I(inode)->layout;
7693 if (!lo || list_empty(&lo->plh_segs)) {
7694 spin_unlock(&inode->i_lock);
7695 /* If the open stateid was bad, then recover it. */
7696 state = lgp->args.ctx->state;
7701 * Mark the bad layout state as invalid, then retry
7702 * with the current stateid.
7704 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
7705 spin_unlock(&inode->i_lock);
7706 pnfs_free_lseg_list(&head);
7708 task->tk_status = 0;
7709 rpc_restart_call_prepare(task);
7712 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN)
7713 rpc_restart_call_prepare(task);
7715 dprintk("<-- %s\n", __func__);
7718 static size_t max_response_pages(struct nfs_server *server)
7720 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
7721 return nfs_page_array_len(0, max_resp_sz);
7724 static void nfs4_free_pages(struct page **pages, size_t size)
7731 for (i = 0; i < size; i++) {
7734 __free_page(pages[i]);
7739 static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
7741 struct page **pages;
7744 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
7746 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
7750 for (i = 0; i < size; i++) {
7751 pages[i] = alloc_page(gfp_flags);
7753 dprintk("%s: failed to allocate page\n", __func__);
7754 nfs4_free_pages(pages, size);
7762 static void nfs4_layoutget_release(void *calldata)
7764 struct nfs4_layoutget *lgp = calldata;
7765 struct inode *inode = lgp->args.inode;
7766 struct nfs_server *server = NFS_SERVER(inode);
7767 size_t max_pages = max_response_pages(server);
7769 dprintk("--> %s\n", __func__);
7770 nfs4_free_pages(lgp->args.layout.pages, max_pages);
7771 pnfs_put_layout_hdr(NFS_I(inode)->layout);
7772 put_nfs_open_context(lgp->args.ctx);
7774 dprintk("<-- %s\n", __func__);
7777 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
7778 .rpc_call_prepare = nfs4_layoutget_prepare,
7779 .rpc_call_done = nfs4_layoutget_done,
7780 .rpc_release = nfs4_layoutget_release,
7783 struct pnfs_layout_segment *
7784 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
7786 struct inode *inode = lgp->args.inode;
7787 struct nfs_server *server = NFS_SERVER(inode);
7788 size_t max_pages = max_response_pages(server);
7789 struct rpc_task *task;
7790 struct rpc_message msg = {
7791 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
7792 .rpc_argp = &lgp->args,
7793 .rpc_resp = &lgp->res,
7794 .rpc_cred = lgp->cred,
7796 struct rpc_task_setup task_setup_data = {
7797 .rpc_client = server->client,
7798 .rpc_message = &msg,
7799 .callback_ops = &nfs4_layoutget_call_ops,
7800 .callback_data = lgp,
7801 .flags = RPC_TASK_ASYNC,
7803 struct pnfs_layout_segment *lseg = NULL;
7806 dprintk("--> %s\n", __func__);
7808 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
7809 pnfs_get_layout_hdr(NFS_I(inode)->layout);
7811 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
7812 if (!lgp->args.layout.pages) {
7813 nfs4_layoutget_release(lgp);
7814 return ERR_PTR(-ENOMEM);
7816 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
7817 lgp->args.timestamp = jiffies;
7819 lgp->res.layoutp = &lgp->args.layout;
7820 lgp->res.seq_res.sr_slot = NULL;
7821 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
7823 task = rpc_run_task(&task_setup_data);
7825 return ERR_CAST(task);
7826 status = nfs4_wait_for_completion_rpc_task(task);
7828 status = task->tk_status;
7829 trace_nfs4_layoutget(lgp->args.ctx,
7833 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
7834 if (status == 0 && lgp->res.layoutp->len)
7835 lseg = pnfs_layout_process(lgp);
7837 dprintk("<-- %s status=%d\n", __func__, status);
7839 return ERR_PTR(status);
7844 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
7846 struct nfs4_layoutreturn *lrp = calldata;
7848 dprintk("--> %s\n", __func__);
7849 nfs41_setup_sequence(lrp->clp->cl_session,
7850 &lrp->args.seq_args,
7855 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
7857 struct nfs4_layoutreturn *lrp = calldata;
7858 struct nfs_server *server;
7860 dprintk("--> %s\n", __func__);
7862 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
7865 server = NFS_SERVER(lrp->args.inode);
7866 switch (task->tk_status) {
7868 task->tk_status = 0;
7871 case -NFS4ERR_DELAY:
7872 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
7874 rpc_restart_call_prepare(task);
7877 dprintk("<-- %s\n", __func__);
7880 static void nfs4_layoutreturn_release(void *calldata)
7882 struct nfs4_layoutreturn *lrp = calldata;
7883 struct pnfs_layout_hdr *lo = lrp->args.layout;
7885 dprintk("--> %s\n", __func__);
7886 spin_lock(&lo->plh_inode->i_lock);
7887 if (lrp->res.lrs_present)
7888 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
7889 pnfs_clear_layoutreturn_waitbit(lo);
7890 clear_bit(NFS_LAYOUT_RETURN_BEFORE_CLOSE, &lo->plh_flags);
7891 rpc_wake_up(&NFS_SERVER(lo->plh_inode)->roc_rpcwaitq);
7892 lo->plh_block_lgets--;
7893 spin_unlock(&lo->plh_inode->i_lock);
7894 pnfs_put_layout_hdr(lrp->args.layout);
7895 nfs_iput_and_deactive(lrp->inode);
7897 dprintk("<-- %s\n", __func__);
7900 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
7901 .rpc_call_prepare = nfs4_layoutreturn_prepare,
7902 .rpc_call_done = nfs4_layoutreturn_done,
7903 .rpc_release = nfs4_layoutreturn_release,
7906 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
7908 struct rpc_task *task;
7909 struct rpc_message msg = {
7910 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
7911 .rpc_argp = &lrp->args,
7912 .rpc_resp = &lrp->res,
7913 .rpc_cred = lrp->cred,
7915 struct rpc_task_setup task_setup_data = {
7916 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
7917 .rpc_message = &msg,
7918 .callback_ops = &nfs4_layoutreturn_call_ops,
7919 .callback_data = lrp,
7923 dprintk("--> %s\n", __func__);
7925 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
7927 nfs4_layoutreturn_release(lrp);
7930 task_setup_data.flags |= RPC_TASK_ASYNC;
7932 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
7933 task = rpc_run_task(&task_setup_data);
7935 return PTR_ERR(task);
7937 status = task->tk_status;
7938 trace_nfs4_layoutreturn(lrp->args.inode, status);
7939 dprintk("<-- %s status=%d\n", __func__, status);
7945 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
7946 struct pnfs_device *pdev,
7947 struct rpc_cred *cred)
7949 struct nfs4_getdeviceinfo_args args = {
7951 .notify_types = NOTIFY_DEVICEID4_CHANGE |
7952 NOTIFY_DEVICEID4_DELETE,
7954 struct nfs4_getdeviceinfo_res res = {
7957 struct rpc_message msg = {
7958 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
7965 dprintk("--> %s\n", __func__);
7966 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
7967 if (res.notification & ~args.notify_types)
7968 dprintk("%s: unsupported notification\n", __func__);
7969 if (res.notification != args.notify_types)
7972 dprintk("<-- %s status=%d\n", __func__, status);
7977 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
7978 struct pnfs_device *pdev,
7979 struct rpc_cred *cred)
7981 struct nfs4_exception exception = { };
7985 err = nfs4_handle_exception(server,
7986 _nfs4_proc_getdeviceinfo(server, pdev, cred),
7988 } while (exception.retry);
7991 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
7993 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
7995 struct nfs4_layoutcommit_data *data = calldata;
7996 struct nfs_server *server = NFS_SERVER(data->args.inode);
7997 struct nfs4_session *session = nfs4_get_session(server);
7999 nfs41_setup_sequence(session,
8000 &data->args.seq_args,
8006 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8008 struct nfs4_layoutcommit_data *data = calldata;
8009 struct nfs_server *server = NFS_SERVER(data->args.inode);
8011 if (!nfs41_sequence_done(task, &data->res.seq_res))
8014 switch (task->tk_status) { /* Just ignore these failures */
8015 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8016 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8017 case -NFS4ERR_BADLAYOUT: /* no layout */
8018 case -NFS4ERR_GRACE: /* loca_recalim always false */
8019 task->tk_status = 0;
8023 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
8024 rpc_restart_call_prepare(task);
8030 static void nfs4_layoutcommit_release(void *calldata)
8032 struct nfs4_layoutcommit_data *data = calldata;
8034 pnfs_cleanup_layoutcommit(data);
8035 nfs_post_op_update_inode_force_wcc(data->args.inode,
8037 put_rpccred(data->cred);
8038 nfs_iput_and_deactive(data->inode);
8042 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8043 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8044 .rpc_call_done = nfs4_layoutcommit_done,
8045 .rpc_release = nfs4_layoutcommit_release,
8049 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
8051 struct rpc_message msg = {
8052 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8053 .rpc_argp = &data->args,
8054 .rpc_resp = &data->res,
8055 .rpc_cred = data->cred,
8057 struct rpc_task_setup task_setup_data = {
8058 .task = &data->task,
8059 .rpc_client = NFS_CLIENT(data->args.inode),
8060 .rpc_message = &msg,
8061 .callback_ops = &nfs4_layoutcommit_ops,
8062 .callback_data = data,
8064 struct rpc_task *task;
8067 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
8068 "lbw: %llu inode %lu\n",
8069 data->task.tk_pid, sync,
8070 data->args.lastbytewritten,
8071 data->args.inode->i_ino);
8074 data->inode = nfs_igrab_and_active(data->args.inode);
8075 if (data->inode == NULL) {
8076 nfs4_layoutcommit_release(data);
8079 task_setup_data.flags = RPC_TASK_ASYNC;
8081 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
8082 task = rpc_run_task(&task_setup_data);
8084 return PTR_ERR(task);
8086 status = task->tk_status;
8087 trace_nfs4_layoutcommit(data->args.inode, status);
8088 dprintk("%s: status %d\n", __func__, status);
8094 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8095 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8098 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8099 struct nfs_fsinfo *info,
8100 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
8102 struct nfs41_secinfo_no_name_args args = {
8103 .style = SECINFO_STYLE_CURRENT_FH,
8105 struct nfs4_secinfo_res res = {
8108 struct rpc_message msg = {
8109 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8113 struct rpc_clnt *clnt = server->client;
8114 struct rpc_cred *cred = NULL;
8117 if (use_integrity) {
8118 clnt = server->nfs_client->cl_rpcclient;
8119 cred = nfs4_get_clid_cred(server->nfs_client);
8120 msg.rpc_cred = cred;
8123 dprintk("--> %s\n", __func__);
8124 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8126 dprintk("<-- %s status=%d\n", __func__, status);
8135 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8136 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8138 struct nfs4_exception exception = { };
8141 /* first try using integrity protection */
8142 err = -NFS4ERR_WRONGSEC;
8144 /* try to use integrity protection with machine cred */
8145 if (_nfs4_is_integrity_protected(server->nfs_client))
8146 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8150 * if unable to use integrity protection, or SECINFO with
8151 * integrity protection returns NFS4ERR_WRONGSEC (which is
8152 * disallowed by spec, but exists in deployed servers) use
8153 * the current filesystem's rpc_client and the user cred.
8155 if (err == -NFS4ERR_WRONGSEC)
8156 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8161 case -NFS4ERR_WRONGSEC:
8165 err = nfs4_handle_exception(server, err, &exception);
8167 } while (exception.retry);
8173 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8174 struct nfs_fsinfo *info)
8178 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
8179 struct nfs4_secinfo_flavors *flavors;
8180 struct nfs4_secinfo4 *secinfo;
8183 page = alloc_page(GFP_KERNEL);
8189 flavors = page_address(page);
8190 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8193 * Fall back on "guess and check" method if
8194 * the server doesn't support SECINFO_NO_NAME
8196 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
8197 err = nfs4_find_root_sec(server, fhandle, info);
8203 for (i = 0; i < flavors->num_flavors; i++) {
8204 secinfo = &flavors->flavors[i];
8206 switch (secinfo->flavor) {
8210 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8211 &secinfo->flavor_info);
8214 flavor = RPC_AUTH_MAXFLAVOR;
8218 if (!nfs_auth_info_match(&server->auth_info, flavor))
8219 flavor = RPC_AUTH_MAXFLAVOR;
8221 if (flavor != RPC_AUTH_MAXFLAVOR) {
8222 err = nfs4_lookup_root_sec(server, fhandle,
8229 if (flavor == RPC_AUTH_MAXFLAVOR)
8240 static int _nfs41_test_stateid(struct nfs_server *server,
8241 nfs4_stateid *stateid,
8242 struct rpc_cred *cred)
8245 struct nfs41_test_stateid_args args = {
8248 struct nfs41_test_stateid_res res;
8249 struct rpc_message msg = {
8250 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8255 struct rpc_clnt *rpc_client = server->client;
8257 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8260 dprintk("NFS call test_stateid %p\n", stateid);
8261 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
8262 nfs4_set_sequence_privileged(&args.seq_args);
8263 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
8264 &args.seq_args, &res.seq_res);
8265 if (status != NFS_OK) {
8266 dprintk("NFS reply test_stateid: failed, %d\n", status);
8269 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
8274 * nfs41_test_stateid - perform a TEST_STATEID operation
8276 * @server: server / transport on which to perform the operation
8277 * @stateid: state ID to test
8280 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8281 * Otherwise a negative NFS4ERR value is returned if the operation
8282 * failed or the state ID is not currently valid.
8284 static int nfs41_test_stateid(struct nfs_server *server,
8285 nfs4_stateid *stateid,
8286 struct rpc_cred *cred)
8288 struct nfs4_exception exception = { };
8291 err = _nfs41_test_stateid(server, stateid, cred);
8292 if (err != -NFS4ERR_DELAY)
8294 nfs4_handle_exception(server, err, &exception);
8295 } while (exception.retry);
8299 struct nfs_free_stateid_data {
8300 struct nfs_server *server;
8301 struct nfs41_free_stateid_args args;
8302 struct nfs41_free_stateid_res res;
8305 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
8307 struct nfs_free_stateid_data *data = calldata;
8308 nfs41_setup_sequence(nfs4_get_session(data->server),
8309 &data->args.seq_args,
8314 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
8316 struct nfs_free_stateid_data *data = calldata;
8318 nfs41_sequence_done(task, &data->res.seq_res);
8320 switch (task->tk_status) {
8321 case -NFS4ERR_DELAY:
8322 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
8323 rpc_restart_call_prepare(task);
8327 static void nfs41_free_stateid_release(void *calldata)
8332 static const struct rpc_call_ops nfs41_free_stateid_ops = {
8333 .rpc_call_prepare = nfs41_free_stateid_prepare,
8334 .rpc_call_done = nfs41_free_stateid_done,
8335 .rpc_release = nfs41_free_stateid_release,
8338 static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
8339 nfs4_stateid *stateid,
8340 struct rpc_cred *cred,
8343 struct rpc_message msg = {
8344 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
8347 struct rpc_task_setup task_setup = {
8348 .rpc_client = server->client,
8349 .rpc_message = &msg,
8350 .callback_ops = &nfs41_free_stateid_ops,
8351 .flags = RPC_TASK_ASYNC,
8353 struct nfs_free_stateid_data *data;
8355 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8356 &task_setup.rpc_client, &msg);
8358 dprintk("NFS call free_stateid %p\n", stateid);
8359 data = kmalloc(sizeof(*data), GFP_NOFS);
8361 return ERR_PTR(-ENOMEM);
8362 data->server = server;
8363 nfs4_stateid_copy(&data->args.stateid, stateid);
8365 task_setup.callback_data = data;
8367 msg.rpc_argp = &data->args;
8368 msg.rpc_resp = &data->res;
8369 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
8371 nfs4_set_sequence_privileged(&data->args.seq_args);
8373 return rpc_run_task(&task_setup);
8377 * nfs41_free_stateid - perform a FREE_STATEID operation
8379 * @server: server / transport on which to perform the operation
8380 * @stateid: state ID to release
8383 * Returns NFS_OK if the server freed "stateid". Otherwise a
8384 * negative NFS4ERR value is returned.
8386 static int nfs41_free_stateid(struct nfs_server *server,
8387 nfs4_stateid *stateid,
8388 struct rpc_cred *cred)
8390 struct rpc_task *task;
8393 task = _nfs41_free_stateid(server, stateid, cred, true);
8395 return PTR_ERR(task);
8396 ret = rpc_wait_for_completion_task(task);
8398 ret = task->tk_status;
8404 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
8406 struct rpc_task *task;
8407 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
8409 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
8410 nfs4_free_lock_state(server, lsp);
8416 static bool nfs41_match_stateid(const nfs4_stateid *s1,
8417 const nfs4_stateid *s2)
8419 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
8422 if (s1->seqid == s2->seqid)
8424 if (s1->seqid == 0 || s2->seqid == 0)
8430 #endif /* CONFIG_NFS_V4_1 */
8432 static bool nfs4_match_stateid(const nfs4_stateid *s1,
8433 const nfs4_stateid *s2)
8435 return nfs4_stateid_match(s1, s2);
8439 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
8440 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
8441 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
8442 .recover_open = nfs4_open_reclaim,
8443 .recover_lock = nfs4_lock_reclaim,
8444 .establish_clid = nfs4_init_clientid,
8445 .detect_trunking = nfs40_discover_server_trunking,
8448 #if defined(CONFIG_NFS_V4_1)
8449 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
8450 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
8451 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
8452 .recover_open = nfs4_open_reclaim,
8453 .recover_lock = nfs4_lock_reclaim,
8454 .establish_clid = nfs41_init_clientid,
8455 .reclaim_complete = nfs41_proc_reclaim_complete,
8456 .detect_trunking = nfs41_discover_server_trunking,
8458 #endif /* CONFIG_NFS_V4_1 */
8460 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
8461 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
8462 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
8463 .recover_open = nfs40_open_expired,
8464 .recover_lock = nfs4_lock_expired,
8465 .establish_clid = nfs4_init_clientid,
8468 #if defined(CONFIG_NFS_V4_1)
8469 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
8470 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
8471 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
8472 .recover_open = nfs41_open_expired,
8473 .recover_lock = nfs41_lock_expired,
8474 .establish_clid = nfs41_init_clientid,
8476 #endif /* CONFIG_NFS_V4_1 */
8478 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
8479 .sched_state_renewal = nfs4_proc_async_renew,
8480 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
8481 .renew_lease = nfs4_proc_renew,
8484 #if defined(CONFIG_NFS_V4_1)
8485 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
8486 .sched_state_renewal = nfs41_proc_async_sequence,
8487 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
8488 .renew_lease = nfs4_proc_sequence,
8492 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
8493 .get_locations = _nfs40_proc_get_locations,
8494 .fsid_present = _nfs40_proc_fsid_present,
8497 #if defined(CONFIG_NFS_V4_1)
8498 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
8499 .get_locations = _nfs41_proc_get_locations,
8500 .fsid_present = _nfs41_proc_fsid_present,
8502 #endif /* CONFIG_NFS_V4_1 */
8504 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
8506 .init_caps = NFS_CAP_READDIRPLUS
8507 | NFS_CAP_ATOMIC_OPEN
8508 | NFS_CAP_CHANGE_ATTR
8509 | NFS_CAP_POSIX_LOCK,
8510 .init_client = nfs40_init_client,
8511 .shutdown_client = nfs40_shutdown_client,
8512 .match_stateid = nfs4_match_stateid,
8513 .find_root_sec = nfs4_find_root_sec,
8514 .free_lock_state = nfs4_release_lockowner,
8515 .alloc_seqid = nfs_alloc_seqid,
8516 .call_sync_ops = &nfs40_call_sync_ops,
8517 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
8518 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
8519 .state_renewal_ops = &nfs40_state_renewal_ops,
8520 .mig_recovery_ops = &nfs40_mig_recovery_ops,
8523 #if defined(CONFIG_NFS_V4_1)
8524 static struct nfs_seqid *
8525 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
8530 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
8532 .init_caps = NFS_CAP_READDIRPLUS
8533 | NFS_CAP_ATOMIC_OPEN
8534 | NFS_CAP_CHANGE_ATTR
8535 | NFS_CAP_POSIX_LOCK
8536 | NFS_CAP_STATEID_NFSV41
8537 | NFS_CAP_ATOMIC_OPEN_V1,
8538 .init_client = nfs41_init_client,
8539 .shutdown_client = nfs41_shutdown_client,
8540 .match_stateid = nfs41_match_stateid,
8541 .find_root_sec = nfs41_find_root_sec,
8542 .free_lock_state = nfs41_free_lock_state,
8543 .alloc_seqid = nfs_alloc_no_seqid,
8544 .call_sync_ops = &nfs41_call_sync_ops,
8545 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8546 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8547 .state_renewal_ops = &nfs41_state_renewal_ops,
8548 .mig_recovery_ops = &nfs41_mig_recovery_ops,
8552 #if defined(CONFIG_NFS_V4_2)
8553 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
8555 .init_caps = NFS_CAP_READDIRPLUS
8556 | NFS_CAP_ATOMIC_OPEN
8557 | NFS_CAP_CHANGE_ATTR
8558 | NFS_CAP_POSIX_LOCK
8559 | NFS_CAP_STATEID_NFSV41
8560 | NFS_CAP_ATOMIC_OPEN_V1
8562 | NFS_CAP_DEALLOCATE
8564 .init_client = nfs41_init_client,
8565 .shutdown_client = nfs41_shutdown_client,
8566 .match_stateid = nfs41_match_stateid,
8567 .find_root_sec = nfs41_find_root_sec,
8568 .free_lock_state = nfs41_free_lock_state,
8569 .call_sync_ops = &nfs41_call_sync_ops,
8570 .alloc_seqid = nfs_alloc_no_seqid,
8571 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8572 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8573 .state_renewal_ops = &nfs41_state_renewal_ops,
8577 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
8578 [0] = &nfs_v4_0_minor_ops,
8579 #if defined(CONFIG_NFS_V4_1)
8580 [1] = &nfs_v4_1_minor_ops,
8582 #if defined(CONFIG_NFS_V4_2)
8583 [2] = &nfs_v4_2_minor_ops,
8587 static const struct inode_operations nfs4_dir_inode_operations = {
8588 .create = nfs_create,
8589 .lookup = nfs_lookup,
8590 .atomic_open = nfs_atomic_open,
8592 .unlink = nfs_unlink,
8593 .symlink = nfs_symlink,
8597 .rename = nfs_rename,
8598 .permission = nfs_permission,
8599 .getattr = nfs_getattr,
8600 .setattr = nfs_setattr,
8601 .getxattr = generic_getxattr,
8602 .setxattr = generic_setxattr,
8603 .listxattr = generic_listxattr,
8604 .removexattr = generic_removexattr,
8607 static const struct inode_operations nfs4_file_inode_operations = {
8608 .permission = nfs_permission,
8609 .getattr = nfs_getattr,
8610 .setattr = nfs_setattr,
8611 .getxattr = generic_getxattr,
8612 .setxattr = generic_setxattr,
8613 .listxattr = generic_listxattr,
8614 .removexattr = generic_removexattr,
8617 const struct nfs_rpc_ops nfs_v4_clientops = {
8618 .version = 4, /* protocol version */
8619 .dentry_ops = &nfs4_dentry_operations,
8620 .dir_inode_ops = &nfs4_dir_inode_operations,
8621 .file_inode_ops = &nfs4_file_inode_operations,
8622 .file_ops = &nfs4_file_operations,
8623 .getroot = nfs4_proc_get_root,
8624 .submount = nfs4_submount,
8625 .try_mount = nfs4_try_mount,
8626 .getattr = nfs4_proc_getattr,
8627 .setattr = nfs4_proc_setattr,
8628 .lookup = nfs4_proc_lookup,
8629 .access = nfs4_proc_access,
8630 .readlink = nfs4_proc_readlink,
8631 .create = nfs4_proc_create,
8632 .remove = nfs4_proc_remove,
8633 .unlink_setup = nfs4_proc_unlink_setup,
8634 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
8635 .unlink_done = nfs4_proc_unlink_done,
8636 .rename_setup = nfs4_proc_rename_setup,
8637 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
8638 .rename_done = nfs4_proc_rename_done,
8639 .link = nfs4_proc_link,
8640 .symlink = nfs4_proc_symlink,
8641 .mkdir = nfs4_proc_mkdir,
8642 .rmdir = nfs4_proc_remove,
8643 .readdir = nfs4_proc_readdir,
8644 .mknod = nfs4_proc_mknod,
8645 .statfs = nfs4_proc_statfs,
8646 .fsinfo = nfs4_proc_fsinfo,
8647 .pathconf = nfs4_proc_pathconf,
8648 .set_capabilities = nfs4_server_capabilities,
8649 .decode_dirent = nfs4_decode_dirent,
8650 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
8651 .read_setup = nfs4_proc_read_setup,
8652 .read_done = nfs4_read_done,
8653 .write_setup = nfs4_proc_write_setup,
8654 .write_done = nfs4_write_done,
8655 .commit_setup = nfs4_proc_commit_setup,
8656 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
8657 .commit_done = nfs4_commit_done,
8658 .lock = nfs4_proc_lock,
8659 .clear_acl_cache = nfs4_zap_acl_attr,
8660 .close_context = nfs4_close_context,
8661 .open_context = nfs4_atomic_open,
8662 .have_delegation = nfs4_have_delegation,
8663 .return_delegation = nfs4_inode_return_delegation,
8664 .alloc_client = nfs4_alloc_client,
8665 .init_client = nfs4_init_client,
8666 .free_client = nfs4_free_client,
8667 .create_server = nfs4_create_server,
8668 .clone_server = nfs_clone_server,
8671 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
8672 .prefix = XATTR_NAME_NFSV4_ACL,
8673 .list = nfs4_xattr_list_nfs4_acl,
8674 .get = nfs4_xattr_get_nfs4_acl,
8675 .set = nfs4_xattr_set_nfs4_acl,
8678 const struct xattr_handler *nfs4_xattr_handlers[] = {
8679 &nfs4_xattr_nfs4_acl_handler,
8680 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
8681 &nfs4_xattr_nfs4_label_handler,