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)
77 /* file attributes which can be mapped to nfs attributes */
78 #define NFS4_VALID_ATTRS (ATTR_MODE \
89 static int _nfs4_proc_open(struct nfs4_opendata *data);
90 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
91 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
92 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
93 static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
94 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label);
95 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
96 struct nfs_fattr *fattr, struct iattr *sattr,
97 struct nfs4_state *state, struct nfs4_label *ilabel,
98 struct nfs4_label *olabel);
99 #ifdef CONFIG_NFS_V4_1
100 static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
102 static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *,
106 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
107 static inline struct nfs4_label *
108 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
109 struct iattr *sattr, struct nfs4_label *label)
116 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
119 err = security_dentry_init_security(dentry, sattr->ia_mode,
120 &dentry->d_name, (void **)&label->label, &label->len);
127 nfs4_label_release_security(struct nfs4_label *label)
130 security_release_secctx(label->label, label->len);
132 static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
135 return server->attr_bitmask;
137 return server->attr_bitmask_nl;
140 static inline struct nfs4_label *
141 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
142 struct iattr *sattr, struct nfs4_label *l)
145 nfs4_label_release_security(struct nfs4_label *label)
148 nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
149 { return server->attr_bitmask; }
152 /* Prevent leaks of NFSv4 errors into userland */
153 static int nfs4_map_errors(int err)
158 case -NFS4ERR_RESOURCE:
159 case -NFS4ERR_LAYOUTTRYLATER:
160 case -NFS4ERR_RECALLCONFLICT:
162 case -NFS4ERR_WRONGSEC:
163 case -NFS4ERR_WRONG_CRED:
165 case -NFS4ERR_BADOWNER:
166 case -NFS4ERR_BADNAME:
168 case -NFS4ERR_SHARE_DENIED:
170 case -NFS4ERR_MINOR_VERS_MISMATCH:
171 return -EPROTONOSUPPORT;
172 case -NFS4ERR_FILE_OPEN:
175 dprintk("%s could not handle NFSv4 error %d\n",
183 * This is our standard bitmap for GETATTR requests.
185 const u32 nfs4_fattr_bitmap[3] = {
187 | FATTR4_WORD0_CHANGE
190 | FATTR4_WORD0_FILEID,
192 | FATTR4_WORD1_NUMLINKS
194 | FATTR4_WORD1_OWNER_GROUP
195 | FATTR4_WORD1_RAWDEV
196 | FATTR4_WORD1_SPACE_USED
197 | FATTR4_WORD1_TIME_ACCESS
198 | FATTR4_WORD1_TIME_METADATA
199 | FATTR4_WORD1_TIME_MODIFY
200 | FATTR4_WORD1_MOUNTED_ON_FILEID,
201 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
202 FATTR4_WORD2_SECURITY_LABEL
206 static const u32 nfs4_pnfs_open_bitmap[3] = {
208 | FATTR4_WORD0_CHANGE
211 | FATTR4_WORD0_FILEID,
213 | FATTR4_WORD1_NUMLINKS
215 | FATTR4_WORD1_OWNER_GROUP
216 | FATTR4_WORD1_RAWDEV
217 | FATTR4_WORD1_SPACE_USED
218 | FATTR4_WORD1_TIME_ACCESS
219 | FATTR4_WORD1_TIME_METADATA
220 | FATTR4_WORD1_TIME_MODIFY,
221 FATTR4_WORD2_MDSTHRESHOLD
222 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
223 | FATTR4_WORD2_SECURITY_LABEL
227 static const u32 nfs4_open_noattr_bitmap[3] = {
229 | FATTR4_WORD0_CHANGE
230 | FATTR4_WORD0_FILEID,
233 const u32 nfs4_statfs_bitmap[3] = {
234 FATTR4_WORD0_FILES_AVAIL
235 | FATTR4_WORD0_FILES_FREE
236 | FATTR4_WORD0_FILES_TOTAL,
237 FATTR4_WORD1_SPACE_AVAIL
238 | FATTR4_WORD1_SPACE_FREE
239 | FATTR4_WORD1_SPACE_TOTAL
242 const u32 nfs4_pathconf_bitmap[3] = {
244 | FATTR4_WORD0_MAXNAME,
248 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
249 | FATTR4_WORD0_MAXREAD
250 | FATTR4_WORD0_MAXWRITE
251 | FATTR4_WORD0_LEASE_TIME,
252 FATTR4_WORD1_TIME_DELTA
253 | FATTR4_WORD1_FS_LAYOUT_TYPES,
254 FATTR4_WORD2_LAYOUT_BLKSIZE
255 | FATTR4_WORD2_CLONE_BLKSIZE
258 const u32 nfs4_fs_locations_bitmap[3] = {
260 | FATTR4_WORD0_CHANGE
263 | FATTR4_WORD0_FILEID
264 | FATTR4_WORD0_FS_LOCATIONS,
266 | FATTR4_WORD1_NUMLINKS
268 | FATTR4_WORD1_OWNER_GROUP
269 | FATTR4_WORD1_RAWDEV
270 | FATTR4_WORD1_SPACE_USED
271 | FATTR4_WORD1_TIME_ACCESS
272 | FATTR4_WORD1_TIME_METADATA
273 | FATTR4_WORD1_TIME_MODIFY
274 | FATTR4_WORD1_MOUNTED_ON_FILEID,
277 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
278 struct nfs4_readdir_arg *readdir)
283 readdir->cookie = cookie;
284 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
289 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
294 * NFSv4 servers do not return entries for '.' and '..'
295 * Therefore, we fake these entries here. We let '.'
296 * have cookie 0 and '..' have cookie 1. Note that
297 * when talking to the server, we always send cookie 0
300 start = p = kmap_atomic(*readdir->pages);
303 *p++ = xdr_one; /* next */
304 *p++ = xdr_zero; /* cookie, first word */
305 *p++ = xdr_one; /* cookie, second word */
306 *p++ = xdr_one; /* entry len */
307 memcpy(p, ".\0\0\0", 4); /* entry */
309 *p++ = xdr_one; /* bitmap length */
310 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
311 *p++ = htonl(8); /* attribute buffer length */
312 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
315 *p++ = xdr_one; /* next */
316 *p++ = xdr_zero; /* cookie, first word */
317 *p++ = xdr_two; /* cookie, second word */
318 *p++ = xdr_two; /* entry len */
319 memcpy(p, "..\0\0", 4); /* entry */
321 *p++ = xdr_one; /* bitmap length */
322 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
323 *p++ = htonl(8); /* attribute buffer length */
324 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
326 readdir->pgbase = (char *)p - (char *)start;
327 readdir->count -= readdir->pgbase;
328 kunmap_atomic(start);
331 static long nfs4_update_delay(long *timeout)
335 return NFS4_POLL_RETRY_MAX;
337 *timeout = NFS4_POLL_RETRY_MIN;
338 if (*timeout > NFS4_POLL_RETRY_MAX)
339 *timeout = NFS4_POLL_RETRY_MAX;
345 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
351 freezable_schedule_timeout_killable_unsafe(
352 nfs4_update_delay(timeout));
353 if (fatal_signal_pending(current))
358 /* This is the error handling routine for processes that are allowed
361 static int nfs4_do_handle_exception(struct nfs_server *server,
362 int errorcode, struct nfs4_exception *exception)
364 struct nfs_client *clp = server->nfs_client;
365 struct nfs4_state *state = exception->state;
366 const nfs4_stateid *stateid = exception->stateid;
367 struct inode *inode = exception->inode;
370 exception->delay = 0;
371 exception->recovering = 0;
372 exception->retry = 0;
376 case -NFS4ERR_OPENMODE:
377 case -NFS4ERR_DELEG_REVOKED:
378 case -NFS4ERR_ADMIN_REVOKED:
379 case -NFS4ERR_BAD_STATEID:
383 err = nfs_async_inode_return_delegation(inode,
386 goto wait_on_recovery;
387 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
388 exception->retry = 1;
394 ret = nfs4_schedule_stateid_recovery(server, state);
397 goto wait_on_recovery;
398 case -NFS4ERR_EXPIRED:
400 ret = nfs4_schedule_stateid_recovery(server, state);
404 case -NFS4ERR_STALE_STATEID:
405 case -NFS4ERR_STALE_CLIENTID:
406 nfs4_schedule_lease_recovery(clp);
407 goto wait_on_recovery;
409 ret = nfs4_schedule_migration_recovery(server);
412 goto wait_on_recovery;
413 case -NFS4ERR_LEASE_MOVED:
414 nfs4_schedule_lease_moved_recovery(clp);
415 goto wait_on_recovery;
416 #if defined(CONFIG_NFS_V4_1)
417 case -NFS4ERR_BADSESSION:
418 case -NFS4ERR_BADSLOT:
419 case -NFS4ERR_BAD_HIGH_SLOT:
420 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
421 case -NFS4ERR_DEADSESSION:
422 case -NFS4ERR_SEQ_FALSE_RETRY:
423 case -NFS4ERR_SEQ_MISORDERED:
424 dprintk("%s ERROR: %d Reset session\n", __func__,
426 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
427 goto wait_on_recovery;
428 #endif /* defined(CONFIG_NFS_V4_1) */
429 case -NFS4ERR_FILE_OPEN:
430 if (exception->timeout > HZ) {
431 /* We have retried a decent amount, time to
438 nfs_inc_server_stats(server, NFSIOS_DELAY);
440 case -NFS4ERR_LAYOUTTRYLATER:
441 case -NFS4ERR_RECALLCONFLICT:
442 exception->delay = 1;
445 case -NFS4ERR_RETRY_UNCACHED_REP:
446 case -NFS4ERR_OLD_STATEID:
447 exception->retry = 1;
449 case -NFS4ERR_BADOWNER:
450 /* The following works around a Linux server bug! */
451 case -NFS4ERR_BADNAME:
452 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
453 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
454 exception->retry = 1;
455 printk(KERN_WARNING "NFS: v4 server %s "
456 "does not accept raw "
458 "Reenabling the idmapper.\n",
459 server->nfs_client->cl_hostname);
462 /* We failed to handle the error */
463 return nfs4_map_errors(ret);
465 exception->recovering = 1;
469 /* This is the error handling routine for processes that are allowed
472 int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
474 struct nfs_client *clp = server->nfs_client;
477 ret = nfs4_do_handle_exception(server, errorcode, exception);
478 if (exception->delay) {
479 ret = nfs4_delay(server->client, &exception->timeout);
482 if (exception->recovering) {
483 ret = nfs4_wait_clnt_recover(clp);
484 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
491 exception->retry = 1;
496 nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
497 int errorcode, struct nfs4_exception *exception)
499 struct nfs_client *clp = server->nfs_client;
502 ret = nfs4_do_handle_exception(server, errorcode, exception);
503 if (exception->delay) {
504 rpc_delay(task, nfs4_update_delay(&exception->timeout));
507 if (exception->recovering) {
508 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
509 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
510 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
513 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
518 exception->retry = 1;
523 nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
524 struct nfs4_state *state, long *timeout)
526 struct nfs4_exception exception = {
530 if (task->tk_status >= 0)
533 exception.timeout = *timeout;
534 task->tk_status = nfs4_async_handle_exception(task, server,
537 if (exception.delay && timeout)
538 *timeout = exception.timeout;
545 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
546 * or 'false' otherwise.
548 static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
550 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
552 if (flavor == RPC_AUTH_GSS_KRB5I ||
553 flavor == RPC_AUTH_GSS_KRB5P)
559 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
561 spin_lock(&clp->cl_lock);
562 if (time_before(clp->cl_last_renewal,timestamp))
563 clp->cl_last_renewal = timestamp;
564 spin_unlock(&clp->cl_lock);
567 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
569 struct nfs_client *clp = server->nfs_client;
571 if (!nfs4_has_session(clp))
572 do_renew_lease(clp, timestamp);
575 struct nfs4_call_sync_data {
576 const struct nfs_server *seq_server;
577 struct nfs4_sequence_args *seq_args;
578 struct nfs4_sequence_res *seq_res;
581 void nfs4_init_sequence(struct nfs4_sequence_args *args,
582 struct nfs4_sequence_res *res, int cache_reply)
584 args->sa_slot = NULL;
585 args->sa_cache_this = cache_reply;
586 args->sa_privileged = 0;
591 static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
593 args->sa_privileged = 1;
596 int nfs40_setup_sequence(struct nfs4_slot_table *tbl,
597 struct nfs4_sequence_args *args,
598 struct nfs4_sequence_res *res,
599 struct rpc_task *task)
601 struct nfs4_slot *slot;
603 /* slot already allocated? */
604 if (res->sr_slot != NULL)
607 spin_lock(&tbl->slot_tbl_lock);
608 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
611 slot = nfs4_alloc_slot(tbl);
613 if (slot == ERR_PTR(-ENOMEM))
614 task->tk_timeout = HZ >> 2;
617 spin_unlock(&tbl->slot_tbl_lock);
619 slot->privileged = args->sa_privileged ? 1 : 0;
620 args->sa_slot = slot;
624 rpc_call_start(task);
628 if (args->sa_privileged)
629 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
630 NULL, RPC_PRIORITY_PRIVILEGED);
632 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
633 spin_unlock(&tbl->slot_tbl_lock);
636 EXPORT_SYMBOL_GPL(nfs40_setup_sequence);
638 static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
640 struct nfs4_slot *slot = res->sr_slot;
641 struct nfs4_slot_table *tbl;
644 spin_lock(&tbl->slot_tbl_lock);
645 if (!nfs41_wake_and_assign_slot(tbl, slot))
646 nfs4_free_slot(tbl, slot);
647 spin_unlock(&tbl->slot_tbl_lock);
652 static int nfs40_sequence_done(struct rpc_task *task,
653 struct nfs4_sequence_res *res)
655 if (res->sr_slot != NULL)
656 nfs40_sequence_free_slot(res);
660 #if defined(CONFIG_NFS_V4_1)
662 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
664 struct nfs4_session *session;
665 struct nfs4_slot_table *tbl;
666 struct nfs4_slot *slot = res->sr_slot;
667 bool send_new_highest_used_slotid = false;
670 session = tbl->session;
672 /* Bump the slot sequence number */
677 spin_lock(&tbl->slot_tbl_lock);
678 /* Be nice to the server: try to ensure that the last transmitted
679 * value for highest_user_slotid <= target_highest_slotid
681 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
682 send_new_highest_used_slotid = true;
684 if (nfs41_wake_and_assign_slot(tbl, slot)) {
685 send_new_highest_used_slotid = false;
688 nfs4_free_slot(tbl, slot);
690 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
691 send_new_highest_used_slotid = false;
693 spin_unlock(&tbl->slot_tbl_lock);
695 if (send_new_highest_used_slotid)
696 nfs41_notify_server(session->clp);
697 if (waitqueue_active(&tbl->slot_waitq))
698 wake_up_all(&tbl->slot_waitq);
701 static int nfs41_sequence_process(struct rpc_task *task,
702 struct nfs4_sequence_res *res)
704 struct nfs4_session *session;
705 struct nfs4_slot *slot = res->sr_slot;
706 struct nfs_client *clp;
707 bool interrupted = false;
712 /* don't increment the sequence number if the task wasn't sent */
713 if (!RPC_WAS_SENT(task))
716 session = slot->table->session;
718 if (slot->interrupted) {
719 slot->interrupted = 0;
723 trace_nfs4_sequence_done(session, res);
724 /* Check the SEQUENCE operation status */
725 switch (res->sr_status) {
727 /* Update the slot's sequence and clientid lease timer */
730 do_renew_lease(clp, res->sr_timestamp);
731 /* Check sequence flags */
732 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
734 nfs41_update_target_slotid(slot->table, slot, res);
738 * sr_status remains 1 if an RPC level error occurred.
739 * The server may or may not have processed the sequence
741 * Mark the slot as having hosted an interrupted RPC call.
743 slot->interrupted = 1;
746 /* The server detected a resend of the RPC call and
747 * returned NFS4ERR_DELAY as per Section 2.10.6.2
750 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
755 case -NFS4ERR_BADSLOT:
757 * The slot id we used was probably retired. Try again
758 * using a different slot id.
761 case -NFS4ERR_SEQ_MISORDERED:
763 * Was the last operation on this sequence interrupted?
764 * If so, retry after bumping the sequence number.
771 * Could this slot have been previously retired?
772 * If so, then the server may be expecting seq_nr = 1!
774 if (slot->seq_nr != 1) {
779 case -NFS4ERR_SEQ_FALSE_RETRY:
783 /* Just update the slot sequence no. */
787 /* The session may be reset by one of the error handlers. */
788 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
792 if (rpc_restart_call_prepare(task)) {
793 nfs41_sequence_free_slot(res);
799 if (!rpc_restart_call(task))
801 rpc_delay(task, NFS4_POLL_RETRY_MAX);
805 int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
807 if (!nfs41_sequence_process(task, res))
809 if (res->sr_slot != NULL)
810 nfs41_sequence_free_slot(res);
814 EXPORT_SYMBOL_GPL(nfs41_sequence_done);
816 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
818 if (res->sr_slot == NULL)
820 if (res->sr_slot->table->session != NULL)
821 return nfs41_sequence_process(task, res);
822 return nfs40_sequence_done(task, res);
825 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
827 if (res->sr_slot != NULL) {
828 if (res->sr_slot->table->session != NULL)
829 nfs41_sequence_free_slot(res);
831 nfs40_sequence_free_slot(res);
835 int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
837 if (res->sr_slot == NULL)
839 if (!res->sr_slot->table->session)
840 return nfs40_sequence_done(task, res);
841 return nfs41_sequence_done(task, res);
843 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
845 int nfs41_setup_sequence(struct nfs4_session *session,
846 struct nfs4_sequence_args *args,
847 struct nfs4_sequence_res *res,
848 struct rpc_task *task)
850 struct nfs4_slot *slot;
851 struct nfs4_slot_table *tbl;
853 dprintk("--> %s\n", __func__);
854 /* slot already allocated? */
855 if (res->sr_slot != NULL)
858 tbl = &session->fc_slot_table;
860 task->tk_timeout = 0;
862 spin_lock(&tbl->slot_tbl_lock);
863 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
864 !args->sa_privileged) {
865 /* The state manager will wait until the slot table is empty */
866 dprintk("%s session is draining\n", __func__);
870 slot = nfs4_alloc_slot(tbl);
872 /* If out of memory, try again in 1/4 second */
873 if (slot == ERR_PTR(-ENOMEM))
874 task->tk_timeout = HZ >> 2;
875 dprintk("<-- %s: no free slots\n", __func__);
878 spin_unlock(&tbl->slot_tbl_lock);
880 slot->privileged = args->sa_privileged ? 1 : 0;
881 args->sa_slot = slot;
883 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
884 slot->slot_nr, slot->seq_nr);
887 res->sr_timestamp = jiffies;
888 res->sr_status_flags = 0;
890 * sr_status is only set in decode_sequence, and so will remain
891 * set to 1 if an rpc level failure occurs.
894 trace_nfs4_setup_sequence(session, args);
896 rpc_call_start(task);
899 /* Privileged tasks are queued with top priority */
900 if (args->sa_privileged)
901 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
902 NULL, RPC_PRIORITY_PRIVILEGED);
904 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
905 spin_unlock(&tbl->slot_tbl_lock);
908 EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
910 static int nfs4_setup_sequence(const struct nfs_server *server,
911 struct nfs4_sequence_args *args,
912 struct nfs4_sequence_res *res,
913 struct rpc_task *task)
915 struct nfs4_session *session = nfs4_get_session(server);
919 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
922 dprintk("--> %s clp %p session %p sr_slot %u\n",
923 __func__, session->clp, session, res->sr_slot ?
924 res->sr_slot->slot_nr : NFS4_NO_SLOT);
926 ret = nfs41_setup_sequence(session, args, res, task);
928 dprintk("<-- %s status=%d\n", __func__, ret);
932 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
934 struct nfs4_call_sync_data *data = calldata;
935 struct nfs4_session *session = nfs4_get_session(data->seq_server);
937 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
939 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
942 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
944 struct nfs4_call_sync_data *data = calldata;
946 nfs41_sequence_done(task, data->seq_res);
949 static const struct rpc_call_ops nfs41_call_sync_ops = {
950 .rpc_call_prepare = nfs41_call_sync_prepare,
951 .rpc_call_done = nfs41_call_sync_done,
954 #else /* !CONFIG_NFS_V4_1 */
956 static int nfs4_setup_sequence(const struct nfs_server *server,
957 struct nfs4_sequence_args *args,
958 struct nfs4_sequence_res *res,
959 struct rpc_task *task)
961 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
965 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
967 return nfs40_sequence_done(task, res);
970 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
972 if (res->sr_slot != NULL)
973 nfs40_sequence_free_slot(res);
976 int nfs4_sequence_done(struct rpc_task *task,
977 struct nfs4_sequence_res *res)
979 return nfs40_sequence_done(task, res);
981 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
983 #endif /* !CONFIG_NFS_V4_1 */
985 static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
987 struct nfs4_call_sync_data *data = calldata;
988 nfs4_setup_sequence(data->seq_server,
989 data->seq_args, data->seq_res, task);
992 static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
994 struct nfs4_call_sync_data *data = calldata;
995 nfs4_sequence_done(task, data->seq_res);
998 static const struct rpc_call_ops nfs40_call_sync_ops = {
999 .rpc_call_prepare = nfs40_call_sync_prepare,
1000 .rpc_call_done = nfs40_call_sync_done,
1003 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
1004 struct nfs_server *server,
1005 struct rpc_message *msg,
1006 struct nfs4_sequence_args *args,
1007 struct nfs4_sequence_res *res)
1010 struct rpc_task *task;
1011 struct nfs_client *clp = server->nfs_client;
1012 struct nfs4_call_sync_data data = {
1013 .seq_server = server,
1017 struct rpc_task_setup task_setup = {
1020 .callback_ops = clp->cl_mvops->call_sync_ops,
1021 .callback_data = &data
1024 task = rpc_run_task(&task_setup);
1026 ret = PTR_ERR(task);
1028 ret = task->tk_status;
1034 int nfs4_call_sync(struct rpc_clnt *clnt,
1035 struct nfs_server *server,
1036 struct rpc_message *msg,
1037 struct nfs4_sequence_args *args,
1038 struct nfs4_sequence_res *res,
1041 nfs4_init_sequence(args, res, cache_reply);
1042 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
1045 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
1047 struct nfs_inode *nfsi = NFS_I(dir);
1049 spin_lock(&dir->i_lock);
1050 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
1051 if (!cinfo->atomic || cinfo->before != dir->i_version)
1052 nfs_force_lookup_revalidate(dir);
1053 dir->i_version = cinfo->after;
1054 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
1055 nfs_fscache_invalidate(dir);
1056 spin_unlock(&dir->i_lock);
1059 struct nfs4_opendata {
1061 struct nfs_openargs o_arg;
1062 struct nfs_openres o_res;
1063 struct nfs_open_confirmargs c_arg;
1064 struct nfs_open_confirmres c_res;
1065 struct nfs4_string owner_name;
1066 struct nfs4_string group_name;
1067 struct nfs4_label *a_label;
1068 struct nfs_fattr f_attr;
1069 struct nfs4_label *f_label;
1071 struct dentry *dentry;
1072 struct nfs4_state_owner *owner;
1073 struct nfs4_state *state;
1075 unsigned long timestamp;
1076 unsigned int rpc_done : 1;
1077 unsigned int file_created : 1;
1078 unsigned int is_recover : 1;
1083 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1084 int err, struct nfs4_exception *exception)
1088 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1090 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1091 exception->retry = 1;
1096 nfs4_map_atomic_open_share(struct nfs_server *server,
1097 fmode_t fmode, int openflags)
1101 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1103 res = NFS4_SHARE_ACCESS_READ;
1106 res = NFS4_SHARE_ACCESS_WRITE;
1108 case FMODE_READ|FMODE_WRITE:
1109 res = NFS4_SHARE_ACCESS_BOTH;
1111 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1113 /* Want no delegation if we're using O_DIRECT */
1114 if (openflags & O_DIRECT)
1115 res |= NFS4_SHARE_WANT_NO_DELEG;
1120 static enum open_claim_type4
1121 nfs4_map_atomic_open_claim(struct nfs_server *server,
1122 enum open_claim_type4 claim)
1124 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1129 case NFS4_OPEN_CLAIM_FH:
1130 return NFS4_OPEN_CLAIM_NULL;
1131 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1132 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1133 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1134 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1138 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
1140 p->o_res.f_attr = &p->f_attr;
1141 p->o_res.f_label = p->f_label;
1142 p->o_res.seqid = p->o_arg.seqid;
1143 p->c_res.seqid = p->c_arg.seqid;
1144 p->o_res.server = p->o_arg.server;
1145 p->o_res.access_request = p->o_arg.access;
1146 nfs_fattr_init(&p->f_attr);
1147 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
1150 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
1151 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
1152 const struct iattr *attrs,
1153 struct nfs4_label *label,
1154 enum open_claim_type4 claim,
1157 struct dentry *parent = dget_parent(dentry);
1158 struct inode *dir = d_inode(parent);
1159 struct nfs_server *server = NFS_SERVER(dir);
1160 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
1161 struct nfs4_opendata *p;
1163 p = kzalloc(sizeof(*p), gfp_mask);
1167 p->f_label = nfs4_label_alloc(server, gfp_mask);
1168 if (IS_ERR(p->f_label))
1171 p->a_label = nfs4_label_alloc(server, gfp_mask);
1172 if (IS_ERR(p->a_label))
1175 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1176 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
1177 if (IS_ERR(p->o_arg.seqid))
1178 goto err_free_label;
1179 nfs_sb_active(dentry->d_sb);
1180 p->dentry = dget(dentry);
1183 atomic_inc(&sp->so_count);
1184 p->o_arg.open_flags = flags;
1185 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
1186 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1188 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1189 * will return permission denied for all bits until close */
1190 if (!(flags & O_EXCL)) {
1191 /* ask server to check for all possible rights as results
1193 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1194 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
1196 p->o_arg.clientid = server->nfs_client->cl_clientid;
1197 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1198 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1199 p->o_arg.name = &dentry->d_name;
1200 p->o_arg.server = server;
1201 p->o_arg.bitmask = nfs4_bitmask(server, label);
1202 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1203 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1204 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1205 switch (p->o_arg.claim) {
1206 case NFS4_OPEN_CLAIM_NULL:
1207 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1208 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1209 p->o_arg.fh = NFS_FH(dir);
1211 case NFS4_OPEN_CLAIM_PREVIOUS:
1212 case NFS4_OPEN_CLAIM_FH:
1213 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1214 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1215 p->o_arg.fh = NFS_FH(d_inode(dentry));
1217 if (attrs != NULL && attrs->ia_valid != 0) {
1220 p->o_arg.u.attrs = &p->attrs;
1221 memcpy(&p->attrs, attrs, sizeof(p->attrs));
1224 verf[1] = current->pid;
1225 memcpy(p->o_arg.u.verifier.data, verf,
1226 sizeof(p->o_arg.u.verifier.data));
1228 p->c_arg.fh = &p->o_res.fh;
1229 p->c_arg.stateid = &p->o_res.stateid;
1230 p->c_arg.seqid = p->o_arg.seqid;
1231 nfs4_init_opendata_res(p);
1232 kref_init(&p->kref);
1236 nfs4_label_free(p->a_label);
1238 nfs4_label_free(p->f_label);
1246 static void nfs4_opendata_free(struct kref *kref)
1248 struct nfs4_opendata *p = container_of(kref,
1249 struct nfs4_opendata, kref);
1250 struct super_block *sb = p->dentry->d_sb;
1252 nfs_free_seqid(p->o_arg.seqid);
1253 nfs4_sequence_free_slot(&p->o_res.seq_res);
1254 if (p->state != NULL)
1255 nfs4_put_open_state(p->state);
1256 nfs4_put_state_owner(p->owner);
1258 nfs4_label_free(p->a_label);
1259 nfs4_label_free(p->f_label);
1263 nfs_sb_deactive(sb);
1264 nfs_fattr_free_names(&p->f_attr);
1265 kfree(p->f_attr.mdsthreshold);
1269 static void nfs4_opendata_put(struct nfs4_opendata *p)
1272 kref_put(&p->kref, nfs4_opendata_free);
1275 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1279 ret = rpc_wait_for_completion_task(task);
1283 static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1286 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1287 case FMODE_READ|FMODE_WRITE:
1288 return state->n_rdwr != 0;
1290 return state->n_wronly != 0;
1292 return state->n_rdonly != 0;
1298 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
1302 if (open_mode & (O_EXCL|O_TRUNC))
1304 switch (mode & (FMODE_READ|FMODE_WRITE)) {
1306 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1307 && state->n_rdonly != 0;
1310 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1311 && state->n_wronly != 0;
1313 case FMODE_READ|FMODE_WRITE:
1314 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1315 && state->n_rdwr != 0;
1321 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1322 enum open_claim_type4 claim)
1324 if (delegation == NULL)
1326 if ((delegation->type & fmode) != fmode)
1328 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1331 case NFS4_OPEN_CLAIM_NULL:
1332 case NFS4_OPEN_CLAIM_FH:
1334 case NFS4_OPEN_CLAIM_PREVIOUS:
1335 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1340 nfs_mark_delegation_referenced(delegation);
1344 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1353 case FMODE_READ|FMODE_WRITE:
1356 nfs4_state_set_mode_locked(state, state->state | fmode);
1359 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
1361 struct nfs_client *clp = state->owner->so_server->nfs_client;
1362 bool need_recover = false;
1364 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1365 need_recover = true;
1366 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1367 need_recover = true;
1368 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1369 need_recover = true;
1371 nfs4_state_mark_reclaim_nograce(clp, state);
1374 static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1375 nfs4_stateid *stateid)
1377 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1379 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1380 nfs_test_and_clear_all_open_stateid(state);
1383 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1388 static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1390 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1392 if (state->n_wronly)
1393 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1394 if (state->n_rdonly)
1395 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1397 set_bit(NFS_O_RDWR_STATE, &state->flags);
1398 set_bit(NFS_OPEN_STATE, &state->flags);
1401 static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1402 nfs4_stateid *arg_stateid,
1403 nfs4_stateid *stateid, fmode_t fmode)
1405 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1406 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1408 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1411 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1414 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1415 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1416 clear_bit(NFS_OPEN_STATE, &state->flags);
1418 if (stateid == NULL)
1420 /* Handle races with OPEN */
1421 if (!nfs4_stateid_match_other(arg_stateid, &state->open_stateid) ||
1422 (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1423 !nfs4_stateid_is_newer(stateid, &state->open_stateid))) {
1424 nfs_resync_open_stateid_locked(state);
1427 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1428 nfs4_stateid_copy(&state->stateid, stateid);
1429 nfs4_stateid_copy(&state->open_stateid, stateid);
1432 static void nfs_clear_open_stateid(struct nfs4_state *state,
1433 nfs4_stateid *arg_stateid,
1434 nfs4_stateid *stateid, fmode_t fmode)
1436 write_seqlock(&state->seqlock);
1437 nfs_clear_open_stateid_locked(state, arg_stateid, stateid, fmode);
1438 write_sequnlock(&state->seqlock);
1439 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1440 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1443 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1447 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1450 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1452 case FMODE_READ|FMODE_WRITE:
1453 set_bit(NFS_O_RDWR_STATE, &state->flags);
1455 if (!nfs_need_update_open_stateid(state, stateid))
1457 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1458 nfs4_stateid_copy(&state->stateid, stateid);
1459 nfs4_stateid_copy(&state->open_stateid, stateid);
1462 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
1465 * Protect the call to nfs4_state_set_mode_locked and
1466 * serialise the stateid update
1468 spin_lock(&state->owner->so_lock);
1469 write_seqlock(&state->seqlock);
1470 if (deleg_stateid != NULL) {
1471 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1472 set_bit(NFS_DELEGATED_STATE, &state->flags);
1474 if (open_stateid != NULL)
1475 nfs_set_open_stateid_locked(state, open_stateid, fmode);
1476 write_sequnlock(&state->seqlock);
1477 update_open_stateflags(state, fmode);
1478 spin_unlock(&state->owner->so_lock);
1481 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
1483 struct nfs_inode *nfsi = NFS_I(state->inode);
1484 struct nfs_delegation *deleg_cur;
1487 fmode &= (FMODE_READ|FMODE_WRITE);
1490 deleg_cur = rcu_dereference(nfsi->delegation);
1491 if (deleg_cur == NULL)
1494 spin_lock(&deleg_cur->lock);
1495 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1496 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1497 (deleg_cur->type & fmode) != fmode)
1498 goto no_delegation_unlock;
1500 if (delegation == NULL)
1501 delegation = &deleg_cur->stateid;
1502 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
1503 goto no_delegation_unlock;
1505 nfs_mark_delegation_referenced(deleg_cur);
1506 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
1508 no_delegation_unlock:
1509 spin_unlock(&deleg_cur->lock);
1513 if (!ret && open_stateid != NULL) {
1514 __update_open_stateid(state, open_stateid, NULL, fmode);
1517 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1518 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1523 static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1524 const nfs4_stateid *stateid)
1526 struct nfs4_state *state = lsp->ls_state;
1529 spin_lock(&state->state_lock);
1530 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1532 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1534 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1537 spin_unlock(&state->state_lock);
1541 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1543 struct nfs_delegation *delegation;
1546 delegation = rcu_dereference(NFS_I(inode)->delegation);
1547 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1552 nfs4_inode_return_delegation(inode);
1555 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1557 struct nfs4_state *state = opendata->state;
1558 struct nfs_inode *nfsi = NFS_I(state->inode);
1559 struct nfs_delegation *delegation;
1560 int open_mode = opendata->o_arg.open_flags;
1561 fmode_t fmode = opendata->o_arg.fmode;
1562 enum open_claim_type4 claim = opendata->o_arg.claim;
1563 nfs4_stateid stateid;
1567 spin_lock(&state->owner->so_lock);
1568 if (can_open_cached(state, fmode, open_mode)) {
1569 update_open_stateflags(state, fmode);
1570 spin_unlock(&state->owner->so_lock);
1571 goto out_return_state;
1573 spin_unlock(&state->owner->so_lock);
1575 delegation = rcu_dereference(nfsi->delegation);
1576 if (!can_open_delegated(delegation, fmode, claim)) {
1580 /* Save the delegation */
1581 nfs4_stateid_copy(&stateid, &delegation->stateid);
1583 nfs_release_seqid(opendata->o_arg.seqid);
1584 if (!opendata->is_recover) {
1585 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1591 /* Try to update the stateid using the delegation */
1592 if (update_open_stateid(state, NULL, &stateid, fmode))
1593 goto out_return_state;
1596 return ERR_PTR(ret);
1598 atomic_inc(&state->count);
1603 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1605 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1606 struct nfs_delegation *delegation;
1607 int delegation_flags = 0;
1610 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1612 delegation_flags = delegation->flags;
1614 switch (data->o_arg.claim) {
1617 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1618 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1619 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1620 "returning a delegation for "
1621 "OPEN(CLAIM_DELEGATE_CUR)\n",
1625 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1626 nfs_inode_set_delegation(state->inode,
1627 data->owner->so_cred,
1630 nfs_inode_reclaim_delegation(state->inode,
1631 data->owner->so_cred,
1636 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1637 * and update the nfs4_state.
1639 static struct nfs4_state *
1640 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1642 struct inode *inode = data->state->inode;
1643 struct nfs4_state *state = data->state;
1646 if (!data->rpc_done) {
1647 if (data->rpc_status) {
1648 ret = data->rpc_status;
1651 /* cached opens have already been processed */
1655 ret = nfs_refresh_inode(inode, &data->f_attr);
1659 if (data->o_res.delegation_type != 0)
1660 nfs4_opendata_check_deleg(data, state);
1662 update_open_stateid(state, &data->o_res.stateid, NULL,
1664 atomic_inc(&state->count);
1668 return ERR_PTR(ret);
1672 static struct nfs4_state *
1673 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1675 struct inode *inode;
1676 struct nfs4_state *state = NULL;
1679 if (!data->rpc_done) {
1680 state = nfs4_try_open_cached(data);
1681 trace_nfs4_cached_open(data->state);
1686 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1688 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
1689 ret = PTR_ERR(inode);
1693 state = nfs4_get_open_state(inode, data->owner);
1696 if (data->o_res.delegation_type != 0)
1697 nfs4_opendata_check_deleg(data, state);
1698 update_open_stateid(state, &data->o_res.stateid, NULL,
1702 nfs_release_seqid(data->o_arg.seqid);
1707 return ERR_PTR(ret);
1710 static struct nfs4_state *
1711 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1713 struct nfs4_state *ret;
1715 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1716 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1718 ret = _nfs4_opendata_to_nfs4_state(data);
1719 nfs4_sequence_free_slot(&data->o_res.seq_res);
1723 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1725 struct nfs_inode *nfsi = NFS_I(state->inode);
1726 struct nfs_open_context *ctx;
1728 spin_lock(&state->inode->i_lock);
1729 list_for_each_entry(ctx, &nfsi->open_files, list) {
1730 if (ctx->state != state)
1732 get_nfs_open_context(ctx);
1733 spin_unlock(&state->inode->i_lock);
1736 spin_unlock(&state->inode->i_lock);
1737 return ERR_PTR(-ENOENT);
1740 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1741 struct nfs4_state *state, enum open_claim_type4 claim)
1743 struct nfs4_opendata *opendata;
1745 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
1746 NULL, NULL, claim, GFP_NOFS);
1747 if (opendata == NULL)
1748 return ERR_PTR(-ENOMEM);
1749 opendata->state = state;
1750 atomic_inc(&state->count);
1754 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1757 struct nfs4_state *newstate;
1760 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
1762 opendata->o_arg.open_flags = 0;
1763 opendata->o_arg.fmode = fmode;
1764 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1765 NFS_SB(opendata->dentry->d_sb),
1767 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1768 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1769 nfs4_init_opendata_res(opendata);
1770 ret = _nfs4_recover_proc_open(opendata);
1773 newstate = nfs4_opendata_to_nfs4_state(opendata);
1774 if (IS_ERR(newstate))
1775 return PTR_ERR(newstate);
1776 if (newstate != opendata->state)
1778 nfs4_close_state(newstate, fmode);
1782 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1786 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1787 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1788 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1789 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1790 /* memory barrier prior to reading state->n_* */
1791 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1792 clear_bit(NFS_OPEN_STATE, &state->flags);
1794 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1797 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1800 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1804 * We may have performed cached opens for all three recoveries.
1805 * Check if we need to update the current stateid.
1807 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1808 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
1809 write_seqlock(&state->seqlock);
1810 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1811 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1812 write_sequnlock(&state->seqlock);
1819 * reclaim state on the server after a reboot.
1821 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1823 struct nfs_delegation *delegation;
1824 struct nfs4_opendata *opendata;
1825 fmode_t delegation_type = 0;
1828 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1829 NFS4_OPEN_CLAIM_PREVIOUS);
1830 if (IS_ERR(opendata))
1831 return PTR_ERR(opendata);
1833 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1834 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1835 delegation_type = delegation->type;
1837 opendata->o_arg.u.delegation_type = delegation_type;
1838 status = nfs4_open_recover(opendata, state);
1839 nfs4_opendata_put(opendata);
1843 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1845 struct nfs_server *server = NFS_SERVER(state->inode);
1846 struct nfs4_exception exception = { };
1849 err = _nfs4_do_open_reclaim(ctx, state);
1850 trace_nfs4_open_reclaim(ctx, 0, err);
1851 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1853 if (err != -NFS4ERR_DELAY)
1855 nfs4_handle_exception(server, err, &exception);
1856 } while (exception.retry);
1860 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1862 struct nfs_open_context *ctx;
1865 ctx = nfs4_state_find_open_context(state);
1868 ret = nfs4_do_open_reclaim(ctx, state);
1869 put_nfs_open_context(ctx);
1873 static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, int err)
1877 printk(KERN_ERR "NFS: %s: unhandled error "
1878 "%d.\n", __func__, err);
1884 case -NFS4ERR_BADSESSION:
1885 case -NFS4ERR_BADSLOT:
1886 case -NFS4ERR_BAD_HIGH_SLOT:
1887 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1888 case -NFS4ERR_DEADSESSION:
1889 set_bit(NFS_DELEGATED_STATE, &state->flags);
1890 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1892 case -NFS4ERR_STALE_CLIENTID:
1893 case -NFS4ERR_STALE_STATEID:
1894 set_bit(NFS_DELEGATED_STATE, &state->flags);
1895 case -NFS4ERR_EXPIRED:
1896 /* Don't recall a delegation if it was lost */
1897 nfs4_schedule_lease_recovery(server->nfs_client);
1899 case -NFS4ERR_MOVED:
1900 nfs4_schedule_migration_recovery(server);
1902 case -NFS4ERR_LEASE_MOVED:
1903 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1905 case -NFS4ERR_DELEG_REVOKED:
1906 case -NFS4ERR_ADMIN_REVOKED:
1907 case -NFS4ERR_BAD_STATEID:
1908 case -NFS4ERR_OPENMODE:
1909 nfs_inode_find_state_and_recover(state->inode,
1911 nfs4_schedule_stateid_recovery(server, state);
1913 case -NFS4ERR_DELAY:
1914 case -NFS4ERR_GRACE:
1915 set_bit(NFS_DELEGATED_STATE, &state->flags);
1919 case -NFS4ERR_DENIED:
1920 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1926 int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
1927 struct nfs4_state *state, const nfs4_stateid *stateid,
1930 struct nfs_server *server = NFS_SERVER(state->inode);
1931 struct nfs4_opendata *opendata;
1934 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1935 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1936 if (IS_ERR(opendata))
1937 return PTR_ERR(opendata);
1938 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1939 write_seqlock(&state->seqlock);
1940 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1941 write_sequnlock(&state->seqlock);
1942 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1943 switch (type & (FMODE_READ|FMODE_WRITE)) {
1944 case FMODE_READ|FMODE_WRITE:
1946 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1949 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1953 err = nfs4_open_recover_helper(opendata, FMODE_READ);
1955 nfs4_opendata_put(opendata);
1956 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1959 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1961 struct nfs4_opendata *data = calldata;
1963 nfs40_setup_sequence(data->o_arg.server->nfs_client->cl_slot_tbl,
1964 &data->c_arg.seq_args, &data->c_res.seq_res, task);
1967 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1969 struct nfs4_opendata *data = calldata;
1971 nfs40_sequence_done(task, &data->c_res.seq_res);
1973 data->rpc_status = task->tk_status;
1974 if (data->rpc_status == 0) {
1975 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
1976 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1977 renew_lease(data->o_res.server, data->timestamp);
1982 static void nfs4_open_confirm_release(void *calldata)
1984 struct nfs4_opendata *data = calldata;
1985 struct nfs4_state *state = NULL;
1987 /* If this request hasn't been cancelled, do nothing */
1988 if (data->cancelled == 0)
1990 /* In case of error, no cleanup! */
1991 if (!data->rpc_done)
1993 state = nfs4_opendata_to_nfs4_state(data);
1995 nfs4_close_state(state, data->o_arg.fmode);
1997 nfs4_opendata_put(data);
2000 static const struct rpc_call_ops nfs4_open_confirm_ops = {
2001 .rpc_call_prepare = nfs4_open_confirm_prepare,
2002 .rpc_call_done = nfs4_open_confirm_done,
2003 .rpc_release = nfs4_open_confirm_release,
2007 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2009 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2011 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
2012 struct rpc_task *task;
2013 struct rpc_message msg = {
2014 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2015 .rpc_argp = &data->c_arg,
2016 .rpc_resp = &data->c_res,
2017 .rpc_cred = data->owner->so_cred,
2019 struct rpc_task_setup task_setup_data = {
2020 .rpc_client = server->client,
2021 .rpc_message = &msg,
2022 .callback_ops = &nfs4_open_confirm_ops,
2023 .callback_data = data,
2024 .workqueue = nfsiod_workqueue,
2025 .flags = RPC_TASK_ASYNC,
2029 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
2030 kref_get(&data->kref);
2032 data->rpc_status = 0;
2033 data->timestamp = jiffies;
2034 if (data->is_recover)
2035 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
2036 task = rpc_run_task(&task_setup_data);
2038 return PTR_ERR(task);
2039 status = nfs4_wait_for_completion_rpc_task(task);
2041 data->cancelled = 1;
2044 status = data->rpc_status;
2049 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
2051 struct nfs4_opendata *data = calldata;
2052 struct nfs4_state_owner *sp = data->owner;
2053 struct nfs_client *clp = sp->so_server->nfs_client;
2054 enum open_claim_type4 claim = data->o_arg.claim;
2056 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
2059 * Check if we still need to send an OPEN call, or if we can use
2060 * a delegation instead.
2062 if (data->state != NULL) {
2063 struct nfs_delegation *delegation;
2065 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
2068 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
2069 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
2070 goto unlock_no_action;
2073 /* Update client id. */
2074 data->o_arg.clientid = clp->cl_clientid;
2078 case NFS4_OPEN_CLAIM_PREVIOUS:
2079 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2080 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
2081 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
2082 case NFS4_OPEN_CLAIM_FH:
2083 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
2084 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
2086 data->timestamp = jiffies;
2087 if (nfs4_setup_sequence(data->o_arg.server,
2088 &data->o_arg.seq_args,
2089 &data->o_res.seq_res,
2091 nfs_release_seqid(data->o_arg.seqid);
2093 /* Set the create mode (note dependency on the session type) */
2094 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2095 if (data->o_arg.open_flags & O_EXCL) {
2096 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2097 if (nfs4_has_persistent_session(clp))
2098 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2099 else if (clp->cl_mvops->minor_version > 0)
2100 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2104 trace_nfs4_cached_open(data->state);
2107 task->tk_action = NULL;
2109 nfs4_sequence_done(task, &data->o_res.seq_res);
2112 static void nfs4_open_done(struct rpc_task *task, void *calldata)
2114 struct nfs4_opendata *data = calldata;
2116 data->rpc_status = task->tk_status;
2118 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
2121 if (task->tk_status == 0) {
2122 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2123 switch (data->o_res.f_attr->mode & S_IFMT) {
2127 data->rpc_status = -ELOOP;
2130 data->rpc_status = -EISDIR;
2133 data->rpc_status = -ENOTDIR;
2136 renew_lease(data->o_res.server, data->timestamp);
2137 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2138 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2143 static void nfs4_open_release(void *calldata)
2145 struct nfs4_opendata *data = calldata;
2146 struct nfs4_state *state = NULL;
2148 /* If this request hasn't been cancelled, do nothing */
2149 if (data->cancelled == 0)
2151 /* In case of error, no cleanup! */
2152 if (data->rpc_status != 0 || !data->rpc_done)
2154 /* In case we need an open_confirm, no cleanup! */
2155 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2157 state = nfs4_opendata_to_nfs4_state(data);
2159 nfs4_close_state(state, data->o_arg.fmode);
2161 nfs4_opendata_put(data);
2164 static const struct rpc_call_ops nfs4_open_ops = {
2165 .rpc_call_prepare = nfs4_open_prepare,
2166 .rpc_call_done = nfs4_open_done,
2167 .rpc_release = nfs4_open_release,
2170 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
2172 struct inode *dir = d_inode(data->dir);
2173 struct nfs_server *server = NFS_SERVER(dir);
2174 struct nfs_openargs *o_arg = &data->o_arg;
2175 struct nfs_openres *o_res = &data->o_res;
2176 struct rpc_task *task;
2177 struct rpc_message msg = {
2178 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2181 .rpc_cred = data->owner->so_cred,
2183 struct rpc_task_setup task_setup_data = {
2184 .rpc_client = server->client,
2185 .rpc_message = &msg,
2186 .callback_ops = &nfs4_open_ops,
2187 .callback_data = data,
2188 .workqueue = nfsiod_workqueue,
2189 .flags = RPC_TASK_ASYNC,
2193 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
2194 kref_get(&data->kref);
2196 data->rpc_status = 0;
2197 data->cancelled = 0;
2198 data->is_recover = 0;
2200 nfs4_set_sequence_privileged(&o_arg->seq_args);
2201 data->is_recover = 1;
2203 task = rpc_run_task(&task_setup_data);
2205 return PTR_ERR(task);
2206 status = nfs4_wait_for_completion_rpc_task(task);
2208 data->cancelled = 1;
2211 status = data->rpc_status;
2217 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2219 struct inode *dir = d_inode(data->dir);
2220 struct nfs_openres *o_res = &data->o_res;
2223 status = nfs4_run_open_task(data, 1);
2224 if (status != 0 || !data->rpc_done)
2227 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2229 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2230 status = _nfs4_proc_open_confirm(data);
2239 * Additional permission checks in order to distinguish between an
2240 * open for read, and an open for execute. This works around the
2241 * fact that NFSv4 OPEN treats read and execute permissions as being
2243 * Note that in the non-execute case, we want to turn off permission
2244 * checking if we just created a new file (POSIX open() semantics).
2246 static int nfs4_opendata_access(struct rpc_cred *cred,
2247 struct nfs4_opendata *opendata,
2248 struct nfs4_state *state, fmode_t fmode,
2251 struct nfs_access_entry cache;
2254 /* access call failed or for some reason the server doesn't
2255 * support any access modes -- defer access call until later */
2256 if (opendata->o_res.access_supported == 0)
2261 * Use openflags to check for exec, because fmode won't
2262 * always have FMODE_EXEC set when file open for exec.
2264 if (openflags & __FMODE_EXEC) {
2265 /* ONLY check for exec rights */
2267 } else if ((fmode & FMODE_READ) && !opendata->file_created)
2271 cache.jiffies = jiffies;
2272 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2273 nfs_access_add_cache(state->inode, &cache);
2275 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
2278 /* even though OPEN succeeded, access is denied. Close the file */
2279 nfs4_close_state(state, fmode);
2284 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2286 static int _nfs4_proc_open(struct nfs4_opendata *data)
2288 struct inode *dir = d_inode(data->dir);
2289 struct nfs_server *server = NFS_SERVER(dir);
2290 struct nfs_openargs *o_arg = &data->o_arg;
2291 struct nfs_openres *o_res = &data->o_res;
2294 status = nfs4_run_open_task(data, 0);
2295 if (!data->rpc_done)
2298 if (status == -NFS4ERR_BADNAME &&
2299 !(o_arg->open_flags & O_CREAT))
2304 nfs_fattr_map_and_free_names(server, &data->f_attr);
2306 if (o_arg->open_flags & O_CREAT) {
2307 update_changeattr(dir, &o_res->cinfo);
2308 if (o_arg->open_flags & O_EXCL)
2309 data->file_created = 1;
2310 else if (o_res->cinfo.before != o_res->cinfo.after)
2311 data->file_created = 1;
2313 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2314 server->caps &= ~NFS_CAP_POSIX_LOCK;
2315 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2316 status = _nfs4_proc_open_confirm(data);
2320 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
2321 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
2325 static int nfs4_recover_expired_lease(struct nfs_server *server)
2327 return nfs4_client_recover_expired_lease(server->nfs_client);
2332 * reclaim state on the server after a network partition.
2333 * Assumes caller holds the appropriate lock
2335 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2337 struct nfs4_opendata *opendata;
2340 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2341 NFS4_OPEN_CLAIM_FH);
2342 if (IS_ERR(opendata))
2343 return PTR_ERR(opendata);
2344 ret = nfs4_open_recover(opendata, state);
2346 d_drop(ctx->dentry);
2347 nfs4_opendata_put(opendata);
2351 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2353 struct nfs_server *server = NFS_SERVER(state->inode);
2354 struct nfs4_exception exception = { };
2358 err = _nfs4_open_expired(ctx, state);
2359 trace_nfs4_open_expired(ctx, 0, err);
2360 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2365 case -NFS4ERR_GRACE:
2366 case -NFS4ERR_DELAY:
2367 nfs4_handle_exception(server, err, &exception);
2370 } while (exception.retry);
2375 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2377 struct nfs_open_context *ctx;
2380 ctx = nfs4_state_find_open_context(state);
2383 ret = nfs4_do_open_expired(ctx, state);
2384 put_nfs_open_context(ctx);
2388 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state)
2390 nfs_remove_bad_delegation(state->inode);
2391 write_seqlock(&state->seqlock);
2392 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2393 write_sequnlock(&state->seqlock);
2394 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2397 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2399 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2400 nfs_finish_clear_delegation_stateid(state);
2403 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2405 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2406 nfs40_clear_delegation_stateid(state);
2407 return nfs4_open_expired(sp, state);
2410 #if defined(CONFIG_NFS_V4_1)
2411 static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2412 nfs4_stateid *stateid,
2413 struct rpc_cred *cred)
2417 status = nfs41_test_stateid(server, stateid, cred);
2420 case -NFS4ERR_EXPIRED:
2421 case -NFS4ERR_ADMIN_REVOKED:
2422 case -NFS4ERR_DELEG_REVOKED:
2423 /* Ack the revoked state to the server */
2424 nfs41_free_stateid(server, stateid, cred);
2425 case -NFS4ERR_BAD_STATEID:
2431 static void nfs41_check_delegation_stateid(struct nfs4_state *state)
2433 struct nfs_server *server = NFS_SERVER(state->inode);
2434 nfs4_stateid stateid;
2435 struct nfs_delegation *delegation;
2436 struct rpc_cred *cred;
2439 /* Get the delegation credential for use by test/free_stateid */
2441 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2442 if (delegation == NULL) {
2446 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
2448 nfs_finish_clear_delegation_stateid(state);
2452 nfs4_stateid_copy(&stateid, &delegation->stateid);
2453 cred = get_rpccred(delegation->cred);
2455 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
2456 trace_nfs4_test_delegation_stateid(state, NULL, status);
2457 if (status != NFS_OK)
2458 nfs_finish_clear_delegation_stateid(state);
2464 * nfs41_check_open_stateid - possibly free an open stateid
2466 * @state: NFSv4 state for an inode
2468 * Returns NFS_OK if recovery for this stateid is now finished.
2469 * Otherwise a negative NFS4ERR value is returned.
2471 static int nfs41_check_open_stateid(struct nfs4_state *state)
2473 struct nfs_server *server = NFS_SERVER(state->inode);
2474 nfs4_stateid *stateid = &state->open_stateid;
2475 struct rpc_cred *cred = state->owner->so_cred;
2478 /* If a state reset has been done, test_stateid is unneeded */
2479 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2480 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2481 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2482 return -NFS4ERR_BAD_STATEID;
2484 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
2485 trace_nfs4_test_open_stateid(state, NULL, status);
2486 if (status != NFS_OK) {
2487 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2488 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2489 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2490 clear_bit(NFS_OPEN_STATE, &state->flags);
2495 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2499 nfs41_check_delegation_stateid(state);
2500 status = nfs41_check_open_stateid(state);
2501 if (status != NFS_OK)
2502 status = nfs4_open_expired(sp, state);
2508 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2509 * fields corresponding to attributes that were used to store the verifier.
2510 * Make sure we clobber those fields in the later setattr call
2512 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2513 struct iattr *sattr, struct nfs4_label **label)
2515 const u32 *attrset = opendata->o_res.attrset;
2517 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2518 !(sattr->ia_valid & ATTR_ATIME_SET))
2519 sattr->ia_valid |= ATTR_ATIME;
2521 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2522 !(sattr->ia_valid & ATTR_MTIME_SET))
2523 sattr->ia_valid |= ATTR_MTIME;
2525 /* Except MODE, it seems harmless of setting twice. */
2526 if ((attrset[1] & FATTR4_WORD1_MODE))
2527 sattr->ia_valid &= ~ATTR_MODE;
2529 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2533 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2536 struct nfs_open_context *ctx)
2538 struct nfs4_state_owner *sp = opendata->owner;
2539 struct nfs_server *server = sp->so_server;
2540 struct dentry *dentry;
2541 struct nfs4_state *state;
2545 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2547 ret = _nfs4_proc_open(opendata);
2551 state = nfs4_opendata_to_nfs4_state(opendata);
2552 ret = PTR_ERR(state);
2555 if (server->caps & NFS_CAP_POSIX_LOCK)
2556 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2557 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2558 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
2560 dentry = opendata->dentry;
2561 if (d_really_is_negative(dentry)) {
2562 struct dentry *alias;
2564 alias = d_exact_alias(dentry, state->inode);
2566 alias = d_splice_alias(igrab(state->inode), dentry);
2567 /* d_splice_alias() can't fail here - it's a non-directory */
2570 ctx->dentry = dentry = alias;
2572 nfs_set_verifier(dentry,
2573 nfs_save_change_attribute(d_inode(opendata->dir)));
2576 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2581 if (d_inode(dentry) == state->inode) {
2582 nfs_inode_attach_open_context(ctx);
2583 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2584 nfs4_schedule_stateid_recovery(server, state);
2591 * Returns a referenced nfs4_state
2593 static int _nfs4_do_open(struct inode *dir,
2594 struct nfs_open_context *ctx,
2596 struct iattr *sattr,
2597 struct nfs4_label *label,
2600 struct nfs4_state_owner *sp;
2601 struct nfs4_state *state = NULL;
2602 struct nfs_server *server = NFS_SERVER(dir);
2603 struct nfs4_opendata *opendata;
2604 struct dentry *dentry = ctx->dentry;
2605 struct rpc_cred *cred = ctx->cred;
2606 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2607 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
2608 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
2609 struct nfs4_label *olabel = NULL;
2612 /* Protect against reboot recovery conflicts */
2614 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2616 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2619 status = nfs4_recover_expired_lease(server);
2621 goto err_put_state_owner;
2622 if (d_really_is_positive(dentry))
2623 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
2625 if (d_really_is_positive(dentry))
2626 claim = NFS4_OPEN_CLAIM_FH;
2627 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
2628 label, claim, GFP_KERNEL);
2629 if (opendata == NULL)
2630 goto err_put_state_owner;
2633 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2634 if (IS_ERR(olabel)) {
2635 status = PTR_ERR(olabel);
2636 goto err_opendata_put;
2640 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2641 if (!opendata->f_attr.mdsthreshold) {
2642 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2643 if (!opendata->f_attr.mdsthreshold)
2644 goto err_free_label;
2646 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
2648 if (d_really_is_positive(dentry))
2649 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
2651 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
2653 goto err_free_label;
2656 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
2657 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
2658 nfs4_exclusive_attrset(opendata, sattr, &label);
2660 * send create attributes which was not set by open
2661 * with an extra setattr.
2663 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2664 nfs_fattr_init(opendata->o_res.f_attr);
2665 status = nfs4_do_setattr(state->inode, cred,
2666 opendata->o_res.f_attr, sattr,
2667 state, label, olabel);
2669 nfs_setattr_update_inode(state->inode, sattr,
2670 opendata->o_res.f_attr);
2671 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2675 if (opened && opendata->file_created)
2676 *opened |= FILE_CREATED;
2678 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
2679 *ctx_th = opendata->f_attr.mdsthreshold;
2680 opendata->f_attr.mdsthreshold = NULL;
2683 nfs4_label_free(olabel);
2685 nfs4_opendata_put(opendata);
2686 nfs4_put_state_owner(sp);
2689 nfs4_label_free(olabel);
2691 nfs4_opendata_put(opendata);
2692 err_put_state_owner:
2693 nfs4_put_state_owner(sp);
2699 static struct nfs4_state *nfs4_do_open(struct inode *dir,
2700 struct nfs_open_context *ctx,
2702 struct iattr *sattr,
2703 struct nfs4_label *label,
2706 struct nfs_server *server = NFS_SERVER(dir);
2707 struct nfs4_exception exception = { };
2708 struct nfs4_state *res;
2712 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
2714 trace_nfs4_open_file(ctx, flags, status);
2717 /* NOTE: BAD_SEQID means the server and client disagree about the
2718 * book-keeping w.r.t. state-changing operations
2719 * (OPEN/CLOSE/LOCK/LOCKU...)
2720 * It is actually a sign of a bug on the client or on the server.
2722 * If we receive a BAD_SEQID error in the particular case of
2723 * doing an OPEN, we assume that nfs_increment_open_seqid() will
2724 * have unhashed the old state_owner for us, and that we can
2725 * therefore safely retry using a new one. We should still warn
2726 * the user though...
2728 if (status == -NFS4ERR_BAD_SEQID) {
2729 pr_warn_ratelimited("NFS: v4 server %s "
2730 " returned a bad sequence-id error!\n",
2731 NFS_SERVER(dir)->nfs_client->cl_hostname);
2732 exception.retry = 1;
2736 * BAD_STATEID on OPEN means that the server cancelled our
2737 * state before it received the OPEN_CONFIRM.
2738 * Recover by retrying the request as per the discussion
2739 * on Page 181 of RFC3530.
2741 if (status == -NFS4ERR_BAD_STATEID) {
2742 exception.retry = 1;
2745 if (status == -EAGAIN) {
2746 /* We must have found a delegation */
2747 exception.retry = 1;
2750 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2752 res = ERR_PTR(nfs4_handle_exception(server,
2753 status, &exception));
2754 } while (exception.retry);
2758 static int _nfs4_do_setattr(struct inode *inode,
2759 struct nfs_setattrargs *arg,
2760 struct nfs_setattrres *res,
2761 struct rpc_cred *cred,
2762 struct nfs4_state *state)
2764 struct nfs_server *server = NFS_SERVER(inode);
2765 struct rpc_message msg = {
2766 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2771 struct rpc_cred *delegation_cred = NULL;
2772 unsigned long timestamp = jiffies;
2777 nfs_fattr_init(res->fattr);
2779 /* Servers should only apply open mode checks for file size changes */
2780 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
2781 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2783 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
2784 /* Use that stateid */
2785 } else if (truncate && state != NULL) {
2786 struct nfs_lockowner lockowner = {
2787 .l_owner = current->files,
2788 .l_pid = current->tgid,
2790 if (!nfs4_valid_open_stateid(state))
2792 if (nfs4_select_rw_stateid(state, FMODE_WRITE, &lockowner,
2793 &arg->stateid, &delegation_cred) == -EIO)
2796 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
2797 if (delegation_cred)
2798 msg.rpc_cred = delegation_cred;
2800 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
2802 put_rpccred(delegation_cred);
2803 if (status == 0 && state != NULL)
2804 renew_lease(server, timestamp);
2805 trace_nfs4_setattr(inode, &arg->stateid, status);
2809 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2810 struct nfs_fattr *fattr, struct iattr *sattr,
2811 struct nfs4_state *state, struct nfs4_label *ilabel,
2812 struct nfs4_label *olabel)
2814 struct nfs_server *server = NFS_SERVER(inode);
2815 struct nfs_setattrargs arg = {
2816 .fh = NFS_FH(inode),
2819 .bitmask = server->attr_bitmask,
2822 struct nfs_setattrres res = {
2827 struct nfs4_exception exception = {
2830 .stateid = &arg.stateid,
2834 arg.bitmask = nfs4_bitmask(server, ilabel);
2836 arg.bitmask = nfs4_bitmask(server, olabel);
2839 err = _nfs4_do_setattr(inode, &arg, &res, cred, state);
2841 case -NFS4ERR_OPENMODE:
2842 if (!(sattr->ia_valid & ATTR_SIZE)) {
2843 pr_warn_once("NFSv4: server %s is incorrectly "
2844 "applying open mode checks to "
2845 "a SETATTR that is not "
2846 "changing file size.\n",
2847 server->nfs_client->cl_hostname);
2849 if (state && !(state->state & FMODE_WRITE)) {
2851 if (sattr->ia_valid & ATTR_OPEN)
2856 err = nfs4_handle_exception(server, err, &exception);
2857 } while (exception.retry);
2863 nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
2865 if (inode == NULL || !nfs_have_layout(inode))
2868 return pnfs_wait_on_layoutreturn(inode, task);
2871 struct nfs4_closedata {
2872 struct inode *inode;
2873 struct nfs4_state *state;
2874 struct nfs_closeargs arg;
2875 struct nfs_closeres res;
2876 struct nfs_fattr fattr;
2877 unsigned long timestamp;
2882 static void nfs4_free_closedata(void *data)
2884 struct nfs4_closedata *calldata = data;
2885 struct nfs4_state_owner *sp = calldata->state->owner;
2886 struct super_block *sb = calldata->state->inode->i_sb;
2889 pnfs_roc_release(calldata->state->inode);
2890 nfs4_put_open_state(calldata->state);
2891 nfs_free_seqid(calldata->arg.seqid);
2892 nfs4_put_state_owner(sp);
2893 nfs_sb_deactive(sb);
2897 static void nfs4_close_done(struct rpc_task *task, void *data)
2899 struct nfs4_closedata *calldata = data;
2900 struct nfs4_state *state = calldata->state;
2901 struct nfs_server *server = NFS_SERVER(calldata->inode);
2902 nfs4_stateid *res_stateid = NULL;
2904 dprintk("%s: begin!\n", __func__);
2905 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2907 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
2908 /* hmm. we are done with the inode, and in the process of freeing
2909 * the state_owner. we keep this around to process errors
2911 switch (task->tk_status) {
2913 res_stateid = &calldata->res.stateid;
2915 pnfs_roc_set_barrier(state->inode,
2916 calldata->roc_barrier);
2917 renew_lease(server, calldata->timestamp);
2919 case -NFS4ERR_ADMIN_REVOKED:
2920 case -NFS4ERR_STALE_STATEID:
2921 case -NFS4ERR_OLD_STATEID:
2922 case -NFS4ERR_BAD_STATEID:
2923 case -NFS4ERR_EXPIRED:
2924 if (!nfs4_stateid_match(&calldata->arg.stateid,
2925 &state->open_stateid)) {
2926 rpc_restart_call_prepare(task);
2929 if (calldata->arg.fmode == 0)
2932 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
2933 rpc_restart_call_prepare(task);
2937 nfs_clear_open_stateid(state, &calldata->arg.stateid,
2938 res_stateid, calldata->arg.fmode);
2940 nfs_release_seqid(calldata->arg.seqid);
2941 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
2942 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
2945 static void nfs4_close_prepare(struct rpc_task *task, void *data)
2947 struct nfs4_closedata *calldata = data;
2948 struct nfs4_state *state = calldata->state;
2949 struct inode *inode = calldata->inode;
2950 bool is_rdonly, is_wronly, is_rdwr;
2953 dprintk("%s: begin!\n", __func__);
2954 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
2957 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2958 spin_lock(&state->owner->so_lock);
2959 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
2960 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
2961 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
2962 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
2963 /* Calculate the change in open mode */
2964 calldata->arg.fmode = 0;
2965 if (state->n_rdwr == 0) {
2966 if (state->n_rdonly == 0)
2967 call_close |= is_rdonly;
2969 calldata->arg.fmode |= FMODE_READ;
2970 if (state->n_wronly == 0)
2971 call_close |= is_wronly;
2973 calldata->arg.fmode |= FMODE_WRITE;
2974 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
2975 call_close |= is_rdwr;
2977 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
2979 if (!nfs4_valid_open_stateid(state))
2981 spin_unlock(&state->owner->so_lock);
2984 /* Note: exit _without_ calling nfs4_close_done */
2988 if (nfs4_wait_on_layoutreturn(inode, task)) {
2989 nfs_release_seqid(calldata->arg.seqid);
2993 if (calldata->arg.fmode == 0)
2994 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
2996 pnfs_roc_get_barrier(inode, &calldata->roc_barrier);
2998 calldata->arg.share_access =
2999 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3000 calldata->arg.fmode, 0);
3002 nfs_fattr_init(calldata->res.fattr);
3003 calldata->timestamp = jiffies;
3004 if (nfs4_setup_sequence(NFS_SERVER(inode),
3005 &calldata->arg.seq_args,
3006 &calldata->res.seq_res,
3008 nfs_release_seqid(calldata->arg.seqid);
3009 dprintk("%s: done!\n", __func__);
3012 task->tk_action = NULL;
3014 nfs4_sequence_done(task, &calldata->res.seq_res);
3017 static const struct rpc_call_ops nfs4_close_ops = {
3018 .rpc_call_prepare = nfs4_close_prepare,
3019 .rpc_call_done = nfs4_close_done,
3020 .rpc_release = nfs4_free_closedata,
3023 static bool nfs4_roc(struct inode *inode)
3025 if (!nfs_have_layout(inode))
3027 return pnfs_roc(inode);
3031 * It is possible for data to be read/written from a mem-mapped file
3032 * after the sys_close call (which hits the vfs layer as a flush).
3033 * This means that we can't safely call nfsv4 close on a file until
3034 * the inode is cleared. This in turn means that we are not good
3035 * NFSv4 citizens - we do not indicate to the server to update the file's
3036 * share state even when we are done with one of the three share
3037 * stateid's in the inode.
3039 * NOTE: Caller must be holding the sp->so_owner semaphore!
3041 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
3043 struct nfs_server *server = NFS_SERVER(state->inode);
3044 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
3045 struct nfs4_closedata *calldata;
3046 struct nfs4_state_owner *sp = state->owner;
3047 struct rpc_task *task;
3048 struct rpc_message msg = {
3049 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3050 .rpc_cred = state->owner->so_cred,
3052 struct rpc_task_setup task_setup_data = {
3053 .rpc_client = server->client,
3054 .rpc_message = &msg,
3055 .callback_ops = &nfs4_close_ops,
3056 .workqueue = nfsiod_workqueue,
3057 .flags = RPC_TASK_ASYNC,
3059 int status = -ENOMEM;
3061 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3062 &task_setup_data.rpc_client, &msg);
3064 calldata = kzalloc(sizeof(*calldata), gfp_mask);
3065 if (calldata == NULL)
3067 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
3068 calldata->inode = state->inode;
3069 calldata->state = state;
3070 calldata->arg.fh = NFS_FH(state->inode);
3071 /* Serialization for the sequence id */
3072 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3073 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
3074 if (IS_ERR(calldata->arg.seqid))
3075 goto out_free_calldata;
3076 calldata->arg.fmode = 0;
3077 calldata->arg.bitmask = server->cache_consistency_bitmask;
3078 calldata->res.fattr = &calldata->fattr;
3079 calldata->res.seqid = calldata->arg.seqid;
3080 calldata->res.server = server;
3081 calldata->roc = nfs4_roc(state->inode);
3082 nfs_sb_active(calldata->inode->i_sb);
3084 msg.rpc_argp = &calldata->arg;
3085 msg.rpc_resp = &calldata->res;
3086 task_setup_data.callback_data = calldata;
3087 task = rpc_run_task(&task_setup_data);
3089 return PTR_ERR(task);
3092 status = rpc_wait_for_completion_task(task);
3098 nfs4_put_open_state(state);
3099 nfs4_put_state_owner(sp);
3103 static struct inode *
3104 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3105 int open_flags, struct iattr *attr, int *opened)
3107 struct nfs4_state *state;
3108 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3110 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
3112 /* Protect against concurrent sillydeletes */
3113 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
3115 nfs4_label_release_security(label);
3118 return ERR_CAST(state);
3119 return state->inode;
3122 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
3124 if (ctx->state == NULL)
3127 nfs4_close_sync(ctx->state, ctx->mode);
3129 nfs4_close_state(ctx->state, ctx->mode);
3132 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3133 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3134 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
3136 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3138 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
3139 struct nfs4_server_caps_arg args = {
3143 struct nfs4_server_caps_res res = {};
3144 struct rpc_message msg = {
3145 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
3151 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3152 FATTR4_WORD0_FH_EXPIRE_TYPE |
3153 FATTR4_WORD0_LINK_SUPPORT |
3154 FATTR4_WORD0_SYMLINK_SUPPORT |
3155 FATTR4_WORD0_ACLSUPPORT;
3157 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3159 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3161 /* Sanity check the server answers */
3162 switch (minorversion) {
3164 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3165 res.attr_bitmask[2] = 0;
3168 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3171 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3173 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
3174 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3175 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3176 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3177 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
3178 NFS_CAP_CTIME|NFS_CAP_MTIME|
3179 NFS_CAP_SECURITY_LABEL);
3180 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3181 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3182 server->caps |= NFS_CAP_ACLS;
3183 if (res.has_links != 0)
3184 server->caps |= NFS_CAP_HARDLINKS;
3185 if (res.has_symlinks != 0)
3186 server->caps |= NFS_CAP_SYMLINKS;
3187 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3188 server->caps |= NFS_CAP_FILEID;
3189 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3190 server->caps |= NFS_CAP_MODE;
3191 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3192 server->caps |= NFS_CAP_NLINK;
3193 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3194 server->caps |= NFS_CAP_OWNER;
3195 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3196 server->caps |= NFS_CAP_OWNER_GROUP;
3197 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3198 server->caps |= NFS_CAP_ATIME;
3199 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3200 server->caps |= NFS_CAP_CTIME;
3201 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3202 server->caps |= NFS_CAP_MTIME;
3203 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3204 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3205 server->caps |= NFS_CAP_SECURITY_LABEL;
3207 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3208 sizeof(server->attr_bitmask));
3209 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3211 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3212 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3213 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
3214 server->cache_consistency_bitmask[2] = 0;
3215 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3216 sizeof(server->exclcreat_bitmask));
3217 server->acl_bitmask = res.acl_bitmask;
3218 server->fh_expire_type = res.fh_expire_type;
3224 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3226 struct nfs4_exception exception = { };
3229 err = nfs4_handle_exception(server,
3230 _nfs4_server_capabilities(server, fhandle),
3232 } while (exception.retry);
3236 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3237 struct nfs_fsinfo *info)
3240 struct nfs4_lookup_root_arg args = {
3243 struct nfs4_lookup_res res = {
3245 .fattr = info->fattr,
3248 struct rpc_message msg = {
3249 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3254 bitmask[0] = nfs4_fattr_bitmap[0];
3255 bitmask[1] = nfs4_fattr_bitmap[1];
3257 * Process the label in the upcoming getfattr
3259 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3261 nfs_fattr_init(info->fattr);
3262 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3265 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3266 struct nfs_fsinfo *info)
3268 struct nfs4_exception exception = { };
3271 err = _nfs4_lookup_root(server, fhandle, info);
3272 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
3275 case -NFS4ERR_WRONGSEC:
3278 err = nfs4_handle_exception(server, err, &exception);
3280 } while (exception.retry);
3285 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3286 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3288 struct rpc_auth_create_args auth_args = {
3289 .pseudoflavor = flavor,
3291 struct rpc_auth *auth;
3294 auth = rpcauth_create(&auth_args, server->client);
3299 ret = nfs4_lookup_root(server, fhandle, info);
3305 * Retry pseudoroot lookup with various security flavors. We do this when:
3307 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3308 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3310 * Returns zero on success, or a negative NFS4ERR value, or a
3311 * negative errno value.
3313 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3314 struct nfs_fsinfo *info)
3316 /* Per 3530bis 15.33.5 */
3317 static const rpc_authflavor_t flav_array[] = {
3321 RPC_AUTH_UNIX, /* courtesy */
3324 int status = -EPERM;
3327 if (server->auth_info.flavor_len > 0) {
3328 /* try each flavor specified by user */
3329 for (i = 0; i < server->auth_info.flavor_len; i++) {
3330 status = nfs4_lookup_root_sec(server, fhandle, info,
3331 server->auth_info.flavors[i]);
3332 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3337 /* no flavors specified by user, try default list */
3338 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3339 status = nfs4_lookup_root_sec(server, fhandle, info,
3341 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3348 * -EACCESS could mean that the user doesn't have correct permissions
3349 * to access the mount. It could also mean that we tried to mount
3350 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3351 * existing mount programs don't handle -EACCES very well so it should
3352 * be mapped to -EPERM instead.
3354 if (status == -EACCES)
3360 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3361 * @server: initialized nfs_server handle
3362 * @fhandle: we fill in the pseudo-fs root file handle
3363 * @info: we fill in an FSINFO struct
3364 * @auth_probe: probe the auth flavours
3366 * Returns zero on success, or a negative errno.
3368 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
3369 struct nfs_fsinfo *info,
3375 status = nfs4_lookup_root(server, fhandle, info);
3377 if (auth_probe || status == NFS4ERR_WRONGSEC)
3378 status = server->nfs_client->cl_mvops->find_root_sec(server,
3382 status = nfs4_server_capabilities(server, fhandle);
3384 status = nfs4_do_fsinfo(server, fhandle, info);
3386 return nfs4_map_errors(status);
3389 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3390 struct nfs_fsinfo *info)
3393 struct nfs_fattr *fattr = info->fattr;
3394 struct nfs4_label *label = NULL;
3396 error = nfs4_server_capabilities(server, mntfh);
3398 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3402 label = nfs4_label_alloc(server, GFP_KERNEL);
3404 return PTR_ERR(label);
3406 error = nfs4_proc_getattr(server, mntfh, fattr, label);
3408 dprintk("nfs4_get_root: getattr error = %d\n", -error);
3409 goto err_free_label;
3412 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3413 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3414 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3417 nfs4_label_free(label);
3423 * Get locations and (maybe) other attributes of a referral.
3424 * Note that we'll actually follow the referral later when
3425 * we detect fsid mismatch in inode revalidation
3427 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3428 const struct qstr *name, struct nfs_fattr *fattr,
3429 struct nfs_fh *fhandle)
3431 int status = -ENOMEM;
3432 struct page *page = NULL;
3433 struct nfs4_fs_locations *locations = NULL;
3435 page = alloc_page(GFP_KERNEL);
3438 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3439 if (locations == NULL)
3442 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
3447 * If the fsid didn't change, this is a migration event, not a
3448 * referral. Cause us to drop into the exception handler, which
3449 * will kick off migration recovery.
3451 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
3452 dprintk("%s: server did not return a different fsid for"
3453 " a referral at %s\n", __func__, name->name);
3454 status = -NFS4ERR_MOVED;
3457 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3458 nfs_fixup_referral_attributes(&locations->fattr);
3460 /* replace the lookup nfs_fattr with the locations nfs_fattr */
3461 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
3462 memset(fhandle, 0, sizeof(struct nfs_fh));
3470 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3471 struct nfs_fattr *fattr, struct nfs4_label *label)
3473 struct nfs4_getattr_arg args = {
3475 .bitmask = server->attr_bitmask,
3477 struct nfs4_getattr_res res = {
3482 struct rpc_message msg = {
3483 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3488 args.bitmask = nfs4_bitmask(server, label);
3490 nfs_fattr_init(fattr);
3491 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3494 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3495 struct nfs_fattr *fattr, struct nfs4_label *label)
3497 struct nfs4_exception exception = { };
3500 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3501 trace_nfs4_getattr(server, fhandle, fattr, err);
3502 err = nfs4_handle_exception(server, err,
3504 } while (exception.retry);
3509 * The file is not closed if it is opened due to the a request to change
3510 * the size of the file. The open call will not be needed once the
3511 * VFS layer lookup-intents are implemented.
3513 * Close is called when the inode is destroyed.
3514 * If we haven't opened the file for O_WRONLY, we
3515 * need to in the size_change case to obtain a stateid.
3518 * Because OPEN is always done by name in nfsv4, it is
3519 * possible that we opened a different file by the same
3520 * name. We can recognize this race condition, but we
3521 * can't do anything about it besides returning an error.
3523 * This will be fixed with VFS changes (lookup-intent).
3526 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3527 struct iattr *sattr)
3529 struct inode *inode = d_inode(dentry);
3530 struct rpc_cred *cred = NULL;
3531 struct nfs4_state *state = NULL;
3532 struct nfs4_label *label = NULL;
3535 if (pnfs_ld_layoutret_on_setattr(inode) &&
3536 sattr->ia_valid & ATTR_SIZE &&
3537 sattr->ia_size < i_size_read(inode))
3538 pnfs_commit_and_return_layout(inode);
3540 nfs_fattr_init(fattr);
3542 /* Deal with open(O_TRUNC) */
3543 if (sattr->ia_valid & ATTR_OPEN)
3544 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
3546 /* Optimization: if the end result is no change, don't RPC */
3547 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
3550 /* Search for an existing open(O_WRITE) file */
3551 if (sattr->ia_valid & ATTR_FILE) {
3552 struct nfs_open_context *ctx;
3554 ctx = nfs_file_open_context(sattr->ia_file);
3561 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3563 return PTR_ERR(label);
3565 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
3567 nfs_setattr_update_inode(inode, sattr, fattr);
3568 nfs_setsecurity(inode, fattr, label);
3570 nfs4_label_free(label);
3574 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3575 const struct qstr *name, struct nfs_fh *fhandle,
3576 struct nfs_fattr *fattr, struct nfs4_label *label)
3578 struct nfs_server *server = NFS_SERVER(dir);
3580 struct nfs4_lookup_arg args = {
3581 .bitmask = server->attr_bitmask,
3582 .dir_fh = NFS_FH(dir),
3585 struct nfs4_lookup_res res = {
3591 struct rpc_message msg = {
3592 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3597 args.bitmask = nfs4_bitmask(server, label);
3599 nfs_fattr_init(fattr);
3601 dprintk("NFS call lookup %s\n", name->name);
3602 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
3603 dprintk("NFS reply lookup: %d\n", status);
3607 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
3609 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
3610 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
3611 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3615 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3616 const struct qstr *name, struct nfs_fh *fhandle,
3617 struct nfs_fattr *fattr, struct nfs4_label *label)
3619 struct nfs4_exception exception = { };
3620 struct rpc_clnt *client = *clnt;
3623 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
3624 trace_nfs4_lookup(dir, name, err);
3626 case -NFS4ERR_BADNAME:
3629 case -NFS4ERR_MOVED:
3630 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
3631 if (err == -NFS4ERR_MOVED)
3632 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3634 case -NFS4ERR_WRONGSEC:
3636 if (client != *clnt)
3638 client = nfs4_negotiate_security(client, dir, name);
3640 return PTR_ERR(client);
3642 exception.retry = 1;
3645 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3647 } while (exception.retry);
3652 else if (client != *clnt)
3653 rpc_shutdown_client(client);
3658 static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
3659 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3660 struct nfs4_label *label)
3663 struct rpc_clnt *client = NFS_CLIENT(dir);
3665 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
3666 if (client != NFS_CLIENT(dir)) {
3667 rpc_shutdown_client(client);
3668 nfs_fixup_secinfo_attributes(fattr);
3674 nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
3675 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3677 struct rpc_clnt *client = NFS_CLIENT(dir);
3680 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
3682 return ERR_PTR(status);
3683 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
3686 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3688 struct nfs_server *server = NFS_SERVER(inode);
3689 struct nfs4_accessargs args = {
3690 .fh = NFS_FH(inode),
3691 .bitmask = server->cache_consistency_bitmask,
3693 struct nfs4_accessres res = {
3696 struct rpc_message msg = {
3697 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3700 .rpc_cred = entry->cred,
3702 int mode = entry->mask;
3706 * Determine which access bits we want to ask for...
3708 if (mode & MAY_READ)
3709 args.access |= NFS4_ACCESS_READ;
3710 if (S_ISDIR(inode->i_mode)) {
3711 if (mode & MAY_WRITE)
3712 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3713 if (mode & MAY_EXEC)
3714 args.access |= NFS4_ACCESS_LOOKUP;
3716 if (mode & MAY_WRITE)
3717 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3718 if (mode & MAY_EXEC)
3719 args.access |= NFS4_ACCESS_EXECUTE;
3722 res.fattr = nfs_alloc_fattr();
3723 if (res.fattr == NULL)
3726 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3728 nfs_access_set_mask(entry, res.access);
3729 nfs_refresh_inode(inode, res.fattr);
3731 nfs_free_fattr(res.fattr);
3735 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3737 struct nfs4_exception exception = { };
3740 err = _nfs4_proc_access(inode, entry);
3741 trace_nfs4_access(inode, err);
3742 err = nfs4_handle_exception(NFS_SERVER(inode), err,
3744 } while (exception.retry);
3749 * TODO: For the time being, we don't try to get any attributes
3750 * along with any of the zero-copy operations READ, READDIR,
3753 * In the case of the first three, we want to put the GETATTR
3754 * after the read-type operation -- this is because it is hard
3755 * to predict the length of a GETATTR response in v4, and thus
3756 * align the READ data correctly. This means that the GETATTR
3757 * may end up partially falling into the page cache, and we should
3758 * shift it into the 'tail' of the xdr_buf before processing.
3759 * To do this efficiently, we need to know the total length
3760 * of data received, which doesn't seem to be available outside
3763 * In the case of WRITE, we also want to put the GETATTR after
3764 * the operation -- in this case because we want to make sure
3765 * we get the post-operation mtime and size.
3767 * Both of these changes to the XDR layer would in fact be quite
3768 * minor, but I decided to leave them for a subsequent patch.
3770 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3771 unsigned int pgbase, unsigned int pglen)
3773 struct nfs4_readlink args = {
3774 .fh = NFS_FH(inode),
3779 struct nfs4_readlink_res res;
3780 struct rpc_message msg = {
3781 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3786 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
3789 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3790 unsigned int pgbase, unsigned int pglen)
3792 struct nfs4_exception exception = { };
3795 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3796 trace_nfs4_readlink(inode, err);
3797 err = nfs4_handle_exception(NFS_SERVER(inode), err,
3799 } while (exception.retry);
3804 * This is just for mknod. open(O_CREAT) will always do ->open_context().
3807 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
3810 struct nfs4_label l, *ilabel = NULL;
3811 struct nfs_open_context *ctx;
3812 struct nfs4_state *state;
3815 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3817 return PTR_ERR(ctx);
3819 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3821 sattr->ia_mode &= ~current_umask();
3822 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
3823 if (IS_ERR(state)) {
3824 status = PTR_ERR(state);
3828 nfs4_label_release_security(ilabel);
3829 put_nfs_open_context(ctx);
3833 static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
3835 struct nfs_server *server = NFS_SERVER(dir);
3836 struct nfs_removeargs args = {
3840 struct nfs_removeres res = {
3843 struct rpc_message msg = {
3844 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3850 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
3852 update_changeattr(dir, &res.cinfo);
3856 static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
3858 struct nfs4_exception exception = { };
3861 err = _nfs4_proc_remove(dir, name);
3862 trace_nfs4_remove(dir, name, err);
3863 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3865 } while (exception.retry);
3869 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
3871 struct nfs_server *server = NFS_SERVER(dir);
3872 struct nfs_removeargs *args = msg->rpc_argp;
3873 struct nfs_removeres *res = msg->rpc_resp;
3875 res->server = server;
3876 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
3877 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
3879 nfs_fattr_init(res->dir_attr);
3882 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3884 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb),
3885 &data->args.seq_args,
3890 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
3892 struct nfs_unlinkdata *data = task->tk_calldata;
3893 struct nfs_removeres *res = &data->res;
3895 if (!nfs4_sequence_done(task, &res->seq_res))
3897 if (nfs4_async_handle_error(task, res->server, NULL,
3898 &data->timeout) == -EAGAIN)
3900 update_changeattr(dir, &res->cinfo);
3904 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3906 struct nfs_server *server = NFS_SERVER(dir);
3907 struct nfs_renameargs *arg = msg->rpc_argp;
3908 struct nfs_renameres *res = msg->rpc_resp;
3910 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
3911 res->server = server;
3912 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
3915 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3917 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3918 &data->args.seq_args,
3923 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3924 struct inode *new_dir)
3926 struct nfs_renamedata *data = task->tk_calldata;
3927 struct nfs_renameres *res = &data->res;
3929 if (!nfs4_sequence_done(task, &res->seq_res))
3931 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
3934 update_changeattr(old_dir, &res->old_cinfo);
3935 update_changeattr(new_dir, &res->new_cinfo);
3939 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
3941 struct nfs_server *server = NFS_SERVER(inode);
3942 struct nfs4_link_arg arg = {
3943 .fh = NFS_FH(inode),
3944 .dir_fh = NFS_FH(dir),
3946 .bitmask = server->attr_bitmask,
3948 struct nfs4_link_res res = {
3952 struct rpc_message msg = {
3953 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3957 int status = -ENOMEM;
3959 res.fattr = nfs_alloc_fattr();
3960 if (res.fattr == NULL)
3963 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3964 if (IS_ERR(res.label)) {
3965 status = PTR_ERR(res.label);
3968 arg.bitmask = nfs4_bitmask(server, res.label);
3970 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3972 update_changeattr(dir, &res.cinfo);
3973 status = nfs_post_op_update_inode(inode, res.fattr);
3975 nfs_setsecurity(inode, res.fattr, res.label);
3979 nfs4_label_free(res.label);
3982 nfs_free_fattr(res.fattr);
3986 static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
3988 struct nfs4_exception exception = { };
3991 err = nfs4_handle_exception(NFS_SERVER(inode),
3992 _nfs4_proc_link(inode, dir, name),
3994 } while (exception.retry);
3998 struct nfs4_createdata {
3999 struct rpc_message msg;
4000 struct nfs4_create_arg arg;
4001 struct nfs4_create_res res;
4003 struct nfs_fattr fattr;
4004 struct nfs4_label *label;
4007 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
4008 const struct qstr *name, struct iattr *sattr, u32 ftype)
4010 struct nfs4_createdata *data;
4012 data = kzalloc(sizeof(*data), GFP_KERNEL);
4014 struct nfs_server *server = NFS_SERVER(dir);
4016 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4017 if (IS_ERR(data->label))
4020 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4021 data->msg.rpc_argp = &data->arg;
4022 data->msg.rpc_resp = &data->res;
4023 data->arg.dir_fh = NFS_FH(dir);
4024 data->arg.server = server;
4025 data->arg.name = name;
4026 data->arg.attrs = sattr;
4027 data->arg.ftype = ftype;
4028 data->arg.bitmask = nfs4_bitmask(server, data->label);
4029 data->res.server = server;
4030 data->res.fh = &data->fh;
4031 data->res.fattr = &data->fattr;
4032 data->res.label = data->label;
4033 nfs_fattr_init(data->res.fattr);
4041 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4043 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
4044 &data->arg.seq_args, &data->res.seq_res, 1);
4046 update_changeattr(dir, &data->res.dir_cinfo);
4047 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
4052 static void nfs4_free_createdata(struct nfs4_createdata *data)
4054 nfs4_label_free(data->label);
4058 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4059 struct page *page, unsigned int len, struct iattr *sattr,
4060 struct nfs4_label *label)
4062 struct nfs4_createdata *data;
4063 int status = -ENAMETOOLONG;
4065 if (len > NFS4_MAXPATHLEN)
4069 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4073 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4074 data->arg.u.symlink.pages = &page;
4075 data->arg.u.symlink.len = len;
4076 data->arg.label = label;
4078 status = nfs4_do_create(dir, dentry, data);
4080 nfs4_free_createdata(data);
4085 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4086 struct page *page, unsigned int len, struct iattr *sattr)
4088 struct nfs4_exception exception = { };
4089 struct nfs4_label l, *label = NULL;
4092 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4095 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4096 trace_nfs4_symlink(dir, &dentry->d_name, err);
4097 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4099 } while (exception.retry);
4101 nfs4_label_release_security(label);
4105 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4106 struct iattr *sattr, struct nfs4_label *label)
4108 struct nfs4_createdata *data;
4109 int status = -ENOMEM;
4111 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4115 data->arg.label = label;
4116 status = nfs4_do_create(dir, dentry, data);
4118 nfs4_free_createdata(data);
4123 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4124 struct iattr *sattr)
4126 struct nfs4_exception exception = { };
4127 struct nfs4_label l, *label = NULL;
4130 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4132 sattr->ia_mode &= ~current_umask();
4134 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4135 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4136 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4138 } while (exception.retry);
4139 nfs4_label_release_security(label);
4144 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
4145 u64 cookie, struct page **pages, unsigned int count, int plus)
4147 struct inode *dir = d_inode(dentry);
4148 struct nfs4_readdir_arg args = {
4153 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
4156 struct nfs4_readdir_res res;
4157 struct rpc_message msg = {
4158 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4165 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4167 (unsigned long long)cookie);
4168 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
4169 res.pgbase = args.pgbase;
4170 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4172 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
4173 status += args.pgbase;
4176 nfs_invalidate_atime(dir);
4178 dprintk("%s: returns %d\n", __func__, status);
4182 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
4183 u64 cookie, struct page **pages, unsigned int count, int plus)
4185 struct nfs4_exception exception = { };
4188 err = _nfs4_proc_readdir(dentry, cred, cookie,
4189 pages, count, plus);
4190 trace_nfs4_readdir(d_inode(dentry), err);
4191 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
4193 } while (exception.retry);
4197 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4198 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
4200 struct nfs4_createdata *data;
4201 int mode = sattr->ia_mode;
4202 int status = -ENOMEM;
4204 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4209 data->arg.ftype = NF4FIFO;
4210 else if (S_ISBLK(mode)) {
4211 data->arg.ftype = NF4BLK;
4212 data->arg.u.device.specdata1 = MAJOR(rdev);
4213 data->arg.u.device.specdata2 = MINOR(rdev);
4215 else if (S_ISCHR(mode)) {
4216 data->arg.ftype = NF4CHR;
4217 data->arg.u.device.specdata1 = MAJOR(rdev);
4218 data->arg.u.device.specdata2 = MINOR(rdev);
4219 } else if (!S_ISSOCK(mode)) {
4224 data->arg.label = label;
4225 status = nfs4_do_create(dir, dentry, data);
4227 nfs4_free_createdata(data);
4232 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4233 struct iattr *sattr, dev_t rdev)
4235 struct nfs4_exception exception = { };
4236 struct nfs4_label l, *label = NULL;
4239 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4241 sattr->ia_mode &= ~current_umask();
4243 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4244 trace_nfs4_mknod(dir, &dentry->d_name, err);
4245 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4247 } while (exception.retry);
4249 nfs4_label_release_security(label);
4254 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4255 struct nfs_fsstat *fsstat)
4257 struct nfs4_statfs_arg args = {
4259 .bitmask = server->attr_bitmask,
4261 struct nfs4_statfs_res res = {
4264 struct rpc_message msg = {
4265 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4270 nfs_fattr_init(fsstat->fattr);
4271 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4274 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4276 struct nfs4_exception exception = { };
4279 err = nfs4_handle_exception(server,
4280 _nfs4_proc_statfs(server, fhandle, fsstat),
4282 } while (exception.retry);
4286 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4287 struct nfs_fsinfo *fsinfo)
4289 struct nfs4_fsinfo_arg args = {
4291 .bitmask = server->attr_bitmask,
4293 struct nfs4_fsinfo_res res = {
4296 struct rpc_message msg = {
4297 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4302 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4305 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4307 struct nfs4_exception exception = { };
4308 unsigned long now = jiffies;
4312 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
4313 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
4315 nfs4_set_lease_period(server->nfs_client,
4316 fsinfo->lease_time * HZ,
4320 err = nfs4_handle_exception(server, err, &exception);
4321 } while (exception.retry);
4325 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4329 nfs_fattr_init(fsinfo->fattr);
4330 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
4332 /* block layout checks this! */
4333 server->pnfs_blksize = fsinfo->blksize;
4334 set_pnfs_layoutdriver(server, fhandle, fsinfo);
4340 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4341 struct nfs_pathconf *pathconf)
4343 struct nfs4_pathconf_arg args = {
4345 .bitmask = server->attr_bitmask,
4347 struct nfs4_pathconf_res res = {
4348 .pathconf = pathconf,
4350 struct rpc_message msg = {
4351 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4356 /* None of the pathconf attributes are mandatory to implement */
4357 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4358 memset(pathconf, 0, sizeof(*pathconf));
4362 nfs_fattr_init(pathconf->fattr);
4363 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4366 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4367 struct nfs_pathconf *pathconf)
4369 struct nfs4_exception exception = { };
4373 err = nfs4_handle_exception(server,
4374 _nfs4_proc_pathconf(server, fhandle, pathconf),
4376 } while (exception.retry);
4380 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
4381 const struct nfs_open_context *ctx,
4382 const struct nfs_lock_context *l_ctx,
4385 const struct nfs_lockowner *lockowner = NULL;
4388 lockowner = &l_ctx->lockowner;
4389 return nfs4_select_rw_stateid(ctx->state, fmode, lockowner, stateid, NULL);
4391 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4393 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4394 const struct nfs_open_context *ctx,
4395 const struct nfs_lock_context *l_ctx,
4398 nfs4_stateid current_stateid;
4400 /* If the current stateid represents a lost lock, then exit */
4401 if (nfs4_set_rw_stateid(¤t_stateid, ctx, l_ctx, fmode) == -EIO)
4403 return nfs4_stateid_match(stateid, ¤t_stateid);
4406 static bool nfs4_error_stateid_expired(int err)
4409 case -NFS4ERR_DELEG_REVOKED:
4410 case -NFS4ERR_ADMIN_REVOKED:
4411 case -NFS4ERR_BAD_STATEID:
4412 case -NFS4ERR_STALE_STATEID:
4413 case -NFS4ERR_OLD_STATEID:
4414 case -NFS4ERR_OPENMODE:
4415 case -NFS4ERR_EXPIRED:
4421 void __nfs4_read_done_cb(struct nfs_pgio_header *hdr)
4423 nfs_invalidate_atime(hdr->inode);
4426 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
4428 struct nfs_server *server = NFS_SERVER(hdr->inode);
4430 trace_nfs4_read(hdr, task->tk_status);
4431 if (nfs4_async_handle_error(task, server,
4432 hdr->args.context->state,
4434 rpc_restart_call_prepare(task);
4438 __nfs4_read_done_cb(hdr);
4439 if (task->tk_status > 0)
4440 renew_lease(server, hdr->timestamp);
4444 static bool nfs4_read_stateid_changed(struct rpc_task *task,
4445 struct nfs_pgio_args *args)
4448 if (!nfs4_error_stateid_expired(task->tk_status) ||
4449 nfs4_stateid_is_current(&args->stateid,
4454 rpc_restart_call_prepare(task);
4458 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
4461 dprintk("--> %s\n", __func__);
4463 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
4465 if (nfs4_read_stateid_changed(task, &hdr->args))
4467 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4468 nfs4_read_done_cb(task, hdr);
4471 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4472 struct rpc_message *msg)
4474 hdr->timestamp = jiffies;
4475 if (!hdr->pgio_done_cb)
4476 hdr->pgio_done_cb = nfs4_read_done_cb;
4477 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
4478 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
4481 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4482 struct nfs_pgio_header *hdr)
4484 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4485 &hdr->args.seq_args,
4489 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4490 hdr->args.lock_context,
4491 hdr->rw_ops->rw_mode) == -EIO)
4493 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
4498 static int nfs4_write_done_cb(struct rpc_task *task,
4499 struct nfs_pgio_header *hdr)
4501 struct inode *inode = hdr->inode;
4503 trace_nfs4_write(hdr, task->tk_status);
4504 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4505 hdr->args.context->state,
4507 rpc_restart_call_prepare(task);
4510 if (task->tk_status >= 0) {
4511 renew_lease(NFS_SERVER(inode), hdr->timestamp);
4512 nfs_writeback_update_inode(hdr);
4517 static bool nfs4_write_stateid_changed(struct rpc_task *task,
4518 struct nfs_pgio_args *args)
4521 if (!nfs4_error_stateid_expired(task->tk_status) ||
4522 nfs4_stateid_is_current(&args->stateid,
4527 rpc_restart_call_prepare(task);
4531 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
4533 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
4535 if (nfs4_write_stateid_changed(task, &hdr->args))
4537 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4538 nfs4_write_done_cb(task, hdr);
4542 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
4544 /* Don't request attributes for pNFS or O_DIRECT writes */
4545 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
4547 /* Otherwise, request attributes if and only if we don't hold
4550 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
4553 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4554 struct rpc_message *msg)
4556 struct nfs_server *server = NFS_SERVER(hdr->inode);
4558 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4559 hdr->args.bitmask = NULL;
4560 hdr->res.fattr = NULL;
4562 hdr->args.bitmask = server->cache_consistency_bitmask;
4564 if (!hdr->pgio_done_cb)
4565 hdr->pgio_done_cb = nfs4_write_done_cb;
4566 hdr->res.server = server;
4567 hdr->timestamp = jiffies;
4569 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
4570 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
4573 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4575 nfs4_setup_sequence(NFS_SERVER(data->inode),
4576 &data->args.seq_args,
4581 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
4583 struct inode *inode = data->inode;
4585 trace_nfs4_commit(data, task->tk_status);
4586 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4587 NULL, NULL) == -EAGAIN) {
4588 rpc_restart_call_prepare(task);
4594 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
4596 if (!nfs4_sequence_done(task, &data->res.seq_res))
4598 return data->commit_done_cb(task, data);
4601 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
4603 struct nfs_server *server = NFS_SERVER(data->inode);
4605 if (data->commit_done_cb == NULL)
4606 data->commit_done_cb = nfs4_commit_done_cb;
4607 data->res.server = server;
4608 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
4609 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4612 struct nfs4_renewdata {
4613 struct nfs_client *client;
4614 unsigned long timestamp;
4618 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4619 * standalone procedure for queueing an asynchronous RENEW.
4621 static void nfs4_renew_release(void *calldata)
4623 struct nfs4_renewdata *data = calldata;
4624 struct nfs_client *clp = data->client;
4626 if (atomic_read(&clp->cl_count) > 1)
4627 nfs4_schedule_state_renewal(clp);
4628 nfs_put_client(clp);
4632 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
4634 struct nfs4_renewdata *data = calldata;
4635 struct nfs_client *clp = data->client;
4636 unsigned long timestamp = data->timestamp;
4638 trace_nfs4_renew_async(clp, task->tk_status);
4639 switch (task->tk_status) {
4642 case -NFS4ERR_LEASE_MOVED:
4643 nfs4_schedule_lease_moved_recovery(clp);
4646 /* Unless we're shutting down, schedule state recovery! */
4647 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4649 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
4650 nfs4_schedule_lease_recovery(clp);
4653 nfs4_schedule_path_down_recovery(clp);
4655 do_renew_lease(clp, timestamp);
4658 static const struct rpc_call_ops nfs4_renew_ops = {
4659 .rpc_call_done = nfs4_renew_done,
4660 .rpc_release = nfs4_renew_release,
4663 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
4665 struct rpc_message msg = {
4666 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4670 struct nfs4_renewdata *data;
4672 if (renew_flags == 0)
4674 if (!atomic_inc_not_zero(&clp->cl_count))
4676 data = kmalloc(sizeof(*data), GFP_NOFS);
4680 data->timestamp = jiffies;
4681 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
4682 &nfs4_renew_ops, data);
4685 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
4687 struct rpc_message msg = {
4688 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4692 unsigned long now = jiffies;
4695 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4698 do_renew_lease(clp, now);
4702 static inline int nfs4_server_supports_acls(struct nfs_server *server)
4704 return server->caps & NFS_CAP_ACLS;
4707 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4708 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
4711 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
4713 static int buf_to_pages_noslab(const void *buf, size_t buflen,
4714 struct page **pages)
4716 struct page *newpage, **spages;
4722 len = min_t(size_t, PAGE_SIZE, buflen);
4723 newpage = alloc_page(GFP_KERNEL);
4725 if (newpage == NULL)
4727 memcpy(page_address(newpage), buf, len);
4732 } while (buflen != 0);
4738 __free_page(spages[rc-1]);
4742 struct nfs4_cached_acl {
4748 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
4750 struct nfs_inode *nfsi = NFS_I(inode);
4752 spin_lock(&inode->i_lock);
4753 kfree(nfsi->nfs4_acl);
4754 nfsi->nfs4_acl = acl;
4755 spin_unlock(&inode->i_lock);
4758 static void nfs4_zap_acl_attr(struct inode *inode)
4760 nfs4_set_cached_acl(inode, NULL);
4763 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4765 struct nfs_inode *nfsi = NFS_I(inode);
4766 struct nfs4_cached_acl *acl;
4769 spin_lock(&inode->i_lock);
4770 acl = nfsi->nfs4_acl;
4773 if (buf == NULL) /* user is just asking for length */
4775 if (acl->cached == 0)
4777 ret = -ERANGE; /* see getxattr(2) man page */
4778 if (acl->len > buflen)
4780 memcpy(buf, acl->data, acl->len);
4784 spin_unlock(&inode->i_lock);
4788 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
4790 struct nfs4_cached_acl *acl;
4791 size_t buflen = sizeof(*acl) + acl_len;
4793 if (buflen <= PAGE_SIZE) {
4794 acl = kmalloc(buflen, GFP_KERNEL);
4798 _copy_from_pages(acl->data, pages, pgbase, acl_len);
4800 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4807 nfs4_set_cached_acl(inode, acl);
4811 * The getxattr API returns the required buffer length when called with a
4812 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4813 * the required buf. On a NULL buf, we send a page of data to the server
4814 * guessing that the ACL request can be serviced by a page. If so, we cache
4815 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4816 * the cache. If not so, we throw away the page, and cache the required
4817 * length. The next getxattr call will then produce another round trip to
4818 * the server, this time with the input buf of the required size.
4820 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4822 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
4823 struct nfs_getaclargs args = {
4824 .fh = NFS_FH(inode),
4828 struct nfs_getaclres res = {
4831 struct rpc_message msg = {
4832 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4836 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4837 int ret = -ENOMEM, i;
4839 /* As long as we're doing a round trip to the server anyway,
4840 * let's be prepared for a page of acl data. */
4843 if (npages > ARRAY_SIZE(pages))
4846 for (i = 0; i < npages; i++) {
4847 pages[i] = alloc_page(GFP_KERNEL);
4852 /* for decoding across pages */
4853 res.acl_scratch = alloc_page(GFP_KERNEL);
4854 if (!res.acl_scratch)
4857 args.acl_len = npages * PAGE_SIZE;
4859 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
4860 __func__, buf, buflen, npages, args.acl_len);
4861 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4862 &msg, &args.seq_args, &res.seq_res, 0);
4866 /* Handle the case where the passed-in buffer is too short */
4867 if (res.acl_flags & NFS4_ACL_TRUNC) {
4868 /* Did the user only issue a request for the acl length? */
4874 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
4876 if (res.acl_len > buflen) {
4880 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
4885 for (i = 0; i < npages; i++)
4887 __free_page(pages[i]);
4888 if (res.acl_scratch)
4889 __free_page(res.acl_scratch);
4893 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4895 struct nfs4_exception exception = { };
4898 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
4899 trace_nfs4_get_acl(inode, ret);
4902 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4903 } while (exception.retry);
4907 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4909 struct nfs_server *server = NFS_SERVER(inode);
4912 if (!nfs4_server_supports_acls(server))
4914 ret = nfs_revalidate_inode(server, inode);
4917 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4918 nfs_zap_acl_cache(inode);
4919 ret = nfs4_read_cached_acl(inode, buf, buflen);
4921 /* -ENOENT is returned if there is no ACL or if there is an ACL
4922 * but no cached acl data, just the acl length */
4924 return nfs4_get_acl_uncached(inode, buf, buflen);
4927 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4929 struct nfs_server *server = NFS_SERVER(inode);
4930 struct page *pages[NFS4ACL_MAXPAGES];
4931 struct nfs_setaclargs arg = {
4932 .fh = NFS_FH(inode),
4936 struct nfs_setaclres res;
4937 struct rpc_message msg = {
4938 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4942 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4945 if (!nfs4_server_supports_acls(server))
4947 if (npages > ARRAY_SIZE(pages))
4949 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
4952 nfs4_inode_return_delegation(inode);
4953 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4956 * Free each page after tx, so the only ref left is
4957 * held by the network stack
4960 put_page(pages[i-1]);
4963 * Acl update can result in inode attribute update.
4964 * so mark the attribute cache invalid.
4966 spin_lock(&inode->i_lock);
4967 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4968 spin_unlock(&inode->i_lock);
4969 nfs_access_zap_cache(inode);
4970 nfs_zap_acl_cache(inode);
4974 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4976 struct nfs4_exception exception = { };
4979 err = __nfs4_proc_set_acl(inode, buf, buflen);
4980 trace_nfs4_set_acl(inode, err);
4981 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4983 } while (exception.retry);
4987 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
4988 static int _nfs4_get_security_label(struct inode *inode, void *buf,
4991 struct nfs_server *server = NFS_SERVER(inode);
4992 struct nfs_fattr fattr;
4993 struct nfs4_label label = {0, 0, buflen, buf};
4995 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4996 struct nfs4_getattr_arg arg = {
4997 .fh = NFS_FH(inode),
5000 struct nfs4_getattr_res res = {
5005 struct rpc_message msg = {
5006 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
5012 nfs_fattr_init(&fattr);
5014 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
5017 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5019 if (buflen < label.len)
5024 static int nfs4_get_security_label(struct inode *inode, void *buf,
5027 struct nfs4_exception exception = { };
5030 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5034 err = _nfs4_get_security_label(inode, buf, buflen);
5035 trace_nfs4_get_security_label(inode, err);
5036 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5038 } while (exception.retry);
5042 static int _nfs4_do_set_security_label(struct inode *inode,
5043 struct nfs4_label *ilabel,
5044 struct nfs_fattr *fattr,
5045 struct nfs4_label *olabel)
5048 struct iattr sattr = {0};
5049 struct nfs_server *server = NFS_SERVER(inode);
5050 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5051 struct nfs_setattrargs arg = {
5052 .fh = NFS_FH(inode),
5058 struct nfs_setattrres res = {
5063 struct rpc_message msg = {
5064 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5070 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
5072 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5074 dprintk("%s failed: %d\n", __func__, status);
5079 static int nfs4_do_set_security_label(struct inode *inode,
5080 struct nfs4_label *ilabel,
5081 struct nfs_fattr *fattr,
5082 struct nfs4_label *olabel)
5084 struct nfs4_exception exception = { };
5088 err = _nfs4_do_set_security_label(inode, ilabel,
5090 trace_nfs4_set_security_label(inode, err);
5091 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5093 } while (exception.retry);
5098 nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
5100 struct nfs4_label ilabel, *olabel = NULL;
5101 struct nfs_fattr fattr;
5102 struct rpc_cred *cred;
5105 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5108 nfs_fattr_init(&fattr);
5112 ilabel.label = (char *)buf;
5113 ilabel.len = buflen;
5115 cred = rpc_lookup_cred();
5117 return PTR_ERR(cred);
5119 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5120 if (IS_ERR(olabel)) {
5121 status = -PTR_ERR(olabel);
5125 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5127 nfs_setsecurity(inode, &fattr, olabel);
5129 nfs4_label_free(olabel);
5134 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5137 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5138 nfs4_verifier *bootverf)
5142 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5143 /* An impossible timestamp guarantees this value
5144 * will never match a generated boot time. */
5146 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
5148 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
5149 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
5150 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
5152 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5156 nfs4_init_nonuniform_client_string(struct nfs_client *clp)
5161 if (clp->cl_owner_id != NULL)
5165 len = 14 + strlen(clp->cl_ipaddr) + 1 +
5166 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5168 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5172 if (len > NFS4_OPAQUE_LIMIT + 1)
5176 * Since this string is allocated at mount time, and held until the
5177 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5178 * about a memory-reclaim deadlock.
5180 str = kmalloc(len, GFP_KERNEL);
5185 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
5187 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5188 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
5191 clp->cl_owner_id = str;
5196 nfs4_init_uniquifier_client_string(struct nfs_client *clp)
5201 len = 10 + 10 + 1 + 10 + 1 +
5202 strlen(nfs4_client_id_uniquifier) + 1 +
5203 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5205 if (len > NFS4_OPAQUE_LIMIT + 1)
5209 * Since this string is allocated at mount time, and held until the
5210 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5211 * about a memory-reclaim deadlock.
5213 str = kmalloc(len, GFP_KERNEL);
5217 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
5218 clp->rpc_ops->version, clp->cl_minorversion,
5219 nfs4_client_id_uniquifier,
5220 clp->cl_rpcclient->cl_nodename);
5221 clp->cl_owner_id = str;
5226 nfs4_init_uniform_client_string(struct nfs_client *clp)
5231 if (clp->cl_owner_id != NULL)
5234 if (nfs4_client_id_uniquifier[0] != '\0')
5235 return nfs4_init_uniquifier_client_string(clp);
5237 len = 10 + 10 + 1 + 10 + 1 +
5238 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5240 if (len > NFS4_OPAQUE_LIMIT + 1)
5244 * Since this string is allocated at mount time, and held until the
5245 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5246 * about a memory-reclaim deadlock.
5248 str = kmalloc(len, GFP_KERNEL);
5252 scnprintf(str, len, "Linux NFSv%u.%u %s",
5253 clp->rpc_ops->version, clp->cl_minorversion,
5254 clp->cl_rpcclient->cl_nodename);
5255 clp->cl_owner_id = str;
5260 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5261 * services. Advertise one based on the address family of the
5265 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5267 if (strchr(clp->cl_ipaddr, ':') != NULL)
5268 return scnprintf(buf, len, "tcp6");
5270 return scnprintf(buf, len, "tcp");
5273 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5275 struct nfs4_setclientid *sc = calldata;
5277 if (task->tk_status == 0)
5278 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5281 static const struct rpc_call_ops nfs4_setclientid_ops = {
5282 .rpc_call_done = nfs4_setclientid_done,
5286 * nfs4_proc_setclientid - Negotiate client ID
5287 * @clp: state data structure
5288 * @program: RPC program for NFSv4 callback service
5289 * @port: IP port number for NFS4 callback service
5290 * @cred: RPC credential to use for this call
5291 * @res: where to place the result
5293 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5295 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5296 unsigned short port, struct rpc_cred *cred,
5297 struct nfs4_setclientid_res *res)
5299 nfs4_verifier sc_verifier;
5300 struct nfs4_setclientid setclientid = {
5301 .sc_verifier = &sc_verifier,
5305 struct rpc_message msg = {
5306 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5307 .rpc_argp = &setclientid,
5311 struct rpc_task *task;
5312 struct rpc_task_setup task_setup_data = {
5313 .rpc_client = clp->cl_rpcclient,
5314 .rpc_message = &msg,
5315 .callback_ops = &nfs4_setclientid_ops,
5316 .callback_data = &setclientid,
5317 .flags = RPC_TASK_TIMEOUT,
5321 /* nfs_client_id4 */
5322 nfs4_init_boot_verifier(clp, &sc_verifier);
5324 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5325 status = nfs4_init_uniform_client_string(clp);
5327 status = nfs4_init_nonuniform_client_string(clp);
5333 setclientid.sc_netid_len =
5334 nfs4_init_callback_netid(clp,
5335 setclientid.sc_netid,
5336 sizeof(setclientid.sc_netid));
5337 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
5338 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
5339 clp->cl_ipaddr, port >> 8, port & 255);
5341 dprintk("NFS call setclientid auth=%s, '%s'\n",
5342 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5344 task = rpc_run_task(&task_setup_data);
5346 status = PTR_ERR(task);
5349 status = task->tk_status;
5350 if (setclientid.sc_cred) {
5351 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5352 put_rpccred(setclientid.sc_cred);
5356 trace_nfs4_setclientid(clp, status);
5357 dprintk("NFS reply setclientid: %d\n", status);
5362 * nfs4_proc_setclientid_confirm - Confirm client ID
5363 * @clp: state data structure
5364 * @res: result of a previous SETCLIENTID
5365 * @cred: RPC credential to use for this call
5367 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5369 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
5370 struct nfs4_setclientid_res *arg,
5371 struct rpc_cred *cred)
5373 struct rpc_message msg = {
5374 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
5380 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5381 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5383 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5384 trace_nfs4_setclientid_confirm(clp, status);
5385 dprintk("NFS reply setclientid_confirm: %d\n", status);
5389 struct nfs4_delegreturndata {
5390 struct nfs4_delegreturnargs args;
5391 struct nfs4_delegreturnres res;
5393 nfs4_stateid stateid;
5394 unsigned long timestamp;
5395 struct nfs_fattr fattr;
5397 struct inode *inode;
5402 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5404 struct nfs4_delegreturndata *data = calldata;
5406 if (!nfs4_sequence_done(task, &data->res.seq_res))
5409 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
5410 switch (task->tk_status) {
5412 renew_lease(data->res.server, data->timestamp);
5413 case -NFS4ERR_ADMIN_REVOKED:
5414 case -NFS4ERR_DELEG_REVOKED:
5415 case -NFS4ERR_BAD_STATEID:
5416 case -NFS4ERR_OLD_STATEID:
5417 case -NFS4ERR_STALE_STATEID:
5418 case -NFS4ERR_EXPIRED:
5419 task->tk_status = 0;
5421 pnfs_roc_set_barrier(data->inode, data->roc_barrier);
5424 if (nfs4_async_handle_error(task, data->res.server,
5425 NULL, NULL) == -EAGAIN) {
5426 rpc_restart_call_prepare(task);
5430 data->rpc_status = task->tk_status;
5433 static void nfs4_delegreturn_release(void *calldata)
5435 struct nfs4_delegreturndata *data = calldata;
5436 struct inode *inode = data->inode;
5440 pnfs_roc_release(inode);
5441 nfs_iput_and_deactive(inode);
5446 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5448 struct nfs4_delegreturndata *d_data;
5450 d_data = (struct nfs4_delegreturndata *)data;
5452 if (nfs4_wait_on_layoutreturn(d_data->inode, task))
5456 pnfs_roc_get_barrier(d_data->inode, &d_data->roc_barrier);
5458 nfs4_setup_sequence(d_data->res.server,
5459 &d_data->args.seq_args,
5460 &d_data->res.seq_res,
5464 static const struct rpc_call_ops nfs4_delegreturn_ops = {
5465 .rpc_call_prepare = nfs4_delegreturn_prepare,
5466 .rpc_call_done = nfs4_delegreturn_done,
5467 .rpc_release = nfs4_delegreturn_release,
5470 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
5472 struct nfs4_delegreturndata *data;
5473 struct nfs_server *server = NFS_SERVER(inode);
5474 struct rpc_task *task;
5475 struct rpc_message msg = {
5476 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5479 struct rpc_task_setup task_setup_data = {
5480 .rpc_client = server->client,
5481 .rpc_message = &msg,
5482 .callback_ops = &nfs4_delegreturn_ops,
5483 .flags = RPC_TASK_ASYNC,
5487 data = kzalloc(sizeof(*data), GFP_NOFS);
5490 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
5492 nfs4_state_protect(server->nfs_client,
5493 NFS_SP4_MACH_CRED_CLEANUP,
5494 &task_setup_data.rpc_client, &msg);
5496 data->args.fhandle = &data->fh;
5497 data->args.stateid = &data->stateid;
5498 data->args.bitmask = server->cache_consistency_bitmask;
5499 nfs_copy_fh(&data->fh, NFS_FH(inode));
5500 nfs4_stateid_copy(&data->stateid, stateid);
5501 data->res.fattr = &data->fattr;
5502 data->res.server = server;
5503 nfs_fattr_init(data->res.fattr);
5504 data->timestamp = jiffies;
5505 data->rpc_status = 0;
5506 data->inode = nfs_igrab_and_active(inode);
5508 data->roc = nfs4_roc(inode);
5510 task_setup_data.callback_data = data;
5511 msg.rpc_argp = &data->args;
5512 msg.rpc_resp = &data->res;
5513 task = rpc_run_task(&task_setup_data);
5515 return PTR_ERR(task);
5518 status = nfs4_wait_for_completion_rpc_task(task);
5521 status = data->rpc_status;
5523 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5525 nfs_refresh_inode(inode, &data->fattr);
5531 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
5533 struct nfs_server *server = NFS_SERVER(inode);
5534 struct nfs4_exception exception = { };
5537 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
5538 trace_nfs4_delegreturn(inode, stateid, err);
5540 case -NFS4ERR_STALE_STATEID:
5541 case -NFS4ERR_EXPIRED:
5545 err = nfs4_handle_exception(server, err, &exception);
5546 } while (exception.retry);
5550 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5552 struct inode *inode = state->inode;
5553 struct nfs_server *server = NFS_SERVER(inode);
5554 struct nfs_client *clp = server->nfs_client;
5555 struct nfs_lockt_args arg = {
5556 .fh = NFS_FH(inode),
5559 struct nfs_lockt_res res = {
5562 struct rpc_message msg = {
5563 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5566 .rpc_cred = state->owner->so_cred,
5568 struct nfs4_lock_state *lsp;
5571 arg.lock_owner.clientid = clp->cl_clientid;
5572 status = nfs4_set_lock_state(state, request);
5575 lsp = request->fl_u.nfs4_fl.owner;
5576 arg.lock_owner.id = lsp->ls_seqid.owner_id;
5577 arg.lock_owner.s_dev = server->s_dev;
5578 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5581 request->fl_type = F_UNLCK;
5583 case -NFS4ERR_DENIED:
5586 request->fl_ops->fl_release_private(request);
5587 request->fl_ops = NULL;
5592 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5594 struct nfs4_exception exception = { };
5598 err = _nfs4_proc_getlk(state, cmd, request);
5599 trace_nfs4_get_lock(request, state, cmd, err);
5600 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
5602 } while (exception.retry);
5606 struct nfs4_unlockdata {
5607 struct nfs_locku_args arg;
5608 struct nfs_locku_res res;
5609 struct nfs4_lock_state *lsp;
5610 struct nfs_open_context *ctx;
5611 struct file_lock fl;
5612 struct nfs_server *server;
5613 unsigned long timestamp;
5616 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5617 struct nfs_open_context *ctx,
5618 struct nfs4_lock_state *lsp,
5619 struct nfs_seqid *seqid)
5621 struct nfs4_unlockdata *p;
5622 struct inode *inode = lsp->ls_state->inode;
5624 p = kzalloc(sizeof(*p), GFP_NOFS);
5627 p->arg.fh = NFS_FH(inode);
5629 p->arg.seqid = seqid;
5630 p->res.seqid = seqid;
5632 atomic_inc(&lsp->ls_count);
5633 /* Ensure we don't close file until we're done freeing locks! */
5634 p->ctx = get_nfs_open_context(ctx);
5635 memcpy(&p->fl, fl, sizeof(p->fl));
5636 p->server = NFS_SERVER(inode);
5640 static void nfs4_locku_release_calldata(void *data)
5642 struct nfs4_unlockdata *calldata = data;
5643 nfs_free_seqid(calldata->arg.seqid);
5644 nfs4_put_lock_state(calldata->lsp);
5645 put_nfs_open_context(calldata->ctx);
5649 static void nfs4_locku_done(struct rpc_task *task, void *data)
5651 struct nfs4_unlockdata *calldata = data;
5653 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5655 switch (task->tk_status) {
5657 renew_lease(calldata->server, calldata->timestamp);
5658 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
5659 if (nfs4_update_lock_stateid(calldata->lsp,
5660 &calldata->res.stateid))
5662 case -NFS4ERR_BAD_STATEID:
5663 case -NFS4ERR_OLD_STATEID:
5664 case -NFS4ERR_STALE_STATEID:
5665 case -NFS4ERR_EXPIRED:
5666 if (!nfs4_stateid_match(&calldata->arg.stateid,
5667 &calldata->lsp->ls_stateid))
5668 rpc_restart_call_prepare(task);
5671 if (nfs4_async_handle_error(task, calldata->server,
5672 NULL, NULL) == -EAGAIN)
5673 rpc_restart_call_prepare(task);
5675 nfs_release_seqid(calldata->arg.seqid);
5678 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
5680 struct nfs4_unlockdata *calldata = data;
5682 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
5684 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
5685 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
5686 /* Note: exit _without_ running nfs4_locku_done */
5689 calldata->timestamp = jiffies;
5690 if (nfs4_setup_sequence(calldata->server,
5691 &calldata->arg.seq_args,
5692 &calldata->res.seq_res,
5694 nfs_release_seqid(calldata->arg.seqid);
5697 task->tk_action = NULL;
5699 nfs4_sequence_done(task, &calldata->res.seq_res);
5702 static const struct rpc_call_ops nfs4_locku_ops = {
5703 .rpc_call_prepare = nfs4_locku_prepare,
5704 .rpc_call_done = nfs4_locku_done,
5705 .rpc_release = nfs4_locku_release_calldata,
5708 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5709 struct nfs_open_context *ctx,
5710 struct nfs4_lock_state *lsp,
5711 struct nfs_seqid *seqid)
5713 struct nfs4_unlockdata *data;
5714 struct rpc_message msg = {
5715 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5716 .rpc_cred = ctx->cred,
5718 struct rpc_task_setup task_setup_data = {
5719 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
5720 .rpc_message = &msg,
5721 .callback_ops = &nfs4_locku_ops,
5722 .workqueue = nfsiod_workqueue,
5723 .flags = RPC_TASK_ASYNC,
5726 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5727 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5729 /* Ensure this is an unlock - when canceling a lock, the
5730 * canceled lock is passed in, and it won't be an unlock.
5732 fl->fl_type = F_UNLCK;
5734 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5736 nfs_free_seqid(seqid);
5737 return ERR_PTR(-ENOMEM);
5740 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
5741 msg.rpc_argp = &data->arg;
5742 msg.rpc_resp = &data->res;
5743 task_setup_data.callback_data = data;
5744 return rpc_run_task(&task_setup_data);
5747 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5749 struct inode *inode = state->inode;
5750 struct nfs4_state_owner *sp = state->owner;
5751 struct nfs_inode *nfsi = NFS_I(inode);
5752 struct nfs_seqid *seqid;
5753 struct nfs4_lock_state *lsp;
5754 struct rpc_task *task;
5755 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
5757 unsigned char fl_flags = request->fl_flags;
5759 status = nfs4_set_lock_state(state, request);
5760 /* Unlock _before_ we do the RPC call */
5761 request->fl_flags |= FL_EXISTS;
5762 /* Exclude nfs_delegation_claim_locks() */
5763 mutex_lock(&sp->so_delegreturn_mutex);
5764 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
5765 down_read(&nfsi->rwsem);
5766 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
5767 up_read(&nfsi->rwsem);
5768 mutex_unlock(&sp->so_delegreturn_mutex);
5771 up_read(&nfsi->rwsem);
5772 mutex_unlock(&sp->so_delegreturn_mutex);
5775 /* Is this a delegated lock? */
5776 lsp = request->fl_u.nfs4_fl.owner;
5777 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5779 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
5780 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
5784 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
5785 status = PTR_ERR(task);
5788 status = nfs4_wait_for_completion_rpc_task(task);
5791 request->fl_flags = fl_flags;
5792 trace_nfs4_unlock(request, state, F_SETLK, status);
5796 struct nfs4_lockdata {
5797 struct nfs_lock_args arg;
5798 struct nfs_lock_res res;
5799 struct nfs4_lock_state *lsp;
5800 struct nfs_open_context *ctx;
5801 struct file_lock fl;
5802 unsigned long timestamp;
5805 struct nfs_server *server;
5808 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
5809 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5812 struct nfs4_lockdata *p;
5813 struct inode *inode = lsp->ls_state->inode;
5814 struct nfs_server *server = NFS_SERVER(inode);
5815 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
5817 p = kzalloc(sizeof(*p), gfp_mask);
5821 p->arg.fh = NFS_FH(inode);
5823 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
5824 if (IS_ERR(p->arg.open_seqid))
5826 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
5827 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
5828 if (IS_ERR(p->arg.lock_seqid))
5829 goto out_free_seqid;
5830 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
5831 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
5832 p->arg.lock_owner.s_dev = server->s_dev;
5833 p->res.lock_seqid = p->arg.lock_seqid;
5836 atomic_inc(&lsp->ls_count);
5837 p->ctx = get_nfs_open_context(ctx);
5838 get_file(fl->fl_file);
5839 memcpy(&p->fl, fl, sizeof(p->fl));
5842 nfs_free_seqid(p->arg.open_seqid);
5848 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5850 struct nfs4_lockdata *data = calldata;
5851 struct nfs4_state *state = data->lsp->ls_state;
5853 dprintk("%s: begin!\n", __func__);
5854 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
5856 /* Do we need to do an open_to_lock_owner? */
5857 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
5858 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
5859 goto out_release_lock_seqid;
5861 nfs4_stateid_copy(&data->arg.open_stateid,
5862 &state->open_stateid);
5863 data->arg.new_lock_owner = 1;
5864 data->res.open_seqid = data->arg.open_seqid;
5866 data->arg.new_lock_owner = 0;
5867 nfs4_stateid_copy(&data->arg.lock_stateid,
5868 &data->lsp->ls_stateid);
5870 if (!nfs4_valid_open_stateid(state)) {
5871 data->rpc_status = -EBADF;
5872 task->tk_action = NULL;
5873 goto out_release_open_seqid;
5875 data->timestamp = jiffies;
5876 if (nfs4_setup_sequence(data->server,
5877 &data->arg.seq_args,
5881 out_release_open_seqid:
5882 nfs_release_seqid(data->arg.open_seqid);
5883 out_release_lock_seqid:
5884 nfs_release_seqid(data->arg.lock_seqid);
5886 nfs4_sequence_done(task, &data->res.seq_res);
5887 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
5890 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5892 struct nfs4_lockdata *data = calldata;
5893 struct nfs4_lock_state *lsp = data->lsp;
5895 dprintk("%s: begin!\n", __func__);
5897 if (!nfs4_sequence_done(task, &data->res.seq_res))
5900 data->rpc_status = task->tk_status;
5901 switch (task->tk_status) {
5903 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
5905 if (data->arg.new_lock) {
5906 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
5907 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
5908 rpc_restart_call_prepare(task);
5912 if (data->arg.new_lock_owner != 0) {
5913 nfs_confirm_seqid(&lsp->ls_seqid, 0);
5914 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
5915 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
5916 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
5917 rpc_restart_call_prepare(task);
5919 case -NFS4ERR_BAD_STATEID:
5920 case -NFS4ERR_OLD_STATEID:
5921 case -NFS4ERR_STALE_STATEID:
5922 case -NFS4ERR_EXPIRED:
5923 if (data->arg.new_lock_owner != 0) {
5924 if (!nfs4_stateid_match(&data->arg.open_stateid,
5925 &lsp->ls_state->open_stateid))
5926 rpc_restart_call_prepare(task);
5927 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
5929 rpc_restart_call_prepare(task);
5931 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
5934 static void nfs4_lock_release(void *calldata)
5936 struct nfs4_lockdata *data = calldata;
5938 dprintk("%s: begin!\n", __func__);
5939 nfs_free_seqid(data->arg.open_seqid);
5940 if (data->cancelled != 0) {
5941 struct rpc_task *task;
5942 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5943 data->arg.lock_seqid);
5945 rpc_put_task_async(task);
5946 dprintk("%s: cancelling lock!\n", __func__);
5948 nfs_free_seqid(data->arg.lock_seqid);
5949 nfs4_put_lock_state(data->lsp);
5950 put_nfs_open_context(data->ctx);
5951 fput(data->fl.fl_file);
5953 dprintk("%s: done!\n", __func__);
5956 static const struct rpc_call_ops nfs4_lock_ops = {
5957 .rpc_call_prepare = nfs4_lock_prepare,
5958 .rpc_call_done = nfs4_lock_done,
5959 .rpc_release = nfs4_lock_release,
5962 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5965 case -NFS4ERR_ADMIN_REVOKED:
5966 case -NFS4ERR_BAD_STATEID:
5967 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
5968 if (new_lock_owner != 0 ||
5969 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
5970 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
5972 case -NFS4ERR_STALE_STATEID:
5973 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
5974 case -NFS4ERR_EXPIRED:
5975 nfs4_schedule_lease_recovery(server->nfs_client);
5979 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
5981 struct nfs4_lockdata *data;
5982 struct rpc_task *task;
5983 struct rpc_message msg = {
5984 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5985 .rpc_cred = state->owner->so_cred,
5987 struct rpc_task_setup task_setup_data = {
5988 .rpc_client = NFS_CLIENT(state->inode),
5989 .rpc_message = &msg,
5990 .callback_ops = &nfs4_lock_ops,
5991 .workqueue = nfsiod_workqueue,
5992 .flags = RPC_TASK_ASYNC,
5996 dprintk("%s: begin!\n", __func__);
5997 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
5998 fl->fl_u.nfs4_fl.owner,
5999 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
6003 data->arg.block = 1;
6004 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
6005 msg.rpc_argp = &data->arg;
6006 msg.rpc_resp = &data->res;
6007 task_setup_data.callback_data = data;
6008 if (recovery_type > NFS_LOCK_NEW) {
6009 if (recovery_type == NFS_LOCK_RECLAIM)
6010 data->arg.reclaim = NFS_LOCK_RECLAIM;
6011 nfs4_set_sequence_privileged(&data->arg.seq_args);
6013 data->arg.new_lock = 1;
6014 task = rpc_run_task(&task_setup_data);
6016 return PTR_ERR(task);
6017 ret = nfs4_wait_for_completion_rpc_task(task);
6019 ret = data->rpc_status;
6021 nfs4_handle_setlk_error(data->server, data->lsp,
6022 data->arg.new_lock_owner, ret);
6024 data->cancelled = 1;
6026 dprintk("%s: done, ret = %d!\n", __func__, ret);
6027 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
6031 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6033 struct nfs_server *server = NFS_SERVER(state->inode);
6034 struct nfs4_exception exception = {
6035 .inode = state->inode,
6040 /* Cache the lock if possible... */
6041 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6043 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
6044 if (err != -NFS4ERR_DELAY)
6046 nfs4_handle_exception(server, err, &exception);
6047 } while (exception.retry);
6051 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6053 struct nfs_server *server = NFS_SERVER(state->inode);
6054 struct nfs4_exception exception = {
6055 .inode = state->inode,
6059 err = nfs4_set_lock_state(state, request);
6062 if (!recover_lost_locks) {
6063 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6067 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6069 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
6073 case -NFS4ERR_GRACE:
6074 case -NFS4ERR_DELAY:
6075 nfs4_handle_exception(server, err, &exception);
6078 } while (exception.retry);
6083 #if defined(CONFIG_NFS_V4_1)
6085 * nfs41_check_expired_locks - possibly free a lock stateid
6087 * @state: NFSv4 state for an inode
6089 * Returns NFS_OK if recovery for this stateid is now finished.
6090 * Otherwise a negative NFS4ERR value is returned.
6092 static int nfs41_check_expired_locks(struct nfs4_state *state)
6094 int status, ret = -NFS4ERR_BAD_STATEID;
6095 struct nfs4_lock_state *lsp;
6096 struct nfs_server *server = NFS_SERVER(state->inode);
6098 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
6099 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
6100 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
6102 status = nfs41_test_and_free_expired_stateid(server,
6105 trace_nfs4_test_lock_stateid(state, lsp, status);
6106 if (status != NFS_OK) {
6107 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6116 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6118 int status = NFS_OK;
6120 if (test_bit(LK_STATE_IN_USE, &state->flags))
6121 status = nfs41_check_expired_locks(state);
6122 if (status != NFS_OK)
6123 status = nfs4_lock_expired(state, request);
6128 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6130 struct nfs_inode *nfsi = NFS_I(state->inode);
6131 struct nfs4_state_owner *sp = state->owner;
6132 unsigned char fl_flags = request->fl_flags;
6135 request->fl_flags |= FL_ACCESS;
6136 status = locks_lock_inode_wait(state->inode, request);
6139 mutex_lock(&sp->so_delegreturn_mutex);
6140 down_read(&nfsi->rwsem);
6141 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
6142 /* Yes: cache locks! */
6143 /* ...but avoid races with delegation recall... */
6144 request->fl_flags = fl_flags & ~FL_SLEEP;
6145 status = locks_lock_inode_wait(state->inode, request);
6146 up_read(&nfsi->rwsem);
6147 mutex_unlock(&sp->so_delegreturn_mutex);
6150 up_read(&nfsi->rwsem);
6151 mutex_unlock(&sp->so_delegreturn_mutex);
6152 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
6154 request->fl_flags = fl_flags;
6158 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6160 struct nfs4_exception exception = {
6162 .inode = state->inode,
6167 err = _nfs4_proc_setlk(state, cmd, request);
6168 if (err == -NFS4ERR_DENIED)
6170 err = nfs4_handle_exception(NFS_SERVER(state->inode),
6172 } while (exception.retry);
6176 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6177 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6180 nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6181 struct file_lock *request)
6183 int status = -ERESTARTSYS;
6184 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6186 while(!signalled()) {
6187 status = nfs4_proc_setlk(state, cmd, request);
6188 if ((status != -EAGAIN) || IS_SETLK(cmd))
6190 freezable_schedule_timeout_interruptible(timeout);
6192 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6193 status = -ERESTARTSYS;
6198 #ifdef CONFIG_NFS_V4_1
6199 struct nfs4_lock_waiter {
6200 struct task_struct *task;
6201 struct inode *inode;
6202 struct nfs_lowner *owner;
6207 nfs4_wake_lock_waiter(wait_queue_t *wait, unsigned int mode, int flags, void *key)
6210 struct cb_notify_lock_args *cbnl = key;
6211 struct nfs4_lock_waiter *waiter = wait->private;
6212 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6213 *wowner = waiter->owner;
6215 /* Only wake if the callback was for the same owner */
6216 if (lowner->clientid != wowner->clientid ||
6217 lowner->id != wowner->id ||
6218 lowner->s_dev != wowner->s_dev)
6221 /* Make sure it's for the right inode */
6222 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6225 waiter->notified = true;
6227 /* override "private" so we can use default_wake_function */
6228 wait->private = waiter->task;
6229 ret = autoremove_wake_function(wait, mode, flags, key);
6230 wait->private = waiter;
6235 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6237 int status = -ERESTARTSYS;
6238 unsigned long flags;
6239 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6240 struct nfs_server *server = NFS_SERVER(state->inode);
6241 struct nfs_client *clp = server->nfs_client;
6242 wait_queue_head_t *q = &clp->cl_lock_waitq;
6243 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6244 .id = lsp->ls_seqid.owner_id,
6245 .s_dev = server->s_dev };
6246 struct nfs4_lock_waiter waiter = { .task = current,
6247 .inode = state->inode,
6249 .notified = false };
6252 /* Don't bother with waitqueue if we don't expect a callback */
6253 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6254 return nfs4_retry_setlk_simple(state, cmd, request);
6257 wait.private = &waiter;
6258 wait.func = nfs4_wake_lock_waiter;
6259 add_wait_queue(q, &wait);
6261 while(!signalled()) {
6262 status = nfs4_proc_setlk(state, cmd, request);
6263 if ((status != -EAGAIN) || IS_SETLK(cmd))
6266 status = -ERESTARTSYS;
6267 spin_lock_irqsave(&q->lock, flags);
6268 if (waiter.notified) {
6269 spin_unlock_irqrestore(&q->lock, flags);
6272 set_current_state(TASK_INTERRUPTIBLE);
6273 spin_unlock_irqrestore(&q->lock, flags);
6275 freezable_schedule_timeout_interruptible(NFS4_LOCK_MAXTIMEOUT);
6278 finish_wait(q, &wait);
6281 #else /* !CONFIG_NFS_V4_1 */
6283 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6285 return nfs4_retry_setlk_simple(state, cmd, request);
6290 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6292 struct nfs_open_context *ctx;
6293 struct nfs4_state *state;
6296 /* verify open state */
6297 ctx = nfs_file_open_context(filp);
6300 if (request->fl_start < 0 || request->fl_end < 0)
6303 if (IS_GETLK(cmd)) {
6305 return nfs4_proc_getlk(state, F_GETLK, request);
6309 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6312 if (request->fl_type == F_UNLCK) {
6314 return nfs4_proc_unlck(state, cmd, request);
6321 if ((request->fl_flags & FL_POSIX) &&
6322 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6326 * Don't rely on the VFS having checked the file open mode,
6327 * since it won't do this for flock() locks.
6329 switch (request->fl_type) {
6331 if (!(filp->f_mode & FMODE_READ))
6335 if (!(filp->f_mode & FMODE_WRITE))
6339 status = nfs4_set_lock_state(state, request);
6343 return nfs4_retry_setlk(state, cmd, request);
6346 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
6348 struct nfs_server *server = NFS_SERVER(state->inode);
6351 err = nfs4_set_lock_state(state, fl);
6354 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
6355 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
6358 struct nfs_release_lockowner_data {
6359 struct nfs4_lock_state *lsp;
6360 struct nfs_server *server;
6361 struct nfs_release_lockowner_args args;
6362 struct nfs_release_lockowner_res res;
6363 unsigned long timestamp;
6366 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6368 struct nfs_release_lockowner_data *data = calldata;
6369 struct nfs_server *server = data->server;
6370 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
6371 &data->args.seq_args, &data->res.seq_res, task);
6372 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6373 data->timestamp = jiffies;
6376 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6378 struct nfs_release_lockowner_data *data = calldata;
6379 struct nfs_server *server = data->server;
6381 nfs40_sequence_done(task, &data->res.seq_res);
6383 switch (task->tk_status) {
6385 renew_lease(server, data->timestamp);
6387 case -NFS4ERR_STALE_CLIENTID:
6388 case -NFS4ERR_EXPIRED:
6389 nfs4_schedule_lease_recovery(server->nfs_client);
6391 case -NFS4ERR_LEASE_MOVED:
6392 case -NFS4ERR_DELAY:
6393 if (nfs4_async_handle_error(task, server,
6394 NULL, NULL) == -EAGAIN)
6395 rpc_restart_call_prepare(task);
6399 static void nfs4_release_lockowner_release(void *calldata)
6401 struct nfs_release_lockowner_data *data = calldata;
6402 nfs4_free_lock_state(data->server, data->lsp);
6406 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
6407 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6408 .rpc_call_done = nfs4_release_lockowner_done,
6409 .rpc_release = nfs4_release_lockowner_release,
6413 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
6415 struct nfs_release_lockowner_data *data;
6416 struct rpc_message msg = {
6417 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6420 if (server->nfs_client->cl_mvops->minor_version != 0)
6423 data = kmalloc(sizeof(*data), GFP_NOFS);
6427 data->server = server;
6428 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6429 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6430 data->args.lock_owner.s_dev = server->s_dev;
6432 msg.rpc_argp = &data->args;
6433 msg.rpc_resp = &data->res;
6434 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
6435 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
6438 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6440 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
6441 struct dentry *unused, struct inode *inode,
6442 const char *key, const void *buf,
6443 size_t buflen, int flags)
6445 return nfs4_proc_set_acl(inode, buf, buflen);
6448 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
6449 struct dentry *unused, struct inode *inode,
6450 const char *key, void *buf, size_t buflen)
6452 return nfs4_proc_get_acl(inode, buf, buflen);
6455 static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
6457 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
6460 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
6462 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
6463 struct dentry *unused, struct inode *inode,
6464 const char *key, const void *buf,
6465 size_t buflen, int flags)
6467 if (security_ismaclabel(key))
6468 return nfs4_set_security_label(inode, buf, buflen);
6473 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
6474 struct dentry *unused, struct inode *inode,
6475 const char *key, void *buf, size_t buflen)
6477 if (security_ismaclabel(key))
6478 return nfs4_get_security_label(inode, buf, buflen);
6483 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6487 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6488 len = security_inode_listsecurity(inode, list, list_len);
6489 if (list_len && len > list_len)
6495 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6496 .prefix = XATTR_SECURITY_PREFIX,
6497 .get = nfs4_xattr_get_nfs4_label,
6498 .set = nfs4_xattr_set_nfs4_label,
6504 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6512 * nfs_fhget will use either the mounted_on_fileid or the fileid
6514 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6516 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6517 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6518 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
6519 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
6522 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
6523 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
6524 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6528 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6529 const struct qstr *name,
6530 struct nfs4_fs_locations *fs_locations,
6533 struct nfs_server *server = NFS_SERVER(dir);
6535 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6537 struct nfs4_fs_locations_arg args = {
6538 .dir_fh = NFS_FH(dir),
6543 struct nfs4_fs_locations_res res = {
6544 .fs_locations = fs_locations,
6546 struct rpc_message msg = {
6547 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6553 dprintk("%s: start\n", __func__);
6555 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6556 * is not supported */
6557 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6558 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6560 bitmask[0] |= FATTR4_WORD0_FILEID;
6562 nfs_fattr_init(&fs_locations->fattr);
6563 fs_locations->server = server;
6564 fs_locations->nlocations = 0;
6565 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
6566 dprintk("%s: returned status = %d\n", __func__, status);
6570 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6571 const struct qstr *name,
6572 struct nfs4_fs_locations *fs_locations,
6575 struct nfs4_exception exception = { };
6578 err = _nfs4_proc_fs_locations(client, dir, name,
6579 fs_locations, page);
6580 trace_nfs4_get_fs_locations(dir, name, err);
6581 err = nfs4_handle_exception(NFS_SERVER(dir), err,
6583 } while (exception.retry);
6588 * This operation also signals the server that this client is
6589 * performing migration recovery. The server can stop returning
6590 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6591 * appended to this compound to identify the client ID which is
6592 * performing recovery.
6594 static int _nfs40_proc_get_locations(struct inode *inode,
6595 struct nfs4_fs_locations *locations,
6596 struct page *page, struct rpc_cred *cred)
6598 struct nfs_server *server = NFS_SERVER(inode);
6599 struct rpc_clnt *clnt = server->client;
6601 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6603 struct nfs4_fs_locations_arg args = {
6604 .clientid = server->nfs_client->cl_clientid,
6605 .fh = NFS_FH(inode),
6608 .migration = 1, /* skip LOOKUP */
6609 .renew = 1, /* append RENEW */
6611 struct nfs4_fs_locations_res res = {
6612 .fs_locations = locations,
6616 struct rpc_message msg = {
6617 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6622 unsigned long now = jiffies;
6625 nfs_fattr_init(&locations->fattr);
6626 locations->server = server;
6627 locations->nlocations = 0;
6629 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6630 nfs4_set_sequence_privileged(&args.seq_args);
6631 status = nfs4_call_sync_sequence(clnt, server, &msg,
6632 &args.seq_args, &res.seq_res);
6636 renew_lease(server, now);
6640 #ifdef CONFIG_NFS_V4_1
6643 * This operation also signals the server that this client is
6644 * performing migration recovery. The server can stop asserting
6645 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6646 * performing this operation is identified in the SEQUENCE
6647 * operation in this compound.
6649 * When the client supports GETATTR(fs_locations_info), it can
6650 * be plumbed in here.
6652 static int _nfs41_proc_get_locations(struct inode *inode,
6653 struct nfs4_fs_locations *locations,
6654 struct page *page, struct rpc_cred *cred)
6656 struct nfs_server *server = NFS_SERVER(inode);
6657 struct rpc_clnt *clnt = server->client;
6659 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6661 struct nfs4_fs_locations_arg args = {
6662 .fh = NFS_FH(inode),
6665 .migration = 1, /* skip LOOKUP */
6667 struct nfs4_fs_locations_res res = {
6668 .fs_locations = locations,
6671 struct rpc_message msg = {
6672 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6679 nfs_fattr_init(&locations->fattr);
6680 locations->server = server;
6681 locations->nlocations = 0;
6683 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6684 nfs4_set_sequence_privileged(&args.seq_args);
6685 status = nfs4_call_sync_sequence(clnt, server, &msg,
6686 &args.seq_args, &res.seq_res);
6687 if (status == NFS4_OK &&
6688 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6689 status = -NFS4ERR_LEASE_MOVED;
6693 #endif /* CONFIG_NFS_V4_1 */
6696 * nfs4_proc_get_locations - discover locations for a migrated FSID
6697 * @inode: inode on FSID that is migrating
6698 * @locations: result of query
6700 * @cred: credential to use for this operation
6702 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6703 * operation failed, or a negative errno if a local error occurred.
6705 * On success, "locations" is filled in, but if the server has
6706 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6709 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6710 * from this client that require migration recovery.
6712 int nfs4_proc_get_locations(struct inode *inode,
6713 struct nfs4_fs_locations *locations,
6714 struct page *page, struct rpc_cred *cred)
6716 struct nfs_server *server = NFS_SERVER(inode);
6717 struct nfs_client *clp = server->nfs_client;
6718 const struct nfs4_mig_recovery_ops *ops =
6719 clp->cl_mvops->mig_recovery_ops;
6720 struct nfs4_exception exception = { };
6723 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6724 (unsigned long long)server->fsid.major,
6725 (unsigned long long)server->fsid.minor,
6727 nfs_display_fhandle(NFS_FH(inode), __func__);
6730 status = ops->get_locations(inode, locations, page, cred);
6731 if (status != -NFS4ERR_DELAY)
6733 nfs4_handle_exception(server, status, &exception);
6734 } while (exception.retry);
6739 * This operation also signals the server that this client is
6740 * performing "lease moved" recovery. The server can stop
6741 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6742 * is appended to this compound to identify the client ID which is
6743 * performing recovery.
6745 static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6747 struct nfs_server *server = NFS_SERVER(inode);
6748 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6749 struct rpc_clnt *clnt = server->client;
6750 struct nfs4_fsid_present_arg args = {
6751 .fh = NFS_FH(inode),
6752 .clientid = clp->cl_clientid,
6753 .renew = 1, /* append RENEW */
6755 struct nfs4_fsid_present_res res = {
6758 struct rpc_message msg = {
6759 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6764 unsigned long now = jiffies;
6767 res.fh = nfs_alloc_fhandle();
6771 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6772 nfs4_set_sequence_privileged(&args.seq_args);
6773 status = nfs4_call_sync_sequence(clnt, server, &msg,
6774 &args.seq_args, &res.seq_res);
6775 nfs_free_fhandle(res.fh);
6779 do_renew_lease(clp, now);
6783 #ifdef CONFIG_NFS_V4_1
6786 * This operation also signals the server that this client is
6787 * performing "lease moved" recovery. The server can stop asserting
6788 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6789 * this operation is identified in the SEQUENCE operation in this
6792 static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6794 struct nfs_server *server = NFS_SERVER(inode);
6795 struct rpc_clnt *clnt = server->client;
6796 struct nfs4_fsid_present_arg args = {
6797 .fh = NFS_FH(inode),
6799 struct nfs4_fsid_present_res res = {
6801 struct rpc_message msg = {
6802 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6809 res.fh = nfs_alloc_fhandle();
6813 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6814 nfs4_set_sequence_privileged(&args.seq_args);
6815 status = nfs4_call_sync_sequence(clnt, server, &msg,
6816 &args.seq_args, &res.seq_res);
6817 nfs_free_fhandle(res.fh);
6818 if (status == NFS4_OK &&
6819 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6820 status = -NFS4ERR_LEASE_MOVED;
6824 #endif /* CONFIG_NFS_V4_1 */
6827 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6828 * @inode: inode on FSID to check
6829 * @cred: credential to use for this operation
6831 * Server indicates whether the FSID is present, moved, or not
6832 * recognized. This operation is necessary to clear a LEASE_MOVED
6833 * condition for this client ID.
6835 * Returns NFS4_OK if the FSID is present on this server,
6836 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6837 * NFS4ERR code if some error occurred on the server, or a
6838 * negative errno if a local failure occurred.
6840 int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6842 struct nfs_server *server = NFS_SERVER(inode);
6843 struct nfs_client *clp = server->nfs_client;
6844 const struct nfs4_mig_recovery_ops *ops =
6845 clp->cl_mvops->mig_recovery_ops;
6846 struct nfs4_exception exception = { };
6849 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6850 (unsigned long long)server->fsid.major,
6851 (unsigned long long)server->fsid.minor,
6853 nfs_display_fhandle(NFS_FH(inode), __func__);
6856 status = ops->fsid_present(inode, cred);
6857 if (status != -NFS4ERR_DELAY)
6859 nfs4_handle_exception(server, status, &exception);
6860 } while (exception.retry);
6865 * If 'use_integrity' is true and the state managment nfs_client
6866 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
6867 * and the machine credential as per RFC3530bis and RFC5661 Security
6868 * Considerations sections. Otherwise, just use the user cred with the
6869 * filesystem's rpc_client.
6871 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
6874 struct nfs4_secinfo_arg args = {
6875 .dir_fh = NFS_FH(dir),
6878 struct nfs4_secinfo_res res = {
6881 struct rpc_message msg = {
6882 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
6886 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
6887 struct rpc_cred *cred = NULL;
6889 if (use_integrity) {
6890 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
6891 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
6892 msg.rpc_cred = cred;
6895 dprintk("NFS call secinfo %s\n", name->name);
6897 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
6898 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
6900 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
6902 dprintk("NFS reply secinfo: %d\n", status);
6910 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
6911 struct nfs4_secinfo_flavors *flavors)
6913 struct nfs4_exception exception = { };
6916 err = -NFS4ERR_WRONGSEC;
6918 /* try to use integrity protection with machine cred */
6919 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
6920 err = _nfs4_proc_secinfo(dir, name, flavors, true);
6923 * if unable to use integrity protection, or SECINFO with
6924 * integrity protection returns NFS4ERR_WRONGSEC (which is
6925 * disallowed by spec, but exists in deployed servers) use
6926 * the current filesystem's rpc_client and the user cred.
6928 if (err == -NFS4ERR_WRONGSEC)
6929 err = _nfs4_proc_secinfo(dir, name, flavors, false);
6931 trace_nfs4_secinfo(dir, name, err);
6932 err = nfs4_handle_exception(NFS_SERVER(dir), err,
6934 } while (exception.retry);
6938 #ifdef CONFIG_NFS_V4_1
6940 * Check the exchange flags returned by the server for invalid flags, having
6941 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
6944 static int nfs4_check_cl_exchange_flags(u32 flags)
6946 if (flags & ~EXCHGID4_FLAG_MASK_R)
6948 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
6949 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
6951 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
6955 return -NFS4ERR_INVAL;
6959 nfs41_same_server_scope(struct nfs41_server_scope *a,
6960 struct nfs41_server_scope *b)
6962 if (a->server_scope_sz == b->server_scope_sz &&
6963 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
6970 nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
6974 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
6975 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
6979 * nfs4_proc_bind_one_conn_to_session()
6981 * The 4.1 client currently uses the same TCP connection for the
6982 * fore and backchannel.
6985 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
6986 struct rpc_xprt *xprt,
6987 struct nfs_client *clp,
6988 struct rpc_cred *cred)
6991 struct nfs41_bind_conn_to_session_args args = {
6993 .dir = NFS4_CDFC4_FORE_OR_BOTH,
6995 struct nfs41_bind_conn_to_session_res res;
6996 struct rpc_message msg = {
6998 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
7003 struct rpc_task_setup task_setup_data = {
7006 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
7007 .rpc_message = &msg,
7008 .flags = RPC_TASK_TIMEOUT,
7010 struct rpc_task *task;
7012 dprintk("--> %s\n", __func__);
7014 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7015 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7016 args.dir = NFS4_CDFC4_FORE;
7018 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7019 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7020 args.dir = NFS4_CDFC4_FORE;
7022 task = rpc_run_task(&task_setup_data);
7023 if (!IS_ERR(task)) {
7024 status = task->tk_status;
7027 status = PTR_ERR(task);
7028 trace_nfs4_bind_conn_to_session(clp, status);
7030 if (memcmp(res.sessionid.data,
7031 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7032 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7036 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
7037 dprintk("NFS: %s: Unexpected direction from server\n",
7042 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
7043 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7050 dprintk("<-- %s status= %d\n", __func__, status);
7054 struct rpc_bind_conn_calldata {
7055 struct nfs_client *clp;
7056 struct rpc_cred *cred;
7060 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7061 struct rpc_xprt *xprt,
7064 struct rpc_bind_conn_calldata *p = calldata;
7066 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7069 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7071 struct rpc_bind_conn_calldata data = {
7075 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7076 nfs4_proc_bind_conn_to_session_callback, &data);
7080 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7081 * and operations we'd like to see to enable certain features in the allow map
7083 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7084 .how = SP4_MACH_CRED,
7085 .enforce.u.words = {
7086 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7087 1 << (OP_EXCHANGE_ID - 32) |
7088 1 << (OP_CREATE_SESSION - 32) |
7089 1 << (OP_DESTROY_SESSION - 32) |
7090 1 << (OP_DESTROY_CLIENTID - 32)
7093 [0] = 1 << (OP_CLOSE) |
7094 1 << (OP_OPEN_DOWNGRADE) |
7096 1 << (OP_DELEGRETURN) |
7098 [1] = 1 << (OP_SECINFO - 32) |
7099 1 << (OP_SECINFO_NO_NAME - 32) |
7100 1 << (OP_LAYOUTRETURN - 32) |
7101 1 << (OP_TEST_STATEID - 32) |
7102 1 << (OP_FREE_STATEID - 32) |
7103 1 << (OP_WRITE - 32)
7108 * Select the state protection mode for client `clp' given the server results
7109 * from exchange_id in `sp'.
7111 * Returns 0 on success, negative errno otherwise.
7113 static int nfs4_sp4_select_mode(struct nfs_client *clp,
7114 struct nfs41_state_protection *sp)
7116 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7117 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7118 1 << (OP_EXCHANGE_ID - 32) |
7119 1 << (OP_CREATE_SESSION - 32) |
7120 1 << (OP_DESTROY_SESSION - 32) |
7121 1 << (OP_DESTROY_CLIENTID - 32)
7125 if (sp->how == SP4_MACH_CRED) {
7126 /* Print state protect result */
7127 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7128 for (i = 0; i <= LAST_NFS4_OP; i++) {
7129 if (test_bit(i, sp->enforce.u.longs))
7130 dfprintk(MOUNT, " enforce op %d\n", i);
7131 if (test_bit(i, sp->allow.u.longs))
7132 dfprintk(MOUNT, " allow op %d\n", i);
7135 /* make sure nothing is on enforce list that isn't supported */
7136 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7137 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7138 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7144 * Minimal mode - state operations are allowed to use machine
7145 * credential. Note this already happens by default, so the
7146 * client doesn't have to do anything more than the negotiation.
7148 * NOTE: we don't care if EXCHANGE_ID is in the list -
7149 * we're already using the machine cred for exchange_id
7150 * and will never use a different cred.
7152 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7153 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7154 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7155 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7156 dfprintk(MOUNT, "sp4_mach_cred:\n");
7157 dfprintk(MOUNT, " minimal mode enabled\n");
7158 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
7160 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7164 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
7165 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7166 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
7167 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7168 dfprintk(MOUNT, " cleanup mode enabled\n");
7169 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
7172 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7173 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7174 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
7175 &clp->cl_sp4_flags);
7178 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7179 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7180 dfprintk(MOUNT, " secinfo mode enabled\n");
7181 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
7184 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7185 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7186 dfprintk(MOUNT, " stateid mode enabled\n");
7187 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
7190 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7191 dfprintk(MOUNT, " write mode enabled\n");
7192 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
7195 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7196 dfprintk(MOUNT, " commit mode enabled\n");
7197 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
7204 struct nfs41_exchange_id_data {
7205 struct nfs41_exchange_id_res res;
7206 struct nfs41_exchange_id_args args;
7207 struct rpc_xprt *xprt;
7211 static void nfs4_exchange_id_done(struct rpc_task *task, void *data)
7213 struct nfs41_exchange_id_data *cdata =
7214 (struct nfs41_exchange_id_data *)data;
7215 struct nfs_client *clp = cdata->args.client;
7216 int status = task->tk_status;
7218 trace_nfs4_exchange_id(clp, status);
7221 status = nfs4_check_cl_exchange_flags(cdata->res.flags);
7223 if (cdata->xprt && status == 0) {
7224 status = nfs4_detect_session_trunking(clp, &cdata->res,
7230 status = nfs4_sp4_select_mode(clp, &cdata->res.state_protect);
7233 clp->cl_clientid = cdata->res.clientid;
7234 clp->cl_exchange_flags = cdata->res.flags;
7235 /* Client ID is not confirmed */
7236 if (!(cdata->res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
7237 clear_bit(NFS4_SESSION_ESTABLISHED,
7238 &clp->cl_session->session_state);
7239 clp->cl_seqid = cdata->res.seqid;
7242 kfree(clp->cl_serverowner);
7243 clp->cl_serverowner = cdata->res.server_owner;
7244 cdata->res.server_owner = NULL;
7246 /* use the most recent implementation id */
7247 kfree(clp->cl_implid);
7248 clp->cl_implid = cdata->res.impl_id;
7249 cdata->res.impl_id = NULL;
7251 if (clp->cl_serverscope != NULL &&
7252 !nfs41_same_server_scope(clp->cl_serverscope,
7253 cdata->res.server_scope)) {
7254 dprintk("%s: server_scope mismatch detected\n",
7256 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
7257 kfree(clp->cl_serverscope);
7258 clp->cl_serverscope = NULL;
7261 if (clp->cl_serverscope == NULL) {
7262 clp->cl_serverscope = cdata->res.server_scope;
7263 cdata->res.server_scope = NULL;
7265 /* Save the EXCHANGE_ID verifier session trunk tests */
7266 memcpy(clp->cl_confirm.data, cdata->args.verifier->data,
7267 sizeof(clp->cl_confirm.data));
7270 cdata->rpc_status = status;
7274 static void nfs4_exchange_id_release(void *data)
7276 struct nfs41_exchange_id_data *cdata =
7277 (struct nfs41_exchange_id_data *)data;
7279 nfs_put_client(cdata->args.client);
7281 xprt_put(cdata->xprt);
7282 rpc_clnt_xprt_switch_put(cdata->args.client->cl_rpcclient);
7284 kfree(cdata->res.impl_id);
7285 kfree(cdata->res.server_scope);
7286 kfree(cdata->res.server_owner);
7290 static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7291 .rpc_call_done = nfs4_exchange_id_done,
7292 .rpc_release = nfs4_exchange_id_release,
7296 * _nfs4_proc_exchange_id()
7298 * Wrapper for EXCHANGE_ID operation.
7300 static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
7301 u32 sp4_how, struct rpc_xprt *xprt)
7303 nfs4_verifier verifier;
7304 struct rpc_message msg = {
7305 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
7308 struct rpc_task_setup task_setup_data = {
7309 .rpc_client = clp->cl_rpcclient,
7310 .callback_ops = &nfs4_exchange_id_call_ops,
7311 .rpc_message = &msg,
7312 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7314 struct nfs41_exchange_id_data *calldata;
7315 struct rpc_task *task;
7318 if (!atomic_inc_not_zero(&clp->cl_count))
7322 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7327 nfs4_init_boot_verifier(clp, &verifier);
7329 status = nfs4_init_uniform_client_string(clp);
7333 dprintk("NFS call exchange_id auth=%s, '%s'\n",
7334 clp->cl_rpcclient->cl_auth->au_ops->au_name,
7337 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7340 if (unlikely(calldata->res.server_owner == NULL))
7343 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
7345 if (unlikely(calldata->res.server_scope == NULL))
7346 goto out_server_owner;
7348 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7349 if (unlikely(calldata->res.impl_id == NULL))
7350 goto out_server_scope;
7354 calldata->args.state_protect.how = SP4_NONE;
7358 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
7368 calldata->xprt = xprt;
7369 task_setup_data.rpc_xprt = xprt;
7370 task_setup_data.flags =
7371 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC;
7372 calldata->args.verifier = &clp->cl_confirm;
7374 calldata->args.verifier = &verifier;
7376 calldata->args.client = clp;
7377 #ifdef CONFIG_NFS_V4_1_MIGRATION
7378 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7379 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7380 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7382 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7383 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7385 msg.rpc_argp = &calldata->args;
7386 msg.rpc_resp = &calldata->res;
7387 task_setup_data.callback_data = calldata;
7389 task = rpc_run_task(&task_setup_data);
7391 status = PTR_ERR(task);
7396 status = rpc_wait_for_completion_task(task);
7398 status = calldata->rpc_status;
7399 } else /* session trunking test */
7400 status = calldata->rpc_status;
7404 if (clp->cl_implid != NULL)
7405 dprintk("NFS reply exchange_id: Server Implementation ID: "
7406 "domain: %s, name: %s, date: %llu,%u\n",
7407 clp->cl_implid->domain, clp->cl_implid->name,
7408 clp->cl_implid->date.seconds,
7409 clp->cl_implid->date.nseconds);
7410 dprintk("NFS reply exchange_id: %d\n", status);
7414 kfree(calldata->res.impl_id);
7416 kfree(calldata->res.server_scope);
7418 kfree(calldata->res.server_owner);
7425 * nfs4_proc_exchange_id()
7427 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7429 * Since the clientid has expired, all compounds using sessions
7430 * associated with the stale clientid will be returning
7431 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7432 * be in some phase of session reset.
7434 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7436 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7438 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7441 /* try SP4_MACH_CRED if krb5i/p */
7442 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7443 authflavor == RPC_AUTH_GSS_KRB5P) {
7444 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED, NULL);
7450 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE, NULL);
7454 * nfs4_test_session_trunk
7456 * This is an add_xprt_test() test function called from
7457 * rpc_clnt_setup_test_and_add_xprt.
7459 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7460 * and is dereferrenced in nfs4_exchange_id_release
7462 * Upon success, add the new transport to the rpc_clnt
7464 * @clnt: struct rpc_clnt to get new transport
7465 * @xprt: the rpc_xprt to test
7466 * @data: call data for _nfs4_proc_exchange_id.
7468 int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7471 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
7474 dprintk("--> %s try %s\n", __func__,
7475 xprt->address_strings[RPC_DISPLAY_ADDR]);
7477 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7479 /* Test connection for session trunking. Async exchange_id call */
7480 return _nfs4_proc_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7482 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7484 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7485 struct rpc_cred *cred)
7487 struct rpc_message msg = {
7488 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7494 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7495 trace_nfs4_destroy_clientid(clp, status);
7497 dprintk("NFS: Got error %d from the server %s on "
7498 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7502 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7503 struct rpc_cred *cred)
7508 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7509 ret = _nfs4_proc_destroy_clientid(clp, cred);
7511 case -NFS4ERR_DELAY:
7512 case -NFS4ERR_CLIENTID_BUSY:
7522 int nfs4_destroy_clientid(struct nfs_client *clp)
7524 struct rpc_cred *cred;
7527 if (clp->cl_mvops->minor_version < 1)
7529 if (clp->cl_exchange_flags == 0)
7531 if (clp->cl_preserve_clid)
7533 cred = nfs4_get_clid_cred(clp);
7534 ret = nfs4_proc_destroy_clientid(clp, cred);
7539 case -NFS4ERR_STALE_CLIENTID:
7540 clp->cl_exchange_flags = 0;
7546 struct nfs4_get_lease_time_data {
7547 struct nfs4_get_lease_time_args *args;
7548 struct nfs4_get_lease_time_res *res;
7549 struct nfs_client *clp;
7552 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7555 struct nfs4_get_lease_time_data *data =
7556 (struct nfs4_get_lease_time_data *)calldata;
7558 dprintk("--> %s\n", __func__);
7559 /* just setup sequence, do not trigger session recovery
7560 since we're invoked within one */
7561 nfs41_setup_sequence(data->clp->cl_session,
7562 &data->args->la_seq_args,
7563 &data->res->lr_seq_res,
7565 dprintk("<-- %s\n", __func__);
7569 * Called from nfs4_state_manager thread for session setup, so don't recover
7570 * from sequence operation or clientid errors.
7572 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7574 struct nfs4_get_lease_time_data *data =
7575 (struct nfs4_get_lease_time_data *)calldata;
7577 dprintk("--> %s\n", __func__);
7578 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7580 switch (task->tk_status) {
7581 case -NFS4ERR_DELAY:
7582 case -NFS4ERR_GRACE:
7583 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7584 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7585 task->tk_status = 0;
7587 case -NFS4ERR_RETRY_UNCACHED_REP:
7588 rpc_restart_call_prepare(task);
7591 dprintk("<-- %s\n", __func__);
7594 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
7595 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7596 .rpc_call_done = nfs4_get_lease_time_done,
7599 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7601 struct rpc_task *task;
7602 struct nfs4_get_lease_time_args args;
7603 struct nfs4_get_lease_time_res res = {
7604 .lr_fsinfo = fsinfo,
7606 struct nfs4_get_lease_time_data data = {
7611 struct rpc_message msg = {
7612 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7616 struct rpc_task_setup task_setup = {
7617 .rpc_client = clp->cl_rpcclient,
7618 .rpc_message = &msg,
7619 .callback_ops = &nfs4_get_lease_time_ops,
7620 .callback_data = &data,
7621 .flags = RPC_TASK_TIMEOUT,
7625 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
7626 nfs4_set_sequence_privileged(&args.la_seq_args);
7627 dprintk("--> %s\n", __func__);
7628 task = rpc_run_task(&task_setup);
7631 status = PTR_ERR(task);
7633 status = task->tk_status;
7636 dprintk("<-- %s return %d\n", __func__, status);
7642 * Initialize the values to be used by the client in CREATE_SESSION
7643 * If nfs4_init_session set the fore channel request and response sizes,
7646 * Set the back channel max_resp_sz_cached to zero to force the client to
7647 * always set csa_cachethis to FALSE because the current implementation
7648 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7650 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7651 struct rpc_clnt *clnt)
7653 unsigned int max_rqst_sz, max_resp_sz;
7654 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
7656 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7657 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7659 /* Fore channel attributes */
7660 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7661 args->fc_attrs.max_resp_sz = max_resp_sz;
7662 args->fc_attrs.max_ops = NFS4_MAX_OPS;
7663 args->fc_attrs.max_reqs = max_session_slots;
7665 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
7666 "max_ops=%u max_reqs=%u\n",
7668 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
7669 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
7671 /* Back channel attributes */
7672 args->bc_attrs.max_rqst_sz = max_bc_payload;
7673 args->bc_attrs.max_resp_sz = max_bc_payload;
7674 args->bc_attrs.max_resp_sz_cached = 0;
7675 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
7676 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
7678 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7679 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7681 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7682 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7683 args->bc_attrs.max_reqs);
7686 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7687 struct nfs41_create_session_res *res)
7689 struct nfs4_channel_attrs *sent = &args->fc_attrs;
7690 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
7692 if (rcvd->max_resp_sz > sent->max_resp_sz)
7695 * Our requested max_ops is the minimum we need; we're not
7696 * prepared to break up compounds into smaller pieces than that.
7697 * So, no point even trying to continue if the server won't
7700 if (rcvd->max_ops < sent->max_ops)
7702 if (rcvd->max_reqs == 0)
7704 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7705 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
7709 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7710 struct nfs41_create_session_res *res)
7712 struct nfs4_channel_attrs *sent = &args->bc_attrs;
7713 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
7715 if (!(res->flags & SESSION4_BACK_CHAN))
7717 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7719 if (rcvd->max_resp_sz < sent->max_resp_sz)
7721 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7723 if (rcvd->max_ops > sent->max_ops)
7725 if (rcvd->max_reqs > sent->max_reqs)
7731 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
7732 struct nfs41_create_session_res *res)
7736 ret = nfs4_verify_fore_channel_attrs(args, res);
7739 return nfs4_verify_back_channel_attrs(args, res);
7742 static void nfs4_update_session(struct nfs4_session *session,
7743 struct nfs41_create_session_res *res)
7745 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
7746 /* Mark client id and session as being confirmed */
7747 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7748 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
7749 session->flags = res->flags;
7750 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
7751 if (res->flags & SESSION4_BACK_CHAN)
7752 memcpy(&session->bc_attrs, &res->bc_attrs,
7753 sizeof(session->bc_attrs));
7756 static int _nfs4_proc_create_session(struct nfs_client *clp,
7757 struct rpc_cred *cred)
7759 struct nfs4_session *session = clp->cl_session;
7760 struct nfs41_create_session_args args = {
7762 .clientid = clp->cl_clientid,
7763 .seqid = clp->cl_seqid,
7764 .cb_program = NFS4_CALLBACK,
7766 struct nfs41_create_session_res res;
7768 struct rpc_message msg = {
7769 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7776 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
7777 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
7779 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7780 trace_nfs4_create_session(clp, status);
7783 case -NFS4ERR_STALE_CLIENTID:
7784 case -NFS4ERR_DELAY:
7793 /* Verify the session's negotiated channel_attrs values */
7794 status = nfs4_verify_channel_attrs(&args, &res);
7795 /* Increment the clientid slot sequence id */
7798 nfs4_update_session(session, &res);
7805 * Issues a CREATE_SESSION operation to the server.
7806 * It is the responsibility of the caller to verify the session is
7807 * expired before calling this routine.
7809 int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
7813 struct nfs4_session *session = clp->cl_session;
7815 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7817 status = _nfs4_proc_create_session(clp, cred);
7821 /* Init or reset the session slot tables */
7822 status = nfs4_setup_session_slot_tables(session);
7823 dprintk("slot table setup returned %d\n", status);
7827 ptr = (unsigned *)&session->sess_id.data[0];
7828 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7829 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
7831 dprintk("<-- %s\n", __func__);
7836 * Issue the over-the-wire RPC DESTROY_SESSION.
7837 * The caller must serialize access to this routine.
7839 int nfs4_proc_destroy_session(struct nfs4_session *session,
7840 struct rpc_cred *cred)
7842 struct rpc_message msg = {
7843 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
7844 .rpc_argp = session,
7849 dprintk("--> nfs4_proc_destroy_session\n");
7851 /* session is still being setup */
7852 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
7855 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7856 trace_nfs4_destroy_session(session->clp, status);
7859 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
7860 "Session has been destroyed regardless...\n", status);
7862 dprintk("<-- nfs4_proc_destroy_session\n");
7867 * Renew the cl_session lease.
7869 struct nfs4_sequence_data {
7870 struct nfs_client *clp;
7871 struct nfs4_sequence_args args;
7872 struct nfs4_sequence_res res;
7875 static void nfs41_sequence_release(void *data)
7877 struct nfs4_sequence_data *calldata = data;
7878 struct nfs_client *clp = calldata->clp;
7880 if (atomic_read(&clp->cl_count) > 1)
7881 nfs4_schedule_state_renewal(clp);
7882 nfs_put_client(clp);
7886 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7888 switch(task->tk_status) {
7889 case -NFS4ERR_DELAY:
7890 rpc_delay(task, NFS4_POLL_RETRY_MAX);
7893 nfs4_schedule_lease_recovery(clp);
7898 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
7900 struct nfs4_sequence_data *calldata = data;
7901 struct nfs_client *clp = calldata->clp;
7903 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
7906 trace_nfs4_sequence(clp, task->tk_status);
7907 if (task->tk_status < 0) {
7908 dprintk("%s ERROR %d\n", __func__, task->tk_status);
7909 if (atomic_read(&clp->cl_count) == 1)
7912 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
7913 rpc_restart_call_prepare(task);
7917 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
7919 dprintk("<-- %s\n", __func__);
7922 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
7924 struct nfs4_sequence_data *calldata = data;
7925 struct nfs_client *clp = calldata->clp;
7926 struct nfs4_sequence_args *args;
7927 struct nfs4_sequence_res *res;
7929 args = task->tk_msg.rpc_argp;
7930 res = task->tk_msg.rpc_resp;
7932 nfs41_setup_sequence(clp->cl_session, args, res, task);
7935 static const struct rpc_call_ops nfs41_sequence_ops = {
7936 .rpc_call_done = nfs41_sequence_call_done,
7937 .rpc_call_prepare = nfs41_sequence_prepare,
7938 .rpc_release = nfs41_sequence_release,
7941 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
7942 struct rpc_cred *cred,
7945 struct nfs4_sequence_data *calldata;
7946 struct rpc_message msg = {
7947 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
7950 struct rpc_task_setup task_setup_data = {
7951 .rpc_client = clp->cl_rpcclient,
7952 .rpc_message = &msg,
7953 .callback_ops = &nfs41_sequence_ops,
7954 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7957 if (!atomic_inc_not_zero(&clp->cl_count))
7958 return ERR_PTR(-EIO);
7959 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7960 if (calldata == NULL) {
7961 nfs_put_client(clp);
7962 return ERR_PTR(-ENOMEM);
7964 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
7966 nfs4_set_sequence_privileged(&calldata->args);
7967 msg.rpc_argp = &calldata->args;
7968 msg.rpc_resp = &calldata->res;
7969 calldata->clp = clp;
7970 task_setup_data.callback_data = calldata;
7972 return rpc_run_task(&task_setup_data);
7975 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
7977 struct rpc_task *task;
7980 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
7982 task = _nfs41_proc_sequence(clp, cred, false);
7984 ret = PTR_ERR(task);
7986 rpc_put_task_async(task);
7987 dprintk("<-- %s status=%d\n", __func__, ret);
7991 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
7993 struct rpc_task *task;
7996 task = _nfs41_proc_sequence(clp, cred, true);
7998 ret = PTR_ERR(task);
8001 ret = rpc_wait_for_completion_task(task);
8003 ret = task->tk_status;
8006 dprintk("<-- %s status=%d\n", __func__, ret);
8010 struct nfs4_reclaim_complete_data {
8011 struct nfs_client *clp;
8012 struct nfs41_reclaim_complete_args arg;
8013 struct nfs41_reclaim_complete_res res;
8016 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8018 struct nfs4_reclaim_complete_data *calldata = data;
8020 nfs41_setup_sequence(calldata->clp->cl_session,
8021 &calldata->arg.seq_args,
8022 &calldata->res.seq_res,
8026 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8028 switch(task->tk_status) {
8030 case -NFS4ERR_COMPLETE_ALREADY:
8031 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8033 case -NFS4ERR_DELAY:
8034 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8036 case -NFS4ERR_RETRY_UNCACHED_REP:
8039 nfs4_schedule_lease_recovery(clp);
8044 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8046 struct nfs4_reclaim_complete_data *calldata = data;
8047 struct nfs_client *clp = calldata->clp;
8048 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8050 dprintk("--> %s\n", __func__);
8051 if (!nfs41_sequence_done(task, res))
8054 trace_nfs4_reclaim_complete(clp, task->tk_status);
8055 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8056 rpc_restart_call_prepare(task);
8059 dprintk("<-- %s\n", __func__);
8062 static void nfs4_free_reclaim_complete_data(void *data)
8064 struct nfs4_reclaim_complete_data *calldata = data;
8069 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8070 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8071 .rpc_call_done = nfs4_reclaim_complete_done,
8072 .rpc_release = nfs4_free_reclaim_complete_data,
8076 * Issue a global reclaim complete.
8078 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8079 struct rpc_cred *cred)
8081 struct nfs4_reclaim_complete_data *calldata;
8082 struct rpc_task *task;
8083 struct rpc_message msg = {
8084 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
8087 struct rpc_task_setup task_setup_data = {
8088 .rpc_client = clp->cl_rpcclient,
8089 .rpc_message = &msg,
8090 .callback_ops = &nfs4_reclaim_complete_call_ops,
8091 .flags = RPC_TASK_ASYNC,
8093 int status = -ENOMEM;
8095 dprintk("--> %s\n", __func__);
8096 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8097 if (calldata == NULL)
8099 calldata->clp = clp;
8100 calldata->arg.one_fs = 0;
8102 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
8103 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
8104 msg.rpc_argp = &calldata->arg;
8105 msg.rpc_resp = &calldata->res;
8106 task_setup_data.callback_data = calldata;
8107 task = rpc_run_task(&task_setup_data);
8109 status = PTR_ERR(task);
8112 status = nfs4_wait_for_completion_rpc_task(task);
8114 status = task->tk_status;
8118 dprintk("<-- %s status=%d\n", __func__, status);
8123 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8125 struct nfs4_layoutget *lgp = calldata;
8126 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
8127 struct nfs4_session *session = nfs4_get_session(server);
8129 dprintk("--> %s\n", __func__);
8130 nfs41_setup_sequence(session, &lgp->args.seq_args,
8131 &lgp->res.seq_res, task);
8132 dprintk("<-- %s\n", __func__);
8135 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8137 struct nfs4_layoutget *lgp = calldata;
8139 dprintk("--> %s\n", __func__);
8140 nfs41_sequence_process(task, &lgp->res.seq_res);
8141 dprintk("<-- %s\n", __func__);
8145 nfs4_layoutget_handle_exception(struct rpc_task *task,
8146 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8148 struct inode *inode = lgp->args.inode;
8149 struct nfs_server *server = NFS_SERVER(inode);
8150 struct pnfs_layout_hdr *lo;
8151 int nfs4err = task->tk_status;
8152 int err, status = 0;
8155 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
8162 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8163 * on the file. set tk_status to -ENODATA to tell upper layer to
8166 case -NFS4ERR_LAYOUTUNAVAILABLE:
8170 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8171 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8173 case -NFS4ERR_BADLAYOUT:
8174 status = -EOVERFLOW;
8177 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
8178 * (or clients) writing to the same RAID stripe except when
8179 * the minlength argument is 0 (see RFC5661 section 18.43.3).
8181 * Treat it like we would RECALLCONFLICT -- we retry for a little
8182 * while, and then eventually give up.
8184 case -NFS4ERR_LAYOUTTRYLATER:
8185 if (lgp->args.minlength == 0) {
8186 status = -EOVERFLOW;
8191 case -NFS4ERR_RECALLCONFLICT:
8192 status = -ERECALLCONFLICT;
8194 case -NFS4ERR_EXPIRED:
8195 case -NFS4ERR_BAD_STATEID:
8196 exception->timeout = 0;
8197 spin_lock(&inode->i_lock);
8198 lo = NFS_I(inode)->layout;
8199 /* If the open stateid was bad, then recover it. */
8200 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8201 nfs4_stateid_match_other(&lgp->args.stateid,
8202 &lgp->args.ctx->state->stateid)) {
8203 spin_unlock(&inode->i_lock);
8204 exception->state = lgp->args.ctx->state;
8209 * Mark the bad layout state as invalid, then retry
8211 pnfs_mark_layout_stateid_invalid(lo, &head);
8212 spin_unlock(&inode->i_lock);
8213 pnfs_free_lseg_list(&head);
8218 err = nfs4_handle_exception(server, nfs4err, exception);
8220 if (exception->retry)
8226 dprintk("<-- %s\n", __func__);
8230 static size_t max_response_pages(struct nfs_server *server)
8232 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8233 return nfs_page_array_len(0, max_resp_sz);
8236 static void nfs4_free_pages(struct page **pages, size_t size)
8243 for (i = 0; i < size; i++) {
8246 __free_page(pages[i]);
8251 static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8253 struct page **pages;
8256 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8258 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8262 for (i = 0; i < size; i++) {
8263 pages[i] = alloc_page(gfp_flags);
8265 dprintk("%s: failed to allocate page\n", __func__);
8266 nfs4_free_pages(pages, size);
8274 static void nfs4_layoutget_release(void *calldata)
8276 struct nfs4_layoutget *lgp = calldata;
8277 struct inode *inode = lgp->args.inode;
8278 struct nfs_server *server = NFS_SERVER(inode);
8279 size_t max_pages = max_response_pages(server);
8281 dprintk("--> %s\n", __func__);
8282 nfs4_free_pages(lgp->args.layout.pages, max_pages);
8283 pnfs_put_layout_hdr(NFS_I(inode)->layout);
8284 put_nfs_open_context(lgp->args.ctx);
8286 dprintk("<-- %s\n", __func__);
8289 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8290 .rpc_call_prepare = nfs4_layoutget_prepare,
8291 .rpc_call_done = nfs4_layoutget_done,
8292 .rpc_release = nfs4_layoutget_release,
8295 struct pnfs_layout_segment *
8296 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
8298 struct inode *inode = lgp->args.inode;
8299 struct nfs_server *server = NFS_SERVER(inode);
8300 size_t max_pages = max_response_pages(server);
8301 struct rpc_task *task;
8302 struct rpc_message msg = {
8303 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8304 .rpc_argp = &lgp->args,
8305 .rpc_resp = &lgp->res,
8306 .rpc_cred = lgp->cred,
8308 struct rpc_task_setup task_setup_data = {
8309 .rpc_client = server->client,
8310 .rpc_message = &msg,
8311 .callback_ops = &nfs4_layoutget_call_ops,
8312 .callback_data = lgp,
8313 .flags = RPC_TASK_ASYNC,
8315 struct pnfs_layout_segment *lseg = NULL;
8316 struct nfs4_exception exception = {
8318 .timeout = *timeout,
8322 dprintk("--> %s\n", __func__);
8324 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8325 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8327 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8328 if (!lgp->args.layout.pages) {
8329 nfs4_layoutget_release(lgp);
8330 return ERR_PTR(-ENOMEM);
8332 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8334 lgp->res.layoutp = &lgp->args.layout;
8335 lgp->res.seq_res.sr_slot = NULL;
8336 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
8338 task = rpc_run_task(&task_setup_data);
8340 return ERR_CAST(task);
8341 status = nfs4_wait_for_completion_rpc_task(task);
8343 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8344 *timeout = exception.timeout;
8347 trace_nfs4_layoutget(lgp->args.ctx,
8353 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8354 if (status == 0 && lgp->res.layoutp->len)
8355 lseg = pnfs_layout_process(lgp);
8356 nfs4_sequence_free_slot(&lgp->res.seq_res);
8358 dprintk("<-- %s status=%d\n", __func__, status);
8360 return ERR_PTR(status);
8365 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8367 struct nfs4_layoutreturn *lrp = calldata;
8369 dprintk("--> %s\n", __func__);
8370 nfs41_setup_sequence(lrp->clp->cl_session,
8371 &lrp->args.seq_args,
8376 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8378 struct nfs4_layoutreturn *lrp = calldata;
8379 struct nfs_server *server;
8381 dprintk("--> %s\n", __func__);
8383 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
8386 server = NFS_SERVER(lrp->args.inode);
8387 switch (task->tk_status) {
8389 task->tk_status = 0;
8392 case -NFS4ERR_DELAY:
8393 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
8395 nfs4_sequence_free_slot(&lrp->res.seq_res);
8396 rpc_restart_call_prepare(task);
8399 dprintk("<-- %s\n", __func__);
8402 static void nfs4_layoutreturn_release(void *calldata)
8404 struct nfs4_layoutreturn *lrp = calldata;
8405 struct pnfs_layout_hdr *lo = lrp->args.layout;
8408 dprintk("--> %s\n", __func__);
8409 spin_lock(&lo->plh_inode->i_lock);
8410 if (lrp->res.lrs_present) {
8411 pnfs_mark_matching_lsegs_invalid(lo, &freeme,
8413 be32_to_cpu(lrp->args.stateid.seqid));
8414 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
8416 pnfs_mark_layout_stateid_invalid(lo, &freeme);
8417 pnfs_clear_layoutreturn_waitbit(lo);
8418 spin_unlock(&lo->plh_inode->i_lock);
8419 nfs4_sequence_free_slot(&lrp->res.seq_res);
8420 pnfs_free_lseg_list(&freeme);
8421 pnfs_put_layout_hdr(lrp->args.layout);
8422 nfs_iput_and_deactive(lrp->inode);
8424 dprintk("<-- %s\n", __func__);
8427 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8428 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8429 .rpc_call_done = nfs4_layoutreturn_done,
8430 .rpc_release = nfs4_layoutreturn_release,
8433 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
8435 struct rpc_task *task;
8436 struct rpc_message msg = {
8437 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8438 .rpc_argp = &lrp->args,
8439 .rpc_resp = &lrp->res,
8440 .rpc_cred = lrp->cred,
8442 struct rpc_task_setup task_setup_data = {
8443 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
8444 .rpc_message = &msg,
8445 .callback_ops = &nfs4_layoutreturn_call_ops,
8446 .callback_data = lrp,
8450 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8451 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8452 &task_setup_data.rpc_client, &msg);
8454 dprintk("--> %s\n", __func__);
8456 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8458 nfs4_layoutreturn_release(lrp);
8461 task_setup_data.flags |= RPC_TASK_ASYNC;
8463 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
8464 task = rpc_run_task(&task_setup_data);
8466 return PTR_ERR(task);
8468 status = task->tk_status;
8469 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
8470 dprintk("<-- %s status=%d\n", __func__, status);
8476 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
8477 struct pnfs_device *pdev,
8478 struct rpc_cred *cred)
8480 struct nfs4_getdeviceinfo_args args = {
8482 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8483 NOTIFY_DEVICEID4_DELETE,
8485 struct nfs4_getdeviceinfo_res res = {
8488 struct rpc_message msg = {
8489 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8496 dprintk("--> %s\n", __func__);
8497 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
8498 if (res.notification & ~args.notify_types)
8499 dprintk("%s: unsupported notification\n", __func__);
8500 if (res.notification != args.notify_types)
8503 dprintk("<-- %s status=%d\n", __func__, status);
8508 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8509 struct pnfs_device *pdev,
8510 struct rpc_cred *cred)
8512 struct nfs4_exception exception = { };
8516 err = nfs4_handle_exception(server,
8517 _nfs4_proc_getdeviceinfo(server, pdev, cred),
8519 } while (exception.retry);
8522 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8524 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8526 struct nfs4_layoutcommit_data *data = calldata;
8527 struct nfs_server *server = NFS_SERVER(data->args.inode);
8528 struct nfs4_session *session = nfs4_get_session(server);
8530 nfs41_setup_sequence(session,
8531 &data->args.seq_args,
8537 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8539 struct nfs4_layoutcommit_data *data = calldata;
8540 struct nfs_server *server = NFS_SERVER(data->args.inode);
8542 if (!nfs41_sequence_done(task, &data->res.seq_res))
8545 switch (task->tk_status) { /* Just ignore these failures */
8546 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8547 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8548 case -NFS4ERR_BADLAYOUT: /* no layout */
8549 case -NFS4ERR_GRACE: /* loca_recalim always false */
8550 task->tk_status = 0;
8554 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
8555 rpc_restart_call_prepare(task);
8561 static void nfs4_layoutcommit_release(void *calldata)
8563 struct nfs4_layoutcommit_data *data = calldata;
8565 pnfs_cleanup_layoutcommit(data);
8566 nfs_post_op_update_inode_force_wcc(data->args.inode,
8568 put_rpccred(data->cred);
8569 nfs_iput_and_deactive(data->inode);
8573 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8574 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8575 .rpc_call_done = nfs4_layoutcommit_done,
8576 .rpc_release = nfs4_layoutcommit_release,
8580 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
8582 struct rpc_message msg = {
8583 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8584 .rpc_argp = &data->args,
8585 .rpc_resp = &data->res,
8586 .rpc_cred = data->cred,
8588 struct rpc_task_setup task_setup_data = {
8589 .task = &data->task,
8590 .rpc_client = NFS_CLIENT(data->args.inode),
8591 .rpc_message = &msg,
8592 .callback_ops = &nfs4_layoutcommit_ops,
8593 .callback_data = data,
8595 struct rpc_task *task;
8598 dprintk("NFS: initiating layoutcommit call. sync %d "
8599 "lbw: %llu inode %lu\n", sync,
8600 data->args.lastbytewritten,
8601 data->args.inode->i_ino);
8604 data->inode = nfs_igrab_and_active(data->args.inode);
8605 if (data->inode == NULL) {
8606 nfs4_layoutcommit_release(data);
8609 task_setup_data.flags = RPC_TASK_ASYNC;
8611 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
8612 task = rpc_run_task(&task_setup_data);
8614 return PTR_ERR(task);
8616 status = task->tk_status;
8617 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
8618 dprintk("%s: status %d\n", __func__, status);
8624 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8625 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8628 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8629 struct nfs_fsinfo *info,
8630 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
8632 struct nfs41_secinfo_no_name_args args = {
8633 .style = SECINFO_STYLE_CURRENT_FH,
8635 struct nfs4_secinfo_res res = {
8638 struct rpc_message msg = {
8639 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8643 struct rpc_clnt *clnt = server->client;
8644 struct rpc_cred *cred = NULL;
8647 if (use_integrity) {
8648 clnt = server->nfs_client->cl_rpcclient;
8649 cred = nfs4_get_clid_cred(server->nfs_client);
8650 msg.rpc_cred = cred;
8653 dprintk("--> %s\n", __func__);
8654 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8656 dprintk("<-- %s status=%d\n", __func__, status);
8665 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8666 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8668 struct nfs4_exception exception = { };
8671 /* first try using integrity protection */
8672 err = -NFS4ERR_WRONGSEC;
8674 /* try to use integrity protection with machine cred */
8675 if (_nfs4_is_integrity_protected(server->nfs_client))
8676 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8680 * if unable to use integrity protection, or SECINFO with
8681 * integrity protection returns NFS4ERR_WRONGSEC (which is
8682 * disallowed by spec, but exists in deployed servers) use
8683 * the current filesystem's rpc_client and the user cred.
8685 if (err == -NFS4ERR_WRONGSEC)
8686 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8691 case -NFS4ERR_WRONGSEC:
8695 err = nfs4_handle_exception(server, err, &exception);
8697 } while (exception.retry);
8703 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8704 struct nfs_fsinfo *info)
8708 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
8709 struct nfs4_secinfo_flavors *flavors;
8710 struct nfs4_secinfo4 *secinfo;
8713 page = alloc_page(GFP_KERNEL);
8719 flavors = page_address(page);
8720 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8723 * Fall back on "guess and check" method if
8724 * the server doesn't support SECINFO_NO_NAME
8726 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
8727 err = nfs4_find_root_sec(server, fhandle, info);
8733 for (i = 0; i < flavors->num_flavors; i++) {
8734 secinfo = &flavors->flavors[i];
8736 switch (secinfo->flavor) {
8740 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8741 &secinfo->flavor_info);
8744 flavor = RPC_AUTH_MAXFLAVOR;
8748 if (!nfs_auth_info_match(&server->auth_info, flavor))
8749 flavor = RPC_AUTH_MAXFLAVOR;
8751 if (flavor != RPC_AUTH_MAXFLAVOR) {
8752 err = nfs4_lookup_root_sec(server, fhandle,
8759 if (flavor == RPC_AUTH_MAXFLAVOR)
8770 static int _nfs41_test_stateid(struct nfs_server *server,
8771 nfs4_stateid *stateid,
8772 struct rpc_cred *cred)
8775 struct nfs41_test_stateid_args args = {
8778 struct nfs41_test_stateid_res res;
8779 struct rpc_message msg = {
8780 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8785 struct rpc_clnt *rpc_client = server->client;
8787 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8790 dprintk("NFS call test_stateid %p\n", stateid);
8791 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
8792 nfs4_set_sequence_privileged(&args.seq_args);
8793 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
8794 &args.seq_args, &res.seq_res);
8795 if (status != NFS_OK) {
8796 dprintk("NFS reply test_stateid: failed, %d\n", status);
8799 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
8803 static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
8804 int err, struct nfs4_exception *exception)
8806 exception->retry = 0;
8808 case -NFS4ERR_DELAY:
8809 nfs4_handle_exception(server, err, exception);
8811 case -NFS4ERR_BADSESSION:
8812 case -NFS4ERR_BADSLOT:
8813 case -NFS4ERR_BAD_HIGH_SLOT:
8814 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8815 case -NFS4ERR_DEADSESSION:
8816 nfs4_do_handle_exception(server, err, exception);
8821 * nfs41_test_stateid - perform a TEST_STATEID operation
8823 * @server: server / transport on which to perform the operation
8824 * @stateid: state ID to test
8827 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8828 * Otherwise a negative NFS4ERR value is returned if the operation
8829 * failed or the state ID is not currently valid.
8831 static int nfs41_test_stateid(struct nfs_server *server,
8832 nfs4_stateid *stateid,
8833 struct rpc_cred *cred)
8835 struct nfs4_exception exception = { };
8838 err = _nfs41_test_stateid(server, stateid, cred);
8839 nfs4_handle_delay_or_session_error(server, err, &exception);
8840 } while (exception.retry);
8844 struct nfs_free_stateid_data {
8845 struct nfs_server *server;
8846 struct nfs41_free_stateid_args args;
8847 struct nfs41_free_stateid_res res;
8850 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
8852 struct nfs_free_stateid_data *data = calldata;
8853 nfs41_setup_sequence(nfs4_get_session(data->server),
8854 &data->args.seq_args,
8859 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
8861 struct nfs_free_stateid_data *data = calldata;
8863 nfs41_sequence_done(task, &data->res.seq_res);
8865 switch (task->tk_status) {
8866 case -NFS4ERR_DELAY:
8867 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
8868 rpc_restart_call_prepare(task);
8872 static void nfs41_free_stateid_release(void *calldata)
8877 static const struct rpc_call_ops nfs41_free_stateid_ops = {
8878 .rpc_call_prepare = nfs41_free_stateid_prepare,
8879 .rpc_call_done = nfs41_free_stateid_done,
8880 .rpc_release = nfs41_free_stateid_release,
8883 static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
8884 nfs4_stateid *stateid,
8885 struct rpc_cred *cred,
8888 struct rpc_message msg = {
8889 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
8892 struct rpc_task_setup task_setup = {
8893 .rpc_client = server->client,
8894 .rpc_message = &msg,
8895 .callback_ops = &nfs41_free_stateid_ops,
8896 .flags = RPC_TASK_ASYNC,
8898 struct nfs_free_stateid_data *data;
8900 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8901 &task_setup.rpc_client, &msg);
8903 dprintk("NFS call free_stateid %p\n", stateid);
8904 data = kmalloc(sizeof(*data), GFP_NOFS);
8906 return ERR_PTR(-ENOMEM);
8907 data->server = server;
8908 nfs4_stateid_copy(&data->args.stateid, stateid);
8910 task_setup.callback_data = data;
8912 msg.rpc_argp = &data->args;
8913 msg.rpc_resp = &data->res;
8914 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
8916 nfs4_set_sequence_privileged(&data->args.seq_args);
8918 return rpc_run_task(&task_setup);
8922 * nfs41_free_stateid - perform a FREE_STATEID operation
8924 * @server: server / transport on which to perform the operation
8925 * @stateid: state ID to release
8928 * Returns NFS_OK if the server freed "stateid". Otherwise a
8929 * negative NFS4ERR value is returned.
8931 static int nfs41_free_stateid(struct nfs_server *server,
8932 nfs4_stateid *stateid,
8933 struct rpc_cred *cred)
8935 struct rpc_task *task;
8938 task = _nfs41_free_stateid(server, stateid, cred, true);
8940 return PTR_ERR(task);
8941 ret = rpc_wait_for_completion_task(task);
8943 ret = task->tk_status;
8949 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
8951 struct rpc_task *task;
8952 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
8954 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
8955 nfs4_free_lock_state(server, lsp);
8961 static bool nfs41_match_stateid(const nfs4_stateid *s1,
8962 const nfs4_stateid *s2)
8964 if (s1->type != s2->type)
8967 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
8970 if (s1->seqid == s2->seqid)
8972 if (s1->seqid == 0 || s2->seqid == 0)
8978 #endif /* CONFIG_NFS_V4_1 */
8980 static bool nfs4_match_stateid(const nfs4_stateid *s1,
8981 const nfs4_stateid *s2)
8983 return nfs4_stateid_match(s1, s2);
8987 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
8988 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
8989 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
8990 .recover_open = nfs4_open_reclaim,
8991 .recover_lock = nfs4_lock_reclaim,
8992 .establish_clid = nfs4_init_clientid,
8993 .detect_trunking = nfs40_discover_server_trunking,
8996 #if defined(CONFIG_NFS_V4_1)
8997 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
8998 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
8999 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9000 .recover_open = nfs4_open_reclaim,
9001 .recover_lock = nfs4_lock_reclaim,
9002 .establish_clid = nfs41_init_clientid,
9003 .reclaim_complete = nfs41_proc_reclaim_complete,
9004 .detect_trunking = nfs41_discover_server_trunking,
9006 #endif /* CONFIG_NFS_V4_1 */
9008 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
9009 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9010 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
9011 .recover_open = nfs40_open_expired,
9012 .recover_lock = nfs4_lock_expired,
9013 .establish_clid = nfs4_init_clientid,
9016 #if defined(CONFIG_NFS_V4_1)
9017 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
9018 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9019 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
9020 .recover_open = nfs41_open_expired,
9021 .recover_lock = nfs41_lock_expired,
9022 .establish_clid = nfs41_init_clientid,
9024 #endif /* CONFIG_NFS_V4_1 */
9026 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
9027 .sched_state_renewal = nfs4_proc_async_renew,
9028 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
9029 .renew_lease = nfs4_proc_renew,
9032 #if defined(CONFIG_NFS_V4_1)
9033 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
9034 .sched_state_renewal = nfs41_proc_async_sequence,
9035 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
9036 .renew_lease = nfs4_proc_sequence,
9040 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
9041 .get_locations = _nfs40_proc_get_locations,
9042 .fsid_present = _nfs40_proc_fsid_present,
9045 #if defined(CONFIG_NFS_V4_1)
9046 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
9047 .get_locations = _nfs41_proc_get_locations,
9048 .fsid_present = _nfs41_proc_fsid_present,
9050 #endif /* CONFIG_NFS_V4_1 */
9052 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9054 .init_caps = NFS_CAP_READDIRPLUS
9055 | NFS_CAP_ATOMIC_OPEN
9056 | NFS_CAP_POSIX_LOCK,
9057 .init_client = nfs40_init_client,
9058 .shutdown_client = nfs40_shutdown_client,
9059 .match_stateid = nfs4_match_stateid,
9060 .find_root_sec = nfs4_find_root_sec,
9061 .free_lock_state = nfs4_release_lockowner,
9062 .alloc_seqid = nfs_alloc_seqid,
9063 .call_sync_ops = &nfs40_call_sync_ops,
9064 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9065 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9066 .state_renewal_ops = &nfs40_state_renewal_ops,
9067 .mig_recovery_ops = &nfs40_mig_recovery_ops,
9070 #if defined(CONFIG_NFS_V4_1)
9071 static struct nfs_seqid *
9072 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9077 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9079 .init_caps = NFS_CAP_READDIRPLUS
9080 | NFS_CAP_ATOMIC_OPEN
9081 | NFS_CAP_POSIX_LOCK
9082 | NFS_CAP_STATEID_NFSV41
9083 | NFS_CAP_ATOMIC_OPEN_V1,
9084 .init_client = nfs41_init_client,
9085 .shutdown_client = nfs41_shutdown_client,
9086 .match_stateid = nfs41_match_stateid,
9087 .find_root_sec = nfs41_find_root_sec,
9088 .free_lock_state = nfs41_free_lock_state,
9089 .alloc_seqid = nfs_alloc_no_seqid,
9090 .session_trunk = nfs4_test_session_trunk,
9091 .call_sync_ops = &nfs41_call_sync_ops,
9092 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9093 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9094 .state_renewal_ops = &nfs41_state_renewal_ops,
9095 .mig_recovery_ops = &nfs41_mig_recovery_ops,
9099 #if defined(CONFIG_NFS_V4_2)
9100 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9102 .init_caps = NFS_CAP_READDIRPLUS
9103 | NFS_CAP_ATOMIC_OPEN
9104 | NFS_CAP_POSIX_LOCK
9105 | NFS_CAP_STATEID_NFSV41
9106 | NFS_CAP_ATOMIC_OPEN_V1
9109 | NFS_CAP_DEALLOCATE
9111 | NFS_CAP_LAYOUTSTATS
9113 .init_client = nfs41_init_client,
9114 .shutdown_client = nfs41_shutdown_client,
9115 .match_stateid = nfs41_match_stateid,
9116 .find_root_sec = nfs41_find_root_sec,
9117 .free_lock_state = nfs41_free_lock_state,
9118 .call_sync_ops = &nfs41_call_sync_ops,
9119 .alloc_seqid = nfs_alloc_no_seqid,
9120 .session_trunk = nfs4_test_session_trunk,
9121 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9122 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9123 .state_renewal_ops = &nfs41_state_renewal_ops,
9124 .mig_recovery_ops = &nfs41_mig_recovery_ops,
9128 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9129 [0] = &nfs_v4_0_minor_ops,
9130 #if defined(CONFIG_NFS_V4_1)
9131 [1] = &nfs_v4_1_minor_ops,
9133 #if defined(CONFIG_NFS_V4_2)
9134 [2] = &nfs_v4_2_minor_ops,
9138 static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
9140 ssize_t error, error2;
9142 error = generic_listxattr(dentry, list, size);
9150 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9153 return error + error2;
9156 static const struct inode_operations nfs4_dir_inode_operations = {
9157 .create = nfs_create,
9158 .lookup = nfs_lookup,
9159 .atomic_open = nfs_atomic_open,
9161 .unlink = nfs_unlink,
9162 .symlink = nfs_symlink,
9166 .rename = nfs_rename,
9167 .permission = nfs_permission,
9168 .getattr = nfs_getattr,
9169 .setattr = nfs_setattr,
9170 .getxattr = generic_getxattr,
9171 .setxattr = generic_setxattr,
9172 .listxattr = nfs4_listxattr,
9173 .removexattr = generic_removexattr,
9176 static const struct inode_operations nfs4_file_inode_operations = {
9177 .permission = nfs_permission,
9178 .getattr = nfs_getattr,
9179 .setattr = nfs_setattr,
9180 .getxattr = generic_getxattr,
9181 .setxattr = generic_setxattr,
9182 .listxattr = nfs4_listxattr,
9183 .removexattr = generic_removexattr,
9186 const struct nfs_rpc_ops nfs_v4_clientops = {
9187 .version = 4, /* protocol version */
9188 .dentry_ops = &nfs4_dentry_operations,
9189 .dir_inode_ops = &nfs4_dir_inode_operations,
9190 .file_inode_ops = &nfs4_file_inode_operations,
9191 .file_ops = &nfs4_file_operations,
9192 .getroot = nfs4_proc_get_root,
9193 .submount = nfs4_submount,
9194 .try_mount = nfs4_try_mount,
9195 .getattr = nfs4_proc_getattr,
9196 .setattr = nfs4_proc_setattr,
9197 .lookup = nfs4_proc_lookup,
9198 .access = nfs4_proc_access,
9199 .readlink = nfs4_proc_readlink,
9200 .create = nfs4_proc_create,
9201 .remove = nfs4_proc_remove,
9202 .unlink_setup = nfs4_proc_unlink_setup,
9203 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
9204 .unlink_done = nfs4_proc_unlink_done,
9205 .rename_setup = nfs4_proc_rename_setup,
9206 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
9207 .rename_done = nfs4_proc_rename_done,
9208 .link = nfs4_proc_link,
9209 .symlink = nfs4_proc_symlink,
9210 .mkdir = nfs4_proc_mkdir,
9211 .rmdir = nfs4_proc_remove,
9212 .readdir = nfs4_proc_readdir,
9213 .mknod = nfs4_proc_mknod,
9214 .statfs = nfs4_proc_statfs,
9215 .fsinfo = nfs4_proc_fsinfo,
9216 .pathconf = nfs4_proc_pathconf,
9217 .set_capabilities = nfs4_server_capabilities,
9218 .decode_dirent = nfs4_decode_dirent,
9219 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
9220 .read_setup = nfs4_proc_read_setup,
9221 .read_done = nfs4_read_done,
9222 .write_setup = nfs4_proc_write_setup,
9223 .write_done = nfs4_write_done,
9224 .commit_setup = nfs4_proc_commit_setup,
9225 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
9226 .commit_done = nfs4_commit_done,
9227 .lock = nfs4_proc_lock,
9228 .clear_acl_cache = nfs4_zap_acl_attr,
9229 .close_context = nfs4_close_context,
9230 .open_context = nfs4_atomic_open,
9231 .have_delegation = nfs4_have_delegation,
9232 .return_delegation = nfs4_inode_return_delegation,
9233 .alloc_client = nfs4_alloc_client,
9234 .init_client = nfs4_init_client,
9235 .free_client = nfs4_free_client,
9236 .create_server = nfs4_create_server,
9237 .clone_server = nfs_clone_server,
9240 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
9241 .name = XATTR_NAME_NFSV4_ACL,
9242 .list = nfs4_xattr_list_nfs4_acl,
9243 .get = nfs4_xattr_get_nfs4_acl,
9244 .set = nfs4_xattr_set_nfs4_acl,
9247 const struct xattr_handler *nfs4_xattr_handlers[] = {
9248 &nfs4_xattr_nfs4_acl_handler,
9249 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
9250 &nfs4_xattr_nfs4_label_handler,