4 * Client-side procedure declarations for NFSv4.
6 * Copyright (c) 2002 The Regents of the University of Michigan.
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/string.h>
42 #include <linux/ratelimit.h>
43 #include <linux/printk.h>
44 #include <linux/slab.h>
45 #include <linux/sunrpc/clnt.h>
46 #include <linux/nfs.h>
47 #include <linux/nfs4.h>
48 #include <linux/nfs_fs.h>
49 #include <linux/nfs_page.h>
50 #include <linux/nfs_mount.h>
51 #include <linux/namei.h>
52 #include <linux/mount.h>
53 #include <linux/module.h>
54 #include <linux/xattr.h>
55 #include <linux/utsname.h>
56 #include <linux/freezer.h>
57 #include <linux/iversion.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_BITMASK_SZ 3
76 #define NFS4_POLL_RETRY_MIN (HZ/10)
77 #define NFS4_POLL_RETRY_MAX (15*HZ)
79 /* file attributes which can be mapped to nfs attributes */
80 #define NFS4_VALID_ATTRS (ATTR_MODE \
91 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
92 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
93 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
94 static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label, struct inode *inode);
95 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label, struct inode *inode);
96 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
97 struct nfs_fattr *fattr, struct iattr *sattr,
98 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
99 struct nfs4_label *olabel);
100 #ifdef CONFIG_NFS_V4_1
101 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
102 struct rpc_cred *cred,
103 struct nfs4_slot *slot,
105 static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
107 static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
108 struct rpc_cred *, bool);
111 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
112 static inline struct nfs4_label *
113 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
114 struct iattr *sattr, struct nfs4_label *label)
121 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
124 err = security_dentry_init_security(dentry, sattr->ia_mode,
125 &dentry->d_name, (void **)&label->label, &label->len);
132 nfs4_label_release_security(struct nfs4_label *label)
135 security_release_secctx(label->label, label->len);
137 static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
140 return server->attr_bitmask;
142 return server->attr_bitmask_nl;
145 static inline struct nfs4_label *
146 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
147 struct iattr *sattr, struct nfs4_label *l)
150 nfs4_label_release_security(struct nfs4_label *label)
153 nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
154 { return server->attr_bitmask; }
157 /* Prevent leaks of NFSv4 errors into userland */
158 static int nfs4_map_errors(int err)
163 case -NFS4ERR_RESOURCE:
164 case -NFS4ERR_LAYOUTTRYLATER:
165 case -NFS4ERR_RECALLCONFLICT:
167 case -NFS4ERR_WRONGSEC:
168 case -NFS4ERR_WRONG_CRED:
170 case -NFS4ERR_BADOWNER:
171 case -NFS4ERR_BADNAME:
173 case -NFS4ERR_SHARE_DENIED:
175 case -NFS4ERR_MINOR_VERS_MISMATCH:
176 return -EPROTONOSUPPORT;
177 case -NFS4ERR_FILE_OPEN:
180 dprintk("%s could not handle NFSv4 error %d\n",
188 * This is our standard bitmap for GETATTR requests.
190 const u32 nfs4_fattr_bitmap[3] = {
192 | FATTR4_WORD0_CHANGE
195 | FATTR4_WORD0_FILEID,
197 | FATTR4_WORD1_NUMLINKS
199 | FATTR4_WORD1_OWNER_GROUP
200 | FATTR4_WORD1_RAWDEV
201 | FATTR4_WORD1_SPACE_USED
202 | FATTR4_WORD1_TIME_ACCESS
203 | FATTR4_WORD1_TIME_METADATA
204 | FATTR4_WORD1_TIME_MODIFY
205 | FATTR4_WORD1_MOUNTED_ON_FILEID,
206 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
207 FATTR4_WORD2_SECURITY_LABEL
211 static const u32 nfs4_pnfs_open_bitmap[3] = {
213 | FATTR4_WORD0_CHANGE
216 | FATTR4_WORD0_FILEID,
218 | FATTR4_WORD1_NUMLINKS
220 | FATTR4_WORD1_OWNER_GROUP
221 | FATTR4_WORD1_RAWDEV
222 | FATTR4_WORD1_SPACE_USED
223 | FATTR4_WORD1_TIME_ACCESS
224 | FATTR4_WORD1_TIME_METADATA
225 | FATTR4_WORD1_TIME_MODIFY,
226 FATTR4_WORD2_MDSTHRESHOLD
227 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
228 | FATTR4_WORD2_SECURITY_LABEL
232 static const u32 nfs4_open_noattr_bitmap[3] = {
234 | FATTR4_WORD0_FILEID,
237 const u32 nfs4_statfs_bitmap[3] = {
238 FATTR4_WORD0_FILES_AVAIL
239 | FATTR4_WORD0_FILES_FREE
240 | FATTR4_WORD0_FILES_TOTAL,
241 FATTR4_WORD1_SPACE_AVAIL
242 | FATTR4_WORD1_SPACE_FREE
243 | FATTR4_WORD1_SPACE_TOTAL
246 const u32 nfs4_pathconf_bitmap[3] = {
248 | FATTR4_WORD0_MAXNAME,
252 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
253 | FATTR4_WORD0_MAXREAD
254 | FATTR4_WORD0_MAXWRITE
255 | FATTR4_WORD0_LEASE_TIME,
256 FATTR4_WORD1_TIME_DELTA
257 | FATTR4_WORD1_FS_LAYOUT_TYPES,
258 FATTR4_WORD2_LAYOUT_BLKSIZE
259 | FATTR4_WORD2_CLONE_BLKSIZE
262 const u32 nfs4_fs_locations_bitmap[3] = {
266 | FATTR4_WORD0_FILEID
267 | FATTR4_WORD0_FS_LOCATIONS,
269 | FATTR4_WORD1_OWNER_GROUP
270 | FATTR4_WORD1_RAWDEV
271 | FATTR4_WORD1_SPACE_USED
272 | FATTR4_WORD1_TIME_ACCESS
273 | FATTR4_WORD1_TIME_METADATA
274 | FATTR4_WORD1_TIME_MODIFY
275 | FATTR4_WORD1_MOUNTED_ON_FILEID,
278 static void nfs4_bitmap_copy_adjust(__u32 *dst, const __u32 *src,
281 unsigned long cache_validity;
283 memcpy(dst, src, NFS4_BITMASK_SZ*sizeof(*dst));
284 if (!inode || !nfs4_have_delegation(inode, FMODE_READ))
287 cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
288 if (!(cache_validity & NFS_INO_REVAL_FORCED))
289 cache_validity &= ~(NFS_INO_INVALID_CHANGE
290 | NFS_INO_INVALID_SIZE);
292 if (!(cache_validity & NFS_INO_INVALID_SIZE))
293 dst[0] &= ~FATTR4_WORD0_SIZE;
295 if (!(cache_validity & NFS_INO_INVALID_CHANGE))
296 dst[0] &= ~FATTR4_WORD0_CHANGE;
299 static void nfs4_bitmap_copy_adjust_setattr(__u32 *dst,
300 const __u32 *src, struct inode *inode)
302 nfs4_bitmap_copy_adjust(dst, src, inode);
305 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
306 struct nfs4_readdir_arg *readdir)
308 unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
312 readdir->cookie = cookie;
313 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
318 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
323 * NFSv4 servers do not return entries for '.' and '..'
324 * Therefore, we fake these entries here. We let '.'
325 * have cookie 0 and '..' have cookie 1. Note that
326 * when talking to the server, we always send cookie 0
329 start = p = kmap_atomic(*readdir->pages);
332 *p++ = xdr_one; /* next */
333 *p++ = xdr_zero; /* cookie, first word */
334 *p++ = xdr_one; /* cookie, second word */
335 *p++ = xdr_one; /* entry len */
336 memcpy(p, ".\0\0\0", 4); /* entry */
338 *p++ = xdr_one; /* bitmap length */
339 *p++ = htonl(attrs); /* bitmap */
340 *p++ = htonl(12); /* attribute buffer length */
341 *p++ = htonl(NF4DIR);
342 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
345 *p++ = xdr_one; /* next */
346 *p++ = xdr_zero; /* cookie, first word */
347 *p++ = xdr_two; /* cookie, second word */
348 *p++ = xdr_two; /* entry len */
349 memcpy(p, "..\0\0", 4); /* entry */
351 *p++ = xdr_one; /* bitmap length */
352 *p++ = htonl(attrs); /* bitmap */
353 *p++ = htonl(12); /* attribute buffer length */
354 *p++ = htonl(NF4DIR);
355 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
357 readdir->pgbase = (char *)p - (char *)start;
358 readdir->count -= readdir->pgbase;
359 kunmap_atomic(start);
362 static void nfs4_test_and_free_stateid(struct nfs_server *server,
363 nfs4_stateid *stateid,
364 struct rpc_cred *cred)
366 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
368 ops->test_and_free_expired(server, stateid, cred);
371 static void __nfs4_free_revoked_stateid(struct nfs_server *server,
372 nfs4_stateid *stateid,
373 struct rpc_cred *cred)
375 stateid->type = NFS4_REVOKED_STATEID_TYPE;
376 nfs4_test_and_free_stateid(server, stateid, cred);
379 static void nfs4_free_revoked_stateid(struct nfs_server *server,
380 const nfs4_stateid *stateid,
381 struct rpc_cred *cred)
385 nfs4_stateid_copy(&tmp, stateid);
386 __nfs4_free_revoked_stateid(server, &tmp, cred);
389 static long nfs4_update_delay(long *timeout)
393 return NFS4_POLL_RETRY_MAX;
395 *timeout = NFS4_POLL_RETRY_MIN;
396 if (*timeout > NFS4_POLL_RETRY_MAX)
397 *timeout = NFS4_POLL_RETRY_MAX;
403 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
409 freezable_schedule_timeout_killable_unsafe(
410 nfs4_update_delay(timeout));
411 if (fatal_signal_pending(current))
416 /* This is the error handling routine for processes that are allowed
419 static int nfs4_do_handle_exception(struct nfs_server *server,
420 int errorcode, struct nfs4_exception *exception)
422 struct nfs_client *clp = server->nfs_client;
423 struct nfs4_state *state = exception->state;
424 const nfs4_stateid *stateid = exception->stateid;
425 struct inode *inode = exception->inode;
428 exception->delay = 0;
429 exception->recovering = 0;
430 exception->retry = 0;
432 if (stateid == NULL && state != NULL)
433 stateid = &state->stateid;
438 case -NFS4ERR_BADHANDLE:
440 if (inode != NULL && S_ISREG(inode->i_mode))
441 pnfs_destroy_layout(NFS_I(inode));
443 case -NFS4ERR_DELEG_REVOKED:
444 case -NFS4ERR_ADMIN_REVOKED:
445 case -NFS4ERR_EXPIRED:
446 case -NFS4ERR_BAD_STATEID:
447 if (inode != NULL && stateid != NULL) {
448 nfs_inode_find_state_and_recover(inode,
450 goto wait_on_recovery;
452 case -NFS4ERR_OPENMODE:
456 err = nfs_async_inode_return_delegation(inode,
459 goto wait_on_recovery;
460 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
461 exception->retry = 1;
467 ret = nfs4_schedule_stateid_recovery(server, state);
470 goto wait_on_recovery;
471 case -NFS4ERR_STALE_STATEID:
472 case -NFS4ERR_STALE_CLIENTID:
473 nfs4_schedule_lease_recovery(clp);
474 goto wait_on_recovery;
476 ret = nfs4_schedule_migration_recovery(server);
479 goto wait_on_recovery;
480 case -NFS4ERR_LEASE_MOVED:
481 nfs4_schedule_lease_moved_recovery(clp);
482 goto wait_on_recovery;
483 #if defined(CONFIG_NFS_V4_1)
484 case -NFS4ERR_BADSESSION:
485 case -NFS4ERR_BADSLOT:
486 case -NFS4ERR_BAD_HIGH_SLOT:
487 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
488 case -NFS4ERR_DEADSESSION:
489 case -NFS4ERR_SEQ_FALSE_RETRY:
490 case -NFS4ERR_SEQ_MISORDERED:
491 dprintk("%s ERROR: %d Reset session\n", __func__,
493 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
494 goto wait_on_recovery;
495 #endif /* defined(CONFIG_NFS_V4_1) */
496 case -NFS4ERR_FILE_OPEN:
497 if (exception->timeout > HZ) {
498 /* We have retried a decent amount, time to
505 nfs_inc_server_stats(server, NFSIOS_DELAY);
507 case -NFS4ERR_LAYOUTTRYLATER:
508 case -NFS4ERR_RECALLCONFLICT:
509 exception->delay = 1;
512 case -NFS4ERR_RETRY_UNCACHED_REP:
513 case -NFS4ERR_OLD_STATEID:
514 exception->retry = 1;
516 case -NFS4ERR_BADOWNER:
517 /* The following works around a Linux server bug! */
518 case -NFS4ERR_BADNAME:
519 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
520 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
521 exception->retry = 1;
522 printk(KERN_WARNING "NFS: v4 server %s "
523 "does not accept raw "
525 "Reenabling the idmapper.\n",
526 server->nfs_client->cl_hostname);
529 /* We failed to handle the error */
530 return nfs4_map_errors(ret);
532 exception->recovering = 1;
536 /* This is the error handling routine for processes that are allowed
539 int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
541 struct nfs_client *clp = server->nfs_client;
544 ret = nfs4_do_handle_exception(server, errorcode, exception);
545 if (exception->delay) {
546 ret = nfs4_delay(server->client, &exception->timeout);
549 if (exception->recovering) {
550 ret = nfs4_wait_clnt_recover(clp);
551 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
558 exception->retry = 1;
563 nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
564 int errorcode, struct nfs4_exception *exception)
566 struct nfs_client *clp = server->nfs_client;
569 ret = nfs4_do_handle_exception(server, errorcode, exception);
570 if (exception->delay) {
571 rpc_delay(task, nfs4_update_delay(&exception->timeout));
574 if (exception->recovering) {
575 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
576 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
577 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
580 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
585 exception->retry = 1;
590 nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
591 struct nfs4_state *state, long *timeout)
593 struct nfs4_exception exception = {
597 if (task->tk_status >= 0)
600 exception.timeout = *timeout;
601 task->tk_status = nfs4_async_handle_exception(task, server,
604 if (exception.delay && timeout)
605 *timeout = exception.timeout;
612 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
613 * or 'false' otherwise.
615 static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
617 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
618 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
621 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
623 spin_lock(&clp->cl_lock);
624 if (time_before(clp->cl_last_renewal,timestamp))
625 clp->cl_last_renewal = timestamp;
626 spin_unlock(&clp->cl_lock);
629 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
631 struct nfs_client *clp = server->nfs_client;
633 if (!nfs4_has_session(clp))
634 do_renew_lease(clp, timestamp);
637 struct nfs4_call_sync_data {
638 const struct nfs_server *seq_server;
639 struct nfs4_sequence_args *seq_args;
640 struct nfs4_sequence_res *seq_res;
643 void nfs4_init_sequence(struct nfs4_sequence_args *args,
644 struct nfs4_sequence_res *res, int cache_reply,
647 args->sa_slot = NULL;
648 args->sa_cache_this = cache_reply;
649 args->sa_privileged = privileged;
654 static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
656 struct nfs4_slot *slot = res->sr_slot;
657 struct nfs4_slot_table *tbl;
660 spin_lock(&tbl->slot_tbl_lock);
661 if (!nfs41_wake_and_assign_slot(tbl, slot))
662 nfs4_free_slot(tbl, slot);
663 spin_unlock(&tbl->slot_tbl_lock);
668 static int nfs40_sequence_done(struct rpc_task *task,
669 struct nfs4_sequence_res *res)
671 if (res->sr_slot != NULL)
672 nfs40_sequence_free_slot(res);
676 #if defined(CONFIG_NFS_V4_1)
678 static void nfs41_release_slot(struct nfs4_slot *slot)
680 struct nfs4_session *session;
681 struct nfs4_slot_table *tbl;
682 bool send_new_highest_used_slotid = false;
687 session = tbl->session;
689 /* Bump the slot sequence number */
694 spin_lock(&tbl->slot_tbl_lock);
695 /* Be nice to the server: try to ensure that the last transmitted
696 * value for highest_user_slotid <= target_highest_slotid
698 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
699 send_new_highest_used_slotid = true;
701 if (nfs41_wake_and_assign_slot(tbl, slot)) {
702 send_new_highest_used_slotid = false;
705 nfs4_free_slot(tbl, slot);
707 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
708 send_new_highest_used_slotid = false;
710 spin_unlock(&tbl->slot_tbl_lock);
711 if (send_new_highest_used_slotid)
712 nfs41_notify_server(session->clp);
713 if (waitqueue_active(&tbl->slot_waitq))
714 wake_up_all(&tbl->slot_waitq);
717 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
719 nfs41_release_slot(res->sr_slot);
723 static int nfs41_sequence_process(struct rpc_task *task,
724 struct nfs4_sequence_res *res)
726 struct nfs4_session *session;
727 struct nfs4_slot *slot = res->sr_slot;
728 struct nfs_client *clp;
729 bool interrupted = false;
734 /* don't increment the sequence number if the task wasn't sent */
735 if (!RPC_WAS_SENT(task))
738 session = slot->table->session;
740 if (slot->interrupted) {
741 if (res->sr_status != -NFS4ERR_DELAY)
742 slot->interrupted = 0;
746 trace_nfs4_sequence_done(session, res);
747 /* Check the SEQUENCE operation status */
748 switch (res->sr_status) {
750 /* Update the slot's sequence and clientid lease timer */
753 do_renew_lease(clp, res->sr_timestamp);
754 /* Check sequence flags */
755 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
757 nfs41_update_target_slotid(slot->table, slot, res);
761 * sr_status remains 1 if an RPC level error occurred.
762 * The server may or may not have processed the sequence
764 * Mark the slot as having hosted an interrupted RPC call.
766 slot->interrupted = 1;
769 /* The server detected a resend of the RPC call and
770 * returned NFS4ERR_DELAY as per Section 2.10.6.2
773 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
778 case -NFS4ERR_RETRY_UNCACHED_REP:
779 case -NFS4ERR_SEQ_FALSE_RETRY:
781 * The server thinks we tried to replay a request.
782 * Retry the call after bumping the sequence ID.
785 case -NFS4ERR_BADSLOT:
787 * The slot id we used was probably retired. Try again
788 * using a different slot id.
790 if (slot->slot_nr < slot->table->target_highest_slotid)
791 goto session_recover;
793 case -NFS4ERR_SEQ_MISORDERED:
795 * Was the last operation on this sequence interrupted?
796 * If so, retry after bumping the sequence number.
801 * Could this slot have been previously retired?
802 * If so, then the server may be expecting seq_nr = 1!
804 if (slot->seq_nr != 1) {
808 goto session_recover;
810 /* Just update the slot sequence no. */
814 /* The session may be reset by one of the error handlers. */
815 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
819 nfs4_schedule_session_recovery(session, res->sr_status);
824 if (rpc_restart_call_prepare(task)) {
825 nfs41_sequence_free_slot(res);
831 if (!rpc_restart_call(task))
833 rpc_delay(task, NFS4_POLL_RETRY_MAX);
837 int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
839 if (!nfs41_sequence_process(task, res))
841 if (res->sr_slot != NULL)
842 nfs41_sequence_free_slot(res);
846 EXPORT_SYMBOL_GPL(nfs41_sequence_done);
848 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
850 if (res->sr_slot == NULL)
852 if (res->sr_slot->table->session != NULL)
853 return nfs41_sequence_process(task, res);
854 return nfs40_sequence_done(task, res);
857 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
859 if (res->sr_slot != NULL) {
860 if (res->sr_slot->table->session != NULL)
861 nfs41_sequence_free_slot(res);
863 nfs40_sequence_free_slot(res);
867 int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
869 if (res->sr_slot == NULL)
871 if (!res->sr_slot->table->session)
872 return nfs40_sequence_done(task, res);
873 return nfs41_sequence_done(task, res);
875 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
877 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
879 struct nfs4_call_sync_data *data = calldata;
881 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
883 nfs4_setup_sequence(data->seq_server->nfs_client,
884 data->seq_args, data->seq_res, task);
887 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
889 struct nfs4_call_sync_data *data = calldata;
891 nfs41_sequence_done(task, data->seq_res);
894 static const struct rpc_call_ops nfs41_call_sync_ops = {
895 .rpc_call_prepare = nfs41_call_sync_prepare,
896 .rpc_call_done = nfs41_call_sync_done,
900 nfs4_sequence_process_interrupted(struct nfs_client *client,
901 struct nfs4_slot *slot, struct rpc_cred *cred)
903 struct rpc_task *task;
905 task = _nfs41_proc_sequence(client, cred, slot, true);
907 rpc_put_task_async(task);
910 #else /* !CONFIG_NFS_V4_1 */
912 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
914 return nfs40_sequence_done(task, res);
917 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
919 if (res->sr_slot != NULL)
920 nfs40_sequence_free_slot(res);
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);
931 nfs4_sequence_process_interrupted(struct nfs_client *client,
932 struct nfs4_slot *slot, struct rpc_cred *cred)
935 slot->interrupted = 0;
938 #endif /* !CONFIG_NFS_V4_1 */
941 void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
942 struct nfs4_sequence_res *res,
943 struct nfs4_slot *slot)
947 slot->privileged = args->sa_privileged ? 1 : 0;
948 args->sa_slot = slot;
951 res->sr_timestamp = jiffies;
952 res->sr_status_flags = 0;
957 int nfs4_setup_sequence(struct nfs_client *client,
958 struct nfs4_sequence_args *args,
959 struct nfs4_sequence_res *res,
960 struct rpc_task *task)
962 struct nfs4_session *session = nfs4_get_session(client);
963 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
964 struct nfs4_slot *slot;
966 /* slot already allocated? */
967 if (res->sr_slot != NULL)
971 tbl = &session->fc_slot_table;
972 task->tk_timeout = 0;
976 spin_lock(&tbl->slot_tbl_lock);
977 /* The state manager will wait until the slot table is empty */
978 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
981 slot = nfs4_alloc_slot(tbl);
983 /* Try again in 1/4 second */
984 if (slot == ERR_PTR(-ENOMEM))
985 task->tk_timeout = HZ >> 2;
988 spin_unlock(&tbl->slot_tbl_lock);
990 if (likely(!slot->interrupted))
992 nfs4_sequence_process_interrupted(client,
993 slot, task->tk_msg.rpc_cred);
996 nfs4_sequence_attach_slot(args, res, slot);
998 trace_nfs4_setup_sequence(session, args);
1000 rpc_call_start(task);
1004 if (args->sa_privileged)
1005 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
1006 NULL, RPC_PRIORITY_PRIVILEGED);
1008 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
1009 spin_unlock(&tbl->slot_tbl_lock);
1012 EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
1014 static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1016 struct nfs4_call_sync_data *data = calldata;
1017 nfs4_setup_sequence(data->seq_server->nfs_client,
1018 data->seq_args, data->seq_res, task);
1021 static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1023 struct nfs4_call_sync_data *data = calldata;
1024 nfs4_sequence_done(task, data->seq_res);
1027 static const struct rpc_call_ops nfs40_call_sync_ops = {
1028 .rpc_call_prepare = nfs40_call_sync_prepare,
1029 .rpc_call_done = nfs40_call_sync_done,
1032 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
1033 struct nfs_server *server,
1034 struct rpc_message *msg,
1035 struct nfs4_sequence_args *args,
1036 struct nfs4_sequence_res *res)
1039 struct rpc_task *task;
1040 struct nfs_client *clp = server->nfs_client;
1041 struct nfs4_call_sync_data data = {
1042 .seq_server = server,
1046 struct rpc_task_setup task_setup = {
1049 .callback_ops = clp->cl_mvops->call_sync_ops,
1050 .callback_data = &data
1053 task = rpc_run_task(&task_setup);
1055 ret = PTR_ERR(task);
1057 ret = task->tk_status;
1063 int nfs4_call_sync(struct rpc_clnt *clnt,
1064 struct nfs_server *server,
1065 struct rpc_message *msg,
1066 struct nfs4_sequence_args *args,
1067 struct nfs4_sequence_res *res,
1070 nfs4_init_sequence(args, res, cache_reply, 0);
1071 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
1074 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1075 unsigned long timestamp)
1077 struct nfs_inode *nfsi = NFS_I(dir);
1079 spin_lock(&dir->i_lock);
1080 nfsi->cache_validity |= NFS_INO_INVALID_CTIME
1081 | NFS_INO_INVALID_MTIME
1082 | NFS_INO_INVALID_DATA;
1083 if (cinfo->atomic && cinfo->before == inode_peek_iversion_raw(dir)) {
1084 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1085 nfsi->attrtimeo_timestamp = jiffies;
1087 nfs_force_lookup_revalidate(dir);
1088 if (cinfo->before != inode_peek_iversion_raw(dir))
1089 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1090 NFS_INO_INVALID_ACL;
1092 inode_set_iversion_raw(dir, cinfo->after);
1093 nfsi->read_cache_jiffies = timestamp;
1094 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
1095 nfs_fscache_invalidate(dir);
1096 spin_unlock(&dir->i_lock);
1099 struct nfs4_open_createattrs {
1100 struct nfs4_label *label;
1101 struct iattr *sattr;
1102 const __u32 verf[2];
1105 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1106 int err, struct nfs4_exception *exception)
1110 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1112 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1113 exception->retry = 1;
1118 nfs4_map_atomic_open_share(struct nfs_server *server,
1119 fmode_t fmode, int openflags)
1123 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1125 res = NFS4_SHARE_ACCESS_READ;
1128 res = NFS4_SHARE_ACCESS_WRITE;
1130 case FMODE_READ|FMODE_WRITE:
1131 res = NFS4_SHARE_ACCESS_BOTH;
1133 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1135 /* Want no delegation if we're using O_DIRECT */
1136 if (openflags & O_DIRECT)
1137 res |= NFS4_SHARE_WANT_NO_DELEG;
1142 static enum open_claim_type4
1143 nfs4_map_atomic_open_claim(struct nfs_server *server,
1144 enum open_claim_type4 claim)
1146 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1151 case NFS4_OPEN_CLAIM_FH:
1152 return NFS4_OPEN_CLAIM_NULL;
1153 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1154 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1155 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1156 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1160 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
1162 p->o_res.f_attr = &p->f_attr;
1163 p->o_res.f_label = p->f_label;
1164 p->o_res.seqid = p->o_arg.seqid;
1165 p->c_res.seqid = p->c_arg.seqid;
1166 p->o_res.server = p->o_arg.server;
1167 p->o_res.access_request = p->o_arg.access;
1168 nfs_fattr_init(&p->f_attr);
1169 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
1172 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
1173 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
1174 const struct nfs4_open_createattrs *c,
1175 enum open_claim_type4 claim,
1178 struct dentry *parent = dget_parent(dentry);
1179 struct inode *dir = d_inode(parent);
1180 struct nfs_server *server = NFS_SERVER(dir);
1181 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
1182 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
1183 struct nfs4_opendata *p;
1185 p = kzalloc(sizeof(*p), gfp_mask);
1189 p->f_label = nfs4_label_alloc(server, gfp_mask);
1190 if (IS_ERR(p->f_label))
1193 p->a_label = nfs4_label_alloc(server, gfp_mask);
1194 if (IS_ERR(p->a_label))
1197 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1198 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
1199 if (IS_ERR(p->o_arg.seqid))
1200 goto err_free_label;
1201 nfs_sb_active(dentry->d_sb);
1202 p->dentry = dget(dentry);
1205 atomic_inc(&sp->so_count);
1206 p->o_arg.open_flags = flags;
1207 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
1208 p->o_arg.umask = current_umask();
1209 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1210 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1212 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1213 * will return permission denied for all bits until close */
1214 if (!(flags & O_EXCL)) {
1215 /* ask server to check for all possible rights as results
1217 switch (p->o_arg.claim) {
1220 case NFS4_OPEN_CLAIM_NULL:
1221 case NFS4_OPEN_CLAIM_FH:
1222 p->o_arg.access = NFS4_ACCESS_READ |
1223 NFS4_ACCESS_MODIFY |
1224 NFS4_ACCESS_EXTEND |
1225 NFS4_ACCESS_EXECUTE;
1228 p->o_arg.clientid = server->nfs_client->cl_clientid;
1229 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1230 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1231 p->o_arg.name = &dentry->d_name;
1232 p->o_arg.server = server;
1233 p->o_arg.bitmask = nfs4_bitmask(server, label);
1234 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1235 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1236 switch (p->o_arg.claim) {
1237 case NFS4_OPEN_CLAIM_NULL:
1238 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1239 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1240 p->o_arg.fh = NFS_FH(dir);
1242 case NFS4_OPEN_CLAIM_PREVIOUS:
1243 case NFS4_OPEN_CLAIM_FH:
1244 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1245 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1246 p->o_arg.fh = NFS_FH(d_inode(dentry));
1248 if (c != NULL && c->sattr != NULL && c->sattr->ia_valid != 0) {
1249 p->o_arg.u.attrs = &p->attrs;
1250 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
1252 memcpy(p->o_arg.u.verifier.data, c->verf,
1253 sizeof(p->o_arg.u.verifier.data));
1255 p->c_arg.fh = &p->o_res.fh;
1256 p->c_arg.stateid = &p->o_res.stateid;
1257 p->c_arg.seqid = p->o_arg.seqid;
1258 nfs4_init_opendata_res(p);
1259 kref_init(&p->kref);
1263 nfs4_label_free(p->a_label);
1265 nfs4_label_free(p->f_label);
1273 static void nfs4_opendata_free(struct kref *kref)
1275 struct nfs4_opendata *p = container_of(kref,
1276 struct nfs4_opendata, kref);
1277 struct super_block *sb = p->dentry->d_sb;
1279 nfs4_lgopen_release(p->lgp);
1280 nfs_free_seqid(p->o_arg.seqid);
1281 nfs4_sequence_free_slot(&p->o_res.seq_res);
1282 if (p->state != NULL)
1283 nfs4_put_open_state(p->state);
1284 nfs4_put_state_owner(p->owner);
1286 nfs4_label_free(p->a_label);
1287 nfs4_label_free(p->f_label);
1291 nfs_sb_deactive(sb);
1292 nfs_fattr_free_names(&p->f_attr);
1293 kfree(p->f_attr.mdsthreshold);
1297 static void nfs4_opendata_put(struct nfs4_opendata *p)
1300 kref_put(&p->kref, nfs4_opendata_free);
1303 static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1306 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1307 case FMODE_READ|FMODE_WRITE:
1308 return state->n_rdwr != 0;
1310 return state->n_wronly != 0;
1312 return state->n_rdonly != 0;
1318 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
1322 if (open_mode & (O_EXCL|O_TRUNC))
1324 switch (mode & (FMODE_READ|FMODE_WRITE)) {
1326 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1327 && state->n_rdonly != 0;
1330 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1331 && state->n_wronly != 0;
1333 case FMODE_READ|FMODE_WRITE:
1334 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1335 && state->n_rdwr != 0;
1341 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1342 enum open_claim_type4 claim)
1344 if (delegation == NULL)
1346 if ((delegation->type & fmode) != fmode)
1348 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1351 case NFS4_OPEN_CLAIM_NULL:
1352 case NFS4_OPEN_CLAIM_FH:
1354 case NFS4_OPEN_CLAIM_PREVIOUS:
1355 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1360 nfs_mark_delegation_referenced(delegation);
1364 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1373 case FMODE_READ|FMODE_WRITE:
1376 nfs4_state_set_mode_locked(state, state->state | fmode);
1379 #ifdef CONFIG_NFS_V4_1
1380 static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1382 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1384 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1386 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1390 #endif /* CONFIG_NFS_V4_1 */
1392 static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1394 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1395 wake_up_all(&state->waitq);
1398 static void nfs_state_log_out_of_order_open_stateid(struct nfs4_state *state,
1399 const nfs4_stateid *stateid)
1401 u32 state_seqid = be32_to_cpu(state->open_stateid.seqid);
1402 u32 stateid_seqid = be32_to_cpu(stateid->seqid);
1404 if (stateid_seqid == state_seqid + 1U ||
1405 (stateid_seqid == 1U && state_seqid == 0xffffffffU))
1406 nfs_state_log_update_open_stateid(state);
1408 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1411 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
1413 struct nfs_client *clp = state->owner->so_server->nfs_client;
1414 bool need_recover = false;
1416 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1417 need_recover = true;
1418 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1419 need_recover = true;
1420 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1421 need_recover = true;
1423 nfs4_state_mark_reclaim_nograce(clp, state);
1427 * Check for whether or not the caller may update the open stateid
1428 * to the value passed in by stateid.
1430 * Note: This function relies heavily on the server implementing
1431 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1433 * i.e. The stateid seqids have to be initialised to 1, and
1434 * are then incremented on every state transition.
1436 static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1437 const nfs4_stateid *stateid)
1439 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0 ||
1440 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1441 if (stateid->seqid == cpu_to_be32(1))
1442 nfs_state_log_update_open_stateid(state);
1444 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1448 if (nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1449 nfs_state_log_out_of_order_open_stateid(state, stateid);
1455 static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1457 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1459 if (state->n_wronly)
1460 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1461 if (state->n_rdonly)
1462 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1464 set_bit(NFS_O_RDWR_STATE, &state->flags);
1465 set_bit(NFS_OPEN_STATE, &state->flags);
1468 static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1469 nfs4_stateid *stateid, fmode_t fmode)
1471 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1472 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1474 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1477 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1480 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1481 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1482 clear_bit(NFS_OPEN_STATE, &state->flags);
1484 if (stateid == NULL)
1486 /* Handle OPEN+OPEN_DOWNGRADE races */
1487 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1488 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1489 nfs_resync_open_stateid_locked(state);
1492 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1493 nfs4_stateid_copy(&state->stateid, stateid);
1494 nfs4_stateid_copy(&state->open_stateid, stateid);
1495 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
1497 nfs_state_log_update_open_stateid(state);
1500 static void nfs_clear_open_stateid(struct nfs4_state *state,
1501 nfs4_stateid *arg_stateid,
1502 nfs4_stateid *stateid, fmode_t fmode)
1504 write_seqlock(&state->seqlock);
1505 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1506 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1507 nfs_clear_open_stateid_locked(state, stateid, fmode);
1508 write_sequnlock(&state->seqlock);
1509 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1510 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1513 static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1514 const nfs4_stateid *stateid, nfs4_stateid *freeme)
1520 if (!nfs_need_update_open_stateid(state, stateid))
1522 if (!test_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1526 /* Rely on seqids for serialisation with NFSv4.0 */
1527 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1530 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1532 * Ensure we process the state changes in the same order
1533 * in which the server processed them by delaying the
1534 * update of the stateid until we are in sequence.
1536 write_sequnlock(&state->seqlock);
1537 spin_unlock(&state->owner->so_lock);
1539 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
1540 if (!signal_pending(current)) {
1541 if (schedule_timeout(5*HZ) == 0)
1547 finish_wait(&state->waitq, &wait);
1549 spin_lock(&state->owner->so_lock);
1550 write_seqlock(&state->seqlock);
1553 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1554 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1555 nfs4_stateid_copy(freeme, &state->open_stateid);
1556 nfs_test_and_clear_all_open_stateid(state);
1559 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1560 nfs4_stateid_copy(&state->stateid, stateid);
1561 nfs4_stateid_copy(&state->open_stateid, stateid);
1562 trace_nfs4_open_stateid_update(state->inode, stateid, status);
1563 nfs_state_log_update_open_stateid(state);
1566 static void nfs_state_set_open_stateid(struct nfs4_state *state,
1567 const nfs4_stateid *open_stateid,
1569 nfs4_stateid *freeme)
1572 * Protect the call to nfs4_state_set_mode_locked and
1573 * serialise the stateid update
1575 write_seqlock(&state->seqlock);
1576 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1579 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1582 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1584 case FMODE_READ|FMODE_WRITE:
1585 set_bit(NFS_O_RDWR_STATE, &state->flags);
1587 set_bit(NFS_OPEN_STATE, &state->flags);
1588 write_sequnlock(&state->seqlock);
1591 static void nfs_state_set_delegation(struct nfs4_state *state,
1592 const nfs4_stateid *deleg_stateid,
1596 * Protect the call to nfs4_state_set_mode_locked and
1597 * serialise the stateid update
1599 write_seqlock(&state->seqlock);
1600 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1601 set_bit(NFS_DELEGATED_STATE, &state->flags);
1602 write_sequnlock(&state->seqlock);
1605 static int update_open_stateid(struct nfs4_state *state,
1606 const nfs4_stateid *open_stateid,
1607 const nfs4_stateid *delegation,
1610 struct nfs_server *server = NFS_SERVER(state->inode);
1611 struct nfs_client *clp = server->nfs_client;
1612 struct nfs_inode *nfsi = NFS_I(state->inode);
1613 struct nfs_delegation *deleg_cur;
1614 nfs4_stateid freeme = { };
1617 fmode &= (FMODE_READ|FMODE_WRITE);
1620 spin_lock(&state->owner->so_lock);
1621 if (open_stateid != NULL) {
1622 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1626 deleg_cur = rcu_dereference(nfsi->delegation);
1627 if (deleg_cur == NULL)
1630 spin_lock(&deleg_cur->lock);
1631 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1632 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1633 (deleg_cur->type & fmode) != fmode)
1634 goto no_delegation_unlock;
1636 if (delegation == NULL)
1637 delegation = &deleg_cur->stateid;
1638 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
1639 goto no_delegation_unlock;
1641 nfs_mark_delegation_referenced(deleg_cur);
1642 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
1644 no_delegation_unlock:
1645 spin_unlock(&deleg_cur->lock);
1648 update_open_stateflags(state, fmode);
1649 spin_unlock(&state->owner->so_lock);
1652 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1653 nfs4_schedule_state_manager(clp);
1654 if (freeme.type != 0)
1655 nfs4_test_and_free_stateid(server, &freeme,
1656 state->owner->so_cred);
1661 static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1662 const nfs4_stateid *stateid)
1664 struct nfs4_state *state = lsp->ls_state;
1667 spin_lock(&state->state_lock);
1668 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1670 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1672 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1675 spin_unlock(&state->state_lock);
1679 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1681 struct nfs_delegation *delegation;
1683 fmode &= FMODE_READ|FMODE_WRITE;
1685 delegation = rcu_dereference(NFS_I(inode)->delegation);
1686 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1691 nfs4_inode_return_delegation(inode);
1694 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1696 struct nfs4_state *state = opendata->state;
1697 struct nfs_inode *nfsi = NFS_I(state->inode);
1698 struct nfs_delegation *delegation;
1699 int open_mode = opendata->o_arg.open_flags;
1700 fmode_t fmode = opendata->o_arg.fmode;
1701 enum open_claim_type4 claim = opendata->o_arg.claim;
1702 nfs4_stateid stateid;
1706 spin_lock(&state->owner->so_lock);
1707 if (can_open_cached(state, fmode, open_mode)) {
1708 update_open_stateflags(state, fmode);
1709 spin_unlock(&state->owner->so_lock);
1710 goto out_return_state;
1712 spin_unlock(&state->owner->so_lock);
1714 delegation = rcu_dereference(nfsi->delegation);
1715 if (!can_open_delegated(delegation, fmode, claim)) {
1719 /* Save the delegation */
1720 nfs4_stateid_copy(&stateid, &delegation->stateid);
1722 nfs_release_seqid(opendata->o_arg.seqid);
1723 if (!opendata->is_recover) {
1724 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1730 /* Try to update the stateid using the delegation */
1731 if (update_open_stateid(state, NULL, &stateid, fmode))
1732 goto out_return_state;
1735 return ERR_PTR(ret);
1737 atomic_inc(&state->count);
1742 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1744 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1745 struct nfs_delegation *delegation;
1746 int delegation_flags = 0;
1749 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1751 delegation_flags = delegation->flags;
1753 switch (data->o_arg.claim) {
1756 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1757 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1758 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1759 "returning a delegation for "
1760 "OPEN(CLAIM_DELEGATE_CUR)\n",
1764 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1765 nfs_inode_set_delegation(state->inode,
1766 data->owner->so_cred,
1767 data->o_res.delegation_type,
1768 &data->o_res.delegation,
1769 data->o_res.pagemod_limit);
1771 nfs_inode_reclaim_delegation(state->inode,
1772 data->owner->so_cred,
1773 data->o_res.delegation_type,
1774 &data->o_res.delegation,
1775 data->o_res.pagemod_limit);
1779 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1780 * and update the nfs4_state.
1782 static struct nfs4_state *
1783 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1785 struct inode *inode = data->state->inode;
1786 struct nfs4_state *state = data->state;
1789 if (!data->rpc_done) {
1790 if (data->rpc_status)
1791 return ERR_PTR(data->rpc_status);
1792 /* cached opens have already been processed */
1796 ret = nfs_refresh_inode(inode, &data->f_attr);
1798 return ERR_PTR(ret);
1800 if (data->o_res.delegation_type != 0)
1801 nfs4_opendata_check_deleg(data, state);
1803 update_open_stateid(state, &data->o_res.stateid, NULL,
1805 atomic_inc(&state->count);
1810 static struct inode *
1811 nfs4_opendata_get_inode(struct nfs4_opendata *data)
1813 struct inode *inode;
1815 switch (data->o_arg.claim) {
1816 case NFS4_OPEN_CLAIM_NULL:
1817 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1818 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1819 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1820 return ERR_PTR(-EAGAIN);
1821 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1822 &data->f_attr, data->f_label);
1825 inode = d_inode(data->dentry);
1827 nfs_refresh_inode(inode, &data->f_attr);
1832 static struct nfs4_state *
1833 nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
1835 struct nfs4_state *state;
1836 struct inode *inode;
1838 inode = nfs4_opendata_get_inode(data);
1840 return ERR_CAST(inode);
1841 if (data->state != NULL && data->state->inode == inode) {
1842 state = data->state;
1843 atomic_inc(&state->count);
1845 state = nfs4_get_open_state(inode, data->owner);
1848 state = ERR_PTR(-ENOMEM);
1852 static struct nfs4_state *
1853 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1855 struct nfs4_state *state;
1857 if (!data->rpc_done) {
1858 state = nfs4_try_open_cached(data);
1859 trace_nfs4_cached_open(data->state);
1863 state = nfs4_opendata_find_nfs4_state(data);
1867 if (data->o_res.delegation_type != 0)
1868 nfs4_opendata_check_deleg(data, state);
1869 update_open_stateid(state, &data->o_res.stateid, NULL,
1872 nfs_release_seqid(data->o_arg.seqid);
1876 static struct nfs4_state *
1877 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1879 struct nfs4_state *ret;
1881 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1882 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1884 ret = _nfs4_opendata_to_nfs4_state(data);
1885 nfs4_sequence_free_slot(&data->o_res.seq_res);
1889 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1891 struct nfs_inode *nfsi = NFS_I(state->inode);
1892 struct nfs_open_context *ctx;
1894 spin_lock(&state->inode->i_lock);
1895 list_for_each_entry(ctx, &nfsi->open_files, list) {
1896 if (ctx->state != state)
1898 get_nfs_open_context(ctx);
1899 spin_unlock(&state->inode->i_lock);
1902 spin_unlock(&state->inode->i_lock);
1903 return ERR_PTR(-ENOENT);
1906 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1907 struct nfs4_state *state, enum open_claim_type4 claim)
1909 struct nfs4_opendata *opendata;
1911 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
1912 NULL, claim, GFP_NOFS);
1913 if (opendata == NULL)
1914 return ERR_PTR(-ENOMEM);
1915 opendata->state = state;
1916 atomic_inc(&state->count);
1920 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1923 struct nfs4_state *newstate;
1926 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
1928 opendata->o_arg.open_flags = 0;
1929 opendata->o_arg.fmode = fmode;
1930 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1931 NFS_SB(opendata->dentry->d_sb),
1933 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1934 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1935 nfs4_init_opendata_res(opendata);
1936 ret = _nfs4_recover_proc_open(opendata);
1939 newstate = nfs4_opendata_to_nfs4_state(opendata);
1940 if (IS_ERR(newstate))
1941 return PTR_ERR(newstate);
1942 if (newstate != opendata->state)
1944 nfs4_close_state(newstate, fmode);
1948 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1952 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1953 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1954 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1955 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1956 /* memory barrier prior to reading state->n_* */
1957 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1958 clear_bit(NFS_OPEN_STATE, &state->flags);
1960 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1963 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1966 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1970 * We may have performed cached opens for all three recoveries.
1971 * Check if we need to update the current stateid.
1973 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1974 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
1975 write_seqlock(&state->seqlock);
1976 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1977 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1978 write_sequnlock(&state->seqlock);
1985 * reclaim state on the server after a reboot.
1987 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1989 struct nfs_delegation *delegation;
1990 struct nfs4_opendata *opendata;
1991 fmode_t delegation_type = 0;
1994 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1995 NFS4_OPEN_CLAIM_PREVIOUS);
1996 if (IS_ERR(opendata))
1997 return PTR_ERR(opendata);
1999 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2000 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
2001 delegation_type = delegation->type;
2003 opendata->o_arg.u.delegation_type = delegation_type;
2004 status = nfs4_open_recover(opendata, state);
2005 nfs4_opendata_put(opendata);
2009 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2011 struct nfs_server *server = NFS_SERVER(state->inode);
2012 struct nfs4_exception exception = { };
2015 err = _nfs4_do_open_reclaim(ctx, state);
2016 trace_nfs4_open_reclaim(ctx, 0, err);
2017 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2019 if (err != -NFS4ERR_DELAY)
2021 nfs4_handle_exception(server, err, &exception);
2022 } while (exception.retry);
2026 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2028 struct nfs_open_context *ctx;
2031 ctx = nfs4_state_find_open_context(state);
2034 ret = nfs4_do_open_reclaim(ctx, state);
2035 put_nfs_open_context(ctx);
2039 static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, struct file_lock *fl, int err)
2043 printk(KERN_ERR "NFS: %s: unhandled error "
2044 "%d.\n", __func__, err);
2050 case -NFS4ERR_BADSESSION:
2051 case -NFS4ERR_BADSLOT:
2052 case -NFS4ERR_BAD_HIGH_SLOT:
2053 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2054 case -NFS4ERR_DEADSESSION:
2055 set_bit(NFS_DELEGATED_STATE, &state->flags);
2056 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
2058 case -NFS4ERR_STALE_CLIENTID:
2059 case -NFS4ERR_STALE_STATEID:
2060 set_bit(NFS_DELEGATED_STATE, &state->flags);
2061 /* Don't recall a delegation if it was lost */
2062 nfs4_schedule_lease_recovery(server->nfs_client);
2064 case -NFS4ERR_MOVED:
2065 nfs4_schedule_migration_recovery(server);
2067 case -NFS4ERR_LEASE_MOVED:
2068 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2070 case -NFS4ERR_DELEG_REVOKED:
2071 case -NFS4ERR_ADMIN_REVOKED:
2072 case -NFS4ERR_EXPIRED:
2073 case -NFS4ERR_BAD_STATEID:
2074 case -NFS4ERR_OPENMODE:
2075 nfs_inode_find_state_and_recover(state->inode,
2077 nfs4_schedule_stateid_recovery(server, state);
2079 case -NFS4ERR_DELAY:
2080 case -NFS4ERR_GRACE:
2081 set_bit(NFS_DELEGATED_STATE, &state->flags);
2085 case -NFS4ERR_DENIED:
2087 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2089 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2096 int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2097 struct nfs4_state *state, const nfs4_stateid *stateid,
2100 struct nfs_server *server = NFS_SERVER(state->inode);
2101 struct nfs4_opendata *opendata;
2104 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2105 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2106 if (IS_ERR(opendata))
2107 return PTR_ERR(opendata);
2108 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
2109 write_seqlock(&state->seqlock);
2110 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2111 write_sequnlock(&state->seqlock);
2112 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2113 switch (type & (FMODE_READ|FMODE_WRITE)) {
2114 case FMODE_READ|FMODE_WRITE:
2116 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2119 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2123 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2125 nfs4_opendata_put(opendata);
2126 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
2129 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2131 struct nfs4_opendata *data = calldata;
2133 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2134 &data->c_arg.seq_args, &data->c_res.seq_res, task);
2137 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2139 struct nfs4_opendata *data = calldata;
2141 nfs40_sequence_done(task, &data->c_res.seq_res);
2143 data->rpc_status = task->tk_status;
2144 if (data->rpc_status == 0) {
2145 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
2146 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2147 renew_lease(data->o_res.server, data->timestamp);
2148 data->rpc_done = true;
2152 static void nfs4_open_confirm_release(void *calldata)
2154 struct nfs4_opendata *data = calldata;
2155 struct nfs4_state *state = NULL;
2157 /* If this request hasn't been cancelled, do nothing */
2158 if (!data->cancelled)
2160 /* In case of error, no cleanup! */
2161 if (!data->rpc_done)
2163 state = nfs4_opendata_to_nfs4_state(data);
2165 nfs4_close_state(state, data->o_arg.fmode);
2167 nfs4_opendata_put(data);
2170 static const struct rpc_call_ops nfs4_open_confirm_ops = {
2171 .rpc_call_prepare = nfs4_open_confirm_prepare,
2172 .rpc_call_done = nfs4_open_confirm_done,
2173 .rpc_release = nfs4_open_confirm_release,
2177 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2179 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2181 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
2182 struct rpc_task *task;
2183 struct rpc_message msg = {
2184 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2185 .rpc_argp = &data->c_arg,
2186 .rpc_resp = &data->c_res,
2187 .rpc_cred = data->owner->so_cred,
2189 struct rpc_task_setup task_setup_data = {
2190 .rpc_client = server->client,
2191 .rpc_message = &msg,
2192 .callback_ops = &nfs4_open_confirm_ops,
2193 .callback_data = data,
2194 .workqueue = nfsiod_workqueue,
2195 .flags = RPC_TASK_ASYNC,
2199 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2201 kref_get(&data->kref);
2202 data->rpc_done = false;
2203 data->rpc_status = 0;
2204 data->timestamp = jiffies;
2205 task = rpc_run_task(&task_setup_data);
2207 return PTR_ERR(task);
2208 status = rpc_wait_for_completion_task(task);
2210 data->cancelled = true;
2213 status = data->rpc_status;
2218 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
2220 struct nfs4_opendata *data = calldata;
2221 struct nfs4_state_owner *sp = data->owner;
2222 struct nfs_client *clp = sp->so_server->nfs_client;
2223 enum open_claim_type4 claim = data->o_arg.claim;
2225 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
2228 * Check if we still need to send an OPEN call, or if we can use
2229 * a delegation instead.
2231 if (data->state != NULL) {
2232 struct nfs_delegation *delegation;
2234 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
2237 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
2238 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
2239 goto unlock_no_action;
2242 /* Update client id. */
2243 data->o_arg.clientid = clp->cl_clientid;
2247 case NFS4_OPEN_CLAIM_PREVIOUS:
2248 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2249 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
2250 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
2251 case NFS4_OPEN_CLAIM_FH:
2252 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
2254 data->timestamp = jiffies;
2255 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
2256 &data->o_arg.seq_args,
2257 &data->o_res.seq_res,
2259 nfs_release_seqid(data->o_arg.seqid);
2261 /* Set the create mode (note dependency on the session type) */
2262 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2263 if (data->o_arg.open_flags & O_EXCL) {
2264 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2265 if (nfs4_has_persistent_session(clp))
2266 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2267 else if (clp->cl_mvops->minor_version > 0)
2268 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2272 trace_nfs4_cached_open(data->state);
2275 task->tk_action = NULL;
2277 nfs4_sequence_done(task, &data->o_res.seq_res);
2280 static void nfs4_open_done(struct rpc_task *task, void *calldata)
2282 struct nfs4_opendata *data = calldata;
2284 data->rpc_status = task->tk_status;
2286 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
2289 if (task->tk_status == 0) {
2290 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2291 switch (data->o_res.f_attr->mode & S_IFMT) {
2295 data->rpc_status = -ELOOP;
2298 data->rpc_status = -EISDIR;
2301 data->rpc_status = -ENOTDIR;
2304 renew_lease(data->o_res.server, data->timestamp);
2305 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2306 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2308 data->rpc_done = true;
2311 static void nfs4_open_release(void *calldata)
2313 struct nfs4_opendata *data = calldata;
2314 struct nfs4_state *state = NULL;
2316 /* If this request hasn't been cancelled, do nothing */
2317 if (!data->cancelled)
2319 /* In case of error, no cleanup! */
2320 if (data->rpc_status != 0 || !data->rpc_done)
2322 /* In case we need an open_confirm, no cleanup! */
2323 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2325 state = nfs4_opendata_to_nfs4_state(data);
2327 nfs4_close_state(state, data->o_arg.fmode);
2329 nfs4_opendata_put(data);
2332 static const struct rpc_call_ops nfs4_open_ops = {
2333 .rpc_call_prepare = nfs4_open_prepare,
2334 .rpc_call_done = nfs4_open_done,
2335 .rpc_release = nfs4_open_release,
2338 static int nfs4_run_open_task(struct nfs4_opendata *data,
2339 struct nfs_open_context *ctx)
2341 struct inode *dir = d_inode(data->dir);
2342 struct nfs_server *server = NFS_SERVER(dir);
2343 struct nfs_openargs *o_arg = &data->o_arg;
2344 struct nfs_openres *o_res = &data->o_res;
2345 struct rpc_task *task;
2346 struct rpc_message msg = {
2347 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2350 .rpc_cred = data->owner->so_cred,
2352 struct rpc_task_setup task_setup_data = {
2353 .rpc_client = server->client,
2354 .rpc_message = &msg,
2355 .callback_ops = &nfs4_open_ops,
2356 .callback_data = data,
2357 .workqueue = nfsiod_workqueue,
2358 .flags = RPC_TASK_ASYNC,
2362 kref_get(&data->kref);
2363 data->rpc_done = false;
2364 data->rpc_status = 0;
2365 data->cancelled = false;
2366 data->is_recover = false;
2368 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
2369 data->is_recover = true;
2371 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
2372 pnfs_lgopen_prepare(data, ctx);
2374 task = rpc_run_task(&task_setup_data);
2376 return PTR_ERR(task);
2377 status = rpc_wait_for_completion_task(task);
2379 data->cancelled = true;
2382 status = data->rpc_status;
2388 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2390 struct inode *dir = d_inode(data->dir);
2391 struct nfs_openres *o_res = &data->o_res;
2394 status = nfs4_run_open_task(data, NULL);
2395 if (status != 0 || !data->rpc_done)
2398 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2400 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
2401 status = _nfs4_proc_open_confirm(data);
2407 * Additional permission checks in order to distinguish between an
2408 * open for read, and an open for execute. This works around the
2409 * fact that NFSv4 OPEN treats read and execute permissions as being
2411 * Note that in the non-execute case, we want to turn off permission
2412 * checking if we just created a new file (POSIX open() semantics).
2414 static int nfs4_opendata_access(struct rpc_cred *cred,
2415 struct nfs4_opendata *opendata,
2416 struct nfs4_state *state, fmode_t fmode,
2419 struct nfs_access_entry cache;
2422 /* access call failed or for some reason the server doesn't
2423 * support any access modes -- defer access call until later */
2424 if (opendata->o_res.access_supported == 0)
2429 * Use openflags to check for exec, because fmode won't
2430 * always have FMODE_EXEC set when file open for exec.
2432 if (openflags & __FMODE_EXEC) {
2433 /* ONLY check for exec rights */
2434 if (S_ISDIR(state->inode->i_mode))
2435 mask = NFS4_ACCESS_LOOKUP;
2437 mask = NFS4_ACCESS_EXECUTE;
2438 } else if ((fmode & FMODE_READ) && !opendata->file_created)
2439 mask = NFS4_ACCESS_READ;
2442 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2443 nfs_access_add_cache(state->inode, &cache);
2445 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2446 if ((mask & ~cache.mask & flags) == 0)
2453 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2455 static int _nfs4_proc_open(struct nfs4_opendata *data,
2456 struct nfs_open_context *ctx)
2458 struct inode *dir = d_inode(data->dir);
2459 struct nfs_server *server = NFS_SERVER(dir);
2460 struct nfs_openargs *o_arg = &data->o_arg;
2461 struct nfs_openres *o_res = &data->o_res;
2464 status = nfs4_run_open_task(data, ctx);
2465 if (!data->rpc_done)
2468 if (status == -NFS4ERR_BADNAME &&
2469 !(o_arg->open_flags & O_CREAT))
2474 nfs_fattr_map_and_free_names(server, &data->f_attr);
2476 if (o_arg->open_flags & O_CREAT) {
2477 if (o_arg->open_flags & O_EXCL)
2478 data->file_created = true;
2479 else if (o_res->cinfo.before != o_res->cinfo.after)
2480 data->file_created = true;
2481 if (data->file_created ||
2482 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
2483 update_changeattr(dir, &o_res->cinfo,
2484 o_res->f_attr->time_start);
2486 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2487 server->caps &= ~NFS_CAP_POSIX_LOCK;
2488 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2489 status = _nfs4_proc_open_confirm(data);
2493 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2494 nfs4_sequence_free_slot(&o_res->seq_res);
2495 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr,
2496 o_res->f_label, NULL);
2503 * reclaim state on the server after a network partition.
2504 * Assumes caller holds the appropriate lock
2506 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2508 struct nfs4_opendata *opendata;
2511 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2512 NFS4_OPEN_CLAIM_FH);
2513 if (IS_ERR(opendata))
2514 return PTR_ERR(opendata);
2515 ret = nfs4_open_recover(opendata, state);
2517 d_drop(ctx->dentry);
2518 nfs4_opendata_put(opendata);
2522 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2524 struct nfs_server *server = NFS_SERVER(state->inode);
2525 struct nfs4_exception exception = { };
2529 err = _nfs4_open_expired(ctx, state);
2530 trace_nfs4_open_expired(ctx, 0, err);
2531 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2536 case -NFS4ERR_GRACE:
2537 case -NFS4ERR_DELAY:
2538 nfs4_handle_exception(server, err, &exception);
2541 } while (exception.retry);
2546 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2548 struct nfs_open_context *ctx;
2551 ctx = nfs4_state_find_open_context(state);
2554 ret = nfs4_do_open_expired(ctx, state);
2555 put_nfs_open_context(ctx);
2559 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2560 const nfs4_stateid *stateid)
2562 nfs_remove_bad_delegation(state->inode, stateid);
2563 write_seqlock(&state->seqlock);
2564 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2565 write_sequnlock(&state->seqlock);
2566 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2569 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2571 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2572 nfs_finish_clear_delegation_stateid(state, NULL);
2575 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2577 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2578 nfs40_clear_delegation_stateid(state);
2579 return nfs4_open_expired(sp, state);
2582 static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2583 nfs4_stateid *stateid,
2584 struct rpc_cred *cred)
2586 return -NFS4ERR_BAD_STATEID;
2589 #if defined(CONFIG_NFS_V4_1)
2590 static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2591 nfs4_stateid *stateid,
2592 struct rpc_cred *cred)
2596 switch (stateid->type) {
2599 case NFS4_INVALID_STATEID_TYPE:
2600 case NFS4_SPECIAL_STATEID_TYPE:
2601 return -NFS4ERR_BAD_STATEID;
2602 case NFS4_REVOKED_STATEID_TYPE:
2606 status = nfs41_test_stateid(server, stateid, cred);
2608 case -NFS4ERR_EXPIRED:
2609 case -NFS4ERR_ADMIN_REVOKED:
2610 case -NFS4ERR_DELEG_REVOKED:
2616 /* Ack the revoked state to the server */
2617 nfs41_free_stateid(server, stateid, cred, true);
2618 return -NFS4ERR_EXPIRED;
2621 static void nfs41_check_delegation_stateid(struct nfs4_state *state)
2623 struct nfs_server *server = NFS_SERVER(state->inode);
2624 nfs4_stateid stateid;
2625 struct nfs_delegation *delegation;
2626 struct rpc_cred *cred;
2629 /* Get the delegation credential for use by test/free_stateid */
2631 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2632 if (delegation == NULL) {
2637 nfs4_stateid_copy(&stateid, &delegation->stateid);
2638 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) ||
2639 !test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2640 &delegation->flags)) {
2642 nfs_finish_clear_delegation_stateid(state, &stateid);
2646 cred = get_rpccred(delegation->cred);
2648 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
2649 trace_nfs4_test_delegation_stateid(state, NULL, status);
2650 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
2651 nfs_finish_clear_delegation_stateid(state, &stateid);
2657 * nfs41_check_expired_locks - possibly free a lock stateid
2659 * @state: NFSv4 state for an inode
2661 * Returns NFS_OK if recovery for this stateid is now finished.
2662 * Otherwise a negative NFS4ERR value is returned.
2664 static int nfs41_check_expired_locks(struct nfs4_state *state)
2666 int status, ret = NFS_OK;
2667 struct nfs4_lock_state *lsp, *prev = NULL;
2668 struct nfs_server *server = NFS_SERVER(state->inode);
2670 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2673 spin_lock(&state->state_lock);
2674 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2675 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2676 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2678 refcount_inc(&lsp->ls_count);
2679 spin_unlock(&state->state_lock);
2681 nfs4_put_lock_state(prev);
2684 status = nfs41_test_and_free_expired_stateid(server,
2687 trace_nfs4_test_lock_stateid(state, lsp, status);
2688 if (status == -NFS4ERR_EXPIRED ||
2689 status == -NFS4ERR_BAD_STATEID) {
2690 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
2691 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
2692 if (!recover_lost_locks)
2693 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2694 } else if (status != NFS_OK) {
2696 nfs4_put_lock_state(prev);
2699 spin_lock(&state->state_lock);
2702 spin_unlock(&state->state_lock);
2703 nfs4_put_lock_state(prev);
2709 * nfs41_check_open_stateid - possibly free an open stateid
2711 * @state: NFSv4 state for an inode
2713 * Returns NFS_OK if recovery for this stateid is now finished.
2714 * Otherwise a negative NFS4ERR value is returned.
2716 static int nfs41_check_open_stateid(struct nfs4_state *state)
2718 struct nfs_server *server = NFS_SERVER(state->inode);
2719 nfs4_stateid *stateid = &state->open_stateid;
2720 struct rpc_cred *cred = state->owner->so_cred;
2723 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
2724 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2725 if (nfs4_have_delegation(state->inode, state->state))
2727 return -NFS4ERR_OPENMODE;
2729 return -NFS4ERR_BAD_STATEID;
2731 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
2732 trace_nfs4_test_open_stateid(state, NULL, status);
2733 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
2734 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2735 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2736 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2737 clear_bit(NFS_OPEN_STATE, &state->flags);
2738 stateid->type = NFS4_INVALID_STATEID_TYPE;
2741 if (nfs_open_stateid_recover_openmode(state))
2742 return -NFS4ERR_OPENMODE;
2746 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2750 nfs41_check_delegation_stateid(state);
2751 status = nfs41_check_expired_locks(state);
2752 if (status != NFS_OK)
2754 status = nfs41_check_open_stateid(state);
2755 if (status != NFS_OK)
2756 status = nfs4_open_expired(sp, state);
2762 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2763 * fields corresponding to attributes that were used to store the verifier.
2764 * Make sure we clobber those fields in the later setattr call
2766 static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2767 struct iattr *sattr, struct nfs4_label **label)
2769 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2774 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
2775 attrset[i] = opendata->o_res.attrset[i];
2776 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
2777 attrset[i] &= ~bitmask[i];
2780 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
2781 sattr->ia_valid : 0;
2783 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
2784 if (sattr->ia_valid & ATTR_ATIME_SET)
2785 ret |= ATTR_ATIME_SET;
2790 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
2791 if (sattr->ia_valid & ATTR_MTIME_SET)
2792 ret |= ATTR_MTIME_SET;
2797 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
2802 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2805 struct nfs_open_context *ctx)
2807 struct nfs4_state_owner *sp = opendata->owner;
2808 struct nfs_server *server = sp->so_server;
2809 struct dentry *dentry;
2810 struct nfs4_state *state;
2814 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2816 ret = _nfs4_proc_open(opendata, ctx);
2820 state = _nfs4_opendata_to_nfs4_state(opendata);
2821 ret = PTR_ERR(state);
2825 if (server->caps & NFS_CAP_POSIX_LOCK)
2826 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2827 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2828 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
2830 dentry = opendata->dentry;
2831 if (d_really_is_negative(dentry)) {
2832 struct dentry *alias;
2834 alias = d_exact_alias(dentry, state->inode);
2836 alias = d_splice_alias(igrab(state->inode), dentry);
2837 /* d_splice_alias() can't fail here - it's a non-directory */
2840 ctx->dentry = dentry = alias;
2842 nfs_set_verifier(dentry,
2843 nfs_save_change_attribute(d_inode(opendata->dir)));
2846 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2850 if (d_inode(dentry) == state->inode) {
2851 nfs_inode_attach_open_context(ctx);
2852 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2853 nfs4_schedule_stateid_recovery(server, state);
2855 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
2859 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
2864 * Returns a referenced nfs4_state
2866 static int _nfs4_do_open(struct inode *dir,
2867 struct nfs_open_context *ctx,
2869 const struct nfs4_open_createattrs *c,
2872 struct nfs4_state_owner *sp;
2873 struct nfs4_state *state = NULL;
2874 struct nfs_server *server = NFS_SERVER(dir);
2875 struct nfs4_opendata *opendata;
2876 struct dentry *dentry = ctx->dentry;
2877 struct rpc_cred *cred = ctx->cred;
2878 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2879 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
2880 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
2881 struct iattr *sattr = c->sattr;
2882 struct nfs4_label *label = c->label;
2883 struct nfs4_label *olabel = NULL;
2886 /* Protect against reboot recovery conflicts */
2888 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2890 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2893 status = nfs4_client_recover_expired_lease(server->nfs_client);
2895 goto err_put_state_owner;
2896 if (d_really_is_positive(dentry))
2897 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
2899 if (d_really_is_positive(dentry))
2900 claim = NFS4_OPEN_CLAIM_FH;
2901 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
2902 c, claim, GFP_KERNEL);
2903 if (opendata == NULL)
2904 goto err_put_state_owner;
2907 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2908 if (IS_ERR(olabel)) {
2909 status = PTR_ERR(olabel);
2910 goto err_opendata_put;
2914 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2915 if (!opendata->f_attr.mdsthreshold) {
2916 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2917 if (!opendata->f_attr.mdsthreshold)
2918 goto err_free_label;
2920 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
2922 if (d_really_is_positive(dentry))
2923 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
2925 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
2927 goto err_free_label;
2930 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
2931 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
2932 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
2934 * send create attributes which was not set by open
2935 * with an extra setattr.
2937 if (attrs || label) {
2938 unsigned ia_old = sattr->ia_valid;
2940 sattr->ia_valid = attrs;
2941 nfs_fattr_init(opendata->o_res.f_attr);
2942 status = nfs4_do_setattr(state->inode, cred,
2943 opendata->o_res.f_attr, sattr,
2944 ctx, label, olabel);
2946 nfs_setattr_update_inode(state->inode, sattr,
2947 opendata->o_res.f_attr);
2948 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2950 sattr->ia_valid = ia_old;
2953 if (opened && opendata->file_created)
2954 *opened |= FILE_CREATED;
2956 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
2957 *ctx_th = opendata->f_attr.mdsthreshold;
2958 opendata->f_attr.mdsthreshold = NULL;
2961 nfs4_label_free(olabel);
2963 nfs4_opendata_put(opendata);
2964 nfs4_put_state_owner(sp);
2967 nfs4_label_free(olabel);
2969 nfs4_opendata_put(opendata);
2970 err_put_state_owner:
2971 nfs4_put_state_owner(sp);
2977 static struct nfs4_state *nfs4_do_open(struct inode *dir,
2978 struct nfs_open_context *ctx,
2980 struct iattr *sattr,
2981 struct nfs4_label *label,
2984 struct nfs_server *server = NFS_SERVER(dir);
2985 struct nfs4_exception exception = { };
2986 struct nfs4_state *res;
2987 struct nfs4_open_createattrs c = {
2991 [0] = (__u32)jiffies,
2992 [1] = (__u32)current->pid,
2998 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
3000 trace_nfs4_open_file(ctx, flags, status);
3003 /* NOTE: BAD_SEQID means the server and client disagree about the
3004 * book-keeping w.r.t. state-changing operations
3005 * (OPEN/CLOSE/LOCK/LOCKU...)
3006 * It is actually a sign of a bug on the client or on the server.
3008 * If we receive a BAD_SEQID error in the particular case of
3009 * doing an OPEN, we assume that nfs_increment_open_seqid() will
3010 * have unhashed the old state_owner for us, and that we can
3011 * therefore safely retry using a new one. We should still warn
3012 * the user though...
3014 if (status == -NFS4ERR_BAD_SEQID) {
3015 pr_warn_ratelimited("NFS: v4 server %s "
3016 " returned a bad sequence-id error!\n",
3017 NFS_SERVER(dir)->nfs_client->cl_hostname);
3018 exception.retry = 1;
3022 * BAD_STATEID on OPEN means that the server cancelled our
3023 * state before it received the OPEN_CONFIRM.
3024 * Recover by retrying the request as per the discussion
3025 * on Page 181 of RFC3530.
3027 if (status == -NFS4ERR_BAD_STATEID) {
3028 exception.retry = 1;
3031 if (status == -EAGAIN) {
3032 /* We must have found a delegation */
3033 exception.retry = 1;
3036 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3038 res = ERR_PTR(nfs4_handle_exception(server,
3039 status, &exception));
3040 } while (exception.retry);
3044 static int _nfs4_do_setattr(struct inode *inode,
3045 struct nfs_setattrargs *arg,
3046 struct nfs_setattrres *res,
3047 struct rpc_cred *cred,
3048 struct nfs_open_context *ctx)
3050 struct nfs_server *server = NFS_SERVER(inode);
3051 struct rpc_message msg = {
3052 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3057 struct rpc_cred *delegation_cred = NULL;
3058 unsigned long timestamp = jiffies;
3062 nfs_fattr_init(res->fattr);
3064 /* Servers should only apply open mode checks for file size changes */
3065 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
3069 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
3070 /* Use that stateid */
3071 } else if (ctx != NULL) {
3072 struct nfs_lock_context *l_ctx;
3073 if (!nfs4_valid_open_stateid(ctx->state))
3075 l_ctx = nfs_get_lock_context(ctx);
3077 return PTR_ERR(l_ctx);
3078 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3079 &arg->stateid, &delegation_cred);
3080 nfs_put_lock_context(l_ctx);
3085 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
3087 if (delegation_cred)
3088 msg.rpc_cred = delegation_cred;
3090 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3092 put_rpccred(delegation_cred);
3093 if (status == 0 && ctx != NULL)
3094 renew_lease(server, timestamp);
3095 trace_nfs4_setattr(inode, &arg->stateid, status);
3099 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
3100 struct nfs_fattr *fattr, struct iattr *sattr,
3101 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
3102 struct nfs4_label *olabel)
3104 struct nfs_server *server = NFS_SERVER(inode);
3105 __u32 bitmask[NFS4_BITMASK_SZ];
3106 struct nfs4_state *state = ctx ? ctx->state : NULL;
3107 struct nfs_setattrargs arg = {
3108 .fh = NFS_FH(inode),
3114 struct nfs_setattrres res = {
3119 struct nfs4_exception exception = {
3122 .stateid = &arg.stateid,
3127 nfs4_bitmap_copy_adjust_setattr(bitmask,
3128 nfs4_bitmask(server, olabel),
3131 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
3133 case -NFS4ERR_OPENMODE:
3134 if (!(sattr->ia_valid & ATTR_SIZE)) {
3135 pr_warn_once("NFSv4: server %s is incorrectly "
3136 "applying open mode checks to "
3137 "a SETATTR that is not "
3138 "changing file size.\n",
3139 server->nfs_client->cl_hostname);
3141 if (state && !(state->state & FMODE_WRITE)) {
3143 if (sattr->ia_valid & ATTR_OPEN)
3148 err = nfs4_handle_exception(server, err, &exception);
3149 } while (exception.retry);
3155 nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3157 if (inode == NULL || !nfs_have_layout(inode))
3160 return pnfs_wait_on_layoutreturn(inode, task);
3163 struct nfs4_closedata {
3164 struct inode *inode;
3165 struct nfs4_state *state;
3166 struct nfs_closeargs arg;
3167 struct nfs_closeres res;
3169 struct nfs4_layoutreturn_args arg;
3170 struct nfs4_layoutreturn_res res;
3171 struct nfs4_xdr_opaque_data ld_private;
3175 struct nfs_fattr fattr;
3176 unsigned long timestamp;
3179 static void nfs4_free_closedata(void *data)
3181 struct nfs4_closedata *calldata = data;
3182 struct nfs4_state_owner *sp = calldata->state->owner;
3183 struct super_block *sb = calldata->state->inode->i_sb;
3185 if (calldata->lr.roc)
3186 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3187 calldata->res.lr_ret);
3188 nfs4_put_open_state(calldata->state);
3189 nfs_free_seqid(calldata->arg.seqid);
3190 nfs4_put_state_owner(sp);
3191 nfs_sb_deactive(sb);
3195 static void nfs4_close_done(struct rpc_task *task, void *data)
3197 struct nfs4_closedata *calldata = data;
3198 struct nfs4_state *state = calldata->state;
3199 struct nfs_server *server = NFS_SERVER(calldata->inode);
3200 nfs4_stateid *res_stateid = NULL;
3201 struct nfs4_exception exception = {
3203 .inode = calldata->inode,
3204 .stateid = &calldata->arg.stateid,
3207 dprintk("%s: begin!\n", __func__);
3208 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3210 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
3212 /* Handle Layoutreturn errors */
3213 if (calldata->arg.lr_args && task->tk_status != 0) {
3214 switch (calldata->res.lr_ret) {
3216 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3219 calldata->arg.lr_args = NULL;
3220 calldata->res.lr_res = NULL;
3222 case -NFS4ERR_OLD_STATEID:
3223 if (nfs4_refresh_layout_stateid(&calldata->arg.lr_args->stateid,
3227 case -NFS4ERR_ADMIN_REVOKED:
3228 case -NFS4ERR_DELEG_REVOKED:
3229 case -NFS4ERR_EXPIRED:
3230 case -NFS4ERR_BAD_STATEID:
3231 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3232 case -NFS4ERR_WRONG_CRED:
3233 calldata->arg.lr_args = NULL;
3234 calldata->res.lr_res = NULL;
3239 /* hmm. we are done with the inode, and in the process of freeing
3240 * the state_owner. we keep this around to process errors
3242 switch (task->tk_status) {
3244 res_stateid = &calldata->res.stateid;
3245 renew_lease(server, calldata->timestamp);
3247 case -NFS4ERR_ACCESS:
3248 if (calldata->arg.bitmask != NULL) {
3249 calldata->arg.bitmask = NULL;
3250 calldata->res.fattr = NULL;
3255 case -NFS4ERR_OLD_STATEID:
3256 /* Did we race with OPEN? */
3257 if (nfs4_refresh_open_stateid(&calldata->arg.stateid,
3261 case -NFS4ERR_ADMIN_REVOKED:
3262 case -NFS4ERR_STALE_STATEID:
3263 case -NFS4ERR_EXPIRED:
3264 nfs4_free_revoked_stateid(server,
3265 &calldata->arg.stateid,
3266 task->tk_msg.rpc_cred);
3268 case -NFS4ERR_BAD_STATEID:
3271 task->tk_status = nfs4_async_handle_exception(task,
3272 server, task->tk_status, &exception);
3273 if (exception.retry)
3276 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3277 res_stateid, calldata->arg.fmode);
3279 task->tk_status = 0;
3280 nfs_release_seqid(calldata->arg.seqid);
3281 nfs_refresh_inode(calldata->inode, &calldata->fattr);
3282 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
3285 calldata->res.lr_ret = 0;
3287 task->tk_status = 0;
3288 rpc_restart_call_prepare(task);
3292 static void nfs4_close_prepare(struct rpc_task *task, void *data)
3294 struct nfs4_closedata *calldata = data;
3295 struct nfs4_state *state = calldata->state;
3296 struct inode *inode = calldata->inode;
3297 struct pnfs_layout_hdr *lo;
3298 bool is_rdonly, is_wronly, is_rdwr;
3301 dprintk("%s: begin!\n", __func__);
3302 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3305 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
3306 spin_lock(&state->owner->so_lock);
3307 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3308 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3309 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
3310 /* Calculate the change in open mode */
3311 calldata->arg.fmode = 0;
3312 if (state->n_rdwr == 0) {
3313 if (state->n_rdonly == 0)
3314 call_close |= is_rdonly;
3316 calldata->arg.fmode |= FMODE_READ;
3317 if (state->n_wronly == 0)
3318 call_close |= is_wronly;
3320 calldata->arg.fmode |= FMODE_WRITE;
3321 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3322 call_close |= is_rdwr;
3324 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3326 if (!nfs4_valid_open_stateid(state) ||
3327 !nfs4_refresh_open_stateid(&calldata->arg.stateid, state))
3329 spin_unlock(&state->owner->so_lock);
3332 /* Note: exit _without_ calling nfs4_close_done */
3336 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
3337 nfs_release_seqid(calldata->arg.seqid);
3341 lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3342 if (lo && !pnfs_layout_is_valid(lo)) {
3343 calldata->arg.lr_args = NULL;
3344 calldata->res.lr_res = NULL;
3347 if (calldata->arg.fmode == 0)
3348 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
3350 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
3351 /* Close-to-open cache consistency revalidation */
3352 if (!nfs4_have_delegation(inode, FMODE_READ))
3353 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3355 calldata->arg.bitmask = NULL;
3358 calldata->arg.share_access =
3359 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3360 calldata->arg.fmode, 0);
3362 if (calldata->res.fattr == NULL)
3363 calldata->arg.bitmask = NULL;
3364 else if (calldata->arg.bitmask == NULL)
3365 calldata->res.fattr = NULL;
3366 calldata->timestamp = jiffies;
3367 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
3368 &calldata->arg.seq_args,
3369 &calldata->res.seq_res,
3371 nfs_release_seqid(calldata->arg.seqid);
3372 dprintk("%s: done!\n", __func__);
3375 task->tk_action = NULL;
3377 nfs4_sequence_done(task, &calldata->res.seq_res);
3380 static const struct rpc_call_ops nfs4_close_ops = {
3381 .rpc_call_prepare = nfs4_close_prepare,
3382 .rpc_call_done = nfs4_close_done,
3383 .rpc_release = nfs4_free_closedata,
3387 * It is possible for data to be read/written from a mem-mapped file
3388 * after the sys_close call (which hits the vfs layer as a flush).
3389 * This means that we can't safely call nfsv4 close on a file until
3390 * the inode is cleared. This in turn means that we are not good
3391 * NFSv4 citizens - we do not indicate to the server to update the file's
3392 * share state even when we are done with one of the three share
3393 * stateid's in the inode.
3395 * NOTE: Caller must be holding the sp->so_owner semaphore!
3397 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
3399 struct nfs_server *server = NFS_SERVER(state->inode);
3400 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
3401 struct nfs4_closedata *calldata;
3402 struct nfs4_state_owner *sp = state->owner;
3403 struct rpc_task *task;
3404 struct rpc_message msg = {
3405 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3406 .rpc_cred = state->owner->so_cred,
3408 struct rpc_task_setup task_setup_data = {
3409 .rpc_client = server->client,
3410 .rpc_message = &msg,
3411 .callback_ops = &nfs4_close_ops,
3412 .workqueue = nfsiod_workqueue,
3413 .flags = RPC_TASK_ASYNC,
3415 int status = -ENOMEM;
3417 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3418 &task_setup_data.rpc_client, &msg);
3420 calldata = kzalloc(sizeof(*calldata), gfp_mask);
3421 if (calldata == NULL)
3423 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
3424 calldata->inode = state->inode;
3425 calldata->state = state;
3426 calldata->arg.fh = NFS_FH(state->inode);
3427 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3428 goto out_free_calldata;
3429 /* Serialization for the sequence id */
3430 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3431 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
3432 if (IS_ERR(calldata->arg.seqid))
3433 goto out_free_calldata;
3434 nfs_fattr_init(&calldata->fattr);
3435 calldata->arg.fmode = 0;
3436 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
3437 calldata->res.fattr = &calldata->fattr;
3438 calldata->res.seqid = calldata->arg.seqid;
3439 calldata->res.server = server;
3440 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3441 calldata->lr.roc = pnfs_roc(state->inode,
3442 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3443 if (calldata->lr.roc) {
3444 calldata->arg.lr_args = &calldata->lr.arg;
3445 calldata->res.lr_res = &calldata->lr.res;
3447 nfs_sb_active(calldata->inode->i_sb);
3449 msg.rpc_argp = &calldata->arg;
3450 msg.rpc_resp = &calldata->res;
3451 task_setup_data.callback_data = calldata;
3452 task = rpc_run_task(&task_setup_data);
3454 return PTR_ERR(task);
3457 status = rpc_wait_for_completion_task(task);
3463 nfs4_put_open_state(state);
3464 nfs4_put_state_owner(sp);
3468 static struct inode *
3469 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3470 int open_flags, struct iattr *attr, int *opened)
3472 struct nfs4_state *state;
3473 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3475 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
3477 /* Protect against concurrent sillydeletes */
3478 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
3480 nfs4_label_release_security(label);
3483 return ERR_CAST(state);
3484 return state->inode;
3487 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
3489 if (ctx->state == NULL)
3492 nfs4_close_sync(ctx->state, ctx->mode);
3494 nfs4_close_state(ctx->state, ctx->mode);
3497 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3498 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3499 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
3501 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3503 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
3504 struct nfs4_server_caps_arg args = {
3508 struct nfs4_server_caps_res res = {};
3509 struct rpc_message msg = {
3510 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
3517 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3518 FATTR4_WORD0_FH_EXPIRE_TYPE |
3519 FATTR4_WORD0_LINK_SUPPORT |
3520 FATTR4_WORD0_SYMLINK_SUPPORT |
3521 FATTR4_WORD0_ACLSUPPORT;
3523 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3525 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3527 /* Sanity check the server answers */
3528 switch (minorversion) {
3530 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3531 res.attr_bitmask[2] = 0;
3534 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3537 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3539 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
3540 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3541 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3542 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3543 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
3544 NFS_CAP_CTIME|NFS_CAP_MTIME|
3545 NFS_CAP_SECURITY_LABEL);
3546 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3547 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3548 server->caps |= NFS_CAP_ACLS;
3549 if (res.has_links != 0)
3550 server->caps |= NFS_CAP_HARDLINKS;
3551 if (res.has_symlinks != 0)
3552 server->caps |= NFS_CAP_SYMLINKS;
3553 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3554 server->caps |= NFS_CAP_FILEID;
3555 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3556 server->caps |= NFS_CAP_MODE;
3557 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3558 server->caps |= NFS_CAP_NLINK;
3559 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3560 server->caps |= NFS_CAP_OWNER;
3561 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3562 server->caps |= NFS_CAP_OWNER_GROUP;
3563 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3564 server->caps |= NFS_CAP_ATIME;
3565 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3566 server->caps |= NFS_CAP_CTIME;
3567 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3568 server->caps |= NFS_CAP_MTIME;
3569 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3570 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3571 server->caps |= NFS_CAP_SECURITY_LABEL;
3573 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3574 sizeof(server->attr_bitmask));
3575 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3577 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3578 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3579 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
3580 server->cache_consistency_bitmask[2] = 0;
3582 /* Avoid a regression due to buggy server */
3583 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3584 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
3585 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3586 sizeof(server->exclcreat_bitmask));
3588 server->acl_bitmask = res.acl_bitmask;
3589 server->fh_expire_type = res.fh_expire_type;
3595 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3597 struct nfs4_exception exception = { };
3600 err = nfs4_handle_exception(server,
3601 _nfs4_server_capabilities(server, fhandle),
3603 } while (exception.retry);
3607 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3608 struct nfs_fsinfo *info)
3611 struct nfs4_lookup_root_arg args = {
3614 struct nfs4_lookup_res res = {
3616 .fattr = info->fattr,
3619 struct rpc_message msg = {
3620 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3625 bitmask[0] = nfs4_fattr_bitmap[0];
3626 bitmask[1] = nfs4_fattr_bitmap[1];
3628 * Process the label in the upcoming getfattr
3630 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3632 nfs_fattr_init(info->fattr);
3633 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3636 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3637 struct nfs_fsinfo *info)
3639 struct nfs4_exception exception = { };
3642 err = _nfs4_lookup_root(server, fhandle, info);
3643 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
3646 case -NFS4ERR_WRONGSEC:
3649 err = nfs4_handle_exception(server, err, &exception);
3651 } while (exception.retry);
3656 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3657 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3659 struct rpc_auth_create_args auth_args = {
3660 .pseudoflavor = flavor,
3662 struct rpc_auth *auth;
3664 auth = rpcauth_create(&auth_args, server->client);
3667 return nfs4_lookup_root(server, fhandle, info);
3671 * Retry pseudoroot lookup with various security flavors. We do this when:
3673 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3674 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3676 * Returns zero on success, or a negative NFS4ERR value, or a
3677 * negative errno value.
3679 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3680 struct nfs_fsinfo *info)
3682 /* Per 3530bis 15.33.5 */
3683 static const rpc_authflavor_t flav_array[] = {
3687 RPC_AUTH_UNIX, /* courtesy */
3690 int status = -EPERM;
3693 if (server->auth_info.flavor_len > 0) {
3694 /* try each flavor specified by user */
3695 for (i = 0; i < server->auth_info.flavor_len; i++) {
3696 status = nfs4_lookup_root_sec(server, fhandle, info,
3697 server->auth_info.flavors[i]);
3698 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3703 /* no flavors specified by user, try default list */
3704 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3705 status = nfs4_lookup_root_sec(server, fhandle, info,
3707 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3714 * -EACCESS could mean that the user doesn't have correct permissions
3715 * to access the mount. It could also mean that we tried to mount
3716 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3717 * existing mount programs don't handle -EACCES very well so it should
3718 * be mapped to -EPERM instead.
3720 if (status == -EACCES)
3726 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3727 * @server: initialized nfs_server handle
3728 * @fhandle: we fill in the pseudo-fs root file handle
3729 * @info: we fill in an FSINFO struct
3730 * @auth_probe: probe the auth flavours
3732 * Returns zero on success, or a negative errno.
3734 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
3735 struct nfs_fsinfo *info,
3741 status = nfs4_lookup_root(server, fhandle, info);
3743 if (auth_probe || status == NFS4ERR_WRONGSEC)
3744 status = server->nfs_client->cl_mvops->find_root_sec(server,
3748 status = nfs4_server_capabilities(server, fhandle);
3750 status = nfs4_do_fsinfo(server, fhandle, info);
3752 return nfs4_map_errors(status);
3755 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3756 struct nfs_fsinfo *info)
3759 struct nfs_fattr *fattr = info->fattr;
3760 struct nfs4_label *label = NULL;
3762 error = nfs4_server_capabilities(server, mntfh);
3764 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3768 label = nfs4_label_alloc(server, GFP_KERNEL);
3770 return PTR_ERR(label);
3772 error = nfs4_proc_getattr(server, mntfh, fattr, label, NULL);
3774 dprintk("nfs4_get_root: getattr error = %d\n", -error);
3775 goto err_free_label;
3778 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3779 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3780 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3783 nfs4_label_free(label);
3789 * Get locations and (maybe) other attributes of a referral.
3790 * Note that we'll actually follow the referral later when
3791 * we detect fsid mismatch in inode revalidation
3793 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3794 const struct qstr *name, struct nfs_fattr *fattr,
3795 struct nfs_fh *fhandle)
3797 int status = -ENOMEM;
3798 struct page *page = NULL;
3799 struct nfs4_fs_locations *locations = NULL;
3801 page = alloc_page(GFP_KERNEL);
3804 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3805 if (locations == NULL)
3808 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
3813 * If the fsid didn't change, this is a migration event, not a
3814 * referral. Cause us to drop into the exception handler, which
3815 * will kick off migration recovery.
3817 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
3818 dprintk("%s: server did not return a different fsid for"
3819 " a referral at %s\n", __func__, name->name);
3820 status = -NFS4ERR_MOVED;
3823 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3824 nfs_fixup_referral_attributes(&locations->fattr);
3826 /* replace the lookup nfs_fattr with the locations nfs_fattr */
3827 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
3828 memset(fhandle, 0, sizeof(struct nfs_fh));
3836 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3837 struct nfs_fattr *fattr, struct nfs4_label *label,
3838 struct inode *inode)
3840 __u32 bitmask[NFS4_BITMASK_SZ];
3841 struct nfs4_getattr_arg args = {
3845 struct nfs4_getattr_res res = {
3850 struct rpc_message msg = {
3851 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3856 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode);
3858 nfs_fattr_init(fattr);
3859 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3862 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3863 struct nfs_fattr *fattr, struct nfs4_label *label,
3864 struct inode *inode)
3866 struct nfs4_exception exception = { };
3869 err = _nfs4_proc_getattr(server, fhandle, fattr, label, inode);
3870 trace_nfs4_getattr(server, fhandle, fattr, err);
3871 err = nfs4_handle_exception(server, err,
3873 } while (exception.retry);
3878 * The file is not closed if it is opened due to the a request to change
3879 * the size of the file. The open call will not be needed once the
3880 * VFS layer lookup-intents are implemented.
3882 * Close is called when the inode is destroyed.
3883 * If we haven't opened the file for O_WRONLY, we
3884 * need to in the size_change case to obtain a stateid.
3887 * Because OPEN is always done by name in nfsv4, it is
3888 * possible that we opened a different file by the same
3889 * name. We can recognize this race condition, but we
3890 * can't do anything about it besides returning an error.
3892 * This will be fixed with VFS changes (lookup-intent).
3895 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3896 struct iattr *sattr)
3898 struct inode *inode = d_inode(dentry);
3899 struct rpc_cred *cred = NULL;
3900 struct nfs_open_context *ctx = NULL;
3901 struct nfs4_label *label = NULL;
3904 if (pnfs_ld_layoutret_on_setattr(inode) &&
3905 sattr->ia_valid & ATTR_SIZE &&
3906 sattr->ia_size < i_size_read(inode))
3907 pnfs_commit_and_return_layout(inode);
3909 nfs_fattr_init(fattr);
3911 /* Deal with open(O_TRUNC) */
3912 if (sattr->ia_valid & ATTR_OPEN)
3913 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
3915 /* Optimization: if the end result is no change, don't RPC */
3916 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
3919 /* Search for an existing open(O_WRITE) file */
3920 if (sattr->ia_valid & ATTR_FILE) {
3922 ctx = nfs_file_open_context(sattr->ia_file);
3927 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3929 return PTR_ERR(label);
3931 /* Return any delegations if we're going to change ACLs */
3932 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
3933 nfs4_inode_make_writeable(inode);
3935 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
3937 nfs_setattr_update_inode(inode, sattr, fattr);
3938 nfs_setsecurity(inode, fattr, label);
3940 nfs4_label_free(label);
3944 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3945 const struct qstr *name, struct nfs_fh *fhandle,
3946 struct nfs_fattr *fattr, struct nfs4_label *label)
3948 struct nfs_server *server = NFS_SERVER(dir);
3950 struct nfs4_lookup_arg args = {
3951 .bitmask = server->attr_bitmask,
3952 .dir_fh = NFS_FH(dir),
3955 struct nfs4_lookup_res res = {
3961 struct rpc_message msg = {
3962 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3967 args.bitmask = nfs4_bitmask(server, label);
3969 nfs_fattr_init(fattr);
3971 dprintk("NFS call lookup %s\n", name->name);
3972 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
3973 dprintk("NFS reply lookup: %d\n", status);
3977 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
3979 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
3980 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
3981 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3985 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3986 const struct qstr *name, struct nfs_fh *fhandle,
3987 struct nfs_fattr *fattr, struct nfs4_label *label)
3989 struct nfs4_exception exception = { };
3990 struct rpc_clnt *client = *clnt;
3993 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
3994 trace_nfs4_lookup(dir, name, err);
3996 case -NFS4ERR_BADNAME:
3999 case -NFS4ERR_MOVED:
4000 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
4001 if (err == -NFS4ERR_MOVED)
4002 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4004 case -NFS4ERR_WRONGSEC:
4006 if (client != *clnt)
4008 client = nfs4_negotiate_security(client, dir, name);
4010 return PTR_ERR(client);
4012 exception.retry = 1;
4015 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4017 } while (exception.retry);
4022 else if (client != *clnt)
4023 rpc_shutdown_client(client);
4028 static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
4029 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4030 struct nfs4_label *label)
4033 struct rpc_clnt *client = NFS_CLIENT(dir);
4035 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
4036 if (client != NFS_CLIENT(dir)) {
4037 rpc_shutdown_client(client);
4038 nfs_fixup_secinfo_attributes(fattr);
4044 nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
4045 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4047 struct rpc_clnt *client = NFS_CLIENT(dir);
4050 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
4052 return ERR_PTR(status);
4053 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
4056 static int _nfs4_proc_lookupp(struct inode *inode,
4057 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4058 struct nfs4_label *label)
4060 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4061 struct nfs_server *server = NFS_SERVER(inode);
4063 struct nfs4_lookupp_arg args = {
4064 .bitmask = server->attr_bitmask,
4065 .fh = NFS_FH(inode),
4067 struct nfs4_lookupp_res res = {
4073 struct rpc_message msg = {
4074 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4079 args.bitmask = nfs4_bitmask(server, label);
4081 nfs_fattr_init(fattr);
4083 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4084 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4086 dprintk("NFS reply lookupp: %d\n", status);
4090 static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4091 struct nfs_fattr *fattr, struct nfs4_label *label)
4093 struct nfs4_exception exception = { };
4096 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4097 trace_nfs4_lookupp(inode, err);
4098 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4100 } while (exception.retry);
4104 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4106 struct nfs_server *server = NFS_SERVER(inode);
4107 struct nfs4_accessargs args = {
4108 .fh = NFS_FH(inode),
4109 .access = entry->mask,
4111 struct nfs4_accessres res = {
4114 struct rpc_message msg = {
4115 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4118 .rpc_cred = entry->cred,
4122 if (!nfs4_have_delegation(inode, FMODE_READ)) {
4123 res.fattr = nfs_alloc_fattr();
4124 if (res.fattr == NULL)
4126 args.bitmask = server->cache_consistency_bitmask;
4129 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4131 nfs_access_set_mask(entry, res.access);
4133 nfs_refresh_inode(inode, res.fattr);
4135 nfs_free_fattr(res.fattr);
4139 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4141 struct nfs4_exception exception = { };
4144 err = _nfs4_proc_access(inode, entry);
4145 trace_nfs4_access(inode, err);
4146 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4148 } while (exception.retry);
4153 * TODO: For the time being, we don't try to get any attributes
4154 * along with any of the zero-copy operations READ, READDIR,
4157 * In the case of the first three, we want to put the GETATTR
4158 * after the read-type operation -- this is because it is hard
4159 * to predict the length of a GETATTR response in v4, and thus
4160 * align the READ data correctly. This means that the GETATTR
4161 * may end up partially falling into the page cache, and we should
4162 * shift it into the 'tail' of the xdr_buf before processing.
4163 * To do this efficiently, we need to know the total length
4164 * of data received, which doesn't seem to be available outside
4167 * In the case of WRITE, we also want to put the GETATTR after
4168 * the operation -- in this case because we want to make sure
4169 * we get the post-operation mtime and size.
4171 * Both of these changes to the XDR layer would in fact be quite
4172 * minor, but I decided to leave them for a subsequent patch.
4174 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4175 unsigned int pgbase, unsigned int pglen)
4177 struct nfs4_readlink args = {
4178 .fh = NFS_FH(inode),
4183 struct nfs4_readlink_res res;
4184 struct rpc_message msg = {
4185 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4190 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
4193 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4194 unsigned int pgbase, unsigned int pglen)
4196 struct nfs4_exception exception = { };
4199 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4200 trace_nfs4_readlink(inode, err);
4201 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4203 } while (exception.retry);
4208 * This is just for mknod. open(O_CREAT) will always do ->open_context().
4211 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
4214 struct nfs_server *server = NFS_SERVER(dir);
4215 struct nfs4_label l, *ilabel = NULL;
4216 struct nfs_open_context *ctx;
4217 struct nfs4_state *state;
4220 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
4222 return PTR_ERR(ctx);
4224 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4226 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4227 sattr->ia_mode &= ~current_umask();
4228 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
4229 if (IS_ERR(state)) {
4230 status = PTR_ERR(state);
4234 nfs4_label_release_security(ilabel);
4235 put_nfs_open_context(ctx);
4239 static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
4241 struct nfs_server *server = NFS_SERVER(dir);
4242 struct nfs_removeargs args = {
4246 struct nfs_removeres res = {
4249 struct rpc_message msg = {
4250 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4254 unsigned long timestamp = jiffies;
4257 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
4259 update_changeattr(dir, &res.cinfo, timestamp);
4263 static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4265 struct nfs4_exception exception = { };
4266 struct inode *inode = d_inode(dentry);
4270 if (inode->i_nlink == 1)
4271 nfs4_inode_return_delegation(inode);
4273 nfs4_inode_make_writeable(inode);
4276 err = _nfs4_proc_remove(dir, &dentry->d_name);
4277 trace_nfs4_remove(dir, &dentry->d_name, err);
4278 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4280 } while (exception.retry);
4284 static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
4286 struct nfs4_exception exception = { };
4290 err = _nfs4_proc_remove(dir, name);
4291 trace_nfs4_remove(dir, name, err);
4292 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4294 } while (exception.retry);
4298 static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4299 struct dentry *dentry,
4300 struct inode *inode)
4302 struct nfs_removeargs *args = msg->rpc_argp;
4303 struct nfs_removeres *res = msg->rpc_resp;
4305 res->server = NFS_SB(dentry->d_sb);
4306 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
4307 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
4309 nfs_fattr_init(res->dir_attr);
4312 nfs4_inode_return_delegation(inode);
4315 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4317 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
4318 &data->args.seq_args,
4323 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
4325 struct nfs_unlinkdata *data = task->tk_calldata;
4326 struct nfs_removeres *res = &data->res;
4328 if (!nfs4_sequence_done(task, &res->seq_res))
4330 if (nfs4_async_handle_error(task, res->server, NULL,
4331 &data->timeout) == -EAGAIN)
4333 if (task->tk_status == 0)
4334 update_changeattr(dir, &res->cinfo, res->dir_attr->time_start);
4338 static void nfs4_proc_rename_setup(struct rpc_message *msg,
4339 struct dentry *old_dentry,
4340 struct dentry *new_dentry)
4342 struct nfs_renameargs *arg = msg->rpc_argp;
4343 struct nfs_renameres *res = msg->rpc_resp;
4344 struct inode *old_inode = d_inode(old_dentry);
4345 struct inode *new_inode = d_inode(new_dentry);
4348 nfs4_inode_make_writeable(old_inode);
4350 nfs4_inode_return_delegation(new_inode);
4351 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
4352 res->server = NFS_SB(old_dentry->d_sb);
4353 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
4356 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4358 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
4359 &data->args.seq_args,
4364 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4365 struct inode *new_dir)
4367 struct nfs_renamedata *data = task->tk_calldata;
4368 struct nfs_renameres *res = &data->res;
4370 if (!nfs4_sequence_done(task, &res->seq_res))
4372 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
4375 if (task->tk_status == 0) {
4376 update_changeattr(old_dir, &res->old_cinfo, res->old_fattr->time_start);
4377 if (new_dir != old_dir)
4378 update_changeattr(new_dir, &res->new_cinfo, res->new_fattr->time_start);
4383 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4385 struct nfs_server *server = NFS_SERVER(inode);
4386 __u32 bitmask[NFS4_BITMASK_SZ];
4387 struct nfs4_link_arg arg = {
4388 .fh = NFS_FH(inode),
4389 .dir_fh = NFS_FH(dir),
4393 struct nfs4_link_res res = {
4397 struct rpc_message msg = {
4398 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4402 int status = -ENOMEM;
4404 res.fattr = nfs_alloc_fattr();
4405 if (res.fattr == NULL)
4408 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4409 if (IS_ERR(res.label)) {
4410 status = PTR_ERR(res.label);
4414 nfs4_inode_make_writeable(inode);
4415 nfs4_bitmap_copy_adjust_setattr(bitmask, nfs4_bitmask(server, res.label), inode);
4417 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4419 update_changeattr(dir, &res.cinfo, res.fattr->time_start);
4420 status = nfs_post_op_update_inode(inode, res.fattr);
4422 nfs_setsecurity(inode, res.fattr, res.label);
4426 nfs4_label_free(res.label);
4429 nfs_free_fattr(res.fattr);
4433 static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4435 struct nfs4_exception exception = { };
4438 err = nfs4_handle_exception(NFS_SERVER(inode),
4439 _nfs4_proc_link(inode, dir, name),
4441 } while (exception.retry);
4445 struct nfs4_createdata {
4446 struct rpc_message msg;
4447 struct nfs4_create_arg arg;
4448 struct nfs4_create_res res;
4450 struct nfs_fattr fattr;
4451 struct nfs4_label *label;
4454 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
4455 const struct qstr *name, struct iattr *sattr, u32 ftype)
4457 struct nfs4_createdata *data;
4459 data = kzalloc(sizeof(*data), GFP_KERNEL);
4461 struct nfs_server *server = NFS_SERVER(dir);
4463 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4464 if (IS_ERR(data->label))
4467 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4468 data->msg.rpc_argp = &data->arg;
4469 data->msg.rpc_resp = &data->res;
4470 data->arg.dir_fh = NFS_FH(dir);
4471 data->arg.server = server;
4472 data->arg.name = name;
4473 data->arg.attrs = sattr;
4474 data->arg.ftype = ftype;
4475 data->arg.bitmask = nfs4_bitmask(server, data->label);
4476 data->arg.umask = current_umask();
4477 data->res.server = server;
4478 data->res.fh = &data->fh;
4479 data->res.fattr = &data->fattr;
4480 data->res.label = data->label;
4481 nfs_fattr_init(data->res.fattr);
4489 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4491 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
4492 &data->arg.seq_args, &data->res.seq_res, 1);
4494 update_changeattr(dir, &data->res.dir_cinfo,
4495 data->res.fattr->time_start);
4496 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
4501 static void nfs4_free_createdata(struct nfs4_createdata *data)
4503 nfs4_label_free(data->label);
4507 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4508 struct page *page, unsigned int len, struct iattr *sattr,
4509 struct nfs4_label *label)
4511 struct nfs4_createdata *data;
4512 int status = -ENAMETOOLONG;
4514 if (len > NFS4_MAXPATHLEN)
4518 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4522 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4523 data->arg.u.symlink.pages = &page;
4524 data->arg.u.symlink.len = len;
4525 data->arg.label = label;
4527 status = nfs4_do_create(dir, dentry, data);
4529 nfs4_free_createdata(data);
4534 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4535 struct page *page, unsigned int len, struct iattr *sattr)
4537 struct nfs4_exception exception = { };
4538 struct nfs4_label l, *label = NULL;
4541 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4544 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4545 trace_nfs4_symlink(dir, &dentry->d_name, err);
4546 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4548 } while (exception.retry);
4550 nfs4_label_release_security(label);
4554 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4555 struct iattr *sattr, struct nfs4_label *label)
4557 struct nfs4_createdata *data;
4558 int status = -ENOMEM;
4560 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4564 data->arg.label = label;
4565 status = nfs4_do_create(dir, dentry, data);
4567 nfs4_free_createdata(data);
4572 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4573 struct iattr *sattr)
4575 struct nfs_server *server = NFS_SERVER(dir);
4576 struct nfs4_exception exception = { };
4577 struct nfs4_label l, *label = NULL;
4580 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4582 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4583 sattr->ia_mode &= ~current_umask();
4585 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4586 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4587 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4589 } while (exception.retry);
4590 nfs4_label_release_security(label);
4595 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
4596 u64 cookie, struct page **pages, unsigned int count, bool plus)
4598 struct inode *dir = d_inode(dentry);
4599 struct nfs4_readdir_arg args = {
4604 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
4607 struct nfs4_readdir_res res;
4608 struct rpc_message msg = {
4609 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4616 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4618 (unsigned long long)cookie);
4619 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
4620 res.pgbase = args.pgbase;
4621 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4623 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
4624 status += args.pgbase;
4627 nfs_invalidate_atime(dir);
4629 dprintk("%s: returns %d\n", __func__, status);
4633 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
4634 u64 cookie, struct page **pages, unsigned int count, bool plus)
4636 struct nfs4_exception exception = { };
4639 err = _nfs4_proc_readdir(dentry, cred, cookie,
4640 pages, count, plus);
4641 trace_nfs4_readdir(d_inode(dentry), err);
4642 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
4644 } while (exception.retry);
4648 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4649 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
4651 struct nfs4_createdata *data;
4652 int mode = sattr->ia_mode;
4653 int status = -ENOMEM;
4655 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4660 data->arg.ftype = NF4FIFO;
4661 else if (S_ISBLK(mode)) {
4662 data->arg.ftype = NF4BLK;
4663 data->arg.u.device.specdata1 = MAJOR(rdev);
4664 data->arg.u.device.specdata2 = MINOR(rdev);
4666 else if (S_ISCHR(mode)) {
4667 data->arg.ftype = NF4CHR;
4668 data->arg.u.device.specdata1 = MAJOR(rdev);
4669 data->arg.u.device.specdata2 = MINOR(rdev);
4670 } else if (!S_ISSOCK(mode)) {
4675 data->arg.label = label;
4676 status = nfs4_do_create(dir, dentry, data);
4678 nfs4_free_createdata(data);
4683 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4684 struct iattr *sattr, dev_t rdev)
4686 struct nfs_server *server = NFS_SERVER(dir);
4687 struct nfs4_exception exception = { };
4688 struct nfs4_label l, *label = NULL;
4691 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4693 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4694 sattr->ia_mode &= ~current_umask();
4696 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4697 trace_nfs4_mknod(dir, &dentry->d_name, err);
4698 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4700 } while (exception.retry);
4702 nfs4_label_release_security(label);
4707 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4708 struct nfs_fsstat *fsstat)
4710 struct nfs4_statfs_arg args = {
4712 .bitmask = server->attr_bitmask,
4714 struct nfs4_statfs_res res = {
4717 struct rpc_message msg = {
4718 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4723 nfs_fattr_init(fsstat->fattr);
4724 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4727 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4729 struct nfs4_exception exception = { };
4732 err = nfs4_handle_exception(server,
4733 _nfs4_proc_statfs(server, fhandle, fsstat),
4735 } while (exception.retry);
4739 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4740 struct nfs_fsinfo *fsinfo)
4742 struct nfs4_fsinfo_arg args = {
4744 .bitmask = server->attr_bitmask,
4746 struct nfs4_fsinfo_res res = {
4749 struct rpc_message msg = {
4750 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4755 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4758 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4760 struct nfs4_exception exception = { };
4761 unsigned long now = jiffies;
4765 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
4766 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
4768 nfs4_set_lease_period(server->nfs_client,
4769 fsinfo->lease_time * HZ,
4773 err = nfs4_handle_exception(server, err, &exception);
4774 } while (exception.retry);
4778 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4782 nfs_fattr_init(fsinfo->fattr);
4783 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
4785 /* block layout checks this! */
4786 server->pnfs_blksize = fsinfo->blksize;
4787 set_pnfs_layoutdriver(server, fhandle, fsinfo);
4793 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4794 struct nfs_pathconf *pathconf)
4796 struct nfs4_pathconf_arg args = {
4798 .bitmask = server->attr_bitmask,
4800 struct nfs4_pathconf_res res = {
4801 .pathconf = pathconf,
4803 struct rpc_message msg = {
4804 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4809 /* None of the pathconf attributes are mandatory to implement */
4810 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4811 memset(pathconf, 0, sizeof(*pathconf));
4815 nfs_fattr_init(pathconf->fattr);
4816 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4819 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4820 struct nfs_pathconf *pathconf)
4822 struct nfs4_exception exception = { };
4826 err = nfs4_handle_exception(server,
4827 _nfs4_proc_pathconf(server, fhandle, pathconf),
4829 } while (exception.retry);
4833 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
4834 const struct nfs_open_context *ctx,
4835 const struct nfs_lock_context *l_ctx,
4838 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
4840 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4842 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4843 const struct nfs_open_context *ctx,
4844 const struct nfs_lock_context *l_ctx,
4847 nfs4_stateid current_stateid;
4849 /* If the current stateid represents a lost lock, then exit */
4850 if (nfs4_set_rw_stateid(¤t_stateid, ctx, l_ctx, fmode) == -EIO)
4852 return nfs4_stateid_match(stateid, ¤t_stateid);
4855 static bool nfs4_error_stateid_expired(int err)
4858 case -NFS4ERR_DELEG_REVOKED:
4859 case -NFS4ERR_ADMIN_REVOKED:
4860 case -NFS4ERR_BAD_STATEID:
4861 case -NFS4ERR_STALE_STATEID:
4862 case -NFS4ERR_OLD_STATEID:
4863 case -NFS4ERR_OPENMODE:
4864 case -NFS4ERR_EXPIRED:
4870 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
4872 struct nfs_server *server = NFS_SERVER(hdr->inode);
4874 trace_nfs4_read(hdr, task->tk_status);
4875 if (task->tk_status < 0) {
4876 struct nfs4_exception exception = {
4877 .inode = hdr->inode,
4878 .state = hdr->args.context->state,
4879 .stateid = &hdr->args.stateid,
4881 task->tk_status = nfs4_async_handle_exception(task,
4882 server, task->tk_status, &exception);
4883 if (exception.retry) {
4884 rpc_restart_call_prepare(task);
4889 if (task->tk_status > 0)
4890 renew_lease(server, hdr->timestamp);
4894 static bool nfs4_read_stateid_changed(struct rpc_task *task,
4895 struct nfs_pgio_args *args)
4898 if (!nfs4_error_stateid_expired(task->tk_status) ||
4899 nfs4_stateid_is_current(&args->stateid,
4904 rpc_restart_call_prepare(task);
4908 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
4911 dprintk("--> %s\n", __func__);
4913 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
4915 if (nfs4_read_stateid_changed(task, &hdr->args))
4917 if (task->tk_status > 0)
4918 nfs_invalidate_atime(hdr->inode);
4919 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4920 nfs4_read_done_cb(task, hdr);
4923 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4924 struct rpc_message *msg)
4926 hdr->timestamp = jiffies;
4927 if (!hdr->pgio_done_cb)
4928 hdr->pgio_done_cb = nfs4_read_done_cb;
4929 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
4930 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
4933 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4934 struct nfs_pgio_header *hdr)
4936 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
4937 &hdr->args.seq_args,
4941 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4942 hdr->args.lock_context,
4943 hdr->rw_mode) == -EIO)
4945 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
4950 static int nfs4_write_done_cb(struct rpc_task *task,
4951 struct nfs_pgio_header *hdr)
4953 struct inode *inode = hdr->inode;
4955 trace_nfs4_write(hdr, task->tk_status);
4956 if (task->tk_status < 0) {
4957 struct nfs4_exception exception = {
4958 .inode = hdr->inode,
4959 .state = hdr->args.context->state,
4960 .stateid = &hdr->args.stateid,
4962 task->tk_status = nfs4_async_handle_exception(task,
4963 NFS_SERVER(inode), task->tk_status,
4965 if (exception.retry) {
4966 rpc_restart_call_prepare(task);
4970 if (task->tk_status >= 0) {
4971 renew_lease(NFS_SERVER(inode), hdr->timestamp);
4972 nfs_writeback_update_inode(hdr);
4977 static bool nfs4_write_stateid_changed(struct rpc_task *task,
4978 struct nfs_pgio_args *args)
4981 if (!nfs4_error_stateid_expired(task->tk_status) ||
4982 nfs4_stateid_is_current(&args->stateid,
4987 rpc_restart_call_prepare(task);
4991 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
4993 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
4995 if (nfs4_write_stateid_changed(task, &hdr->args))
4997 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4998 nfs4_write_done_cb(task, hdr);
5002 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
5004 /* Don't request attributes for pNFS or O_DIRECT writes */
5005 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
5007 /* Otherwise, request attributes if and only if we don't hold
5010 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
5013 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
5014 struct rpc_message *msg,
5015 struct rpc_clnt **clnt)
5017 struct nfs_server *server = NFS_SERVER(hdr->inode);
5019 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5020 hdr->args.bitmask = NULL;
5021 hdr->res.fattr = NULL;
5023 hdr->args.bitmask = server->cache_consistency_bitmask;
5025 if (!hdr->pgio_done_cb)
5026 hdr->pgio_done_cb = nfs4_write_done_cb;
5027 hdr->res.server = server;
5028 hdr->timestamp = jiffies;
5030 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
5031 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1, 0);
5032 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
5035 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5037 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
5038 &data->args.seq_args,
5043 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
5045 struct inode *inode = data->inode;
5047 trace_nfs4_commit(data, task->tk_status);
5048 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5049 NULL, NULL) == -EAGAIN) {
5050 rpc_restart_call_prepare(task);
5056 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
5058 if (!nfs4_sequence_done(task, &data->res.seq_res))
5060 return data->commit_done_cb(task, data);
5063 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5064 struct rpc_clnt **clnt)
5066 struct nfs_server *server = NFS_SERVER(data->inode);
5068 if (data->commit_done_cb == NULL)
5069 data->commit_done_cb = nfs4_commit_done_cb;
5070 data->res.server = server;
5071 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
5072 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
5073 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
5076 struct nfs4_renewdata {
5077 struct nfs_client *client;
5078 unsigned long timestamp;
5082 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5083 * standalone procedure for queueing an asynchronous RENEW.
5085 static void nfs4_renew_release(void *calldata)
5087 struct nfs4_renewdata *data = calldata;
5088 struct nfs_client *clp = data->client;
5090 if (refcount_read(&clp->cl_count) > 1)
5091 nfs4_schedule_state_renewal(clp);
5092 nfs_put_client(clp);
5096 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
5098 struct nfs4_renewdata *data = calldata;
5099 struct nfs_client *clp = data->client;
5100 unsigned long timestamp = data->timestamp;
5102 trace_nfs4_renew_async(clp, task->tk_status);
5103 switch (task->tk_status) {
5106 case -NFS4ERR_LEASE_MOVED:
5107 nfs4_schedule_lease_moved_recovery(clp);
5110 /* Unless we're shutting down, schedule state recovery! */
5111 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5113 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
5114 nfs4_schedule_lease_recovery(clp);
5117 nfs4_schedule_path_down_recovery(clp);
5119 do_renew_lease(clp, timestamp);
5122 static const struct rpc_call_ops nfs4_renew_ops = {
5123 .rpc_call_done = nfs4_renew_done,
5124 .rpc_release = nfs4_renew_release,
5127 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
5129 struct rpc_message msg = {
5130 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5134 struct nfs4_renewdata *data;
5136 if (renew_flags == 0)
5138 if (!refcount_inc_not_zero(&clp->cl_count))
5140 data = kmalloc(sizeof(*data), GFP_NOFS);
5142 nfs_put_client(clp);
5146 data->timestamp = jiffies;
5147 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
5148 &nfs4_renew_ops, data);
5151 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
5153 struct rpc_message msg = {
5154 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5158 unsigned long now = jiffies;
5161 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5164 do_renew_lease(clp, now);
5168 static inline int nfs4_server_supports_acls(struct nfs_server *server)
5170 return server->caps & NFS_CAP_ACLS;
5173 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5174 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
5177 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
5179 static int buf_to_pages_noslab(const void *buf, size_t buflen,
5180 struct page **pages)
5182 struct page *newpage, **spages;
5188 len = min_t(size_t, PAGE_SIZE, buflen);
5189 newpage = alloc_page(GFP_KERNEL);
5191 if (newpage == NULL)
5193 memcpy(page_address(newpage), buf, len);
5198 } while (buflen != 0);
5204 __free_page(spages[rc-1]);
5208 struct nfs4_cached_acl {
5214 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
5216 struct nfs_inode *nfsi = NFS_I(inode);
5218 spin_lock(&inode->i_lock);
5219 kfree(nfsi->nfs4_acl);
5220 nfsi->nfs4_acl = acl;
5221 spin_unlock(&inode->i_lock);
5224 static void nfs4_zap_acl_attr(struct inode *inode)
5226 nfs4_set_cached_acl(inode, NULL);
5229 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5231 struct nfs_inode *nfsi = NFS_I(inode);
5232 struct nfs4_cached_acl *acl;
5235 spin_lock(&inode->i_lock);
5236 acl = nfsi->nfs4_acl;
5239 if (buf == NULL) /* user is just asking for length */
5241 if (acl->cached == 0)
5243 ret = -ERANGE; /* see getxattr(2) man page */
5244 if (acl->len > buflen)
5246 memcpy(buf, acl->data, acl->len);
5250 spin_unlock(&inode->i_lock);
5254 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
5256 struct nfs4_cached_acl *acl;
5257 size_t buflen = sizeof(*acl) + acl_len;
5259 if (buflen <= PAGE_SIZE) {
5260 acl = kmalloc(buflen, GFP_KERNEL);
5264 _copy_from_pages(acl->data, pages, pgbase, acl_len);
5266 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5273 nfs4_set_cached_acl(inode, acl);
5277 * The getxattr API returns the required buffer length when called with a
5278 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5279 * the required buf. On a NULL buf, we send a page of data to the server
5280 * guessing that the ACL request can be serviced by a page. If so, we cache
5281 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5282 * the cache. If not so, we throw away the page, and cache the required
5283 * length. The next getxattr call will then produce another round trip to
5284 * the server, this time with the input buf of the required size.
5286 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5288 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
5289 struct nfs_getaclargs args = {
5290 .fh = NFS_FH(inode),
5294 struct nfs_getaclres res = {
5297 struct rpc_message msg = {
5298 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5302 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
5303 int ret = -ENOMEM, i;
5305 if (npages > ARRAY_SIZE(pages))
5308 for (i = 0; i < npages; i++) {
5309 pages[i] = alloc_page(GFP_KERNEL);
5314 /* for decoding across pages */
5315 res.acl_scratch = alloc_page(GFP_KERNEL);
5316 if (!res.acl_scratch)
5319 args.acl_len = npages * PAGE_SIZE;
5321 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
5322 __func__, buf, buflen, npages, args.acl_len);
5323 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5324 &msg, &args.seq_args, &res.seq_res, 0);
5328 /* Handle the case where the passed-in buffer is too short */
5329 if (res.acl_flags & NFS4_ACL_TRUNC) {
5330 /* Did the user only issue a request for the acl length? */
5336 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
5338 if (res.acl_len > buflen) {
5342 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
5347 for (i = 0; i < npages; i++)
5349 __free_page(pages[i]);
5350 if (res.acl_scratch)
5351 __free_page(res.acl_scratch);
5355 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5357 struct nfs4_exception exception = { };
5360 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
5361 trace_nfs4_get_acl(inode, ret);
5364 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5365 } while (exception.retry);
5369 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5371 struct nfs_server *server = NFS_SERVER(inode);
5374 if (!nfs4_server_supports_acls(server))
5376 ret = nfs_revalidate_inode(server, inode);
5379 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5380 nfs_zap_acl_cache(inode);
5381 ret = nfs4_read_cached_acl(inode, buf, buflen);
5383 /* -ENOENT is returned if there is no ACL or if there is an ACL
5384 * but no cached acl data, just the acl length */
5386 return nfs4_get_acl_uncached(inode, buf, buflen);
5389 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5391 struct nfs_server *server = NFS_SERVER(inode);
5392 struct page *pages[NFS4ACL_MAXPAGES];
5393 struct nfs_setaclargs arg = {
5394 .fh = NFS_FH(inode),
5398 struct nfs_setaclres res;
5399 struct rpc_message msg = {
5400 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5404 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
5407 if (!nfs4_server_supports_acls(server))
5409 if (npages > ARRAY_SIZE(pages))
5411 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
5414 nfs4_inode_make_writeable(inode);
5415 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5418 * Free each page after tx, so the only ref left is
5419 * held by the network stack
5422 put_page(pages[i-1]);
5425 * Acl update can result in inode attribute update.
5426 * so mark the attribute cache invalid.
5428 spin_lock(&inode->i_lock);
5429 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
5430 | NFS_INO_INVALID_CTIME
5431 | NFS_INO_REVAL_FORCED;
5432 spin_unlock(&inode->i_lock);
5433 nfs_access_zap_cache(inode);
5434 nfs_zap_acl_cache(inode);
5438 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5440 struct nfs4_exception exception = { };
5443 err = __nfs4_proc_set_acl(inode, buf, buflen);
5444 trace_nfs4_set_acl(inode, err);
5445 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5447 } while (exception.retry);
5451 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
5452 static int _nfs4_get_security_label(struct inode *inode, void *buf,
5455 struct nfs_server *server = NFS_SERVER(inode);
5456 struct nfs_fattr fattr;
5457 struct nfs4_label label = {0, 0, buflen, buf};
5459 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5460 struct nfs4_getattr_arg arg = {
5461 .fh = NFS_FH(inode),
5464 struct nfs4_getattr_res res = {
5469 struct rpc_message msg = {
5470 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
5476 nfs_fattr_init(&fattr);
5478 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
5481 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5483 if (buflen < label.len)
5488 static int nfs4_get_security_label(struct inode *inode, void *buf,
5491 struct nfs4_exception exception = { };
5494 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5498 err = _nfs4_get_security_label(inode, buf, buflen);
5499 trace_nfs4_get_security_label(inode, err);
5500 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5502 } while (exception.retry);
5506 static int _nfs4_do_set_security_label(struct inode *inode,
5507 struct nfs4_label *ilabel,
5508 struct nfs_fattr *fattr,
5509 struct nfs4_label *olabel)
5512 struct iattr sattr = {0};
5513 struct nfs_server *server = NFS_SERVER(inode);
5514 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5515 struct nfs_setattrargs arg = {
5516 .fh = NFS_FH(inode),
5522 struct nfs_setattrres res = {
5527 struct rpc_message msg = {
5528 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5534 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
5536 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5538 dprintk("%s failed: %d\n", __func__, status);
5543 static int nfs4_do_set_security_label(struct inode *inode,
5544 struct nfs4_label *ilabel,
5545 struct nfs_fattr *fattr,
5546 struct nfs4_label *olabel)
5548 struct nfs4_exception exception = { };
5552 err = _nfs4_do_set_security_label(inode, ilabel,
5554 trace_nfs4_set_security_label(inode, err);
5555 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5557 } while (exception.retry);
5562 nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
5564 struct nfs4_label ilabel, *olabel = NULL;
5565 struct nfs_fattr fattr;
5566 struct rpc_cred *cred;
5569 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5572 nfs_fattr_init(&fattr);
5576 ilabel.label = (char *)buf;
5577 ilabel.len = buflen;
5579 cred = rpc_lookup_cred();
5581 return PTR_ERR(cred);
5583 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5584 if (IS_ERR(olabel)) {
5585 status = -PTR_ERR(olabel);
5589 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5591 nfs_setsecurity(inode, &fattr, olabel);
5593 nfs4_label_free(olabel);
5598 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5601 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5602 nfs4_verifier *bootverf)
5606 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5607 /* An impossible timestamp guarantees this value
5608 * will never match a generated boot time. */
5609 verf[0] = cpu_to_be32(U32_MAX);
5610 verf[1] = cpu_to_be32(U32_MAX);
5612 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
5613 u64 ns = ktime_to_ns(nn->boot_time);
5615 verf[0] = cpu_to_be32(ns >> 32);
5616 verf[1] = cpu_to_be32(ns);
5618 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5622 nfs4_init_nonuniform_client_string(struct nfs_client *clp)
5627 if (clp->cl_owner_id != NULL)
5632 strlen(clp->cl_rpcclient->cl_nodename) +
5634 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5637 if (nfs4_client_id_uniquifier[0] != '\0')
5638 len += strlen(nfs4_client_id_uniquifier) + 1;
5639 if (len > NFS4_OPAQUE_LIMIT + 1)
5643 * Since this string is allocated at mount time, and held until the
5644 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5645 * about a memory-reclaim deadlock.
5647 str = kmalloc(len, GFP_KERNEL);
5652 if (nfs4_client_id_uniquifier[0] != '\0')
5653 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
5654 clp->cl_rpcclient->cl_nodename,
5655 nfs4_client_id_uniquifier,
5656 rpc_peeraddr2str(clp->cl_rpcclient,
5659 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
5660 clp->cl_rpcclient->cl_nodename,
5661 rpc_peeraddr2str(clp->cl_rpcclient,
5665 clp->cl_owner_id = str;
5670 nfs4_init_uniquifier_client_string(struct nfs_client *clp)
5675 len = 10 + 10 + 1 + 10 + 1 +
5676 strlen(nfs4_client_id_uniquifier) + 1 +
5677 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5679 if (len > NFS4_OPAQUE_LIMIT + 1)
5683 * Since this string is allocated at mount time, and held until the
5684 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5685 * about a memory-reclaim deadlock.
5687 str = kmalloc(len, GFP_KERNEL);
5691 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
5692 clp->rpc_ops->version, clp->cl_minorversion,
5693 nfs4_client_id_uniquifier,
5694 clp->cl_rpcclient->cl_nodename);
5695 clp->cl_owner_id = str;
5700 nfs4_init_uniform_client_string(struct nfs_client *clp)
5705 if (clp->cl_owner_id != NULL)
5708 if (nfs4_client_id_uniquifier[0] != '\0')
5709 return nfs4_init_uniquifier_client_string(clp);
5711 len = 10 + 10 + 1 + 10 + 1 +
5712 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5714 if (len > NFS4_OPAQUE_LIMIT + 1)
5718 * Since this string is allocated at mount time, and held until the
5719 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5720 * about a memory-reclaim deadlock.
5722 str = kmalloc(len, GFP_KERNEL);
5726 scnprintf(str, len, "Linux NFSv%u.%u %s",
5727 clp->rpc_ops->version, clp->cl_minorversion,
5728 clp->cl_rpcclient->cl_nodename);
5729 clp->cl_owner_id = str;
5734 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5735 * services. Advertise one based on the address family of the
5739 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5741 if (strchr(clp->cl_ipaddr, ':') != NULL)
5742 return scnprintf(buf, len, "tcp6");
5744 return scnprintf(buf, len, "tcp");
5747 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5749 struct nfs4_setclientid *sc = calldata;
5751 if (task->tk_status == 0)
5752 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5755 static const struct rpc_call_ops nfs4_setclientid_ops = {
5756 .rpc_call_done = nfs4_setclientid_done,
5760 * nfs4_proc_setclientid - Negotiate client ID
5761 * @clp: state data structure
5762 * @program: RPC program for NFSv4 callback service
5763 * @port: IP port number for NFS4 callback service
5764 * @cred: RPC credential to use for this call
5765 * @res: where to place the result
5767 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5769 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5770 unsigned short port, struct rpc_cred *cred,
5771 struct nfs4_setclientid_res *res)
5773 nfs4_verifier sc_verifier;
5774 struct nfs4_setclientid setclientid = {
5775 .sc_verifier = &sc_verifier,
5779 struct rpc_message msg = {
5780 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5781 .rpc_argp = &setclientid,
5785 struct rpc_task *task;
5786 struct rpc_task_setup task_setup_data = {
5787 .rpc_client = clp->cl_rpcclient,
5788 .rpc_message = &msg,
5789 .callback_ops = &nfs4_setclientid_ops,
5790 .callback_data = &setclientid,
5791 .flags = RPC_TASK_TIMEOUT,
5795 /* nfs_client_id4 */
5796 nfs4_init_boot_verifier(clp, &sc_verifier);
5798 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5799 status = nfs4_init_uniform_client_string(clp);
5801 status = nfs4_init_nonuniform_client_string(clp);
5807 setclientid.sc_netid_len =
5808 nfs4_init_callback_netid(clp,
5809 setclientid.sc_netid,
5810 sizeof(setclientid.sc_netid));
5811 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
5812 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
5813 clp->cl_ipaddr, port >> 8, port & 255);
5815 dprintk("NFS call setclientid auth=%s, '%s'\n",
5816 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5818 task = rpc_run_task(&task_setup_data);
5820 status = PTR_ERR(task);
5823 status = task->tk_status;
5824 if (setclientid.sc_cred) {
5825 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5826 put_rpccred(setclientid.sc_cred);
5830 trace_nfs4_setclientid(clp, status);
5831 dprintk("NFS reply setclientid: %d\n", status);
5836 * nfs4_proc_setclientid_confirm - Confirm client ID
5837 * @clp: state data structure
5838 * @res: result of a previous SETCLIENTID
5839 * @cred: RPC credential to use for this call
5841 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5843 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
5844 struct nfs4_setclientid_res *arg,
5845 struct rpc_cred *cred)
5847 struct rpc_message msg = {
5848 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
5854 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5855 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5857 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5858 trace_nfs4_setclientid_confirm(clp, status);
5859 dprintk("NFS reply setclientid_confirm: %d\n", status);
5863 struct nfs4_delegreturndata {
5864 struct nfs4_delegreturnargs args;
5865 struct nfs4_delegreturnres res;
5867 nfs4_stateid stateid;
5868 unsigned long timestamp;
5870 struct nfs4_layoutreturn_args arg;
5871 struct nfs4_layoutreturn_res res;
5872 struct nfs4_xdr_opaque_data ld_private;
5876 struct nfs_fattr fattr;
5878 struct inode *inode;
5881 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5883 struct nfs4_delegreturndata *data = calldata;
5884 struct nfs4_exception exception = {
5885 .inode = data->inode,
5886 .stateid = &data->stateid,
5889 if (!nfs4_sequence_done(task, &data->res.seq_res))
5892 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
5894 /* Handle Layoutreturn errors */
5895 if (data->args.lr_args && task->tk_status != 0) {
5896 switch(data->res.lr_ret) {
5898 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5901 data->args.lr_args = NULL;
5902 data->res.lr_res = NULL;
5904 case -NFS4ERR_OLD_STATEID:
5905 if (nfs4_refresh_layout_stateid(&data->args.lr_args->stateid,
5909 case -NFS4ERR_ADMIN_REVOKED:
5910 case -NFS4ERR_DELEG_REVOKED:
5911 case -NFS4ERR_EXPIRED:
5912 case -NFS4ERR_BAD_STATEID:
5913 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
5914 case -NFS4ERR_WRONG_CRED:
5915 data->args.lr_args = NULL;
5916 data->res.lr_res = NULL;
5921 switch (task->tk_status) {
5923 renew_lease(data->res.server, data->timestamp);
5925 case -NFS4ERR_ADMIN_REVOKED:
5926 case -NFS4ERR_DELEG_REVOKED:
5927 case -NFS4ERR_EXPIRED:
5928 nfs4_free_revoked_stateid(data->res.server,
5930 task->tk_msg.rpc_cred);
5932 case -NFS4ERR_BAD_STATEID:
5933 case -NFS4ERR_STALE_STATEID:
5934 task->tk_status = 0;
5936 case -NFS4ERR_OLD_STATEID:
5937 if (nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
5939 task->tk_status = 0;
5941 case -NFS4ERR_ACCESS:
5942 if (data->args.bitmask) {
5943 data->args.bitmask = NULL;
5944 data->res.fattr = NULL;
5949 task->tk_status = nfs4_async_handle_exception(task,
5950 data->res.server, task->tk_status,
5952 if (exception.retry)
5955 data->rpc_status = task->tk_status;
5958 data->res.lr_ret = 0;
5960 task->tk_status = 0;
5961 rpc_restart_call_prepare(task);
5964 static void nfs4_delegreturn_release(void *calldata)
5966 struct nfs4_delegreturndata *data = calldata;
5967 struct inode *inode = data->inode;
5971 pnfs_roc_release(&data->lr.arg, &data->lr.res,
5973 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5974 nfs_iput_and_deactive(inode);
5979 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5981 struct nfs4_delegreturndata *d_data;
5982 struct pnfs_layout_hdr *lo;
5984 d_data = (struct nfs4_delegreturndata *)data;
5986 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
5989 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
5990 if (lo && !pnfs_layout_is_valid(lo)) {
5991 d_data->args.lr_args = NULL;
5992 d_data->res.lr_res = NULL;
5995 nfs4_setup_sequence(d_data->res.server->nfs_client,
5996 &d_data->args.seq_args,
5997 &d_data->res.seq_res,
6001 static const struct rpc_call_ops nfs4_delegreturn_ops = {
6002 .rpc_call_prepare = nfs4_delegreturn_prepare,
6003 .rpc_call_done = nfs4_delegreturn_done,
6004 .rpc_release = nfs4_delegreturn_release,
6007 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
6009 struct nfs4_delegreturndata *data;
6010 struct nfs_server *server = NFS_SERVER(inode);
6011 struct rpc_task *task;
6012 struct rpc_message msg = {
6013 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6016 struct rpc_task_setup task_setup_data = {
6017 .rpc_client = server->client,
6018 .rpc_message = &msg,
6019 .callback_ops = &nfs4_delegreturn_ops,
6020 .flags = RPC_TASK_ASYNC,
6024 data = kzalloc(sizeof(*data), GFP_NOFS);
6027 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
6029 nfs4_state_protect(server->nfs_client,
6030 NFS_SP4_MACH_CRED_CLEANUP,
6031 &task_setup_data.rpc_client, &msg);
6033 data->args.fhandle = &data->fh;
6034 data->args.stateid = &data->stateid;
6035 data->args.bitmask = server->cache_consistency_bitmask;
6036 nfs_copy_fh(&data->fh, NFS_FH(inode));
6037 nfs4_stateid_copy(&data->stateid, stateid);
6038 data->res.fattr = &data->fattr;
6039 data->res.server = server;
6040 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
6041 data->lr.arg.ld_private = &data->lr.ld_private;
6042 nfs_fattr_init(data->res.fattr);
6043 data->timestamp = jiffies;
6044 data->rpc_status = 0;
6045 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
6046 data->inode = nfs_igrab_and_active(inode);
6049 data->args.lr_args = &data->lr.arg;
6050 data->res.lr_res = &data->lr.res;
6052 } else if (data->lr.roc) {
6053 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
6054 data->lr.roc = false;
6057 task_setup_data.callback_data = data;
6058 msg.rpc_argp = &data->args;
6059 msg.rpc_resp = &data->res;
6060 task = rpc_run_task(&task_setup_data);
6062 return PTR_ERR(task);
6065 status = rpc_wait_for_completion_task(task);
6068 status = data->rpc_status;
6074 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
6076 struct nfs_server *server = NFS_SERVER(inode);
6077 struct nfs4_exception exception = { };
6080 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
6081 trace_nfs4_delegreturn(inode, stateid, err);
6083 case -NFS4ERR_STALE_STATEID:
6084 case -NFS4ERR_EXPIRED:
6088 err = nfs4_handle_exception(server, err, &exception);
6089 } while (exception.retry);
6093 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6095 struct inode *inode = state->inode;
6096 struct nfs_server *server = NFS_SERVER(inode);
6097 struct nfs_client *clp = server->nfs_client;
6098 struct nfs_lockt_args arg = {
6099 .fh = NFS_FH(inode),
6102 struct nfs_lockt_res res = {
6105 struct rpc_message msg = {
6106 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
6109 .rpc_cred = state->owner->so_cred,
6111 struct nfs4_lock_state *lsp;
6114 arg.lock_owner.clientid = clp->cl_clientid;
6115 status = nfs4_set_lock_state(state, request);
6118 lsp = request->fl_u.nfs4_fl.owner;
6119 arg.lock_owner.id = lsp->ls_seqid.owner_id;
6120 arg.lock_owner.s_dev = server->s_dev;
6121 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6124 request->fl_type = F_UNLCK;
6126 case -NFS4ERR_DENIED:
6129 request->fl_ops->fl_release_private(request);
6130 request->fl_ops = NULL;
6135 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6137 struct nfs4_exception exception = { };
6141 err = _nfs4_proc_getlk(state, cmd, request);
6142 trace_nfs4_get_lock(request, state, cmd, err);
6143 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
6145 } while (exception.retry);
6149 struct nfs4_unlockdata {
6150 struct nfs_locku_args arg;
6151 struct nfs_locku_res res;
6152 struct nfs4_lock_state *lsp;
6153 struct nfs_open_context *ctx;
6154 struct nfs_lock_context *l_ctx;
6155 struct file_lock fl;
6156 struct nfs_server *server;
6157 unsigned long timestamp;
6160 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6161 struct nfs_open_context *ctx,
6162 struct nfs4_lock_state *lsp,
6163 struct nfs_seqid *seqid)
6165 struct nfs4_unlockdata *p;
6166 struct inode *inode = lsp->ls_state->inode;
6168 p = kzalloc(sizeof(*p), GFP_NOFS);
6171 p->arg.fh = NFS_FH(inode);
6173 p->arg.seqid = seqid;
6174 p->res.seqid = seqid;
6176 refcount_inc(&lsp->ls_count);
6177 /* Ensure we don't close file until we're done freeing locks! */
6178 p->ctx = get_nfs_open_context(ctx);
6179 p->l_ctx = nfs_get_lock_context(ctx);
6180 memcpy(&p->fl, fl, sizeof(p->fl));
6181 p->server = NFS_SERVER(inode);
6185 static void nfs4_locku_release_calldata(void *data)
6187 struct nfs4_unlockdata *calldata = data;
6188 nfs_free_seqid(calldata->arg.seqid);
6189 nfs4_put_lock_state(calldata->lsp);
6190 nfs_put_lock_context(calldata->l_ctx);
6191 put_nfs_open_context(calldata->ctx);
6195 static void nfs4_locku_done(struct rpc_task *task, void *data)
6197 struct nfs4_unlockdata *calldata = data;
6198 struct nfs4_exception exception = {
6199 .inode = calldata->lsp->ls_state->inode,
6200 .stateid = &calldata->arg.stateid,
6203 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6205 switch (task->tk_status) {
6207 renew_lease(calldata->server, calldata->timestamp);
6208 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
6209 if (nfs4_update_lock_stateid(calldata->lsp,
6210 &calldata->res.stateid))
6212 case -NFS4ERR_ADMIN_REVOKED:
6213 case -NFS4ERR_EXPIRED:
6214 nfs4_free_revoked_stateid(calldata->server,
6215 &calldata->arg.stateid,
6216 task->tk_msg.rpc_cred);
6217 case -NFS4ERR_BAD_STATEID:
6218 case -NFS4ERR_OLD_STATEID:
6219 case -NFS4ERR_STALE_STATEID:
6220 if (!nfs4_stateid_match(&calldata->arg.stateid,
6221 &calldata->lsp->ls_stateid))
6222 rpc_restart_call_prepare(task);
6225 task->tk_status = nfs4_async_handle_exception(task,
6226 calldata->server, task->tk_status,
6228 if (exception.retry)
6229 rpc_restart_call_prepare(task);
6231 nfs_release_seqid(calldata->arg.seqid);
6234 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
6236 struct nfs4_unlockdata *calldata = data;
6238 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6239 nfs_async_iocounter_wait(task, calldata->l_ctx))
6242 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
6244 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
6245 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
6246 /* Note: exit _without_ running nfs4_locku_done */
6249 calldata->timestamp = jiffies;
6250 if (nfs4_setup_sequence(calldata->server->nfs_client,
6251 &calldata->arg.seq_args,
6252 &calldata->res.seq_res,
6254 nfs_release_seqid(calldata->arg.seqid);
6257 task->tk_action = NULL;
6259 nfs4_sequence_done(task, &calldata->res.seq_res);
6262 static const struct rpc_call_ops nfs4_locku_ops = {
6263 .rpc_call_prepare = nfs4_locku_prepare,
6264 .rpc_call_done = nfs4_locku_done,
6265 .rpc_release = nfs4_locku_release_calldata,
6268 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6269 struct nfs_open_context *ctx,
6270 struct nfs4_lock_state *lsp,
6271 struct nfs_seqid *seqid)
6273 struct nfs4_unlockdata *data;
6274 struct rpc_message msg = {
6275 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6276 .rpc_cred = ctx->cred,
6278 struct rpc_task_setup task_setup_data = {
6279 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
6280 .rpc_message = &msg,
6281 .callback_ops = &nfs4_locku_ops,
6282 .workqueue = nfsiod_workqueue,
6283 .flags = RPC_TASK_ASYNC,
6286 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6287 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6289 /* Ensure this is an unlock - when canceling a lock, the
6290 * canceled lock is passed in, and it won't be an unlock.
6292 fl->fl_type = F_UNLCK;
6293 if (fl->fl_flags & FL_CLOSE)
6294 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
6296 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6298 nfs_free_seqid(seqid);
6299 return ERR_PTR(-ENOMEM);
6302 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
6303 msg.rpc_argp = &data->arg;
6304 msg.rpc_resp = &data->res;
6305 task_setup_data.callback_data = data;
6306 return rpc_run_task(&task_setup_data);
6309 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6311 struct inode *inode = state->inode;
6312 struct nfs4_state_owner *sp = state->owner;
6313 struct nfs_inode *nfsi = NFS_I(inode);
6314 struct nfs_seqid *seqid;
6315 struct nfs4_lock_state *lsp;
6316 struct rpc_task *task;
6317 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6319 unsigned char fl_flags = request->fl_flags;
6321 status = nfs4_set_lock_state(state, request);
6322 /* Unlock _before_ we do the RPC call */
6323 request->fl_flags |= FL_EXISTS;
6324 /* Exclude nfs_delegation_claim_locks() */
6325 mutex_lock(&sp->so_delegreturn_mutex);
6326 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
6327 down_read(&nfsi->rwsem);
6328 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
6329 up_read(&nfsi->rwsem);
6330 mutex_unlock(&sp->so_delegreturn_mutex);
6333 up_read(&nfsi->rwsem);
6334 mutex_unlock(&sp->so_delegreturn_mutex);
6337 /* Is this a delegated lock? */
6338 lsp = request->fl_u.nfs4_fl.owner;
6339 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6341 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6342 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
6346 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
6347 status = PTR_ERR(task);
6350 status = rpc_wait_for_completion_task(task);
6353 request->fl_flags = fl_flags;
6354 trace_nfs4_unlock(request, state, F_SETLK, status);
6358 struct nfs4_lockdata {
6359 struct nfs_lock_args arg;
6360 struct nfs_lock_res res;
6361 struct nfs4_lock_state *lsp;
6362 struct nfs_open_context *ctx;
6363 struct file_lock fl;
6364 unsigned long timestamp;
6367 struct nfs_server *server;
6370 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
6371 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6374 struct nfs4_lockdata *p;
6375 struct inode *inode = lsp->ls_state->inode;
6376 struct nfs_server *server = NFS_SERVER(inode);
6377 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6379 p = kzalloc(sizeof(*p), gfp_mask);
6383 p->arg.fh = NFS_FH(inode);
6385 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
6386 if (IS_ERR(p->arg.open_seqid))
6388 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6389 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
6390 if (IS_ERR(p->arg.lock_seqid))
6391 goto out_free_seqid;
6392 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
6393 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
6394 p->arg.lock_owner.s_dev = server->s_dev;
6395 p->res.lock_seqid = p->arg.lock_seqid;
6398 refcount_inc(&lsp->ls_count);
6399 p->ctx = get_nfs_open_context(ctx);
6400 memcpy(&p->fl, fl, sizeof(p->fl));
6403 nfs_free_seqid(p->arg.open_seqid);
6409 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6411 struct nfs4_lockdata *data = calldata;
6412 struct nfs4_state *state = data->lsp->ls_state;
6414 dprintk("%s: begin!\n", __func__);
6415 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
6417 /* Do we need to do an open_to_lock_owner? */
6418 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
6419 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
6420 goto out_release_lock_seqid;
6422 nfs4_stateid_copy(&data->arg.open_stateid,
6423 &state->open_stateid);
6424 data->arg.new_lock_owner = 1;
6425 data->res.open_seqid = data->arg.open_seqid;
6427 data->arg.new_lock_owner = 0;
6428 nfs4_stateid_copy(&data->arg.lock_stateid,
6429 &data->lsp->ls_stateid);
6431 if (!nfs4_valid_open_stateid(state)) {
6432 data->rpc_status = -EBADF;
6433 task->tk_action = NULL;
6434 goto out_release_open_seqid;
6436 data->timestamp = jiffies;
6437 if (nfs4_setup_sequence(data->server->nfs_client,
6438 &data->arg.seq_args,
6442 out_release_open_seqid:
6443 nfs_release_seqid(data->arg.open_seqid);
6444 out_release_lock_seqid:
6445 nfs_release_seqid(data->arg.lock_seqid);
6447 nfs4_sequence_done(task, &data->res.seq_res);
6448 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
6451 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6453 struct nfs4_lockdata *data = calldata;
6454 struct nfs4_lock_state *lsp = data->lsp;
6456 dprintk("%s: begin!\n", __func__);
6458 if (!nfs4_sequence_done(task, &data->res.seq_res))
6461 data->rpc_status = task->tk_status;
6462 switch (task->tk_status) {
6464 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
6466 if (data->arg.new_lock && !data->cancelled) {
6467 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
6468 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
6472 if (data->arg.new_lock_owner != 0) {
6473 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6474 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6475 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6477 } else if (nfs4_update_lock_stateid(lsp, &data->res.stateid))
6481 case -NFS4ERR_BAD_STATEID:
6482 case -NFS4ERR_OLD_STATEID:
6483 case -NFS4ERR_STALE_STATEID:
6484 case -NFS4ERR_EXPIRED:
6485 if (data->arg.new_lock_owner != 0) {
6486 if (nfs4_stateid_match(&data->arg.open_stateid,
6487 &lsp->ls_state->open_stateid))
6489 } else if (nfs4_stateid_match(&data->arg.lock_stateid,
6493 if (!data->cancelled)
6494 rpc_restart_call_prepare(task);
6496 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
6499 static void nfs4_lock_release(void *calldata)
6501 struct nfs4_lockdata *data = calldata;
6503 dprintk("%s: begin!\n", __func__);
6504 nfs_free_seqid(data->arg.open_seqid);
6505 if (data->cancelled) {
6506 struct rpc_task *task;
6507 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6508 data->arg.lock_seqid);
6510 rpc_put_task_async(task);
6511 dprintk("%s: cancelling lock!\n", __func__);
6513 nfs_free_seqid(data->arg.lock_seqid);
6514 nfs4_put_lock_state(data->lsp);
6515 put_nfs_open_context(data->ctx);
6517 dprintk("%s: done!\n", __func__);
6520 static const struct rpc_call_ops nfs4_lock_ops = {
6521 .rpc_call_prepare = nfs4_lock_prepare,
6522 .rpc_call_done = nfs4_lock_done,
6523 .rpc_release = nfs4_lock_release,
6526 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6529 case -NFS4ERR_ADMIN_REVOKED:
6530 case -NFS4ERR_EXPIRED:
6531 case -NFS4ERR_BAD_STATEID:
6532 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6533 if (new_lock_owner != 0 ||
6534 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
6535 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
6537 case -NFS4ERR_STALE_STATEID:
6538 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6539 nfs4_schedule_lease_recovery(server->nfs_client);
6543 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
6545 struct nfs4_lockdata *data;
6546 struct rpc_task *task;
6547 struct rpc_message msg = {
6548 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6549 .rpc_cred = state->owner->so_cred,
6551 struct rpc_task_setup task_setup_data = {
6552 .rpc_client = NFS_CLIENT(state->inode),
6553 .rpc_message = &msg,
6554 .callback_ops = &nfs4_lock_ops,
6555 .workqueue = nfsiod_workqueue,
6556 .flags = RPC_TASK_ASYNC,
6560 dprintk("%s: begin!\n", __func__);
6561 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
6562 fl->fl_u.nfs4_fl.owner,
6563 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
6567 data->arg.block = 1;
6568 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
6569 recovery_type > NFS_LOCK_NEW);
6570 msg.rpc_argp = &data->arg;
6571 msg.rpc_resp = &data->res;
6572 task_setup_data.callback_data = data;
6573 if (recovery_type > NFS_LOCK_NEW) {
6574 if (recovery_type == NFS_LOCK_RECLAIM)
6575 data->arg.reclaim = NFS_LOCK_RECLAIM;
6577 data->arg.new_lock = 1;
6578 task = rpc_run_task(&task_setup_data);
6580 return PTR_ERR(task);
6581 ret = rpc_wait_for_completion_task(task);
6583 ret = data->rpc_status;
6585 nfs4_handle_setlk_error(data->server, data->lsp,
6586 data->arg.new_lock_owner, ret);
6588 data->cancelled = true;
6590 dprintk("%s: done, ret = %d!\n", __func__, ret);
6591 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
6595 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6597 struct nfs_server *server = NFS_SERVER(state->inode);
6598 struct nfs4_exception exception = {
6599 .inode = state->inode,
6604 /* Cache the lock if possible... */
6605 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6607 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
6608 if (err != -NFS4ERR_DELAY)
6610 nfs4_handle_exception(server, err, &exception);
6611 } while (exception.retry);
6615 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6617 struct nfs_server *server = NFS_SERVER(state->inode);
6618 struct nfs4_exception exception = {
6619 .inode = state->inode,
6623 err = nfs4_set_lock_state(state, request);
6626 if (!recover_lost_locks) {
6627 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6631 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6633 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
6637 case -NFS4ERR_GRACE:
6638 case -NFS4ERR_DELAY:
6639 nfs4_handle_exception(server, err, &exception);
6642 } while (exception.retry);
6647 #if defined(CONFIG_NFS_V4_1)
6648 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6650 struct nfs4_lock_state *lsp;
6653 status = nfs4_set_lock_state(state, request);
6656 lsp = request->fl_u.nfs4_fl.owner;
6657 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6658 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6660 return nfs4_lock_expired(state, request);
6664 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6666 struct nfs_inode *nfsi = NFS_I(state->inode);
6667 struct nfs4_state_owner *sp = state->owner;
6668 unsigned char fl_flags = request->fl_flags;
6671 request->fl_flags |= FL_ACCESS;
6672 status = locks_lock_inode_wait(state->inode, request);
6675 mutex_lock(&sp->so_delegreturn_mutex);
6676 down_read(&nfsi->rwsem);
6677 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
6678 /* Yes: cache locks! */
6679 /* ...but avoid races with delegation recall... */
6680 request->fl_flags = fl_flags & ~FL_SLEEP;
6681 status = locks_lock_inode_wait(state->inode, request);
6682 up_read(&nfsi->rwsem);
6683 mutex_unlock(&sp->so_delegreturn_mutex);
6686 up_read(&nfsi->rwsem);
6687 mutex_unlock(&sp->so_delegreturn_mutex);
6688 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
6690 request->fl_flags = fl_flags;
6694 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6696 struct nfs4_exception exception = {
6698 .inode = state->inode,
6703 err = _nfs4_proc_setlk(state, cmd, request);
6704 if (err == -NFS4ERR_DENIED)
6706 err = nfs4_handle_exception(NFS_SERVER(state->inode),
6708 } while (exception.retry);
6712 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6713 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6716 nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6717 struct file_lock *request)
6719 int status = -ERESTARTSYS;
6720 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6722 while(!signalled()) {
6723 status = nfs4_proc_setlk(state, cmd, request);
6724 if ((status != -EAGAIN) || IS_SETLK(cmd))
6726 freezable_schedule_timeout_interruptible(timeout);
6728 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6729 status = -ERESTARTSYS;
6734 #ifdef CONFIG_NFS_V4_1
6735 struct nfs4_lock_waiter {
6736 struct task_struct *task;
6737 struct inode *inode;
6738 struct nfs_lowner *owner;
6743 nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
6746 struct nfs4_lock_waiter *waiter = wait->private;
6748 /* NULL key means to wake up everyone */
6750 struct cb_notify_lock_args *cbnl = key;
6751 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6752 *wowner = waiter->owner;
6754 /* Only wake if the callback was for the same owner. */
6755 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
6758 /* Make sure it's for the right inode */
6759 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6762 waiter->notified = true;
6765 /* override "private" so we can use default_wake_function */
6766 wait->private = waiter->task;
6767 ret = autoremove_wake_function(wait, mode, flags, key);
6768 wait->private = waiter;
6773 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6775 int status = -ERESTARTSYS;
6776 unsigned long flags;
6777 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6778 struct nfs_server *server = NFS_SERVER(state->inode);
6779 struct nfs_client *clp = server->nfs_client;
6780 wait_queue_head_t *q = &clp->cl_lock_waitq;
6781 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6782 .id = lsp->ls_seqid.owner_id,
6783 .s_dev = server->s_dev };
6784 struct nfs4_lock_waiter waiter = { .task = current,
6785 .inode = state->inode,
6787 .notified = false };
6788 wait_queue_entry_t wait;
6790 /* Don't bother with waitqueue if we don't expect a callback */
6791 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6792 return nfs4_retry_setlk_simple(state, cmd, request);
6795 wait.private = &waiter;
6796 wait.func = nfs4_wake_lock_waiter;
6797 add_wait_queue(q, &wait);
6799 while(!signalled()) {
6800 waiter.notified = false;
6801 status = nfs4_proc_setlk(state, cmd, request);
6802 if ((status != -EAGAIN) || IS_SETLK(cmd))
6805 status = -ERESTARTSYS;
6806 spin_lock_irqsave(&q->lock, flags);
6807 if (waiter.notified) {
6808 spin_unlock_irqrestore(&q->lock, flags);
6811 set_current_state(TASK_INTERRUPTIBLE);
6812 spin_unlock_irqrestore(&q->lock, flags);
6814 freezable_schedule_timeout(NFS4_LOCK_MAXTIMEOUT);
6817 finish_wait(q, &wait);
6820 #else /* !CONFIG_NFS_V4_1 */
6822 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6824 return nfs4_retry_setlk_simple(state, cmd, request);
6829 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6831 struct nfs_open_context *ctx;
6832 struct nfs4_state *state;
6835 /* verify open state */
6836 ctx = nfs_file_open_context(filp);
6839 if (IS_GETLK(cmd)) {
6841 return nfs4_proc_getlk(state, F_GETLK, request);
6845 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6848 if (request->fl_type == F_UNLCK) {
6850 return nfs4_proc_unlck(state, cmd, request);
6857 if ((request->fl_flags & FL_POSIX) &&
6858 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6862 * Don't rely on the VFS having checked the file open mode,
6863 * since it won't do this for flock() locks.
6865 switch (request->fl_type) {
6867 if (!(filp->f_mode & FMODE_READ))
6871 if (!(filp->f_mode & FMODE_WRITE))
6875 status = nfs4_set_lock_state(state, request);
6879 return nfs4_retry_setlk(state, cmd, request);
6882 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
6884 struct nfs_server *server = NFS_SERVER(state->inode);
6887 err = nfs4_set_lock_state(state, fl);
6890 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
6891 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
6894 struct nfs_release_lockowner_data {
6895 struct nfs4_lock_state *lsp;
6896 struct nfs_server *server;
6897 struct nfs_release_lockowner_args args;
6898 struct nfs_release_lockowner_res res;
6899 unsigned long timestamp;
6902 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6904 struct nfs_release_lockowner_data *data = calldata;
6905 struct nfs_server *server = data->server;
6906 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
6907 &data->res.seq_res, task);
6908 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6909 data->timestamp = jiffies;
6912 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6914 struct nfs_release_lockowner_data *data = calldata;
6915 struct nfs_server *server = data->server;
6917 nfs40_sequence_done(task, &data->res.seq_res);
6919 switch (task->tk_status) {
6921 renew_lease(server, data->timestamp);
6923 case -NFS4ERR_STALE_CLIENTID:
6924 case -NFS4ERR_EXPIRED:
6925 nfs4_schedule_lease_recovery(server->nfs_client);
6927 case -NFS4ERR_LEASE_MOVED:
6928 case -NFS4ERR_DELAY:
6929 if (nfs4_async_handle_error(task, server,
6930 NULL, NULL) == -EAGAIN)
6931 rpc_restart_call_prepare(task);
6935 static void nfs4_release_lockowner_release(void *calldata)
6937 struct nfs_release_lockowner_data *data = calldata;
6938 nfs4_free_lock_state(data->server, data->lsp);
6942 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
6943 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6944 .rpc_call_done = nfs4_release_lockowner_done,
6945 .rpc_release = nfs4_release_lockowner_release,
6949 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
6951 struct nfs_release_lockowner_data *data;
6952 struct rpc_message msg = {
6953 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6956 if (server->nfs_client->cl_mvops->minor_version != 0)
6959 data = kmalloc(sizeof(*data), GFP_NOFS);
6963 data->server = server;
6964 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6965 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6966 data->args.lock_owner.s_dev = server->s_dev;
6968 msg.rpc_argp = &data->args;
6969 msg.rpc_resp = &data->res;
6970 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
6971 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
6974 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6976 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
6977 struct dentry *unused, struct inode *inode,
6978 const char *key, const void *buf,
6979 size_t buflen, int flags)
6981 return nfs4_proc_set_acl(inode, buf, buflen);
6984 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
6985 struct dentry *unused, struct inode *inode,
6986 const char *key, void *buf, size_t buflen)
6988 return nfs4_proc_get_acl(inode, buf, buflen);
6991 static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
6993 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
6996 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
6998 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
6999 struct dentry *unused, struct inode *inode,
7000 const char *key, const void *buf,
7001 size_t buflen, int flags)
7003 if (security_ismaclabel(key))
7004 return nfs4_set_security_label(inode, buf, buflen);
7009 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
7010 struct dentry *unused, struct inode *inode,
7011 const char *key, void *buf, size_t buflen)
7013 if (security_ismaclabel(key))
7014 return nfs4_get_security_label(inode, buf, buflen);
7019 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7023 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7024 len = security_inode_listsecurity(inode, list, list_len);
7025 if (list_len && len > list_len)
7031 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7032 .prefix = XATTR_SECURITY_PREFIX,
7033 .get = nfs4_xattr_get_nfs4_label,
7034 .set = nfs4_xattr_set_nfs4_label,
7040 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7048 * nfs_fhget will use either the mounted_on_fileid or the fileid
7050 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7052 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7053 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7054 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
7055 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
7058 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
7059 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
7060 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7064 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7065 const struct qstr *name,
7066 struct nfs4_fs_locations *fs_locations,
7069 struct nfs_server *server = NFS_SERVER(dir);
7071 struct nfs4_fs_locations_arg args = {
7072 .dir_fh = NFS_FH(dir),
7077 struct nfs4_fs_locations_res res = {
7078 .fs_locations = fs_locations,
7080 struct rpc_message msg = {
7081 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7087 dprintk("%s: start\n", __func__);
7089 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7090 bitmask[1] = nfs4_fattr_bitmap[1];
7092 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7093 * is not supported */
7094 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
7095 bitmask[0] &= ~FATTR4_WORD0_FILEID;
7097 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
7099 nfs_fattr_init(&fs_locations->fattr);
7100 fs_locations->server = server;
7101 fs_locations->nlocations = 0;
7102 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
7103 dprintk("%s: returned status = %d\n", __func__, status);
7107 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7108 const struct qstr *name,
7109 struct nfs4_fs_locations *fs_locations,
7112 struct nfs4_exception exception = { };
7115 err = _nfs4_proc_fs_locations(client, dir, name,
7116 fs_locations, page);
7117 trace_nfs4_get_fs_locations(dir, name, err);
7118 err = nfs4_handle_exception(NFS_SERVER(dir), err,
7120 } while (exception.retry);
7125 * This operation also signals the server that this client is
7126 * performing migration recovery. The server can stop returning
7127 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7128 * appended to this compound to identify the client ID which is
7129 * performing recovery.
7131 static int _nfs40_proc_get_locations(struct inode *inode,
7132 struct nfs4_fs_locations *locations,
7133 struct page *page, struct rpc_cred *cred)
7135 struct nfs_server *server = NFS_SERVER(inode);
7136 struct rpc_clnt *clnt = server->client;
7138 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7140 struct nfs4_fs_locations_arg args = {
7141 .clientid = server->nfs_client->cl_clientid,
7142 .fh = NFS_FH(inode),
7145 .migration = 1, /* skip LOOKUP */
7146 .renew = 1, /* append RENEW */
7148 struct nfs4_fs_locations_res res = {
7149 .fs_locations = locations,
7153 struct rpc_message msg = {
7154 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7159 unsigned long now = jiffies;
7162 nfs_fattr_init(&locations->fattr);
7163 locations->server = server;
7164 locations->nlocations = 0;
7166 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7167 status = nfs4_call_sync_sequence(clnt, server, &msg,
7168 &args.seq_args, &res.seq_res);
7172 renew_lease(server, now);
7176 #ifdef CONFIG_NFS_V4_1
7179 * This operation also signals the server that this client is
7180 * performing migration recovery. The server can stop asserting
7181 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7182 * performing this operation is identified in the SEQUENCE
7183 * operation in this compound.
7185 * When the client supports GETATTR(fs_locations_info), it can
7186 * be plumbed in here.
7188 static int _nfs41_proc_get_locations(struct inode *inode,
7189 struct nfs4_fs_locations *locations,
7190 struct page *page, struct rpc_cred *cred)
7192 struct nfs_server *server = NFS_SERVER(inode);
7193 struct rpc_clnt *clnt = server->client;
7195 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7197 struct nfs4_fs_locations_arg args = {
7198 .fh = NFS_FH(inode),
7201 .migration = 1, /* skip LOOKUP */
7203 struct nfs4_fs_locations_res res = {
7204 .fs_locations = locations,
7207 struct rpc_message msg = {
7208 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7215 nfs_fattr_init(&locations->fattr);
7216 locations->server = server;
7217 locations->nlocations = 0;
7219 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7220 status = nfs4_call_sync_sequence(clnt, server, &msg,
7221 &args.seq_args, &res.seq_res);
7222 if (status == NFS4_OK &&
7223 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7224 status = -NFS4ERR_LEASE_MOVED;
7228 #endif /* CONFIG_NFS_V4_1 */
7231 * nfs4_proc_get_locations - discover locations for a migrated FSID
7232 * @inode: inode on FSID that is migrating
7233 * @locations: result of query
7235 * @cred: credential to use for this operation
7237 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7238 * operation failed, or a negative errno if a local error occurred.
7240 * On success, "locations" is filled in, but if the server has
7241 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7244 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7245 * from this client that require migration recovery.
7247 int nfs4_proc_get_locations(struct inode *inode,
7248 struct nfs4_fs_locations *locations,
7249 struct page *page, struct rpc_cred *cred)
7251 struct nfs_server *server = NFS_SERVER(inode);
7252 struct nfs_client *clp = server->nfs_client;
7253 const struct nfs4_mig_recovery_ops *ops =
7254 clp->cl_mvops->mig_recovery_ops;
7255 struct nfs4_exception exception = { };
7258 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7259 (unsigned long long)server->fsid.major,
7260 (unsigned long long)server->fsid.minor,
7262 nfs_display_fhandle(NFS_FH(inode), __func__);
7265 status = ops->get_locations(inode, locations, page, cred);
7266 if (status != -NFS4ERR_DELAY)
7268 nfs4_handle_exception(server, status, &exception);
7269 } while (exception.retry);
7274 * This operation also signals the server that this client is
7275 * performing "lease moved" recovery. The server can stop
7276 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7277 * is appended to this compound to identify the client ID which is
7278 * performing recovery.
7280 static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7282 struct nfs_server *server = NFS_SERVER(inode);
7283 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7284 struct rpc_clnt *clnt = server->client;
7285 struct nfs4_fsid_present_arg args = {
7286 .fh = NFS_FH(inode),
7287 .clientid = clp->cl_clientid,
7288 .renew = 1, /* append RENEW */
7290 struct nfs4_fsid_present_res res = {
7293 struct rpc_message msg = {
7294 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7299 unsigned long now = jiffies;
7302 res.fh = nfs_alloc_fhandle();
7306 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7307 status = nfs4_call_sync_sequence(clnt, server, &msg,
7308 &args.seq_args, &res.seq_res);
7309 nfs_free_fhandle(res.fh);
7313 do_renew_lease(clp, now);
7317 #ifdef CONFIG_NFS_V4_1
7320 * This operation also signals the server that this client is
7321 * performing "lease moved" recovery. The server can stop asserting
7322 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7323 * this operation is identified in the SEQUENCE operation in this
7326 static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7328 struct nfs_server *server = NFS_SERVER(inode);
7329 struct rpc_clnt *clnt = server->client;
7330 struct nfs4_fsid_present_arg args = {
7331 .fh = NFS_FH(inode),
7333 struct nfs4_fsid_present_res res = {
7335 struct rpc_message msg = {
7336 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7343 res.fh = nfs_alloc_fhandle();
7347 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7348 status = nfs4_call_sync_sequence(clnt, server, &msg,
7349 &args.seq_args, &res.seq_res);
7350 nfs_free_fhandle(res.fh);
7351 if (status == NFS4_OK &&
7352 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7353 status = -NFS4ERR_LEASE_MOVED;
7357 #endif /* CONFIG_NFS_V4_1 */
7360 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7361 * @inode: inode on FSID to check
7362 * @cred: credential to use for this operation
7364 * Server indicates whether the FSID is present, moved, or not
7365 * recognized. This operation is necessary to clear a LEASE_MOVED
7366 * condition for this client ID.
7368 * Returns NFS4_OK if the FSID is present on this server,
7369 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7370 * NFS4ERR code if some error occurred on the server, or a
7371 * negative errno if a local failure occurred.
7373 int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7375 struct nfs_server *server = NFS_SERVER(inode);
7376 struct nfs_client *clp = server->nfs_client;
7377 const struct nfs4_mig_recovery_ops *ops =
7378 clp->cl_mvops->mig_recovery_ops;
7379 struct nfs4_exception exception = { };
7382 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7383 (unsigned long long)server->fsid.major,
7384 (unsigned long long)server->fsid.minor,
7386 nfs_display_fhandle(NFS_FH(inode), __func__);
7389 status = ops->fsid_present(inode, cred);
7390 if (status != -NFS4ERR_DELAY)
7392 nfs4_handle_exception(server, status, &exception);
7393 } while (exception.retry);
7398 * If 'use_integrity' is true and the state managment nfs_client
7399 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7400 * and the machine credential as per RFC3530bis and RFC5661 Security
7401 * Considerations sections. Otherwise, just use the user cred with the
7402 * filesystem's rpc_client.
7404 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
7407 struct nfs4_secinfo_arg args = {
7408 .dir_fh = NFS_FH(dir),
7411 struct nfs4_secinfo_res res = {
7414 struct rpc_message msg = {
7415 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7419 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
7420 struct rpc_cred *cred = NULL;
7422 if (use_integrity) {
7423 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
7424 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7425 msg.rpc_cred = cred;
7428 dprintk("NFS call secinfo %s\n", name->name);
7430 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7431 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7433 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7435 dprintk("NFS reply secinfo: %d\n", status);
7443 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7444 struct nfs4_secinfo_flavors *flavors)
7446 struct nfs4_exception exception = { };
7449 err = -NFS4ERR_WRONGSEC;
7451 /* try to use integrity protection with machine cred */
7452 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7453 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7456 * if unable to use integrity protection, or SECINFO with
7457 * integrity protection returns NFS4ERR_WRONGSEC (which is
7458 * disallowed by spec, but exists in deployed servers) use
7459 * the current filesystem's rpc_client and the user cred.
7461 if (err == -NFS4ERR_WRONGSEC)
7462 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7464 trace_nfs4_secinfo(dir, name, err);
7465 err = nfs4_handle_exception(NFS_SERVER(dir), err,
7467 } while (exception.retry);
7471 #ifdef CONFIG_NFS_V4_1
7473 * Check the exchange flags returned by the server for invalid flags, having
7474 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7477 static int nfs4_check_cl_exchange_flags(u32 flags)
7479 if (flags & ~EXCHGID4_FLAG_MASK_R)
7481 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7482 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7484 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7488 return -NFS4ERR_INVAL;
7492 nfs41_same_server_scope(struct nfs41_server_scope *a,
7493 struct nfs41_server_scope *b)
7495 if (a->server_scope_sz != b->server_scope_sz)
7497 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
7501 nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7505 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7506 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7510 * nfs4_proc_bind_one_conn_to_session()
7512 * The 4.1 client currently uses the same TCP connection for the
7513 * fore and backchannel.
7516 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7517 struct rpc_xprt *xprt,
7518 struct nfs_client *clp,
7519 struct rpc_cred *cred)
7522 struct nfs41_bind_conn_to_session_args args = {
7524 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7526 struct nfs41_bind_conn_to_session_res res;
7527 struct rpc_message msg = {
7529 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
7534 struct rpc_task_setup task_setup_data = {
7537 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
7538 .rpc_message = &msg,
7539 .flags = RPC_TASK_TIMEOUT,
7541 struct rpc_task *task;
7543 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7544 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7545 args.dir = NFS4_CDFC4_FORE;
7547 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7548 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7549 args.dir = NFS4_CDFC4_FORE;
7551 task = rpc_run_task(&task_setup_data);
7552 if (!IS_ERR(task)) {
7553 status = task->tk_status;
7556 status = PTR_ERR(task);
7557 trace_nfs4_bind_conn_to_session(clp, status);
7559 if (memcmp(res.sessionid.data,
7560 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7561 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7564 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
7565 dprintk("NFS: %s: Unexpected direction from server\n",
7569 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
7570 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7579 struct rpc_bind_conn_calldata {
7580 struct nfs_client *clp;
7581 struct rpc_cred *cred;
7585 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7586 struct rpc_xprt *xprt,
7589 struct rpc_bind_conn_calldata *p = calldata;
7591 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7594 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7596 struct rpc_bind_conn_calldata data = {
7600 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7601 nfs4_proc_bind_conn_to_session_callback, &data);
7605 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7606 * and operations we'd like to see to enable certain features in the allow map
7608 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7609 .how = SP4_MACH_CRED,
7610 .enforce.u.words = {
7611 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7612 1 << (OP_EXCHANGE_ID - 32) |
7613 1 << (OP_CREATE_SESSION - 32) |
7614 1 << (OP_DESTROY_SESSION - 32) |
7615 1 << (OP_DESTROY_CLIENTID - 32)
7618 [0] = 1 << (OP_CLOSE) |
7619 1 << (OP_OPEN_DOWNGRADE) |
7621 1 << (OP_DELEGRETURN) |
7623 [1] = 1 << (OP_SECINFO - 32) |
7624 1 << (OP_SECINFO_NO_NAME - 32) |
7625 1 << (OP_LAYOUTRETURN - 32) |
7626 1 << (OP_TEST_STATEID - 32) |
7627 1 << (OP_FREE_STATEID - 32) |
7628 1 << (OP_WRITE - 32)
7633 * Select the state protection mode for client `clp' given the server results
7634 * from exchange_id in `sp'.
7636 * Returns 0 on success, negative errno otherwise.
7638 static int nfs4_sp4_select_mode(struct nfs_client *clp,
7639 struct nfs41_state_protection *sp)
7641 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7642 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7643 1 << (OP_EXCHANGE_ID - 32) |
7644 1 << (OP_CREATE_SESSION - 32) |
7645 1 << (OP_DESTROY_SESSION - 32) |
7646 1 << (OP_DESTROY_CLIENTID - 32)
7648 unsigned long flags = 0;
7652 if (sp->how == SP4_MACH_CRED) {
7653 /* Print state protect result */
7654 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7655 for (i = 0; i <= LAST_NFS4_OP; i++) {
7656 if (test_bit(i, sp->enforce.u.longs))
7657 dfprintk(MOUNT, " enforce op %d\n", i);
7658 if (test_bit(i, sp->allow.u.longs))
7659 dfprintk(MOUNT, " allow op %d\n", i);
7662 /* make sure nothing is on enforce list that isn't supported */
7663 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7664 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7665 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7672 * Minimal mode - state operations are allowed to use machine
7673 * credential. Note this already happens by default, so the
7674 * client doesn't have to do anything more than the negotiation.
7676 * NOTE: we don't care if EXCHANGE_ID is in the list -
7677 * we're already using the machine cred for exchange_id
7678 * and will never use a different cred.
7680 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7681 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7682 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7683 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7684 dfprintk(MOUNT, "sp4_mach_cred:\n");
7685 dfprintk(MOUNT, " minimal mode enabled\n");
7686 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
7688 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7693 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
7694 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7695 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
7696 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7697 dfprintk(MOUNT, " cleanup mode enabled\n");
7698 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
7701 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7702 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7703 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
7706 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7707 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7708 dfprintk(MOUNT, " secinfo mode enabled\n");
7709 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
7712 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7713 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7714 dfprintk(MOUNT, " stateid mode enabled\n");
7715 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
7718 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7719 dfprintk(MOUNT, " write mode enabled\n");
7720 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
7723 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7724 dfprintk(MOUNT, " commit mode enabled\n");
7725 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
7729 clp->cl_sp4_flags = flags;
7733 struct nfs41_exchange_id_data {
7734 struct nfs41_exchange_id_res res;
7735 struct nfs41_exchange_id_args args;
7738 static void nfs4_exchange_id_release(void *data)
7740 struct nfs41_exchange_id_data *cdata =
7741 (struct nfs41_exchange_id_data *)data;
7743 nfs_put_client(cdata->args.client);
7744 kfree(cdata->res.impl_id);
7745 kfree(cdata->res.server_scope);
7746 kfree(cdata->res.server_owner);
7750 static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7751 .rpc_release = nfs4_exchange_id_release,
7755 * _nfs4_proc_exchange_id()
7757 * Wrapper for EXCHANGE_ID operation.
7759 static struct rpc_task *
7760 nfs4_run_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
7761 u32 sp4_how, struct rpc_xprt *xprt)
7763 struct rpc_message msg = {
7764 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
7767 struct rpc_task_setup task_setup_data = {
7768 .rpc_client = clp->cl_rpcclient,
7769 .callback_ops = &nfs4_exchange_id_call_ops,
7770 .rpc_message = &msg,
7771 .flags = RPC_TASK_TIMEOUT,
7773 struct nfs41_exchange_id_data *calldata;
7776 if (!refcount_inc_not_zero(&clp->cl_count))
7777 return ERR_PTR(-EIO);
7780 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7784 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
7786 status = nfs4_init_uniform_client_string(clp);
7790 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7793 if (unlikely(calldata->res.server_owner == NULL))
7796 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
7798 if (unlikely(calldata->res.server_scope == NULL))
7799 goto out_server_owner;
7801 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7802 if (unlikely(calldata->res.impl_id == NULL))
7803 goto out_server_scope;
7807 calldata->args.state_protect.how = SP4_NONE;
7811 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
7821 task_setup_data.rpc_xprt = xprt;
7822 task_setup_data.flags |= RPC_TASK_SOFTCONN;
7823 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
7824 sizeof(calldata->args.verifier.data));
7826 calldata->args.client = clp;
7827 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7828 EXCHGID4_FLAG_BIND_PRINC_STATEID;
7829 #ifdef CONFIG_NFS_V4_1_MIGRATION
7830 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
7832 msg.rpc_argp = &calldata->args;
7833 msg.rpc_resp = &calldata->res;
7834 task_setup_data.callback_data = calldata;
7836 return rpc_run_task(&task_setup_data);
7839 kfree(calldata->res.impl_id);
7841 kfree(calldata->res.server_scope);
7843 kfree(calldata->res.server_owner);
7847 nfs_put_client(clp);
7848 return ERR_PTR(status);
7852 * _nfs4_proc_exchange_id()
7854 * Wrapper for EXCHANGE_ID operation.
7856 static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
7859 struct rpc_task *task;
7860 struct nfs41_exchange_id_args *argp;
7861 struct nfs41_exchange_id_res *resp;
7864 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
7866 return PTR_ERR(task);
7868 argp = task->tk_msg.rpc_argp;
7869 resp = task->tk_msg.rpc_resp;
7870 status = task->tk_status;
7874 status = nfs4_check_cl_exchange_flags(resp->flags);
7878 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
7882 clp->cl_clientid = resp->clientid;
7883 clp->cl_exchange_flags = resp->flags;
7884 clp->cl_seqid = resp->seqid;
7885 /* Client ID is not confirmed */
7886 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
7887 clear_bit(NFS4_SESSION_ESTABLISHED,
7888 &clp->cl_session->session_state);
7890 if (clp->cl_serverscope != NULL &&
7891 !nfs41_same_server_scope(clp->cl_serverscope,
7892 resp->server_scope)) {
7893 dprintk("%s: server_scope mismatch detected\n",
7895 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
7898 swap(clp->cl_serverowner, resp->server_owner);
7899 swap(clp->cl_serverscope, resp->server_scope);
7900 swap(clp->cl_implid, resp->impl_id);
7902 /* Save the EXCHANGE_ID verifier session trunk tests */
7903 memcpy(clp->cl_confirm.data, argp->verifier.data,
7904 sizeof(clp->cl_confirm.data));
7906 trace_nfs4_exchange_id(clp, status);
7912 * nfs4_proc_exchange_id()
7914 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7916 * Since the clientid has expired, all compounds using sessions
7917 * associated with the stale clientid will be returning
7918 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7919 * be in some phase of session reset.
7921 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7923 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7925 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7928 /* try SP4_MACH_CRED if krb5i/p */
7929 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7930 authflavor == RPC_AUTH_GSS_KRB5P) {
7931 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
7937 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
7941 * nfs4_test_session_trunk
7943 * This is an add_xprt_test() test function called from
7944 * rpc_clnt_setup_test_and_add_xprt.
7946 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7947 * and is dereferrenced in nfs4_exchange_id_release
7949 * Upon success, add the new transport to the rpc_clnt
7951 * @clnt: struct rpc_clnt to get new transport
7952 * @xprt: the rpc_xprt to test
7953 * @data: call data for _nfs4_proc_exchange_id.
7955 int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7958 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
7959 struct rpc_task *task;
7964 dprintk("--> %s try %s\n", __func__,
7965 xprt->address_strings[RPC_DISPLAY_ADDR]);
7967 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7969 /* Test connection for session trunking. Async exchange_id call */
7970 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7972 return PTR_ERR(task);
7974 status = task->tk_status;
7976 status = nfs4_detect_session_trunking(adata->clp,
7977 task->tk_msg.rpc_resp, xprt);
7982 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7984 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7985 struct rpc_cred *cred)
7987 struct rpc_message msg = {
7988 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7994 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7995 trace_nfs4_destroy_clientid(clp, status);
7997 dprintk("NFS: Got error %d from the server %s on "
7998 "DESTROY_CLIENTID.", status, clp->cl_hostname);
8002 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
8003 struct rpc_cred *cred)
8008 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8009 ret = _nfs4_proc_destroy_clientid(clp, cred);
8011 case -NFS4ERR_DELAY:
8012 case -NFS4ERR_CLIENTID_BUSY:
8022 int nfs4_destroy_clientid(struct nfs_client *clp)
8024 struct rpc_cred *cred;
8027 if (clp->cl_mvops->minor_version < 1)
8029 if (clp->cl_exchange_flags == 0)
8031 if (clp->cl_preserve_clid)
8033 cred = nfs4_get_clid_cred(clp);
8034 ret = nfs4_proc_destroy_clientid(clp, cred);
8039 case -NFS4ERR_STALE_CLIENTID:
8040 clp->cl_exchange_flags = 0;
8046 struct nfs4_get_lease_time_data {
8047 struct nfs4_get_lease_time_args *args;
8048 struct nfs4_get_lease_time_res *res;
8049 struct nfs_client *clp;
8052 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
8055 struct nfs4_get_lease_time_data *data =
8056 (struct nfs4_get_lease_time_data *)calldata;
8058 dprintk("--> %s\n", __func__);
8059 /* just setup sequence, do not trigger session recovery
8060 since we're invoked within one */
8061 nfs4_setup_sequence(data->clp,
8062 &data->args->la_seq_args,
8063 &data->res->lr_seq_res,
8065 dprintk("<-- %s\n", __func__);
8069 * Called from nfs4_state_manager thread for session setup, so don't recover
8070 * from sequence operation or clientid errors.
8072 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8074 struct nfs4_get_lease_time_data *data =
8075 (struct nfs4_get_lease_time_data *)calldata;
8077 dprintk("--> %s\n", __func__);
8078 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
8080 switch (task->tk_status) {
8081 case -NFS4ERR_DELAY:
8082 case -NFS4ERR_GRACE:
8083 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
8084 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8085 task->tk_status = 0;
8087 case -NFS4ERR_RETRY_UNCACHED_REP:
8088 rpc_restart_call_prepare(task);
8091 dprintk("<-- %s\n", __func__);
8094 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
8095 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8096 .rpc_call_done = nfs4_get_lease_time_done,
8099 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8101 struct rpc_task *task;
8102 struct nfs4_get_lease_time_args args;
8103 struct nfs4_get_lease_time_res res = {
8104 .lr_fsinfo = fsinfo,
8106 struct nfs4_get_lease_time_data data = {
8111 struct rpc_message msg = {
8112 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8116 struct rpc_task_setup task_setup = {
8117 .rpc_client = clp->cl_rpcclient,
8118 .rpc_message = &msg,
8119 .callback_ops = &nfs4_get_lease_time_ops,
8120 .callback_data = &data,
8121 .flags = RPC_TASK_TIMEOUT,
8125 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
8126 task = rpc_run_task(&task_setup);
8129 return PTR_ERR(task);
8131 status = task->tk_status;
8137 * Initialize the values to be used by the client in CREATE_SESSION
8138 * If nfs4_init_session set the fore channel request and response sizes,
8141 * Set the back channel max_resp_sz_cached to zero to force the client to
8142 * always set csa_cachethis to FALSE because the current implementation
8143 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8145 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8146 struct rpc_clnt *clnt)
8148 unsigned int max_rqst_sz, max_resp_sz;
8149 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
8151 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8152 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8154 /* Fore channel attributes */
8155 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8156 args->fc_attrs.max_resp_sz = max_resp_sz;
8157 args->fc_attrs.max_ops = NFS4_MAX_OPS;
8158 args->fc_attrs.max_reqs = max_session_slots;
8160 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
8161 "max_ops=%u max_reqs=%u\n",
8163 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
8164 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
8166 /* Back channel attributes */
8167 args->bc_attrs.max_rqst_sz = max_bc_payload;
8168 args->bc_attrs.max_resp_sz = max_bc_payload;
8169 args->bc_attrs.max_resp_sz_cached = 0;
8170 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
8171 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
8173 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8174 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8176 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8177 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8178 args->bc_attrs.max_reqs);
8181 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8182 struct nfs41_create_session_res *res)
8184 struct nfs4_channel_attrs *sent = &args->fc_attrs;
8185 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
8187 if (rcvd->max_resp_sz > sent->max_resp_sz)
8190 * Our requested max_ops is the minimum we need; we're not
8191 * prepared to break up compounds into smaller pieces than that.
8192 * So, no point even trying to continue if the server won't
8195 if (rcvd->max_ops < sent->max_ops)
8197 if (rcvd->max_reqs == 0)
8199 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8200 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
8204 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8205 struct nfs41_create_session_res *res)
8207 struct nfs4_channel_attrs *sent = &args->bc_attrs;
8208 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
8210 if (!(res->flags & SESSION4_BACK_CHAN))
8212 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8214 if (rcvd->max_resp_sz < sent->max_resp_sz)
8216 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8218 if (rcvd->max_ops > sent->max_ops)
8220 if (rcvd->max_reqs > sent->max_reqs)
8226 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
8227 struct nfs41_create_session_res *res)
8231 ret = nfs4_verify_fore_channel_attrs(args, res);
8234 return nfs4_verify_back_channel_attrs(args, res);
8237 static void nfs4_update_session(struct nfs4_session *session,
8238 struct nfs41_create_session_res *res)
8240 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
8241 /* Mark client id and session as being confirmed */
8242 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8243 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
8244 session->flags = res->flags;
8245 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
8246 if (res->flags & SESSION4_BACK_CHAN)
8247 memcpy(&session->bc_attrs, &res->bc_attrs,
8248 sizeof(session->bc_attrs));
8251 static int _nfs4_proc_create_session(struct nfs_client *clp,
8252 struct rpc_cred *cred)
8254 struct nfs4_session *session = clp->cl_session;
8255 struct nfs41_create_session_args args = {
8257 .clientid = clp->cl_clientid,
8258 .seqid = clp->cl_seqid,
8259 .cb_program = NFS4_CALLBACK,
8261 struct nfs41_create_session_res res;
8263 struct rpc_message msg = {
8264 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8271 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
8272 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
8274 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
8275 trace_nfs4_create_session(clp, status);
8278 case -NFS4ERR_STALE_CLIENTID:
8279 case -NFS4ERR_DELAY:
8288 /* Verify the session's negotiated channel_attrs values */
8289 status = nfs4_verify_channel_attrs(&args, &res);
8290 /* Increment the clientid slot sequence id */
8293 nfs4_update_session(session, &res);
8300 * Issues a CREATE_SESSION operation to the server.
8301 * It is the responsibility of the caller to verify the session is
8302 * expired before calling this routine.
8304 int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
8308 struct nfs4_session *session = clp->cl_session;
8310 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8312 status = _nfs4_proc_create_session(clp, cred);
8316 /* Init or reset the session slot tables */
8317 status = nfs4_setup_session_slot_tables(session);
8318 dprintk("slot table setup returned %d\n", status);
8322 ptr = (unsigned *)&session->sess_id.data[0];
8323 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8324 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
8326 dprintk("<-- %s\n", __func__);
8331 * Issue the over-the-wire RPC DESTROY_SESSION.
8332 * The caller must serialize access to this routine.
8334 int nfs4_proc_destroy_session(struct nfs4_session *session,
8335 struct rpc_cred *cred)
8337 struct rpc_message msg = {
8338 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8339 .rpc_argp = session,
8344 dprintk("--> nfs4_proc_destroy_session\n");
8346 /* session is still being setup */
8347 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8350 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
8351 trace_nfs4_destroy_session(session->clp, status);
8354 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
8355 "Session has been destroyed regardless...\n", status);
8357 dprintk("<-- nfs4_proc_destroy_session\n");
8362 * Renew the cl_session lease.
8364 struct nfs4_sequence_data {
8365 struct nfs_client *clp;
8366 struct nfs4_sequence_args args;
8367 struct nfs4_sequence_res res;
8370 static void nfs41_sequence_release(void *data)
8372 struct nfs4_sequence_data *calldata = data;
8373 struct nfs_client *clp = calldata->clp;
8375 if (refcount_read(&clp->cl_count) > 1)
8376 nfs4_schedule_state_renewal(clp);
8377 nfs_put_client(clp);
8381 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8383 switch(task->tk_status) {
8384 case -NFS4ERR_DELAY:
8385 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8388 nfs4_schedule_lease_recovery(clp);
8393 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
8395 struct nfs4_sequence_data *calldata = data;
8396 struct nfs_client *clp = calldata->clp;
8398 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8401 trace_nfs4_sequence(clp, task->tk_status);
8402 if (task->tk_status < 0) {
8403 dprintk("%s ERROR %d\n", __func__, task->tk_status);
8404 if (refcount_read(&clp->cl_count) == 1)
8407 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8408 rpc_restart_call_prepare(task);
8412 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
8414 dprintk("<-- %s\n", __func__);
8417 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8419 struct nfs4_sequence_data *calldata = data;
8420 struct nfs_client *clp = calldata->clp;
8421 struct nfs4_sequence_args *args;
8422 struct nfs4_sequence_res *res;
8424 args = task->tk_msg.rpc_argp;
8425 res = task->tk_msg.rpc_resp;
8427 nfs4_setup_sequence(clp, args, res, task);
8430 static const struct rpc_call_ops nfs41_sequence_ops = {
8431 .rpc_call_done = nfs41_sequence_call_done,
8432 .rpc_call_prepare = nfs41_sequence_prepare,
8433 .rpc_release = nfs41_sequence_release,
8436 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8437 struct rpc_cred *cred,
8438 struct nfs4_slot *slot,
8441 struct nfs4_sequence_data *calldata;
8442 struct rpc_message msg = {
8443 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8446 struct rpc_task_setup task_setup_data = {
8447 .rpc_client = clp->cl_rpcclient,
8448 .rpc_message = &msg,
8449 .callback_ops = &nfs41_sequence_ops,
8450 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
8452 struct rpc_task *ret;
8454 ret = ERR_PTR(-EIO);
8455 if (!refcount_inc_not_zero(&clp->cl_count))
8458 ret = ERR_PTR(-ENOMEM);
8459 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8460 if (calldata == NULL)
8462 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
8463 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
8464 msg.rpc_argp = &calldata->args;
8465 msg.rpc_resp = &calldata->res;
8466 calldata->clp = clp;
8467 task_setup_data.callback_data = calldata;
8469 ret = rpc_run_task(&task_setup_data);
8474 nfs_put_client(clp);
8476 nfs41_release_slot(slot);
8480 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
8482 struct rpc_task *task;
8485 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
8487 task = _nfs41_proc_sequence(clp, cred, NULL, false);
8489 ret = PTR_ERR(task);
8491 rpc_put_task_async(task);
8492 dprintk("<-- %s status=%d\n", __func__, ret);
8496 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8498 struct rpc_task *task;
8501 task = _nfs41_proc_sequence(clp, cred, NULL, true);
8503 ret = PTR_ERR(task);
8506 ret = rpc_wait_for_completion_task(task);
8508 ret = task->tk_status;
8511 dprintk("<-- %s status=%d\n", __func__, ret);
8515 struct nfs4_reclaim_complete_data {
8516 struct nfs_client *clp;
8517 struct nfs41_reclaim_complete_args arg;
8518 struct nfs41_reclaim_complete_res res;
8521 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8523 struct nfs4_reclaim_complete_data *calldata = data;
8525 nfs4_setup_sequence(calldata->clp,
8526 &calldata->arg.seq_args,
8527 &calldata->res.seq_res,
8531 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8533 switch(task->tk_status) {
8535 wake_up_all(&clp->cl_lock_waitq);
8537 case -NFS4ERR_COMPLETE_ALREADY:
8538 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8540 case -NFS4ERR_DELAY:
8541 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8543 case -NFS4ERR_RETRY_UNCACHED_REP:
8545 case -NFS4ERR_BADSESSION:
8546 case -NFS4ERR_DEADSESSION:
8547 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8548 nfs4_schedule_session_recovery(clp->cl_session,
8552 nfs4_schedule_lease_recovery(clp);
8557 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8559 struct nfs4_reclaim_complete_data *calldata = data;
8560 struct nfs_client *clp = calldata->clp;
8561 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8563 dprintk("--> %s\n", __func__);
8564 if (!nfs41_sequence_done(task, res))
8567 trace_nfs4_reclaim_complete(clp, task->tk_status);
8568 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8569 rpc_restart_call_prepare(task);
8572 dprintk("<-- %s\n", __func__);
8575 static void nfs4_free_reclaim_complete_data(void *data)
8577 struct nfs4_reclaim_complete_data *calldata = data;
8582 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8583 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8584 .rpc_call_done = nfs4_reclaim_complete_done,
8585 .rpc_release = nfs4_free_reclaim_complete_data,
8589 * Issue a global reclaim complete.
8591 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8592 struct rpc_cred *cred)
8594 struct nfs4_reclaim_complete_data *calldata;
8595 struct rpc_task *task;
8596 struct rpc_message msg = {
8597 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
8600 struct rpc_task_setup task_setup_data = {
8601 .rpc_client = clp->cl_rpcclient,
8602 .rpc_message = &msg,
8603 .callback_ops = &nfs4_reclaim_complete_call_ops,
8604 .flags = RPC_TASK_ASYNC,
8606 int status = -ENOMEM;
8608 dprintk("--> %s\n", __func__);
8609 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8610 if (calldata == NULL)
8612 calldata->clp = clp;
8613 calldata->arg.one_fs = 0;
8615 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
8616 msg.rpc_argp = &calldata->arg;
8617 msg.rpc_resp = &calldata->res;
8618 task_setup_data.callback_data = calldata;
8619 task = rpc_run_task(&task_setup_data);
8621 status = PTR_ERR(task);
8624 status = rpc_wait_for_completion_task(task);
8626 status = task->tk_status;
8629 dprintk("<-- %s status=%d\n", __func__, status);
8634 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8636 struct nfs4_layoutget *lgp = calldata;
8637 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
8639 dprintk("--> %s\n", __func__);
8640 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
8641 &lgp->res.seq_res, task);
8642 dprintk("<-- %s\n", __func__);
8645 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8647 struct nfs4_layoutget *lgp = calldata;
8649 dprintk("--> %s\n", __func__);
8650 nfs41_sequence_process(task, &lgp->res.seq_res);
8651 dprintk("<-- %s\n", __func__);
8655 nfs4_layoutget_handle_exception(struct rpc_task *task,
8656 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8658 struct inode *inode = lgp->args.inode;
8659 struct nfs_server *server = NFS_SERVER(inode);
8660 struct pnfs_layout_hdr *lo;
8661 int nfs4err = task->tk_status;
8662 int err, status = 0;
8665 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
8667 nfs4_sequence_free_slot(&lgp->res.seq_res);
8674 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8675 * on the file. set tk_status to -ENODATA to tell upper layer to
8678 case -NFS4ERR_LAYOUTUNAVAILABLE:
8682 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8683 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8685 case -NFS4ERR_BADLAYOUT:
8686 status = -EOVERFLOW;
8689 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
8690 * (or clients) writing to the same RAID stripe except when
8691 * the minlength argument is 0 (see RFC5661 section 18.43.3).
8693 * Treat it like we would RECALLCONFLICT -- we retry for a little
8694 * while, and then eventually give up.
8696 case -NFS4ERR_LAYOUTTRYLATER:
8697 if (lgp->args.minlength == 0) {
8698 status = -EOVERFLOW;
8703 case -NFS4ERR_RECALLCONFLICT:
8704 status = -ERECALLCONFLICT;
8706 case -NFS4ERR_DELEG_REVOKED:
8707 case -NFS4ERR_ADMIN_REVOKED:
8708 case -NFS4ERR_EXPIRED:
8709 case -NFS4ERR_BAD_STATEID:
8710 exception->timeout = 0;
8711 spin_lock(&inode->i_lock);
8712 lo = NFS_I(inode)->layout;
8713 /* If the open stateid was bad, then recover it. */
8714 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8715 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
8716 spin_unlock(&inode->i_lock);
8717 exception->state = lgp->args.ctx->state;
8718 exception->stateid = &lgp->args.stateid;
8723 * Mark the bad layout state as invalid, then retry
8725 pnfs_mark_layout_stateid_invalid(lo, &head);
8726 spin_unlock(&inode->i_lock);
8727 nfs_commit_inode(inode, 0);
8728 pnfs_free_lseg_list(&head);
8733 err = nfs4_handle_exception(server, nfs4err, exception);
8735 if (exception->retry)
8741 dprintk("<-- %s\n", __func__);
8745 size_t max_response_pages(struct nfs_server *server)
8747 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8748 return nfs_page_array_len(0, max_resp_sz);
8751 static void nfs4_layoutget_release(void *calldata)
8753 struct nfs4_layoutget *lgp = calldata;
8755 dprintk("--> %s\n", __func__);
8756 nfs4_sequence_free_slot(&lgp->res.seq_res);
8757 pnfs_layoutget_free(lgp);
8758 dprintk("<-- %s\n", __func__);
8761 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8762 .rpc_call_prepare = nfs4_layoutget_prepare,
8763 .rpc_call_done = nfs4_layoutget_done,
8764 .rpc_release = nfs4_layoutget_release,
8767 struct pnfs_layout_segment *
8768 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
8770 struct inode *inode = lgp->args.inode;
8771 struct nfs_server *server = NFS_SERVER(inode);
8772 struct rpc_task *task;
8773 struct rpc_message msg = {
8774 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8775 .rpc_argp = &lgp->args,
8776 .rpc_resp = &lgp->res,
8777 .rpc_cred = lgp->cred,
8779 struct rpc_task_setup task_setup_data = {
8780 .rpc_client = server->client,
8781 .rpc_message = &msg,
8782 .callback_ops = &nfs4_layoutget_call_ops,
8783 .callback_data = lgp,
8784 .flags = RPC_TASK_ASYNC,
8786 struct pnfs_layout_segment *lseg = NULL;
8787 struct nfs4_exception exception = {
8789 .timeout = *timeout,
8793 dprintk("--> %s\n", __func__);
8795 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8796 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8798 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
8800 task = rpc_run_task(&task_setup_data);
8802 return ERR_CAST(task);
8803 status = rpc_wait_for_completion_task(task);
8807 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8808 if (task->tk_status < 0 || lgp->res.layoutp->len == 0) {
8809 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8810 *timeout = exception.timeout;
8812 lseg = pnfs_layout_process(lgp);
8814 trace_nfs4_layoutget(lgp->args.ctx,
8821 dprintk("<-- %s status=%d\n", __func__, status);
8823 return ERR_PTR(status);
8828 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8830 struct nfs4_layoutreturn *lrp = calldata;
8832 dprintk("--> %s\n", __func__);
8833 nfs4_setup_sequence(lrp->clp,
8834 &lrp->args.seq_args,
8837 if (!pnfs_layout_is_valid(lrp->args.layout))
8841 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8843 struct nfs4_layoutreturn *lrp = calldata;
8844 struct nfs_server *server;
8846 dprintk("--> %s\n", __func__);
8848 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
8851 server = NFS_SERVER(lrp->args.inode);
8852 switch (task->tk_status) {
8853 case -NFS4ERR_OLD_STATEID:
8854 if (nfs4_refresh_layout_stateid(&lrp->args.stateid,
8859 task->tk_status = 0;
8863 case -NFS4ERR_DELAY:
8864 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
8868 dprintk("<-- %s\n", __func__);
8871 task->tk_status = 0;
8872 nfs4_sequence_free_slot(&lrp->res.seq_res);
8873 rpc_restart_call_prepare(task);
8876 static void nfs4_layoutreturn_release(void *calldata)
8878 struct nfs4_layoutreturn *lrp = calldata;
8879 struct pnfs_layout_hdr *lo = lrp->args.layout;
8881 dprintk("--> %s\n", __func__);
8882 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
8883 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
8884 nfs4_sequence_free_slot(&lrp->res.seq_res);
8885 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
8886 lrp->ld_private.ops->free(&lrp->ld_private);
8887 pnfs_put_layout_hdr(lrp->args.layout);
8888 nfs_iput_and_deactive(lrp->inode);
8890 dprintk("<-- %s\n", __func__);
8893 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8894 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8895 .rpc_call_done = nfs4_layoutreturn_done,
8896 .rpc_release = nfs4_layoutreturn_release,
8899 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
8901 struct rpc_task *task;
8902 struct rpc_message msg = {
8903 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8904 .rpc_argp = &lrp->args,
8905 .rpc_resp = &lrp->res,
8906 .rpc_cred = lrp->cred,
8908 struct rpc_task_setup task_setup_data = {
8909 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
8910 .rpc_message = &msg,
8911 .callback_ops = &nfs4_layoutreturn_call_ops,
8912 .callback_data = lrp,
8916 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8917 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8918 &task_setup_data.rpc_client, &msg);
8920 dprintk("--> %s\n", __func__);
8922 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8924 nfs4_layoutreturn_release(lrp);
8927 task_setup_data.flags |= RPC_TASK_ASYNC;
8929 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1, 0);
8930 task = rpc_run_task(&task_setup_data);
8932 return PTR_ERR(task);
8934 status = task->tk_status;
8935 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
8936 dprintk("<-- %s status=%d\n", __func__, status);
8942 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
8943 struct pnfs_device *pdev,
8944 struct rpc_cred *cred)
8946 struct nfs4_getdeviceinfo_args args = {
8948 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8949 NOTIFY_DEVICEID4_DELETE,
8951 struct nfs4_getdeviceinfo_res res = {
8954 struct rpc_message msg = {
8955 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8962 dprintk("--> %s\n", __func__);
8963 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
8964 if (res.notification & ~args.notify_types)
8965 dprintk("%s: unsupported notification\n", __func__);
8966 if (res.notification != args.notify_types)
8969 dprintk("<-- %s status=%d\n", __func__, status);
8974 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8975 struct pnfs_device *pdev,
8976 struct rpc_cred *cred)
8978 struct nfs4_exception exception = { };
8982 err = nfs4_handle_exception(server,
8983 _nfs4_proc_getdeviceinfo(server, pdev, cred),
8985 } while (exception.retry);
8988 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8990 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8992 struct nfs4_layoutcommit_data *data = calldata;
8993 struct nfs_server *server = NFS_SERVER(data->args.inode);
8995 nfs4_setup_sequence(server->nfs_client,
8996 &data->args.seq_args,
9002 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9004 struct nfs4_layoutcommit_data *data = calldata;
9005 struct nfs_server *server = NFS_SERVER(data->args.inode);
9007 if (!nfs41_sequence_done(task, &data->res.seq_res))
9010 switch (task->tk_status) { /* Just ignore these failures */
9011 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9012 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9013 case -NFS4ERR_BADLAYOUT: /* no layout */
9014 case -NFS4ERR_GRACE: /* loca_recalim always false */
9015 task->tk_status = 0;
9019 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
9020 rpc_restart_call_prepare(task);
9026 static void nfs4_layoutcommit_release(void *calldata)
9028 struct nfs4_layoutcommit_data *data = calldata;
9030 pnfs_cleanup_layoutcommit(data);
9031 nfs_post_op_update_inode_force_wcc(data->args.inode,
9033 put_rpccred(data->cred);
9034 nfs_iput_and_deactive(data->inode);
9038 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9039 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9040 .rpc_call_done = nfs4_layoutcommit_done,
9041 .rpc_release = nfs4_layoutcommit_release,
9045 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
9047 struct rpc_message msg = {
9048 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9049 .rpc_argp = &data->args,
9050 .rpc_resp = &data->res,
9051 .rpc_cred = data->cred,
9053 struct rpc_task_setup task_setup_data = {
9054 .task = &data->task,
9055 .rpc_client = NFS_CLIENT(data->args.inode),
9056 .rpc_message = &msg,
9057 .callback_ops = &nfs4_layoutcommit_ops,
9058 .callback_data = data,
9060 struct rpc_task *task;
9063 dprintk("NFS: initiating layoutcommit call. sync %d "
9064 "lbw: %llu inode %lu\n", sync,
9065 data->args.lastbytewritten,
9066 data->args.inode->i_ino);
9069 data->inode = nfs_igrab_and_active(data->args.inode);
9070 if (data->inode == NULL) {
9071 nfs4_layoutcommit_release(data);
9074 task_setup_data.flags = RPC_TASK_ASYNC;
9076 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
9077 task = rpc_run_task(&task_setup_data);
9079 return PTR_ERR(task);
9081 status = task->tk_status;
9082 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
9083 dprintk("%s: status %d\n", __func__, status);
9089 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9090 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9093 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9094 struct nfs_fsinfo *info,
9095 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
9097 struct nfs41_secinfo_no_name_args args = {
9098 .style = SECINFO_STYLE_CURRENT_FH,
9100 struct nfs4_secinfo_res res = {
9103 struct rpc_message msg = {
9104 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9108 struct rpc_clnt *clnt = server->client;
9109 struct rpc_cred *cred = NULL;
9112 if (use_integrity) {
9113 clnt = server->nfs_client->cl_rpcclient;
9114 cred = nfs4_get_clid_cred(server->nfs_client);
9115 msg.rpc_cred = cred;
9118 dprintk("--> %s\n", __func__);
9119 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
9121 dprintk("<-- %s status=%d\n", __func__, status);
9130 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9131 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9133 struct nfs4_exception exception = { };
9136 /* first try using integrity protection */
9137 err = -NFS4ERR_WRONGSEC;
9139 /* try to use integrity protection with machine cred */
9140 if (_nfs4_is_integrity_protected(server->nfs_client))
9141 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9145 * if unable to use integrity protection, or SECINFO with
9146 * integrity protection returns NFS4ERR_WRONGSEC (which is
9147 * disallowed by spec, but exists in deployed servers) use
9148 * the current filesystem's rpc_client and the user cred.
9150 if (err == -NFS4ERR_WRONGSEC)
9151 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9156 case -NFS4ERR_WRONGSEC:
9160 err = nfs4_handle_exception(server, err, &exception);
9162 } while (exception.retry);
9168 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9169 struct nfs_fsinfo *info)
9173 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
9174 struct nfs4_secinfo_flavors *flavors;
9175 struct nfs4_secinfo4 *secinfo;
9178 page = alloc_page(GFP_KERNEL);
9184 flavors = page_address(page);
9185 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9188 * Fall back on "guess and check" method if
9189 * the server doesn't support SECINFO_NO_NAME
9191 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
9192 err = nfs4_find_root_sec(server, fhandle, info);
9198 for (i = 0; i < flavors->num_flavors; i++) {
9199 secinfo = &flavors->flavors[i];
9201 switch (secinfo->flavor) {
9205 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9206 &secinfo->flavor_info);
9209 flavor = RPC_AUTH_MAXFLAVOR;
9213 if (!nfs_auth_info_match(&server->auth_info, flavor))
9214 flavor = RPC_AUTH_MAXFLAVOR;
9216 if (flavor != RPC_AUTH_MAXFLAVOR) {
9217 err = nfs4_lookup_root_sec(server, fhandle,
9224 if (flavor == RPC_AUTH_MAXFLAVOR)
9235 static int _nfs41_test_stateid(struct nfs_server *server,
9236 nfs4_stateid *stateid,
9237 struct rpc_cred *cred)
9240 struct nfs41_test_stateid_args args = {
9243 struct nfs41_test_stateid_res res;
9244 struct rpc_message msg = {
9245 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9250 struct rpc_clnt *rpc_client = server->client;
9252 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9255 dprintk("NFS call test_stateid %p\n", stateid);
9256 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
9257 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
9258 &args.seq_args, &res.seq_res);
9259 if (status != NFS_OK) {
9260 dprintk("NFS reply test_stateid: failed, %d\n", status);
9263 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
9267 static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9268 int err, struct nfs4_exception *exception)
9270 exception->retry = 0;
9272 case -NFS4ERR_DELAY:
9273 case -NFS4ERR_RETRY_UNCACHED_REP:
9274 nfs4_handle_exception(server, err, exception);
9276 case -NFS4ERR_BADSESSION:
9277 case -NFS4ERR_BADSLOT:
9278 case -NFS4ERR_BAD_HIGH_SLOT:
9279 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9280 case -NFS4ERR_DEADSESSION:
9281 nfs4_do_handle_exception(server, err, exception);
9286 * nfs41_test_stateid - perform a TEST_STATEID operation
9288 * @server: server / transport on which to perform the operation
9289 * @stateid: state ID to test
9292 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9293 * Otherwise a negative NFS4ERR value is returned if the operation
9294 * failed or the state ID is not currently valid.
9296 static int nfs41_test_stateid(struct nfs_server *server,
9297 nfs4_stateid *stateid,
9298 struct rpc_cred *cred)
9300 struct nfs4_exception exception = { };
9303 err = _nfs41_test_stateid(server, stateid, cred);
9304 nfs4_handle_delay_or_session_error(server, err, &exception);
9305 } while (exception.retry);
9309 struct nfs_free_stateid_data {
9310 struct nfs_server *server;
9311 struct nfs41_free_stateid_args args;
9312 struct nfs41_free_stateid_res res;
9315 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9317 struct nfs_free_stateid_data *data = calldata;
9318 nfs4_setup_sequence(data->server->nfs_client,
9319 &data->args.seq_args,
9324 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9326 struct nfs_free_stateid_data *data = calldata;
9328 nfs41_sequence_done(task, &data->res.seq_res);
9330 switch (task->tk_status) {
9331 case -NFS4ERR_DELAY:
9332 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
9333 rpc_restart_call_prepare(task);
9337 static void nfs41_free_stateid_release(void *calldata)
9342 static const struct rpc_call_ops nfs41_free_stateid_ops = {
9343 .rpc_call_prepare = nfs41_free_stateid_prepare,
9344 .rpc_call_done = nfs41_free_stateid_done,
9345 .rpc_release = nfs41_free_stateid_release,
9349 * nfs41_free_stateid - perform a FREE_STATEID operation
9351 * @server: server / transport on which to perform the operation
9352 * @stateid: state ID to release
9354 * @is_recovery: set to true if this call needs to be privileged
9356 * Note: this function is always asynchronous.
9358 static int nfs41_free_stateid(struct nfs_server *server,
9359 const nfs4_stateid *stateid,
9360 struct rpc_cred *cred,
9363 struct rpc_message msg = {
9364 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
9367 struct rpc_task_setup task_setup = {
9368 .rpc_client = server->client,
9369 .rpc_message = &msg,
9370 .callback_ops = &nfs41_free_stateid_ops,
9371 .flags = RPC_TASK_ASYNC,
9373 struct nfs_free_stateid_data *data;
9374 struct rpc_task *task;
9376 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9377 &task_setup.rpc_client, &msg);
9379 dprintk("NFS call free_stateid %p\n", stateid);
9380 data = kmalloc(sizeof(*data), GFP_NOFS);
9383 data->server = server;
9384 nfs4_stateid_copy(&data->args.stateid, stateid);
9386 task_setup.callback_data = data;
9388 msg.rpc_argp = &data->args;
9389 msg.rpc_resp = &data->res;
9390 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
9391 task = rpc_run_task(&task_setup);
9393 return PTR_ERR(task);
9399 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
9401 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
9403 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
9404 nfs4_free_lock_state(server, lsp);
9407 static bool nfs41_match_stateid(const nfs4_stateid *s1,
9408 const nfs4_stateid *s2)
9410 if (s1->type != s2->type)
9413 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
9416 if (s1->seqid == s2->seqid)
9419 return s1->seqid == 0 || s2->seqid == 0;
9422 #endif /* CONFIG_NFS_V4_1 */
9424 static bool nfs4_match_stateid(const nfs4_stateid *s1,
9425 const nfs4_stateid *s2)
9427 return nfs4_stateid_match(s1, s2);
9431 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
9432 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9433 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9434 .recover_open = nfs4_open_reclaim,
9435 .recover_lock = nfs4_lock_reclaim,
9436 .establish_clid = nfs4_init_clientid,
9437 .detect_trunking = nfs40_discover_server_trunking,
9440 #if defined(CONFIG_NFS_V4_1)
9441 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
9442 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9443 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9444 .recover_open = nfs4_open_reclaim,
9445 .recover_lock = nfs4_lock_reclaim,
9446 .establish_clid = nfs41_init_clientid,
9447 .reclaim_complete = nfs41_proc_reclaim_complete,
9448 .detect_trunking = nfs41_discover_server_trunking,
9450 #endif /* CONFIG_NFS_V4_1 */
9452 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
9453 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9454 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
9455 .recover_open = nfs40_open_expired,
9456 .recover_lock = nfs4_lock_expired,
9457 .establish_clid = nfs4_init_clientid,
9460 #if defined(CONFIG_NFS_V4_1)
9461 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
9462 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9463 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
9464 .recover_open = nfs41_open_expired,
9465 .recover_lock = nfs41_lock_expired,
9466 .establish_clid = nfs41_init_clientid,
9468 #endif /* CONFIG_NFS_V4_1 */
9470 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
9471 .sched_state_renewal = nfs4_proc_async_renew,
9472 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
9473 .renew_lease = nfs4_proc_renew,
9476 #if defined(CONFIG_NFS_V4_1)
9477 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
9478 .sched_state_renewal = nfs41_proc_async_sequence,
9479 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
9480 .renew_lease = nfs4_proc_sequence,
9484 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
9485 .get_locations = _nfs40_proc_get_locations,
9486 .fsid_present = _nfs40_proc_fsid_present,
9489 #if defined(CONFIG_NFS_V4_1)
9490 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
9491 .get_locations = _nfs41_proc_get_locations,
9492 .fsid_present = _nfs41_proc_fsid_present,
9494 #endif /* CONFIG_NFS_V4_1 */
9496 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9498 .init_caps = NFS_CAP_READDIRPLUS
9499 | NFS_CAP_ATOMIC_OPEN
9500 | NFS_CAP_POSIX_LOCK,
9501 .init_client = nfs40_init_client,
9502 .shutdown_client = nfs40_shutdown_client,
9503 .match_stateid = nfs4_match_stateid,
9504 .find_root_sec = nfs4_find_root_sec,
9505 .free_lock_state = nfs4_release_lockowner,
9506 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
9507 .alloc_seqid = nfs_alloc_seqid,
9508 .call_sync_ops = &nfs40_call_sync_ops,
9509 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9510 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9511 .state_renewal_ops = &nfs40_state_renewal_ops,
9512 .mig_recovery_ops = &nfs40_mig_recovery_ops,
9515 #if defined(CONFIG_NFS_V4_1)
9516 static struct nfs_seqid *
9517 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9522 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9524 .init_caps = NFS_CAP_READDIRPLUS
9525 | NFS_CAP_ATOMIC_OPEN
9526 | NFS_CAP_POSIX_LOCK
9527 | NFS_CAP_STATEID_NFSV41
9528 | NFS_CAP_ATOMIC_OPEN_V1
9530 .init_client = nfs41_init_client,
9531 .shutdown_client = nfs41_shutdown_client,
9532 .match_stateid = nfs41_match_stateid,
9533 .find_root_sec = nfs41_find_root_sec,
9534 .free_lock_state = nfs41_free_lock_state,
9535 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
9536 .alloc_seqid = nfs_alloc_no_seqid,
9537 .session_trunk = nfs4_test_session_trunk,
9538 .call_sync_ops = &nfs41_call_sync_ops,
9539 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9540 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9541 .state_renewal_ops = &nfs41_state_renewal_ops,
9542 .mig_recovery_ops = &nfs41_mig_recovery_ops,
9546 #if defined(CONFIG_NFS_V4_2)
9547 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9549 .init_caps = NFS_CAP_READDIRPLUS
9550 | NFS_CAP_ATOMIC_OPEN
9551 | NFS_CAP_POSIX_LOCK
9552 | NFS_CAP_STATEID_NFSV41
9553 | NFS_CAP_ATOMIC_OPEN_V1
9557 | NFS_CAP_DEALLOCATE
9559 | NFS_CAP_LAYOUTSTATS
9561 .init_client = nfs41_init_client,
9562 .shutdown_client = nfs41_shutdown_client,
9563 .match_stateid = nfs41_match_stateid,
9564 .find_root_sec = nfs41_find_root_sec,
9565 .free_lock_state = nfs41_free_lock_state,
9566 .call_sync_ops = &nfs41_call_sync_ops,
9567 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
9568 .alloc_seqid = nfs_alloc_no_seqid,
9569 .session_trunk = nfs4_test_session_trunk,
9570 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9571 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9572 .state_renewal_ops = &nfs41_state_renewal_ops,
9573 .mig_recovery_ops = &nfs41_mig_recovery_ops,
9577 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9578 [0] = &nfs_v4_0_minor_ops,
9579 #if defined(CONFIG_NFS_V4_1)
9580 [1] = &nfs_v4_1_minor_ops,
9582 #if defined(CONFIG_NFS_V4_2)
9583 [2] = &nfs_v4_2_minor_ops,
9587 static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
9589 ssize_t error, error2;
9591 error = generic_listxattr(dentry, list, size);
9599 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9602 return error + error2;
9605 static const struct inode_operations nfs4_dir_inode_operations = {
9606 .create = nfs_create,
9607 .lookup = nfs_lookup,
9608 .atomic_open = nfs_atomic_open,
9610 .unlink = nfs_unlink,
9611 .symlink = nfs_symlink,
9615 .rename = nfs_rename,
9616 .permission = nfs_permission,
9617 .getattr = nfs_getattr,
9618 .setattr = nfs_setattr,
9619 .listxattr = nfs4_listxattr,
9622 static const struct inode_operations nfs4_file_inode_operations = {
9623 .permission = nfs_permission,
9624 .getattr = nfs_getattr,
9625 .setattr = nfs_setattr,
9626 .listxattr = nfs4_listxattr,
9629 const struct nfs_rpc_ops nfs_v4_clientops = {
9630 .version = 4, /* protocol version */
9631 .dentry_ops = &nfs4_dentry_operations,
9632 .dir_inode_ops = &nfs4_dir_inode_operations,
9633 .file_inode_ops = &nfs4_file_inode_operations,
9634 .file_ops = &nfs4_file_operations,
9635 .getroot = nfs4_proc_get_root,
9636 .submount = nfs4_submount,
9637 .try_mount = nfs4_try_mount,
9638 .getattr = nfs4_proc_getattr,
9639 .setattr = nfs4_proc_setattr,
9640 .lookup = nfs4_proc_lookup,
9641 .lookupp = nfs4_proc_lookupp,
9642 .access = nfs4_proc_access,
9643 .readlink = nfs4_proc_readlink,
9644 .create = nfs4_proc_create,
9645 .remove = nfs4_proc_remove,
9646 .unlink_setup = nfs4_proc_unlink_setup,
9647 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
9648 .unlink_done = nfs4_proc_unlink_done,
9649 .rename_setup = nfs4_proc_rename_setup,
9650 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
9651 .rename_done = nfs4_proc_rename_done,
9652 .link = nfs4_proc_link,
9653 .symlink = nfs4_proc_symlink,
9654 .mkdir = nfs4_proc_mkdir,
9655 .rmdir = nfs4_proc_rmdir,
9656 .readdir = nfs4_proc_readdir,
9657 .mknod = nfs4_proc_mknod,
9658 .statfs = nfs4_proc_statfs,
9659 .fsinfo = nfs4_proc_fsinfo,
9660 .pathconf = nfs4_proc_pathconf,
9661 .set_capabilities = nfs4_server_capabilities,
9662 .decode_dirent = nfs4_decode_dirent,
9663 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
9664 .read_setup = nfs4_proc_read_setup,
9665 .read_done = nfs4_read_done,
9666 .write_setup = nfs4_proc_write_setup,
9667 .write_done = nfs4_write_done,
9668 .commit_setup = nfs4_proc_commit_setup,
9669 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
9670 .commit_done = nfs4_commit_done,
9671 .lock = nfs4_proc_lock,
9672 .clear_acl_cache = nfs4_zap_acl_attr,
9673 .close_context = nfs4_close_context,
9674 .open_context = nfs4_atomic_open,
9675 .have_delegation = nfs4_have_delegation,
9676 .alloc_client = nfs4_alloc_client,
9677 .init_client = nfs4_init_client,
9678 .free_client = nfs4_free_client,
9679 .create_server = nfs4_create_server,
9680 .clone_server = nfs_clone_server,
9683 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
9684 .name = XATTR_NAME_NFSV4_ACL,
9685 .list = nfs4_xattr_list_nfs4_acl,
9686 .get = nfs4_xattr_get_nfs4_acl,
9687 .set = nfs4_xattr_set_nfs4_acl,
9690 const struct xattr_handler *nfs4_xattr_handlers[] = {
9691 &nfs4_xattr_nfs4_acl_handler,
9692 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
9693 &nfs4_xattr_nfs4_label_handler,