4 * Client-side procedure declarations for NFSv4.
6 * Copyright (c) 2002 The Regents of the University of Michigan.
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <linux/delay.h>
40 #include <linux/errno.h>
41 #include <linux/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>
59 #include "delegation.h"
65 #include "nfs4idmap.h"
66 #include "nfs4session.h"
69 #include "nfs4trace.h"
71 #define NFSDBG_FACILITY NFSDBG_PROC
73 #define NFS4_POLL_RETRY_MIN (HZ/10)
74 #define NFS4_POLL_RETRY_MAX (15*HZ)
76 /* file attributes which can be mapped to nfs attributes */
77 #define NFS4_VALID_ATTRS (ATTR_MODE \
88 static int _nfs4_proc_open(struct nfs4_opendata *data);
89 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
90 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
91 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
92 static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
93 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label);
94 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
95 struct nfs_fattr *fattr, struct iattr *sattr,
96 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
97 struct nfs4_label *olabel);
98 #ifdef CONFIG_NFS_V4_1
99 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
100 struct rpc_cred *cred,
101 struct nfs4_slot *slot,
103 static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
105 static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
106 struct rpc_cred *, bool);
109 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
110 static inline struct nfs4_label *
111 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
112 struct iattr *sattr, struct nfs4_label *label)
119 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
122 err = security_dentry_init_security(dentry, sattr->ia_mode,
123 &dentry->d_name, (void **)&label->label, &label->len);
130 nfs4_label_release_security(struct nfs4_label *label)
133 security_release_secctx(label->label, label->len);
135 static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
138 return server->attr_bitmask;
140 return server->attr_bitmask_nl;
143 static inline struct nfs4_label *
144 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
145 struct iattr *sattr, struct nfs4_label *l)
148 nfs4_label_release_security(struct nfs4_label *label)
151 nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
152 { return server->attr_bitmask; }
155 /* Prevent leaks of NFSv4 errors into userland */
156 static int nfs4_map_errors(int err)
161 case -NFS4ERR_RESOURCE:
162 case -NFS4ERR_LAYOUTTRYLATER:
163 case -NFS4ERR_RECALLCONFLICT:
165 case -NFS4ERR_WRONGSEC:
166 case -NFS4ERR_WRONG_CRED:
168 case -NFS4ERR_BADOWNER:
169 case -NFS4ERR_BADNAME:
171 case -NFS4ERR_SHARE_DENIED:
173 case -NFS4ERR_MINOR_VERS_MISMATCH:
174 return -EPROTONOSUPPORT;
175 case -NFS4ERR_FILE_OPEN:
178 dprintk("%s could not handle NFSv4 error %d\n",
186 * This is our standard bitmap for GETATTR requests.
188 const u32 nfs4_fattr_bitmap[3] = {
190 | FATTR4_WORD0_CHANGE
193 | FATTR4_WORD0_FILEID,
195 | FATTR4_WORD1_NUMLINKS
197 | FATTR4_WORD1_OWNER_GROUP
198 | FATTR4_WORD1_RAWDEV
199 | FATTR4_WORD1_SPACE_USED
200 | FATTR4_WORD1_TIME_ACCESS
201 | FATTR4_WORD1_TIME_METADATA
202 | FATTR4_WORD1_TIME_MODIFY
203 | FATTR4_WORD1_MOUNTED_ON_FILEID,
204 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
205 FATTR4_WORD2_SECURITY_LABEL
209 static const u32 nfs4_pnfs_open_bitmap[3] = {
211 | FATTR4_WORD0_CHANGE
214 | FATTR4_WORD0_FILEID,
216 | FATTR4_WORD1_NUMLINKS
218 | FATTR4_WORD1_OWNER_GROUP
219 | FATTR4_WORD1_RAWDEV
220 | FATTR4_WORD1_SPACE_USED
221 | FATTR4_WORD1_TIME_ACCESS
222 | FATTR4_WORD1_TIME_METADATA
223 | FATTR4_WORD1_TIME_MODIFY,
224 FATTR4_WORD2_MDSTHRESHOLD
225 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
226 | FATTR4_WORD2_SECURITY_LABEL
230 static const u32 nfs4_open_noattr_bitmap[3] = {
232 | FATTR4_WORD0_FILEID,
235 const u32 nfs4_statfs_bitmap[3] = {
236 FATTR4_WORD0_FILES_AVAIL
237 | FATTR4_WORD0_FILES_FREE
238 | FATTR4_WORD0_FILES_TOTAL,
239 FATTR4_WORD1_SPACE_AVAIL
240 | FATTR4_WORD1_SPACE_FREE
241 | FATTR4_WORD1_SPACE_TOTAL
244 const u32 nfs4_pathconf_bitmap[3] = {
246 | FATTR4_WORD0_MAXNAME,
250 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
251 | FATTR4_WORD0_MAXREAD
252 | FATTR4_WORD0_MAXWRITE
253 | FATTR4_WORD0_LEASE_TIME,
254 FATTR4_WORD1_TIME_DELTA
255 | FATTR4_WORD1_FS_LAYOUT_TYPES,
256 FATTR4_WORD2_LAYOUT_BLKSIZE
257 | FATTR4_WORD2_CLONE_BLKSIZE
260 const u32 nfs4_fs_locations_bitmap[3] = {
264 | FATTR4_WORD0_FILEID
265 | FATTR4_WORD0_FS_LOCATIONS,
267 | FATTR4_WORD1_OWNER_GROUP
268 | FATTR4_WORD1_RAWDEV
269 | FATTR4_WORD1_SPACE_USED
270 | FATTR4_WORD1_TIME_ACCESS
271 | FATTR4_WORD1_TIME_METADATA
272 | FATTR4_WORD1_TIME_MODIFY
273 | FATTR4_WORD1_MOUNTED_ON_FILEID,
276 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
277 struct nfs4_readdir_arg *readdir)
279 unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
283 readdir->cookie = cookie;
284 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
289 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
294 * NFSv4 servers do not return entries for '.' and '..'
295 * Therefore, we fake these entries here. We let '.'
296 * have cookie 0 and '..' have cookie 1. Note that
297 * when talking to the server, we always send cookie 0
300 start = p = kmap_atomic(*readdir->pages);
303 *p++ = xdr_one; /* next */
304 *p++ = xdr_zero; /* cookie, first word */
305 *p++ = xdr_one; /* cookie, second word */
306 *p++ = xdr_one; /* entry len */
307 memcpy(p, ".\0\0\0", 4); /* entry */
309 *p++ = xdr_one; /* bitmap length */
310 *p++ = htonl(attrs); /* bitmap */
311 *p++ = htonl(12); /* attribute buffer length */
312 *p++ = htonl(NF4DIR);
313 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
316 *p++ = xdr_one; /* next */
317 *p++ = xdr_zero; /* cookie, first word */
318 *p++ = xdr_two; /* cookie, second word */
319 *p++ = xdr_two; /* entry len */
320 memcpy(p, "..\0\0", 4); /* entry */
322 *p++ = xdr_one; /* bitmap length */
323 *p++ = htonl(attrs); /* bitmap */
324 *p++ = htonl(12); /* attribute buffer length */
325 *p++ = htonl(NF4DIR);
326 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
328 readdir->pgbase = (char *)p - (char *)start;
329 readdir->count -= readdir->pgbase;
330 kunmap_atomic(start);
333 static void nfs4_test_and_free_stateid(struct nfs_server *server,
334 nfs4_stateid *stateid,
335 struct rpc_cred *cred)
337 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
339 ops->test_and_free_expired(server, stateid, cred);
342 static void __nfs4_free_revoked_stateid(struct nfs_server *server,
343 nfs4_stateid *stateid,
344 struct rpc_cred *cred)
346 stateid->type = NFS4_REVOKED_STATEID_TYPE;
347 nfs4_test_and_free_stateid(server, stateid, cred);
350 static void nfs4_free_revoked_stateid(struct nfs_server *server,
351 const nfs4_stateid *stateid,
352 struct rpc_cred *cred)
356 nfs4_stateid_copy(&tmp, stateid);
357 __nfs4_free_revoked_stateid(server, &tmp, cred);
360 static long nfs4_update_delay(long *timeout)
364 return NFS4_POLL_RETRY_MAX;
366 *timeout = NFS4_POLL_RETRY_MIN;
367 if (*timeout > NFS4_POLL_RETRY_MAX)
368 *timeout = NFS4_POLL_RETRY_MAX;
374 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
380 freezable_schedule_timeout_killable_unsafe(
381 nfs4_update_delay(timeout));
382 if (fatal_signal_pending(current))
387 /* This is the error handling routine for processes that are allowed
390 static int nfs4_do_handle_exception(struct nfs_server *server,
391 int errorcode, struct nfs4_exception *exception)
393 struct nfs_client *clp = server->nfs_client;
394 struct nfs4_state *state = exception->state;
395 const nfs4_stateid *stateid = exception->stateid;
396 struct inode *inode = exception->inode;
399 exception->delay = 0;
400 exception->recovering = 0;
401 exception->retry = 0;
403 if (stateid == NULL && state != NULL)
404 stateid = &state->stateid;
409 case -NFS4ERR_DELEG_REVOKED:
410 case -NFS4ERR_ADMIN_REVOKED:
411 case -NFS4ERR_EXPIRED:
412 case -NFS4ERR_BAD_STATEID:
413 if (inode != NULL && stateid != NULL) {
414 nfs_inode_find_state_and_recover(inode,
416 goto wait_on_recovery;
418 case -NFS4ERR_OPENMODE:
422 err = nfs_async_inode_return_delegation(inode,
425 goto wait_on_recovery;
426 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
427 exception->retry = 1;
433 ret = nfs4_schedule_stateid_recovery(server, state);
436 goto wait_on_recovery;
437 case -NFS4ERR_STALE_STATEID:
438 case -NFS4ERR_STALE_CLIENTID:
439 nfs4_schedule_lease_recovery(clp);
440 goto wait_on_recovery;
442 ret = nfs4_schedule_migration_recovery(server);
445 goto wait_on_recovery;
446 case -NFS4ERR_LEASE_MOVED:
447 nfs4_schedule_lease_moved_recovery(clp);
448 goto wait_on_recovery;
449 #if defined(CONFIG_NFS_V4_1)
450 case -NFS4ERR_BADSESSION:
451 case -NFS4ERR_BADSLOT:
452 case -NFS4ERR_BAD_HIGH_SLOT:
453 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
454 case -NFS4ERR_DEADSESSION:
455 case -NFS4ERR_SEQ_FALSE_RETRY:
456 case -NFS4ERR_SEQ_MISORDERED:
457 dprintk("%s ERROR: %d Reset session\n", __func__,
459 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
460 goto wait_on_recovery;
461 #endif /* defined(CONFIG_NFS_V4_1) */
462 case -NFS4ERR_FILE_OPEN:
463 if (exception->timeout > HZ) {
464 /* We have retried a decent amount, time to
471 nfs_inc_server_stats(server, NFSIOS_DELAY);
473 case -NFS4ERR_LAYOUTTRYLATER:
474 case -NFS4ERR_RECALLCONFLICT:
475 exception->delay = 1;
478 case -NFS4ERR_RETRY_UNCACHED_REP:
479 case -NFS4ERR_OLD_STATEID:
480 exception->retry = 1;
482 case -NFS4ERR_BADOWNER:
483 /* The following works around a Linux server bug! */
484 case -NFS4ERR_BADNAME:
485 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
486 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
487 exception->retry = 1;
488 printk(KERN_WARNING "NFS: v4 server %s "
489 "does not accept raw "
491 "Reenabling the idmapper.\n",
492 server->nfs_client->cl_hostname);
495 /* We failed to handle the error */
496 return nfs4_map_errors(ret);
498 exception->recovering = 1;
502 /* This is the error handling routine for processes that are allowed
505 int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
507 struct nfs_client *clp = server->nfs_client;
510 ret = nfs4_do_handle_exception(server, errorcode, exception);
511 if (exception->delay) {
512 ret = nfs4_delay(server->client, &exception->timeout);
515 if (exception->recovering) {
516 ret = nfs4_wait_clnt_recover(clp);
517 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
524 exception->retry = 1;
529 nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
530 int errorcode, struct nfs4_exception *exception)
532 struct nfs_client *clp = server->nfs_client;
535 ret = nfs4_do_handle_exception(server, errorcode, exception);
536 if (exception->delay) {
537 rpc_delay(task, nfs4_update_delay(&exception->timeout));
540 if (exception->recovering) {
541 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
542 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
543 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
546 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
551 exception->retry = 1;
553 * For NFS4ERR_MOVED, the client transport will need to
554 * be recomputed after migration recovery has completed.
556 if (errorcode == -NFS4ERR_MOVED)
557 rpc_task_release_transport(task);
563 nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
564 struct nfs4_state *state, long *timeout)
566 struct nfs4_exception exception = {
570 if (task->tk_status >= 0)
573 exception.timeout = *timeout;
574 task->tk_status = nfs4_async_handle_exception(task, server,
577 if (exception.delay && timeout)
578 *timeout = exception.timeout;
585 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
586 * or 'false' otherwise.
588 static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
590 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
591 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
594 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
596 spin_lock(&clp->cl_lock);
597 if (time_before(clp->cl_last_renewal,timestamp))
598 clp->cl_last_renewal = timestamp;
599 spin_unlock(&clp->cl_lock);
602 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
604 struct nfs_client *clp = server->nfs_client;
606 if (!nfs4_has_session(clp))
607 do_renew_lease(clp, timestamp);
610 struct nfs4_call_sync_data {
611 const struct nfs_server *seq_server;
612 struct nfs4_sequence_args *seq_args;
613 struct nfs4_sequence_res *seq_res;
616 void nfs4_init_sequence(struct nfs4_sequence_args *args,
617 struct nfs4_sequence_res *res, int cache_reply)
619 args->sa_slot = NULL;
620 args->sa_cache_this = cache_reply;
621 args->sa_privileged = 0;
626 static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
628 args->sa_privileged = 1;
631 static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
633 struct nfs4_slot *slot = res->sr_slot;
634 struct nfs4_slot_table *tbl;
637 spin_lock(&tbl->slot_tbl_lock);
638 if (!nfs41_wake_and_assign_slot(tbl, slot))
639 nfs4_free_slot(tbl, slot);
640 spin_unlock(&tbl->slot_tbl_lock);
645 static int nfs40_sequence_done(struct rpc_task *task,
646 struct nfs4_sequence_res *res)
648 if (res->sr_slot != NULL)
649 nfs40_sequence_free_slot(res);
653 #if defined(CONFIG_NFS_V4_1)
655 static void nfs41_release_slot(struct nfs4_slot *slot)
657 struct nfs4_session *session;
658 struct nfs4_slot_table *tbl;
659 bool send_new_highest_used_slotid = false;
664 session = tbl->session;
666 /* Bump the slot sequence number */
671 spin_lock(&tbl->slot_tbl_lock);
672 /* Be nice to the server: try to ensure that the last transmitted
673 * value for highest_user_slotid <= target_highest_slotid
675 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
676 send_new_highest_used_slotid = true;
678 if (nfs41_wake_and_assign_slot(tbl, slot)) {
679 send_new_highest_used_slotid = false;
682 nfs4_free_slot(tbl, slot);
684 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
685 send_new_highest_used_slotid = false;
687 spin_unlock(&tbl->slot_tbl_lock);
688 if (send_new_highest_used_slotid)
689 nfs41_notify_server(session->clp);
690 if (waitqueue_active(&tbl->slot_waitq))
691 wake_up_all(&tbl->slot_waitq);
694 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
696 nfs41_release_slot(res->sr_slot);
700 static int nfs41_sequence_process(struct rpc_task *task,
701 struct nfs4_sequence_res *res)
703 struct nfs4_session *session;
704 struct nfs4_slot *slot = res->sr_slot;
705 struct nfs_client *clp;
706 bool interrupted = false;
711 /* don't increment the sequence number if the task wasn't sent */
712 if (!RPC_WAS_SENT(task))
715 session = slot->table->session;
717 if (slot->interrupted) {
718 if (res->sr_status != -NFS4ERR_DELAY)
719 slot->interrupted = 0;
723 trace_nfs4_sequence_done(session, res);
724 /* Check the SEQUENCE operation status */
725 switch (res->sr_status) {
727 /* Update the slot's sequence and clientid lease timer */
730 do_renew_lease(clp, res->sr_timestamp);
731 /* Check sequence flags */
732 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
734 nfs41_update_target_slotid(slot->table, slot, res);
738 * sr_status remains 1 if an RPC level error occurred.
739 * The server may or may not have processed the sequence
741 * Mark the slot as having hosted an interrupted RPC call.
743 slot->interrupted = 1;
746 /* The server detected a resend of the RPC call and
747 * returned NFS4ERR_DELAY as per Section 2.10.6.2
750 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
755 case -NFS4ERR_RETRY_UNCACHED_REP:
756 case -NFS4ERR_SEQ_FALSE_RETRY:
758 * The server thinks we tried to replay a request.
759 * Retry the call after bumping the sequence ID.
762 case -NFS4ERR_BADSLOT:
764 * The slot id we used was probably retired. Try again
765 * using a different slot id.
767 if (slot->slot_nr < slot->table->target_highest_slotid)
768 goto session_recover;
770 case -NFS4ERR_SEQ_MISORDERED:
772 * Was the last operation on this sequence interrupted?
773 * If so, retry after bumping the sequence number.
778 * Could this slot have been previously retired?
779 * If so, then the server may be expecting seq_nr = 1!
781 if (slot->seq_nr != 1) {
785 goto session_recover;
787 /* Just update the slot sequence no. */
791 /* The session may be reset by one of the error handlers. */
792 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
796 nfs4_schedule_session_recovery(session, res->sr_status);
801 if (rpc_restart_call_prepare(task)) {
802 nfs41_sequence_free_slot(res);
808 if (!rpc_restart_call(task))
810 rpc_delay(task, NFS4_POLL_RETRY_MAX);
814 int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
816 if (!nfs41_sequence_process(task, res))
818 if (res->sr_slot != NULL)
819 nfs41_sequence_free_slot(res);
823 EXPORT_SYMBOL_GPL(nfs41_sequence_done);
825 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
827 if (res->sr_slot == NULL)
829 if (res->sr_slot->table->session != NULL)
830 return nfs41_sequence_process(task, res);
831 return nfs40_sequence_done(task, res);
834 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
836 if (res->sr_slot != NULL) {
837 if (res->sr_slot->table->session != NULL)
838 nfs41_sequence_free_slot(res);
840 nfs40_sequence_free_slot(res);
844 int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
846 if (res->sr_slot == NULL)
848 if (!res->sr_slot->table->session)
849 return nfs40_sequence_done(task, res);
850 return nfs41_sequence_done(task, res);
852 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
854 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
856 struct nfs4_call_sync_data *data = calldata;
858 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
860 nfs4_setup_sequence(data->seq_server->nfs_client,
861 data->seq_args, data->seq_res, task);
864 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
866 struct nfs4_call_sync_data *data = calldata;
868 nfs41_sequence_done(task, data->seq_res);
871 static const struct rpc_call_ops nfs41_call_sync_ops = {
872 .rpc_call_prepare = nfs41_call_sync_prepare,
873 .rpc_call_done = nfs41_call_sync_done,
877 nfs4_sequence_process_interrupted(struct nfs_client *client,
878 struct nfs4_slot *slot, struct rpc_cred *cred)
880 struct rpc_task *task;
882 task = _nfs41_proc_sequence(client, cred, slot, true);
884 rpc_put_task_async(task);
887 #else /* !CONFIG_NFS_V4_1 */
889 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
891 return nfs40_sequence_done(task, res);
894 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
896 if (res->sr_slot != NULL)
897 nfs40_sequence_free_slot(res);
900 int nfs4_sequence_done(struct rpc_task *task,
901 struct nfs4_sequence_res *res)
903 return nfs40_sequence_done(task, res);
905 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
908 nfs4_sequence_process_interrupted(struct nfs_client *client,
909 struct nfs4_slot *slot, struct rpc_cred *cred)
912 slot->interrupted = 0;
915 #endif /* !CONFIG_NFS_V4_1 */
917 static void nfs41_sequence_res_init(struct nfs4_sequence_res *res)
919 res->sr_timestamp = jiffies;
920 res->sr_status_flags = 0;
925 void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
926 struct nfs4_sequence_res *res,
927 struct nfs4_slot *slot)
931 slot->privileged = args->sa_privileged ? 1 : 0;
932 args->sa_slot = slot;
937 int nfs4_setup_sequence(struct nfs_client *client,
938 struct nfs4_sequence_args *args,
939 struct nfs4_sequence_res *res,
940 struct rpc_task *task)
942 struct nfs4_session *session = nfs4_get_session(client);
943 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
944 struct nfs4_slot *slot;
946 /* slot already allocated? */
947 if (res->sr_slot != NULL)
951 tbl = &session->fc_slot_table;
952 task->tk_timeout = 0;
956 spin_lock(&tbl->slot_tbl_lock);
957 /* The state manager will wait until the slot table is empty */
958 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
961 slot = nfs4_alloc_slot(tbl);
963 /* Try again in 1/4 second */
964 if (slot == ERR_PTR(-ENOMEM))
965 task->tk_timeout = HZ >> 2;
968 spin_unlock(&tbl->slot_tbl_lock);
970 if (likely(!slot->interrupted))
972 nfs4_sequence_process_interrupted(client,
973 slot, task->tk_msg.rpc_cred);
976 nfs4_sequence_attach_slot(args, res, slot);
978 trace_nfs4_setup_sequence(session, args);
980 nfs41_sequence_res_init(res);
981 rpc_call_start(task);
985 if (args->sa_privileged)
986 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
987 NULL, RPC_PRIORITY_PRIVILEGED);
989 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
990 spin_unlock(&tbl->slot_tbl_lock);
993 EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
995 static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
997 struct nfs4_call_sync_data *data = calldata;
998 nfs4_setup_sequence(data->seq_server->nfs_client,
999 data->seq_args, data->seq_res, task);
1002 static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1004 struct nfs4_call_sync_data *data = calldata;
1005 nfs4_sequence_done(task, data->seq_res);
1008 static const struct rpc_call_ops nfs40_call_sync_ops = {
1009 .rpc_call_prepare = nfs40_call_sync_prepare,
1010 .rpc_call_done = nfs40_call_sync_done,
1013 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
1014 struct nfs_server *server,
1015 struct rpc_message *msg,
1016 struct nfs4_sequence_args *args,
1017 struct nfs4_sequence_res *res)
1020 struct rpc_task *task;
1021 struct nfs_client *clp = server->nfs_client;
1022 struct nfs4_call_sync_data data = {
1023 .seq_server = server,
1027 struct rpc_task_setup task_setup = {
1030 .callback_ops = clp->cl_mvops->call_sync_ops,
1031 .callback_data = &data
1034 task = rpc_run_task(&task_setup);
1036 ret = PTR_ERR(task);
1038 ret = task->tk_status;
1044 int nfs4_call_sync(struct rpc_clnt *clnt,
1045 struct nfs_server *server,
1046 struct rpc_message *msg,
1047 struct nfs4_sequence_args *args,
1048 struct nfs4_sequence_res *res,
1051 nfs4_init_sequence(args, res, cache_reply);
1052 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
1055 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1056 unsigned long timestamp)
1058 struct nfs_inode *nfsi = NFS_I(dir);
1060 spin_lock(&dir->i_lock);
1061 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
1062 if (cinfo->atomic && cinfo->before == dir->i_version) {
1063 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1064 nfsi->attrtimeo_timestamp = jiffies;
1066 nfs_force_lookup_revalidate(dir);
1067 if (cinfo->before != dir->i_version)
1068 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1069 NFS_INO_INVALID_ACL;
1071 dir->i_version = cinfo->after;
1072 nfsi->read_cache_jiffies = timestamp;
1073 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
1074 nfs_fscache_invalidate(dir);
1075 spin_unlock(&dir->i_lock);
1078 struct nfs4_opendata {
1080 struct nfs_openargs o_arg;
1081 struct nfs_openres o_res;
1082 struct nfs_open_confirmargs c_arg;
1083 struct nfs_open_confirmres c_res;
1084 struct nfs4_string owner_name;
1085 struct nfs4_string group_name;
1086 struct nfs4_label *a_label;
1087 struct nfs_fattr f_attr;
1088 struct nfs4_label *f_label;
1090 struct dentry *dentry;
1091 struct nfs4_state_owner *owner;
1092 struct nfs4_state *state;
1094 unsigned long timestamp;
1102 struct nfs4_open_createattrs {
1103 struct nfs4_label *label;
1104 struct iattr *sattr;
1105 const __u32 verf[2];
1108 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1109 int err, struct nfs4_exception *exception)
1113 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1115 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1116 exception->retry = 1;
1121 nfs4_map_atomic_open_share(struct nfs_server *server,
1122 fmode_t fmode, int openflags)
1126 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1128 res = NFS4_SHARE_ACCESS_READ;
1131 res = NFS4_SHARE_ACCESS_WRITE;
1133 case FMODE_READ|FMODE_WRITE:
1134 res = NFS4_SHARE_ACCESS_BOTH;
1136 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1138 /* Want no delegation if we're using O_DIRECT */
1139 if (openflags & O_DIRECT)
1140 res |= NFS4_SHARE_WANT_NO_DELEG;
1145 static enum open_claim_type4
1146 nfs4_map_atomic_open_claim(struct nfs_server *server,
1147 enum open_claim_type4 claim)
1149 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1154 case NFS4_OPEN_CLAIM_FH:
1155 return NFS4_OPEN_CLAIM_NULL;
1156 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1157 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1158 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1159 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1163 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
1165 p->o_res.f_attr = &p->f_attr;
1166 p->o_res.f_label = p->f_label;
1167 p->o_res.seqid = p->o_arg.seqid;
1168 p->c_res.seqid = p->c_arg.seqid;
1169 p->o_res.server = p->o_arg.server;
1170 p->o_res.access_request = p->o_arg.access;
1171 nfs_fattr_init(&p->f_attr);
1172 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
1175 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
1176 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
1177 const struct nfs4_open_createattrs *c,
1178 enum open_claim_type4 claim,
1181 struct dentry *parent = dget_parent(dentry);
1182 struct inode *dir = d_inode(parent);
1183 struct nfs_server *server = NFS_SERVER(dir);
1184 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
1185 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
1186 struct nfs4_opendata *p;
1188 p = kzalloc(sizeof(*p), gfp_mask);
1192 p->f_label = nfs4_label_alloc(server, gfp_mask);
1193 if (IS_ERR(p->f_label))
1196 p->a_label = nfs4_label_alloc(server, gfp_mask);
1197 if (IS_ERR(p->a_label))
1200 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1201 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
1202 if (IS_ERR(p->o_arg.seqid))
1203 goto err_free_label;
1204 nfs_sb_active(dentry->d_sb);
1205 p->dentry = dget(dentry);
1208 atomic_inc(&sp->so_count);
1209 p->o_arg.open_flags = flags;
1210 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
1211 p->o_arg.umask = current_umask();
1212 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1213 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1215 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1216 * will return permission denied for all bits until close */
1217 if (!(flags & O_EXCL)) {
1218 /* ask server to check for all possible rights as results
1220 switch (p->o_arg.claim) {
1223 case NFS4_OPEN_CLAIM_NULL:
1224 case NFS4_OPEN_CLAIM_FH:
1225 p->o_arg.access = NFS4_ACCESS_READ |
1226 NFS4_ACCESS_MODIFY |
1227 NFS4_ACCESS_EXTEND |
1228 NFS4_ACCESS_EXECUTE;
1231 p->o_arg.clientid = server->nfs_client->cl_clientid;
1232 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1233 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1234 p->o_arg.name = &dentry->d_name;
1235 p->o_arg.server = server;
1236 p->o_arg.bitmask = nfs4_bitmask(server, label);
1237 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1238 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1239 switch (p->o_arg.claim) {
1240 case NFS4_OPEN_CLAIM_NULL:
1241 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1242 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1243 p->o_arg.fh = NFS_FH(dir);
1245 case NFS4_OPEN_CLAIM_PREVIOUS:
1246 case NFS4_OPEN_CLAIM_FH:
1247 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1248 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1249 p->o_arg.fh = NFS_FH(d_inode(dentry));
1251 if (c != NULL && c->sattr != NULL && c->sattr->ia_valid != 0) {
1252 p->o_arg.u.attrs = &p->attrs;
1253 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
1255 memcpy(p->o_arg.u.verifier.data, c->verf,
1256 sizeof(p->o_arg.u.verifier.data));
1258 p->c_arg.fh = &p->o_res.fh;
1259 p->c_arg.stateid = &p->o_res.stateid;
1260 p->c_arg.seqid = p->o_arg.seqid;
1261 nfs4_init_opendata_res(p);
1262 kref_init(&p->kref);
1266 nfs4_label_free(p->a_label);
1268 nfs4_label_free(p->f_label);
1276 static void nfs4_opendata_free(struct kref *kref)
1278 struct nfs4_opendata *p = container_of(kref,
1279 struct nfs4_opendata, kref);
1280 struct super_block *sb = p->dentry->d_sb;
1282 nfs_free_seqid(p->o_arg.seqid);
1283 nfs4_sequence_free_slot(&p->o_res.seq_res);
1284 if (p->state != NULL)
1285 nfs4_put_open_state(p->state);
1286 nfs4_put_state_owner(p->owner);
1288 nfs4_label_free(p->a_label);
1289 nfs4_label_free(p->f_label);
1293 nfs_sb_deactive(sb);
1294 nfs_fattr_free_names(&p->f_attr);
1295 kfree(p->f_attr.mdsthreshold);
1299 static void nfs4_opendata_put(struct nfs4_opendata *p)
1302 kref_put(&p->kref, nfs4_opendata_free);
1305 static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1308 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1309 case FMODE_READ|FMODE_WRITE:
1310 return state->n_rdwr != 0;
1312 return state->n_wronly != 0;
1314 return state->n_rdonly != 0;
1320 static int can_open_cached(struct nfs4_state *state, fmode_t mode,
1321 int open_mode, enum open_claim_type4 claim)
1325 if (open_mode & (O_EXCL|O_TRUNC))
1328 case NFS4_OPEN_CLAIM_NULL:
1329 case NFS4_OPEN_CLAIM_FH:
1334 switch (mode & (FMODE_READ|FMODE_WRITE)) {
1336 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1337 && state->n_rdonly != 0;
1340 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1341 && state->n_wronly != 0;
1343 case FMODE_READ|FMODE_WRITE:
1344 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1345 && state->n_rdwr != 0;
1351 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1352 enum open_claim_type4 claim)
1354 if (delegation == NULL)
1356 if ((delegation->type & fmode) != fmode)
1359 case NFS4_OPEN_CLAIM_NULL:
1360 case NFS4_OPEN_CLAIM_FH:
1362 case NFS4_OPEN_CLAIM_PREVIOUS:
1363 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1368 nfs_mark_delegation_referenced(delegation);
1372 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1381 case FMODE_READ|FMODE_WRITE:
1384 nfs4_state_set_mode_locked(state, state->state | fmode);
1387 #ifdef CONFIG_NFS_V4_1
1388 static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1390 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1392 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1394 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1398 #endif /* CONFIG_NFS_V4_1 */
1400 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
1402 struct nfs_client *clp = state->owner->so_server->nfs_client;
1403 bool need_recover = false;
1405 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1406 need_recover = true;
1407 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1408 need_recover = true;
1409 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1410 need_recover = true;
1412 nfs4_state_mark_reclaim_nograce(clp, state);
1415 static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1416 const nfs4_stateid *stateid, nfs4_stateid *freeme)
1418 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1420 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1421 nfs4_stateid_copy(freeme, &state->open_stateid);
1422 nfs_test_and_clear_all_open_stateid(state);
1425 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1430 static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1432 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1434 if (state->n_wronly)
1435 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1436 if (state->n_rdonly)
1437 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1439 set_bit(NFS_O_RDWR_STATE, &state->flags);
1440 set_bit(NFS_OPEN_STATE, &state->flags);
1443 static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1444 nfs4_stateid *stateid, fmode_t fmode)
1446 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1447 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1449 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1452 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1455 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1456 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1457 clear_bit(NFS_OPEN_STATE, &state->flags);
1459 if (stateid == NULL)
1461 /* Handle OPEN+OPEN_DOWNGRADE races */
1462 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1463 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1464 nfs_resync_open_stateid_locked(state);
1467 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1468 nfs4_stateid_copy(&state->stateid, stateid);
1469 nfs4_stateid_copy(&state->open_stateid, stateid);
1472 static void nfs_clear_open_stateid(struct nfs4_state *state,
1473 nfs4_stateid *arg_stateid,
1474 nfs4_stateid *stateid, fmode_t fmode)
1476 write_seqlock(&state->seqlock);
1477 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1478 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1479 nfs_clear_open_stateid_locked(state, stateid, fmode);
1480 write_sequnlock(&state->seqlock);
1481 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1482 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1485 static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1486 const nfs4_stateid *stateid, fmode_t fmode,
1487 nfs4_stateid *freeme)
1491 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1494 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1496 case FMODE_READ|FMODE_WRITE:
1497 set_bit(NFS_O_RDWR_STATE, &state->flags);
1499 if (!nfs_need_update_open_stateid(state, stateid, freeme))
1501 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1502 nfs4_stateid_copy(&state->stateid, stateid);
1503 nfs4_stateid_copy(&state->open_stateid, stateid);
1506 static void __update_open_stateid(struct nfs4_state *state,
1507 const nfs4_stateid *open_stateid,
1508 const nfs4_stateid *deleg_stateid,
1510 nfs4_stateid *freeme)
1513 * Protect the call to nfs4_state_set_mode_locked and
1514 * serialise the stateid update
1516 spin_lock(&state->owner->so_lock);
1517 write_seqlock(&state->seqlock);
1518 if (deleg_stateid != NULL) {
1519 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1520 set_bit(NFS_DELEGATED_STATE, &state->flags);
1522 if (open_stateid != NULL)
1523 nfs_set_open_stateid_locked(state, open_stateid, fmode, freeme);
1524 write_sequnlock(&state->seqlock);
1525 update_open_stateflags(state, fmode);
1526 spin_unlock(&state->owner->so_lock);
1529 static int update_open_stateid(struct nfs4_state *state,
1530 const nfs4_stateid *open_stateid,
1531 const nfs4_stateid *delegation,
1534 struct nfs_server *server = NFS_SERVER(state->inode);
1535 struct nfs_client *clp = server->nfs_client;
1536 struct nfs_inode *nfsi = NFS_I(state->inode);
1537 struct nfs_delegation *deleg_cur;
1538 nfs4_stateid freeme = { };
1541 fmode &= (FMODE_READ|FMODE_WRITE);
1544 deleg_cur = rcu_dereference(nfsi->delegation);
1545 if (deleg_cur == NULL)
1548 spin_lock(&deleg_cur->lock);
1549 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1550 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1551 (deleg_cur->type & fmode) != fmode)
1552 goto no_delegation_unlock;
1554 if (delegation == NULL)
1555 delegation = &deleg_cur->stateid;
1556 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
1557 goto no_delegation_unlock;
1559 nfs_mark_delegation_referenced(deleg_cur);
1560 __update_open_stateid(state, open_stateid, &deleg_cur->stateid,
1563 no_delegation_unlock:
1564 spin_unlock(&deleg_cur->lock);
1568 if (!ret && open_stateid != NULL) {
1569 __update_open_stateid(state, open_stateid, NULL, fmode, &freeme);
1572 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1573 nfs4_schedule_state_manager(clp);
1574 if (freeme.type != 0)
1575 nfs4_test_and_free_stateid(server, &freeme,
1576 state->owner->so_cred);
1581 static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1582 const nfs4_stateid *stateid)
1584 struct nfs4_state *state = lsp->ls_state;
1587 spin_lock(&state->state_lock);
1588 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1590 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1592 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1595 spin_unlock(&state->state_lock);
1599 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1601 struct nfs_delegation *delegation;
1604 delegation = rcu_dereference(NFS_I(inode)->delegation);
1605 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1610 nfs4_inode_return_delegation(inode);
1613 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1615 struct nfs4_state *state = opendata->state;
1616 struct nfs_delegation *delegation;
1617 int open_mode = opendata->o_arg.open_flags;
1618 fmode_t fmode = opendata->o_arg.fmode;
1619 enum open_claim_type4 claim = opendata->o_arg.claim;
1620 nfs4_stateid stateid;
1624 spin_lock(&state->owner->so_lock);
1625 if (can_open_cached(state, fmode, open_mode, claim)) {
1626 update_open_stateflags(state, fmode);
1627 spin_unlock(&state->owner->so_lock);
1628 goto out_return_state;
1630 spin_unlock(&state->owner->so_lock);
1632 delegation = nfs4_get_valid_delegation(state->inode);
1633 if (!can_open_delegated(delegation, fmode, claim)) {
1637 /* Save the delegation */
1638 nfs4_stateid_copy(&stateid, &delegation->stateid);
1640 nfs_release_seqid(opendata->o_arg.seqid);
1641 if (!opendata->is_recover) {
1642 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1648 /* Try to update the stateid using the delegation */
1649 if (update_open_stateid(state, NULL, &stateid, fmode))
1650 goto out_return_state;
1653 return ERR_PTR(ret);
1655 atomic_inc(&state->count);
1660 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1662 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1663 struct nfs_delegation *delegation;
1664 int delegation_flags = 0;
1667 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1669 delegation_flags = delegation->flags;
1671 switch (data->o_arg.claim) {
1674 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1675 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1676 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1677 "returning a delegation for "
1678 "OPEN(CLAIM_DELEGATE_CUR)\n",
1682 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1683 nfs_inode_set_delegation(state->inode,
1684 data->owner->so_cred,
1687 nfs_inode_reclaim_delegation(state->inode,
1688 data->owner->so_cred,
1693 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1694 * and update the nfs4_state.
1696 static struct nfs4_state *
1697 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1699 struct inode *inode = data->state->inode;
1700 struct nfs4_state *state = data->state;
1703 if (!data->rpc_done) {
1704 if (data->rpc_status)
1705 return ERR_PTR(data->rpc_status);
1706 /* cached opens have already been processed */
1710 ret = nfs_refresh_inode(inode, &data->f_attr);
1712 return ERR_PTR(ret);
1714 if (data->o_res.delegation_type != 0)
1715 nfs4_opendata_check_deleg(data, state);
1717 update_open_stateid(state, &data->o_res.stateid, NULL,
1719 atomic_inc(&state->count);
1724 static struct inode *
1725 nfs4_opendata_get_inode(struct nfs4_opendata *data)
1727 struct inode *inode;
1729 switch (data->o_arg.claim) {
1730 case NFS4_OPEN_CLAIM_NULL:
1731 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1732 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1733 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1734 return ERR_PTR(-EAGAIN);
1735 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1736 &data->f_attr, data->f_label);
1739 inode = d_inode(data->dentry);
1741 nfs_refresh_inode(inode, &data->f_attr);
1746 static struct nfs4_state *
1747 nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
1749 struct nfs4_state *state;
1750 struct inode *inode;
1752 inode = nfs4_opendata_get_inode(data);
1754 return ERR_CAST(inode);
1755 if (data->state != NULL && data->state->inode == inode) {
1756 state = data->state;
1757 atomic_inc(&state->count);
1759 state = nfs4_get_open_state(inode, data->owner);
1762 state = ERR_PTR(-ENOMEM);
1766 static struct nfs4_state *
1767 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1769 struct nfs4_state *state;
1771 if (!data->rpc_done) {
1772 state = nfs4_try_open_cached(data);
1773 trace_nfs4_cached_open(data->state);
1777 state = nfs4_opendata_find_nfs4_state(data);
1781 if (data->o_res.delegation_type != 0)
1782 nfs4_opendata_check_deleg(data, state);
1783 update_open_stateid(state, &data->o_res.stateid, NULL,
1786 nfs_release_seqid(data->o_arg.seqid);
1790 static struct nfs4_state *
1791 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1793 struct nfs4_state *ret;
1795 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1796 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1798 ret = _nfs4_opendata_to_nfs4_state(data);
1799 nfs4_sequence_free_slot(&data->o_res.seq_res);
1803 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1805 struct nfs_inode *nfsi = NFS_I(state->inode);
1806 struct nfs_open_context *ctx;
1808 spin_lock(&state->inode->i_lock);
1809 list_for_each_entry(ctx, &nfsi->open_files, list) {
1810 if (ctx->state != state)
1812 get_nfs_open_context(ctx);
1813 spin_unlock(&state->inode->i_lock);
1816 spin_unlock(&state->inode->i_lock);
1817 return ERR_PTR(-ENOENT);
1820 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1821 struct nfs4_state *state, enum open_claim_type4 claim)
1823 struct nfs4_opendata *opendata;
1825 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
1826 NULL, claim, GFP_NOFS);
1827 if (opendata == NULL)
1828 return ERR_PTR(-ENOMEM);
1829 opendata->state = state;
1830 atomic_inc(&state->count);
1834 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1837 struct nfs4_state *newstate;
1838 struct nfs_server *server = NFS_SB(opendata->dentry->d_sb);
1839 int openflags = opendata->o_arg.open_flags;
1842 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
1844 opendata->o_arg.fmode = fmode;
1845 opendata->o_arg.share_access =
1846 nfs4_map_atomic_open_share(server, fmode, openflags);
1847 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1848 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1849 nfs4_init_opendata_res(opendata);
1850 ret = _nfs4_recover_proc_open(opendata);
1853 newstate = nfs4_opendata_to_nfs4_state(opendata);
1854 if (IS_ERR(newstate))
1855 return PTR_ERR(newstate);
1856 if (newstate != opendata->state)
1858 nfs4_close_state(newstate, fmode);
1862 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1866 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1867 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1868 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1869 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1870 /* memory barrier prior to reading state->n_* */
1871 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1872 clear_bit(NFS_OPEN_STATE, &state->flags);
1874 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1877 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1880 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1884 * We may have performed cached opens for all three recoveries.
1885 * Check if we need to update the current stateid.
1887 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1888 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
1889 write_seqlock(&state->seqlock);
1890 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1891 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1892 write_sequnlock(&state->seqlock);
1899 * reclaim state on the server after a reboot.
1901 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1903 struct nfs_delegation *delegation;
1904 struct nfs4_opendata *opendata;
1905 fmode_t delegation_type = 0;
1908 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1909 NFS4_OPEN_CLAIM_PREVIOUS);
1910 if (IS_ERR(opendata))
1911 return PTR_ERR(opendata);
1913 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1914 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1915 delegation_type = delegation->type;
1917 opendata->o_arg.u.delegation_type = delegation_type;
1918 status = nfs4_open_recover(opendata, state);
1919 nfs4_opendata_put(opendata);
1923 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1925 struct nfs_server *server = NFS_SERVER(state->inode);
1926 struct nfs4_exception exception = { };
1929 err = _nfs4_do_open_reclaim(ctx, state);
1930 trace_nfs4_open_reclaim(ctx, 0, err);
1931 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1933 if (err != -NFS4ERR_DELAY)
1935 nfs4_handle_exception(server, err, &exception);
1936 } while (exception.retry);
1940 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1942 struct nfs_open_context *ctx;
1945 ctx = nfs4_state_find_open_context(state);
1948 ret = nfs4_do_open_reclaim(ctx, state);
1949 put_nfs_open_context(ctx);
1953 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)
1957 printk(KERN_ERR "NFS: %s: unhandled error "
1958 "%d.\n", __func__, err);
1964 case -NFS4ERR_BADSESSION:
1965 case -NFS4ERR_BADSLOT:
1966 case -NFS4ERR_BAD_HIGH_SLOT:
1967 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1968 case -NFS4ERR_DEADSESSION:
1969 set_bit(NFS_DELEGATED_STATE, &state->flags);
1970 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1972 case -NFS4ERR_STALE_CLIENTID:
1973 case -NFS4ERR_STALE_STATEID:
1974 set_bit(NFS_DELEGATED_STATE, &state->flags);
1975 /* Don't recall a delegation if it was lost */
1976 nfs4_schedule_lease_recovery(server->nfs_client);
1978 case -NFS4ERR_MOVED:
1979 nfs4_schedule_migration_recovery(server);
1981 case -NFS4ERR_LEASE_MOVED:
1982 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1984 case -NFS4ERR_DELEG_REVOKED:
1985 case -NFS4ERR_ADMIN_REVOKED:
1986 case -NFS4ERR_EXPIRED:
1987 case -NFS4ERR_BAD_STATEID:
1988 case -NFS4ERR_OPENMODE:
1989 nfs_inode_find_state_and_recover(state->inode,
1991 nfs4_schedule_stateid_recovery(server, state);
1993 case -NFS4ERR_DELAY:
1994 case -NFS4ERR_GRACE:
1995 set_bit(NFS_DELEGATED_STATE, &state->flags);
1999 case -NFS4ERR_DENIED:
2001 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2003 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2010 int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2011 struct nfs4_state *state, const nfs4_stateid *stateid,
2014 struct nfs_server *server = NFS_SERVER(state->inode);
2015 struct nfs4_opendata *opendata;
2018 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2019 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2020 if (IS_ERR(opendata))
2021 return PTR_ERR(opendata);
2022 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
2023 write_seqlock(&state->seqlock);
2024 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2025 write_sequnlock(&state->seqlock);
2026 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2027 switch (type & (FMODE_READ|FMODE_WRITE)) {
2028 case FMODE_READ|FMODE_WRITE:
2030 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2033 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2037 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2039 nfs4_opendata_put(opendata);
2040 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
2043 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2045 struct nfs4_opendata *data = calldata;
2047 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2048 &data->c_arg.seq_args, &data->c_res.seq_res, task);
2051 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2053 struct nfs4_opendata *data = calldata;
2055 nfs40_sequence_done(task, &data->c_res.seq_res);
2057 data->rpc_status = task->tk_status;
2058 if (data->rpc_status == 0) {
2059 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
2060 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2061 renew_lease(data->o_res.server, data->timestamp);
2062 data->rpc_done = true;
2066 static void nfs4_open_confirm_release(void *calldata)
2068 struct nfs4_opendata *data = calldata;
2069 struct nfs4_state *state = NULL;
2071 /* If this request hasn't been cancelled, do nothing */
2072 if (!data->cancelled)
2074 /* In case of error, no cleanup! */
2075 if (!data->rpc_done)
2077 state = nfs4_opendata_to_nfs4_state(data);
2079 nfs4_close_state(state, data->o_arg.fmode);
2081 nfs4_opendata_put(data);
2084 static const struct rpc_call_ops nfs4_open_confirm_ops = {
2085 .rpc_call_prepare = nfs4_open_confirm_prepare,
2086 .rpc_call_done = nfs4_open_confirm_done,
2087 .rpc_release = nfs4_open_confirm_release,
2091 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2093 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2095 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
2096 struct rpc_task *task;
2097 struct rpc_message msg = {
2098 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2099 .rpc_argp = &data->c_arg,
2100 .rpc_resp = &data->c_res,
2101 .rpc_cred = data->owner->so_cred,
2103 struct rpc_task_setup task_setup_data = {
2104 .rpc_client = server->client,
2105 .rpc_message = &msg,
2106 .callback_ops = &nfs4_open_confirm_ops,
2107 .callback_data = data,
2108 .workqueue = nfsiod_workqueue,
2109 .flags = RPC_TASK_ASYNC,
2113 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
2114 kref_get(&data->kref);
2115 data->rpc_done = false;
2116 data->rpc_status = 0;
2117 data->timestamp = jiffies;
2118 if (data->is_recover)
2119 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
2120 task = rpc_run_task(&task_setup_data);
2122 return PTR_ERR(task);
2123 status = rpc_wait_for_completion_task(task);
2125 data->cancelled = true;
2128 status = data->rpc_status;
2133 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
2135 struct nfs4_opendata *data = calldata;
2136 struct nfs4_state_owner *sp = data->owner;
2137 struct nfs_client *clp = sp->so_server->nfs_client;
2138 enum open_claim_type4 claim = data->o_arg.claim;
2140 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
2143 * Check if we still need to send an OPEN call, or if we can use
2144 * a delegation instead.
2146 if (data->state != NULL) {
2147 struct nfs_delegation *delegation;
2149 if (can_open_cached(data->state, data->o_arg.fmode,
2150 data->o_arg.open_flags, claim))
2153 delegation = nfs4_get_valid_delegation(data->state->inode);
2154 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
2155 goto unlock_no_action;
2158 /* Update client id. */
2159 data->o_arg.clientid = clp->cl_clientid;
2163 case NFS4_OPEN_CLAIM_PREVIOUS:
2164 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2165 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
2166 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
2167 case NFS4_OPEN_CLAIM_FH:
2168 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
2170 data->timestamp = jiffies;
2171 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
2172 &data->o_arg.seq_args,
2173 &data->o_res.seq_res,
2175 nfs_release_seqid(data->o_arg.seqid);
2177 /* Set the create mode (note dependency on the session type) */
2178 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2179 if (data->o_arg.open_flags & O_EXCL) {
2180 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2181 if (nfs4_has_persistent_session(clp))
2182 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2183 else if (clp->cl_mvops->minor_version > 0)
2184 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2188 trace_nfs4_cached_open(data->state);
2191 task->tk_action = NULL;
2193 nfs4_sequence_done(task, &data->o_res.seq_res);
2196 static void nfs4_open_done(struct rpc_task *task, void *calldata)
2198 struct nfs4_opendata *data = calldata;
2200 data->rpc_status = task->tk_status;
2202 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
2205 if (task->tk_status == 0) {
2206 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2207 switch (data->o_res.f_attr->mode & S_IFMT) {
2211 data->rpc_status = -ELOOP;
2214 data->rpc_status = -EISDIR;
2217 data->rpc_status = -ENOTDIR;
2220 renew_lease(data->o_res.server, data->timestamp);
2221 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2222 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2224 data->rpc_done = true;
2227 static void nfs4_open_release(void *calldata)
2229 struct nfs4_opendata *data = calldata;
2230 struct nfs4_state *state = NULL;
2232 /* If this request hasn't been cancelled, do nothing */
2233 if (!data->cancelled)
2235 /* In case of error, no cleanup! */
2236 if (data->rpc_status != 0 || !data->rpc_done)
2238 /* In case we need an open_confirm, no cleanup! */
2239 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2241 state = nfs4_opendata_to_nfs4_state(data);
2243 nfs4_close_state(state, data->o_arg.fmode);
2245 nfs4_opendata_put(data);
2248 static const struct rpc_call_ops nfs4_open_ops = {
2249 .rpc_call_prepare = nfs4_open_prepare,
2250 .rpc_call_done = nfs4_open_done,
2251 .rpc_release = nfs4_open_release,
2254 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
2256 struct inode *dir = d_inode(data->dir);
2257 struct nfs_server *server = NFS_SERVER(dir);
2258 struct nfs_openargs *o_arg = &data->o_arg;
2259 struct nfs_openres *o_res = &data->o_res;
2260 struct rpc_task *task;
2261 struct rpc_message msg = {
2262 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2265 .rpc_cred = data->owner->so_cred,
2267 struct rpc_task_setup task_setup_data = {
2268 .rpc_client = server->client,
2269 .rpc_message = &msg,
2270 .callback_ops = &nfs4_open_ops,
2271 .callback_data = data,
2272 .workqueue = nfsiod_workqueue,
2273 .flags = RPC_TASK_ASYNC,
2277 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
2278 kref_get(&data->kref);
2279 data->rpc_done = false;
2280 data->rpc_status = 0;
2281 data->cancelled = false;
2282 data->is_recover = false;
2284 nfs4_set_sequence_privileged(&o_arg->seq_args);
2285 data->is_recover = true;
2287 task = rpc_run_task(&task_setup_data);
2289 return PTR_ERR(task);
2290 status = rpc_wait_for_completion_task(task);
2292 data->cancelled = true;
2295 status = data->rpc_status;
2301 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2303 struct inode *dir = d_inode(data->dir);
2304 struct nfs_openres *o_res = &data->o_res;
2307 status = nfs4_run_open_task(data, 1);
2308 if (status != 0 || !data->rpc_done)
2311 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2313 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
2314 status = _nfs4_proc_open_confirm(data);
2320 * Additional permission checks in order to distinguish between an
2321 * open for read, and an open for execute. This works around the
2322 * fact that NFSv4 OPEN treats read and execute permissions as being
2324 * Note that in the non-execute case, we want to turn off permission
2325 * checking if we just created a new file (POSIX open() semantics).
2327 static int nfs4_opendata_access(struct rpc_cred *cred,
2328 struct nfs4_opendata *opendata,
2329 struct nfs4_state *state, fmode_t fmode,
2332 struct nfs_access_entry cache;
2335 /* access call failed or for some reason the server doesn't
2336 * support any access modes -- defer access call until later */
2337 if (opendata->o_res.access_supported == 0)
2342 * Use openflags to check for exec, because fmode won't
2343 * always have FMODE_EXEC set when file open for exec.
2345 if (openflags & __FMODE_EXEC) {
2346 /* ONLY check for exec rights */
2347 if (S_ISDIR(state->inode->i_mode))
2348 mask = NFS4_ACCESS_LOOKUP;
2350 mask = NFS4_ACCESS_EXECUTE;
2351 } else if ((fmode & FMODE_READ) && !opendata->file_created)
2352 mask = NFS4_ACCESS_READ;
2355 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2356 nfs_access_add_cache(state->inode, &cache);
2358 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2359 if ((mask & ~cache.mask & flags) == 0)
2366 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2368 static int _nfs4_proc_open(struct nfs4_opendata *data)
2370 struct inode *dir = d_inode(data->dir);
2371 struct nfs_server *server = NFS_SERVER(dir);
2372 struct nfs_openargs *o_arg = &data->o_arg;
2373 struct nfs_openres *o_res = &data->o_res;
2376 status = nfs4_run_open_task(data, 0);
2377 if (!data->rpc_done)
2380 if (status == -NFS4ERR_BADNAME &&
2381 !(o_arg->open_flags & O_CREAT))
2386 nfs_fattr_map_and_free_names(server, &data->f_attr);
2388 if (o_arg->open_flags & O_CREAT) {
2389 if (o_arg->open_flags & O_EXCL)
2390 data->file_created = true;
2391 else if (o_res->cinfo.before != o_res->cinfo.after)
2392 data->file_created = true;
2393 if (data->file_created || dir->i_version != o_res->cinfo.after)
2394 update_changeattr(dir, &o_res->cinfo,
2395 o_res->f_attr->time_start);
2397 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2398 server->caps &= ~NFS_CAP_POSIX_LOCK;
2399 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2400 status = _nfs4_proc_open_confirm(data);
2404 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2405 nfs4_sequence_free_slot(&o_res->seq_res);
2406 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
2413 * reclaim state on the server after a network partition.
2414 * Assumes caller holds the appropriate lock
2416 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2418 struct nfs4_opendata *opendata;
2421 opendata = nfs4_open_recoverdata_alloc(ctx, state, NFS4_OPEN_CLAIM_FH);
2422 if (IS_ERR(opendata))
2423 return PTR_ERR(opendata);
2425 * We're not recovering a delegation, so ask for no delegation.
2426 * Otherwise the recovery thread could deadlock with an outstanding
2427 * delegation return.
2429 opendata->o_arg.open_flags = O_DIRECT;
2430 ret = nfs4_open_recover(opendata, state);
2432 d_drop(ctx->dentry);
2433 nfs4_opendata_put(opendata);
2437 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2439 struct nfs_server *server = NFS_SERVER(state->inode);
2440 struct nfs4_exception exception = { };
2444 err = _nfs4_open_expired(ctx, state);
2445 trace_nfs4_open_expired(ctx, 0, err);
2446 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2451 case -NFS4ERR_GRACE:
2452 case -NFS4ERR_DELAY:
2453 nfs4_handle_exception(server, err, &exception);
2456 } while (exception.retry);
2461 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2463 struct nfs_open_context *ctx;
2466 ctx = nfs4_state_find_open_context(state);
2469 ret = nfs4_do_open_expired(ctx, state);
2470 put_nfs_open_context(ctx);
2474 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2475 const nfs4_stateid *stateid)
2477 nfs_remove_bad_delegation(state->inode, stateid);
2478 write_seqlock(&state->seqlock);
2479 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2480 write_sequnlock(&state->seqlock);
2481 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2484 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2486 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2487 nfs_finish_clear_delegation_stateid(state, NULL);
2490 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2492 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2493 nfs40_clear_delegation_stateid(state);
2494 return nfs4_open_expired(sp, state);
2497 static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2498 nfs4_stateid *stateid,
2499 struct rpc_cred *cred)
2501 return -NFS4ERR_BAD_STATEID;
2504 #if defined(CONFIG_NFS_V4_1)
2505 static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2506 nfs4_stateid *stateid,
2507 struct rpc_cred *cred)
2511 switch (stateid->type) {
2514 case NFS4_INVALID_STATEID_TYPE:
2515 case NFS4_SPECIAL_STATEID_TYPE:
2516 return -NFS4ERR_BAD_STATEID;
2517 case NFS4_REVOKED_STATEID_TYPE:
2521 status = nfs41_test_stateid(server, stateid, cred);
2523 case -NFS4ERR_EXPIRED:
2524 case -NFS4ERR_ADMIN_REVOKED:
2525 case -NFS4ERR_DELEG_REVOKED:
2531 /* Ack the revoked state to the server */
2532 nfs41_free_stateid(server, stateid, cred, true);
2533 return -NFS4ERR_EXPIRED;
2536 static void nfs41_check_delegation_stateid(struct nfs4_state *state)
2538 struct nfs_server *server = NFS_SERVER(state->inode);
2539 nfs4_stateid stateid;
2540 struct nfs_delegation *delegation;
2541 struct rpc_cred *cred;
2544 /* Get the delegation credential for use by test/free_stateid */
2546 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2547 if (delegation == NULL) {
2552 nfs4_stateid_copy(&stateid, &delegation->stateid);
2553 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
2555 nfs_finish_clear_delegation_stateid(state, &stateid);
2559 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2560 &delegation->flags)) {
2565 cred = get_rpccred(delegation->cred);
2567 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
2568 trace_nfs4_test_delegation_stateid(state, NULL, status);
2569 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
2570 nfs_finish_clear_delegation_stateid(state, &stateid);
2576 * nfs41_check_expired_locks - possibly free a lock stateid
2578 * @state: NFSv4 state for an inode
2580 * Returns NFS_OK if recovery for this stateid is now finished.
2581 * Otherwise a negative NFS4ERR value is returned.
2583 static int nfs41_check_expired_locks(struct nfs4_state *state)
2585 int status, ret = NFS_OK;
2586 struct nfs4_lock_state *lsp, *prev = NULL;
2587 struct nfs_server *server = NFS_SERVER(state->inode);
2589 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2592 spin_lock(&state->state_lock);
2593 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2594 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2595 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2597 atomic_inc(&lsp->ls_count);
2598 spin_unlock(&state->state_lock);
2600 nfs4_put_lock_state(prev);
2603 status = nfs41_test_and_free_expired_stateid(server,
2606 trace_nfs4_test_lock_stateid(state, lsp, status);
2607 if (status == -NFS4ERR_EXPIRED ||
2608 status == -NFS4ERR_BAD_STATEID) {
2609 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
2610 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
2611 if (!recover_lost_locks)
2612 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2613 } else if (status != NFS_OK) {
2615 nfs4_put_lock_state(prev);
2618 spin_lock(&state->state_lock);
2621 spin_unlock(&state->state_lock);
2622 nfs4_put_lock_state(prev);
2628 * nfs41_check_open_stateid - possibly free an open stateid
2630 * @state: NFSv4 state for an inode
2632 * Returns NFS_OK if recovery for this stateid is now finished.
2633 * Otherwise a negative NFS4ERR value is returned.
2635 static int nfs41_check_open_stateid(struct nfs4_state *state)
2637 struct nfs_server *server = NFS_SERVER(state->inode);
2638 nfs4_stateid *stateid = &state->open_stateid;
2639 struct rpc_cred *cred = state->owner->so_cred;
2642 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
2643 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2644 if (nfs4_have_delegation(state->inode, state->state))
2646 return -NFS4ERR_OPENMODE;
2648 return -NFS4ERR_BAD_STATEID;
2650 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
2651 trace_nfs4_test_open_stateid(state, NULL, status);
2652 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
2653 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2654 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2655 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2656 clear_bit(NFS_OPEN_STATE, &state->flags);
2657 stateid->type = NFS4_INVALID_STATEID_TYPE;
2660 if (nfs_open_stateid_recover_openmode(state))
2661 return -NFS4ERR_OPENMODE;
2665 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2669 nfs41_check_delegation_stateid(state);
2670 status = nfs41_check_expired_locks(state);
2671 if (status != NFS_OK)
2673 status = nfs41_check_open_stateid(state);
2674 if (status != NFS_OK)
2675 status = nfs4_open_expired(sp, state);
2681 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2682 * fields corresponding to attributes that were used to store the verifier.
2683 * Make sure we clobber those fields in the later setattr call
2685 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2686 struct iattr *sattr, struct nfs4_label **label)
2688 const u32 *attrset = opendata->o_res.attrset;
2690 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2691 !(sattr->ia_valid & ATTR_ATIME_SET))
2692 sattr->ia_valid |= ATTR_ATIME;
2694 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2695 !(sattr->ia_valid & ATTR_MTIME_SET))
2696 sattr->ia_valid |= ATTR_MTIME;
2698 /* Except MODE, it seems harmless of setting twice. */
2699 if (opendata->o_arg.createmode != NFS4_CREATE_EXCLUSIVE &&
2700 (attrset[1] & FATTR4_WORD1_MODE ||
2701 attrset[2] & FATTR4_WORD2_MODE_UMASK))
2702 sattr->ia_valid &= ~ATTR_MODE;
2704 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2708 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2711 struct nfs_open_context *ctx)
2713 struct nfs4_state_owner *sp = opendata->owner;
2714 struct nfs_server *server = sp->so_server;
2715 struct dentry *dentry;
2716 struct nfs4_state *state;
2720 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2722 ret = _nfs4_proc_open(opendata);
2726 state = _nfs4_opendata_to_nfs4_state(opendata);
2727 ret = PTR_ERR(state);
2731 if (server->caps & NFS_CAP_POSIX_LOCK)
2732 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2733 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2734 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
2736 dentry = opendata->dentry;
2737 if (d_really_is_negative(dentry)) {
2738 struct dentry *alias;
2740 alias = d_exact_alias(dentry, state->inode);
2742 alias = d_splice_alias(igrab(state->inode), dentry);
2743 /* d_splice_alias() can't fail here - it's a non-directory */
2746 ctx->dentry = dentry = alias;
2748 nfs_set_verifier(dentry,
2749 nfs_save_change_attribute(d_inode(opendata->dir)));
2752 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2756 if (d_inode(dentry) == state->inode) {
2757 nfs_inode_attach_open_context(ctx);
2758 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2759 nfs4_schedule_stateid_recovery(server, state);
2762 if (!opendata->cancelled)
2763 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
2768 * Returns a referenced nfs4_state
2770 static int _nfs4_do_open(struct inode *dir,
2771 struct nfs_open_context *ctx,
2773 const struct nfs4_open_createattrs *c,
2776 struct nfs4_state_owner *sp;
2777 struct nfs4_state *state = NULL;
2778 struct nfs_server *server = NFS_SERVER(dir);
2779 struct nfs4_opendata *opendata;
2780 struct dentry *dentry = ctx->dentry;
2781 struct rpc_cred *cred = ctx->cred;
2782 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2783 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
2784 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
2785 struct iattr *sattr = c->sattr;
2786 struct nfs4_label *label = c->label;
2787 struct nfs4_label *olabel = NULL;
2790 /* Protect against reboot recovery conflicts */
2792 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2794 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2797 status = nfs4_client_recover_expired_lease(server->nfs_client);
2799 goto err_put_state_owner;
2800 if (d_really_is_positive(dentry))
2801 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
2803 if (d_really_is_positive(dentry))
2804 claim = NFS4_OPEN_CLAIM_FH;
2805 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
2806 c, claim, GFP_KERNEL);
2807 if (opendata == NULL)
2808 goto err_put_state_owner;
2811 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2812 if (IS_ERR(olabel)) {
2813 status = PTR_ERR(olabel);
2814 goto err_opendata_put;
2818 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2819 if (!opendata->f_attr.mdsthreshold) {
2820 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2821 if (!opendata->f_attr.mdsthreshold)
2822 goto err_free_label;
2824 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
2826 if (d_really_is_positive(dentry))
2827 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
2829 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
2831 goto err_free_label;
2834 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
2835 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
2836 nfs4_exclusive_attrset(opendata, sattr, &label);
2838 * send create attributes which was not set by open
2839 * with an extra setattr.
2841 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2842 nfs_fattr_init(opendata->o_res.f_attr);
2843 status = nfs4_do_setattr(state->inode, cred,
2844 opendata->o_res.f_attr, sattr,
2845 ctx, label, olabel);
2847 nfs_setattr_update_inode(state->inode, sattr,
2848 opendata->o_res.f_attr);
2849 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2853 if (opened && opendata->file_created)
2854 *opened |= FILE_CREATED;
2856 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
2857 *ctx_th = opendata->f_attr.mdsthreshold;
2858 opendata->f_attr.mdsthreshold = NULL;
2861 nfs4_label_free(olabel);
2863 nfs4_opendata_put(opendata);
2864 nfs4_put_state_owner(sp);
2867 nfs4_label_free(olabel);
2869 nfs4_opendata_put(opendata);
2870 err_put_state_owner:
2871 nfs4_put_state_owner(sp);
2877 static struct nfs4_state *nfs4_do_open(struct inode *dir,
2878 struct nfs_open_context *ctx,
2880 struct iattr *sattr,
2881 struct nfs4_label *label,
2884 struct nfs_server *server = NFS_SERVER(dir);
2885 struct nfs4_exception exception = { };
2886 struct nfs4_state *res;
2887 struct nfs4_open_createattrs c = {
2891 [0] = (__u32)jiffies,
2892 [1] = (__u32)current->pid,
2898 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
2900 trace_nfs4_open_file(ctx, flags, status);
2903 /* NOTE: BAD_SEQID means the server and client disagree about the
2904 * book-keeping w.r.t. state-changing operations
2905 * (OPEN/CLOSE/LOCK/LOCKU...)
2906 * It is actually a sign of a bug on the client or on the server.
2908 * If we receive a BAD_SEQID error in the particular case of
2909 * doing an OPEN, we assume that nfs_increment_open_seqid() will
2910 * have unhashed the old state_owner for us, and that we can
2911 * therefore safely retry using a new one. We should still warn
2912 * the user though...
2914 if (status == -NFS4ERR_BAD_SEQID) {
2915 pr_warn_ratelimited("NFS: v4 server %s "
2916 " returned a bad sequence-id error!\n",
2917 NFS_SERVER(dir)->nfs_client->cl_hostname);
2918 exception.retry = 1;
2922 * BAD_STATEID on OPEN means that the server cancelled our
2923 * state before it received the OPEN_CONFIRM.
2924 * Recover by retrying the request as per the discussion
2925 * on Page 181 of RFC3530.
2927 if (status == -NFS4ERR_BAD_STATEID) {
2928 exception.retry = 1;
2931 if (status == -NFS4ERR_EXPIRED) {
2932 nfs4_schedule_lease_recovery(server->nfs_client);
2933 exception.retry = 1;
2936 if (status == -EAGAIN) {
2937 /* We must have found a delegation */
2938 exception.retry = 1;
2941 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2943 res = ERR_PTR(nfs4_handle_exception(server,
2944 status, &exception));
2945 } while (exception.retry);
2949 static int _nfs4_do_setattr(struct inode *inode,
2950 struct nfs_setattrargs *arg,
2951 struct nfs_setattrres *res,
2952 struct rpc_cred *cred,
2953 struct nfs_open_context *ctx)
2955 struct nfs_server *server = NFS_SERVER(inode);
2956 struct rpc_message msg = {
2957 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2962 struct rpc_cred *delegation_cred = NULL;
2963 unsigned long timestamp = jiffies;
2967 nfs_fattr_init(res->fattr);
2969 /* Servers should only apply open mode checks for file size changes */
2970 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
2974 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
2975 /* Use that stateid */
2976 } else if (ctx != NULL && ctx->state) {
2977 struct nfs_lock_context *l_ctx;
2978 if (!nfs4_valid_open_stateid(ctx->state))
2980 l_ctx = nfs_get_lock_context(ctx);
2982 return PTR_ERR(l_ctx);
2983 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
2984 &arg->stateid, &delegation_cred);
2985 nfs_put_lock_context(l_ctx);
2990 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
2992 if (delegation_cred)
2993 msg.rpc_cred = delegation_cred;
2995 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
2997 put_rpccred(delegation_cred);
2998 if (status == 0 && ctx != NULL)
2999 renew_lease(server, timestamp);
3000 trace_nfs4_setattr(inode, &arg->stateid, status);
3004 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
3005 struct nfs_fattr *fattr, struct iattr *sattr,
3006 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
3007 struct nfs4_label *olabel)
3009 struct nfs_server *server = NFS_SERVER(inode);
3010 struct nfs4_state *state = ctx ? ctx->state : NULL;
3011 struct nfs_setattrargs arg = {
3012 .fh = NFS_FH(inode),
3015 .bitmask = server->attr_bitmask,
3018 struct nfs_setattrres res = {
3023 struct nfs4_exception exception = {
3026 .stateid = &arg.stateid,
3030 arg.bitmask = nfs4_bitmask(server, ilabel);
3032 arg.bitmask = nfs4_bitmask(server, olabel);
3035 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
3037 case -NFS4ERR_OPENMODE:
3038 if (!(sattr->ia_valid & ATTR_SIZE)) {
3039 pr_warn_once("NFSv4: server %s is incorrectly "
3040 "applying open mode checks to "
3041 "a SETATTR that is not "
3042 "changing file size.\n",
3043 server->nfs_client->cl_hostname);
3045 if (state && !(state->state & FMODE_WRITE)) {
3047 if (sattr->ia_valid & ATTR_OPEN)
3052 err = nfs4_handle_exception(server, err, &exception);
3053 } while (exception.retry);
3059 nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3061 if (inode == NULL || !nfs_have_layout(inode))
3064 return pnfs_wait_on_layoutreturn(inode, task);
3067 struct nfs4_closedata {
3068 struct inode *inode;
3069 struct nfs4_state *state;
3070 struct nfs_closeargs arg;
3071 struct nfs_closeres res;
3073 struct nfs4_layoutreturn_args arg;
3074 struct nfs4_layoutreturn_res res;
3075 struct nfs4_xdr_opaque_data ld_private;
3079 struct nfs_fattr fattr;
3080 unsigned long timestamp;
3083 static void nfs4_free_closedata(void *data)
3085 struct nfs4_closedata *calldata = data;
3086 struct nfs4_state_owner *sp = calldata->state->owner;
3087 struct super_block *sb = calldata->state->inode->i_sb;
3089 if (calldata->lr.roc)
3090 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3091 calldata->res.lr_ret);
3092 nfs4_put_open_state(calldata->state);
3093 nfs_free_seqid(calldata->arg.seqid);
3094 nfs4_put_state_owner(sp);
3095 nfs_sb_deactive(sb);
3099 static void nfs4_close_done(struct rpc_task *task, void *data)
3101 struct nfs4_closedata *calldata = data;
3102 struct nfs4_state *state = calldata->state;
3103 struct nfs_server *server = NFS_SERVER(calldata->inode);
3104 nfs4_stateid *res_stateid = NULL;
3106 dprintk("%s: begin!\n", __func__);
3107 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3109 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
3111 /* Handle Layoutreturn errors */
3112 if (calldata->arg.lr_args && task->tk_status != 0) {
3113 switch (calldata->res.lr_ret) {
3115 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3118 calldata->arg.lr_args = NULL;
3119 calldata->res.lr_res = NULL;
3121 case -NFS4ERR_ADMIN_REVOKED:
3122 case -NFS4ERR_DELEG_REVOKED:
3123 case -NFS4ERR_EXPIRED:
3124 case -NFS4ERR_BAD_STATEID:
3125 case -NFS4ERR_OLD_STATEID:
3126 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3127 case -NFS4ERR_WRONG_CRED:
3128 calldata->arg.lr_args = NULL;
3129 calldata->res.lr_res = NULL;
3130 calldata->res.lr_ret = 0;
3131 rpc_restart_call_prepare(task);
3136 /* hmm. we are done with the inode, and in the process of freeing
3137 * the state_owner. we keep this around to process errors
3139 switch (task->tk_status) {
3141 res_stateid = &calldata->res.stateid;
3142 renew_lease(server, calldata->timestamp);
3144 case -NFS4ERR_ACCESS:
3145 if (calldata->arg.bitmask != NULL) {
3146 calldata->arg.bitmask = NULL;
3147 calldata->res.fattr = NULL;
3148 task->tk_status = 0;
3149 rpc_restart_call_prepare(task);
3154 case -NFS4ERR_ADMIN_REVOKED:
3155 case -NFS4ERR_STALE_STATEID:
3156 case -NFS4ERR_EXPIRED:
3157 nfs4_free_revoked_stateid(server,
3158 &calldata->arg.stateid,
3159 task->tk_msg.rpc_cred);
3160 case -NFS4ERR_OLD_STATEID:
3161 case -NFS4ERR_BAD_STATEID:
3162 if (!nfs4_stateid_match(&calldata->arg.stateid,
3163 &state->open_stateid)) {
3164 rpc_restart_call_prepare(task);
3167 if (calldata->arg.fmode == 0)
3170 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
3171 rpc_restart_call_prepare(task);
3175 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3176 res_stateid, calldata->arg.fmode);
3178 nfs_release_seqid(calldata->arg.seqid);
3179 nfs_refresh_inode(calldata->inode, &calldata->fattr);
3180 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
3183 static void nfs4_close_prepare(struct rpc_task *task, void *data)
3185 struct nfs4_closedata *calldata = data;
3186 struct nfs4_state *state = calldata->state;
3187 struct inode *inode = calldata->inode;
3188 bool is_rdonly, is_wronly, is_rdwr;
3191 dprintk("%s: begin!\n", __func__);
3192 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3195 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
3196 spin_lock(&state->owner->so_lock);
3197 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3198 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3199 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
3200 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
3201 /* Calculate the change in open mode */
3202 calldata->arg.fmode = 0;
3203 if (state->n_rdwr == 0) {
3204 if (state->n_rdonly == 0)
3205 call_close |= is_rdonly;
3207 calldata->arg.fmode |= FMODE_READ;
3208 if (state->n_wronly == 0)
3209 call_close |= is_wronly;
3211 calldata->arg.fmode |= FMODE_WRITE;
3212 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3213 call_close |= is_rdwr;
3215 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3217 if (!nfs4_valid_open_stateid(state) ||
3218 test_bit(NFS_OPEN_STATE, &state->flags) == 0)
3220 spin_unlock(&state->owner->so_lock);
3223 /* Note: exit _without_ calling nfs4_close_done */
3227 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
3228 nfs_release_seqid(calldata->arg.seqid);
3232 if (calldata->arg.fmode == 0)
3233 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
3235 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
3236 /* Close-to-open cache consistency revalidation */
3237 if (!nfs4_have_delegation(inode, FMODE_READ))
3238 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3240 calldata->arg.bitmask = NULL;
3243 calldata->arg.share_access =
3244 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3245 calldata->arg.fmode, 0);
3247 if (calldata->res.fattr == NULL)
3248 calldata->arg.bitmask = NULL;
3249 else if (calldata->arg.bitmask == NULL)
3250 calldata->res.fattr = NULL;
3251 calldata->timestamp = jiffies;
3252 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
3253 &calldata->arg.seq_args,
3254 &calldata->res.seq_res,
3256 nfs_release_seqid(calldata->arg.seqid);
3257 dprintk("%s: done!\n", __func__);
3260 task->tk_action = NULL;
3262 nfs4_sequence_done(task, &calldata->res.seq_res);
3265 static const struct rpc_call_ops nfs4_close_ops = {
3266 .rpc_call_prepare = nfs4_close_prepare,
3267 .rpc_call_done = nfs4_close_done,
3268 .rpc_release = nfs4_free_closedata,
3272 * It is possible for data to be read/written from a mem-mapped file
3273 * after the sys_close call (which hits the vfs layer as a flush).
3274 * This means that we can't safely call nfsv4 close on a file until
3275 * the inode is cleared. This in turn means that we are not good
3276 * NFSv4 citizens - we do not indicate to the server to update the file's
3277 * share state even when we are done with one of the three share
3278 * stateid's in the inode.
3280 * NOTE: Caller must be holding the sp->so_owner semaphore!
3282 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
3284 struct nfs_server *server = NFS_SERVER(state->inode);
3285 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
3286 struct nfs4_closedata *calldata;
3287 struct nfs4_state_owner *sp = state->owner;
3288 struct rpc_task *task;
3289 struct rpc_message msg = {
3290 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3291 .rpc_cred = state->owner->so_cred,
3293 struct rpc_task_setup task_setup_data = {
3294 .rpc_client = server->client,
3295 .rpc_message = &msg,
3296 .callback_ops = &nfs4_close_ops,
3297 .workqueue = nfsiod_workqueue,
3298 .flags = RPC_TASK_ASYNC,
3300 int status = -ENOMEM;
3302 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3303 &task_setup_data.rpc_client, &msg);
3305 calldata = kzalloc(sizeof(*calldata), gfp_mask);
3306 if (calldata == NULL)
3308 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
3309 calldata->inode = state->inode;
3310 calldata->state = state;
3311 calldata->arg.fh = NFS_FH(state->inode);
3312 /* Serialization for the sequence id */
3313 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3314 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
3315 if (IS_ERR(calldata->arg.seqid))
3316 goto out_free_calldata;
3317 nfs_fattr_init(&calldata->fattr);
3318 calldata->arg.fmode = 0;
3319 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
3320 calldata->res.fattr = &calldata->fattr;
3321 calldata->res.seqid = calldata->arg.seqid;
3322 calldata->res.server = server;
3323 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3324 calldata->lr.roc = pnfs_roc(state->inode,
3325 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3326 if (calldata->lr.roc) {
3327 calldata->arg.lr_args = &calldata->lr.arg;
3328 calldata->res.lr_res = &calldata->lr.res;
3330 nfs_sb_active(calldata->inode->i_sb);
3332 msg.rpc_argp = &calldata->arg;
3333 msg.rpc_resp = &calldata->res;
3334 task_setup_data.callback_data = calldata;
3335 task = rpc_run_task(&task_setup_data);
3337 return PTR_ERR(task);
3340 status = rpc_wait_for_completion_task(task);
3346 nfs4_put_open_state(state);
3347 nfs4_put_state_owner(sp);
3351 static struct inode *
3352 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3353 int open_flags, struct iattr *attr, int *opened)
3355 struct nfs4_state *state;
3356 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3358 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
3360 /* Protect against concurrent sillydeletes */
3361 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
3363 nfs4_label_release_security(label);
3366 return ERR_CAST(state);
3367 return state->inode;
3370 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
3372 if (ctx->state == NULL)
3375 nfs4_close_sync(ctx->state, ctx->mode);
3377 nfs4_close_state(ctx->state, ctx->mode);
3380 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3381 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3382 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
3384 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3386 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
3387 struct nfs4_server_caps_arg args = {
3391 struct nfs4_server_caps_res res = {};
3392 struct rpc_message msg = {
3393 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
3400 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3401 FATTR4_WORD0_FH_EXPIRE_TYPE |
3402 FATTR4_WORD0_LINK_SUPPORT |
3403 FATTR4_WORD0_SYMLINK_SUPPORT |
3404 FATTR4_WORD0_ACLSUPPORT;
3406 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3408 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3410 /* Sanity check the server answers */
3411 switch (minorversion) {
3413 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3414 res.attr_bitmask[2] = 0;
3417 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3420 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3422 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
3423 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3424 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3425 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3426 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
3427 NFS_CAP_CTIME|NFS_CAP_MTIME|
3428 NFS_CAP_SECURITY_LABEL);
3429 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3430 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3431 server->caps |= NFS_CAP_ACLS;
3432 if (res.has_links != 0)
3433 server->caps |= NFS_CAP_HARDLINKS;
3434 if (res.has_symlinks != 0)
3435 server->caps |= NFS_CAP_SYMLINKS;
3436 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3437 server->caps |= NFS_CAP_FILEID;
3438 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3439 server->caps |= NFS_CAP_MODE;
3440 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3441 server->caps |= NFS_CAP_NLINK;
3442 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3443 server->caps |= NFS_CAP_OWNER;
3444 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3445 server->caps |= NFS_CAP_OWNER_GROUP;
3446 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3447 server->caps |= NFS_CAP_ATIME;
3448 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3449 server->caps |= NFS_CAP_CTIME;
3450 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3451 server->caps |= NFS_CAP_MTIME;
3452 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3453 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3454 server->caps |= NFS_CAP_SECURITY_LABEL;
3456 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3457 sizeof(server->attr_bitmask));
3458 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3460 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3461 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3462 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
3463 server->cache_consistency_bitmask[2] = 0;
3465 /* Avoid a regression due to buggy server */
3466 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3467 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
3468 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3469 sizeof(server->exclcreat_bitmask));
3471 server->acl_bitmask = res.acl_bitmask;
3472 server->fh_expire_type = res.fh_expire_type;
3478 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3480 struct nfs4_exception exception = { };
3483 err = nfs4_handle_exception(server,
3484 _nfs4_server_capabilities(server, fhandle),
3486 } while (exception.retry);
3490 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3491 struct nfs_fsinfo *info)
3494 struct nfs4_lookup_root_arg args = {
3497 struct nfs4_lookup_res res = {
3499 .fattr = info->fattr,
3502 struct rpc_message msg = {
3503 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3508 bitmask[0] = nfs4_fattr_bitmap[0];
3509 bitmask[1] = nfs4_fattr_bitmap[1];
3511 * Process the label in the upcoming getfattr
3513 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3515 nfs_fattr_init(info->fattr);
3516 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3519 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3520 struct nfs_fsinfo *info)
3522 struct nfs4_exception exception = { };
3525 err = _nfs4_lookup_root(server, fhandle, info);
3526 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
3529 case -NFS4ERR_WRONGSEC:
3532 err = nfs4_handle_exception(server, err, &exception);
3534 } while (exception.retry);
3539 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3540 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3542 struct rpc_auth_create_args auth_args = {
3543 .pseudoflavor = flavor,
3545 struct rpc_auth *auth;
3547 auth = rpcauth_create(&auth_args, server->client);
3550 return nfs4_lookup_root(server, fhandle, info);
3554 * Retry pseudoroot lookup with various security flavors. We do this when:
3556 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3557 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3559 * Returns zero on success, or a negative NFS4ERR value, or a
3560 * negative errno value.
3562 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3563 struct nfs_fsinfo *info)
3565 /* Per 3530bis 15.33.5 */
3566 static const rpc_authflavor_t flav_array[] = {
3570 RPC_AUTH_UNIX, /* courtesy */
3573 int status = -EPERM;
3576 if (server->auth_info.flavor_len > 0) {
3577 /* try each flavor specified by user */
3578 for (i = 0; i < server->auth_info.flavor_len; i++) {
3579 status = nfs4_lookup_root_sec(server, fhandle, info,
3580 server->auth_info.flavors[i]);
3581 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3586 /* no flavors specified by user, try default list */
3587 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3588 status = nfs4_lookup_root_sec(server, fhandle, info,
3590 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3597 * -EACCESS could mean that the user doesn't have correct permissions
3598 * to access the mount. It could also mean that we tried to mount
3599 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3600 * existing mount programs don't handle -EACCES very well so it should
3601 * be mapped to -EPERM instead.
3603 if (status == -EACCES)
3609 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3610 * @server: initialized nfs_server handle
3611 * @fhandle: we fill in the pseudo-fs root file handle
3612 * @info: we fill in an FSINFO struct
3613 * @auth_probe: probe the auth flavours
3615 * Returns zero on success, or a negative errno.
3617 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
3618 struct nfs_fsinfo *info,
3624 status = nfs4_lookup_root(server, fhandle, info);
3626 if (auth_probe || status == NFS4ERR_WRONGSEC)
3627 status = server->nfs_client->cl_mvops->find_root_sec(server,
3631 status = nfs4_server_capabilities(server, fhandle);
3633 status = nfs4_do_fsinfo(server, fhandle, info);
3635 return nfs4_map_errors(status);
3638 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3639 struct nfs_fsinfo *info)
3642 struct nfs_fattr *fattr = info->fattr;
3643 struct nfs4_label *label = NULL;
3645 error = nfs4_server_capabilities(server, mntfh);
3647 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3651 label = nfs4_label_alloc(server, GFP_KERNEL);
3653 return PTR_ERR(label);
3655 error = nfs4_proc_getattr(server, mntfh, fattr, label);
3657 dprintk("nfs4_get_root: getattr error = %d\n", -error);
3658 goto err_free_label;
3661 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3662 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3663 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3666 nfs4_label_free(label);
3672 * Get locations and (maybe) other attributes of a referral.
3673 * Note that we'll actually follow the referral later when
3674 * we detect fsid mismatch in inode revalidation
3676 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3677 const struct qstr *name, struct nfs_fattr *fattr,
3678 struct nfs_fh *fhandle)
3680 int status = -ENOMEM;
3681 struct page *page = NULL;
3682 struct nfs4_fs_locations *locations = NULL;
3684 page = alloc_page(GFP_KERNEL);
3687 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3688 if (locations == NULL)
3691 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
3696 * If the fsid didn't change, this is a migration event, not a
3697 * referral. Cause us to drop into the exception handler, which
3698 * will kick off migration recovery.
3700 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
3701 dprintk("%s: server did not return a different fsid for"
3702 " a referral at %s\n", __func__, name->name);
3703 status = -NFS4ERR_MOVED;
3706 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3707 nfs_fixup_referral_attributes(&locations->fattr);
3709 /* replace the lookup nfs_fattr with the locations nfs_fattr */
3710 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
3711 memset(fhandle, 0, sizeof(struct nfs_fh));
3719 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3720 struct nfs_fattr *fattr, struct nfs4_label *label)
3722 struct nfs4_getattr_arg args = {
3724 .bitmask = server->attr_bitmask,
3726 struct nfs4_getattr_res res = {
3731 struct rpc_message msg = {
3732 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3737 args.bitmask = nfs4_bitmask(server, label);
3739 nfs_fattr_init(fattr);
3740 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3743 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3744 struct nfs_fattr *fattr, struct nfs4_label *label)
3746 struct nfs4_exception exception = { };
3749 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3750 trace_nfs4_getattr(server, fhandle, fattr, err);
3751 err = nfs4_handle_exception(server, err,
3753 } while (exception.retry);
3758 * The file is not closed if it is opened due to the a request to change
3759 * the size of the file. The open call will not be needed once the
3760 * VFS layer lookup-intents are implemented.
3762 * Close is called when the inode is destroyed.
3763 * If we haven't opened the file for O_WRONLY, we
3764 * need to in the size_change case to obtain a stateid.
3767 * Because OPEN is always done by name in nfsv4, it is
3768 * possible that we opened a different file by the same
3769 * name. We can recognize this race condition, but we
3770 * can't do anything about it besides returning an error.
3772 * This will be fixed with VFS changes (lookup-intent).
3775 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3776 struct iattr *sattr)
3778 struct inode *inode = d_inode(dentry);
3779 struct rpc_cred *cred = NULL;
3780 struct nfs_open_context *ctx = NULL;
3781 struct nfs4_label *label = NULL;
3784 if (pnfs_ld_layoutret_on_setattr(inode) &&
3785 sattr->ia_valid & ATTR_SIZE &&
3786 sattr->ia_size < i_size_read(inode))
3787 pnfs_commit_and_return_layout(inode);
3789 nfs_fattr_init(fattr);
3791 /* Deal with open(O_TRUNC) */
3792 if (sattr->ia_valid & ATTR_OPEN)
3793 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
3795 /* Optimization: if the end result is no change, don't RPC */
3796 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
3799 /* Search for an existing open(O_WRITE) file */
3800 if (sattr->ia_valid & ATTR_FILE) {
3802 ctx = nfs_file_open_context(sattr->ia_file);
3807 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3809 return PTR_ERR(label);
3811 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
3813 nfs_setattr_update_inode(inode, sattr, fattr);
3814 nfs_setsecurity(inode, fattr, label);
3816 nfs4_label_free(label);
3820 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3821 const struct qstr *name, struct nfs_fh *fhandle,
3822 struct nfs_fattr *fattr, struct nfs4_label *label)
3824 struct nfs_server *server = NFS_SERVER(dir);
3826 struct nfs4_lookup_arg args = {
3827 .bitmask = server->attr_bitmask,
3828 .dir_fh = NFS_FH(dir),
3831 struct nfs4_lookup_res res = {
3837 struct rpc_message msg = {
3838 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3843 args.bitmask = nfs4_bitmask(server, label);
3845 nfs_fattr_init(fattr);
3847 dprintk("NFS call lookup %s\n", name->name);
3848 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
3849 dprintk("NFS reply lookup: %d\n", status);
3853 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
3855 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
3856 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
3857 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3861 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3862 const struct qstr *name, struct nfs_fh *fhandle,
3863 struct nfs_fattr *fattr, struct nfs4_label *label)
3865 struct nfs4_exception exception = { };
3866 struct rpc_clnt *client = *clnt;
3869 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
3870 trace_nfs4_lookup(dir, name, err);
3872 case -NFS4ERR_BADNAME:
3875 case -NFS4ERR_MOVED:
3876 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
3877 if (err == -NFS4ERR_MOVED)
3878 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3880 case -NFS4ERR_WRONGSEC:
3882 if (client != *clnt)
3884 client = nfs4_negotiate_security(client, dir, name);
3886 return PTR_ERR(client);
3888 exception.retry = 1;
3891 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3893 } while (exception.retry);
3898 else if (client != *clnt)
3899 rpc_shutdown_client(client);
3904 static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
3905 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3906 struct nfs4_label *label)
3909 struct rpc_clnt *client = NFS_CLIENT(dir);
3911 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
3912 if (client != NFS_CLIENT(dir)) {
3913 rpc_shutdown_client(client);
3914 nfs_fixup_secinfo_attributes(fattr);
3920 nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
3921 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3923 struct rpc_clnt *client = NFS_CLIENT(dir);
3926 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
3928 return ERR_PTR(status);
3929 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
3932 static int _nfs4_proc_lookupp(struct inode *inode,
3933 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3934 struct nfs4_label *label)
3936 struct rpc_clnt *clnt = NFS_CLIENT(inode);
3937 struct nfs_server *server = NFS_SERVER(inode);
3939 struct nfs4_lookupp_arg args = {
3940 .bitmask = server->attr_bitmask,
3941 .fh = NFS_FH(inode),
3943 struct nfs4_lookupp_res res = {
3949 struct rpc_message msg = {
3950 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
3955 args.bitmask = nfs4_bitmask(server, label);
3957 nfs_fattr_init(fattr);
3959 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
3960 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
3962 dprintk("NFS reply lookupp: %d\n", status);
3966 static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
3967 struct nfs_fattr *fattr, struct nfs4_label *label)
3969 struct nfs4_exception exception = { };
3972 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
3973 trace_nfs4_lookupp(inode, err);
3974 err = nfs4_handle_exception(NFS_SERVER(inode), err,
3976 } while (exception.retry);
3980 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3982 struct nfs_server *server = NFS_SERVER(inode);
3983 struct nfs4_accessargs args = {
3984 .fh = NFS_FH(inode),
3985 .bitmask = server->cache_consistency_bitmask,
3987 struct nfs4_accessres res = {
3990 struct rpc_message msg = {
3991 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3994 .rpc_cred = entry->cred,
3996 int mode = entry->mask;
4000 * Determine which access bits we want to ask for...
4002 if (mode & MAY_READ)
4003 args.access |= NFS4_ACCESS_READ;
4004 if (S_ISDIR(inode->i_mode)) {
4005 if (mode & MAY_WRITE)
4006 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
4007 if (mode & MAY_EXEC)
4008 args.access |= NFS4_ACCESS_LOOKUP;
4010 if (mode & MAY_WRITE)
4011 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
4012 if (mode & MAY_EXEC)
4013 args.access |= NFS4_ACCESS_EXECUTE;
4016 res.fattr = nfs_alloc_fattr();
4017 if (res.fattr == NULL)
4020 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4022 nfs_access_set_mask(entry, res.access);
4023 nfs_refresh_inode(inode, res.fattr);
4025 nfs_free_fattr(res.fattr);
4029 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4031 struct nfs4_exception exception = { };
4034 err = _nfs4_proc_access(inode, entry);
4035 trace_nfs4_access(inode, err);
4036 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4038 } while (exception.retry);
4043 * TODO: For the time being, we don't try to get any attributes
4044 * along with any of the zero-copy operations READ, READDIR,
4047 * In the case of the first three, we want to put the GETATTR
4048 * after the read-type operation -- this is because it is hard
4049 * to predict the length of a GETATTR response in v4, and thus
4050 * align the READ data correctly. This means that the GETATTR
4051 * may end up partially falling into the page cache, and we should
4052 * shift it into the 'tail' of the xdr_buf before processing.
4053 * To do this efficiently, we need to know the total length
4054 * of data received, which doesn't seem to be available outside
4057 * In the case of WRITE, we also want to put the GETATTR after
4058 * the operation -- in this case because we want to make sure
4059 * we get the post-operation mtime and size.
4061 * Both of these changes to the XDR layer would in fact be quite
4062 * minor, but I decided to leave them for a subsequent patch.
4064 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4065 unsigned int pgbase, unsigned int pglen)
4067 struct nfs4_readlink args = {
4068 .fh = NFS_FH(inode),
4073 struct nfs4_readlink_res res;
4074 struct rpc_message msg = {
4075 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4080 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
4083 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4084 unsigned int pgbase, unsigned int pglen)
4086 struct nfs4_exception exception = { };
4089 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4090 trace_nfs4_readlink(inode, err);
4091 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4093 } while (exception.retry);
4098 * This is just for mknod. open(O_CREAT) will always do ->open_context().
4101 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
4104 struct nfs_server *server = NFS_SERVER(dir);
4105 struct nfs4_label l, *ilabel = NULL;
4106 struct nfs_open_context *ctx;
4107 struct nfs4_state *state;
4110 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
4112 return PTR_ERR(ctx);
4114 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4116 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4117 sattr->ia_mode &= ~current_umask();
4118 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
4119 if (IS_ERR(state)) {
4120 status = PTR_ERR(state);
4124 nfs4_label_release_security(ilabel);
4125 put_nfs_open_context(ctx);
4129 static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
4131 struct nfs_server *server = NFS_SERVER(dir);
4132 struct nfs_removeargs args = {
4136 struct nfs_removeres res = {
4139 struct rpc_message msg = {
4140 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4144 unsigned long timestamp = jiffies;
4147 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
4149 update_changeattr(dir, &res.cinfo, timestamp);
4153 static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
4155 struct nfs4_exception exception = { };
4158 err = _nfs4_proc_remove(dir, name);
4159 trace_nfs4_remove(dir, name, err);
4160 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4162 } while (exception.retry);
4166 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
4168 struct nfs_server *server = NFS_SERVER(dir);
4169 struct nfs_removeargs *args = msg->rpc_argp;
4170 struct nfs_removeres *res = msg->rpc_resp;
4172 res->server = server;
4173 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
4174 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
4176 nfs_fattr_init(res->dir_attr);
4179 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4181 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
4182 &data->args.seq_args,
4187 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
4189 struct nfs_unlinkdata *data = task->tk_calldata;
4190 struct nfs_removeres *res = &data->res;
4192 if (!nfs4_sequence_done(task, &res->seq_res))
4194 if (nfs4_async_handle_error(task, res->server, NULL,
4195 &data->timeout) == -EAGAIN)
4197 if (task->tk_status == 0)
4198 update_changeattr(dir, &res->cinfo, res->dir_attr->time_start);
4202 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4204 struct nfs_server *server = NFS_SERVER(dir);
4205 struct nfs_renameargs *arg = msg->rpc_argp;
4206 struct nfs_renameres *res = msg->rpc_resp;
4208 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
4209 res->server = server;
4210 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
4213 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4215 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
4216 &data->args.seq_args,
4221 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4222 struct inode *new_dir)
4224 struct nfs_renamedata *data = task->tk_calldata;
4225 struct nfs_renameres *res = &data->res;
4227 if (!nfs4_sequence_done(task, &res->seq_res))
4229 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
4232 if (task->tk_status == 0) {
4233 update_changeattr(old_dir, &res->old_cinfo, res->old_fattr->time_start);
4234 if (new_dir != old_dir)
4235 update_changeattr(new_dir, &res->new_cinfo, res->new_fattr->time_start);
4240 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4242 struct nfs_server *server = NFS_SERVER(inode);
4243 struct nfs4_link_arg arg = {
4244 .fh = NFS_FH(inode),
4245 .dir_fh = NFS_FH(dir),
4247 .bitmask = server->attr_bitmask,
4249 struct nfs4_link_res res = {
4253 struct rpc_message msg = {
4254 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4258 int status = -ENOMEM;
4260 res.fattr = nfs_alloc_fattr();
4261 if (res.fattr == NULL)
4264 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4265 if (IS_ERR(res.label)) {
4266 status = PTR_ERR(res.label);
4269 arg.bitmask = nfs4_bitmask(server, res.label);
4271 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4273 update_changeattr(dir, &res.cinfo, res.fattr->time_start);
4274 status = nfs_post_op_update_inode(inode, res.fattr);
4276 nfs_setsecurity(inode, res.fattr, res.label);
4280 nfs4_label_free(res.label);
4283 nfs_free_fattr(res.fattr);
4287 static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4289 struct nfs4_exception exception = { };
4292 err = nfs4_handle_exception(NFS_SERVER(inode),
4293 _nfs4_proc_link(inode, dir, name),
4295 } while (exception.retry);
4299 struct nfs4_createdata {
4300 struct rpc_message msg;
4301 struct nfs4_create_arg arg;
4302 struct nfs4_create_res res;
4304 struct nfs_fattr fattr;
4305 struct nfs4_label *label;
4308 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
4309 const struct qstr *name, struct iattr *sattr, u32 ftype)
4311 struct nfs4_createdata *data;
4313 data = kzalloc(sizeof(*data), GFP_KERNEL);
4315 struct nfs_server *server = NFS_SERVER(dir);
4317 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4318 if (IS_ERR(data->label))
4321 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4322 data->msg.rpc_argp = &data->arg;
4323 data->msg.rpc_resp = &data->res;
4324 data->arg.dir_fh = NFS_FH(dir);
4325 data->arg.server = server;
4326 data->arg.name = name;
4327 data->arg.attrs = sattr;
4328 data->arg.ftype = ftype;
4329 data->arg.bitmask = nfs4_bitmask(server, data->label);
4330 data->arg.umask = current_umask();
4331 data->res.server = server;
4332 data->res.fh = &data->fh;
4333 data->res.fattr = &data->fattr;
4334 data->res.label = data->label;
4335 nfs_fattr_init(data->res.fattr);
4343 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4345 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
4346 &data->arg.seq_args, &data->res.seq_res, 1);
4348 update_changeattr(dir, &data->res.dir_cinfo,
4349 data->res.fattr->time_start);
4350 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
4355 static void nfs4_free_createdata(struct nfs4_createdata *data)
4357 nfs4_label_free(data->label);
4361 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4362 struct page *page, unsigned int len, struct iattr *sattr,
4363 struct nfs4_label *label)
4365 struct nfs4_createdata *data;
4366 int status = -ENAMETOOLONG;
4368 if (len > NFS4_MAXPATHLEN)
4372 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4376 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4377 data->arg.u.symlink.pages = &page;
4378 data->arg.u.symlink.len = len;
4379 data->arg.label = label;
4381 status = nfs4_do_create(dir, dentry, data);
4383 nfs4_free_createdata(data);
4388 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4389 struct page *page, unsigned int len, struct iattr *sattr)
4391 struct nfs4_exception exception = { };
4392 struct nfs4_label l, *label = NULL;
4395 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4398 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4399 trace_nfs4_symlink(dir, &dentry->d_name, err);
4400 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4402 } while (exception.retry);
4404 nfs4_label_release_security(label);
4408 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4409 struct iattr *sattr, struct nfs4_label *label)
4411 struct nfs4_createdata *data;
4412 int status = -ENOMEM;
4414 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4418 data->arg.label = label;
4419 status = nfs4_do_create(dir, dentry, data);
4421 nfs4_free_createdata(data);
4426 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4427 struct iattr *sattr)
4429 struct nfs_server *server = NFS_SERVER(dir);
4430 struct nfs4_exception exception = { };
4431 struct nfs4_label l, *label = NULL;
4434 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4436 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4437 sattr->ia_mode &= ~current_umask();
4439 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4440 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4441 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4443 } while (exception.retry);
4444 nfs4_label_release_security(label);
4449 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
4450 u64 cookie, struct page **pages, unsigned int count, bool plus)
4452 struct inode *dir = d_inode(dentry);
4453 struct nfs_server *server = NFS_SERVER(dir);
4454 struct nfs4_readdir_arg args = {
4461 struct nfs4_readdir_res res;
4462 struct rpc_message msg = {
4463 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4470 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4472 (unsigned long long)cookie);
4473 if (!(server->caps & NFS_CAP_SECURITY_LABEL))
4474 args.bitmask = server->attr_bitmask_nl;
4476 args.bitmask = server->attr_bitmask;
4478 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
4479 res.pgbase = args.pgbase;
4480 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
4483 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
4484 status += args.pgbase;
4487 nfs_invalidate_atime(dir);
4489 dprintk("%s: returns %d\n", __func__, status);
4493 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
4494 u64 cookie, struct page **pages, unsigned int count, bool plus)
4496 struct nfs4_exception exception = { };
4499 err = _nfs4_proc_readdir(dentry, cred, cookie,
4500 pages, count, plus);
4501 trace_nfs4_readdir(d_inode(dentry), err);
4502 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
4504 } while (exception.retry);
4508 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4509 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
4511 struct nfs4_createdata *data;
4512 int mode = sattr->ia_mode;
4513 int status = -ENOMEM;
4515 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4520 data->arg.ftype = NF4FIFO;
4521 else if (S_ISBLK(mode)) {
4522 data->arg.ftype = NF4BLK;
4523 data->arg.u.device.specdata1 = MAJOR(rdev);
4524 data->arg.u.device.specdata2 = MINOR(rdev);
4526 else if (S_ISCHR(mode)) {
4527 data->arg.ftype = NF4CHR;
4528 data->arg.u.device.specdata1 = MAJOR(rdev);
4529 data->arg.u.device.specdata2 = MINOR(rdev);
4530 } else if (!S_ISSOCK(mode)) {
4535 data->arg.label = label;
4536 status = nfs4_do_create(dir, dentry, data);
4538 nfs4_free_createdata(data);
4543 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4544 struct iattr *sattr, dev_t rdev)
4546 struct nfs_server *server = NFS_SERVER(dir);
4547 struct nfs4_exception exception = { };
4548 struct nfs4_label l, *label = NULL;
4551 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4553 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4554 sattr->ia_mode &= ~current_umask();
4556 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4557 trace_nfs4_mknod(dir, &dentry->d_name, err);
4558 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4560 } while (exception.retry);
4562 nfs4_label_release_security(label);
4567 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4568 struct nfs_fsstat *fsstat)
4570 struct nfs4_statfs_arg args = {
4572 .bitmask = server->attr_bitmask,
4574 struct nfs4_statfs_res res = {
4577 struct rpc_message msg = {
4578 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4583 nfs_fattr_init(fsstat->fattr);
4584 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4587 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4589 struct nfs4_exception exception = { };
4592 err = nfs4_handle_exception(server,
4593 _nfs4_proc_statfs(server, fhandle, fsstat),
4595 } while (exception.retry);
4599 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4600 struct nfs_fsinfo *fsinfo)
4602 struct nfs4_fsinfo_arg args = {
4604 .bitmask = server->attr_bitmask,
4606 struct nfs4_fsinfo_res res = {
4609 struct rpc_message msg = {
4610 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4615 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4618 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4620 struct nfs4_exception exception = { };
4621 unsigned long now = jiffies;
4625 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
4626 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
4628 nfs4_set_lease_period(server->nfs_client,
4629 fsinfo->lease_time * HZ,
4633 err = nfs4_handle_exception(server, err, &exception);
4634 } while (exception.retry);
4638 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4642 nfs_fattr_init(fsinfo->fattr);
4643 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
4645 /* block layout checks this! */
4646 server->pnfs_blksize = fsinfo->blksize;
4647 set_pnfs_layoutdriver(server, fhandle, fsinfo);
4653 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4654 struct nfs_pathconf *pathconf)
4656 struct nfs4_pathconf_arg args = {
4658 .bitmask = server->attr_bitmask,
4660 struct nfs4_pathconf_res res = {
4661 .pathconf = pathconf,
4663 struct rpc_message msg = {
4664 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4669 /* None of the pathconf attributes are mandatory to implement */
4670 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4671 memset(pathconf, 0, sizeof(*pathconf));
4675 nfs_fattr_init(pathconf->fattr);
4676 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4679 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4680 struct nfs_pathconf *pathconf)
4682 struct nfs4_exception exception = { };
4686 err = nfs4_handle_exception(server,
4687 _nfs4_proc_pathconf(server, fhandle, pathconf),
4689 } while (exception.retry);
4693 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
4694 const struct nfs_open_context *ctx,
4695 const struct nfs_lock_context *l_ctx,
4698 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
4700 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4702 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4703 const struct nfs_open_context *ctx,
4704 const struct nfs_lock_context *l_ctx,
4707 nfs4_stateid current_stateid;
4709 /* If the current stateid represents a lost lock, then exit */
4710 if (nfs4_set_rw_stateid(¤t_stateid, ctx, l_ctx, fmode) == -EIO)
4712 return nfs4_stateid_match(stateid, ¤t_stateid);
4715 static bool nfs4_error_stateid_expired(int err)
4718 case -NFS4ERR_DELEG_REVOKED:
4719 case -NFS4ERR_ADMIN_REVOKED:
4720 case -NFS4ERR_BAD_STATEID:
4721 case -NFS4ERR_STALE_STATEID:
4722 case -NFS4ERR_OLD_STATEID:
4723 case -NFS4ERR_OPENMODE:
4724 case -NFS4ERR_EXPIRED:
4730 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
4732 struct nfs_server *server = NFS_SERVER(hdr->inode);
4734 trace_nfs4_read(hdr, task->tk_status);
4735 if (task->tk_status < 0) {
4736 struct nfs4_exception exception = {
4737 .inode = hdr->inode,
4738 .state = hdr->args.context->state,
4739 .stateid = &hdr->args.stateid,
4741 task->tk_status = nfs4_async_handle_exception(task,
4742 server, task->tk_status, &exception);
4743 if (exception.retry) {
4744 rpc_restart_call_prepare(task);
4749 if (task->tk_status > 0)
4750 renew_lease(server, hdr->timestamp);
4754 static bool nfs4_read_stateid_changed(struct rpc_task *task,
4755 struct nfs_pgio_args *args)
4758 if (!nfs4_error_stateid_expired(task->tk_status) ||
4759 nfs4_stateid_is_current(&args->stateid,
4764 rpc_restart_call_prepare(task);
4768 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
4771 dprintk("--> %s\n", __func__);
4773 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
4775 if (nfs4_read_stateid_changed(task, &hdr->args))
4777 if (task->tk_status > 0)
4778 nfs_invalidate_atime(hdr->inode);
4779 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4780 nfs4_read_done_cb(task, hdr);
4783 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4784 struct rpc_message *msg)
4786 hdr->timestamp = jiffies;
4787 if (!hdr->pgio_done_cb)
4788 hdr->pgio_done_cb = nfs4_read_done_cb;
4789 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
4790 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
4793 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4794 struct nfs_pgio_header *hdr)
4796 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
4797 &hdr->args.seq_args,
4801 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4802 hdr->args.lock_context,
4803 hdr->rw_mode) == -EIO)
4805 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
4810 static int nfs4_write_done_cb(struct rpc_task *task,
4811 struct nfs_pgio_header *hdr)
4813 struct inode *inode = hdr->inode;
4815 trace_nfs4_write(hdr, task->tk_status);
4816 if (task->tk_status < 0) {
4817 struct nfs4_exception exception = {
4818 .inode = hdr->inode,
4819 .state = hdr->args.context->state,
4820 .stateid = &hdr->args.stateid,
4822 task->tk_status = nfs4_async_handle_exception(task,
4823 NFS_SERVER(inode), task->tk_status,
4825 if (exception.retry) {
4826 rpc_restart_call_prepare(task);
4830 if (task->tk_status >= 0) {
4831 renew_lease(NFS_SERVER(inode), hdr->timestamp);
4832 nfs_writeback_update_inode(hdr);
4837 static bool nfs4_write_stateid_changed(struct rpc_task *task,
4838 struct nfs_pgio_args *args)
4841 if (!nfs4_error_stateid_expired(task->tk_status) ||
4842 nfs4_stateid_is_current(&args->stateid,
4847 rpc_restart_call_prepare(task);
4851 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
4853 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
4855 if (nfs4_write_stateid_changed(task, &hdr->args))
4857 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4858 nfs4_write_done_cb(task, hdr);
4862 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
4864 /* Don't request attributes for pNFS or O_DIRECT writes */
4865 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
4867 /* Otherwise, request attributes if and only if we don't hold
4870 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
4873 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4874 struct rpc_message *msg)
4876 struct nfs_server *server = NFS_SERVER(hdr->inode);
4878 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4879 hdr->args.bitmask = NULL;
4880 hdr->res.fattr = NULL;
4882 hdr->args.bitmask = server->cache_consistency_bitmask;
4884 if (!hdr->pgio_done_cb)
4885 hdr->pgio_done_cb = nfs4_write_done_cb;
4886 hdr->res.server = server;
4887 hdr->timestamp = jiffies;
4889 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
4890 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
4893 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4895 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
4896 &data->args.seq_args,
4901 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
4903 struct inode *inode = data->inode;
4905 trace_nfs4_commit(data, task->tk_status);
4906 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4907 NULL, NULL) == -EAGAIN) {
4908 rpc_restart_call_prepare(task);
4914 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
4916 if (!nfs4_sequence_done(task, &data->res.seq_res))
4918 return data->commit_done_cb(task, data);
4921 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
4923 struct nfs_server *server = NFS_SERVER(data->inode);
4925 if (data->commit_done_cb == NULL)
4926 data->commit_done_cb = nfs4_commit_done_cb;
4927 data->res.server = server;
4928 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
4929 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4932 struct nfs4_renewdata {
4933 struct nfs_client *client;
4934 unsigned long timestamp;
4938 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4939 * standalone procedure for queueing an asynchronous RENEW.
4941 static void nfs4_renew_release(void *calldata)
4943 struct nfs4_renewdata *data = calldata;
4944 struct nfs_client *clp = data->client;
4946 if (atomic_read(&clp->cl_count) > 1)
4947 nfs4_schedule_state_renewal(clp);
4948 nfs_put_client(clp);
4952 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
4954 struct nfs4_renewdata *data = calldata;
4955 struct nfs_client *clp = data->client;
4956 unsigned long timestamp = data->timestamp;
4958 trace_nfs4_renew_async(clp, task->tk_status);
4959 switch (task->tk_status) {
4962 case -NFS4ERR_LEASE_MOVED:
4963 nfs4_schedule_lease_moved_recovery(clp);
4966 /* Unless we're shutting down, schedule state recovery! */
4967 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4969 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
4970 nfs4_schedule_lease_recovery(clp);
4973 nfs4_schedule_path_down_recovery(clp);
4975 do_renew_lease(clp, timestamp);
4978 static const struct rpc_call_ops nfs4_renew_ops = {
4979 .rpc_call_done = nfs4_renew_done,
4980 .rpc_release = nfs4_renew_release,
4983 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
4985 struct rpc_message msg = {
4986 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4990 struct nfs4_renewdata *data;
4992 if (renew_flags == 0)
4994 if (!atomic_inc_not_zero(&clp->cl_count))
4996 data = kmalloc(sizeof(*data), GFP_NOFS);
4998 nfs_put_client(clp);
5002 data->timestamp = jiffies;
5003 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
5004 &nfs4_renew_ops, data);
5007 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
5009 struct rpc_message msg = {
5010 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5014 unsigned long now = jiffies;
5017 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5020 do_renew_lease(clp, now);
5024 static inline int nfs4_server_supports_acls(struct nfs_server *server)
5026 return server->caps & NFS_CAP_ACLS;
5029 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5030 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
5033 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
5035 static int buf_to_pages_noslab(const void *buf, size_t buflen,
5036 struct page **pages)
5038 struct page *newpage, **spages;
5044 len = min_t(size_t, PAGE_SIZE, buflen);
5045 newpage = alloc_page(GFP_KERNEL);
5047 if (newpage == NULL)
5049 memcpy(page_address(newpage), buf, len);
5054 } while (buflen != 0);
5060 __free_page(spages[rc-1]);
5064 struct nfs4_cached_acl {
5070 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
5072 struct nfs_inode *nfsi = NFS_I(inode);
5074 spin_lock(&inode->i_lock);
5075 kfree(nfsi->nfs4_acl);
5076 nfsi->nfs4_acl = acl;
5077 spin_unlock(&inode->i_lock);
5080 static void nfs4_zap_acl_attr(struct inode *inode)
5082 nfs4_set_cached_acl(inode, NULL);
5085 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5087 struct nfs_inode *nfsi = NFS_I(inode);
5088 struct nfs4_cached_acl *acl;
5091 spin_lock(&inode->i_lock);
5092 acl = nfsi->nfs4_acl;
5095 if (buf == NULL) /* user is just asking for length */
5097 if (acl->cached == 0)
5099 ret = -ERANGE; /* see getxattr(2) man page */
5100 if (acl->len > buflen)
5102 memcpy(buf, acl->data, acl->len);
5106 spin_unlock(&inode->i_lock);
5110 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
5112 struct nfs4_cached_acl *acl;
5113 size_t buflen = sizeof(*acl) + acl_len;
5115 if (buflen <= PAGE_SIZE) {
5116 acl = kmalloc(buflen, GFP_KERNEL);
5120 _copy_from_pages(acl->data, pages, pgbase, acl_len);
5122 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5129 nfs4_set_cached_acl(inode, acl);
5133 * The getxattr API returns the required buffer length when called with a
5134 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5135 * the required buf. On a NULL buf, we send a page of data to the server
5136 * guessing that the ACL request can be serviced by a page. If so, we cache
5137 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5138 * the cache. If not so, we throw away the page, and cache the required
5139 * length. The next getxattr call will then produce another round trip to
5140 * the server, this time with the input buf of the required size.
5142 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5144 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
5145 struct nfs_getaclargs args = {
5146 .fh = NFS_FH(inode),
5150 struct nfs_getaclres res = {
5153 struct rpc_message msg = {
5154 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5158 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
5159 int ret = -ENOMEM, i;
5161 if (npages > ARRAY_SIZE(pages))
5164 for (i = 0; i < npages; i++) {
5165 pages[i] = alloc_page(GFP_KERNEL);
5170 /* for decoding across pages */
5171 res.acl_scratch = alloc_page(GFP_KERNEL);
5172 if (!res.acl_scratch)
5175 args.acl_len = npages * PAGE_SIZE;
5177 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
5178 __func__, buf, buflen, npages, args.acl_len);
5179 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5180 &msg, &args.seq_args, &res.seq_res, 0);
5184 /* Handle the case where the passed-in buffer is too short */
5185 if (res.acl_flags & NFS4_ACL_TRUNC) {
5186 /* Did the user only issue a request for the acl length? */
5192 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
5194 if (res.acl_len > buflen) {
5198 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
5203 for (i = 0; i < npages; i++)
5205 __free_page(pages[i]);
5206 if (res.acl_scratch)
5207 __free_page(res.acl_scratch);
5211 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5213 struct nfs4_exception exception = { };
5216 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
5217 trace_nfs4_get_acl(inode, ret);
5220 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5221 } while (exception.retry);
5225 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5227 struct nfs_server *server = NFS_SERVER(inode);
5230 if (!nfs4_server_supports_acls(server))
5232 ret = nfs_revalidate_inode(server, inode);
5235 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5236 nfs_zap_acl_cache(inode);
5237 ret = nfs4_read_cached_acl(inode, buf, buflen);
5239 /* -ENOENT is returned if there is no ACL or if there is an ACL
5240 * but no cached acl data, just the acl length */
5242 return nfs4_get_acl_uncached(inode, buf, buflen);
5245 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5247 struct nfs_server *server = NFS_SERVER(inode);
5248 struct page *pages[NFS4ACL_MAXPAGES];
5249 struct nfs_setaclargs arg = {
5250 .fh = NFS_FH(inode),
5254 struct nfs_setaclres res;
5255 struct rpc_message msg = {
5256 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5260 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
5263 /* You can't remove system.nfs4_acl: */
5266 if (!nfs4_server_supports_acls(server))
5268 if (npages > ARRAY_SIZE(pages))
5270 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
5273 nfs4_inode_return_delegation(inode);
5274 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5277 * Free each page after tx, so the only ref left is
5278 * held by the network stack
5281 put_page(pages[i-1]);
5284 * Acl update can result in inode attribute update.
5285 * so mark the attribute cache invalid.
5287 spin_lock(&inode->i_lock);
5288 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5289 spin_unlock(&inode->i_lock);
5290 nfs_access_zap_cache(inode);
5291 nfs_zap_acl_cache(inode);
5295 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5297 struct nfs4_exception exception = { };
5300 err = __nfs4_proc_set_acl(inode, buf, buflen);
5301 trace_nfs4_set_acl(inode, err);
5302 if (err == -NFS4ERR_BADOWNER || err == -NFS4ERR_BADNAME) {
5304 * no need to retry since the kernel
5305 * isn't involved in encoding the ACEs.
5310 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5312 } while (exception.retry);
5316 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
5317 static int _nfs4_get_security_label(struct inode *inode, void *buf,
5320 struct nfs_server *server = NFS_SERVER(inode);
5321 struct nfs_fattr fattr;
5322 struct nfs4_label label = {0, 0, buflen, buf};
5324 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5325 struct nfs4_getattr_arg arg = {
5326 .fh = NFS_FH(inode),
5329 struct nfs4_getattr_res res = {
5334 struct rpc_message msg = {
5335 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
5341 nfs_fattr_init(&fattr);
5343 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
5346 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5351 static int nfs4_get_security_label(struct inode *inode, void *buf,
5354 struct nfs4_exception exception = { };
5357 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5361 err = _nfs4_get_security_label(inode, buf, buflen);
5362 trace_nfs4_get_security_label(inode, err);
5363 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5365 } while (exception.retry);
5369 static int _nfs4_do_set_security_label(struct inode *inode,
5370 struct nfs4_label *ilabel,
5371 struct nfs_fattr *fattr,
5372 struct nfs4_label *olabel)
5375 struct iattr sattr = {0};
5376 struct nfs_server *server = NFS_SERVER(inode);
5377 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5378 struct nfs_setattrargs arg = {
5379 .fh = NFS_FH(inode),
5385 struct nfs_setattrres res = {
5390 struct rpc_message msg = {
5391 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5397 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
5399 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5401 dprintk("%s failed: %d\n", __func__, status);
5406 static int nfs4_do_set_security_label(struct inode *inode,
5407 struct nfs4_label *ilabel,
5408 struct nfs_fattr *fattr,
5409 struct nfs4_label *olabel)
5411 struct nfs4_exception exception = { };
5415 err = _nfs4_do_set_security_label(inode, ilabel,
5417 trace_nfs4_set_security_label(inode, err);
5418 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5420 } while (exception.retry);
5425 nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
5427 struct nfs4_label ilabel, *olabel = NULL;
5428 struct nfs_fattr fattr;
5429 struct rpc_cred *cred;
5432 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5435 nfs_fattr_init(&fattr);
5439 ilabel.label = (char *)buf;
5440 ilabel.len = buflen;
5442 cred = rpc_lookup_cred();
5444 return PTR_ERR(cred);
5446 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5447 if (IS_ERR(olabel)) {
5448 status = -PTR_ERR(olabel);
5452 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5454 nfs_setsecurity(inode, &fattr, olabel);
5456 nfs4_label_free(olabel);
5461 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5464 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5465 nfs4_verifier *bootverf)
5469 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5470 /* An impossible timestamp guarantees this value
5471 * will never match a generated boot time. */
5472 verf[0] = cpu_to_be32(U32_MAX);
5473 verf[1] = cpu_to_be32(U32_MAX);
5475 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
5476 u64 ns = ktime_to_ns(nn->boot_time);
5478 verf[0] = cpu_to_be32(ns >> 32);
5479 verf[1] = cpu_to_be32(ns);
5481 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5485 nfs4_init_nonuniform_client_string(struct nfs_client *clp)
5490 if (clp->cl_owner_id != NULL)
5494 len = 14 + strlen(clp->cl_ipaddr) + 1 +
5495 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5497 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5501 if (len > NFS4_OPAQUE_LIMIT + 1)
5505 * Since this string is allocated at mount time, and held until the
5506 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5507 * about a memory-reclaim deadlock.
5509 str = kmalloc(len, GFP_KERNEL);
5514 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
5516 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5517 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
5520 clp->cl_owner_id = str;
5525 nfs4_init_uniquifier_client_string(struct nfs_client *clp)
5530 len = 10 + 10 + 1 + 10 + 1 +
5531 strlen(nfs4_client_id_uniquifier) + 1 +
5532 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5534 if (len > NFS4_OPAQUE_LIMIT + 1)
5538 * Since this string is allocated at mount time, and held until the
5539 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5540 * about a memory-reclaim deadlock.
5542 str = kmalloc(len, GFP_KERNEL);
5546 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
5547 clp->rpc_ops->version, clp->cl_minorversion,
5548 nfs4_client_id_uniquifier,
5549 clp->cl_rpcclient->cl_nodename);
5550 clp->cl_owner_id = str;
5555 nfs4_init_uniform_client_string(struct nfs_client *clp)
5560 if (clp->cl_owner_id != NULL)
5563 if (nfs4_client_id_uniquifier[0] != '\0')
5564 return nfs4_init_uniquifier_client_string(clp);
5566 len = 10 + 10 + 1 + 10 + 1 +
5567 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5569 if (len > NFS4_OPAQUE_LIMIT + 1)
5573 * Since this string is allocated at mount time, and held until the
5574 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5575 * about a memory-reclaim deadlock.
5577 str = kmalloc(len, GFP_KERNEL);
5581 scnprintf(str, len, "Linux NFSv%u.%u %s",
5582 clp->rpc_ops->version, clp->cl_minorversion,
5583 clp->cl_rpcclient->cl_nodename);
5584 clp->cl_owner_id = str;
5589 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5590 * services. Advertise one based on the address family of the
5594 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5596 if (strchr(clp->cl_ipaddr, ':') != NULL)
5597 return scnprintf(buf, len, "tcp6");
5599 return scnprintf(buf, len, "tcp");
5602 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5604 struct nfs4_setclientid *sc = calldata;
5606 if (task->tk_status == 0)
5607 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5610 static const struct rpc_call_ops nfs4_setclientid_ops = {
5611 .rpc_call_done = nfs4_setclientid_done,
5615 * nfs4_proc_setclientid - Negotiate client ID
5616 * @clp: state data structure
5617 * @program: RPC program for NFSv4 callback service
5618 * @port: IP port number for NFS4 callback service
5619 * @cred: RPC credential to use for this call
5620 * @res: where to place the result
5622 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5624 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5625 unsigned short port, struct rpc_cred *cred,
5626 struct nfs4_setclientid_res *res)
5628 nfs4_verifier sc_verifier;
5629 struct nfs4_setclientid setclientid = {
5630 .sc_verifier = &sc_verifier,
5634 struct rpc_message msg = {
5635 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5636 .rpc_argp = &setclientid,
5640 struct rpc_task *task;
5641 struct rpc_task_setup task_setup_data = {
5642 .rpc_client = clp->cl_rpcclient,
5643 .rpc_message = &msg,
5644 .callback_ops = &nfs4_setclientid_ops,
5645 .callback_data = &setclientid,
5646 .flags = RPC_TASK_TIMEOUT,
5650 /* nfs_client_id4 */
5651 nfs4_init_boot_verifier(clp, &sc_verifier);
5653 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5654 status = nfs4_init_uniform_client_string(clp);
5656 status = nfs4_init_nonuniform_client_string(clp);
5662 setclientid.sc_netid_len =
5663 nfs4_init_callback_netid(clp,
5664 setclientid.sc_netid,
5665 sizeof(setclientid.sc_netid));
5666 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
5667 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
5668 clp->cl_ipaddr, port >> 8, port & 255);
5670 dprintk("NFS call setclientid auth=%s, '%s'\n",
5671 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5673 task = rpc_run_task(&task_setup_data);
5675 status = PTR_ERR(task);
5678 status = task->tk_status;
5679 if (setclientid.sc_cred) {
5680 kfree(clp->cl_acceptor);
5681 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5682 put_rpccred(setclientid.sc_cred);
5686 trace_nfs4_setclientid(clp, status);
5687 dprintk("NFS reply setclientid: %d\n", status);
5692 * nfs4_proc_setclientid_confirm - Confirm client ID
5693 * @clp: state data structure
5694 * @res: result of a previous SETCLIENTID
5695 * @cred: RPC credential to use for this call
5697 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5699 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
5700 struct nfs4_setclientid_res *arg,
5701 struct rpc_cred *cred)
5703 struct rpc_message msg = {
5704 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
5710 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5711 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5713 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5714 trace_nfs4_setclientid_confirm(clp, status);
5715 dprintk("NFS reply setclientid_confirm: %d\n", status);
5719 struct nfs4_delegreturndata {
5720 struct nfs4_delegreturnargs args;
5721 struct nfs4_delegreturnres res;
5723 nfs4_stateid stateid;
5724 unsigned long timestamp;
5726 struct nfs4_layoutreturn_args arg;
5727 struct nfs4_layoutreturn_res res;
5728 struct nfs4_xdr_opaque_data ld_private;
5732 struct nfs_fattr fattr;
5734 struct inode *inode;
5737 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5739 struct nfs4_delegreturndata *data = calldata;
5741 if (!nfs4_sequence_done(task, &data->res.seq_res))
5744 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
5746 /* Handle Layoutreturn errors */
5747 if (data->args.lr_args && task->tk_status != 0) {
5748 switch(data->res.lr_ret) {
5750 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5753 data->args.lr_args = NULL;
5754 data->res.lr_res = NULL;
5756 case -NFS4ERR_ADMIN_REVOKED:
5757 case -NFS4ERR_DELEG_REVOKED:
5758 case -NFS4ERR_EXPIRED:
5759 case -NFS4ERR_BAD_STATEID:
5760 case -NFS4ERR_OLD_STATEID:
5761 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
5762 case -NFS4ERR_WRONG_CRED:
5763 data->args.lr_args = NULL;
5764 data->res.lr_res = NULL;
5765 data->res.lr_ret = 0;
5766 rpc_restart_call_prepare(task);
5771 switch (task->tk_status) {
5773 renew_lease(data->res.server, data->timestamp);
5775 case -NFS4ERR_ADMIN_REVOKED:
5776 case -NFS4ERR_DELEG_REVOKED:
5777 case -NFS4ERR_EXPIRED:
5778 nfs4_free_revoked_stateid(data->res.server,
5780 task->tk_msg.rpc_cred);
5781 case -NFS4ERR_BAD_STATEID:
5782 case -NFS4ERR_OLD_STATEID:
5783 case -NFS4ERR_STALE_STATEID:
5784 task->tk_status = 0;
5786 case -NFS4ERR_ACCESS:
5787 if (data->args.bitmask) {
5788 data->args.bitmask = NULL;
5789 data->res.fattr = NULL;
5790 task->tk_status = 0;
5791 rpc_restart_call_prepare(task);
5795 if (nfs4_async_handle_error(task, data->res.server,
5796 NULL, NULL) == -EAGAIN) {
5797 rpc_restart_call_prepare(task);
5801 data->rpc_status = task->tk_status;
5804 static void nfs4_delegreturn_release(void *calldata)
5806 struct nfs4_delegreturndata *data = calldata;
5807 struct inode *inode = data->inode;
5811 pnfs_roc_release(&data->lr.arg, &data->lr.res,
5813 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5814 nfs_iput_and_deactive(inode);
5819 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5821 struct nfs4_delegreturndata *d_data;
5823 d_data = (struct nfs4_delegreturndata *)data;
5825 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task)) {
5826 nfs4_sequence_done(task, &d_data->res.seq_res);
5830 nfs4_setup_sequence(d_data->res.server->nfs_client,
5831 &d_data->args.seq_args,
5832 &d_data->res.seq_res,
5836 static const struct rpc_call_ops nfs4_delegreturn_ops = {
5837 .rpc_call_prepare = nfs4_delegreturn_prepare,
5838 .rpc_call_done = nfs4_delegreturn_done,
5839 .rpc_release = nfs4_delegreturn_release,
5842 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
5844 struct nfs4_delegreturndata *data;
5845 struct nfs_server *server = NFS_SERVER(inode);
5846 struct rpc_task *task;
5847 struct rpc_message msg = {
5848 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5851 struct rpc_task_setup task_setup_data = {
5852 .rpc_client = server->client,
5853 .rpc_message = &msg,
5854 .callback_ops = &nfs4_delegreturn_ops,
5855 .flags = RPC_TASK_ASYNC,
5859 data = kzalloc(sizeof(*data), GFP_NOFS);
5862 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
5864 nfs4_state_protect(server->nfs_client,
5865 NFS_SP4_MACH_CRED_CLEANUP,
5866 &task_setup_data.rpc_client, &msg);
5868 data->args.fhandle = &data->fh;
5869 data->args.stateid = &data->stateid;
5870 data->args.bitmask = server->cache_consistency_bitmask;
5871 nfs_copy_fh(&data->fh, NFS_FH(inode));
5872 nfs4_stateid_copy(&data->stateid, stateid);
5873 data->res.fattr = &data->fattr;
5874 data->res.server = server;
5875 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5876 data->lr.arg.ld_private = &data->lr.ld_private;
5877 nfs_fattr_init(data->res.fattr);
5878 data->timestamp = jiffies;
5879 data->rpc_status = 0;
5880 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
5881 data->inode = nfs_igrab_and_active(inode);
5884 data->args.lr_args = &data->lr.arg;
5885 data->res.lr_res = &data->lr.res;
5887 } else if (data->lr.roc) {
5888 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
5889 data->lr.roc = false;
5892 task_setup_data.callback_data = data;
5893 msg.rpc_argp = &data->args;
5894 msg.rpc_resp = &data->res;
5895 task = rpc_run_task(&task_setup_data);
5897 return PTR_ERR(task);
5900 status = rpc_wait_for_completion_task(task);
5903 status = data->rpc_status;
5909 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
5911 struct nfs_server *server = NFS_SERVER(inode);
5912 struct nfs4_exception exception = { };
5915 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
5916 trace_nfs4_delegreturn(inode, stateid, err);
5918 case -NFS4ERR_STALE_STATEID:
5919 case -NFS4ERR_EXPIRED:
5923 err = nfs4_handle_exception(server, err, &exception);
5924 } while (exception.retry);
5928 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5930 struct inode *inode = state->inode;
5931 struct nfs_server *server = NFS_SERVER(inode);
5932 struct nfs_client *clp = server->nfs_client;
5933 struct nfs_lockt_args arg = {
5934 .fh = NFS_FH(inode),
5937 struct nfs_lockt_res res = {
5940 struct rpc_message msg = {
5941 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5944 .rpc_cred = state->owner->so_cred,
5946 struct nfs4_lock_state *lsp;
5949 arg.lock_owner.clientid = clp->cl_clientid;
5950 status = nfs4_set_lock_state(state, request);
5953 lsp = request->fl_u.nfs4_fl.owner;
5954 arg.lock_owner.id = lsp->ls_seqid.owner_id;
5955 arg.lock_owner.s_dev = server->s_dev;
5956 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5959 request->fl_type = F_UNLCK;
5961 case -NFS4ERR_DENIED:
5964 request->fl_ops->fl_release_private(request);
5965 request->fl_ops = NULL;
5970 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5972 struct nfs4_exception exception = { };
5976 err = _nfs4_proc_getlk(state, cmd, request);
5977 trace_nfs4_get_lock(request, state, cmd, err);
5978 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
5980 } while (exception.retry);
5984 struct nfs4_unlockdata {
5985 struct nfs_locku_args arg;
5986 struct nfs_locku_res res;
5987 struct nfs4_lock_state *lsp;
5988 struct nfs_open_context *ctx;
5989 struct nfs_lock_context *l_ctx;
5990 struct file_lock fl;
5991 struct nfs_server *server;
5992 unsigned long timestamp;
5995 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5996 struct nfs_open_context *ctx,
5997 struct nfs4_lock_state *lsp,
5998 struct nfs_seqid *seqid)
6000 struct nfs4_unlockdata *p;
6001 struct inode *inode = lsp->ls_state->inode;
6003 p = kzalloc(sizeof(*p), GFP_NOFS);
6006 p->arg.fh = NFS_FH(inode);
6008 p->arg.seqid = seqid;
6009 p->res.seqid = seqid;
6011 atomic_inc(&lsp->ls_count);
6012 /* Ensure we don't close file until we're done freeing locks! */
6013 p->ctx = get_nfs_open_context(ctx);
6014 p->l_ctx = nfs_get_lock_context(ctx);
6015 memcpy(&p->fl, fl, sizeof(p->fl));
6016 p->server = NFS_SERVER(inode);
6020 static void nfs4_locku_release_calldata(void *data)
6022 struct nfs4_unlockdata *calldata = data;
6023 nfs_free_seqid(calldata->arg.seqid);
6024 nfs4_put_lock_state(calldata->lsp);
6025 nfs_put_lock_context(calldata->l_ctx);
6026 put_nfs_open_context(calldata->ctx);
6030 static void nfs4_locku_done(struct rpc_task *task, void *data)
6032 struct nfs4_unlockdata *calldata = data;
6034 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6036 switch (task->tk_status) {
6038 renew_lease(calldata->server, calldata->timestamp);
6039 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
6040 if (nfs4_update_lock_stateid(calldata->lsp,
6041 &calldata->res.stateid))
6043 case -NFS4ERR_ADMIN_REVOKED:
6044 case -NFS4ERR_EXPIRED:
6045 nfs4_free_revoked_stateid(calldata->server,
6046 &calldata->arg.stateid,
6047 task->tk_msg.rpc_cred);
6048 case -NFS4ERR_BAD_STATEID:
6049 case -NFS4ERR_OLD_STATEID:
6050 case -NFS4ERR_STALE_STATEID:
6051 if (!nfs4_stateid_match(&calldata->arg.stateid,
6052 &calldata->lsp->ls_stateid))
6053 rpc_restart_call_prepare(task);
6056 if (nfs4_async_handle_error(task, calldata->server,
6057 NULL, NULL) == -EAGAIN)
6058 rpc_restart_call_prepare(task);
6060 nfs_release_seqid(calldata->arg.seqid);
6063 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
6065 struct nfs4_unlockdata *calldata = data;
6067 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6068 nfs_async_iocounter_wait(task, calldata->l_ctx))
6071 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
6073 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
6074 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
6075 /* Note: exit _without_ running nfs4_locku_done */
6078 calldata->timestamp = jiffies;
6079 if (nfs4_setup_sequence(calldata->server->nfs_client,
6080 &calldata->arg.seq_args,
6081 &calldata->res.seq_res,
6083 nfs_release_seqid(calldata->arg.seqid);
6086 task->tk_action = NULL;
6088 nfs4_sequence_done(task, &calldata->res.seq_res);
6091 static const struct rpc_call_ops nfs4_locku_ops = {
6092 .rpc_call_prepare = nfs4_locku_prepare,
6093 .rpc_call_done = nfs4_locku_done,
6094 .rpc_release = nfs4_locku_release_calldata,
6097 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6098 struct nfs_open_context *ctx,
6099 struct nfs4_lock_state *lsp,
6100 struct nfs_seqid *seqid)
6102 struct nfs4_unlockdata *data;
6103 struct rpc_message msg = {
6104 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6105 .rpc_cred = ctx->cred,
6107 struct rpc_task_setup task_setup_data = {
6108 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
6109 .rpc_message = &msg,
6110 .callback_ops = &nfs4_locku_ops,
6111 .workqueue = nfsiod_workqueue,
6112 .flags = RPC_TASK_ASYNC,
6115 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6116 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6118 /* Ensure this is an unlock - when canceling a lock, the
6119 * canceled lock is passed in, and it won't be an unlock.
6121 fl->fl_type = F_UNLCK;
6122 if (fl->fl_flags & FL_CLOSE)
6123 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
6125 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6127 nfs_free_seqid(seqid);
6128 return ERR_PTR(-ENOMEM);
6131 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
6132 msg.rpc_argp = &data->arg;
6133 msg.rpc_resp = &data->res;
6134 task_setup_data.callback_data = data;
6135 return rpc_run_task(&task_setup_data);
6138 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6140 struct inode *inode = state->inode;
6141 struct nfs4_state_owner *sp = state->owner;
6142 struct nfs_inode *nfsi = NFS_I(inode);
6143 struct nfs_seqid *seqid;
6144 struct nfs4_lock_state *lsp;
6145 struct rpc_task *task;
6146 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6148 unsigned char fl_flags = request->fl_flags;
6150 status = nfs4_set_lock_state(state, request);
6151 /* Unlock _before_ we do the RPC call */
6152 request->fl_flags |= FL_EXISTS;
6153 /* Exclude nfs_delegation_claim_locks() */
6154 mutex_lock(&sp->so_delegreturn_mutex);
6155 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
6156 down_read(&nfsi->rwsem);
6157 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
6158 up_read(&nfsi->rwsem);
6159 mutex_unlock(&sp->so_delegreturn_mutex);
6162 up_read(&nfsi->rwsem);
6163 mutex_unlock(&sp->so_delegreturn_mutex);
6166 /* Is this a delegated lock? */
6167 lsp = request->fl_u.nfs4_fl.owner;
6168 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6170 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6171 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
6175 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
6176 status = PTR_ERR(task);
6179 status = rpc_wait_for_completion_task(task);
6182 request->fl_flags = fl_flags;
6183 trace_nfs4_unlock(request, state, F_SETLK, status);
6187 struct nfs4_lockdata {
6188 struct nfs_lock_args arg;
6189 struct nfs_lock_res res;
6190 struct nfs4_lock_state *lsp;
6191 struct nfs_open_context *ctx;
6192 struct file_lock fl;
6193 unsigned long timestamp;
6196 struct nfs_server *server;
6199 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
6200 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6203 struct nfs4_lockdata *p;
6204 struct inode *inode = lsp->ls_state->inode;
6205 struct nfs_server *server = NFS_SERVER(inode);
6206 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6208 p = kzalloc(sizeof(*p), gfp_mask);
6212 p->arg.fh = NFS_FH(inode);
6214 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
6215 if (IS_ERR(p->arg.open_seqid))
6217 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6218 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
6219 if (IS_ERR(p->arg.lock_seqid))
6220 goto out_free_seqid;
6221 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
6222 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
6223 p->arg.lock_owner.s_dev = server->s_dev;
6224 p->res.lock_seqid = p->arg.lock_seqid;
6227 atomic_inc(&lsp->ls_count);
6228 p->ctx = get_nfs_open_context(ctx);
6229 memcpy(&p->fl, fl, sizeof(p->fl));
6232 nfs_free_seqid(p->arg.open_seqid);
6238 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6240 struct nfs4_lockdata *data = calldata;
6241 struct nfs4_state *state = data->lsp->ls_state;
6243 dprintk("%s: begin!\n", __func__);
6244 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
6246 /* Do we need to do an open_to_lock_owner? */
6247 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
6248 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
6249 goto out_release_lock_seqid;
6251 nfs4_stateid_copy(&data->arg.open_stateid,
6252 &state->open_stateid);
6253 data->arg.new_lock_owner = 1;
6254 data->res.open_seqid = data->arg.open_seqid;
6256 data->arg.new_lock_owner = 0;
6257 nfs4_stateid_copy(&data->arg.lock_stateid,
6258 &data->lsp->ls_stateid);
6260 if (!nfs4_valid_open_stateid(state)) {
6261 data->rpc_status = -EBADF;
6262 task->tk_action = NULL;
6263 goto out_release_open_seqid;
6265 data->timestamp = jiffies;
6266 if (nfs4_setup_sequence(data->server->nfs_client,
6267 &data->arg.seq_args,
6271 out_release_open_seqid:
6272 nfs_release_seqid(data->arg.open_seqid);
6273 out_release_lock_seqid:
6274 nfs_release_seqid(data->arg.lock_seqid);
6276 nfs4_sequence_done(task, &data->res.seq_res);
6277 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
6280 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6282 struct nfs4_lockdata *data = calldata;
6283 struct nfs4_lock_state *lsp = data->lsp;
6285 dprintk("%s: begin!\n", __func__);
6287 if (!nfs4_sequence_done(task, &data->res.seq_res))
6290 data->rpc_status = task->tk_status;
6291 switch (task->tk_status) {
6293 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
6295 if (data->arg.new_lock) {
6296 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
6297 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
6298 rpc_restart_call_prepare(task);
6302 if (data->arg.new_lock_owner != 0) {
6303 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6304 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6305 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6306 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6307 rpc_restart_call_prepare(task);
6309 case -NFS4ERR_BAD_STATEID:
6310 case -NFS4ERR_OLD_STATEID:
6311 case -NFS4ERR_STALE_STATEID:
6312 case -NFS4ERR_EXPIRED:
6313 if (data->arg.new_lock_owner != 0) {
6314 if (!nfs4_stateid_match(&data->arg.open_stateid,
6315 &lsp->ls_state->open_stateid))
6316 rpc_restart_call_prepare(task);
6317 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6319 rpc_restart_call_prepare(task);
6321 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
6324 static void nfs4_lock_release(void *calldata)
6326 struct nfs4_lockdata *data = calldata;
6328 dprintk("%s: begin!\n", __func__);
6329 nfs_free_seqid(data->arg.open_seqid);
6330 if (data->cancelled) {
6331 struct rpc_task *task;
6332 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6333 data->arg.lock_seqid);
6335 rpc_put_task_async(task);
6336 dprintk("%s: cancelling lock!\n", __func__);
6338 nfs_free_seqid(data->arg.lock_seqid);
6339 nfs4_put_lock_state(data->lsp);
6340 put_nfs_open_context(data->ctx);
6342 dprintk("%s: done!\n", __func__);
6345 static const struct rpc_call_ops nfs4_lock_ops = {
6346 .rpc_call_prepare = nfs4_lock_prepare,
6347 .rpc_call_done = nfs4_lock_done,
6348 .rpc_release = nfs4_lock_release,
6351 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6354 case -NFS4ERR_ADMIN_REVOKED:
6355 case -NFS4ERR_EXPIRED:
6356 case -NFS4ERR_BAD_STATEID:
6357 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6358 if (new_lock_owner != 0 ||
6359 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
6360 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
6362 case -NFS4ERR_STALE_STATEID:
6363 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6364 nfs4_schedule_lease_recovery(server->nfs_client);
6368 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
6370 struct nfs4_lockdata *data;
6371 struct rpc_task *task;
6372 struct rpc_message msg = {
6373 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6374 .rpc_cred = state->owner->so_cred,
6376 struct rpc_task_setup task_setup_data = {
6377 .rpc_client = NFS_CLIENT(state->inode),
6378 .rpc_message = &msg,
6379 .callback_ops = &nfs4_lock_ops,
6380 .workqueue = nfsiod_workqueue,
6381 .flags = RPC_TASK_ASYNC,
6385 dprintk("%s: begin!\n", __func__);
6386 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
6387 fl->fl_u.nfs4_fl.owner,
6388 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
6392 data->arg.block = 1;
6393 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
6394 msg.rpc_argp = &data->arg;
6395 msg.rpc_resp = &data->res;
6396 task_setup_data.callback_data = data;
6397 if (recovery_type > NFS_LOCK_NEW) {
6398 if (recovery_type == NFS_LOCK_RECLAIM)
6399 data->arg.reclaim = NFS_LOCK_RECLAIM;
6400 nfs4_set_sequence_privileged(&data->arg.seq_args);
6402 data->arg.new_lock = 1;
6403 task = rpc_run_task(&task_setup_data);
6405 return PTR_ERR(task);
6406 ret = rpc_wait_for_completion_task(task);
6408 ret = data->rpc_status;
6410 nfs4_handle_setlk_error(data->server, data->lsp,
6411 data->arg.new_lock_owner, ret);
6413 data->cancelled = true;
6414 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
6416 dprintk("%s: done, ret = %d!\n", __func__, ret);
6420 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6422 struct nfs_server *server = NFS_SERVER(state->inode);
6423 struct nfs4_exception exception = {
6424 .inode = state->inode,
6429 /* Cache the lock if possible... */
6430 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6432 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
6433 if (err != -NFS4ERR_DELAY)
6435 nfs4_handle_exception(server, err, &exception);
6436 } while (exception.retry);
6440 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6442 struct nfs_server *server = NFS_SERVER(state->inode);
6443 struct nfs4_exception exception = {
6444 .inode = state->inode,
6448 err = nfs4_set_lock_state(state, request);
6451 if (!recover_lost_locks) {
6452 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6456 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6458 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
6462 case -NFS4ERR_GRACE:
6463 case -NFS4ERR_DELAY:
6464 nfs4_handle_exception(server, err, &exception);
6467 } while (exception.retry);
6472 #if defined(CONFIG_NFS_V4_1)
6473 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6475 struct nfs4_lock_state *lsp;
6478 status = nfs4_set_lock_state(state, request);
6481 lsp = request->fl_u.nfs4_fl.owner;
6482 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6483 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6485 return nfs4_lock_expired(state, request);
6489 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6491 struct nfs_inode *nfsi = NFS_I(state->inode);
6492 struct nfs4_state_owner *sp = state->owner;
6493 unsigned char fl_flags = request->fl_flags;
6496 request->fl_flags |= FL_ACCESS;
6497 status = locks_lock_inode_wait(state->inode, request);
6500 mutex_lock(&sp->so_delegreturn_mutex);
6501 down_read(&nfsi->rwsem);
6502 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
6503 /* Yes: cache locks! */
6504 /* ...but avoid races with delegation recall... */
6505 request->fl_flags = fl_flags & ~FL_SLEEP;
6506 status = locks_lock_inode_wait(state->inode, request);
6507 up_read(&nfsi->rwsem);
6508 mutex_unlock(&sp->so_delegreturn_mutex);
6511 up_read(&nfsi->rwsem);
6512 mutex_unlock(&sp->so_delegreturn_mutex);
6513 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
6515 request->fl_flags = fl_flags;
6519 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6521 struct nfs4_exception exception = {
6523 .inode = state->inode,
6528 err = _nfs4_proc_setlk(state, cmd, request);
6529 if (err == -NFS4ERR_DENIED)
6531 err = nfs4_handle_exception(NFS_SERVER(state->inode),
6533 } while (exception.retry);
6537 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6538 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6541 nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6542 struct file_lock *request)
6544 int status = -ERESTARTSYS;
6545 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6547 while(!signalled()) {
6548 status = nfs4_proc_setlk(state, cmd, request);
6549 if ((status != -EAGAIN) || IS_SETLK(cmd))
6551 freezable_schedule_timeout_interruptible(timeout);
6553 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6554 status = -ERESTARTSYS;
6559 #ifdef CONFIG_NFS_V4_1
6560 struct nfs4_lock_waiter {
6561 struct task_struct *task;
6562 struct inode *inode;
6563 struct nfs_lowner *owner;
6568 nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
6571 struct cb_notify_lock_args *cbnl = key;
6572 struct nfs4_lock_waiter *waiter = wait->private;
6573 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6574 *wowner = waiter->owner;
6576 /* Only wake if the callback was for the same owner */
6577 if (lowner->clientid != wowner->clientid ||
6578 lowner->id != wowner->id ||
6579 lowner->s_dev != wowner->s_dev)
6582 /* Make sure it's for the right inode */
6583 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6586 waiter->notified = true;
6588 /* override "private" so we can use default_wake_function */
6589 wait->private = waiter->task;
6590 ret = autoremove_wake_function(wait, mode, flags, key);
6591 wait->private = waiter;
6596 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6598 int status = -ERESTARTSYS;
6599 unsigned long flags;
6600 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6601 struct nfs_server *server = NFS_SERVER(state->inode);
6602 struct nfs_client *clp = server->nfs_client;
6603 wait_queue_head_t *q = &clp->cl_lock_waitq;
6604 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6605 .id = lsp->ls_seqid.owner_id,
6606 .s_dev = server->s_dev };
6607 struct nfs4_lock_waiter waiter = { .task = current,
6608 .inode = state->inode,
6610 .notified = false };
6611 wait_queue_entry_t wait;
6613 /* Don't bother with waitqueue if we don't expect a callback */
6614 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6615 return nfs4_retry_setlk_simple(state, cmd, request);
6618 wait.private = &waiter;
6619 wait.func = nfs4_wake_lock_waiter;
6620 add_wait_queue(q, &wait);
6622 while(!signalled()) {
6623 status = nfs4_proc_setlk(state, cmd, request);
6624 if ((status != -EAGAIN) || IS_SETLK(cmd))
6627 status = -ERESTARTSYS;
6628 spin_lock_irqsave(&q->lock, flags);
6629 if (waiter.notified) {
6630 spin_unlock_irqrestore(&q->lock, flags);
6633 set_current_state(TASK_INTERRUPTIBLE);
6634 spin_unlock_irqrestore(&q->lock, flags);
6636 freezable_schedule_timeout(NFS4_LOCK_MAXTIMEOUT);
6639 finish_wait(q, &wait);
6642 #else /* !CONFIG_NFS_V4_1 */
6644 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6646 return nfs4_retry_setlk_simple(state, cmd, request);
6651 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6653 struct nfs_open_context *ctx;
6654 struct nfs4_state *state;
6657 /* verify open state */
6658 ctx = nfs_file_open_context(filp);
6661 if (IS_GETLK(cmd)) {
6663 return nfs4_proc_getlk(state, F_GETLK, request);
6667 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6670 if (request->fl_type == F_UNLCK) {
6672 return nfs4_proc_unlck(state, cmd, request);
6679 if ((request->fl_flags & FL_POSIX) &&
6680 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6684 * Don't rely on the VFS having checked the file open mode,
6685 * since it won't do this for flock() locks.
6687 switch (request->fl_type) {
6689 if (!(filp->f_mode & FMODE_READ))
6693 if (!(filp->f_mode & FMODE_WRITE))
6697 status = nfs4_set_lock_state(state, request);
6701 return nfs4_retry_setlk(state, cmd, request);
6704 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
6706 struct nfs_server *server = NFS_SERVER(state->inode);
6709 err = nfs4_set_lock_state(state, fl);
6713 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
6714 if (err != -NFS4ERR_DELAY)
6717 } while (err == -NFS4ERR_DELAY);
6718 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
6721 struct nfs_release_lockowner_data {
6722 struct nfs4_lock_state *lsp;
6723 struct nfs_server *server;
6724 struct nfs_release_lockowner_args args;
6725 struct nfs_release_lockowner_res res;
6726 unsigned long timestamp;
6729 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6731 struct nfs_release_lockowner_data *data = calldata;
6732 struct nfs_server *server = data->server;
6733 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
6734 &data->res.seq_res, task);
6735 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6736 data->timestamp = jiffies;
6739 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6741 struct nfs_release_lockowner_data *data = calldata;
6742 struct nfs_server *server = data->server;
6744 nfs40_sequence_done(task, &data->res.seq_res);
6746 switch (task->tk_status) {
6748 renew_lease(server, data->timestamp);
6750 case -NFS4ERR_STALE_CLIENTID:
6751 case -NFS4ERR_EXPIRED:
6752 nfs4_schedule_lease_recovery(server->nfs_client);
6754 case -NFS4ERR_LEASE_MOVED:
6755 case -NFS4ERR_DELAY:
6756 if (nfs4_async_handle_error(task, server,
6757 NULL, NULL) == -EAGAIN)
6758 rpc_restart_call_prepare(task);
6762 static void nfs4_release_lockowner_release(void *calldata)
6764 struct nfs_release_lockowner_data *data = calldata;
6765 nfs4_free_lock_state(data->server, data->lsp);
6769 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
6770 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6771 .rpc_call_done = nfs4_release_lockowner_done,
6772 .rpc_release = nfs4_release_lockowner_release,
6776 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
6778 struct nfs_release_lockowner_data *data;
6779 struct rpc_message msg = {
6780 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6783 if (server->nfs_client->cl_mvops->minor_version != 0)
6786 data = kmalloc(sizeof(*data), GFP_NOFS);
6790 data->server = server;
6791 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6792 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6793 data->args.lock_owner.s_dev = server->s_dev;
6795 msg.rpc_argp = &data->args;
6796 msg.rpc_resp = &data->res;
6797 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
6798 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
6801 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6803 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
6804 struct dentry *unused, struct inode *inode,
6805 const char *key, const void *buf,
6806 size_t buflen, int flags)
6808 return nfs4_proc_set_acl(inode, buf, buflen);
6811 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
6812 struct dentry *unused, struct inode *inode,
6813 const char *key, void *buf, size_t buflen)
6815 return nfs4_proc_get_acl(inode, buf, buflen);
6818 static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
6820 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
6823 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
6825 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
6826 struct dentry *unused, struct inode *inode,
6827 const char *key, const void *buf,
6828 size_t buflen, int flags)
6830 if (security_ismaclabel(key))
6831 return nfs4_set_security_label(inode, buf, buflen);
6836 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
6837 struct dentry *unused, struct inode *inode,
6838 const char *key, void *buf, size_t buflen)
6840 if (security_ismaclabel(key))
6841 return nfs4_get_security_label(inode, buf, buflen);
6846 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6850 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6851 len = security_inode_listsecurity(inode, list, list_len);
6852 if (list_len && len > list_len)
6858 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6859 .prefix = XATTR_SECURITY_PREFIX,
6860 .get = nfs4_xattr_get_nfs4_label,
6861 .set = nfs4_xattr_set_nfs4_label,
6867 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6875 * nfs_fhget will use either the mounted_on_fileid or the fileid
6877 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6879 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6880 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6881 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
6882 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
6885 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
6886 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
6887 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6891 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6892 const struct qstr *name,
6893 struct nfs4_fs_locations *fs_locations,
6896 struct nfs_server *server = NFS_SERVER(dir);
6898 struct nfs4_fs_locations_arg args = {
6899 .dir_fh = NFS_FH(dir),
6904 struct nfs4_fs_locations_res res = {
6905 .fs_locations = fs_locations,
6907 struct rpc_message msg = {
6908 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6914 dprintk("%s: start\n", __func__);
6916 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
6917 bitmask[1] = nfs4_fattr_bitmap[1];
6919 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6920 * is not supported */
6921 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6922 bitmask[0] &= ~FATTR4_WORD0_FILEID;
6924 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
6926 nfs_fattr_init(&fs_locations->fattr);
6927 fs_locations->server = server;
6928 fs_locations->nlocations = 0;
6929 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
6930 dprintk("%s: returned status = %d\n", __func__, status);
6934 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6935 const struct qstr *name,
6936 struct nfs4_fs_locations *fs_locations,
6939 struct nfs4_exception exception = { };
6942 err = _nfs4_proc_fs_locations(client, dir, name,
6943 fs_locations, page);
6944 trace_nfs4_get_fs_locations(dir, name, err);
6945 err = nfs4_handle_exception(NFS_SERVER(dir), err,
6947 } while (exception.retry);
6952 * This operation also signals the server that this client is
6953 * performing migration recovery. The server can stop returning
6954 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6955 * appended to this compound to identify the client ID which is
6956 * performing recovery.
6958 static int _nfs40_proc_get_locations(struct inode *inode,
6959 struct nfs4_fs_locations *locations,
6960 struct page *page, struct rpc_cred *cred)
6962 struct nfs_server *server = NFS_SERVER(inode);
6963 struct rpc_clnt *clnt = server->client;
6965 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6967 struct nfs4_fs_locations_arg args = {
6968 .clientid = server->nfs_client->cl_clientid,
6969 .fh = NFS_FH(inode),
6972 .migration = 1, /* skip LOOKUP */
6973 .renew = 1, /* append RENEW */
6975 struct nfs4_fs_locations_res res = {
6976 .fs_locations = locations,
6980 struct rpc_message msg = {
6981 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6986 unsigned long now = jiffies;
6989 nfs_fattr_init(&locations->fattr);
6990 locations->server = server;
6991 locations->nlocations = 0;
6993 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6994 nfs4_set_sequence_privileged(&args.seq_args);
6995 status = nfs4_call_sync_sequence(clnt, server, &msg,
6996 &args.seq_args, &res.seq_res);
7000 renew_lease(server, now);
7004 #ifdef CONFIG_NFS_V4_1
7007 * This operation also signals the server that this client is
7008 * performing migration recovery. The server can stop asserting
7009 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7010 * performing this operation is identified in the SEQUENCE
7011 * operation in this compound.
7013 * When the client supports GETATTR(fs_locations_info), it can
7014 * be plumbed in here.
7016 static int _nfs41_proc_get_locations(struct inode *inode,
7017 struct nfs4_fs_locations *locations,
7018 struct page *page, struct rpc_cred *cred)
7020 struct nfs_server *server = NFS_SERVER(inode);
7021 struct rpc_clnt *clnt = server->client;
7023 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7025 struct nfs4_fs_locations_arg args = {
7026 .fh = NFS_FH(inode),
7029 .migration = 1, /* skip LOOKUP */
7031 struct nfs4_fs_locations_res res = {
7032 .fs_locations = locations,
7035 struct rpc_message msg = {
7036 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7043 nfs_fattr_init(&locations->fattr);
7044 locations->server = server;
7045 locations->nlocations = 0;
7047 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7048 nfs4_set_sequence_privileged(&args.seq_args);
7049 status = nfs4_call_sync_sequence(clnt, server, &msg,
7050 &args.seq_args, &res.seq_res);
7051 if (status == NFS4_OK &&
7052 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7053 status = -NFS4ERR_LEASE_MOVED;
7057 #endif /* CONFIG_NFS_V4_1 */
7060 * nfs4_proc_get_locations - discover locations for a migrated FSID
7061 * @inode: inode on FSID that is migrating
7062 * @locations: result of query
7064 * @cred: credential to use for this operation
7066 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7067 * operation failed, or a negative errno if a local error occurred.
7069 * On success, "locations" is filled in, but if the server has
7070 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7073 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7074 * from this client that require migration recovery.
7076 int nfs4_proc_get_locations(struct inode *inode,
7077 struct nfs4_fs_locations *locations,
7078 struct page *page, struct rpc_cred *cred)
7080 struct nfs_server *server = NFS_SERVER(inode);
7081 struct nfs_client *clp = server->nfs_client;
7082 const struct nfs4_mig_recovery_ops *ops =
7083 clp->cl_mvops->mig_recovery_ops;
7084 struct nfs4_exception exception = { };
7087 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7088 (unsigned long long)server->fsid.major,
7089 (unsigned long long)server->fsid.minor,
7091 nfs_display_fhandle(NFS_FH(inode), __func__);
7094 status = ops->get_locations(inode, locations, page, cred);
7095 if (status != -NFS4ERR_DELAY)
7097 nfs4_handle_exception(server, status, &exception);
7098 } while (exception.retry);
7103 * This operation also signals the server that this client is
7104 * performing "lease moved" recovery. The server can stop
7105 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7106 * is appended to this compound to identify the client ID which is
7107 * performing recovery.
7109 static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7111 struct nfs_server *server = NFS_SERVER(inode);
7112 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7113 struct rpc_clnt *clnt = server->client;
7114 struct nfs4_fsid_present_arg args = {
7115 .fh = NFS_FH(inode),
7116 .clientid = clp->cl_clientid,
7117 .renew = 1, /* append RENEW */
7119 struct nfs4_fsid_present_res res = {
7122 struct rpc_message msg = {
7123 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7128 unsigned long now = jiffies;
7131 res.fh = nfs_alloc_fhandle();
7135 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7136 nfs4_set_sequence_privileged(&args.seq_args);
7137 status = nfs4_call_sync_sequence(clnt, server, &msg,
7138 &args.seq_args, &res.seq_res);
7139 nfs_free_fhandle(res.fh);
7143 do_renew_lease(clp, now);
7147 #ifdef CONFIG_NFS_V4_1
7150 * This operation also signals the server that this client is
7151 * performing "lease moved" recovery. The server can stop asserting
7152 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7153 * this operation is identified in the SEQUENCE operation in this
7156 static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7158 struct nfs_server *server = NFS_SERVER(inode);
7159 struct rpc_clnt *clnt = server->client;
7160 struct nfs4_fsid_present_arg args = {
7161 .fh = NFS_FH(inode),
7163 struct nfs4_fsid_present_res res = {
7165 struct rpc_message msg = {
7166 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7173 res.fh = nfs_alloc_fhandle();
7177 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7178 nfs4_set_sequence_privileged(&args.seq_args);
7179 status = nfs4_call_sync_sequence(clnt, server, &msg,
7180 &args.seq_args, &res.seq_res);
7181 nfs_free_fhandle(res.fh);
7182 if (status == NFS4_OK &&
7183 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7184 status = -NFS4ERR_LEASE_MOVED;
7188 #endif /* CONFIG_NFS_V4_1 */
7191 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7192 * @inode: inode on FSID to check
7193 * @cred: credential to use for this operation
7195 * Server indicates whether the FSID is present, moved, or not
7196 * recognized. This operation is necessary to clear a LEASE_MOVED
7197 * condition for this client ID.
7199 * Returns NFS4_OK if the FSID is present on this server,
7200 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7201 * NFS4ERR code if some error occurred on the server, or a
7202 * negative errno if a local failure occurred.
7204 int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7206 struct nfs_server *server = NFS_SERVER(inode);
7207 struct nfs_client *clp = server->nfs_client;
7208 const struct nfs4_mig_recovery_ops *ops =
7209 clp->cl_mvops->mig_recovery_ops;
7210 struct nfs4_exception exception = { };
7213 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7214 (unsigned long long)server->fsid.major,
7215 (unsigned long long)server->fsid.minor,
7217 nfs_display_fhandle(NFS_FH(inode), __func__);
7220 status = ops->fsid_present(inode, cred);
7221 if (status != -NFS4ERR_DELAY)
7223 nfs4_handle_exception(server, status, &exception);
7224 } while (exception.retry);
7229 * If 'use_integrity' is true and the state managment nfs_client
7230 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7231 * and the machine credential as per RFC3530bis and RFC5661 Security
7232 * Considerations sections. Otherwise, just use the user cred with the
7233 * filesystem's rpc_client.
7235 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
7238 struct nfs4_secinfo_arg args = {
7239 .dir_fh = NFS_FH(dir),
7242 struct nfs4_secinfo_res res = {
7245 struct rpc_message msg = {
7246 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7250 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
7251 struct rpc_cred *cred = NULL;
7253 if (use_integrity) {
7254 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
7255 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7256 msg.rpc_cred = cred;
7259 dprintk("NFS call secinfo %s\n", name->name);
7261 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7262 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7264 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7266 dprintk("NFS reply secinfo: %d\n", status);
7274 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7275 struct nfs4_secinfo_flavors *flavors)
7277 struct nfs4_exception exception = { };
7280 err = -NFS4ERR_WRONGSEC;
7282 /* try to use integrity protection with machine cred */
7283 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7284 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7287 * if unable to use integrity protection, or SECINFO with
7288 * integrity protection returns NFS4ERR_WRONGSEC (which is
7289 * disallowed by spec, but exists in deployed servers) use
7290 * the current filesystem's rpc_client and the user cred.
7292 if (err == -NFS4ERR_WRONGSEC)
7293 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7295 trace_nfs4_secinfo(dir, name, err);
7296 err = nfs4_handle_exception(NFS_SERVER(dir), err,
7298 } while (exception.retry);
7302 #ifdef CONFIG_NFS_V4_1
7304 * Check the exchange flags returned by the server for invalid flags, having
7305 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7308 static int nfs4_check_cl_exchange_flags(u32 flags, u32 version)
7310 if (version >= 2 && (flags & ~EXCHGID4_2_FLAG_MASK_R))
7312 else if (version < 2 && (flags & ~EXCHGID4_FLAG_MASK_R))
7314 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7315 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7317 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7321 return -NFS4ERR_INVAL;
7325 nfs41_same_server_scope(struct nfs41_server_scope *a,
7326 struct nfs41_server_scope *b)
7328 if (a->server_scope_sz != b->server_scope_sz)
7330 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
7334 nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7338 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7339 .rpc_call_done = nfs4_bind_one_conn_to_session_done,
7343 * nfs4_proc_bind_one_conn_to_session()
7345 * The 4.1 client currently uses the same TCP connection for the
7346 * fore and backchannel.
7349 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7350 struct rpc_xprt *xprt,
7351 struct nfs_client *clp,
7352 struct rpc_cred *cred)
7355 struct nfs41_bind_conn_to_session_args args = {
7357 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7359 struct nfs41_bind_conn_to_session_res res;
7360 struct rpc_message msg = {
7362 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
7367 struct rpc_task_setup task_setup_data = {
7370 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
7371 .rpc_message = &msg,
7372 .flags = RPC_TASK_TIMEOUT,
7374 struct rpc_task *task;
7376 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7377 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7378 args.dir = NFS4_CDFC4_FORE;
7380 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7381 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7382 args.dir = NFS4_CDFC4_FORE;
7384 task = rpc_run_task(&task_setup_data);
7385 if (!IS_ERR(task)) {
7386 status = task->tk_status;
7389 status = PTR_ERR(task);
7390 trace_nfs4_bind_conn_to_session(clp, status);
7392 if (memcmp(res.sessionid.data,
7393 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7394 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7397 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
7398 dprintk("NFS: %s: Unexpected direction from server\n",
7402 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
7403 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7412 struct rpc_bind_conn_calldata {
7413 struct nfs_client *clp;
7414 struct rpc_cred *cred;
7418 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7419 struct rpc_xprt *xprt,
7422 struct rpc_bind_conn_calldata *p = calldata;
7424 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7427 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7429 struct rpc_bind_conn_calldata data = {
7433 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7434 nfs4_proc_bind_conn_to_session_callback, &data);
7438 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7439 * and operations we'd like to see to enable certain features in the allow map
7441 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7442 .how = SP4_MACH_CRED,
7443 .enforce.u.words = {
7444 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7445 1 << (OP_EXCHANGE_ID - 32) |
7446 1 << (OP_CREATE_SESSION - 32) |
7447 1 << (OP_DESTROY_SESSION - 32) |
7448 1 << (OP_DESTROY_CLIENTID - 32)
7451 [0] = 1 << (OP_CLOSE) |
7452 1 << (OP_OPEN_DOWNGRADE) |
7454 1 << (OP_DELEGRETURN) |
7456 [1] = 1 << (OP_SECINFO - 32) |
7457 1 << (OP_SECINFO_NO_NAME - 32) |
7458 1 << (OP_LAYOUTRETURN - 32) |
7459 1 << (OP_TEST_STATEID - 32) |
7460 1 << (OP_FREE_STATEID - 32) |
7461 1 << (OP_WRITE - 32)
7466 * Select the state protection mode for client `clp' given the server results
7467 * from exchange_id in `sp'.
7469 * Returns 0 on success, negative errno otherwise.
7471 static int nfs4_sp4_select_mode(struct nfs_client *clp,
7472 struct nfs41_state_protection *sp)
7474 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7475 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7476 1 << (OP_EXCHANGE_ID - 32) |
7477 1 << (OP_CREATE_SESSION - 32) |
7478 1 << (OP_DESTROY_SESSION - 32) |
7479 1 << (OP_DESTROY_CLIENTID - 32)
7481 unsigned long flags = 0;
7485 if (sp->how == SP4_MACH_CRED) {
7486 /* Print state protect result */
7487 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7488 for (i = 0; i <= LAST_NFS4_OP; i++) {
7489 if (test_bit(i, sp->enforce.u.longs))
7490 dfprintk(MOUNT, " enforce op %d\n", i);
7491 if (test_bit(i, sp->allow.u.longs))
7492 dfprintk(MOUNT, " allow op %d\n", i);
7495 /* make sure nothing is on enforce list that isn't supported */
7496 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7497 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7498 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7505 * Minimal mode - state operations are allowed to use machine
7506 * credential. Note this already happens by default, so the
7507 * client doesn't have to do anything more than the negotiation.
7509 * NOTE: we don't care if EXCHANGE_ID is in the list -
7510 * we're already using the machine cred for exchange_id
7511 * and will never use a different cred.
7513 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7514 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7515 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7516 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7517 dfprintk(MOUNT, "sp4_mach_cred:\n");
7518 dfprintk(MOUNT, " minimal mode enabled\n");
7519 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
7521 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7526 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
7527 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7528 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
7529 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7530 dfprintk(MOUNT, " cleanup mode enabled\n");
7531 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
7534 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7535 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7536 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
7539 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7540 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7541 dfprintk(MOUNT, " secinfo mode enabled\n");
7542 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
7545 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7546 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7547 dfprintk(MOUNT, " stateid mode enabled\n");
7548 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
7551 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7552 dfprintk(MOUNT, " write mode enabled\n");
7553 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
7556 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7557 dfprintk(MOUNT, " commit mode enabled\n");
7558 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
7562 clp->cl_sp4_flags = flags;
7566 struct nfs41_exchange_id_data {
7567 struct nfs41_exchange_id_res res;
7568 struct nfs41_exchange_id_args args;
7571 static void nfs4_exchange_id_release(void *data)
7573 struct nfs41_exchange_id_data *cdata =
7574 (struct nfs41_exchange_id_data *)data;
7576 nfs_put_client(cdata->args.client);
7577 kfree(cdata->res.impl_id);
7578 kfree(cdata->res.server_scope);
7579 kfree(cdata->res.server_owner);
7583 static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7584 .rpc_release = nfs4_exchange_id_release,
7588 * _nfs4_proc_exchange_id()
7590 * Wrapper for EXCHANGE_ID operation.
7592 static struct rpc_task *
7593 nfs4_run_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
7594 u32 sp4_how, struct rpc_xprt *xprt)
7596 struct rpc_message msg = {
7597 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
7600 struct rpc_task_setup task_setup_data = {
7601 .rpc_client = clp->cl_rpcclient,
7602 .callback_ops = &nfs4_exchange_id_call_ops,
7603 .rpc_message = &msg,
7604 .flags = RPC_TASK_TIMEOUT,
7606 struct nfs41_exchange_id_data *calldata;
7609 if (!atomic_inc_not_zero(&clp->cl_count))
7610 return ERR_PTR(-EIO);
7613 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7617 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
7619 status = nfs4_init_uniform_client_string(clp);
7623 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7626 if (unlikely(calldata->res.server_owner == NULL))
7629 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
7631 if (unlikely(calldata->res.server_scope == NULL))
7632 goto out_server_owner;
7634 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7635 if (unlikely(calldata->res.impl_id == NULL))
7636 goto out_server_scope;
7640 calldata->args.state_protect.how = SP4_NONE;
7644 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
7654 task_setup_data.rpc_xprt = xprt;
7655 task_setup_data.flags |= RPC_TASK_SOFTCONN;
7656 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
7657 sizeof(calldata->args.verifier.data));
7659 calldata->args.client = clp;
7660 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7661 EXCHGID4_FLAG_BIND_PRINC_STATEID;
7662 #ifdef CONFIG_NFS_V4_1_MIGRATION
7663 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
7665 msg.rpc_argp = &calldata->args;
7666 msg.rpc_resp = &calldata->res;
7667 task_setup_data.callback_data = calldata;
7669 return rpc_run_task(&task_setup_data);
7672 kfree(calldata->res.impl_id);
7674 kfree(calldata->res.server_scope);
7676 kfree(calldata->res.server_owner);
7680 nfs_put_client(clp);
7681 return ERR_PTR(status);
7685 * _nfs4_proc_exchange_id()
7687 * Wrapper for EXCHANGE_ID operation.
7689 static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
7692 struct rpc_task *task;
7693 struct nfs41_exchange_id_args *argp;
7694 struct nfs41_exchange_id_res *resp;
7697 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
7699 return PTR_ERR(task);
7701 argp = task->tk_msg.rpc_argp;
7702 resp = task->tk_msg.rpc_resp;
7703 status = task->tk_status;
7707 status = nfs4_check_cl_exchange_flags(resp->flags,
7708 clp->cl_mvops->minor_version);
7712 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
7716 clp->cl_clientid = resp->clientid;
7717 clp->cl_exchange_flags = resp->flags;
7718 clp->cl_seqid = resp->seqid;
7719 /* Client ID is not confirmed */
7720 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
7721 clear_bit(NFS4_SESSION_ESTABLISHED,
7722 &clp->cl_session->session_state);
7724 if (clp->cl_serverscope != NULL &&
7725 !nfs41_same_server_scope(clp->cl_serverscope,
7726 resp->server_scope)) {
7727 dprintk("%s: server_scope mismatch detected\n",
7729 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
7732 swap(clp->cl_serverowner, resp->server_owner);
7733 swap(clp->cl_serverscope, resp->server_scope);
7734 swap(clp->cl_implid, resp->impl_id);
7736 /* Save the EXCHANGE_ID verifier session trunk tests */
7737 memcpy(clp->cl_confirm.data, argp->verifier.data,
7738 sizeof(clp->cl_confirm.data));
7740 trace_nfs4_exchange_id(clp, status);
7746 * nfs4_proc_exchange_id()
7748 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7750 * Since the clientid has expired, all compounds using sessions
7751 * associated with the stale clientid will be returning
7752 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7753 * be in some phase of session reset.
7755 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7757 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7759 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7762 /* try SP4_MACH_CRED if krb5i/p */
7763 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7764 authflavor == RPC_AUTH_GSS_KRB5P) {
7765 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
7771 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
7775 * nfs4_test_session_trunk
7777 * This is an add_xprt_test() test function called from
7778 * rpc_clnt_setup_test_and_add_xprt.
7780 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7781 * and is dereferrenced in nfs4_exchange_id_release
7783 * Upon success, add the new transport to the rpc_clnt
7785 * @clnt: struct rpc_clnt to get new transport
7786 * @xprt: the rpc_xprt to test
7787 * @data: call data for _nfs4_proc_exchange_id.
7789 int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7792 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
7793 struct rpc_task *task;
7798 dprintk("--> %s try %s\n", __func__,
7799 xprt->address_strings[RPC_DISPLAY_ADDR]);
7801 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7803 /* Test connection for session trunking. Async exchange_id call */
7804 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7806 return PTR_ERR(task);
7808 status = task->tk_status;
7810 status = nfs4_detect_session_trunking(adata->clp,
7811 task->tk_msg.rpc_resp, xprt);
7816 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7818 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7819 struct rpc_cred *cred)
7821 struct rpc_message msg = {
7822 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7828 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7829 trace_nfs4_destroy_clientid(clp, status);
7831 dprintk("NFS: Got error %d from the server %s on "
7832 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7836 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7837 struct rpc_cred *cred)
7842 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7843 ret = _nfs4_proc_destroy_clientid(clp, cred);
7845 case -NFS4ERR_DELAY:
7846 case -NFS4ERR_CLIENTID_BUSY:
7856 int nfs4_destroy_clientid(struct nfs_client *clp)
7858 struct rpc_cred *cred;
7861 if (clp->cl_mvops->minor_version < 1)
7863 if (clp->cl_exchange_flags == 0)
7865 if (clp->cl_preserve_clid)
7867 cred = nfs4_get_clid_cred(clp);
7868 ret = nfs4_proc_destroy_clientid(clp, cred);
7873 case -NFS4ERR_STALE_CLIENTID:
7874 clp->cl_exchange_flags = 0;
7880 struct nfs4_get_lease_time_data {
7881 struct nfs4_get_lease_time_args *args;
7882 struct nfs4_get_lease_time_res *res;
7883 struct nfs_client *clp;
7886 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7889 struct nfs4_get_lease_time_data *data =
7890 (struct nfs4_get_lease_time_data *)calldata;
7892 dprintk("--> %s\n", __func__);
7893 /* just setup sequence, do not trigger session recovery
7894 since we're invoked within one */
7895 nfs4_setup_sequence(data->clp,
7896 &data->args->la_seq_args,
7897 &data->res->lr_seq_res,
7899 dprintk("<-- %s\n", __func__);
7903 * Called from nfs4_state_manager thread for session setup, so don't recover
7904 * from sequence operation or clientid errors.
7906 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7908 struct nfs4_get_lease_time_data *data =
7909 (struct nfs4_get_lease_time_data *)calldata;
7911 dprintk("--> %s\n", __func__);
7912 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7914 switch (task->tk_status) {
7915 case -NFS4ERR_DELAY:
7916 case -NFS4ERR_GRACE:
7917 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7918 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7919 task->tk_status = 0;
7921 case -NFS4ERR_RETRY_UNCACHED_REP:
7922 rpc_restart_call_prepare(task);
7925 dprintk("<-- %s\n", __func__);
7928 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
7929 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7930 .rpc_call_done = nfs4_get_lease_time_done,
7933 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7935 struct rpc_task *task;
7936 struct nfs4_get_lease_time_args args;
7937 struct nfs4_get_lease_time_res res = {
7938 .lr_fsinfo = fsinfo,
7940 struct nfs4_get_lease_time_data data = {
7945 struct rpc_message msg = {
7946 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7950 struct rpc_task_setup task_setup = {
7951 .rpc_client = clp->cl_rpcclient,
7952 .rpc_message = &msg,
7953 .callback_ops = &nfs4_get_lease_time_ops,
7954 .callback_data = &data,
7955 .flags = RPC_TASK_TIMEOUT,
7959 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
7960 nfs4_set_sequence_privileged(&args.la_seq_args);
7961 task = rpc_run_task(&task_setup);
7964 return PTR_ERR(task);
7966 status = task->tk_status;
7972 * Initialize the values to be used by the client in CREATE_SESSION
7973 * If nfs4_init_session set the fore channel request and response sizes,
7976 * Set the back channel max_resp_sz_cached to zero to force the client to
7977 * always set csa_cachethis to FALSE because the current implementation
7978 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7980 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7981 struct rpc_clnt *clnt)
7983 unsigned int max_rqst_sz, max_resp_sz;
7984 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
7986 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7987 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7989 /* Fore channel attributes */
7990 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7991 args->fc_attrs.max_resp_sz = max_resp_sz;
7992 args->fc_attrs.max_ops = NFS4_MAX_OPS;
7993 args->fc_attrs.max_reqs = max_session_slots;
7995 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
7996 "max_ops=%u max_reqs=%u\n",
7998 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
7999 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
8001 /* Back channel attributes */
8002 args->bc_attrs.max_rqst_sz = max_bc_payload;
8003 args->bc_attrs.max_resp_sz = max_bc_payload;
8004 args->bc_attrs.max_resp_sz_cached = 0;
8005 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
8006 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
8008 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8009 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8011 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8012 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8013 args->bc_attrs.max_reqs);
8016 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8017 struct nfs41_create_session_res *res)
8019 struct nfs4_channel_attrs *sent = &args->fc_attrs;
8020 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
8022 if (rcvd->max_resp_sz > sent->max_resp_sz)
8025 * Our requested max_ops is the minimum we need; we're not
8026 * prepared to break up compounds into smaller pieces than that.
8027 * So, no point even trying to continue if the server won't
8030 if (rcvd->max_ops < sent->max_ops)
8032 if (rcvd->max_reqs == 0)
8034 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8035 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
8039 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8040 struct nfs41_create_session_res *res)
8042 struct nfs4_channel_attrs *sent = &args->bc_attrs;
8043 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
8045 if (!(res->flags & SESSION4_BACK_CHAN))
8047 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8049 if (rcvd->max_resp_sz < sent->max_resp_sz)
8051 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8053 if (rcvd->max_ops > sent->max_ops)
8055 if (rcvd->max_reqs > sent->max_reqs)
8061 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
8062 struct nfs41_create_session_res *res)
8066 ret = nfs4_verify_fore_channel_attrs(args, res);
8069 return nfs4_verify_back_channel_attrs(args, res);
8072 static void nfs4_update_session(struct nfs4_session *session,
8073 struct nfs41_create_session_res *res)
8075 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
8076 /* Mark client id and session as being confirmed */
8077 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8078 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
8079 session->flags = res->flags;
8080 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
8081 if (res->flags & SESSION4_BACK_CHAN)
8082 memcpy(&session->bc_attrs, &res->bc_attrs,
8083 sizeof(session->bc_attrs));
8086 static int _nfs4_proc_create_session(struct nfs_client *clp,
8087 struct rpc_cred *cred)
8089 struct nfs4_session *session = clp->cl_session;
8090 struct nfs41_create_session_args args = {
8092 .clientid = clp->cl_clientid,
8093 .seqid = clp->cl_seqid,
8094 .cb_program = NFS4_CALLBACK,
8096 struct nfs41_create_session_res res;
8098 struct rpc_message msg = {
8099 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8106 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
8107 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
8109 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
8110 trace_nfs4_create_session(clp, status);
8113 case -NFS4ERR_STALE_CLIENTID:
8114 case -NFS4ERR_DELAY:
8123 /* Verify the session's negotiated channel_attrs values */
8124 status = nfs4_verify_channel_attrs(&args, &res);
8125 /* Increment the clientid slot sequence id */
8128 nfs4_update_session(session, &res);
8135 * Issues a CREATE_SESSION operation to the server.
8136 * It is the responsibility of the caller to verify the session is
8137 * expired before calling this routine.
8139 int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
8143 struct nfs4_session *session = clp->cl_session;
8145 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8147 status = _nfs4_proc_create_session(clp, cred);
8151 /* Init or reset the session slot tables */
8152 status = nfs4_setup_session_slot_tables(session);
8153 dprintk("slot table setup returned %d\n", status);
8157 ptr = (unsigned *)&session->sess_id.data[0];
8158 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8159 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
8161 dprintk("<-- %s\n", __func__);
8166 * Issue the over-the-wire RPC DESTROY_SESSION.
8167 * The caller must serialize access to this routine.
8169 int nfs4_proc_destroy_session(struct nfs4_session *session,
8170 struct rpc_cred *cred)
8172 struct rpc_message msg = {
8173 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8174 .rpc_argp = session,
8179 dprintk("--> nfs4_proc_destroy_session\n");
8181 /* session is still being setup */
8182 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8185 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
8186 trace_nfs4_destroy_session(session->clp, status);
8189 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
8190 "Session has been destroyed regardless...\n", status);
8192 dprintk("<-- nfs4_proc_destroy_session\n");
8197 * Renew the cl_session lease.
8199 struct nfs4_sequence_data {
8200 struct nfs_client *clp;
8201 struct nfs4_sequence_args args;
8202 struct nfs4_sequence_res res;
8205 static void nfs41_sequence_release(void *data)
8207 struct nfs4_sequence_data *calldata = data;
8208 struct nfs_client *clp = calldata->clp;
8210 if (atomic_read(&clp->cl_count) > 1)
8211 nfs4_schedule_state_renewal(clp);
8212 nfs_put_client(clp);
8216 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8218 switch(task->tk_status) {
8219 case -NFS4ERR_DELAY:
8220 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8223 nfs4_schedule_lease_recovery(clp);
8228 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
8230 struct nfs4_sequence_data *calldata = data;
8231 struct nfs_client *clp = calldata->clp;
8233 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8236 trace_nfs4_sequence(clp, task->tk_status);
8237 if (task->tk_status < 0) {
8238 dprintk("%s ERROR %d\n", __func__, task->tk_status);
8239 if (atomic_read(&clp->cl_count) == 1)
8242 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8243 rpc_restart_call_prepare(task);
8247 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
8249 dprintk("<-- %s\n", __func__);
8252 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8254 struct nfs4_sequence_data *calldata = data;
8255 struct nfs_client *clp = calldata->clp;
8256 struct nfs4_sequence_args *args;
8257 struct nfs4_sequence_res *res;
8259 args = task->tk_msg.rpc_argp;
8260 res = task->tk_msg.rpc_resp;
8262 nfs4_setup_sequence(clp, args, res, task);
8265 static const struct rpc_call_ops nfs41_sequence_ops = {
8266 .rpc_call_done = nfs41_sequence_call_done,
8267 .rpc_call_prepare = nfs41_sequence_prepare,
8268 .rpc_release = nfs41_sequence_release,
8271 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8272 struct rpc_cred *cred,
8273 struct nfs4_slot *slot,
8276 struct nfs4_sequence_data *calldata;
8277 struct rpc_message msg = {
8278 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8281 struct rpc_task_setup task_setup_data = {
8282 .rpc_client = clp->cl_rpcclient,
8283 .rpc_message = &msg,
8284 .callback_ops = &nfs41_sequence_ops,
8285 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
8287 struct rpc_task *ret;
8289 ret = ERR_PTR(-EIO);
8290 if (!atomic_inc_not_zero(&clp->cl_count))
8293 ret = ERR_PTR(-ENOMEM);
8294 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8295 if (calldata == NULL)
8297 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
8298 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
8300 nfs4_set_sequence_privileged(&calldata->args);
8301 msg.rpc_argp = &calldata->args;
8302 msg.rpc_resp = &calldata->res;
8303 calldata->clp = clp;
8304 task_setup_data.callback_data = calldata;
8306 ret = rpc_run_task(&task_setup_data);
8311 nfs_put_client(clp);
8313 nfs41_release_slot(slot);
8317 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
8319 struct rpc_task *task;
8322 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
8324 task = _nfs41_proc_sequence(clp, cred, NULL, false);
8326 ret = PTR_ERR(task);
8328 rpc_put_task_async(task);
8329 dprintk("<-- %s status=%d\n", __func__, ret);
8333 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8335 struct rpc_task *task;
8338 task = _nfs41_proc_sequence(clp, cred, NULL, true);
8340 ret = PTR_ERR(task);
8343 ret = rpc_wait_for_completion_task(task);
8345 ret = task->tk_status;
8348 dprintk("<-- %s status=%d\n", __func__, ret);
8352 struct nfs4_reclaim_complete_data {
8353 struct nfs_client *clp;
8354 struct nfs41_reclaim_complete_args arg;
8355 struct nfs41_reclaim_complete_res res;
8358 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8360 struct nfs4_reclaim_complete_data *calldata = data;
8362 nfs4_setup_sequence(calldata->clp,
8363 &calldata->arg.seq_args,
8364 &calldata->res.seq_res,
8368 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8370 switch(task->tk_status) {
8372 case -NFS4ERR_COMPLETE_ALREADY:
8373 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8375 case -NFS4ERR_DELAY:
8376 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8378 case -NFS4ERR_RETRY_UNCACHED_REP:
8380 dprintk("%s: failed to reclaim complete error %d for server %s, retrying\n",
8381 __func__, task->tk_status, clp->cl_hostname);
8383 case -NFS4ERR_BADSESSION:
8384 case -NFS4ERR_DEADSESSION:
8385 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8386 nfs4_schedule_session_recovery(clp->cl_session,
8390 nfs4_schedule_lease_recovery(clp);
8395 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8397 struct nfs4_reclaim_complete_data *calldata = data;
8398 struct nfs_client *clp = calldata->clp;
8399 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8401 dprintk("--> %s\n", __func__);
8402 if (!nfs41_sequence_done(task, res))
8405 trace_nfs4_reclaim_complete(clp, task->tk_status);
8406 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8407 rpc_restart_call_prepare(task);
8410 dprintk("<-- %s\n", __func__);
8413 static void nfs4_free_reclaim_complete_data(void *data)
8415 struct nfs4_reclaim_complete_data *calldata = data;
8420 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8421 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8422 .rpc_call_done = nfs4_reclaim_complete_done,
8423 .rpc_release = nfs4_free_reclaim_complete_data,
8427 * Issue a global reclaim complete.
8429 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8430 struct rpc_cred *cred)
8432 struct nfs4_reclaim_complete_data *calldata;
8433 struct rpc_task *task;
8434 struct rpc_message msg = {
8435 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
8438 struct rpc_task_setup task_setup_data = {
8439 .rpc_client = clp->cl_rpcclient,
8440 .rpc_message = &msg,
8441 .callback_ops = &nfs4_reclaim_complete_call_ops,
8442 .flags = RPC_TASK_ASYNC,
8444 int status = -ENOMEM;
8446 dprintk("--> %s\n", __func__);
8447 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8448 if (calldata == NULL)
8450 calldata->clp = clp;
8451 calldata->arg.one_fs = 0;
8453 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
8454 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
8455 msg.rpc_argp = &calldata->arg;
8456 msg.rpc_resp = &calldata->res;
8457 task_setup_data.callback_data = calldata;
8458 task = rpc_run_task(&task_setup_data);
8460 status = PTR_ERR(task);
8463 status = rpc_wait_for_completion_task(task);
8465 status = task->tk_status;
8468 dprintk("<-- %s status=%d\n", __func__, status);
8473 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8475 struct nfs4_layoutget *lgp = calldata;
8476 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
8478 dprintk("--> %s\n", __func__);
8479 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
8480 &lgp->res.seq_res, task);
8481 dprintk("<-- %s\n", __func__);
8484 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8486 struct nfs4_layoutget *lgp = calldata;
8488 dprintk("--> %s\n", __func__);
8489 nfs41_sequence_process(task, &lgp->res.seq_res);
8490 dprintk("<-- %s\n", __func__);
8494 nfs4_layoutget_handle_exception(struct rpc_task *task,
8495 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8497 struct inode *inode = lgp->args.inode;
8498 struct nfs_server *server = NFS_SERVER(inode);
8499 struct pnfs_layout_hdr *lo;
8500 int nfs4err = task->tk_status;
8501 int err, status = 0;
8504 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
8506 nfs4_sequence_free_slot(&lgp->res.seq_res);
8513 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8514 * on the file. set tk_status to -ENODATA to tell upper layer to
8517 case -NFS4ERR_LAYOUTUNAVAILABLE:
8521 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8522 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8524 case -NFS4ERR_BADLAYOUT:
8525 status = -EOVERFLOW;
8528 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
8529 * (or clients) writing to the same RAID stripe except when
8530 * the minlength argument is 0 (see RFC5661 section 18.43.3).
8532 * Treat it like we would RECALLCONFLICT -- we retry for a little
8533 * while, and then eventually give up.
8535 case -NFS4ERR_LAYOUTTRYLATER:
8536 if (lgp->args.minlength == 0) {
8537 status = -EOVERFLOW;
8542 case -NFS4ERR_RECALLCONFLICT:
8543 status = -ERECALLCONFLICT;
8545 case -NFS4ERR_DELEG_REVOKED:
8546 case -NFS4ERR_ADMIN_REVOKED:
8547 case -NFS4ERR_EXPIRED:
8548 case -NFS4ERR_BAD_STATEID:
8549 exception->timeout = 0;
8550 spin_lock(&inode->i_lock);
8551 lo = NFS_I(inode)->layout;
8552 /* If the open stateid was bad, then recover it. */
8553 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8554 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
8555 spin_unlock(&inode->i_lock);
8556 exception->state = lgp->args.ctx->state;
8557 exception->stateid = &lgp->args.stateid;
8562 * Mark the bad layout state as invalid, then retry
8564 pnfs_mark_layout_stateid_invalid(lo, &head);
8565 spin_unlock(&inode->i_lock);
8566 nfs_commit_inode(inode, 0);
8567 pnfs_free_lseg_list(&head);
8572 err = nfs4_handle_exception(server, nfs4err, exception);
8574 if (exception->retry)
8580 dprintk("<-- %s\n", __func__);
8584 static size_t max_response_pages(struct nfs_server *server)
8586 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8587 return nfs_page_array_len(0, max_resp_sz);
8590 static void nfs4_free_pages(struct page **pages, size_t size)
8597 for (i = 0; i < size; i++) {
8600 __free_page(pages[i]);
8605 static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8607 struct page **pages;
8610 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8612 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8616 for (i = 0; i < size; i++) {
8617 pages[i] = alloc_page(gfp_flags);
8619 dprintk("%s: failed to allocate page\n", __func__);
8620 nfs4_free_pages(pages, size);
8628 static void nfs4_layoutget_release(void *calldata)
8630 struct nfs4_layoutget *lgp = calldata;
8631 struct inode *inode = lgp->args.inode;
8632 struct nfs_server *server = NFS_SERVER(inode);
8633 size_t max_pages = max_response_pages(server);
8635 dprintk("--> %s\n", __func__);
8636 nfs4_sequence_free_slot(&lgp->res.seq_res);
8637 nfs4_free_pages(lgp->args.layout.pages, max_pages);
8638 pnfs_put_layout_hdr(NFS_I(inode)->layout);
8639 put_nfs_open_context(lgp->args.ctx);
8641 dprintk("<-- %s\n", __func__);
8644 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8645 .rpc_call_prepare = nfs4_layoutget_prepare,
8646 .rpc_call_done = nfs4_layoutget_done,
8647 .rpc_release = nfs4_layoutget_release,
8650 struct pnfs_layout_segment *
8651 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
8653 struct inode *inode = lgp->args.inode;
8654 struct nfs_server *server = NFS_SERVER(inode);
8655 size_t max_pages = max_response_pages(server);
8656 struct rpc_task *task;
8657 struct rpc_message msg = {
8658 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8659 .rpc_argp = &lgp->args,
8660 .rpc_resp = &lgp->res,
8661 .rpc_cred = lgp->cred,
8663 struct rpc_task_setup task_setup_data = {
8664 .rpc_client = server->client,
8665 .rpc_message = &msg,
8666 .callback_ops = &nfs4_layoutget_call_ops,
8667 .callback_data = lgp,
8668 .flags = RPC_TASK_ASYNC,
8670 struct pnfs_layout_segment *lseg = NULL;
8671 struct nfs4_exception exception = {
8673 .timeout = *timeout,
8677 dprintk("--> %s\n", __func__);
8679 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8680 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8682 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8683 if (!lgp->args.layout.pages) {
8684 nfs4_layoutget_release(lgp);
8685 return ERR_PTR(-ENOMEM);
8687 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8689 lgp->res.layoutp = &lgp->args.layout;
8690 lgp->res.seq_res.sr_slot = NULL;
8691 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
8693 task = rpc_run_task(&task_setup_data);
8695 return ERR_CAST(task);
8696 status = rpc_wait_for_completion_task(task);
8700 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8701 if (task->tk_status < 0 || lgp->res.layoutp->len == 0) {
8702 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8703 *timeout = exception.timeout;
8705 lseg = pnfs_layout_process(lgp);
8707 trace_nfs4_layoutget(lgp->args.ctx,
8714 dprintk("<-- %s status=%d\n", __func__, status);
8716 return ERR_PTR(status);
8721 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8723 struct nfs4_layoutreturn *lrp = calldata;
8725 dprintk("--> %s\n", __func__);
8726 nfs4_setup_sequence(lrp->clp,
8727 &lrp->args.seq_args,
8732 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8734 struct nfs4_layoutreturn *lrp = calldata;
8735 struct nfs_server *server;
8737 dprintk("--> %s\n", __func__);
8739 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
8742 server = NFS_SERVER(lrp->args.inode);
8743 switch (task->tk_status) {
8745 task->tk_status = 0;
8748 case -NFS4ERR_DELAY:
8749 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
8751 nfs4_sequence_free_slot(&lrp->res.seq_res);
8752 rpc_restart_call_prepare(task);
8755 dprintk("<-- %s\n", __func__);
8758 static void nfs4_layoutreturn_release(void *calldata)
8760 struct nfs4_layoutreturn *lrp = calldata;
8761 struct pnfs_layout_hdr *lo = lrp->args.layout;
8763 dprintk("--> %s\n", __func__);
8764 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
8765 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
8766 nfs4_sequence_free_slot(&lrp->res.seq_res);
8767 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
8768 lrp->ld_private.ops->free(&lrp->ld_private);
8769 pnfs_put_layout_hdr(lrp->args.layout);
8770 nfs_iput_and_deactive(lrp->inode);
8772 dprintk("<-- %s\n", __func__);
8775 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8776 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8777 .rpc_call_done = nfs4_layoutreturn_done,
8778 .rpc_release = nfs4_layoutreturn_release,
8781 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
8783 struct rpc_task *task;
8784 struct rpc_message msg = {
8785 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8786 .rpc_argp = &lrp->args,
8787 .rpc_resp = &lrp->res,
8788 .rpc_cred = lrp->cred,
8790 struct rpc_task_setup task_setup_data = {
8791 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
8792 .rpc_message = &msg,
8793 .callback_ops = &nfs4_layoutreturn_call_ops,
8794 .callback_data = lrp,
8798 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8799 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8800 &task_setup_data.rpc_client, &msg);
8802 dprintk("--> %s\n", __func__);
8804 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8806 nfs4_layoutreturn_release(lrp);
8809 task_setup_data.flags |= RPC_TASK_ASYNC;
8811 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
8812 task = rpc_run_task(&task_setup_data);
8814 return PTR_ERR(task);
8816 status = task->tk_status;
8817 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
8818 dprintk("<-- %s status=%d\n", __func__, status);
8824 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
8825 struct pnfs_device *pdev,
8826 struct rpc_cred *cred)
8828 struct nfs4_getdeviceinfo_args args = {
8830 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8831 NOTIFY_DEVICEID4_DELETE,
8833 struct nfs4_getdeviceinfo_res res = {
8836 struct rpc_message msg = {
8837 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8844 dprintk("--> %s\n", __func__);
8845 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
8846 if (res.notification & ~args.notify_types)
8847 dprintk("%s: unsupported notification\n", __func__);
8848 if (res.notification != args.notify_types)
8851 dprintk("<-- %s status=%d\n", __func__, status);
8856 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8857 struct pnfs_device *pdev,
8858 struct rpc_cred *cred)
8860 struct nfs4_exception exception = { };
8864 err = nfs4_handle_exception(server,
8865 _nfs4_proc_getdeviceinfo(server, pdev, cred),
8867 } while (exception.retry);
8870 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8872 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8874 struct nfs4_layoutcommit_data *data = calldata;
8875 struct nfs_server *server = NFS_SERVER(data->args.inode);
8877 nfs4_setup_sequence(server->nfs_client,
8878 &data->args.seq_args,
8884 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8886 struct nfs4_layoutcommit_data *data = calldata;
8887 struct nfs_server *server = NFS_SERVER(data->args.inode);
8889 if (!nfs41_sequence_done(task, &data->res.seq_res))
8892 switch (task->tk_status) { /* Just ignore these failures */
8893 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8894 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8895 case -NFS4ERR_BADLAYOUT: /* no layout */
8896 case -NFS4ERR_GRACE: /* loca_recalim always false */
8897 task->tk_status = 0;
8901 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
8902 rpc_restart_call_prepare(task);
8908 static void nfs4_layoutcommit_release(void *calldata)
8910 struct nfs4_layoutcommit_data *data = calldata;
8912 pnfs_cleanup_layoutcommit(data);
8913 nfs_post_op_update_inode_force_wcc(data->args.inode,
8915 put_rpccred(data->cred);
8916 nfs_iput_and_deactive(data->inode);
8920 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8921 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8922 .rpc_call_done = nfs4_layoutcommit_done,
8923 .rpc_release = nfs4_layoutcommit_release,
8927 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
8929 struct rpc_message msg = {
8930 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8931 .rpc_argp = &data->args,
8932 .rpc_resp = &data->res,
8933 .rpc_cred = data->cred,
8935 struct rpc_task_setup task_setup_data = {
8936 .task = &data->task,
8937 .rpc_client = NFS_CLIENT(data->args.inode),
8938 .rpc_message = &msg,
8939 .callback_ops = &nfs4_layoutcommit_ops,
8940 .callback_data = data,
8942 struct rpc_task *task;
8945 dprintk("NFS: initiating layoutcommit call. sync %d "
8946 "lbw: %llu inode %lu\n", sync,
8947 data->args.lastbytewritten,
8948 data->args.inode->i_ino);
8951 data->inode = nfs_igrab_and_active(data->args.inode);
8952 if (data->inode == NULL) {
8953 nfs4_layoutcommit_release(data);
8956 task_setup_data.flags = RPC_TASK_ASYNC;
8958 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
8959 task = rpc_run_task(&task_setup_data);
8961 return PTR_ERR(task);
8963 status = task->tk_status;
8964 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
8965 dprintk("%s: status %d\n", __func__, status);
8971 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8972 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8975 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8976 struct nfs_fsinfo *info,
8977 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
8979 struct nfs41_secinfo_no_name_args args = {
8980 .style = SECINFO_STYLE_CURRENT_FH,
8982 struct nfs4_secinfo_res res = {
8985 struct rpc_message msg = {
8986 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8990 struct rpc_clnt *clnt = server->client;
8991 struct rpc_cred *cred = NULL;
8994 if (use_integrity) {
8995 clnt = server->nfs_client->cl_rpcclient;
8996 cred = nfs4_get_clid_cred(server->nfs_client);
8997 msg.rpc_cred = cred;
9000 dprintk("--> %s\n", __func__);
9001 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
9003 dprintk("<-- %s status=%d\n", __func__, status);
9012 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9013 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9015 struct nfs4_exception exception = { };
9018 /* first try using integrity protection */
9019 err = -NFS4ERR_WRONGSEC;
9021 /* try to use integrity protection with machine cred */
9022 if (_nfs4_is_integrity_protected(server->nfs_client))
9023 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9027 * if unable to use integrity protection, or SECINFO with
9028 * integrity protection returns NFS4ERR_WRONGSEC (which is
9029 * disallowed by spec, but exists in deployed servers) use
9030 * the current filesystem's rpc_client and the user cred.
9032 if (err == -NFS4ERR_WRONGSEC)
9033 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9038 case -NFS4ERR_WRONGSEC:
9042 err = nfs4_handle_exception(server, err, &exception);
9044 } while (exception.retry);
9050 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9051 struct nfs_fsinfo *info)
9055 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
9056 struct nfs4_secinfo_flavors *flavors;
9057 struct nfs4_secinfo4 *secinfo;
9060 page = alloc_page(GFP_KERNEL);
9066 flavors = page_address(page);
9067 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9070 * Fall back on "guess and check" method if
9071 * the server doesn't support SECINFO_NO_NAME
9073 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
9074 err = nfs4_find_root_sec(server, fhandle, info);
9080 for (i = 0; i < flavors->num_flavors; i++) {
9081 secinfo = &flavors->flavors[i];
9083 switch (secinfo->flavor) {
9087 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9088 &secinfo->flavor_info);
9091 flavor = RPC_AUTH_MAXFLAVOR;
9095 if (!nfs_auth_info_match(&server->auth_info, flavor))
9096 flavor = RPC_AUTH_MAXFLAVOR;
9098 if (flavor != RPC_AUTH_MAXFLAVOR) {
9099 err = nfs4_lookup_root_sec(server, fhandle,
9106 if (flavor == RPC_AUTH_MAXFLAVOR)
9117 static int _nfs41_test_stateid(struct nfs_server *server,
9118 nfs4_stateid *stateid,
9119 struct rpc_cred *cred)
9122 struct nfs41_test_stateid_args args = {
9125 struct nfs41_test_stateid_res res;
9126 struct rpc_message msg = {
9127 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9132 struct rpc_clnt *rpc_client = server->client;
9134 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9137 dprintk("NFS call test_stateid %p\n", stateid);
9138 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
9139 nfs4_set_sequence_privileged(&args.seq_args);
9140 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
9141 &args.seq_args, &res.seq_res);
9142 if (status != NFS_OK) {
9143 dprintk("NFS reply test_stateid: failed, %d\n", status);
9146 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
9150 static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9151 int err, struct nfs4_exception *exception)
9153 exception->retry = 0;
9155 case -NFS4ERR_DELAY:
9156 case -NFS4ERR_RETRY_UNCACHED_REP:
9157 nfs4_handle_exception(server, err, exception);
9159 case -NFS4ERR_BADSESSION:
9160 case -NFS4ERR_BADSLOT:
9161 case -NFS4ERR_BAD_HIGH_SLOT:
9162 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9163 case -NFS4ERR_DEADSESSION:
9164 nfs4_do_handle_exception(server, err, exception);
9169 * nfs41_test_stateid - perform a TEST_STATEID operation
9171 * @server: server / transport on which to perform the operation
9172 * @stateid: state ID to test
9175 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9176 * Otherwise a negative NFS4ERR value is returned if the operation
9177 * failed or the state ID is not currently valid.
9179 static int nfs41_test_stateid(struct nfs_server *server,
9180 nfs4_stateid *stateid,
9181 struct rpc_cred *cred)
9183 struct nfs4_exception exception = { };
9186 err = _nfs41_test_stateid(server, stateid, cred);
9187 nfs4_handle_delay_or_session_error(server, err, &exception);
9188 } while (exception.retry);
9192 struct nfs_free_stateid_data {
9193 struct nfs_server *server;
9194 struct nfs41_free_stateid_args args;
9195 struct nfs41_free_stateid_res res;
9198 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9200 struct nfs_free_stateid_data *data = calldata;
9201 nfs4_setup_sequence(data->server->nfs_client,
9202 &data->args.seq_args,
9207 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9209 struct nfs_free_stateid_data *data = calldata;
9211 nfs41_sequence_done(task, &data->res.seq_res);
9213 switch (task->tk_status) {
9214 case -NFS4ERR_DELAY:
9215 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
9216 rpc_restart_call_prepare(task);
9220 static void nfs41_free_stateid_release(void *calldata)
9225 static const struct rpc_call_ops nfs41_free_stateid_ops = {
9226 .rpc_call_prepare = nfs41_free_stateid_prepare,
9227 .rpc_call_done = nfs41_free_stateid_done,
9228 .rpc_release = nfs41_free_stateid_release,
9231 static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
9232 const nfs4_stateid *stateid,
9233 struct rpc_cred *cred,
9236 struct rpc_message msg = {
9237 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
9240 struct rpc_task_setup task_setup = {
9241 .rpc_client = server->client,
9242 .rpc_message = &msg,
9243 .callback_ops = &nfs41_free_stateid_ops,
9244 .flags = RPC_TASK_ASYNC,
9246 struct nfs_free_stateid_data *data;
9248 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9249 &task_setup.rpc_client, &msg);
9251 dprintk("NFS call free_stateid %p\n", stateid);
9252 data = kmalloc(sizeof(*data), GFP_NOFS);
9254 return ERR_PTR(-ENOMEM);
9255 data->server = server;
9256 nfs4_stateid_copy(&data->args.stateid, stateid);
9258 task_setup.callback_data = data;
9260 msg.rpc_argp = &data->args;
9261 msg.rpc_resp = &data->res;
9262 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
9264 nfs4_set_sequence_privileged(&data->args.seq_args);
9266 return rpc_run_task(&task_setup);
9270 * nfs41_free_stateid - perform a FREE_STATEID operation
9272 * @server: server / transport on which to perform the operation
9273 * @stateid: state ID to release
9275 * @is_recovery: set to true if this call needs to be privileged
9277 * Note: this function is always asynchronous.
9279 static int nfs41_free_stateid(struct nfs_server *server,
9280 const nfs4_stateid *stateid,
9281 struct rpc_cred *cred,
9284 struct rpc_task *task;
9286 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
9288 return PTR_ERR(task);
9294 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
9296 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
9298 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
9299 nfs4_free_lock_state(server, lsp);
9302 static bool nfs41_match_stateid(const nfs4_stateid *s1,
9303 const nfs4_stateid *s2)
9305 if (s1->type != s2->type)
9308 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
9311 if (s1->seqid == s2->seqid)
9314 return s1->seqid == 0 || s2->seqid == 0;
9317 #endif /* CONFIG_NFS_V4_1 */
9319 static bool nfs4_match_stateid(const nfs4_stateid *s1,
9320 const nfs4_stateid *s2)
9322 return nfs4_stateid_match(s1, s2);
9326 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
9327 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9328 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9329 .recover_open = nfs4_open_reclaim,
9330 .recover_lock = nfs4_lock_reclaim,
9331 .establish_clid = nfs4_init_clientid,
9332 .detect_trunking = nfs40_discover_server_trunking,
9335 #if defined(CONFIG_NFS_V4_1)
9336 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
9337 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9338 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9339 .recover_open = nfs4_open_reclaim,
9340 .recover_lock = nfs4_lock_reclaim,
9341 .establish_clid = nfs41_init_clientid,
9342 .reclaim_complete = nfs41_proc_reclaim_complete,
9343 .detect_trunking = nfs41_discover_server_trunking,
9345 #endif /* CONFIG_NFS_V4_1 */
9347 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
9348 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9349 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
9350 .recover_open = nfs40_open_expired,
9351 .recover_lock = nfs4_lock_expired,
9352 .establish_clid = nfs4_init_clientid,
9355 #if defined(CONFIG_NFS_V4_1)
9356 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
9357 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9358 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
9359 .recover_open = nfs41_open_expired,
9360 .recover_lock = nfs41_lock_expired,
9361 .establish_clid = nfs41_init_clientid,
9363 #endif /* CONFIG_NFS_V4_1 */
9365 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
9366 .sched_state_renewal = nfs4_proc_async_renew,
9367 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
9368 .renew_lease = nfs4_proc_renew,
9371 #if defined(CONFIG_NFS_V4_1)
9372 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
9373 .sched_state_renewal = nfs41_proc_async_sequence,
9374 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
9375 .renew_lease = nfs4_proc_sequence,
9379 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
9380 .get_locations = _nfs40_proc_get_locations,
9381 .fsid_present = _nfs40_proc_fsid_present,
9384 #if defined(CONFIG_NFS_V4_1)
9385 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
9386 .get_locations = _nfs41_proc_get_locations,
9387 .fsid_present = _nfs41_proc_fsid_present,
9389 #endif /* CONFIG_NFS_V4_1 */
9391 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9393 .init_caps = NFS_CAP_READDIRPLUS
9394 | NFS_CAP_ATOMIC_OPEN
9395 | NFS_CAP_POSIX_LOCK,
9396 .init_client = nfs40_init_client,
9397 .shutdown_client = nfs40_shutdown_client,
9398 .match_stateid = nfs4_match_stateid,
9399 .find_root_sec = nfs4_find_root_sec,
9400 .free_lock_state = nfs4_release_lockowner,
9401 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
9402 .alloc_seqid = nfs_alloc_seqid,
9403 .call_sync_ops = &nfs40_call_sync_ops,
9404 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9405 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9406 .state_renewal_ops = &nfs40_state_renewal_ops,
9407 .mig_recovery_ops = &nfs40_mig_recovery_ops,
9410 #if defined(CONFIG_NFS_V4_1)
9411 static struct nfs_seqid *
9412 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9417 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9419 .init_caps = NFS_CAP_READDIRPLUS
9420 | NFS_CAP_ATOMIC_OPEN
9421 | NFS_CAP_POSIX_LOCK
9422 | NFS_CAP_STATEID_NFSV41
9423 | NFS_CAP_ATOMIC_OPEN_V1,
9424 .init_client = nfs41_init_client,
9425 .shutdown_client = nfs41_shutdown_client,
9426 .match_stateid = nfs41_match_stateid,
9427 .find_root_sec = nfs41_find_root_sec,
9428 .free_lock_state = nfs41_free_lock_state,
9429 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
9430 .alloc_seqid = nfs_alloc_no_seqid,
9431 .session_trunk = nfs4_test_session_trunk,
9432 .call_sync_ops = &nfs41_call_sync_ops,
9433 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9434 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9435 .state_renewal_ops = &nfs41_state_renewal_ops,
9436 .mig_recovery_ops = &nfs41_mig_recovery_ops,
9440 #if defined(CONFIG_NFS_V4_2)
9441 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9443 .init_caps = NFS_CAP_READDIRPLUS
9444 | NFS_CAP_ATOMIC_OPEN
9445 | NFS_CAP_POSIX_LOCK
9446 | NFS_CAP_STATEID_NFSV41
9447 | NFS_CAP_ATOMIC_OPEN_V1
9450 | NFS_CAP_DEALLOCATE
9452 | NFS_CAP_LAYOUTSTATS
9454 .init_client = nfs41_init_client,
9455 .shutdown_client = nfs41_shutdown_client,
9456 .match_stateid = nfs41_match_stateid,
9457 .find_root_sec = nfs41_find_root_sec,
9458 .free_lock_state = nfs41_free_lock_state,
9459 .call_sync_ops = &nfs41_call_sync_ops,
9460 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
9461 .alloc_seqid = nfs_alloc_no_seqid,
9462 .session_trunk = nfs4_test_session_trunk,
9463 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9464 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9465 .state_renewal_ops = &nfs41_state_renewal_ops,
9466 .mig_recovery_ops = &nfs41_mig_recovery_ops,
9470 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9471 [0] = &nfs_v4_0_minor_ops,
9472 #if defined(CONFIG_NFS_V4_1)
9473 [1] = &nfs_v4_1_minor_ops,
9475 #if defined(CONFIG_NFS_V4_2)
9476 [2] = &nfs_v4_2_minor_ops,
9480 static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
9482 ssize_t error, error2;
9484 error = generic_listxattr(dentry, list, size);
9492 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9495 return error + error2;
9498 static const struct inode_operations nfs4_dir_inode_operations = {
9499 .create = nfs_create,
9500 .lookup = nfs_lookup,
9501 .atomic_open = nfs_atomic_open,
9503 .unlink = nfs_unlink,
9504 .symlink = nfs_symlink,
9508 .rename = nfs_rename,
9509 .permission = nfs_permission,
9510 .getattr = nfs_getattr,
9511 .setattr = nfs_setattr,
9512 .listxattr = nfs4_listxattr,
9515 static const struct inode_operations nfs4_file_inode_operations = {
9516 .permission = nfs_permission,
9517 .getattr = nfs_getattr,
9518 .setattr = nfs_setattr,
9519 .listxattr = nfs4_listxattr,
9522 const struct nfs_rpc_ops nfs_v4_clientops = {
9523 .version = 4, /* protocol version */
9524 .dentry_ops = &nfs4_dentry_operations,
9525 .dir_inode_ops = &nfs4_dir_inode_operations,
9526 .file_inode_ops = &nfs4_file_inode_operations,
9527 .file_ops = &nfs4_file_operations,
9528 .getroot = nfs4_proc_get_root,
9529 .submount = nfs4_submount,
9530 .try_mount = nfs4_try_mount,
9531 .getattr = nfs4_proc_getattr,
9532 .setattr = nfs4_proc_setattr,
9533 .lookup = nfs4_proc_lookup,
9534 .lookupp = nfs4_proc_lookupp,
9535 .access = nfs4_proc_access,
9536 .readlink = nfs4_proc_readlink,
9537 .create = nfs4_proc_create,
9538 .remove = nfs4_proc_remove,
9539 .unlink_setup = nfs4_proc_unlink_setup,
9540 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
9541 .unlink_done = nfs4_proc_unlink_done,
9542 .rename_setup = nfs4_proc_rename_setup,
9543 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
9544 .rename_done = nfs4_proc_rename_done,
9545 .link = nfs4_proc_link,
9546 .symlink = nfs4_proc_symlink,
9547 .mkdir = nfs4_proc_mkdir,
9548 .rmdir = nfs4_proc_remove,
9549 .readdir = nfs4_proc_readdir,
9550 .mknod = nfs4_proc_mknod,
9551 .statfs = nfs4_proc_statfs,
9552 .fsinfo = nfs4_proc_fsinfo,
9553 .pathconf = nfs4_proc_pathconf,
9554 .set_capabilities = nfs4_server_capabilities,
9555 .decode_dirent = nfs4_decode_dirent,
9556 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
9557 .read_setup = nfs4_proc_read_setup,
9558 .read_done = nfs4_read_done,
9559 .write_setup = nfs4_proc_write_setup,
9560 .write_done = nfs4_write_done,
9561 .commit_setup = nfs4_proc_commit_setup,
9562 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
9563 .commit_done = nfs4_commit_done,
9564 .lock = nfs4_proc_lock,
9565 .clear_acl_cache = nfs4_zap_acl_attr,
9566 .close_context = nfs4_close_context,
9567 .open_context = nfs4_atomic_open,
9568 .have_delegation = nfs4_have_delegation,
9569 .return_delegation = nfs4_inode_return_delegation,
9570 .alloc_client = nfs4_alloc_client,
9571 .init_client = nfs4_init_client,
9572 .free_client = nfs4_free_client,
9573 .create_server = nfs4_create_server,
9574 .clone_server = nfs_clone_server,
9577 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
9578 .name = XATTR_NAME_NFSV4_ACL,
9579 .list = nfs4_xattr_list_nfs4_acl,
9580 .get = nfs4_xattr_get_nfs4_acl,
9581 .set = nfs4_xattr_set_nfs4_acl,
9584 const struct xattr_handler *nfs4_xattr_handlers[] = {
9585 &nfs4_xattr_nfs4_acl_handler,
9586 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
9587 &nfs4_xattr_nfs4_label_handler,