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,
2389 const nfs4_stateid *stateid)
2391 nfs_remove_bad_delegation(state->inode, stateid);
2392 write_seqlock(&state->seqlock);
2393 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2394 write_sequnlock(&state->seqlock);
2395 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2398 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2400 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2401 nfs_finish_clear_delegation_stateid(state, NULL);
2404 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2406 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2407 nfs40_clear_delegation_stateid(state);
2408 return nfs4_open_expired(sp, state);
2411 static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2412 nfs4_stateid *stateid,
2413 struct rpc_cred *cred)
2415 return -NFS4ERR_BAD_STATEID;
2418 #if defined(CONFIG_NFS_V4_1)
2419 static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2420 nfs4_stateid *stateid,
2421 struct rpc_cred *cred)
2425 switch (stateid->type) {
2428 case NFS4_INVALID_STATEID_TYPE:
2429 case NFS4_SPECIAL_STATEID_TYPE:
2430 return -NFS4ERR_BAD_STATEID;
2431 case NFS4_REVOKED_STATEID_TYPE:
2435 status = nfs41_test_stateid(server, stateid, cred);
2437 case -NFS4ERR_EXPIRED:
2438 case -NFS4ERR_ADMIN_REVOKED:
2439 case -NFS4ERR_DELEG_REVOKED:
2445 /* Ack the revoked state to the server */
2446 nfs41_free_stateid(server, stateid, cred);
2447 return -NFS4ERR_EXPIRED;
2450 static void nfs41_check_delegation_stateid(struct nfs4_state *state)
2452 struct nfs_server *server = NFS_SERVER(state->inode);
2453 nfs4_stateid stateid;
2454 struct nfs_delegation *delegation;
2455 struct rpc_cred *cred;
2458 /* Get the delegation credential for use by test/free_stateid */
2460 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2461 if (delegation == NULL) {
2466 nfs4_stateid_copy(&stateid, &delegation->stateid);
2467 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
2469 nfs_finish_clear_delegation_stateid(state, &stateid);
2473 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags)) {
2478 cred = get_rpccred(delegation->cred);
2480 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
2481 trace_nfs4_test_delegation_stateid(state, NULL, status);
2482 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
2483 nfs_finish_clear_delegation_stateid(state, &stateid);
2489 * nfs41_check_expired_locks - possibly free a lock stateid
2491 * @state: NFSv4 state for an inode
2493 * Returns NFS_OK if recovery for this stateid is now finished.
2494 * Otherwise a negative NFS4ERR value is returned.
2496 static int nfs41_check_expired_locks(struct nfs4_state *state)
2498 int status, ret = NFS_OK;
2499 struct nfs4_lock_state *lsp;
2500 struct nfs_server *server = NFS_SERVER(state->inode);
2502 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2504 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2505 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2506 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2508 status = nfs41_test_and_free_expired_stateid(server,
2511 trace_nfs4_test_lock_stateid(state, lsp, status);
2512 if (status == -NFS4ERR_EXPIRED ||
2513 status == -NFS4ERR_BAD_STATEID) {
2514 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
2515 if (!recover_lost_locks)
2516 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2517 } else if (status != NFS_OK) {
2528 * nfs41_check_open_stateid - possibly free an open stateid
2530 * @state: NFSv4 state for an inode
2532 * Returns NFS_OK if recovery for this stateid is now finished.
2533 * Otherwise a negative NFS4ERR value is returned.
2535 static int nfs41_check_open_stateid(struct nfs4_state *state)
2537 struct nfs_server *server = NFS_SERVER(state->inode);
2538 nfs4_stateid *stateid = &state->open_stateid;
2539 struct rpc_cred *cred = state->owner->so_cred;
2542 /* If a state reset has been done, test_stateid is unneeded */
2543 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2544 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2545 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2546 return -NFS4ERR_BAD_STATEID;
2548 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
2549 trace_nfs4_test_open_stateid(state, NULL, status);
2550 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
2551 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2552 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2553 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2554 clear_bit(NFS_OPEN_STATE, &state->flags);
2559 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2563 nfs41_check_delegation_stateid(state);
2564 status = nfs41_check_expired_locks(state);
2565 if (status != NFS_OK)
2567 status = nfs41_check_open_stateid(state);
2568 if (status != NFS_OK)
2569 status = nfs4_open_expired(sp, state);
2575 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2576 * fields corresponding to attributes that were used to store the verifier.
2577 * Make sure we clobber those fields in the later setattr call
2579 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2580 struct iattr *sattr, struct nfs4_label **label)
2582 const u32 *attrset = opendata->o_res.attrset;
2584 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2585 !(sattr->ia_valid & ATTR_ATIME_SET))
2586 sattr->ia_valid |= ATTR_ATIME;
2588 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2589 !(sattr->ia_valid & ATTR_MTIME_SET))
2590 sattr->ia_valid |= ATTR_MTIME;
2592 /* Except MODE, it seems harmless of setting twice. */
2593 if ((attrset[1] & FATTR4_WORD1_MODE))
2594 sattr->ia_valid &= ~ATTR_MODE;
2596 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2600 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2603 struct nfs_open_context *ctx)
2605 struct nfs4_state_owner *sp = opendata->owner;
2606 struct nfs_server *server = sp->so_server;
2607 struct dentry *dentry;
2608 struct nfs4_state *state;
2612 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2614 ret = _nfs4_proc_open(opendata);
2618 state = nfs4_opendata_to_nfs4_state(opendata);
2619 ret = PTR_ERR(state);
2622 if (server->caps & NFS_CAP_POSIX_LOCK)
2623 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2624 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2625 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
2627 dentry = opendata->dentry;
2628 if (d_really_is_negative(dentry)) {
2629 struct dentry *alias;
2631 alias = d_exact_alias(dentry, state->inode);
2633 alias = d_splice_alias(igrab(state->inode), dentry);
2634 /* d_splice_alias() can't fail here - it's a non-directory */
2637 ctx->dentry = dentry = alias;
2639 nfs_set_verifier(dentry,
2640 nfs_save_change_attribute(d_inode(opendata->dir)));
2643 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2648 if (d_inode(dentry) == state->inode) {
2649 nfs_inode_attach_open_context(ctx);
2650 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2651 nfs4_schedule_stateid_recovery(server, state);
2658 * Returns a referenced nfs4_state
2660 static int _nfs4_do_open(struct inode *dir,
2661 struct nfs_open_context *ctx,
2663 struct iattr *sattr,
2664 struct nfs4_label *label,
2667 struct nfs4_state_owner *sp;
2668 struct nfs4_state *state = NULL;
2669 struct nfs_server *server = NFS_SERVER(dir);
2670 struct nfs4_opendata *opendata;
2671 struct dentry *dentry = ctx->dentry;
2672 struct rpc_cred *cred = ctx->cred;
2673 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2674 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
2675 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
2676 struct nfs4_label *olabel = NULL;
2679 /* Protect against reboot recovery conflicts */
2681 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2683 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2686 status = nfs4_recover_expired_lease(server);
2688 goto err_put_state_owner;
2689 if (d_really_is_positive(dentry))
2690 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
2692 if (d_really_is_positive(dentry))
2693 claim = NFS4_OPEN_CLAIM_FH;
2694 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
2695 label, claim, GFP_KERNEL);
2696 if (opendata == NULL)
2697 goto err_put_state_owner;
2700 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2701 if (IS_ERR(olabel)) {
2702 status = PTR_ERR(olabel);
2703 goto err_opendata_put;
2707 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2708 if (!opendata->f_attr.mdsthreshold) {
2709 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2710 if (!opendata->f_attr.mdsthreshold)
2711 goto err_free_label;
2713 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
2715 if (d_really_is_positive(dentry))
2716 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
2718 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
2720 goto err_free_label;
2723 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
2724 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
2725 nfs4_exclusive_attrset(opendata, sattr, &label);
2727 * send create attributes which was not set by open
2728 * with an extra setattr.
2730 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2731 nfs_fattr_init(opendata->o_res.f_attr);
2732 status = nfs4_do_setattr(state->inode, cred,
2733 opendata->o_res.f_attr, sattr,
2734 state, label, olabel);
2736 nfs_setattr_update_inode(state->inode, sattr,
2737 opendata->o_res.f_attr);
2738 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2742 if (opened && opendata->file_created)
2743 *opened |= FILE_CREATED;
2745 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
2746 *ctx_th = opendata->f_attr.mdsthreshold;
2747 opendata->f_attr.mdsthreshold = NULL;
2750 nfs4_label_free(olabel);
2752 nfs4_opendata_put(opendata);
2753 nfs4_put_state_owner(sp);
2756 nfs4_label_free(olabel);
2758 nfs4_opendata_put(opendata);
2759 err_put_state_owner:
2760 nfs4_put_state_owner(sp);
2766 static struct nfs4_state *nfs4_do_open(struct inode *dir,
2767 struct nfs_open_context *ctx,
2769 struct iattr *sattr,
2770 struct nfs4_label *label,
2773 struct nfs_server *server = NFS_SERVER(dir);
2774 struct nfs4_exception exception = { };
2775 struct nfs4_state *res;
2779 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
2781 trace_nfs4_open_file(ctx, flags, status);
2784 /* NOTE: BAD_SEQID means the server and client disagree about the
2785 * book-keeping w.r.t. state-changing operations
2786 * (OPEN/CLOSE/LOCK/LOCKU...)
2787 * It is actually a sign of a bug on the client or on the server.
2789 * If we receive a BAD_SEQID error in the particular case of
2790 * doing an OPEN, we assume that nfs_increment_open_seqid() will
2791 * have unhashed the old state_owner for us, and that we can
2792 * therefore safely retry using a new one. We should still warn
2793 * the user though...
2795 if (status == -NFS4ERR_BAD_SEQID) {
2796 pr_warn_ratelimited("NFS: v4 server %s "
2797 " returned a bad sequence-id error!\n",
2798 NFS_SERVER(dir)->nfs_client->cl_hostname);
2799 exception.retry = 1;
2803 * BAD_STATEID on OPEN means that the server cancelled our
2804 * state before it received the OPEN_CONFIRM.
2805 * Recover by retrying the request as per the discussion
2806 * on Page 181 of RFC3530.
2808 if (status == -NFS4ERR_BAD_STATEID) {
2809 exception.retry = 1;
2812 if (status == -EAGAIN) {
2813 /* We must have found a delegation */
2814 exception.retry = 1;
2817 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2819 res = ERR_PTR(nfs4_handle_exception(server,
2820 status, &exception));
2821 } while (exception.retry);
2825 static int _nfs4_do_setattr(struct inode *inode,
2826 struct nfs_setattrargs *arg,
2827 struct nfs_setattrres *res,
2828 struct rpc_cred *cred,
2829 struct nfs4_state *state)
2831 struct nfs_server *server = NFS_SERVER(inode);
2832 struct rpc_message msg = {
2833 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2838 struct rpc_cred *delegation_cred = NULL;
2839 unsigned long timestamp = jiffies;
2844 nfs_fattr_init(res->fattr);
2846 /* Servers should only apply open mode checks for file size changes */
2847 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
2848 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2850 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
2851 /* Use that stateid */
2852 } else if (truncate && state != NULL) {
2853 struct nfs_lockowner lockowner = {
2854 .l_owner = current->files,
2855 .l_pid = current->tgid,
2857 if (!nfs4_valid_open_stateid(state))
2859 if (nfs4_select_rw_stateid(state, FMODE_WRITE, &lockowner,
2860 &arg->stateid, &delegation_cred) == -EIO)
2863 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
2864 if (delegation_cred)
2865 msg.rpc_cred = delegation_cred;
2867 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
2869 put_rpccred(delegation_cred);
2870 if (status == 0 && state != NULL)
2871 renew_lease(server, timestamp);
2872 trace_nfs4_setattr(inode, &arg->stateid, status);
2876 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2877 struct nfs_fattr *fattr, struct iattr *sattr,
2878 struct nfs4_state *state, struct nfs4_label *ilabel,
2879 struct nfs4_label *olabel)
2881 struct nfs_server *server = NFS_SERVER(inode);
2882 struct nfs_setattrargs arg = {
2883 .fh = NFS_FH(inode),
2886 .bitmask = server->attr_bitmask,
2889 struct nfs_setattrres res = {
2894 struct nfs4_exception exception = {
2897 .stateid = &arg.stateid,
2901 arg.bitmask = nfs4_bitmask(server, ilabel);
2903 arg.bitmask = nfs4_bitmask(server, olabel);
2906 err = _nfs4_do_setattr(inode, &arg, &res, cred, state);
2908 case -NFS4ERR_OPENMODE:
2909 if (!(sattr->ia_valid & ATTR_SIZE)) {
2910 pr_warn_once("NFSv4: server %s is incorrectly "
2911 "applying open mode checks to "
2912 "a SETATTR that is not "
2913 "changing file size.\n",
2914 server->nfs_client->cl_hostname);
2916 if (state && !(state->state & FMODE_WRITE)) {
2918 if (sattr->ia_valid & ATTR_OPEN)
2923 err = nfs4_handle_exception(server, err, &exception);
2924 } while (exception.retry);
2930 nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
2932 if (inode == NULL || !nfs_have_layout(inode))
2935 return pnfs_wait_on_layoutreturn(inode, task);
2938 struct nfs4_closedata {
2939 struct inode *inode;
2940 struct nfs4_state *state;
2941 struct nfs_closeargs arg;
2942 struct nfs_closeres res;
2943 struct nfs_fattr fattr;
2944 unsigned long timestamp;
2949 static void nfs4_free_closedata(void *data)
2951 struct nfs4_closedata *calldata = data;
2952 struct nfs4_state_owner *sp = calldata->state->owner;
2953 struct super_block *sb = calldata->state->inode->i_sb;
2956 pnfs_roc_release(calldata->state->inode);
2957 nfs4_put_open_state(calldata->state);
2958 nfs_free_seqid(calldata->arg.seqid);
2959 nfs4_put_state_owner(sp);
2960 nfs_sb_deactive(sb);
2964 static void nfs4_close_done(struct rpc_task *task, void *data)
2966 struct nfs4_closedata *calldata = data;
2967 struct nfs4_state *state = calldata->state;
2968 struct nfs_server *server = NFS_SERVER(calldata->inode);
2969 nfs4_stateid *res_stateid = NULL;
2971 dprintk("%s: begin!\n", __func__);
2972 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2974 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
2975 /* hmm. we are done with the inode, and in the process of freeing
2976 * the state_owner. we keep this around to process errors
2978 switch (task->tk_status) {
2980 res_stateid = &calldata->res.stateid;
2982 pnfs_roc_set_barrier(state->inode,
2983 calldata->roc_barrier);
2984 renew_lease(server, calldata->timestamp);
2986 case -NFS4ERR_ADMIN_REVOKED:
2987 case -NFS4ERR_STALE_STATEID:
2988 case -NFS4ERR_OLD_STATEID:
2989 case -NFS4ERR_BAD_STATEID:
2990 case -NFS4ERR_EXPIRED:
2991 if (!nfs4_stateid_match(&calldata->arg.stateid,
2992 &state->open_stateid)) {
2993 rpc_restart_call_prepare(task);
2996 if (calldata->arg.fmode == 0)
2999 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
3000 rpc_restart_call_prepare(task);
3004 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3005 res_stateid, calldata->arg.fmode);
3007 nfs_release_seqid(calldata->arg.seqid);
3008 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
3009 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
3012 static void nfs4_close_prepare(struct rpc_task *task, void *data)
3014 struct nfs4_closedata *calldata = data;
3015 struct nfs4_state *state = calldata->state;
3016 struct inode *inode = calldata->inode;
3017 bool is_rdonly, is_wronly, is_rdwr;
3020 dprintk("%s: begin!\n", __func__);
3021 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3024 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
3025 spin_lock(&state->owner->so_lock);
3026 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3027 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3028 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
3029 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
3030 /* Calculate the change in open mode */
3031 calldata->arg.fmode = 0;
3032 if (state->n_rdwr == 0) {
3033 if (state->n_rdonly == 0)
3034 call_close |= is_rdonly;
3036 calldata->arg.fmode |= FMODE_READ;
3037 if (state->n_wronly == 0)
3038 call_close |= is_wronly;
3040 calldata->arg.fmode |= FMODE_WRITE;
3041 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3042 call_close |= is_rdwr;
3044 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3046 if (!nfs4_valid_open_stateid(state))
3048 spin_unlock(&state->owner->so_lock);
3051 /* Note: exit _without_ calling nfs4_close_done */
3055 if (nfs4_wait_on_layoutreturn(inode, task)) {
3056 nfs_release_seqid(calldata->arg.seqid);
3060 if (calldata->arg.fmode == 0)
3061 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
3063 pnfs_roc_get_barrier(inode, &calldata->roc_barrier);
3065 calldata->arg.share_access =
3066 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3067 calldata->arg.fmode, 0);
3069 nfs_fattr_init(calldata->res.fattr);
3070 calldata->timestamp = jiffies;
3071 if (nfs4_setup_sequence(NFS_SERVER(inode),
3072 &calldata->arg.seq_args,
3073 &calldata->res.seq_res,
3075 nfs_release_seqid(calldata->arg.seqid);
3076 dprintk("%s: done!\n", __func__);
3079 task->tk_action = NULL;
3081 nfs4_sequence_done(task, &calldata->res.seq_res);
3084 static const struct rpc_call_ops nfs4_close_ops = {
3085 .rpc_call_prepare = nfs4_close_prepare,
3086 .rpc_call_done = nfs4_close_done,
3087 .rpc_release = nfs4_free_closedata,
3090 static bool nfs4_roc(struct inode *inode)
3092 if (!nfs_have_layout(inode))
3094 return pnfs_roc(inode);
3098 * It is possible for data to be read/written from a mem-mapped file
3099 * after the sys_close call (which hits the vfs layer as a flush).
3100 * This means that we can't safely call nfsv4 close on a file until
3101 * the inode is cleared. This in turn means that we are not good
3102 * NFSv4 citizens - we do not indicate to the server to update the file's
3103 * share state even when we are done with one of the three share
3104 * stateid's in the inode.
3106 * NOTE: Caller must be holding the sp->so_owner semaphore!
3108 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
3110 struct nfs_server *server = NFS_SERVER(state->inode);
3111 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
3112 struct nfs4_closedata *calldata;
3113 struct nfs4_state_owner *sp = state->owner;
3114 struct rpc_task *task;
3115 struct rpc_message msg = {
3116 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3117 .rpc_cred = state->owner->so_cred,
3119 struct rpc_task_setup task_setup_data = {
3120 .rpc_client = server->client,
3121 .rpc_message = &msg,
3122 .callback_ops = &nfs4_close_ops,
3123 .workqueue = nfsiod_workqueue,
3124 .flags = RPC_TASK_ASYNC,
3126 int status = -ENOMEM;
3128 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3129 &task_setup_data.rpc_client, &msg);
3131 calldata = kzalloc(sizeof(*calldata), gfp_mask);
3132 if (calldata == NULL)
3134 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
3135 calldata->inode = state->inode;
3136 calldata->state = state;
3137 calldata->arg.fh = NFS_FH(state->inode);
3138 /* Serialization for the sequence id */
3139 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3140 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
3141 if (IS_ERR(calldata->arg.seqid))
3142 goto out_free_calldata;
3143 calldata->arg.fmode = 0;
3144 calldata->arg.bitmask = server->cache_consistency_bitmask;
3145 calldata->res.fattr = &calldata->fattr;
3146 calldata->res.seqid = calldata->arg.seqid;
3147 calldata->res.server = server;
3148 calldata->roc = nfs4_roc(state->inode);
3149 nfs_sb_active(calldata->inode->i_sb);
3151 msg.rpc_argp = &calldata->arg;
3152 msg.rpc_resp = &calldata->res;
3153 task_setup_data.callback_data = calldata;
3154 task = rpc_run_task(&task_setup_data);
3156 return PTR_ERR(task);
3159 status = rpc_wait_for_completion_task(task);
3165 nfs4_put_open_state(state);
3166 nfs4_put_state_owner(sp);
3170 static struct inode *
3171 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3172 int open_flags, struct iattr *attr, int *opened)
3174 struct nfs4_state *state;
3175 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3177 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
3179 /* Protect against concurrent sillydeletes */
3180 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
3182 nfs4_label_release_security(label);
3185 return ERR_CAST(state);
3186 return state->inode;
3189 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
3191 if (ctx->state == NULL)
3194 nfs4_close_sync(ctx->state, ctx->mode);
3196 nfs4_close_state(ctx->state, ctx->mode);
3199 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3200 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3201 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
3203 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3205 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
3206 struct nfs4_server_caps_arg args = {
3210 struct nfs4_server_caps_res res = {};
3211 struct rpc_message msg = {
3212 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
3218 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3219 FATTR4_WORD0_FH_EXPIRE_TYPE |
3220 FATTR4_WORD0_LINK_SUPPORT |
3221 FATTR4_WORD0_SYMLINK_SUPPORT |
3222 FATTR4_WORD0_ACLSUPPORT;
3224 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3226 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3228 /* Sanity check the server answers */
3229 switch (minorversion) {
3231 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3232 res.attr_bitmask[2] = 0;
3235 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3238 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3240 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
3241 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3242 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3243 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3244 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
3245 NFS_CAP_CTIME|NFS_CAP_MTIME|
3246 NFS_CAP_SECURITY_LABEL);
3247 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3248 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3249 server->caps |= NFS_CAP_ACLS;
3250 if (res.has_links != 0)
3251 server->caps |= NFS_CAP_HARDLINKS;
3252 if (res.has_symlinks != 0)
3253 server->caps |= NFS_CAP_SYMLINKS;
3254 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3255 server->caps |= NFS_CAP_FILEID;
3256 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3257 server->caps |= NFS_CAP_MODE;
3258 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3259 server->caps |= NFS_CAP_NLINK;
3260 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3261 server->caps |= NFS_CAP_OWNER;
3262 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3263 server->caps |= NFS_CAP_OWNER_GROUP;
3264 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3265 server->caps |= NFS_CAP_ATIME;
3266 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3267 server->caps |= NFS_CAP_CTIME;
3268 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3269 server->caps |= NFS_CAP_MTIME;
3270 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3271 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3272 server->caps |= NFS_CAP_SECURITY_LABEL;
3274 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3275 sizeof(server->attr_bitmask));
3276 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3278 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3279 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3280 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
3281 server->cache_consistency_bitmask[2] = 0;
3282 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3283 sizeof(server->exclcreat_bitmask));
3284 server->acl_bitmask = res.acl_bitmask;
3285 server->fh_expire_type = res.fh_expire_type;
3291 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3293 struct nfs4_exception exception = { };
3296 err = nfs4_handle_exception(server,
3297 _nfs4_server_capabilities(server, fhandle),
3299 } while (exception.retry);
3303 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3304 struct nfs_fsinfo *info)
3307 struct nfs4_lookup_root_arg args = {
3310 struct nfs4_lookup_res res = {
3312 .fattr = info->fattr,
3315 struct rpc_message msg = {
3316 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3321 bitmask[0] = nfs4_fattr_bitmap[0];
3322 bitmask[1] = nfs4_fattr_bitmap[1];
3324 * Process the label in the upcoming getfattr
3326 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3328 nfs_fattr_init(info->fattr);
3329 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3332 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3333 struct nfs_fsinfo *info)
3335 struct nfs4_exception exception = { };
3338 err = _nfs4_lookup_root(server, fhandle, info);
3339 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
3342 case -NFS4ERR_WRONGSEC:
3345 err = nfs4_handle_exception(server, err, &exception);
3347 } while (exception.retry);
3352 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3353 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3355 struct rpc_auth_create_args auth_args = {
3356 .pseudoflavor = flavor,
3358 struct rpc_auth *auth;
3361 auth = rpcauth_create(&auth_args, server->client);
3366 ret = nfs4_lookup_root(server, fhandle, info);
3372 * Retry pseudoroot lookup with various security flavors. We do this when:
3374 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3375 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3377 * Returns zero on success, or a negative NFS4ERR value, or a
3378 * negative errno value.
3380 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3381 struct nfs_fsinfo *info)
3383 /* Per 3530bis 15.33.5 */
3384 static const rpc_authflavor_t flav_array[] = {
3388 RPC_AUTH_UNIX, /* courtesy */
3391 int status = -EPERM;
3394 if (server->auth_info.flavor_len > 0) {
3395 /* try each flavor specified by user */
3396 for (i = 0; i < server->auth_info.flavor_len; i++) {
3397 status = nfs4_lookup_root_sec(server, fhandle, info,
3398 server->auth_info.flavors[i]);
3399 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3404 /* no flavors specified by user, try default list */
3405 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3406 status = nfs4_lookup_root_sec(server, fhandle, info,
3408 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3415 * -EACCESS could mean that the user doesn't have correct permissions
3416 * to access the mount. It could also mean that we tried to mount
3417 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3418 * existing mount programs don't handle -EACCES very well so it should
3419 * be mapped to -EPERM instead.
3421 if (status == -EACCES)
3427 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3428 * @server: initialized nfs_server handle
3429 * @fhandle: we fill in the pseudo-fs root file handle
3430 * @info: we fill in an FSINFO struct
3431 * @auth_probe: probe the auth flavours
3433 * Returns zero on success, or a negative errno.
3435 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
3436 struct nfs_fsinfo *info,
3442 status = nfs4_lookup_root(server, fhandle, info);
3444 if (auth_probe || status == NFS4ERR_WRONGSEC)
3445 status = server->nfs_client->cl_mvops->find_root_sec(server,
3449 status = nfs4_server_capabilities(server, fhandle);
3451 status = nfs4_do_fsinfo(server, fhandle, info);
3453 return nfs4_map_errors(status);
3456 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3457 struct nfs_fsinfo *info)
3460 struct nfs_fattr *fattr = info->fattr;
3461 struct nfs4_label *label = NULL;
3463 error = nfs4_server_capabilities(server, mntfh);
3465 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3469 label = nfs4_label_alloc(server, GFP_KERNEL);
3471 return PTR_ERR(label);
3473 error = nfs4_proc_getattr(server, mntfh, fattr, label);
3475 dprintk("nfs4_get_root: getattr error = %d\n", -error);
3476 goto err_free_label;
3479 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3480 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3481 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3484 nfs4_label_free(label);
3490 * Get locations and (maybe) other attributes of a referral.
3491 * Note that we'll actually follow the referral later when
3492 * we detect fsid mismatch in inode revalidation
3494 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3495 const struct qstr *name, struct nfs_fattr *fattr,
3496 struct nfs_fh *fhandle)
3498 int status = -ENOMEM;
3499 struct page *page = NULL;
3500 struct nfs4_fs_locations *locations = NULL;
3502 page = alloc_page(GFP_KERNEL);
3505 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3506 if (locations == NULL)
3509 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
3514 * If the fsid didn't change, this is a migration event, not a
3515 * referral. Cause us to drop into the exception handler, which
3516 * will kick off migration recovery.
3518 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
3519 dprintk("%s: server did not return a different fsid for"
3520 " a referral at %s\n", __func__, name->name);
3521 status = -NFS4ERR_MOVED;
3524 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3525 nfs_fixup_referral_attributes(&locations->fattr);
3527 /* replace the lookup nfs_fattr with the locations nfs_fattr */
3528 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
3529 memset(fhandle, 0, sizeof(struct nfs_fh));
3537 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3538 struct nfs_fattr *fattr, struct nfs4_label *label)
3540 struct nfs4_getattr_arg args = {
3542 .bitmask = server->attr_bitmask,
3544 struct nfs4_getattr_res res = {
3549 struct rpc_message msg = {
3550 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3555 args.bitmask = nfs4_bitmask(server, label);
3557 nfs_fattr_init(fattr);
3558 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3561 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3562 struct nfs_fattr *fattr, struct nfs4_label *label)
3564 struct nfs4_exception exception = { };
3567 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3568 trace_nfs4_getattr(server, fhandle, fattr, err);
3569 err = nfs4_handle_exception(server, err,
3571 } while (exception.retry);
3576 * The file is not closed if it is opened due to the a request to change
3577 * the size of the file. The open call will not be needed once the
3578 * VFS layer lookup-intents are implemented.
3580 * Close is called when the inode is destroyed.
3581 * If we haven't opened the file for O_WRONLY, we
3582 * need to in the size_change case to obtain a stateid.
3585 * Because OPEN is always done by name in nfsv4, it is
3586 * possible that we opened a different file by the same
3587 * name. We can recognize this race condition, but we
3588 * can't do anything about it besides returning an error.
3590 * This will be fixed with VFS changes (lookup-intent).
3593 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3594 struct iattr *sattr)
3596 struct inode *inode = d_inode(dentry);
3597 struct rpc_cred *cred = NULL;
3598 struct nfs4_state *state = NULL;
3599 struct nfs4_label *label = NULL;
3602 if (pnfs_ld_layoutret_on_setattr(inode) &&
3603 sattr->ia_valid & ATTR_SIZE &&
3604 sattr->ia_size < i_size_read(inode))
3605 pnfs_commit_and_return_layout(inode);
3607 nfs_fattr_init(fattr);
3609 /* Deal with open(O_TRUNC) */
3610 if (sattr->ia_valid & ATTR_OPEN)
3611 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
3613 /* Optimization: if the end result is no change, don't RPC */
3614 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
3617 /* Search for an existing open(O_WRITE) file */
3618 if (sattr->ia_valid & ATTR_FILE) {
3619 struct nfs_open_context *ctx;
3621 ctx = nfs_file_open_context(sattr->ia_file);
3628 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3630 return PTR_ERR(label);
3632 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
3634 nfs_setattr_update_inode(inode, sattr, fattr);
3635 nfs_setsecurity(inode, fattr, label);
3637 nfs4_label_free(label);
3641 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3642 const struct qstr *name, struct nfs_fh *fhandle,
3643 struct nfs_fattr *fattr, struct nfs4_label *label)
3645 struct nfs_server *server = NFS_SERVER(dir);
3647 struct nfs4_lookup_arg args = {
3648 .bitmask = server->attr_bitmask,
3649 .dir_fh = NFS_FH(dir),
3652 struct nfs4_lookup_res res = {
3658 struct rpc_message msg = {
3659 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3664 args.bitmask = nfs4_bitmask(server, label);
3666 nfs_fattr_init(fattr);
3668 dprintk("NFS call lookup %s\n", name->name);
3669 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
3670 dprintk("NFS reply lookup: %d\n", status);
3674 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
3676 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
3677 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
3678 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3682 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3683 const struct qstr *name, struct nfs_fh *fhandle,
3684 struct nfs_fattr *fattr, struct nfs4_label *label)
3686 struct nfs4_exception exception = { };
3687 struct rpc_clnt *client = *clnt;
3690 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
3691 trace_nfs4_lookup(dir, name, err);
3693 case -NFS4ERR_BADNAME:
3696 case -NFS4ERR_MOVED:
3697 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
3698 if (err == -NFS4ERR_MOVED)
3699 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3701 case -NFS4ERR_WRONGSEC:
3703 if (client != *clnt)
3705 client = nfs4_negotiate_security(client, dir, name);
3707 return PTR_ERR(client);
3709 exception.retry = 1;
3712 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3714 } while (exception.retry);
3719 else if (client != *clnt)
3720 rpc_shutdown_client(client);
3725 static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
3726 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3727 struct nfs4_label *label)
3730 struct rpc_clnt *client = NFS_CLIENT(dir);
3732 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
3733 if (client != NFS_CLIENT(dir)) {
3734 rpc_shutdown_client(client);
3735 nfs_fixup_secinfo_attributes(fattr);
3741 nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
3742 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3744 struct rpc_clnt *client = NFS_CLIENT(dir);
3747 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
3749 return ERR_PTR(status);
3750 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
3753 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3755 struct nfs_server *server = NFS_SERVER(inode);
3756 struct nfs4_accessargs args = {
3757 .fh = NFS_FH(inode),
3758 .bitmask = server->cache_consistency_bitmask,
3760 struct nfs4_accessres res = {
3763 struct rpc_message msg = {
3764 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3767 .rpc_cred = entry->cred,
3769 int mode = entry->mask;
3773 * Determine which access bits we want to ask for...
3775 if (mode & MAY_READ)
3776 args.access |= NFS4_ACCESS_READ;
3777 if (S_ISDIR(inode->i_mode)) {
3778 if (mode & MAY_WRITE)
3779 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3780 if (mode & MAY_EXEC)
3781 args.access |= NFS4_ACCESS_LOOKUP;
3783 if (mode & MAY_WRITE)
3784 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3785 if (mode & MAY_EXEC)
3786 args.access |= NFS4_ACCESS_EXECUTE;
3789 res.fattr = nfs_alloc_fattr();
3790 if (res.fattr == NULL)
3793 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3795 nfs_access_set_mask(entry, res.access);
3796 nfs_refresh_inode(inode, res.fattr);
3798 nfs_free_fattr(res.fattr);
3802 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3804 struct nfs4_exception exception = { };
3807 err = _nfs4_proc_access(inode, entry);
3808 trace_nfs4_access(inode, err);
3809 err = nfs4_handle_exception(NFS_SERVER(inode), err,
3811 } while (exception.retry);
3816 * TODO: For the time being, we don't try to get any attributes
3817 * along with any of the zero-copy operations READ, READDIR,
3820 * In the case of the first three, we want to put the GETATTR
3821 * after the read-type operation -- this is because it is hard
3822 * to predict the length of a GETATTR response in v4, and thus
3823 * align the READ data correctly. This means that the GETATTR
3824 * may end up partially falling into the page cache, and we should
3825 * shift it into the 'tail' of the xdr_buf before processing.
3826 * To do this efficiently, we need to know the total length
3827 * of data received, which doesn't seem to be available outside
3830 * In the case of WRITE, we also want to put the GETATTR after
3831 * the operation -- in this case because we want to make sure
3832 * we get the post-operation mtime and size.
3834 * Both of these changes to the XDR layer would in fact be quite
3835 * minor, but I decided to leave them for a subsequent patch.
3837 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3838 unsigned int pgbase, unsigned int pglen)
3840 struct nfs4_readlink args = {
3841 .fh = NFS_FH(inode),
3846 struct nfs4_readlink_res res;
3847 struct rpc_message msg = {
3848 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3853 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
3856 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3857 unsigned int pgbase, unsigned int pglen)
3859 struct nfs4_exception exception = { };
3862 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3863 trace_nfs4_readlink(inode, err);
3864 err = nfs4_handle_exception(NFS_SERVER(inode), err,
3866 } while (exception.retry);
3871 * This is just for mknod. open(O_CREAT) will always do ->open_context().
3874 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
3877 struct nfs4_label l, *ilabel = NULL;
3878 struct nfs_open_context *ctx;
3879 struct nfs4_state *state;
3882 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3884 return PTR_ERR(ctx);
3886 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3888 sattr->ia_mode &= ~current_umask();
3889 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
3890 if (IS_ERR(state)) {
3891 status = PTR_ERR(state);
3895 nfs4_label_release_security(ilabel);
3896 put_nfs_open_context(ctx);
3900 static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
3902 struct nfs_server *server = NFS_SERVER(dir);
3903 struct nfs_removeargs args = {
3907 struct nfs_removeres res = {
3910 struct rpc_message msg = {
3911 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3917 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
3919 update_changeattr(dir, &res.cinfo);
3923 static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
3925 struct nfs4_exception exception = { };
3928 err = _nfs4_proc_remove(dir, name);
3929 trace_nfs4_remove(dir, name, err);
3930 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3932 } while (exception.retry);
3936 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
3938 struct nfs_server *server = NFS_SERVER(dir);
3939 struct nfs_removeargs *args = msg->rpc_argp;
3940 struct nfs_removeres *res = msg->rpc_resp;
3942 res->server = server;
3943 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
3944 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
3946 nfs_fattr_init(res->dir_attr);
3949 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3951 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb),
3952 &data->args.seq_args,
3957 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
3959 struct nfs_unlinkdata *data = task->tk_calldata;
3960 struct nfs_removeres *res = &data->res;
3962 if (!nfs4_sequence_done(task, &res->seq_res))
3964 if (nfs4_async_handle_error(task, res->server, NULL,
3965 &data->timeout) == -EAGAIN)
3967 update_changeattr(dir, &res->cinfo);
3971 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3973 struct nfs_server *server = NFS_SERVER(dir);
3974 struct nfs_renameargs *arg = msg->rpc_argp;
3975 struct nfs_renameres *res = msg->rpc_resp;
3977 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
3978 res->server = server;
3979 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
3982 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3984 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3985 &data->args.seq_args,
3990 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3991 struct inode *new_dir)
3993 struct nfs_renamedata *data = task->tk_calldata;
3994 struct nfs_renameres *res = &data->res;
3996 if (!nfs4_sequence_done(task, &res->seq_res))
3998 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
4001 update_changeattr(old_dir, &res->old_cinfo);
4002 update_changeattr(new_dir, &res->new_cinfo);
4006 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4008 struct nfs_server *server = NFS_SERVER(inode);
4009 struct nfs4_link_arg arg = {
4010 .fh = NFS_FH(inode),
4011 .dir_fh = NFS_FH(dir),
4013 .bitmask = server->attr_bitmask,
4015 struct nfs4_link_res res = {
4019 struct rpc_message msg = {
4020 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4024 int status = -ENOMEM;
4026 res.fattr = nfs_alloc_fattr();
4027 if (res.fattr == NULL)
4030 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4031 if (IS_ERR(res.label)) {
4032 status = PTR_ERR(res.label);
4035 arg.bitmask = nfs4_bitmask(server, res.label);
4037 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4039 update_changeattr(dir, &res.cinfo);
4040 status = nfs_post_op_update_inode(inode, res.fattr);
4042 nfs_setsecurity(inode, res.fattr, res.label);
4046 nfs4_label_free(res.label);
4049 nfs_free_fattr(res.fattr);
4053 static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4055 struct nfs4_exception exception = { };
4058 err = nfs4_handle_exception(NFS_SERVER(inode),
4059 _nfs4_proc_link(inode, dir, name),
4061 } while (exception.retry);
4065 struct nfs4_createdata {
4066 struct rpc_message msg;
4067 struct nfs4_create_arg arg;
4068 struct nfs4_create_res res;
4070 struct nfs_fattr fattr;
4071 struct nfs4_label *label;
4074 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
4075 const struct qstr *name, struct iattr *sattr, u32 ftype)
4077 struct nfs4_createdata *data;
4079 data = kzalloc(sizeof(*data), GFP_KERNEL);
4081 struct nfs_server *server = NFS_SERVER(dir);
4083 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4084 if (IS_ERR(data->label))
4087 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4088 data->msg.rpc_argp = &data->arg;
4089 data->msg.rpc_resp = &data->res;
4090 data->arg.dir_fh = NFS_FH(dir);
4091 data->arg.server = server;
4092 data->arg.name = name;
4093 data->arg.attrs = sattr;
4094 data->arg.ftype = ftype;
4095 data->arg.bitmask = nfs4_bitmask(server, data->label);
4096 data->res.server = server;
4097 data->res.fh = &data->fh;
4098 data->res.fattr = &data->fattr;
4099 data->res.label = data->label;
4100 nfs_fattr_init(data->res.fattr);
4108 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4110 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
4111 &data->arg.seq_args, &data->res.seq_res, 1);
4113 update_changeattr(dir, &data->res.dir_cinfo);
4114 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
4119 static void nfs4_free_createdata(struct nfs4_createdata *data)
4121 nfs4_label_free(data->label);
4125 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4126 struct page *page, unsigned int len, struct iattr *sattr,
4127 struct nfs4_label *label)
4129 struct nfs4_createdata *data;
4130 int status = -ENAMETOOLONG;
4132 if (len > NFS4_MAXPATHLEN)
4136 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4140 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4141 data->arg.u.symlink.pages = &page;
4142 data->arg.u.symlink.len = len;
4143 data->arg.label = label;
4145 status = nfs4_do_create(dir, dentry, data);
4147 nfs4_free_createdata(data);
4152 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4153 struct page *page, unsigned int len, struct iattr *sattr)
4155 struct nfs4_exception exception = { };
4156 struct nfs4_label l, *label = NULL;
4159 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4162 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4163 trace_nfs4_symlink(dir, &dentry->d_name, err);
4164 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4166 } while (exception.retry);
4168 nfs4_label_release_security(label);
4172 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4173 struct iattr *sattr, struct nfs4_label *label)
4175 struct nfs4_createdata *data;
4176 int status = -ENOMEM;
4178 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4182 data->arg.label = label;
4183 status = nfs4_do_create(dir, dentry, data);
4185 nfs4_free_createdata(data);
4190 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4191 struct iattr *sattr)
4193 struct nfs4_exception exception = { };
4194 struct nfs4_label l, *label = NULL;
4197 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4199 sattr->ia_mode &= ~current_umask();
4201 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4202 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4203 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4205 } while (exception.retry);
4206 nfs4_label_release_security(label);
4211 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
4212 u64 cookie, struct page **pages, unsigned int count, int plus)
4214 struct inode *dir = d_inode(dentry);
4215 struct nfs4_readdir_arg args = {
4220 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
4223 struct nfs4_readdir_res res;
4224 struct rpc_message msg = {
4225 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4232 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4234 (unsigned long long)cookie);
4235 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
4236 res.pgbase = args.pgbase;
4237 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4239 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
4240 status += args.pgbase;
4243 nfs_invalidate_atime(dir);
4245 dprintk("%s: returns %d\n", __func__, status);
4249 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
4250 u64 cookie, struct page **pages, unsigned int count, int plus)
4252 struct nfs4_exception exception = { };
4255 err = _nfs4_proc_readdir(dentry, cred, cookie,
4256 pages, count, plus);
4257 trace_nfs4_readdir(d_inode(dentry), err);
4258 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
4260 } while (exception.retry);
4264 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4265 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
4267 struct nfs4_createdata *data;
4268 int mode = sattr->ia_mode;
4269 int status = -ENOMEM;
4271 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4276 data->arg.ftype = NF4FIFO;
4277 else if (S_ISBLK(mode)) {
4278 data->arg.ftype = NF4BLK;
4279 data->arg.u.device.specdata1 = MAJOR(rdev);
4280 data->arg.u.device.specdata2 = MINOR(rdev);
4282 else if (S_ISCHR(mode)) {
4283 data->arg.ftype = NF4CHR;
4284 data->arg.u.device.specdata1 = MAJOR(rdev);
4285 data->arg.u.device.specdata2 = MINOR(rdev);
4286 } else if (!S_ISSOCK(mode)) {
4291 data->arg.label = label;
4292 status = nfs4_do_create(dir, dentry, data);
4294 nfs4_free_createdata(data);
4299 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4300 struct iattr *sattr, dev_t rdev)
4302 struct nfs4_exception exception = { };
4303 struct nfs4_label l, *label = NULL;
4306 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4308 sattr->ia_mode &= ~current_umask();
4310 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4311 trace_nfs4_mknod(dir, &dentry->d_name, err);
4312 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4314 } while (exception.retry);
4316 nfs4_label_release_security(label);
4321 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4322 struct nfs_fsstat *fsstat)
4324 struct nfs4_statfs_arg args = {
4326 .bitmask = server->attr_bitmask,
4328 struct nfs4_statfs_res res = {
4331 struct rpc_message msg = {
4332 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4337 nfs_fattr_init(fsstat->fattr);
4338 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4341 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4343 struct nfs4_exception exception = { };
4346 err = nfs4_handle_exception(server,
4347 _nfs4_proc_statfs(server, fhandle, fsstat),
4349 } while (exception.retry);
4353 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4354 struct nfs_fsinfo *fsinfo)
4356 struct nfs4_fsinfo_arg args = {
4358 .bitmask = server->attr_bitmask,
4360 struct nfs4_fsinfo_res res = {
4363 struct rpc_message msg = {
4364 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4369 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4372 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4374 struct nfs4_exception exception = { };
4375 unsigned long now = jiffies;
4379 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
4380 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
4382 nfs4_set_lease_period(server->nfs_client,
4383 fsinfo->lease_time * HZ,
4387 err = nfs4_handle_exception(server, err, &exception);
4388 } while (exception.retry);
4392 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4396 nfs_fattr_init(fsinfo->fattr);
4397 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
4399 /* block layout checks this! */
4400 server->pnfs_blksize = fsinfo->blksize;
4401 set_pnfs_layoutdriver(server, fhandle, fsinfo);
4407 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4408 struct nfs_pathconf *pathconf)
4410 struct nfs4_pathconf_arg args = {
4412 .bitmask = server->attr_bitmask,
4414 struct nfs4_pathconf_res res = {
4415 .pathconf = pathconf,
4417 struct rpc_message msg = {
4418 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4423 /* None of the pathconf attributes are mandatory to implement */
4424 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4425 memset(pathconf, 0, sizeof(*pathconf));
4429 nfs_fattr_init(pathconf->fattr);
4430 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4433 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4434 struct nfs_pathconf *pathconf)
4436 struct nfs4_exception exception = { };
4440 err = nfs4_handle_exception(server,
4441 _nfs4_proc_pathconf(server, fhandle, pathconf),
4443 } while (exception.retry);
4447 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
4448 const struct nfs_open_context *ctx,
4449 const struct nfs_lock_context *l_ctx,
4452 const struct nfs_lockowner *lockowner = NULL;
4455 lockowner = &l_ctx->lockowner;
4456 return nfs4_select_rw_stateid(ctx->state, fmode, lockowner, stateid, NULL);
4458 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4460 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4461 const struct nfs_open_context *ctx,
4462 const struct nfs_lock_context *l_ctx,
4465 nfs4_stateid current_stateid;
4467 /* If the current stateid represents a lost lock, then exit */
4468 if (nfs4_set_rw_stateid(¤t_stateid, ctx, l_ctx, fmode) == -EIO)
4470 return nfs4_stateid_match(stateid, ¤t_stateid);
4473 static bool nfs4_error_stateid_expired(int err)
4476 case -NFS4ERR_DELEG_REVOKED:
4477 case -NFS4ERR_ADMIN_REVOKED:
4478 case -NFS4ERR_BAD_STATEID:
4479 case -NFS4ERR_STALE_STATEID:
4480 case -NFS4ERR_OLD_STATEID:
4481 case -NFS4ERR_OPENMODE:
4482 case -NFS4ERR_EXPIRED:
4488 void __nfs4_read_done_cb(struct nfs_pgio_header *hdr)
4490 nfs_invalidate_atime(hdr->inode);
4493 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
4495 struct nfs_server *server = NFS_SERVER(hdr->inode);
4497 trace_nfs4_read(hdr, task->tk_status);
4498 if (nfs4_async_handle_error(task, server,
4499 hdr->args.context->state,
4501 rpc_restart_call_prepare(task);
4505 __nfs4_read_done_cb(hdr);
4506 if (task->tk_status > 0)
4507 renew_lease(server, hdr->timestamp);
4511 static bool nfs4_read_stateid_changed(struct rpc_task *task,
4512 struct nfs_pgio_args *args)
4515 if (!nfs4_error_stateid_expired(task->tk_status) ||
4516 nfs4_stateid_is_current(&args->stateid,
4521 rpc_restart_call_prepare(task);
4525 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
4528 dprintk("--> %s\n", __func__);
4530 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
4532 if (nfs4_read_stateid_changed(task, &hdr->args))
4534 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4535 nfs4_read_done_cb(task, hdr);
4538 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4539 struct rpc_message *msg)
4541 hdr->timestamp = jiffies;
4542 if (!hdr->pgio_done_cb)
4543 hdr->pgio_done_cb = nfs4_read_done_cb;
4544 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
4545 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
4548 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4549 struct nfs_pgio_header *hdr)
4551 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4552 &hdr->args.seq_args,
4556 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4557 hdr->args.lock_context,
4558 hdr->rw_ops->rw_mode) == -EIO)
4560 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
4565 static int nfs4_write_done_cb(struct rpc_task *task,
4566 struct nfs_pgio_header *hdr)
4568 struct inode *inode = hdr->inode;
4570 trace_nfs4_write(hdr, task->tk_status);
4571 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4572 hdr->args.context->state,
4574 rpc_restart_call_prepare(task);
4577 if (task->tk_status >= 0) {
4578 renew_lease(NFS_SERVER(inode), hdr->timestamp);
4579 nfs_writeback_update_inode(hdr);
4584 static bool nfs4_write_stateid_changed(struct rpc_task *task,
4585 struct nfs_pgio_args *args)
4588 if (!nfs4_error_stateid_expired(task->tk_status) ||
4589 nfs4_stateid_is_current(&args->stateid,
4594 rpc_restart_call_prepare(task);
4598 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
4600 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
4602 if (nfs4_write_stateid_changed(task, &hdr->args))
4604 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4605 nfs4_write_done_cb(task, hdr);
4609 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
4611 /* Don't request attributes for pNFS or O_DIRECT writes */
4612 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
4614 /* Otherwise, request attributes if and only if we don't hold
4617 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
4620 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4621 struct rpc_message *msg)
4623 struct nfs_server *server = NFS_SERVER(hdr->inode);
4625 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4626 hdr->args.bitmask = NULL;
4627 hdr->res.fattr = NULL;
4629 hdr->args.bitmask = server->cache_consistency_bitmask;
4631 if (!hdr->pgio_done_cb)
4632 hdr->pgio_done_cb = nfs4_write_done_cb;
4633 hdr->res.server = server;
4634 hdr->timestamp = jiffies;
4636 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
4637 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
4640 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4642 nfs4_setup_sequence(NFS_SERVER(data->inode),
4643 &data->args.seq_args,
4648 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
4650 struct inode *inode = data->inode;
4652 trace_nfs4_commit(data, task->tk_status);
4653 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4654 NULL, NULL) == -EAGAIN) {
4655 rpc_restart_call_prepare(task);
4661 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
4663 if (!nfs4_sequence_done(task, &data->res.seq_res))
4665 return data->commit_done_cb(task, data);
4668 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
4670 struct nfs_server *server = NFS_SERVER(data->inode);
4672 if (data->commit_done_cb == NULL)
4673 data->commit_done_cb = nfs4_commit_done_cb;
4674 data->res.server = server;
4675 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
4676 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4679 struct nfs4_renewdata {
4680 struct nfs_client *client;
4681 unsigned long timestamp;
4685 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4686 * standalone procedure for queueing an asynchronous RENEW.
4688 static void nfs4_renew_release(void *calldata)
4690 struct nfs4_renewdata *data = calldata;
4691 struct nfs_client *clp = data->client;
4693 if (atomic_read(&clp->cl_count) > 1)
4694 nfs4_schedule_state_renewal(clp);
4695 nfs_put_client(clp);
4699 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
4701 struct nfs4_renewdata *data = calldata;
4702 struct nfs_client *clp = data->client;
4703 unsigned long timestamp = data->timestamp;
4705 trace_nfs4_renew_async(clp, task->tk_status);
4706 switch (task->tk_status) {
4709 case -NFS4ERR_LEASE_MOVED:
4710 nfs4_schedule_lease_moved_recovery(clp);
4713 /* Unless we're shutting down, schedule state recovery! */
4714 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4716 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
4717 nfs4_schedule_lease_recovery(clp);
4720 nfs4_schedule_path_down_recovery(clp);
4722 do_renew_lease(clp, timestamp);
4725 static const struct rpc_call_ops nfs4_renew_ops = {
4726 .rpc_call_done = nfs4_renew_done,
4727 .rpc_release = nfs4_renew_release,
4730 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
4732 struct rpc_message msg = {
4733 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4737 struct nfs4_renewdata *data;
4739 if (renew_flags == 0)
4741 if (!atomic_inc_not_zero(&clp->cl_count))
4743 data = kmalloc(sizeof(*data), GFP_NOFS);
4747 data->timestamp = jiffies;
4748 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
4749 &nfs4_renew_ops, data);
4752 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
4754 struct rpc_message msg = {
4755 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4759 unsigned long now = jiffies;
4762 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4765 do_renew_lease(clp, now);
4769 static inline int nfs4_server_supports_acls(struct nfs_server *server)
4771 return server->caps & NFS_CAP_ACLS;
4774 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4775 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
4778 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
4780 static int buf_to_pages_noslab(const void *buf, size_t buflen,
4781 struct page **pages)
4783 struct page *newpage, **spages;
4789 len = min_t(size_t, PAGE_SIZE, buflen);
4790 newpage = alloc_page(GFP_KERNEL);
4792 if (newpage == NULL)
4794 memcpy(page_address(newpage), buf, len);
4799 } while (buflen != 0);
4805 __free_page(spages[rc-1]);
4809 struct nfs4_cached_acl {
4815 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
4817 struct nfs_inode *nfsi = NFS_I(inode);
4819 spin_lock(&inode->i_lock);
4820 kfree(nfsi->nfs4_acl);
4821 nfsi->nfs4_acl = acl;
4822 spin_unlock(&inode->i_lock);
4825 static void nfs4_zap_acl_attr(struct inode *inode)
4827 nfs4_set_cached_acl(inode, NULL);
4830 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4832 struct nfs_inode *nfsi = NFS_I(inode);
4833 struct nfs4_cached_acl *acl;
4836 spin_lock(&inode->i_lock);
4837 acl = nfsi->nfs4_acl;
4840 if (buf == NULL) /* user is just asking for length */
4842 if (acl->cached == 0)
4844 ret = -ERANGE; /* see getxattr(2) man page */
4845 if (acl->len > buflen)
4847 memcpy(buf, acl->data, acl->len);
4851 spin_unlock(&inode->i_lock);
4855 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
4857 struct nfs4_cached_acl *acl;
4858 size_t buflen = sizeof(*acl) + acl_len;
4860 if (buflen <= PAGE_SIZE) {
4861 acl = kmalloc(buflen, GFP_KERNEL);
4865 _copy_from_pages(acl->data, pages, pgbase, acl_len);
4867 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4874 nfs4_set_cached_acl(inode, acl);
4878 * The getxattr API returns the required buffer length when called with a
4879 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4880 * the required buf. On a NULL buf, we send a page of data to the server
4881 * guessing that the ACL request can be serviced by a page. If so, we cache
4882 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4883 * the cache. If not so, we throw away the page, and cache the required
4884 * length. The next getxattr call will then produce another round trip to
4885 * the server, this time with the input buf of the required size.
4887 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4889 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
4890 struct nfs_getaclargs args = {
4891 .fh = NFS_FH(inode),
4895 struct nfs_getaclres res = {
4898 struct rpc_message msg = {
4899 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4903 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4904 int ret = -ENOMEM, i;
4906 /* As long as we're doing a round trip to the server anyway,
4907 * let's be prepared for a page of acl data. */
4910 if (npages > ARRAY_SIZE(pages))
4913 for (i = 0; i < npages; i++) {
4914 pages[i] = alloc_page(GFP_KERNEL);
4919 /* for decoding across pages */
4920 res.acl_scratch = alloc_page(GFP_KERNEL);
4921 if (!res.acl_scratch)
4924 args.acl_len = npages * PAGE_SIZE;
4926 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
4927 __func__, buf, buflen, npages, args.acl_len);
4928 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4929 &msg, &args.seq_args, &res.seq_res, 0);
4933 /* Handle the case where the passed-in buffer is too short */
4934 if (res.acl_flags & NFS4_ACL_TRUNC) {
4935 /* Did the user only issue a request for the acl length? */
4941 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
4943 if (res.acl_len > buflen) {
4947 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
4952 for (i = 0; i < npages; i++)
4954 __free_page(pages[i]);
4955 if (res.acl_scratch)
4956 __free_page(res.acl_scratch);
4960 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4962 struct nfs4_exception exception = { };
4965 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
4966 trace_nfs4_get_acl(inode, ret);
4969 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4970 } while (exception.retry);
4974 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4976 struct nfs_server *server = NFS_SERVER(inode);
4979 if (!nfs4_server_supports_acls(server))
4981 ret = nfs_revalidate_inode(server, inode);
4984 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4985 nfs_zap_acl_cache(inode);
4986 ret = nfs4_read_cached_acl(inode, buf, buflen);
4988 /* -ENOENT is returned if there is no ACL or if there is an ACL
4989 * but no cached acl data, just the acl length */
4991 return nfs4_get_acl_uncached(inode, buf, buflen);
4994 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4996 struct nfs_server *server = NFS_SERVER(inode);
4997 struct page *pages[NFS4ACL_MAXPAGES];
4998 struct nfs_setaclargs arg = {
4999 .fh = NFS_FH(inode),
5003 struct nfs_setaclres res;
5004 struct rpc_message msg = {
5005 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5009 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
5012 if (!nfs4_server_supports_acls(server))
5014 if (npages > ARRAY_SIZE(pages))
5016 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
5019 nfs4_inode_return_delegation(inode);
5020 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5023 * Free each page after tx, so the only ref left is
5024 * held by the network stack
5027 put_page(pages[i-1]);
5030 * Acl update can result in inode attribute update.
5031 * so mark the attribute cache invalid.
5033 spin_lock(&inode->i_lock);
5034 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5035 spin_unlock(&inode->i_lock);
5036 nfs_access_zap_cache(inode);
5037 nfs_zap_acl_cache(inode);
5041 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5043 struct nfs4_exception exception = { };
5046 err = __nfs4_proc_set_acl(inode, buf, buflen);
5047 trace_nfs4_set_acl(inode, err);
5048 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5050 } while (exception.retry);
5054 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
5055 static int _nfs4_get_security_label(struct inode *inode, void *buf,
5058 struct nfs_server *server = NFS_SERVER(inode);
5059 struct nfs_fattr fattr;
5060 struct nfs4_label label = {0, 0, buflen, buf};
5062 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5063 struct nfs4_getattr_arg arg = {
5064 .fh = NFS_FH(inode),
5067 struct nfs4_getattr_res res = {
5072 struct rpc_message msg = {
5073 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
5079 nfs_fattr_init(&fattr);
5081 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
5084 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5086 if (buflen < label.len)
5091 static int nfs4_get_security_label(struct inode *inode, void *buf,
5094 struct nfs4_exception exception = { };
5097 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5101 err = _nfs4_get_security_label(inode, buf, buflen);
5102 trace_nfs4_get_security_label(inode, err);
5103 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5105 } while (exception.retry);
5109 static int _nfs4_do_set_security_label(struct inode *inode,
5110 struct nfs4_label *ilabel,
5111 struct nfs_fattr *fattr,
5112 struct nfs4_label *olabel)
5115 struct iattr sattr = {0};
5116 struct nfs_server *server = NFS_SERVER(inode);
5117 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5118 struct nfs_setattrargs arg = {
5119 .fh = NFS_FH(inode),
5125 struct nfs_setattrres res = {
5130 struct rpc_message msg = {
5131 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5137 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
5139 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5141 dprintk("%s failed: %d\n", __func__, status);
5146 static int nfs4_do_set_security_label(struct inode *inode,
5147 struct nfs4_label *ilabel,
5148 struct nfs_fattr *fattr,
5149 struct nfs4_label *olabel)
5151 struct nfs4_exception exception = { };
5155 err = _nfs4_do_set_security_label(inode, ilabel,
5157 trace_nfs4_set_security_label(inode, err);
5158 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5160 } while (exception.retry);
5165 nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
5167 struct nfs4_label ilabel, *olabel = NULL;
5168 struct nfs_fattr fattr;
5169 struct rpc_cred *cred;
5172 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5175 nfs_fattr_init(&fattr);
5179 ilabel.label = (char *)buf;
5180 ilabel.len = buflen;
5182 cred = rpc_lookup_cred();
5184 return PTR_ERR(cred);
5186 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5187 if (IS_ERR(olabel)) {
5188 status = -PTR_ERR(olabel);
5192 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5194 nfs_setsecurity(inode, &fattr, olabel);
5196 nfs4_label_free(olabel);
5201 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5204 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5205 nfs4_verifier *bootverf)
5209 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5210 /* An impossible timestamp guarantees this value
5211 * will never match a generated boot time. */
5213 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
5215 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
5216 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
5217 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
5219 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5223 nfs4_init_nonuniform_client_string(struct nfs_client *clp)
5228 if (clp->cl_owner_id != NULL)
5232 len = 14 + strlen(clp->cl_ipaddr) + 1 +
5233 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5235 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5239 if (len > NFS4_OPAQUE_LIMIT + 1)
5243 * Since this string is allocated at mount time, and held until the
5244 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5245 * about a memory-reclaim deadlock.
5247 str = kmalloc(len, GFP_KERNEL);
5252 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
5254 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5255 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
5258 clp->cl_owner_id = str;
5263 nfs4_init_uniquifier_client_string(struct nfs_client *clp)
5268 len = 10 + 10 + 1 + 10 + 1 +
5269 strlen(nfs4_client_id_uniquifier) + 1 +
5270 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5272 if (len > NFS4_OPAQUE_LIMIT + 1)
5276 * Since this string is allocated at mount time, and held until the
5277 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5278 * about a memory-reclaim deadlock.
5280 str = kmalloc(len, GFP_KERNEL);
5284 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
5285 clp->rpc_ops->version, clp->cl_minorversion,
5286 nfs4_client_id_uniquifier,
5287 clp->cl_rpcclient->cl_nodename);
5288 clp->cl_owner_id = str;
5293 nfs4_init_uniform_client_string(struct nfs_client *clp)
5298 if (clp->cl_owner_id != NULL)
5301 if (nfs4_client_id_uniquifier[0] != '\0')
5302 return nfs4_init_uniquifier_client_string(clp);
5304 len = 10 + 10 + 1 + 10 + 1 +
5305 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5307 if (len > NFS4_OPAQUE_LIMIT + 1)
5311 * Since this string is allocated at mount time, and held until the
5312 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5313 * about a memory-reclaim deadlock.
5315 str = kmalloc(len, GFP_KERNEL);
5319 scnprintf(str, len, "Linux NFSv%u.%u %s",
5320 clp->rpc_ops->version, clp->cl_minorversion,
5321 clp->cl_rpcclient->cl_nodename);
5322 clp->cl_owner_id = str;
5327 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5328 * services. Advertise one based on the address family of the
5332 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5334 if (strchr(clp->cl_ipaddr, ':') != NULL)
5335 return scnprintf(buf, len, "tcp6");
5337 return scnprintf(buf, len, "tcp");
5340 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5342 struct nfs4_setclientid *sc = calldata;
5344 if (task->tk_status == 0)
5345 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5348 static const struct rpc_call_ops nfs4_setclientid_ops = {
5349 .rpc_call_done = nfs4_setclientid_done,
5353 * nfs4_proc_setclientid - Negotiate client ID
5354 * @clp: state data structure
5355 * @program: RPC program for NFSv4 callback service
5356 * @port: IP port number for NFS4 callback service
5357 * @cred: RPC credential to use for this call
5358 * @res: where to place the result
5360 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5362 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5363 unsigned short port, struct rpc_cred *cred,
5364 struct nfs4_setclientid_res *res)
5366 nfs4_verifier sc_verifier;
5367 struct nfs4_setclientid setclientid = {
5368 .sc_verifier = &sc_verifier,
5372 struct rpc_message msg = {
5373 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5374 .rpc_argp = &setclientid,
5378 struct rpc_task *task;
5379 struct rpc_task_setup task_setup_data = {
5380 .rpc_client = clp->cl_rpcclient,
5381 .rpc_message = &msg,
5382 .callback_ops = &nfs4_setclientid_ops,
5383 .callback_data = &setclientid,
5384 .flags = RPC_TASK_TIMEOUT,
5388 /* nfs_client_id4 */
5389 nfs4_init_boot_verifier(clp, &sc_verifier);
5391 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5392 status = nfs4_init_uniform_client_string(clp);
5394 status = nfs4_init_nonuniform_client_string(clp);
5400 setclientid.sc_netid_len =
5401 nfs4_init_callback_netid(clp,
5402 setclientid.sc_netid,
5403 sizeof(setclientid.sc_netid));
5404 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
5405 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
5406 clp->cl_ipaddr, port >> 8, port & 255);
5408 dprintk("NFS call setclientid auth=%s, '%s'\n",
5409 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5411 task = rpc_run_task(&task_setup_data);
5413 status = PTR_ERR(task);
5416 status = task->tk_status;
5417 if (setclientid.sc_cred) {
5418 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5419 put_rpccred(setclientid.sc_cred);
5423 trace_nfs4_setclientid(clp, status);
5424 dprintk("NFS reply setclientid: %d\n", status);
5429 * nfs4_proc_setclientid_confirm - Confirm client ID
5430 * @clp: state data structure
5431 * @res: result of a previous SETCLIENTID
5432 * @cred: RPC credential to use for this call
5434 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5436 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
5437 struct nfs4_setclientid_res *arg,
5438 struct rpc_cred *cred)
5440 struct rpc_message msg = {
5441 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
5447 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5448 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5450 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5451 trace_nfs4_setclientid_confirm(clp, status);
5452 dprintk("NFS reply setclientid_confirm: %d\n", status);
5456 struct nfs4_delegreturndata {
5457 struct nfs4_delegreturnargs args;
5458 struct nfs4_delegreturnres res;
5460 nfs4_stateid stateid;
5461 unsigned long timestamp;
5462 struct nfs_fattr fattr;
5464 struct inode *inode;
5469 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5471 struct nfs4_delegreturndata *data = calldata;
5473 if (!nfs4_sequence_done(task, &data->res.seq_res))
5476 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
5477 switch (task->tk_status) {
5479 renew_lease(data->res.server, data->timestamp);
5480 case -NFS4ERR_ADMIN_REVOKED:
5481 case -NFS4ERR_DELEG_REVOKED:
5482 case -NFS4ERR_BAD_STATEID:
5483 case -NFS4ERR_OLD_STATEID:
5484 case -NFS4ERR_STALE_STATEID:
5485 case -NFS4ERR_EXPIRED:
5486 task->tk_status = 0;
5488 pnfs_roc_set_barrier(data->inode, data->roc_barrier);
5491 if (nfs4_async_handle_error(task, data->res.server,
5492 NULL, NULL) == -EAGAIN) {
5493 rpc_restart_call_prepare(task);
5497 data->rpc_status = task->tk_status;
5500 static void nfs4_delegreturn_release(void *calldata)
5502 struct nfs4_delegreturndata *data = calldata;
5503 struct inode *inode = data->inode;
5507 pnfs_roc_release(inode);
5508 nfs_iput_and_deactive(inode);
5513 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5515 struct nfs4_delegreturndata *d_data;
5517 d_data = (struct nfs4_delegreturndata *)data;
5519 if (nfs4_wait_on_layoutreturn(d_data->inode, task))
5523 pnfs_roc_get_barrier(d_data->inode, &d_data->roc_barrier);
5525 nfs4_setup_sequence(d_data->res.server,
5526 &d_data->args.seq_args,
5527 &d_data->res.seq_res,
5531 static const struct rpc_call_ops nfs4_delegreturn_ops = {
5532 .rpc_call_prepare = nfs4_delegreturn_prepare,
5533 .rpc_call_done = nfs4_delegreturn_done,
5534 .rpc_release = nfs4_delegreturn_release,
5537 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
5539 struct nfs4_delegreturndata *data;
5540 struct nfs_server *server = NFS_SERVER(inode);
5541 struct rpc_task *task;
5542 struct rpc_message msg = {
5543 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5546 struct rpc_task_setup task_setup_data = {
5547 .rpc_client = server->client,
5548 .rpc_message = &msg,
5549 .callback_ops = &nfs4_delegreturn_ops,
5550 .flags = RPC_TASK_ASYNC,
5554 data = kzalloc(sizeof(*data), GFP_NOFS);
5557 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
5559 nfs4_state_protect(server->nfs_client,
5560 NFS_SP4_MACH_CRED_CLEANUP,
5561 &task_setup_data.rpc_client, &msg);
5563 data->args.fhandle = &data->fh;
5564 data->args.stateid = &data->stateid;
5565 data->args.bitmask = server->cache_consistency_bitmask;
5566 nfs_copy_fh(&data->fh, NFS_FH(inode));
5567 nfs4_stateid_copy(&data->stateid, stateid);
5568 data->res.fattr = &data->fattr;
5569 data->res.server = server;
5570 nfs_fattr_init(data->res.fattr);
5571 data->timestamp = jiffies;
5572 data->rpc_status = 0;
5573 data->inode = nfs_igrab_and_active(inode);
5575 data->roc = nfs4_roc(inode);
5577 task_setup_data.callback_data = data;
5578 msg.rpc_argp = &data->args;
5579 msg.rpc_resp = &data->res;
5580 task = rpc_run_task(&task_setup_data);
5582 return PTR_ERR(task);
5585 status = nfs4_wait_for_completion_rpc_task(task);
5588 status = data->rpc_status;
5590 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5592 nfs_refresh_inode(inode, &data->fattr);
5598 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
5600 struct nfs_server *server = NFS_SERVER(inode);
5601 struct nfs4_exception exception = { };
5604 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
5605 trace_nfs4_delegreturn(inode, stateid, err);
5607 case -NFS4ERR_STALE_STATEID:
5608 case -NFS4ERR_EXPIRED:
5612 err = nfs4_handle_exception(server, err, &exception);
5613 } while (exception.retry);
5617 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5619 struct inode *inode = state->inode;
5620 struct nfs_server *server = NFS_SERVER(inode);
5621 struct nfs_client *clp = server->nfs_client;
5622 struct nfs_lockt_args arg = {
5623 .fh = NFS_FH(inode),
5626 struct nfs_lockt_res res = {
5629 struct rpc_message msg = {
5630 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5633 .rpc_cred = state->owner->so_cred,
5635 struct nfs4_lock_state *lsp;
5638 arg.lock_owner.clientid = clp->cl_clientid;
5639 status = nfs4_set_lock_state(state, request);
5642 lsp = request->fl_u.nfs4_fl.owner;
5643 arg.lock_owner.id = lsp->ls_seqid.owner_id;
5644 arg.lock_owner.s_dev = server->s_dev;
5645 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5648 request->fl_type = F_UNLCK;
5650 case -NFS4ERR_DENIED:
5653 request->fl_ops->fl_release_private(request);
5654 request->fl_ops = NULL;
5659 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5661 struct nfs4_exception exception = { };
5665 err = _nfs4_proc_getlk(state, cmd, request);
5666 trace_nfs4_get_lock(request, state, cmd, err);
5667 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
5669 } while (exception.retry);
5673 struct nfs4_unlockdata {
5674 struct nfs_locku_args arg;
5675 struct nfs_locku_res res;
5676 struct nfs4_lock_state *lsp;
5677 struct nfs_open_context *ctx;
5678 struct file_lock fl;
5679 struct nfs_server *server;
5680 unsigned long timestamp;
5683 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5684 struct nfs_open_context *ctx,
5685 struct nfs4_lock_state *lsp,
5686 struct nfs_seqid *seqid)
5688 struct nfs4_unlockdata *p;
5689 struct inode *inode = lsp->ls_state->inode;
5691 p = kzalloc(sizeof(*p), GFP_NOFS);
5694 p->arg.fh = NFS_FH(inode);
5696 p->arg.seqid = seqid;
5697 p->res.seqid = seqid;
5699 atomic_inc(&lsp->ls_count);
5700 /* Ensure we don't close file until we're done freeing locks! */
5701 p->ctx = get_nfs_open_context(ctx);
5702 memcpy(&p->fl, fl, sizeof(p->fl));
5703 p->server = NFS_SERVER(inode);
5707 static void nfs4_locku_release_calldata(void *data)
5709 struct nfs4_unlockdata *calldata = data;
5710 nfs_free_seqid(calldata->arg.seqid);
5711 nfs4_put_lock_state(calldata->lsp);
5712 put_nfs_open_context(calldata->ctx);
5716 static void nfs4_locku_done(struct rpc_task *task, void *data)
5718 struct nfs4_unlockdata *calldata = data;
5720 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5722 switch (task->tk_status) {
5724 renew_lease(calldata->server, calldata->timestamp);
5725 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
5726 if (nfs4_update_lock_stateid(calldata->lsp,
5727 &calldata->res.stateid))
5729 case -NFS4ERR_BAD_STATEID:
5730 case -NFS4ERR_OLD_STATEID:
5731 case -NFS4ERR_STALE_STATEID:
5732 case -NFS4ERR_EXPIRED:
5733 if (!nfs4_stateid_match(&calldata->arg.stateid,
5734 &calldata->lsp->ls_stateid))
5735 rpc_restart_call_prepare(task);
5738 if (nfs4_async_handle_error(task, calldata->server,
5739 NULL, NULL) == -EAGAIN)
5740 rpc_restart_call_prepare(task);
5742 nfs_release_seqid(calldata->arg.seqid);
5745 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
5747 struct nfs4_unlockdata *calldata = data;
5749 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
5751 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
5752 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
5753 /* Note: exit _without_ running nfs4_locku_done */
5756 calldata->timestamp = jiffies;
5757 if (nfs4_setup_sequence(calldata->server,
5758 &calldata->arg.seq_args,
5759 &calldata->res.seq_res,
5761 nfs_release_seqid(calldata->arg.seqid);
5764 task->tk_action = NULL;
5766 nfs4_sequence_done(task, &calldata->res.seq_res);
5769 static const struct rpc_call_ops nfs4_locku_ops = {
5770 .rpc_call_prepare = nfs4_locku_prepare,
5771 .rpc_call_done = nfs4_locku_done,
5772 .rpc_release = nfs4_locku_release_calldata,
5775 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5776 struct nfs_open_context *ctx,
5777 struct nfs4_lock_state *lsp,
5778 struct nfs_seqid *seqid)
5780 struct nfs4_unlockdata *data;
5781 struct rpc_message msg = {
5782 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5783 .rpc_cred = ctx->cred,
5785 struct rpc_task_setup task_setup_data = {
5786 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
5787 .rpc_message = &msg,
5788 .callback_ops = &nfs4_locku_ops,
5789 .workqueue = nfsiod_workqueue,
5790 .flags = RPC_TASK_ASYNC,
5793 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5794 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5796 /* Ensure this is an unlock - when canceling a lock, the
5797 * canceled lock is passed in, and it won't be an unlock.
5799 fl->fl_type = F_UNLCK;
5801 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5803 nfs_free_seqid(seqid);
5804 return ERR_PTR(-ENOMEM);
5807 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
5808 msg.rpc_argp = &data->arg;
5809 msg.rpc_resp = &data->res;
5810 task_setup_data.callback_data = data;
5811 return rpc_run_task(&task_setup_data);
5814 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5816 struct inode *inode = state->inode;
5817 struct nfs4_state_owner *sp = state->owner;
5818 struct nfs_inode *nfsi = NFS_I(inode);
5819 struct nfs_seqid *seqid;
5820 struct nfs4_lock_state *lsp;
5821 struct rpc_task *task;
5822 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
5824 unsigned char fl_flags = request->fl_flags;
5826 status = nfs4_set_lock_state(state, request);
5827 /* Unlock _before_ we do the RPC call */
5828 request->fl_flags |= FL_EXISTS;
5829 /* Exclude nfs_delegation_claim_locks() */
5830 mutex_lock(&sp->so_delegreturn_mutex);
5831 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
5832 down_read(&nfsi->rwsem);
5833 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
5834 up_read(&nfsi->rwsem);
5835 mutex_unlock(&sp->so_delegreturn_mutex);
5838 up_read(&nfsi->rwsem);
5839 mutex_unlock(&sp->so_delegreturn_mutex);
5842 /* Is this a delegated lock? */
5843 lsp = request->fl_u.nfs4_fl.owner;
5844 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5846 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
5847 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
5851 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
5852 status = PTR_ERR(task);
5855 status = nfs4_wait_for_completion_rpc_task(task);
5858 request->fl_flags = fl_flags;
5859 trace_nfs4_unlock(request, state, F_SETLK, status);
5863 struct nfs4_lockdata {
5864 struct nfs_lock_args arg;
5865 struct nfs_lock_res res;
5866 struct nfs4_lock_state *lsp;
5867 struct nfs_open_context *ctx;
5868 struct file_lock fl;
5869 unsigned long timestamp;
5872 struct nfs_server *server;
5875 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
5876 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5879 struct nfs4_lockdata *p;
5880 struct inode *inode = lsp->ls_state->inode;
5881 struct nfs_server *server = NFS_SERVER(inode);
5882 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
5884 p = kzalloc(sizeof(*p), gfp_mask);
5888 p->arg.fh = NFS_FH(inode);
5890 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
5891 if (IS_ERR(p->arg.open_seqid))
5893 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
5894 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
5895 if (IS_ERR(p->arg.lock_seqid))
5896 goto out_free_seqid;
5897 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
5898 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
5899 p->arg.lock_owner.s_dev = server->s_dev;
5900 p->res.lock_seqid = p->arg.lock_seqid;
5903 atomic_inc(&lsp->ls_count);
5904 p->ctx = get_nfs_open_context(ctx);
5905 get_file(fl->fl_file);
5906 memcpy(&p->fl, fl, sizeof(p->fl));
5909 nfs_free_seqid(p->arg.open_seqid);
5915 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5917 struct nfs4_lockdata *data = calldata;
5918 struct nfs4_state *state = data->lsp->ls_state;
5920 dprintk("%s: begin!\n", __func__);
5921 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
5923 /* Do we need to do an open_to_lock_owner? */
5924 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
5925 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
5926 goto out_release_lock_seqid;
5928 nfs4_stateid_copy(&data->arg.open_stateid,
5929 &state->open_stateid);
5930 data->arg.new_lock_owner = 1;
5931 data->res.open_seqid = data->arg.open_seqid;
5933 data->arg.new_lock_owner = 0;
5934 nfs4_stateid_copy(&data->arg.lock_stateid,
5935 &data->lsp->ls_stateid);
5937 if (!nfs4_valid_open_stateid(state)) {
5938 data->rpc_status = -EBADF;
5939 task->tk_action = NULL;
5940 goto out_release_open_seqid;
5942 data->timestamp = jiffies;
5943 if (nfs4_setup_sequence(data->server,
5944 &data->arg.seq_args,
5948 out_release_open_seqid:
5949 nfs_release_seqid(data->arg.open_seqid);
5950 out_release_lock_seqid:
5951 nfs_release_seqid(data->arg.lock_seqid);
5953 nfs4_sequence_done(task, &data->res.seq_res);
5954 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
5957 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5959 struct nfs4_lockdata *data = calldata;
5960 struct nfs4_lock_state *lsp = data->lsp;
5962 dprintk("%s: begin!\n", __func__);
5964 if (!nfs4_sequence_done(task, &data->res.seq_res))
5967 data->rpc_status = task->tk_status;
5968 switch (task->tk_status) {
5970 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
5972 if (data->arg.new_lock) {
5973 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
5974 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
5975 rpc_restart_call_prepare(task);
5979 if (data->arg.new_lock_owner != 0) {
5980 nfs_confirm_seqid(&lsp->ls_seqid, 0);
5981 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
5982 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
5983 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
5984 rpc_restart_call_prepare(task);
5986 case -NFS4ERR_BAD_STATEID:
5987 case -NFS4ERR_OLD_STATEID:
5988 case -NFS4ERR_STALE_STATEID:
5989 case -NFS4ERR_EXPIRED:
5990 if (data->arg.new_lock_owner != 0) {
5991 if (!nfs4_stateid_match(&data->arg.open_stateid,
5992 &lsp->ls_state->open_stateid))
5993 rpc_restart_call_prepare(task);
5994 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
5996 rpc_restart_call_prepare(task);
5998 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
6001 static void nfs4_lock_release(void *calldata)
6003 struct nfs4_lockdata *data = calldata;
6005 dprintk("%s: begin!\n", __func__);
6006 nfs_free_seqid(data->arg.open_seqid);
6007 if (data->cancelled != 0) {
6008 struct rpc_task *task;
6009 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6010 data->arg.lock_seqid);
6012 rpc_put_task_async(task);
6013 dprintk("%s: cancelling lock!\n", __func__);
6015 nfs_free_seqid(data->arg.lock_seqid);
6016 nfs4_put_lock_state(data->lsp);
6017 put_nfs_open_context(data->ctx);
6018 fput(data->fl.fl_file);
6020 dprintk("%s: done!\n", __func__);
6023 static const struct rpc_call_ops nfs4_lock_ops = {
6024 .rpc_call_prepare = nfs4_lock_prepare,
6025 .rpc_call_done = nfs4_lock_done,
6026 .rpc_release = nfs4_lock_release,
6029 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6032 case -NFS4ERR_ADMIN_REVOKED:
6033 case -NFS4ERR_BAD_STATEID:
6034 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6035 if (new_lock_owner != 0 ||
6036 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
6037 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
6039 case -NFS4ERR_STALE_STATEID:
6040 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6041 case -NFS4ERR_EXPIRED:
6042 nfs4_schedule_lease_recovery(server->nfs_client);
6046 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
6048 struct nfs4_lockdata *data;
6049 struct rpc_task *task;
6050 struct rpc_message msg = {
6051 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6052 .rpc_cred = state->owner->so_cred,
6054 struct rpc_task_setup task_setup_data = {
6055 .rpc_client = NFS_CLIENT(state->inode),
6056 .rpc_message = &msg,
6057 .callback_ops = &nfs4_lock_ops,
6058 .workqueue = nfsiod_workqueue,
6059 .flags = RPC_TASK_ASYNC,
6063 dprintk("%s: begin!\n", __func__);
6064 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
6065 fl->fl_u.nfs4_fl.owner,
6066 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
6070 data->arg.block = 1;
6071 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
6072 msg.rpc_argp = &data->arg;
6073 msg.rpc_resp = &data->res;
6074 task_setup_data.callback_data = data;
6075 if (recovery_type > NFS_LOCK_NEW) {
6076 if (recovery_type == NFS_LOCK_RECLAIM)
6077 data->arg.reclaim = NFS_LOCK_RECLAIM;
6078 nfs4_set_sequence_privileged(&data->arg.seq_args);
6080 data->arg.new_lock = 1;
6081 task = rpc_run_task(&task_setup_data);
6083 return PTR_ERR(task);
6084 ret = nfs4_wait_for_completion_rpc_task(task);
6086 ret = data->rpc_status;
6088 nfs4_handle_setlk_error(data->server, data->lsp,
6089 data->arg.new_lock_owner, ret);
6091 data->cancelled = 1;
6093 dprintk("%s: done, ret = %d!\n", __func__, ret);
6094 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
6098 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6100 struct nfs_server *server = NFS_SERVER(state->inode);
6101 struct nfs4_exception exception = {
6102 .inode = state->inode,
6107 /* Cache the lock if possible... */
6108 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6110 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
6111 if (err != -NFS4ERR_DELAY)
6113 nfs4_handle_exception(server, err, &exception);
6114 } while (exception.retry);
6118 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6120 struct nfs_server *server = NFS_SERVER(state->inode);
6121 struct nfs4_exception exception = {
6122 .inode = state->inode,
6126 err = nfs4_set_lock_state(state, request);
6129 if (!recover_lost_locks) {
6130 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6134 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6136 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
6140 case -NFS4ERR_GRACE:
6141 case -NFS4ERR_DELAY:
6142 nfs4_handle_exception(server, err, &exception);
6145 } while (exception.retry);
6150 #if defined(CONFIG_NFS_V4_1)
6151 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6153 struct nfs4_lock_state *lsp;
6156 status = nfs4_set_lock_state(state, request);
6159 lsp = request->fl_u.nfs4_fl.owner;
6160 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6161 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6163 status = nfs4_lock_expired(state, request);
6168 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6170 struct nfs_inode *nfsi = NFS_I(state->inode);
6171 struct nfs4_state_owner *sp = state->owner;
6172 unsigned char fl_flags = request->fl_flags;
6175 request->fl_flags |= FL_ACCESS;
6176 status = locks_lock_inode_wait(state->inode, request);
6179 mutex_lock(&sp->so_delegreturn_mutex);
6180 down_read(&nfsi->rwsem);
6181 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
6182 /* Yes: cache locks! */
6183 /* ...but avoid races with delegation recall... */
6184 request->fl_flags = fl_flags & ~FL_SLEEP;
6185 status = locks_lock_inode_wait(state->inode, request);
6186 up_read(&nfsi->rwsem);
6187 mutex_unlock(&sp->so_delegreturn_mutex);
6190 up_read(&nfsi->rwsem);
6191 mutex_unlock(&sp->so_delegreturn_mutex);
6192 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
6194 request->fl_flags = fl_flags;
6198 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6200 struct nfs4_exception exception = {
6202 .inode = state->inode,
6207 err = _nfs4_proc_setlk(state, cmd, request);
6208 if (err == -NFS4ERR_DENIED)
6210 err = nfs4_handle_exception(NFS_SERVER(state->inode),
6212 } while (exception.retry);
6216 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6217 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6220 nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6221 struct file_lock *request)
6223 int status = -ERESTARTSYS;
6224 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6226 while(!signalled()) {
6227 status = nfs4_proc_setlk(state, cmd, request);
6228 if ((status != -EAGAIN) || IS_SETLK(cmd))
6230 freezable_schedule_timeout_interruptible(timeout);
6232 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6233 status = -ERESTARTSYS;
6238 #ifdef CONFIG_NFS_V4_1
6239 struct nfs4_lock_waiter {
6240 struct task_struct *task;
6241 struct inode *inode;
6242 struct nfs_lowner *owner;
6247 nfs4_wake_lock_waiter(wait_queue_t *wait, unsigned int mode, int flags, void *key)
6250 struct cb_notify_lock_args *cbnl = key;
6251 struct nfs4_lock_waiter *waiter = wait->private;
6252 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6253 *wowner = waiter->owner;
6255 /* Only wake if the callback was for the same owner */
6256 if (lowner->clientid != wowner->clientid ||
6257 lowner->id != wowner->id ||
6258 lowner->s_dev != wowner->s_dev)
6261 /* Make sure it's for the right inode */
6262 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6265 waiter->notified = true;
6267 /* override "private" so we can use default_wake_function */
6268 wait->private = waiter->task;
6269 ret = autoremove_wake_function(wait, mode, flags, key);
6270 wait->private = waiter;
6275 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6277 int status = -ERESTARTSYS;
6278 unsigned long flags;
6279 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6280 struct nfs_server *server = NFS_SERVER(state->inode);
6281 struct nfs_client *clp = server->nfs_client;
6282 wait_queue_head_t *q = &clp->cl_lock_waitq;
6283 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6284 .id = lsp->ls_seqid.owner_id,
6285 .s_dev = server->s_dev };
6286 struct nfs4_lock_waiter waiter = { .task = current,
6287 .inode = state->inode,
6289 .notified = false };
6292 /* Don't bother with waitqueue if we don't expect a callback */
6293 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6294 return nfs4_retry_setlk_simple(state, cmd, request);
6297 wait.private = &waiter;
6298 wait.func = nfs4_wake_lock_waiter;
6299 add_wait_queue(q, &wait);
6301 while(!signalled()) {
6302 status = nfs4_proc_setlk(state, cmd, request);
6303 if ((status != -EAGAIN) || IS_SETLK(cmd))
6306 status = -ERESTARTSYS;
6307 spin_lock_irqsave(&q->lock, flags);
6308 if (waiter.notified) {
6309 spin_unlock_irqrestore(&q->lock, flags);
6312 set_current_state(TASK_INTERRUPTIBLE);
6313 spin_unlock_irqrestore(&q->lock, flags);
6315 freezable_schedule_timeout_interruptible(NFS4_LOCK_MAXTIMEOUT);
6318 finish_wait(q, &wait);
6321 #else /* !CONFIG_NFS_V4_1 */
6323 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6325 return nfs4_retry_setlk_simple(state, cmd, request);
6330 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6332 struct nfs_open_context *ctx;
6333 struct nfs4_state *state;
6336 /* verify open state */
6337 ctx = nfs_file_open_context(filp);
6340 if (request->fl_start < 0 || request->fl_end < 0)
6343 if (IS_GETLK(cmd)) {
6345 return nfs4_proc_getlk(state, F_GETLK, request);
6349 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6352 if (request->fl_type == F_UNLCK) {
6354 return nfs4_proc_unlck(state, cmd, request);
6361 if ((request->fl_flags & FL_POSIX) &&
6362 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6366 * Don't rely on the VFS having checked the file open mode,
6367 * since it won't do this for flock() locks.
6369 switch (request->fl_type) {
6371 if (!(filp->f_mode & FMODE_READ))
6375 if (!(filp->f_mode & FMODE_WRITE))
6379 status = nfs4_set_lock_state(state, request);
6383 return nfs4_retry_setlk(state, cmd, request);
6386 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
6388 struct nfs_server *server = NFS_SERVER(state->inode);
6391 err = nfs4_set_lock_state(state, fl);
6394 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
6395 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
6398 struct nfs_release_lockowner_data {
6399 struct nfs4_lock_state *lsp;
6400 struct nfs_server *server;
6401 struct nfs_release_lockowner_args args;
6402 struct nfs_release_lockowner_res res;
6403 unsigned long timestamp;
6406 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6408 struct nfs_release_lockowner_data *data = calldata;
6409 struct nfs_server *server = data->server;
6410 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
6411 &data->args.seq_args, &data->res.seq_res, task);
6412 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6413 data->timestamp = jiffies;
6416 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6418 struct nfs_release_lockowner_data *data = calldata;
6419 struct nfs_server *server = data->server;
6421 nfs40_sequence_done(task, &data->res.seq_res);
6423 switch (task->tk_status) {
6425 renew_lease(server, data->timestamp);
6427 case -NFS4ERR_STALE_CLIENTID:
6428 case -NFS4ERR_EXPIRED:
6429 nfs4_schedule_lease_recovery(server->nfs_client);
6431 case -NFS4ERR_LEASE_MOVED:
6432 case -NFS4ERR_DELAY:
6433 if (nfs4_async_handle_error(task, server,
6434 NULL, NULL) == -EAGAIN)
6435 rpc_restart_call_prepare(task);
6439 static void nfs4_release_lockowner_release(void *calldata)
6441 struct nfs_release_lockowner_data *data = calldata;
6442 nfs4_free_lock_state(data->server, data->lsp);
6446 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
6447 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6448 .rpc_call_done = nfs4_release_lockowner_done,
6449 .rpc_release = nfs4_release_lockowner_release,
6453 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
6455 struct nfs_release_lockowner_data *data;
6456 struct rpc_message msg = {
6457 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6460 if (server->nfs_client->cl_mvops->minor_version != 0)
6463 data = kmalloc(sizeof(*data), GFP_NOFS);
6467 data->server = server;
6468 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6469 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6470 data->args.lock_owner.s_dev = server->s_dev;
6472 msg.rpc_argp = &data->args;
6473 msg.rpc_resp = &data->res;
6474 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
6475 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
6478 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6480 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
6481 struct dentry *unused, struct inode *inode,
6482 const char *key, const void *buf,
6483 size_t buflen, int flags)
6485 return nfs4_proc_set_acl(inode, buf, buflen);
6488 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
6489 struct dentry *unused, struct inode *inode,
6490 const char *key, void *buf, size_t buflen)
6492 return nfs4_proc_get_acl(inode, buf, buflen);
6495 static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
6497 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
6500 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
6502 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
6503 struct dentry *unused, struct inode *inode,
6504 const char *key, const void *buf,
6505 size_t buflen, int flags)
6507 if (security_ismaclabel(key))
6508 return nfs4_set_security_label(inode, buf, buflen);
6513 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
6514 struct dentry *unused, struct inode *inode,
6515 const char *key, void *buf, size_t buflen)
6517 if (security_ismaclabel(key))
6518 return nfs4_get_security_label(inode, buf, buflen);
6523 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6527 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6528 len = security_inode_listsecurity(inode, list, list_len);
6529 if (list_len && len > list_len)
6535 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6536 .prefix = XATTR_SECURITY_PREFIX,
6537 .get = nfs4_xattr_get_nfs4_label,
6538 .set = nfs4_xattr_set_nfs4_label,
6544 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6552 * nfs_fhget will use either the mounted_on_fileid or the fileid
6554 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6556 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6557 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6558 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
6559 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
6562 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
6563 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
6564 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6568 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6569 const struct qstr *name,
6570 struct nfs4_fs_locations *fs_locations,
6573 struct nfs_server *server = NFS_SERVER(dir);
6575 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6577 struct nfs4_fs_locations_arg args = {
6578 .dir_fh = NFS_FH(dir),
6583 struct nfs4_fs_locations_res res = {
6584 .fs_locations = fs_locations,
6586 struct rpc_message msg = {
6587 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6593 dprintk("%s: start\n", __func__);
6595 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6596 * is not supported */
6597 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6598 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6600 bitmask[0] |= FATTR4_WORD0_FILEID;
6602 nfs_fattr_init(&fs_locations->fattr);
6603 fs_locations->server = server;
6604 fs_locations->nlocations = 0;
6605 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
6606 dprintk("%s: returned status = %d\n", __func__, status);
6610 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6611 const struct qstr *name,
6612 struct nfs4_fs_locations *fs_locations,
6615 struct nfs4_exception exception = { };
6618 err = _nfs4_proc_fs_locations(client, dir, name,
6619 fs_locations, page);
6620 trace_nfs4_get_fs_locations(dir, name, err);
6621 err = nfs4_handle_exception(NFS_SERVER(dir), err,
6623 } while (exception.retry);
6628 * This operation also signals the server that this client is
6629 * performing migration recovery. The server can stop returning
6630 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6631 * appended to this compound to identify the client ID which is
6632 * performing recovery.
6634 static int _nfs40_proc_get_locations(struct inode *inode,
6635 struct nfs4_fs_locations *locations,
6636 struct page *page, struct rpc_cred *cred)
6638 struct nfs_server *server = NFS_SERVER(inode);
6639 struct rpc_clnt *clnt = server->client;
6641 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6643 struct nfs4_fs_locations_arg args = {
6644 .clientid = server->nfs_client->cl_clientid,
6645 .fh = NFS_FH(inode),
6648 .migration = 1, /* skip LOOKUP */
6649 .renew = 1, /* append RENEW */
6651 struct nfs4_fs_locations_res res = {
6652 .fs_locations = locations,
6656 struct rpc_message msg = {
6657 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6662 unsigned long now = jiffies;
6665 nfs_fattr_init(&locations->fattr);
6666 locations->server = server;
6667 locations->nlocations = 0;
6669 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6670 nfs4_set_sequence_privileged(&args.seq_args);
6671 status = nfs4_call_sync_sequence(clnt, server, &msg,
6672 &args.seq_args, &res.seq_res);
6676 renew_lease(server, now);
6680 #ifdef CONFIG_NFS_V4_1
6683 * This operation also signals the server that this client is
6684 * performing migration recovery. The server can stop asserting
6685 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6686 * performing this operation is identified in the SEQUENCE
6687 * operation in this compound.
6689 * When the client supports GETATTR(fs_locations_info), it can
6690 * be plumbed in here.
6692 static int _nfs41_proc_get_locations(struct inode *inode,
6693 struct nfs4_fs_locations *locations,
6694 struct page *page, struct rpc_cred *cred)
6696 struct nfs_server *server = NFS_SERVER(inode);
6697 struct rpc_clnt *clnt = server->client;
6699 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6701 struct nfs4_fs_locations_arg args = {
6702 .fh = NFS_FH(inode),
6705 .migration = 1, /* skip LOOKUP */
6707 struct nfs4_fs_locations_res res = {
6708 .fs_locations = locations,
6711 struct rpc_message msg = {
6712 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6719 nfs_fattr_init(&locations->fattr);
6720 locations->server = server;
6721 locations->nlocations = 0;
6723 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6724 nfs4_set_sequence_privileged(&args.seq_args);
6725 status = nfs4_call_sync_sequence(clnt, server, &msg,
6726 &args.seq_args, &res.seq_res);
6727 if (status == NFS4_OK &&
6728 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6729 status = -NFS4ERR_LEASE_MOVED;
6733 #endif /* CONFIG_NFS_V4_1 */
6736 * nfs4_proc_get_locations - discover locations for a migrated FSID
6737 * @inode: inode on FSID that is migrating
6738 * @locations: result of query
6740 * @cred: credential to use for this operation
6742 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6743 * operation failed, or a negative errno if a local error occurred.
6745 * On success, "locations" is filled in, but if the server has
6746 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6749 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6750 * from this client that require migration recovery.
6752 int nfs4_proc_get_locations(struct inode *inode,
6753 struct nfs4_fs_locations *locations,
6754 struct page *page, struct rpc_cred *cred)
6756 struct nfs_server *server = NFS_SERVER(inode);
6757 struct nfs_client *clp = server->nfs_client;
6758 const struct nfs4_mig_recovery_ops *ops =
6759 clp->cl_mvops->mig_recovery_ops;
6760 struct nfs4_exception exception = { };
6763 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6764 (unsigned long long)server->fsid.major,
6765 (unsigned long long)server->fsid.minor,
6767 nfs_display_fhandle(NFS_FH(inode), __func__);
6770 status = ops->get_locations(inode, locations, page, cred);
6771 if (status != -NFS4ERR_DELAY)
6773 nfs4_handle_exception(server, status, &exception);
6774 } while (exception.retry);
6779 * This operation also signals the server that this client is
6780 * performing "lease moved" recovery. The server can stop
6781 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6782 * is appended to this compound to identify the client ID which is
6783 * performing recovery.
6785 static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6787 struct nfs_server *server = NFS_SERVER(inode);
6788 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6789 struct rpc_clnt *clnt = server->client;
6790 struct nfs4_fsid_present_arg args = {
6791 .fh = NFS_FH(inode),
6792 .clientid = clp->cl_clientid,
6793 .renew = 1, /* append RENEW */
6795 struct nfs4_fsid_present_res res = {
6798 struct rpc_message msg = {
6799 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6804 unsigned long now = jiffies;
6807 res.fh = nfs_alloc_fhandle();
6811 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6812 nfs4_set_sequence_privileged(&args.seq_args);
6813 status = nfs4_call_sync_sequence(clnt, server, &msg,
6814 &args.seq_args, &res.seq_res);
6815 nfs_free_fhandle(res.fh);
6819 do_renew_lease(clp, now);
6823 #ifdef CONFIG_NFS_V4_1
6826 * This operation also signals the server that this client is
6827 * performing "lease moved" recovery. The server can stop asserting
6828 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6829 * this operation is identified in the SEQUENCE operation in this
6832 static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6834 struct nfs_server *server = NFS_SERVER(inode);
6835 struct rpc_clnt *clnt = server->client;
6836 struct nfs4_fsid_present_arg args = {
6837 .fh = NFS_FH(inode),
6839 struct nfs4_fsid_present_res res = {
6841 struct rpc_message msg = {
6842 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6849 res.fh = nfs_alloc_fhandle();
6853 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6854 nfs4_set_sequence_privileged(&args.seq_args);
6855 status = nfs4_call_sync_sequence(clnt, server, &msg,
6856 &args.seq_args, &res.seq_res);
6857 nfs_free_fhandle(res.fh);
6858 if (status == NFS4_OK &&
6859 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6860 status = -NFS4ERR_LEASE_MOVED;
6864 #endif /* CONFIG_NFS_V4_1 */
6867 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6868 * @inode: inode on FSID to check
6869 * @cred: credential to use for this operation
6871 * Server indicates whether the FSID is present, moved, or not
6872 * recognized. This operation is necessary to clear a LEASE_MOVED
6873 * condition for this client ID.
6875 * Returns NFS4_OK if the FSID is present on this server,
6876 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6877 * NFS4ERR code if some error occurred on the server, or a
6878 * negative errno if a local failure occurred.
6880 int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6882 struct nfs_server *server = NFS_SERVER(inode);
6883 struct nfs_client *clp = server->nfs_client;
6884 const struct nfs4_mig_recovery_ops *ops =
6885 clp->cl_mvops->mig_recovery_ops;
6886 struct nfs4_exception exception = { };
6889 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6890 (unsigned long long)server->fsid.major,
6891 (unsigned long long)server->fsid.minor,
6893 nfs_display_fhandle(NFS_FH(inode), __func__);
6896 status = ops->fsid_present(inode, cred);
6897 if (status != -NFS4ERR_DELAY)
6899 nfs4_handle_exception(server, status, &exception);
6900 } while (exception.retry);
6905 * If 'use_integrity' is true and the state managment nfs_client
6906 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
6907 * and the machine credential as per RFC3530bis and RFC5661 Security
6908 * Considerations sections. Otherwise, just use the user cred with the
6909 * filesystem's rpc_client.
6911 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
6914 struct nfs4_secinfo_arg args = {
6915 .dir_fh = NFS_FH(dir),
6918 struct nfs4_secinfo_res res = {
6921 struct rpc_message msg = {
6922 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
6926 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
6927 struct rpc_cred *cred = NULL;
6929 if (use_integrity) {
6930 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
6931 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
6932 msg.rpc_cred = cred;
6935 dprintk("NFS call secinfo %s\n", name->name);
6937 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
6938 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
6940 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
6942 dprintk("NFS reply secinfo: %d\n", status);
6950 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
6951 struct nfs4_secinfo_flavors *flavors)
6953 struct nfs4_exception exception = { };
6956 err = -NFS4ERR_WRONGSEC;
6958 /* try to use integrity protection with machine cred */
6959 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
6960 err = _nfs4_proc_secinfo(dir, name, flavors, true);
6963 * if unable to use integrity protection, or SECINFO with
6964 * integrity protection returns NFS4ERR_WRONGSEC (which is
6965 * disallowed by spec, but exists in deployed servers) use
6966 * the current filesystem's rpc_client and the user cred.
6968 if (err == -NFS4ERR_WRONGSEC)
6969 err = _nfs4_proc_secinfo(dir, name, flavors, false);
6971 trace_nfs4_secinfo(dir, name, err);
6972 err = nfs4_handle_exception(NFS_SERVER(dir), err,
6974 } while (exception.retry);
6978 #ifdef CONFIG_NFS_V4_1
6980 * Check the exchange flags returned by the server for invalid flags, having
6981 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
6984 static int nfs4_check_cl_exchange_flags(u32 flags)
6986 if (flags & ~EXCHGID4_FLAG_MASK_R)
6988 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
6989 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
6991 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
6995 return -NFS4ERR_INVAL;
6999 nfs41_same_server_scope(struct nfs41_server_scope *a,
7000 struct nfs41_server_scope *b)
7002 if (a->server_scope_sz == b->server_scope_sz &&
7003 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
7010 nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7014 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7015 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7019 * nfs4_proc_bind_one_conn_to_session()
7021 * The 4.1 client currently uses the same TCP connection for the
7022 * fore and backchannel.
7025 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7026 struct rpc_xprt *xprt,
7027 struct nfs_client *clp,
7028 struct rpc_cred *cred)
7031 struct nfs41_bind_conn_to_session_args args = {
7033 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7035 struct nfs41_bind_conn_to_session_res res;
7036 struct rpc_message msg = {
7038 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
7043 struct rpc_task_setup task_setup_data = {
7046 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
7047 .rpc_message = &msg,
7048 .flags = RPC_TASK_TIMEOUT,
7050 struct rpc_task *task;
7052 dprintk("--> %s\n", __func__);
7054 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7055 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7056 args.dir = NFS4_CDFC4_FORE;
7058 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7059 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7060 args.dir = NFS4_CDFC4_FORE;
7062 task = rpc_run_task(&task_setup_data);
7063 if (!IS_ERR(task)) {
7064 status = task->tk_status;
7067 status = PTR_ERR(task);
7068 trace_nfs4_bind_conn_to_session(clp, status);
7070 if (memcmp(res.sessionid.data,
7071 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7072 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7076 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
7077 dprintk("NFS: %s: Unexpected direction from server\n",
7082 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
7083 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7090 dprintk("<-- %s status= %d\n", __func__, status);
7094 struct rpc_bind_conn_calldata {
7095 struct nfs_client *clp;
7096 struct rpc_cred *cred;
7100 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7101 struct rpc_xprt *xprt,
7104 struct rpc_bind_conn_calldata *p = calldata;
7106 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7109 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7111 struct rpc_bind_conn_calldata data = {
7115 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7116 nfs4_proc_bind_conn_to_session_callback, &data);
7120 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7121 * and operations we'd like to see to enable certain features in the allow map
7123 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7124 .how = SP4_MACH_CRED,
7125 .enforce.u.words = {
7126 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7127 1 << (OP_EXCHANGE_ID - 32) |
7128 1 << (OP_CREATE_SESSION - 32) |
7129 1 << (OP_DESTROY_SESSION - 32) |
7130 1 << (OP_DESTROY_CLIENTID - 32)
7133 [0] = 1 << (OP_CLOSE) |
7134 1 << (OP_OPEN_DOWNGRADE) |
7136 1 << (OP_DELEGRETURN) |
7138 [1] = 1 << (OP_SECINFO - 32) |
7139 1 << (OP_SECINFO_NO_NAME - 32) |
7140 1 << (OP_LAYOUTRETURN - 32) |
7141 1 << (OP_TEST_STATEID - 32) |
7142 1 << (OP_FREE_STATEID - 32) |
7143 1 << (OP_WRITE - 32)
7148 * Select the state protection mode for client `clp' given the server results
7149 * from exchange_id in `sp'.
7151 * Returns 0 on success, negative errno otherwise.
7153 static int nfs4_sp4_select_mode(struct nfs_client *clp,
7154 struct nfs41_state_protection *sp)
7156 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7157 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7158 1 << (OP_EXCHANGE_ID - 32) |
7159 1 << (OP_CREATE_SESSION - 32) |
7160 1 << (OP_DESTROY_SESSION - 32) |
7161 1 << (OP_DESTROY_CLIENTID - 32)
7165 if (sp->how == SP4_MACH_CRED) {
7166 /* Print state protect result */
7167 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7168 for (i = 0; i <= LAST_NFS4_OP; i++) {
7169 if (test_bit(i, sp->enforce.u.longs))
7170 dfprintk(MOUNT, " enforce op %d\n", i);
7171 if (test_bit(i, sp->allow.u.longs))
7172 dfprintk(MOUNT, " allow op %d\n", i);
7175 /* make sure nothing is on enforce list that isn't supported */
7176 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7177 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7178 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7184 * Minimal mode - state operations are allowed to use machine
7185 * credential. Note this already happens by default, so the
7186 * client doesn't have to do anything more than the negotiation.
7188 * NOTE: we don't care if EXCHANGE_ID is in the list -
7189 * we're already using the machine cred for exchange_id
7190 * and will never use a different cred.
7192 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7193 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7194 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7195 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7196 dfprintk(MOUNT, "sp4_mach_cred:\n");
7197 dfprintk(MOUNT, " minimal mode enabled\n");
7198 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
7200 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7204 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
7205 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7206 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
7207 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7208 dfprintk(MOUNT, " cleanup mode enabled\n");
7209 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
7212 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7213 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7214 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
7215 &clp->cl_sp4_flags);
7218 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7219 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7220 dfprintk(MOUNT, " secinfo mode enabled\n");
7221 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
7224 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7225 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7226 dfprintk(MOUNT, " stateid mode enabled\n");
7227 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
7230 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7231 dfprintk(MOUNT, " write mode enabled\n");
7232 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
7235 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7236 dfprintk(MOUNT, " commit mode enabled\n");
7237 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
7244 struct nfs41_exchange_id_data {
7245 struct nfs41_exchange_id_res res;
7246 struct nfs41_exchange_id_args args;
7247 struct rpc_xprt *xprt;
7251 static void nfs4_exchange_id_done(struct rpc_task *task, void *data)
7253 struct nfs41_exchange_id_data *cdata =
7254 (struct nfs41_exchange_id_data *)data;
7255 struct nfs_client *clp = cdata->args.client;
7256 int status = task->tk_status;
7258 trace_nfs4_exchange_id(clp, status);
7261 status = nfs4_check_cl_exchange_flags(cdata->res.flags);
7263 if (cdata->xprt && status == 0) {
7264 status = nfs4_detect_session_trunking(clp, &cdata->res,
7270 status = nfs4_sp4_select_mode(clp, &cdata->res.state_protect);
7273 clp->cl_clientid = cdata->res.clientid;
7274 clp->cl_exchange_flags = cdata->res.flags;
7275 /* Client ID is not confirmed */
7276 if (!(cdata->res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
7277 clear_bit(NFS4_SESSION_ESTABLISHED,
7278 &clp->cl_session->session_state);
7279 clp->cl_seqid = cdata->res.seqid;
7282 kfree(clp->cl_serverowner);
7283 clp->cl_serverowner = cdata->res.server_owner;
7284 cdata->res.server_owner = NULL;
7286 /* use the most recent implementation id */
7287 kfree(clp->cl_implid);
7288 clp->cl_implid = cdata->res.impl_id;
7289 cdata->res.impl_id = NULL;
7291 if (clp->cl_serverscope != NULL &&
7292 !nfs41_same_server_scope(clp->cl_serverscope,
7293 cdata->res.server_scope)) {
7294 dprintk("%s: server_scope mismatch detected\n",
7296 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
7297 kfree(clp->cl_serverscope);
7298 clp->cl_serverscope = NULL;
7301 if (clp->cl_serverscope == NULL) {
7302 clp->cl_serverscope = cdata->res.server_scope;
7303 cdata->res.server_scope = NULL;
7305 /* Save the EXCHANGE_ID verifier session trunk tests */
7306 memcpy(clp->cl_confirm.data, cdata->args.verifier->data,
7307 sizeof(clp->cl_confirm.data));
7310 cdata->rpc_status = status;
7314 static void nfs4_exchange_id_release(void *data)
7316 struct nfs41_exchange_id_data *cdata =
7317 (struct nfs41_exchange_id_data *)data;
7319 nfs_put_client(cdata->args.client);
7321 xprt_put(cdata->xprt);
7322 rpc_clnt_xprt_switch_put(cdata->args.client->cl_rpcclient);
7324 kfree(cdata->res.impl_id);
7325 kfree(cdata->res.server_scope);
7326 kfree(cdata->res.server_owner);
7330 static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7331 .rpc_call_done = nfs4_exchange_id_done,
7332 .rpc_release = nfs4_exchange_id_release,
7336 * _nfs4_proc_exchange_id()
7338 * Wrapper for EXCHANGE_ID operation.
7340 static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
7341 u32 sp4_how, struct rpc_xprt *xprt)
7343 nfs4_verifier verifier;
7344 struct rpc_message msg = {
7345 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
7348 struct rpc_task_setup task_setup_data = {
7349 .rpc_client = clp->cl_rpcclient,
7350 .callback_ops = &nfs4_exchange_id_call_ops,
7351 .rpc_message = &msg,
7352 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7354 struct nfs41_exchange_id_data *calldata;
7355 struct rpc_task *task;
7358 if (!atomic_inc_not_zero(&clp->cl_count))
7362 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7367 nfs4_init_boot_verifier(clp, &verifier);
7369 status = nfs4_init_uniform_client_string(clp);
7373 dprintk("NFS call exchange_id auth=%s, '%s'\n",
7374 clp->cl_rpcclient->cl_auth->au_ops->au_name,
7377 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7380 if (unlikely(calldata->res.server_owner == NULL))
7383 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
7385 if (unlikely(calldata->res.server_scope == NULL))
7386 goto out_server_owner;
7388 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7389 if (unlikely(calldata->res.impl_id == NULL))
7390 goto out_server_scope;
7394 calldata->args.state_protect.how = SP4_NONE;
7398 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
7408 calldata->xprt = xprt;
7409 task_setup_data.rpc_xprt = xprt;
7410 task_setup_data.flags =
7411 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC;
7412 calldata->args.verifier = &clp->cl_confirm;
7414 calldata->args.verifier = &verifier;
7416 calldata->args.client = clp;
7417 #ifdef CONFIG_NFS_V4_1_MIGRATION
7418 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7419 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7420 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7422 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7423 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7425 msg.rpc_argp = &calldata->args;
7426 msg.rpc_resp = &calldata->res;
7427 task_setup_data.callback_data = calldata;
7429 task = rpc_run_task(&task_setup_data);
7431 status = PTR_ERR(task);
7436 status = rpc_wait_for_completion_task(task);
7438 status = calldata->rpc_status;
7439 } else /* session trunking test */
7440 status = calldata->rpc_status;
7444 if (clp->cl_implid != NULL)
7445 dprintk("NFS reply exchange_id: Server Implementation ID: "
7446 "domain: %s, name: %s, date: %llu,%u\n",
7447 clp->cl_implid->domain, clp->cl_implid->name,
7448 clp->cl_implid->date.seconds,
7449 clp->cl_implid->date.nseconds);
7450 dprintk("NFS reply exchange_id: %d\n", status);
7454 kfree(calldata->res.impl_id);
7456 kfree(calldata->res.server_scope);
7458 kfree(calldata->res.server_owner);
7465 * nfs4_proc_exchange_id()
7467 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7469 * Since the clientid has expired, all compounds using sessions
7470 * associated with the stale clientid will be returning
7471 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7472 * be in some phase of session reset.
7474 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7476 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7478 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7481 /* try SP4_MACH_CRED if krb5i/p */
7482 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7483 authflavor == RPC_AUTH_GSS_KRB5P) {
7484 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED, NULL);
7490 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE, NULL);
7494 * nfs4_test_session_trunk
7496 * This is an add_xprt_test() test function called from
7497 * rpc_clnt_setup_test_and_add_xprt.
7499 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7500 * and is dereferrenced in nfs4_exchange_id_release
7502 * Upon success, add the new transport to the rpc_clnt
7504 * @clnt: struct rpc_clnt to get new transport
7505 * @xprt: the rpc_xprt to test
7506 * @data: call data for _nfs4_proc_exchange_id.
7508 int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7511 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
7514 dprintk("--> %s try %s\n", __func__,
7515 xprt->address_strings[RPC_DISPLAY_ADDR]);
7517 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7519 /* Test connection for session trunking. Async exchange_id call */
7520 return _nfs4_proc_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7522 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7524 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7525 struct rpc_cred *cred)
7527 struct rpc_message msg = {
7528 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7534 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7535 trace_nfs4_destroy_clientid(clp, status);
7537 dprintk("NFS: Got error %d from the server %s on "
7538 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7542 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7543 struct rpc_cred *cred)
7548 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7549 ret = _nfs4_proc_destroy_clientid(clp, cred);
7551 case -NFS4ERR_DELAY:
7552 case -NFS4ERR_CLIENTID_BUSY:
7562 int nfs4_destroy_clientid(struct nfs_client *clp)
7564 struct rpc_cred *cred;
7567 if (clp->cl_mvops->minor_version < 1)
7569 if (clp->cl_exchange_flags == 0)
7571 if (clp->cl_preserve_clid)
7573 cred = nfs4_get_clid_cred(clp);
7574 ret = nfs4_proc_destroy_clientid(clp, cred);
7579 case -NFS4ERR_STALE_CLIENTID:
7580 clp->cl_exchange_flags = 0;
7586 struct nfs4_get_lease_time_data {
7587 struct nfs4_get_lease_time_args *args;
7588 struct nfs4_get_lease_time_res *res;
7589 struct nfs_client *clp;
7592 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7595 struct nfs4_get_lease_time_data *data =
7596 (struct nfs4_get_lease_time_data *)calldata;
7598 dprintk("--> %s\n", __func__);
7599 /* just setup sequence, do not trigger session recovery
7600 since we're invoked within one */
7601 nfs41_setup_sequence(data->clp->cl_session,
7602 &data->args->la_seq_args,
7603 &data->res->lr_seq_res,
7605 dprintk("<-- %s\n", __func__);
7609 * Called from nfs4_state_manager thread for session setup, so don't recover
7610 * from sequence operation or clientid errors.
7612 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7614 struct nfs4_get_lease_time_data *data =
7615 (struct nfs4_get_lease_time_data *)calldata;
7617 dprintk("--> %s\n", __func__);
7618 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7620 switch (task->tk_status) {
7621 case -NFS4ERR_DELAY:
7622 case -NFS4ERR_GRACE:
7623 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7624 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7625 task->tk_status = 0;
7627 case -NFS4ERR_RETRY_UNCACHED_REP:
7628 rpc_restart_call_prepare(task);
7631 dprintk("<-- %s\n", __func__);
7634 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
7635 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7636 .rpc_call_done = nfs4_get_lease_time_done,
7639 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7641 struct rpc_task *task;
7642 struct nfs4_get_lease_time_args args;
7643 struct nfs4_get_lease_time_res res = {
7644 .lr_fsinfo = fsinfo,
7646 struct nfs4_get_lease_time_data data = {
7651 struct rpc_message msg = {
7652 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7656 struct rpc_task_setup task_setup = {
7657 .rpc_client = clp->cl_rpcclient,
7658 .rpc_message = &msg,
7659 .callback_ops = &nfs4_get_lease_time_ops,
7660 .callback_data = &data,
7661 .flags = RPC_TASK_TIMEOUT,
7665 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
7666 nfs4_set_sequence_privileged(&args.la_seq_args);
7667 dprintk("--> %s\n", __func__);
7668 task = rpc_run_task(&task_setup);
7671 status = PTR_ERR(task);
7673 status = task->tk_status;
7676 dprintk("<-- %s return %d\n", __func__, status);
7682 * Initialize the values to be used by the client in CREATE_SESSION
7683 * If nfs4_init_session set the fore channel request and response sizes,
7686 * Set the back channel max_resp_sz_cached to zero to force the client to
7687 * always set csa_cachethis to FALSE because the current implementation
7688 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7690 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7691 struct rpc_clnt *clnt)
7693 unsigned int max_rqst_sz, max_resp_sz;
7694 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
7696 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7697 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7699 /* Fore channel attributes */
7700 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7701 args->fc_attrs.max_resp_sz = max_resp_sz;
7702 args->fc_attrs.max_ops = NFS4_MAX_OPS;
7703 args->fc_attrs.max_reqs = max_session_slots;
7705 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
7706 "max_ops=%u max_reqs=%u\n",
7708 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
7709 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
7711 /* Back channel attributes */
7712 args->bc_attrs.max_rqst_sz = max_bc_payload;
7713 args->bc_attrs.max_resp_sz = max_bc_payload;
7714 args->bc_attrs.max_resp_sz_cached = 0;
7715 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
7716 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
7718 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7719 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7721 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7722 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7723 args->bc_attrs.max_reqs);
7726 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7727 struct nfs41_create_session_res *res)
7729 struct nfs4_channel_attrs *sent = &args->fc_attrs;
7730 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
7732 if (rcvd->max_resp_sz > sent->max_resp_sz)
7735 * Our requested max_ops is the minimum we need; we're not
7736 * prepared to break up compounds into smaller pieces than that.
7737 * So, no point even trying to continue if the server won't
7740 if (rcvd->max_ops < sent->max_ops)
7742 if (rcvd->max_reqs == 0)
7744 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7745 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
7749 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7750 struct nfs41_create_session_res *res)
7752 struct nfs4_channel_attrs *sent = &args->bc_attrs;
7753 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
7755 if (!(res->flags & SESSION4_BACK_CHAN))
7757 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7759 if (rcvd->max_resp_sz < sent->max_resp_sz)
7761 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7763 if (rcvd->max_ops > sent->max_ops)
7765 if (rcvd->max_reqs > sent->max_reqs)
7771 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
7772 struct nfs41_create_session_res *res)
7776 ret = nfs4_verify_fore_channel_attrs(args, res);
7779 return nfs4_verify_back_channel_attrs(args, res);
7782 static void nfs4_update_session(struct nfs4_session *session,
7783 struct nfs41_create_session_res *res)
7785 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
7786 /* Mark client id and session as being confirmed */
7787 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7788 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
7789 session->flags = res->flags;
7790 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
7791 if (res->flags & SESSION4_BACK_CHAN)
7792 memcpy(&session->bc_attrs, &res->bc_attrs,
7793 sizeof(session->bc_attrs));
7796 static int _nfs4_proc_create_session(struct nfs_client *clp,
7797 struct rpc_cred *cred)
7799 struct nfs4_session *session = clp->cl_session;
7800 struct nfs41_create_session_args args = {
7802 .clientid = clp->cl_clientid,
7803 .seqid = clp->cl_seqid,
7804 .cb_program = NFS4_CALLBACK,
7806 struct nfs41_create_session_res res;
7808 struct rpc_message msg = {
7809 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7816 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
7817 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
7819 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7820 trace_nfs4_create_session(clp, status);
7823 case -NFS4ERR_STALE_CLIENTID:
7824 case -NFS4ERR_DELAY:
7833 /* Verify the session's negotiated channel_attrs values */
7834 status = nfs4_verify_channel_attrs(&args, &res);
7835 /* Increment the clientid slot sequence id */
7838 nfs4_update_session(session, &res);
7845 * Issues a CREATE_SESSION operation to the server.
7846 * It is the responsibility of the caller to verify the session is
7847 * expired before calling this routine.
7849 int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
7853 struct nfs4_session *session = clp->cl_session;
7855 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7857 status = _nfs4_proc_create_session(clp, cred);
7861 /* Init or reset the session slot tables */
7862 status = nfs4_setup_session_slot_tables(session);
7863 dprintk("slot table setup returned %d\n", status);
7867 ptr = (unsigned *)&session->sess_id.data[0];
7868 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7869 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
7871 dprintk("<-- %s\n", __func__);
7876 * Issue the over-the-wire RPC DESTROY_SESSION.
7877 * The caller must serialize access to this routine.
7879 int nfs4_proc_destroy_session(struct nfs4_session *session,
7880 struct rpc_cred *cred)
7882 struct rpc_message msg = {
7883 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
7884 .rpc_argp = session,
7889 dprintk("--> nfs4_proc_destroy_session\n");
7891 /* session is still being setup */
7892 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
7895 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7896 trace_nfs4_destroy_session(session->clp, status);
7899 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
7900 "Session has been destroyed regardless...\n", status);
7902 dprintk("<-- nfs4_proc_destroy_session\n");
7907 * Renew the cl_session lease.
7909 struct nfs4_sequence_data {
7910 struct nfs_client *clp;
7911 struct nfs4_sequence_args args;
7912 struct nfs4_sequence_res res;
7915 static void nfs41_sequence_release(void *data)
7917 struct nfs4_sequence_data *calldata = data;
7918 struct nfs_client *clp = calldata->clp;
7920 if (atomic_read(&clp->cl_count) > 1)
7921 nfs4_schedule_state_renewal(clp);
7922 nfs_put_client(clp);
7926 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7928 switch(task->tk_status) {
7929 case -NFS4ERR_DELAY:
7930 rpc_delay(task, NFS4_POLL_RETRY_MAX);
7933 nfs4_schedule_lease_recovery(clp);
7938 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
7940 struct nfs4_sequence_data *calldata = data;
7941 struct nfs_client *clp = calldata->clp;
7943 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
7946 trace_nfs4_sequence(clp, task->tk_status);
7947 if (task->tk_status < 0) {
7948 dprintk("%s ERROR %d\n", __func__, task->tk_status);
7949 if (atomic_read(&clp->cl_count) == 1)
7952 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
7953 rpc_restart_call_prepare(task);
7957 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
7959 dprintk("<-- %s\n", __func__);
7962 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
7964 struct nfs4_sequence_data *calldata = data;
7965 struct nfs_client *clp = calldata->clp;
7966 struct nfs4_sequence_args *args;
7967 struct nfs4_sequence_res *res;
7969 args = task->tk_msg.rpc_argp;
7970 res = task->tk_msg.rpc_resp;
7972 nfs41_setup_sequence(clp->cl_session, args, res, task);
7975 static const struct rpc_call_ops nfs41_sequence_ops = {
7976 .rpc_call_done = nfs41_sequence_call_done,
7977 .rpc_call_prepare = nfs41_sequence_prepare,
7978 .rpc_release = nfs41_sequence_release,
7981 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
7982 struct rpc_cred *cred,
7985 struct nfs4_sequence_data *calldata;
7986 struct rpc_message msg = {
7987 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
7990 struct rpc_task_setup task_setup_data = {
7991 .rpc_client = clp->cl_rpcclient,
7992 .rpc_message = &msg,
7993 .callback_ops = &nfs41_sequence_ops,
7994 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7997 if (!atomic_inc_not_zero(&clp->cl_count))
7998 return ERR_PTR(-EIO);
7999 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8000 if (calldata == NULL) {
8001 nfs_put_client(clp);
8002 return ERR_PTR(-ENOMEM);
8004 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
8006 nfs4_set_sequence_privileged(&calldata->args);
8007 msg.rpc_argp = &calldata->args;
8008 msg.rpc_resp = &calldata->res;
8009 calldata->clp = clp;
8010 task_setup_data.callback_data = calldata;
8012 return rpc_run_task(&task_setup_data);
8015 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
8017 struct rpc_task *task;
8020 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
8022 task = _nfs41_proc_sequence(clp, cred, false);
8024 ret = PTR_ERR(task);
8026 rpc_put_task_async(task);
8027 dprintk("<-- %s status=%d\n", __func__, ret);
8031 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8033 struct rpc_task *task;
8036 task = _nfs41_proc_sequence(clp, cred, true);
8038 ret = PTR_ERR(task);
8041 ret = rpc_wait_for_completion_task(task);
8043 ret = task->tk_status;
8046 dprintk("<-- %s status=%d\n", __func__, ret);
8050 struct nfs4_reclaim_complete_data {
8051 struct nfs_client *clp;
8052 struct nfs41_reclaim_complete_args arg;
8053 struct nfs41_reclaim_complete_res res;
8056 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8058 struct nfs4_reclaim_complete_data *calldata = data;
8060 nfs41_setup_sequence(calldata->clp->cl_session,
8061 &calldata->arg.seq_args,
8062 &calldata->res.seq_res,
8066 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8068 switch(task->tk_status) {
8070 case -NFS4ERR_COMPLETE_ALREADY:
8071 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8073 case -NFS4ERR_DELAY:
8074 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8076 case -NFS4ERR_RETRY_UNCACHED_REP:
8079 nfs4_schedule_lease_recovery(clp);
8084 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8086 struct nfs4_reclaim_complete_data *calldata = data;
8087 struct nfs_client *clp = calldata->clp;
8088 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8090 dprintk("--> %s\n", __func__);
8091 if (!nfs41_sequence_done(task, res))
8094 trace_nfs4_reclaim_complete(clp, task->tk_status);
8095 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8096 rpc_restart_call_prepare(task);
8099 dprintk("<-- %s\n", __func__);
8102 static void nfs4_free_reclaim_complete_data(void *data)
8104 struct nfs4_reclaim_complete_data *calldata = data;
8109 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8110 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8111 .rpc_call_done = nfs4_reclaim_complete_done,
8112 .rpc_release = nfs4_free_reclaim_complete_data,
8116 * Issue a global reclaim complete.
8118 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8119 struct rpc_cred *cred)
8121 struct nfs4_reclaim_complete_data *calldata;
8122 struct rpc_task *task;
8123 struct rpc_message msg = {
8124 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
8127 struct rpc_task_setup task_setup_data = {
8128 .rpc_client = clp->cl_rpcclient,
8129 .rpc_message = &msg,
8130 .callback_ops = &nfs4_reclaim_complete_call_ops,
8131 .flags = RPC_TASK_ASYNC,
8133 int status = -ENOMEM;
8135 dprintk("--> %s\n", __func__);
8136 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8137 if (calldata == NULL)
8139 calldata->clp = clp;
8140 calldata->arg.one_fs = 0;
8142 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
8143 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
8144 msg.rpc_argp = &calldata->arg;
8145 msg.rpc_resp = &calldata->res;
8146 task_setup_data.callback_data = calldata;
8147 task = rpc_run_task(&task_setup_data);
8149 status = PTR_ERR(task);
8152 status = nfs4_wait_for_completion_rpc_task(task);
8154 status = task->tk_status;
8158 dprintk("<-- %s status=%d\n", __func__, status);
8163 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8165 struct nfs4_layoutget *lgp = calldata;
8166 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
8167 struct nfs4_session *session = nfs4_get_session(server);
8169 dprintk("--> %s\n", __func__);
8170 nfs41_setup_sequence(session, &lgp->args.seq_args,
8171 &lgp->res.seq_res, task);
8172 dprintk("<-- %s\n", __func__);
8175 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8177 struct nfs4_layoutget *lgp = calldata;
8179 dprintk("--> %s\n", __func__);
8180 nfs41_sequence_process(task, &lgp->res.seq_res);
8181 dprintk("<-- %s\n", __func__);
8185 nfs4_layoutget_handle_exception(struct rpc_task *task,
8186 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8188 struct inode *inode = lgp->args.inode;
8189 struct nfs_server *server = NFS_SERVER(inode);
8190 struct pnfs_layout_hdr *lo;
8191 int nfs4err = task->tk_status;
8192 int err, status = 0;
8195 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
8202 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8203 * on the file. set tk_status to -ENODATA to tell upper layer to
8206 case -NFS4ERR_LAYOUTUNAVAILABLE:
8210 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8211 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8213 case -NFS4ERR_BADLAYOUT:
8214 status = -EOVERFLOW;
8217 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
8218 * (or clients) writing to the same RAID stripe except when
8219 * the minlength argument is 0 (see RFC5661 section 18.43.3).
8221 * Treat it like we would RECALLCONFLICT -- we retry for a little
8222 * while, and then eventually give up.
8224 case -NFS4ERR_LAYOUTTRYLATER:
8225 if (lgp->args.minlength == 0) {
8226 status = -EOVERFLOW;
8231 case -NFS4ERR_RECALLCONFLICT:
8232 status = -ERECALLCONFLICT;
8234 case -NFS4ERR_EXPIRED:
8235 case -NFS4ERR_BAD_STATEID:
8236 exception->timeout = 0;
8237 spin_lock(&inode->i_lock);
8238 lo = NFS_I(inode)->layout;
8239 /* If the open stateid was bad, then recover it. */
8240 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8241 nfs4_stateid_match_other(&lgp->args.stateid,
8242 &lgp->args.ctx->state->stateid)) {
8243 spin_unlock(&inode->i_lock);
8244 exception->state = lgp->args.ctx->state;
8249 * Mark the bad layout state as invalid, then retry
8251 pnfs_mark_layout_stateid_invalid(lo, &head);
8252 spin_unlock(&inode->i_lock);
8253 pnfs_free_lseg_list(&head);
8258 err = nfs4_handle_exception(server, nfs4err, exception);
8260 if (exception->retry)
8266 dprintk("<-- %s\n", __func__);
8270 static size_t max_response_pages(struct nfs_server *server)
8272 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8273 return nfs_page_array_len(0, max_resp_sz);
8276 static void nfs4_free_pages(struct page **pages, size_t size)
8283 for (i = 0; i < size; i++) {
8286 __free_page(pages[i]);
8291 static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8293 struct page **pages;
8296 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8298 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8302 for (i = 0; i < size; i++) {
8303 pages[i] = alloc_page(gfp_flags);
8305 dprintk("%s: failed to allocate page\n", __func__);
8306 nfs4_free_pages(pages, size);
8314 static void nfs4_layoutget_release(void *calldata)
8316 struct nfs4_layoutget *lgp = calldata;
8317 struct inode *inode = lgp->args.inode;
8318 struct nfs_server *server = NFS_SERVER(inode);
8319 size_t max_pages = max_response_pages(server);
8321 dprintk("--> %s\n", __func__);
8322 nfs4_free_pages(lgp->args.layout.pages, max_pages);
8323 pnfs_put_layout_hdr(NFS_I(inode)->layout);
8324 put_nfs_open_context(lgp->args.ctx);
8326 dprintk("<-- %s\n", __func__);
8329 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8330 .rpc_call_prepare = nfs4_layoutget_prepare,
8331 .rpc_call_done = nfs4_layoutget_done,
8332 .rpc_release = nfs4_layoutget_release,
8335 struct pnfs_layout_segment *
8336 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
8338 struct inode *inode = lgp->args.inode;
8339 struct nfs_server *server = NFS_SERVER(inode);
8340 size_t max_pages = max_response_pages(server);
8341 struct rpc_task *task;
8342 struct rpc_message msg = {
8343 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8344 .rpc_argp = &lgp->args,
8345 .rpc_resp = &lgp->res,
8346 .rpc_cred = lgp->cred,
8348 struct rpc_task_setup task_setup_data = {
8349 .rpc_client = server->client,
8350 .rpc_message = &msg,
8351 .callback_ops = &nfs4_layoutget_call_ops,
8352 .callback_data = lgp,
8353 .flags = RPC_TASK_ASYNC,
8355 struct pnfs_layout_segment *lseg = NULL;
8356 struct nfs4_exception exception = {
8358 .timeout = *timeout,
8362 dprintk("--> %s\n", __func__);
8364 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8365 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8367 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8368 if (!lgp->args.layout.pages) {
8369 nfs4_layoutget_release(lgp);
8370 return ERR_PTR(-ENOMEM);
8372 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8374 lgp->res.layoutp = &lgp->args.layout;
8375 lgp->res.seq_res.sr_slot = NULL;
8376 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
8378 task = rpc_run_task(&task_setup_data);
8380 return ERR_CAST(task);
8381 status = nfs4_wait_for_completion_rpc_task(task);
8383 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8384 *timeout = exception.timeout;
8387 trace_nfs4_layoutget(lgp->args.ctx,
8393 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8394 if (status == 0 && lgp->res.layoutp->len)
8395 lseg = pnfs_layout_process(lgp);
8396 nfs4_sequence_free_slot(&lgp->res.seq_res);
8398 dprintk("<-- %s status=%d\n", __func__, status);
8400 return ERR_PTR(status);
8405 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8407 struct nfs4_layoutreturn *lrp = calldata;
8409 dprintk("--> %s\n", __func__);
8410 nfs41_setup_sequence(lrp->clp->cl_session,
8411 &lrp->args.seq_args,
8416 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8418 struct nfs4_layoutreturn *lrp = calldata;
8419 struct nfs_server *server;
8421 dprintk("--> %s\n", __func__);
8423 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
8426 server = NFS_SERVER(lrp->args.inode);
8427 switch (task->tk_status) {
8429 task->tk_status = 0;
8432 case -NFS4ERR_DELAY:
8433 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
8435 nfs4_sequence_free_slot(&lrp->res.seq_res);
8436 rpc_restart_call_prepare(task);
8439 dprintk("<-- %s\n", __func__);
8442 static void nfs4_layoutreturn_release(void *calldata)
8444 struct nfs4_layoutreturn *lrp = calldata;
8445 struct pnfs_layout_hdr *lo = lrp->args.layout;
8448 dprintk("--> %s\n", __func__);
8449 spin_lock(&lo->plh_inode->i_lock);
8450 if (lrp->res.lrs_present) {
8451 pnfs_mark_matching_lsegs_invalid(lo, &freeme,
8453 be32_to_cpu(lrp->args.stateid.seqid));
8454 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
8456 pnfs_mark_layout_stateid_invalid(lo, &freeme);
8457 pnfs_clear_layoutreturn_waitbit(lo);
8458 spin_unlock(&lo->plh_inode->i_lock);
8459 nfs4_sequence_free_slot(&lrp->res.seq_res);
8460 pnfs_free_lseg_list(&freeme);
8461 pnfs_put_layout_hdr(lrp->args.layout);
8462 nfs_iput_and_deactive(lrp->inode);
8464 dprintk("<-- %s\n", __func__);
8467 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8468 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8469 .rpc_call_done = nfs4_layoutreturn_done,
8470 .rpc_release = nfs4_layoutreturn_release,
8473 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
8475 struct rpc_task *task;
8476 struct rpc_message msg = {
8477 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8478 .rpc_argp = &lrp->args,
8479 .rpc_resp = &lrp->res,
8480 .rpc_cred = lrp->cred,
8482 struct rpc_task_setup task_setup_data = {
8483 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
8484 .rpc_message = &msg,
8485 .callback_ops = &nfs4_layoutreturn_call_ops,
8486 .callback_data = lrp,
8490 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8491 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8492 &task_setup_data.rpc_client, &msg);
8494 dprintk("--> %s\n", __func__);
8496 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8498 nfs4_layoutreturn_release(lrp);
8501 task_setup_data.flags |= RPC_TASK_ASYNC;
8503 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
8504 task = rpc_run_task(&task_setup_data);
8506 return PTR_ERR(task);
8508 status = task->tk_status;
8509 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
8510 dprintk("<-- %s status=%d\n", __func__, status);
8516 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
8517 struct pnfs_device *pdev,
8518 struct rpc_cred *cred)
8520 struct nfs4_getdeviceinfo_args args = {
8522 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8523 NOTIFY_DEVICEID4_DELETE,
8525 struct nfs4_getdeviceinfo_res res = {
8528 struct rpc_message msg = {
8529 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8536 dprintk("--> %s\n", __func__);
8537 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
8538 if (res.notification & ~args.notify_types)
8539 dprintk("%s: unsupported notification\n", __func__);
8540 if (res.notification != args.notify_types)
8543 dprintk("<-- %s status=%d\n", __func__, status);
8548 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8549 struct pnfs_device *pdev,
8550 struct rpc_cred *cred)
8552 struct nfs4_exception exception = { };
8556 err = nfs4_handle_exception(server,
8557 _nfs4_proc_getdeviceinfo(server, pdev, cred),
8559 } while (exception.retry);
8562 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8564 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8566 struct nfs4_layoutcommit_data *data = calldata;
8567 struct nfs_server *server = NFS_SERVER(data->args.inode);
8568 struct nfs4_session *session = nfs4_get_session(server);
8570 nfs41_setup_sequence(session,
8571 &data->args.seq_args,
8577 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8579 struct nfs4_layoutcommit_data *data = calldata;
8580 struct nfs_server *server = NFS_SERVER(data->args.inode);
8582 if (!nfs41_sequence_done(task, &data->res.seq_res))
8585 switch (task->tk_status) { /* Just ignore these failures */
8586 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8587 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8588 case -NFS4ERR_BADLAYOUT: /* no layout */
8589 case -NFS4ERR_GRACE: /* loca_recalim always false */
8590 task->tk_status = 0;
8594 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
8595 rpc_restart_call_prepare(task);
8601 static void nfs4_layoutcommit_release(void *calldata)
8603 struct nfs4_layoutcommit_data *data = calldata;
8605 pnfs_cleanup_layoutcommit(data);
8606 nfs_post_op_update_inode_force_wcc(data->args.inode,
8608 put_rpccred(data->cred);
8609 nfs_iput_and_deactive(data->inode);
8613 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8614 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8615 .rpc_call_done = nfs4_layoutcommit_done,
8616 .rpc_release = nfs4_layoutcommit_release,
8620 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
8622 struct rpc_message msg = {
8623 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8624 .rpc_argp = &data->args,
8625 .rpc_resp = &data->res,
8626 .rpc_cred = data->cred,
8628 struct rpc_task_setup task_setup_data = {
8629 .task = &data->task,
8630 .rpc_client = NFS_CLIENT(data->args.inode),
8631 .rpc_message = &msg,
8632 .callback_ops = &nfs4_layoutcommit_ops,
8633 .callback_data = data,
8635 struct rpc_task *task;
8638 dprintk("NFS: initiating layoutcommit call. sync %d "
8639 "lbw: %llu inode %lu\n", sync,
8640 data->args.lastbytewritten,
8641 data->args.inode->i_ino);
8644 data->inode = nfs_igrab_and_active(data->args.inode);
8645 if (data->inode == NULL) {
8646 nfs4_layoutcommit_release(data);
8649 task_setup_data.flags = RPC_TASK_ASYNC;
8651 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
8652 task = rpc_run_task(&task_setup_data);
8654 return PTR_ERR(task);
8656 status = task->tk_status;
8657 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
8658 dprintk("%s: status %d\n", __func__, status);
8664 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8665 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8668 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8669 struct nfs_fsinfo *info,
8670 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
8672 struct nfs41_secinfo_no_name_args args = {
8673 .style = SECINFO_STYLE_CURRENT_FH,
8675 struct nfs4_secinfo_res res = {
8678 struct rpc_message msg = {
8679 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8683 struct rpc_clnt *clnt = server->client;
8684 struct rpc_cred *cred = NULL;
8687 if (use_integrity) {
8688 clnt = server->nfs_client->cl_rpcclient;
8689 cred = nfs4_get_clid_cred(server->nfs_client);
8690 msg.rpc_cred = cred;
8693 dprintk("--> %s\n", __func__);
8694 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8696 dprintk("<-- %s status=%d\n", __func__, status);
8705 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8706 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8708 struct nfs4_exception exception = { };
8711 /* first try using integrity protection */
8712 err = -NFS4ERR_WRONGSEC;
8714 /* try to use integrity protection with machine cred */
8715 if (_nfs4_is_integrity_protected(server->nfs_client))
8716 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8720 * if unable to use integrity protection, or SECINFO with
8721 * integrity protection returns NFS4ERR_WRONGSEC (which is
8722 * disallowed by spec, but exists in deployed servers) use
8723 * the current filesystem's rpc_client and the user cred.
8725 if (err == -NFS4ERR_WRONGSEC)
8726 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8731 case -NFS4ERR_WRONGSEC:
8735 err = nfs4_handle_exception(server, err, &exception);
8737 } while (exception.retry);
8743 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8744 struct nfs_fsinfo *info)
8748 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
8749 struct nfs4_secinfo_flavors *flavors;
8750 struct nfs4_secinfo4 *secinfo;
8753 page = alloc_page(GFP_KERNEL);
8759 flavors = page_address(page);
8760 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8763 * Fall back on "guess and check" method if
8764 * the server doesn't support SECINFO_NO_NAME
8766 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
8767 err = nfs4_find_root_sec(server, fhandle, info);
8773 for (i = 0; i < flavors->num_flavors; i++) {
8774 secinfo = &flavors->flavors[i];
8776 switch (secinfo->flavor) {
8780 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8781 &secinfo->flavor_info);
8784 flavor = RPC_AUTH_MAXFLAVOR;
8788 if (!nfs_auth_info_match(&server->auth_info, flavor))
8789 flavor = RPC_AUTH_MAXFLAVOR;
8791 if (flavor != RPC_AUTH_MAXFLAVOR) {
8792 err = nfs4_lookup_root_sec(server, fhandle,
8799 if (flavor == RPC_AUTH_MAXFLAVOR)
8810 static int _nfs41_test_stateid(struct nfs_server *server,
8811 nfs4_stateid *stateid,
8812 struct rpc_cred *cred)
8815 struct nfs41_test_stateid_args args = {
8818 struct nfs41_test_stateid_res res;
8819 struct rpc_message msg = {
8820 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8825 struct rpc_clnt *rpc_client = server->client;
8827 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8830 dprintk("NFS call test_stateid %p\n", stateid);
8831 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
8832 nfs4_set_sequence_privileged(&args.seq_args);
8833 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
8834 &args.seq_args, &res.seq_res);
8835 if (status != NFS_OK) {
8836 dprintk("NFS reply test_stateid: failed, %d\n", status);
8839 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
8843 static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
8844 int err, struct nfs4_exception *exception)
8846 exception->retry = 0;
8848 case -NFS4ERR_DELAY:
8849 nfs4_handle_exception(server, err, exception);
8851 case -NFS4ERR_BADSESSION:
8852 case -NFS4ERR_BADSLOT:
8853 case -NFS4ERR_BAD_HIGH_SLOT:
8854 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8855 case -NFS4ERR_DEADSESSION:
8856 nfs4_do_handle_exception(server, err, exception);
8861 * nfs41_test_stateid - perform a TEST_STATEID operation
8863 * @server: server / transport on which to perform the operation
8864 * @stateid: state ID to test
8867 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8868 * Otherwise a negative NFS4ERR value is returned if the operation
8869 * failed or the state ID is not currently valid.
8871 static int nfs41_test_stateid(struct nfs_server *server,
8872 nfs4_stateid *stateid,
8873 struct rpc_cred *cred)
8875 struct nfs4_exception exception = { };
8878 err = _nfs41_test_stateid(server, stateid, cred);
8879 nfs4_handle_delay_or_session_error(server, err, &exception);
8880 } while (exception.retry);
8884 struct nfs_free_stateid_data {
8885 struct nfs_server *server;
8886 struct nfs41_free_stateid_args args;
8887 struct nfs41_free_stateid_res res;
8890 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
8892 struct nfs_free_stateid_data *data = calldata;
8893 nfs41_setup_sequence(nfs4_get_session(data->server),
8894 &data->args.seq_args,
8899 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
8901 struct nfs_free_stateid_data *data = calldata;
8903 nfs41_sequence_done(task, &data->res.seq_res);
8905 switch (task->tk_status) {
8906 case -NFS4ERR_DELAY:
8907 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
8908 rpc_restart_call_prepare(task);
8912 static void nfs41_free_stateid_release(void *calldata)
8917 static const struct rpc_call_ops nfs41_free_stateid_ops = {
8918 .rpc_call_prepare = nfs41_free_stateid_prepare,
8919 .rpc_call_done = nfs41_free_stateid_done,
8920 .rpc_release = nfs41_free_stateid_release,
8923 static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
8924 nfs4_stateid *stateid,
8925 struct rpc_cred *cred,
8928 struct rpc_message msg = {
8929 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
8932 struct rpc_task_setup task_setup = {
8933 .rpc_client = server->client,
8934 .rpc_message = &msg,
8935 .callback_ops = &nfs41_free_stateid_ops,
8936 .flags = RPC_TASK_ASYNC,
8938 struct nfs_free_stateid_data *data;
8940 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8941 &task_setup.rpc_client, &msg);
8943 dprintk("NFS call free_stateid %p\n", stateid);
8944 data = kmalloc(sizeof(*data), GFP_NOFS);
8946 return ERR_PTR(-ENOMEM);
8947 data->server = server;
8948 nfs4_stateid_copy(&data->args.stateid, stateid);
8950 task_setup.callback_data = data;
8952 msg.rpc_argp = &data->args;
8953 msg.rpc_resp = &data->res;
8954 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
8956 nfs4_set_sequence_privileged(&data->args.seq_args);
8958 return rpc_run_task(&task_setup);
8962 * nfs41_free_stateid - perform a FREE_STATEID operation
8964 * @server: server / transport on which to perform the operation
8965 * @stateid: state ID to release
8968 * Returns NFS_OK if the server freed "stateid". Otherwise a
8969 * negative NFS4ERR value is returned.
8971 static int nfs41_free_stateid(struct nfs_server *server,
8972 nfs4_stateid *stateid,
8973 struct rpc_cred *cred)
8975 struct rpc_task *task;
8978 task = _nfs41_free_stateid(server, stateid, cred, true);
8980 return PTR_ERR(task);
8981 ret = rpc_wait_for_completion_task(task);
8983 ret = task->tk_status;
8989 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
8991 struct rpc_task *task;
8992 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
8994 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
8995 nfs4_free_lock_state(server, lsp);
9001 static bool nfs41_match_stateid(const nfs4_stateid *s1,
9002 const nfs4_stateid *s2)
9004 if (s1->type != s2->type)
9007 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
9010 if (s1->seqid == s2->seqid)
9012 if (s1->seqid == 0 || s2->seqid == 0)
9018 #endif /* CONFIG_NFS_V4_1 */
9020 static bool nfs4_match_stateid(const nfs4_stateid *s1,
9021 const nfs4_stateid *s2)
9023 return nfs4_stateid_match(s1, s2);
9027 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
9028 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9029 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9030 .recover_open = nfs4_open_reclaim,
9031 .recover_lock = nfs4_lock_reclaim,
9032 .establish_clid = nfs4_init_clientid,
9033 .detect_trunking = nfs40_discover_server_trunking,
9036 #if defined(CONFIG_NFS_V4_1)
9037 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
9038 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9039 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9040 .recover_open = nfs4_open_reclaim,
9041 .recover_lock = nfs4_lock_reclaim,
9042 .establish_clid = nfs41_init_clientid,
9043 .reclaim_complete = nfs41_proc_reclaim_complete,
9044 .detect_trunking = nfs41_discover_server_trunking,
9046 #endif /* CONFIG_NFS_V4_1 */
9048 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
9049 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9050 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
9051 .recover_open = nfs40_open_expired,
9052 .recover_lock = nfs4_lock_expired,
9053 .establish_clid = nfs4_init_clientid,
9056 #if defined(CONFIG_NFS_V4_1)
9057 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
9058 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9059 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
9060 .recover_open = nfs41_open_expired,
9061 .recover_lock = nfs41_lock_expired,
9062 .establish_clid = nfs41_init_clientid,
9064 #endif /* CONFIG_NFS_V4_1 */
9066 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
9067 .sched_state_renewal = nfs4_proc_async_renew,
9068 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
9069 .renew_lease = nfs4_proc_renew,
9072 #if defined(CONFIG_NFS_V4_1)
9073 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
9074 .sched_state_renewal = nfs41_proc_async_sequence,
9075 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
9076 .renew_lease = nfs4_proc_sequence,
9080 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
9081 .get_locations = _nfs40_proc_get_locations,
9082 .fsid_present = _nfs40_proc_fsid_present,
9085 #if defined(CONFIG_NFS_V4_1)
9086 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
9087 .get_locations = _nfs41_proc_get_locations,
9088 .fsid_present = _nfs41_proc_fsid_present,
9090 #endif /* CONFIG_NFS_V4_1 */
9092 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9094 .init_caps = NFS_CAP_READDIRPLUS
9095 | NFS_CAP_ATOMIC_OPEN
9096 | NFS_CAP_POSIX_LOCK,
9097 .init_client = nfs40_init_client,
9098 .shutdown_client = nfs40_shutdown_client,
9099 .match_stateid = nfs4_match_stateid,
9100 .find_root_sec = nfs4_find_root_sec,
9101 .free_lock_state = nfs4_release_lockowner,
9102 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
9103 .alloc_seqid = nfs_alloc_seqid,
9104 .call_sync_ops = &nfs40_call_sync_ops,
9105 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9106 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9107 .state_renewal_ops = &nfs40_state_renewal_ops,
9108 .mig_recovery_ops = &nfs40_mig_recovery_ops,
9111 #if defined(CONFIG_NFS_V4_1)
9112 static struct nfs_seqid *
9113 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9118 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9120 .init_caps = NFS_CAP_READDIRPLUS
9121 | NFS_CAP_ATOMIC_OPEN
9122 | NFS_CAP_POSIX_LOCK
9123 | NFS_CAP_STATEID_NFSV41
9124 | NFS_CAP_ATOMIC_OPEN_V1,
9125 .init_client = nfs41_init_client,
9126 .shutdown_client = nfs41_shutdown_client,
9127 .match_stateid = nfs41_match_stateid,
9128 .find_root_sec = nfs41_find_root_sec,
9129 .free_lock_state = nfs41_free_lock_state,
9130 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
9131 .alloc_seqid = nfs_alloc_no_seqid,
9132 .session_trunk = nfs4_test_session_trunk,
9133 .call_sync_ops = &nfs41_call_sync_ops,
9134 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9135 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9136 .state_renewal_ops = &nfs41_state_renewal_ops,
9137 .mig_recovery_ops = &nfs41_mig_recovery_ops,
9141 #if defined(CONFIG_NFS_V4_2)
9142 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9144 .init_caps = NFS_CAP_READDIRPLUS
9145 | NFS_CAP_ATOMIC_OPEN
9146 | NFS_CAP_POSIX_LOCK
9147 | NFS_CAP_STATEID_NFSV41
9148 | NFS_CAP_ATOMIC_OPEN_V1
9151 | NFS_CAP_DEALLOCATE
9153 | NFS_CAP_LAYOUTSTATS
9155 .init_client = nfs41_init_client,
9156 .shutdown_client = nfs41_shutdown_client,
9157 .match_stateid = nfs41_match_stateid,
9158 .find_root_sec = nfs41_find_root_sec,
9159 .free_lock_state = nfs41_free_lock_state,
9160 .call_sync_ops = &nfs41_call_sync_ops,
9161 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
9162 .alloc_seqid = nfs_alloc_no_seqid,
9163 .session_trunk = nfs4_test_session_trunk,
9164 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9165 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9166 .state_renewal_ops = &nfs41_state_renewal_ops,
9167 .mig_recovery_ops = &nfs41_mig_recovery_ops,
9171 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9172 [0] = &nfs_v4_0_minor_ops,
9173 #if defined(CONFIG_NFS_V4_1)
9174 [1] = &nfs_v4_1_minor_ops,
9176 #if defined(CONFIG_NFS_V4_2)
9177 [2] = &nfs_v4_2_minor_ops,
9181 static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
9183 ssize_t error, error2;
9185 error = generic_listxattr(dentry, list, size);
9193 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9196 return error + error2;
9199 static const struct inode_operations nfs4_dir_inode_operations = {
9200 .create = nfs_create,
9201 .lookup = nfs_lookup,
9202 .atomic_open = nfs_atomic_open,
9204 .unlink = nfs_unlink,
9205 .symlink = nfs_symlink,
9209 .rename = nfs_rename,
9210 .permission = nfs_permission,
9211 .getattr = nfs_getattr,
9212 .setattr = nfs_setattr,
9213 .getxattr = generic_getxattr,
9214 .setxattr = generic_setxattr,
9215 .listxattr = nfs4_listxattr,
9216 .removexattr = generic_removexattr,
9219 static const struct inode_operations nfs4_file_inode_operations = {
9220 .permission = nfs_permission,
9221 .getattr = nfs_getattr,
9222 .setattr = nfs_setattr,
9223 .getxattr = generic_getxattr,
9224 .setxattr = generic_setxattr,
9225 .listxattr = nfs4_listxattr,
9226 .removexattr = generic_removexattr,
9229 const struct nfs_rpc_ops nfs_v4_clientops = {
9230 .version = 4, /* protocol version */
9231 .dentry_ops = &nfs4_dentry_operations,
9232 .dir_inode_ops = &nfs4_dir_inode_operations,
9233 .file_inode_ops = &nfs4_file_inode_operations,
9234 .file_ops = &nfs4_file_operations,
9235 .getroot = nfs4_proc_get_root,
9236 .submount = nfs4_submount,
9237 .try_mount = nfs4_try_mount,
9238 .getattr = nfs4_proc_getattr,
9239 .setattr = nfs4_proc_setattr,
9240 .lookup = nfs4_proc_lookup,
9241 .access = nfs4_proc_access,
9242 .readlink = nfs4_proc_readlink,
9243 .create = nfs4_proc_create,
9244 .remove = nfs4_proc_remove,
9245 .unlink_setup = nfs4_proc_unlink_setup,
9246 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
9247 .unlink_done = nfs4_proc_unlink_done,
9248 .rename_setup = nfs4_proc_rename_setup,
9249 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
9250 .rename_done = nfs4_proc_rename_done,
9251 .link = nfs4_proc_link,
9252 .symlink = nfs4_proc_symlink,
9253 .mkdir = nfs4_proc_mkdir,
9254 .rmdir = nfs4_proc_remove,
9255 .readdir = nfs4_proc_readdir,
9256 .mknod = nfs4_proc_mknod,
9257 .statfs = nfs4_proc_statfs,
9258 .fsinfo = nfs4_proc_fsinfo,
9259 .pathconf = nfs4_proc_pathconf,
9260 .set_capabilities = nfs4_server_capabilities,
9261 .decode_dirent = nfs4_decode_dirent,
9262 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
9263 .read_setup = nfs4_proc_read_setup,
9264 .read_done = nfs4_read_done,
9265 .write_setup = nfs4_proc_write_setup,
9266 .write_done = nfs4_write_done,
9267 .commit_setup = nfs4_proc_commit_setup,
9268 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
9269 .commit_done = nfs4_commit_done,
9270 .lock = nfs4_proc_lock,
9271 .clear_acl_cache = nfs4_zap_acl_attr,
9272 .close_context = nfs4_close_context,
9273 .open_context = nfs4_atomic_open,
9274 .have_delegation = nfs4_have_delegation,
9275 .return_delegation = nfs4_inode_return_delegation,
9276 .alloc_client = nfs4_alloc_client,
9277 .init_client = nfs4_init_client,
9278 .free_client = nfs4_free_client,
9279 .create_server = nfs4_create_server,
9280 .clone_server = nfs_clone_server,
9283 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
9284 .name = XATTR_NAME_NFSV4_ACL,
9285 .list = nfs4_xattr_list_nfs4_acl,
9286 .get = nfs4_xattr_get_nfs4_acl,
9287 .set = nfs4_xattr_set_nfs4_acl,
9290 const struct xattr_handler *nfs4_xattr_handlers[] = {
9291 &nfs4_xattr_nfs4_acl_handler,
9292 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
9293 &nfs4_xattr_nfs4_label_handler,