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:
374 if (inode && nfs4_have_delegation(inode,
376 nfs4_inode_return_delegation(inode);
377 exception->retry = 1;
381 ret = nfs4_schedule_stateid_recovery(server, state);
384 goto wait_on_recovery;
385 case -NFS4ERR_EXPIRED:
387 ret = nfs4_schedule_stateid_recovery(server, state);
391 case -NFS4ERR_STALE_STATEID:
392 case -NFS4ERR_STALE_CLIENTID:
393 nfs4_schedule_lease_recovery(clp);
394 goto wait_on_recovery;
396 ret = nfs4_schedule_migration_recovery(server);
399 goto wait_on_recovery;
400 case -NFS4ERR_LEASE_MOVED:
401 nfs4_schedule_lease_moved_recovery(clp);
402 goto wait_on_recovery;
403 #if defined(CONFIG_NFS_V4_1)
404 case -NFS4ERR_BADSESSION:
405 case -NFS4ERR_BADSLOT:
406 case -NFS4ERR_BAD_HIGH_SLOT:
407 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
408 case -NFS4ERR_DEADSESSION:
409 case -NFS4ERR_SEQ_FALSE_RETRY:
410 case -NFS4ERR_SEQ_MISORDERED:
411 dprintk("%s ERROR: %d Reset session\n", __func__,
413 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
414 goto wait_on_recovery;
415 #endif /* defined(CONFIG_NFS_V4_1) */
416 case -NFS4ERR_FILE_OPEN:
417 if (exception->timeout > HZ) {
418 /* We have retried a decent amount, time to
426 ret = nfs4_delay(server->client, &exception->timeout);
429 case -NFS4ERR_RETRY_UNCACHED_REP:
430 case -NFS4ERR_OLD_STATEID:
431 exception->retry = 1;
433 case -NFS4ERR_BADOWNER:
434 /* The following works around a Linux server bug! */
435 case -NFS4ERR_BADNAME:
436 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
437 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
438 exception->retry = 1;
439 printk(KERN_WARNING "NFS: v4 server %s "
440 "does not accept raw "
442 "Reenabling the idmapper.\n",
443 server->nfs_client->cl_hostname);
446 /* We failed to handle the error */
447 return nfs4_map_errors(ret);
449 ret = nfs4_wait_clnt_recover(clp);
450 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
453 exception->retry = 1;
458 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
459 * or 'false' otherwise.
461 static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
463 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
465 if (flavor == RPC_AUTH_GSS_KRB5I ||
466 flavor == RPC_AUTH_GSS_KRB5P)
472 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
474 spin_lock(&clp->cl_lock);
475 if (time_before(clp->cl_last_renewal,timestamp))
476 clp->cl_last_renewal = timestamp;
477 spin_unlock(&clp->cl_lock);
480 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
482 do_renew_lease(server->nfs_client, timestamp);
485 struct nfs4_call_sync_data {
486 const struct nfs_server *seq_server;
487 struct nfs4_sequence_args *seq_args;
488 struct nfs4_sequence_res *seq_res;
491 static void nfs4_init_sequence(struct nfs4_sequence_args *args,
492 struct nfs4_sequence_res *res, int cache_reply)
494 args->sa_slot = NULL;
495 args->sa_cache_this = cache_reply;
496 args->sa_privileged = 0;
501 static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
503 args->sa_privileged = 1;
506 int nfs40_setup_sequence(struct nfs4_slot_table *tbl,
507 struct nfs4_sequence_args *args,
508 struct nfs4_sequence_res *res,
509 struct rpc_task *task)
511 struct nfs4_slot *slot;
513 /* slot already allocated? */
514 if (res->sr_slot != NULL)
517 spin_lock(&tbl->slot_tbl_lock);
518 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
521 slot = nfs4_alloc_slot(tbl);
523 if (slot == ERR_PTR(-ENOMEM))
524 task->tk_timeout = HZ >> 2;
527 spin_unlock(&tbl->slot_tbl_lock);
529 args->sa_slot = slot;
533 rpc_call_start(task);
537 if (args->sa_privileged)
538 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
539 NULL, RPC_PRIORITY_PRIVILEGED);
541 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
542 spin_unlock(&tbl->slot_tbl_lock);
545 EXPORT_SYMBOL_GPL(nfs40_setup_sequence);
547 static int nfs40_sequence_done(struct rpc_task *task,
548 struct nfs4_sequence_res *res)
550 struct nfs4_slot *slot = res->sr_slot;
551 struct nfs4_slot_table *tbl;
557 spin_lock(&tbl->slot_tbl_lock);
558 if (!nfs41_wake_and_assign_slot(tbl, slot))
559 nfs4_free_slot(tbl, slot);
560 spin_unlock(&tbl->slot_tbl_lock);
567 #if defined(CONFIG_NFS_V4_1)
569 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
571 struct nfs4_session *session;
572 struct nfs4_slot_table *tbl;
573 struct nfs4_slot *slot = res->sr_slot;
574 bool send_new_highest_used_slotid = false;
577 session = tbl->session;
579 spin_lock(&tbl->slot_tbl_lock);
580 /* Be nice to the server: try to ensure that the last transmitted
581 * value for highest_user_slotid <= target_highest_slotid
583 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
584 send_new_highest_used_slotid = true;
586 if (nfs41_wake_and_assign_slot(tbl, slot)) {
587 send_new_highest_used_slotid = false;
590 nfs4_free_slot(tbl, slot);
592 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
593 send_new_highest_used_slotid = false;
595 spin_unlock(&tbl->slot_tbl_lock);
597 if (send_new_highest_used_slotid)
598 nfs41_server_notify_highest_slotid_update(session->clp);
601 int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
603 struct nfs4_session *session;
604 struct nfs4_slot *slot = res->sr_slot;
605 struct nfs_client *clp;
606 bool interrupted = false;
611 /* don't increment the sequence number if the task wasn't sent */
612 if (!RPC_WAS_SENT(task))
615 session = slot->table->session;
617 if (slot->interrupted) {
618 slot->interrupted = 0;
622 trace_nfs4_sequence_done(session, res);
623 /* Check the SEQUENCE operation status */
624 switch (res->sr_status) {
626 /* Update the slot's sequence and clientid lease timer */
629 do_renew_lease(clp, res->sr_timestamp);
630 /* Check sequence flags */
631 if (res->sr_status_flags != 0)
632 nfs4_schedule_lease_recovery(clp);
633 nfs41_update_target_slotid(slot->table, slot, res);
637 * sr_status remains 1 if an RPC level error occurred.
638 * The server may or may not have processed the sequence
640 * Mark the slot as having hosted an interrupted RPC call.
642 slot->interrupted = 1;
645 /* The server detected a resend of the RPC call and
646 * returned NFS4ERR_DELAY as per Section 2.10.6.2
649 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
654 case -NFS4ERR_BADSLOT:
656 * The slot id we used was probably retired. Try again
657 * using a different slot id.
660 case -NFS4ERR_SEQ_MISORDERED:
662 * Was the last operation on this sequence interrupted?
663 * If so, retry after bumping the sequence number.
670 * Could this slot have been previously retired?
671 * If so, then the server may be expecting seq_nr = 1!
673 if (slot->seq_nr != 1) {
678 case -NFS4ERR_SEQ_FALSE_RETRY:
682 /* Just update the slot sequence no. */
686 /* The session may be reset by one of the error handlers. */
687 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
688 nfs41_sequence_free_slot(res);
692 if (rpc_restart_call_prepare(task)) {
698 if (!rpc_restart_call(task))
700 rpc_delay(task, NFS4_POLL_RETRY_MAX);
703 EXPORT_SYMBOL_GPL(nfs41_sequence_done);
705 int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
707 if (res->sr_slot == NULL)
709 if (!res->sr_slot->table->session)
710 return nfs40_sequence_done(task, res);
711 return nfs41_sequence_done(task, res);
713 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
715 int nfs41_setup_sequence(struct nfs4_session *session,
716 struct nfs4_sequence_args *args,
717 struct nfs4_sequence_res *res,
718 struct rpc_task *task)
720 struct nfs4_slot *slot;
721 struct nfs4_slot_table *tbl;
723 dprintk("--> %s\n", __func__);
724 /* slot already allocated? */
725 if (res->sr_slot != NULL)
728 tbl = &session->fc_slot_table;
730 task->tk_timeout = 0;
732 spin_lock(&tbl->slot_tbl_lock);
733 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
734 !args->sa_privileged) {
735 /* The state manager will wait until the slot table is empty */
736 dprintk("%s session is draining\n", __func__);
740 slot = nfs4_alloc_slot(tbl);
742 /* If out of memory, try again in 1/4 second */
743 if (slot == ERR_PTR(-ENOMEM))
744 task->tk_timeout = HZ >> 2;
745 dprintk("<-- %s: no free slots\n", __func__);
748 spin_unlock(&tbl->slot_tbl_lock);
750 args->sa_slot = slot;
752 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
753 slot->slot_nr, slot->seq_nr);
756 res->sr_timestamp = jiffies;
757 res->sr_status_flags = 0;
759 * sr_status is only set in decode_sequence, and so will remain
760 * set to 1 if an rpc level failure occurs.
763 trace_nfs4_setup_sequence(session, args);
765 rpc_call_start(task);
768 /* Privileged tasks are queued with top priority */
769 if (args->sa_privileged)
770 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
771 NULL, RPC_PRIORITY_PRIVILEGED);
773 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
774 spin_unlock(&tbl->slot_tbl_lock);
777 EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
779 static int nfs4_setup_sequence(const struct nfs_server *server,
780 struct nfs4_sequence_args *args,
781 struct nfs4_sequence_res *res,
782 struct rpc_task *task)
784 struct nfs4_session *session = nfs4_get_session(server);
788 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
791 dprintk("--> %s clp %p session %p sr_slot %u\n",
792 __func__, session->clp, session, res->sr_slot ?
793 res->sr_slot->slot_nr : NFS4_NO_SLOT);
795 ret = nfs41_setup_sequence(session, args, res, task);
797 dprintk("<-- %s status=%d\n", __func__, ret);
801 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
803 struct nfs4_call_sync_data *data = calldata;
804 struct nfs4_session *session = nfs4_get_session(data->seq_server);
806 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
808 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
811 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
813 struct nfs4_call_sync_data *data = calldata;
815 nfs41_sequence_done(task, data->seq_res);
818 static const struct rpc_call_ops nfs41_call_sync_ops = {
819 .rpc_call_prepare = nfs41_call_sync_prepare,
820 .rpc_call_done = nfs41_call_sync_done,
823 #else /* !CONFIG_NFS_V4_1 */
825 static int nfs4_setup_sequence(const struct nfs_server *server,
826 struct nfs4_sequence_args *args,
827 struct nfs4_sequence_res *res,
828 struct rpc_task *task)
830 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
834 int nfs4_sequence_done(struct rpc_task *task,
835 struct nfs4_sequence_res *res)
837 return nfs40_sequence_done(task, res);
839 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
841 #endif /* !CONFIG_NFS_V4_1 */
843 static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
845 struct nfs4_call_sync_data *data = calldata;
846 nfs4_setup_sequence(data->seq_server,
847 data->seq_args, data->seq_res, task);
850 static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
852 struct nfs4_call_sync_data *data = calldata;
853 nfs4_sequence_done(task, data->seq_res);
856 static const struct rpc_call_ops nfs40_call_sync_ops = {
857 .rpc_call_prepare = nfs40_call_sync_prepare,
858 .rpc_call_done = nfs40_call_sync_done,
861 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
862 struct nfs_server *server,
863 struct rpc_message *msg,
864 struct nfs4_sequence_args *args,
865 struct nfs4_sequence_res *res)
868 struct rpc_task *task;
869 struct nfs_client *clp = server->nfs_client;
870 struct nfs4_call_sync_data data = {
871 .seq_server = server,
875 struct rpc_task_setup task_setup = {
878 .callback_ops = clp->cl_mvops->call_sync_ops,
879 .callback_data = &data
882 task = rpc_run_task(&task_setup);
886 ret = task->tk_status;
892 int nfs4_call_sync(struct rpc_clnt *clnt,
893 struct nfs_server *server,
894 struct rpc_message *msg,
895 struct nfs4_sequence_args *args,
896 struct nfs4_sequence_res *res,
899 nfs4_init_sequence(args, res, cache_reply);
900 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
903 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
905 struct nfs_inode *nfsi = NFS_I(dir);
907 spin_lock(&dir->i_lock);
908 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
909 if (!cinfo->atomic || cinfo->before != dir->i_version)
910 nfs_force_lookup_revalidate(dir);
911 dir->i_version = cinfo->after;
912 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
913 nfs_fscache_invalidate(dir);
914 spin_unlock(&dir->i_lock);
917 struct nfs4_opendata {
919 struct nfs_openargs o_arg;
920 struct nfs_openres o_res;
921 struct nfs_open_confirmargs c_arg;
922 struct nfs_open_confirmres c_res;
923 struct nfs4_string owner_name;
924 struct nfs4_string group_name;
925 struct nfs_fattr f_attr;
926 struct nfs4_label *f_label;
928 struct dentry *dentry;
929 struct nfs4_state_owner *owner;
930 struct nfs4_state *state;
932 unsigned long timestamp;
933 unsigned int rpc_done : 1;
934 unsigned int file_created : 1;
935 unsigned int is_recover : 1;
940 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
941 int err, struct nfs4_exception *exception)
945 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
947 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
948 exception->retry = 1;
953 nfs4_map_atomic_open_share(struct nfs_server *server,
954 fmode_t fmode, int openflags)
958 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
960 res = NFS4_SHARE_ACCESS_READ;
963 res = NFS4_SHARE_ACCESS_WRITE;
965 case FMODE_READ|FMODE_WRITE:
966 res = NFS4_SHARE_ACCESS_BOTH;
968 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
970 /* Want no delegation if we're using O_DIRECT */
971 if (openflags & O_DIRECT)
972 res |= NFS4_SHARE_WANT_NO_DELEG;
977 static enum open_claim_type4
978 nfs4_map_atomic_open_claim(struct nfs_server *server,
979 enum open_claim_type4 claim)
981 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
986 case NFS4_OPEN_CLAIM_FH:
987 return NFS4_OPEN_CLAIM_NULL;
988 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
989 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
990 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
991 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
995 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
997 p->o_res.f_attr = &p->f_attr;
998 p->o_res.f_label = p->f_label;
999 p->o_res.seqid = p->o_arg.seqid;
1000 p->c_res.seqid = p->c_arg.seqid;
1001 p->o_res.server = p->o_arg.server;
1002 p->o_res.access_request = p->o_arg.access;
1003 nfs_fattr_init(&p->f_attr);
1004 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
1007 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
1008 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
1009 const struct iattr *attrs,
1010 struct nfs4_label *label,
1011 enum open_claim_type4 claim,
1014 struct dentry *parent = dget_parent(dentry);
1015 struct inode *dir = d_inode(parent);
1016 struct nfs_server *server = NFS_SERVER(dir);
1017 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
1018 struct nfs4_opendata *p;
1020 p = kzalloc(sizeof(*p), gfp_mask);
1024 p->f_label = nfs4_label_alloc(server, gfp_mask);
1025 if (IS_ERR(p->f_label))
1028 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1029 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
1030 if (IS_ERR(p->o_arg.seqid))
1031 goto err_free_label;
1032 nfs_sb_active(dentry->d_sb);
1033 p->dentry = dget(dentry);
1036 atomic_inc(&sp->so_count);
1037 p->o_arg.open_flags = flags;
1038 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
1039 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1041 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1042 * will return permission denied for all bits until close */
1043 if (!(flags & O_EXCL)) {
1044 /* ask server to check for all possible rights as results
1046 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1047 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
1049 p->o_arg.clientid = server->nfs_client->cl_clientid;
1050 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1051 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1052 p->o_arg.name = &dentry->d_name;
1053 p->o_arg.server = server;
1054 p->o_arg.bitmask = nfs4_bitmask(server, label);
1055 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1056 p->o_arg.label = label;
1057 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1058 switch (p->o_arg.claim) {
1059 case NFS4_OPEN_CLAIM_NULL:
1060 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1061 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1062 p->o_arg.fh = NFS_FH(dir);
1064 case NFS4_OPEN_CLAIM_PREVIOUS:
1065 case NFS4_OPEN_CLAIM_FH:
1066 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1067 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1068 p->o_arg.fh = NFS_FH(d_inode(dentry));
1070 if (attrs != NULL && attrs->ia_valid != 0) {
1073 p->o_arg.u.attrs = &p->attrs;
1074 memcpy(&p->attrs, attrs, sizeof(p->attrs));
1077 verf[1] = current->pid;
1078 memcpy(p->o_arg.u.verifier.data, verf,
1079 sizeof(p->o_arg.u.verifier.data));
1081 p->c_arg.fh = &p->o_res.fh;
1082 p->c_arg.stateid = &p->o_res.stateid;
1083 p->c_arg.seqid = p->o_arg.seqid;
1084 nfs4_init_opendata_res(p);
1085 kref_init(&p->kref);
1089 nfs4_label_free(p->f_label);
1097 static void nfs4_opendata_free(struct kref *kref)
1099 struct nfs4_opendata *p = container_of(kref,
1100 struct nfs4_opendata, kref);
1101 struct super_block *sb = p->dentry->d_sb;
1103 nfs_free_seqid(p->o_arg.seqid);
1104 if (p->state != NULL)
1105 nfs4_put_open_state(p->state);
1106 nfs4_put_state_owner(p->owner);
1108 nfs4_label_free(p->f_label);
1112 nfs_sb_deactive(sb);
1113 nfs_fattr_free_names(&p->f_attr);
1114 kfree(p->f_attr.mdsthreshold);
1118 static void nfs4_opendata_put(struct nfs4_opendata *p)
1121 kref_put(&p->kref, nfs4_opendata_free);
1124 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1128 ret = rpc_wait_for_completion_task(task);
1132 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
1136 if (open_mode & (O_EXCL|O_TRUNC))
1138 switch (mode & (FMODE_READ|FMODE_WRITE)) {
1140 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1141 && state->n_rdonly != 0;
1144 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1145 && state->n_wronly != 0;
1147 case FMODE_READ|FMODE_WRITE:
1148 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1149 && state->n_rdwr != 0;
1155 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
1157 if (delegation == NULL)
1159 if ((delegation->type & fmode) != fmode)
1161 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1163 nfs_mark_delegation_referenced(delegation);
1167 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1176 case FMODE_READ|FMODE_WRITE:
1179 nfs4_state_set_mode_locked(state, state->state | fmode);
1182 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
1184 struct nfs_client *clp = state->owner->so_server->nfs_client;
1185 bool need_recover = false;
1187 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1188 need_recover = true;
1189 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1190 need_recover = true;
1191 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1192 need_recover = true;
1194 nfs4_state_mark_reclaim_nograce(clp, state);
1197 static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1198 nfs4_stateid *stateid)
1200 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1202 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1203 nfs_test_and_clear_all_open_stateid(state);
1206 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1211 static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1213 if (state->n_wronly)
1214 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1215 if (state->n_rdonly)
1216 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1218 set_bit(NFS_O_RDWR_STATE, &state->flags);
1221 static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1222 nfs4_stateid *stateid, fmode_t fmode)
1224 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1225 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1227 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1230 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1233 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1234 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1235 clear_bit(NFS_OPEN_STATE, &state->flags);
1237 if (stateid == NULL)
1239 /* Handle races with OPEN */
1240 if (!nfs4_stateid_match_other(stateid, &state->open_stateid) ||
1241 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1242 nfs_resync_open_stateid_locked(state);
1245 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1246 nfs4_stateid_copy(&state->stateid, stateid);
1247 nfs4_stateid_copy(&state->open_stateid, stateid);
1250 static void nfs_clear_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1252 write_seqlock(&state->seqlock);
1253 nfs_clear_open_stateid_locked(state, stateid, fmode);
1254 write_sequnlock(&state->seqlock);
1255 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1256 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1259 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1263 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1266 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1268 case FMODE_READ|FMODE_WRITE:
1269 set_bit(NFS_O_RDWR_STATE, &state->flags);
1271 if (!nfs_need_update_open_stateid(state, stateid))
1273 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1274 nfs4_stateid_copy(&state->stateid, stateid);
1275 nfs4_stateid_copy(&state->open_stateid, stateid);
1278 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
1281 * Protect the call to nfs4_state_set_mode_locked and
1282 * serialise the stateid update
1284 write_seqlock(&state->seqlock);
1285 if (deleg_stateid != NULL) {
1286 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1287 set_bit(NFS_DELEGATED_STATE, &state->flags);
1289 if (open_stateid != NULL)
1290 nfs_set_open_stateid_locked(state, open_stateid, fmode);
1291 write_sequnlock(&state->seqlock);
1292 spin_lock(&state->owner->so_lock);
1293 update_open_stateflags(state, fmode);
1294 spin_unlock(&state->owner->so_lock);
1297 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
1299 struct nfs_inode *nfsi = NFS_I(state->inode);
1300 struct nfs_delegation *deleg_cur;
1303 fmode &= (FMODE_READ|FMODE_WRITE);
1306 deleg_cur = rcu_dereference(nfsi->delegation);
1307 if (deleg_cur == NULL)
1310 spin_lock(&deleg_cur->lock);
1311 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1312 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1313 (deleg_cur->type & fmode) != fmode)
1314 goto no_delegation_unlock;
1316 if (delegation == NULL)
1317 delegation = &deleg_cur->stateid;
1318 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
1319 goto no_delegation_unlock;
1321 nfs_mark_delegation_referenced(deleg_cur);
1322 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
1324 no_delegation_unlock:
1325 spin_unlock(&deleg_cur->lock);
1329 if (!ret && open_stateid != NULL) {
1330 __update_open_stateid(state, open_stateid, NULL, fmode);
1333 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1334 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1339 static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1340 const nfs4_stateid *stateid)
1342 struct nfs4_state *state = lsp->ls_state;
1345 spin_lock(&state->state_lock);
1346 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1348 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1350 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1353 spin_unlock(&state->state_lock);
1357 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1359 struct nfs_delegation *delegation;
1362 delegation = rcu_dereference(NFS_I(inode)->delegation);
1363 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1368 nfs4_inode_return_delegation(inode);
1371 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1373 struct nfs4_state *state = opendata->state;
1374 struct nfs_inode *nfsi = NFS_I(state->inode);
1375 struct nfs_delegation *delegation;
1376 int open_mode = opendata->o_arg.open_flags;
1377 fmode_t fmode = opendata->o_arg.fmode;
1378 nfs4_stateid stateid;
1382 spin_lock(&state->owner->so_lock);
1383 if (can_open_cached(state, fmode, open_mode)) {
1384 update_open_stateflags(state, fmode);
1385 spin_unlock(&state->owner->so_lock);
1386 goto out_return_state;
1388 spin_unlock(&state->owner->so_lock);
1390 delegation = rcu_dereference(nfsi->delegation);
1391 if (!can_open_delegated(delegation, fmode)) {
1395 /* Save the delegation */
1396 nfs4_stateid_copy(&stateid, &delegation->stateid);
1398 nfs_release_seqid(opendata->o_arg.seqid);
1399 if (!opendata->is_recover) {
1400 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1406 /* Try to update the stateid using the delegation */
1407 if (update_open_stateid(state, NULL, &stateid, fmode))
1408 goto out_return_state;
1411 return ERR_PTR(ret);
1413 atomic_inc(&state->count);
1418 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1420 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1421 struct nfs_delegation *delegation;
1422 int delegation_flags = 0;
1425 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1427 delegation_flags = delegation->flags;
1429 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1430 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1431 "returning a delegation for "
1432 "OPEN(CLAIM_DELEGATE_CUR)\n",
1434 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1435 nfs_inode_set_delegation(state->inode,
1436 data->owner->so_cred,
1439 nfs_inode_reclaim_delegation(state->inode,
1440 data->owner->so_cred,
1445 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1446 * and update the nfs4_state.
1448 static struct nfs4_state *
1449 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1451 struct inode *inode = data->state->inode;
1452 struct nfs4_state *state = data->state;
1455 if (!data->rpc_done) {
1456 if (data->rpc_status) {
1457 ret = data->rpc_status;
1460 /* cached opens have already been processed */
1464 ret = nfs_refresh_inode(inode, &data->f_attr);
1468 if (data->o_res.delegation_type != 0)
1469 nfs4_opendata_check_deleg(data, state);
1471 update_open_stateid(state, &data->o_res.stateid, NULL,
1473 atomic_inc(&state->count);
1477 return ERR_PTR(ret);
1481 static struct nfs4_state *
1482 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1484 struct inode *inode;
1485 struct nfs4_state *state = NULL;
1488 if (!data->rpc_done) {
1489 state = nfs4_try_open_cached(data);
1494 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1496 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
1497 ret = PTR_ERR(inode);
1501 state = nfs4_get_open_state(inode, data->owner);
1504 if (data->o_res.delegation_type != 0)
1505 nfs4_opendata_check_deleg(data, state);
1506 update_open_stateid(state, &data->o_res.stateid, NULL,
1510 nfs_release_seqid(data->o_arg.seqid);
1515 return ERR_PTR(ret);
1518 static struct nfs4_state *
1519 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1521 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1522 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1523 return _nfs4_opendata_to_nfs4_state(data);
1526 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1528 struct nfs_inode *nfsi = NFS_I(state->inode);
1529 struct nfs_open_context *ctx;
1531 spin_lock(&state->inode->i_lock);
1532 list_for_each_entry(ctx, &nfsi->open_files, list) {
1533 if (ctx->state != state)
1535 get_nfs_open_context(ctx);
1536 spin_unlock(&state->inode->i_lock);
1539 spin_unlock(&state->inode->i_lock);
1540 return ERR_PTR(-ENOENT);
1543 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1544 struct nfs4_state *state, enum open_claim_type4 claim)
1546 struct nfs4_opendata *opendata;
1548 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
1549 NULL, NULL, claim, GFP_NOFS);
1550 if (opendata == NULL)
1551 return ERR_PTR(-ENOMEM);
1552 opendata->state = state;
1553 atomic_inc(&state->count);
1557 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1559 struct nfs4_state *newstate;
1562 opendata->o_arg.open_flags = 0;
1563 opendata->o_arg.fmode = fmode;
1564 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1565 NFS_SB(opendata->dentry->d_sb),
1567 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1568 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1569 nfs4_init_opendata_res(opendata);
1570 ret = _nfs4_recover_proc_open(opendata);
1573 newstate = nfs4_opendata_to_nfs4_state(opendata);
1574 if (IS_ERR(newstate))
1575 return PTR_ERR(newstate);
1576 nfs4_close_state(newstate, fmode);
1581 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1583 struct nfs4_state *newstate;
1586 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1587 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1588 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1589 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1590 /* memory barrier prior to reading state->n_* */
1591 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1592 clear_bit(NFS_OPEN_STATE, &state->flags);
1594 if (state->n_rdwr != 0) {
1595 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1598 if (newstate != state)
1601 if (state->n_wronly != 0) {
1602 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1605 if (newstate != state)
1608 if (state->n_rdonly != 0) {
1609 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1612 if (newstate != state)
1616 * We may have performed cached opens for all three recoveries.
1617 * Check if we need to update the current stateid.
1619 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1620 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
1621 write_seqlock(&state->seqlock);
1622 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1623 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1624 write_sequnlock(&state->seqlock);
1631 * reclaim state on the server after a reboot.
1633 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1635 struct nfs_delegation *delegation;
1636 struct nfs4_opendata *opendata;
1637 fmode_t delegation_type = 0;
1640 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1641 NFS4_OPEN_CLAIM_PREVIOUS);
1642 if (IS_ERR(opendata))
1643 return PTR_ERR(opendata);
1645 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1646 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1647 delegation_type = delegation->type;
1649 opendata->o_arg.u.delegation_type = delegation_type;
1650 status = nfs4_open_recover(opendata, state);
1651 nfs4_opendata_put(opendata);
1655 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1657 struct nfs_server *server = NFS_SERVER(state->inode);
1658 struct nfs4_exception exception = { };
1661 err = _nfs4_do_open_reclaim(ctx, state);
1662 trace_nfs4_open_reclaim(ctx, 0, err);
1663 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1665 if (err != -NFS4ERR_DELAY)
1667 nfs4_handle_exception(server, err, &exception);
1668 } while (exception.retry);
1672 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1674 struct nfs_open_context *ctx;
1677 ctx = nfs4_state_find_open_context(state);
1680 ret = nfs4_do_open_reclaim(ctx, state);
1681 put_nfs_open_context(ctx);
1685 static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, int err)
1689 printk(KERN_ERR "NFS: %s: unhandled error "
1690 "%d.\n", __func__, err);
1695 case -NFS4ERR_BADSESSION:
1696 case -NFS4ERR_BADSLOT:
1697 case -NFS4ERR_BAD_HIGH_SLOT:
1698 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1699 case -NFS4ERR_DEADSESSION:
1700 set_bit(NFS_DELEGATED_STATE, &state->flags);
1701 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1703 case -NFS4ERR_STALE_CLIENTID:
1704 case -NFS4ERR_STALE_STATEID:
1705 set_bit(NFS_DELEGATED_STATE, &state->flags);
1706 case -NFS4ERR_EXPIRED:
1707 /* Don't recall a delegation if it was lost */
1708 nfs4_schedule_lease_recovery(server->nfs_client);
1710 case -NFS4ERR_MOVED:
1711 nfs4_schedule_migration_recovery(server);
1713 case -NFS4ERR_LEASE_MOVED:
1714 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1716 case -NFS4ERR_DELEG_REVOKED:
1717 case -NFS4ERR_ADMIN_REVOKED:
1718 case -NFS4ERR_BAD_STATEID:
1719 case -NFS4ERR_OPENMODE:
1720 nfs_inode_find_state_and_recover(state->inode,
1722 nfs4_schedule_stateid_recovery(server, state);
1724 case -NFS4ERR_DELAY:
1725 case -NFS4ERR_GRACE:
1726 set_bit(NFS_DELEGATED_STATE, &state->flags);
1730 case -NFS4ERR_DENIED:
1731 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1737 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1739 struct nfs_server *server = NFS_SERVER(state->inode);
1740 struct nfs4_opendata *opendata;
1743 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1744 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1745 if (IS_ERR(opendata))
1746 return PTR_ERR(opendata);
1747 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1748 err = nfs4_open_recover(opendata, state);
1749 nfs4_opendata_put(opendata);
1750 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1753 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1755 struct nfs4_opendata *data = calldata;
1757 nfs40_setup_sequence(data->o_arg.server->nfs_client->cl_slot_tbl,
1758 &data->c_arg.seq_args, &data->c_res.seq_res, task);
1761 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1763 struct nfs4_opendata *data = calldata;
1765 nfs40_sequence_done(task, &data->c_res.seq_res);
1767 data->rpc_status = task->tk_status;
1768 if (data->rpc_status == 0) {
1769 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
1770 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1771 renew_lease(data->o_res.server, data->timestamp);
1776 static void nfs4_open_confirm_release(void *calldata)
1778 struct nfs4_opendata *data = calldata;
1779 struct nfs4_state *state = NULL;
1781 /* If this request hasn't been cancelled, do nothing */
1782 if (data->cancelled == 0)
1784 /* In case of error, no cleanup! */
1785 if (!data->rpc_done)
1787 state = nfs4_opendata_to_nfs4_state(data);
1789 nfs4_close_state(state, data->o_arg.fmode);
1791 nfs4_opendata_put(data);
1794 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1795 .rpc_call_prepare = nfs4_open_confirm_prepare,
1796 .rpc_call_done = nfs4_open_confirm_done,
1797 .rpc_release = nfs4_open_confirm_release,
1801 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1803 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1805 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
1806 struct rpc_task *task;
1807 struct rpc_message msg = {
1808 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1809 .rpc_argp = &data->c_arg,
1810 .rpc_resp = &data->c_res,
1811 .rpc_cred = data->owner->so_cred,
1813 struct rpc_task_setup task_setup_data = {
1814 .rpc_client = server->client,
1815 .rpc_message = &msg,
1816 .callback_ops = &nfs4_open_confirm_ops,
1817 .callback_data = data,
1818 .workqueue = nfsiod_workqueue,
1819 .flags = RPC_TASK_ASYNC,
1823 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
1824 kref_get(&data->kref);
1826 data->rpc_status = 0;
1827 data->timestamp = jiffies;
1828 task = rpc_run_task(&task_setup_data);
1830 return PTR_ERR(task);
1831 status = nfs4_wait_for_completion_rpc_task(task);
1833 data->cancelled = 1;
1836 status = data->rpc_status;
1841 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1843 struct nfs4_opendata *data = calldata;
1844 struct nfs4_state_owner *sp = data->owner;
1845 struct nfs_client *clp = sp->so_server->nfs_client;
1847 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1850 * Check if we still need to send an OPEN call, or if we can use
1851 * a delegation instead.
1853 if (data->state != NULL) {
1854 struct nfs_delegation *delegation;
1856 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1859 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1860 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1861 data->o_arg.claim != NFS4_OPEN_CLAIM_DELEG_CUR_FH &&
1862 can_open_delegated(delegation, data->o_arg.fmode))
1863 goto unlock_no_action;
1866 /* Update client id. */
1867 data->o_arg.clientid = clp->cl_clientid;
1868 switch (data->o_arg.claim) {
1869 case NFS4_OPEN_CLAIM_PREVIOUS:
1870 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1871 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1872 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
1873 case NFS4_OPEN_CLAIM_FH:
1874 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1875 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1877 data->timestamp = jiffies;
1878 if (nfs4_setup_sequence(data->o_arg.server,
1879 &data->o_arg.seq_args,
1880 &data->o_res.seq_res,
1882 nfs_release_seqid(data->o_arg.seqid);
1884 /* Set the create mode (note dependency on the session type) */
1885 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1886 if (data->o_arg.open_flags & O_EXCL) {
1887 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1888 if (nfs4_has_persistent_session(clp))
1889 data->o_arg.createmode = NFS4_CREATE_GUARDED;
1890 else if (clp->cl_mvops->minor_version > 0)
1891 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1897 task->tk_action = NULL;
1899 nfs4_sequence_done(task, &data->o_res.seq_res);
1902 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1904 struct nfs4_opendata *data = calldata;
1906 data->rpc_status = task->tk_status;
1908 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1911 if (task->tk_status == 0) {
1912 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1913 switch (data->o_res.f_attr->mode & S_IFMT) {
1917 data->rpc_status = -ELOOP;
1920 data->rpc_status = -EISDIR;
1923 data->rpc_status = -ENOTDIR;
1926 renew_lease(data->o_res.server, data->timestamp);
1927 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1928 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1933 static void nfs4_open_release(void *calldata)
1935 struct nfs4_opendata *data = calldata;
1936 struct nfs4_state *state = NULL;
1938 /* If this request hasn't been cancelled, do nothing */
1939 if (data->cancelled == 0)
1941 /* In case of error, no cleanup! */
1942 if (data->rpc_status != 0 || !data->rpc_done)
1944 /* In case we need an open_confirm, no cleanup! */
1945 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1947 state = nfs4_opendata_to_nfs4_state(data);
1949 nfs4_close_state(state, data->o_arg.fmode);
1951 nfs4_opendata_put(data);
1954 static const struct rpc_call_ops nfs4_open_ops = {
1955 .rpc_call_prepare = nfs4_open_prepare,
1956 .rpc_call_done = nfs4_open_done,
1957 .rpc_release = nfs4_open_release,
1960 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
1962 struct inode *dir = d_inode(data->dir);
1963 struct nfs_server *server = NFS_SERVER(dir);
1964 struct nfs_openargs *o_arg = &data->o_arg;
1965 struct nfs_openres *o_res = &data->o_res;
1966 struct rpc_task *task;
1967 struct rpc_message msg = {
1968 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1971 .rpc_cred = data->owner->so_cred,
1973 struct rpc_task_setup task_setup_data = {
1974 .rpc_client = server->client,
1975 .rpc_message = &msg,
1976 .callback_ops = &nfs4_open_ops,
1977 .callback_data = data,
1978 .workqueue = nfsiod_workqueue,
1979 .flags = RPC_TASK_ASYNC,
1983 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
1984 kref_get(&data->kref);
1986 data->rpc_status = 0;
1987 data->cancelled = 0;
1988 data->is_recover = 0;
1990 nfs4_set_sequence_privileged(&o_arg->seq_args);
1991 data->is_recover = 1;
1993 task = rpc_run_task(&task_setup_data);
1995 return PTR_ERR(task);
1996 status = nfs4_wait_for_completion_rpc_task(task);
1998 data->cancelled = 1;
2001 status = data->rpc_status;
2007 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2009 struct inode *dir = d_inode(data->dir);
2010 struct nfs_openres *o_res = &data->o_res;
2013 status = nfs4_run_open_task(data, 1);
2014 if (status != 0 || !data->rpc_done)
2017 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2019 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2020 status = _nfs4_proc_open_confirm(data);
2029 * Additional permission checks in order to distinguish between an
2030 * open for read, and an open for execute. This works around the
2031 * fact that NFSv4 OPEN treats read and execute permissions as being
2033 * Note that in the non-execute case, we want to turn off permission
2034 * checking if we just created a new file (POSIX open() semantics).
2036 static int nfs4_opendata_access(struct rpc_cred *cred,
2037 struct nfs4_opendata *opendata,
2038 struct nfs4_state *state, fmode_t fmode,
2041 struct nfs_access_entry cache;
2044 /* access call failed or for some reason the server doesn't
2045 * support any access modes -- defer access call until later */
2046 if (opendata->o_res.access_supported == 0)
2051 * Use openflags to check for exec, because fmode won't
2052 * always have FMODE_EXEC set when file open for exec.
2054 if (openflags & __FMODE_EXEC) {
2055 /* ONLY check for exec rights */
2057 } else if ((fmode & FMODE_READ) && !opendata->file_created)
2061 cache.jiffies = jiffies;
2062 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2063 nfs_access_add_cache(state->inode, &cache);
2065 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
2068 /* even though OPEN succeeded, access is denied. Close the file */
2069 nfs4_close_state(state, fmode);
2074 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2076 static int _nfs4_proc_open(struct nfs4_opendata *data)
2078 struct inode *dir = d_inode(data->dir);
2079 struct nfs_server *server = NFS_SERVER(dir);
2080 struct nfs_openargs *o_arg = &data->o_arg;
2081 struct nfs_openres *o_res = &data->o_res;
2084 status = nfs4_run_open_task(data, 0);
2085 if (!data->rpc_done)
2088 if (status == -NFS4ERR_BADNAME &&
2089 !(o_arg->open_flags & O_CREAT))
2094 nfs_fattr_map_and_free_names(server, &data->f_attr);
2096 if (o_arg->open_flags & O_CREAT) {
2097 update_changeattr(dir, &o_res->cinfo);
2098 if (o_arg->open_flags & O_EXCL)
2099 data->file_created = 1;
2100 else if (o_res->cinfo.before != o_res->cinfo.after)
2101 data->file_created = 1;
2103 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2104 server->caps &= ~NFS_CAP_POSIX_LOCK;
2105 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2106 status = _nfs4_proc_open_confirm(data);
2110 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
2111 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
2115 static int nfs4_recover_expired_lease(struct nfs_server *server)
2117 return nfs4_client_recover_expired_lease(server->nfs_client);
2122 * reclaim state on the server after a network partition.
2123 * Assumes caller holds the appropriate lock
2125 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2127 struct nfs4_opendata *opendata;
2130 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2131 NFS4_OPEN_CLAIM_FH);
2132 if (IS_ERR(opendata))
2133 return PTR_ERR(opendata);
2134 ret = nfs4_open_recover(opendata, state);
2136 d_drop(ctx->dentry);
2137 nfs4_opendata_put(opendata);
2141 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2143 struct nfs_server *server = NFS_SERVER(state->inode);
2144 struct nfs4_exception exception = { };
2148 err = _nfs4_open_expired(ctx, state);
2149 trace_nfs4_open_expired(ctx, 0, err);
2150 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2155 case -NFS4ERR_GRACE:
2156 case -NFS4ERR_DELAY:
2157 nfs4_handle_exception(server, err, &exception);
2160 } while (exception.retry);
2165 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2167 struct nfs_open_context *ctx;
2170 ctx = nfs4_state_find_open_context(state);
2173 ret = nfs4_do_open_expired(ctx, state);
2174 put_nfs_open_context(ctx);
2178 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state)
2180 nfs_remove_bad_delegation(state->inode);
2181 write_seqlock(&state->seqlock);
2182 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2183 write_sequnlock(&state->seqlock);
2184 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2187 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2189 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2190 nfs_finish_clear_delegation_stateid(state);
2193 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2195 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2196 nfs40_clear_delegation_stateid(state);
2197 return nfs4_open_expired(sp, state);
2200 #if defined(CONFIG_NFS_V4_1)
2201 static void nfs41_check_delegation_stateid(struct nfs4_state *state)
2203 struct nfs_server *server = NFS_SERVER(state->inode);
2204 nfs4_stateid stateid;
2205 struct nfs_delegation *delegation;
2206 struct rpc_cred *cred;
2209 /* Get the delegation credential for use by test/free_stateid */
2211 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2212 if (delegation == NULL) {
2217 nfs4_stateid_copy(&stateid, &delegation->stateid);
2218 cred = get_rpccred(delegation->cred);
2220 status = nfs41_test_stateid(server, &stateid, cred);
2221 trace_nfs4_test_delegation_stateid(state, NULL, status);
2223 if (status != NFS_OK) {
2224 /* Free the stateid unless the server explicitly
2225 * informs us the stateid is unrecognized. */
2226 if (status != -NFS4ERR_BAD_STATEID)
2227 nfs41_free_stateid(server, &stateid, cred);
2228 nfs_finish_clear_delegation_stateid(state);
2235 * nfs41_check_open_stateid - possibly free an open stateid
2237 * @state: NFSv4 state for an inode
2239 * Returns NFS_OK if recovery for this stateid is now finished.
2240 * Otherwise a negative NFS4ERR value is returned.
2242 static int nfs41_check_open_stateid(struct nfs4_state *state)
2244 struct nfs_server *server = NFS_SERVER(state->inode);
2245 nfs4_stateid *stateid = &state->open_stateid;
2246 struct rpc_cred *cred = state->owner->so_cred;
2249 /* If a state reset has been done, test_stateid is unneeded */
2250 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2251 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2252 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2253 return -NFS4ERR_BAD_STATEID;
2255 status = nfs41_test_stateid(server, stateid, cred);
2256 trace_nfs4_test_open_stateid(state, NULL, status);
2257 if (status != NFS_OK) {
2258 /* Free the stateid unless the server explicitly
2259 * informs us the stateid is unrecognized. */
2260 if (status != -NFS4ERR_BAD_STATEID)
2261 nfs41_free_stateid(server, stateid, cred);
2263 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2264 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2265 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2266 clear_bit(NFS_OPEN_STATE, &state->flags);
2271 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2275 nfs41_check_delegation_stateid(state);
2276 status = nfs41_check_open_stateid(state);
2277 if (status != NFS_OK)
2278 status = nfs4_open_expired(sp, state);
2284 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2285 * fields corresponding to attributes that were used to store the verifier.
2286 * Make sure we clobber those fields in the later setattr call
2288 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
2290 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2291 !(sattr->ia_valid & ATTR_ATIME_SET))
2292 sattr->ia_valid |= ATTR_ATIME;
2294 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2295 !(sattr->ia_valid & ATTR_MTIME_SET))
2296 sattr->ia_valid |= ATTR_MTIME;
2299 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2302 struct nfs_open_context *ctx)
2304 struct nfs4_state_owner *sp = opendata->owner;
2305 struct nfs_server *server = sp->so_server;
2306 struct dentry *dentry;
2307 struct nfs4_state *state;
2311 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2313 ret = _nfs4_proc_open(opendata);
2317 state = nfs4_opendata_to_nfs4_state(opendata);
2318 ret = PTR_ERR(state);
2321 if (server->caps & NFS_CAP_POSIX_LOCK)
2322 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2324 dentry = opendata->dentry;
2325 if (d_really_is_negative(dentry)) {
2326 /* FIXME: Is this d_drop() ever needed? */
2328 dentry = d_add_unique(dentry, igrab(state->inode));
2329 if (dentry == NULL) {
2330 dentry = opendata->dentry;
2331 } else if (dentry != ctx->dentry) {
2333 ctx->dentry = dget(dentry);
2335 nfs_set_verifier(dentry,
2336 nfs_save_change_attribute(d_inode(opendata->dir)));
2339 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2344 if (d_inode(dentry) == state->inode) {
2345 nfs_inode_attach_open_context(ctx);
2346 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2347 nfs4_schedule_stateid_recovery(server, state);
2354 * Returns a referenced nfs4_state
2356 static int _nfs4_do_open(struct inode *dir,
2357 struct nfs_open_context *ctx,
2359 struct iattr *sattr,
2360 struct nfs4_label *label,
2363 struct nfs4_state_owner *sp;
2364 struct nfs4_state *state = NULL;
2365 struct nfs_server *server = NFS_SERVER(dir);
2366 struct nfs4_opendata *opendata;
2367 struct dentry *dentry = ctx->dentry;
2368 struct rpc_cred *cred = ctx->cred;
2369 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2370 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
2371 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
2372 struct nfs4_label *olabel = NULL;
2375 /* Protect against reboot recovery conflicts */
2377 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2379 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2382 status = nfs4_recover_expired_lease(server);
2384 goto err_put_state_owner;
2385 if (d_really_is_positive(dentry))
2386 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
2388 if (d_really_is_positive(dentry))
2389 claim = NFS4_OPEN_CLAIM_FH;
2390 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
2391 label, claim, GFP_KERNEL);
2392 if (opendata == NULL)
2393 goto err_put_state_owner;
2396 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2397 if (IS_ERR(olabel)) {
2398 status = PTR_ERR(olabel);
2399 goto err_opendata_put;
2403 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2404 if (!opendata->f_attr.mdsthreshold) {
2405 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2406 if (!opendata->f_attr.mdsthreshold)
2407 goto err_free_label;
2409 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
2411 if (d_really_is_positive(dentry))
2412 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
2414 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
2416 goto err_free_label;
2419 if ((opendata->o_arg.open_flags & O_EXCL) &&
2420 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
2421 nfs4_exclusive_attrset(opendata, sattr);
2423 nfs_fattr_init(opendata->o_res.f_attr);
2424 status = nfs4_do_setattr(state->inode, cred,
2425 opendata->o_res.f_attr, sattr,
2426 state, label, olabel);
2428 nfs_setattr_update_inode(state->inode, sattr,
2429 opendata->o_res.f_attr);
2430 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2433 if (opendata->file_created)
2434 *opened |= FILE_CREATED;
2436 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
2437 *ctx_th = opendata->f_attr.mdsthreshold;
2438 opendata->f_attr.mdsthreshold = NULL;
2441 nfs4_label_free(olabel);
2443 nfs4_opendata_put(opendata);
2444 nfs4_put_state_owner(sp);
2447 nfs4_label_free(olabel);
2449 nfs4_opendata_put(opendata);
2450 err_put_state_owner:
2451 nfs4_put_state_owner(sp);
2457 static struct nfs4_state *nfs4_do_open(struct inode *dir,
2458 struct nfs_open_context *ctx,
2460 struct iattr *sattr,
2461 struct nfs4_label *label,
2464 struct nfs_server *server = NFS_SERVER(dir);
2465 struct nfs4_exception exception = { };
2466 struct nfs4_state *res;
2470 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
2472 trace_nfs4_open_file(ctx, flags, status);
2475 /* NOTE: BAD_SEQID means the server and client disagree about the
2476 * book-keeping w.r.t. state-changing operations
2477 * (OPEN/CLOSE/LOCK/LOCKU...)
2478 * It is actually a sign of a bug on the client or on the server.
2480 * If we receive a BAD_SEQID error in the particular case of
2481 * doing an OPEN, we assume that nfs_increment_open_seqid() will
2482 * have unhashed the old state_owner for us, and that we can
2483 * therefore safely retry using a new one. We should still warn
2484 * the user though...
2486 if (status == -NFS4ERR_BAD_SEQID) {
2487 pr_warn_ratelimited("NFS: v4 server %s "
2488 " returned a bad sequence-id error!\n",
2489 NFS_SERVER(dir)->nfs_client->cl_hostname);
2490 exception.retry = 1;
2494 * BAD_STATEID on OPEN means that the server cancelled our
2495 * state before it received the OPEN_CONFIRM.
2496 * Recover by retrying the request as per the discussion
2497 * on Page 181 of RFC3530.
2499 if (status == -NFS4ERR_BAD_STATEID) {
2500 exception.retry = 1;
2503 if (status == -EAGAIN) {
2504 /* We must have found a delegation */
2505 exception.retry = 1;
2508 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2510 res = ERR_PTR(nfs4_handle_exception(server,
2511 status, &exception));
2512 } while (exception.retry);
2516 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2517 struct nfs_fattr *fattr, struct iattr *sattr,
2518 struct nfs4_state *state, struct nfs4_label *ilabel,
2519 struct nfs4_label *olabel)
2521 struct nfs_server *server = NFS_SERVER(inode);
2522 struct nfs_setattrargs arg = {
2523 .fh = NFS_FH(inode),
2526 .bitmask = server->attr_bitmask,
2529 struct nfs_setattrres res = {
2534 struct rpc_message msg = {
2535 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2540 unsigned long timestamp = jiffies;
2545 arg.bitmask = nfs4_bitmask(server, ilabel);
2547 arg.bitmask = nfs4_bitmask(server, olabel);
2549 nfs_fattr_init(fattr);
2551 /* Servers should only apply open mode checks for file size changes */
2552 truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false;
2553 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2555 if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) {
2556 /* Use that stateid */
2557 } else if (truncate && state != NULL) {
2558 struct nfs_lockowner lockowner = {
2559 .l_owner = current->files,
2560 .l_pid = current->tgid,
2562 if (!nfs4_valid_open_stateid(state))
2564 if (nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
2565 &lockowner) == -EIO)
2568 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
2570 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2571 if (status == 0 && state != NULL)
2572 renew_lease(server, timestamp);
2576 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2577 struct nfs_fattr *fattr, struct iattr *sattr,
2578 struct nfs4_state *state, struct nfs4_label *ilabel,
2579 struct nfs4_label *olabel)
2581 struct nfs_server *server = NFS_SERVER(inode);
2582 struct nfs4_exception exception = {
2588 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state, ilabel, olabel);
2589 trace_nfs4_setattr(inode, err);
2591 case -NFS4ERR_OPENMODE:
2592 if (!(sattr->ia_valid & ATTR_SIZE)) {
2593 pr_warn_once("NFSv4: server %s is incorrectly "
2594 "applying open mode checks to "
2595 "a SETATTR that is not "
2596 "changing file size.\n",
2597 server->nfs_client->cl_hostname);
2599 if (state && !(state->state & FMODE_WRITE)) {
2601 if (sattr->ia_valid & ATTR_OPEN)
2606 err = nfs4_handle_exception(server, err, &exception);
2607 } while (exception.retry);
2612 struct nfs4_closedata {
2613 struct inode *inode;
2614 struct nfs4_state *state;
2615 struct nfs_closeargs arg;
2616 struct nfs_closeres res;
2617 struct nfs_fattr fattr;
2618 unsigned long timestamp;
2623 static void nfs4_free_closedata(void *data)
2625 struct nfs4_closedata *calldata = data;
2626 struct nfs4_state_owner *sp = calldata->state->owner;
2627 struct super_block *sb = calldata->state->inode->i_sb;
2630 pnfs_roc_release(calldata->state->inode);
2631 nfs4_put_open_state(calldata->state);
2632 nfs_free_seqid(calldata->arg.seqid);
2633 nfs4_put_state_owner(sp);
2634 nfs_sb_deactive(sb);
2638 static void nfs4_close_done(struct rpc_task *task, void *data)
2640 struct nfs4_closedata *calldata = data;
2641 struct nfs4_state *state = calldata->state;
2642 struct nfs_server *server = NFS_SERVER(calldata->inode);
2643 nfs4_stateid *res_stateid = NULL;
2645 dprintk("%s: begin!\n", __func__);
2646 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2648 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
2649 /* hmm. we are done with the inode, and in the process of freeing
2650 * the state_owner. we keep this around to process errors
2652 switch (task->tk_status) {
2654 res_stateid = &calldata->res.stateid;
2655 if (calldata->arg.fmode == 0 && calldata->roc)
2656 pnfs_roc_set_barrier(state->inode,
2657 calldata->roc_barrier);
2658 renew_lease(server, calldata->timestamp);
2660 case -NFS4ERR_ADMIN_REVOKED:
2661 case -NFS4ERR_STALE_STATEID:
2662 case -NFS4ERR_OLD_STATEID:
2663 case -NFS4ERR_BAD_STATEID:
2664 case -NFS4ERR_EXPIRED:
2665 if (!nfs4_stateid_match(&calldata->arg.stateid,
2666 &state->open_stateid)) {
2667 rpc_restart_call_prepare(task);
2670 if (calldata->arg.fmode == 0)
2673 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
2674 rpc_restart_call_prepare(task);
2678 nfs_clear_open_stateid(state, res_stateid, calldata->arg.fmode);
2680 nfs_release_seqid(calldata->arg.seqid);
2681 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
2682 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
2685 static void nfs4_close_prepare(struct rpc_task *task, void *data)
2687 struct nfs4_closedata *calldata = data;
2688 struct nfs4_state *state = calldata->state;
2689 struct inode *inode = calldata->inode;
2690 bool is_rdonly, is_wronly, is_rdwr;
2693 dprintk("%s: begin!\n", __func__);
2694 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
2697 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2698 spin_lock(&state->owner->so_lock);
2699 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
2700 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
2701 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
2702 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
2703 /* Calculate the change in open mode */
2704 calldata->arg.fmode = 0;
2705 if (state->n_rdwr == 0) {
2706 if (state->n_rdonly == 0)
2707 call_close |= is_rdonly;
2709 calldata->arg.fmode |= FMODE_READ;
2710 if (state->n_wronly == 0)
2711 call_close |= is_wronly;
2713 calldata->arg.fmode |= FMODE_WRITE;
2715 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
2717 if (calldata->arg.fmode == 0)
2718 call_close |= is_rdwr;
2720 if (!nfs4_valid_open_stateid(state))
2722 spin_unlock(&state->owner->so_lock);
2725 /* Note: exit _without_ calling nfs4_close_done */
2729 if (calldata->arg.fmode == 0) {
2730 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
2731 if (calldata->roc &&
2732 pnfs_roc_drain(inode, &calldata->roc_barrier, task)) {
2733 nfs_release_seqid(calldata->arg.seqid);
2737 calldata->arg.share_access =
2738 nfs4_map_atomic_open_share(NFS_SERVER(inode),
2739 calldata->arg.fmode, 0);
2741 nfs_fattr_init(calldata->res.fattr);
2742 calldata->timestamp = jiffies;
2743 if (nfs4_setup_sequence(NFS_SERVER(inode),
2744 &calldata->arg.seq_args,
2745 &calldata->res.seq_res,
2747 nfs_release_seqid(calldata->arg.seqid);
2748 dprintk("%s: done!\n", __func__);
2751 task->tk_action = NULL;
2753 nfs4_sequence_done(task, &calldata->res.seq_res);
2756 static const struct rpc_call_ops nfs4_close_ops = {
2757 .rpc_call_prepare = nfs4_close_prepare,
2758 .rpc_call_done = nfs4_close_done,
2759 .rpc_release = nfs4_free_closedata,
2762 static bool nfs4_roc(struct inode *inode)
2764 if (!nfs_have_layout(inode))
2766 return pnfs_roc(inode);
2770 * It is possible for data to be read/written from a mem-mapped file
2771 * after the sys_close call (which hits the vfs layer as a flush).
2772 * This means that we can't safely call nfsv4 close on a file until
2773 * the inode is cleared. This in turn means that we are not good
2774 * NFSv4 citizens - we do not indicate to the server to update the file's
2775 * share state even when we are done with one of the three share
2776 * stateid's in the inode.
2778 * NOTE: Caller must be holding the sp->so_owner semaphore!
2780 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
2782 struct nfs_server *server = NFS_SERVER(state->inode);
2783 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
2784 struct nfs4_closedata *calldata;
2785 struct nfs4_state_owner *sp = state->owner;
2786 struct rpc_task *task;
2787 struct rpc_message msg = {
2788 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2789 .rpc_cred = state->owner->so_cred,
2791 struct rpc_task_setup task_setup_data = {
2792 .rpc_client = server->client,
2793 .rpc_message = &msg,
2794 .callback_ops = &nfs4_close_ops,
2795 .workqueue = nfsiod_workqueue,
2796 .flags = RPC_TASK_ASYNC,
2798 int status = -ENOMEM;
2800 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
2801 &task_setup_data.rpc_client, &msg);
2803 calldata = kzalloc(sizeof(*calldata), gfp_mask);
2804 if (calldata == NULL)
2806 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
2807 calldata->inode = state->inode;
2808 calldata->state = state;
2809 calldata->arg.fh = NFS_FH(state->inode);
2810 /* Serialization for the sequence id */
2811 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
2812 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
2813 if (IS_ERR(calldata->arg.seqid))
2814 goto out_free_calldata;
2815 calldata->arg.fmode = 0;
2816 calldata->arg.bitmask = server->cache_consistency_bitmask;
2817 calldata->res.fattr = &calldata->fattr;
2818 calldata->res.seqid = calldata->arg.seqid;
2819 calldata->res.server = server;
2820 calldata->roc = nfs4_roc(state->inode);
2821 nfs_sb_active(calldata->inode->i_sb);
2823 msg.rpc_argp = &calldata->arg;
2824 msg.rpc_resp = &calldata->res;
2825 task_setup_data.callback_data = calldata;
2826 task = rpc_run_task(&task_setup_data);
2828 return PTR_ERR(task);
2831 status = rpc_wait_for_completion_task(task);
2837 nfs4_put_open_state(state);
2838 nfs4_put_state_owner(sp);
2842 static struct inode *
2843 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
2844 int open_flags, struct iattr *attr, int *opened)
2846 struct nfs4_state *state;
2847 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
2849 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
2851 /* Protect against concurrent sillydeletes */
2852 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
2854 nfs4_label_release_security(label);
2857 return ERR_CAST(state);
2858 return state->inode;
2861 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2863 if (ctx->state == NULL)
2866 nfs4_close_sync(ctx->state, ctx->mode);
2868 nfs4_close_state(ctx->state, ctx->mode);
2871 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
2872 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
2873 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
2875 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2877 struct nfs4_server_caps_arg args = {
2880 struct nfs4_server_caps_res res = {};
2881 struct rpc_message msg = {
2882 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2888 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2890 /* Sanity check the server answers */
2891 switch (server->nfs_client->cl_minorversion) {
2893 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
2894 res.attr_bitmask[2] = 0;
2897 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
2900 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
2902 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2903 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2904 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2905 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2906 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2907 NFS_CAP_CTIME|NFS_CAP_MTIME|
2908 NFS_CAP_SECURITY_LABEL);
2909 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
2910 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
2911 server->caps |= NFS_CAP_ACLS;
2912 if (res.has_links != 0)
2913 server->caps |= NFS_CAP_HARDLINKS;
2914 if (res.has_symlinks != 0)
2915 server->caps |= NFS_CAP_SYMLINKS;
2916 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2917 server->caps |= NFS_CAP_FILEID;
2918 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2919 server->caps |= NFS_CAP_MODE;
2920 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2921 server->caps |= NFS_CAP_NLINK;
2922 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2923 server->caps |= NFS_CAP_OWNER;
2924 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2925 server->caps |= NFS_CAP_OWNER_GROUP;
2926 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2927 server->caps |= NFS_CAP_ATIME;
2928 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2929 server->caps |= NFS_CAP_CTIME;
2930 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2931 server->caps |= NFS_CAP_MTIME;
2932 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
2933 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
2934 server->caps |= NFS_CAP_SECURITY_LABEL;
2936 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
2937 sizeof(server->attr_bitmask));
2938 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2940 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2941 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2942 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2943 server->cache_consistency_bitmask[2] = 0;
2944 server->acl_bitmask = res.acl_bitmask;
2945 server->fh_expire_type = res.fh_expire_type;
2951 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2953 struct nfs4_exception exception = { };
2956 err = nfs4_handle_exception(server,
2957 _nfs4_server_capabilities(server, fhandle),
2959 } while (exception.retry);
2963 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2964 struct nfs_fsinfo *info)
2967 struct nfs4_lookup_root_arg args = {
2970 struct nfs4_lookup_res res = {
2972 .fattr = info->fattr,
2975 struct rpc_message msg = {
2976 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2981 bitmask[0] = nfs4_fattr_bitmap[0];
2982 bitmask[1] = nfs4_fattr_bitmap[1];
2984 * Process the label in the upcoming getfattr
2986 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
2988 nfs_fattr_init(info->fattr);
2989 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2992 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2993 struct nfs_fsinfo *info)
2995 struct nfs4_exception exception = { };
2998 err = _nfs4_lookup_root(server, fhandle, info);
2999 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
3002 case -NFS4ERR_WRONGSEC:
3005 err = nfs4_handle_exception(server, err, &exception);
3007 } while (exception.retry);
3012 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3013 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3015 struct rpc_auth_create_args auth_args = {
3016 .pseudoflavor = flavor,
3018 struct rpc_auth *auth;
3021 auth = rpcauth_create(&auth_args, server->client);
3026 ret = nfs4_lookup_root(server, fhandle, info);
3032 * Retry pseudoroot lookup with various security flavors. We do this when:
3034 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3035 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3037 * Returns zero on success, or a negative NFS4ERR value, or a
3038 * negative errno value.
3040 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3041 struct nfs_fsinfo *info)
3043 /* Per 3530bis 15.33.5 */
3044 static const rpc_authflavor_t flav_array[] = {
3048 RPC_AUTH_UNIX, /* courtesy */
3051 int status = -EPERM;
3054 if (server->auth_info.flavor_len > 0) {
3055 /* try each flavor specified by user */
3056 for (i = 0; i < server->auth_info.flavor_len; i++) {
3057 status = nfs4_lookup_root_sec(server, fhandle, info,
3058 server->auth_info.flavors[i]);
3059 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3064 /* no flavors specified by user, try default list */
3065 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3066 status = nfs4_lookup_root_sec(server, fhandle, info,
3068 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3075 * -EACCESS could mean that the user doesn't have correct permissions
3076 * to access the mount. It could also mean that we tried to mount
3077 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3078 * existing mount programs don't handle -EACCES very well so it should
3079 * be mapped to -EPERM instead.
3081 if (status == -EACCES)
3086 static int nfs4_do_find_root_sec(struct nfs_server *server,
3087 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
3089 int mv = server->nfs_client->cl_minorversion;
3090 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
3094 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3095 * @server: initialized nfs_server handle
3096 * @fhandle: we fill in the pseudo-fs root file handle
3097 * @info: we fill in an FSINFO struct
3098 * @auth_probe: probe the auth flavours
3100 * Returns zero on success, or a negative errno.
3102 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
3103 struct nfs_fsinfo *info,
3109 status = nfs4_lookup_root(server, fhandle, info);
3111 if (auth_probe || status == NFS4ERR_WRONGSEC)
3112 status = nfs4_do_find_root_sec(server, fhandle, info);
3115 status = nfs4_server_capabilities(server, fhandle);
3117 status = nfs4_do_fsinfo(server, fhandle, info);
3119 return nfs4_map_errors(status);
3122 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3123 struct nfs_fsinfo *info)
3126 struct nfs_fattr *fattr = info->fattr;
3127 struct nfs4_label *label = NULL;
3129 error = nfs4_server_capabilities(server, mntfh);
3131 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3135 label = nfs4_label_alloc(server, GFP_KERNEL);
3137 return PTR_ERR(label);
3139 error = nfs4_proc_getattr(server, mntfh, fattr, label);
3141 dprintk("nfs4_get_root: getattr error = %d\n", -error);
3142 goto err_free_label;
3145 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3146 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3147 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3150 nfs4_label_free(label);
3156 * Get locations and (maybe) other attributes of a referral.
3157 * Note that we'll actually follow the referral later when
3158 * we detect fsid mismatch in inode revalidation
3160 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3161 const struct qstr *name, struct nfs_fattr *fattr,
3162 struct nfs_fh *fhandle)
3164 int status = -ENOMEM;
3165 struct page *page = NULL;
3166 struct nfs4_fs_locations *locations = NULL;
3168 page = alloc_page(GFP_KERNEL);
3171 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3172 if (locations == NULL)
3175 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
3180 * If the fsid didn't change, this is a migration event, not a
3181 * referral. Cause us to drop into the exception handler, which
3182 * will kick off migration recovery.
3184 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
3185 dprintk("%s: server did not return a different fsid for"
3186 " a referral at %s\n", __func__, name->name);
3187 status = -NFS4ERR_MOVED;
3190 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3191 nfs_fixup_referral_attributes(&locations->fattr);
3193 /* replace the lookup nfs_fattr with the locations nfs_fattr */
3194 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
3195 memset(fhandle, 0, sizeof(struct nfs_fh));
3203 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3204 struct nfs_fattr *fattr, struct nfs4_label *label)
3206 struct nfs4_getattr_arg args = {
3208 .bitmask = server->attr_bitmask,
3210 struct nfs4_getattr_res res = {
3215 struct rpc_message msg = {
3216 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3221 args.bitmask = nfs4_bitmask(server, label);
3223 nfs_fattr_init(fattr);
3224 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3227 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3228 struct nfs_fattr *fattr, struct nfs4_label *label)
3230 struct nfs4_exception exception = { };
3233 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3234 trace_nfs4_getattr(server, fhandle, fattr, err);
3235 err = nfs4_handle_exception(server, err,
3237 } while (exception.retry);
3242 * The file is not closed if it is opened due to the a request to change
3243 * the size of the file. The open call will not be needed once the
3244 * VFS layer lookup-intents are implemented.
3246 * Close is called when the inode is destroyed.
3247 * If we haven't opened the file for O_WRONLY, we
3248 * need to in the size_change case to obtain a stateid.
3251 * Because OPEN is always done by name in nfsv4, it is
3252 * possible that we opened a different file by the same
3253 * name. We can recognize this race condition, but we
3254 * can't do anything about it besides returning an error.
3256 * This will be fixed with VFS changes (lookup-intent).
3259 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3260 struct iattr *sattr)
3262 struct inode *inode = d_inode(dentry);
3263 struct rpc_cred *cred = NULL;
3264 struct nfs4_state *state = NULL;
3265 struct nfs4_label *label = NULL;
3268 if (pnfs_ld_layoutret_on_setattr(inode) &&
3269 sattr->ia_valid & ATTR_SIZE &&
3270 sattr->ia_size < i_size_read(inode))
3271 pnfs_commit_and_return_layout(inode);
3273 nfs_fattr_init(fattr);
3275 /* Deal with open(O_TRUNC) */
3276 if (sattr->ia_valid & ATTR_OPEN)
3277 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
3279 /* Optimization: if the end result is no change, don't RPC */
3280 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
3283 /* Search for an existing open(O_WRITE) file */
3284 if (sattr->ia_valid & ATTR_FILE) {
3285 struct nfs_open_context *ctx;
3287 ctx = nfs_file_open_context(sattr->ia_file);
3294 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3296 return PTR_ERR(label);
3298 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
3300 nfs_setattr_update_inode(inode, sattr, fattr);
3301 nfs_setsecurity(inode, fattr, label);
3303 nfs4_label_free(label);
3307 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3308 const struct qstr *name, struct nfs_fh *fhandle,
3309 struct nfs_fattr *fattr, struct nfs4_label *label)
3311 struct nfs_server *server = NFS_SERVER(dir);
3313 struct nfs4_lookup_arg args = {
3314 .bitmask = server->attr_bitmask,
3315 .dir_fh = NFS_FH(dir),
3318 struct nfs4_lookup_res res = {
3324 struct rpc_message msg = {
3325 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3330 args.bitmask = nfs4_bitmask(server, label);
3332 nfs_fattr_init(fattr);
3334 dprintk("NFS call lookup %s\n", name->name);
3335 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
3336 dprintk("NFS reply lookup: %d\n", status);
3340 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
3342 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
3343 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
3344 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3348 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3349 struct qstr *name, struct nfs_fh *fhandle,
3350 struct nfs_fattr *fattr, struct nfs4_label *label)
3352 struct nfs4_exception exception = { };
3353 struct rpc_clnt *client = *clnt;
3356 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
3357 trace_nfs4_lookup(dir, name, err);
3359 case -NFS4ERR_BADNAME:
3362 case -NFS4ERR_MOVED:
3363 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
3364 if (err == -NFS4ERR_MOVED)
3365 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3367 case -NFS4ERR_WRONGSEC:
3369 if (client != *clnt)
3371 client = nfs4_negotiate_security(client, dir, name);
3373 return PTR_ERR(client);
3375 exception.retry = 1;
3378 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3380 } while (exception.retry);
3385 else if (client != *clnt)
3386 rpc_shutdown_client(client);
3391 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
3392 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3393 struct nfs4_label *label)
3396 struct rpc_clnt *client = NFS_CLIENT(dir);
3398 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
3399 if (client != NFS_CLIENT(dir)) {
3400 rpc_shutdown_client(client);
3401 nfs_fixup_secinfo_attributes(fattr);
3407 nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
3408 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3410 struct rpc_clnt *client = NFS_CLIENT(dir);
3413 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
3415 return ERR_PTR(status);
3416 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
3419 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3421 struct nfs_server *server = NFS_SERVER(inode);
3422 struct nfs4_accessargs args = {
3423 .fh = NFS_FH(inode),
3424 .bitmask = server->cache_consistency_bitmask,
3426 struct nfs4_accessres res = {
3429 struct rpc_message msg = {
3430 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3433 .rpc_cred = entry->cred,
3435 int mode = entry->mask;
3439 * Determine which access bits we want to ask for...
3441 if (mode & MAY_READ)
3442 args.access |= NFS4_ACCESS_READ;
3443 if (S_ISDIR(inode->i_mode)) {
3444 if (mode & MAY_WRITE)
3445 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3446 if (mode & MAY_EXEC)
3447 args.access |= NFS4_ACCESS_LOOKUP;
3449 if (mode & MAY_WRITE)
3450 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3451 if (mode & MAY_EXEC)
3452 args.access |= NFS4_ACCESS_EXECUTE;
3455 res.fattr = nfs_alloc_fattr();
3456 if (res.fattr == NULL)
3459 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3461 nfs_access_set_mask(entry, res.access);
3462 nfs_refresh_inode(inode, res.fattr);
3464 nfs_free_fattr(res.fattr);
3468 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3470 struct nfs4_exception exception = { };
3473 err = _nfs4_proc_access(inode, entry);
3474 trace_nfs4_access(inode, err);
3475 err = nfs4_handle_exception(NFS_SERVER(inode), err,
3477 } while (exception.retry);
3482 * TODO: For the time being, we don't try to get any attributes
3483 * along with any of the zero-copy operations READ, READDIR,
3486 * In the case of the first three, we want to put the GETATTR
3487 * after the read-type operation -- this is because it is hard
3488 * to predict the length of a GETATTR response in v4, and thus
3489 * align the READ data correctly. This means that the GETATTR
3490 * may end up partially falling into the page cache, and we should
3491 * shift it into the 'tail' of the xdr_buf before processing.
3492 * To do this efficiently, we need to know the total length
3493 * of data received, which doesn't seem to be available outside
3496 * In the case of WRITE, we also want to put the GETATTR after
3497 * the operation -- in this case because we want to make sure
3498 * we get the post-operation mtime and size.
3500 * Both of these changes to the XDR layer would in fact be quite
3501 * minor, but I decided to leave them for a subsequent patch.
3503 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3504 unsigned int pgbase, unsigned int pglen)
3506 struct nfs4_readlink args = {
3507 .fh = NFS_FH(inode),
3512 struct nfs4_readlink_res res;
3513 struct rpc_message msg = {
3514 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3519 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
3522 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3523 unsigned int pgbase, unsigned int pglen)
3525 struct nfs4_exception exception = { };
3528 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3529 trace_nfs4_readlink(inode, err);
3530 err = nfs4_handle_exception(NFS_SERVER(inode), err,
3532 } while (exception.retry);
3537 * This is just for mknod. open(O_CREAT) will always do ->open_context().
3540 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
3543 struct nfs4_label l, *ilabel = NULL;
3544 struct nfs_open_context *ctx;
3545 struct nfs4_state *state;
3549 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3551 return PTR_ERR(ctx);
3553 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3555 sattr->ia_mode &= ~current_umask();
3556 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, &opened);
3557 if (IS_ERR(state)) {
3558 status = PTR_ERR(state);
3562 nfs4_label_release_security(ilabel);
3563 put_nfs_open_context(ctx);
3567 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3569 struct nfs_server *server = NFS_SERVER(dir);
3570 struct nfs_removeargs args = {
3574 struct nfs_removeres res = {
3577 struct rpc_message msg = {
3578 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3584 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
3586 update_changeattr(dir, &res.cinfo);
3590 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3592 struct nfs4_exception exception = { };
3595 err = _nfs4_proc_remove(dir, name);
3596 trace_nfs4_remove(dir, name, err);
3597 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3599 } while (exception.retry);
3603 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
3605 struct nfs_server *server = NFS_SERVER(dir);
3606 struct nfs_removeargs *args = msg->rpc_argp;
3607 struct nfs_removeres *res = msg->rpc_resp;
3609 res->server = server;
3610 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
3611 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
3613 nfs_fattr_init(res->dir_attr);
3616 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3618 nfs4_setup_sequence(NFS_SERVER(data->dir),
3619 &data->args.seq_args,
3624 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
3626 struct nfs_unlinkdata *data = task->tk_calldata;
3627 struct nfs_removeres *res = &data->res;
3629 if (!nfs4_sequence_done(task, &res->seq_res))
3631 if (nfs4_async_handle_error(task, res->server, NULL,
3632 &data->timeout) == -EAGAIN)
3634 update_changeattr(dir, &res->cinfo);
3638 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3640 struct nfs_server *server = NFS_SERVER(dir);
3641 struct nfs_renameargs *arg = msg->rpc_argp;
3642 struct nfs_renameres *res = msg->rpc_resp;
3644 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
3645 res->server = server;
3646 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
3649 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3651 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3652 &data->args.seq_args,
3657 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3658 struct inode *new_dir)
3660 struct nfs_renamedata *data = task->tk_calldata;
3661 struct nfs_renameres *res = &data->res;
3663 if (!nfs4_sequence_done(task, &res->seq_res))
3665 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
3668 update_changeattr(old_dir, &res->old_cinfo);
3669 update_changeattr(new_dir, &res->new_cinfo);
3673 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3675 struct nfs_server *server = NFS_SERVER(inode);
3676 struct nfs4_link_arg arg = {
3677 .fh = NFS_FH(inode),
3678 .dir_fh = NFS_FH(dir),
3680 .bitmask = server->attr_bitmask,
3682 struct nfs4_link_res res = {
3686 struct rpc_message msg = {
3687 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3691 int status = -ENOMEM;
3693 res.fattr = nfs_alloc_fattr();
3694 if (res.fattr == NULL)
3697 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3698 if (IS_ERR(res.label)) {
3699 status = PTR_ERR(res.label);
3702 arg.bitmask = nfs4_bitmask(server, res.label);
3704 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3706 update_changeattr(dir, &res.cinfo);
3707 status = nfs_post_op_update_inode(inode, res.fattr);
3709 nfs_setsecurity(inode, res.fattr, res.label);
3713 nfs4_label_free(res.label);
3716 nfs_free_fattr(res.fattr);
3720 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3722 struct nfs4_exception exception = { };
3725 err = nfs4_handle_exception(NFS_SERVER(inode),
3726 _nfs4_proc_link(inode, dir, name),
3728 } while (exception.retry);
3732 struct nfs4_createdata {
3733 struct rpc_message msg;
3734 struct nfs4_create_arg arg;
3735 struct nfs4_create_res res;
3737 struct nfs_fattr fattr;
3738 struct nfs4_label *label;
3741 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3742 struct qstr *name, struct iattr *sattr, u32 ftype)
3744 struct nfs4_createdata *data;
3746 data = kzalloc(sizeof(*data), GFP_KERNEL);
3748 struct nfs_server *server = NFS_SERVER(dir);
3750 data->label = nfs4_label_alloc(server, GFP_KERNEL);
3751 if (IS_ERR(data->label))
3754 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3755 data->msg.rpc_argp = &data->arg;
3756 data->msg.rpc_resp = &data->res;
3757 data->arg.dir_fh = NFS_FH(dir);
3758 data->arg.server = server;
3759 data->arg.name = name;
3760 data->arg.attrs = sattr;
3761 data->arg.ftype = ftype;
3762 data->arg.bitmask = nfs4_bitmask(server, data->label);
3763 data->res.server = server;
3764 data->res.fh = &data->fh;
3765 data->res.fattr = &data->fattr;
3766 data->res.label = data->label;
3767 nfs_fattr_init(data->res.fattr);
3775 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3777 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
3778 &data->arg.seq_args, &data->res.seq_res, 1);
3780 update_changeattr(dir, &data->res.dir_cinfo);
3781 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
3786 static void nfs4_free_createdata(struct nfs4_createdata *data)
3788 nfs4_label_free(data->label);
3792 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3793 struct page *page, unsigned int len, struct iattr *sattr,
3794 struct nfs4_label *label)
3796 struct nfs4_createdata *data;
3797 int status = -ENAMETOOLONG;
3799 if (len > NFS4_MAXPATHLEN)
3803 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3807 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3808 data->arg.u.symlink.pages = &page;
3809 data->arg.u.symlink.len = len;
3810 data->arg.label = label;
3812 status = nfs4_do_create(dir, dentry, data);
3814 nfs4_free_createdata(data);
3819 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3820 struct page *page, unsigned int len, struct iattr *sattr)
3822 struct nfs4_exception exception = { };
3823 struct nfs4_label l, *label = NULL;
3826 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3829 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
3830 trace_nfs4_symlink(dir, &dentry->d_name, err);
3831 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3833 } while (exception.retry);
3835 nfs4_label_release_security(label);
3839 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3840 struct iattr *sattr, struct nfs4_label *label)
3842 struct nfs4_createdata *data;
3843 int status = -ENOMEM;
3845 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3849 data->arg.label = label;
3850 status = nfs4_do_create(dir, dentry, data);
3852 nfs4_free_createdata(data);
3857 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3858 struct iattr *sattr)
3860 struct nfs4_exception exception = { };
3861 struct nfs4_label l, *label = NULL;
3864 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3866 sattr->ia_mode &= ~current_umask();
3868 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
3869 trace_nfs4_mkdir(dir, &dentry->d_name, err);
3870 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3872 } while (exception.retry);
3873 nfs4_label_release_security(label);
3878 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3879 u64 cookie, struct page **pages, unsigned int count, int plus)
3881 struct inode *dir = d_inode(dentry);
3882 struct nfs4_readdir_arg args = {
3887 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
3890 struct nfs4_readdir_res res;
3891 struct rpc_message msg = {
3892 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3899 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
3901 (unsigned long long)cookie);
3902 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
3903 res.pgbase = args.pgbase;
3904 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
3906 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
3907 status += args.pgbase;
3910 nfs_invalidate_atime(dir);
3912 dprintk("%s: returns %d\n", __func__, status);
3916 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3917 u64 cookie, struct page **pages, unsigned int count, int plus)
3919 struct nfs4_exception exception = { };
3922 err = _nfs4_proc_readdir(dentry, cred, cookie,
3923 pages, count, plus);
3924 trace_nfs4_readdir(d_inode(dentry), err);
3925 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
3927 } while (exception.retry);
3931 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3932 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
3934 struct nfs4_createdata *data;
3935 int mode = sattr->ia_mode;
3936 int status = -ENOMEM;
3938 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3943 data->arg.ftype = NF4FIFO;
3944 else if (S_ISBLK(mode)) {
3945 data->arg.ftype = NF4BLK;
3946 data->arg.u.device.specdata1 = MAJOR(rdev);
3947 data->arg.u.device.specdata2 = MINOR(rdev);
3949 else if (S_ISCHR(mode)) {
3950 data->arg.ftype = NF4CHR;
3951 data->arg.u.device.specdata1 = MAJOR(rdev);
3952 data->arg.u.device.specdata2 = MINOR(rdev);
3953 } else if (!S_ISSOCK(mode)) {
3958 data->arg.label = label;
3959 status = nfs4_do_create(dir, dentry, data);
3961 nfs4_free_createdata(data);
3966 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3967 struct iattr *sattr, dev_t rdev)
3969 struct nfs4_exception exception = { };
3970 struct nfs4_label l, *label = NULL;
3973 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3975 sattr->ia_mode &= ~current_umask();
3977 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
3978 trace_nfs4_mknod(dir, &dentry->d_name, err);
3979 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3981 } while (exception.retry);
3983 nfs4_label_release_security(label);
3988 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3989 struct nfs_fsstat *fsstat)
3991 struct nfs4_statfs_arg args = {
3993 .bitmask = server->attr_bitmask,
3995 struct nfs4_statfs_res res = {
3998 struct rpc_message msg = {
3999 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4004 nfs_fattr_init(fsstat->fattr);
4005 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4008 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4010 struct nfs4_exception exception = { };
4013 err = nfs4_handle_exception(server,
4014 _nfs4_proc_statfs(server, fhandle, fsstat),
4016 } while (exception.retry);
4020 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4021 struct nfs_fsinfo *fsinfo)
4023 struct nfs4_fsinfo_arg args = {
4025 .bitmask = server->attr_bitmask,
4027 struct nfs4_fsinfo_res res = {
4030 struct rpc_message msg = {
4031 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4036 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4039 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4041 struct nfs4_exception exception = { };
4042 unsigned long now = jiffies;
4046 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
4047 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
4049 struct nfs_client *clp = server->nfs_client;
4051 spin_lock(&clp->cl_lock);
4052 clp->cl_lease_time = fsinfo->lease_time * HZ;
4053 clp->cl_last_renewal = now;
4054 spin_unlock(&clp->cl_lock);
4057 err = nfs4_handle_exception(server, err, &exception);
4058 } while (exception.retry);
4062 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4066 nfs_fattr_init(fsinfo->fattr);
4067 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
4069 /* block layout checks this! */
4070 server->pnfs_blksize = fsinfo->blksize;
4071 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
4077 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4078 struct nfs_pathconf *pathconf)
4080 struct nfs4_pathconf_arg args = {
4082 .bitmask = server->attr_bitmask,
4084 struct nfs4_pathconf_res res = {
4085 .pathconf = pathconf,
4087 struct rpc_message msg = {
4088 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4093 /* None of the pathconf attributes are mandatory to implement */
4094 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4095 memset(pathconf, 0, sizeof(*pathconf));
4099 nfs_fattr_init(pathconf->fattr);
4100 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4103 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4104 struct nfs_pathconf *pathconf)
4106 struct nfs4_exception exception = { };
4110 err = nfs4_handle_exception(server,
4111 _nfs4_proc_pathconf(server, fhandle, pathconf),
4113 } while (exception.retry);
4117 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
4118 const struct nfs_open_context *ctx,
4119 const struct nfs_lock_context *l_ctx,
4122 const struct nfs_lockowner *lockowner = NULL;
4125 lockowner = &l_ctx->lockowner;
4126 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
4128 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4130 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4131 const struct nfs_open_context *ctx,
4132 const struct nfs_lock_context *l_ctx,
4135 nfs4_stateid current_stateid;
4137 /* If the current stateid represents a lost lock, then exit */
4138 if (nfs4_set_rw_stateid(¤t_stateid, ctx, l_ctx, fmode) == -EIO)
4140 return nfs4_stateid_match(stateid, ¤t_stateid);
4143 static bool nfs4_error_stateid_expired(int err)
4146 case -NFS4ERR_DELEG_REVOKED:
4147 case -NFS4ERR_ADMIN_REVOKED:
4148 case -NFS4ERR_BAD_STATEID:
4149 case -NFS4ERR_STALE_STATEID:
4150 case -NFS4ERR_OLD_STATEID:
4151 case -NFS4ERR_OPENMODE:
4152 case -NFS4ERR_EXPIRED:
4158 void __nfs4_read_done_cb(struct nfs_pgio_header *hdr)
4160 nfs_invalidate_atime(hdr->inode);
4163 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
4165 struct nfs_server *server = NFS_SERVER(hdr->inode);
4167 trace_nfs4_read(hdr, task->tk_status);
4168 if (nfs4_async_handle_error(task, server,
4169 hdr->args.context->state,
4171 rpc_restart_call_prepare(task);
4175 __nfs4_read_done_cb(hdr);
4176 if (task->tk_status > 0)
4177 renew_lease(server, hdr->timestamp);
4181 static bool nfs4_read_stateid_changed(struct rpc_task *task,
4182 struct nfs_pgio_args *args)
4185 if (!nfs4_error_stateid_expired(task->tk_status) ||
4186 nfs4_stateid_is_current(&args->stateid,
4191 rpc_restart_call_prepare(task);
4195 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
4198 dprintk("--> %s\n", __func__);
4200 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
4202 if (nfs4_read_stateid_changed(task, &hdr->args))
4204 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4205 nfs4_read_done_cb(task, hdr);
4208 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4209 struct rpc_message *msg)
4211 hdr->timestamp = jiffies;
4212 hdr->pgio_done_cb = nfs4_read_done_cb;
4213 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
4214 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
4217 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4218 struct nfs_pgio_header *hdr)
4220 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4221 &hdr->args.seq_args,
4225 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4226 hdr->args.lock_context,
4227 hdr->rw_ops->rw_mode) == -EIO)
4229 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
4234 static int nfs4_write_done_cb(struct rpc_task *task,
4235 struct nfs_pgio_header *hdr)
4237 struct inode *inode = hdr->inode;
4239 trace_nfs4_write(hdr, task->tk_status);
4240 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4241 hdr->args.context->state,
4243 rpc_restart_call_prepare(task);
4246 if (task->tk_status >= 0) {
4247 renew_lease(NFS_SERVER(inode), hdr->timestamp);
4248 nfs_writeback_update_inode(hdr);
4253 static bool nfs4_write_stateid_changed(struct rpc_task *task,
4254 struct nfs_pgio_args *args)
4257 if (!nfs4_error_stateid_expired(task->tk_status) ||
4258 nfs4_stateid_is_current(&args->stateid,
4263 rpc_restart_call_prepare(task);
4267 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
4269 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
4271 if (nfs4_write_stateid_changed(task, &hdr->args))
4273 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4274 nfs4_write_done_cb(task, hdr);
4278 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
4280 /* Don't request attributes for pNFS or O_DIRECT writes */
4281 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
4283 /* Otherwise, request attributes if and only if we don't hold
4286 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
4289 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4290 struct rpc_message *msg)
4292 struct nfs_server *server = NFS_SERVER(hdr->inode);
4294 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4295 hdr->args.bitmask = NULL;
4296 hdr->res.fattr = NULL;
4298 hdr->args.bitmask = server->cache_consistency_bitmask;
4300 if (!hdr->pgio_done_cb)
4301 hdr->pgio_done_cb = nfs4_write_done_cb;
4302 hdr->res.server = server;
4303 hdr->timestamp = jiffies;
4305 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
4306 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
4309 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4311 nfs4_setup_sequence(NFS_SERVER(data->inode),
4312 &data->args.seq_args,
4317 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
4319 struct inode *inode = data->inode;
4321 trace_nfs4_commit(data, task->tk_status);
4322 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4323 NULL, NULL) == -EAGAIN) {
4324 rpc_restart_call_prepare(task);
4330 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
4332 if (!nfs4_sequence_done(task, &data->res.seq_res))
4334 return data->commit_done_cb(task, data);
4337 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
4339 struct nfs_server *server = NFS_SERVER(data->inode);
4341 if (data->commit_done_cb == NULL)
4342 data->commit_done_cb = nfs4_commit_done_cb;
4343 data->res.server = server;
4344 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
4345 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4348 struct nfs4_renewdata {
4349 struct nfs_client *client;
4350 unsigned long timestamp;
4354 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4355 * standalone procedure for queueing an asynchronous RENEW.
4357 static void nfs4_renew_release(void *calldata)
4359 struct nfs4_renewdata *data = calldata;
4360 struct nfs_client *clp = data->client;
4362 if (atomic_read(&clp->cl_count) > 1)
4363 nfs4_schedule_state_renewal(clp);
4364 nfs_put_client(clp);
4368 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
4370 struct nfs4_renewdata *data = calldata;
4371 struct nfs_client *clp = data->client;
4372 unsigned long timestamp = data->timestamp;
4374 trace_nfs4_renew_async(clp, task->tk_status);
4375 switch (task->tk_status) {
4378 case -NFS4ERR_LEASE_MOVED:
4379 nfs4_schedule_lease_moved_recovery(clp);
4382 /* Unless we're shutting down, schedule state recovery! */
4383 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4385 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
4386 nfs4_schedule_lease_recovery(clp);
4389 nfs4_schedule_path_down_recovery(clp);
4391 do_renew_lease(clp, timestamp);
4394 static const struct rpc_call_ops nfs4_renew_ops = {
4395 .rpc_call_done = nfs4_renew_done,
4396 .rpc_release = nfs4_renew_release,
4399 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
4401 struct rpc_message msg = {
4402 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4406 struct nfs4_renewdata *data;
4408 if (renew_flags == 0)
4410 if (!atomic_inc_not_zero(&clp->cl_count))
4412 data = kmalloc(sizeof(*data), GFP_NOFS);
4416 data->timestamp = jiffies;
4417 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
4418 &nfs4_renew_ops, data);
4421 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
4423 struct rpc_message msg = {
4424 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4428 unsigned long now = jiffies;
4431 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4434 do_renew_lease(clp, now);
4438 static inline int nfs4_server_supports_acls(struct nfs_server *server)
4440 return server->caps & NFS_CAP_ACLS;
4443 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4444 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
4447 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
4449 static int buf_to_pages_noslab(const void *buf, size_t buflen,
4450 struct page **pages, unsigned int *pgbase)
4452 struct page *newpage, **spages;
4458 len = min_t(size_t, PAGE_SIZE, buflen);
4459 newpage = alloc_page(GFP_KERNEL);
4461 if (newpage == NULL)
4463 memcpy(page_address(newpage), buf, len);
4468 } while (buflen != 0);
4474 __free_page(spages[rc-1]);
4478 struct nfs4_cached_acl {
4484 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
4486 struct nfs_inode *nfsi = NFS_I(inode);
4488 spin_lock(&inode->i_lock);
4489 kfree(nfsi->nfs4_acl);
4490 nfsi->nfs4_acl = acl;
4491 spin_unlock(&inode->i_lock);
4494 static void nfs4_zap_acl_attr(struct inode *inode)
4496 nfs4_set_cached_acl(inode, NULL);
4499 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4501 struct nfs_inode *nfsi = NFS_I(inode);
4502 struct nfs4_cached_acl *acl;
4505 spin_lock(&inode->i_lock);
4506 acl = nfsi->nfs4_acl;
4509 if (buf == NULL) /* user is just asking for length */
4511 if (acl->cached == 0)
4513 ret = -ERANGE; /* see getxattr(2) man page */
4514 if (acl->len > buflen)
4516 memcpy(buf, acl->data, acl->len);
4520 spin_unlock(&inode->i_lock);
4524 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
4526 struct nfs4_cached_acl *acl;
4527 size_t buflen = sizeof(*acl) + acl_len;
4529 if (buflen <= PAGE_SIZE) {
4530 acl = kmalloc(buflen, GFP_KERNEL);
4534 _copy_from_pages(acl->data, pages, pgbase, acl_len);
4536 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4543 nfs4_set_cached_acl(inode, acl);
4547 * The getxattr API returns the required buffer length when called with a
4548 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4549 * the required buf. On a NULL buf, we send a page of data to the server
4550 * guessing that the ACL request can be serviced by a page. If so, we cache
4551 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4552 * the cache. If not so, we throw away the page, and cache the required
4553 * length. The next getxattr call will then produce another round trip to
4554 * the server, this time with the input buf of the required size.
4556 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4558 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
4559 struct nfs_getaclargs args = {
4560 .fh = NFS_FH(inode),
4564 struct nfs_getaclres res = {
4567 struct rpc_message msg = {
4568 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4572 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4573 int ret = -ENOMEM, i;
4575 /* As long as we're doing a round trip to the server anyway,
4576 * let's be prepared for a page of acl data. */
4579 if (npages > ARRAY_SIZE(pages))
4582 for (i = 0; i < npages; i++) {
4583 pages[i] = alloc_page(GFP_KERNEL);
4588 /* for decoding across pages */
4589 res.acl_scratch = alloc_page(GFP_KERNEL);
4590 if (!res.acl_scratch)
4593 args.acl_len = npages * PAGE_SIZE;
4594 args.acl_pgbase = 0;
4596 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
4597 __func__, buf, buflen, npages, args.acl_len);
4598 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4599 &msg, &args.seq_args, &res.seq_res, 0);
4603 /* Handle the case where the passed-in buffer is too short */
4604 if (res.acl_flags & NFS4_ACL_TRUNC) {
4605 /* Did the user only issue a request for the acl length? */
4611 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
4613 if (res.acl_len > buflen) {
4617 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
4622 for (i = 0; i < npages; i++)
4624 __free_page(pages[i]);
4625 if (res.acl_scratch)
4626 __free_page(res.acl_scratch);
4630 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4632 struct nfs4_exception exception = { };
4635 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
4636 trace_nfs4_get_acl(inode, ret);
4639 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4640 } while (exception.retry);
4644 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4646 struct nfs_server *server = NFS_SERVER(inode);
4649 if (!nfs4_server_supports_acls(server))
4651 ret = nfs_revalidate_inode(server, inode);
4654 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4655 nfs_zap_acl_cache(inode);
4656 ret = nfs4_read_cached_acl(inode, buf, buflen);
4658 /* -ENOENT is returned if there is no ACL or if there is an ACL
4659 * but no cached acl data, just the acl length */
4661 return nfs4_get_acl_uncached(inode, buf, buflen);
4664 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4666 struct nfs_server *server = NFS_SERVER(inode);
4667 struct page *pages[NFS4ACL_MAXPAGES];
4668 struct nfs_setaclargs arg = {
4669 .fh = NFS_FH(inode),
4673 struct nfs_setaclres res;
4674 struct rpc_message msg = {
4675 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4679 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4682 if (!nfs4_server_supports_acls(server))
4684 if (npages > ARRAY_SIZE(pages))
4686 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4689 nfs4_inode_return_delegation(inode);
4690 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4693 * Free each page after tx, so the only ref left is
4694 * held by the network stack
4697 put_page(pages[i-1]);
4700 * Acl update can result in inode attribute update.
4701 * so mark the attribute cache invalid.
4703 spin_lock(&inode->i_lock);
4704 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4705 spin_unlock(&inode->i_lock);
4706 nfs_access_zap_cache(inode);
4707 nfs_zap_acl_cache(inode);
4711 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4713 struct nfs4_exception exception = { };
4716 err = __nfs4_proc_set_acl(inode, buf, buflen);
4717 trace_nfs4_set_acl(inode, err);
4718 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4720 } while (exception.retry);
4724 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
4725 static int _nfs4_get_security_label(struct inode *inode, void *buf,
4728 struct nfs_server *server = NFS_SERVER(inode);
4729 struct nfs_fattr fattr;
4730 struct nfs4_label label = {0, 0, buflen, buf};
4732 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4733 struct nfs4_getattr_arg arg = {
4734 .fh = NFS_FH(inode),
4737 struct nfs4_getattr_res res = {
4742 struct rpc_message msg = {
4743 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4749 nfs_fattr_init(&fattr);
4751 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
4754 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
4756 if (buflen < label.len)
4761 static int nfs4_get_security_label(struct inode *inode, void *buf,
4764 struct nfs4_exception exception = { };
4767 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4771 err = _nfs4_get_security_label(inode, buf, buflen);
4772 trace_nfs4_get_security_label(inode, err);
4773 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4775 } while (exception.retry);
4779 static int _nfs4_do_set_security_label(struct inode *inode,
4780 struct nfs4_label *ilabel,
4781 struct nfs_fattr *fattr,
4782 struct nfs4_label *olabel)
4785 struct iattr sattr = {0};
4786 struct nfs_server *server = NFS_SERVER(inode);
4787 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4788 struct nfs_setattrargs arg = {
4789 .fh = NFS_FH(inode),
4795 struct nfs_setattrres res = {
4800 struct rpc_message msg = {
4801 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
4807 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
4809 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4811 dprintk("%s failed: %d\n", __func__, status);
4816 static int nfs4_do_set_security_label(struct inode *inode,
4817 struct nfs4_label *ilabel,
4818 struct nfs_fattr *fattr,
4819 struct nfs4_label *olabel)
4821 struct nfs4_exception exception = { };
4825 err = _nfs4_do_set_security_label(inode, ilabel,
4827 trace_nfs4_set_security_label(inode, err);
4828 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4830 } while (exception.retry);
4835 nfs4_set_security_label(struct dentry *dentry, const void *buf, size_t buflen)
4837 struct nfs4_label ilabel, *olabel = NULL;
4838 struct nfs_fattr fattr;
4839 struct rpc_cred *cred;
4840 struct inode *inode = d_inode(dentry);
4843 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4846 nfs_fattr_init(&fattr);
4850 ilabel.label = (char *)buf;
4851 ilabel.len = buflen;
4853 cred = rpc_lookup_cred();
4855 return PTR_ERR(cred);
4857 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4858 if (IS_ERR(olabel)) {
4859 status = -PTR_ERR(olabel);
4863 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
4865 nfs_setsecurity(inode, &fattr, olabel);
4867 nfs4_label_free(olabel);
4872 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
4876 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server,
4877 struct nfs4_state *state, long *timeout)
4879 struct nfs_client *clp = server->nfs_client;
4881 if (task->tk_status >= 0)
4883 switch(task->tk_status) {
4884 case -NFS4ERR_DELEG_REVOKED:
4885 case -NFS4ERR_ADMIN_REVOKED:
4886 case -NFS4ERR_BAD_STATEID:
4887 case -NFS4ERR_OPENMODE:
4890 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4891 goto recovery_failed;
4892 goto wait_on_recovery;
4893 case -NFS4ERR_EXPIRED:
4894 if (state != NULL) {
4895 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4896 goto recovery_failed;
4898 case -NFS4ERR_STALE_STATEID:
4899 case -NFS4ERR_STALE_CLIENTID:
4900 nfs4_schedule_lease_recovery(clp);
4901 goto wait_on_recovery;
4902 case -NFS4ERR_MOVED:
4903 if (nfs4_schedule_migration_recovery(server) < 0)
4904 goto recovery_failed;
4905 goto wait_on_recovery;
4906 case -NFS4ERR_LEASE_MOVED:
4907 nfs4_schedule_lease_moved_recovery(clp);
4908 goto wait_on_recovery;
4909 #if defined(CONFIG_NFS_V4_1)
4910 case -NFS4ERR_BADSESSION:
4911 case -NFS4ERR_BADSLOT:
4912 case -NFS4ERR_BAD_HIGH_SLOT:
4913 case -NFS4ERR_DEADSESSION:
4914 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4915 case -NFS4ERR_SEQ_FALSE_RETRY:
4916 case -NFS4ERR_SEQ_MISORDERED:
4917 dprintk("%s ERROR %d, Reset session\n", __func__,
4919 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
4920 goto wait_on_recovery;
4921 #endif /* CONFIG_NFS_V4_1 */
4922 case -NFS4ERR_DELAY:
4923 nfs_inc_server_stats(server, NFSIOS_DELAY);
4924 rpc_delay(task, nfs4_update_delay(timeout));
4926 case -NFS4ERR_GRACE:
4927 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4928 case -NFS4ERR_RETRY_UNCACHED_REP:
4929 case -NFS4ERR_OLD_STATEID:
4932 task->tk_status = nfs4_map_errors(task->tk_status);
4935 task->tk_status = -EIO;
4938 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
4939 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4940 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4941 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
4942 goto recovery_failed;
4944 task->tk_status = 0;
4948 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4949 nfs4_verifier *bootverf)
4953 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4954 /* An impossible timestamp guarantees this value
4955 * will never match a generated boot time. */
4957 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
4959 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4960 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
4961 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
4963 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4967 nfs4_init_nonuniform_client_string(struct nfs_client *clp)
4972 bool retried = false;
4974 if (clp->cl_owner_id != NULL)
4978 len = 10 + strlen(clp->cl_ipaddr) + 1 +
4979 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
4981 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
4985 if (len > NFS4_OPAQUE_LIMIT + 1)
4989 * Since this string is allocated at mount time, and held until the
4990 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
4991 * about a memory-reclaim deadlock.
4993 str = kmalloc(len, GFP_KERNEL);
4998 result = scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
5000 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5001 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
5004 /* Did something change? */
5005 if (result >= len) {
5012 clp->cl_owner_id = str;
5017 nfs4_init_uniquifier_client_string(struct nfs_client *clp)
5023 len = 10 + 10 + 1 + 10 + 1 +
5024 strlen(nfs4_client_id_uniquifier) + 1 +
5025 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5027 if (len > NFS4_OPAQUE_LIMIT + 1)
5031 * Since this string is allocated at mount time, and held until the
5032 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5033 * about a memory-reclaim deadlock.
5035 str = kmalloc(len, GFP_KERNEL);
5039 result = scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
5040 clp->rpc_ops->version, clp->cl_minorversion,
5041 nfs4_client_id_uniquifier,
5042 clp->cl_rpcclient->cl_nodename);
5043 if (result >= len) {
5047 clp->cl_owner_id = str;
5052 nfs4_init_uniform_client_string(struct nfs_client *clp)
5058 if (clp->cl_owner_id != NULL)
5061 if (nfs4_client_id_uniquifier[0] != '\0')
5062 return nfs4_init_uniquifier_client_string(clp);
5064 len = 10 + 10 + 1 + 10 + 1 +
5065 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5067 if (len > NFS4_OPAQUE_LIMIT + 1)
5071 * Since this string is allocated at mount time, and held until the
5072 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5073 * about a memory-reclaim deadlock.
5075 str = kmalloc(len, GFP_KERNEL);
5079 result = scnprintf(str, len, "Linux NFSv%u.%u %s",
5080 clp->rpc_ops->version, clp->cl_minorversion,
5081 clp->cl_rpcclient->cl_nodename);
5082 if (result >= len) {
5086 clp->cl_owner_id = str;
5091 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5092 * services. Advertise one based on the address family of the
5096 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5098 if (strchr(clp->cl_ipaddr, ':') != NULL)
5099 return scnprintf(buf, len, "tcp6");
5101 return scnprintf(buf, len, "tcp");
5104 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5106 struct nfs4_setclientid *sc = calldata;
5108 if (task->tk_status == 0)
5109 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5112 static const struct rpc_call_ops nfs4_setclientid_ops = {
5113 .rpc_call_done = nfs4_setclientid_done,
5117 * nfs4_proc_setclientid - Negotiate client ID
5118 * @clp: state data structure
5119 * @program: RPC program for NFSv4 callback service
5120 * @port: IP port number for NFS4 callback service
5121 * @cred: RPC credential to use for this call
5122 * @res: where to place the result
5124 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5126 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5127 unsigned short port, struct rpc_cred *cred,
5128 struct nfs4_setclientid_res *res)
5130 nfs4_verifier sc_verifier;
5131 struct nfs4_setclientid setclientid = {
5132 .sc_verifier = &sc_verifier,
5136 struct rpc_message msg = {
5137 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5138 .rpc_argp = &setclientid,
5142 struct rpc_task *task;
5143 struct rpc_task_setup task_setup_data = {
5144 .rpc_client = clp->cl_rpcclient,
5145 .rpc_message = &msg,
5146 .callback_ops = &nfs4_setclientid_ops,
5147 .callback_data = &setclientid,
5148 .flags = RPC_TASK_TIMEOUT,
5152 /* nfs_client_id4 */
5153 nfs4_init_boot_verifier(clp, &sc_verifier);
5155 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5156 status = nfs4_init_uniform_client_string(clp);
5158 status = nfs4_init_nonuniform_client_string(clp);
5164 setclientid.sc_netid_len =
5165 nfs4_init_callback_netid(clp,
5166 setclientid.sc_netid,
5167 sizeof(setclientid.sc_netid));
5168 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
5169 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
5170 clp->cl_ipaddr, port >> 8, port & 255);
5172 dprintk("NFS call setclientid auth=%s, '%s'\n",
5173 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5175 task = rpc_run_task(&task_setup_data);
5177 status = PTR_ERR(task);
5180 status = task->tk_status;
5181 if (setclientid.sc_cred) {
5182 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5183 put_rpccred(setclientid.sc_cred);
5187 trace_nfs4_setclientid(clp, status);
5188 dprintk("NFS reply setclientid: %d\n", status);
5193 * nfs4_proc_setclientid_confirm - Confirm client ID
5194 * @clp: state data structure
5195 * @res: result of a previous SETCLIENTID
5196 * @cred: RPC credential to use for this call
5198 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5200 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
5201 struct nfs4_setclientid_res *arg,
5202 struct rpc_cred *cred)
5204 struct rpc_message msg = {
5205 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
5211 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5212 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5214 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5215 trace_nfs4_setclientid_confirm(clp, status);
5216 dprintk("NFS reply setclientid_confirm: %d\n", status);
5220 struct nfs4_delegreturndata {
5221 struct nfs4_delegreturnargs args;
5222 struct nfs4_delegreturnres res;
5224 nfs4_stateid stateid;
5225 unsigned long timestamp;
5226 struct nfs_fattr fattr;
5228 struct inode *inode;
5233 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5235 struct nfs4_delegreturndata *data = calldata;
5237 if (!nfs4_sequence_done(task, &data->res.seq_res))
5240 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
5241 switch (task->tk_status) {
5243 renew_lease(data->res.server, data->timestamp);
5244 case -NFS4ERR_ADMIN_REVOKED:
5245 case -NFS4ERR_DELEG_REVOKED:
5246 case -NFS4ERR_BAD_STATEID:
5247 case -NFS4ERR_OLD_STATEID:
5248 case -NFS4ERR_STALE_STATEID:
5249 case -NFS4ERR_EXPIRED:
5250 task->tk_status = 0;
5252 pnfs_roc_set_barrier(data->inode, data->roc_barrier);
5255 if (nfs4_async_handle_error(task, data->res.server,
5256 NULL, NULL) == -EAGAIN) {
5257 rpc_restart_call_prepare(task);
5261 data->rpc_status = task->tk_status;
5264 static void nfs4_delegreturn_release(void *calldata)
5266 struct nfs4_delegreturndata *data = calldata;
5267 struct inode *inode = data->inode;
5271 pnfs_roc_release(inode);
5272 nfs_iput_and_deactive(inode);
5277 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5279 struct nfs4_delegreturndata *d_data;
5281 d_data = (struct nfs4_delegreturndata *)data;
5284 pnfs_roc_drain(d_data->inode, &d_data->roc_barrier, task))
5287 nfs4_setup_sequence(d_data->res.server,
5288 &d_data->args.seq_args,
5289 &d_data->res.seq_res,
5293 static const struct rpc_call_ops nfs4_delegreturn_ops = {
5294 .rpc_call_prepare = nfs4_delegreturn_prepare,
5295 .rpc_call_done = nfs4_delegreturn_done,
5296 .rpc_release = nfs4_delegreturn_release,
5299 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
5301 struct nfs4_delegreturndata *data;
5302 struct nfs_server *server = NFS_SERVER(inode);
5303 struct rpc_task *task;
5304 struct rpc_message msg = {
5305 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5308 struct rpc_task_setup task_setup_data = {
5309 .rpc_client = server->client,
5310 .rpc_message = &msg,
5311 .callback_ops = &nfs4_delegreturn_ops,
5312 .flags = RPC_TASK_ASYNC,
5316 data = kzalloc(sizeof(*data), GFP_NOFS);
5319 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
5320 data->args.fhandle = &data->fh;
5321 data->args.stateid = &data->stateid;
5322 data->args.bitmask = server->cache_consistency_bitmask;
5323 nfs_copy_fh(&data->fh, NFS_FH(inode));
5324 nfs4_stateid_copy(&data->stateid, stateid);
5325 data->res.fattr = &data->fattr;
5326 data->res.server = server;
5327 nfs_fattr_init(data->res.fattr);
5328 data->timestamp = jiffies;
5329 data->rpc_status = 0;
5330 data->inode = nfs_igrab_and_active(inode);
5332 data->roc = nfs4_roc(inode);
5334 task_setup_data.callback_data = data;
5335 msg.rpc_argp = &data->args;
5336 msg.rpc_resp = &data->res;
5337 task = rpc_run_task(&task_setup_data);
5339 return PTR_ERR(task);
5342 status = nfs4_wait_for_completion_rpc_task(task);
5345 status = data->rpc_status;
5347 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5349 nfs_refresh_inode(inode, &data->fattr);
5355 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
5357 struct nfs_server *server = NFS_SERVER(inode);
5358 struct nfs4_exception exception = { };
5361 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
5362 trace_nfs4_delegreturn(inode, err);
5364 case -NFS4ERR_STALE_STATEID:
5365 case -NFS4ERR_EXPIRED:
5369 err = nfs4_handle_exception(server, err, &exception);
5370 } while (exception.retry);
5374 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
5375 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
5378 * sleep, with exponential backoff, and retry the LOCK operation.
5380 static unsigned long
5381 nfs4_set_lock_task_retry(unsigned long timeout)
5383 freezable_schedule_timeout_killable_unsafe(timeout);
5385 if (timeout > NFS4_LOCK_MAXTIMEOUT)
5386 return NFS4_LOCK_MAXTIMEOUT;
5390 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5392 struct inode *inode = state->inode;
5393 struct nfs_server *server = NFS_SERVER(inode);
5394 struct nfs_client *clp = server->nfs_client;
5395 struct nfs_lockt_args arg = {
5396 .fh = NFS_FH(inode),
5399 struct nfs_lockt_res res = {
5402 struct rpc_message msg = {
5403 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5406 .rpc_cred = state->owner->so_cred,
5408 struct nfs4_lock_state *lsp;
5411 arg.lock_owner.clientid = clp->cl_clientid;
5412 status = nfs4_set_lock_state(state, request);
5415 lsp = request->fl_u.nfs4_fl.owner;
5416 arg.lock_owner.id = lsp->ls_seqid.owner_id;
5417 arg.lock_owner.s_dev = server->s_dev;
5418 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5421 request->fl_type = F_UNLCK;
5423 case -NFS4ERR_DENIED:
5426 request->fl_ops->fl_release_private(request);
5427 request->fl_ops = NULL;
5432 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5434 struct nfs4_exception exception = { };
5438 err = _nfs4_proc_getlk(state, cmd, request);
5439 trace_nfs4_get_lock(request, state, cmd, err);
5440 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
5442 } while (exception.retry);
5446 static int do_vfs_lock(struct file *file, struct file_lock *fl)
5449 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
5451 res = posix_lock_file_wait(file, fl);
5454 res = flock_lock_file_wait(file, fl);
5462 struct nfs4_unlockdata {
5463 struct nfs_locku_args arg;
5464 struct nfs_locku_res res;
5465 struct nfs4_lock_state *lsp;
5466 struct nfs_open_context *ctx;
5467 struct file_lock fl;
5468 const struct nfs_server *server;
5469 unsigned long timestamp;
5472 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5473 struct nfs_open_context *ctx,
5474 struct nfs4_lock_state *lsp,
5475 struct nfs_seqid *seqid)
5477 struct nfs4_unlockdata *p;
5478 struct inode *inode = lsp->ls_state->inode;
5480 p = kzalloc(sizeof(*p), GFP_NOFS);
5483 p->arg.fh = NFS_FH(inode);
5485 p->arg.seqid = seqid;
5486 p->res.seqid = seqid;
5488 atomic_inc(&lsp->ls_count);
5489 /* Ensure we don't close file until we're done freeing locks! */
5490 p->ctx = get_nfs_open_context(ctx);
5491 memcpy(&p->fl, fl, sizeof(p->fl));
5492 p->server = NFS_SERVER(inode);
5496 static void nfs4_locku_release_calldata(void *data)
5498 struct nfs4_unlockdata *calldata = data;
5499 nfs_free_seqid(calldata->arg.seqid);
5500 nfs4_put_lock_state(calldata->lsp);
5501 put_nfs_open_context(calldata->ctx);
5505 static void nfs4_locku_done(struct rpc_task *task, void *data)
5507 struct nfs4_unlockdata *calldata = data;
5509 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5511 switch (task->tk_status) {
5513 renew_lease(calldata->server, calldata->timestamp);
5514 do_vfs_lock(calldata->fl.fl_file, &calldata->fl);
5515 if (nfs4_update_lock_stateid(calldata->lsp,
5516 &calldata->res.stateid))
5518 case -NFS4ERR_BAD_STATEID:
5519 case -NFS4ERR_OLD_STATEID:
5520 case -NFS4ERR_STALE_STATEID:
5521 case -NFS4ERR_EXPIRED:
5522 if (!nfs4_stateid_match(&calldata->arg.stateid,
5523 &calldata->lsp->ls_stateid))
5524 rpc_restart_call_prepare(task);
5527 if (nfs4_async_handle_error(task, calldata->server,
5528 NULL, NULL) == -EAGAIN)
5529 rpc_restart_call_prepare(task);
5531 nfs_release_seqid(calldata->arg.seqid);
5534 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
5536 struct nfs4_unlockdata *calldata = data;
5538 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
5540 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
5541 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
5542 /* Note: exit _without_ running nfs4_locku_done */
5545 calldata->timestamp = jiffies;
5546 if (nfs4_setup_sequence(calldata->server,
5547 &calldata->arg.seq_args,
5548 &calldata->res.seq_res,
5550 nfs_release_seqid(calldata->arg.seqid);
5553 task->tk_action = NULL;
5555 nfs4_sequence_done(task, &calldata->res.seq_res);
5558 static const struct rpc_call_ops nfs4_locku_ops = {
5559 .rpc_call_prepare = nfs4_locku_prepare,
5560 .rpc_call_done = nfs4_locku_done,
5561 .rpc_release = nfs4_locku_release_calldata,
5564 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5565 struct nfs_open_context *ctx,
5566 struct nfs4_lock_state *lsp,
5567 struct nfs_seqid *seqid)
5569 struct nfs4_unlockdata *data;
5570 struct rpc_message msg = {
5571 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5572 .rpc_cred = ctx->cred,
5574 struct rpc_task_setup task_setup_data = {
5575 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
5576 .rpc_message = &msg,
5577 .callback_ops = &nfs4_locku_ops,
5578 .workqueue = nfsiod_workqueue,
5579 .flags = RPC_TASK_ASYNC,
5582 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5583 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5585 /* Ensure this is an unlock - when canceling a lock, the
5586 * canceled lock is passed in, and it won't be an unlock.
5588 fl->fl_type = F_UNLCK;
5590 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5592 nfs_free_seqid(seqid);
5593 return ERR_PTR(-ENOMEM);
5596 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
5597 msg.rpc_argp = &data->arg;
5598 msg.rpc_resp = &data->res;
5599 task_setup_data.callback_data = data;
5600 return rpc_run_task(&task_setup_data);
5603 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5605 struct inode *inode = state->inode;
5606 struct nfs4_state_owner *sp = state->owner;
5607 struct nfs_inode *nfsi = NFS_I(inode);
5608 struct nfs_seqid *seqid;
5609 struct nfs4_lock_state *lsp;
5610 struct rpc_task *task;
5611 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
5613 unsigned char fl_flags = request->fl_flags;
5615 status = nfs4_set_lock_state(state, request);
5616 /* Unlock _before_ we do the RPC call */
5617 request->fl_flags |= FL_EXISTS;
5618 /* Exclude nfs_delegation_claim_locks() */
5619 mutex_lock(&sp->so_delegreturn_mutex);
5620 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
5621 down_read(&nfsi->rwsem);
5622 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
5623 up_read(&nfsi->rwsem);
5624 mutex_unlock(&sp->so_delegreturn_mutex);
5627 up_read(&nfsi->rwsem);
5628 mutex_unlock(&sp->so_delegreturn_mutex);
5631 /* Is this a delegated lock? */
5632 lsp = request->fl_u.nfs4_fl.owner;
5633 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5635 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
5636 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
5640 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
5641 status = PTR_ERR(task);
5644 status = nfs4_wait_for_completion_rpc_task(task);
5647 request->fl_flags = fl_flags;
5648 trace_nfs4_unlock(request, state, F_SETLK, status);
5652 struct nfs4_lockdata {
5653 struct nfs_lock_args arg;
5654 struct nfs_lock_res res;
5655 struct nfs4_lock_state *lsp;
5656 struct nfs_open_context *ctx;
5657 struct file_lock fl;
5658 unsigned long timestamp;
5661 struct nfs_server *server;
5664 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
5665 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5668 struct nfs4_lockdata *p;
5669 struct inode *inode = lsp->ls_state->inode;
5670 struct nfs_server *server = NFS_SERVER(inode);
5671 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
5673 p = kzalloc(sizeof(*p), gfp_mask);
5677 p->arg.fh = NFS_FH(inode);
5679 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
5680 if (IS_ERR(p->arg.open_seqid))
5682 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
5683 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
5684 if (IS_ERR(p->arg.lock_seqid))
5685 goto out_free_seqid;
5686 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
5687 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
5688 p->arg.lock_owner.s_dev = server->s_dev;
5689 p->res.lock_seqid = p->arg.lock_seqid;
5692 atomic_inc(&lsp->ls_count);
5693 p->ctx = get_nfs_open_context(ctx);
5694 get_file(fl->fl_file);
5695 memcpy(&p->fl, fl, sizeof(p->fl));
5698 nfs_free_seqid(p->arg.open_seqid);
5704 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5706 struct nfs4_lockdata *data = calldata;
5707 struct nfs4_state *state = data->lsp->ls_state;
5709 dprintk("%s: begin!\n", __func__);
5710 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
5712 /* Do we need to do an open_to_lock_owner? */
5713 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
5714 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
5715 goto out_release_lock_seqid;
5717 nfs4_stateid_copy(&data->arg.open_stateid,
5718 &state->open_stateid);
5719 data->arg.new_lock_owner = 1;
5720 data->res.open_seqid = data->arg.open_seqid;
5722 data->arg.new_lock_owner = 0;
5723 nfs4_stateid_copy(&data->arg.lock_stateid,
5724 &data->lsp->ls_stateid);
5726 if (!nfs4_valid_open_stateid(state)) {
5727 data->rpc_status = -EBADF;
5728 task->tk_action = NULL;
5729 goto out_release_open_seqid;
5731 data->timestamp = jiffies;
5732 if (nfs4_setup_sequence(data->server,
5733 &data->arg.seq_args,
5737 out_release_open_seqid:
5738 nfs_release_seqid(data->arg.open_seqid);
5739 out_release_lock_seqid:
5740 nfs_release_seqid(data->arg.lock_seqid);
5742 nfs4_sequence_done(task, &data->res.seq_res);
5743 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
5746 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5748 struct nfs4_lockdata *data = calldata;
5749 struct nfs4_lock_state *lsp = data->lsp;
5751 dprintk("%s: begin!\n", __func__);
5753 if (!nfs4_sequence_done(task, &data->res.seq_res))
5756 data->rpc_status = task->tk_status;
5757 switch (task->tk_status) {
5759 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
5761 if (data->arg.new_lock) {
5762 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
5763 if (do_vfs_lock(data->fl.fl_file, &data->fl) < 0) {
5764 rpc_restart_call_prepare(task);
5768 if (data->arg.new_lock_owner != 0) {
5769 nfs_confirm_seqid(&lsp->ls_seqid, 0);
5770 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
5771 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
5772 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
5773 rpc_restart_call_prepare(task);
5775 case -NFS4ERR_BAD_STATEID:
5776 case -NFS4ERR_OLD_STATEID:
5777 case -NFS4ERR_STALE_STATEID:
5778 case -NFS4ERR_EXPIRED:
5779 if (data->arg.new_lock_owner != 0) {
5780 if (!nfs4_stateid_match(&data->arg.open_stateid,
5781 &lsp->ls_state->open_stateid))
5782 rpc_restart_call_prepare(task);
5783 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
5785 rpc_restart_call_prepare(task);
5787 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
5790 static void nfs4_lock_release(void *calldata)
5792 struct nfs4_lockdata *data = calldata;
5794 dprintk("%s: begin!\n", __func__);
5795 nfs_free_seqid(data->arg.open_seqid);
5796 if (data->cancelled != 0) {
5797 struct rpc_task *task;
5798 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5799 data->arg.lock_seqid);
5801 rpc_put_task_async(task);
5802 dprintk("%s: cancelling lock!\n", __func__);
5804 nfs_free_seqid(data->arg.lock_seqid);
5805 nfs4_put_lock_state(data->lsp);
5806 put_nfs_open_context(data->ctx);
5807 fput(data->fl.fl_file);
5809 dprintk("%s: done!\n", __func__);
5812 static const struct rpc_call_ops nfs4_lock_ops = {
5813 .rpc_call_prepare = nfs4_lock_prepare,
5814 .rpc_call_done = nfs4_lock_done,
5815 .rpc_release = nfs4_lock_release,
5818 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5821 case -NFS4ERR_ADMIN_REVOKED:
5822 case -NFS4ERR_BAD_STATEID:
5823 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
5824 if (new_lock_owner != 0 ||
5825 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
5826 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
5828 case -NFS4ERR_STALE_STATEID:
5829 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
5830 case -NFS4ERR_EXPIRED:
5831 nfs4_schedule_lease_recovery(server->nfs_client);
5835 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
5837 struct nfs4_lockdata *data;
5838 struct rpc_task *task;
5839 struct rpc_message msg = {
5840 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5841 .rpc_cred = state->owner->so_cred,
5843 struct rpc_task_setup task_setup_data = {
5844 .rpc_client = NFS_CLIENT(state->inode),
5845 .rpc_message = &msg,
5846 .callback_ops = &nfs4_lock_ops,
5847 .workqueue = nfsiod_workqueue,
5848 .flags = RPC_TASK_ASYNC,
5852 dprintk("%s: begin!\n", __func__);
5853 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
5854 fl->fl_u.nfs4_fl.owner,
5855 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
5859 data->arg.block = 1;
5860 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
5861 msg.rpc_argp = &data->arg;
5862 msg.rpc_resp = &data->res;
5863 task_setup_data.callback_data = data;
5864 if (recovery_type > NFS_LOCK_NEW) {
5865 if (recovery_type == NFS_LOCK_RECLAIM)
5866 data->arg.reclaim = NFS_LOCK_RECLAIM;
5867 nfs4_set_sequence_privileged(&data->arg.seq_args);
5869 data->arg.new_lock = 1;
5870 task = rpc_run_task(&task_setup_data);
5872 return PTR_ERR(task);
5873 ret = nfs4_wait_for_completion_rpc_task(task);
5875 ret = data->rpc_status;
5877 nfs4_handle_setlk_error(data->server, data->lsp,
5878 data->arg.new_lock_owner, ret);
5880 data->cancelled = 1;
5882 dprintk("%s: done, ret = %d!\n", __func__, ret);
5886 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
5888 struct nfs_server *server = NFS_SERVER(state->inode);
5889 struct nfs4_exception exception = {
5890 .inode = state->inode,
5895 /* Cache the lock if possible... */
5896 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5898 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
5899 trace_nfs4_lock_reclaim(request, state, F_SETLK, err);
5900 if (err != -NFS4ERR_DELAY)
5902 nfs4_handle_exception(server, err, &exception);
5903 } while (exception.retry);
5907 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
5909 struct nfs_server *server = NFS_SERVER(state->inode);
5910 struct nfs4_exception exception = {
5911 .inode = state->inode,
5915 err = nfs4_set_lock_state(state, request);
5918 if (!recover_lost_locks) {
5919 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
5923 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5925 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
5926 trace_nfs4_lock_expired(request, state, F_SETLK, err);
5930 case -NFS4ERR_GRACE:
5931 case -NFS4ERR_DELAY:
5932 nfs4_handle_exception(server, err, &exception);
5935 } while (exception.retry);
5940 #if defined(CONFIG_NFS_V4_1)
5942 * nfs41_check_expired_locks - possibly free a lock stateid
5944 * @state: NFSv4 state for an inode
5946 * Returns NFS_OK if recovery for this stateid is now finished.
5947 * Otherwise a negative NFS4ERR value is returned.
5949 static int nfs41_check_expired_locks(struct nfs4_state *state)
5951 int status, ret = -NFS4ERR_BAD_STATEID;
5952 struct nfs4_lock_state *lsp;
5953 struct nfs_server *server = NFS_SERVER(state->inode);
5955 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
5956 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
5957 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
5959 status = nfs41_test_stateid(server,
5962 trace_nfs4_test_lock_stateid(state, lsp, status);
5963 if (status != NFS_OK) {
5964 /* Free the stateid unless the server
5965 * informs us the stateid is unrecognized. */
5966 if (status != -NFS4ERR_BAD_STATEID)
5967 nfs41_free_stateid(server,
5970 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
5979 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5981 int status = NFS_OK;
5983 if (test_bit(LK_STATE_IN_USE, &state->flags))
5984 status = nfs41_check_expired_locks(state);
5985 if (status != NFS_OK)
5986 status = nfs4_lock_expired(state, request);
5991 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5993 struct nfs_inode *nfsi = NFS_I(state->inode);
5994 unsigned char fl_flags = request->fl_flags;
5995 int status = -ENOLCK;
5997 if ((fl_flags & FL_POSIX) &&
5998 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6000 /* Is this a delegated open? */
6001 status = nfs4_set_lock_state(state, request);
6004 request->fl_flags |= FL_ACCESS;
6005 status = do_vfs_lock(request->fl_file, request);
6008 down_read(&nfsi->rwsem);
6009 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
6010 /* Yes: cache locks! */
6011 /* ...but avoid races with delegation recall... */
6012 request->fl_flags = fl_flags & ~FL_SLEEP;
6013 status = do_vfs_lock(request->fl_file, request);
6014 up_read(&nfsi->rwsem);
6017 up_read(&nfsi->rwsem);
6018 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
6020 request->fl_flags = fl_flags;
6024 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6026 struct nfs4_exception exception = {
6028 .inode = state->inode,
6033 err = _nfs4_proc_setlk(state, cmd, request);
6034 trace_nfs4_set_lock(request, state, cmd, err);
6035 if (err == -NFS4ERR_DENIED)
6037 err = nfs4_handle_exception(NFS_SERVER(state->inode),
6039 } while (exception.retry);
6044 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6046 struct nfs_open_context *ctx;
6047 struct nfs4_state *state;
6048 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6051 /* verify open state */
6052 ctx = nfs_file_open_context(filp);
6055 if (request->fl_start < 0 || request->fl_end < 0)
6058 if (IS_GETLK(cmd)) {
6060 return nfs4_proc_getlk(state, F_GETLK, request);
6064 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6067 if (request->fl_type == F_UNLCK) {
6069 return nfs4_proc_unlck(state, cmd, request);
6076 * Don't rely on the VFS having checked the file open mode,
6077 * since it won't do this for flock() locks.
6079 switch (request->fl_type) {
6081 if (!(filp->f_mode & FMODE_READ))
6085 if (!(filp->f_mode & FMODE_WRITE))
6090 status = nfs4_proc_setlk(state, cmd, request);
6091 if ((status != -EAGAIN) || IS_SETLK(cmd))
6093 timeout = nfs4_set_lock_task_retry(timeout);
6094 status = -ERESTARTSYS;
6097 } while(status < 0);
6101 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
6103 struct nfs_server *server = NFS_SERVER(state->inode);
6106 err = nfs4_set_lock_state(state, fl);
6109 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
6110 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
6113 struct nfs_release_lockowner_data {
6114 struct nfs4_lock_state *lsp;
6115 struct nfs_server *server;
6116 struct nfs_release_lockowner_args args;
6117 struct nfs_release_lockowner_res res;
6118 unsigned long timestamp;
6121 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6123 struct nfs_release_lockowner_data *data = calldata;
6124 struct nfs_server *server = data->server;
6125 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
6126 &data->args.seq_args, &data->res.seq_res, task);
6127 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6128 data->timestamp = jiffies;
6131 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6133 struct nfs_release_lockowner_data *data = calldata;
6134 struct nfs_server *server = data->server;
6136 nfs40_sequence_done(task, &data->res.seq_res);
6138 switch (task->tk_status) {
6140 renew_lease(server, data->timestamp);
6142 case -NFS4ERR_STALE_CLIENTID:
6143 case -NFS4ERR_EXPIRED:
6144 nfs4_schedule_lease_recovery(server->nfs_client);
6146 case -NFS4ERR_LEASE_MOVED:
6147 case -NFS4ERR_DELAY:
6148 if (nfs4_async_handle_error(task, server,
6149 NULL, NULL) == -EAGAIN)
6150 rpc_restart_call_prepare(task);
6154 static void nfs4_release_lockowner_release(void *calldata)
6156 struct nfs_release_lockowner_data *data = calldata;
6157 nfs4_free_lock_state(data->server, data->lsp);
6161 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
6162 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6163 .rpc_call_done = nfs4_release_lockowner_done,
6164 .rpc_release = nfs4_release_lockowner_release,
6168 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
6170 struct nfs_release_lockowner_data *data;
6171 struct rpc_message msg = {
6172 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6175 if (server->nfs_client->cl_mvops->minor_version != 0)
6178 data = kmalloc(sizeof(*data), GFP_NOFS);
6182 data->server = server;
6183 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6184 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6185 data->args.lock_owner.s_dev = server->s_dev;
6187 msg.rpc_argp = &data->args;
6188 msg.rpc_resp = &data->res;
6189 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
6190 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
6193 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6195 static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
6196 const void *buf, size_t buflen,
6197 int flags, int type)
6199 if (strcmp(key, "") != 0)
6202 return nfs4_proc_set_acl(d_inode(dentry), buf, buflen);
6205 static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
6206 void *buf, size_t buflen, int type)
6208 if (strcmp(key, "") != 0)
6211 return nfs4_proc_get_acl(d_inode(dentry), buf, buflen);
6214 static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
6215 size_t list_len, const char *name,
6216 size_t name_len, int type)
6218 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
6220 if (!nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry))))
6223 if (list && len <= list_len)
6224 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
6228 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
6229 static inline int nfs4_server_supports_labels(struct nfs_server *server)
6231 return server->caps & NFS_CAP_SECURITY_LABEL;
6234 static int nfs4_xattr_set_nfs4_label(struct dentry *dentry, const char *key,
6235 const void *buf, size_t buflen,
6236 int flags, int type)
6238 if (security_ismaclabel(key))
6239 return nfs4_set_security_label(dentry, buf, buflen);
6244 static int nfs4_xattr_get_nfs4_label(struct dentry *dentry, const char *key,
6245 void *buf, size_t buflen, int type)
6247 if (security_ismaclabel(key))
6248 return nfs4_get_security_label(d_inode(dentry), buf, buflen);
6252 static size_t nfs4_xattr_list_nfs4_label(struct dentry *dentry, char *list,
6253 size_t list_len, const char *name,
6254 size_t name_len, int type)
6258 if (nfs_server_capable(d_inode(dentry), NFS_CAP_SECURITY_LABEL)) {
6259 len = security_inode_listsecurity(d_inode(dentry), NULL, 0);
6260 if (list && len <= list_len)
6261 security_inode_listsecurity(d_inode(dentry), list, len);
6266 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6267 .prefix = XATTR_SECURITY_PREFIX,
6268 .list = nfs4_xattr_list_nfs4_label,
6269 .get = nfs4_xattr_get_nfs4_label,
6270 .set = nfs4_xattr_set_nfs4_label,
6276 * nfs_fhget will use either the mounted_on_fileid or the fileid
6278 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6280 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6281 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6282 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
6283 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
6286 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
6287 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
6288 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6292 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6293 const struct qstr *name,
6294 struct nfs4_fs_locations *fs_locations,
6297 struct nfs_server *server = NFS_SERVER(dir);
6299 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6301 struct nfs4_fs_locations_arg args = {
6302 .dir_fh = NFS_FH(dir),
6307 struct nfs4_fs_locations_res res = {
6308 .fs_locations = fs_locations,
6310 struct rpc_message msg = {
6311 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6317 dprintk("%s: start\n", __func__);
6319 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6320 * is not supported */
6321 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6322 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6324 bitmask[0] |= FATTR4_WORD0_FILEID;
6326 nfs_fattr_init(&fs_locations->fattr);
6327 fs_locations->server = server;
6328 fs_locations->nlocations = 0;
6329 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
6330 dprintk("%s: returned status = %d\n", __func__, status);
6334 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6335 const struct qstr *name,
6336 struct nfs4_fs_locations *fs_locations,
6339 struct nfs4_exception exception = { };
6342 err = _nfs4_proc_fs_locations(client, dir, name,
6343 fs_locations, page);
6344 trace_nfs4_get_fs_locations(dir, name, err);
6345 err = nfs4_handle_exception(NFS_SERVER(dir), err,
6347 } while (exception.retry);
6352 * This operation also signals the server that this client is
6353 * performing migration recovery. The server can stop returning
6354 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6355 * appended to this compound to identify the client ID which is
6356 * performing recovery.
6358 static int _nfs40_proc_get_locations(struct inode *inode,
6359 struct nfs4_fs_locations *locations,
6360 struct page *page, struct rpc_cred *cred)
6362 struct nfs_server *server = NFS_SERVER(inode);
6363 struct rpc_clnt *clnt = server->client;
6365 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6367 struct nfs4_fs_locations_arg args = {
6368 .clientid = server->nfs_client->cl_clientid,
6369 .fh = NFS_FH(inode),
6372 .migration = 1, /* skip LOOKUP */
6373 .renew = 1, /* append RENEW */
6375 struct nfs4_fs_locations_res res = {
6376 .fs_locations = locations,
6380 struct rpc_message msg = {
6381 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6386 unsigned long now = jiffies;
6389 nfs_fattr_init(&locations->fattr);
6390 locations->server = server;
6391 locations->nlocations = 0;
6393 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6394 nfs4_set_sequence_privileged(&args.seq_args);
6395 status = nfs4_call_sync_sequence(clnt, server, &msg,
6396 &args.seq_args, &res.seq_res);
6400 renew_lease(server, now);
6404 #ifdef CONFIG_NFS_V4_1
6407 * This operation also signals the server that this client is
6408 * performing migration recovery. The server can stop asserting
6409 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6410 * performing this operation is identified in the SEQUENCE
6411 * operation in this compound.
6413 * When the client supports GETATTR(fs_locations_info), it can
6414 * be plumbed in here.
6416 static int _nfs41_proc_get_locations(struct inode *inode,
6417 struct nfs4_fs_locations *locations,
6418 struct page *page, struct rpc_cred *cred)
6420 struct nfs_server *server = NFS_SERVER(inode);
6421 struct rpc_clnt *clnt = server->client;
6423 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6425 struct nfs4_fs_locations_arg args = {
6426 .fh = NFS_FH(inode),
6429 .migration = 1, /* skip LOOKUP */
6431 struct nfs4_fs_locations_res res = {
6432 .fs_locations = locations,
6435 struct rpc_message msg = {
6436 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6443 nfs_fattr_init(&locations->fattr);
6444 locations->server = server;
6445 locations->nlocations = 0;
6447 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6448 nfs4_set_sequence_privileged(&args.seq_args);
6449 status = nfs4_call_sync_sequence(clnt, server, &msg,
6450 &args.seq_args, &res.seq_res);
6451 if (status == NFS4_OK &&
6452 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6453 status = -NFS4ERR_LEASE_MOVED;
6457 #endif /* CONFIG_NFS_V4_1 */
6460 * nfs4_proc_get_locations - discover locations for a migrated FSID
6461 * @inode: inode on FSID that is migrating
6462 * @locations: result of query
6464 * @cred: credential to use for this operation
6466 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6467 * operation failed, or a negative errno if a local error occurred.
6469 * On success, "locations" is filled in, but if the server has
6470 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6473 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6474 * from this client that require migration recovery.
6476 int nfs4_proc_get_locations(struct inode *inode,
6477 struct nfs4_fs_locations *locations,
6478 struct page *page, struct rpc_cred *cred)
6480 struct nfs_server *server = NFS_SERVER(inode);
6481 struct nfs_client *clp = server->nfs_client;
6482 const struct nfs4_mig_recovery_ops *ops =
6483 clp->cl_mvops->mig_recovery_ops;
6484 struct nfs4_exception exception = { };
6487 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6488 (unsigned long long)server->fsid.major,
6489 (unsigned long long)server->fsid.minor,
6491 nfs_display_fhandle(NFS_FH(inode), __func__);
6494 status = ops->get_locations(inode, locations, page, cred);
6495 if (status != -NFS4ERR_DELAY)
6497 nfs4_handle_exception(server, status, &exception);
6498 } while (exception.retry);
6503 * This operation also signals the server that this client is
6504 * performing "lease moved" recovery. The server can stop
6505 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6506 * is appended to this compound to identify the client ID which is
6507 * performing recovery.
6509 static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6511 struct nfs_server *server = NFS_SERVER(inode);
6512 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6513 struct rpc_clnt *clnt = server->client;
6514 struct nfs4_fsid_present_arg args = {
6515 .fh = NFS_FH(inode),
6516 .clientid = clp->cl_clientid,
6517 .renew = 1, /* append RENEW */
6519 struct nfs4_fsid_present_res res = {
6522 struct rpc_message msg = {
6523 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6528 unsigned long now = jiffies;
6531 res.fh = nfs_alloc_fhandle();
6535 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6536 nfs4_set_sequence_privileged(&args.seq_args);
6537 status = nfs4_call_sync_sequence(clnt, server, &msg,
6538 &args.seq_args, &res.seq_res);
6539 nfs_free_fhandle(res.fh);
6543 do_renew_lease(clp, now);
6547 #ifdef CONFIG_NFS_V4_1
6550 * This operation also signals the server that this client is
6551 * performing "lease moved" recovery. The server can stop asserting
6552 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6553 * this operation is identified in the SEQUENCE operation in this
6556 static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6558 struct nfs_server *server = NFS_SERVER(inode);
6559 struct rpc_clnt *clnt = server->client;
6560 struct nfs4_fsid_present_arg args = {
6561 .fh = NFS_FH(inode),
6563 struct nfs4_fsid_present_res res = {
6565 struct rpc_message msg = {
6566 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6573 res.fh = nfs_alloc_fhandle();
6577 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6578 nfs4_set_sequence_privileged(&args.seq_args);
6579 status = nfs4_call_sync_sequence(clnt, server, &msg,
6580 &args.seq_args, &res.seq_res);
6581 nfs_free_fhandle(res.fh);
6582 if (status == NFS4_OK &&
6583 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6584 status = -NFS4ERR_LEASE_MOVED;
6588 #endif /* CONFIG_NFS_V4_1 */
6591 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6592 * @inode: inode on FSID to check
6593 * @cred: credential to use for this operation
6595 * Server indicates whether the FSID is present, moved, or not
6596 * recognized. This operation is necessary to clear a LEASE_MOVED
6597 * condition for this client ID.
6599 * Returns NFS4_OK if the FSID is present on this server,
6600 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6601 * NFS4ERR code if some error occurred on the server, or a
6602 * negative errno if a local failure occurred.
6604 int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6606 struct nfs_server *server = NFS_SERVER(inode);
6607 struct nfs_client *clp = server->nfs_client;
6608 const struct nfs4_mig_recovery_ops *ops =
6609 clp->cl_mvops->mig_recovery_ops;
6610 struct nfs4_exception exception = { };
6613 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6614 (unsigned long long)server->fsid.major,
6615 (unsigned long long)server->fsid.minor,
6617 nfs_display_fhandle(NFS_FH(inode), __func__);
6620 status = ops->fsid_present(inode, cred);
6621 if (status != -NFS4ERR_DELAY)
6623 nfs4_handle_exception(server, status, &exception);
6624 } while (exception.retry);
6629 * If 'use_integrity' is true and the state managment nfs_client
6630 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
6631 * and the machine credential as per RFC3530bis and RFC5661 Security
6632 * Considerations sections. Otherwise, just use the user cred with the
6633 * filesystem's rpc_client.
6635 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
6638 struct nfs4_secinfo_arg args = {
6639 .dir_fh = NFS_FH(dir),
6642 struct nfs4_secinfo_res res = {
6645 struct rpc_message msg = {
6646 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
6650 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
6651 struct rpc_cred *cred = NULL;
6653 if (use_integrity) {
6654 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
6655 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
6656 msg.rpc_cred = cred;
6659 dprintk("NFS call secinfo %s\n", name->name);
6661 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
6662 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
6664 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
6666 dprintk("NFS reply secinfo: %d\n", status);
6674 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
6675 struct nfs4_secinfo_flavors *flavors)
6677 struct nfs4_exception exception = { };
6680 err = -NFS4ERR_WRONGSEC;
6682 /* try to use integrity protection with machine cred */
6683 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
6684 err = _nfs4_proc_secinfo(dir, name, flavors, true);
6687 * if unable to use integrity protection, or SECINFO with
6688 * integrity protection returns NFS4ERR_WRONGSEC (which is
6689 * disallowed by spec, but exists in deployed servers) use
6690 * the current filesystem's rpc_client and the user cred.
6692 if (err == -NFS4ERR_WRONGSEC)
6693 err = _nfs4_proc_secinfo(dir, name, flavors, false);
6695 trace_nfs4_secinfo(dir, name, err);
6696 err = nfs4_handle_exception(NFS_SERVER(dir), err,
6698 } while (exception.retry);
6702 #ifdef CONFIG_NFS_V4_1
6704 * Check the exchange flags returned by the server for invalid flags, having
6705 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
6708 static int nfs4_check_cl_exchange_flags(u32 flags)
6710 if (flags & ~EXCHGID4_FLAG_MASK_R)
6712 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
6713 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
6715 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
6719 return -NFS4ERR_INVAL;
6723 nfs41_same_server_scope(struct nfs41_server_scope *a,
6724 struct nfs41_server_scope *b)
6726 if (a->server_scope_sz == b->server_scope_sz &&
6727 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
6734 * nfs4_proc_bind_conn_to_session()
6736 * The 4.1 client currently uses the same TCP connection for the
6737 * fore and backchannel.
6739 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
6742 struct nfs41_bind_conn_to_session_args args = {
6744 .dir = NFS4_CDFC4_FORE_OR_BOTH,
6746 struct nfs41_bind_conn_to_session_res res;
6747 struct rpc_message msg = {
6749 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
6755 dprintk("--> %s\n", __func__);
6757 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
6758 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
6759 args.dir = NFS4_CDFC4_FORE;
6761 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
6762 trace_nfs4_bind_conn_to_session(clp, status);
6764 if (memcmp(res.sessionid.data,
6765 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
6766 dprintk("NFS: %s: Session ID mismatch\n", __func__);
6770 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
6771 dprintk("NFS: %s: Unexpected direction from server\n",
6776 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
6777 dprintk("NFS: %s: Server returned RDMA mode = true\n",
6784 dprintk("<-- %s status= %d\n", __func__, status);
6789 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
6790 * and operations we'd like to see to enable certain features in the allow map
6792 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
6793 .how = SP4_MACH_CRED,
6794 .enforce.u.words = {
6795 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6796 1 << (OP_EXCHANGE_ID - 32) |
6797 1 << (OP_CREATE_SESSION - 32) |
6798 1 << (OP_DESTROY_SESSION - 32) |
6799 1 << (OP_DESTROY_CLIENTID - 32)
6802 [0] = 1 << (OP_CLOSE) |
6805 [1] = 1 << (OP_SECINFO - 32) |
6806 1 << (OP_SECINFO_NO_NAME - 32) |
6807 1 << (OP_TEST_STATEID - 32) |
6808 1 << (OP_FREE_STATEID - 32) |
6809 1 << (OP_WRITE - 32)
6814 * Select the state protection mode for client `clp' given the server results
6815 * from exchange_id in `sp'.
6817 * Returns 0 on success, negative errno otherwise.
6819 static int nfs4_sp4_select_mode(struct nfs_client *clp,
6820 struct nfs41_state_protection *sp)
6822 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
6823 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6824 1 << (OP_EXCHANGE_ID - 32) |
6825 1 << (OP_CREATE_SESSION - 32) |
6826 1 << (OP_DESTROY_SESSION - 32) |
6827 1 << (OP_DESTROY_CLIENTID - 32)
6831 if (sp->how == SP4_MACH_CRED) {
6832 /* Print state protect result */
6833 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
6834 for (i = 0; i <= LAST_NFS4_OP; i++) {
6835 if (test_bit(i, sp->enforce.u.longs))
6836 dfprintk(MOUNT, " enforce op %d\n", i);
6837 if (test_bit(i, sp->allow.u.longs))
6838 dfprintk(MOUNT, " allow op %d\n", i);
6841 /* make sure nothing is on enforce list that isn't supported */
6842 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
6843 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
6844 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6850 * Minimal mode - state operations are allowed to use machine
6851 * credential. Note this already happens by default, so the
6852 * client doesn't have to do anything more than the negotiation.
6854 * NOTE: we don't care if EXCHANGE_ID is in the list -
6855 * we're already using the machine cred for exchange_id
6856 * and will never use a different cred.
6858 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
6859 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
6860 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
6861 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
6862 dfprintk(MOUNT, "sp4_mach_cred:\n");
6863 dfprintk(MOUNT, " minimal mode enabled\n");
6864 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
6866 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6870 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
6871 test_bit(OP_LOCKU, sp->allow.u.longs)) {
6872 dfprintk(MOUNT, " cleanup mode enabled\n");
6873 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
6876 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
6877 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
6878 dfprintk(MOUNT, " secinfo mode enabled\n");
6879 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
6882 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
6883 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
6884 dfprintk(MOUNT, " stateid mode enabled\n");
6885 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
6888 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
6889 dfprintk(MOUNT, " write mode enabled\n");
6890 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
6893 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
6894 dfprintk(MOUNT, " commit mode enabled\n");
6895 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
6903 * _nfs4_proc_exchange_id()
6905 * Wrapper for EXCHANGE_ID operation.
6907 static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
6910 nfs4_verifier verifier;
6911 struct nfs41_exchange_id_args args = {
6912 .verifier = &verifier,
6914 #ifdef CONFIG_NFS_V4_1_MIGRATION
6915 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
6916 EXCHGID4_FLAG_BIND_PRINC_STATEID |
6917 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
6919 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
6920 EXCHGID4_FLAG_BIND_PRINC_STATEID,
6923 struct nfs41_exchange_id_res res = {
6927 struct rpc_message msg = {
6928 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
6934 nfs4_init_boot_verifier(clp, &verifier);
6936 status = nfs4_init_uniform_client_string(clp);
6940 dprintk("NFS call exchange_id auth=%s, '%s'\n",
6941 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6944 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
6946 if (unlikely(res.server_owner == NULL)) {
6951 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
6953 if (unlikely(res.server_scope == NULL)) {
6955 goto out_server_owner;
6958 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
6959 if (unlikely(res.impl_id == NULL)) {
6961 goto out_server_scope;
6966 args.state_protect.how = SP4_NONE;
6970 args.state_protect = nfs4_sp4_mach_cred_request;
6977 goto out_server_scope;
6980 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
6981 trace_nfs4_exchange_id(clp, status);
6983 status = nfs4_check_cl_exchange_flags(res.flags);
6986 status = nfs4_sp4_select_mode(clp, &res.state_protect);
6989 clp->cl_clientid = res.clientid;
6990 clp->cl_exchange_flags = res.flags;
6991 /* Client ID is not confirmed */
6992 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
6993 clear_bit(NFS4_SESSION_ESTABLISHED,
6994 &clp->cl_session->session_state);
6995 clp->cl_seqid = res.seqid;
6998 kfree(clp->cl_serverowner);
6999 clp->cl_serverowner = res.server_owner;
7000 res.server_owner = NULL;
7002 /* use the most recent implementation id */
7003 kfree(clp->cl_implid);
7004 clp->cl_implid = res.impl_id;
7006 if (clp->cl_serverscope != NULL &&
7007 !nfs41_same_server_scope(clp->cl_serverscope,
7008 res.server_scope)) {
7009 dprintk("%s: server_scope mismatch detected\n",
7011 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
7012 kfree(clp->cl_serverscope);
7013 clp->cl_serverscope = NULL;
7016 if (clp->cl_serverscope == NULL) {
7017 clp->cl_serverscope = res.server_scope;
7024 kfree(res.server_owner);
7026 kfree(res.server_scope);
7028 if (clp->cl_implid != NULL)
7029 dprintk("NFS reply exchange_id: Server Implementation ID: "
7030 "domain: %s, name: %s, date: %llu,%u\n",
7031 clp->cl_implid->domain, clp->cl_implid->name,
7032 clp->cl_implid->date.seconds,
7033 clp->cl_implid->date.nseconds);
7034 dprintk("NFS reply exchange_id: %d\n", status);
7039 * nfs4_proc_exchange_id()
7041 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7043 * Since the clientid has expired, all compounds using sessions
7044 * associated with the stale clientid will be returning
7045 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7046 * be in some phase of session reset.
7048 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7050 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7052 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7055 /* try SP4_MACH_CRED if krb5i/p */
7056 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7057 authflavor == RPC_AUTH_GSS_KRB5P) {
7058 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
7064 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
7067 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7068 struct rpc_cred *cred)
7070 struct rpc_message msg = {
7071 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7077 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7078 trace_nfs4_destroy_clientid(clp, status);
7080 dprintk("NFS: Got error %d from the server %s on "
7081 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7085 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7086 struct rpc_cred *cred)
7091 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7092 ret = _nfs4_proc_destroy_clientid(clp, cred);
7094 case -NFS4ERR_DELAY:
7095 case -NFS4ERR_CLIENTID_BUSY:
7105 int nfs4_destroy_clientid(struct nfs_client *clp)
7107 struct rpc_cred *cred;
7110 if (clp->cl_mvops->minor_version < 1)
7112 if (clp->cl_exchange_flags == 0)
7114 if (clp->cl_preserve_clid)
7116 cred = nfs4_get_clid_cred(clp);
7117 ret = nfs4_proc_destroy_clientid(clp, cred);
7122 case -NFS4ERR_STALE_CLIENTID:
7123 clp->cl_exchange_flags = 0;
7129 struct nfs4_get_lease_time_data {
7130 struct nfs4_get_lease_time_args *args;
7131 struct nfs4_get_lease_time_res *res;
7132 struct nfs_client *clp;
7135 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7138 struct nfs4_get_lease_time_data *data =
7139 (struct nfs4_get_lease_time_data *)calldata;
7141 dprintk("--> %s\n", __func__);
7142 /* just setup sequence, do not trigger session recovery
7143 since we're invoked within one */
7144 nfs41_setup_sequence(data->clp->cl_session,
7145 &data->args->la_seq_args,
7146 &data->res->lr_seq_res,
7148 dprintk("<-- %s\n", __func__);
7152 * Called from nfs4_state_manager thread for session setup, so don't recover
7153 * from sequence operation or clientid errors.
7155 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7157 struct nfs4_get_lease_time_data *data =
7158 (struct nfs4_get_lease_time_data *)calldata;
7160 dprintk("--> %s\n", __func__);
7161 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7163 switch (task->tk_status) {
7164 case -NFS4ERR_DELAY:
7165 case -NFS4ERR_GRACE:
7166 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7167 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7168 task->tk_status = 0;
7170 case -NFS4ERR_RETRY_UNCACHED_REP:
7171 rpc_restart_call_prepare(task);
7174 dprintk("<-- %s\n", __func__);
7177 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
7178 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7179 .rpc_call_done = nfs4_get_lease_time_done,
7182 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7184 struct rpc_task *task;
7185 struct nfs4_get_lease_time_args args;
7186 struct nfs4_get_lease_time_res res = {
7187 .lr_fsinfo = fsinfo,
7189 struct nfs4_get_lease_time_data data = {
7194 struct rpc_message msg = {
7195 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7199 struct rpc_task_setup task_setup = {
7200 .rpc_client = clp->cl_rpcclient,
7201 .rpc_message = &msg,
7202 .callback_ops = &nfs4_get_lease_time_ops,
7203 .callback_data = &data,
7204 .flags = RPC_TASK_TIMEOUT,
7208 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
7209 nfs4_set_sequence_privileged(&args.la_seq_args);
7210 dprintk("--> %s\n", __func__);
7211 task = rpc_run_task(&task_setup);
7214 status = PTR_ERR(task);
7216 status = task->tk_status;
7219 dprintk("<-- %s return %d\n", __func__, status);
7225 * Initialize the values to be used by the client in CREATE_SESSION
7226 * If nfs4_init_session set the fore channel request and response sizes,
7229 * Set the back channel max_resp_sz_cached to zero to force the client to
7230 * always set csa_cachethis to FALSE because the current implementation
7231 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7233 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
7235 unsigned int max_rqst_sz, max_resp_sz;
7237 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7238 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7240 /* Fore channel attributes */
7241 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7242 args->fc_attrs.max_resp_sz = max_resp_sz;
7243 args->fc_attrs.max_ops = NFS4_MAX_OPS;
7244 args->fc_attrs.max_reqs = max_session_slots;
7246 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
7247 "max_ops=%u max_reqs=%u\n",
7249 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
7250 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
7252 /* Back channel attributes */
7253 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
7254 args->bc_attrs.max_resp_sz = PAGE_SIZE;
7255 args->bc_attrs.max_resp_sz_cached = 0;
7256 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
7257 args->bc_attrs.max_reqs = 1;
7259 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7260 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7262 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7263 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7264 args->bc_attrs.max_reqs);
7267 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7268 struct nfs41_create_session_res *res)
7270 struct nfs4_channel_attrs *sent = &args->fc_attrs;
7271 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
7273 if (rcvd->max_resp_sz > sent->max_resp_sz)
7276 * Our requested max_ops is the minimum we need; we're not
7277 * prepared to break up compounds into smaller pieces than that.
7278 * So, no point even trying to continue if the server won't
7281 if (rcvd->max_ops < sent->max_ops)
7283 if (rcvd->max_reqs == 0)
7285 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7286 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
7290 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7291 struct nfs41_create_session_res *res)
7293 struct nfs4_channel_attrs *sent = &args->bc_attrs;
7294 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
7296 if (!(res->flags & SESSION4_BACK_CHAN))
7298 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7300 if (rcvd->max_resp_sz < sent->max_resp_sz)
7302 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7304 /* These would render the backchannel useless: */
7305 if (rcvd->max_ops != sent->max_ops)
7307 if (rcvd->max_reqs != sent->max_reqs)
7313 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
7314 struct nfs41_create_session_res *res)
7318 ret = nfs4_verify_fore_channel_attrs(args, res);
7321 return nfs4_verify_back_channel_attrs(args, res);
7324 static void nfs4_update_session(struct nfs4_session *session,
7325 struct nfs41_create_session_res *res)
7327 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
7328 /* Mark client id and session as being confirmed */
7329 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7330 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
7331 session->flags = res->flags;
7332 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
7333 if (res->flags & SESSION4_BACK_CHAN)
7334 memcpy(&session->bc_attrs, &res->bc_attrs,
7335 sizeof(session->bc_attrs));
7338 static int _nfs4_proc_create_session(struct nfs_client *clp,
7339 struct rpc_cred *cred)
7341 struct nfs4_session *session = clp->cl_session;
7342 struct nfs41_create_session_args args = {
7344 .clientid = clp->cl_clientid,
7345 .seqid = clp->cl_seqid,
7346 .cb_program = NFS4_CALLBACK,
7348 struct nfs41_create_session_res res;
7350 struct rpc_message msg = {
7351 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7358 nfs4_init_channel_attrs(&args);
7359 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
7361 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7362 trace_nfs4_create_session(clp, status);
7365 /* Verify the session's negotiated channel_attrs values */
7366 status = nfs4_verify_channel_attrs(&args, &res);
7367 /* Increment the clientid slot sequence id */
7368 if (clp->cl_seqid == res.seqid)
7372 nfs4_update_session(session, &res);
7379 * Issues a CREATE_SESSION operation to the server.
7380 * It is the responsibility of the caller to verify the session is
7381 * expired before calling this routine.
7383 int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
7387 struct nfs4_session *session = clp->cl_session;
7389 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7391 status = _nfs4_proc_create_session(clp, cred);
7395 /* Init or reset the session slot tables */
7396 status = nfs4_setup_session_slot_tables(session);
7397 dprintk("slot table setup returned %d\n", status);
7401 ptr = (unsigned *)&session->sess_id.data[0];
7402 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7403 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
7405 dprintk("<-- %s\n", __func__);
7410 * Issue the over-the-wire RPC DESTROY_SESSION.
7411 * The caller must serialize access to this routine.
7413 int nfs4_proc_destroy_session(struct nfs4_session *session,
7414 struct rpc_cred *cred)
7416 struct rpc_message msg = {
7417 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
7418 .rpc_argp = session,
7423 dprintk("--> nfs4_proc_destroy_session\n");
7425 /* session is still being setup */
7426 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
7429 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7430 trace_nfs4_destroy_session(session->clp, status);
7433 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
7434 "Session has been destroyed regardless...\n", status);
7436 dprintk("<-- nfs4_proc_destroy_session\n");
7441 * Renew the cl_session lease.
7443 struct nfs4_sequence_data {
7444 struct nfs_client *clp;
7445 struct nfs4_sequence_args args;
7446 struct nfs4_sequence_res res;
7449 static void nfs41_sequence_release(void *data)
7451 struct nfs4_sequence_data *calldata = data;
7452 struct nfs_client *clp = calldata->clp;
7454 if (atomic_read(&clp->cl_count) > 1)
7455 nfs4_schedule_state_renewal(clp);
7456 nfs_put_client(clp);
7460 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7462 switch(task->tk_status) {
7463 case -NFS4ERR_DELAY:
7464 rpc_delay(task, NFS4_POLL_RETRY_MAX);
7467 nfs4_schedule_lease_recovery(clp);
7472 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
7474 struct nfs4_sequence_data *calldata = data;
7475 struct nfs_client *clp = calldata->clp;
7477 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
7480 trace_nfs4_sequence(clp, task->tk_status);
7481 if (task->tk_status < 0) {
7482 dprintk("%s ERROR %d\n", __func__, task->tk_status);
7483 if (atomic_read(&clp->cl_count) == 1)
7486 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
7487 rpc_restart_call_prepare(task);
7491 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
7493 dprintk("<-- %s\n", __func__);
7496 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
7498 struct nfs4_sequence_data *calldata = data;
7499 struct nfs_client *clp = calldata->clp;
7500 struct nfs4_sequence_args *args;
7501 struct nfs4_sequence_res *res;
7503 args = task->tk_msg.rpc_argp;
7504 res = task->tk_msg.rpc_resp;
7506 nfs41_setup_sequence(clp->cl_session, args, res, task);
7509 static const struct rpc_call_ops nfs41_sequence_ops = {
7510 .rpc_call_done = nfs41_sequence_call_done,
7511 .rpc_call_prepare = nfs41_sequence_prepare,
7512 .rpc_release = nfs41_sequence_release,
7515 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
7516 struct rpc_cred *cred,
7519 struct nfs4_sequence_data *calldata;
7520 struct rpc_message msg = {
7521 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
7524 struct rpc_task_setup task_setup_data = {
7525 .rpc_client = clp->cl_rpcclient,
7526 .rpc_message = &msg,
7527 .callback_ops = &nfs41_sequence_ops,
7528 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7531 if (!atomic_inc_not_zero(&clp->cl_count))
7532 return ERR_PTR(-EIO);
7533 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7534 if (calldata == NULL) {
7535 nfs_put_client(clp);
7536 return ERR_PTR(-ENOMEM);
7538 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
7540 nfs4_set_sequence_privileged(&calldata->args);
7541 msg.rpc_argp = &calldata->args;
7542 msg.rpc_resp = &calldata->res;
7543 calldata->clp = clp;
7544 task_setup_data.callback_data = calldata;
7546 return rpc_run_task(&task_setup_data);
7549 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
7551 struct rpc_task *task;
7554 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
7556 task = _nfs41_proc_sequence(clp, cred, false);
7558 ret = PTR_ERR(task);
7560 rpc_put_task_async(task);
7561 dprintk("<-- %s status=%d\n", __func__, ret);
7565 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
7567 struct rpc_task *task;
7570 task = _nfs41_proc_sequence(clp, cred, true);
7572 ret = PTR_ERR(task);
7575 ret = rpc_wait_for_completion_task(task);
7577 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
7579 if (task->tk_status == 0)
7580 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
7581 ret = task->tk_status;
7585 dprintk("<-- %s status=%d\n", __func__, ret);
7589 struct nfs4_reclaim_complete_data {
7590 struct nfs_client *clp;
7591 struct nfs41_reclaim_complete_args arg;
7592 struct nfs41_reclaim_complete_res res;
7595 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
7597 struct nfs4_reclaim_complete_data *calldata = data;
7599 nfs41_setup_sequence(calldata->clp->cl_session,
7600 &calldata->arg.seq_args,
7601 &calldata->res.seq_res,
7605 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7607 switch(task->tk_status) {
7609 case -NFS4ERR_COMPLETE_ALREADY:
7610 case -NFS4ERR_WRONG_CRED: /* What to do here? */
7612 case -NFS4ERR_DELAY:
7613 rpc_delay(task, NFS4_POLL_RETRY_MAX);
7615 case -NFS4ERR_RETRY_UNCACHED_REP:
7618 nfs4_schedule_lease_recovery(clp);
7623 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
7625 struct nfs4_reclaim_complete_data *calldata = data;
7626 struct nfs_client *clp = calldata->clp;
7627 struct nfs4_sequence_res *res = &calldata->res.seq_res;
7629 dprintk("--> %s\n", __func__);
7630 if (!nfs41_sequence_done(task, res))
7633 trace_nfs4_reclaim_complete(clp, task->tk_status);
7634 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
7635 rpc_restart_call_prepare(task);
7638 dprintk("<-- %s\n", __func__);
7641 static void nfs4_free_reclaim_complete_data(void *data)
7643 struct nfs4_reclaim_complete_data *calldata = data;
7648 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
7649 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
7650 .rpc_call_done = nfs4_reclaim_complete_done,
7651 .rpc_release = nfs4_free_reclaim_complete_data,
7655 * Issue a global reclaim complete.
7657 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
7658 struct rpc_cred *cred)
7660 struct nfs4_reclaim_complete_data *calldata;
7661 struct rpc_task *task;
7662 struct rpc_message msg = {
7663 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
7666 struct rpc_task_setup task_setup_data = {
7667 .rpc_client = clp->cl_rpcclient,
7668 .rpc_message = &msg,
7669 .callback_ops = &nfs4_reclaim_complete_call_ops,
7670 .flags = RPC_TASK_ASYNC,
7672 int status = -ENOMEM;
7674 dprintk("--> %s\n", __func__);
7675 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7676 if (calldata == NULL)
7678 calldata->clp = clp;
7679 calldata->arg.one_fs = 0;
7681 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
7682 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
7683 msg.rpc_argp = &calldata->arg;
7684 msg.rpc_resp = &calldata->res;
7685 task_setup_data.callback_data = calldata;
7686 task = rpc_run_task(&task_setup_data);
7688 status = PTR_ERR(task);
7691 status = nfs4_wait_for_completion_rpc_task(task);
7693 status = task->tk_status;
7697 dprintk("<-- %s status=%d\n", __func__, status);
7702 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
7704 struct nfs4_layoutget *lgp = calldata;
7705 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
7706 struct nfs4_session *session = nfs4_get_session(server);
7708 dprintk("--> %s\n", __func__);
7709 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
7710 * right now covering the LAYOUTGET we are about to send.
7711 * However, that is not so catastrophic, and there seems
7712 * to be no way to prevent it completely.
7714 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
7715 &lgp->res.seq_res, task))
7717 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
7718 NFS_I(lgp->args.inode)->layout,
7720 lgp->args.ctx->state)) {
7721 rpc_exit(task, NFS4_OK);
7725 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
7727 struct nfs4_layoutget *lgp = calldata;
7728 struct inode *inode = lgp->args.inode;
7729 struct nfs_server *server = NFS_SERVER(inode);
7730 struct pnfs_layout_hdr *lo;
7731 struct nfs4_state *state = NULL;
7732 unsigned long timeo, now, giveup;
7734 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
7736 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
7739 switch (task->tk_status) {
7743 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
7744 * (or clients) writing to the same RAID stripe
7746 case -NFS4ERR_LAYOUTTRYLATER:
7748 * NFS4ERR_RECALLCONFLICT is when conflict with self (must recall
7749 * existing layout before getting a new one).
7751 case -NFS4ERR_RECALLCONFLICT:
7752 timeo = rpc_get_timeout(task->tk_client);
7753 giveup = lgp->args.timestamp + timeo;
7755 if (time_after(giveup, now)) {
7756 unsigned long delay;
7759 * - Not less then NFS4_POLL_RETRY_MIN.
7760 * - One last time a jiffie before we give up
7761 * - exponential backoff (time_now minus start_attempt)
7763 delay = max_t(unsigned long, NFS4_POLL_RETRY_MIN,
7764 min((giveup - now - 1),
7765 now - lgp->args.timestamp));
7767 dprintk("%s: NFS4ERR_RECALLCONFLICT waiting %lu\n",
7769 rpc_delay(task, delay);
7770 task->tk_status = 0;
7771 rpc_restart_call_prepare(task);
7772 goto out; /* Do not call nfs4_async_handle_error() */
7775 case -NFS4ERR_EXPIRED:
7776 case -NFS4ERR_BAD_STATEID:
7777 spin_lock(&inode->i_lock);
7778 lo = NFS_I(inode)->layout;
7779 if (!lo || list_empty(&lo->plh_segs)) {
7780 spin_unlock(&inode->i_lock);
7781 /* If the open stateid was bad, then recover it. */
7782 state = lgp->args.ctx->state;
7787 * Mark the bad layout state as invalid, then retry
7788 * with the current stateid.
7790 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
7791 spin_unlock(&inode->i_lock);
7792 pnfs_free_lseg_list(&head);
7794 task->tk_status = 0;
7795 rpc_restart_call_prepare(task);
7798 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN)
7799 rpc_restart_call_prepare(task);
7801 dprintk("<-- %s\n", __func__);
7804 static size_t max_response_pages(struct nfs_server *server)
7806 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
7807 return nfs_page_array_len(0, max_resp_sz);
7810 static void nfs4_free_pages(struct page **pages, size_t size)
7817 for (i = 0; i < size; i++) {
7820 __free_page(pages[i]);
7825 static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
7827 struct page **pages;
7830 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
7832 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
7836 for (i = 0; i < size; i++) {
7837 pages[i] = alloc_page(gfp_flags);
7839 dprintk("%s: failed to allocate page\n", __func__);
7840 nfs4_free_pages(pages, size);
7848 static void nfs4_layoutget_release(void *calldata)
7850 struct nfs4_layoutget *lgp = calldata;
7851 struct inode *inode = lgp->args.inode;
7852 struct nfs_server *server = NFS_SERVER(inode);
7853 size_t max_pages = max_response_pages(server);
7855 dprintk("--> %s\n", __func__);
7856 nfs4_free_pages(lgp->args.layout.pages, max_pages);
7857 pnfs_put_layout_hdr(NFS_I(inode)->layout);
7858 put_nfs_open_context(lgp->args.ctx);
7860 dprintk("<-- %s\n", __func__);
7863 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
7864 .rpc_call_prepare = nfs4_layoutget_prepare,
7865 .rpc_call_done = nfs4_layoutget_done,
7866 .rpc_release = nfs4_layoutget_release,
7869 struct pnfs_layout_segment *
7870 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
7872 struct inode *inode = lgp->args.inode;
7873 struct nfs_server *server = NFS_SERVER(inode);
7874 size_t max_pages = max_response_pages(server);
7875 struct rpc_task *task;
7876 struct rpc_message msg = {
7877 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
7878 .rpc_argp = &lgp->args,
7879 .rpc_resp = &lgp->res,
7880 .rpc_cred = lgp->cred,
7882 struct rpc_task_setup task_setup_data = {
7883 .rpc_client = server->client,
7884 .rpc_message = &msg,
7885 .callback_ops = &nfs4_layoutget_call_ops,
7886 .callback_data = lgp,
7887 .flags = RPC_TASK_ASYNC,
7889 struct pnfs_layout_segment *lseg = NULL;
7892 dprintk("--> %s\n", __func__);
7894 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
7895 pnfs_get_layout_hdr(NFS_I(inode)->layout);
7897 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
7898 if (!lgp->args.layout.pages) {
7899 nfs4_layoutget_release(lgp);
7900 return ERR_PTR(-ENOMEM);
7902 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
7903 lgp->args.timestamp = jiffies;
7905 lgp->res.layoutp = &lgp->args.layout;
7906 lgp->res.seq_res.sr_slot = NULL;
7907 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
7909 task = rpc_run_task(&task_setup_data);
7911 return ERR_CAST(task);
7912 status = nfs4_wait_for_completion_rpc_task(task);
7914 status = task->tk_status;
7915 trace_nfs4_layoutget(lgp->args.ctx,
7919 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
7920 if (status == 0 && lgp->res.layoutp->len)
7921 lseg = pnfs_layout_process(lgp);
7923 dprintk("<-- %s status=%d\n", __func__, status);
7925 return ERR_PTR(status);
7930 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
7932 struct nfs4_layoutreturn *lrp = calldata;
7934 dprintk("--> %s\n", __func__);
7935 nfs41_setup_sequence(lrp->clp->cl_session,
7936 &lrp->args.seq_args,
7941 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
7943 struct nfs4_layoutreturn *lrp = calldata;
7944 struct nfs_server *server;
7946 dprintk("--> %s\n", __func__);
7948 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
7951 server = NFS_SERVER(lrp->args.inode);
7952 switch (task->tk_status) {
7954 task->tk_status = 0;
7957 case -NFS4ERR_DELAY:
7958 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
7960 rpc_restart_call_prepare(task);
7963 dprintk("<-- %s\n", __func__);
7966 static void nfs4_layoutreturn_release(void *calldata)
7968 struct nfs4_layoutreturn *lrp = calldata;
7969 struct pnfs_layout_hdr *lo = lrp->args.layout;
7971 dprintk("--> %s\n", __func__);
7972 spin_lock(&lo->plh_inode->i_lock);
7973 if (lrp->res.lrs_present)
7974 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
7975 pnfs_clear_layoutreturn_waitbit(lo);
7976 clear_bit(NFS_LAYOUT_RETURN_BEFORE_CLOSE, &lo->plh_flags);
7977 rpc_wake_up(&NFS_SERVER(lo->plh_inode)->roc_rpcwaitq);
7978 lo->plh_block_lgets--;
7979 spin_unlock(&lo->plh_inode->i_lock);
7980 pnfs_put_layout_hdr(lrp->args.layout);
7981 nfs_iput_and_deactive(lrp->inode);
7983 dprintk("<-- %s\n", __func__);
7986 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
7987 .rpc_call_prepare = nfs4_layoutreturn_prepare,
7988 .rpc_call_done = nfs4_layoutreturn_done,
7989 .rpc_release = nfs4_layoutreturn_release,
7992 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
7994 struct rpc_task *task;
7995 struct rpc_message msg = {
7996 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
7997 .rpc_argp = &lrp->args,
7998 .rpc_resp = &lrp->res,
7999 .rpc_cred = lrp->cred,
8001 struct rpc_task_setup task_setup_data = {
8002 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
8003 .rpc_message = &msg,
8004 .callback_ops = &nfs4_layoutreturn_call_ops,
8005 .callback_data = lrp,
8009 dprintk("--> %s\n", __func__);
8011 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8013 nfs4_layoutreturn_release(lrp);
8016 task_setup_data.flags |= RPC_TASK_ASYNC;
8018 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
8019 task = rpc_run_task(&task_setup_data);
8021 return PTR_ERR(task);
8023 status = task->tk_status;
8024 trace_nfs4_layoutreturn(lrp->args.inode, status);
8025 dprintk("<-- %s status=%d\n", __func__, status);
8031 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
8032 struct pnfs_device *pdev,
8033 struct rpc_cred *cred)
8035 struct nfs4_getdeviceinfo_args args = {
8037 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8038 NOTIFY_DEVICEID4_DELETE,
8040 struct nfs4_getdeviceinfo_res res = {
8043 struct rpc_message msg = {
8044 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8051 dprintk("--> %s\n", __func__);
8052 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
8053 if (res.notification & ~args.notify_types)
8054 dprintk("%s: unsupported notification\n", __func__);
8055 if (res.notification != args.notify_types)
8058 dprintk("<-- %s status=%d\n", __func__, status);
8063 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8064 struct pnfs_device *pdev,
8065 struct rpc_cred *cred)
8067 struct nfs4_exception exception = { };
8071 err = nfs4_handle_exception(server,
8072 _nfs4_proc_getdeviceinfo(server, pdev, cred),
8074 } while (exception.retry);
8077 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8079 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8081 struct nfs4_layoutcommit_data *data = calldata;
8082 struct nfs_server *server = NFS_SERVER(data->args.inode);
8083 struct nfs4_session *session = nfs4_get_session(server);
8085 nfs41_setup_sequence(session,
8086 &data->args.seq_args,
8092 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8094 struct nfs4_layoutcommit_data *data = calldata;
8095 struct nfs_server *server = NFS_SERVER(data->args.inode);
8097 if (!nfs41_sequence_done(task, &data->res.seq_res))
8100 switch (task->tk_status) { /* Just ignore these failures */
8101 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8102 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8103 case -NFS4ERR_BADLAYOUT: /* no layout */
8104 case -NFS4ERR_GRACE: /* loca_recalim always false */
8105 task->tk_status = 0;
8109 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
8110 rpc_restart_call_prepare(task);
8116 static void nfs4_layoutcommit_release(void *calldata)
8118 struct nfs4_layoutcommit_data *data = calldata;
8120 pnfs_cleanup_layoutcommit(data);
8121 nfs_post_op_update_inode_force_wcc(data->args.inode,
8123 put_rpccred(data->cred);
8124 nfs_iput_and_deactive(data->inode);
8128 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8129 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8130 .rpc_call_done = nfs4_layoutcommit_done,
8131 .rpc_release = nfs4_layoutcommit_release,
8135 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
8137 struct rpc_message msg = {
8138 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8139 .rpc_argp = &data->args,
8140 .rpc_resp = &data->res,
8141 .rpc_cred = data->cred,
8143 struct rpc_task_setup task_setup_data = {
8144 .task = &data->task,
8145 .rpc_client = NFS_CLIENT(data->args.inode),
8146 .rpc_message = &msg,
8147 .callback_ops = &nfs4_layoutcommit_ops,
8148 .callback_data = data,
8150 struct rpc_task *task;
8153 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
8154 "lbw: %llu inode %lu\n",
8155 data->task.tk_pid, sync,
8156 data->args.lastbytewritten,
8157 data->args.inode->i_ino);
8160 data->inode = nfs_igrab_and_active(data->args.inode);
8161 if (data->inode == NULL) {
8162 nfs4_layoutcommit_release(data);
8165 task_setup_data.flags = RPC_TASK_ASYNC;
8167 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
8168 task = rpc_run_task(&task_setup_data);
8170 return PTR_ERR(task);
8172 status = task->tk_status;
8173 trace_nfs4_layoutcommit(data->args.inode, status);
8174 dprintk("%s: status %d\n", __func__, status);
8180 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8181 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8184 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8185 struct nfs_fsinfo *info,
8186 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
8188 struct nfs41_secinfo_no_name_args args = {
8189 .style = SECINFO_STYLE_CURRENT_FH,
8191 struct nfs4_secinfo_res res = {
8194 struct rpc_message msg = {
8195 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8199 struct rpc_clnt *clnt = server->client;
8200 struct rpc_cred *cred = NULL;
8203 if (use_integrity) {
8204 clnt = server->nfs_client->cl_rpcclient;
8205 cred = nfs4_get_clid_cred(server->nfs_client);
8206 msg.rpc_cred = cred;
8209 dprintk("--> %s\n", __func__);
8210 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8212 dprintk("<-- %s status=%d\n", __func__, status);
8221 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8222 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8224 struct nfs4_exception exception = { };
8227 /* first try using integrity protection */
8228 err = -NFS4ERR_WRONGSEC;
8230 /* try to use integrity protection with machine cred */
8231 if (_nfs4_is_integrity_protected(server->nfs_client))
8232 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8236 * if unable to use integrity protection, or SECINFO with
8237 * integrity protection returns NFS4ERR_WRONGSEC (which is
8238 * disallowed by spec, but exists in deployed servers) use
8239 * the current filesystem's rpc_client and the user cred.
8241 if (err == -NFS4ERR_WRONGSEC)
8242 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8247 case -NFS4ERR_WRONGSEC:
8251 err = nfs4_handle_exception(server, err, &exception);
8253 } while (exception.retry);
8259 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8260 struct nfs_fsinfo *info)
8264 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
8265 struct nfs4_secinfo_flavors *flavors;
8266 struct nfs4_secinfo4 *secinfo;
8269 page = alloc_page(GFP_KERNEL);
8275 flavors = page_address(page);
8276 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8279 * Fall back on "guess and check" method if
8280 * the server doesn't support SECINFO_NO_NAME
8282 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
8283 err = nfs4_find_root_sec(server, fhandle, info);
8289 for (i = 0; i < flavors->num_flavors; i++) {
8290 secinfo = &flavors->flavors[i];
8292 switch (secinfo->flavor) {
8296 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8297 &secinfo->flavor_info);
8300 flavor = RPC_AUTH_MAXFLAVOR;
8304 if (!nfs_auth_info_match(&server->auth_info, flavor))
8305 flavor = RPC_AUTH_MAXFLAVOR;
8307 if (flavor != RPC_AUTH_MAXFLAVOR) {
8308 err = nfs4_lookup_root_sec(server, fhandle,
8315 if (flavor == RPC_AUTH_MAXFLAVOR)
8326 static int _nfs41_test_stateid(struct nfs_server *server,
8327 nfs4_stateid *stateid,
8328 struct rpc_cred *cred)
8331 struct nfs41_test_stateid_args args = {
8334 struct nfs41_test_stateid_res res;
8335 struct rpc_message msg = {
8336 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8341 struct rpc_clnt *rpc_client = server->client;
8343 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8346 dprintk("NFS call test_stateid %p\n", stateid);
8347 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
8348 nfs4_set_sequence_privileged(&args.seq_args);
8349 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
8350 &args.seq_args, &res.seq_res);
8351 if (status != NFS_OK) {
8352 dprintk("NFS reply test_stateid: failed, %d\n", status);
8355 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
8360 * nfs41_test_stateid - perform a TEST_STATEID operation
8362 * @server: server / transport on which to perform the operation
8363 * @stateid: state ID to test
8366 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8367 * Otherwise a negative NFS4ERR value is returned if the operation
8368 * failed or the state ID is not currently valid.
8370 static int nfs41_test_stateid(struct nfs_server *server,
8371 nfs4_stateid *stateid,
8372 struct rpc_cred *cred)
8374 struct nfs4_exception exception = { };
8377 err = _nfs41_test_stateid(server, stateid, cred);
8378 if (err != -NFS4ERR_DELAY)
8380 nfs4_handle_exception(server, err, &exception);
8381 } while (exception.retry);
8385 struct nfs_free_stateid_data {
8386 struct nfs_server *server;
8387 struct nfs41_free_stateid_args args;
8388 struct nfs41_free_stateid_res res;
8391 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
8393 struct nfs_free_stateid_data *data = calldata;
8394 nfs41_setup_sequence(nfs4_get_session(data->server),
8395 &data->args.seq_args,
8400 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
8402 struct nfs_free_stateid_data *data = calldata;
8404 nfs41_sequence_done(task, &data->res.seq_res);
8406 switch (task->tk_status) {
8407 case -NFS4ERR_DELAY:
8408 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
8409 rpc_restart_call_prepare(task);
8413 static void nfs41_free_stateid_release(void *calldata)
8418 static const struct rpc_call_ops nfs41_free_stateid_ops = {
8419 .rpc_call_prepare = nfs41_free_stateid_prepare,
8420 .rpc_call_done = nfs41_free_stateid_done,
8421 .rpc_release = nfs41_free_stateid_release,
8424 static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
8425 nfs4_stateid *stateid,
8426 struct rpc_cred *cred,
8429 struct rpc_message msg = {
8430 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
8433 struct rpc_task_setup task_setup = {
8434 .rpc_client = server->client,
8435 .rpc_message = &msg,
8436 .callback_ops = &nfs41_free_stateid_ops,
8437 .flags = RPC_TASK_ASYNC,
8439 struct nfs_free_stateid_data *data;
8441 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8442 &task_setup.rpc_client, &msg);
8444 dprintk("NFS call free_stateid %p\n", stateid);
8445 data = kmalloc(sizeof(*data), GFP_NOFS);
8447 return ERR_PTR(-ENOMEM);
8448 data->server = server;
8449 nfs4_stateid_copy(&data->args.stateid, stateid);
8451 task_setup.callback_data = data;
8453 msg.rpc_argp = &data->args;
8454 msg.rpc_resp = &data->res;
8455 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
8457 nfs4_set_sequence_privileged(&data->args.seq_args);
8459 return rpc_run_task(&task_setup);
8463 * nfs41_free_stateid - perform a FREE_STATEID operation
8465 * @server: server / transport on which to perform the operation
8466 * @stateid: state ID to release
8469 * Returns NFS_OK if the server freed "stateid". Otherwise a
8470 * negative NFS4ERR value is returned.
8472 static int nfs41_free_stateid(struct nfs_server *server,
8473 nfs4_stateid *stateid,
8474 struct rpc_cred *cred)
8476 struct rpc_task *task;
8479 task = _nfs41_free_stateid(server, stateid, cred, true);
8481 return PTR_ERR(task);
8482 ret = rpc_wait_for_completion_task(task);
8484 ret = task->tk_status;
8490 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
8492 struct rpc_task *task;
8493 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
8495 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
8496 nfs4_free_lock_state(server, lsp);
8502 static bool nfs41_match_stateid(const nfs4_stateid *s1,
8503 const nfs4_stateid *s2)
8505 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
8508 if (s1->seqid == s2->seqid)
8510 if (s1->seqid == 0 || s2->seqid == 0)
8516 #endif /* CONFIG_NFS_V4_1 */
8518 static bool nfs4_match_stateid(const nfs4_stateid *s1,
8519 const nfs4_stateid *s2)
8521 return nfs4_stateid_match(s1, s2);
8525 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
8526 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
8527 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
8528 .recover_open = nfs4_open_reclaim,
8529 .recover_lock = nfs4_lock_reclaim,
8530 .establish_clid = nfs4_init_clientid,
8531 .detect_trunking = nfs40_discover_server_trunking,
8534 #if defined(CONFIG_NFS_V4_1)
8535 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
8536 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
8537 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
8538 .recover_open = nfs4_open_reclaim,
8539 .recover_lock = nfs4_lock_reclaim,
8540 .establish_clid = nfs41_init_clientid,
8541 .reclaim_complete = nfs41_proc_reclaim_complete,
8542 .detect_trunking = nfs41_discover_server_trunking,
8544 #endif /* CONFIG_NFS_V4_1 */
8546 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
8547 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
8548 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
8549 .recover_open = nfs40_open_expired,
8550 .recover_lock = nfs4_lock_expired,
8551 .establish_clid = nfs4_init_clientid,
8554 #if defined(CONFIG_NFS_V4_1)
8555 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
8556 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
8557 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
8558 .recover_open = nfs41_open_expired,
8559 .recover_lock = nfs41_lock_expired,
8560 .establish_clid = nfs41_init_clientid,
8562 #endif /* CONFIG_NFS_V4_1 */
8564 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
8565 .sched_state_renewal = nfs4_proc_async_renew,
8566 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
8567 .renew_lease = nfs4_proc_renew,
8570 #if defined(CONFIG_NFS_V4_1)
8571 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
8572 .sched_state_renewal = nfs41_proc_async_sequence,
8573 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
8574 .renew_lease = nfs4_proc_sequence,
8578 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
8579 .get_locations = _nfs40_proc_get_locations,
8580 .fsid_present = _nfs40_proc_fsid_present,
8583 #if defined(CONFIG_NFS_V4_1)
8584 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
8585 .get_locations = _nfs41_proc_get_locations,
8586 .fsid_present = _nfs41_proc_fsid_present,
8588 #endif /* CONFIG_NFS_V4_1 */
8590 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
8592 .init_caps = NFS_CAP_READDIRPLUS
8593 | NFS_CAP_ATOMIC_OPEN
8594 | NFS_CAP_CHANGE_ATTR
8595 | NFS_CAP_POSIX_LOCK,
8596 .init_client = nfs40_init_client,
8597 .shutdown_client = nfs40_shutdown_client,
8598 .match_stateid = nfs4_match_stateid,
8599 .find_root_sec = nfs4_find_root_sec,
8600 .free_lock_state = nfs4_release_lockowner,
8601 .alloc_seqid = nfs_alloc_seqid,
8602 .call_sync_ops = &nfs40_call_sync_ops,
8603 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
8604 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
8605 .state_renewal_ops = &nfs40_state_renewal_ops,
8606 .mig_recovery_ops = &nfs40_mig_recovery_ops,
8609 #if defined(CONFIG_NFS_V4_1)
8610 static struct nfs_seqid *
8611 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
8616 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
8618 .init_caps = NFS_CAP_READDIRPLUS
8619 | NFS_CAP_ATOMIC_OPEN
8620 | NFS_CAP_CHANGE_ATTR
8621 | NFS_CAP_POSIX_LOCK
8622 | NFS_CAP_STATEID_NFSV41
8623 | NFS_CAP_ATOMIC_OPEN_V1,
8624 .init_client = nfs41_init_client,
8625 .shutdown_client = nfs41_shutdown_client,
8626 .match_stateid = nfs41_match_stateid,
8627 .find_root_sec = nfs41_find_root_sec,
8628 .free_lock_state = nfs41_free_lock_state,
8629 .alloc_seqid = nfs_alloc_no_seqid,
8630 .call_sync_ops = &nfs41_call_sync_ops,
8631 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8632 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8633 .state_renewal_ops = &nfs41_state_renewal_ops,
8634 .mig_recovery_ops = &nfs41_mig_recovery_ops,
8638 #if defined(CONFIG_NFS_V4_2)
8639 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
8641 .init_caps = NFS_CAP_READDIRPLUS
8642 | NFS_CAP_ATOMIC_OPEN
8643 | NFS_CAP_CHANGE_ATTR
8644 | NFS_CAP_POSIX_LOCK
8645 | NFS_CAP_STATEID_NFSV41
8646 | NFS_CAP_ATOMIC_OPEN_V1
8648 | NFS_CAP_DEALLOCATE
8650 .init_client = nfs41_init_client,
8651 .shutdown_client = nfs41_shutdown_client,
8652 .match_stateid = nfs41_match_stateid,
8653 .find_root_sec = nfs41_find_root_sec,
8654 .free_lock_state = nfs41_free_lock_state,
8655 .call_sync_ops = &nfs41_call_sync_ops,
8656 .alloc_seqid = nfs_alloc_no_seqid,
8657 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8658 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8659 .state_renewal_ops = &nfs41_state_renewal_ops,
8663 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
8664 [0] = &nfs_v4_0_minor_ops,
8665 #if defined(CONFIG_NFS_V4_1)
8666 [1] = &nfs_v4_1_minor_ops,
8668 #if defined(CONFIG_NFS_V4_2)
8669 [2] = &nfs_v4_2_minor_ops,
8673 static const struct inode_operations nfs4_dir_inode_operations = {
8674 .create = nfs_create,
8675 .lookup = nfs_lookup,
8676 .atomic_open = nfs_atomic_open,
8678 .unlink = nfs_unlink,
8679 .symlink = nfs_symlink,
8683 .rename = nfs_rename,
8684 .permission = nfs_permission,
8685 .getattr = nfs_getattr,
8686 .setattr = nfs_setattr,
8687 .getxattr = generic_getxattr,
8688 .setxattr = generic_setxattr,
8689 .listxattr = generic_listxattr,
8690 .removexattr = generic_removexattr,
8693 static const struct inode_operations nfs4_file_inode_operations = {
8694 .permission = nfs_permission,
8695 .getattr = nfs_getattr,
8696 .setattr = nfs_setattr,
8697 .getxattr = generic_getxattr,
8698 .setxattr = generic_setxattr,
8699 .listxattr = generic_listxattr,
8700 .removexattr = generic_removexattr,
8703 const struct nfs_rpc_ops nfs_v4_clientops = {
8704 .version = 4, /* protocol version */
8705 .dentry_ops = &nfs4_dentry_operations,
8706 .dir_inode_ops = &nfs4_dir_inode_operations,
8707 .file_inode_ops = &nfs4_file_inode_operations,
8708 .file_ops = &nfs4_file_operations,
8709 .getroot = nfs4_proc_get_root,
8710 .submount = nfs4_submount,
8711 .try_mount = nfs4_try_mount,
8712 .getattr = nfs4_proc_getattr,
8713 .setattr = nfs4_proc_setattr,
8714 .lookup = nfs4_proc_lookup,
8715 .access = nfs4_proc_access,
8716 .readlink = nfs4_proc_readlink,
8717 .create = nfs4_proc_create,
8718 .remove = nfs4_proc_remove,
8719 .unlink_setup = nfs4_proc_unlink_setup,
8720 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
8721 .unlink_done = nfs4_proc_unlink_done,
8722 .rename_setup = nfs4_proc_rename_setup,
8723 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
8724 .rename_done = nfs4_proc_rename_done,
8725 .link = nfs4_proc_link,
8726 .symlink = nfs4_proc_symlink,
8727 .mkdir = nfs4_proc_mkdir,
8728 .rmdir = nfs4_proc_remove,
8729 .readdir = nfs4_proc_readdir,
8730 .mknod = nfs4_proc_mknod,
8731 .statfs = nfs4_proc_statfs,
8732 .fsinfo = nfs4_proc_fsinfo,
8733 .pathconf = nfs4_proc_pathconf,
8734 .set_capabilities = nfs4_server_capabilities,
8735 .decode_dirent = nfs4_decode_dirent,
8736 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
8737 .read_setup = nfs4_proc_read_setup,
8738 .read_done = nfs4_read_done,
8739 .write_setup = nfs4_proc_write_setup,
8740 .write_done = nfs4_write_done,
8741 .commit_setup = nfs4_proc_commit_setup,
8742 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
8743 .commit_done = nfs4_commit_done,
8744 .lock = nfs4_proc_lock,
8745 .clear_acl_cache = nfs4_zap_acl_attr,
8746 .close_context = nfs4_close_context,
8747 .open_context = nfs4_atomic_open,
8748 .have_delegation = nfs4_have_delegation,
8749 .return_delegation = nfs4_inode_return_delegation,
8750 .alloc_client = nfs4_alloc_client,
8751 .init_client = nfs4_init_client,
8752 .free_client = nfs4_free_client,
8753 .create_server = nfs4_create_server,
8754 .clone_server = nfs_clone_server,
8757 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
8758 .prefix = XATTR_NAME_NFSV4_ACL,
8759 .list = nfs4_xattr_list_nfs4_acl,
8760 .get = nfs4_xattr_get_nfs4_acl,
8761 .set = nfs4_xattr_set_nfs4_acl,
8764 const struct xattr_handler *nfs4_xattr_handlers[] = {
8765 &nfs4_xattr_nfs4_acl_handler,
8766 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
8767 &nfs4_xattr_nfs4_label_handler,