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 args->sa_slot = slot;
623 rpc_call_start(task);
627 if (args->sa_privileged)
628 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
629 NULL, RPC_PRIORITY_PRIVILEGED);
631 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
632 spin_unlock(&tbl->slot_tbl_lock);
635 EXPORT_SYMBOL_GPL(nfs40_setup_sequence);
637 static int nfs40_sequence_done(struct rpc_task *task,
638 struct nfs4_sequence_res *res)
640 struct nfs4_slot *slot = res->sr_slot;
641 struct nfs4_slot_table *tbl;
647 spin_lock(&tbl->slot_tbl_lock);
648 if (!nfs41_wake_and_assign_slot(tbl, slot))
649 nfs4_free_slot(tbl, slot);
650 spin_unlock(&tbl->slot_tbl_lock);
657 #if defined(CONFIG_NFS_V4_1)
659 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
661 struct nfs4_session *session;
662 struct nfs4_slot_table *tbl;
663 struct nfs4_slot *slot = res->sr_slot;
664 bool send_new_highest_used_slotid = false;
667 session = tbl->session;
669 spin_lock(&tbl->slot_tbl_lock);
670 /* Be nice to the server: try to ensure that the last transmitted
671 * value for highest_user_slotid <= target_highest_slotid
673 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
674 send_new_highest_used_slotid = true;
676 if (nfs41_wake_and_assign_slot(tbl, slot)) {
677 send_new_highest_used_slotid = false;
680 nfs4_free_slot(tbl, slot);
682 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
683 send_new_highest_used_slotid = false;
685 spin_unlock(&tbl->slot_tbl_lock);
687 if (send_new_highest_used_slotid)
688 nfs41_notify_server(session->clp);
691 int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
693 struct nfs4_session *session;
694 struct nfs4_slot *slot = res->sr_slot;
695 struct nfs_client *clp;
696 bool interrupted = false;
701 /* don't increment the sequence number if the task wasn't sent */
702 if (!RPC_WAS_SENT(task))
705 session = slot->table->session;
707 if (slot->interrupted) {
708 slot->interrupted = 0;
712 trace_nfs4_sequence_done(session, res);
713 /* Check the SEQUENCE operation status */
714 switch (res->sr_status) {
716 /* Update the slot's sequence and clientid lease timer */
719 do_renew_lease(clp, res->sr_timestamp);
720 /* Check sequence flags */
721 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
722 nfs41_update_target_slotid(slot->table, slot, res);
726 * sr_status remains 1 if an RPC level error occurred.
727 * The server may or may not have processed the sequence
729 * Mark the slot as having hosted an interrupted RPC call.
731 slot->interrupted = 1;
734 /* The server detected a resend of the RPC call and
735 * returned NFS4ERR_DELAY as per Section 2.10.6.2
738 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
743 case -NFS4ERR_BADSLOT:
745 * The slot id we used was probably retired. Try again
746 * using a different slot id.
749 case -NFS4ERR_SEQ_MISORDERED:
751 * Was the last operation on this sequence interrupted?
752 * If so, retry after bumping the sequence number.
759 * Could this slot have been previously retired?
760 * If so, then the server may be expecting seq_nr = 1!
762 if (slot->seq_nr != 1) {
767 case -NFS4ERR_SEQ_FALSE_RETRY:
771 /* Just update the slot sequence no. */
775 /* The session may be reset by one of the error handlers. */
776 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
777 nfs41_sequence_free_slot(res);
781 if (rpc_restart_call_prepare(task)) {
787 if (!rpc_restart_call(task))
789 rpc_delay(task, NFS4_POLL_RETRY_MAX);
792 EXPORT_SYMBOL_GPL(nfs41_sequence_done);
794 int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
796 if (res->sr_slot == NULL)
798 if (!res->sr_slot->table->session)
799 return nfs40_sequence_done(task, res);
800 return nfs41_sequence_done(task, res);
802 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
804 int nfs41_setup_sequence(struct nfs4_session *session,
805 struct nfs4_sequence_args *args,
806 struct nfs4_sequence_res *res,
807 struct rpc_task *task)
809 struct nfs4_slot *slot;
810 struct nfs4_slot_table *tbl;
812 dprintk("--> %s\n", __func__);
813 /* slot already allocated? */
814 if (res->sr_slot != NULL)
817 tbl = &session->fc_slot_table;
819 task->tk_timeout = 0;
821 spin_lock(&tbl->slot_tbl_lock);
822 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
823 !args->sa_privileged) {
824 /* The state manager will wait until the slot table is empty */
825 dprintk("%s session is draining\n", __func__);
829 slot = nfs4_alloc_slot(tbl);
831 /* If out of memory, try again in 1/4 second */
832 if (slot == ERR_PTR(-ENOMEM))
833 task->tk_timeout = HZ >> 2;
834 dprintk("<-- %s: no free slots\n", __func__);
837 spin_unlock(&tbl->slot_tbl_lock);
839 args->sa_slot = slot;
841 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
842 slot->slot_nr, slot->seq_nr);
845 res->sr_timestamp = jiffies;
846 res->sr_status_flags = 0;
848 * sr_status is only set in decode_sequence, and so will remain
849 * set to 1 if an rpc level failure occurs.
852 trace_nfs4_setup_sequence(session, args);
854 rpc_call_start(task);
857 /* Privileged tasks are queued with top priority */
858 if (args->sa_privileged)
859 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
860 NULL, RPC_PRIORITY_PRIVILEGED);
862 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
863 spin_unlock(&tbl->slot_tbl_lock);
866 EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
868 static int nfs4_setup_sequence(const struct nfs_server *server,
869 struct nfs4_sequence_args *args,
870 struct nfs4_sequence_res *res,
871 struct rpc_task *task)
873 struct nfs4_session *session = nfs4_get_session(server);
877 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
880 dprintk("--> %s clp %p session %p sr_slot %u\n",
881 __func__, session->clp, session, res->sr_slot ?
882 res->sr_slot->slot_nr : NFS4_NO_SLOT);
884 ret = nfs41_setup_sequence(session, args, res, task);
886 dprintk("<-- %s status=%d\n", __func__, ret);
890 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
892 struct nfs4_call_sync_data *data = calldata;
893 struct nfs4_session *session = nfs4_get_session(data->seq_server);
895 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
897 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
900 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
902 struct nfs4_call_sync_data *data = calldata;
904 nfs41_sequence_done(task, data->seq_res);
907 static const struct rpc_call_ops nfs41_call_sync_ops = {
908 .rpc_call_prepare = nfs41_call_sync_prepare,
909 .rpc_call_done = nfs41_call_sync_done,
912 #else /* !CONFIG_NFS_V4_1 */
914 static int nfs4_setup_sequence(const struct nfs_server *server,
915 struct nfs4_sequence_args *args,
916 struct nfs4_sequence_res *res,
917 struct rpc_task *task)
919 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
923 int nfs4_sequence_done(struct rpc_task *task,
924 struct nfs4_sequence_res *res)
926 return nfs40_sequence_done(task, res);
928 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
930 #endif /* !CONFIG_NFS_V4_1 */
932 static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
934 struct nfs4_call_sync_data *data = calldata;
935 nfs4_setup_sequence(data->seq_server,
936 data->seq_args, data->seq_res, task);
939 static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
941 struct nfs4_call_sync_data *data = calldata;
942 nfs4_sequence_done(task, data->seq_res);
945 static const struct rpc_call_ops nfs40_call_sync_ops = {
946 .rpc_call_prepare = nfs40_call_sync_prepare,
947 .rpc_call_done = nfs40_call_sync_done,
950 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
951 struct nfs_server *server,
952 struct rpc_message *msg,
953 struct nfs4_sequence_args *args,
954 struct nfs4_sequence_res *res)
957 struct rpc_task *task;
958 struct nfs_client *clp = server->nfs_client;
959 struct nfs4_call_sync_data data = {
960 .seq_server = server,
964 struct rpc_task_setup task_setup = {
967 .callback_ops = clp->cl_mvops->call_sync_ops,
968 .callback_data = &data
971 task = rpc_run_task(&task_setup);
975 ret = task->tk_status;
981 int nfs4_call_sync(struct rpc_clnt *clnt,
982 struct nfs_server *server,
983 struct rpc_message *msg,
984 struct nfs4_sequence_args *args,
985 struct nfs4_sequence_res *res,
988 nfs4_init_sequence(args, res, cache_reply);
989 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
992 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
994 struct nfs_inode *nfsi = NFS_I(dir);
996 spin_lock(&dir->i_lock);
997 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
998 if (!cinfo->atomic || cinfo->before != dir->i_version)
999 nfs_force_lookup_revalidate(dir);
1000 dir->i_version = cinfo->after;
1001 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
1002 nfs_fscache_invalidate(dir);
1003 spin_unlock(&dir->i_lock);
1006 struct nfs4_opendata {
1008 struct nfs_openargs o_arg;
1009 struct nfs_openres o_res;
1010 struct nfs_open_confirmargs c_arg;
1011 struct nfs_open_confirmres c_res;
1012 struct nfs4_string owner_name;
1013 struct nfs4_string group_name;
1014 struct nfs4_label *a_label;
1015 struct nfs_fattr f_attr;
1016 struct nfs4_label *f_label;
1018 struct dentry *dentry;
1019 struct nfs4_state_owner *owner;
1020 struct nfs4_state *state;
1022 unsigned long timestamp;
1023 unsigned int rpc_done : 1;
1024 unsigned int file_created : 1;
1025 unsigned int is_recover : 1;
1030 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1031 int err, struct nfs4_exception *exception)
1035 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1037 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1038 exception->retry = 1;
1043 nfs4_map_atomic_open_share(struct nfs_server *server,
1044 fmode_t fmode, int openflags)
1048 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1050 res = NFS4_SHARE_ACCESS_READ;
1053 res = NFS4_SHARE_ACCESS_WRITE;
1055 case FMODE_READ|FMODE_WRITE:
1056 res = NFS4_SHARE_ACCESS_BOTH;
1058 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1060 /* Want no delegation if we're using O_DIRECT */
1061 if (openflags & O_DIRECT)
1062 res |= NFS4_SHARE_WANT_NO_DELEG;
1067 static enum open_claim_type4
1068 nfs4_map_atomic_open_claim(struct nfs_server *server,
1069 enum open_claim_type4 claim)
1071 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1076 case NFS4_OPEN_CLAIM_FH:
1077 return NFS4_OPEN_CLAIM_NULL;
1078 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1079 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1080 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1081 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1085 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
1087 p->o_res.f_attr = &p->f_attr;
1088 p->o_res.f_label = p->f_label;
1089 p->o_res.seqid = p->o_arg.seqid;
1090 p->c_res.seqid = p->c_arg.seqid;
1091 p->o_res.server = p->o_arg.server;
1092 p->o_res.access_request = p->o_arg.access;
1093 nfs_fattr_init(&p->f_attr);
1094 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
1097 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
1098 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
1099 const struct iattr *attrs,
1100 struct nfs4_label *label,
1101 enum open_claim_type4 claim,
1104 struct dentry *parent = dget_parent(dentry);
1105 struct inode *dir = d_inode(parent);
1106 struct nfs_server *server = NFS_SERVER(dir);
1107 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
1108 struct nfs4_opendata *p;
1110 p = kzalloc(sizeof(*p), gfp_mask);
1114 p->f_label = nfs4_label_alloc(server, gfp_mask);
1115 if (IS_ERR(p->f_label))
1118 p->a_label = nfs4_label_alloc(server, gfp_mask);
1119 if (IS_ERR(p->a_label))
1122 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1123 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
1124 if (IS_ERR(p->o_arg.seqid))
1125 goto err_free_label;
1126 nfs_sb_active(dentry->d_sb);
1127 p->dentry = dget(dentry);
1130 atomic_inc(&sp->so_count);
1131 p->o_arg.open_flags = flags;
1132 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
1133 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1135 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1136 * will return permission denied for all bits until close */
1137 if (!(flags & O_EXCL)) {
1138 /* ask server to check for all possible rights as results
1140 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1141 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
1143 p->o_arg.clientid = server->nfs_client->cl_clientid;
1144 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1145 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1146 p->o_arg.name = &dentry->d_name;
1147 p->o_arg.server = server;
1148 p->o_arg.bitmask = nfs4_bitmask(server, label);
1149 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1150 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1151 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1152 switch (p->o_arg.claim) {
1153 case NFS4_OPEN_CLAIM_NULL:
1154 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1155 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1156 p->o_arg.fh = NFS_FH(dir);
1158 case NFS4_OPEN_CLAIM_PREVIOUS:
1159 case NFS4_OPEN_CLAIM_FH:
1160 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1161 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1162 p->o_arg.fh = NFS_FH(d_inode(dentry));
1164 if (attrs != NULL && attrs->ia_valid != 0) {
1167 p->o_arg.u.attrs = &p->attrs;
1168 memcpy(&p->attrs, attrs, sizeof(p->attrs));
1171 verf[1] = current->pid;
1172 memcpy(p->o_arg.u.verifier.data, verf,
1173 sizeof(p->o_arg.u.verifier.data));
1175 p->c_arg.fh = &p->o_res.fh;
1176 p->c_arg.stateid = &p->o_res.stateid;
1177 p->c_arg.seqid = p->o_arg.seqid;
1178 nfs4_init_opendata_res(p);
1179 kref_init(&p->kref);
1183 nfs4_label_free(p->a_label);
1185 nfs4_label_free(p->f_label);
1193 static void nfs4_opendata_free(struct kref *kref)
1195 struct nfs4_opendata *p = container_of(kref,
1196 struct nfs4_opendata, kref);
1197 struct super_block *sb = p->dentry->d_sb;
1199 nfs_free_seqid(p->o_arg.seqid);
1200 if (p->state != NULL)
1201 nfs4_put_open_state(p->state);
1202 nfs4_put_state_owner(p->owner);
1204 nfs4_label_free(p->a_label);
1205 nfs4_label_free(p->f_label);
1209 nfs_sb_deactive(sb);
1210 nfs_fattr_free_names(&p->f_attr);
1211 kfree(p->f_attr.mdsthreshold);
1215 static void nfs4_opendata_put(struct nfs4_opendata *p)
1218 kref_put(&p->kref, nfs4_opendata_free);
1221 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1225 ret = rpc_wait_for_completion_task(task);
1229 static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1232 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1233 case FMODE_READ|FMODE_WRITE:
1234 return state->n_rdwr != 0;
1236 return state->n_wronly != 0;
1238 return state->n_rdonly != 0;
1244 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
1248 if (open_mode & (O_EXCL|O_TRUNC))
1250 switch (mode & (FMODE_READ|FMODE_WRITE)) {
1252 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1253 && state->n_rdonly != 0;
1256 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1257 && state->n_wronly != 0;
1259 case FMODE_READ|FMODE_WRITE:
1260 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1261 && state->n_rdwr != 0;
1267 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1268 enum open_claim_type4 claim)
1270 if (delegation == NULL)
1272 if ((delegation->type & fmode) != fmode)
1274 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1277 case NFS4_OPEN_CLAIM_NULL:
1278 case NFS4_OPEN_CLAIM_FH:
1280 case NFS4_OPEN_CLAIM_PREVIOUS:
1281 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1286 nfs_mark_delegation_referenced(delegation);
1290 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1299 case FMODE_READ|FMODE_WRITE:
1302 nfs4_state_set_mode_locked(state, state->state | fmode);
1305 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
1307 struct nfs_client *clp = state->owner->so_server->nfs_client;
1308 bool need_recover = false;
1310 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1311 need_recover = true;
1312 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1313 need_recover = true;
1314 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1315 need_recover = true;
1317 nfs4_state_mark_reclaim_nograce(clp, state);
1320 static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1321 nfs4_stateid *stateid)
1323 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1325 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1326 nfs_test_and_clear_all_open_stateid(state);
1329 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1334 static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1336 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1338 if (state->n_wronly)
1339 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1340 if (state->n_rdonly)
1341 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1343 set_bit(NFS_O_RDWR_STATE, &state->flags);
1344 set_bit(NFS_OPEN_STATE, &state->flags);
1347 static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1348 nfs4_stateid *arg_stateid,
1349 nfs4_stateid *stateid, fmode_t fmode)
1351 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1352 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1354 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1357 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1360 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1361 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1362 clear_bit(NFS_OPEN_STATE, &state->flags);
1364 if (stateid == NULL)
1366 /* Handle races with OPEN */
1367 if (!nfs4_stateid_match_other(arg_stateid, &state->open_stateid) ||
1368 (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1369 !nfs4_stateid_is_newer(stateid, &state->open_stateid))) {
1370 nfs_resync_open_stateid_locked(state);
1373 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1374 nfs4_stateid_copy(&state->stateid, stateid);
1375 nfs4_stateid_copy(&state->open_stateid, stateid);
1378 static void nfs_clear_open_stateid(struct nfs4_state *state,
1379 nfs4_stateid *arg_stateid,
1380 nfs4_stateid *stateid, fmode_t fmode)
1382 write_seqlock(&state->seqlock);
1383 nfs_clear_open_stateid_locked(state, arg_stateid, stateid, fmode);
1384 write_sequnlock(&state->seqlock);
1385 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1386 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1389 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1393 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1396 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1398 case FMODE_READ|FMODE_WRITE:
1399 set_bit(NFS_O_RDWR_STATE, &state->flags);
1401 if (!nfs_need_update_open_stateid(state, stateid))
1403 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1404 nfs4_stateid_copy(&state->stateid, stateid);
1405 nfs4_stateid_copy(&state->open_stateid, stateid);
1408 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
1411 * Protect the call to nfs4_state_set_mode_locked and
1412 * serialise the stateid update
1414 spin_lock(&state->owner->so_lock);
1415 write_seqlock(&state->seqlock);
1416 if (deleg_stateid != NULL) {
1417 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1418 set_bit(NFS_DELEGATED_STATE, &state->flags);
1420 if (open_stateid != NULL)
1421 nfs_set_open_stateid_locked(state, open_stateid, fmode);
1422 write_sequnlock(&state->seqlock);
1423 update_open_stateflags(state, fmode);
1424 spin_unlock(&state->owner->so_lock);
1427 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
1429 struct nfs_inode *nfsi = NFS_I(state->inode);
1430 struct nfs_delegation *deleg_cur;
1433 fmode &= (FMODE_READ|FMODE_WRITE);
1436 deleg_cur = rcu_dereference(nfsi->delegation);
1437 if (deleg_cur == NULL)
1440 spin_lock(&deleg_cur->lock);
1441 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1442 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1443 (deleg_cur->type & fmode) != fmode)
1444 goto no_delegation_unlock;
1446 if (delegation == NULL)
1447 delegation = &deleg_cur->stateid;
1448 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
1449 goto no_delegation_unlock;
1451 nfs_mark_delegation_referenced(deleg_cur);
1452 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
1454 no_delegation_unlock:
1455 spin_unlock(&deleg_cur->lock);
1459 if (!ret && open_stateid != NULL) {
1460 __update_open_stateid(state, open_stateid, NULL, fmode);
1463 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1464 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1469 static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1470 const nfs4_stateid *stateid)
1472 struct nfs4_state *state = lsp->ls_state;
1475 spin_lock(&state->state_lock);
1476 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1478 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1480 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1483 spin_unlock(&state->state_lock);
1487 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1489 struct nfs_delegation *delegation;
1492 delegation = rcu_dereference(NFS_I(inode)->delegation);
1493 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1498 nfs4_inode_return_delegation(inode);
1501 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1503 struct nfs4_state *state = opendata->state;
1504 struct nfs_inode *nfsi = NFS_I(state->inode);
1505 struct nfs_delegation *delegation;
1506 int open_mode = opendata->o_arg.open_flags;
1507 fmode_t fmode = opendata->o_arg.fmode;
1508 enum open_claim_type4 claim = opendata->o_arg.claim;
1509 nfs4_stateid stateid;
1513 spin_lock(&state->owner->so_lock);
1514 if (can_open_cached(state, fmode, open_mode)) {
1515 update_open_stateflags(state, fmode);
1516 spin_unlock(&state->owner->so_lock);
1517 goto out_return_state;
1519 spin_unlock(&state->owner->so_lock);
1521 delegation = rcu_dereference(nfsi->delegation);
1522 if (!can_open_delegated(delegation, fmode, claim)) {
1526 /* Save the delegation */
1527 nfs4_stateid_copy(&stateid, &delegation->stateid);
1529 nfs_release_seqid(opendata->o_arg.seqid);
1530 if (!opendata->is_recover) {
1531 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1537 /* Try to update the stateid using the delegation */
1538 if (update_open_stateid(state, NULL, &stateid, fmode))
1539 goto out_return_state;
1542 return ERR_PTR(ret);
1544 atomic_inc(&state->count);
1549 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1551 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1552 struct nfs_delegation *delegation;
1553 int delegation_flags = 0;
1556 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1558 delegation_flags = delegation->flags;
1560 switch (data->o_arg.claim) {
1563 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1564 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1565 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1566 "returning a delegation for "
1567 "OPEN(CLAIM_DELEGATE_CUR)\n",
1571 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1572 nfs_inode_set_delegation(state->inode,
1573 data->owner->so_cred,
1576 nfs_inode_reclaim_delegation(state->inode,
1577 data->owner->so_cred,
1582 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1583 * and update the nfs4_state.
1585 static struct nfs4_state *
1586 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1588 struct inode *inode = data->state->inode;
1589 struct nfs4_state *state = data->state;
1592 if (!data->rpc_done) {
1593 if (data->rpc_status) {
1594 ret = data->rpc_status;
1597 /* cached opens have already been processed */
1601 ret = nfs_refresh_inode(inode, &data->f_attr);
1605 if (data->o_res.delegation_type != 0)
1606 nfs4_opendata_check_deleg(data, state);
1608 update_open_stateid(state, &data->o_res.stateid, NULL,
1610 atomic_inc(&state->count);
1614 return ERR_PTR(ret);
1618 static struct nfs4_state *
1619 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1621 struct inode *inode;
1622 struct nfs4_state *state = NULL;
1625 if (!data->rpc_done) {
1626 state = nfs4_try_open_cached(data);
1627 trace_nfs4_cached_open(data->state);
1632 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1634 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
1635 ret = PTR_ERR(inode);
1639 state = nfs4_get_open_state(inode, data->owner);
1642 if (data->o_res.delegation_type != 0)
1643 nfs4_opendata_check_deleg(data, state);
1644 update_open_stateid(state, &data->o_res.stateid, NULL,
1648 nfs_release_seqid(data->o_arg.seqid);
1653 return ERR_PTR(ret);
1656 static struct nfs4_state *
1657 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1659 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1660 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1661 return _nfs4_opendata_to_nfs4_state(data);
1664 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1666 struct nfs_inode *nfsi = NFS_I(state->inode);
1667 struct nfs_open_context *ctx;
1669 spin_lock(&state->inode->i_lock);
1670 list_for_each_entry(ctx, &nfsi->open_files, list) {
1671 if (ctx->state != state)
1673 get_nfs_open_context(ctx);
1674 spin_unlock(&state->inode->i_lock);
1677 spin_unlock(&state->inode->i_lock);
1678 return ERR_PTR(-ENOENT);
1681 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1682 struct nfs4_state *state, enum open_claim_type4 claim)
1684 struct nfs4_opendata *opendata;
1686 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
1687 NULL, NULL, claim, GFP_NOFS);
1688 if (opendata == NULL)
1689 return ERR_PTR(-ENOMEM);
1690 opendata->state = state;
1691 atomic_inc(&state->count);
1695 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1698 struct nfs4_state *newstate;
1701 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
1703 opendata->o_arg.open_flags = 0;
1704 opendata->o_arg.fmode = fmode;
1705 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1706 NFS_SB(opendata->dentry->d_sb),
1708 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1709 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1710 nfs4_init_opendata_res(opendata);
1711 ret = _nfs4_recover_proc_open(opendata);
1714 newstate = nfs4_opendata_to_nfs4_state(opendata);
1715 if (IS_ERR(newstate))
1716 return PTR_ERR(newstate);
1717 if (newstate != opendata->state)
1719 nfs4_close_state(newstate, fmode);
1723 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1727 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1728 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1729 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1730 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1731 /* memory barrier prior to reading state->n_* */
1732 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1733 clear_bit(NFS_OPEN_STATE, &state->flags);
1735 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1738 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1741 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1745 * We may have performed cached opens for all three recoveries.
1746 * Check if we need to update the current stateid.
1748 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1749 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
1750 write_seqlock(&state->seqlock);
1751 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1752 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1753 write_sequnlock(&state->seqlock);
1760 * reclaim state on the server after a reboot.
1762 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1764 struct nfs_delegation *delegation;
1765 struct nfs4_opendata *opendata;
1766 fmode_t delegation_type = 0;
1769 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1770 NFS4_OPEN_CLAIM_PREVIOUS);
1771 if (IS_ERR(opendata))
1772 return PTR_ERR(opendata);
1774 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1775 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1776 delegation_type = delegation->type;
1778 opendata->o_arg.u.delegation_type = delegation_type;
1779 status = nfs4_open_recover(opendata, state);
1780 nfs4_opendata_put(opendata);
1784 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1786 struct nfs_server *server = NFS_SERVER(state->inode);
1787 struct nfs4_exception exception = { };
1790 err = _nfs4_do_open_reclaim(ctx, state);
1791 trace_nfs4_open_reclaim(ctx, 0, err);
1792 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1794 if (err != -NFS4ERR_DELAY)
1796 nfs4_handle_exception(server, err, &exception);
1797 } while (exception.retry);
1801 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1803 struct nfs_open_context *ctx;
1806 ctx = nfs4_state_find_open_context(state);
1809 ret = nfs4_do_open_reclaim(ctx, state);
1810 put_nfs_open_context(ctx);
1814 static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, int err)
1818 printk(KERN_ERR "NFS: %s: unhandled error "
1819 "%d.\n", __func__, err);
1825 case -NFS4ERR_BADSESSION:
1826 case -NFS4ERR_BADSLOT:
1827 case -NFS4ERR_BAD_HIGH_SLOT:
1828 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1829 case -NFS4ERR_DEADSESSION:
1830 set_bit(NFS_DELEGATED_STATE, &state->flags);
1831 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1833 case -NFS4ERR_STALE_CLIENTID:
1834 case -NFS4ERR_STALE_STATEID:
1835 set_bit(NFS_DELEGATED_STATE, &state->flags);
1836 case -NFS4ERR_EXPIRED:
1837 /* Don't recall a delegation if it was lost */
1838 nfs4_schedule_lease_recovery(server->nfs_client);
1840 case -NFS4ERR_MOVED:
1841 nfs4_schedule_migration_recovery(server);
1843 case -NFS4ERR_LEASE_MOVED:
1844 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1846 case -NFS4ERR_DELEG_REVOKED:
1847 case -NFS4ERR_ADMIN_REVOKED:
1848 case -NFS4ERR_BAD_STATEID:
1849 case -NFS4ERR_OPENMODE:
1850 nfs_inode_find_state_and_recover(state->inode,
1852 nfs4_schedule_stateid_recovery(server, state);
1854 case -NFS4ERR_DELAY:
1855 case -NFS4ERR_GRACE:
1856 set_bit(NFS_DELEGATED_STATE, &state->flags);
1860 case -NFS4ERR_DENIED:
1861 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1867 int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
1868 struct nfs4_state *state, const nfs4_stateid *stateid,
1871 struct nfs_server *server = NFS_SERVER(state->inode);
1872 struct nfs4_opendata *opendata;
1875 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1876 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1877 if (IS_ERR(opendata))
1878 return PTR_ERR(opendata);
1879 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1880 write_seqlock(&state->seqlock);
1881 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1882 write_sequnlock(&state->seqlock);
1883 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1884 switch (type & (FMODE_READ|FMODE_WRITE)) {
1885 case FMODE_READ|FMODE_WRITE:
1887 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1890 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1894 err = nfs4_open_recover_helper(opendata, FMODE_READ);
1896 nfs4_opendata_put(opendata);
1897 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1900 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1902 struct nfs4_opendata *data = calldata;
1904 nfs40_setup_sequence(data->o_arg.server->nfs_client->cl_slot_tbl,
1905 &data->c_arg.seq_args, &data->c_res.seq_res, task);
1908 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1910 struct nfs4_opendata *data = calldata;
1912 nfs40_sequence_done(task, &data->c_res.seq_res);
1914 data->rpc_status = task->tk_status;
1915 if (data->rpc_status == 0) {
1916 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
1917 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1918 renew_lease(data->o_res.server, data->timestamp);
1923 static void nfs4_open_confirm_release(void *calldata)
1925 struct nfs4_opendata *data = calldata;
1926 struct nfs4_state *state = NULL;
1928 /* If this request hasn't been cancelled, do nothing */
1929 if (data->cancelled == 0)
1931 /* In case of error, no cleanup! */
1932 if (!data->rpc_done)
1934 state = nfs4_opendata_to_nfs4_state(data);
1936 nfs4_close_state(state, data->o_arg.fmode);
1938 nfs4_opendata_put(data);
1941 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1942 .rpc_call_prepare = nfs4_open_confirm_prepare,
1943 .rpc_call_done = nfs4_open_confirm_done,
1944 .rpc_release = nfs4_open_confirm_release,
1948 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1950 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1952 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
1953 struct rpc_task *task;
1954 struct rpc_message msg = {
1955 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1956 .rpc_argp = &data->c_arg,
1957 .rpc_resp = &data->c_res,
1958 .rpc_cred = data->owner->so_cred,
1960 struct rpc_task_setup task_setup_data = {
1961 .rpc_client = server->client,
1962 .rpc_message = &msg,
1963 .callback_ops = &nfs4_open_confirm_ops,
1964 .callback_data = data,
1965 .workqueue = nfsiod_workqueue,
1966 .flags = RPC_TASK_ASYNC,
1970 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
1971 kref_get(&data->kref);
1973 data->rpc_status = 0;
1974 data->timestamp = jiffies;
1975 if (data->is_recover)
1976 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
1977 task = rpc_run_task(&task_setup_data);
1979 return PTR_ERR(task);
1980 status = nfs4_wait_for_completion_rpc_task(task);
1982 data->cancelled = 1;
1985 status = data->rpc_status;
1990 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1992 struct nfs4_opendata *data = calldata;
1993 struct nfs4_state_owner *sp = data->owner;
1994 struct nfs_client *clp = sp->so_server->nfs_client;
1995 enum open_claim_type4 claim = data->o_arg.claim;
1997 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
2000 * Check if we still need to send an OPEN call, or if we can use
2001 * a delegation instead.
2003 if (data->state != NULL) {
2004 struct nfs_delegation *delegation;
2006 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
2009 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
2010 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
2011 goto unlock_no_action;
2014 /* Update client id. */
2015 data->o_arg.clientid = clp->cl_clientid;
2019 case NFS4_OPEN_CLAIM_PREVIOUS:
2020 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2021 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
2022 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
2023 case NFS4_OPEN_CLAIM_FH:
2024 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
2025 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
2027 data->timestamp = jiffies;
2028 if (nfs4_setup_sequence(data->o_arg.server,
2029 &data->o_arg.seq_args,
2030 &data->o_res.seq_res,
2032 nfs_release_seqid(data->o_arg.seqid);
2034 /* Set the create mode (note dependency on the session type) */
2035 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2036 if (data->o_arg.open_flags & O_EXCL) {
2037 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2038 if (nfs4_has_persistent_session(clp))
2039 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2040 else if (clp->cl_mvops->minor_version > 0)
2041 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2045 trace_nfs4_cached_open(data->state);
2048 task->tk_action = NULL;
2050 nfs4_sequence_done(task, &data->o_res.seq_res);
2053 static void nfs4_open_done(struct rpc_task *task, void *calldata)
2055 struct nfs4_opendata *data = calldata;
2057 data->rpc_status = task->tk_status;
2059 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
2062 if (task->tk_status == 0) {
2063 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2064 switch (data->o_res.f_attr->mode & S_IFMT) {
2068 data->rpc_status = -ELOOP;
2071 data->rpc_status = -EISDIR;
2074 data->rpc_status = -ENOTDIR;
2077 renew_lease(data->o_res.server, data->timestamp);
2078 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2079 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2084 static void nfs4_open_release(void *calldata)
2086 struct nfs4_opendata *data = calldata;
2087 struct nfs4_state *state = NULL;
2089 /* If this request hasn't been cancelled, do nothing */
2090 if (data->cancelled == 0)
2092 /* In case of error, no cleanup! */
2093 if (data->rpc_status != 0 || !data->rpc_done)
2095 /* In case we need an open_confirm, no cleanup! */
2096 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2098 state = nfs4_opendata_to_nfs4_state(data);
2100 nfs4_close_state(state, data->o_arg.fmode);
2102 nfs4_opendata_put(data);
2105 static const struct rpc_call_ops nfs4_open_ops = {
2106 .rpc_call_prepare = nfs4_open_prepare,
2107 .rpc_call_done = nfs4_open_done,
2108 .rpc_release = nfs4_open_release,
2111 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
2113 struct inode *dir = d_inode(data->dir);
2114 struct nfs_server *server = NFS_SERVER(dir);
2115 struct nfs_openargs *o_arg = &data->o_arg;
2116 struct nfs_openres *o_res = &data->o_res;
2117 struct rpc_task *task;
2118 struct rpc_message msg = {
2119 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2122 .rpc_cred = data->owner->so_cred,
2124 struct rpc_task_setup task_setup_data = {
2125 .rpc_client = server->client,
2126 .rpc_message = &msg,
2127 .callback_ops = &nfs4_open_ops,
2128 .callback_data = data,
2129 .workqueue = nfsiod_workqueue,
2130 .flags = RPC_TASK_ASYNC,
2134 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
2135 kref_get(&data->kref);
2137 data->rpc_status = 0;
2138 data->cancelled = 0;
2139 data->is_recover = 0;
2141 nfs4_set_sequence_privileged(&o_arg->seq_args);
2142 data->is_recover = 1;
2144 task = rpc_run_task(&task_setup_data);
2146 return PTR_ERR(task);
2147 status = nfs4_wait_for_completion_rpc_task(task);
2149 data->cancelled = 1;
2152 status = data->rpc_status;
2158 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2160 struct inode *dir = d_inode(data->dir);
2161 struct nfs_openres *o_res = &data->o_res;
2164 status = nfs4_run_open_task(data, 1);
2165 if (status != 0 || !data->rpc_done)
2168 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2170 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2171 status = _nfs4_proc_open_confirm(data);
2180 * Additional permission checks in order to distinguish between an
2181 * open for read, and an open for execute. This works around the
2182 * fact that NFSv4 OPEN treats read and execute permissions as being
2184 * Note that in the non-execute case, we want to turn off permission
2185 * checking if we just created a new file (POSIX open() semantics).
2187 static int nfs4_opendata_access(struct rpc_cred *cred,
2188 struct nfs4_opendata *opendata,
2189 struct nfs4_state *state, fmode_t fmode,
2192 struct nfs_access_entry cache;
2195 /* access call failed or for some reason the server doesn't
2196 * support any access modes -- defer access call until later */
2197 if (opendata->o_res.access_supported == 0)
2202 * Use openflags to check for exec, because fmode won't
2203 * always have FMODE_EXEC set when file open for exec.
2205 if (openflags & __FMODE_EXEC) {
2206 /* ONLY check for exec rights */
2208 } else if ((fmode & FMODE_READ) && !opendata->file_created)
2212 cache.jiffies = jiffies;
2213 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2214 nfs_access_add_cache(state->inode, &cache);
2216 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
2219 /* even though OPEN succeeded, access is denied. Close the file */
2220 nfs4_close_state(state, fmode);
2225 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2227 static int _nfs4_proc_open(struct nfs4_opendata *data)
2229 struct inode *dir = d_inode(data->dir);
2230 struct nfs_server *server = NFS_SERVER(dir);
2231 struct nfs_openargs *o_arg = &data->o_arg;
2232 struct nfs_openres *o_res = &data->o_res;
2235 status = nfs4_run_open_task(data, 0);
2236 if (!data->rpc_done)
2239 if (status == -NFS4ERR_BADNAME &&
2240 !(o_arg->open_flags & O_CREAT))
2245 nfs_fattr_map_and_free_names(server, &data->f_attr);
2247 if (o_arg->open_flags & O_CREAT) {
2248 update_changeattr(dir, &o_res->cinfo);
2249 if (o_arg->open_flags & O_EXCL)
2250 data->file_created = 1;
2251 else if (o_res->cinfo.before != o_res->cinfo.after)
2252 data->file_created = 1;
2254 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2255 server->caps &= ~NFS_CAP_POSIX_LOCK;
2256 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2257 status = _nfs4_proc_open_confirm(data);
2261 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
2262 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
2266 static int nfs4_recover_expired_lease(struct nfs_server *server)
2268 return nfs4_client_recover_expired_lease(server->nfs_client);
2273 * reclaim state on the server after a network partition.
2274 * Assumes caller holds the appropriate lock
2276 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2278 struct nfs4_opendata *opendata;
2281 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2282 NFS4_OPEN_CLAIM_FH);
2283 if (IS_ERR(opendata))
2284 return PTR_ERR(opendata);
2285 ret = nfs4_open_recover(opendata, state);
2287 d_drop(ctx->dentry);
2288 nfs4_opendata_put(opendata);
2292 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2294 struct nfs_server *server = NFS_SERVER(state->inode);
2295 struct nfs4_exception exception = { };
2299 err = _nfs4_open_expired(ctx, state);
2300 trace_nfs4_open_expired(ctx, 0, err);
2301 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2306 case -NFS4ERR_GRACE:
2307 case -NFS4ERR_DELAY:
2308 nfs4_handle_exception(server, err, &exception);
2311 } while (exception.retry);
2316 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2318 struct nfs_open_context *ctx;
2321 ctx = nfs4_state_find_open_context(state);
2324 ret = nfs4_do_open_expired(ctx, state);
2325 put_nfs_open_context(ctx);
2329 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state)
2331 nfs_remove_bad_delegation(state->inode);
2332 write_seqlock(&state->seqlock);
2333 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2334 write_sequnlock(&state->seqlock);
2335 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2338 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2340 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2341 nfs_finish_clear_delegation_stateid(state);
2344 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2346 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2347 nfs40_clear_delegation_stateid(state);
2348 return nfs4_open_expired(sp, state);
2351 #if defined(CONFIG_NFS_V4_1)
2352 static void nfs41_check_delegation_stateid(struct nfs4_state *state)
2354 struct nfs_server *server = NFS_SERVER(state->inode);
2355 nfs4_stateid stateid;
2356 struct nfs_delegation *delegation;
2357 struct rpc_cred *cred;
2360 /* Get the delegation credential for use by test/free_stateid */
2362 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2363 if (delegation == NULL) {
2368 nfs4_stateid_copy(&stateid, &delegation->stateid);
2369 cred = get_rpccred(delegation->cred);
2371 status = nfs41_test_stateid(server, &stateid, cred);
2372 trace_nfs4_test_delegation_stateid(state, NULL, status);
2374 if (status != NFS_OK) {
2375 /* Free the stateid unless the server explicitly
2376 * informs us the stateid is unrecognized. */
2377 if (status != -NFS4ERR_BAD_STATEID)
2378 nfs41_free_stateid(server, &stateid, cred);
2379 nfs_finish_clear_delegation_stateid(state);
2386 * nfs41_check_open_stateid - possibly free an open stateid
2388 * @state: NFSv4 state for an inode
2390 * Returns NFS_OK if recovery for this stateid is now finished.
2391 * Otherwise a negative NFS4ERR value is returned.
2393 static int nfs41_check_open_stateid(struct nfs4_state *state)
2395 struct nfs_server *server = NFS_SERVER(state->inode);
2396 nfs4_stateid *stateid = &state->open_stateid;
2397 struct rpc_cred *cred = state->owner->so_cred;
2400 /* If a state reset has been done, test_stateid is unneeded */
2401 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2402 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2403 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2404 return -NFS4ERR_BAD_STATEID;
2406 status = nfs41_test_stateid(server, stateid, cred);
2407 trace_nfs4_test_open_stateid(state, NULL, status);
2408 if (status != NFS_OK) {
2409 /* Free the stateid unless the server explicitly
2410 * informs us the stateid is unrecognized. */
2411 if (status != -NFS4ERR_BAD_STATEID)
2412 nfs41_free_stateid(server, stateid, cred);
2414 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2415 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2416 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2417 clear_bit(NFS_OPEN_STATE, &state->flags);
2422 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2426 nfs41_check_delegation_stateid(state);
2427 status = nfs41_check_open_stateid(state);
2428 if (status != NFS_OK)
2429 status = nfs4_open_expired(sp, state);
2435 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2436 * fields corresponding to attributes that were used to store the verifier.
2437 * Make sure we clobber those fields in the later setattr call
2439 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2440 struct iattr *sattr, struct nfs4_label **label)
2442 const u32 *attrset = opendata->o_res.attrset;
2444 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2445 !(sattr->ia_valid & ATTR_ATIME_SET))
2446 sattr->ia_valid |= ATTR_ATIME;
2448 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2449 !(sattr->ia_valid & ATTR_MTIME_SET))
2450 sattr->ia_valid |= ATTR_MTIME;
2452 /* Except MODE, it seems harmless of setting twice. */
2453 if ((attrset[1] & FATTR4_WORD1_MODE))
2454 sattr->ia_valid &= ~ATTR_MODE;
2456 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2460 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2463 struct nfs_open_context *ctx)
2465 struct nfs4_state_owner *sp = opendata->owner;
2466 struct nfs_server *server = sp->so_server;
2467 struct dentry *dentry;
2468 struct nfs4_state *state;
2472 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2474 ret = _nfs4_proc_open(opendata);
2478 state = nfs4_opendata_to_nfs4_state(opendata);
2479 ret = PTR_ERR(state);
2482 if (server->caps & NFS_CAP_POSIX_LOCK)
2483 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2485 dentry = opendata->dentry;
2486 if (d_really_is_negative(dentry)) {
2487 struct dentry *alias;
2489 alias = d_exact_alias(dentry, state->inode);
2491 alias = d_splice_alias(igrab(state->inode), dentry);
2492 /* d_splice_alias() can't fail here - it's a non-directory */
2495 ctx->dentry = dentry = alias;
2497 nfs_set_verifier(dentry,
2498 nfs_save_change_attribute(d_inode(opendata->dir)));
2501 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2506 if (d_inode(dentry) == state->inode) {
2507 nfs_inode_attach_open_context(ctx);
2508 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2509 nfs4_schedule_stateid_recovery(server, state);
2516 * Returns a referenced nfs4_state
2518 static int _nfs4_do_open(struct inode *dir,
2519 struct nfs_open_context *ctx,
2521 struct iattr *sattr,
2522 struct nfs4_label *label,
2525 struct nfs4_state_owner *sp;
2526 struct nfs4_state *state = NULL;
2527 struct nfs_server *server = NFS_SERVER(dir);
2528 struct nfs4_opendata *opendata;
2529 struct dentry *dentry = ctx->dentry;
2530 struct rpc_cred *cred = ctx->cred;
2531 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2532 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
2533 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
2534 struct nfs4_label *olabel = NULL;
2537 /* Protect against reboot recovery conflicts */
2539 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2541 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2544 status = nfs4_recover_expired_lease(server);
2546 goto err_put_state_owner;
2547 if (d_really_is_positive(dentry))
2548 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
2550 if (d_really_is_positive(dentry))
2551 claim = NFS4_OPEN_CLAIM_FH;
2552 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
2553 label, claim, GFP_KERNEL);
2554 if (opendata == NULL)
2555 goto err_put_state_owner;
2558 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2559 if (IS_ERR(olabel)) {
2560 status = PTR_ERR(olabel);
2561 goto err_opendata_put;
2565 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2566 if (!opendata->f_attr.mdsthreshold) {
2567 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2568 if (!opendata->f_attr.mdsthreshold)
2569 goto err_free_label;
2571 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
2573 if (d_really_is_positive(dentry))
2574 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
2576 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
2578 goto err_free_label;
2581 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
2582 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
2583 nfs4_exclusive_attrset(opendata, sattr, &label);
2585 * send create attributes which was not set by open
2586 * with an extra setattr.
2588 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2589 nfs_fattr_init(opendata->o_res.f_attr);
2590 status = nfs4_do_setattr(state->inode, cred,
2591 opendata->o_res.f_attr, sattr,
2592 state, label, olabel);
2594 nfs_setattr_update_inode(state->inode, sattr,
2595 opendata->o_res.f_attr);
2596 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2600 if (opened && opendata->file_created)
2601 *opened |= FILE_CREATED;
2603 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
2604 *ctx_th = opendata->f_attr.mdsthreshold;
2605 opendata->f_attr.mdsthreshold = NULL;
2608 nfs4_label_free(olabel);
2610 nfs4_opendata_put(opendata);
2611 nfs4_put_state_owner(sp);
2614 nfs4_label_free(olabel);
2616 nfs4_opendata_put(opendata);
2617 err_put_state_owner:
2618 nfs4_put_state_owner(sp);
2624 static struct nfs4_state *nfs4_do_open(struct inode *dir,
2625 struct nfs_open_context *ctx,
2627 struct iattr *sattr,
2628 struct nfs4_label *label,
2631 struct nfs_server *server = NFS_SERVER(dir);
2632 struct nfs4_exception exception = { };
2633 struct nfs4_state *res;
2637 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
2639 trace_nfs4_open_file(ctx, flags, status);
2642 /* NOTE: BAD_SEQID means the server and client disagree about the
2643 * book-keeping w.r.t. state-changing operations
2644 * (OPEN/CLOSE/LOCK/LOCKU...)
2645 * It is actually a sign of a bug on the client or on the server.
2647 * If we receive a BAD_SEQID error in the particular case of
2648 * doing an OPEN, we assume that nfs_increment_open_seqid() will
2649 * have unhashed the old state_owner for us, and that we can
2650 * therefore safely retry using a new one. We should still warn
2651 * the user though...
2653 if (status == -NFS4ERR_BAD_SEQID) {
2654 pr_warn_ratelimited("NFS: v4 server %s "
2655 " returned a bad sequence-id error!\n",
2656 NFS_SERVER(dir)->nfs_client->cl_hostname);
2657 exception.retry = 1;
2661 * BAD_STATEID on OPEN means that the server cancelled our
2662 * state before it received the OPEN_CONFIRM.
2663 * Recover by retrying the request as per the discussion
2664 * on Page 181 of RFC3530.
2666 if (status == -NFS4ERR_BAD_STATEID) {
2667 exception.retry = 1;
2670 if (status == -EAGAIN) {
2671 /* We must have found a delegation */
2672 exception.retry = 1;
2675 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2677 res = ERR_PTR(nfs4_handle_exception(server,
2678 status, &exception));
2679 } while (exception.retry);
2683 static int _nfs4_do_setattr(struct inode *inode,
2684 struct nfs_setattrargs *arg,
2685 struct nfs_setattrres *res,
2686 struct rpc_cred *cred,
2687 struct nfs4_state *state)
2689 struct nfs_server *server = NFS_SERVER(inode);
2690 struct rpc_message msg = {
2691 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2696 struct rpc_cred *delegation_cred = NULL;
2697 unsigned long timestamp = jiffies;
2702 nfs_fattr_init(res->fattr);
2704 /* Servers should only apply open mode checks for file size changes */
2705 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
2706 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2708 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
2709 /* Use that stateid */
2710 } else if (truncate && state != NULL) {
2711 struct nfs_lockowner lockowner = {
2712 .l_owner = current->files,
2713 .l_pid = current->tgid,
2715 if (!nfs4_valid_open_stateid(state))
2717 if (nfs4_select_rw_stateid(state, FMODE_WRITE, &lockowner,
2718 &arg->stateid, &delegation_cred) == -EIO)
2721 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
2722 if (delegation_cred)
2723 msg.rpc_cred = delegation_cred;
2725 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
2727 put_rpccred(delegation_cred);
2728 if (status == 0 && state != NULL)
2729 renew_lease(server, timestamp);
2730 trace_nfs4_setattr(inode, &arg->stateid, status);
2734 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2735 struct nfs_fattr *fattr, struct iattr *sattr,
2736 struct nfs4_state *state, struct nfs4_label *ilabel,
2737 struct nfs4_label *olabel)
2739 struct nfs_server *server = NFS_SERVER(inode);
2740 struct nfs_setattrargs arg = {
2741 .fh = NFS_FH(inode),
2744 .bitmask = server->attr_bitmask,
2747 struct nfs_setattrres res = {
2752 struct nfs4_exception exception = {
2755 .stateid = &arg.stateid,
2759 arg.bitmask = nfs4_bitmask(server, ilabel);
2761 arg.bitmask = nfs4_bitmask(server, olabel);
2764 err = _nfs4_do_setattr(inode, &arg, &res, cred, state);
2766 case -NFS4ERR_OPENMODE:
2767 if (!(sattr->ia_valid & ATTR_SIZE)) {
2768 pr_warn_once("NFSv4: server %s is incorrectly "
2769 "applying open mode checks to "
2770 "a SETATTR that is not "
2771 "changing file size.\n",
2772 server->nfs_client->cl_hostname);
2774 if (state && !(state->state & FMODE_WRITE)) {
2776 if (sattr->ia_valid & ATTR_OPEN)
2781 err = nfs4_handle_exception(server, err, &exception);
2782 } while (exception.retry);
2788 nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
2790 if (inode == NULL || !nfs_have_layout(inode))
2793 return pnfs_wait_on_layoutreturn(inode, task);
2796 struct nfs4_closedata {
2797 struct inode *inode;
2798 struct nfs4_state *state;
2799 struct nfs_closeargs arg;
2800 struct nfs_closeres res;
2801 struct nfs_fattr fattr;
2802 unsigned long timestamp;
2807 static void nfs4_free_closedata(void *data)
2809 struct nfs4_closedata *calldata = data;
2810 struct nfs4_state_owner *sp = calldata->state->owner;
2811 struct super_block *sb = calldata->state->inode->i_sb;
2814 pnfs_roc_release(calldata->state->inode);
2815 nfs4_put_open_state(calldata->state);
2816 nfs_free_seqid(calldata->arg.seqid);
2817 nfs4_put_state_owner(sp);
2818 nfs_sb_deactive(sb);
2822 static void nfs4_close_done(struct rpc_task *task, void *data)
2824 struct nfs4_closedata *calldata = data;
2825 struct nfs4_state *state = calldata->state;
2826 struct nfs_server *server = NFS_SERVER(calldata->inode);
2827 nfs4_stateid *res_stateid = NULL;
2829 dprintk("%s: begin!\n", __func__);
2830 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2832 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
2833 /* hmm. we are done with the inode, and in the process of freeing
2834 * the state_owner. we keep this around to process errors
2836 switch (task->tk_status) {
2838 res_stateid = &calldata->res.stateid;
2840 pnfs_roc_set_barrier(state->inode,
2841 calldata->roc_barrier);
2842 renew_lease(server, calldata->timestamp);
2844 case -NFS4ERR_ADMIN_REVOKED:
2845 case -NFS4ERR_STALE_STATEID:
2846 case -NFS4ERR_OLD_STATEID:
2847 case -NFS4ERR_BAD_STATEID:
2848 case -NFS4ERR_EXPIRED:
2849 if (!nfs4_stateid_match(&calldata->arg.stateid,
2850 &state->open_stateid)) {
2851 rpc_restart_call_prepare(task);
2854 if (calldata->arg.fmode == 0)
2857 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
2858 rpc_restart_call_prepare(task);
2862 nfs_clear_open_stateid(state, &calldata->arg.stateid,
2863 res_stateid, calldata->arg.fmode);
2865 nfs_release_seqid(calldata->arg.seqid);
2866 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
2867 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
2870 static void nfs4_close_prepare(struct rpc_task *task, void *data)
2872 struct nfs4_closedata *calldata = data;
2873 struct nfs4_state *state = calldata->state;
2874 struct inode *inode = calldata->inode;
2875 bool is_rdonly, is_wronly, is_rdwr;
2878 dprintk("%s: begin!\n", __func__);
2879 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
2882 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2883 spin_lock(&state->owner->so_lock);
2884 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
2885 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
2886 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
2887 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
2888 /* Calculate the change in open mode */
2889 calldata->arg.fmode = 0;
2890 if (state->n_rdwr == 0) {
2891 if (state->n_rdonly == 0)
2892 call_close |= is_rdonly;
2894 calldata->arg.fmode |= FMODE_READ;
2895 if (state->n_wronly == 0)
2896 call_close |= is_wronly;
2898 calldata->arg.fmode |= FMODE_WRITE;
2899 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
2900 call_close |= is_rdwr;
2902 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
2904 if (!nfs4_valid_open_stateid(state))
2906 spin_unlock(&state->owner->so_lock);
2909 /* Note: exit _without_ calling nfs4_close_done */
2913 if (nfs4_wait_on_layoutreturn(inode, task)) {
2914 nfs_release_seqid(calldata->arg.seqid);
2918 if (calldata->arg.fmode == 0)
2919 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
2921 pnfs_roc_get_barrier(inode, &calldata->roc_barrier);
2923 calldata->arg.share_access =
2924 nfs4_map_atomic_open_share(NFS_SERVER(inode),
2925 calldata->arg.fmode, 0);
2927 nfs_fattr_init(calldata->res.fattr);
2928 calldata->timestamp = jiffies;
2929 if (nfs4_setup_sequence(NFS_SERVER(inode),
2930 &calldata->arg.seq_args,
2931 &calldata->res.seq_res,
2933 nfs_release_seqid(calldata->arg.seqid);
2934 dprintk("%s: done!\n", __func__);
2937 task->tk_action = NULL;
2939 nfs4_sequence_done(task, &calldata->res.seq_res);
2942 static const struct rpc_call_ops nfs4_close_ops = {
2943 .rpc_call_prepare = nfs4_close_prepare,
2944 .rpc_call_done = nfs4_close_done,
2945 .rpc_release = nfs4_free_closedata,
2948 static bool nfs4_roc(struct inode *inode)
2950 if (!nfs_have_layout(inode))
2952 return pnfs_roc(inode);
2956 * It is possible for data to be read/written from a mem-mapped file
2957 * after the sys_close call (which hits the vfs layer as a flush).
2958 * This means that we can't safely call nfsv4 close on a file until
2959 * the inode is cleared. This in turn means that we are not good
2960 * NFSv4 citizens - we do not indicate to the server to update the file's
2961 * share state even when we are done with one of the three share
2962 * stateid's in the inode.
2964 * NOTE: Caller must be holding the sp->so_owner semaphore!
2966 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
2968 struct nfs_server *server = NFS_SERVER(state->inode);
2969 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
2970 struct nfs4_closedata *calldata;
2971 struct nfs4_state_owner *sp = state->owner;
2972 struct rpc_task *task;
2973 struct rpc_message msg = {
2974 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2975 .rpc_cred = state->owner->so_cred,
2977 struct rpc_task_setup task_setup_data = {
2978 .rpc_client = server->client,
2979 .rpc_message = &msg,
2980 .callback_ops = &nfs4_close_ops,
2981 .workqueue = nfsiod_workqueue,
2982 .flags = RPC_TASK_ASYNC,
2984 int status = -ENOMEM;
2986 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
2987 &task_setup_data.rpc_client, &msg);
2989 calldata = kzalloc(sizeof(*calldata), gfp_mask);
2990 if (calldata == NULL)
2992 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
2993 calldata->inode = state->inode;
2994 calldata->state = state;
2995 calldata->arg.fh = NFS_FH(state->inode);
2996 /* Serialization for the sequence id */
2997 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
2998 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
2999 if (IS_ERR(calldata->arg.seqid))
3000 goto out_free_calldata;
3001 calldata->arg.fmode = 0;
3002 calldata->arg.bitmask = server->cache_consistency_bitmask;
3003 calldata->res.fattr = &calldata->fattr;
3004 calldata->res.seqid = calldata->arg.seqid;
3005 calldata->res.server = server;
3006 calldata->roc = nfs4_roc(state->inode);
3007 nfs_sb_active(calldata->inode->i_sb);
3009 msg.rpc_argp = &calldata->arg;
3010 msg.rpc_resp = &calldata->res;
3011 task_setup_data.callback_data = calldata;
3012 task = rpc_run_task(&task_setup_data);
3014 return PTR_ERR(task);
3017 status = rpc_wait_for_completion_task(task);
3023 nfs4_put_open_state(state);
3024 nfs4_put_state_owner(sp);
3028 static struct inode *
3029 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3030 int open_flags, struct iattr *attr, int *opened)
3032 struct nfs4_state *state;
3033 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3035 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
3037 /* Protect against concurrent sillydeletes */
3038 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
3040 nfs4_label_release_security(label);
3043 return ERR_CAST(state);
3044 return state->inode;
3047 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
3049 if (ctx->state == NULL)
3052 nfs4_close_sync(ctx->state, ctx->mode);
3054 nfs4_close_state(ctx->state, ctx->mode);
3057 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3058 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3059 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
3061 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3063 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
3064 struct nfs4_server_caps_arg args = {
3068 struct nfs4_server_caps_res res = {};
3069 struct rpc_message msg = {
3070 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
3076 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3077 FATTR4_WORD0_FH_EXPIRE_TYPE |
3078 FATTR4_WORD0_LINK_SUPPORT |
3079 FATTR4_WORD0_SYMLINK_SUPPORT |
3080 FATTR4_WORD0_ACLSUPPORT;
3082 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3084 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3086 /* Sanity check the server answers */
3087 switch (minorversion) {
3089 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3090 res.attr_bitmask[2] = 0;
3093 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3096 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3098 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
3099 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3100 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3101 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3102 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
3103 NFS_CAP_CTIME|NFS_CAP_MTIME|
3104 NFS_CAP_SECURITY_LABEL);
3105 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3106 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3107 server->caps |= NFS_CAP_ACLS;
3108 if (res.has_links != 0)
3109 server->caps |= NFS_CAP_HARDLINKS;
3110 if (res.has_symlinks != 0)
3111 server->caps |= NFS_CAP_SYMLINKS;
3112 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3113 server->caps |= NFS_CAP_FILEID;
3114 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3115 server->caps |= NFS_CAP_MODE;
3116 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3117 server->caps |= NFS_CAP_NLINK;
3118 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3119 server->caps |= NFS_CAP_OWNER;
3120 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3121 server->caps |= NFS_CAP_OWNER_GROUP;
3122 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3123 server->caps |= NFS_CAP_ATIME;
3124 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3125 server->caps |= NFS_CAP_CTIME;
3126 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3127 server->caps |= NFS_CAP_MTIME;
3128 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3129 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3130 server->caps |= NFS_CAP_SECURITY_LABEL;
3132 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3133 sizeof(server->attr_bitmask));
3134 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3136 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3137 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3138 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
3139 server->cache_consistency_bitmask[2] = 0;
3140 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3141 sizeof(server->exclcreat_bitmask));
3142 server->acl_bitmask = res.acl_bitmask;
3143 server->fh_expire_type = res.fh_expire_type;
3149 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3151 struct nfs4_exception exception = { };
3154 err = nfs4_handle_exception(server,
3155 _nfs4_server_capabilities(server, fhandle),
3157 } while (exception.retry);
3161 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3162 struct nfs_fsinfo *info)
3165 struct nfs4_lookup_root_arg args = {
3168 struct nfs4_lookup_res res = {
3170 .fattr = info->fattr,
3173 struct rpc_message msg = {
3174 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3179 bitmask[0] = nfs4_fattr_bitmap[0];
3180 bitmask[1] = nfs4_fattr_bitmap[1];
3182 * Process the label in the upcoming getfattr
3184 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3186 nfs_fattr_init(info->fattr);
3187 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3190 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3191 struct nfs_fsinfo *info)
3193 struct nfs4_exception exception = { };
3196 err = _nfs4_lookup_root(server, fhandle, info);
3197 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
3200 case -NFS4ERR_WRONGSEC:
3203 err = nfs4_handle_exception(server, err, &exception);
3205 } while (exception.retry);
3210 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3211 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3213 struct rpc_auth_create_args auth_args = {
3214 .pseudoflavor = flavor,
3216 struct rpc_auth *auth;
3219 auth = rpcauth_create(&auth_args, server->client);
3224 ret = nfs4_lookup_root(server, fhandle, info);
3230 * Retry pseudoroot lookup with various security flavors. We do this when:
3232 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3233 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3235 * Returns zero on success, or a negative NFS4ERR value, or a
3236 * negative errno value.
3238 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3239 struct nfs_fsinfo *info)
3241 /* Per 3530bis 15.33.5 */
3242 static const rpc_authflavor_t flav_array[] = {
3246 RPC_AUTH_UNIX, /* courtesy */
3249 int status = -EPERM;
3252 if (server->auth_info.flavor_len > 0) {
3253 /* try each flavor specified by user */
3254 for (i = 0; i < server->auth_info.flavor_len; i++) {
3255 status = nfs4_lookup_root_sec(server, fhandle, info,
3256 server->auth_info.flavors[i]);
3257 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3262 /* no flavors specified by user, try default list */
3263 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3264 status = nfs4_lookup_root_sec(server, fhandle, info,
3266 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3273 * -EACCESS could mean that the user doesn't have correct permissions
3274 * to access the mount. It could also mean that we tried to mount
3275 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3276 * existing mount programs don't handle -EACCES very well so it should
3277 * be mapped to -EPERM instead.
3279 if (status == -EACCES)
3284 static int nfs4_do_find_root_sec(struct nfs_server *server,
3285 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
3287 int mv = server->nfs_client->cl_minorversion;
3288 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
3292 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3293 * @server: initialized nfs_server handle
3294 * @fhandle: we fill in the pseudo-fs root file handle
3295 * @info: we fill in an FSINFO struct
3296 * @auth_probe: probe the auth flavours
3298 * Returns zero on success, or a negative errno.
3300 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
3301 struct nfs_fsinfo *info,
3307 status = nfs4_lookup_root(server, fhandle, info);
3309 if (auth_probe || status == NFS4ERR_WRONGSEC)
3310 status = nfs4_do_find_root_sec(server, fhandle, info);
3313 status = nfs4_server_capabilities(server, fhandle);
3315 status = nfs4_do_fsinfo(server, fhandle, info);
3317 return nfs4_map_errors(status);
3320 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3321 struct nfs_fsinfo *info)
3324 struct nfs_fattr *fattr = info->fattr;
3325 struct nfs4_label *label = NULL;
3327 error = nfs4_server_capabilities(server, mntfh);
3329 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3333 label = nfs4_label_alloc(server, GFP_KERNEL);
3335 return PTR_ERR(label);
3337 error = nfs4_proc_getattr(server, mntfh, fattr, label);
3339 dprintk("nfs4_get_root: getattr error = %d\n", -error);
3340 goto err_free_label;
3343 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3344 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3345 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3348 nfs4_label_free(label);
3354 * Get locations and (maybe) other attributes of a referral.
3355 * Note that we'll actually follow the referral later when
3356 * we detect fsid mismatch in inode revalidation
3358 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3359 const struct qstr *name, struct nfs_fattr *fattr,
3360 struct nfs_fh *fhandle)
3362 int status = -ENOMEM;
3363 struct page *page = NULL;
3364 struct nfs4_fs_locations *locations = NULL;
3366 page = alloc_page(GFP_KERNEL);
3369 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3370 if (locations == NULL)
3373 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
3378 * If the fsid didn't change, this is a migration event, not a
3379 * referral. Cause us to drop into the exception handler, which
3380 * will kick off migration recovery.
3382 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
3383 dprintk("%s: server did not return a different fsid for"
3384 " a referral at %s\n", __func__, name->name);
3385 status = -NFS4ERR_MOVED;
3388 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3389 nfs_fixup_referral_attributes(&locations->fattr);
3391 /* replace the lookup nfs_fattr with the locations nfs_fattr */
3392 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
3393 memset(fhandle, 0, sizeof(struct nfs_fh));
3401 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3402 struct nfs_fattr *fattr, struct nfs4_label *label)
3404 struct nfs4_getattr_arg args = {
3406 .bitmask = server->attr_bitmask,
3408 struct nfs4_getattr_res res = {
3413 struct rpc_message msg = {
3414 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3419 args.bitmask = nfs4_bitmask(server, label);
3421 nfs_fattr_init(fattr);
3422 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3425 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3426 struct nfs_fattr *fattr, struct nfs4_label *label)
3428 struct nfs4_exception exception = { };
3431 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3432 trace_nfs4_getattr(server, fhandle, fattr, err);
3433 err = nfs4_handle_exception(server, err,
3435 } while (exception.retry);
3440 * The file is not closed if it is opened due to the a request to change
3441 * the size of the file. The open call will not be needed once the
3442 * VFS layer lookup-intents are implemented.
3444 * Close is called when the inode is destroyed.
3445 * If we haven't opened the file for O_WRONLY, we
3446 * need to in the size_change case to obtain a stateid.
3449 * Because OPEN is always done by name in nfsv4, it is
3450 * possible that we opened a different file by the same
3451 * name. We can recognize this race condition, but we
3452 * can't do anything about it besides returning an error.
3454 * This will be fixed with VFS changes (lookup-intent).
3457 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3458 struct iattr *sattr)
3460 struct inode *inode = d_inode(dentry);
3461 struct rpc_cred *cred = NULL;
3462 struct nfs4_state *state = NULL;
3463 struct nfs4_label *label = NULL;
3466 if (pnfs_ld_layoutret_on_setattr(inode) &&
3467 sattr->ia_valid & ATTR_SIZE &&
3468 sattr->ia_size < i_size_read(inode))
3469 pnfs_commit_and_return_layout(inode);
3471 nfs_fattr_init(fattr);
3473 /* Deal with open(O_TRUNC) */
3474 if (sattr->ia_valid & ATTR_OPEN)
3475 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
3477 /* Optimization: if the end result is no change, don't RPC */
3478 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
3481 /* Search for an existing open(O_WRITE) file */
3482 if (sattr->ia_valid & ATTR_FILE) {
3483 struct nfs_open_context *ctx;
3485 ctx = nfs_file_open_context(sattr->ia_file);
3492 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3494 return PTR_ERR(label);
3496 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
3498 nfs_setattr_update_inode(inode, sattr, fattr);
3499 nfs_setsecurity(inode, fattr, label);
3501 nfs4_label_free(label);
3505 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3506 const struct qstr *name, struct nfs_fh *fhandle,
3507 struct nfs_fattr *fattr, struct nfs4_label *label)
3509 struct nfs_server *server = NFS_SERVER(dir);
3511 struct nfs4_lookup_arg args = {
3512 .bitmask = server->attr_bitmask,
3513 .dir_fh = NFS_FH(dir),
3516 struct nfs4_lookup_res res = {
3522 struct rpc_message msg = {
3523 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3528 args.bitmask = nfs4_bitmask(server, label);
3530 nfs_fattr_init(fattr);
3532 dprintk("NFS call lookup %s\n", name->name);
3533 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
3534 dprintk("NFS reply lookup: %d\n", status);
3538 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
3540 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
3541 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
3542 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3546 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3547 struct qstr *name, struct nfs_fh *fhandle,
3548 struct nfs_fattr *fattr, struct nfs4_label *label)
3550 struct nfs4_exception exception = { };
3551 struct rpc_clnt *client = *clnt;
3554 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
3555 trace_nfs4_lookup(dir, name, err);
3557 case -NFS4ERR_BADNAME:
3560 case -NFS4ERR_MOVED:
3561 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
3562 if (err == -NFS4ERR_MOVED)
3563 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3565 case -NFS4ERR_WRONGSEC:
3567 if (client != *clnt)
3569 client = nfs4_negotiate_security(client, dir, name);
3571 return PTR_ERR(client);
3573 exception.retry = 1;
3576 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3578 } while (exception.retry);
3583 else if (client != *clnt)
3584 rpc_shutdown_client(client);
3589 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
3590 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3591 struct nfs4_label *label)
3594 struct rpc_clnt *client = NFS_CLIENT(dir);
3596 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
3597 if (client != NFS_CLIENT(dir)) {
3598 rpc_shutdown_client(client);
3599 nfs_fixup_secinfo_attributes(fattr);
3605 nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
3606 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3608 struct rpc_clnt *client = NFS_CLIENT(dir);
3611 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
3613 return ERR_PTR(status);
3614 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
3617 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3619 struct nfs_server *server = NFS_SERVER(inode);
3620 struct nfs4_accessargs args = {
3621 .fh = NFS_FH(inode),
3622 .bitmask = server->cache_consistency_bitmask,
3624 struct nfs4_accessres res = {
3627 struct rpc_message msg = {
3628 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3631 .rpc_cred = entry->cred,
3633 int mode = entry->mask;
3637 * Determine which access bits we want to ask for...
3639 if (mode & MAY_READ)
3640 args.access |= NFS4_ACCESS_READ;
3641 if (S_ISDIR(inode->i_mode)) {
3642 if (mode & MAY_WRITE)
3643 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3644 if (mode & MAY_EXEC)
3645 args.access |= NFS4_ACCESS_LOOKUP;
3647 if (mode & MAY_WRITE)
3648 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3649 if (mode & MAY_EXEC)
3650 args.access |= NFS4_ACCESS_EXECUTE;
3653 res.fattr = nfs_alloc_fattr();
3654 if (res.fattr == NULL)
3657 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3659 nfs_access_set_mask(entry, res.access);
3660 nfs_refresh_inode(inode, res.fattr);
3662 nfs_free_fattr(res.fattr);
3666 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3668 struct nfs4_exception exception = { };
3671 err = _nfs4_proc_access(inode, entry);
3672 trace_nfs4_access(inode, err);
3673 err = nfs4_handle_exception(NFS_SERVER(inode), err,
3675 } while (exception.retry);
3680 * TODO: For the time being, we don't try to get any attributes
3681 * along with any of the zero-copy operations READ, READDIR,
3684 * In the case of the first three, we want to put the GETATTR
3685 * after the read-type operation -- this is because it is hard
3686 * to predict the length of a GETATTR response in v4, and thus
3687 * align the READ data correctly. This means that the GETATTR
3688 * may end up partially falling into the page cache, and we should
3689 * shift it into the 'tail' of the xdr_buf before processing.
3690 * To do this efficiently, we need to know the total length
3691 * of data received, which doesn't seem to be available outside
3694 * In the case of WRITE, we also want to put the GETATTR after
3695 * the operation -- in this case because we want to make sure
3696 * we get the post-operation mtime and size.
3698 * Both of these changes to the XDR layer would in fact be quite
3699 * minor, but I decided to leave them for a subsequent patch.
3701 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3702 unsigned int pgbase, unsigned int pglen)
3704 struct nfs4_readlink args = {
3705 .fh = NFS_FH(inode),
3710 struct nfs4_readlink_res res;
3711 struct rpc_message msg = {
3712 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3717 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
3720 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3721 unsigned int pgbase, unsigned int pglen)
3723 struct nfs4_exception exception = { };
3726 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3727 trace_nfs4_readlink(inode, err);
3728 err = nfs4_handle_exception(NFS_SERVER(inode), err,
3730 } while (exception.retry);
3735 * This is just for mknod. open(O_CREAT) will always do ->open_context().
3738 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
3741 struct nfs4_label l, *ilabel = NULL;
3742 struct nfs_open_context *ctx;
3743 struct nfs4_state *state;
3746 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3748 return PTR_ERR(ctx);
3750 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3752 sattr->ia_mode &= ~current_umask();
3753 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
3754 if (IS_ERR(state)) {
3755 status = PTR_ERR(state);
3759 nfs4_label_release_security(ilabel);
3760 put_nfs_open_context(ctx);
3764 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3766 struct nfs_server *server = NFS_SERVER(dir);
3767 struct nfs_removeargs args = {
3771 struct nfs_removeres res = {
3774 struct rpc_message msg = {
3775 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3781 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
3783 update_changeattr(dir, &res.cinfo);
3787 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3789 struct nfs4_exception exception = { };
3792 err = _nfs4_proc_remove(dir, name);
3793 trace_nfs4_remove(dir, name, err);
3794 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3796 } while (exception.retry);
3800 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
3802 struct nfs_server *server = NFS_SERVER(dir);
3803 struct nfs_removeargs *args = msg->rpc_argp;
3804 struct nfs_removeres *res = msg->rpc_resp;
3806 res->server = server;
3807 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
3808 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
3810 nfs_fattr_init(res->dir_attr);
3813 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3815 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb),
3816 &data->args.seq_args,
3821 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
3823 struct nfs_unlinkdata *data = task->tk_calldata;
3824 struct nfs_removeres *res = &data->res;
3826 if (!nfs4_sequence_done(task, &res->seq_res))
3828 if (nfs4_async_handle_error(task, res->server, NULL,
3829 &data->timeout) == -EAGAIN)
3831 update_changeattr(dir, &res->cinfo);
3835 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3837 struct nfs_server *server = NFS_SERVER(dir);
3838 struct nfs_renameargs *arg = msg->rpc_argp;
3839 struct nfs_renameres *res = msg->rpc_resp;
3841 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
3842 res->server = server;
3843 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
3846 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3848 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3849 &data->args.seq_args,
3854 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3855 struct inode *new_dir)
3857 struct nfs_renamedata *data = task->tk_calldata;
3858 struct nfs_renameres *res = &data->res;
3860 if (!nfs4_sequence_done(task, &res->seq_res))
3862 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
3865 update_changeattr(old_dir, &res->old_cinfo);
3866 update_changeattr(new_dir, &res->new_cinfo);
3870 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3872 struct nfs_server *server = NFS_SERVER(inode);
3873 struct nfs4_link_arg arg = {
3874 .fh = NFS_FH(inode),
3875 .dir_fh = NFS_FH(dir),
3877 .bitmask = server->attr_bitmask,
3879 struct nfs4_link_res res = {
3883 struct rpc_message msg = {
3884 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3888 int status = -ENOMEM;
3890 res.fattr = nfs_alloc_fattr();
3891 if (res.fattr == NULL)
3894 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3895 if (IS_ERR(res.label)) {
3896 status = PTR_ERR(res.label);
3899 arg.bitmask = nfs4_bitmask(server, res.label);
3901 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3903 update_changeattr(dir, &res.cinfo);
3904 status = nfs_post_op_update_inode(inode, res.fattr);
3906 nfs_setsecurity(inode, res.fattr, res.label);
3910 nfs4_label_free(res.label);
3913 nfs_free_fattr(res.fattr);
3917 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3919 struct nfs4_exception exception = { };
3922 err = nfs4_handle_exception(NFS_SERVER(inode),
3923 _nfs4_proc_link(inode, dir, name),
3925 } while (exception.retry);
3929 struct nfs4_createdata {
3930 struct rpc_message msg;
3931 struct nfs4_create_arg arg;
3932 struct nfs4_create_res res;
3934 struct nfs_fattr fattr;
3935 struct nfs4_label *label;
3938 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3939 struct qstr *name, struct iattr *sattr, u32 ftype)
3941 struct nfs4_createdata *data;
3943 data = kzalloc(sizeof(*data), GFP_KERNEL);
3945 struct nfs_server *server = NFS_SERVER(dir);
3947 data->label = nfs4_label_alloc(server, GFP_KERNEL);
3948 if (IS_ERR(data->label))
3951 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3952 data->msg.rpc_argp = &data->arg;
3953 data->msg.rpc_resp = &data->res;
3954 data->arg.dir_fh = NFS_FH(dir);
3955 data->arg.server = server;
3956 data->arg.name = name;
3957 data->arg.attrs = sattr;
3958 data->arg.ftype = ftype;
3959 data->arg.bitmask = nfs4_bitmask(server, data->label);
3960 data->res.server = server;
3961 data->res.fh = &data->fh;
3962 data->res.fattr = &data->fattr;
3963 data->res.label = data->label;
3964 nfs_fattr_init(data->res.fattr);
3972 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3974 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
3975 &data->arg.seq_args, &data->res.seq_res, 1);
3977 update_changeattr(dir, &data->res.dir_cinfo);
3978 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
3983 static void nfs4_free_createdata(struct nfs4_createdata *data)
3985 nfs4_label_free(data->label);
3989 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3990 struct page *page, unsigned int len, struct iattr *sattr,
3991 struct nfs4_label *label)
3993 struct nfs4_createdata *data;
3994 int status = -ENAMETOOLONG;
3996 if (len > NFS4_MAXPATHLEN)
4000 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4004 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4005 data->arg.u.symlink.pages = &page;
4006 data->arg.u.symlink.len = len;
4007 data->arg.label = label;
4009 status = nfs4_do_create(dir, dentry, data);
4011 nfs4_free_createdata(data);
4016 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4017 struct page *page, unsigned int len, struct iattr *sattr)
4019 struct nfs4_exception exception = { };
4020 struct nfs4_label l, *label = NULL;
4023 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4026 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4027 trace_nfs4_symlink(dir, &dentry->d_name, err);
4028 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4030 } while (exception.retry);
4032 nfs4_label_release_security(label);
4036 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4037 struct iattr *sattr, struct nfs4_label *label)
4039 struct nfs4_createdata *data;
4040 int status = -ENOMEM;
4042 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4046 data->arg.label = label;
4047 status = nfs4_do_create(dir, dentry, data);
4049 nfs4_free_createdata(data);
4054 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4055 struct iattr *sattr)
4057 struct nfs4_exception exception = { };
4058 struct nfs4_label l, *label = NULL;
4061 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4063 sattr->ia_mode &= ~current_umask();
4065 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4066 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4067 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4069 } while (exception.retry);
4070 nfs4_label_release_security(label);
4075 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
4076 u64 cookie, struct page **pages, unsigned int count, int plus)
4078 struct inode *dir = d_inode(dentry);
4079 struct nfs4_readdir_arg args = {
4084 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
4087 struct nfs4_readdir_res res;
4088 struct rpc_message msg = {
4089 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4096 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4098 (unsigned long long)cookie);
4099 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
4100 res.pgbase = args.pgbase;
4101 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4103 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
4104 status += args.pgbase;
4107 nfs_invalidate_atime(dir);
4109 dprintk("%s: returns %d\n", __func__, status);
4113 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
4114 u64 cookie, struct page **pages, unsigned int count, int plus)
4116 struct nfs4_exception exception = { };
4119 err = _nfs4_proc_readdir(dentry, cred, cookie,
4120 pages, count, plus);
4121 trace_nfs4_readdir(d_inode(dentry), err);
4122 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
4124 } while (exception.retry);
4128 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4129 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
4131 struct nfs4_createdata *data;
4132 int mode = sattr->ia_mode;
4133 int status = -ENOMEM;
4135 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4140 data->arg.ftype = NF4FIFO;
4141 else if (S_ISBLK(mode)) {
4142 data->arg.ftype = NF4BLK;
4143 data->arg.u.device.specdata1 = MAJOR(rdev);
4144 data->arg.u.device.specdata2 = MINOR(rdev);
4146 else if (S_ISCHR(mode)) {
4147 data->arg.ftype = NF4CHR;
4148 data->arg.u.device.specdata1 = MAJOR(rdev);
4149 data->arg.u.device.specdata2 = MINOR(rdev);
4150 } else if (!S_ISSOCK(mode)) {
4155 data->arg.label = label;
4156 status = nfs4_do_create(dir, dentry, data);
4158 nfs4_free_createdata(data);
4163 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4164 struct iattr *sattr, dev_t rdev)
4166 struct nfs4_exception exception = { };
4167 struct nfs4_label l, *label = NULL;
4170 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4172 sattr->ia_mode &= ~current_umask();
4174 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4175 trace_nfs4_mknod(dir, &dentry->d_name, err);
4176 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4178 } while (exception.retry);
4180 nfs4_label_release_security(label);
4185 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4186 struct nfs_fsstat *fsstat)
4188 struct nfs4_statfs_arg args = {
4190 .bitmask = server->attr_bitmask,
4192 struct nfs4_statfs_res res = {
4195 struct rpc_message msg = {
4196 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4201 nfs_fattr_init(fsstat->fattr);
4202 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4205 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4207 struct nfs4_exception exception = { };
4210 err = nfs4_handle_exception(server,
4211 _nfs4_proc_statfs(server, fhandle, fsstat),
4213 } while (exception.retry);
4217 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4218 struct nfs_fsinfo *fsinfo)
4220 struct nfs4_fsinfo_arg args = {
4222 .bitmask = server->attr_bitmask,
4224 struct nfs4_fsinfo_res res = {
4227 struct rpc_message msg = {
4228 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4233 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4236 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4238 struct nfs4_exception exception = { };
4239 unsigned long now = jiffies;
4243 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
4244 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
4246 struct nfs_client *clp = server->nfs_client;
4248 spin_lock(&clp->cl_lock);
4249 clp->cl_lease_time = fsinfo->lease_time * HZ;
4250 clp->cl_last_renewal = now;
4251 spin_unlock(&clp->cl_lock);
4254 err = nfs4_handle_exception(server, err, &exception);
4255 } while (exception.retry);
4259 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4263 nfs_fattr_init(fsinfo->fattr);
4264 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
4266 /* block layout checks this! */
4267 server->pnfs_blksize = fsinfo->blksize;
4268 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
4274 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4275 struct nfs_pathconf *pathconf)
4277 struct nfs4_pathconf_arg args = {
4279 .bitmask = server->attr_bitmask,
4281 struct nfs4_pathconf_res res = {
4282 .pathconf = pathconf,
4284 struct rpc_message msg = {
4285 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4290 /* None of the pathconf attributes are mandatory to implement */
4291 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4292 memset(pathconf, 0, sizeof(*pathconf));
4296 nfs_fattr_init(pathconf->fattr);
4297 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4300 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4301 struct nfs_pathconf *pathconf)
4303 struct nfs4_exception exception = { };
4307 err = nfs4_handle_exception(server,
4308 _nfs4_proc_pathconf(server, fhandle, pathconf),
4310 } while (exception.retry);
4314 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
4315 const struct nfs_open_context *ctx,
4316 const struct nfs_lock_context *l_ctx,
4319 const struct nfs_lockowner *lockowner = NULL;
4322 lockowner = &l_ctx->lockowner;
4323 return nfs4_select_rw_stateid(ctx->state, fmode, lockowner, stateid, NULL);
4325 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4327 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4328 const struct nfs_open_context *ctx,
4329 const struct nfs_lock_context *l_ctx,
4332 nfs4_stateid current_stateid;
4334 /* If the current stateid represents a lost lock, then exit */
4335 if (nfs4_set_rw_stateid(¤t_stateid, ctx, l_ctx, fmode) == -EIO)
4337 return nfs4_stateid_match(stateid, ¤t_stateid);
4340 static bool nfs4_error_stateid_expired(int err)
4343 case -NFS4ERR_DELEG_REVOKED:
4344 case -NFS4ERR_ADMIN_REVOKED:
4345 case -NFS4ERR_BAD_STATEID:
4346 case -NFS4ERR_STALE_STATEID:
4347 case -NFS4ERR_OLD_STATEID:
4348 case -NFS4ERR_OPENMODE:
4349 case -NFS4ERR_EXPIRED:
4355 void __nfs4_read_done_cb(struct nfs_pgio_header *hdr)
4357 nfs_invalidate_atime(hdr->inode);
4360 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
4362 struct nfs_server *server = NFS_SERVER(hdr->inode);
4364 trace_nfs4_read(hdr, task->tk_status);
4365 if (nfs4_async_handle_error(task, server,
4366 hdr->args.context->state,
4368 rpc_restart_call_prepare(task);
4372 __nfs4_read_done_cb(hdr);
4373 if (task->tk_status > 0)
4374 renew_lease(server, hdr->timestamp);
4378 static bool nfs4_read_stateid_changed(struct rpc_task *task,
4379 struct nfs_pgio_args *args)
4382 if (!nfs4_error_stateid_expired(task->tk_status) ||
4383 nfs4_stateid_is_current(&args->stateid,
4388 rpc_restart_call_prepare(task);
4392 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
4395 dprintk("--> %s\n", __func__);
4397 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
4399 if (nfs4_read_stateid_changed(task, &hdr->args))
4401 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4402 nfs4_read_done_cb(task, hdr);
4405 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4406 struct rpc_message *msg)
4408 hdr->timestamp = jiffies;
4409 if (!hdr->pgio_done_cb)
4410 hdr->pgio_done_cb = nfs4_read_done_cb;
4411 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
4412 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
4415 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4416 struct nfs_pgio_header *hdr)
4418 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4419 &hdr->args.seq_args,
4423 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4424 hdr->args.lock_context,
4425 hdr->rw_ops->rw_mode) == -EIO)
4427 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
4432 static int nfs4_write_done_cb(struct rpc_task *task,
4433 struct nfs_pgio_header *hdr)
4435 struct inode *inode = hdr->inode;
4437 trace_nfs4_write(hdr, task->tk_status);
4438 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4439 hdr->args.context->state,
4441 rpc_restart_call_prepare(task);
4444 if (task->tk_status >= 0) {
4445 renew_lease(NFS_SERVER(inode), hdr->timestamp);
4446 nfs_writeback_update_inode(hdr);
4451 static bool nfs4_write_stateid_changed(struct rpc_task *task,
4452 struct nfs_pgio_args *args)
4455 if (!nfs4_error_stateid_expired(task->tk_status) ||
4456 nfs4_stateid_is_current(&args->stateid,
4461 rpc_restart_call_prepare(task);
4465 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
4467 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
4469 if (nfs4_write_stateid_changed(task, &hdr->args))
4471 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4472 nfs4_write_done_cb(task, hdr);
4476 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
4478 /* Don't request attributes for pNFS or O_DIRECT writes */
4479 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
4481 /* Otherwise, request attributes if and only if we don't hold
4484 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
4487 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4488 struct rpc_message *msg)
4490 struct nfs_server *server = NFS_SERVER(hdr->inode);
4492 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4493 hdr->args.bitmask = NULL;
4494 hdr->res.fattr = NULL;
4496 hdr->args.bitmask = server->cache_consistency_bitmask;
4498 if (!hdr->pgio_done_cb)
4499 hdr->pgio_done_cb = nfs4_write_done_cb;
4500 hdr->res.server = server;
4501 hdr->timestamp = jiffies;
4503 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
4504 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
4507 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4509 nfs4_setup_sequence(NFS_SERVER(data->inode),
4510 &data->args.seq_args,
4515 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
4517 struct inode *inode = data->inode;
4519 trace_nfs4_commit(data, task->tk_status);
4520 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4521 NULL, NULL) == -EAGAIN) {
4522 rpc_restart_call_prepare(task);
4528 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
4530 if (!nfs4_sequence_done(task, &data->res.seq_res))
4532 return data->commit_done_cb(task, data);
4535 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
4537 struct nfs_server *server = NFS_SERVER(data->inode);
4539 if (data->commit_done_cb == NULL)
4540 data->commit_done_cb = nfs4_commit_done_cb;
4541 data->res.server = server;
4542 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
4543 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4546 struct nfs4_renewdata {
4547 struct nfs_client *client;
4548 unsigned long timestamp;
4552 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4553 * standalone procedure for queueing an asynchronous RENEW.
4555 static void nfs4_renew_release(void *calldata)
4557 struct nfs4_renewdata *data = calldata;
4558 struct nfs_client *clp = data->client;
4560 if (atomic_read(&clp->cl_count) > 1)
4561 nfs4_schedule_state_renewal(clp);
4562 nfs_put_client(clp);
4566 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
4568 struct nfs4_renewdata *data = calldata;
4569 struct nfs_client *clp = data->client;
4570 unsigned long timestamp = data->timestamp;
4572 trace_nfs4_renew_async(clp, task->tk_status);
4573 switch (task->tk_status) {
4576 case -NFS4ERR_LEASE_MOVED:
4577 nfs4_schedule_lease_moved_recovery(clp);
4580 /* Unless we're shutting down, schedule state recovery! */
4581 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4583 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
4584 nfs4_schedule_lease_recovery(clp);
4587 nfs4_schedule_path_down_recovery(clp);
4589 do_renew_lease(clp, timestamp);
4592 static const struct rpc_call_ops nfs4_renew_ops = {
4593 .rpc_call_done = nfs4_renew_done,
4594 .rpc_release = nfs4_renew_release,
4597 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
4599 struct rpc_message msg = {
4600 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4604 struct nfs4_renewdata *data;
4606 if (renew_flags == 0)
4608 if (!atomic_inc_not_zero(&clp->cl_count))
4610 data = kmalloc(sizeof(*data), GFP_NOFS);
4614 data->timestamp = jiffies;
4615 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
4616 &nfs4_renew_ops, data);
4619 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
4621 struct rpc_message msg = {
4622 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4626 unsigned long now = jiffies;
4629 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4632 do_renew_lease(clp, now);
4636 static inline int nfs4_server_supports_acls(struct nfs_server *server)
4638 return server->caps & NFS_CAP_ACLS;
4641 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4642 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
4645 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
4647 static int buf_to_pages_noslab(const void *buf, size_t buflen,
4648 struct page **pages)
4650 struct page *newpage, **spages;
4656 len = min_t(size_t, PAGE_SIZE, buflen);
4657 newpage = alloc_page(GFP_KERNEL);
4659 if (newpage == NULL)
4661 memcpy(page_address(newpage), buf, len);
4666 } while (buflen != 0);
4672 __free_page(spages[rc-1]);
4676 struct nfs4_cached_acl {
4682 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
4684 struct nfs_inode *nfsi = NFS_I(inode);
4686 spin_lock(&inode->i_lock);
4687 kfree(nfsi->nfs4_acl);
4688 nfsi->nfs4_acl = acl;
4689 spin_unlock(&inode->i_lock);
4692 static void nfs4_zap_acl_attr(struct inode *inode)
4694 nfs4_set_cached_acl(inode, NULL);
4697 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4699 struct nfs_inode *nfsi = NFS_I(inode);
4700 struct nfs4_cached_acl *acl;
4703 spin_lock(&inode->i_lock);
4704 acl = nfsi->nfs4_acl;
4707 if (buf == NULL) /* user is just asking for length */
4709 if (acl->cached == 0)
4711 ret = -ERANGE; /* see getxattr(2) man page */
4712 if (acl->len > buflen)
4714 memcpy(buf, acl->data, acl->len);
4718 spin_unlock(&inode->i_lock);
4722 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
4724 struct nfs4_cached_acl *acl;
4725 size_t buflen = sizeof(*acl) + acl_len;
4727 if (buflen <= PAGE_SIZE) {
4728 acl = kmalloc(buflen, GFP_KERNEL);
4732 _copy_from_pages(acl->data, pages, pgbase, acl_len);
4734 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4741 nfs4_set_cached_acl(inode, acl);
4745 * The getxattr API returns the required buffer length when called with a
4746 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4747 * the required buf. On a NULL buf, we send a page of data to the server
4748 * guessing that the ACL request can be serviced by a page. If so, we cache
4749 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4750 * the cache. If not so, we throw away the page, and cache the required
4751 * length. The next getxattr call will then produce another round trip to
4752 * the server, this time with the input buf of the required size.
4754 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4756 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
4757 struct nfs_getaclargs args = {
4758 .fh = NFS_FH(inode),
4762 struct nfs_getaclres res = {
4765 struct rpc_message msg = {
4766 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4770 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4771 int ret = -ENOMEM, i;
4773 /* As long as we're doing a round trip to the server anyway,
4774 * let's be prepared for a page of acl data. */
4777 if (npages > ARRAY_SIZE(pages))
4780 for (i = 0; i < npages; i++) {
4781 pages[i] = alloc_page(GFP_KERNEL);
4786 /* for decoding across pages */
4787 res.acl_scratch = alloc_page(GFP_KERNEL);
4788 if (!res.acl_scratch)
4791 args.acl_len = npages * PAGE_SIZE;
4793 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
4794 __func__, buf, buflen, npages, args.acl_len);
4795 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4796 &msg, &args.seq_args, &res.seq_res, 0);
4800 /* Handle the case where the passed-in buffer is too short */
4801 if (res.acl_flags & NFS4_ACL_TRUNC) {
4802 /* Did the user only issue a request for the acl length? */
4808 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
4810 if (res.acl_len > buflen) {
4814 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
4819 for (i = 0; i < npages; i++)
4821 __free_page(pages[i]);
4822 if (res.acl_scratch)
4823 __free_page(res.acl_scratch);
4827 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4829 struct nfs4_exception exception = { };
4832 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
4833 trace_nfs4_get_acl(inode, ret);
4836 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4837 } while (exception.retry);
4841 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4843 struct nfs_server *server = NFS_SERVER(inode);
4846 if (!nfs4_server_supports_acls(server))
4848 ret = nfs_revalidate_inode(server, inode);
4851 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4852 nfs_zap_acl_cache(inode);
4853 ret = nfs4_read_cached_acl(inode, buf, buflen);
4855 /* -ENOENT is returned if there is no ACL or if there is an ACL
4856 * but no cached acl data, just the acl length */
4858 return nfs4_get_acl_uncached(inode, buf, buflen);
4861 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4863 struct nfs_server *server = NFS_SERVER(inode);
4864 struct page *pages[NFS4ACL_MAXPAGES];
4865 struct nfs_setaclargs arg = {
4866 .fh = NFS_FH(inode),
4870 struct nfs_setaclres res;
4871 struct rpc_message msg = {
4872 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4876 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4879 if (!nfs4_server_supports_acls(server))
4881 if (npages > ARRAY_SIZE(pages))
4883 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
4886 nfs4_inode_return_delegation(inode);
4887 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4890 * Free each page after tx, so the only ref left is
4891 * held by the network stack
4894 put_page(pages[i-1]);
4897 * Acl update can result in inode attribute update.
4898 * so mark the attribute cache invalid.
4900 spin_lock(&inode->i_lock);
4901 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4902 spin_unlock(&inode->i_lock);
4903 nfs_access_zap_cache(inode);
4904 nfs_zap_acl_cache(inode);
4908 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4910 struct nfs4_exception exception = { };
4913 err = __nfs4_proc_set_acl(inode, buf, buflen);
4914 trace_nfs4_set_acl(inode, err);
4915 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4917 } while (exception.retry);
4921 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
4922 static int _nfs4_get_security_label(struct inode *inode, void *buf,
4925 struct nfs_server *server = NFS_SERVER(inode);
4926 struct nfs_fattr fattr;
4927 struct nfs4_label label = {0, 0, buflen, buf};
4929 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4930 struct nfs4_getattr_arg arg = {
4931 .fh = NFS_FH(inode),
4934 struct nfs4_getattr_res res = {
4939 struct rpc_message msg = {
4940 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4946 nfs_fattr_init(&fattr);
4948 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
4951 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
4953 if (buflen < label.len)
4958 static int nfs4_get_security_label(struct inode *inode, void *buf,
4961 struct nfs4_exception exception = { };
4964 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4968 err = _nfs4_get_security_label(inode, buf, buflen);
4969 trace_nfs4_get_security_label(inode, err);
4970 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4972 } while (exception.retry);
4976 static int _nfs4_do_set_security_label(struct inode *inode,
4977 struct nfs4_label *ilabel,
4978 struct nfs_fattr *fattr,
4979 struct nfs4_label *olabel)
4982 struct iattr sattr = {0};
4983 struct nfs_server *server = NFS_SERVER(inode);
4984 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4985 struct nfs_setattrargs arg = {
4986 .fh = NFS_FH(inode),
4992 struct nfs_setattrres res = {
4997 struct rpc_message msg = {
4998 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5004 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
5006 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5008 dprintk("%s failed: %d\n", __func__, status);
5013 static int nfs4_do_set_security_label(struct inode *inode,
5014 struct nfs4_label *ilabel,
5015 struct nfs_fattr *fattr,
5016 struct nfs4_label *olabel)
5018 struct nfs4_exception exception = { };
5022 err = _nfs4_do_set_security_label(inode, ilabel,
5024 trace_nfs4_set_security_label(inode, err);
5025 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5027 } while (exception.retry);
5032 nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
5034 struct nfs4_label ilabel, *olabel = NULL;
5035 struct nfs_fattr fattr;
5036 struct rpc_cred *cred;
5039 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5042 nfs_fattr_init(&fattr);
5046 ilabel.label = (char *)buf;
5047 ilabel.len = buflen;
5049 cred = rpc_lookup_cred();
5051 return PTR_ERR(cred);
5053 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5054 if (IS_ERR(olabel)) {
5055 status = -PTR_ERR(olabel);
5059 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5061 nfs_setsecurity(inode, &fattr, olabel);
5063 nfs4_label_free(olabel);
5068 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5071 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5072 nfs4_verifier *bootverf)
5076 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5077 /* An impossible timestamp guarantees this value
5078 * will never match a generated boot time. */
5080 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
5082 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
5083 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
5084 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
5086 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5090 nfs4_init_nonuniform_client_string(struct nfs_client *clp)
5095 if (clp->cl_owner_id != NULL)
5099 len = 14 + strlen(clp->cl_ipaddr) + 1 +
5100 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5102 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5106 if (len > NFS4_OPAQUE_LIMIT + 1)
5110 * Since this string is allocated at mount time, and held until the
5111 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5112 * about a memory-reclaim deadlock.
5114 str = kmalloc(len, GFP_KERNEL);
5119 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
5121 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5122 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
5125 clp->cl_owner_id = str;
5130 nfs4_init_uniquifier_client_string(struct nfs_client *clp)
5135 len = 10 + 10 + 1 + 10 + 1 +
5136 strlen(nfs4_client_id_uniquifier) + 1 +
5137 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5139 if (len > NFS4_OPAQUE_LIMIT + 1)
5143 * Since this string is allocated at mount time, and held until the
5144 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5145 * about a memory-reclaim deadlock.
5147 str = kmalloc(len, GFP_KERNEL);
5151 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
5152 clp->rpc_ops->version, clp->cl_minorversion,
5153 nfs4_client_id_uniquifier,
5154 clp->cl_rpcclient->cl_nodename);
5155 clp->cl_owner_id = str;
5160 nfs4_init_uniform_client_string(struct nfs_client *clp)
5165 if (clp->cl_owner_id != NULL)
5168 if (nfs4_client_id_uniquifier[0] != '\0')
5169 return nfs4_init_uniquifier_client_string(clp);
5171 len = 10 + 10 + 1 + 10 + 1 +
5172 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5174 if (len > NFS4_OPAQUE_LIMIT + 1)
5178 * Since this string is allocated at mount time, and held until the
5179 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5180 * about a memory-reclaim deadlock.
5182 str = kmalloc(len, GFP_KERNEL);
5186 scnprintf(str, len, "Linux NFSv%u.%u %s",
5187 clp->rpc_ops->version, clp->cl_minorversion,
5188 clp->cl_rpcclient->cl_nodename);
5189 clp->cl_owner_id = str;
5194 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5195 * services. Advertise one based on the address family of the
5199 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5201 if (strchr(clp->cl_ipaddr, ':') != NULL)
5202 return scnprintf(buf, len, "tcp6");
5204 return scnprintf(buf, len, "tcp");
5207 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5209 struct nfs4_setclientid *sc = calldata;
5211 if (task->tk_status == 0)
5212 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5215 static const struct rpc_call_ops nfs4_setclientid_ops = {
5216 .rpc_call_done = nfs4_setclientid_done,
5220 * nfs4_proc_setclientid - Negotiate client ID
5221 * @clp: state data structure
5222 * @program: RPC program for NFSv4 callback service
5223 * @port: IP port number for NFS4 callback service
5224 * @cred: RPC credential to use for this call
5225 * @res: where to place the result
5227 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5229 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5230 unsigned short port, struct rpc_cred *cred,
5231 struct nfs4_setclientid_res *res)
5233 nfs4_verifier sc_verifier;
5234 struct nfs4_setclientid setclientid = {
5235 .sc_verifier = &sc_verifier,
5239 struct rpc_message msg = {
5240 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5241 .rpc_argp = &setclientid,
5245 struct rpc_task *task;
5246 struct rpc_task_setup task_setup_data = {
5247 .rpc_client = clp->cl_rpcclient,
5248 .rpc_message = &msg,
5249 .callback_ops = &nfs4_setclientid_ops,
5250 .callback_data = &setclientid,
5251 .flags = RPC_TASK_TIMEOUT,
5255 /* nfs_client_id4 */
5256 nfs4_init_boot_verifier(clp, &sc_verifier);
5258 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5259 status = nfs4_init_uniform_client_string(clp);
5261 status = nfs4_init_nonuniform_client_string(clp);
5267 setclientid.sc_netid_len =
5268 nfs4_init_callback_netid(clp,
5269 setclientid.sc_netid,
5270 sizeof(setclientid.sc_netid));
5271 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
5272 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
5273 clp->cl_ipaddr, port >> 8, port & 255);
5275 dprintk("NFS call setclientid auth=%s, '%s'\n",
5276 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5278 task = rpc_run_task(&task_setup_data);
5280 status = PTR_ERR(task);
5283 status = task->tk_status;
5284 if (setclientid.sc_cred) {
5285 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5286 put_rpccred(setclientid.sc_cred);
5290 trace_nfs4_setclientid(clp, status);
5291 dprintk("NFS reply setclientid: %d\n", status);
5296 * nfs4_proc_setclientid_confirm - Confirm client ID
5297 * @clp: state data structure
5298 * @res: result of a previous SETCLIENTID
5299 * @cred: RPC credential to use for this call
5301 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5303 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
5304 struct nfs4_setclientid_res *arg,
5305 struct rpc_cred *cred)
5307 struct rpc_message msg = {
5308 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
5314 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5315 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5317 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5318 trace_nfs4_setclientid_confirm(clp, status);
5319 dprintk("NFS reply setclientid_confirm: %d\n", status);
5323 struct nfs4_delegreturndata {
5324 struct nfs4_delegreturnargs args;
5325 struct nfs4_delegreturnres res;
5327 nfs4_stateid stateid;
5328 unsigned long timestamp;
5329 struct nfs_fattr fattr;
5331 struct inode *inode;
5336 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5338 struct nfs4_delegreturndata *data = calldata;
5340 if (!nfs4_sequence_done(task, &data->res.seq_res))
5343 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
5344 switch (task->tk_status) {
5346 renew_lease(data->res.server, data->timestamp);
5347 case -NFS4ERR_ADMIN_REVOKED:
5348 case -NFS4ERR_DELEG_REVOKED:
5349 case -NFS4ERR_BAD_STATEID:
5350 case -NFS4ERR_OLD_STATEID:
5351 case -NFS4ERR_STALE_STATEID:
5352 case -NFS4ERR_EXPIRED:
5353 task->tk_status = 0;
5355 pnfs_roc_set_barrier(data->inode, data->roc_barrier);
5358 if (nfs4_async_handle_error(task, data->res.server,
5359 NULL, NULL) == -EAGAIN) {
5360 rpc_restart_call_prepare(task);
5364 data->rpc_status = task->tk_status;
5367 static void nfs4_delegreturn_release(void *calldata)
5369 struct nfs4_delegreturndata *data = calldata;
5370 struct inode *inode = data->inode;
5374 pnfs_roc_release(inode);
5375 nfs_iput_and_deactive(inode);
5380 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5382 struct nfs4_delegreturndata *d_data;
5384 d_data = (struct nfs4_delegreturndata *)data;
5386 if (nfs4_wait_on_layoutreturn(d_data->inode, task))
5390 pnfs_roc_get_barrier(d_data->inode, &d_data->roc_barrier);
5392 nfs4_setup_sequence(d_data->res.server,
5393 &d_data->args.seq_args,
5394 &d_data->res.seq_res,
5398 static const struct rpc_call_ops nfs4_delegreturn_ops = {
5399 .rpc_call_prepare = nfs4_delegreturn_prepare,
5400 .rpc_call_done = nfs4_delegreturn_done,
5401 .rpc_release = nfs4_delegreturn_release,
5404 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
5406 struct nfs4_delegreturndata *data;
5407 struct nfs_server *server = NFS_SERVER(inode);
5408 struct rpc_task *task;
5409 struct rpc_message msg = {
5410 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5413 struct rpc_task_setup task_setup_data = {
5414 .rpc_client = server->client,
5415 .rpc_message = &msg,
5416 .callback_ops = &nfs4_delegreturn_ops,
5417 .flags = RPC_TASK_ASYNC,
5421 data = kzalloc(sizeof(*data), GFP_NOFS);
5424 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
5426 nfs4_state_protect(server->nfs_client,
5427 NFS_SP4_MACH_CRED_CLEANUP,
5428 &task_setup_data.rpc_client, &msg);
5430 data->args.fhandle = &data->fh;
5431 data->args.stateid = &data->stateid;
5432 data->args.bitmask = server->cache_consistency_bitmask;
5433 nfs_copy_fh(&data->fh, NFS_FH(inode));
5434 nfs4_stateid_copy(&data->stateid, stateid);
5435 data->res.fattr = &data->fattr;
5436 data->res.server = server;
5437 nfs_fattr_init(data->res.fattr);
5438 data->timestamp = jiffies;
5439 data->rpc_status = 0;
5440 data->inode = nfs_igrab_and_active(inode);
5442 data->roc = nfs4_roc(inode);
5444 task_setup_data.callback_data = data;
5445 msg.rpc_argp = &data->args;
5446 msg.rpc_resp = &data->res;
5447 task = rpc_run_task(&task_setup_data);
5449 return PTR_ERR(task);
5452 status = nfs4_wait_for_completion_rpc_task(task);
5455 status = data->rpc_status;
5457 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5459 nfs_refresh_inode(inode, &data->fattr);
5465 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
5467 struct nfs_server *server = NFS_SERVER(inode);
5468 struct nfs4_exception exception = { };
5471 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
5472 trace_nfs4_delegreturn(inode, stateid, err);
5474 case -NFS4ERR_STALE_STATEID:
5475 case -NFS4ERR_EXPIRED:
5479 err = nfs4_handle_exception(server, err, &exception);
5480 } while (exception.retry);
5484 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
5485 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
5488 * sleep, with exponential backoff, and retry the LOCK operation.
5490 static unsigned long
5491 nfs4_set_lock_task_retry(unsigned long timeout)
5493 freezable_schedule_timeout_killable_unsafe(timeout);
5495 if (timeout > NFS4_LOCK_MAXTIMEOUT)
5496 return NFS4_LOCK_MAXTIMEOUT;
5500 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5502 struct inode *inode = state->inode;
5503 struct nfs_server *server = NFS_SERVER(inode);
5504 struct nfs_client *clp = server->nfs_client;
5505 struct nfs_lockt_args arg = {
5506 .fh = NFS_FH(inode),
5509 struct nfs_lockt_res res = {
5512 struct rpc_message msg = {
5513 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5516 .rpc_cred = state->owner->so_cred,
5518 struct nfs4_lock_state *lsp;
5521 arg.lock_owner.clientid = clp->cl_clientid;
5522 status = nfs4_set_lock_state(state, request);
5525 lsp = request->fl_u.nfs4_fl.owner;
5526 arg.lock_owner.id = lsp->ls_seqid.owner_id;
5527 arg.lock_owner.s_dev = server->s_dev;
5528 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5531 request->fl_type = F_UNLCK;
5533 case -NFS4ERR_DENIED:
5536 request->fl_ops->fl_release_private(request);
5537 request->fl_ops = NULL;
5542 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5544 struct nfs4_exception exception = { };
5548 err = _nfs4_proc_getlk(state, cmd, request);
5549 trace_nfs4_get_lock(request, state, cmd, err);
5550 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
5552 } while (exception.retry);
5556 static int do_vfs_lock(struct inode *inode, struct file_lock *fl)
5558 return locks_lock_inode_wait(inode, fl);
5561 struct nfs4_unlockdata {
5562 struct nfs_locku_args arg;
5563 struct nfs_locku_res res;
5564 struct nfs4_lock_state *lsp;
5565 struct nfs_open_context *ctx;
5566 struct file_lock fl;
5567 struct nfs_server *server;
5568 unsigned long timestamp;
5571 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5572 struct nfs_open_context *ctx,
5573 struct nfs4_lock_state *lsp,
5574 struct nfs_seqid *seqid)
5576 struct nfs4_unlockdata *p;
5577 struct inode *inode = lsp->ls_state->inode;
5579 p = kzalloc(sizeof(*p), GFP_NOFS);
5582 p->arg.fh = NFS_FH(inode);
5584 p->arg.seqid = seqid;
5585 p->res.seqid = seqid;
5587 atomic_inc(&lsp->ls_count);
5588 /* Ensure we don't close file until we're done freeing locks! */
5589 p->ctx = get_nfs_open_context(ctx);
5590 memcpy(&p->fl, fl, sizeof(p->fl));
5591 p->server = NFS_SERVER(inode);
5595 static void nfs4_locku_release_calldata(void *data)
5597 struct nfs4_unlockdata *calldata = data;
5598 nfs_free_seqid(calldata->arg.seqid);
5599 nfs4_put_lock_state(calldata->lsp);
5600 put_nfs_open_context(calldata->ctx);
5604 static void nfs4_locku_done(struct rpc_task *task, void *data)
5606 struct nfs4_unlockdata *calldata = data;
5608 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5610 switch (task->tk_status) {
5612 renew_lease(calldata->server, calldata->timestamp);
5613 do_vfs_lock(calldata->lsp->ls_state->inode, &calldata->fl);
5614 if (nfs4_update_lock_stateid(calldata->lsp,
5615 &calldata->res.stateid))
5617 case -NFS4ERR_BAD_STATEID:
5618 case -NFS4ERR_OLD_STATEID:
5619 case -NFS4ERR_STALE_STATEID:
5620 case -NFS4ERR_EXPIRED:
5621 if (!nfs4_stateid_match(&calldata->arg.stateid,
5622 &calldata->lsp->ls_stateid))
5623 rpc_restart_call_prepare(task);
5626 if (nfs4_async_handle_error(task, calldata->server,
5627 NULL, NULL) == -EAGAIN)
5628 rpc_restart_call_prepare(task);
5630 nfs_release_seqid(calldata->arg.seqid);
5633 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
5635 struct nfs4_unlockdata *calldata = data;
5637 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
5639 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
5640 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
5641 /* Note: exit _without_ running nfs4_locku_done */
5644 calldata->timestamp = jiffies;
5645 if (nfs4_setup_sequence(calldata->server,
5646 &calldata->arg.seq_args,
5647 &calldata->res.seq_res,
5649 nfs_release_seqid(calldata->arg.seqid);
5652 task->tk_action = NULL;
5654 nfs4_sequence_done(task, &calldata->res.seq_res);
5657 static const struct rpc_call_ops nfs4_locku_ops = {
5658 .rpc_call_prepare = nfs4_locku_prepare,
5659 .rpc_call_done = nfs4_locku_done,
5660 .rpc_release = nfs4_locku_release_calldata,
5663 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5664 struct nfs_open_context *ctx,
5665 struct nfs4_lock_state *lsp,
5666 struct nfs_seqid *seqid)
5668 struct nfs4_unlockdata *data;
5669 struct rpc_message msg = {
5670 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5671 .rpc_cred = ctx->cred,
5673 struct rpc_task_setup task_setup_data = {
5674 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
5675 .rpc_message = &msg,
5676 .callback_ops = &nfs4_locku_ops,
5677 .workqueue = nfsiod_workqueue,
5678 .flags = RPC_TASK_ASYNC,
5681 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5682 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5684 /* Ensure this is an unlock - when canceling a lock, the
5685 * canceled lock is passed in, and it won't be an unlock.
5687 fl->fl_type = F_UNLCK;
5689 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5691 nfs_free_seqid(seqid);
5692 return ERR_PTR(-ENOMEM);
5695 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
5696 msg.rpc_argp = &data->arg;
5697 msg.rpc_resp = &data->res;
5698 task_setup_data.callback_data = data;
5699 return rpc_run_task(&task_setup_data);
5702 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5704 struct inode *inode = state->inode;
5705 struct nfs4_state_owner *sp = state->owner;
5706 struct nfs_inode *nfsi = NFS_I(inode);
5707 struct nfs_seqid *seqid;
5708 struct nfs4_lock_state *lsp;
5709 struct rpc_task *task;
5710 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
5712 unsigned char fl_flags = request->fl_flags;
5714 status = nfs4_set_lock_state(state, request);
5715 /* Unlock _before_ we do the RPC call */
5716 request->fl_flags |= FL_EXISTS;
5717 /* Exclude nfs_delegation_claim_locks() */
5718 mutex_lock(&sp->so_delegreturn_mutex);
5719 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
5720 down_read(&nfsi->rwsem);
5721 if (do_vfs_lock(inode, request) == -ENOENT) {
5722 up_read(&nfsi->rwsem);
5723 mutex_unlock(&sp->so_delegreturn_mutex);
5726 up_read(&nfsi->rwsem);
5727 mutex_unlock(&sp->so_delegreturn_mutex);
5730 /* Is this a delegated lock? */
5731 lsp = request->fl_u.nfs4_fl.owner;
5732 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5734 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
5735 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
5739 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
5740 status = PTR_ERR(task);
5743 status = nfs4_wait_for_completion_rpc_task(task);
5746 request->fl_flags = fl_flags;
5747 trace_nfs4_unlock(request, state, F_SETLK, status);
5751 struct nfs4_lockdata {
5752 struct nfs_lock_args arg;
5753 struct nfs_lock_res res;
5754 struct nfs4_lock_state *lsp;
5755 struct nfs_open_context *ctx;
5756 struct file_lock fl;
5757 unsigned long timestamp;
5760 struct nfs_server *server;
5763 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
5764 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5767 struct nfs4_lockdata *p;
5768 struct inode *inode = lsp->ls_state->inode;
5769 struct nfs_server *server = NFS_SERVER(inode);
5770 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
5772 p = kzalloc(sizeof(*p), gfp_mask);
5776 p->arg.fh = NFS_FH(inode);
5778 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
5779 if (IS_ERR(p->arg.open_seqid))
5781 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
5782 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
5783 if (IS_ERR(p->arg.lock_seqid))
5784 goto out_free_seqid;
5785 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
5786 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
5787 p->arg.lock_owner.s_dev = server->s_dev;
5788 p->res.lock_seqid = p->arg.lock_seqid;
5791 atomic_inc(&lsp->ls_count);
5792 p->ctx = get_nfs_open_context(ctx);
5793 get_file(fl->fl_file);
5794 memcpy(&p->fl, fl, sizeof(p->fl));
5797 nfs_free_seqid(p->arg.open_seqid);
5803 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5805 struct nfs4_lockdata *data = calldata;
5806 struct nfs4_state *state = data->lsp->ls_state;
5808 dprintk("%s: begin!\n", __func__);
5809 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
5811 /* Do we need to do an open_to_lock_owner? */
5812 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
5813 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
5814 goto out_release_lock_seqid;
5816 nfs4_stateid_copy(&data->arg.open_stateid,
5817 &state->open_stateid);
5818 data->arg.new_lock_owner = 1;
5819 data->res.open_seqid = data->arg.open_seqid;
5821 data->arg.new_lock_owner = 0;
5822 nfs4_stateid_copy(&data->arg.lock_stateid,
5823 &data->lsp->ls_stateid);
5825 if (!nfs4_valid_open_stateid(state)) {
5826 data->rpc_status = -EBADF;
5827 task->tk_action = NULL;
5828 goto out_release_open_seqid;
5830 data->timestamp = jiffies;
5831 if (nfs4_setup_sequence(data->server,
5832 &data->arg.seq_args,
5836 out_release_open_seqid:
5837 nfs_release_seqid(data->arg.open_seqid);
5838 out_release_lock_seqid:
5839 nfs_release_seqid(data->arg.lock_seqid);
5841 nfs4_sequence_done(task, &data->res.seq_res);
5842 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
5845 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5847 struct nfs4_lockdata *data = calldata;
5848 struct nfs4_lock_state *lsp = data->lsp;
5850 dprintk("%s: begin!\n", __func__);
5852 if (!nfs4_sequence_done(task, &data->res.seq_res))
5855 data->rpc_status = task->tk_status;
5856 switch (task->tk_status) {
5858 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
5860 if (data->arg.new_lock) {
5861 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
5862 if (do_vfs_lock(lsp->ls_state->inode, &data->fl) < 0) {
5863 rpc_restart_call_prepare(task);
5867 if (data->arg.new_lock_owner != 0) {
5868 nfs_confirm_seqid(&lsp->ls_seqid, 0);
5869 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
5870 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
5871 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
5872 rpc_restart_call_prepare(task);
5874 case -NFS4ERR_BAD_STATEID:
5875 case -NFS4ERR_OLD_STATEID:
5876 case -NFS4ERR_STALE_STATEID:
5877 case -NFS4ERR_EXPIRED:
5878 if (data->arg.new_lock_owner != 0) {
5879 if (!nfs4_stateid_match(&data->arg.open_stateid,
5880 &lsp->ls_state->open_stateid))
5881 rpc_restart_call_prepare(task);
5882 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
5884 rpc_restart_call_prepare(task);
5886 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
5889 static void nfs4_lock_release(void *calldata)
5891 struct nfs4_lockdata *data = calldata;
5893 dprintk("%s: begin!\n", __func__);
5894 nfs_free_seqid(data->arg.open_seqid);
5895 if (data->cancelled != 0) {
5896 struct rpc_task *task;
5897 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5898 data->arg.lock_seqid);
5900 rpc_put_task_async(task);
5901 dprintk("%s: cancelling lock!\n", __func__);
5903 nfs_free_seqid(data->arg.lock_seqid);
5904 nfs4_put_lock_state(data->lsp);
5905 put_nfs_open_context(data->ctx);
5906 fput(data->fl.fl_file);
5908 dprintk("%s: done!\n", __func__);
5911 static const struct rpc_call_ops nfs4_lock_ops = {
5912 .rpc_call_prepare = nfs4_lock_prepare,
5913 .rpc_call_done = nfs4_lock_done,
5914 .rpc_release = nfs4_lock_release,
5917 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5920 case -NFS4ERR_ADMIN_REVOKED:
5921 case -NFS4ERR_BAD_STATEID:
5922 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
5923 if (new_lock_owner != 0 ||
5924 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
5925 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
5927 case -NFS4ERR_STALE_STATEID:
5928 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
5929 case -NFS4ERR_EXPIRED:
5930 nfs4_schedule_lease_recovery(server->nfs_client);
5934 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
5936 struct nfs4_lockdata *data;
5937 struct rpc_task *task;
5938 struct rpc_message msg = {
5939 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5940 .rpc_cred = state->owner->so_cred,
5942 struct rpc_task_setup task_setup_data = {
5943 .rpc_client = NFS_CLIENT(state->inode),
5944 .rpc_message = &msg,
5945 .callback_ops = &nfs4_lock_ops,
5946 .workqueue = nfsiod_workqueue,
5947 .flags = RPC_TASK_ASYNC,
5951 dprintk("%s: begin!\n", __func__);
5952 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
5953 fl->fl_u.nfs4_fl.owner,
5954 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
5958 data->arg.block = 1;
5959 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
5960 msg.rpc_argp = &data->arg;
5961 msg.rpc_resp = &data->res;
5962 task_setup_data.callback_data = data;
5963 if (recovery_type > NFS_LOCK_NEW) {
5964 if (recovery_type == NFS_LOCK_RECLAIM)
5965 data->arg.reclaim = NFS_LOCK_RECLAIM;
5966 nfs4_set_sequence_privileged(&data->arg.seq_args);
5968 data->arg.new_lock = 1;
5969 task = rpc_run_task(&task_setup_data);
5971 return PTR_ERR(task);
5972 ret = nfs4_wait_for_completion_rpc_task(task);
5974 ret = data->rpc_status;
5976 nfs4_handle_setlk_error(data->server, data->lsp,
5977 data->arg.new_lock_owner, ret);
5979 data->cancelled = 1;
5981 dprintk("%s: done, ret = %d!\n", __func__, ret);
5982 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
5986 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
5988 struct nfs_server *server = NFS_SERVER(state->inode);
5989 struct nfs4_exception exception = {
5990 .inode = state->inode,
5995 /* Cache the lock if possible... */
5996 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5998 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
5999 if (err != -NFS4ERR_DELAY)
6001 nfs4_handle_exception(server, err, &exception);
6002 } while (exception.retry);
6006 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6008 struct nfs_server *server = NFS_SERVER(state->inode);
6009 struct nfs4_exception exception = {
6010 .inode = state->inode,
6014 err = nfs4_set_lock_state(state, request);
6017 if (!recover_lost_locks) {
6018 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6022 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6024 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
6028 case -NFS4ERR_GRACE:
6029 case -NFS4ERR_DELAY:
6030 nfs4_handle_exception(server, err, &exception);
6033 } while (exception.retry);
6038 #if defined(CONFIG_NFS_V4_1)
6040 * nfs41_check_expired_locks - possibly free a lock stateid
6042 * @state: NFSv4 state for an inode
6044 * Returns NFS_OK if recovery for this stateid is now finished.
6045 * Otherwise a negative NFS4ERR value is returned.
6047 static int nfs41_check_expired_locks(struct nfs4_state *state)
6049 int status, ret = -NFS4ERR_BAD_STATEID;
6050 struct nfs4_lock_state *lsp;
6051 struct nfs_server *server = NFS_SERVER(state->inode);
6053 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
6054 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
6055 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
6057 status = nfs41_test_stateid(server,
6060 trace_nfs4_test_lock_stateid(state, lsp, status);
6061 if (status != NFS_OK) {
6062 /* Free the stateid unless the server
6063 * informs us the stateid is unrecognized. */
6064 if (status != -NFS4ERR_BAD_STATEID)
6065 nfs41_free_stateid(server,
6068 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6077 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6079 int status = NFS_OK;
6081 if (test_bit(LK_STATE_IN_USE, &state->flags))
6082 status = nfs41_check_expired_locks(state);
6083 if (status != NFS_OK)
6084 status = nfs4_lock_expired(state, request);
6089 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6091 struct nfs_inode *nfsi = NFS_I(state->inode);
6092 struct nfs4_state_owner *sp = state->owner;
6093 unsigned char fl_flags = request->fl_flags;
6094 int status = -ENOLCK;
6096 if ((fl_flags & FL_POSIX) &&
6097 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6099 /* Is this a delegated open? */
6100 status = nfs4_set_lock_state(state, request);
6103 request->fl_flags |= FL_ACCESS;
6104 status = do_vfs_lock(state->inode, request);
6107 mutex_lock(&sp->so_delegreturn_mutex);
6108 down_read(&nfsi->rwsem);
6109 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
6110 /* Yes: cache locks! */
6111 /* ...but avoid races with delegation recall... */
6112 request->fl_flags = fl_flags & ~FL_SLEEP;
6113 status = do_vfs_lock(state->inode, request);
6114 up_read(&nfsi->rwsem);
6115 mutex_unlock(&sp->so_delegreturn_mutex);
6118 up_read(&nfsi->rwsem);
6119 mutex_unlock(&sp->so_delegreturn_mutex);
6120 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
6122 request->fl_flags = fl_flags;
6126 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6128 struct nfs4_exception exception = {
6130 .inode = state->inode,
6135 err = _nfs4_proc_setlk(state, cmd, request);
6136 if (err == -NFS4ERR_DENIED)
6138 err = nfs4_handle_exception(NFS_SERVER(state->inode),
6140 } while (exception.retry);
6145 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6147 struct nfs_open_context *ctx;
6148 struct nfs4_state *state;
6149 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6152 /* verify open state */
6153 ctx = nfs_file_open_context(filp);
6156 if (request->fl_start < 0 || request->fl_end < 0)
6159 if (IS_GETLK(cmd)) {
6161 return nfs4_proc_getlk(state, F_GETLK, request);
6165 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6168 if (request->fl_type == F_UNLCK) {
6170 return nfs4_proc_unlck(state, cmd, request);
6177 * Don't rely on the VFS having checked the file open mode,
6178 * since it won't do this for flock() locks.
6180 switch (request->fl_type) {
6182 if (!(filp->f_mode & FMODE_READ))
6186 if (!(filp->f_mode & FMODE_WRITE))
6191 status = nfs4_proc_setlk(state, cmd, request);
6192 if ((status != -EAGAIN) || IS_SETLK(cmd))
6194 timeout = nfs4_set_lock_task_retry(timeout);
6195 status = -ERESTARTSYS;
6198 } while(status < 0);
6202 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
6204 struct nfs_server *server = NFS_SERVER(state->inode);
6207 err = nfs4_set_lock_state(state, fl);
6210 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
6211 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
6214 struct nfs_release_lockowner_data {
6215 struct nfs4_lock_state *lsp;
6216 struct nfs_server *server;
6217 struct nfs_release_lockowner_args args;
6218 struct nfs_release_lockowner_res res;
6219 unsigned long timestamp;
6222 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6224 struct nfs_release_lockowner_data *data = calldata;
6225 struct nfs_server *server = data->server;
6226 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
6227 &data->args.seq_args, &data->res.seq_res, task);
6228 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6229 data->timestamp = jiffies;
6232 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6234 struct nfs_release_lockowner_data *data = calldata;
6235 struct nfs_server *server = data->server;
6237 nfs40_sequence_done(task, &data->res.seq_res);
6239 switch (task->tk_status) {
6241 renew_lease(server, data->timestamp);
6243 case -NFS4ERR_STALE_CLIENTID:
6244 case -NFS4ERR_EXPIRED:
6245 nfs4_schedule_lease_recovery(server->nfs_client);
6247 case -NFS4ERR_LEASE_MOVED:
6248 case -NFS4ERR_DELAY:
6249 if (nfs4_async_handle_error(task, server,
6250 NULL, NULL) == -EAGAIN)
6251 rpc_restart_call_prepare(task);
6255 static void nfs4_release_lockowner_release(void *calldata)
6257 struct nfs_release_lockowner_data *data = calldata;
6258 nfs4_free_lock_state(data->server, data->lsp);
6262 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
6263 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6264 .rpc_call_done = nfs4_release_lockowner_done,
6265 .rpc_release = nfs4_release_lockowner_release,
6269 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
6271 struct nfs_release_lockowner_data *data;
6272 struct rpc_message msg = {
6273 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6276 if (server->nfs_client->cl_mvops->minor_version != 0)
6279 data = kmalloc(sizeof(*data), GFP_NOFS);
6283 data->server = server;
6284 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6285 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6286 data->args.lock_owner.s_dev = server->s_dev;
6288 msg.rpc_argp = &data->args;
6289 msg.rpc_resp = &data->res;
6290 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
6291 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
6294 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6296 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
6297 struct dentry *unused, struct inode *inode,
6298 const char *key, const void *buf,
6299 size_t buflen, int flags)
6301 return nfs4_proc_set_acl(inode, buf, buflen);
6304 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
6305 struct dentry *unused, struct inode *inode,
6306 const char *key, void *buf, size_t buflen)
6308 return nfs4_proc_get_acl(inode, buf, buflen);
6311 static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
6313 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
6316 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
6318 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
6319 struct dentry *unused, struct inode *inode,
6320 const char *key, const void *buf,
6321 size_t buflen, int flags)
6323 if (security_ismaclabel(key))
6324 return nfs4_set_security_label(inode, buf, buflen);
6329 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
6330 struct dentry *unused, struct inode *inode,
6331 const char *key, void *buf, size_t buflen)
6333 if (security_ismaclabel(key))
6334 return nfs4_get_security_label(inode, buf, buflen);
6339 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6343 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6344 len = security_inode_listsecurity(inode, list, list_len);
6345 if (list_len && len > list_len)
6351 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6352 .prefix = XATTR_SECURITY_PREFIX,
6353 .get = nfs4_xattr_get_nfs4_label,
6354 .set = nfs4_xattr_set_nfs4_label,
6360 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6368 * nfs_fhget will use either the mounted_on_fileid or the fileid
6370 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6372 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6373 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6374 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
6375 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
6378 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
6379 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
6380 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6384 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6385 const struct qstr *name,
6386 struct nfs4_fs_locations *fs_locations,
6389 struct nfs_server *server = NFS_SERVER(dir);
6391 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6393 struct nfs4_fs_locations_arg args = {
6394 .dir_fh = NFS_FH(dir),
6399 struct nfs4_fs_locations_res res = {
6400 .fs_locations = fs_locations,
6402 struct rpc_message msg = {
6403 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6409 dprintk("%s: start\n", __func__);
6411 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6412 * is not supported */
6413 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6414 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6416 bitmask[0] |= FATTR4_WORD0_FILEID;
6418 nfs_fattr_init(&fs_locations->fattr);
6419 fs_locations->server = server;
6420 fs_locations->nlocations = 0;
6421 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
6422 dprintk("%s: returned status = %d\n", __func__, status);
6426 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6427 const struct qstr *name,
6428 struct nfs4_fs_locations *fs_locations,
6431 struct nfs4_exception exception = { };
6434 err = _nfs4_proc_fs_locations(client, dir, name,
6435 fs_locations, page);
6436 trace_nfs4_get_fs_locations(dir, name, err);
6437 err = nfs4_handle_exception(NFS_SERVER(dir), err,
6439 } while (exception.retry);
6444 * This operation also signals the server that this client is
6445 * performing migration recovery. The server can stop returning
6446 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6447 * appended to this compound to identify the client ID which is
6448 * performing recovery.
6450 static int _nfs40_proc_get_locations(struct inode *inode,
6451 struct nfs4_fs_locations *locations,
6452 struct page *page, struct rpc_cred *cred)
6454 struct nfs_server *server = NFS_SERVER(inode);
6455 struct rpc_clnt *clnt = server->client;
6457 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6459 struct nfs4_fs_locations_arg args = {
6460 .clientid = server->nfs_client->cl_clientid,
6461 .fh = NFS_FH(inode),
6464 .migration = 1, /* skip LOOKUP */
6465 .renew = 1, /* append RENEW */
6467 struct nfs4_fs_locations_res res = {
6468 .fs_locations = locations,
6472 struct rpc_message msg = {
6473 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6478 unsigned long now = jiffies;
6481 nfs_fattr_init(&locations->fattr);
6482 locations->server = server;
6483 locations->nlocations = 0;
6485 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6486 nfs4_set_sequence_privileged(&args.seq_args);
6487 status = nfs4_call_sync_sequence(clnt, server, &msg,
6488 &args.seq_args, &res.seq_res);
6492 renew_lease(server, now);
6496 #ifdef CONFIG_NFS_V4_1
6499 * This operation also signals the server that this client is
6500 * performing migration recovery. The server can stop asserting
6501 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6502 * performing this operation is identified in the SEQUENCE
6503 * operation in this compound.
6505 * When the client supports GETATTR(fs_locations_info), it can
6506 * be plumbed in here.
6508 static int _nfs41_proc_get_locations(struct inode *inode,
6509 struct nfs4_fs_locations *locations,
6510 struct page *page, struct rpc_cred *cred)
6512 struct nfs_server *server = NFS_SERVER(inode);
6513 struct rpc_clnt *clnt = server->client;
6515 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6517 struct nfs4_fs_locations_arg args = {
6518 .fh = NFS_FH(inode),
6521 .migration = 1, /* skip LOOKUP */
6523 struct nfs4_fs_locations_res res = {
6524 .fs_locations = locations,
6527 struct rpc_message msg = {
6528 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6535 nfs_fattr_init(&locations->fattr);
6536 locations->server = server;
6537 locations->nlocations = 0;
6539 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6540 nfs4_set_sequence_privileged(&args.seq_args);
6541 status = nfs4_call_sync_sequence(clnt, server, &msg,
6542 &args.seq_args, &res.seq_res);
6543 if (status == NFS4_OK &&
6544 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6545 status = -NFS4ERR_LEASE_MOVED;
6549 #endif /* CONFIG_NFS_V4_1 */
6552 * nfs4_proc_get_locations - discover locations for a migrated FSID
6553 * @inode: inode on FSID that is migrating
6554 * @locations: result of query
6556 * @cred: credential to use for this operation
6558 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6559 * operation failed, or a negative errno if a local error occurred.
6561 * On success, "locations" is filled in, but if the server has
6562 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6565 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6566 * from this client that require migration recovery.
6568 int nfs4_proc_get_locations(struct inode *inode,
6569 struct nfs4_fs_locations *locations,
6570 struct page *page, struct rpc_cred *cred)
6572 struct nfs_server *server = NFS_SERVER(inode);
6573 struct nfs_client *clp = server->nfs_client;
6574 const struct nfs4_mig_recovery_ops *ops =
6575 clp->cl_mvops->mig_recovery_ops;
6576 struct nfs4_exception exception = { };
6579 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6580 (unsigned long long)server->fsid.major,
6581 (unsigned long long)server->fsid.minor,
6583 nfs_display_fhandle(NFS_FH(inode), __func__);
6586 status = ops->get_locations(inode, locations, page, cred);
6587 if (status != -NFS4ERR_DELAY)
6589 nfs4_handle_exception(server, status, &exception);
6590 } while (exception.retry);
6595 * This operation also signals the server that this client is
6596 * performing "lease moved" recovery. The server can stop
6597 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6598 * is appended to this compound to identify the client ID which is
6599 * performing recovery.
6601 static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6603 struct nfs_server *server = NFS_SERVER(inode);
6604 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6605 struct rpc_clnt *clnt = server->client;
6606 struct nfs4_fsid_present_arg args = {
6607 .fh = NFS_FH(inode),
6608 .clientid = clp->cl_clientid,
6609 .renew = 1, /* append RENEW */
6611 struct nfs4_fsid_present_res res = {
6614 struct rpc_message msg = {
6615 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6620 unsigned long now = jiffies;
6623 res.fh = nfs_alloc_fhandle();
6627 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6628 nfs4_set_sequence_privileged(&args.seq_args);
6629 status = nfs4_call_sync_sequence(clnt, server, &msg,
6630 &args.seq_args, &res.seq_res);
6631 nfs_free_fhandle(res.fh);
6635 do_renew_lease(clp, now);
6639 #ifdef CONFIG_NFS_V4_1
6642 * This operation also signals the server that this client is
6643 * performing "lease moved" recovery. The server can stop asserting
6644 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6645 * this operation is identified in the SEQUENCE operation in this
6648 static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6650 struct nfs_server *server = NFS_SERVER(inode);
6651 struct rpc_clnt *clnt = server->client;
6652 struct nfs4_fsid_present_arg args = {
6653 .fh = NFS_FH(inode),
6655 struct nfs4_fsid_present_res res = {
6657 struct rpc_message msg = {
6658 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6665 res.fh = nfs_alloc_fhandle();
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);
6673 nfs_free_fhandle(res.fh);
6674 if (status == NFS4_OK &&
6675 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6676 status = -NFS4ERR_LEASE_MOVED;
6680 #endif /* CONFIG_NFS_V4_1 */
6683 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6684 * @inode: inode on FSID to check
6685 * @cred: credential to use for this operation
6687 * Server indicates whether the FSID is present, moved, or not
6688 * recognized. This operation is necessary to clear a LEASE_MOVED
6689 * condition for this client ID.
6691 * Returns NFS4_OK if the FSID is present on this server,
6692 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6693 * NFS4ERR code if some error occurred on the server, or a
6694 * negative errno if a local failure occurred.
6696 int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6698 struct nfs_server *server = NFS_SERVER(inode);
6699 struct nfs_client *clp = server->nfs_client;
6700 const struct nfs4_mig_recovery_ops *ops =
6701 clp->cl_mvops->mig_recovery_ops;
6702 struct nfs4_exception exception = { };
6705 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6706 (unsigned long long)server->fsid.major,
6707 (unsigned long long)server->fsid.minor,
6709 nfs_display_fhandle(NFS_FH(inode), __func__);
6712 status = ops->fsid_present(inode, cred);
6713 if (status != -NFS4ERR_DELAY)
6715 nfs4_handle_exception(server, status, &exception);
6716 } while (exception.retry);
6721 * If 'use_integrity' is true and the state managment nfs_client
6722 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
6723 * and the machine credential as per RFC3530bis and RFC5661 Security
6724 * Considerations sections. Otherwise, just use the user cred with the
6725 * filesystem's rpc_client.
6727 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
6730 struct nfs4_secinfo_arg args = {
6731 .dir_fh = NFS_FH(dir),
6734 struct nfs4_secinfo_res res = {
6737 struct rpc_message msg = {
6738 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
6742 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
6743 struct rpc_cred *cred = NULL;
6745 if (use_integrity) {
6746 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
6747 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
6748 msg.rpc_cred = cred;
6751 dprintk("NFS call secinfo %s\n", name->name);
6753 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
6754 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
6756 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
6758 dprintk("NFS reply secinfo: %d\n", status);
6766 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
6767 struct nfs4_secinfo_flavors *flavors)
6769 struct nfs4_exception exception = { };
6772 err = -NFS4ERR_WRONGSEC;
6774 /* try to use integrity protection with machine cred */
6775 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
6776 err = _nfs4_proc_secinfo(dir, name, flavors, true);
6779 * if unable to use integrity protection, or SECINFO with
6780 * integrity protection returns NFS4ERR_WRONGSEC (which is
6781 * disallowed by spec, but exists in deployed servers) use
6782 * the current filesystem's rpc_client and the user cred.
6784 if (err == -NFS4ERR_WRONGSEC)
6785 err = _nfs4_proc_secinfo(dir, name, flavors, false);
6787 trace_nfs4_secinfo(dir, name, err);
6788 err = nfs4_handle_exception(NFS_SERVER(dir), err,
6790 } while (exception.retry);
6794 #ifdef CONFIG_NFS_V4_1
6796 * Check the exchange flags returned by the server for invalid flags, having
6797 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
6800 static int nfs4_check_cl_exchange_flags(u32 flags)
6802 if (flags & ~EXCHGID4_FLAG_MASK_R)
6804 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
6805 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
6807 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
6811 return -NFS4ERR_INVAL;
6815 nfs41_same_server_scope(struct nfs41_server_scope *a,
6816 struct nfs41_server_scope *b)
6818 if (a->server_scope_sz == b->server_scope_sz &&
6819 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
6826 nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
6830 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
6831 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
6835 * nfs4_proc_bind_one_conn_to_session()
6837 * The 4.1 client currently uses the same TCP connection for the
6838 * fore and backchannel.
6841 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
6842 struct rpc_xprt *xprt,
6843 struct nfs_client *clp,
6844 struct rpc_cred *cred)
6847 struct nfs41_bind_conn_to_session_args args = {
6849 .dir = NFS4_CDFC4_FORE_OR_BOTH,
6851 struct nfs41_bind_conn_to_session_res res;
6852 struct rpc_message msg = {
6854 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
6859 struct rpc_task_setup task_setup_data = {
6862 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
6863 .rpc_message = &msg,
6864 .flags = RPC_TASK_TIMEOUT,
6866 struct rpc_task *task;
6868 dprintk("--> %s\n", __func__);
6870 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
6871 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
6872 args.dir = NFS4_CDFC4_FORE;
6874 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
6875 if (xprt != rcu_access_pointer(clnt->cl_xprt))
6876 args.dir = NFS4_CDFC4_FORE;
6878 task = rpc_run_task(&task_setup_data);
6879 if (!IS_ERR(task)) {
6880 status = task->tk_status;
6883 status = PTR_ERR(task);
6884 trace_nfs4_bind_conn_to_session(clp, status);
6886 if (memcmp(res.sessionid.data,
6887 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
6888 dprintk("NFS: %s: Session ID mismatch\n", __func__);
6892 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
6893 dprintk("NFS: %s: Unexpected direction from server\n",
6898 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
6899 dprintk("NFS: %s: Server returned RDMA mode = true\n",
6906 dprintk("<-- %s status= %d\n", __func__, status);
6910 struct rpc_bind_conn_calldata {
6911 struct nfs_client *clp;
6912 struct rpc_cred *cred;
6916 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
6917 struct rpc_xprt *xprt,
6920 struct rpc_bind_conn_calldata *p = calldata;
6922 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
6925 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
6927 struct rpc_bind_conn_calldata data = {
6931 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
6932 nfs4_proc_bind_conn_to_session_callback, &data);
6936 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
6937 * and operations we'd like to see to enable certain features in the allow map
6939 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
6940 .how = SP4_MACH_CRED,
6941 .enforce.u.words = {
6942 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6943 1 << (OP_EXCHANGE_ID - 32) |
6944 1 << (OP_CREATE_SESSION - 32) |
6945 1 << (OP_DESTROY_SESSION - 32) |
6946 1 << (OP_DESTROY_CLIENTID - 32)
6949 [0] = 1 << (OP_CLOSE) |
6950 1 << (OP_OPEN_DOWNGRADE) |
6952 1 << (OP_DELEGRETURN) |
6954 [1] = 1 << (OP_SECINFO - 32) |
6955 1 << (OP_SECINFO_NO_NAME - 32) |
6956 1 << (OP_LAYOUTRETURN - 32) |
6957 1 << (OP_TEST_STATEID - 32) |
6958 1 << (OP_FREE_STATEID - 32) |
6959 1 << (OP_WRITE - 32)
6964 * Select the state protection mode for client `clp' given the server results
6965 * from exchange_id in `sp'.
6967 * Returns 0 on success, negative errno otherwise.
6969 static int nfs4_sp4_select_mode(struct nfs_client *clp,
6970 struct nfs41_state_protection *sp)
6972 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
6973 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6974 1 << (OP_EXCHANGE_ID - 32) |
6975 1 << (OP_CREATE_SESSION - 32) |
6976 1 << (OP_DESTROY_SESSION - 32) |
6977 1 << (OP_DESTROY_CLIENTID - 32)
6981 if (sp->how == SP4_MACH_CRED) {
6982 /* Print state protect result */
6983 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
6984 for (i = 0; i <= LAST_NFS4_OP; i++) {
6985 if (test_bit(i, sp->enforce.u.longs))
6986 dfprintk(MOUNT, " enforce op %d\n", i);
6987 if (test_bit(i, sp->allow.u.longs))
6988 dfprintk(MOUNT, " allow op %d\n", i);
6991 /* make sure nothing is on enforce list that isn't supported */
6992 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
6993 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
6994 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7000 * Minimal mode - state operations are allowed to use machine
7001 * credential. Note this already happens by default, so the
7002 * client doesn't have to do anything more than the negotiation.
7004 * NOTE: we don't care if EXCHANGE_ID is in the list -
7005 * we're already using the machine cred for exchange_id
7006 * and will never use a different cred.
7008 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7009 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7010 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7011 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7012 dfprintk(MOUNT, "sp4_mach_cred:\n");
7013 dfprintk(MOUNT, " minimal mode enabled\n");
7014 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
7016 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7020 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
7021 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7022 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
7023 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7024 dfprintk(MOUNT, " cleanup mode enabled\n");
7025 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
7028 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7029 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7030 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
7031 &clp->cl_sp4_flags);
7034 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7035 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7036 dfprintk(MOUNT, " secinfo mode enabled\n");
7037 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
7040 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7041 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7042 dfprintk(MOUNT, " stateid mode enabled\n");
7043 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
7046 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7047 dfprintk(MOUNT, " write mode enabled\n");
7048 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
7051 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7052 dfprintk(MOUNT, " commit mode enabled\n");
7053 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
7061 * _nfs4_proc_exchange_id()
7063 * Wrapper for EXCHANGE_ID operation.
7065 static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
7068 nfs4_verifier verifier;
7069 struct nfs41_exchange_id_args args = {
7070 .verifier = &verifier,
7072 #ifdef CONFIG_NFS_V4_1_MIGRATION
7073 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7074 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7075 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7077 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7078 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7081 struct nfs41_exchange_id_res res = {
7085 struct rpc_message msg = {
7086 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
7092 nfs4_init_boot_verifier(clp, &verifier);
7094 status = nfs4_init_uniform_client_string(clp);
7098 dprintk("NFS call exchange_id auth=%s, '%s'\n",
7099 clp->cl_rpcclient->cl_auth->au_ops->au_name,
7102 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7104 if (unlikely(res.server_owner == NULL)) {
7109 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
7111 if (unlikely(res.server_scope == NULL)) {
7113 goto out_server_owner;
7116 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7117 if (unlikely(res.impl_id == NULL)) {
7119 goto out_server_scope;
7124 args.state_protect.how = SP4_NONE;
7128 args.state_protect = nfs4_sp4_mach_cred_request;
7138 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7139 trace_nfs4_exchange_id(clp, status);
7141 status = nfs4_check_cl_exchange_flags(res.flags);
7144 status = nfs4_sp4_select_mode(clp, &res.state_protect);
7147 clp->cl_clientid = res.clientid;
7148 clp->cl_exchange_flags = res.flags;
7149 /* Client ID is not confirmed */
7150 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
7151 clear_bit(NFS4_SESSION_ESTABLISHED,
7152 &clp->cl_session->session_state);
7153 clp->cl_seqid = res.seqid;
7156 kfree(clp->cl_serverowner);
7157 clp->cl_serverowner = res.server_owner;
7158 res.server_owner = NULL;
7160 /* use the most recent implementation id */
7161 kfree(clp->cl_implid);
7162 clp->cl_implid = res.impl_id;
7165 if (clp->cl_serverscope != NULL &&
7166 !nfs41_same_server_scope(clp->cl_serverscope,
7167 res.server_scope)) {
7168 dprintk("%s: server_scope mismatch detected\n",
7170 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
7171 kfree(clp->cl_serverscope);
7172 clp->cl_serverscope = NULL;
7175 if (clp->cl_serverscope == NULL) {
7176 clp->cl_serverscope = res.server_scope;
7177 res.server_scope = NULL;
7184 kfree(res.server_scope);
7186 kfree(res.server_owner);
7188 if (clp->cl_implid != NULL)
7189 dprintk("NFS reply exchange_id: Server Implementation ID: "
7190 "domain: %s, name: %s, date: %llu,%u\n",
7191 clp->cl_implid->domain, clp->cl_implid->name,
7192 clp->cl_implid->date.seconds,
7193 clp->cl_implid->date.nseconds);
7194 dprintk("NFS reply exchange_id: %d\n", status);
7199 * nfs4_proc_exchange_id()
7201 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7203 * Since the clientid has expired, all compounds using sessions
7204 * associated with the stale clientid will be returning
7205 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7206 * be in some phase of session reset.
7208 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7210 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7212 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7215 /* try SP4_MACH_CRED if krb5i/p */
7216 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7217 authflavor == RPC_AUTH_GSS_KRB5P) {
7218 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
7224 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
7227 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7228 struct rpc_cred *cred)
7230 struct rpc_message msg = {
7231 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7237 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7238 trace_nfs4_destroy_clientid(clp, status);
7240 dprintk("NFS: Got error %d from the server %s on "
7241 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7245 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7246 struct rpc_cred *cred)
7251 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7252 ret = _nfs4_proc_destroy_clientid(clp, cred);
7254 case -NFS4ERR_DELAY:
7255 case -NFS4ERR_CLIENTID_BUSY:
7265 int nfs4_destroy_clientid(struct nfs_client *clp)
7267 struct rpc_cred *cred;
7270 if (clp->cl_mvops->minor_version < 1)
7272 if (clp->cl_exchange_flags == 0)
7274 if (clp->cl_preserve_clid)
7276 cred = nfs4_get_clid_cred(clp);
7277 ret = nfs4_proc_destroy_clientid(clp, cred);
7282 case -NFS4ERR_STALE_CLIENTID:
7283 clp->cl_exchange_flags = 0;
7289 struct nfs4_get_lease_time_data {
7290 struct nfs4_get_lease_time_args *args;
7291 struct nfs4_get_lease_time_res *res;
7292 struct nfs_client *clp;
7295 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7298 struct nfs4_get_lease_time_data *data =
7299 (struct nfs4_get_lease_time_data *)calldata;
7301 dprintk("--> %s\n", __func__);
7302 /* just setup sequence, do not trigger session recovery
7303 since we're invoked within one */
7304 nfs41_setup_sequence(data->clp->cl_session,
7305 &data->args->la_seq_args,
7306 &data->res->lr_seq_res,
7308 dprintk("<-- %s\n", __func__);
7312 * Called from nfs4_state_manager thread for session setup, so don't recover
7313 * from sequence operation or clientid errors.
7315 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7317 struct nfs4_get_lease_time_data *data =
7318 (struct nfs4_get_lease_time_data *)calldata;
7320 dprintk("--> %s\n", __func__);
7321 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7323 switch (task->tk_status) {
7324 case -NFS4ERR_DELAY:
7325 case -NFS4ERR_GRACE:
7326 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7327 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7328 task->tk_status = 0;
7330 case -NFS4ERR_RETRY_UNCACHED_REP:
7331 rpc_restart_call_prepare(task);
7334 dprintk("<-- %s\n", __func__);
7337 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
7338 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7339 .rpc_call_done = nfs4_get_lease_time_done,
7342 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7344 struct rpc_task *task;
7345 struct nfs4_get_lease_time_args args;
7346 struct nfs4_get_lease_time_res res = {
7347 .lr_fsinfo = fsinfo,
7349 struct nfs4_get_lease_time_data data = {
7354 struct rpc_message msg = {
7355 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7359 struct rpc_task_setup task_setup = {
7360 .rpc_client = clp->cl_rpcclient,
7361 .rpc_message = &msg,
7362 .callback_ops = &nfs4_get_lease_time_ops,
7363 .callback_data = &data,
7364 .flags = RPC_TASK_TIMEOUT,
7368 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
7369 nfs4_set_sequence_privileged(&args.la_seq_args);
7370 dprintk("--> %s\n", __func__);
7371 task = rpc_run_task(&task_setup);
7374 status = PTR_ERR(task);
7376 status = task->tk_status;
7379 dprintk("<-- %s return %d\n", __func__, status);
7385 * Initialize the values to be used by the client in CREATE_SESSION
7386 * If nfs4_init_session set the fore channel request and response sizes,
7389 * Set the back channel max_resp_sz_cached to zero to force the client to
7390 * always set csa_cachethis to FALSE because the current implementation
7391 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7393 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7394 struct rpc_clnt *clnt)
7396 unsigned int max_rqst_sz, max_resp_sz;
7397 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
7399 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7400 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7402 /* Fore channel attributes */
7403 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7404 args->fc_attrs.max_resp_sz = max_resp_sz;
7405 args->fc_attrs.max_ops = NFS4_MAX_OPS;
7406 args->fc_attrs.max_reqs = max_session_slots;
7408 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
7409 "max_ops=%u max_reqs=%u\n",
7411 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
7412 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
7414 /* Back channel attributes */
7415 args->bc_attrs.max_rqst_sz = max_bc_payload;
7416 args->bc_attrs.max_resp_sz = max_bc_payload;
7417 args->bc_attrs.max_resp_sz_cached = 0;
7418 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
7419 args->bc_attrs.max_reqs = NFS41_BC_MAX_CALLBACKS;
7421 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7422 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7424 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7425 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7426 args->bc_attrs.max_reqs);
7429 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7430 struct nfs41_create_session_res *res)
7432 struct nfs4_channel_attrs *sent = &args->fc_attrs;
7433 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
7435 if (rcvd->max_resp_sz > sent->max_resp_sz)
7438 * Our requested max_ops is the minimum we need; we're not
7439 * prepared to break up compounds into smaller pieces than that.
7440 * So, no point even trying to continue if the server won't
7443 if (rcvd->max_ops < sent->max_ops)
7445 if (rcvd->max_reqs == 0)
7447 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7448 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
7452 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7453 struct nfs41_create_session_res *res)
7455 struct nfs4_channel_attrs *sent = &args->bc_attrs;
7456 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
7458 if (!(res->flags & SESSION4_BACK_CHAN))
7460 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7462 if (rcvd->max_resp_sz < sent->max_resp_sz)
7464 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7466 /* These would render the backchannel useless: */
7467 if (rcvd->max_ops != sent->max_ops)
7469 if (rcvd->max_reqs != sent->max_reqs)
7475 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
7476 struct nfs41_create_session_res *res)
7480 ret = nfs4_verify_fore_channel_attrs(args, res);
7483 return nfs4_verify_back_channel_attrs(args, res);
7486 static void nfs4_update_session(struct nfs4_session *session,
7487 struct nfs41_create_session_res *res)
7489 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
7490 /* Mark client id and session as being confirmed */
7491 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7492 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
7493 session->flags = res->flags;
7494 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
7495 if (res->flags & SESSION4_BACK_CHAN)
7496 memcpy(&session->bc_attrs, &res->bc_attrs,
7497 sizeof(session->bc_attrs));
7500 static int _nfs4_proc_create_session(struct nfs_client *clp,
7501 struct rpc_cred *cred)
7503 struct nfs4_session *session = clp->cl_session;
7504 struct nfs41_create_session_args args = {
7506 .clientid = clp->cl_clientid,
7507 .seqid = clp->cl_seqid,
7508 .cb_program = NFS4_CALLBACK,
7510 struct nfs41_create_session_res res;
7512 struct rpc_message msg = {
7513 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7520 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
7521 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
7523 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7524 trace_nfs4_create_session(clp, status);
7527 /* Verify the session's negotiated channel_attrs values */
7528 status = nfs4_verify_channel_attrs(&args, &res);
7529 /* Increment the clientid slot sequence id */
7530 if (clp->cl_seqid == res.seqid)
7534 nfs4_update_session(session, &res);
7541 * Issues a CREATE_SESSION operation to the server.
7542 * It is the responsibility of the caller to verify the session is
7543 * expired before calling this routine.
7545 int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
7549 struct nfs4_session *session = clp->cl_session;
7551 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7553 status = _nfs4_proc_create_session(clp, cred);
7557 /* Init or reset the session slot tables */
7558 status = nfs4_setup_session_slot_tables(session);
7559 dprintk("slot table setup returned %d\n", status);
7563 ptr = (unsigned *)&session->sess_id.data[0];
7564 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7565 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
7567 dprintk("<-- %s\n", __func__);
7572 * Issue the over-the-wire RPC DESTROY_SESSION.
7573 * The caller must serialize access to this routine.
7575 int nfs4_proc_destroy_session(struct nfs4_session *session,
7576 struct rpc_cred *cred)
7578 struct rpc_message msg = {
7579 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
7580 .rpc_argp = session,
7585 dprintk("--> nfs4_proc_destroy_session\n");
7587 /* session is still being setup */
7588 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
7591 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7592 trace_nfs4_destroy_session(session->clp, status);
7595 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
7596 "Session has been destroyed regardless...\n", status);
7598 dprintk("<-- nfs4_proc_destroy_session\n");
7603 * Renew the cl_session lease.
7605 struct nfs4_sequence_data {
7606 struct nfs_client *clp;
7607 struct nfs4_sequence_args args;
7608 struct nfs4_sequence_res res;
7611 static void nfs41_sequence_release(void *data)
7613 struct nfs4_sequence_data *calldata = data;
7614 struct nfs_client *clp = calldata->clp;
7616 if (atomic_read(&clp->cl_count) > 1)
7617 nfs4_schedule_state_renewal(clp);
7618 nfs_put_client(clp);
7622 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7624 switch(task->tk_status) {
7625 case -NFS4ERR_DELAY:
7626 rpc_delay(task, NFS4_POLL_RETRY_MAX);
7629 nfs4_schedule_lease_recovery(clp);
7634 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
7636 struct nfs4_sequence_data *calldata = data;
7637 struct nfs_client *clp = calldata->clp;
7639 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
7642 trace_nfs4_sequence(clp, task->tk_status);
7643 if (task->tk_status < 0) {
7644 dprintk("%s ERROR %d\n", __func__, task->tk_status);
7645 if (atomic_read(&clp->cl_count) == 1)
7648 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
7649 rpc_restart_call_prepare(task);
7653 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
7655 dprintk("<-- %s\n", __func__);
7658 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
7660 struct nfs4_sequence_data *calldata = data;
7661 struct nfs_client *clp = calldata->clp;
7662 struct nfs4_sequence_args *args;
7663 struct nfs4_sequence_res *res;
7665 args = task->tk_msg.rpc_argp;
7666 res = task->tk_msg.rpc_resp;
7668 nfs41_setup_sequence(clp->cl_session, args, res, task);
7671 static const struct rpc_call_ops nfs41_sequence_ops = {
7672 .rpc_call_done = nfs41_sequence_call_done,
7673 .rpc_call_prepare = nfs41_sequence_prepare,
7674 .rpc_release = nfs41_sequence_release,
7677 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
7678 struct rpc_cred *cred,
7681 struct nfs4_sequence_data *calldata;
7682 struct rpc_message msg = {
7683 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
7686 struct rpc_task_setup task_setup_data = {
7687 .rpc_client = clp->cl_rpcclient,
7688 .rpc_message = &msg,
7689 .callback_ops = &nfs41_sequence_ops,
7690 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7693 if (!atomic_inc_not_zero(&clp->cl_count))
7694 return ERR_PTR(-EIO);
7695 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7696 if (calldata == NULL) {
7697 nfs_put_client(clp);
7698 return ERR_PTR(-ENOMEM);
7700 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
7702 nfs4_set_sequence_privileged(&calldata->args);
7703 msg.rpc_argp = &calldata->args;
7704 msg.rpc_resp = &calldata->res;
7705 calldata->clp = clp;
7706 task_setup_data.callback_data = calldata;
7708 return rpc_run_task(&task_setup_data);
7711 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
7713 struct rpc_task *task;
7716 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
7718 task = _nfs41_proc_sequence(clp, cred, false);
7720 ret = PTR_ERR(task);
7722 rpc_put_task_async(task);
7723 dprintk("<-- %s status=%d\n", __func__, ret);
7727 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
7729 struct rpc_task *task;
7732 task = _nfs41_proc_sequence(clp, cred, true);
7734 ret = PTR_ERR(task);
7737 ret = rpc_wait_for_completion_task(task);
7739 ret = task->tk_status;
7742 dprintk("<-- %s status=%d\n", __func__, ret);
7746 struct nfs4_reclaim_complete_data {
7747 struct nfs_client *clp;
7748 struct nfs41_reclaim_complete_args arg;
7749 struct nfs41_reclaim_complete_res res;
7752 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
7754 struct nfs4_reclaim_complete_data *calldata = data;
7756 nfs41_setup_sequence(calldata->clp->cl_session,
7757 &calldata->arg.seq_args,
7758 &calldata->res.seq_res,
7762 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7764 switch(task->tk_status) {
7766 case -NFS4ERR_COMPLETE_ALREADY:
7767 case -NFS4ERR_WRONG_CRED: /* What to do here? */
7769 case -NFS4ERR_DELAY:
7770 rpc_delay(task, NFS4_POLL_RETRY_MAX);
7772 case -NFS4ERR_RETRY_UNCACHED_REP:
7775 nfs4_schedule_lease_recovery(clp);
7780 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
7782 struct nfs4_reclaim_complete_data *calldata = data;
7783 struct nfs_client *clp = calldata->clp;
7784 struct nfs4_sequence_res *res = &calldata->res.seq_res;
7786 dprintk("--> %s\n", __func__);
7787 if (!nfs41_sequence_done(task, res))
7790 trace_nfs4_reclaim_complete(clp, task->tk_status);
7791 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
7792 rpc_restart_call_prepare(task);
7795 dprintk("<-- %s\n", __func__);
7798 static void nfs4_free_reclaim_complete_data(void *data)
7800 struct nfs4_reclaim_complete_data *calldata = data;
7805 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
7806 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
7807 .rpc_call_done = nfs4_reclaim_complete_done,
7808 .rpc_release = nfs4_free_reclaim_complete_data,
7812 * Issue a global reclaim complete.
7814 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
7815 struct rpc_cred *cred)
7817 struct nfs4_reclaim_complete_data *calldata;
7818 struct rpc_task *task;
7819 struct rpc_message msg = {
7820 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
7823 struct rpc_task_setup task_setup_data = {
7824 .rpc_client = clp->cl_rpcclient,
7825 .rpc_message = &msg,
7826 .callback_ops = &nfs4_reclaim_complete_call_ops,
7827 .flags = RPC_TASK_ASYNC,
7829 int status = -ENOMEM;
7831 dprintk("--> %s\n", __func__);
7832 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7833 if (calldata == NULL)
7835 calldata->clp = clp;
7836 calldata->arg.one_fs = 0;
7838 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
7839 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
7840 msg.rpc_argp = &calldata->arg;
7841 msg.rpc_resp = &calldata->res;
7842 task_setup_data.callback_data = calldata;
7843 task = rpc_run_task(&task_setup_data);
7845 status = PTR_ERR(task);
7848 status = nfs4_wait_for_completion_rpc_task(task);
7850 status = task->tk_status;
7854 dprintk("<-- %s status=%d\n", __func__, status);
7859 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
7861 struct nfs4_layoutget *lgp = calldata;
7862 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
7863 struct nfs4_session *session = nfs4_get_session(server);
7865 dprintk("--> %s\n", __func__);
7866 nfs41_setup_sequence(session, &lgp->args.seq_args,
7867 &lgp->res.seq_res, task);
7868 dprintk("<-- %s\n", __func__);
7871 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
7873 struct nfs4_layoutget *lgp = calldata;
7875 dprintk("--> %s\n", __func__);
7876 nfs41_sequence_done(task, &lgp->res.seq_res);
7877 dprintk("<-- %s\n", __func__);
7881 nfs4_layoutget_handle_exception(struct rpc_task *task,
7882 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
7884 struct inode *inode = lgp->args.inode;
7885 struct nfs_server *server = NFS_SERVER(inode);
7886 struct pnfs_layout_hdr *lo;
7887 int nfs4err = task->tk_status;
7888 int err, status = 0;
7890 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
7897 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
7898 * on the file. set tk_status to -ENODATA to tell upper layer to
7901 case -NFS4ERR_LAYOUTUNAVAILABLE:
7905 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
7906 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
7908 case -NFS4ERR_BADLAYOUT:
7909 status = -EOVERFLOW;
7912 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
7913 * (or clients) writing to the same RAID stripe except when
7914 * the minlength argument is 0 (see RFC5661 section 18.43.3).
7916 * Treat it like we would RECALLCONFLICT -- we retry for a little
7917 * while, and then eventually give up.
7919 case -NFS4ERR_LAYOUTTRYLATER:
7920 if (lgp->args.minlength == 0) {
7921 status = -EOVERFLOW;
7926 case -NFS4ERR_RECALLCONFLICT:
7927 status = -ERECALLCONFLICT;
7929 case -NFS4ERR_EXPIRED:
7930 case -NFS4ERR_BAD_STATEID:
7931 exception->timeout = 0;
7932 spin_lock(&inode->i_lock);
7933 if (nfs4_stateid_match(&lgp->args.stateid,
7934 &lgp->args.ctx->state->stateid)) {
7935 spin_unlock(&inode->i_lock);
7936 /* If the open stateid was bad, then recover it. */
7937 exception->state = lgp->args.ctx->state;
7940 lo = NFS_I(inode)->layout;
7941 if (lo && !test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) &&
7942 nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
7946 * Mark the bad layout state as invalid, then retry
7947 * with the current stateid.
7949 set_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags);
7950 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL, 0);
7951 spin_unlock(&inode->i_lock);
7952 pnfs_free_lseg_list(&head);
7956 spin_unlock(&inode->i_lock);
7959 err = nfs4_handle_exception(server, nfs4err, exception);
7961 if (exception->retry)
7967 dprintk("<-- %s\n", __func__);
7971 static size_t max_response_pages(struct nfs_server *server)
7973 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
7974 return nfs_page_array_len(0, max_resp_sz);
7977 static void nfs4_free_pages(struct page **pages, size_t size)
7984 for (i = 0; i < size; i++) {
7987 __free_page(pages[i]);
7992 static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
7994 struct page **pages;
7997 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
7999 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8003 for (i = 0; i < size; i++) {
8004 pages[i] = alloc_page(gfp_flags);
8006 dprintk("%s: failed to allocate page\n", __func__);
8007 nfs4_free_pages(pages, size);
8015 static void nfs4_layoutget_release(void *calldata)
8017 struct nfs4_layoutget *lgp = calldata;
8018 struct inode *inode = lgp->args.inode;
8019 struct nfs_server *server = NFS_SERVER(inode);
8020 size_t max_pages = max_response_pages(server);
8022 dprintk("--> %s\n", __func__);
8023 nfs4_free_pages(lgp->args.layout.pages, max_pages);
8024 pnfs_put_layout_hdr(NFS_I(inode)->layout);
8025 put_nfs_open_context(lgp->args.ctx);
8027 dprintk("<-- %s\n", __func__);
8030 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8031 .rpc_call_prepare = nfs4_layoutget_prepare,
8032 .rpc_call_done = nfs4_layoutget_done,
8033 .rpc_release = nfs4_layoutget_release,
8036 struct pnfs_layout_segment *
8037 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
8039 struct inode *inode = lgp->args.inode;
8040 struct nfs_server *server = NFS_SERVER(inode);
8041 size_t max_pages = max_response_pages(server);
8042 struct rpc_task *task;
8043 struct rpc_message msg = {
8044 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8045 .rpc_argp = &lgp->args,
8046 .rpc_resp = &lgp->res,
8047 .rpc_cred = lgp->cred,
8049 struct rpc_task_setup task_setup_data = {
8050 .rpc_client = server->client,
8051 .rpc_message = &msg,
8052 .callback_ops = &nfs4_layoutget_call_ops,
8053 .callback_data = lgp,
8054 .flags = RPC_TASK_ASYNC,
8056 struct pnfs_layout_segment *lseg = NULL;
8057 struct nfs4_exception exception = {
8059 .timeout = *timeout,
8063 dprintk("--> %s\n", __func__);
8065 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8066 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8068 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8069 if (!lgp->args.layout.pages) {
8070 nfs4_layoutget_release(lgp);
8071 return ERR_PTR(-ENOMEM);
8073 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8075 lgp->res.layoutp = &lgp->args.layout;
8076 lgp->res.seq_res.sr_slot = NULL;
8077 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
8079 task = rpc_run_task(&task_setup_data);
8081 return ERR_CAST(task);
8082 status = nfs4_wait_for_completion_rpc_task(task);
8084 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8085 *timeout = exception.timeout;
8088 trace_nfs4_layoutget(lgp->args.ctx,
8094 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8095 if (status == 0 && lgp->res.layoutp->len)
8096 lseg = pnfs_layout_process(lgp);
8098 dprintk("<-- %s status=%d\n", __func__, status);
8100 return ERR_PTR(status);
8105 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8107 struct nfs4_layoutreturn *lrp = calldata;
8109 dprintk("--> %s\n", __func__);
8110 nfs41_setup_sequence(lrp->clp->cl_session,
8111 &lrp->args.seq_args,
8116 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8118 struct nfs4_layoutreturn *lrp = calldata;
8119 struct nfs_server *server;
8121 dprintk("--> %s\n", __func__);
8123 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
8126 server = NFS_SERVER(lrp->args.inode);
8127 switch (task->tk_status) {
8129 task->tk_status = 0;
8132 case -NFS4ERR_DELAY:
8133 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
8135 rpc_restart_call_prepare(task);
8138 dprintk("<-- %s\n", __func__);
8141 static void nfs4_layoutreturn_release(void *calldata)
8143 struct nfs4_layoutreturn *lrp = calldata;
8144 struct pnfs_layout_hdr *lo = lrp->args.layout;
8147 dprintk("--> %s\n", __func__);
8148 spin_lock(&lo->plh_inode->i_lock);
8149 pnfs_mark_matching_lsegs_invalid(lo, &freeme, &lrp->args.range,
8150 be32_to_cpu(lrp->args.stateid.seqid));
8151 pnfs_mark_layout_returned_if_empty(lo);
8152 if (lrp->res.lrs_present)
8153 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
8154 pnfs_clear_layoutreturn_waitbit(lo);
8155 spin_unlock(&lo->plh_inode->i_lock);
8156 pnfs_free_lseg_list(&freeme);
8157 pnfs_put_layout_hdr(lrp->args.layout);
8158 nfs_iput_and_deactive(lrp->inode);
8160 dprintk("<-- %s\n", __func__);
8163 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8164 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8165 .rpc_call_done = nfs4_layoutreturn_done,
8166 .rpc_release = nfs4_layoutreturn_release,
8169 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
8171 struct rpc_task *task;
8172 struct rpc_message msg = {
8173 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8174 .rpc_argp = &lrp->args,
8175 .rpc_resp = &lrp->res,
8176 .rpc_cred = lrp->cred,
8178 struct rpc_task_setup task_setup_data = {
8179 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
8180 .rpc_message = &msg,
8181 .callback_ops = &nfs4_layoutreturn_call_ops,
8182 .callback_data = lrp,
8186 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8187 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8188 &task_setup_data.rpc_client, &msg);
8190 dprintk("--> %s\n", __func__);
8192 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8194 nfs4_layoutreturn_release(lrp);
8197 task_setup_data.flags |= RPC_TASK_ASYNC;
8199 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
8200 task = rpc_run_task(&task_setup_data);
8202 return PTR_ERR(task);
8204 status = task->tk_status;
8205 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
8206 dprintk("<-- %s status=%d\n", __func__, status);
8212 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
8213 struct pnfs_device *pdev,
8214 struct rpc_cred *cred)
8216 struct nfs4_getdeviceinfo_args args = {
8218 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8219 NOTIFY_DEVICEID4_DELETE,
8221 struct nfs4_getdeviceinfo_res res = {
8224 struct rpc_message msg = {
8225 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8232 dprintk("--> %s\n", __func__);
8233 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
8234 if (res.notification & ~args.notify_types)
8235 dprintk("%s: unsupported notification\n", __func__);
8236 if (res.notification != args.notify_types)
8239 dprintk("<-- %s status=%d\n", __func__, status);
8244 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8245 struct pnfs_device *pdev,
8246 struct rpc_cred *cred)
8248 struct nfs4_exception exception = { };
8252 err = nfs4_handle_exception(server,
8253 _nfs4_proc_getdeviceinfo(server, pdev, cred),
8255 } while (exception.retry);
8258 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8260 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8262 struct nfs4_layoutcommit_data *data = calldata;
8263 struct nfs_server *server = NFS_SERVER(data->args.inode);
8264 struct nfs4_session *session = nfs4_get_session(server);
8266 nfs41_setup_sequence(session,
8267 &data->args.seq_args,
8273 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8275 struct nfs4_layoutcommit_data *data = calldata;
8276 struct nfs_server *server = NFS_SERVER(data->args.inode);
8278 if (!nfs41_sequence_done(task, &data->res.seq_res))
8281 switch (task->tk_status) { /* Just ignore these failures */
8282 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8283 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8284 case -NFS4ERR_BADLAYOUT: /* no layout */
8285 case -NFS4ERR_GRACE: /* loca_recalim always false */
8286 task->tk_status = 0;
8290 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
8291 rpc_restart_call_prepare(task);
8297 static void nfs4_layoutcommit_release(void *calldata)
8299 struct nfs4_layoutcommit_data *data = calldata;
8301 pnfs_cleanup_layoutcommit(data);
8302 nfs_post_op_update_inode_force_wcc(data->args.inode,
8304 put_rpccred(data->cred);
8305 nfs_iput_and_deactive(data->inode);
8309 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8310 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8311 .rpc_call_done = nfs4_layoutcommit_done,
8312 .rpc_release = nfs4_layoutcommit_release,
8316 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
8318 struct rpc_message msg = {
8319 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8320 .rpc_argp = &data->args,
8321 .rpc_resp = &data->res,
8322 .rpc_cred = data->cred,
8324 struct rpc_task_setup task_setup_data = {
8325 .task = &data->task,
8326 .rpc_client = NFS_CLIENT(data->args.inode),
8327 .rpc_message = &msg,
8328 .callback_ops = &nfs4_layoutcommit_ops,
8329 .callback_data = data,
8331 struct rpc_task *task;
8334 dprintk("NFS: initiating layoutcommit call. sync %d "
8335 "lbw: %llu inode %lu\n", sync,
8336 data->args.lastbytewritten,
8337 data->args.inode->i_ino);
8340 data->inode = nfs_igrab_and_active(data->args.inode);
8341 if (data->inode == NULL) {
8342 nfs4_layoutcommit_release(data);
8345 task_setup_data.flags = RPC_TASK_ASYNC;
8347 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
8348 task = rpc_run_task(&task_setup_data);
8350 return PTR_ERR(task);
8352 status = task->tk_status;
8353 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
8354 dprintk("%s: status %d\n", __func__, status);
8360 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8361 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8364 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8365 struct nfs_fsinfo *info,
8366 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
8368 struct nfs41_secinfo_no_name_args args = {
8369 .style = SECINFO_STYLE_CURRENT_FH,
8371 struct nfs4_secinfo_res res = {
8374 struct rpc_message msg = {
8375 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8379 struct rpc_clnt *clnt = server->client;
8380 struct rpc_cred *cred = NULL;
8383 if (use_integrity) {
8384 clnt = server->nfs_client->cl_rpcclient;
8385 cred = nfs4_get_clid_cred(server->nfs_client);
8386 msg.rpc_cred = cred;
8389 dprintk("--> %s\n", __func__);
8390 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8392 dprintk("<-- %s status=%d\n", __func__, status);
8401 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8402 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8404 struct nfs4_exception exception = { };
8407 /* first try using integrity protection */
8408 err = -NFS4ERR_WRONGSEC;
8410 /* try to use integrity protection with machine cred */
8411 if (_nfs4_is_integrity_protected(server->nfs_client))
8412 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8416 * if unable to use integrity protection, or SECINFO with
8417 * integrity protection returns NFS4ERR_WRONGSEC (which is
8418 * disallowed by spec, but exists in deployed servers) use
8419 * the current filesystem's rpc_client and the user cred.
8421 if (err == -NFS4ERR_WRONGSEC)
8422 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8427 case -NFS4ERR_WRONGSEC:
8431 err = nfs4_handle_exception(server, err, &exception);
8433 } while (exception.retry);
8439 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8440 struct nfs_fsinfo *info)
8444 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
8445 struct nfs4_secinfo_flavors *flavors;
8446 struct nfs4_secinfo4 *secinfo;
8449 page = alloc_page(GFP_KERNEL);
8455 flavors = page_address(page);
8456 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8459 * Fall back on "guess and check" method if
8460 * the server doesn't support SECINFO_NO_NAME
8462 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
8463 err = nfs4_find_root_sec(server, fhandle, info);
8469 for (i = 0; i < flavors->num_flavors; i++) {
8470 secinfo = &flavors->flavors[i];
8472 switch (secinfo->flavor) {
8476 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8477 &secinfo->flavor_info);
8480 flavor = RPC_AUTH_MAXFLAVOR;
8484 if (!nfs_auth_info_match(&server->auth_info, flavor))
8485 flavor = RPC_AUTH_MAXFLAVOR;
8487 if (flavor != RPC_AUTH_MAXFLAVOR) {
8488 err = nfs4_lookup_root_sec(server, fhandle,
8495 if (flavor == RPC_AUTH_MAXFLAVOR)
8506 static int _nfs41_test_stateid(struct nfs_server *server,
8507 nfs4_stateid *stateid,
8508 struct rpc_cred *cred)
8511 struct nfs41_test_stateid_args args = {
8514 struct nfs41_test_stateid_res res;
8515 struct rpc_message msg = {
8516 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8521 struct rpc_clnt *rpc_client = server->client;
8523 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8526 dprintk("NFS call test_stateid %p\n", stateid);
8527 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
8528 nfs4_set_sequence_privileged(&args.seq_args);
8529 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
8530 &args.seq_args, &res.seq_res);
8531 if (status != NFS_OK) {
8532 dprintk("NFS reply test_stateid: failed, %d\n", status);
8535 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
8540 * nfs41_test_stateid - perform a TEST_STATEID operation
8542 * @server: server / transport on which to perform the operation
8543 * @stateid: state ID to test
8546 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8547 * Otherwise a negative NFS4ERR value is returned if the operation
8548 * failed or the state ID is not currently valid.
8550 static int nfs41_test_stateid(struct nfs_server *server,
8551 nfs4_stateid *stateid,
8552 struct rpc_cred *cred)
8554 struct nfs4_exception exception = { };
8557 err = _nfs41_test_stateid(server, stateid, cred);
8558 if (err != -NFS4ERR_DELAY)
8560 nfs4_handle_exception(server, err, &exception);
8561 } while (exception.retry);
8565 struct nfs_free_stateid_data {
8566 struct nfs_server *server;
8567 struct nfs41_free_stateid_args args;
8568 struct nfs41_free_stateid_res res;
8571 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
8573 struct nfs_free_stateid_data *data = calldata;
8574 nfs41_setup_sequence(nfs4_get_session(data->server),
8575 &data->args.seq_args,
8580 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
8582 struct nfs_free_stateid_data *data = calldata;
8584 nfs41_sequence_done(task, &data->res.seq_res);
8586 switch (task->tk_status) {
8587 case -NFS4ERR_DELAY:
8588 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
8589 rpc_restart_call_prepare(task);
8593 static void nfs41_free_stateid_release(void *calldata)
8598 static const struct rpc_call_ops nfs41_free_stateid_ops = {
8599 .rpc_call_prepare = nfs41_free_stateid_prepare,
8600 .rpc_call_done = nfs41_free_stateid_done,
8601 .rpc_release = nfs41_free_stateid_release,
8604 static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
8605 nfs4_stateid *stateid,
8606 struct rpc_cred *cred,
8609 struct rpc_message msg = {
8610 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
8613 struct rpc_task_setup task_setup = {
8614 .rpc_client = server->client,
8615 .rpc_message = &msg,
8616 .callback_ops = &nfs41_free_stateid_ops,
8617 .flags = RPC_TASK_ASYNC,
8619 struct nfs_free_stateid_data *data;
8621 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8622 &task_setup.rpc_client, &msg);
8624 dprintk("NFS call free_stateid %p\n", stateid);
8625 data = kmalloc(sizeof(*data), GFP_NOFS);
8627 return ERR_PTR(-ENOMEM);
8628 data->server = server;
8629 nfs4_stateid_copy(&data->args.stateid, stateid);
8631 task_setup.callback_data = data;
8633 msg.rpc_argp = &data->args;
8634 msg.rpc_resp = &data->res;
8635 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
8637 nfs4_set_sequence_privileged(&data->args.seq_args);
8639 return rpc_run_task(&task_setup);
8643 * nfs41_free_stateid - perform a FREE_STATEID operation
8645 * @server: server / transport on which to perform the operation
8646 * @stateid: state ID to release
8649 * Returns NFS_OK if the server freed "stateid". Otherwise a
8650 * negative NFS4ERR value is returned.
8652 static int nfs41_free_stateid(struct nfs_server *server,
8653 nfs4_stateid *stateid,
8654 struct rpc_cred *cred)
8656 struct rpc_task *task;
8659 task = _nfs41_free_stateid(server, stateid, cred, true);
8661 return PTR_ERR(task);
8662 ret = rpc_wait_for_completion_task(task);
8664 ret = task->tk_status;
8670 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
8672 struct rpc_task *task;
8673 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
8675 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
8676 nfs4_free_lock_state(server, lsp);
8682 static bool nfs41_match_stateid(const nfs4_stateid *s1,
8683 const nfs4_stateid *s2)
8685 if (s1->type != s2->type)
8688 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
8691 if (s1->seqid == s2->seqid)
8693 if (s1->seqid == 0 || s2->seqid == 0)
8699 #endif /* CONFIG_NFS_V4_1 */
8701 static bool nfs4_match_stateid(const nfs4_stateid *s1,
8702 const nfs4_stateid *s2)
8704 return nfs4_stateid_match(s1, s2);
8708 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
8709 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
8710 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
8711 .recover_open = nfs4_open_reclaim,
8712 .recover_lock = nfs4_lock_reclaim,
8713 .establish_clid = nfs4_init_clientid,
8714 .detect_trunking = nfs40_discover_server_trunking,
8717 #if defined(CONFIG_NFS_V4_1)
8718 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
8719 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
8720 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
8721 .recover_open = nfs4_open_reclaim,
8722 .recover_lock = nfs4_lock_reclaim,
8723 .establish_clid = nfs41_init_clientid,
8724 .reclaim_complete = nfs41_proc_reclaim_complete,
8725 .detect_trunking = nfs41_discover_server_trunking,
8727 #endif /* CONFIG_NFS_V4_1 */
8729 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
8730 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
8731 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
8732 .recover_open = nfs40_open_expired,
8733 .recover_lock = nfs4_lock_expired,
8734 .establish_clid = nfs4_init_clientid,
8737 #if defined(CONFIG_NFS_V4_1)
8738 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
8739 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
8740 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
8741 .recover_open = nfs41_open_expired,
8742 .recover_lock = nfs41_lock_expired,
8743 .establish_clid = nfs41_init_clientid,
8745 #endif /* CONFIG_NFS_V4_1 */
8747 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
8748 .sched_state_renewal = nfs4_proc_async_renew,
8749 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
8750 .renew_lease = nfs4_proc_renew,
8753 #if defined(CONFIG_NFS_V4_1)
8754 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
8755 .sched_state_renewal = nfs41_proc_async_sequence,
8756 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
8757 .renew_lease = nfs4_proc_sequence,
8761 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
8762 .get_locations = _nfs40_proc_get_locations,
8763 .fsid_present = _nfs40_proc_fsid_present,
8766 #if defined(CONFIG_NFS_V4_1)
8767 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
8768 .get_locations = _nfs41_proc_get_locations,
8769 .fsid_present = _nfs41_proc_fsid_present,
8771 #endif /* CONFIG_NFS_V4_1 */
8773 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
8775 .init_caps = NFS_CAP_READDIRPLUS
8776 | NFS_CAP_ATOMIC_OPEN
8777 | NFS_CAP_POSIX_LOCK,
8778 .init_client = nfs40_init_client,
8779 .shutdown_client = nfs40_shutdown_client,
8780 .match_stateid = nfs4_match_stateid,
8781 .find_root_sec = nfs4_find_root_sec,
8782 .free_lock_state = nfs4_release_lockowner,
8783 .alloc_seqid = nfs_alloc_seqid,
8784 .call_sync_ops = &nfs40_call_sync_ops,
8785 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
8786 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
8787 .state_renewal_ops = &nfs40_state_renewal_ops,
8788 .mig_recovery_ops = &nfs40_mig_recovery_ops,
8791 #if defined(CONFIG_NFS_V4_1)
8792 static struct nfs_seqid *
8793 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
8798 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
8800 .init_caps = NFS_CAP_READDIRPLUS
8801 | NFS_CAP_ATOMIC_OPEN
8802 | NFS_CAP_POSIX_LOCK
8803 | NFS_CAP_STATEID_NFSV41
8804 | NFS_CAP_ATOMIC_OPEN_V1,
8805 .init_client = nfs41_init_client,
8806 .shutdown_client = nfs41_shutdown_client,
8807 .match_stateid = nfs41_match_stateid,
8808 .find_root_sec = nfs41_find_root_sec,
8809 .free_lock_state = nfs41_free_lock_state,
8810 .alloc_seqid = nfs_alloc_no_seqid,
8811 .call_sync_ops = &nfs41_call_sync_ops,
8812 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8813 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8814 .state_renewal_ops = &nfs41_state_renewal_ops,
8815 .mig_recovery_ops = &nfs41_mig_recovery_ops,
8819 #if defined(CONFIG_NFS_V4_2)
8820 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
8822 .init_caps = NFS_CAP_READDIRPLUS
8823 | NFS_CAP_ATOMIC_OPEN
8824 | NFS_CAP_POSIX_LOCK
8825 | NFS_CAP_STATEID_NFSV41
8826 | NFS_CAP_ATOMIC_OPEN_V1
8829 | NFS_CAP_DEALLOCATE
8831 | NFS_CAP_LAYOUTSTATS
8833 .init_client = nfs41_init_client,
8834 .shutdown_client = nfs41_shutdown_client,
8835 .match_stateid = nfs41_match_stateid,
8836 .find_root_sec = nfs41_find_root_sec,
8837 .free_lock_state = nfs41_free_lock_state,
8838 .call_sync_ops = &nfs41_call_sync_ops,
8839 .alloc_seqid = nfs_alloc_no_seqid,
8840 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8841 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8842 .state_renewal_ops = &nfs41_state_renewal_ops,
8843 .mig_recovery_ops = &nfs41_mig_recovery_ops,
8847 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
8848 [0] = &nfs_v4_0_minor_ops,
8849 #if defined(CONFIG_NFS_V4_1)
8850 [1] = &nfs_v4_1_minor_ops,
8852 #if defined(CONFIG_NFS_V4_2)
8853 [2] = &nfs_v4_2_minor_ops,
8857 ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
8859 ssize_t error, error2;
8861 error = generic_listxattr(dentry, list, size);
8869 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
8872 return error + error2;
8875 static const struct inode_operations nfs4_dir_inode_operations = {
8876 .create = nfs_create,
8877 .lookup = nfs_lookup,
8878 .atomic_open = nfs_atomic_open,
8880 .unlink = nfs_unlink,
8881 .symlink = nfs_symlink,
8885 .rename = nfs_rename,
8886 .permission = nfs_permission,
8887 .getattr = nfs_getattr,
8888 .setattr = nfs_setattr,
8889 .getxattr = generic_getxattr,
8890 .setxattr = generic_setxattr,
8891 .listxattr = nfs4_listxattr,
8892 .removexattr = generic_removexattr,
8895 static const struct inode_operations nfs4_file_inode_operations = {
8896 .permission = nfs_permission,
8897 .getattr = nfs_getattr,
8898 .setattr = nfs_setattr,
8899 .getxattr = generic_getxattr,
8900 .setxattr = generic_setxattr,
8901 .listxattr = nfs4_listxattr,
8902 .removexattr = generic_removexattr,
8905 const struct nfs_rpc_ops nfs_v4_clientops = {
8906 .version = 4, /* protocol version */
8907 .dentry_ops = &nfs4_dentry_operations,
8908 .dir_inode_ops = &nfs4_dir_inode_operations,
8909 .file_inode_ops = &nfs4_file_inode_operations,
8910 .file_ops = &nfs4_file_operations,
8911 .getroot = nfs4_proc_get_root,
8912 .submount = nfs4_submount,
8913 .try_mount = nfs4_try_mount,
8914 .getattr = nfs4_proc_getattr,
8915 .setattr = nfs4_proc_setattr,
8916 .lookup = nfs4_proc_lookup,
8917 .access = nfs4_proc_access,
8918 .readlink = nfs4_proc_readlink,
8919 .create = nfs4_proc_create,
8920 .remove = nfs4_proc_remove,
8921 .unlink_setup = nfs4_proc_unlink_setup,
8922 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
8923 .unlink_done = nfs4_proc_unlink_done,
8924 .rename_setup = nfs4_proc_rename_setup,
8925 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
8926 .rename_done = nfs4_proc_rename_done,
8927 .link = nfs4_proc_link,
8928 .symlink = nfs4_proc_symlink,
8929 .mkdir = nfs4_proc_mkdir,
8930 .rmdir = nfs4_proc_remove,
8931 .readdir = nfs4_proc_readdir,
8932 .mknod = nfs4_proc_mknod,
8933 .statfs = nfs4_proc_statfs,
8934 .fsinfo = nfs4_proc_fsinfo,
8935 .pathconf = nfs4_proc_pathconf,
8936 .set_capabilities = nfs4_server_capabilities,
8937 .decode_dirent = nfs4_decode_dirent,
8938 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
8939 .read_setup = nfs4_proc_read_setup,
8940 .read_done = nfs4_read_done,
8941 .write_setup = nfs4_proc_write_setup,
8942 .write_done = nfs4_write_done,
8943 .commit_setup = nfs4_proc_commit_setup,
8944 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
8945 .commit_done = nfs4_commit_done,
8946 .lock = nfs4_proc_lock,
8947 .clear_acl_cache = nfs4_zap_acl_attr,
8948 .close_context = nfs4_close_context,
8949 .open_context = nfs4_atomic_open,
8950 .have_delegation = nfs4_have_delegation,
8951 .return_delegation = nfs4_inode_return_delegation,
8952 .alloc_client = nfs4_alloc_client,
8953 .init_client = nfs4_init_client,
8954 .free_client = nfs4_free_client,
8955 .create_server = nfs4_create_server,
8956 .clone_server = nfs_clone_server,
8959 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
8960 .name = XATTR_NAME_NFSV4_ACL,
8961 .list = nfs4_xattr_list_nfs4_acl,
8962 .get = nfs4_xattr_get_nfs4_acl,
8963 .set = nfs4_xattr_set_nfs4_acl,
8966 const struct xattr_handler *nfs4_xattr_handlers[] = {
8967 &nfs4_xattr_nfs4_acl_handler,
8968 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
8969 &nfs4_xattr_nfs4_label_handler,