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;
1840 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
1842 opendata->o_arg.open_flags = 0;
1843 opendata->o_arg.fmode = fmode;
1844 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1845 NFS_SB(opendata->dentry->d_sb),
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,
2422 NFS4_OPEN_CLAIM_FH);
2423 if (IS_ERR(opendata))
2424 return PTR_ERR(opendata);
2425 ret = nfs4_open_recover(opendata, state);
2427 d_drop(ctx->dentry);
2428 nfs4_opendata_put(opendata);
2432 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2434 struct nfs_server *server = NFS_SERVER(state->inode);
2435 struct nfs4_exception exception = { };
2439 err = _nfs4_open_expired(ctx, state);
2440 trace_nfs4_open_expired(ctx, 0, err);
2441 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2446 case -NFS4ERR_GRACE:
2447 case -NFS4ERR_DELAY:
2448 nfs4_handle_exception(server, err, &exception);
2451 } while (exception.retry);
2456 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2458 struct nfs_open_context *ctx;
2461 ctx = nfs4_state_find_open_context(state);
2464 ret = nfs4_do_open_expired(ctx, state);
2465 put_nfs_open_context(ctx);
2469 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2470 const nfs4_stateid *stateid)
2472 nfs_remove_bad_delegation(state->inode, stateid);
2473 write_seqlock(&state->seqlock);
2474 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2475 write_sequnlock(&state->seqlock);
2476 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2479 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2481 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2482 nfs_finish_clear_delegation_stateid(state, NULL);
2485 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2487 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2488 nfs40_clear_delegation_stateid(state);
2489 return nfs4_open_expired(sp, state);
2492 static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2493 nfs4_stateid *stateid,
2494 struct rpc_cred *cred)
2496 return -NFS4ERR_BAD_STATEID;
2499 #if defined(CONFIG_NFS_V4_1)
2500 static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2501 nfs4_stateid *stateid,
2502 struct rpc_cred *cred)
2506 switch (stateid->type) {
2509 case NFS4_INVALID_STATEID_TYPE:
2510 case NFS4_SPECIAL_STATEID_TYPE:
2511 return -NFS4ERR_BAD_STATEID;
2512 case NFS4_REVOKED_STATEID_TYPE:
2516 status = nfs41_test_stateid(server, stateid, cred);
2518 case -NFS4ERR_EXPIRED:
2519 case -NFS4ERR_ADMIN_REVOKED:
2520 case -NFS4ERR_DELEG_REVOKED:
2526 /* Ack the revoked state to the server */
2527 nfs41_free_stateid(server, stateid, cred, true);
2528 return -NFS4ERR_EXPIRED;
2531 static void nfs41_check_delegation_stateid(struct nfs4_state *state)
2533 struct nfs_server *server = NFS_SERVER(state->inode);
2534 nfs4_stateid stateid;
2535 struct nfs_delegation *delegation;
2536 struct rpc_cred *cred;
2539 /* Get the delegation credential for use by test/free_stateid */
2541 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2542 if (delegation == NULL) {
2547 nfs4_stateid_copy(&stateid, &delegation->stateid);
2548 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
2550 nfs_finish_clear_delegation_stateid(state, &stateid);
2554 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2555 &delegation->flags)) {
2560 cred = get_rpccred(delegation->cred);
2562 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
2563 trace_nfs4_test_delegation_stateid(state, NULL, status);
2564 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
2565 nfs_finish_clear_delegation_stateid(state, &stateid);
2571 * nfs41_check_expired_locks - possibly free a lock stateid
2573 * @state: NFSv4 state for an inode
2575 * Returns NFS_OK if recovery for this stateid is now finished.
2576 * Otherwise a negative NFS4ERR value is returned.
2578 static int nfs41_check_expired_locks(struct nfs4_state *state)
2580 int status, ret = NFS_OK;
2581 struct nfs4_lock_state *lsp, *prev = NULL;
2582 struct nfs_server *server = NFS_SERVER(state->inode);
2584 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2587 spin_lock(&state->state_lock);
2588 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2589 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2590 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2592 atomic_inc(&lsp->ls_count);
2593 spin_unlock(&state->state_lock);
2595 nfs4_put_lock_state(prev);
2598 status = nfs41_test_and_free_expired_stateid(server,
2601 trace_nfs4_test_lock_stateid(state, lsp, status);
2602 if (status == -NFS4ERR_EXPIRED ||
2603 status == -NFS4ERR_BAD_STATEID) {
2604 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
2605 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
2606 if (!recover_lost_locks)
2607 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2608 } else if (status != NFS_OK) {
2610 nfs4_put_lock_state(prev);
2613 spin_lock(&state->state_lock);
2616 spin_unlock(&state->state_lock);
2617 nfs4_put_lock_state(prev);
2623 * nfs41_check_open_stateid - possibly free an open stateid
2625 * @state: NFSv4 state for an inode
2627 * Returns NFS_OK if recovery for this stateid is now finished.
2628 * Otherwise a negative NFS4ERR value is returned.
2630 static int nfs41_check_open_stateid(struct nfs4_state *state)
2632 struct nfs_server *server = NFS_SERVER(state->inode);
2633 nfs4_stateid *stateid = &state->open_stateid;
2634 struct rpc_cred *cred = state->owner->so_cred;
2637 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
2638 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2639 if (nfs4_have_delegation(state->inode, state->state))
2641 return -NFS4ERR_OPENMODE;
2643 return -NFS4ERR_BAD_STATEID;
2645 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
2646 trace_nfs4_test_open_stateid(state, NULL, status);
2647 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
2648 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2649 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2650 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2651 clear_bit(NFS_OPEN_STATE, &state->flags);
2652 stateid->type = NFS4_INVALID_STATEID_TYPE;
2655 if (nfs_open_stateid_recover_openmode(state))
2656 return -NFS4ERR_OPENMODE;
2660 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2664 nfs41_check_delegation_stateid(state);
2665 status = nfs41_check_expired_locks(state);
2666 if (status != NFS_OK)
2668 status = nfs41_check_open_stateid(state);
2669 if (status != NFS_OK)
2670 status = nfs4_open_expired(sp, state);
2676 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2677 * fields corresponding to attributes that were used to store the verifier.
2678 * Make sure we clobber those fields in the later setattr call
2680 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2681 struct iattr *sattr, struct nfs4_label **label)
2683 const u32 *attrset = opendata->o_res.attrset;
2685 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2686 !(sattr->ia_valid & ATTR_ATIME_SET))
2687 sattr->ia_valid |= ATTR_ATIME;
2689 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2690 !(sattr->ia_valid & ATTR_MTIME_SET))
2691 sattr->ia_valid |= ATTR_MTIME;
2693 /* Except MODE, it seems harmless of setting twice. */
2694 if (opendata->o_arg.createmode != NFS4_CREATE_EXCLUSIVE &&
2695 (attrset[1] & FATTR4_WORD1_MODE ||
2696 attrset[2] & FATTR4_WORD2_MODE_UMASK))
2697 sattr->ia_valid &= ~ATTR_MODE;
2699 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2703 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2706 struct nfs_open_context *ctx)
2708 struct nfs4_state_owner *sp = opendata->owner;
2709 struct nfs_server *server = sp->so_server;
2710 struct dentry *dentry;
2711 struct nfs4_state *state;
2715 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2717 ret = _nfs4_proc_open(opendata);
2721 state = _nfs4_opendata_to_nfs4_state(opendata);
2722 ret = PTR_ERR(state);
2726 if (server->caps & NFS_CAP_POSIX_LOCK)
2727 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2728 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2729 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
2731 dentry = opendata->dentry;
2732 if (d_really_is_negative(dentry)) {
2733 struct dentry *alias;
2735 alias = d_exact_alias(dentry, state->inode);
2737 alias = d_splice_alias(igrab(state->inode), dentry);
2738 /* d_splice_alias() can't fail here - it's a non-directory */
2741 ctx->dentry = dentry = alias;
2743 nfs_set_verifier(dentry,
2744 nfs_save_change_attribute(d_inode(opendata->dir)));
2747 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2751 if (d_inode(dentry) == state->inode) {
2752 nfs_inode_attach_open_context(ctx);
2753 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2754 nfs4_schedule_stateid_recovery(server, state);
2757 if (!opendata->cancelled)
2758 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
2763 * Returns a referenced nfs4_state
2765 static int _nfs4_do_open(struct inode *dir,
2766 struct nfs_open_context *ctx,
2768 const struct nfs4_open_createattrs *c,
2771 struct nfs4_state_owner *sp;
2772 struct nfs4_state *state = NULL;
2773 struct nfs_server *server = NFS_SERVER(dir);
2774 struct nfs4_opendata *opendata;
2775 struct dentry *dentry = ctx->dentry;
2776 struct rpc_cred *cred = ctx->cred;
2777 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2778 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
2779 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
2780 struct iattr *sattr = c->sattr;
2781 struct nfs4_label *label = c->label;
2782 struct nfs4_label *olabel = NULL;
2785 /* Protect against reboot recovery conflicts */
2787 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2789 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2792 status = nfs4_client_recover_expired_lease(server->nfs_client);
2794 goto err_put_state_owner;
2795 if (d_really_is_positive(dentry))
2796 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
2798 if (d_really_is_positive(dentry))
2799 claim = NFS4_OPEN_CLAIM_FH;
2800 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
2801 c, claim, GFP_KERNEL);
2802 if (opendata == NULL)
2803 goto err_put_state_owner;
2806 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2807 if (IS_ERR(olabel)) {
2808 status = PTR_ERR(olabel);
2809 goto err_opendata_put;
2813 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2814 if (!opendata->f_attr.mdsthreshold) {
2815 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2816 if (!opendata->f_attr.mdsthreshold)
2817 goto err_free_label;
2819 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
2821 if (d_really_is_positive(dentry))
2822 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
2824 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
2826 goto err_free_label;
2829 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
2830 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
2831 nfs4_exclusive_attrset(opendata, sattr, &label);
2833 * send create attributes which was not set by open
2834 * with an extra setattr.
2836 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2837 nfs_fattr_init(opendata->o_res.f_attr);
2838 status = nfs4_do_setattr(state->inode, cred,
2839 opendata->o_res.f_attr, sattr,
2840 ctx, label, olabel);
2842 nfs_setattr_update_inode(state->inode, sattr,
2843 opendata->o_res.f_attr);
2844 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2848 if (opened && opendata->file_created)
2849 *opened |= FILE_CREATED;
2851 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
2852 *ctx_th = opendata->f_attr.mdsthreshold;
2853 opendata->f_attr.mdsthreshold = NULL;
2856 nfs4_label_free(olabel);
2858 nfs4_opendata_put(opendata);
2859 nfs4_put_state_owner(sp);
2862 nfs4_label_free(olabel);
2864 nfs4_opendata_put(opendata);
2865 err_put_state_owner:
2866 nfs4_put_state_owner(sp);
2872 static struct nfs4_state *nfs4_do_open(struct inode *dir,
2873 struct nfs_open_context *ctx,
2875 struct iattr *sattr,
2876 struct nfs4_label *label,
2879 struct nfs_server *server = NFS_SERVER(dir);
2880 struct nfs4_exception exception = { };
2881 struct nfs4_state *res;
2882 struct nfs4_open_createattrs c = {
2886 [0] = (__u32)jiffies,
2887 [1] = (__u32)current->pid,
2893 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
2895 trace_nfs4_open_file(ctx, flags, status);
2898 /* NOTE: BAD_SEQID means the server and client disagree about the
2899 * book-keeping w.r.t. state-changing operations
2900 * (OPEN/CLOSE/LOCK/LOCKU...)
2901 * It is actually a sign of a bug on the client or on the server.
2903 * If we receive a BAD_SEQID error in the particular case of
2904 * doing an OPEN, we assume that nfs_increment_open_seqid() will
2905 * have unhashed the old state_owner for us, and that we can
2906 * therefore safely retry using a new one. We should still warn
2907 * the user though...
2909 if (status == -NFS4ERR_BAD_SEQID) {
2910 pr_warn_ratelimited("NFS: v4 server %s "
2911 " returned a bad sequence-id error!\n",
2912 NFS_SERVER(dir)->nfs_client->cl_hostname);
2913 exception.retry = 1;
2917 * BAD_STATEID on OPEN means that the server cancelled our
2918 * state before it received the OPEN_CONFIRM.
2919 * Recover by retrying the request as per the discussion
2920 * on Page 181 of RFC3530.
2922 if (status == -NFS4ERR_BAD_STATEID) {
2923 exception.retry = 1;
2926 if (status == -NFS4ERR_EXPIRED) {
2927 nfs4_schedule_lease_recovery(server->nfs_client);
2928 exception.retry = 1;
2931 if (status == -EAGAIN) {
2932 /* We must have found a delegation */
2933 exception.retry = 1;
2936 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2938 res = ERR_PTR(nfs4_handle_exception(server,
2939 status, &exception));
2940 } while (exception.retry);
2944 static int _nfs4_do_setattr(struct inode *inode,
2945 struct nfs_setattrargs *arg,
2946 struct nfs_setattrres *res,
2947 struct rpc_cred *cred,
2948 struct nfs_open_context *ctx)
2950 struct nfs_server *server = NFS_SERVER(inode);
2951 struct rpc_message msg = {
2952 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2957 struct rpc_cred *delegation_cred = NULL;
2958 unsigned long timestamp = jiffies;
2962 nfs_fattr_init(res->fattr);
2964 /* Servers should only apply open mode checks for file size changes */
2965 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
2969 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
2970 /* Use that stateid */
2971 } else if (ctx != NULL && ctx->state) {
2972 struct nfs_lock_context *l_ctx;
2973 if (!nfs4_valid_open_stateid(ctx->state))
2975 l_ctx = nfs_get_lock_context(ctx);
2977 return PTR_ERR(l_ctx);
2978 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
2979 &arg->stateid, &delegation_cred);
2980 nfs_put_lock_context(l_ctx);
2985 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
2987 if (delegation_cred)
2988 msg.rpc_cred = delegation_cred;
2990 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
2992 put_rpccred(delegation_cred);
2993 if (status == 0 && ctx != NULL)
2994 renew_lease(server, timestamp);
2995 trace_nfs4_setattr(inode, &arg->stateid, status);
2999 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
3000 struct nfs_fattr *fattr, struct iattr *sattr,
3001 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
3002 struct nfs4_label *olabel)
3004 struct nfs_server *server = NFS_SERVER(inode);
3005 struct nfs4_state *state = ctx ? ctx->state : NULL;
3006 struct nfs_setattrargs arg = {
3007 .fh = NFS_FH(inode),
3010 .bitmask = server->attr_bitmask,
3013 struct nfs_setattrres res = {
3018 struct nfs4_exception exception = {
3021 .stateid = &arg.stateid,
3025 arg.bitmask = nfs4_bitmask(server, ilabel);
3027 arg.bitmask = nfs4_bitmask(server, olabel);
3030 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
3032 case -NFS4ERR_OPENMODE:
3033 if (!(sattr->ia_valid & ATTR_SIZE)) {
3034 pr_warn_once("NFSv4: server %s is incorrectly "
3035 "applying open mode checks to "
3036 "a SETATTR that is not "
3037 "changing file size.\n",
3038 server->nfs_client->cl_hostname);
3040 if (state && !(state->state & FMODE_WRITE)) {
3042 if (sattr->ia_valid & ATTR_OPEN)
3047 err = nfs4_handle_exception(server, err, &exception);
3048 } while (exception.retry);
3054 nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3056 if (inode == NULL || !nfs_have_layout(inode))
3059 return pnfs_wait_on_layoutreturn(inode, task);
3062 struct nfs4_closedata {
3063 struct inode *inode;
3064 struct nfs4_state *state;
3065 struct nfs_closeargs arg;
3066 struct nfs_closeres res;
3068 struct nfs4_layoutreturn_args arg;
3069 struct nfs4_layoutreturn_res res;
3070 struct nfs4_xdr_opaque_data ld_private;
3074 struct nfs_fattr fattr;
3075 unsigned long timestamp;
3078 static void nfs4_free_closedata(void *data)
3080 struct nfs4_closedata *calldata = data;
3081 struct nfs4_state_owner *sp = calldata->state->owner;
3082 struct super_block *sb = calldata->state->inode->i_sb;
3084 if (calldata->lr.roc)
3085 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3086 calldata->res.lr_ret);
3087 nfs4_put_open_state(calldata->state);
3088 nfs_free_seqid(calldata->arg.seqid);
3089 nfs4_put_state_owner(sp);
3090 nfs_sb_deactive(sb);
3094 static void nfs4_close_done(struct rpc_task *task, void *data)
3096 struct nfs4_closedata *calldata = data;
3097 struct nfs4_state *state = calldata->state;
3098 struct nfs_server *server = NFS_SERVER(calldata->inode);
3099 nfs4_stateid *res_stateid = NULL;
3101 dprintk("%s: begin!\n", __func__);
3102 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3104 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
3106 /* Handle Layoutreturn errors */
3107 if (calldata->arg.lr_args && task->tk_status != 0) {
3108 switch (calldata->res.lr_ret) {
3110 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3113 calldata->arg.lr_args = NULL;
3114 calldata->res.lr_res = NULL;
3116 case -NFS4ERR_ADMIN_REVOKED:
3117 case -NFS4ERR_DELEG_REVOKED:
3118 case -NFS4ERR_EXPIRED:
3119 case -NFS4ERR_BAD_STATEID:
3120 case -NFS4ERR_OLD_STATEID:
3121 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3122 case -NFS4ERR_WRONG_CRED:
3123 calldata->arg.lr_args = NULL;
3124 calldata->res.lr_res = NULL;
3125 calldata->res.lr_ret = 0;
3126 rpc_restart_call_prepare(task);
3131 /* hmm. we are done with the inode, and in the process of freeing
3132 * the state_owner. we keep this around to process errors
3134 switch (task->tk_status) {
3136 res_stateid = &calldata->res.stateid;
3137 renew_lease(server, calldata->timestamp);
3139 case -NFS4ERR_ACCESS:
3140 if (calldata->arg.bitmask != NULL) {
3141 calldata->arg.bitmask = NULL;
3142 calldata->res.fattr = NULL;
3143 task->tk_status = 0;
3144 rpc_restart_call_prepare(task);
3149 case -NFS4ERR_ADMIN_REVOKED:
3150 case -NFS4ERR_STALE_STATEID:
3151 case -NFS4ERR_EXPIRED:
3152 nfs4_free_revoked_stateid(server,
3153 &calldata->arg.stateid,
3154 task->tk_msg.rpc_cred);
3155 case -NFS4ERR_OLD_STATEID:
3156 case -NFS4ERR_BAD_STATEID:
3157 if (!nfs4_stateid_match(&calldata->arg.stateid,
3158 &state->open_stateid)) {
3159 rpc_restart_call_prepare(task);
3162 if (calldata->arg.fmode == 0)
3165 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
3166 rpc_restart_call_prepare(task);
3170 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3171 res_stateid, calldata->arg.fmode);
3173 nfs_release_seqid(calldata->arg.seqid);
3174 nfs_refresh_inode(calldata->inode, &calldata->fattr);
3175 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
3178 static void nfs4_close_prepare(struct rpc_task *task, void *data)
3180 struct nfs4_closedata *calldata = data;
3181 struct nfs4_state *state = calldata->state;
3182 struct inode *inode = calldata->inode;
3183 bool is_rdonly, is_wronly, is_rdwr;
3186 dprintk("%s: begin!\n", __func__);
3187 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3190 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
3191 spin_lock(&state->owner->so_lock);
3192 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3193 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3194 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
3195 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
3196 /* Calculate the change in open mode */
3197 calldata->arg.fmode = 0;
3198 if (state->n_rdwr == 0) {
3199 if (state->n_rdonly == 0)
3200 call_close |= is_rdonly;
3202 calldata->arg.fmode |= FMODE_READ;
3203 if (state->n_wronly == 0)
3204 call_close |= is_wronly;
3206 calldata->arg.fmode |= FMODE_WRITE;
3207 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3208 call_close |= is_rdwr;
3210 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3212 if (!nfs4_valid_open_stateid(state) ||
3213 test_bit(NFS_OPEN_STATE, &state->flags) == 0)
3215 spin_unlock(&state->owner->so_lock);
3218 /* Note: exit _without_ calling nfs4_close_done */
3222 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
3223 nfs_release_seqid(calldata->arg.seqid);
3227 if (calldata->arg.fmode == 0)
3228 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
3230 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
3231 /* Close-to-open cache consistency revalidation */
3232 if (!nfs4_have_delegation(inode, FMODE_READ))
3233 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3235 calldata->arg.bitmask = NULL;
3238 calldata->arg.share_access =
3239 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3240 calldata->arg.fmode, 0);
3242 if (calldata->res.fattr == NULL)
3243 calldata->arg.bitmask = NULL;
3244 else if (calldata->arg.bitmask == NULL)
3245 calldata->res.fattr = NULL;
3246 calldata->timestamp = jiffies;
3247 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
3248 &calldata->arg.seq_args,
3249 &calldata->res.seq_res,
3251 nfs_release_seqid(calldata->arg.seqid);
3252 dprintk("%s: done!\n", __func__);
3255 task->tk_action = NULL;
3257 nfs4_sequence_done(task, &calldata->res.seq_res);
3260 static const struct rpc_call_ops nfs4_close_ops = {
3261 .rpc_call_prepare = nfs4_close_prepare,
3262 .rpc_call_done = nfs4_close_done,
3263 .rpc_release = nfs4_free_closedata,
3267 * It is possible for data to be read/written from a mem-mapped file
3268 * after the sys_close call (which hits the vfs layer as a flush).
3269 * This means that we can't safely call nfsv4 close on a file until
3270 * the inode is cleared. This in turn means that we are not good
3271 * NFSv4 citizens - we do not indicate to the server to update the file's
3272 * share state even when we are done with one of the three share
3273 * stateid's in the inode.
3275 * NOTE: Caller must be holding the sp->so_owner semaphore!
3277 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
3279 struct nfs_server *server = NFS_SERVER(state->inode);
3280 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
3281 struct nfs4_closedata *calldata;
3282 struct nfs4_state_owner *sp = state->owner;
3283 struct rpc_task *task;
3284 struct rpc_message msg = {
3285 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3286 .rpc_cred = state->owner->so_cred,
3288 struct rpc_task_setup task_setup_data = {
3289 .rpc_client = server->client,
3290 .rpc_message = &msg,
3291 .callback_ops = &nfs4_close_ops,
3292 .workqueue = nfsiod_workqueue,
3293 .flags = RPC_TASK_ASYNC,
3295 int status = -ENOMEM;
3297 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3298 &task_setup_data.rpc_client, &msg);
3300 calldata = kzalloc(sizeof(*calldata), gfp_mask);
3301 if (calldata == NULL)
3303 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
3304 calldata->inode = state->inode;
3305 calldata->state = state;
3306 calldata->arg.fh = NFS_FH(state->inode);
3307 /* Serialization for the sequence id */
3308 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3309 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
3310 if (IS_ERR(calldata->arg.seqid))
3311 goto out_free_calldata;
3312 nfs_fattr_init(&calldata->fattr);
3313 calldata->arg.fmode = 0;
3314 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
3315 calldata->res.fattr = &calldata->fattr;
3316 calldata->res.seqid = calldata->arg.seqid;
3317 calldata->res.server = server;
3318 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3319 calldata->lr.roc = pnfs_roc(state->inode,
3320 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3321 if (calldata->lr.roc) {
3322 calldata->arg.lr_args = &calldata->lr.arg;
3323 calldata->res.lr_res = &calldata->lr.res;
3325 nfs_sb_active(calldata->inode->i_sb);
3327 msg.rpc_argp = &calldata->arg;
3328 msg.rpc_resp = &calldata->res;
3329 task_setup_data.callback_data = calldata;
3330 task = rpc_run_task(&task_setup_data);
3332 return PTR_ERR(task);
3335 status = rpc_wait_for_completion_task(task);
3341 nfs4_put_open_state(state);
3342 nfs4_put_state_owner(sp);
3346 static struct inode *
3347 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3348 int open_flags, struct iattr *attr, int *opened)
3350 struct nfs4_state *state;
3351 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3353 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
3355 /* Protect against concurrent sillydeletes */
3356 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
3358 nfs4_label_release_security(label);
3361 return ERR_CAST(state);
3362 return state->inode;
3365 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
3367 if (ctx->state == NULL)
3370 nfs4_close_sync(ctx->state, ctx->mode);
3372 nfs4_close_state(ctx->state, ctx->mode);
3375 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3376 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3377 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
3379 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3381 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
3382 struct nfs4_server_caps_arg args = {
3386 struct nfs4_server_caps_res res = {};
3387 struct rpc_message msg = {
3388 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
3395 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3396 FATTR4_WORD0_FH_EXPIRE_TYPE |
3397 FATTR4_WORD0_LINK_SUPPORT |
3398 FATTR4_WORD0_SYMLINK_SUPPORT |
3399 FATTR4_WORD0_ACLSUPPORT;
3401 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3403 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3405 /* Sanity check the server answers */
3406 switch (minorversion) {
3408 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3409 res.attr_bitmask[2] = 0;
3412 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3415 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3417 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
3418 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3419 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3420 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3421 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
3422 NFS_CAP_CTIME|NFS_CAP_MTIME|
3423 NFS_CAP_SECURITY_LABEL);
3424 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3425 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3426 server->caps |= NFS_CAP_ACLS;
3427 if (res.has_links != 0)
3428 server->caps |= NFS_CAP_HARDLINKS;
3429 if (res.has_symlinks != 0)
3430 server->caps |= NFS_CAP_SYMLINKS;
3431 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3432 server->caps |= NFS_CAP_FILEID;
3433 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3434 server->caps |= NFS_CAP_MODE;
3435 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3436 server->caps |= NFS_CAP_NLINK;
3437 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3438 server->caps |= NFS_CAP_OWNER;
3439 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3440 server->caps |= NFS_CAP_OWNER_GROUP;
3441 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3442 server->caps |= NFS_CAP_ATIME;
3443 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3444 server->caps |= NFS_CAP_CTIME;
3445 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3446 server->caps |= NFS_CAP_MTIME;
3447 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3448 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3449 server->caps |= NFS_CAP_SECURITY_LABEL;
3451 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3452 sizeof(server->attr_bitmask));
3453 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3455 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3456 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3457 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
3458 server->cache_consistency_bitmask[2] = 0;
3460 /* Avoid a regression due to buggy server */
3461 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3462 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
3463 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3464 sizeof(server->exclcreat_bitmask));
3466 server->acl_bitmask = res.acl_bitmask;
3467 server->fh_expire_type = res.fh_expire_type;
3473 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3475 struct nfs4_exception exception = { };
3478 err = nfs4_handle_exception(server,
3479 _nfs4_server_capabilities(server, fhandle),
3481 } while (exception.retry);
3485 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3486 struct nfs_fsinfo *info)
3489 struct nfs4_lookup_root_arg args = {
3492 struct nfs4_lookup_res res = {
3494 .fattr = info->fattr,
3497 struct rpc_message msg = {
3498 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3503 bitmask[0] = nfs4_fattr_bitmap[0];
3504 bitmask[1] = nfs4_fattr_bitmap[1];
3506 * Process the label in the upcoming getfattr
3508 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3510 nfs_fattr_init(info->fattr);
3511 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3514 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3515 struct nfs_fsinfo *info)
3517 struct nfs4_exception exception = { };
3520 err = _nfs4_lookup_root(server, fhandle, info);
3521 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
3524 case -NFS4ERR_WRONGSEC:
3527 err = nfs4_handle_exception(server, err, &exception);
3529 } while (exception.retry);
3534 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3535 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3537 struct rpc_auth_create_args auth_args = {
3538 .pseudoflavor = flavor,
3540 struct rpc_auth *auth;
3542 auth = rpcauth_create(&auth_args, server->client);
3545 return nfs4_lookup_root(server, fhandle, info);
3549 * Retry pseudoroot lookup with various security flavors. We do this when:
3551 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3552 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3554 * Returns zero on success, or a negative NFS4ERR value, or a
3555 * negative errno value.
3557 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3558 struct nfs_fsinfo *info)
3560 /* Per 3530bis 15.33.5 */
3561 static const rpc_authflavor_t flav_array[] = {
3565 RPC_AUTH_UNIX, /* courtesy */
3568 int status = -EPERM;
3571 if (server->auth_info.flavor_len > 0) {
3572 /* try each flavor specified by user */
3573 for (i = 0; i < server->auth_info.flavor_len; i++) {
3574 status = nfs4_lookup_root_sec(server, fhandle, info,
3575 server->auth_info.flavors[i]);
3576 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3581 /* no flavors specified by user, try default list */
3582 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3583 status = nfs4_lookup_root_sec(server, fhandle, info,
3585 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3592 * -EACCESS could mean that the user doesn't have correct permissions
3593 * to access the mount. It could also mean that we tried to mount
3594 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3595 * existing mount programs don't handle -EACCES very well so it should
3596 * be mapped to -EPERM instead.
3598 if (status == -EACCES)
3604 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3605 * @server: initialized nfs_server handle
3606 * @fhandle: we fill in the pseudo-fs root file handle
3607 * @info: we fill in an FSINFO struct
3608 * @auth_probe: probe the auth flavours
3610 * Returns zero on success, or a negative errno.
3612 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
3613 struct nfs_fsinfo *info,
3619 status = nfs4_lookup_root(server, fhandle, info);
3621 if (auth_probe || status == NFS4ERR_WRONGSEC)
3622 status = server->nfs_client->cl_mvops->find_root_sec(server,
3626 status = nfs4_server_capabilities(server, fhandle);
3628 status = nfs4_do_fsinfo(server, fhandle, info);
3630 return nfs4_map_errors(status);
3633 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3634 struct nfs_fsinfo *info)
3637 struct nfs_fattr *fattr = info->fattr;
3638 struct nfs4_label *label = NULL;
3640 error = nfs4_server_capabilities(server, mntfh);
3642 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3646 label = nfs4_label_alloc(server, GFP_KERNEL);
3648 return PTR_ERR(label);
3650 error = nfs4_proc_getattr(server, mntfh, fattr, label);
3652 dprintk("nfs4_get_root: getattr error = %d\n", -error);
3653 goto err_free_label;
3656 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3657 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3658 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3661 nfs4_label_free(label);
3667 * Get locations and (maybe) other attributes of a referral.
3668 * Note that we'll actually follow the referral later when
3669 * we detect fsid mismatch in inode revalidation
3671 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3672 const struct qstr *name, struct nfs_fattr *fattr,
3673 struct nfs_fh *fhandle)
3675 int status = -ENOMEM;
3676 struct page *page = NULL;
3677 struct nfs4_fs_locations *locations = NULL;
3679 page = alloc_page(GFP_KERNEL);
3682 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3683 if (locations == NULL)
3686 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
3691 * If the fsid didn't change, this is a migration event, not a
3692 * referral. Cause us to drop into the exception handler, which
3693 * will kick off migration recovery.
3695 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
3696 dprintk("%s: server did not return a different fsid for"
3697 " a referral at %s\n", __func__, name->name);
3698 status = -NFS4ERR_MOVED;
3701 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3702 nfs_fixup_referral_attributes(&locations->fattr);
3704 /* replace the lookup nfs_fattr with the locations nfs_fattr */
3705 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
3706 memset(fhandle, 0, sizeof(struct nfs_fh));
3714 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3715 struct nfs_fattr *fattr, struct nfs4_label *label)
3717 struct nfs4_getattr_arg args = {
3719 .bitmask = server->attr_bitmask,
3721 struct nfs4_getattr_res res = {
3726 struct rpc_message msg = {
3727 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3732 args.bitmask = nfs4_bitmask(server, label);
3734 nfs_fattr_init(fattr);
3735 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3738 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3739 struct nfs_fattr *fattr, struct nfs4_label *label)
3741 struct nfs4_exception exception = { };
3744 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3745 trace_nfs4_getattr(server, fhandle, fattr, err);
3746 err = nfs4_handle_exception(server, err,
3748 } while (exception.retry);
3753 * The file is not closed if it is opened due to the a request to change
3754 * the size of the file. The open call will not be needed once the
3755 * VFS layer lookup-intents are implemented.
3757 * Close is called when the inode is destroyed.
3758 * If we haven't opened the file for O_WRONLY, we
3759 * need to in the size_change case to obtain a stateid.
3762 * Because OPEN is always done by name in nfsv4, it is
3763 * possible that we opened a different file by the same
3764 * name. We can recognize this race condition, but we
3765 * can't do anything about it besides returning an error.
3767 * This will be fixed with VFS changes (lookup-intent).
3770 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3771 struct iattr *sattr)
3773 struct inode *inode = d_inode(dentry);
3774 struct rpc_cred *cred = NULL;
3775 struct nfs_open_context *ctx = NULL;
3776 struct nfs4_label *label = NULL;
3779 if (pnfs_ld_layoutret_on_setattr(inode) &&
3780 sattr->ia_valid & ATTR_SIZE &&
3781 sattr->ia_size < i_size_read(inode))
3782 pnfs_commit_and_return_layout(inode);
3784 nfs_fattr_init(fattr);
3786 /* Deal with open(O_TRUNC) */
3787 if (sattr->ia_valid & ATTR_OPEN)
3788 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
3790 /* Optimization: if the end result is no change, don't RPC */
3791 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
3794 /* Search for an existing open(O_WRITE) file */
3795 if (sattr->ia_valid & ATTR_FILE) {
3797 ctx = nfs_file_open_context(sattr->ia_file);
3802 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3804 return PTR_ERR(label);
3806 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
3808 nfs_setattr_update_inode(inode, sattr, fattr);
3809 nfs_setsecurity(inode, fattr, label);
3811 nfs4_label_free(label);
3815 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3816 const struct qstr *name, struct nfs_fh *fhandle,
3817 struct nfs_fattr *fattr, struct nfs4_label *label)
3819 struct nfs_server *server = NFS_SERVER(dir);
3821 struct nfs4_lookup_arg args = {
3822 .bitmask = server->attr_bitmask,
3823 .dir_fh = NFS_FH(dir),
3826 struct nfs4_lookup_res res = {
3832 struct rpc_message msg = {
3833 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3838 args.bitmask = nfs4_bitmask(server, label);
3840 nfs_fattr_init(fattr);
3842 dprintk("NFS call lookup %s\n", name->name);
3843 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
3844 dprintk("NFS reply lookup: %d\n", status);
3848 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
3850 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
3851 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
3852 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3856 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3857 const struct qstr *name, struct nfs_fh *fhandle,
3858 struct nfs_fattr *fattr, struct nfs4_label *label)
3860 struct nfs4_exception exception = { };
3861 struct rpc_clnt *client = *clnt;
3864 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
3865 trace_nfs4_lookup(dir, name, err);
3867 case -NFS4ERR_BADNAME:
3870 case -NFS4ERR_MOVED:
3871 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
3872 if (err == -NFS4ERR_MOVED)
3873 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3875 case -NFS4ERR_WRONGSEC:
3877 if (client != *clnt)
3879 client = nfs4_negotiate_security(client, dir, name);
3881 return PTR_ERR(client);
3883 exception.retry = 1;
3886 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3888 } while (exception.retry);
3893 else if (client != *clnt)
3894 rpc_shutdown_client(client);
3899 static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
3900 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3901 struct nfs4_label *label)
3904 struct rpc_clnt *client = NFS_CLIENT(dir);
3906 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
3907 if (client != NFS_CLIENT(dir)) {
3908 rpc_shutdown_client(client);
3909 nfs_fixup_secinfo_attributes(fattr);
3915 nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
3916 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3918 struct rpc_clnt *client = NFS_CLIENT(dir);
3921 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
3923 return ERR_PTR(status);
3924 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
3927 static int _nfs4_proc_lookupp(struct inode *inode,
3928 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3929 struct nfs4_label *label)
3931 struct rpc_clnt *clnt = NFS_CLIENT(inode);
3932 struct nfs_server *server = NFS_SERVER(inode);
3934 struct nfs4_lookupp_arg args = {
3935 .bitmask = server->attr_bitmask,
3936 .fh = NFS_FH(inode),
3938 struct nfs4_lookupp_res res = {
3944 struct rpc_message msg = {
3945 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
3950 args.bitmask = nfs4_bitmask(server, label);
3952 nfs_fattr_init(fattr);
3954 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
3955 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
3957 dprintk("NFS reply lookupp: %d\n", status);
3961 static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
3962 struct nfs_fattr *fattr, struct nfs4_label *label)
3964 struct nfs4_exception exception = { };
3967 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
3968 trace_nfs4_lookupp(inode, err);
3969 err = nfs4_handle_exception(NFS_SERVER(inode), err,
3971 } while (exception.retry);
3975 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3977 struct nfs_server *server = NFS_SERVER(inode);
3978 struct nfs4_accessargs args = {
3979 .fh = NFS_FH(inode),
3980 .bitmask = server->cache_consistency_bitmask,
3982 struct nfs4_accessres res = {
3985 struct rpc_message msg = {
3986 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3989 .rpc_cred = entry->cred,
3991 int mode = entry->mask;
3995 * Determine which access bits we want to ask for...
3997 if (mode & MAY_READ)
3998 args.access |= NFS4_ACCESS_READ;
3999 if (S_ISDIR(inode->i_mode)) {
4000 if (mode & MAY_WRITE)
4001 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
4002 if (mode & MAY_EXEC)
4003 args.access |= NFS4_ACCESS_LOOKUP;
4005 if (mode & MAY_WRITE)
4006 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
4007 if (mode & MAY_EXEC)
4008 args.access |= NFS4_ACCESS_EXECUTE;
4011 res.fattr = nfs_alloc_fattr();
4012 if (res.fattr == NULL)
4015 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4017 nfs_access_set_mask(entry, res.access);
4018 nfs_refresh_inode(inode, res.fattr);
4020 nfs_free_fattr(res.fattr);
4024 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4026 struct nfs4_exception exception = { };
4029 err = _nfs4_proc_access(inode, entry);
4030 trace_nfs4_access(inode, err);
4031 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4033 } while (exception.retry);
4038 * TODO: For the time being, we don't try to get any attributes
4039 * along with any of the zero-copy operations READ, READDIR,
4042 * In the case of the first three, we want to put the GETATTR
4043 * after the read-type operation -- this is because it is hard
4044 * to predict the length of a GETATTR response in v4, and thus
4045 * align the READ data correctly. This means that the GETATTR
4046 * may end up partially falling into the page cache, and we should
4047 * shift it into the 'tail' of the xdr_buf before processing.
4048 * To do this efficiently, we need to know the total length
4049 * of data received, which doesn't seem to be available outside
4052 * In the case of WRITE, we also want to put the GETATTR after
4053 * the operation -- in this case because we want to make sure
4054 * we get the post-operation mtime and size.
4056 * Both of these changes to the XDR layer would in fact be quite
4057 * minor, but I decided to leave them for a subsequent patch.
4059 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4060 unsigned int pgbase, unsigned int pglen)
4062 struct nfs4_readlink args = {
4063 .fh = NFS_FH(inode),
4068 struct nfs4_readlink_res res;
4069 struct rpc_message msg = {
4070 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4075 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
4078 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4079 unsigned int pgbase, unsigned int pglen)
4081 struct nfs4_exception exception = { };
4084 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4085 trace_nfs4_readlink(inode, err);
4086 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4088 } while (exception.retry);
4093 * This is just for mknod. open(O_CREAT) will always do ->open_context().
4096 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
4099 struct nfs_server *server = NFS_SERVER(dir);
4100 struct nfs4_label l, *ilabel = NULL;
4101 struct nfs_open_context *ctx;
4102 struct nfs4_state *state;
4105 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
4107 return PTR_ERR(ctx);
4109 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4111 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4112 sattr->ia_mode &= ~current_umask();
4113 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
4114 if (IS_ERR(state)) {
4115 status = PTR_ERR(state);
4119 nfs4_label_release_security(ilabel);
4120 put_nfs_open_context(ctx);
4124 static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
4126 struct nfs_server *server = NFS_SERVER(dir);
4127 struct nfs_removeargs args = {
4131 struct nfs_removeres res = {
4134 struct rpc_message msg = {
4135 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4139 unsigned long timestamp = jiffies;
4142 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
4144 update_changeattr(dir, &res.cinfo, timestamp);
4148 static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
4150 struct nfs4_exception exception = { };
4153 err = _nfs4_proc_remove(dir, name);
4154 trace_nfs4_remove(dir, name, err);
4155 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4157 } while (exception.retry);
4161 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
4163 struct nfs_server *server = NFS_SERVER(dir);
4164 struct nfs_removeargs *args = msg->rpc_argp;
4165 struct nfs_removeres *res = msg->rpc_resp;
4167 res->server = server;
4168 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
4169 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
4171 nfs_fattr_init(res->dir_attr);
4174 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4176 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
4177 &data->args.seq_args,
4182 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
4184 struct nfs_unlinkdata *data = task->tk_calldata;
4185 struct nfs_removeres *res = &data->res;
4187 if (!nfs4_sequence_done(task, &res->seq_res))
4189 if (nfs4_async_handle_error(task, res->server, NULL,
4190 &data->timeout) == -EAGAIN)
4192 if (task->tk_status == 0)
4193 update_changeattr(dir, &res->cinfo, res->dir_attr->time_start);
4197 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4199 struct nfs_server *server = NFS_SERVER(dir);
4200 struct nfs_renameargs *arg = msg->rpc_argp;
4201 struct nfs_renameres *res = msg->rpc_resp;
4203 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
4204 res->server = server;
4205 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
4208 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4210 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
4211 &data->args.seq_args,
4216 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4217 struct inode *new_dir)
4219 struct nfs_renamedata *data = task->tk_calldata;
4220 struct nfs_renameres *res = &data->res;
4222 if (!nfs4_sequence_done(task, &res->seq_res))
4224 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
4227 if (task->tk_status == 0) {
4228 update_changeattr(old_dir, &res->old_cinfo, res->old_fattr->time_start);
4229 if (new_dir != old_dir)
4230 update_changeattr(new_dir, &res->new_cinfo, res->new_fattr->time_start);
4235 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4237 struct nfs_server *server = NFS_SERVER(inode);
4238 struct nfs4_link_arg arg = {
4239 .fh = NFS_FH(inode),
4240 .dir_fh = NFS_FH(dir),
4242 .bitmask = server->attr_bitmask,
4244 struct nfs4_link_res res = {
4248 struct rpc_message msg = {
4249 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4253 int status = -ENOMEM;
4255 res.fattr = nfs_alloc_fattr();
4256 if (res.fattr == NULL)
4259 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4260 if (IS_ERR(res.label)) {
4261 status = PTR_ERR(res.label);
4264 arg.bitmask = nfs4_bitmask(server, res.label);
4266 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4268 update_changeattr(dir, &res.cinfo, res.fattr->time_start);
4269 status = nfs_post_op_update_inode(inode, res.fattr);
4271 nfs_setsecurity(inode, res.fattr, res.label);
4275 nfs4_label_free(res.label);
4278 nfs_free_fattr(res.fattr);
4282 static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4284 struct nfs4_exception exception = { };
4287 err = nfs4_handle_exception(NFS_SERVER(inode),
4288 _nfs4_proc_link(inode, dir, name),
4290 } while (exception.retry);
4294 struct nfs4_createdata {
4295 struct rpc_message msg;
4296 struct nfs4_create_arg arg;
4297 struct nfs4_create_res res;
4299 struct nfs_fattr fattr;
4300 struct nfs4_label *label;
4303 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
4304 const struct qstr *name, struct iattr *sattr, u32 ftype)
4306 struct nfs4_createdata *data;
4308 data = kzalloc(sizeof(*data), GFP_KERNEL);
4310 struct nfs_server *server = NFS_SERVER(dir);
4312 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4313 if (IS_ERR(data->label))
4316 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4317 data->msg.rpc_argp = &data->arg;
4318 data->msg.rpc_resp = &data->res;
4319 data->arg.dir_fh = NFS_FH(dir);
4320 data->arg.server = server;
4321 data->arg.name = name;
4322 data->arg.attrs = sattr;
4323 data->arg.ftype = ftype;
4324 data->arg.bitmask = nfs4_bitmask(server, data->label);
4325 data->arg.umask = current_umask();
4326 data->res.server = server;
4327 data->res.fh = &data->fh;
4328 data->res.fattr = &data->fattr;
4329 data->res.label = data->label;
4330 nfs_fattr_init(data->res.fattr);
4338 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4340 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
4341 &data->arg.seq_args, &data->res.seq_res, 1);
4343 update_changeattr(dir, &data->res.dir_cinfo,
4344 data->res.fattr->time_start);
4345 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
4350 static void nfs4_free_createdata(struct nfs4_createdata *data)
4352 nfs4_label_free(data->label);
4356 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4357 struct page *page, unsigned int len, struct iattr *sattr,
4358 struct nfs4_label *label)
4360 struct nfs4_createdata *data;
4361 int status = -ENAMETOOLONG;
4363 if (len > NFS4_MAXPATHLEN)
4367 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4371 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4372 data->arg.u.symlink.pages = &page;
4373 data->arg.u.symlink.len = len;
4374 data->arg.label = label;
4376 status = nfs4_do_create(dir, dentry, data);
4378 nfs4_free_createdata(data);
4383 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4384 struct page *page, unsigned int len, struct iattr *sattr)
4386 struct nfs4_exception exception = { };
4387 struct nfs4_label l, *label = NULL;
4390 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4393 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4394 trace_nfs4_symlink(dir, &dentry->d_name, err);
4395 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4397 } while (exception.retry);
4399 nfs4_label_release_security(label);
4403 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4404 struct iattr *sattr, struct nfs4_label *label)
4406 struct nfs4_createdata *data;
4407 int status = -ENOMEM;
4409 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4413 data->arg.label = label;
4414 status = nfs4_do_create(dir, dentry, data);
4416 nfs4_free_createdata(data);
4421 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4422 struct iattr *sattr)
4424 struct nfs_server *server = NFS_SERVER(dir);
4425 struct nfs4_exception exception = { };
4426 struct nfs4_label l, *label = NULL;
4429 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4431 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4432 sattr->ia_mode &= ~current_umask();
4434 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4435 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4436 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4438 } while (exception.retry);
4439 nfs4_label_release_security(label);
4444 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
4445 u64 cookie, struct page **pages, unsigned int count, bool plus)
4447 struct inode *dir = d_inode(dentry);
4448 struct nfs_server *server = NFS_SERVER(dir);
4449 struct nfs4_readdir_arg args = {
4456 struct nfs4_readdir_res res;
4457 struct rpc_message msg = {
4458 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4465 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4467 (unsigned long long)cookie);
4468 if (!(server->caps & NFS_CAP_SECURITY_LABEL))
4469 args.bitmask = server->attr_bitmask_nl;
4471 args.bitmask = server->attr_bitmask;
4473 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
4474 res.pgbase = args.pgbase;
4475 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
4478 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
4479 status += args.pgbase;
4482 nfs_invalidate_atime(dir);
4484 dprintk("%s: returns %d\n", __func__, status);
4488 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
4489 u64 cookie, struct page **pages, unsigned int count, bool plus)
4491 struct nfs4_exception exception = { };
4494 err = _nfs4_proc_readdir(dentry, cred, cookie,
4495 pages, count, plus);
4496 trace_nfs4_readdir(d_inode(dentry), err);
4497 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
4499 } while (exception.retry);
4503 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4504 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
4506 struct nfs4_createdata *data;
4507 int mode = sattr->ia_mode;
4508 int status = -ENOMEM;
4510 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4515 data->arg.ftype = NF4FIFO;
4516 else if (S_ISBLK(mode)) {
4517 data->arg.ftype = NF4BLK;
4518 data->arg.u.device.specdata1 = MAJOR(rdev);
4519 data->arg.u.device.specdata2 = MINOR(rdev);
4521 else if (S_ISCHR(mode)) {
4522 data->arg.ftype = NF4CHR;
4523 data->arg.u.device.specdata1 = MAJOR(rdev);
4524 data->arg.u.device.specdata2 = MINOR(rdev);
4525 } else if (!S_ISSOCK(mode)) {
4530 data->arg.label = label;
4531 status = nfs4_do_create(dir, dentry, data);
4533 nfs4_free_createdata(data);
4538 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4539 struct iattr *sattr, dev_t rdev)
4541 struct nfs_server *server = NFS_SERVER(dir);
4542 struct nfs4_exception exception = { };
4543 struct nfs4_label l, *label = NULL;
4546 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4548 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4549 sattr->ia_mode &= ~current_umask();
4551 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4552 trace_nfs4_mknod(dir, &dentry->d_name, err);
4553 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4555 } while (exception.retry);
4557 nfs4_label_release_security(label);
4562 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4563 struct nfs_fsstat *fsstat)
4565 struct nfs4_statfs_arg args = {
4567 .bitmask = server->attr_bitmask,
4569 struct nfs4_statfs_res res = {
4572 struct rpc_message msg = {
4573 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4578 nfs_fattr_init(fsstat->fattr);
4579 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4582 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4584 struct nfs4_exception exception = { };
4587 err = nfs4_handle_exception(server,
4588 _nfs4_proc_statfs(server, fhandle, fsstat),
4590 } while (exception.retry);
4594 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4595 struct nfs_fsinfo *fsinfo)
4597 struct nfs4_fsinfo_arg args = {
4599 .bitmask = server->attr_bitmask,
4601 struct nfs4_fsinfo_res res = {
4604 struct rpc_message msg = {
4605 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4610 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4613 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4615 struct nfs4_exception exception = { };
4616 unsigned long now = jiffies;
4620 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
4621 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
4623 nfs4_set_lease_period(server->nfs_client,
4624 fsinfo->lease_time * HZ,
4628 err = nfs4_handle_exception(server, err, &exception);
4629 } while (exception.retry);
4633 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4637 nfs_fattr_init(fsinfo->fattr);
4638 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
4640 /* block layout checks this! */
4641 server->pnfs_blksize = fsinfo->blksize;
4642 set_pnfs_layoutdriver(server, fhandle, fsinfo);
4648 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4649 struct nfs_pathconf *pathconf)
4651 struct nfs4_pathconf_arg args = {
4653 .bitmask = server->attr_bitmask,
4655 struct nfs4_pathconf_res res = {
4656 .pathconf = pathconf,
4658 struct rpc_message msg = {
4659 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4664 /* None of the pathconf attributes are mandatory to implement */
4665 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4666 memset(pathconf, 0, sizeof(*pathconf));
4670 nfs_fattr_init(pathconf->fattr);
4671 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4674 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4675 struct nfs_pathconf *pathconf)
4677 struct nfs4_exception exception = { };
4681 err = nfs4_handle_exception(server,
4682 _nfs4_proc_pathconf(server, fhandle, pathconf),
4684 } while (exception.retry);
4688 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
4689 const struct nfs_open_context *ctx,
4690 const struct nfs_lock_context *l_ctx,
4693 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
4695 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4697 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4698 const struct nfs_open_context *ctx,
4699 const struct nfs_lock_context *l_ctx,
4702 nfs4_stateid current_stateid;
4704 /* If the current stateid represents a lost lock, then exit */
4705 if (nfs4_set_rw_stateid(¤t_stateid, ctx, l_ctx, fmode) == -EIO)
4707 return nfs4_stateid_match(stateid, ¤t_stateid);
4710 static bool nfs4_error_stateid_expired(int err)
4713 case -NFS4ERR_DELEG_REVOKED:
4714 case -NFS4ERR_ADMIN_REVOKED:
4715 case -NFS4ERR_BAD_STATEID:
4716 case -NFS4ERR_STALE_STATEID:
4717 case -NFS4ERR_OLD_STATEID:
4718 case -NFS4ERR_OPENMODE:
4719 case -NFS4ERR_EXPIRED:
4725 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
4727 struct nfs_server *server = NFS_SERVER(hdr->inode);
4729 trace_nfs4_read(hdr, task->tk_status);
4730 if (task->tk_status < 0) {
4731 struct nfs4_exception exception = {
4732 .inode = hdr->inode,
4733 .state = hdr->args.context->state,
4734 .stateid = &hdr->args.stateid,
4736 task->tk_status = nfs4_async_handle_exception(task,
4737 server, task->tk_status, &exception);
4738 if (exception.retry) {
4739 rpc_restart_call_prepare(task);
4744 if (task->tk_status > 0)
4745 renew_lease(server, hdr->timestamp);
4749 static bool nfs4_read_stateid_changed(struct rpc_task *task,
4750 struct nfs_pgio_args *args)
4753 if (!nfs4_error_stateid_expired(task->tk_status) ||
4754 nfs4_stateid_is_current(&args->stateid,
4759 rpc_restart_call_prepare(task);
4763 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
4766 dprintk("--> %s\n", __func__);
4768 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
4770 if (nfs4_read_stateid_changed(task, &hdr->args))
4772 if (task->tk_status > 0)
4773 nfs_invalidate_atime(hdr->inode);
4774 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4775 nfs4_read_done_cb(task, hdr);
4778 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4779 struct rpc_message *msg)
4781 hdr->timestamp = jiffies;
4782 if (!hdr->pgio_done_cb)
4783 hdr->pgio_done_cb = nfs4_read_done_cb;
4784 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
4785 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
4788 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4789 struct nfs_pgio_header *hdr)
4791 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
4792 &hdr->args.seq_args,
4796 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4797 hdr->args.lock_context,
4798 hdr->rw_mode) == -EIO)
4800 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
4805 static int nfs4_write_done_cb(struct rpc_task *task,
4806 struct nfs_pgio_header *hdr)
4808 struct inode *inode = hdr->inode;
4810 trace_nfs4_write(hdr, task->tk_status);
4811 if (task->tk_status < 0) {
4812 struct nfs4_exception exception = {
4813 .inode = hdr->inode,
4814 .state = hdr->args.context->state,
4815 .stateid = &hdr->args.stateid,
4817 task->tk_status = nfs4_async_handle_exception(task,
4818 NFS_SERVER(inode), task->tk_status,
4820 if (exception.retry) {
4821 rpc_restart_call_prepare(task);
4825 if (task->tk_status >= 0) {
4826 renew_lease(NFS_SERVER(inode), hdr->timestamp);
4827 nfs_writeback_update_inode(hdr);
4832 static bool nfs4_write_stateid_changed(struct rpc_task *task,
4833 struct nfs_pgio_args *args)
4836 if (!nfs4_error_stateid_expired(task->tk_status) ||
4837 nfs4_stateid_is_current(&args->stateid,
4842 rpc_restart_call_prepare(task);
4846 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
4848 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
4850 if (nfs4_write_stateid_changed(task, &hdr->args))
4852 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4853 nfs4_write_done_cb(task, hdr);
4857 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
4859 /* Don't request attributes for pNFS or O_DIRECT writes */
4860 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
4862 /* Otherwise, request attributes if and only if we don't hold
4865 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
4868 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4869 struct rpc_message *msg)
4871 struct nfs_server *server = NFS_SERVER(hdr->inode);
4873 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4874 hdr->args.bitmask = NULL;
4875 hdr->res.fattr = NULL;
4877 hdr->args.bitmask = server->cache_consistency_bitmask;
4879 if (!hdr->pgio_done_cb)
4880 hdr->pgio_done_cb = nfs4_write_done_cb;
4881 hdr->res.server = server;
4882 hdr->timestamp = jiffies;
4884 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
4885 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
4888 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4890 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
4891 &data->args.seq_args,
4896 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
4898 struct inode *inode = data->inode;
4900 trace_nfs4_commit(data, task->tk_status);
4901 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4902 NULL, NULL) == -EAGAIN) {
4903 rpc_restart_call_prepare(task);
4909 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
4911 if (!nfs4_sequence_done(task, &data->res.seq_res))
4913 return data->commit_done_cb(task, data);
4916 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
4918 struct nfs_server *server = NFS_SERVER(data->inode);
4920 if (data->commit_done_cb == NULL)
4921 data->commit_done_cb = nfs4_commit_done_cb;
4922 data->res.server = server;
4923 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
4924 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4927 struct nfs4_renewdata {
4928 struct nfs_client *client;
4929 unsigned long timestamp;
4933 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4934 * standalone procedure for queueing an asynchronous RENEW.
4936 static void nfs4_renew_release(void *calldata)
4938 struct nfs4_renewdata *data = calldata;
4939 struct nfs_client *clp = data->client;
4941 if (atomic_read(&clp->cl_count) > 1)
4942 nfs4_schedule_state_renewal(clp);
4943 nfs_put_client(clp);
4947 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
4949 struct nfs4_renewdata *data = calldata;
4950 struct nfs_client *clp = data->client;
4951 unsigned long timestamp = data->timestamp;
4953 trace_nfs4_renew_async(clp, task->tk_status);
4954 switch (task->tk_status) {
4957 case -NFS4ERR_LEASE_MOVED:
4958 nfs4_schedule_lease_moved_recovery(clp);
4961 /* Unless we're shutting down, schedule state recovery! */
4962 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4964 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
4965 nfs4_schedule_lease_recovery(clp);
4968 nfs4_schedule_path_down_recovery(clp);
4970 do_renew_lease(clp, timestamp);
4973 static const struct rpc_call_ops nfs4_renew_ops = {
4974 .rpc_call_done = nfs4_renew_done,
4975 .rpc_release = nfs4_renew_release,
4978 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
4980 struct rpc_message msg = {
4981 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4985 struct nfs4_renewdata *data;
4987 if (renew_flags == 0)
4989 if (!atomic_inc_not_zero(&clp->cl_count))
4991 data = kmalloc(sizeof(*data), GFP_NOFS);
4993 nfs_put_client(clp);
4997 data->timestamp = jiffies;
4998 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
4999 &nfs4_renew_ops, data);
5002 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
5004 struct rpc_message msg = {
5005 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5009 unsigned long now = jiffies;
5012 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5015 do_renew_lease(clp, now);
5019 static inline int nfs4_server_supports_acls(struct nfs_server *server)
5021 return server->caps & NFS_CAP_ACLS;
5024 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5025 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
5028 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
5030 static int buf_to_pages_noslab(const void *buf, size_t buflen,
5031 struct page **pages)
5033 struct page *newpage, **spages;
5039 len = min_t(size_t, PAGE_SIZE, buflen);
5040 newpage = alloc_page(GFP_KERNEL);
5042 if (newpage == NULL)
5044 memcpy(page_address(newpage), buf, len);
5049 } while (buflen != 0);
5055 __free_page(spages[rc-1]);
5059 struct nfs4_cached_acl {
5065 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
5067 struct nfs_inode *nfsi = NFS_I(inode);
5069 spin_lock(&inode->i_lock);
5070 kfree(nfsi->nfs4_acl);
5071 nfsi->nfs4_acl = acl;
5072 spin_unlock(&inode->i_lock);
5075 static void nfs4_zap_acl_attr(struct inode *inode)
5077 nfs4_set_cached_acl(inode, NULL);
5080 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5082 struct nfs_inode *nfsi = NFS_I(inode);
5083 struct nfs4_cached_acl *acl;
5086 spin_lock(&inode->i_lock);
5087 acl = nfsi->nfs4_acl;
5090 if (buf == NULL) /* user is just asking for length */
5092 if (acl->cached == 0)
5094 ret = -ERANGE; /* see getxattr(2) man page */
5095 if (acl->len > buflen)
5097 memcpy(buf, acl->data, acl->len);
5101 spin_unlock(&inode->i_lock);
5105 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
5107 struct nfs4_cached_acl *acl;
5108 size_t buflen = sizeof(*acl) + acl_len;
5110 if (buflen <= PAGE_SIZE) {
5111 acl = kmalloc(buflen, GFP_KERNEL);
5115 _copy_from_pages(acl->data, pages, pgbase, acl_len);
5117 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5124 nfs4_set_cached_acl(inode, acl);
5128 * The getxattr API returns the required buffer length when called with a
5129 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5130 * the required buf. On a NULL buf, we send a page of data to the server
5131 * guessing that the ACL request can be serviced by a page. If so, we cache
5132 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5133 * the cache. If not so, we throw away the page, and cache the required
5134 * length. The next getxattr call will then produce another round trip to
5135 * the server, this time with the input buf of the required size.
5137 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5139 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
5140 struct nfs_getaclargs args = {
5141 .fh = NFS_FH(inode),
5145 struct nfs_getaclres res = {
5148 struct rpc_message msg = {
5149 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5153 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
5154 int ret = -ENOMEM, i;
5156 if (npages > ARRAY_SIZE(pages))
5159 for (i = 0; i < npages; i++) {
5160 pages[i] = alloc_page(GFP_KERNEL);
5165 /* for decoding across pages */
5166 res.acl_scratch = alloc_page(GFP_KERNEL);
5167 if (!res.acl_scratch)
5170 args.acl_len = npages * PAGE_SIZE;
5172 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
5173 __func__, buf, buflen, npages, args.acl_len);
5174 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5175 &msg, &args.seq_args, &res.seq_res, 0);
5179 /* Handle the case where the passed-in buffer is too short */
5180 if (res.acl_flags & NFS4_ACL_TRUNC) {
5181 /* Did the user only issue a request for the acl length? */
5187 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
5189 if (res.acl_len > buflen) {
5193 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
5198 for (i = 0; i < npages; i++)
5200 __free_page(pages[i]);
5201 if (res.acl_scratch)
5202 __free_page(res.acl_scratch);
5206 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5208 struct nfs4_exception exception = { };
5211 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
5212 trace_nfs4_get_acl(inode, ret);
5215 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5216 } while (exception.retry);
5220 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5222 struct nfs_server *server = NFS_SERVER(inode);
5225 if (!nfs4_server_supports_acls(server))
5227 ret = nfs_revalidate_inode(server, inode);
5230 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5231 nfs_zap_acl_cache(inode);
5232 ret = nfs4_read_cached_acl(inode, buf, buflen);
5234 /* -ENOENT is returned if there is no ACL or if there is an ACL
5235 * but no cached acl data, just the acl length */
5237 return nfs4_get_acl_uncached(inode, buf, buflen);
5240 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5242 struct nfs_server *server = NFS_SERVER(inode);
5243 struct page *pages[NFS4ACL_MAXPAGES];
5244 struct nfs_setaclargs arg = {
5245 .fh = NFS_FH(inode),
5249 struct nfs_setaclres res;
5250 struct rpc_message msg = {
5251 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5255 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
5258 /* You can't remove system.nfs4_acl: */
5261 if (!nfs4_server_supports_acls(server))
5263 if (npages > ARRAY_SIZE(pages))
5265 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
5268 nfs4_inode_return_delegation(inode);
5269 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5272 * Free each page after tx, so the only ref left is
5273 * held by the network stack
5276 put_page(pages[i-1]);
5279 * Acl update can result in inode attribute update.
5280 * so mark the attribute cache invalid.
5282 spin_lock(&inode->i_lock);
5283 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5284 spin_unlock(&inode->i_lock);
5285 nfs_access_zap_cache(inode);
5286 nfs_zap_acl_cache(inode);
5290 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5292 struct nfs4_exception exception = { };
5295 err = __nfs4_proc_set_acl(inode, buf, buflen);
5296 trace_nfs4_set_acl(inode, err);
5297 if (err == -NFS4ERR_BADOWNER || err == -NFS4ERR_BADNAME) {
5299 * no need to retry since the kernel
5300 * isn't involved in encoding the ACEs.
5305 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5307 } while (exception.retry);
5311 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
5312 static int _nfs4_get_security_label(struct inode *inode, void *buf,
5315 struct nfs_server *server = NFS_SERVER(inode);
5316 struct nfs_fattr fattr;
5317 struct nfs4_label label = {0, 0, buflen, buf};
5319 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5320 struct nfs4_getattr_arg arg = {
5321 .fh = NFS_FH(inode),
5324 struct nfs4_getattr_res res = {
5329 struct rpc_message msg = {
5330 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
5336 nfs_fattr_init(&fattr);
5338 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
5341 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5346 static int nfs4_get_security_label(struct inode *inode, void *buf,
5349 struct nfs4_exception exception = { };
5352 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5356 err = _nfs4_get_security_label(inode, buf, buflen);
5357 trace_nfs4_get_security_label(inode, err);
5358 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5360 } while (exception.retry);
5364 static int _nfs4_do_set_security_label(struct inode *inode,
5365 struct nfs4_label *ilabel,
5366 struct nfs_fattr *fattr,
5367 struct nfs4_label *olabel)
5370 struct iattr sattr = {0};
5371 struct nfs_server *server = NFS_SERVER(inode);
5372 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5373 struct nfs_setattrargs arg = {
5374 .fh = NFS_FH(inode),
5380 struct nfs_setattrres res = {
5385 struct rpc_message msg = {
5386 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5392 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
5394 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5396 dprintk("%s failed: %d\n", __func__, status);
5401 static int nfs4_do_set_security_label(struct inode *inode,
5402 struct nfs4_label *ilabel,
5403 struct nfs_fattr *fattr,
5404 struct nfs4_label *olabel)
5406 struct nfs4_exception exception = { };
5410 err = _nfs4_do_set_security_label(inode, ilabel,
5412 trace_nfs4_set_security_label(inode, err);
5413 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5415 } while (exception.retry);
5420 nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
5422 struct nfs4_label ilabel, *olabel = NULL;
5423 struct nfs_fattr fattr;
5424 struct rpc_cred *cred;
5427 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5430 nfs_fattr_init(&fattr);
5434 ilabel.label = (char *)buf;
5435 ilabel.len = buflen;
5437 cred = rpc_lookup_cred();
5439 return PTR_ERR(cred);
5441 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5442 if (IS_ERR(olabel)) {
5443 status = -PTR_ERR(olabel);
5447 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5449 nfs_setsecurity(inode, &fattr, olabel);
5451 nfs4_label_free(olabel);
5456 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5459 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5460 nfs4_verifier *bootverf)
5464 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5465 /* An impossible timestamp guarantees this value
5466 * will never match a generated boot time. */
5467 verf[0] = cpu_to_be32(U32_MAX);
5468 verf[1] = cpu_to_be32(U32_MAX);
5470 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
5471 u64 ns = ktime_to_ns(nn->boot_time);
5473 verf[0] = cpu_to_be32(ns >> 32);
5474 verf[1] = cpu_to_be32(ns);
5476 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5480 nfs4_init_nonuniform_client_string(struct nfs_client *clp)
5485 if (clp->cl_owner_id != NULL)
5489 len = 14 + strlen(clp->cl_ipaddr) + 1 +
5490 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5492 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5496 if (len > NFS4_OPAQUE_LIMIT + 1)
5500 * Since this string is allocated at mount time, and held until the
5501 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5502 * about a memory-reclaim deadlock.
5504 str = kmalloc(len, GFP_KERNEL);
5509 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
5511 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5512 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
5515 clp->cl_owner_id = str;
5520 nfs4_init_uniquifier_client_string(struct nfs_client *clp)
5525 len = 10 + 10 + 1 + 10 + 1 +
5526 strlen(nfs4_client_id_uniquifier) + 1 +
5527 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5529 if (len > NFS4_OPAQUE_LIMIT + 1)
5533 * Since this string is allocated at mount time, and held until the
5534 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5535 * about a memory-reclaim deadlock.
5537 str = kmalloc(len, GFP_KERNEL);
5541 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
5542 clp->rpc_ops->version, clp->cl_minorversion,
5543 nfs4_client_id_uniquifier,
5544 clp->cl_rpcclient->cl_nodename);
5545 clp->cl_owner_id = str;
5550 nfs4_init_uniform_client_string(struct nfs_client *clp)
5555 if (clp->cl_owner_id != NULL)
5558 if (nfs4_client_id_uniquifier[0] != '\0')
5559 return nfs4_init_uniquifier_client_string(clp);
5561 len = 10 + 10 + 1 + 10 + 1 +
5562 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5564 if (len > NFS4_OPAQUE_LIMIT + 1)
5568 * Since this string is allocated at mount time, and held until the
5569 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5570 * about a memory-reclaim deadlock.
5572 str = kmalloc(len, GFP_KERNEL);
5576 scnprintf(str, len, "Linux NFSv%u.%u %s",
5577 clp->rpc_ops->version, clp->cl_minorversion,
5578 clp->cl_rpcclient->cl_nodename);
5579 clp->cl_owner_id = str;
5584 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5585 * services. Advertise one based on the address family of the
5589 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5591 if (strchr(clp->cl_ipaddr, ':') != NULL)
5592 return scnprintf(buf, len, "tcp6");
5594 return scnprintf(buf, len, "tcp");
5597 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5599 struct nfs4_setclientid *sc = calldata;
5601 if (task->tk_status == 0)
5602 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5605 static const struct rpc_call_ops nfs4_setclientid_ops = {
5606 .rpc_call_done = nfs4_setclientid_done,
5610 * nfs4_proc_setclientid - Negotiate client ID
5611 * @clp: state data structure
5612 * @program: RPC program for NFSv4 callback service
5613 * @port: IP port number for NFS4 callback service
5614 * @cred: RPC credential to use for this call
5615 * @res: where to place the result
5617 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5619 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5620 unsigned short port, struct rpc_cred *cred,
5621 struct nfs4_setclientid_res *res)
5623 nfs4_verifier sc_verifier;
5624 struct nfs4_setclientid setclientid = {
5625 .sc_verifier = &sc_verifier,
5629 struct rpc_message msg = {
5630 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5631 .rpc_argp = &setclientid,
5635 struct rpc_task *task;
5636 struct rpc_task_setup task_setup_data = {
5637 .rpc_client = clp->cl_rpcclient,
5638 .rpc_message = &msg,
5639 .callback_ops = &nfs4_setclientid_ops,
5640 .callback_data = &setclientid,
5641 .flags = RPC_TASK_TIMEOUT,
5645 /* nfs_client_id4 */
5646 nfs4_init_boot_verifier(clp, &sc_verifier);
5648 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5649 status = nfs4_init_uniform_client_string(clp);
5651 status = nfs4_init_nonuniform_client_string(clp);
5657 setclientid.sc_netid_len =
5658 nfs4_init_callback_netid(clp,
5659 setclientid.sc_netid,
5660 sizeof(setclientid.sc_netid));
5661 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
5662 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
5663 clp->cl_ipaddr, port >> 8, port & 255);
5665 dprintk("NFS call setclientid auth=%s, '%s'\n",
5666 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5668 task = rpc_run_task(&task_setup_data);
5670 status = PTR_ERR(task);
5673 status = task->tk_status;
5674 if (setclientid.sc_cred) {
5675 kfree(clp->cl_acceptor);
5676 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5677 put_rpccred(setclientid.sc_cred);
5681 trace_nfs4_setclientid(clp, status);
5682 dprintk("NFS reply setclientid: %d\n", status);
5687 * nfs4_proc_setclientid_confirm - Confirm client ID
5688 * @clp: state data structure
5689 * @res: result of a previous SETCLIENTID
5690 * @cred: RPC credential to use for this call
5692 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5694 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
5695 struct nfs4_setclientid_res *arg,
5696 struct rpc_cred *cred)
5698 struct rpc_message msg = {
5699 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
5705 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5706 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5708 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5709 trace_nfs4_setclientid_confirm(clp, status);
5710 dprintk("NFS reply setclientid_confirm: %d\n", status);
5714 struct nfs4_delegreturndata {
5715 struct nfs4_delegreturnargs args;
5716 struct nfs4_delegreturnres res;
5718 nfs4_stateid stateid;
5719 unsigned long timestamp;
5721 struct nfs4_layoutreturn_args arg;
5722 struct nfs4_layoutreturn_res res;
5723 struct nfs4_xdr_opaque_data ld_private;
5727 struct nfs_fattr fattr;
5729 struct inode *inode;
5732 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5734 struct nfs4_delegreturndata *data = calldata;
5736 if (!nfs4_sequence_done(task, &data->res.seq_res))
5739 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
5741 /* Handle Layoutreturn errors */
5742 if (data->args.lr_args && task->tk_status != 0) {
5743 switch(data->res.lr_ret) {
5745 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5748 data->args.lr_args = NULL;
5749 data->res.lr_res = NULL;
5751 case -NFS4ERR_ADMIN_REVOKED:
5752 case -NFS4ERR_DELEG_REVOKED:
5753 case -NFS4ERR_EXPIRED:
5754 case -NFS4ERR_BAD_STATEID:
5755 case -NFS4ERR_OLD_STATEID:
5756 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
5757 case -NFS4ERR_WRONG_CRED:
5758 data->args.lr_args = NULL;
5759 data->res.lr_res = NULL;
5760 data->res.lr_ret = 0;
5761 rpc_restart_call_prepare(task);
5766 switch (task->tk_status) {
5768 renew_lease(data->res.server, data->timestamp);
5770 case -NFS4ERR_ADMIN_REVOKED:
5771 case -NFS4ERR_DELEG_REVOKED:
5772 case -NFS4ERR_EXPIRED:
5773 nfs4_free_revoked_stateid(data->res.server,
5775 task->tk_msg.rpc_cred);
5776 case -NFS4ERR_BAD_STATEID:
5777 case -NFS4ERR_OLD_STATEID:
5778 case -NFS4ERR_STALE_STATEID:
5779 task->tk_status = 0;
5781 case -NFS4ERR_ACCESS:
5782 if (data->args.bitmask) {
5783 data->args.bitmask = NULL;
5784 data->res.fattr = NULL;
5785 task->tk_status = 0;
5786 rpc_restart_call_prepare(task);
5790 if (nfs4_async_handle_error(task, data->res.server,
5791 NULL, NULL) == -EAGAIN) {
5792 rpc_restart_call_prepare(task);
5796 data->rpc_status = task->tk_status;
5799 static void nfs4_delegreturn_release(void *calldata)
5801 struct nfs4_delegreturndata *data = calldata;
5802 struct inode *inode = data->inode;
5806 pnfs_roc_release(&data->lr.arg, &data->lr.res,
5808 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5809 nfs_iput_and_deactive(inode);
5814 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5816 struct nfs4_delegreturndata *d_data;
5818 d_data = (struct nfs4_delegreturndata *)data;
5820 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task)) {
5821 nfs4_sequence_done(task, &d_data->res.seq_res);
5825 nfs4_setup_sequence(d_data->res.server->nfs_client,
5826 &d_data->args.seq_args,
5827 &d_data->res.seq_res,
5831 static const struct rpc_call_ops nfs4_delegreturn_ops = {
5832 .rpc_call_prepare = nfs4_delegreturn_prepare,
5833 .rpc_call_done = nfs4_delegreturn_done,
5834 .rpc_release = nfs4_delegreturn_release,
5837 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
5839 struct nfs4_delegreturndata *data;
5840 struct nfs_server *server = NFS_SERVER(inode);
5841 struct rpc_task *task;
5842 struct rpc_message msg = {
5843 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5846 struct rpc_task_setup task_setup_data = {
5847 .rpc_client = server->client,
5848 .rpc_message = &msg,
5849 .callback_ops = &nfs4_delegreturn_ops,
5850 .flags = RPC_TASK_ASYNC,
5854 data = kzalloc(sizeof(*data), GFP_NOFS);
5857 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
5859 nfs4_state_protect(server->nfs_client,
5860 NFS_SP4_MACH_CRED_CLEANUP,
5861 &task_setup_data.rpc_client, &msg);
5863 data->args.fhandle = &data->fh;
5864 data->args.stateid = &data->stateid;
5865 data->args.bitmask = server->cache_consistency_bitmask;
5866 nfs_copy_fh(&data->fh, NFS_FH(inode));
5867 nfs4_stateid_copy(&data->stateid, stateid);
5868 data->res.fattr = &data->fattr;
5869 data->res.server = server;
5870 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5871 data->lr.arg.ld_private = &data->lr.ld_private;
5872 nfs_fattr_init(data->res.fattr);
5873 data->timestamp = jiffies;
5874 data->rpc_status = 0;
5875 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
5876 data->inode = nfs_igrab_and_active(inode);
5879 data->args.lr_args = &data->lr.arg;
5880 data->res.lr_res = &data->lr.res;
5882 } else if (data->lr.roc) {
5883 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
5884 data->lr.roc = false;
5887 task_setup_data.callback_data = data;
5888 msg.rpc_argp = &data->args;
5889 msg.rpc_resp = &data->res;
5890 task = rpc_run_task(&task_setup_data);
5892 return PTR_ERR(task);
5895 status = rpc_wait_for_completion_task(task);
5898 status = data->rpc_status;
5904 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
5906 struct nfs_server *server = NFS_SERVER(inode);
5907 struct nfs4_exception exception = { };
5910 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
5911 trace_nfs4_delegreturn(inode, stateid, err);
5913 case -NFS4ERR_STALE_STATEID:
5914 case -NFS4ERR_EXPIRED:
5918 err = nfs4_handle_exception(server, err, &exception);
5919 } while (exception.retry);
5923 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5925 struct inode *inode = state->inode;
5926 struct nfs_server *server = NFS_SERVER(inode);
5927 struct nfs_client *clp = server->nfs_client;
5928 struct nfs_lockt_args arg = {
5929 .fh = NFS_FH(inode),
5932 struct nfs_lockt_res res = {
5935 struct rpc_message msg = {
5936 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5939 .rpc_cred = state->owner->so_cred,
5941 struct nfs4_lock_state *lsp;
5944 arg.lock_owner.clientid = clp->cl_clientid;
5945 status = nfs4_set_lock_state(state, request);
5948 lsp = request->fl_u.nfs4_fl.owner;
5949 arg.lock_owner.id = lsp->ls_seqid.owner_id;
5950 arg.lock_owner.s_dev = server->s_dev;
5951 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5954 request->fl_type = F_UNLCK;
5956 case -NFS4ERR_DENIED:
5959 request->fl_ops->fl_release_private(request);
5960 request->fl_ops = NULL;
5965 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5967 struct nfs4_exception exception = { };
5971 err = _nfs4_proc_getlk(state, cmd, request);
5972 trace_nfs4_get_lock(request, state, cmd, err);
5973 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
5975 } while (exception.retry);
5979 struct nfs4_unlockdata {
5980 struct nfs_locku_args arg;
5981 struct nfs_locku_res res;
5982 struct nfs4_lock_state *lsp;
5983 struct nfs_open_context *ctx;
5984 struct nfs_lock_context *l_ctx;
5985 struct file_lock fl;
5986 struct nfs_server *server;
5987 unsigned long timestamp;
5990 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5991 struct nfs_open_context *ctx,
5992 struct nfs4_lock_state *lsp,
5993 struct nfs_seqid *seqid)
5995 struct nfs4_unlockdata *p;
5996 struct inode *inode = lsp->ls_state->inode;
5998 p = kzalloc(sizeof(*p), GFP_NOFS);
6001 p->arg.fh = NFS_FH(inode);
6003 p->arg.seqid = seqid;
6004 p->res.seqid = seqid;
6006 atomic_inc(&lsp->ls_count);
6007 /* Ensure we don't close file until we're done freeing locks! */
6008 p->ctx = get_nfs_open_context(ctx);
6009 p->l_ctx = nfs_get_lock_context(ctx);
6010 memcpy(&p->fl, fl, sizeof(p->fl));
6011 p->server = NFS_SERVER(inode);
6015 static void nfs4_locku_release_calldata(void *data)
6017 struct nfs4_unlockdata *calldata = data;
6018 nfs_free_seqid(calldata->arg.seqid);
6019 nfs4_put_lock_state(calldata->lsp);
6020 nfs_put_lock_context(calldata->l_ctx);
6021 put_nfs_open_context(calldata->ctx);
6025 static void nfs4_locku_done(struct rpc_task *task, void *data)
6027 struct nfs4_unlockdata *calldata = data;
6029 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6031 switch (task->tk_status) {
6033 renew_lease(calldata->server, calldata->timestamp);
6034 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
6035 if (nfs4_update_lock_stateid(calldata->lsp,
6036 &calldata->res.stateid))
6038 case -NFS4ERR_ADMIN_REVOKED:
6039 case -NFS4ERR_EXPIRED:
6040 nfs4_free_revoked_stateid(calldata->server,
6041 &calldata->arg.stateid,
6042 task->tk_msg.rpc_cred);
6043 case -NFS4ERR_BAD_STATEID:
6044 case -NFS4ERR_OLD_STATEID:
6045 case -NFS4ERR_STALE_STATEID:
6046 if (!nfs4_stateid_match(&calldata->arg.stateid,
6047 &calldata->lsp->ls_stateid))
6048 rpc_restart_call_prepare(task);
6051 if (nfs4_async_handle_error(task, calldata->server,
6052 NULL, NULL) == -EAGAIN)
6053 rpc_restart_call_prepare(task);
6055 nfs_release_seqid(calldata->arg.seqid);
6058 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
6060 struct nfs4_unlockdata *calldata = data;
6062 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6063 nfs_async_iocounter_wait(task, calldata->l_ctx))
6066 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
6068 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
6069 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
6070 /* Note: exit _without_ running nfs4_locku_done */
6073 calldata->timestamp = jiffies;
6074 if (nfs4_setup_sequence(calldata->server->nfs_client,
6075 &calldata->arg.seq_args,
6076 &calldata->res.seq_res,
6078 nfs_release_seqid(calldata->arg.seqid);
6081 task->tk_action = NULL;
6083 nfs4_sequence_done(task, &calldata->res.seq_res);
6086 static const struct rpc_call_ops nfs4_locku_ops = {
6087 .rpc_call_prepare = nfs4_locku_prepare,
6088 .rpc_call_done = nfs4_locku_done,
6089 .rpc_release = nfs4_locku_release_calldata,
6092 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6093 struct nfs_open_context *ctx,
6094 struct nfs4_lock_state *lsp,
6095 struct nfs_seqid *seqid)
6097 struct nfs4_unlockdata *data;
6098 struct rpc_message msg = {
6099 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6100 .rpc_cred = ctx->cred,
6102 struct rpc_task_setup task_setup_data = {
6103 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
6104 .rpc_message = &msg,
6105 .callback_ops = &nfs4_locku_ops,
6106 .workqueue = nfsiod_workqueue,
6107 .flags = RPC_TASK_ASYNC,
6110 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6111 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6113 /* Ensure this is an unlock - when canceling a lock, the
6114 * canceled lock is passed in, and it won't be an unlock.
6116 fl->fl_type = F_UNLCK;
6117 if (fl->fl_flags & FL_CLOSE)
6118 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
6120 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6122 nfs_free_seqid(seqid);
6123 return ERR_PTR(-ENOMEM);
6126 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
6127 msg.rpc_argp = &data->arg;
6128 msg.rpc_resp = &data->res;
6129 task_setup_data.callback_data = data;
6130 return rpc_run_task(&task_setup_data);
6133 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6135 struct inode *inode = state->inode;
6136 struct nfs4_state_owner *sp = state->owner;
6137 struct nfs_inode *nfsi = NFS_I(inode);
6138 struct nfs_seqid *seqid;
6139 struct nfs4_lock_state *lsp;
6140 struct rpc_task *task;
6141 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6143 unsigned char fl_flags = request->fl_flags;
6145 status = nfs4_set_lock_state(state, request);
6146 /* Unlock _before_ we do the RPC call */
6147 request->fl_flags |= FL_EXISTS;
6148 /* Exclude nfs_delegation_claim_locks() */
6149 mutex_lock(&sp->so_delegreturn_mutex);
6150 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
6151 down_read(&nfsi->rwsem);
6152 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
6153 up_read(&nfsi->rwsem);
6154 mutex_unlock(&sp->so_delegreturn_mutex);
6157 up_read(&nfsi->rwsem);
6158 mutex_unlock(&sp->so_delegreturn_mutex);
6161 /* Is this a delegated lock? */
6162 lsp = request->fl_u.nfs4_fl.owner;
6163 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6165 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6166 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
6170 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
6171 status = PTR_ERR(task);
6174 status = rpc_wait_for_completion_task(task);
6177 request->fl_flags = fl_flags;
6178 trace_nfs4_unlock(request, state, F_SETLK, status);
6182 struct nfs4_lockdata {
6183 struct nfs_lock_args arg;
6184 struct nfs_lock_res res;
6185 struct nfs4_lock_state *lsp;
6186 struct nfs_open_context *ctx;
6187 struct file_lock fl;
6188 unsigned long timestamp;
6191 struct nfs_server *server;
6194 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
6195 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6198 struct nfs4_lockdata *p;
6199 struct inode *inode = lsp->ls_state->inode;
6200 struct nfs_server *server = NFS_SERVER(inode);
6201 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6203 p = kzalloc(sizeof(*p), gfp_mask);
6207 p->arg.fh = NFS_FH(inode);
6209 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
6210 if (IS_ERR(p->arg.open_seqid))
6212 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6213 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
6214 if (IS_ERR(p->arg.lock_seqid))
6215 goto out_free_seqid;
6216 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
6217 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
6218 p->arg.lock_owner.s_dev = server->s_dev;
6219 p->res.lock_seqid = p->arg.lock_seqid;
6222 atomic_inc(&lsp->ls_count);
6223 p->ctx = get_nfs_open_context(ctx);
6224 memcpy(&p->fl, fl, sizeof(p->fl));
6227 nfs_free_seqid(p->arg.open_seqid);
6233 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6235 struct nfs4_lockdata *data = calldata;
6236 struct nfs4_state *state = data->lsp->ls_state;
6238 dprintk("%s: begin!\n", __func__);
6239 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
6241 /* Do we need to do an open_to_lock_owner? */
6242 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
6243 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
6244 goto out_release_lock_seqid;
6246 nfs4_stateid_copy(&data->arg.open_stateid,
6247 &state->open_stateid);
6248 data->arg.new_lock_owner = 1;
6249 data->res.open_seqid = data->arg.open_seqid;
6251 data->arg.new_lock_owner = 0;
6252 nfs4_stateid_copy(&data->arg.lock_stateid,
6253 &data->lsp->ls_stateid);
6255 if (!nfs4_valid_open_stateid(state)) {
6256 data->rpc_status = -EBADF;
6257 task->tk_action = NULL;
6258 goto out_release_open_seqid;
6260 data->timestamp = jiffies;
6261 if (nfs4_setup_sequence(data->server->nfs_client,
6262 &data->arg.seq_args,
6266 out_release_open_seqid:
6267 nfs_release_seqid(data->arg.open_seqid);
6268 out_release_lock_seqid:
6269 nfs_release_seqid(data->arg.lock_seqid);
6271 nfs4_sequence_done(task, &data->res.seq_res);
6272 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
6275 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6277 struct nfs4_lockdata *data = calldata;
6278 struct nfs4_lock_state *lsp = data->lsp;
6280 dprintk("%s: begin!\n", __func__);
6282 if (!nfs4_sequence_done(task, &data->res.seq_res))
6285 data->rpc_status = task->tk_status;
6286 switch (task->tk_status) {
6288 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
6290 if (data->arg.new_lock) {
6291 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
6292 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
6293 rpc_restart_call_prepare(task);
6297 if (data->arg.new_lock_owner != 0) {
6298 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6299 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6300 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6301 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6302 rpc_restart_call_prepare(task);
6304 case -NFS4ERR_BAD_STATEID:
6305 case -NFS4ERR_OLD_STATEID:
6306 case -NFS4ERR_STALE_STATEID:
6307 case -NFS4ERR_EXPIRED:
6308 if (data->arg.new_lock_owner != 0) {
6309 if (!nfs4_stateid_match(&data->arg.open_stateid,
6310 &lsp->ls_state->open_stateid))
6311 rpc_restart_call_prepare(task);
6312 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6314 rpc_restart_call_prepare(task);
6316 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
6319 static void nfs4_lock_release(void *calldata)
6321 struct nfs4_lockdata *data = calldata;
6323 dprintk("%s: begin!\n", __func__);
6324 nfs_free_seqid(data->arg.open_seqid);
6325 if (data->cancelled) {
6326 struct rpc_task *task;
6327 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6328 data->arg.lock_seqid);
6330 rpc_put_task_async(task);
6331 dprintk("%s: cancelling lock!\n", __func__);
6333 nfs_free_seqid(data->arg.lock_seqid);
6334 nfs4_put_lock_state(data->lsp);
6335 put_nfs_open_context(data->ctx);
6337 dprintk("%s: done!\n", __func__);
6340 static const struct rpc_call_ops nfs4_lock_ops = {
6341 .rpc_call_prepare = nfs4_lock_prepare,
6342 .rpc_call_done = nfs4_lock_done,
6343 .rpc_release = nfs4_lock_release,
6346 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6349 case -NFS4ERR_ADMIN_REVOKED:
6350 case -NFS4ERR_EXPIRED:
6351 case -NFS4ERR_BAD_STATEID:
6352 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6353 if (new_lock_owner != 0 ||
6354 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
6355 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
6357 case -NFS4ERR_STALE_STATEID:
6358 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6359 nfs4_schedule_lease_recovery(server->nfs_client);
6363 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
6365 struct nfs4_lockdata *data;
6366 struct rpc_task *task;
6367 struct rpc_message msg = {
6368 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6369 .rpc_cred = state->owner->so_cred,
6371 struct rpc_task_setup task_setup_data = {
6372 .rpc_client = NFS_CLIENT(state->inode),
6373 .rpc_message = &msg,
6374 .callback_ops = &nfs4_lock_ops,
6375 .workqueue = nfsiod_workqueue,
6376 .flags = RPC_TASK_ASYNC,
6380 dprintk("%s: begin!\n", __func__);
6381 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
6382 fl->fl_u.nfs4_fl.owner,
6383 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
6387 data->arg.block = 1;
6388 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
6389 msg.rpc_argp = &data->arg;
6390 msg.rpc_resp = &data->res;
6391 task_setup_data.callback_data = data;
6392 if (recovery_type > NFS_LOCK_NEW) {
6393 if (recovery_type == NFS_LOCK_RECLAIM)
6394 data->arg.reclaim = NFS_LOCK_RECLAIM;
6395 nfs4_set_sequence_privileged(&data->arg.seq_args);
6397 data->arg.new_lock = 1;
6398 task = rpc_run_task(&task_setup_data);
6400 return PTR_ERR(task);
6401 ret = rpc_wait_for_completion_task(task);
6403 ret = data->rpc_status;
6405 nfs4_handle_setlk_error(data->server, data->lsp,
6406 data->arg.new_lock_owner, ret);
6408 data->cancelled = true;
6409 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
6411 dprintk("%s: done, ret = %d!\n", __func__, ret);
6415 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6417 struct nfs_server *server = NFS_SERVER(state->inode);
6418 struct nfs4_exception exception = {
6419 .inode = state->inode,
6424 /* Cache the lock if possible... */
6425 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6427 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
6428 if (err != -NFS4ERR_DELAY)
6430 nfs4_handle_exception(server, err, &exception);
6431 } while (exception.retry);
6435 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6437 struct nfs_server *server = NFS_SERVER(state->inode);
6438 struct nfs4_exception exception = {
6439 .inode = state->inode,
6443 err = nfs4_set_lock_state(state, request);
6446 if (!recover_lost_locks) {
6447 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6451 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6453 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
6457 case -NFS4ERR_GRACE:
6458 case -NFS4ERR_DELAY:
6459 nfs4_handle_exception(server, err, &exception);
6462 } while (exception.retry);
6467 #if defined(CONFIG_NFS_V4_1)
6468 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6470 struct nfs4_lock_state *lsp;
6473 status = nfs4_set_lock_state(state, request);
6476 lsp = request->fl_u.nfs4_fl.owner;
6477 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6478 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6480 return nfs4_lock_expired(state, request);
6484 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6486 struct nfs_inode *nfsi = NFS_I(state->inode);
6487 struct nfs4_state_owner *sp = state->owner;
6488 unsigned char fl_flags = request->fl_flags;
6491 request->fl_flags |= FL_ACCESS;
6492 status = locks_lock_inode_wait(state->inode, request);
6495 mutex_lock(&sp->so_delegreturn_mutex);
6496 down_read(&nfsi->rwsem);
6497 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
6498 /* Yes: cache locks! */
6499 /* ...but avoid races with delegation recall... */
6500 request->fl_flags = fl_flags & ~FL_SLEEP;
6501 status = locks_lock_inode_wait(state->inode, request);
6502 up_read(&nfsi->rwsem);
6503 mutex_unlock(&sp->so_delegreturn_mutex);
6506 up_read(&nfsi->rwsem);
6507 mutex_unlock(&sp->so_delegreturn_mutex);
6508 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
6510 request->fl_flags = fl_flags;
6514 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6516 struct nfs4_exception exception = {
6518 .inode = state->inode,
6523 err = _nfs4_proc_setlk(state, cmd, request);
6524 if (err == -NFS4ERR_DENIED)
6526 err = nfs4_handle_exception(NFS_SERVER(state->inode),
6528 } while (exception.retry);
6532 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6533 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6536 nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6537 struct file_lock *request)
6539 int status = -ERESTARTSYS;
6540 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6542 while(!signalled()) {
6543 status = nfs4_proc_setlk(state, cmd, request);
6544 if ((status != -EAGAIN) || IS_SETLK(cmd))
6546 freezable_schedule_timeout_interruptible(timeout);
6548 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6549 status = -ERESTARTSYS;
6554 #ifdef CONFIG_NFS_V4_1
6555 struct nfs4_lock_waiter {
6556 struct task_struct *task;
6557 struct inode *inode;
6558 struct nfs_lowner *owner;
6563 nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
6566 struct cb_notify_lock_args *cbnl = key;
6567 struct nfs4_lock_waiter *waiter = wait->private;
6568 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6569 *wowner = waiter->owner;
6571 /* Only wake if the callback was for the same owner */
6572 if (lowner->clientid != wowner->clientid ||
6573 lowner->id != wowner->id ||
6574 lowner->s_dev != wowner->s_dev)
6577 /* Make sure it's for the right inode */
6578 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6581 waiter->notified = true;
6583 /* override "private" so we can use default_wake_function */
6584 wait->private = waiter->task;
6585 ret = autoremove_wake_function(wait, mode, flags, key);
6586 wait->private = waiter;
6591 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6593 int status = -ERESTARTSYS;
6594 unsigned long flags;
6595 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6596 struct nfs_server *server = NFS_SERVER(state->inode);
6597 struct nfs_client *clp = server->nfs_client;
6598 wait_queue_head_t *q = &clp->cl_lock_waitq;
6599 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6600 .id = lsp->ls_seqid.owner_id,
6601 .s_dev = server->s_dev };
6602 struct nfs4_lock_waiter waiter = { .task = current,
6603 .inode = state->inode,
6605 .notified = false };
6606 wait_queue_entry_t wait;
6608 /* Don't bother with waitqueue if we don't expect a callback */
6609 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6610 return nfs4_retry_setlk_simple(state, cmd, request);
6613 wait.private = &waiter;
6614 wait.func = nfs4_wake_lock_waiter;
6615 add_wait_queue(q, &wait);
6617 while(!signalled()) {
6618 status = nfs4_proc_setlk(state, cmd, request);
6619 if ((status != -EAGAIN) || IS_SETLK(cmd))
6622 status = -ERESTARTSYS;
6623 spin_lock_irqsave(&q->lock, flags);
6624 if (waiter.notified) {
6625 spin_unlock_irqrestore(&q->lock, flags);
6628 set_current_state(TASK_INTERRUPTIBLE);
6629 spin_unlock_irqrestore(&q->lock, flags);
6631 freezable_schedule_timeout(NFS4_LOCK_MAXTIMEOUT);
6634 finish_wait(q, &wait);
6637 #else /* !CONFIG_NFS_V4_1 */
6639 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6641 return nfs4_retry_setlk_simple(state, cmd, request);
6646 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6648 struct nfs_open_context *ctx;
6649 struct nfs4_state *state;
6652 /* verify open state */
6653 ctx = nfs_file_open_context(filp);
6656 if (IS_GETLK(cmd)) {
6658 return nfs4_proc_getlk(state, F_GETLK, request);
6662 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6665 if (request->fl_type == F_UNLCK) {
6667 return nfs4_proc_unlck(state, cmd, request);
6674 if ((request->fl_flags & FL_POSIX) &&
6675 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6679 * Don't rely on the VFS having checked the file open mode,
6680 * since it won't do this for flock() locks.
6682 switch (request->fl_type) {
6684 if (!(filp->f_mode & FMODE_READ))
6688 if (!(filp->f_mode & FMODE_WRITE))
6692 status = nfs4_set_lock_state(state, request);
6696 return nfs4_retry_setlk(state, cmd, request);
6699 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
6701 struct nfs_server *server = NFS_SERVER(state->inode);
6704 err = nfs4_set_lock_state(state, fl);
6708 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
6709 if (err != -NFS4ERR_DELAY)
6712 } while (err == -NFS4ERR_DELAY);
6713 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
6716 struct nfs_release_lockowner_data {
6717 struct nfs4_lock_state *lsp;
6718 struct nfs_server *server;
6719 struct nfs_release_lockowner_args args;
6720 struct nfs_release_lockowner_res res;
6721 unsigned long timestamp;
6724 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6726 struct nfs_release_lockowner_data *data = calldata;
6727 struct nfs_server *server = data->server;
6728 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
6729 &data->res.seq_res, task);
6730 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6731 data->timestamp = jiffies;
6734 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6736 struct nfs_release_lockowner_data *data = calldata;
6737 struct nfs_server *server = data->server;
6739 nfs40_sequence_done(task, &data->res.seq_res);
6741 switch (task->tk_status) {
6743 renew_lease(server, data->timestamp);
6745 case -NFS4ERR_STALE_CLIENTID:
6746 case -NFS4ERR_EXPIRED:
6747 nfs4_schedule_lease_recovery(server->nfs_client);
6749 case -NFS4ERR_LEASE_MOVED:
6750 case -NFS4ERR_DELAY:
6751 if (nfs4_async_handle_error(task, server,
6752 NULL, NULL) == -EAGAIN)
6753 rpc_restart_call_prepare(task);
6757 static void nfs4_release_lockowner_release(void *calldata)
6759 struct nfs_release_lockowner_data *data = calldata;
6760 nfs4_free_lock_state(data->server, data->lsp);
6764 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
6765 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6766 .rpc_call_done = nfs4_release_lockowner_done,
6767 .rpc_release = nfs4_release_lockowner_release,
6771 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
6773 struct nfs_release_lockowner_data *data;
6774 struct rpc_message msg = {
6775 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6778 if (server->nfs_client->cl_mvops->minor_version != 0)
6781 data = kmalloc(sizeof(*data), GFP_NOFS);
6785 data->server = server;
6786 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6787 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6788 data->args.lock_owner.s_dev = server->s_dev;
6790 msg.rpc_argp = &data->args;
6791 msg.rpc_resp = &data->res;
6792 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
6793 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
6796 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6798 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
6799 struct dentry *unused, struct inode *inode,
6800 const char *key, const void *buf,
6801 size_t buflen, int flags)
6803 return nfs4_proc_set_acl(inode, buf, buflen);
6806 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
6807 struct dentry *unused, struct inode *inode,
6808 const char *key, void *buf, size_t buflen)
6810 return nfs4_proc_get_acl(inode, buf, buflen);
6813 static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
6815 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
6818 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
6820 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
6821 struct dentry *unused, struct inode *inode,
6822 const char *key, const void *buf,
6823 size_t buflen, int flags)
6825 if (security_ismaclabel(key))
6826 return nfs4_set_security_label(inode, buf, buflen);
6831 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
6832 struct dentry *unused, struct inode *inode,
6833 const char *key, void *buf, size_t buflen)
6835 if (security_ismaclabel(key))
6836 return nfs4_get_security_label(inode, buf, buflen);
6841 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6845 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6846 len = security_inode_listsecurity(inode, list, list_len);
6847 if (list_len && len > list_len)
6853 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6854 .prefix = XATTR_SECURITY_PREFIX,
6855 .get = nfs4_xattr_get_nfs4_label,
6856 .set = nfs4_xattr_set_nfs4_label,
6862 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6870 * nfs_fhget will use either the mounted_on_fileid or the fileid
6872 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6874 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6875 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6876 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
6877 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
6880 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
6881 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
6882 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6886 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6887 const struct qstr *name,
6888 struct nfs4_fs_locations *fs_locations,
6891 struct nfs_server *server = NFS_SERVER(dir);
6893 struct nfs4_fs_locations_arg args = {
6894 .dir_fh = NFS_FH(dir),
6899 struct nfs4_fs_locations_res res = {
6900 .fs_locations = fs_locations,
6902 struct rpc_message msg = {
6903 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6909 dprintk("%s: start\n", __func__);
6911 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
6912 bitmask[1] = nfs4_fattr_bitmap[1];
6914 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6915 * is not supported */
6916 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6917 bitmask[0] &= ~FATTR4_WORD0_FILEID;
6919 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
6921 nfs_fattr_init(&fs_locations->fattr);
6922 fs_locations->server = server;
6923 fs_locations->nlocations = 0;
6924 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
6925 dprintk("%s: returned status = %d\n", __func__, status);
6929 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6930 const struct qstr *name,
6931 struct nfs4_fs_locations *fs_locations,
6934 struct nfs4_exception exception = { };
6937 err = _nfs4_proc_fs_locations(client, dir, name,
6938 fs_locations, page);
6939 trace_nfs4_get_fs_locations(dir, name, err);
6940 err = nfs4_handle_exception(NFS_SERVER(dir), err,
6942 } while (exception.retry);
6947 * This operation also signals the server that this client is
6948 * performing migration recovery. The server can stop returning
6949 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6950 * appended to this compound to identify the client ID which is
6951 * performing recovery.
6953 static int _nfs40_proc_get_locations(struct inode *inode,
6954 struct nfs4_fs_locations *locations,
6955 struct page *page, struct rpc_cred *cred)
6957 struct nfs_server *server = NFS_SERVER(inode);
6958 struct rpc_clnt *clnt = server->client;
6960 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6962 struct nfs4_fs_locations_arg args = {
6963 .clientid = server->nfs_client->cl_clientid,
6964 .fh = NFS_FH(inode),
6967 .migration = 1, /* skip LOOKUP */
6968 .renew = 1, /* append RENEW */
6970 struct nfs4_fs_locations_res res = {
6971 .fs_locations = locations,
6975 struct rpc_message msg = {
6976 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6981 unsigned long now = jiffies;
6984 nfs_fattr_init(&locations->fattr);
6985 locations->server = server;
6986 locations->nlocations = 0;
6988 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6989 nfs4_set_sequence_privileged(&args.seq_args);
6990 status = nfs4_call_sync_sequence(clnt, server, &msg,
6991 &args.seq_args, &res.seq_res);
6995 renew_lease(server, now);
6999 #ifdef CONFIG_NFS_V4_1
7002 * This operation also signals the server that this client is
7003 * performing migration recovery. The server can stop asserting
7004 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7005 * performing this operation is identified in the SEQUENCE
7006 * operation in this compound.
7008 * When the client supports GETATTR(fs_locations_info), it can
7009 * be plumbed in here.
7011 static int _nfs41_proc_get_locations(struct inode *inode,
7012 struct nfs4_fs_locations *locations,
7013 struct page *page, struct rpc_cred *cred)
7015 struct nfs_server *server = NFS_SERVER(inode);
7016 struct rpc_clnt *clnt = server->client;
7018 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7020 struct nfs4_fs_locations_arg args = {
7021 .fh = NFS_FH(inode),
7024 .migration = 1, /* skip LOOKUP */
7026 struct nfs4_fs_locations_res res = {
7027 .fs_locations = locations,
7030 struct rpc_message msg = {
7031 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7038 nfs_fattr_init(&locations->fattr);
7039 locations->server = server;
7040 locations->nlocations = 0;
7042 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7043 nfs4_set_sequence_privileged(&args.seq_args);
7044 status = nfs4_call_sync_sequence(clnt, server, &msg,
7045 &args.seq_args, &res.seq_res);
7046 if (status == NFS4_OK &&
7047 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7048 status = -NFS4ERR_LEASE_MOVED;
7052 #endif /* CONFIG_NFS_V4_1 */
7055 * nfs4_proc_get_locations - discover locations for a migrated FSID
7056 * @inode: inode on FSID that is migrating
7057 * @locations: result of query
7059 * @cred: credential to use for this operation
7061 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7062 * operation failed, or a negative errno if a local error occurred.
7064 * On success, "locations" is filled in, but if the server has
7065 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7068 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7069 * from this client that require migration recovery.
7071 int nfs4_proc_get_locations(struct inode *inode,
7072 struct nfs4_fs_locations *locations,
7073 struct page *page, struct rpc_cred *cred)
7075 struct nfs_server *server = NFS_SERVER(inode);
7076 struct nfs_client *clp = server->nfs_client;
7077 const struct nfs4_mig_recovery_ops *ops =
7078 clp->cl_mvops->mig_recovery_ops;
7079 struct nfs4_exception exception = { };
7082 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7083 (unsigned long long)server->fsid.major,
7084 (unsigned long long)server->fsid.minor,
7086 nfs_display_fhandle(NFS_FH(inode), __func__);
7089 status = ops->get_locations(inode, locations, page, cred);
7090 if (status != -NFS4ERR_DELAY)
7092 nfs4_handle_exception(server, status, &exception);
7093 } while (exception.retry);
7098 * This operation also signals the server that this client is
7099 * performing "lease moved" recovery. The server can stop
7100 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7101 * is appended to this compound to identify the client ID which is
7102 * performing recovery.
7104 static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7106 struct nfs_server *server = NFS_SERVER(inode);
7107 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7108 struct rpc_clnt *clnt = server->client;
7109 struct nfs4_fsid_present_arg args = {
7110 .fh = NFS_FH(inode),
7111 .clientid = clp->cl_clientid,
7112 .renew = 1, /* append RENEW */
7114 struct nfs4_fsid_present_res res = {
7117 struct rpc_message msg = {
7118 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7123 unsigned long now = jiffies;
7126 res.fh = nfs_alloc_fhandle();
7130 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7131 nfs4_set_sequence_privileged(&args.seq_args);
7132 status = nfs4_call_sync_sequence(clnt, server, &msg,
7133 &args.seq_args, &res.seq_res);
7134 nfs_free_fhandle(res.fh);
7138 do_renew_lease(clp, now);
7142 #ifdef CONFIG_NFS_V4_1
7145 * This operation also signals the server that this client is
7146 * performing "lease moved" recovery. The server can stop asserting
7147 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7148 * this operation is identified in the SEQUENCE operation in this
7151 static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7153 struct nfs_server *server = NFS_SERVER(inode);
7154 struct rpc_clnt *clnt = server->client;
7155 struct nfs4_fsid_present_arg args = {
7156 .fh = NFS_FH(inode),
7158 struct nfs4_fsid_present_res res = {
7160 struct rpc_message msg = {
7161 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7168 res.fh = nfs_alloc_fhandle();
7172 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7173 nfs4_set_sequence_privileged(&args.seq_args);
7174 status = nfs4_call_sync_sequence(clnt, server, &msg,
7175 &args.seq_args, &res.seq_res);
7176 nfs_free_fhandle(res.fh);
7177 if (status == NFS4_OK &&
7178 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7179 status = -NFS4ERR_LEASE_MOVED;
7183 #endif /* CONFIG_NFS_V4_1 */
7186 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7187 * @inode: inode on FSID to check
7188 * @cred: credential to use for this operation
7190 * Server indicates whether the FSID is present, moved, or not
7191 * recognized. This operation is necessary to clear a LEASE_MOVED
7192 * condition for this client ID.
7194 * Returns NFS4_OK if the FSID is present on this server,
7195 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7196 * NFS4ERR code if some error occurred on the server, or a
7197 * negative errno if a local failure occurred.
7199 int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7201 struct nfs_server *server = NFS_SERVER(inode);
7202 struct nfs_client *clp = server->nfs_client;
7203 const struct nfs4_mig_recovery_ops *ops =
7204 clp->cl_mvops->mig_recovery_ops;
7205 struct nfs4_exception exception = { };
7208 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7209 (unsigned long long)server->fsid.major,
7210 (unsigned long long)server->fsid.minor,
7212 nfs_display_fhandle(NFS_FH(inode), __func__);
7215 status = ops->fsid_present(inode, cred);
7216 if (status != -NFS4ERR_DELAY)
7218 nfs4_handle_exception(server, status, &exception);
7219 } while (exception.retry);
7224 * If 'use_integrity' is true and the state managment nfs_client
7225 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7226 * and the machine credential as per RFC3530bis and RFC5661 Security
7227 * Considerations sections. Otherwise, just use the user cred with the
7228 * filesystem's rpc_client.
7230 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
7233 struct nfs4_secinfo_arg args = {
7234 .dir_fh = NFS_FH(dir),
7237 struct nfs4_secinfo_res res = {
7240 struct rpc_message msg = {
7241 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7245 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
7246 struct rpc_cred *cred = NULL;
7248 if (use_integrity) {
7249 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
7250 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7251 msg.rpc_cred = cred;
7254 dprintk("NFS call secinfo %s\n", name->name);
7256 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7257 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7259 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7261 dprintk("NFS reply secinfo: %d\n", status);
7269 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7270 struct nfs4_secinfo_flavors *flavors)
7272 struct nfs4_exception exception = { };
7275 err = -NFS4ERR_WRONGSEC;
7277 /* try to use integrity protection with machine cred */
7278 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7279 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7282 * if unable to use integrity protection, or SECINFO with
7283 * integrity protection returns NFS4ERR_WRONGSEC (which is
7284 * disallowed by spec, but exists in deployed servers) use
7285 * the current filesystem's rpc_client and the user cred.
7287 if (err == -NFS4ERR_WRONGSEC)
7288 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7290 trace_nfs4_secinfo(dir, name, err);
7291 err = nfs4_handle_exception(NFS_SERVER(dir), err,
7293 } while (exception.retry);
7297 #ifdef CONFIG_NFS_V4_1
7299 * Check the exchange flags returned by the server for invalid flags, having
7300 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7303 static int nfs4_check_cl_exchange_flags(u32 flags, u32 version)
7305 if (version >= 2 && (flags & ~EXCHGID4_2_FLAG_MASK_R))
7307 else if (version < 2 && (flags & ~EXCHGID4_FLAG_MASK_R))
7309 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7310 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7312 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7316 return -NFS4ERR_INVAL;
7320 nfs41_same_server_scope(struct nfs41_server_scope *a,
7321 struct nfs41_server_scope *b)
7323 if (a->server_scope_sz != b->server_scope_sz)
7325 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
7329 nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7333 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7334 .rpc_call_done = nfs4_bind_one_conn_to_session_done,
7338 * nfs4_proc_bind_one_conn_to_session()
7340 * The 4.1 client currently uses the same TCP connection for the
7341 * fore and backchannel.
7344 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7345 struct rpc_xprt *xprt,
7346 struct nfs_client *clp,
7347 struct rpc_cred *cred)
7350 struct nfs41_bind_conn_to_session_args args = {
7352 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7354 struct nfs41_bind_conn_to_session_res res;
7355 struct rpc_message msg = {
7357 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
7362 struct rpc_task_setup task_setup_data = {
7365 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
7366 .rpc_message = &msg,
7367 .flags = RPC_TASK_TIMEOUT,
7369 struct rpc_task *task;
7371 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7372 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7373 args.dir = NFS4_CDFC4_FORE;
7375 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7376 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7377 args.dir = NFS4_CDFC4_FORE;
7379 task = rpc_run_task(&task_setup_data);
7380 if (!IS_ERR(task)) {
7381 status = task->tk_status;
7384 status = PTR_ERR(task);
7385 trace_nfs4_bind_conn_to_session(clp, status);
7387 if (memcmp(res.sessionid.data,
7388 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7389 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7392 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
7393 dprintk("NFS: %s: Unexpected direction from server\n",
7397 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
7398 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7407 struct rpc_bind_conn_calldata {
7408 struct nfs_client *clp;
7409 struct rpc_cred *cred;
7413 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7414 struct rpc_xprt *xprt,
7417 struct rpc_bind_conn_calldata *p = calldata;
7419 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7422 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7424 struct rpc_bind_conn_calldata data = {
7428 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7429 nfs4_proc_bind_conn_to_session_callback, &data);
7433 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7434 * and operations we'd like to see to enable certain features in the allow map
7436 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7437 .how = SP4_MACH_CRED,
7438 .enforce.u.words = {
7439 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7440 1 << (OP_EXCHANGE_ID - 32) |
7441 1 << (OP_CREATE_SESSION - 32) |
7442 1 << (OP_DESTROY_SESSION - 32) |
7443 1 << (OP_DESTROY_CLIENTID - 32)
7446 [0] = 1 << (OP_CLOSE) |
7447 1 << (OP_OPEN_DOWNGRADE) |
7449 1 << (OP_DELEGRETURN) |
7451 [1] = 1 << (OP_SECINFO - 32) |
7452 1 << (OP_SECINFO_NO_NAME - 32) |
7453 1 << (OP_LAYOUTRETURN - 32) |
7454 1 << (OP_TEST_STATEID - 32) |
7455 1 << (OP_FREE_STATEID - 32) |
7456 1 << (OP_WRITE - 32)
7461 * Select the state protection mode for client `clp' given the server results
7462 * from exchange_id in `sp'.
7464 * Returns 0 on success, negative errno otherwise.
7466 static int nfs4_sp4_select_mode(struct nfs_client *clp,
7467 struct nfs41_state_protection *sp)
7469 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7470 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7471 1 << (OP_EXCHANGE_ID - 32) |
7472 1 << (OP_CREATE_SESSION - 32) |
7473 1 << (OP_DESTROY_SESSION - 32) |
7474 1 << (OP_DESTROY_CLIENTID - 32)
7476 unsigned long flags = 0;
7480 if (sp->how == SP4_MACH_CRED) {
7481 /* Print state protect result */
7482 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7483 for (i = 0; i <= LAST_NFS4_OP; i++) {
7484 if (test_bit(i, sp->enforce.u.longs))
7485 dfprintk(MOUNT, " enforce op %d\n", i);
7486 if (test_bit(i, sp->allow.u.longs))
7487 dfprintk(MOUNT, " allow op %d\n", i);
7490 /* make sure nothing is on enforce list that isn't supported */
7491 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7492 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7493 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7500 * Minimal mode - state operations are allowed to use machine
7501 * credential. Note this already happens by default, so the
7502 * client doesn't have to do anything more than the negotiation.
7504 * NOTE: we don't care if EXCHANGE_ID is in the list -
7505 * we're already using the machine cred for exchange_id
7506 * and will never use a different cred.
7508 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7509 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7510 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7511 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7512 dfprintk(MOUNT, "sp4_mach_cred:\n");
7513 dfprintk(MOUNT, " minimal mode enabled\n");
7514 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
7516 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7521 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
7522 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7523 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
7524 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7525 dfprintk(MOUNT, " cleanup mode enabled\n");
7526 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
7529 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7530 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7531 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
7534 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7535 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7536 dfprintk(MOUNT, " secinfo mode enabled\n");
7537 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
7540 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7541 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7542 dfprintk(MOUNT, " stateid mode enabled\n");
7543 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
7546 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7547 dfprintk(MOUNT, " write mode enabled\n");
7548 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
7551 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7552 dfprintk(MOUNT, " commit mode enabled\n");
7553 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
7557 clp->cl_sp4_flags = flags;
7561 struct nfs41_exchange_id_data {
7562 struct nfs41_exchange_id_res res;
7563 struct nfs41_exchange_id_args args;
7566 static void nfs4_exchange_id_release(void *data)
7568 struct nfs41_exchange_id_data *cdata =
7569 (struct nfs41_exchange_id_data *)data;
7571 nfs_put_client(cdata->args.client);
7572 kfree(cdata->res.impl_id);
7573 kfree(cdata->res.server_scope);
7574 kfree(cdata->res.server_owner);
7578 static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7579 .rpc_release = nfs4_exchange_id_release,
7583 * _nfs4_proc_exchange_id()
7585 * Wrapper for EXCHANGE_ID operation.
7587 static struct rpc_task *
7588 nfs4_run_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
7589 u32 sp4_how, struct rpc_xprt *xprt)
7591 struct rpc_message msg = {
7592 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
7595 struct rpc_task_setup task_setup_data = {
7596 .rpc_client = clp->cl_rpcclient,
7597 .callback_ops = &nfs4_exchange_id_call_ops,
7598 .rpc_message = &msg,
7599 .flags = RPC_TASK_TIMEOUT,
7601 struct nfs41_exchange_id_data *calldata;
7604 if (!atomic_inc_not_zero(&clp->cl_count))
7605 return ERR_PTR(-EIO);
7608 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7612 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
7614 status = nfs4_init_uniform_client_string(clp);
7618 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7621 if (unlikely(calldata->res.server_owner == NULL))
7624 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
7626 if (unlikely(calldata->res.server_scope == NULL))
7627 goto out_server_owner;
7629 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7630 if (unlikely(calldata->res.impl_id == NULL))
7631 goto out_server_scope;
7635 calldata->args.state_protect.how = SP4_NONE;
7639 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
7649 task_setup_data.rpc_xprt = xprt;
7650 task_setup_data.flags |= RPC_TASK_SOFTCONN;
7651 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
7652 sizeof(calldata->args.verifier.data));
7654 calldata->args.client = clp;
7655 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7656 EXCHGID4_FLAG_BIND_PRINC_STATEID;
7657 #ifdef CONFIG_NFS_V4_1_MIGRATION
7658 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
7660 msg.rpc_argp = &calldata->args;
7661 msg.rpc_resp = &calldata->res;
7662 task_setup_data.callback_data = calldata;
7664 return rpc_run_task(&task_setup_data);
7667 kfree(calldata->res.impl_id);
7669 kfree(calldata->res.server_scope);
7671 kfree(calldata->res.server_owner);
7675 nfs_put_client(clp);
7676 return ERR_PTR(status);
7680 * _nfs4_proc_exchange_id()
7682 * Wrapper for EXCHANGE_ID operation.
7684 static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
7687 struct rpc_task *task;
7688 struct nfs41_exchange_id_args *argp;
7689 struct nfs41_exchange_id_res *resp;
7692 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
7694 return PTR_ERR(task);
7696 argp = task->tk_msg.rpc_argp;
7697 resp = task->tk_msg.rpc_resp;
7698 status = task->tk_status;
7702 status = nfs4_check_cl_exchange_flags(resp->flags,
7703 clp->cl_mvops->minor_version);
7707 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
7711 clp->cl_clientid = resp->clientid;
7712 clp->cl_exchange_flags = resp->flags;
7713 clp->cl_seqid = resp->seqid;
7714 /* Client ID is not confirmed */
7715 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
7716 clear_bit(NFS4_SESSION_ESTABLISHED,
7717 &clp->cl_session->session_state);
7719 if (clp->cl_serverscope != NULL &&
7720 !nfs41_same_server_scope(clp->cl_serverscope,
7721 resp->server_scope)) {
7722 dprintk("%s: server_scope mismatch detected\n",
7724 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
7727 swap(clp->cl_serverowner, resp->server_owner);
7728 swap(clp->cl_serverscope, resp->server_scope);
7729 swap(clp->cl_implid, resp->impl_id);
7731 /* Save the EXCHANGE_ID verifier session trunk tests */
7732 memcpy(clp->cl_confirm.data, argp->verifier.data,
7733 sizeof(clp->cl_confirm.data));
7735 trace_nfs4_exchange_id(clp, status);
7741 * nfs4_proc_exchange_id()
7743 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7745 * Since the clientid has expired, all compounds using sessions
7746 * associated with the stale clientid will be returning
7747 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7748 * be in some phase of session reset.
7750 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7752 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7754 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7757 /* try SP4_MACH_CRED if krb5i/p */
7758 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7759 authflavor == RPC_AUTH_GSS_KRB5P) {
7760 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
7766 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
7770 * nfs4_test_session_trunk
7772 * This is an add_xprt_test() test function called from
7773 * rpc_clnt_setup_test_and_add_xprt.
7775 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7776 * and is dereferrenced in nfs4_exchange_id_release
7778 * Upon success, add the new transport to the rpc_clnt
7780 * @clnt: struct rpc_clnt to get new transport
7781 * @xprt: the rpc_xprt to test
7782 * @data: call data for _nfs4_proc_exchange_id.
7784 int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7787 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
7788 struct rpc_task *task;
7793 dprintk("--> %s try %s\n", __func__,
7794 xprt->address_strings[RPC_DISPLAY_ADDR]);
7796 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7798 /* Test connection for session trunking. Async exchange_id call */
7799 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7801 return PTR_ERR(task);
7803 status = task->tk_status;
7805 status = nfs4_detect_session_trunking(adata->clp,
7806 task->tk_msg.rpc_resp, xprt);
7811 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7813 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7814 struct rpc_cred *cred)
7816 struct rpc_message msg = {
7817 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7823 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7824 trace_nfs4_destroy_clientid(clp, status);
7826 dprintk("NFS: Got error %d from the server %s on "
7827 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7831 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7832 struct rpc_cred *cred)
7837 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7838 ret = _nfs4_proc_destroy_clientid(clp, cred);
7840 case -NFS4ERR_DELAY:
7841 case -NFS4ERR_CLIENTID_BUSY:
7851 int nfs4_destroy_clientid(struct nfs_client *clp)
7853 struct rpc_cred *cred;
7856 if (clp->cl_mvops->minor_version < 1)
7858 if (clp->cl_exchange_flags == 0)
7860 if (clp->cl_preserve_clid)
7862 cred = nfs4_get_clid_cred(clp);
7863 ret = nfs4_proc_destroy_clientid(clp, cred);
7868 case -NFS4ERR_STALE_CLIENTID:
7869 clp->cl_exchange_flags = 0;
7875 struct nfs4_get_lease_time_data {
7876 struct nfs4_get_lease_time_args *args;
7877 struct nfs4_get_lease_time_res *res;
7878 struct nfs_client *clp;
7881 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7884 struct nfs4_get_lease_time_data *data =
7885 (struct nfs4_get_lease_time_data *)calldata;
7887 dprintk("--> %s\n", __func__);
7888 /* just setup sequence, do not trigger session recovery
7889 since we're invoked within one */
7890 nfs4_setup_sequence(data->clp,
7891 &data->args->la_seq_args,
7892 &data->res->lr_seq_res,
7894 dprintk("<-- %s\n", __func__);
7898 * Called from nfs4_state_manager thread for session setup, so don't recover
7899 * from sequence operation or clientid errors.
7901 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7903 struct nfs4_get_lease_time_data *data =
7904 (struct nfs4_get_lease_time_data *)calldata;
7906 dprintk("--> %s\n", __func__);
7907 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7909 switch (task->tk_status) {
7910 case -NFS4ERR_DELAY:
7911 case -NFS4ERR_GRACE:
7912 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7913 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7914 task->tk_status = 0;
7916 case -NFS4ERR_RETRY_UNCACHED_REP:
7917 rpc_restart_call_prepare(task);
7920 dprintk("<-- %s\n", __func__);
7923 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
7924 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7925 .rpc_call_done = nfs4_get_lease_time_done,
7928 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7930 struct rpc_task *task;
7931 struct nfs4_get_lease_time_args args;
7932 struct nfs4_get_lease_time_res res = {
7933 .lr_fsinfo = fsinfo,
7935 struct nfs4_get_lease_time_data data = {
7940 struct rpc_message msg = {
7941 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7945 struct rpc_task_setup task_setup = {
7946 .rpc_client = clp->cl_rpcclient,
7947 .rpc_message = &msg,
7948 .callback_ops = &nfs4_get_lease_time_ops,
7949 .callback_data = &data,
7950 .flags = RPC_TASK_TIMEOUT,
7954 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
7955 nfs4_set_sequence_privileged(&args.la_seq_args);
7956 task = rpc_run_task(&task_setup);
7959 return PTR_ERR(task);
7961 status = task->tk_status;
7967 * Initialize the values to be used by the client in CREATE_SESSION
7968 * If nfs4_init_session set the fore channel request and response sizes,
7971 * Set the back channel max_resp_sz_cached to zero to force the client to
7972 * always set csa_cachethis to FALSE because the current implementation
7973 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7975 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7976 struct rpc_clnt *clnt)
7978 unsigned int max_rqst_sz, max_resp_sz;
7979 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
7981 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7982 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7984 /* Fore channel attributes */
7985 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7986 args->fc_attrs.max_resp_sz = max_resp_sz;
7987 args->fc_attrs.max_ops = NFS4_MAX_OPS;
7988 args->fc_attrs.max_reqs = max_session_slots;
7990 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
7991 "max_ops=%u max_reqs=%u\n",
7993 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
7994 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
7996 /* Back channel attributes */
7997 args->bc_attrs.max_rqst_sz = max_bc_payload;
7998 args->bc_attrs.max_resp_sz = max_bc_payload;
7999 args->bc_attrs.max_resp_sz_cached = 0;
8000 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
8001 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
8003 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8004 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8006 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8007 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8008 args->bc_attrs.max_reqs);
8011 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8012 struct nfs41_create_session_res *res)
8014 struct nfs4_channel_attrs *sent = &args->fc_attrs;
8015 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
8017 if (rcvd->max_resp_sz > sent->max_resp_sz)
8020 * Our requested max_ops is the minimum we need; we're not
8021 * prepared to break up compounds into smaller pieces than that.
8022 * So, no point even trying to continue if the server won't
8025 if (rcvd->max_ops < sent->max_ops)
8027 if (rcvd->max_reqs == 0)
8029 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8030 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
8034 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8035 struct nfs41_create_session_res *res)
8037 struct nfs4_channel_attrs *sent = &args->bc_attrs;
8038 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
8040 if (!(res->flags & SESSION4_BACK_CHAN))
8042 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8044 if (rcvd->max_resp_sz < sent->max_resp_sz)
8046 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8048 if (rcvd->max_ops > sent->max_ops)
8050 if (rcvd->max_reqs > sent->max_reqs)
8056 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
8057 struct nfs41_create_session_res *res)
8061 ret = nfs4_verify_fore_channel_attrs(args, res);
8064 return nfs4_verify_back_channel_attrs(args, res);
8067 static void nfs4_update_session(struct nfs4_session *session,
8068 struct nfs41_create_session_res *res)
8070 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
8071 /* Mark client id and session as being confirmed */
8072 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8073 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
8074 session->flags = res->flags;
8075 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
8076 if (res->flags & SESSION4_BACK_CHAN)
8077 memcpy(&session->bc_attrs, &res->bc_attrs,
8078 sizeof(session->bc_attrs));
8081 static int _nfs4_proc_create_session(struct nfs_client *clp,
8082 struct rpc_cred *cred)
8084 struct nfs4_session *session = clp->cl_session;
8085 struct nfs41_create_session_args args = {
8087 .clientid = clp->cl_clientid,
8088 .seqid = clp->cl_seqid,
8089 .cb_program = NFS4_CALLBACK,
8091 struct nfs41_create_session_res res;
8093 struct rpc_message msg = {
8094 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8101 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
8102 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
8104 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
8105 trace_nfs4_create_session(clp, status);
8108 case -NFS4ERR_STALE_CLIENTID:
8109 case -NFS4ERR_DELAY:
8118 /* Verify the session's negotiated channel_attrs values */
8119 status = nfs4_verify_channel_attrs(&args, &res);
8120 /* Increment the clientid slot sequence id */
8123 nfs4_update_session(session, &res);
8130 * Issues a CREATE_SESSION operation to the server.
8131 * It is the responsibility of the caller to verify the session is
8132 * expired before calling this routine.
8134 int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
8138 struct nfs4_session *session = clp->cl_session;
8140 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8142 status = _nfs4_proc_create_session(clp, cred);
8146 /* Init or reset the session slot tables */
8147 status = nfs4_setup_session_slot_tables(session);
8148 dprintk("slot table setup returned %d\n", status);
8152 ptr = (unsigned *)&session->sess_id.data[0];
8153 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8154 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
8156 dprintk("<-- %s\n", __func__);
8161 * Issue the over-the-wire RPC DESTROY_SESSION.
8162 * The caller must serialize access to this routine.
8164 int nfs4_proc_destroy_session(struct nfs4_session *session,
8165 struct rpc_cred *cred)
8167 struct rpc_message msg = {
8168 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8169 .rpc_argp = session,
8174 dprintk("--> nfs4_proc_destroy_session\n");
8176 /* session is still being setup */
8177 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8180 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
8181 trace_nfs4_destroy_session(session->clp, status);
8184 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
8185 "Session has been destroyed regardless...\n", status);
8187 dprintk("<-- nfs4_proc_destroy_session\n");
8192 * Renew the cl_session lease.
8194 struct nfs4_sequence_data {
8195 struct nfs_client *clp;
8196 struct nfs4_sequence_args args;
8197 struct nfs4_sequence_res res;
8200 static void nfs41_sequence_release(void *data)
8202 struct nfs4_sequence_data *calldata = data;
8203 struct nfs_client *clp = calldata->clp;
8205 if (atomic_read(&clp->cl_count) > 1)
8206 nfs4_schedule_state_renewal(clp);
8207 nfs_put_client(clp);
8211 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8213 switch(task->tk_status) {
8214 case -NFS4ERR_DELAY:
8215 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8218 nfs4_schedule_lease_recovery(clp);
8223 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
8225 struct nfs4_sequence_data *calldata = data;
8226 struct nfs_client *clp = calldata->clp;
8228 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8231 trace_nfs4_sequence(clp, task->tk_status);
8232 if (task->tk_status < 0) {
8233 dprintk("%s ERROR %d\n", __func__, task->tk_status);
8234 if (atomic_read(&clp->cl_count) == 1)
8237 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8238 rpc_restart_call_prepare(task);
8242 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
8244 dprintk("<-- %s\n", __func__);
8247 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8249 struct nfs4_sequence_data *calldata = data;
8250 struct nfs_client *clp = calldata->clp;
8251 struct nfs4_sequence_args *args;
8252 struct nfs4_sequence_res *res;
8254 args = task->tk_msg.rpc_argp;
8255 res = task->tk_msg.rpc_resp;
8257 nfs4_setup_sequence(clp, args, res, task);
8260 static const struct rpc_call_ops nfs41_sequence_ops = {
8261 .rpc_call_done = nfs41_sequence_call_done,
8262 .rpc_call_prepare = nfs41_sequence_prepare,
8263 .rpc_release = nfs41_sequence_release,
8266 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8267 struct rpc_cred *cred,
8268 struct nfs4_slot *slot,
8271 struct nfs4_sequence_data *calldata;
8272 struct rpc_message msg = {
8273 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8276 struct rpc_task_setup task_setup_data = {
8277 .rpc_client = clp->cl_rpcclient,
8278 .rpc_message = &msg,
8279 .callback_ops = &nfs41_sequence_ops,
8280 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
8282 struct rpc_task *ret;
8284 ret = ERR_PTR(-EIO);
8285 if (!atomic_inc_not_zero(&clp->cl_count))
8288 ret = ERR_PTR(-ENOMEM);
8289 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8290 if (calldata == NULL)
8292 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
8293 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
8295 nfs4_set_sequence_privileged(&calldata->args);
8296 msg.rpc_argp = &calldata->args;
8297 msg.rpc_resp = &calldata->res;
8298 calldata->clp = clp;
8299 task_setup_data.callback_data = calldata;
8301 ret = rpc_run_task(&task_setup_data);
8306 nfs_put_client(clp);
8308 nfs41_release_slot(slot);
8312 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
8314 struct rpc_task *task;
8317 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
8319 task = _nfs41_proc_sequence(clp, cred, NULL, false);
8321 ret = PTR_ERR(task);
8323 rpc_put_task_async(task);
8324 dprintk("<-- %s status=%d\n", __func__, ret);
8328 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8330 struct rpc_task *task;
8333 task = _nfs41_proc_sequence(clp, cred, NULL, true);
8335 ret = PTR_ERR(task);
8338 ret = rpc_wait_for_completion_task(task);
8340 ret = task->tk_status;
8343 dprintk("<-- %s status=%d\n", __func__, ret);
8347 struct nfs4_reclaim_complete_data {
8348 struct nfs_client *clp;
8349 struct nfs41_reclaim_complete_args arg;
8350 struct nfs41_reclaim_complete_res res;
8353 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8355 struct nfs4_reclaim_complete_data *calldata = data;
8357 nfs4_setup_sequence(calldata->clp,
8358 &calldata->arg.seq_args,
8359 &calldata->res.seq_res,
8363 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8365 switch(task->tk_status) {
8367 case -NFS4ERR_COMPLETE_ALREADY:
8368 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8370 case -NFS4ERR_DELAY:
8371 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8373 case -NFS4ERR_RETRY_UNCACHED_REP:
8375 case -NFS4ERR_BADSESSION:
8376 case -NFS4ERR_DEADSESSION:
8377 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8378 nfs4_schedule_session_recovery(clp->cl_session,
8382 nfs4_schedule_lease_recovery(clp);
8387 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8389 struct nfs4_reclaim_complete_data *calldata = data;
8390 struct nfs_client *clp = calldata->clp;
8391 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8393 dprintk("--> %s\n", __func__);
8394 if (!nfs41_sequence_done(task, res))
8397 trace_nfs4_reclaim_complete(clp, task->tk_status);
8398 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8399 rpc_restart_call_prepare(task);
8402 dprintk("<-- %s\n", __func__);
8405 static void nfs4_free_reclaim_complete_data(void *data)
8407 struct nfs4_reclaim_complete_data *calldata = data;
8412 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8413 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8414 .rpc_call_done = nfs4_reclaim_complete_done,
8415 .rpc_release = nfs4_free_reclaim_complete_data,
8419 * Issue a global reclaim complete.
8421 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8422 struct rpc_cred *cred)
8424 struct nfs4_reclaim_complete_data *calldata;
8425 struct rpc_task *task;
8426 struct rpc_message msg = {
8427 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
8430 struct rpc_task_setup task_setup_data = {
8431 .rpc_client = clp->cl_rpcclient,
8432 .rpc_message = &msg,
8433 .callback_ops = &nfs4_reclaim_complete_call_ops,
8434 .flags = RPC_TASK_ASYNC,
8436 int status = -ENOMEM;
8438 dprintk("--> %s\n", __func__);
8439 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8440 if (calldata == NULL)
8442 calldata->clp = clp;
8443 calldata->arg.one_fs = 0;
8445 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
8446 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
8447 msg.rpc_argp = &calldata->arg;
8448 msg.rpc_resp = &calldata->res;
8449 task_setup_data.callback_data = calldata;
8450 task = rpc_run_task(&task_setup_data);
8452 status = PTR_ERR(task);
8455 status = rpc_wait_for_completion_task(task);
8457 status = task->tk_status;
8460 dprintk("<-- %s status=%d\n", __func__, status);
8465 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8467 struct nfs4_layoutget *lgp = calldata;
8468 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
8470 dprintk("--> %s\n", __func__);
8471 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
8472 &lgp->res.seq_res, task);
8473 dprintk("<-- %s\n", __func__);
8476 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8478 struct nfs4_layoutget *lgp = calldata;
8480 dprintk("--> %s\n", __func__);
8481 nfs41_sequence_process(task, &lgp->res.seq_res);
8482 dprintk("<-- %s\n", __func__);
8486 nfs4_layoutget_handle_exception(struct rpc_task *task,
8487 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8489 struct inode *inode = lgp->args.inode;
8490 struct nfs_server *server = NFS_SERVER(inode);
8491 struct pnfs_layout_hdr *lo;
8492 int nfs4err = task->tk_status;
8493 int err, status = 0;
8496 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
8498 nfs4_sequence_free_slot(&lgp->res.seq_res);
8505 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8506 * on the file. set tk_status to -ENODATA to tell upper layer to
8509 case -NFS4ERR_LAYOUTUNAVAILABLE:
8513 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8514 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8516 case -NFS4ERR_BADLAYOUT:
8517 status = -EOVERFLOW;
8520 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
8521 * (or clients) writing to the same RAID stripe except when
8522 * the minlength argument is 0 (see RFC5661 section 18.43.3).
8524 * Treat it like we would RECALLCONFLICT -- we retry for a little
8525 * while, and then eventually give up.
8527 case -NFS4ERR_LAYOUTTRYLATER:
8528 if (lgp->args.minlength == 0) {
8529 status = -EOVERFLOW;
8534 case -NFS4ERR_RECALLCONFLICT:
8535 status = -ERECALLCONFLICT;
8537 case -NFS4ERR_DELEG_REVOKED:
8538 case -NFS4ERR_ADMIN_REVOKED:
8539 case -NFS4ERR_EXPIRED:
8540 case -NFS4ERR_BAD_STATEID:
8541 exception->timeout = 0;
8542 spin_lock(&inode->i_lock);
8543 lo = NFS_I(inode)->layout;
8544 /* If the open stateid was bad, then recover it. */
8545 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8546 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
8547 spin_unlock(&inode->i_lock);
8548 exception->state = lgp->args.ctx->state;
8549 exception->stateid = &lgp->args.stateid;
8554 * Mark the bad layout state as invalid, then retry
8556 pnfs_mark_layout_stateid_invalid(lo, &head);
8557 spin_unlock(&inode->i_lock);
8558 nfs_commit_inode(inode, 0);
8559 pnfs_free_lseg_list(&head);
8564 err = nfs4_handle_exception(server, nfs4err, exception);
8566 if (exception->retry)
8572 dprintk("<-- %s\n", __func__);
8576 static size_t max_response_pages(struct nfs_server *server)
8578 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8579 return nfs_page_array_len(0, max_resp_sz);
8582 static void nfs4_free_pages(struct page **pages, size_t size)
8589 for (i = 0; i < size; i++) {
8592 __free_page(pages[i]);
8597 static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8599 struct page **pages;
8602 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8604 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8608 for (i = 0; i < size; i++) {
8609 pages[i] = alloc_page(gfp_flags);
8611 dprintk("%s: failed to allocate page\n", __func__);
8612 nfs4_free_pages(pages, size);
8620 static void nfs4_layoutget_release(void *calldata)
8622 struct nfs4_layoutget *lgp = calldata;
8623 struct inode *inode = lgp->args.inode;
8624 struct nfs_server *server = NFS_SERVER(inode);
8625 size_t max_pages = max_response_pages(server);
8627 dprintk("--> %s\n", __func__);
8628 nfs4_sequence_free_slot(&lgp->res.seq_res);
8629 nfs4_free_pages(lgp->args.layout.pages, max_pages);
8630 pnfs_put_layout_hdr(NFS_I(inode)->layout);
8631 put_nfs_open_context(lgp->args.ctx);
8633 dprintk("<-- %s\n", __func__);
8636 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8637 .rpc_call_prepare = nfs4_layoutget_prepare,
8638 .rpc_call_done = nfs4_layoutget_done,
8639 .rpc_release = nfs4_layoutget_release,
8642 struct pnfs_layout_segment *
8643 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
8645 struct inode *inode = lgp->args.inode;
8646 struct nfs_server *server = NFS_SERVER(inode);
8647 size_t max_pages = max_response_pages(server);
8648 struct rpc_task *task;
8649 struct rpc_message msg = {
8650 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8651 .rpc_argp = &lgp->args,
8652 .rpc_resp = &lgp->res,
8653 .rpc_cred = lgp->cred,
8655 struct rpc_task_setup task_setup_data = {
8656 .rpc_client = server->client,
8657 .rpc_message = &msg,
8658 .callback_ops = &nfs4_layoutget_call_ops,
8659 .callback_data = lgp,
8660 .flags = RPC_TASK_ASYNC,
8662 struct pnfs_layout_segment *lseg = NULL;
8663 struct nfs4_exception exception = {
8665 .timeout = *timeout,
8669 dprintk("--> %s\n", __func__);
8671 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8672 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8674 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8675 if (!lgp->args.layout.pages) {
8676 nfs4_layoutget_release(lgp);
8677 return ERR_PTR(-ENOMEM);
8679 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8681 lgp->res.layoutp = &lgp->args.layout;
8682 lgp->res.seq_res.sr_slot = NULL;
8683 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
8685 task = rpc_run_task(&task_setup_data);
8687 return ERR_CAST(task);
8688 status = rpc_wait_for_completion_task(task);
8692 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8693 if (task->tk_status < 0 || lgp->res.layoutp->len == 0) {
8694 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8695 *timeout = exception.timeout;
8697 lseg = pnfs_layout_process(lgp);
8699 trace_nfs4_layoutget(lgp->args.ctx,
8706 dprintk("<-- %s status=%d\n", __func__, status);
8708 return ERR_PTR(status);
8713 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8715 struct nfs4_layoutreturn *lrp = calldata;
8717 dprintk("--> %s\n", __func__);
8718 nfs4_setup_sequence(lrp->clp,
8719 &lrp->args.seq_args,
8724 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8726 struct nfs4_layoutreturn *lrp = calldata;
8727 struct nfs_server *server;
8729 dprintk("--> %s\n", __func__);
8731 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
8734 server = NFS_SERVER(lrp->args.inode);
8735 switch (task->tk_status) {
8737 task->tk_status = 0;
8740 case -NFS4ERR_DELAY:
8741 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
8743 nfs4_sequence_free_slot(&lrp->res.seq_res);
8744 rpc_restart_call_prepare(task);
8747 dprintk("<-- %s\n", __func__);
8750 static void nfs4_layoutreturn_release(void *calldata)
8752 struct nfs4_layoutreturn *lrp = calldata;
8753 struct pnfs_layout_hdr *lo = lrp->args.layout;
8755 dprintk("--> %s\n", __func__);
8756 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
8757 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
8758 nfs4_sequence_free_slot(&lrp->res.seq_res);
8759 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
8760 lrp->ld_private.ops->free(&lrp->ld_private);
8761 pnfs_put_layout_hdr(lrp->args.layout);
8762 nfs_iput_and_deactive(lrp->inode);
8764 dprintk("<-- %s\n", __func__);
8767 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8768 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8769 .rpc_call_done = nfs4_layoutreturn_done,
8770 .rpc_release = nfs4_layoutreturn_release,
8773 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
8775 struct rpc_task *task;
8776 struct rpc_message msg = {
8777 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8778 .rpc_argp = &lrp->args,
8779 .rpc_resp = &lrp->res,
8780 .rpc_cred = lrp->cred,
8782 struct rpc_task_setup task_setup_data = {
8783 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
8784 .rpc_message = &msg,
8785 .callback_ops = &nfs4_layoutreturn_call_ops,
8786 .callback_data = lrp,
8790 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8791 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8792 &task_setup_data.rpc_client, &msg);
8794 dprintk("--> %s\n", __func__);
8796 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8798 nfs4_layoutreturn_release(lrp);
8801 task_setup_data.flags |= RPC_TASK_ASYNC;
8803 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
8804 task = rpc_run_task(&task_setup_data);
8806 return PTR_ERR(task);
8808 status = task->tk_status;
8809 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
8810 dprintk("<-- %s status=%d\n", __func__, status);
8816 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
8817 struct pnfs_device *pdev,
8818 struct rpc_cred *cred)
8820 struct nfs4_getdeviceinfo_args args = {
8822 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8823 NOTIFY_DEVICEID4_DELETE,
8825 struct nfs4_getdeviceinfo_res res = {
8828 struct rpc_message msg = {
8829 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8836 dprintk("--> %s\n", __func__);
8837 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
8838 if (res.notification & ~args.notify_types)
8839 dprintk("%s: unsupported notification\n", __func__);
8840 if (res.notification != args.notify_types)
8843 dprintk("<-- %s status=%d\n", __func__, status);
8848 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8849 struct pnfs_device *pdev,
8850 struct rpc_cred *cred)
8852 struct nfs4_exception exception = { };
8856 err = nfs4_handle_exception(server,
8857 _nfs4_proc_getdeviceinfo(server, pdev, cred),
8859 } while (exception.retry);
8862 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8864 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8866 struct nfs4_layoutcommit_data *data = calldata;
8867 struct nfs_server *server = NFS_SERVER(data->args.inode);
8869 nfs4_setup_sequence(server->nfs_client,
8870 &data->args.seq_args,
8876 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8878 struct nfs4_layoutcommit_data *data = calldata;
8879 struct nfs_server *server = NFS_SERVER(data->args.inode);
8881 if (!nfs41_sequence_done(task, &data->res.seq_res))
8884 switch (task->tk_status) { /* Just ignore these failures */
8885 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8886 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8887 case -NFS4ERR_BADLAYOUT: /* no layout */
8888 case -NFS4ERR_GRACE: /* loca_recalim always false */
8889 task->tk_status = 0;
8893 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
8894 rpc_restart_call_prepare(task);
8900 static void nfs4_layoutcommit_release(void *calldata)
8902 struct nfs4_layoutcommit_data *data = calldata;
8904 pnfs_cleanup_layoutcommit(data);
8905 nfs_post_op_update_inode_force_wcc(data->args.inode,
8907 put_rpccred(data->cred);
8908 nfs_iput_and_deactive(data->inode);
8912 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8913 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8914 .rpc_call_done = nfs4_layoutcommit_done,
8915 .rpc_release = nfs4_layoutcommit_release,
8919 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
8921 struct rpc_message msg = {
8922 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8923 .rpc_argp = &data->args,
8924 .rpc_resp = &data->res,
8925 .rpc_cred = data->cred,
8927 struct rpc_task_setup task_setup_data = {
8928 .task = &data->task,
8929 .rpc_client = NFS_CLIENT(data->args.inode),
8930 .rpc_message = &msg,
8931 .callback_ops = &nfs4_layoutcommit_ops,
8932 .callback_data = data,
8934 struct rpc_task *task;
8937 dprintk("NFS: initiating layoutcommit call. sync %d "
8938 "lbw: %llu inode %lu\n", sync,
8939 data->args.lastbytewritten,
8940 data->args.inode->i_ino);
8943 data->inode = nfs_igrab_and_active(data->args.inode);
8944 if (data->inode == NULL) {
8945 nfs4_layoutcommit_release(data);
8948 task_setup_data.flags = RPC_TASK_ASYNC;
8950 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
8951 task = rpc_run_task(&task_setup_data);
8953 return PTR_ERR(task);
8955 status = task->tk_status;
8956 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
8957 dprintk("%s: status %d\n", __func__, status);
8963 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8964 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8967 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8968 struct nfs_fsinfo *info,
8969 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
8971 struct nfs41_secinfo_no_name_args args = {
8972 .style = SECINFO_STYLE_CURRENT_FH,
8974 struct nfs4_secinfo_res res = {
8977 struct rpc_message msg = {
8978 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8982 struct rpc_clnt *clnt = server->client;
8983 struct rpc_cred *cred = NULL;
8986 if (use_integrity) {
8987 clnt = server->nfs_client->cl_rpcclient;
8988 cred = nfs4_get_clid_cred(server->nfs_client);
8989 msg.rpc_cred = cred;
8992 dprintk("--> %s\n", __func__);
8993 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8995 dprintk("<-- %s status=%d\n", __func__, status);
9004 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9005 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9007 struct nfs4_exception exception = { };
9010 /* first try using integrity protection */
9011 err = -NFS4ERR_WRONGSEC;
9013 /* try to use integrity protection with machine cred */
9014 if (_nfs4_is_integrity_protected(server->nfs_client))
9015 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9019 * if unable to use integrity protection, or SECINFO with
9020 * integrity protection returns NFS4ERR_WRONGSEC (which is
9021 * disallowed by spec, but exists in deployed servers) use
9022 * the current filesystem's rpc_client and the user cred.
9024 if (err == -NFS4ERR_WRONGSEC)
9025 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9030 case -NFS4ERR_WRONGSEC:
9034 err = nfs4_handle_exception(server, err, &exception);
9036 } while (exception.retry);
9042 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9043 struct nfs_fsinfo *info)
9047 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
9048 struct nfs4_secinfo_flavors *flavors;
9049 struct nfs4_secinfo4 *secinfo;
9052 page = alloc_page(GFP_KERNEL);
9058 flavors = page_address(page);
9059 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9062 * Fall back on "guess and check" method if
9063 * the server doesn't support SECINFO_NO_NAME
9065 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
9066 err = nfs4_find_root_sec(server, fhandle, info);
9072 for (i = 0; i < flavors->num_flavors; i++) {
9073 secinfo = &flavors->flavors[i];
9075 switch (secinfo->flavor) {
9079 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9080 &secinfo->flavor_info);
9083 flavor = RPC_AUTH_MAXFLAVOR;
9087 if (!nfs_auth_info_match(&server->auth_info, flavor))
9088 flavor = RPC_AUTH_MAXFLAVOR;
9090 if (flavor != RPC_AUTH_MAXFLAVOR) {
9091 err = nfs4_lookup_root_sec(server, fhandle,
9098 if (flavor == RPC_AUTH_MAXFLAVOR)
9109 static int _nfs41_test_stateid(struct nfs_server *server,
9110 nfs4_stateid *stateid,
9111 struct rpc_cred *cred)
9114 struct nfs41_test_stateid_args args = {
9117 struct nfs41_test_stateid_res res;
9118 struct rpc_message msg = {
9119 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9124 struct rpc_clnt *rpc_client = server->client;
9126 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9129 dprintk("NFS call test_stateid %p\n", stateid);
9130 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
9131 nfs4_set_sequence_privileged(&args.seq_args);
9132 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
9133 &args.seq_args, &res.seq_res);
9134 if (status != NFS_OK) {
9135 dprintk("NFS reply test_stateid: failed, %d\n", status);
9138 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
9142 static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9143 int err, struct nfs4_exception *exception)
9145 exception->retry = 0;
9147 case -NFS4ERR_DELAY:
9148 case -NFS4ERR_RETRY_UNCACHED_REP:
9149 nfs4_handle_exception(server, err, exception);
9151 case -NFS4ERR_BADSESSION:
9152 case -NFS4ERR_BADSLOT:
9153 case -NFS4ERR_BAD_HIGH_SLOT:
9154 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9155 case -NFS4ERR_DEADSESSION:
9156 nfs4_do_handle_exception(server, err, exception);
9161 * nfs41_test_stateid - perform a TEST_STATEID operation
9163 * @server: server / transport on which to perform the operation
9164 * @stateid: state ID to test
9167 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9168 * Otherwise a negative NFS4ERR value is returned if the operation
9169 * failed or the state ID is not currently valid.
9171 static int nfs41_test_stateid(struct nfs_server *server,
9172 nfs4_stateid *stateid,
9173 struct rpc_cred *cred)
9175 struct nfs4_exception exception = { };
9178 err = _nfs41_test_stateid(server, stateid, cred);
9179 nfs4_handle_delay_or_session_error(server, err, &exception);
9180 } while (exception.retry);
9184 struct nfs_free_stateid_data {
9185 struct nfs_server *server;
9186 struct nfs41_free_stateid_args args;
9187 struct nfs41_free_stateid_res res;
9190 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9192 struct nfs_free_stateid_data *data = calldata;
9193 nfs4_setup_sequence(data->server->nfs_client,
9194 &data->args.seq_args,
9199 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9201 struct nfs_free_stateid_data *data = calldata;
9203 nfs41_sequence_done(task, &data->res.seq_res);
9205 switch (task->tk_status) {
9206 case -NFS4ERR_DELAY:
9207 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
9208 rpc_restart_call_prepare(task);
9212 static void nfs41_free_stateid_release(void *calldata)
9217 static const struct rpc_call_ops nfs41_free_stateid_ops = {
9218 .rpc_call_prepare = nfs41_free_stateid_prepare,
9219 .rpc_call_done = nfs41_free_stateid_done,
9220 .rpc_release = nfs41_free_stateid_release,
9223 static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
9224 const nfs4_stateid *stateid,
9225 struct rpc_cred *cred,
9228 struct rpc_message msg = {
9229 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
9232 struct rpc_task_setup task_setup = {
9233 .rpc_client = server->client,
9234 .rpc_message = &msg,
9235 .callback_ops = &nfs41_free_stateid_ops,
9236 .flags = RPC_TASK_ASYNC,
9238 struct nfs_free_stateid_data *data;
9240 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9241 &task_setup.rpc_client, &msg);
9243 dprintk("NFS call free_stateid %p\n", stateid);
9244 data = kmalloc(sizeof(*data), GFP_NOFS);
9246 return ERR_PTR(-ENOMEM);
9247 data->server = server;
9248 nfs4_stateid_copy(&data->args.stateid, stateid);
9250 task_setup.callback_data = data;
9252 msg.rpc_argp = &data->args;
9253 msg.rpc_resp = &data->res;
9254 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
9256 nfs4_set_sequence_privileged(&data->args.seq_args);
9258 return rpc_run_task(&task_setup);
9262 * nfs41_free_stateid - perform a FREE_STATEID operation
9264 * @server: server / transport on which to perform the operation
9265 * @stateid: state ID to release
9267 * @is_recovery: set to true if this call needs to be privileged
9269 * Note: this function is always asynchronous.
9271 static int nfs41_free_stateid(struct nfs_server *server,
9272 const nfs4_stateid *stateid,
9273 struct rpc_cred *cred,
9276 struct rpc_task *task;
9278 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
9280 return PTR_ERR(task);
9286 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
9288 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
9290 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
9291 nfs4_free_lock_state(server, lsp);
9294 static bool nfs41_match_stateid(const nfs4_stateid *s1,
9295 const nfs4_stateid *s2)
9297 if (s1->type != s2->type)
9300 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
9303 if (s1->seqid == s2->seqid)
9306 return s1->seqid == 0 || s2->seqid == 0;
9309 #endif /* CONFIG_NFS_V4_1 */
9311 static bool nfs4_match_stateid(const nfs4_stateid *s1,
9312 const nfs4_stateid *s2)
9314 return nfs4_stateid_match(s1, s2);
9318 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
9319 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9320 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9321 .recover_open = nfs4_open_reclaim,
9322 .recover_lock = nfs4_lock_reclaim,
9323 .establish_clid = nfs4_init_clientid,
9324 .detect_trunking = nfs40_discover_server_trunking,
9327 #if defined(CONFIG_NFS_V4_1)
9328 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
9329 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9330 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9331 .recover_open = nfs4_open_reclaim,
9332 .recover_lock = nfs4_lock_reclaim,
9333 .establish_clid = nfs41_init_clientid,
9334 .reclaim_complete = nfs41_proc_reclaim_complete,
9335 .detect_trunking = nfs41_discover_server_trunking,
9337 #endif /* CONFIG_NFS_V4_1 */
9339 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
9340 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9341 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
9342 .recover_open = nfs40_open_expired,
9343 .recover_lock = nfs4_lock_expired,
9344 .establish_clid = nfs4_init_clientid,
9347 #if defined(CONFIG_NFS_V4_1)
9348 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
9349 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9350 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
9351 .recover_open = nfs41_open_expired,
9352 .recover_lock = nfs41_lock_expired,
9353 .establish_clid = nfs41_init_clientid,
9355 #endif /* CONFIG_NFS_V4_1 */
9357 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
9358 .sched_state_renewal = nfs4_proc_async_renew,
9359 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
9360 .renew_lease = nfs4_proc_renew,
9363 #if defined(CONFIG_NFS_V4_1)
9364 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
9365 .sched_state_renewal = nfs41_proc_async_sequence,
9366 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
9367 .renew_lease = nfs4_proc_sequence,
9371 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
9372 .get_locations = _nfs40_proc_get_locations,
9373 .fsid_present = _nfs40_proc_fsid_present,
9376 #if defined(CONFIG_NFS_V4_1)
9377 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
9378 .get_locations = _nfs41_proc_get_locations,
9379 .fsid_present = _nfs41_proc_fsid_present,
9381 #endif /* CONFIG_NFS_V4_1 */
9383 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9385 .init_caps = NFS_CAP_READDIRPLUS
9386 | NFS_CAP_ATOMIC_OPEN
9387 | NFS_CAP_POSIX_LOCK,
9388 .init_client = nfs40_init_client,
9389 .shutdown_client = nfs40_shutdown_client,
9390 .match_stateid = nfs4_match_stateid,
9391 .find_root_sec = nfs4_find_root_sec,
9392 .free_lock_state = nfs4_release_lockowner,
9393 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
9394 .alloc_seqid = nfs_alloc_seqid,
9395 .call_sync_ops = &nfs40_call_sync_ops,
9396 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9397 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9398 .state_renewal_ops = &nfs40_state_renewal_ops,
9399 .mig_recovery_ops = &nfs40_mig_recovery_ops,
9402 #if defined(CONFIG_NFS_V4_1)
9403 static struct nfs_seqid *
9404 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9409 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9411 .init_caps = NFS_CAP_READDIRPLUS
9412 | NFS_CAP_ATOMIC_OPEN
9413 | NFS_CAP_POSIX_LOCK
9414 | NFS_CAP_STATEID_NFSV41
9415 | NFS_CAP_ATOMIC_OPEN_V1,
9416 .init_client = nfs41_init_client,
9417 .shutdown_client = nfs41_shutdown_client,
9418 .match_stateid = nfs41_match_stateid,
9419 .find_root_sec = nfs41_find_root_sec,
9420 .free_lock_state = nfs41_free_lock_state,
9421 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
9422 .alloc_seqid = nfs_alloc_no_seqid,
9423 .session_trunk = nfs4_test_session_trunk,
9424 .call_sync_ops = &nfs41_call_sync_ops,
9425 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9426 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9427 .state_renewal_ops = &nfs41_state_renewal_ops,
9428 .mig_recovery_ops = &nfs41_mig_recovery_ops,
9432 #if defined(CONFIG_NFS_V4_2)
9433 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9435 .init_caps = NFS_CAP_READDIRPLUS
9436 | NFS_CAP_ATOMIC_OPEN
9437 | NFS_CAP_POSIX_LOCK
9438 | NFS_CAP_STATEID_NFSV41
9439 | NFS_CAP_ATOMIC_OPEN_V1
9442 | NFS_CAP_DEALLOCATE
9444 | NFS_CAP_LAYOUTSTATS
9446 .init_client = nfs41_init_client,
9447 .shutdown_client = nfs41_shutdown_client,
9448 .match_stateid = nfs41_match_stateid,
9449 .find_root_sec = nfs41_find_root_sec,
9450 .free_lock_state = nfs41_free_lock_state,
9451 .call_sync_ops = &nfs41_call_sync_ops,
9452 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
9453 .alloc_seqid = nfs_alloc_no_seqid,
9454 .session_trunk = nfs4_test_session_trunk,
9455 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9456 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9457 .state_renewal_ops = &nfs41_state_renewal_ops,
9458 .mig_recovery_ops = &nfs41_mig_recovery_ops,
9462 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9463 [0] = &nfs_v4_0_minor_ops,
9464 #if defined(CONFIG_NFS_V4_1)
9465 [1] = &nfs_v4_1_minor_ops,
9467 #if defined(CONFIG_NFS_V4_2)
9468 [2] = &nfs_v4_2_minor_ops,
9472 static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
9474 ssize_t error, error2;
9476 error = generic_listxattr(dentry, list, size);
9484 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9487 return error + error2;
9490 static const struct inode_operations nfs4_dir_inode_operations = {
9491 .create = nfs_create,
9492 .lookup = nfs_lookup,
9493 .atomic_open = nfs_atomic_open,
9495 .unlink = nfs_unlink,
9496 .symlink = nfs_symlink,
9500 .rename = nfs_rename,
9501 .permission = nfs_permission,
9502 .getattr = nfs_getattr,
9503 .setattr = nfs_setattr,
9504 .listxattr = nfs4_listxattr,
9507 static const struct inode_operations nfs4_file_inode_operations = {
9508 .permission = nfs_permission,
9509 .getattr = nfs_getattr,
9510 .setattr = nfs_setattr,
9511 .listxattr = nfs4_listxattr,
9514 const struct nfs_rpc_ops nfs_v4_clientops = {
9515 .version = 4, /* protocol version */
9516 .dentry_ops = &nfs4_dentry_operations,
9517 .dir_inode_ops = &nfs4_dir_inode_operations,
9518 .file_inode_ops = &nfs4_file_inode_operations,
9519 .file_ops = &nfs4_file_operations,
9520 .getroot = nfs4_proc_get_root,
9521 .submount = nfs4_submount,
9522 .try_mount = nfs4_try_mount,
9523 .getattr = nfs4_proc_getattr,
9524 .setattr = nfs4_proc_setattr,
9525 .lookup = nfs4_proc_lookup,
9526 .lookupp = nfs4_proc_lookupp,
9527 .access = nfs4_proc_access,
9528 .readlink = nfs4_proc_readlink,
9529 .create = nfs4_proc_create,
9530 .remove = nfs4_proc_remove,
9531 .unlink_setup = nfs4_proc_unlink_setup,
9532 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
9533 .unlink_done = nfs4_proc_unlink_done,
9534 .rename_setup = nfs4_proc_rename_setup,
9535 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
9536 .rename_done = nfs4_proc_rename_done,
9537 .link = nfs4_proc_link,
9538 .symlink = nfs4_proc_symlink,
9539 .mkdir = nfs4_proc_mkdir,
9540 .rmdir = nfs4_proc_remove,
9541 .readdir = nfs4_proc_readdir,
9542 .mknod = nfs4_proc_mknod,
9543 .statfs = nfs4_proc_statfs,
9544 .fsinfo = nfs4_proc_fsinfo,
9545 .pathconf = nfs4_proc_pathconf,
9546 .set_capabilities = nfs4_server_capabilities,
9547 .decode_dirent = nfs4_decode_dirent,
9548 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
9549 .read_setup = nfs4_proc_read_setup,
9550 .read_done = nfs4_read_done,
9551 .write_setup = nfs4_proc_write_setup,
9552 .write_done = nfs4_write_done,
9553 .commit_setup = nfs4_proc_commit_setup,
9554 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
9555 .commit_done = nfs4_commit_done,
9556 .lock = nfs4_proc_lock,
9557 .clear_acl_cache = nfs4_zap_acl_attr,
9558 .close_context = nfs4_close_context,
9559 .open_context = nfs4_atomic_open,
9560 .have_delegation = nfs4_have_delegation,
9561 .return_delegation = nfs4_inode_return_delegation,
9562 .alloc_client = nfs4_alloc_client,
9563 .init_client = nfs4_init_client,
9564 .free_client = nfs4_free_client,
9565 .create_server = nfs4_create_server,
9566 .clone_server = nfs_clone_server,
9569 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
9570 .name = XATTR_NAME_NFSV4_ACL,
9571 .list = nfs4_xattr_list_nfs4_acl,
9572 .get = nfs4_xattr_get_nfs4_acl,
9573 .set = nfs4_xattr_set_nfs4_acl,
9576 const struct xattr_handler *nfs4_xattr_handlers[] = {
9577 &nfs4_xattr_nfs4_acl_handler,
9578 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
9579 &nfs4_xattr_nfs4_label_handler,